-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.py
143 lines (107 loc) · 3.5 KB
/
util.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import time
import bisect
import sqlalchemy
from app import db
def dict_from_dir(obj, keys_to_ignore=None, keys_to_show="all"):
if keys_to_ignore is None:
keys_to_ignore = []
elif isinstance(keys_to_ignore, basestring):
keys_to_ignore = [keys_to_ignore]
ret = {}
if keys_to_show != "all":
for key in keys_to_show:
ret[key] = getattr(obj, key)
return ret
for k in dir(obj):
value = getattr(obj, k)
if k.startswith("_"):
pass
elif k in keys_to_ignore:
pass
# hide sqlalchemy stuff
elif k in ["query", "query_class", "metadata"]:
pass
elif callable(value):
pass
else:
try:
# convert datetime objects...generally this will fail becase
# most things aren't datetime object.
ret[k] = time.mktime(value.timetuple())
except AttributeError:
ret[k] = value
return ret
def median(my_list):
"""
Find the median of a list of ints
from https://stackoverflow.com/questions/24101524/finding-median-of-list-in-python/24101655#comment37177662_24101655
"""
my_list = sorted(my_list)
if len(my_list) < 1:
return None
if len(my_list) %2 == 1:
return my_list[((len(my_list)+1)/2)-1]
if len(my_list) %2 == 0:
return float(sum(my_list[(len(my_list)/2)-1:(len(my_list)/2)+1]))/2.0
def underscore_to_camelcase(value):
words = value.split("_")
capitalized_words = []
for word in words:
capitalized_words.append(word.capitalize())
return "".join(capitalized_words)
def chunks(l, n):
"""
Yield successive n-sized chunks from l.
from http://stackoverflow.com/a/312464
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def page_query(q, page_size=1000):
offset = 0
while True:
r = False
print "util.page_query() retrieved {} things".format(page_query())
for elem in q.limit(page_size).offset(offset):
r = True
yield elem
offset += page_size
if not r:
break
def elapsed(since, round_places=2):
return round(time.time() - since, round_places)
def truncate(str, max=100):
if len(str) > max:
return str[0:max] + u"..."
else:
return str
def str_to_bool(x):
if x.lower() in ["true", "1", "yes"]:
return True
elif x.lower() in ["false", "0", "no"]:
return False
else:
raise ValueError("This string can't be cast to a boolean.")
def calculate_percentile(refset, value):
if value is None: # distinguish between that and zero
return None
matching_index = bisect.bisect_left(refset, value)
percentile = float(matching_index) / len(refset)
# print u"percentile for {} is {}".format(value, percentile)
return percentile
# from http://stackoverflow.com/a/20007730/226013
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4])
def safe_commit(db):
try:
db.session.commit()
return True
except (KeyboardInterrupt, SystemExit):
# let these ones through, don't save anything to db
raise
except sqlalchemy.exc.DataError:
db.session.rollback()
print u"sqlalchemy.exc.DataError on commit. rolling back."
except Exception as e:
print "error", e
print u"generic exception in commit. rolling back."
db.session.rollback()
return False