Пример #1
0
    def summarize_run(self, lines, start_inv, end_inv):
        """ summarize and report on the summary """
        print('', file=self.outfile)
        print('*** RUN SUMMARY {} ***'.format(self.stream_id), file=self.outfile)
        print('', file=self.outfile)

        print('Value Stream Summary:', file=self.outfile)
        stream_ledger = Ledger(self.stream_id, 'RUN.TOTALS', None, None, self.market)
        for line in lines:
            stream_ledger.add_ledger(line.ledger)
        stream_summary = stream_ledger.output_summary(self.duration, self.outfile)

        print('', file=self.outfile)
        print('Production Line Summaries:', file=self.outfile)
        line_summary = []
        for line in lines:
            line.ledger.output_summary(self.duration, self.outfile)
            line_summary.append(line.line_identity())
            print("", file=self.outfile)

        print('Inventory Summaries:', file=self.outfile)
        start_inv.output_summary('Starting Assets', self.market, self.outfile)
        end_inv.output_summary('Ending Assets', self.market, self.outfile)
        net_inv = end_inv.diff(start_inv)
        net_inv.output_summary('Asset Changes', self.market, self.outfile)

        self.log_run({
            'net': '{:5.2f}'.format(stream_summary['net']),
            'fp': '-'.join(line_summary),
            'uptime': stream_summary['uptime'],
            'e-start': '{:5.2%}'.format(stream_summary['e-start']),
            'e-delta': '{:5.2%}'.format(stream_summary['e-delta']),
            'cdate': self.config_date,
            'id': self.stream_id
        })
Пример #2
0
    def __init__(self, stream_id, line_spec, config, master_clock):
        self.line_id = line_spec['line-id']
        self.linetype = line_spec['line-type']
        self.site_name = line_spec['site-name']
        self.site_efficiency = config['efficiency'][self.site_name]
        self.building = Buildings[self.linetype]
        self.building_count = line_spec[
            'buildingCount']  #TODO change camel to kebab case
        self.production = self._init_production(self.building_count)
        self.queue_identity = ''
        self.queue = self._init_production_queue(line_spec['queue'])
        self.ledger = Ledger(stream_id, self.line_id, self.linetype,
                             self.building_count, config['market'])
        self.inventory = config['inventory']
        self.sourcing_strategy = config['sourcing-strategy']
        self.essentials_strategy = config['essentials-strategy']
        self.non_essentials_strategy = config['non-essentials-strategy']

        # initialize workers and efficiency before initializing production
        self.worker_efficiency = 0.0
        self.workers = self._init_workers()
        self.worker_clock = DecrClock(Duration("0"))
        self._reset_workers(master_clock)
        self.efficiency = self._calc_line_efficiency()

        for bnum in range(0, self.building_count):
            self._set_next_recipe_active(master_clock, bnum)
            self.production[bnum]['producing'] = \
                self._start_next_recipe(master_clock, bnum, last_round_producing=False)

        print('{} {} initialized - {} buildings'.format(
            master_clock, self.line_id, self.building_count),
              file=config['outfile'])
Пример #3
0
    def __init__(self):
        # Used for the server to send outbound messages
        self.receivesocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # Used for the server to receive inbound messages
        # self.receivesocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
        #Used for the shuffle protocol
        self.randomnumber = (int)(random.random() * 1000)

        ### SOCKET CREATION ###
        # Creates a socket at localhost and next available 5000 client
        self.MY_IP = "127.0.0.1"
        self.MY_PORT = 5000
        self.CURRENT_GEN_POWERED = G
        connected = False
        while not connected:
            try:
                self.receivesocket.bind((self.MY_IP, self.MY_PORT))
                print("Connected to port ", self.MY_PORT)
                connected = True
            except:
                print("Port {} is probably in use".format(self.MY_PORT))
                self.MY_PORT += 1

        self.serverjoin()
        ### CLIENT CREATION ###
        self.MY_CLIENTS = {}
        self.newclient()

        self.known_clients = []
        ### LEDGER CREATION ###
        ### we can have the ledgers be in just the clients or just the servers or both
        self.MY_LEDGER = Ledger()

        self.current_round = "NONE"
