-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaries.py
More file actions
104 lines (89 loc) · 2.51 KB
/
Copy pathDictionaries.py
File metadata and controls
104 lines (89 loc) · 2.51 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# import matplotlib.pyplot as plt
import json
from nltk.data import retrieve
from time import time, ctime
from os import SEEK_END
def Histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
name = 'Herr Albert Einstein is a Genius'
H = Histogram(name)
print(H)
print([x for x in H.keys()])
print([x for x in H.values()])
x_dict ={'a':1, 'b':2}
print('before update:', x_dict)
x_dict.update({'c':3})
print('after update:', x_dict)
# plt.bar(range(len(H)), H.values(), align='center')
# plt.xticks(range(len(H)), H.keys())
# plt.show()
data = dict()
data['David'] = 500000
data['Sam'] = 20000
data['Honey'] = '7000'
data = json.dumps(data) # convert it into a JSON string to be turned into binary later on
print("data after being json-dumped: %s" %data)
with open('data.star', 'wb') as file:
file.write(bytes(data, 'utf8'))
file.close()
with open('data.star') as file:
retrieve = file.read()
file.close()
print(retrieve)
d = {} # or dict()
for pair in retrieve.split(","):
i = pair.split(":")
d[i[0]] = i[1]
print(d)
# truncate the last letter from file
with open('data.star', 'rb+') as filehandle:
filehandle.seek(-1, SEEK_END)
filehandle.truncate()
adata = dict()
adata['Alien'] = 101
adata['Big'] = 3000
adata = json.dumps(adata)
adata = adata[1:-1]
# append to file
with open('data.star', 'ab+') as file:
file.write(bytes(", ", 'ascii') + bytes(adata, 'ascii') + bytes("}", 'ascii'))
userlog = {
'level_1a': 1,
'level_1b': {
'level_2a': 2,
'level_2b': {
'level_3a': 'genius',
'IQ_level': {'David': 80000, 'Jane': 80, 'John': 137137 }
}
},
'level_1c': {
'level_2a': 2,
'level_2b': {
'level_3a': 'genius',
'IQ_level': {'David': 80000, 'Jane': 80, 'John': 137137 }
}
}
}
for a, b in userlog.items():
print('a: %s' %a)
print('b: %s' %b)
# recursive method
def getpath(nested_dict, value, prepath=()):
for k, v in nested_dict.items():
path = prepath + (k,)
if v == value: # found the value!
return path
elif hasattr(v, 'items'): # see if v is a dick
p = getpath(v, value, path) # recursive call
if p is not None:
return p # stop the recursive-loop if the value matched, else continue the for-loop
start = time()
print("Path: %s" %[x for x in getpath(userlog, 'genius')])
duration = time() - start
print("Recursive algo required %ss" %duration)