Exemplo n.º 1
0
def test_extract_numbers():
    assert "160" == extract_numbers("160")
    assert "1694" == extract_numbers("1,694")
    assert "1806" == extract_numbers("1,806 원")

    assert 170 == extract_numbers("170", int)
    assert 3925321 == extract_numbers("3,925,321", int)

    assert 150.25 == extract_numbers("150.25", float)

    with pytest.raises(TypeError):
        extract_numbers(1)

    with pytest.raises(TypeError):
        extract_numbers(b"\x00")
Exemplo n.º 2
0
def test_extract_numbers():
    assert '160' == extract_numbers('160')
    assert '1694' == extract_numbers('1,694')
    assert '1806' == extract_numbers('1,806 원')

    assert 170 == extract_numbers('170', int)
    assert 3925321 == extract_numbers('3,925,321', int)

    assert 150.25 == extract_numbers('150.25', float)

    with pytest.raises(TypeError):
        extract_numbers(1)

    with pytest.raises(TypeError):
        extract_numbers(b'\x00')
Exemplo n.º 3
0
def import_sp500():
    app = create_app(__name__)
    with app.app_context():
        account_checking = Account.get(id=1001)
        account_sp500 = Account.get(id=7001)
        asset_krw = Asset.query.filter_by(name='KRW').first()
        asset_sp500 = Asset.query.filter_by(name='KB S&P500').first()

        with open('sample-data/sp500.csv') as fin:
            for line in fin:
                cols = line.split()
                if len(cols) != 5:
                    continue
                date = parse_date(cols[0], '%Y.%m.%d')
                _type = cols[1]
                quantity_krw, quantity_sp500 = \
                    [int(extract_numbers(v)) for v in cols[2:4]]

                print(cols)

                withdraw = _type == '일반입금'

                with Transaction.create() as t:
                    if withdraw:
                        Record.create(
                            created_at=date, account=account_checking,
                            asset=asset_krw, quantity=-quantity_krw,
                            transaction=t)
                    Record.create(
                        created_at=date, account=account_sp500,
                        asset=asset_sp500, quantity=quantity_sp500,
                        transaction=t)

        print(account_sp500.net_worth(make_date('2016-02-25'), target_asset=asset_krw))
Exemplo n.º 4
0
def import_sp500_records():
    """Import S&P500 fund sample data. Expects a tab seprated value document.
    """
    app = create_app(__name__)
    app.app_context().push()

    account_checking = Account.get(id=1001)
    account_sp500 = Account.get(id=7001)
    asset_krw = Asset.query.filter_by(name='KRW').first()
    asset_sp500 = Asset.query.filter_by(name='KB S&P500').first()

    # Expected number of columns
    expected_col_count = 6

    with open('sample-data/sp500.csv') as fin:
        # Skip the first row (headers)
        headers = next(fin)
        col_count = len(headers.split())
        if col_count != expected_col_count:
            raise Exception(
                'Expected number of columns = {}, '
                'actual number of columns = {}'.format(
                    expected_col_count, col_count))

        for line in fin:
            cols = line.split('\t')
            if len(cols) != expected_col_count:
                continue
            date = parse_date(cols[0], '%Y.%m.%d')
            _type = cols[1]
            quantity_krw, quantity_sp500 = \
                [int(extract_numbers(v)) for v in cols[3:5]]

            log.info(', '.join([c.strip() for c in cols]))

            if not (_type == '일반입금' or _type == '일반신규'):
                log.info('Record type \'{}\' will be ignored', _type)
                continue

            with Transaction.create() as t:
                # NOTE: The actual deposit date and the buying date generally
                # differ by a few days. Need to figure out how to parse this
                # properly from the raw data.
                try:
                    Record.create(
                        created_at=date, account=account_checking,
                        asset=asset_krw, quantity=-quantity_krw,
                        transaction=t)
                except IntegrityError:
                    log.warn('Identical record exists')
                    db.session.rollback()

                try:
                    Record.create(
                        created_at=date, account=account_sp500,
                        asset=asset_sp500, quantity=quantity_sp500,
                        transaction=t)
                except IntegrityError:
                    log.warn('Identical record exists')
                    db.session.rollback()