Пример #4
0
def test_is_balance_gt_0():
    """ Test the is balance greater than 0 function """
    # Setup
    ledger_1 = Ledger('monies')
    user_2 = User('foo', ledger_1)
    user_1 = User('Anon', ledger_1)
    amount = 1

    # test base case for Anon
    user_1._credit = 100
    result = user_1.is_balance_gt_0(amount)
    assert result == True

    # test send amount for users
    amount = 101
    ledger_1.transactions_by_user['foo']['running_balance'] = []
    ledger_1.transactions_by_user['foo']['running_balance'].append(-101)
    assert user_2.is_balance_gt_0(amount) == True
    # test when user doesn't have enough money
    ledger_1.transactions_by_user['foo']['running_balance'].append(-102)
    assert user_2.is_balance_gt_0(amount) == False

    # test exception case
    ledger_1.transactions_by_user = Exception
    with raises(Exception):
        user_2.is_balance_gt_0(amount)
Пример #5
0
 def __init__(self, consensus, transport):
     assert isinstance(consensus, SCP)
     assert isinstance(transport, BaseTransport)
     super(Herder, self).__init__()
     self.consensus = consensus
     self.overlay = transport
     self.ledger = Ledger(self.consensus, self)
Пример #6
0
    def __init__(self, client_id):
        # I imagined wallets to be a list of dictionaries and each dictionary would have the following:
        # {
        #    "private_key": the key that will be used to sign new messages
        #    "public_key": the public key of the wallet
        #    "Reputation": the amount of reputation that the wallet has << all wallets have only one reputation, so this is unncessary
        # maybe we want to add another item in here for the wallet's identity
        # }
        self.wallets = []

        # start with 1 wallet with reputation=1 (feel free to change this)
        first_wallet = self.createwallet()
        # maybe we want to publish to the ledger? it's not an important issue, but we want to make sure that we can't just create random wallets out of nowhere
        self.wallets.append(first_wallet)

        # Client ID is just the ID of the port it's on, since we only have 1 client/port
        # Long standing private key
        # I think we're going to need a list of private keys that correspond to the individual wallets
        # ^that should be taken care of by self.wallets 
        self.client_id = client_id
        self.private_key = util.generatePrivateKey()
        self.public_key = util.generatePublicKey(self.private_key)

        self.my_ledger = Ledger()
        print("Client ID: ", self.client_id)
        print("Client public key: ", self.public_key)
        print("Client private key: ", self.private_key)
Пример #7
0
def test_mine_blocks_on_ledger(monkeypatch):
    """ test mining blocks """
    mock_ledger = Ledger('foo')
    mock_miner = Miner('foominer', mock_ledger)
    mock_miner_2 = Miner('barminer', mock_ledger)
    mock_miners = [mock_miner, mock_miner_2]
    mock_ledger.transaction_sequence = 256
    mine_blocks_on_ledger(mock_ledger, mock_miners, 10)
