) tmp_transaction = example_schema.AccountingTransaction(transaction=trans_details, journal_list=trans_journals) for i in range(0,n): tmp_transaction.transaction.narrative='Random transaction '+str(i) x = random.randint(-500, 500) z = decimal.Decimal(x).scaleb(-2) tmp_transaction.journal_list[0].amount = z tmp_transaction.journal_list[0].account = random.randint(1000,1020) tmp_transaction.journal_list[1].amount = -z tmp_transaction.journal_list[1].account = random.randint(1000,1020) tmp_transaction._insert_new(cursor) if __name__ == '__main__': conn = utils.process_command_line('Demonstrate pyxact transactions with hooks') cursor = conn.cursor() example_schema.create_example_schema(cursor) example_schema.populate_example_schema(cursor) # Now we are going to select the first transaction created by populate_example_schema rev_trans = ReverseTransaction(tid=1) rev_trans._context_select(cursor) # rev_trans will have been normalized at the end of the context_select - i.e. the sign of the # amounts in the journals will have been flipped rev_trans._insert_new(cursor) # As insert_new rather than insert_existing has been used, updated values for tid and
class TrafficLight(enum.Enum): RED = 1 AMBER = 2 GREEN = 3 class TrafficLightField(pyxact.enums.EnumField): enum_type = TrafficLight enum_sql = 'trafficlight' class EnumTestTable(tables.SQLTable, table_name='enum_test'): tid = fields.IntField(context_used='tid') traffic_light = TrafficLightField() if __name__ == '__main__': conn = utils.process_command_line( 'Demonstrate usage of pyxact Enum support') cursor = conn.cursor() # example_schema.create_example_schema(cursor) # example_schema.populate_example_schema(cursor) if utils.DATABASE_USED == 'PostgreSQL': cursor.execute('DROP TYPE IF EXISTS trafficlight CASCADE;') pyxact.psycopg2.Psycopg2Dialect.create_enum_type( cursor, TrafficLight, 'trafficlight') cursor.execute('COMMIT;')
# database. The check_version_info method of a schema will do this and raise an exception if # there is a version mis-match. def populate_example_schema(cursor): '''Add some sample data to the example 'accounting' schema.''' # Note that SQLTransactions issue 'BEGIN TRANSACTION' and 'COMMIT TRANSACTION' themselves test_transaction1._insert_new(cursor) test_transaction2._insert_new(cursor) if __name__ == '__main__': conn = utils.process_command_line( 'Demonstrate usage of pyxact with a simple example schema') cursor = conn.cursor() create_example_schema(cursor) populate_example_schema(cursor) # It is possible to read existing AccountingTransactions back into Python. Simply set up the # context fields appropriately and call context_select to fill in the rest of the data. new_trans = AccountingTransaction(tid=2) new_trans._context_select(cursor) assert new_trans.journal_list[2].account == 1003 assert new_trans.transaction.narrative == 'Example usage of pyxact'
record_json = json.dumps(example_schema.test_transaction1.transaction, cls=serialize_json.PyxactEncoder, indent=4) recordlist_json = json.dumps(example_schema.test_transaction1.journal_list, cls=serialize_json.PyxactEncoder, indent=4) transaction_json = json.dumps(example_schema.test_transaction1, cls=serialize_json.PyxactEncoder, indent=4) # PyxactEncoder is a customised JSON encoder class that knows how to serialise SQLRecord, # SQLRecordList and SQLTransaction types into JSON. custom_decoder = serialize_json.PyxactDecoder() custom_decoder.register_sqlschema(example_schema.accounting) custom_decoder.register_sqlrecordlist(example_schema.JournalList) custom_decoder.register_sqltransaction(example_schema.AccountingTransaction) # PyxactDecoder can turn the output of PyxactEncoder back into Python objects, provided the relevant # classes have been registered with it. if __name__ == '__main__': conn = utils.process_command_line( 'Demonstrate usage of pyxact JSON serialisation') cursor = conn.cursor() example_schema.create_example_schema(cursor) example_schema.populate_example_schema(cursor)
tid = fields.IntField() total_transaction_count = fields.IntField(query=TransactionCountQuery) row_count = transactions.SQLTransactionField(JournalRowCountResultTID) # The QueryTransaction class combines two queries. When the tid context field is set and the # _context_select_sql method is called on the transaction, the queries will be updated and the # appropriate records from the view will be retrieved. (Note that in this case the # TransactionCountQuery doesn't actually depend on the tid set, but the JournalRowCountResultQueryTID # underneath the JournalRowCountResultTID does). The purpose of being able to do this is that all of # the queries (and any view retrieval) will happen in a single transaction, so consistent results # will be obtained even where there are concurrent writes to the database. if __name__ == '__main__': conn = utils.process_command_line( 'Demonstrate usage of pyxact with simple queries') cursor = conn.cursor() example_schema.create_example_schema(cursor) example_schema.populate_example_schema(cursor) # First we are going to count the number of transactions in the example schema... transaction_count_query = TransactionCountQuery() transaction_count_query._execute(cursor) assert transaction_count_query._result_singlevalue(cursor) == 2 # Now we are going to count the number of journal rows for all transactions created by all # creators (all creators are selected as the 'created_by' pattern is the SQL wildcard '%'). journal_row_count_query_creator = JournalRowCountResultCreator( creator_pattern='%') journal_row_count_query_creator._refresh(cursor)
source_out['source'] += source source_out['len'] += source_len source_out['attn'] += attn.tolist() source_out['ids'] += ids sales_out['label'] += log_sales sales_out['pred'] += sales_hat.tolist() price_out['label'] += price price_out['pred'] += price_hat.tolist() shop_out['label'] += shop shop_out['pred'] += shop_hat.tolist() category_out['label'] += category category_out['pred'] += category_hat.tolist() loss_out += loss if args.output is not None: print '\nINFO: dumping output to ', args.output write_pickle(source_out, args.output) print 'INFO: done \(^o^)/' if __name__ == '__main__': args = utils.process_command_line() main(args)