-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
44 lines (36 loc) · 1.1 KB
/
utilities.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
class Utilities(object):
@staticmethod
def is_structure(value):
return (isinstance(value, list) or isinstance(value, dict))
# drill down to, and return, value in nested dicts
# usage example:
## d = {'a':{'b':{'c':5}}}
## lookup(d, 'a', 'b', 'c')
@staticmethod
def dict_lookup(dic, key, *keys):
if keys:
return Utilities.dict_lookup(dic.get(key, {}), *keys)
return dic.get(key)
# drill down to, and add, value in nested dicts, only if key doesn't exist
# adding nested dicts as needed
@staticmethod
def dict_nested_add(dic, value, key, *keys):
if keys:
if not dic.get(key):
dic[key] = {}
Utilities.dict_nested_add(dic[key], value, *keys)
elif not dic.get(key):
dic[key] = value
# drill down to, and add or update, value in nested dicts
# adding nested dicts as needed
@staticmethod
def dict_nested_update(dic, value, key, *keys):
if keys:
if not dic.get(key):
dic[key] = {}
Utilities.dict_nested_update(dic[key], value, *keys)
else:
dic[key] = value
@staticmethod
def add_slashes(s):
return s.replace("'", "\\'").replace('"', '\\"').replace("\\", "\\")