2019-03-27 20:52:05 +00:00
|
|
|
from sys import stderr
|
2019-05-01 00:34:26 +00:00
|
|
|
from traceback import print_exc
|
2019-05-07 05:00:20 +00:00
|
|
|
from datetime import datetime, timezone
|
2019-03-27 20:52:05 +00:00
|
|
|
|
|
|
|
def eprint(*args, **kwargs):
|
2019-04-12 20:57:22 +00:00
|
|
|
print(*args, file=stderr, **kwargs)
|
|
|
|
|
2019-05-01 00:34:26 +00:00
|
|
|
def noop(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
2019-05-07 05:00:20 +00:00
|
|
|
def obtain(source, attribute):
|
|
|
|
return getattr(source, attribute, None)
|
|
|
|
|
2019-05-01 00:34:26 +00:00
|
|
|
def cast(source, method, *args, **kwargs):
|
|
|
|
source_method = getattr(source, method, noop)
|
|
|
|
try:
|
|
|
|
return source_method(*args, **kwargs)
|
|
|
|
except Exception:
|
|
|
|
status = print_exc()
|
|
|
|
eprint(status)
|
|
|
|
return status
|
|
|
|
|
2019-04-12 20:57:22 +00:00
|
|
|
def deduce(reference, attributes):
|
|
|
|
if type(attributes) is not list:
|
|
|
|
attributes = [attributes]
|
|
|
|
return list(map(lambda attr: getattr(reference, attr, None), attributes))
|
|
|
|
|
|
|
|
def get_digit(number, position):
|
2019-05-07 05:00:20 +00:00
|
|
|
return False if number - 10**position < 0 else number // 10*position % 10
|
|
|
|
|
|
|
|
def utc_now():
|
|
|
|
return datetime.utcnow().replace(tzinfo=timezone.utc)
|
|
|
|
|
|
|
|
def utc_now_timestamp():
|
2019-05-10 04:57:06 +00:00
|
|
|
return utc_now().timestamp()
|