Пример #8
0
def main() -> None:
    # Prompt user for initial game info, including players and cards
    player = input('Enter your name: ').strip()
    opponents = input('Enter opponents (in order): ').strip().split()
    own_cards = [
        Card.parse(s) for s in input('Enter your cards: ').strip().split()
    ]

    # Prompt for opponents' hand sizes if necessary
    hand_sizes = [len(own_cards)]
    card_count = len(Card.__members__)
    player_count = 1 + len(opponents)
    if (card_count - 3) % player_count != 0:
        for opponent in opponents:
            hand_sizes.append(
                int(
                    input(
                        'Enter hand size for {}: '.format(opponent)).strip()))
    else:
        hand_sizes *= player_count

    # Set up the ledger and card/suggestion trackers
    all_players = [player] + opponents
    ledger = Ledger(all_players, hand_sizes, player, own_cards)
    suggestions = SuggestionTracker()
    shown_cards = ShownCardTracker(opponents)
    skipped_cards = SkippedCardTracker()

    # Main game loop
    did_solve = False
    while True:
        # Display the current game info
        print()
        print('#' * 79)
        print()
        for info in (suggestions, shown_cards, skipped_cards, ledger):
            print(info)
            print()

        # Check if we have a unique solution
        solution = ledger.solve()
        if solution is not None:
            print('*** Unique solution found! ***')
            print(', '.join(card.name for card in solution))
            if not did_solve:
                show_continue_prompt()
                did_solve = True
            print()

        # noinspection PyBroadException
        try:
            process_input(player, all_players, ledger, shown_cards,
                          skipped_cards, suggestions)
        except Exception:
            print()
            print('Whoops! Something went wrong...')
            traceback.print_exc()
            show_continue_prompt(1)
Пример #9
0
def test_send_exception():
    """ Test if send will throw an exception if no monies """
    # Setup
    ledger_1 = Ledger('monies')
    user_2 = User('foo', ledger_1)
    user_1 = User('oops', ledger_1)
    amount = 1
    with raises(Exception):
        user_1.send(amount, user_2)
Пример #10
0
def get_ledger():
    '''
    Creates a new ledger object based on the blockchain stored in
    redis.
    '''
    blockchain = read_from_redis()
    ledger = Ledger()
    ledger.load_blockchain(blockchain)
    return ledger
Пример #11
0
def test_queryset():

    lut = FunctionTrigger()

    ledger1 = pd.DataFrame({"Key1": ["A1", "B1"], "Key2": ["A2", "B2"]})

    ledger2 = pd.DataFrame({
        "Key1": ["A1", "D1", "E1"],
        "Key2": ["A2", "D2", "E2"]
    })

    cls1 = ConstantLedgerSource(ledger1)
    cls2 = ConstantLedgerSource(ledger2)

    led1 = Ledger(lut, cls1)
    led2 = Ledger(lut, cls2)

    lut.invoke()

    qs = QuerySet([led1, led2],
                  pd.DataFrame({
                      "Source": ["S0", "S0"],
                      "Query": ["Q0", "Q2"]
                  }))

    result_iterator = qs.join(3)

    results = result_iterator.as_list()

    target = pd.DataFrame({
        "Key1": ["A1", "B1", "D1", "A1", "B1", "D1", "E1", "E1"],
        "Key2": ["A2", "B2", "D2", "A2", "B2", "D2", "E2", "E2"],
        "Source": ["S0"] * 8,
        "Query": ["Q0", "Q2"] * 4
    })

    c = 0
    for result in results:
        row = target.loc[[c]]
        query = Query(**row.to_dict(orient="index")[c])
        assert query == result

        c += 1
Пример #12
0
 def __init__(self):
     AppWrapper.__init__(self)
     AppClient.__init__(self, wrapper=self)
     RequestManager.__init__(self)
     self.ledger = Ledger()
     try:
         signal.signal(signal.SIGINT, self.interruptHandler)
         signal.signal(signal.SIGTSTP, self.statusHandler)
     except AttributeError:
         console().warning("Warning. Unable to Bind a Signal Handler.")
Пример #13
0
def test_generate_initial_balance():
    """ test to make sure initial balance is on a user """
    # Setup
    mock_ledger = Ledger('foo')
    mock_anon = User('Anon', mock_ledger)
    mock_user = User('bar', mock_ledger)
    mock_users = [mock_user]
    initial_value = 1000
    generate_initial_balance(mock_anon, mock_users, initial_value, 10)
    assert mock_anon._credit == initial_value * len(mock_users) * 10
