Files
@ a1811663115e
Branch filter:
Location: NPO-Accounting/oxrlib/oxrlib/rate.py - annotation
a1811663115e
1.3 KiB
text/x-python
rate: Initial commit.
a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e a1811663115e | import datetime
import functools
import json
class Rate:
FIELDS = [
'base',
'disclaimer',
'license',
'rates',
'timestamp',
]
@classmethod
def walk_fields(cls, get_field, transform_prefix):
for fieldname in cls.FIELDS:
value = get_field(fieldname)
if value is not None:
try:
transformer = getattr(cls, '{}_{}'.format(transform_prefix, fieldname))
except AttributeError:
pass
else:
value = transformer(value)
yield (fieldname, value)
def __init__(self, base, rates, **kwargs):
for key, value in self.walk_fields(kwargs.get, 'deserialize'):
setattr(self, key, value)
self.base = base
self.rates = rates
@classmethod
def deserialize_timestamp(cls, value):
return datetime.datetime.fromtimestamp(value)
@classmethod
def serialize_timestamp(cls, value):
return int(value.timestamp())
@classmethod
def from_json_file(cls, json_file):
response = json.load(json_file)
return cls(**response)
def serialize(self):
return dict(self.walk_fields(functools.partial(getattr, self), 'serialize'))
|