-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnestes_dict.py
51 lines (40 loc) · 1.71 KB
/
nestes_dict.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
48
49
50
51
def find(key, dictionary):
for k, v in dictionary.items():
if k == key:
yield v
elif isinstance(v, dict):
for result in find(key, v):
yield result
elif isinstance(v, list):
for d in v:
for result in find(key, d):
yield result
example = {'app_url': '', 'models': [{'perms': {'add': True, 'change': True, 'delete': True}, 'add_url': '/admin/cms/news/add/', 'admin_url': '/admin/cms/news/', 'name': ''}], 'has_module_perms': True, 'name': u'CMS'}
list(find('admin_url', example))
# Find all occurences of a key in nested python dictionaries and lists - http://stackoverflow.com/questions/9807634/find-all-occurences-of-a-key-in-nested-python-dictionaries-and-lists
# This is helpful when importing deeply nested JSON file and need to access some of the inner nests
'''
def find4(keys, dictionary):
...: for key in keys:
...:
...: for k, v in dictionary.items():
...: if k == key:
...: yield v
...: elif isinstance(v, dict):
...: for result in find3(key, v):
...: yield result
...: elif isinstance(v, list):
...: for d in v:
...: for result in find3(key, d):
...: yield result
list(find4(keys, d))
Programming routines
Python
import datetime
def date_from_webkit(webkit_timestamp):
epoch_start = datetime.datetime(1601,1,1)
delta = datetime.timedelta(microseconds=int(webkit_timestamp))
print epoch_start + delta
inTime = int(raw_input('Enter a Webkit timestamp to convert:'))
date_from_webkit(inTime)
'''