File diff 0120e4ce5a5a → 9d4723824592
oxrlib/rate.py
Show inline comments
 
import datetime
 
import decimal
 
import functools
 
import json
 

	
...
 
@@ -34,14 +35,21 @@ class Rate:
 
    def deserialize_timestamp(cls, value):
 
        return datetime.datetime.fromtimestamp(value)
 

	
 
    @classmethod
 
    def serialize_rates(cls, rates):
 
        return {code: float(rates[code]) for code in rates}
 

	
 
    @classmethod
 
    def serialize_timestamp(cls, value):
 
        return int(value.timestamp())
 

	
 
    @classmethod
 
    def from_json_file(cls, json_file):
 
        response = json.load(json_file)
 
        response = json.load(json_file, parse_float=decimal.Decimal)
 
        return cls(**response)
 

	
 
    def convert(self, amount, from_currency, to_currency):
 
        return amount * self.rates[to_currency] / self.rates[from_currency]
 

	
 
    def serialize(self):
 
        return dict(self.walk_fields(functools.partial(getattr, self), 'serialize'))