Exemplo n.º 5
0
def import_sp500_records():
    """Import S&P500 fund sample data. Expects a tab seprated value document.
    """
    app = create_app(__name__)
    app.app_context().push()

    account_checking = Account.get(id=1001)
    account_sp500 = Account.get(id=7001)
    asset_krw = Asset.query.filter_by(name="KRW").first()
    asset_sp500 = Asset.query.filter_by(name="KB S&P500").first()

    # Expected number of columns
    expected_col_count = 6

    with open("sample-data/sp500.csv") as fin:
        # Skip the first row (headers)
        headers = next(fin)
        col_count = len(headers.split())
        if col_count != expected_col_count:
            raise Exception("Expected number of columns = {}, "
                            "actual number of columns = {}".format(
                                expected_col_count, col_count))

        for line in fin:
            cols = line.split("\t")
            if len(cols) != expected_col_count:
                continue
            date = parse_date(cols[0], "%Y.%m.%d")
            _type = cols[1]
            quantity_krw, quantity_sp500 = [
                int(extract_numbers(v)) for v in cols[3:5]
            ]

            log.info(", ".join([c.strip() for c in cols]))

            if not (_type == "일반입금" or _type == "일반신규"):
                log.info("Record type '{}' will be ignored", _type)
                continue

            with Transaction.create() as t:
                # NOTE: The actual deposit date and the buying date generally
                # differ by a few days. Need to figure out how to parse this
                # properly from the raw data.
                try:
                    deposit(account_checking, asset_krw, -quantity_krw, date,
                            t)
                except IntegrityError:
                    log.warn("Identical record exists")
                    db.session.rollback()

                try:
                    deposit(account_sp500, asset_sp500, quantity_sp500, date,
                            t)
                except IntegrityError:
                    log.warn("Identical record exists")
                    db.session.rollback()
Exemplo n.º 6
0
def test_extract_numbers():
    assert '160' == extract_numbers('160')
    assert '1694' == extract_numbers('1,694')
    assert '1806' == extract_numbers('1,806 원')

    assert 170 == extract_numbers('170', int)
    assert 3925321 == extract_numbers('3,925,321', int)

    assert 150.25 == extract_numbers('150.25', float)
Exemplo n.º 7
0
def import_sp500():
    app = create_app(__name__)
    with app.app_context():
        account_checking = Account.get(id=1001)
        account_sp500 = Account.get(id=7001)
        asset_krw = Asset.query.filter_by(name='KRW').first()
        asset_sp500 = Asset.query.filter_by(name='KB S&P500').first()

        with open('sample-data/sp500.csv') as fin:
            for line in fin:
                cols = line.split()
                if len(cols) != 5:
                    continue
                date = parse_date(cols[0], '%Y.%m.%d')
                _type = cols[1]
                quantity_krw, quantity_sp500 = \
                    [int(extract_numbers(v)) for v in cols[2:4]]

                print(cols)

                withdraw = _type == '일반입금'

                with Transaction.create() as t:
                    if withdraw:
                        Record.create(created_at=date,
                                      account=account_checking,
                                      asset=asset_krw,
                                      quantity=-quantity_krw,
                                      transaction=t)
                    Record.create(created_at=date,
                                  account=account_sp500,
                                  asset=asset_sp500,
                                  quantity=quantity_sp500,
                                  transaction=t)

        print(
            account_sp500.net_worth(make_date('2016-02-25'),
                                    target_asset=asset_krw))
Exemplo n.º 8
0
 def etnc(soup, class_, f):
     return f(extract_numbers(extract_div_text(soup, class_=class_)))
Exemplo n.º 9
0
 def etni(soup, id, f):
     return f(extract_numbers(extract_div_text(soup, id=id)))