我正在从“使用Python自动化无聊的东西”的第8章开始练习项目。我需要编写将从架子中删除关键字的命令,并删除整个关键字数据库。我不知道该怎么做。我试图删除任何东西似乎都没有效果。非常感谢任何帮助。谢谢
#!/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()
答案 0 :(得分:1)
或者,您可以编写mcbShelf.clear()
。这是我的完整代码:
#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()
答案 1 :(得分:0)
我只是尝试了你的代码(因为我正在做同样的练习)而不是在elfi语句中添加第二个'和'arg。尝试将它变成一个嵌入in。
的新if语句elif len(sys.argv) == 3 and sys.argv[1].lower()== 'delete':
if sys.argv[2] in mcbShelf:
mcbShelf.pop(sys.argv[2])
答案 2 :(得分:0)
这是我对项目的解决方案:
WHATEVER_THESE_ARE = [
:new_app, :updates_approved, :updates_disapproved,
:new_partial_app, :completed_app, :received_lead
].freeze
BG_CHECK_DONE = [
:bg_check_received, :bg_check_failed, :bg_check_completed
].freeze
...
# You'll want to put this somewhere else.
def flatten_keys(hash)
hash.each_with_object({}) { |(keys, value), new_hash|
keys.each { |key| new_hash[event] = value }
}
end
EVENT_TO_APPLICATION_PATH_METHOD = flatten_keys({
WHATEVER_THESE_ARE => :company_dot_application_path,
BG_CHECK_DONE => :company_dot_application_background_checks_path,
...
}).freeze
def notification_path(h, object)
return h.company_trial_activation_index_path if User.current.company.trial_pending?
method = EVENT_TO_DOT_APPLICATION_PATH_METHOD[object.event.to_sym] || :home_users_path
h.send(method, dot)
end