-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
38 lines (28 loc) · 956 Bytes
/
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
import time
import sys
from sage.rings.all import ZZ
LOG = sys.stdout
def mod_near_poly(x,q):
''' mod_near the coefficients of x (mod q) '''
return map(lambda y: mod_near(y,q), x)
def mod_near(a,b):
''' returns a mod b where -b/2 <= a < b/2 '''
a = ZZ(a)
b = ZZ(b)
return a - b*((2*a+b) // (2*b))
def profile(log_file, message):
def timed(func):
def wrap(*args, **kwargs):
if type(log_file) != 'str':
start = time.time()
result = func(*args, **kwargs)
log_file.write(message+": " + str(time.time() - start) + "\n")
else:
with open(log_file, 'a') as f:
f.write(message + "\n")
start = time.time()
result = func(*args, **kwargs)
f.write("time: " + str(time.time() - start) + "\n")
return result
return wrap
return timed