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

James Austin james.londonsw15 at gmail.com
Sun Oct 20 14:19:54 EDT 2019


Hi Cameron


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.



On 19/10/2019 23:09, Cameron Simpson wrote:
> Personally I tend to pop things off sys.argv progressively so that 
> they can get nice names:
>
>    cmd = sys.argv.pop(0)   # command name
>    op = sys.argv.pop(0)    # operation
>    if op == 'delete':
>        key = sys.argv.pop(0)
>
> This has the advantage of not hardwiring argument positions into your 
> code. (Suppose you latter add some option parsing ahead of the 
> 'delete' operation word?) It also mades the code more readable ("key" 
> instead of "sys.argv[2]").
>
I did not know this was possible, so thank you. I am always looking to 
improve and have readaable code.
> Anyway, to the code again:
>
>>   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?


Thanks

James

>
>
>
>


More information about the Tutor mailing list