[Tutor] Deleting items from a shelve file when items are unknown

Cameron Simpson cs at cskk.id.au
Sun Oct 20 18:51:05 EDT 2019


On 20Oct2019 19:19, James Austin <james.londonsw15 at gmail.com> wrote:
>Thanks for the advice. I'll try as you suggest and report back here 
>either way, espeically if I have more questions regarding your 
>suggestions.

Great. Remember to do it by replying to a post in this thread.

[..]
>>>  if clipboardShelf[sys.argv[2]] in clipboardShelf.keys():
>>>   clipboardShelf.pop()
>>
>>A Shelf is a mutable mapping of keys to values. So
>>clipboardShelf[sys.argv[2]] is a clipboard value.  That will not be 
>>in the keys. You probably mean:
>>
>>   if sys.argv[2] in clipboardShelf.keys():
>>
>>Then you go: clipboardShelf.pop(). I imagine your intent here it to 
>>remove the clipboard entry with key sys.argv[2], but you do not tell 
>>the pop method what key to remove.
>
>How is thsi to be achieved? The key could be anything, is there some 
>way of informing pop of this?

Well, isn't sys.argv[2] the key in question? You could just del that 
key:

    del clipboardShelf[sys.argv[2]]

BTW, since clipboardShelf is a mapping, you can replace:

    if sys.argv[2] in clipboardShelf.keys():

with:

    if sys.argv[2] in clipboardShelf:

More readable again.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list