Пример #14
0
def reconcile(book, bank, externalData):
    """
	Reconciles a book statement with a bank statement, supported by extra external data.

	Args:
		book: a Ledger object 
		bank: a Ledger object
		externalData: extra data such as previous bank reconciliations
	Returns:
		Nothing 
	Raises:
		Nothing
	"""
    # Algorithm 1: One-to-one reconciliation
    ut.clean_columns(book.ledger)
    bankColumn = ut.clean_string('G/L Account Name')  # Filter by bank
    bookByBank = Ledger(
        ledger=book.ledger.query('{} == "{}"'.format(bankColumn, bank.name)))
    pivotTable = Ledger(ledger=ut.toPivotTable(bookByBank.ledger))
    vendorPivotTable = Ledger(
        ledger=pivotTable.ledger.drop('Customer', axis=1).copy())
    customerPivotTable = Ledger(
        ledger=pivotTable.ledger.drop('Vendor', axis=1).copy())
    appendMatchColumn(vendorPivotTable)
    appendMatchColumn(customerPivotTable)
    appendMatchColumn(bank)
    oneToOneMatch(vendorPivotTable, bank, ut.clean_string('Vendor'),
                  ut.clean_string('Debit'))
    oneToOneMatch(customerPivotTable, bank, ut.clean_string('Customer'),
                  ut.clean_string('Credit'))

    # After getting all pivot table matches, map it back up to the Book Ledger
    reversePivotMapping(book, bookByBank, vendorPivotTable, customerPivotTable)

    # Reflect changes in excel
    highlightMatches(book)
    highlightMatches(bank)
    ut.newExcel(vendorPivotTable, 'VendorByDate.xlsx', 'Reconciliation')
    ut.newExcel(customerPivotTable, 'CustomerByDate.xlsx', 'Reconciliation')
    highlightMatches(vendorPivotTable)
    highlightMatches(customerPivotTable)
    return
Пример #15
0
 def __init__(self, config):
     """ Open the socket for information retrieval at port <port>.
 
 Arguments:
 config -- a Config object that contains the configuration for the node
 """
     self._unpack(config)
     self.msg_size_mapping = self._create_msg_mapping()
     self.socket = self._create_socket_server()
     self.ledger = Ledger()
     self.threads = []
     self.thread_lock = threading.Lock()
Пример #16
0
def create_users_and_ledger():
    """ Create the users and ledger """
    monies = Ledger('monies')
    alice = User('Alice', monies)
    bob = Miner('Bob', monies)
    jon = User('Jon', monies)
    howard = User('Howard', monies)
    rocky = Miner('Rocky', monies)
    # using an anon user for creating an initial balance
    anon = User('Anon', monies)
    users = [alice, jon, howard]
    miners = [rocky, bob]
    return (monies, anon, users, miners)
Пример #17
0
    def import_data(account, currency, dry_run, gnucash_file, classifier):
        """
        Import data from a given file into a given gnucash file.

        Must have an account and a gnucash file defined.
        Is optional define the currency (default can setted in seupt.cfg - usiing BRL).
        Also, is optional define dry_run (default is **true**).
        """
        logging.info(
            Util.info("Importing data to ") +
            colored("{a}".format(a=account.name),
                    'yellow',
                    attrs=['bold', 'underline']) + Util.info("'s account"))
        Ledger(account, currency, dry_run, gnucash_file, classifier).write()
Пример #18
0
def readLedgers(cfg, type):
    """
	Reads a list of excel files from the list specified in cfg for the type specified

	Args:
		cfg: The config file that contains the list of files
		type: The name of the list that you want from cfg
	Returns:
		A list of Ledger objects that contain the data within the excel files
	Raises:
		Nothing
	"""

    # TODO: How do I do read-only access, and not have them open up when I call Book()?
    return [Ledger(file=file) for file in cfg['files'][type]]
