def test_create_table_with_index(self): tuples = [ ('USD', '1111.00'), ('CAD', '1333.33'), ] table_object = table.create_table(tuples, [(0, 'Currency'), 1]) self.assertEqual( table.Table(columns=[0, 1], header=['Currency', 'Field 1'], body=[['USD', '1111.00'], ['CAD', '1333.33']]), table_object)
def main(): """Extract trades from metadata-annotated postings and report on them. """ logging.basicConfig(level=logging.INFO, format='%(levelname)-8s: %(message)s') parser = version.ArgumentParser(description=__doc__.strip()) parser.add_argument('filename', help='Beancount input filename') oparser = parser.add_argument_group('Outputs') oparser.add_argument( '-o', '--output', action='store', help="Filename to output results to (default goes to stdout)") oparser.add_argument('-f', '--format', default='text', choices=['text', 'csv'], help="Output format to render to (text, csv)") args = parser.parse_args() # Load the input file. entries, errors, options_map = loader.load_file(args.filename) # Get the list of trades. trades = extract_trades(entries) # Produce a table of all the trades. columns = ('units currency cost_currency ' 'buy_date buy_price sell_date sell_price pnl').split() header = [ 'Units', 'Currency', 'Cost Currency', 'Buy Date', 'Buy Price', 'Sell Date', 'Sell Price', 'P/L' ] body = [] for aug, red in trades: units = -red.posting.units.number buy_price = aug.posting.price.number sell_price = red.posting.price.number pnl = (units * (sell_price - buy_price)).quantize(buy_price) body.append([ -red.posting.units.number, red.posting.units.currency, red.posting.price.currency, aug.txn.date.isoformat(), buy_price, red.txn.date.isoformat(), sell_price, pnl ]) trades_table = table.Table(columns, header, body) # Render the table as text or CSV. outfile = open(args.output, 'w') if args.output else sys.stdout table.render_table(trades_table, outfile, args.format)