Go to the documentation of this file.
5 def __matches(regex, textlist):
6 """Returns subset of input textlist which matches regex."""
7 return [item
for item
in textlist
if re.match(regex, item)]
12 Assign value of a single-item inner dictionary to its outer key.
13 (cut out the middleman)
15 mydict = {'OUTER': {'INNER': val}}
16 promote_value(mydict, 'IN.*')
17 mydict => {'OUTER': val}
19 for k, v
in list(mydict.items()):
20 if isinstance(v, dict):
22 if len(
list(v.keys())) == 1
and re.match(keyregex,
list(v.keys())[0]):
23 mydict[k] = v[
list(v.keys())[0]]
28 A single-item inner dictionary takes the place of its outer key.
31 mydict = {'OUTER': {'INNER': val}}
32 promote_dict(mydict, 'IN.*')
33 mydict => {'INNER': val}
35 for k, v
in list(mydict.items()):
36 if isinstance(v, dict):
38 if len(
list(v.keys())) == 1
and re.match(keyregex,
list(v.keys())[0]):
39 key =
list(v.keys())[0]
47 All keys are promoted, as long as a key of the same name does not exist at the upper level.
49 mydict = {'OUTER': {'INNER1': {'INNER2': {'INNER3': val}}}}
51 mydict => {'INNER3': val}
53 for k, v
in list(mydict.items()):
54 if isinstance(v, dict):
56 for key
in list(v.keys()):
60 if not len(
list(v.keys())):
66 Remove all keys with name matching keyregex from nested dictionary.
68 mydict = {'OUTER': {'INNER1': {'INNER2': {'INNER3': val}}}}
69 delete_key(mydict, '*.2')
70 mydict => {'OUTER': {'INNER1': {}}}
72 for v
in list(mydict.values()):
73 if isinstance(v, dict):
75 for k
in __matches(keyregex,
list(mydict.keys())):
81 Remove empty dictionaries from nested dictionary.
83 mydict = {'OUTER': {'INNER': val}, 'EMPTY': {'EMPTY': {}}}
85 mydict => {'OUTER': {'INNER': val} }
87 for k, v
in list(mydict.items()):
88 if isinstance(v, dict):
90 if not len(
list(v.keys())):
96 Combine two key/value pairs.
98 mydict = {'OUTER': {'namekey': 'key', 'valuekey': val}}
99 reassign_keys_in_dict(mydict, 'namekey', 'valuekey')
100 mydict => {'OUTER': {'key': val}}
102 for v
in list(mydict.values()):
103 if isinstance(v, dict):
106 v[v[namekey]] = v[valuekey]
109 except (KeyError, TypeError):
pass
112 def _allkeys(mydict, myset):
114 for d
in mydict.values():
115 if isinstance(d, dict):
120 """Return list of all unique keys in dictionary."""
122 _allkeys(mydict, myset)
127 if __name__ ==
"__main__":
130 def __dotest(cmd, dict1, dict2):
132 print(
"before: ", dict1)
133 start_time = time.time()
135 end_time = time.time()
136 print(
"after: ", dict1)
137 print(
"target: ", dict2)
138 print(1000 * (end_time - start_time),
"ms")
142 cmd =
"promote_value(dict1, 'IN.*')"
143 dict1 = {
'OUTER': {
'INNER': val}}
144 dict2 = {
'OUTER': val}
145 __dotest(cmd, dict1, dict2)
147 cmd =
"promote_dict(dict1, 'IN.*')"
148 dict1 = {
'OUTER': {
'INNER': val}}
149 dict2 = {
'INNER': val}
150 __dotest(cmd, dict1, dict2)
152 cmd =
"flatten_dict(dict1)"
153 dict1 = {
'OUTER': {
'INNER1': {
'INNER2': {
'INNER3': val}}}}
154 dict2 = {
'INNER3': val}
155 __dotest(cmd, dict1, dict2)
157 cmd =
"delete_key(dict1, '.*2')"
158 dict1 = {
'OUTER': {
'INNER1': {
'INNER2': {
'INNER3': val}}}}
159 dict2 = {
'OUTER': {
'INNER1': {}}}
160 __dotest(cmd, dict1, dict2)
162 cmd =
"delete_empty(dict1)"
163 dict1 = {
'OUTER': {
'INNER': val},
'EMPTY': {
'EMPTY': {}}}
164 dict2 = {
'OUTER': {
'INNER': val}}
165 __dotest(cmd, dict1, dict2)
167 cmd =
"reassign_keys_in_dict(dict1, 'namekey', 'valuekey')"
168 dict1 = {
'OUTER': {
'namekey':
'key',
'valuekey': val}}
169 dict2 = {
'OUTER': {
'key': val}}
170 __dotest(cmd, dict1, dict2)
def promote_value(mydict, keyregex)
def delete_key(mydict, keyregex)
list(APPEND LIBS ${PGSTK_LIBRARIES}) add_executable(atteph_info_modis atteph_info_modis.c) target_link_libraries(atteph_info_modis $
def promote_dict(mydict, keyregex)
def reassign_keys_in_dict(mydict, namekey, valuekey)