2016-04-26 23:32:57 +00:00
|
|
|
import sys
|
2016-09-26 14:23:48 +00:00
|
|
|
import gc
|
|
|
|
|
2018-02-23 20:27:26 +00:00
|
|
|
from trezorutils import halt, memcpy, set_mode_unprivileged, symbol
|
2016-04-27 19:07:37 +00:00
|
|
|
|
2017-06-14 17:34:23 +00:00
|
|
|
|
2018-01-30 17:50:59 +00:00
|
|
|
def unimport(genfunc):
|
2016-09-26 14:23:48 +00:00
|
|
|
async def inner(*args, **kwargs):
|
2016-04-26 23:32:57 +00:00
|
|
|
mods = set(sys.modules)
|
2016-04-28 05:44:15 +00:00
|
|
|
try:
|
2016-09-26 14:23:48 +00:00
|
|
|
ret = await genfunc(*args, **kwargs)
|
2016-04-28 05:44:15 +00:00
|
|
|
finally:
|
2016-12-12 14:20:20 +00:00
|
|
|
for mod in sys.modules:
|
|
|
|
if mod not in mods:
|
|
|
|
del sys.modules[mod]
|
2016-09-26 14:23:48 +00:00
|
|
|
gc.collect()
|
2016-04-26 23:32:57 +00:00
|
|
|
return ret
|
|
|
|
return inner
|
2016-06-09 14:31:53 +00:00
|
|
|
|
2016-09-21 12:24:12 +00:00
|
|
|
|
2017-06-14 17:34:23 +00:00
|
|
|
def chunks(items, size):
|
|
|
|
for i in range(0, len(items), size):
|
|
|
|
yield items[i:i + size]
|
2016-11-23 13:46:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ensure(cond):
|
|
|
|
if not cond:
|
|
|
|
raise AssertionError()
|
2018-02-06 17:35:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_amount(amount, decimals):
|
|
|
|
d = pow(10, decimals)
|
|
|
|
amount = ('%d.%0*d' % (amount // d, decimals, amount % d)).rstrip('0')
|
|
|
|
if amount.endswith('.'):
|
2018-02-09 12:36:08 +00:00
|
|
|
amount = amount[:-1]
|
2018-02-06 17:35:14 +00:00
|
|
|
return amount
|
2018-02-25 14:05:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_ordinal(number):
|
|
|
|
return str(number) + {1: 'st', 2: 'nd', 3: 'rd'}.get(4 if 10 <= number % 100 < 20 else number % 10, 'th')
|