Пример #19
0
def test_last_block_hash():
    """
    Test function for last block hash
    """
    # Setup
    mock_ledger = Ledger('monies')
    mock_miner = Miner('Mock', mock_ledger)
    mock_header = {'hdr': 'foo'}
    mock_ledger.block_count = 1
    mock_ledger.all_transactions.append(mock_header)
    mock_message = mock_ledger.all_transactions[0]['hdr']
    mock_nonce_attempts = 1000000
    mock_difficulty = 4
    mock_hash = cryptohash(mock_message, mock_nonce_attempts, mock_difficulty)
    # Assert
    assert mock_miner.last_block_hash() == mock_hash
Пример #20
0
def test_generate_transactions_on_ledger():
    """ test generating transactions """
    # Setup
    mock_ledger = Ledger('foo')
    mock_anon = User('Anon', mock_ledger)
    mock_user = User('bar', mock_ledger)
    mock_user_2 = User('foobar', mock_ledger)
    mock_miner = Miner('foominer', mock_ledger)
    mock_users = [mock_user, mock_user_2]
    mock_miners = [mock_miner]
    initial_value = 100
    generate_initial_balance(mock_anon, mock_users, initial_value, 10)
    ledger, users = generate_transactions_on_ledger(
        mock_ledger, mock_users, 3, mock_miners, 10)
    assert isinstance(ledger, Ledger)
    for user in users:
        assert isinstance(user, User)
Пример #21
0
def test_ledger():
    arr = np.array([["id 1a", "id 1b"], ["id 2a", "id 2b"]])
    ls = ConstantLedgerSource(arr)
    t = FunctionTrigger()

    ledger = Ledger(t, ls)
    t.invoke()

    assert np.array_equal(ledger.get(), arr)

    arr.put([2, 3], ["id 3a", "id 3b"])

    assert not np.array_equal(ledger.get(), arr)

    t.invoke()

    assert np.array_equal(ledger.get(), arr)
Пример #22
0
def test_app():
    l = Ledger()
    g1 = [2, 3, 3]
    g2 = [1, 2, 2]
    g3 = [1, 1, 1]
    gmax = 5  # range of each element
    g_length = 3  # length of the vector
    total_bound = 10  # range of the vector

    # Phase 0: initiate
    z1 = Zorro(l, 0, g_length, gmax, total_bound)
    z2 = Zorro(l, 1, g_length, gmax, total_bound)
    z3 = Zorro(l, 2, g_length, gmax, total_bound)

    print("Phase 1: commit...")
    # Phase 1: commit
    if (l.phase('c') == 1):
        z1.commit(g1)
        z2.commit(g2)
        z3.commit(g3)
    else:
        print("Phase 1 failed!")

    print("Phase 2: proof...")
    # Phase 2: generate zkp proofs
    if (l.phase('p') == 1):
        z1.prove()
        z2.prove()
        z3.prove()
    else:
        print("Phase 2 failed!")

    print("Phase 3: results...")
    # Phase 3: get results
    if (l.phase('r') == 1):
        print(z1.results())
        #z2.results()
        #z3.results()
    else:
        print("Phase 3 failed!")
Пример #23
0
def test_initializeLedgers_split1():
    expected = {
        0: {
            1: Ledger(5, 5),
            2: Ledger(5, 5)
        },
        1: {
            0: Ledger(5, 5),
            2: Ledger(5, 5)
        },
        2: {
            0: Ledger(5, 5),
            1: Ledger(5, 5)
        },
    }
    actual = initialLedgers('split', [10, 10, 10])
    assert actual == expected, "split ledger generation not as expected (1)"
Пример #24
0
def test_initializeLedgers_split2():
    expected = {
        0: {
            1: Ledger(5, 10),
            2: Ledger(15, 10)
        },
        1: {
            0: Ledger(10, 5),
            2: Ledger(15, 5)
        },
        2: {
            0: Ledger(10, 15),
            1: Ledger(5, 15)
        },
    }
    actual = initialLedgers('split', [20, 10, 30])
    assert actual == expected, "split ledger generation not as expected (2)"
