{
"name": "John"
}
import json
import atexit
class JSONController:
def __init__(self, filename):
self.handler = open(filename, 'r+')
atexit.register(self.close)
def load(self):
text = self.handler.read()
return json.loads(text)
def save(self, data):
text = json.dumps(data)
self.handler.seek(0)
self.handler.write(text)
def close(self):
self.handler.close()
json_controller = JSONController('./data.json')
data = json_controller.load()
data['age'] = 30
print(data) # {'name': 'John', 'age': 30}
json_controller.save(data)
def function1(string):
print(string+'\nfunction1')
def function2(string):
print(string+'\nfunction2')
def function3(string):
print(string+'\nfunction3')
functions = {
'function1': ['1','function1'],
'function2': ['2','function2'],
'function3': ['3','function3']
}
call_func = input()
for func, keys in functions.items():
if call_func in keys:
eval(f'{func}(call_func)')
break
handlers = {
'ADD': lambda x: print('ADD with %s' % x),
'DELETE': lambda x: print('DELETE with %s' % x),
'INSERT': lambda x: print('INSERT with %s' % x)
}
for _ in range(int(input())):
handler_name, arg = input().split()
handler = handlers.get(
handler_name, lambda _: print("%s wasn't found" % handler_name)
)
handler(arg)
% cat input.txt
4
DELETE 10
INSERT 2
ADD 15
SELECT 7
% python3 ans.py < input.txt
DELETE with 10
INSERT with 2
ADD with 15
SELECT wasn't found