forked from AppScale/gts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.py
87 lines (77 loc) · 2.14 KB
/
helper_functions.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
82
83
84
85
86
"""
Author: Navraj Chohan
Random functions/classes which are used by datastores
"""
import hashlib
import logging
import logging.handlers
import os
import os.path
import random
import time
"""
strings must be in unicode to reverse the string
strings are returned in unicode and may not able
able to be converted to a regular string
"""
def reverseLex(ustring):
newstr = ""
for ii in ustring:
ordinance = ord(ii)
new_byte = 255 - ordinance
char = chr(new_byte)
newstr += char
return newstr
"""
Cetain datastores are unable to store keys with unichars of 128 or more
this function reflects on 127 and less.
"""
def reverseLex128(ustring):
newstr = u""
for ii in ustring:
ordinance = ord(ii)
new_byte = 127 - ordinance
char = unichr(new_byte)
newstr += char
return newstr
class ThreadedLogger():
def __init__(self, filename):
split_path = os.path.split(filename)
dir = split_path[0]
if not os.path.exists(dir): os.mkdir(dir, 0777)
self.log_logger = logging.getLogger(filename)
self.log_logger.setLevel(logging.INFO)
self.formatter = logging.Formatter("%(asctime)s %(module)s:%(lineno)-4d %(message)s")
self.handler = logging.handlers.RotatingFileHandler(filename, maxBytes=10000000, backupCount=10)
self.handler.setFormatter(self.formatter)
self.log_logger.addHandler(self.handler)
self.loggingOn = False
def turnLoggingOn(self):
self.loggingOn = True
def debug(self, string):
if self.loggingOn:
self.log_logger.info(string)
def randomString(length):
s = hashlib.sha256()
ret = "a"
while len(ret) < length:
s.update(str(random.random()))
ret += s.hexdigest()
return ret[0:length]
import inspect
def lineno():
"""Returns the current line number in our program."""
return inspect.currentframe().f_back.f_lineno
class Timer():
def __init__(self):
self.init = time.time()
self.start = time.time()
def done(self):
self.end = time.time()
self.interval = self.end - self.start
self.start = time.time()
return self.interval
def total(self):
self.end = time.time()
self.interval = self.end - self.init
return self.interval