Changeset - a1811663115e
[Not reviewed]
0 0 6
Brett Smith - 7 years ago 2017-05-09 13:56:08
brettcsmith@brettcsmith.org
rate: Initial commit.
6 files changed with 88 insertions and 0 deletions:
0 comments (0 inline, 0 general)
.gitignore
Show inline comments
 
new file 100644
 
__pycache__/
oxrlib/__init__.py
Show inline comments
 
new file 100644
oxrlib/rate.py
Show inline comments
 
new file 100644
 
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'))
tests/__init__.py
Show inline comments
 
new file 100644
 
import pathlib
 

	
 
TEST_DIR = pathlib.Path(__file__).parent
 

	
 
def relpath(path):
 
    return TEST_DIR / pathlib.Path(path)
tests/historical1.json
Show inline comments
 
new file 100644
 
{
 
    "disclaimer": "https://openexchangerates.org/terms/",
 
    "license": "https://openexchangerates.org/license/",
 
    "timestamp": 982342800,
 
    "base": "USD",
 
    "rates": {
 
        "AED": 3.67246,
 
        "ALL": 144.529793,
 
        "ANG": 1.79
 
    }
 
}
tests/test_Rate.py
Show inline comments
 
new file 100644
 
import datetime
 
import io
 
import json
 

	
 
from . import relpath
 

	
 
import oxrlib.rate
 
import pytest
 

	
 
@pytest.fixture
 
def historical1_rate():
 
    with open(relpath('historical1.json').as_posix()) as rate_file:
 
        return oxrlib.rate.Rate.from_json_file(rate_file)
 

	
 
def test_rate_from_json(historical1_rate):
 
    assert historical1_rate.base == 'USD'
 
    assert historical1_rate.timestamp == datetime.datetime(2001, 2, 16, 12, 0, 0)
 

	
 
def test_serialize(historical1_rate):
 
    with open(relpath('historical1.json').as_posix()) as rate_file:
 
        expected = json.load(rate_file)
 
    assert expected == historical1_rate.serialize()
 

	
0 comments (0 inline, 0 general)