-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
39 lines (27 loc) · 844 Bytes
/
tools.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
from json import JSONEncoder
from bson import ObjectId
import pytz
import datetime
from infinity import app
#helper methods - mongoencoder, utc time, timeit
def timeit(method):
def timed(*args, **kw):
ts = datetime.datetime.now()
result = method(*args, **kw)
te = datetime.datetime.now()
app.logger.info('Timer got: %s for %r (%r, %r)' % (te-ts, method.__name__, args, kw))
return result
return timed
class MongoEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, ObjectId):
return str(obj)
op = getattr(obj, "to_mongo", None)
if callable(op):
return obj.to_mongo()
else:
return JSONEncoder.default(self, obj)
def utc_now():
u = datetime.utcnow()
u = u.replace(tzinfo=pytz.utc)
return u