-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_value_pair_storage.py
47 lines (36 loc) · 1.45 KB
/
key_value_pair_storage.py
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
import argparse
import json
import os
import tempfile
parser = argparse.ArgumentParser(description='Store key-value pair and retrieve it')
parser.add_argument('--key', nargs='?',
help='the key of key-value pair to be stored')
parser.add_argument('--value', nargs='+',
help='the value of key-value pair to be stored')
args = parser.parse_args()
key_pair_object = vars(args)
with open("test.txt", "r") as f:
content = f.read()
try:
if not content:
storage = {}
else:
storage = json.loads(content)
# if value exists within incoming parameters then need
# to write such value by given key
if key_pair_object['value']:
if key_pair_object['key']:
if not key_pair_object['key'] in storage:
storage[key_pair_object['key']] = []
for i in key_pair_object['value']:
storage[key_pair_object['key']].append(i)
with open("test.txt", "w") as f_write:
f_write.write(json.dumps(storage))
print("new value added")
else:
if key_pair_object['key'] in storage:
[print(x) for x in storage[key_pair_object['key']]]
else:
print("no values found for the key: ", key_pair_object['key'])
except ValueError as e:
print("Error occurs", e)