-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_methods.py
65 lines (58 loc) · 1.78 KB
/
common_methods.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
import requests
import csv
import codecs
import os.path
import cStringIO
def query_request(url):
for i in range(0,5):
try:
resp = requests.get(url)
if resp.status_code == 200:
return resp
except Exception:
pass
raise
def pretty_time(time):
sep = lambda n: (int(n / 60), int(n % 60))
mins, secs = sep(time)
hrs, mins = sep(mins)
return '{0}h {1}m {2}s'.format(hrs, mins, secs)
def csv_out(imdb, file):
print '\nWriting to', file
username = os.path.splitext(os.path.basename(file))[0]
with codecs.open(file, 'wb') as outfile:
w = UnicodeWriter(outfile)
row0 = []
for n in imdb:
row0.extend(n.keys())
row0 = set(row0)
w.writerow(row0)
for n in imdb:
row1 = []
for m in row0:
v = n.get(m)
if(v == None):
v = ""
row1.append(v)
w.writerow(row1)
# docs.python.org/2/library/csv.html#examples
class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
data = self.queue.getvalue()
data = data.decode("utf-8")
data = self.encoder.encode(data)
self.stream.write(data)
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)