-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathtiming_util.py
55 lines (47 loc) · 1.46 KB
/
timing_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
"""
Provide a timing utility context manager
"""
import contextlib
@contextlib.contextmanager
def timing(results=None):
"""
Time the execution of the block of code. If a results list is
passed in, the time is appended to the list. Also returns a list
of one element containing the time the execution took.
To use, do something like::
from time import sleep
results_list=[]
with timing(results_list) as t:
sleep(1)
print results_list, t
Exceptions in the code should be re-raised and the timing should
correctly be set regardless of the exceptions.
"""
from time import time
try:
# code in the context is executed when we yield
start=[time()]
yield start
except:
# any exceptions in the code should get propogated
raise
finally:
start.append(time()-start[0])
if results is not None:
results.append(start)
import urllib
import urllib2
try: import simplejson as json
except ImportError: import json
def json_request(url, data=None):
"""
Send a JSON message to the URL and return the result as a
dictionary.
:param data: a JSON-stringifiable object, passed in the POST
variable ``message``
:returns: a JSON-parsed dict/list/whatever from the server reply
"""
if data is not None:
data = urllib.urlencode(data)
response = urllib2.urlopen(url, data)
return json.loads(response.read())