def parse(filename: str) -> table.Table: """Parse the NASDAQ ETFs list.""" tbl = table.read_csv(filename) outrows = [] for row in tbl: for regexp, issuer in [('Vanguard', 'Vanguard'), ('iShares', 'iShares'), ('PowerShares', 'PowerShares'), ('StateStreet', 'StateStreet')]: if re.search(regexp, row.name): outrows.append((row.symbol, issuer, row.name)) break return table.Table(['ticker', 'issuer', 'name'], [str, str, str], outrows)
def test_read_csv(): buf = io.StringIO( textwrap.dedent(""" units,currency 0.01,USD 0.02,CAD 0.03,AUD """)) t = table.read_csv(buf) e = table.Table(['units', 'currency'], [str, str], [[('0.01'), 'USD'], [('0.02'), 'CAD'], [('0.03'), 'AUD']]) assert t.columns == e.columns assert t.types == e.types assert t.rows == e.rows
def parse(filename: str) -> Table: """Parse the holdings file.""" with open(filename) as infile: next(infile) # Title row next(infile) # Date tow tbl = table.read_csv(infile) # Compute market value. tbl = utils.create_fraction_from_market_value(tbl, 'market_value') # I think it's all equity for that issuer AFAIK. tbl = tbl.create('asstype', lambda _: 'Equity') # Select what we got (not much). return tbl.select(['name', 'asstype', 'fraction'])
def read_exported_portfolio(filename: str, ignore_options: bool = False) -> table.Table: """Load a file in beancount.projects.export format.""" tbl = table.read_csv(filename) if ignore_options: tbl = tbl.filter(lambda row: row.assetcls != 'Options') tbl = (tbl.select([ 'account_abbrev', 'currency', 'cost_currency', 'export', 'number', 'issuer', 'price_file', 'rate_file' ]).rename(('account_abbrev', 'account')).map('price_file', safefloat).map( 'rate_file', safefloat).map('number', float).rename( ('number', 'quantity')).create('ticker', get_ticker).delete([ 'export', 'currency' ]).filter(lambda row: bool(row.ticker)).create( 'price', lambda row: row.price_file * row.rate_file).delete([ 'price_file', 'rate_file' ]).group( ('ticker', 'account', 'issuer', 'price'), 'quantity', sum).order(lambda row: (row.ticker, row.issuer, row. account, row.price)).checkall([ 'ticker', 'account', 'issuer', 'price', 'quantity' ])) return tbl
def read_regular_portfolio(filename: str): """Read the public file format for assets.""" with open(filename) as infile: assets = table.read_csv(infile) assets.checkall(['ticker', 'account', 'issuer', 'price', 'quantity']) return (assets.map('price', float).map('quantity', float))