-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
48 lines (44 loc) · 1.34 KB
/
utils.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
import time
from threading import Thread,Event
import socket
class RepeatTimer(Thread):
def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.iterations = iterations
self.args = args
self.kwargs = kwargs
self.finished = Event()
def run(self):
count = 0
global DOWNLOADED
while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):
if not self.finished.is_set():
time1=time.time()
self.function(*self.args, **self.kwargs)
time2=time.time()
count += 1
diff=time2-time1
if self.interval-diff>0:
self.finished.wait(self.interval-diff)
def cancel(self):
self.finished.set()
def copyListofDict(ListofDict):
tmpList=[]
for node in ListofDict:
tmpDict={}
for key in node.keys():
tmpDict[key]=node[key]
tmpList.append(tmpDict)
return tmpList
def get_ip():
t = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try :
t.connect(("gmail.com",80))
x=(t.getsockname()[0])
except:
x='127.0.0.1'
print "Ip = ", x
t.close()
return x