Carmin
Carmin

Reputation: 41

Can't delete data from shelve (Automate the boring stuff book)

I am doing practice project from Chapter 8 of "Automate the boring stuff with Python". I need to write commands that will delete a keyword from shelf and delete the whole database of keywords. I don't know what to try more. None of my attempts to delete anything seem to work. Would appreciate any help. Thank you

#!/usr/bin/env python3
# mcb.pyw - Multiclipboard
# A program that can save and load pieces of text to the clipboard.
# Usage: python3 mcb.pyw save <keyword> - Saves clipboard to keyword.
#        python3 mcb.pyw <keyword> - Load keyword to clipboard.
#        python3 mcb.pyw list - Load all keywords to clipboard.

import shelve, xerox, sys

mcbShelf = shelve.open('mcb')

if len(sys.argv) == 3 and sys.argv[1].lower()== 'save':
    mcbShelf[sys.argv[2]] = xerox.paste()
elif len(sys.argv) == 2 and sys.argv[1].lower== 'clear':
    for i in list(mcbShelf.keys()):
        del mcbShelf[i]
elif len(sys.argv) == 2: 
    if sys.argv[1].lower() == 'list':
        xerox.copy(str(list(mcbShelf.keys())))
    elif sys.argv[1] in mcbShelf:
        xerox.copy(mcbShelf[sys.argv[1]])

#Extend the multiclipboard program in this so that it has a delet #<keyword> command line argument that will delete a keyword from the shelf.
elif len(sys.argv) == 3 and sys.argv[1].lower()== 'delete' and sys.argv[2].lower() in mcbShelf.keys():
    mcbShelf.pop[sys.argv[2]]

mcbShelf.close()

Upvotes: 3

Views: 1724

Answers (3)

Slim Shady
Slim Shady

Reputation: 220

Alternatively, you can write mcbShelf.clear(). This is my full code:

#Extending the Multiclipboard
import shelve, pyperclip, sys
mcbShelf = shelve.open('mcb')
if len(sys.argv) == 3:                             # If 3 arguments in the command line -> save or delete the 3rd agrument
     if sys.argv[1].lower() == 'save':   
         mcbShelf[sys.argv[2]] = pyperclip.paste()
     elif sys.argv[1].lower()=='delete':
        del mcbShelf[sys.argv[2]]         
        
elif len(sys.argv) == 2:                           # If 2 argument in the command line -> copy list arguments to clipboard or
 # List keywords and load content.                  # delete all list arguments
     if sys.argv[1].lower() == 'list':
          pyperclip.copy(str(list(mcbShelf.keys())))
            
     elif sys.argv[1].lower()=='delete':
        mcbShelf.clear()
        
     elif sys.argv[1] in mcbShelf:
          pyperclip.copy(mcbShelf[sys.argv[1]])
mcbShelf.close()

Upvotes: 1

FrancesSun
FrancesSun

Reputation: 33

This is my solution to the project:

#! python3
# mcb.pyw - Saves and loads pieces of text to the clipboard.

""" Usage
    py.exe mcb.pyw save <keyword> - Saves clipboard to keyword.
    py.exe mcb.pyw <keyword> - Load keyword to clipboard.
    py.exe mcb.pyw list - Loads all keywords to clipboard.
    py.exe mcb.pyw delete <keyword> - Delete keyword from shelf
    py.exe mcb.pyw delete - Delete all keywords from shelf """


import sys, pyperclip, shelve

# Open new shelf file and save it inside a variable
mcb_shelf = shelve.open('mcb')

# Saves copied text in clipboard to keyword provided in argument to mcb_shelf
if len(sys.argv) == 3 and sys.argv[1].lower() == 'save':
    mcb_shelf[sys.argv[2]] = pyperclip.paste()

# Deletes keyword in argument if it exists in mcb_shelf
elif len(sys.argv) == 3 and sys.argv[1].lower() == 'delete':
    if sys.argv[2] in mcb_shelf:
        del mcb_shelf[sys.argv[2]]

# Checks if argument's length is 2
elif len(sys.argv) == 2:
    # Lists keywords if argument passed is list
    if sys.argv[1].lower() == 'list':
        pyperclip.copy(str(list(mcb_shelf.keys())))

    # Copies values in keyword passed in argument if it exists to the clipboard
    elif sys.argv[1] in mcb_shelf:
        pyperclip.copy(mcb_shelf[sys.argv[1]])

    # For loop that iterates through every keyword in mcb_shelf and deletes
    elif sys.argv[1] == 'delete':
        for keywords in list(mcb_shelf.keys()):
            del mcb_shelf[keywords]

mcb_shelf.close()

Upvotes: 2

Carter ash
Carter ash

Reputation: 67

I just tried you code (because I am doing the same exersice) and instead of adding a second 'and' arg to the the elfi statement. try to make it into a new if statement nested with in.

elif len(sys.argv) == 3 and sys.argv[1].lower()== 'delete':
    if sys.argv[2] in mcbShelf:
        mcbShelf.pop(sys.argv[2])

Upvotes: 1

Related Questions