This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
81 lines (68 loc) · 2.19 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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
utility functions and classes
"""
import codecs
import csv
import datetime
# from https://gist.github.com/nzjrs/207624
def humanize_date_difference(now, otherdate=None, offset=None):
if otherdate:
dt = otherdate - now
offset = dt.seconds + (dt.days * 60 * 60 * 24)
if offset:
delta_s = offset % 60
offset /= 60
delta_m = offset % 60
offset /= 60
delta_h = offset % 24
offset /= 24
delta_d = offset
else:
raise ValueError("Must supply otherdate or offset (from now)")
if delta_d > 1:
if delta_d > 6:
date = now + datetime.timedelta(days=-delta_d, hours=-delta_h,
minutes=-delta_m)
return date.strftime('%A, %Y %B %m, %H:%I')
else:
wday = now + datetime.timedelta(days=-delta_d)
return wday.strftime('%A')
if delta_d == 1:
return "Yesterday"
if delta_h > 0:
return "%dh%dm ago" % (delta_h, delta_m)
if delta_m > 0:
return "%dm%ds ago" % (delta_m, delta_s)
else:
return "%ds ago" % delta_s
# from https://gist.github.com/eightysteele/1174811
class UTF8Recoder:
"""
Iterator that reads an encoded stream and reencodes the input to UTF-8.
"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
n = self.reader.next()
n = n.encode("utf-8")
return n
# from https://gist.github.com/eightysteele/1174811
class UnicodeDictReader:
"""
A CSV reader which will iterate over lines in the CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
self.header = self.reader.next()
def next(self):
row = self.reader.next()
vals = [unicode(s, "utf-8") for s in row]
return dict((self.header[x], vals[x]) for x in range(len(self.header)))
def __iter__(self):
return self