-
Notifications
You must be signed in to change notification settings - Fork 3
/
utilities.py
332 lines (297 loc) · 9.51 KB
/
utilities.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# pylint: disable=broad-except, unspecified-encoding
"""
╔╗ ┬┌┬┐┌─┐┬ ┬┌─┐┬─┐┌─┐┌─┐ ╔╦╗┌─┐─┐ ┬ ╦ ╦─┐ ┬
╠╩╗│ │ └─┐├─┤├─┤├┬┘├┤ └─┐ ║║├┤ ┌┴┬┘ ║ ║┌┴┬┘
╚═╝┴ ┴ └─┘┴ ┴┴ ┴┴└─└─┘└─┘ ═╩╝└─┘┴ └─ ╚═╝┴ └─
Bitshares Decentralized Exchange User Experience
"""
# STANDARD MODULES
import contextlib
import math
import os
import sys
import time
from datetime import datetime
from json import loads as json_loads
from threading import Thread
from traceback import format_exc
# GLOBAL CONSTANTS
ATTEMPTS = 3
PATH = f"{str(os.path.dirname(os.path.abspath(__file__)))}/"
class NonceSafe:
"""
╔═══════════════════════════════╗
║ ╔╗╔╔═╗╔╗╔╔═╗╔═╗ ╔═╗╔═╗╔═╗╔═╗ ║
║ ║║║║ ║║║║║ ║╣ ╚═╗╠═╣╠╣ ║╣ ║
║ ╝╚╝╚═╝╝╚╝╚═╝╚═╝ ╚═╝╩ ╩╚ ╚═╝ ║
╚═══════════════════════════════╝
context manager for process-safe nonce generation and inter process communication
nonce generation
process safe read
process safe write
process safe atomic read/write
wtfpl litepresence.com 2022
"""
@staticmethod
def __enter__(*_) -> None:
"""
file lock: try until success to change name of nonce.vacant to nonce.occupied
"""
if not os.path.exists(f"{PATH}nonce_safe/nonce.vacant") and not os.path.exists(
f"{PATH}nonce_safe/nonce.occupied"
):
NonceSafe.restart()
while True:
# fails when nonce.occupied
try:
os.rename("nonce_safe/nonce.vacant", "nonce_safe/nonce.occupied")
break
except Exception:
time.sleep(0.01)
@staticmethod
def __exit__(*_) -> None:
"""
file unlock : change name of nonce.occupied back to nonce.vacant
"""
os.rename("nonce_safe/nonce.occupied", "nonce_safe/nonce.vacant")
@staticmethod
def restart() -> None:
"""
new locker: on startup, delete directory and start fresh
"""
os.system(
f"rm -r {PATH}nonce_safe; "
+ f"mkdir {PATH}nonce_safe; "
+ f"touch {PATH}nonce_safe/nonce.vacant"
)
thread = Thread(target=NonceSafe.free)
thread.start()
@staticmethod
def free() -> None:
"""
the nonce locker should never be occupied for more than a few milliseconds
plausible the locker could get stuck, e.g. a process terminates while occupied
"""
while True:
# check every three seconds if the nonce is vacant
if os.path.exists(f"{PATH}nonce_safe/nonce.vacant"):
time.sleep(3)
else:
# check repeatedly for 3 seconds for vacancy
start = time.time()
while True:
elapsed = time.time() - start
if os.path.exists(f"{PATH}nonce_safe/nonce.vacant"):
break
# liberate the nonce locker
if elapsed > 3:
os.rename(
"nonce_safe/nonce.occupied", "nonce_safe/nonce.vacant"
)
def get_nonce(precision: int = 1e9) -> int:
"""
:param precision: int(10^n)
:return nonce:
"""
with NonceSafe():
now = int(time.time() * precision)
while True:
nonce = int(time.time() * precision)
if nonce > now:
return nonce
time.sleep(1 / (10 * precision))
def chunks(l, n):
"""Yield n number of striped chunks from l."""
return [l[i::n] for i in range(n)]
def is_whitelisted(asset):
"""
return the whitelisting status of the asset
"""
grey = 3 # Not Rated
if asset == "BTS":
grey = 6
elif "COMPUMATRIX" in asset:
grey = 1 # Blacklisted
elif asset == "CNY":
grey = 2 # Not Recommended
elif ("XBTSX" in asset) or ("GDEX" in asset):
grey = 4 # Whitelisted
elif "HONEST" in asset:
grey = 6 if ("BTC" in asset) or ("USD" in asset) else 5
return grey * (255 / 6)
def it(style, text):
"""
Color printing in terminal
"""
emphasis = {
"red": 91,
"green": 92,
"yellow": 93,
"blue": 94,
"purple": 95,
"cyan": 96,
"orange": "38;5;202",
}
return f"\033[{emphasis[style]}m{str(text)}\033[0m"
# ~ def block_print():
# ~ """
# ~ temporarily disable printing
# ~ """
# ~ sys.stdout = open(os.devnull, "w")
# ~ def enable_print():
# ~ """
# ~ re-enable printing
# ~ """
# ~ sys.stdout = sys.__stdout__
def trace(error):
"""
print and return stack trace upon exception
"""
msg = str(type(error).__name__) + "\n"
msg += str(error.args) + "\n"
msg += str(format_exc()) + "\n"
print(msg)
return msg
def sigfig(number, n_figures=6):
"""
Formats a number to have a specified number of significant figures.
The number is rounded to the nearest significant figure. If the number is
zero or close to zero, it is returned as 0.0. Positive and negative infinity
and NaN are returned as-is.
Parameters:
number (float): The number to be formatted.
n_figures (int, optional): The number of significant figures to be displayed (default is 6).
Returns:
float: The formatted number.
"""
if number == 0 or number != number or math.isinf(number):
return number
elif number > 10**-15:
return round(
number, -int(math.floor(math.log10(abs(number)))) + (n_figures - 1)
)
else:
return 0.0
# ~ def no_sci(flt, n_figures=6):
# ~ """
# ~ format floats without scientific notation
# ~ """
# ~ flt = str(sigfig(flt, n_figures))
# ~ was_neg = False
# ~ return_val = _extracted_from_no_sci_8(flt, was_neg) if "e" in flt else flt
# ~ return return_val.ljust(n_figures - len(return_val.lstrip(".0")), "0")
# ~ # TODO Rename this here and in `no_sci`
# ~ def _extracted_from_no_sci_8(flt, was_neg):
# ~ if flt.startswith("-"):
# ~ flt = flt[1:]
# ~ was_neg = True
# ~ str_vals = str(flt).split("e")
# ~ coef = float(str_vals[0])
# ~ exp = int(str_vals[1])
# ~ result = ""
# ~ if exp > 0:
# ~ result += str(coef).replace(".", "")
# ~ result += "".join(["0" for _ in range(abs(exp - len(str(coef).split(".")[1])))])
# ~ elif exp < 0:
# ~ result += "0."
# ~ result += "".join(["0" for _ in range(abs(exp) - 1)])
# ~ result += str(coef).replace(".", "")
# ~ if was_neg:
# ~ result = f"-{result}"
# ~ return result
def nbsp(count):
"""
returns a string of multiple non breaking spaces
"""
return "".join(" " for _ in range(count))
def no_sci(flt, n_figures=6):
"""
Formats a float to a specified number of figures, avoiding scientific notation.
Parameters:
flt (float): The float to be formatted.
n_figures (int, optional): The number of figures to be displayed (default is 6).
Returns:
str: The formatted float.
"""
flt = str(sigfig(float(flt), n_figures))
was_neg = False
if "e" in flt:
if flt.startswith("-"):
flt = flt[1:]
was_neg = True
str_vals = flt.split("e")
coef = float(str_vals[0])
exp = int(str_vals[1])
result = ""
if exp > 0:
result += str(coef).replace(".", "")
result += "".join(
["0" for _ in range(abs(exp - len(str(coef).split(".")[1])))]
)
elif exp < 0:
result += "0."
result += "".join(["0" for _ in range(abs(exp) - 1)])
result += str(coef).replace(".", "")
if was_neg:
result = f"-{result}"
flt = result
return flt.ljust(n_figures - len(flt.lstrip(".0")), "0")
# ~ def race_write(doc="", text=""):
# ~ """
# ~ Concurrent Write to File Operation
# ~ """
# ~ text = str(text)
# ~ i = 0
# ~ doc = f"{PATH}pipe/{doc}"
# ~ while True:
# ~ try:
# ~ time.sleep(0.05 * i ** 2)
# ~ i += 1
# ~ with open(doc, "w+") as handle:
# ~ handle.write(text)
# ~ handle.close()
# ~ break
# ~ except Exception as error:
# ~ msg = str(type(error).__name__) + str(error.args)
# ~ msg += " race_write()"
# ~ print(msg)
# ~ with contextlib.suppress(Exception):
# ~ handle.close()
# ~ continue
# ~ finally:
# ~ with contextlib.suppress(Exception):
# ~ handle.close()
# ~ def race_read_json(doc=""):
# ~ """
# ~ Concurrent Read JSON from File Operation
# ~ """
# ~ doc = f"{PATH}pipe/{doc}"
# ~ i = 0
# ~ while True:
# ~ try:
# ~ time.sleep(0.05 * i ** 2)
# ~ i += 1
# ~ with open(doc, "r") as handle:
# ~ data = json_loads(handle.read())
# ~ handle.close()
# ~ return data
# ~ except Exception as error:
# ~ msg = str(type(error).__name__) + str(error.args)
# ~ msg += " race_read_json()"
# ~ print(msg)
# ~ with contextlib.suppress(Exception):
# ~ handle.close()
# ~ continue
# ~ finally:
# ~ with contextlib.suppress(Exception):
# ~ handle.close()
def to_iso_date(unix):
"""
returns CONSTANTS.core.ISO8601 datetime given unix epoch
"""
return datetime.utcfromtimestamp(int(unix / 1000)).isoformat()
def from_iso_date(date):
"""
returns unix epoch given YYYY-MM-DD
"""
return int(time.mktime(time.strptime(str(date), "%Y-%m-%d %H:%M:%S")))