-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauxiliary.py
47 lines (39 loc) · 1.49 KB
/
auxiliary.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
def copy_file(filename):
with open(filename, 'rb') as input:
return input.read()
def clean_json(input):
if isinstance(input, dict):
return {clean_json(key.lower()): clean_json(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [clean_json(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
def to_dict(obj, classkey=None):
if isinstance(obj, dict):
for k in obj.keys():
obj[k] = to_dict(obj[k], classkey)
return obj
elif hasattr(obj, "__iter__"):
return [to_dict(v, classkey) for v in obj]
elif hasattr(obj, "__dict__"):
data = dict([(key, to_dict(value, classkey))
for key, value in obj.__dict__.iteritems()
if not callable(value) and not key.startswith('_')])
if classkey is not None and hasattr(obj, "__class__"):
data[classkey] = obj.__class__.__name__
return data
else:
return obj
def snake_case(string):
return string.replace(" ", "_").lower()
def camel_case_caps(string):
return ''.join(x for x in string.title() if not x.isspace())
def camel_case(string):
s = camel_case_caps(string)
return s[0].lower() + s[1:] if s else ""
def flat_case(string):
return string.replace(" ", "").replace("_", "").lower()
def kebab_case(string):
return string.replace(" ", "-").replace("_", "-").lower()