-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.py
81 lines (64 loc) · 2.48 KB
/
common.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
'''
Copyright (C) Nicolas Fischer, [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from decimal import Decimal as D
import time
__all__ = ["D", "Trade", "dec", "say", "debug_print", "timeout", "BTC_PREC", "USD_PREC", "PRICE_PREC", "VOL_PREC", "VOL2_PREC", "MYVOL_PREC"]
BTC_PREC = D('0.00000001')
USD_PREC = D('0.00001')
PRICE_PREC = D('0.00001')
VOL_PREC = D('0.1')
VOL2_PREC = D('0')
MYVOL_PREC = D('0.01')
# class Trade
class Trade:
def __init__(S, tid, time, amount, price, type):
(S.tid, S.time, S.amount, S.price, S.type) = (tid, time, D(amount), D(price), type)
def str(S):
return "%s: {%s} | %s for %s - %s" % (time.strftime('%H:%M:%S',time.localtime(S.time)), S.tid, S.amount, S.price, S.type)
# talking
def say(text):
print text
#festival = subprocess.os.popen2("echo '(SayText \"" + text.replace("'", "") + "\")' | /usr/bin/festival --pipe")
# align D number at decimal point
def dec(dec, before, after):
rc = dec.normalize().to_eng_string()
if rc.find('.') >= 0:
rc = "%*s" % (before-rc.find('.')+len(rc), rc)
else: rc = "%*s.0" % (before, rc)
rc += ' '[:(after+before+1)-len(rc)]
rc = rc[:(before+after+1)]
return rc # rc.replace('0.', 'o.').replace('.0', '.o')
debug_log = open('debug.log', 'w')
def debug_print(str):
tm = time.localtime()
debug_log.write( '%s: %s\n' % (time.strftime('%Y/%m/%d-%H:%M:%S',tm), str) )
debug_log.flush()
def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
import threading
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default
it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result