-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathutils.py
45 lines (40 loc) · 1.31 KB
/
utils.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
#!/usr/bin/python
import collections
import json
def prepare_dict(dictionary):
for key in dictionary.keys():
if key.count(" ") > 0:
new_key = key.replace(" ", "_")
dictionary[new_key] = dictionary.pop(key)
return dictionary
def rename_duplicates(immutable, mutable, prefix):
"""
Add prefix to elements of `mutable` if they are in `immutable`.
:param immutable: list of strings
:param mutable: list of strings
:param prefix: string
:return: list with renamed elements
"""
for value in mutable:
if value in immutable:
value = str(prefix) + value
return mutable
def namedtuple_with_defaults(typename, field_names, default_values=()):
"""
http://stackoverflow.com/questions/11351032/named-tuple-and-optional-keyword-arguments
"""
T = collections.namedtuple(typename, field_names)
T.__new__.__defaults__ = (None,) * len(T._fields)
if isinstance(default_values, collections.abc.Mapping):
prototype = T(**default_values)
else:
prototype = T(*default_values)
T.__new__.__defaults__ = tuple(prototype)
return T
def load_json(dictionary, key):
parameters_json = dictionary.get(key)
try:
parameters = json.loads(parameters_json)
except:
parameters = {}
return parameters