3

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()
Carmin
  • 41
  • 3
  • 6
    You're missing parenthesis after ``lower`` (i.e. ``lower()``), so you're essentially comparing the ``lower`` method to the string ``'clear'``, and since they're not equal the statement is never true. – Dan Gittik Dec 24 '17 at 16:15
  • Thanks. I just fixed it and tried the code again. The program saves the keyword but still doesn't want to delete it after I tell it to. – Carmin Dec 24 '17 at 16:31
  • `mcbShelf.pop` is a function. Change this line to `mcbShelf.pop(sys.argv[2])` – MegaIng Dec 24 '17 at 16:48
  • Thanks a lot for the help. I found my problem. It had nothing to do with the code...I forgot I made a copy of mcb.pyw(before I added 'delete' function) and moved it to another folder for easier access. So I was making changes to the main file but opening its copy..so naturally, the program didn't work. – Carmin Dec 24 '17 at 20:31

3 Answers3

2

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()
FrancesSun
  • 33
  • 1
  • 12
1

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])
Carter ash
  • 67
  • 8
1

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()
Slim Shady
  • 220
  • 3
  • 18