-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary_json_example.py
More file actions
44 lines (33 loc) · 1.25 KB
/
dictionary_json_example.py
File metadata and controls
44 lines (33 loc) · 1.25 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
import json
import os
from collections import defaultdict
def my_database(k: str, d: dict) -> dict:
""" Class example to create and maintain a persistent database
using JSON and dictionaries
Recebe como parâmetros, uma chave (nome, por exemplo) e um dicionário]
com as informações acessarem acrescentadas.
Salva num JSON.
"""
# Checking if database exists
if os.path.exists('my_database.json'):
with open('my_database.json', 'r') as f:
data = json.load(f)
# If not saved before, create dictionary for the first time
else:
# Prepara o dicionário para receber outros dicionários como valores
data = defaultdict(dict)
# Adding data to the dictionary
data.update({k: d})
# Saving it in file
with open('my_database.json', 'w') as f:
json.dump(data, f)
print('Current dictionary is: \n{}'.format(data))
return data
if __name__ == '__main__':
# JSON --> use code -> reformat file
key = 'Bianca'
new_dict_value = {'age': 36, 'cargo': 'advogado', 'salario': 1680}
d = my_database(key, new_dict_value)
key = 'Fredegonda'
value = {'local': 'Fargo, Montana', 'cargo': 'nem-nem', 'salario': 15}
d = my_database(key, value)