Exemplo n.º 1
0
 def test_FilenameToAssets(self):
     # Generate file
     filename = os.path.join(tempfile.gettempdir(), 'test.csv')
     assets = [generate_asset() for i in range(5)]
     objects_to_csv(objects=assets, filename=filename, clazz=Asset)
     assets = csv_filename_to_objects(filename, json_handler=json_to_asset)
     self.assertEqual(len(assets), 5)
     self.assertEqual(type(assets[0]), Asset)
     os.remove(filename)
Exemplo n.º 2
0
 def test_AssetsToCSV(self):
     filename = os.path.join(tempfile.gettempdir(), 'test.csv')
     assets = [generate_asset() for i in range(5)]
     objects_to_csv(objects=assets, filename=filename, clazz=Asset)
     # Read the file back out again
     with open(filename, 'r') as temp_file:
         data = temp_file.readlines()
     self.assertEqual(len(data), 6)  # 5 assets + header
     os.remove(filename)
def main():
    """ Main example """
    logging.info("--- SETTING UP IDENTIFIERS ---")
    asset_manager_id = random.randint(1, 2**31-1)
    asset_manager_party_id = 'AMID' + str(asset_manager_id)
    traders = [('TJ', 'Joe', 'Trader'), ('GG', 'Gordon', 'Gekko'), ('AP', 'Patrick', 'Bateman')]
    brokers = [('BROKER1', 'Best Brokers Inc.'), ('BROKER2', 'World Broker')]
    csv_path = tempfile.gettempdir()  # Modify this to read and write from a different directory
    no_of_books = 5
    no_of_equities = 10
    no_of_transactions = 20

    logging.info("--- CREATING BOOKS CSV FILE ---")
    books = []
    asset_book_ids = ['BOOK' + str(i+1) for i in range(no_of_books)]
    for book_id in asset_book_ids:
        book = create_book(asset_manager_id=asset_manager_id, book_id=book_id, party_id=asset_manager_party_id,
                           owner_id=random.choice([trader[0] for trader in traders]))
        books.append(book)
    for broker in brokers:
        book = create_book(asset_manager_id=asset_manager_id, book_id=broker[0], party_id=broker[0])
        books.append(book)
    books_filename = os.path.join(csv_path, 'books.csv')
    logging.info("--- WRITING TO %s ---", books_filename)
    objects_to_csv(objects=books, clazz=Book, filename=books_filename)

    logging.info("--- CREATING PARTIES CSV FILE ---")
    parties = []
    for trader_id, first_name, surname in traders:
        individual = Individual(asset_manager_id=asset_manager_id, party_id=trader_id, given_names=first_name,
                                surname=surname)
        parties.append(individual)
    for broker_id, broker_name in brokers:
        broker = Broker(asset_manager_id=asset_manager_id, party_id=broker_id, description=broker_name)
        parties.append(broker)
    parties_filename = os.path.join(csv_path, 'parties.csv')
    logging.info("--- WRITING TO %s ---", parties_filename)
    objects_to_csv(objects=parties, clazz=Party, filename=parties_filename)

    logging.info("--- CREATING EQUITIES CSV FILE ---")
    assets = []
    asset_ids = ['EQ' + str(i+1) for i in range(no_of_equities)]
    for asset_id in asset_ids:
        asset = create_equity(asset_manager_id=asset_manager_id, asset_id=asset_id)
        assets.append(asset)
    equities_filename = os.path.join(csv_path, 'equities.csv')
    logging.info("--- WRITING TO %s ---", equities_filename)
    objects_to_csv(objects=assets, clazz=Equity, filename=equities_filename)

    logging.info("--- CREATING TRANSACTIONS CSV FILE ---")
    transactions = []
    for i in range(no_of_transactions):
        transaction_id = str(i+1)
        asset_id = random.choice(asset_ids)
        asset_book_id = random.choice(asset_book_ids)
        cpty_book_id = random.choice(['BROKER1', 'BROKER2'])
        transaction = generate_transaction(asset_manager_id=asset_manager_id, transaction_id=transaction_id,
                                           asset_id=asset_id,
                                           asset_book_id=asset_book_id, counterparty_book_id=cpty_book_id)
        transactions.append(transaction)
    transactions_filename = os.path.join(csv_path, 'transactions.csv')
    logging.info("--- WRITING TO %s ---", transactions_filename)
    objects_to_csv(objects=transactions, clazz=Transaction, filename=transactions_filename)

    logging.info("--- READING BOOKS CSV AND CREATING ---")
    books = csv_filename_to_objects(filename=books_filename, json_handler=json_to_book)
    for book in books:
        books_interface.new(book)

    logging.info("--- READING PARTIES CSV AND CREATING ---")
    parties = csv_filename_to_objects(filename=parties_filename, json_handler=json_to_party)
    for party in parties:
        parties_interface.new(party)

    logging.info("--- READING EQUITIES CSV AND CREATING ---")
    equities = csv_filename_to_objects(filename=equities_filename, json_handler=json_to_asset)
    for equity in equities:
        assets_interface.new(equity)

    logging.info("--- READING TRANSACTIONS CSV AND CREATING ---")
    transactions = csv_filename_to_objects(filename=transactions_filename, json_handler=json_to_transaction)
    for transaction in transactions:
        transaction_interface.new(transaction)