forked from ourresearch/unpaywall-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
328 lines (252 loc) · 8.46 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import time
import unicodedata
import math
import bisect
import re
import collections
class NoDoiException(Exception):
pass
# from http://stackoverflow.com/a/3233356/596939
def update_recursive_sum(d, u):
for k, v in u.iteritems():
if isinstance(v, collections.Mapping):
r = update_recursive_sum(d.get(k, {}), v)
d[k] = r
else:
if k in d:
d[k] += u[k]
else:
d[k] = u[k]
return d
# returns dict with values that are proportion of all values
def as_proportion(my_dict):
if not my_dict:
return {}
total = sum(my_dict.values())
resp = {}
for k, v in my_dict.iteritems():
resp[k] = round(float(v)/total, 2)
return resp
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
# good for deduping strings. output removes spaces so isn't readable.
def normalize(text):
# remove all white space
response = re.sub(u"\s+", u"", text)
response = remove_punctuation(response.lower())
return response
def remove_punctuation(input_string):
# from http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python
no_punc = input_string
if input_string:
no_punc = u"".join(e for e in input_string if (e.isalnum() or e.isspace()))
return no_punc
# from http://stackoverflow.com/a/11066579/596939
def replace_punctuation(text, sub):
punctutation_cats = set(['Pc', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Po'])
chars = []
for my_char in text:
if unicodedata.category(my_char) in punctutation_cats:
chars.append(sub)
else:
chars.append(my_char)
return u"".join(chars)
def conversational_number(number):
words = {
"1.0": "one",
"2.0": "two",
"3.0": "three",
"4.0": "four",
"5.0": "five",
"6.0": "six",
"7.0": "seven",
"8.0": "eight",
"9.0": "nine",
}
if number < 1:
return round(number, 2)
elif number < 1000:
return int(math.floor(number))
elif number < 1000000:
divided = number / 1000.0
unit = "thousand"
else:
divided = number / 1000000.0
unit = "million"
short_number = '{}'.format(round(divided, 2))[:-1]
if short_number in words:
short_number = words[short_number]
return short_number + " " + unit
def is_doi_url(url):
# test urls at https://regex101.com/r/yX5cK0/2
p = re.compile("https?:\/\/(?:dx.)?doi.org\/(.*)")
matches = re.findall(p, url)
if len(matches) > 0:
return True
return False
def clean_doi(dirty_doi):
if not dirty_doi:
raise NoDoiException("There's no valid DOI.")
dirty_doi = remove_nonprinting_characters(dirty_doi)
dirty_doi = dirty_doi.strip()
# test cases for this regex are at https://regex101.com/r/zS4hA0/1
p = re.compile(ur'.*?(10.+)')
matches = re.findall(p, dirty_doi)
if len(matches) == 0:
raise NoDoiException("There's no valid DOI.")
match = matches[0]
try:
resp = unicode(match, "utf-8") # unicode is valid in dois
except (TypeError, UnicodeDecodeError):
resp = match
# remove any url fragments
if u"#" in resp:
resp = resp.split(u"#")[0]
return resp
def pick_best_url(urls):
if not urls:
return None
#get a backup
response = urls[0]
# now go through and pick the best one
for url in urls:
# doi if available
if "doi.org" in url:
response = url
# anything else if what we currently have is bogus
if response == "http://www.ncbi.nlm.nih.gov/pmc/articles/PMC":
response = url
return response
def date_as_iso_utc(datetime_object):
if datetime_object is None:
return None
date_string = u"{}{}".format(datetime_object, "+00:00")
return date_string
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.")
# from http://stackoverflow.com/a/20007730/226013
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4])
#from http://farmdev.com/talks/unicode/
def to_unicode_or_bust(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
def remove_nonprinting_characters(input, encoding='utf-8'):
input_was_unicode = True
if isinstance(input, basestring):
if not isinstance(input, unicode):
input_was_unicode = False
unicode_input = to_unicode_or_bust(input)
# see http://www.fileformat.info/info/unicode/category/index.htm
char_classes_to_remove = ["C", "M", "Z"]
response = u''.join(c for c in unicode_input if unicodedata.category(c)[0] not in char_classes_to_remove)
if not input_was_unicode:
response = response.encode(encoding)
return response
# getting a "decoding Unicode is not supported" error in this function?
# might need to reinstall libaries as per
# http://stackoverflow.com/questions/17092849/flask-login-typeerror-decoding-unicode-is-not-supported
class HTTPMethodOverrideMiddleware(object):
allowed_methods = frozenset([
'GET',
'HEAD',
'POST',
'DELETE',
'PUT',
'PATCH',
'OPTIONS'
])
bodyless_methods = frozenset(['GET', 'HEAD', 'OPTIONS', 'DELETE'])
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
method = environ.get('HTTP_X_HTTP_METHOD_OVERRIDE', '').upper()
if method in self.allowed_methods:
method = method.encode('ascii', 'replace')
environ['REQUEST_METHOD'] = method
if method in self.bodyless_methods:
environ['CONTENT_LENGTH'] = '0'
return self.app(environ, start_response)