Пример #25
0
def test_initializeLedgers_proportional2():
    expected = {
        0: {
            1: Ledger(1.2, 1),
            2: Ledger(9, 3)
        },
        1: {
            0: Ledger(1, 1.2),
            2: Ledger(27, 10.8)
        },
        2: {
            0: Ledger(3, 9),
            1: Ledger(10.8, 27)
        },
    }
    actual = initialLedgers('proportional', [4, 12, 36])
    assert actual == expected, "proportional ledger generation not as expected (2)"
Пример #26
0
def test_initializeLedgers_proportional1():
    expected = {
        0: {
            1: Ledger(5, 5),
            2: Ledger(5, 5)
        },
        1: {
            0: Ledger(5, 5),
            2: Ledger(5, 5)
        },
        2: {
            0: Ledger(5, 5),
            1: Ledger(5, 5)
        },
    }
    actual = initialLedgers('proportional', [10, 10, 10])
    assert actual == expected, "proportional ledger generation not as expected (1)"
Пример #27
0
def test_initializeLedgers_constant(c):
    expected = {
        0: {
            1: Ledger(c, c),
            2: Ledger(c, c)
        },
        1: {
            0: Ledger(c, c),
            2: Ledger(c, c)
        },
        2: {
            0: Ledger(c, c),
            1: Ledger(c, c)
        },
    }
    actual = initialLedgers('constant', [0] * 3, c=c)
    assert actual == expected, "actual ledger does not match expected with constant {}".format(
        c)
Пример #28
0
def test_updateLedgersIncremental_multipleRounds_heterogeneous_1():
    ledgers = initialLedgers('constant', [0] * 3, c=1)
    upload_rates = [10, 20, 30]
    actual_ledgers, _ = propagateN(20, 20, lambda x: x, upload_rates, ledgers)
    expected_ledgers = {
        0: {
            1: Ledger(11, 11),
            2: Ledger(11, 11)
        },
        1: {
            0: Ledger(11, 11),
            2: Ledger(11, 11)
        },
        2: {
            0: Ledger(11, 11),
            1: Ledger(11, 11)
        },
    }
    assert actual_ledgers == expected_ledgers, "actual ledgers do not match expected"
Пример #29
0
def test_updateLedgersIncremental_multipleRounds_homogeneous_3(c, n, u):
    ledgers = initialLedgers('constant', [0] * 3, c=c)
    upload_rates = [u] * 3
    actual_ledgers, _ = propagateN(1000 * n, 1000, lambda x: x, upload_rates,
                                   ledgers)
    expected_ledgers = {
        0: {
            1: Ledger(c + n * 500, c + n * 500),
            2: Ledger(c + n * 500, c + n * 500)
        },
        1: {
            0: Ledger(c + n * 500, c + n * 500),
            2: Ledger(c + n * 500, c + n * 500)
        },
        2: {
            0: Ledger(c + n * 500, c + n * 500),
            1: Ledger(c + n * 500, c + n * 500)
        },
    }
    assert actual_ledgers == expected_ledgers, "actual ledgers do not match expected"
Пример #30
0
logging.basicConfig(
    level=logging.INFO,
    format=f'{asctime_colored} {name_colored} {levelname_colored} %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.FileHandler('log/main.log', 'w')
    ])

if __name__ == '__main__':
    logging.info('Initializing')
    logging.info(f'Pairs to watch: {pairs_to_watch}')

    ledgers_by_pair = {}
    for pair in pairs_to_watch:
        ledgers_by_pair[pair] = Ledger(pair)

    api = krakenex.API()
    pair_watcher = PairWatcher(pairs_to_watch, api)
    price_logger = PriceLogger(pair_watcher.price_history)
    trader = Trader(pair_watcher.price_history, ledgers_by_pair)

    logging.info(f'Fetching price history')
    pair_watcher.get_price_history()

    logging.info(f'Starting watch')
    while True:
        try:
            price_logger.log_price()
            trader.auto_trade()
            pair_watcher.watch()