Shelve
You can save variables to binary shelve files.
import shelve, os
DIR = os.path.dirname(os.path.realpath(__file__))
FILE = DIR + "/data/vars.shelve"
SH = shelve.open(FILE)
answers = ['a', 'b', 'c']
SH['answers'] = answers
SH.close()
SH = shelve.open(FILE)
answers = list(SH['answers'])
answers.append('d')
SH['answers'] = answers
SH.close()
SH = shelve.open(FILE)
answers = SH['answers']
print(answers[3])
SH.close()
assert answers[3] == 'd'
assert answers[:3] == ['a', 'b', 'c']
Dictionaries
import shelve, sys, os
DIR = os.path.dirname(os.path.realpath(__file__))
FILE = DIR + "/data/dict.shelve"
db = shelve.open(FILE)
dict = db['spanish'] if 'spanish' in db.keys() else {}
if len(sys.argv) == 3:
if sys.argv[1] == 'add':
words = sys.argv[2].split(':')
dict[words[0]] = words[1]
db['spanish'] = dict
print(db['spanish'])
db.close()
Multi Clipboard (A)
You can save new strings in clipboard, with no code modificiations.
import shelve, pyperclip, sys, os
DIR = os.path.dirname(os.path.realpath(__file__))
FILE = DIR + "/data/clipboard.shelve"
with shelve.open(FILE) as db:
dict = db['keywords'] if 'keywords' in db.keys() else {}
action = sys.argv[1] if len(sys.argv) == 3 else ''
keyword = sys.argv[2] if len(sys.argv) == 3 else ''
if action.lower() == 'load':
text = dict[sys.argv[2]]
pyperclip.copy(text)
print("Text in clipboard: " + text)
if action.lower() == 'save':
keyword = sys.argv[2]
dict[keyword] = pyperclip.paste()
db['keywords'] = dict
print(db['keywords'])