-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
294 lines (215 loc) · 7.94 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 13 16:03:13 2014
@author: Gaspard, Thomas, Arnaud
"""
import time, datetime, urllib, sys
from math import ceil, log
from bs4 import BeautifulSoup
# TODO put that in localurl
debug_mode = True
def timestamp(date):
return int(time.mktime(datetime.datetime.strptime(date,"%d.%m.%Y").timetuple()))
def createChronology_old(date, round_):
return timestamp(date)*10 + int(round_)
def createChronology(startDate, endDate, round_, draw ):
nRounds = float( ceil( log(int(draw)) / log(2)) )
start = timestamp(startDate)
end = 86400 + timestamp(endDate)
interv = float(end - start)
return start + int(interv/(2.0*nRounds) + float(int(round_)-8+nRounds)*interv/nRounds)
def getNotNoneTime(day=None, month=None, year=None):
if not day or not month or not year:
lt = time.localtime()
return (lt.tm_mday, lt.tm_mon, lt.tm_year)
else:
return (day, month, year)
class Chrono:
def __init__(self):
self.sizeBar = 18
self.periodTime = 2.0
def start(self, nbIterations = 0):
self.total = int( nbIterations )
self.absoluteTotal = self.total
self.strTotal = str( self.absoluteTotal )
self.i = 0
self.previous = 0
self.startTime = time.time()
self.previousTime = self.startTime
def needPrint(self):
return time.time()-self.previousTime > self.periodTime
def tick(self, iterations = 1):
self.i += iterations
def decTotal(self, iterations = 1):
self.total -= 1
def remainingTime(self):
remaining = (self.total - self.i) * self.elapsedTime() / self.i
self.previous = self.i
return remaining
def remaining(self):
return prettyPrintTime( self.remainingTime() )
def elapsedTime(self):
self.previousTime = time.time()
return self.previousTime - self.startTime
def elapsed(self):
return prettyPrintTime( self.elapsedTime() )
def getBar(self):
return loadingBar(self.sizeBar, self.i,self.total)
def printRemaining(self):
r = self.remaining()
if self.absoluteTotal == self.total:
debugCL('Chrono: ' + str( self.i ) + ' / ' + self.strTotal +
' Remaining: ' + r )
else:
debugCL('Chrono: ' + str( self.i ) + ' / ' + str( self.total ) +
' (Total: '+ self.strTotal +
') Remaining: ' + r )
class Clock:
def __init__(self):
self.time = 0
def clock(self):
t = time.time()
result = t - self.time
self.time = t
return result
def strClock(self):
return "Time: " + prettyPrintTime(self.clock())
def done(self):
debug("Done. " + self.strClock())
currencyName = {'\xe2\x82\xac' :'E',
'$' :'D',
'\xc2\xa3' :'P',
'\xc3\x87' :'E',
'\xc3\xba' :'P',
'A$' :'A'}
def cleanLine(line):
if line[-1] == '\n' or line[-1] == '\r':
return cleanLine( line[:-1] )
else:
return line
def printObject(arg, prefix = ""):
if type(arg) == type(dict()):
for i in arg:
printObject( arg[i], prefix + " " + i )
elif type(arg) == type([]):
for i in range(len(arg)):
printObject( arg[i], prefix + " " + str(i+1) )
else:
print prefix + " : " + str(arg)
def prettyPrintTime(sec):
s = int(sec)
hours, remainder = divmod(s, 3600)
minutes, seconds = divmod(remainder, 60)
if hours > 0:
return ('%sh %smin %ssec' % (hours, minutes, seconds) )
elif minutes > 0:
return ('%smin %ssec' % (minutes, seconds) )
else:
ms = int( 100 * (sec - s))
return ('%ssec %sms' % (seconds, ms) )
def urlOpen(url):
a = urllib.urlopen(url)
# handle timeout here
return a
def getDOM(url):
return BeautifulSoup( urlOpen(url) )
def getHTML(url):
return urlOpen(url).read()
trueIndic = ['yes', 'Yes', 'True', 'true', 'T']
falseIndic = ['no', 'No', 'False', 'false', 'F']
def getBool(s):
if s in trueIndic:
return True
elif s in falseIndic:
return False
else:
raise Exception("Wrong boolean indicator")
def updateCategory(e, cat):
if cat==4: return 5 # ATP Challenger Tour
elif cat==3: return 4 # ATP World Tour 250
elif cat==2: return 3 # ATP World Tour 500
elif cat==1: return 2 # ATP World Tour Masters 1000
elif cat ==-1:
if e == 96: # Olympics
return 1
elif e in {352,403,404,410,416,421,422,1536,357}:
return 2 # ATP World Tour Masters 1000
elif e in {328,329,402,407,414,418,425,495,573,747,807,408}:
return 3 # ATP World Tour 500
elif e in { 301,306,308,311,314,315,316,321,337,338,339,341,360,375,
419,423,424,429,438,439,440,451,468,496,499,500,505,506,
533,568,717,741,773,891,1720,2276,3348,317,409,309,359,
620,481,615,890,336,433,441,3465,475,325,73,327,319 }:
return 4 # ATP World Tour 250
return cat
# -------------------------------------------------------------
# ----------- DEBUGGING ---------------------------------------
# -------------------------------------------------------------
debugFileOut = 'NoFile'
debugNewline = True
def setDebugFileOut(fileOut):
global debugFileOut
debugFileOut = fileOut
debugPrintTime("Starting:")
def debugPrintTime(prefix=''):
debugLines(["", "-"*(35 + len(prefix)),
" {1} {0.tm_mday} / {0.tm_mon} / {0.tm_year} {0.tm_hour}:{0.tm_min}:{0.tm_sec}".format(time.localtime(), prefix),
"-"*(35 + len(prefix)), "" ] )
def debug(line = ''):
global debugNewline
if not debugNewline:
print
debugNewline = True
ln = str(line) + '\n'
sys.stdout.write( ln )
if debugFileOut != 'NoFile':
debugFileOut.write( ln )
def debugLines(lines):
for l in lines:
debug(l)
def debugCL(line = ''):
global debugNewline
if not debugNewline:
sys.stdout.write('\b\r')
sys.stdout.flush()
debugNewline = False
ln = str(line)
sys.stdout.write( ln )
if debugFileOut != 'NoFile':
debugFileOut.write( ln + '\n' )
#
#def debug(msg):
# if debug_mode:
# Debug.println("D: " + msg)
#
def printError(msg):
debug( "Error: " + msg + " !!!" )
def restartLine():
sys.stdout.write('\b\r')
sys.stdout.flush()
def printLine(l):
debugCL(l)
def loadingBar(length, progress, total=1):
progress = float(length * progress) / total
n = int( progress )
p = int( 10 * (progress - n) )
return " |" + ('#' * n) + str(p) + ('_'*(length-n-1)) + "| "
def deltaDays(date1, date2):
return (time.mktime( date1 ) - time.mktime( date2 )) / (3600 * 24)
# -------------------------------------------------------------
# ----------- PRETTY PRINTING --------------------------------
# -------------------------------------------------------------
def printMatrix(matches):
l = len(matches[0])
lengths = [0] * l
for m in matches:
for i in range(l):
if len(str(m[i])) > lengths[i]:
lengths[i] = len( str(m[i]) )
s = "%" + ("s %".join( [str(e) for e in lengths])) + "s"
for m in matches:
print s % tuple( [str(e) for e in m])
def printDict(d):
printMatrix( [ [ str(e[0]), " : ", str(e[1]) ] for e in d.items() ] )
def printDicts(dicts):
printMatrix( [ [ str(e[0]), " : ", str(e[1]) ] for d in dicts for e in d.items() ] )