-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_utils.py
84 lines (79 loc) · 2.52 KB
/
py_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from collections import Counter
# lol -> list of lists
# lod -> list of dicts
# lot -> list of tuples
# dod -> dic of dicts
# dlod -> dic of lod
# data -> i.e. undetermined type
# s -> string
# assumption is that there are two basic data types a dlod and a dod.
tmp_lod = [dict(fake_key='f1', A='a1', B='b1'),
dict(fake_key='f2', A='a2', B='b2'),
dict(fake_key='f3', A='a3', B='b3'),
dict(fake_key='f4', A='a4', B='b4')]
def search_types(data):
# not tested
# change to include type leaf nodes ?
# assumes all entries are same type and depth is equal
if type(data) in [list, dict, tuple]:
if type(data) == dict:
return str(type(data)) + search_types(data[data.keys()[0]])
else:
return str(type(data)) + search_types(data[0])
return ''
def cast_to_short_cuts(s):
# are these all reasonable types?
if "<type 'list'><type 'dict'>" == s:
return 'lod'
elif "<type 'list'><type 'list'>" == s:
return 'lol'
elif "<type 'dict'><type 'list'>" == s:
return 'dol'
elif "<type 'dict'><type 'dict'>" == s:
return 'dod'
elif "<type 'dict'><type 'list'><type 'dict'>" == s:
return 'dod'
return 'error'
def check(data):
return cast_to_short_cuts(search_types(data))
def unique_values_from_d_in_lod_by_key(lod, s, verbose=False):
# test
# type data?
# lod = lod?
# make a list of unique items
v = set([d[s] for d in lod])
if verbose:
print "Total Number of Unique", s, "'s:", len(v)
return v
def convert_lod_to_dlod_by_key(lod, key):
values = unique_values_from_d_in_lod_by_key(lod, key)
a_dict = {}
for v in values:
a_dict[v] = [x for x in lod if x[key] == v]
return a_dict
def get(dod, key):
# test
# type check dod ?
return [dod[v][key] for v in dod.keys()]
def cnt_by_key(dod, k):
return Counter(get(dod,k))[True]
def print_dlod_subset_keys(dlod, k, header='name,desc,latitude,longitude',
keys=['date', 'latitude', 'longitude']):
if keys == None:
keys = dlod.keys()
ts = ",".join(['%s' for x in range(len(keys))])
print header
for entry in dlod[k]:
print ts % tuple([str(entry[x]) for x in keys])
return
def load_json(f_loc):
import json
with open(f_loc) as f:
data =[json.loads(x) for x in list(f)]
return data
def superset_of_keys(t):
# takes lod, or dlod
return
def superset_of_items_by_key(t):
# takes lod, or dlod
return