-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw_2_1.py
More file actions
57 lines (40 loc) · 1.09 KB
/
w_2_1.py
File metadata and controls
57 lines (40 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import argparse
import json
import os
import tempfile
storage_path = os.path.join(tempfile.gettempdir(), 'storage.data')
def clear():
os.remove(storage_path)
def get_data():
if not os.path.exists(storage_path):
return {}
with open(storage_path, 'r') as f:
raw_data = f.read()
if raw_data:
return json.loads(raw_data)
return {}
def put(key, value):
data = get_data()
if key in data:
data[key].append(value)
else:
data[key] = [value]
with open(storage_path, 'w') as f:
f.write(json.dumps(data))
def get(key):
data = get_data()
return data.get(key)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--key', help='Key')
parser.add_argument('--val', help='Value')
parser.add_argument('--clear', action='store_true', help='Clear')
args = parser.parse_args()
if args.clear:
clear()
elif args.key and args.val:
put(args.key, args.val)
elif args.key:
print(get(args.key))
else:
print('Wrong command')