Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
suminb committed Jul 3, 2016
1 parent 853f62e commit 91058f4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 56 deletions.
9 changes: 5 additions & 4 deletions finance/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

from finance import create_app
from finance.importers import \
import_8percent_data, \
import_stock_values as import_stock_values_ # Avoid name clashes
from finance.models import * # noqa
from finance.providers import _8Percent, Kofia, Yahoo
from finance.utils import (
extract_numbers, import_8percent_data, insert_asset, insert_record,
extract_numbers, insert_asset, insert_record,
insert_stock_record, parse_date, parse_stock_data)


Expand All @@ -22,7 +23,7 @@

def insert_accounts(user):
yield Account.create(
id=1001, type='checking', name='Shinhan Checking', user=user)
id=1001, type='checking', name='신한은행 입출금', user=user)
yield Account.create(
id=9001, type='investment', name='Woori Gold Banking', user=user)
yield Account.create(
Expand Down Expand Up @@ -209,9 +210,9 @@ def import_8percent(filename):
with app.app_context():
with open(filename) as fin:
raw = fin.read()
account_8p = Account.query.get(8001)
account_8p = Account.query.filter(Account.name == '8퍼센트').first()
account_checking = Account.query.filter(
Account.name == 'Shinhan Checking').first()
Account.name == '신한은행 입출금').first()
asset_krw = Asset.query.filter(Asset.name == 'KRW').first()

parsed_data = provider.parse_data(raw)
Expand Down
48 changes: 48 additions & 0 deletions finance/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,54 @@
from finance.models import Asset, AssetValue, get_asset_by_stock_code, \
Granularity
from finance.providers.provider import AssetValueProvider
from finance.utils import DictReader


def import_8percent_data(parsed_data, account_checking, account_8p, asset_krw):
"""Import 8percent `AssetValue`s and `Record`s altogether."""
from finance.models import Asset, AssetType, AssetValue, Record, \
Transaction

assert account_checking
assert account_8p
assert asset_krw

parsed_data = DictReader(parsed_data)
asset_data = {
'started_at': parsed_data.started_at.isoformat()
}
keys = ['annual_percentage_yield', 'amount', 'grade', 'duration',
'originator']
for key in keys:
asset_data[key] = parsed_data[key]

asset_8p = Asset.create(name=parsed_data.name, type=AssetType.p2p_bond,
data=asset_data)
remaining_value = parsed_data.amount
started_at = parsed_data.started_at

with Transaction.create() as t:
Record.create(
created_at=started_at, transaction=t, account=account_checking,
asset=asset_krw, quantity=-remaining_value)
Record.create(
created_at=started_at, transaction=t, account=account_8p,
asset=asset_8p, quantity=1)
AssetValue.create(
evaluated_at=started_at, asset=asset_8p,
base_asset=asset_krw, granularity='1day', close=remaining_value)

for record in parsed_data.records:
date, principle, interest, tax, fees = record
returned = principle + interest - (tax + fees)
remaining_value -= principle
with Transaction.create() as t:
Record.create(
created_at=date, transaction=t,
account=account_checking, asset=asset_krw, quantity=returned)
AssetValue.create(
evaluated_at=date, asset=asset_8p,
base_asset=asset_krw, granularity='1day', close=remaining_value)


@typed
Expand Down
50 changes: 0 additions & 50 deletions finance/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,52 +171,6 @@ def insert_stock_record(data: dict):
)


def import_8percent_data(parsed_data, account_checking, account_8p, asset_krw):
from finance.models import Asset, AssetType, AssetValue, Record, \
Transaction

assert account_checking
assert account_8p
assert asset_krw

parsed_data = DictReader(parsed_data)
asset_data = {
'started_at': parsed_data.started_at.isoformat()
}
keys = ['annual_percentage_yield', 'amount', 'grade', 'duration',
'originator']
for key in keys:
asset_data[key] = parsed_data[key]

asset_8p = Asset.create(name=parsed_data.name, type=AssetType.p2p_bond,
data=asset_data)
remaining_value = parsed_data.amount
started_at = parsed_data.started_at

with Transaction.create() as t:
Record.create(
created_at=started_at, transaction=t, account=account_checking,
asset=asset_krw, quantity=-remaining_value)
Record.create(
created_at=started_at, transaction=t, account=account_8p,
asset=asset_8p, quantity=1)
AssetValue.create(
evaluated_at=started_at, asset=asset_8p,
base_asset=asset_krw, granularity='1day', close=remaining_value)

for record in parsed_data.records:
date, principle, interest, tax, fees = record
returned = principle + interest - (tax + fees)
remaining_value -= principle
with Transaction.create() as t:
Record.create(
created_at=date, transaction=t,
account=account_checking, asset=asset_krw, quantity=returned)
AssetValue.create(
evaluated_at=date, asset=asset_8p,
base_asset=asset_krw, granularity='1day', close=remaining_value)


def insert_asset(row, data=None):
"""Parses a comma separated values to fill in an Asset object.
(type, name, description)
Expand Down Expand Up @@ -258,10 +212,6 @@ def insert_record(row, account, asset, transaction):
created_at=created_at, category=category, quantity=quantity)


class AssetValueImporter(object):
pass


class DictReader(object):
def __init__(self, value):
if not isinstance(value, dict):
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def account_savings(request, db):
return account


@pytest.fixture(scope='function')
def account_8p(request, db):
account = Account.create(type='virtual', name='8퍼센트')
request.addfinalizer(partial(teardown, db=db, record=account))
return account


@pytest.fixture(scope='function')
def account_hf(request, db):
account = Account.create(type='virtual', name='어니스트펀드')
Expand Down
5 changes: 3 additions & 2 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def test_insert_test_data_all():
assert result.exit_code == 0


def test_import_8percent():
def test_import_8percent(account_checking, account_8p, asset_krw):
runner = CliRunner()
result = runner.invoke(import_8percent, ['tests/data/8percent-829.html'])
result = runner.invoke(import_8percent, ['tests/data/8percent-829.html'],
catch_exceptions=False)
assert result.exit_code == 0


Expand Down

0 comments on commit 91058f4

Please sign in to comment.