Exemplo n.º 1
0
def optimize(
    yml_file,
    draw,
    show_balances,
    basic,
):
    if not yml_file:
        yml_file = stdin
    ledger = yaml.load(yml_file)
    optimized_graph, optimized_ledger = optimize_debts_from_ledger(ledger, basic=basic)
    yaml.dump(optimized_ledger, stdout, default_flow_style=False)
    if draw:
        import matplotlib.pyplot as plt
        pos = nx.spring_layout(optimized_graph)
        nx.draw(
            optimized_graph,
            pos=pos,
            node_color='white',
            node_size=3000,
            with_labels=True
        )
        edge_labels = {
            (u, v): format_amount(data['amount'])
            for u, v, data in optimized_graph.edges_iter(data=True)
        }
        nx.draw_networkx_edge_labels(
            optimized_graph,
            pos=pos,
            edge_labels=edge_labels
        )
        if show_balances:
            node_labels = {
                node: format_amount(data['orig_balance'])
                for node, data in optimized_graph.nodes_iter(data=True)
                }
            elevated_pos = {
                key: (x, y + 5 / 100)
                for key, (x, y) in pos.items()
                }
            nx.draw_networkx_labels(
                optimized_graph,
                pos=elevated_pos,
                labels=node_labels,
                font_weight='bold'
            )
        plt.show()
Exemplo n.º 2
0
def build_transactions_from_ledger(ledger: dict):
    transactions = []
    for debtor, debts in ledger.items():
        for creditor, amount in debts.items():
            tx = dict(
                debtor=debtor,
                creditor=creditor,
                amount=format_amount(amount)
            )
            transactions.append(tx)
    return transactions
Exemplo n.º 3
0
def optimize(
    yml_file,
    draw,
    show_balances,
    basic,
):
    if not yml_file:
        yml_file = stdin
    ledger = yaml.load(yml_file)
    optimized_graph, optimized_ledger = optimize_debts_from_ledger(ledger,
                                                                   basic=basic)
    yaml.dump(optimized_ledger, stdout, default_flow_style=False)
    if draw:
        import matplotlib.pyplot as plt
        pos = nx.spring_layout(optimized_graph)
        nx.draw(optimized_graph,
                pos=pos,
                node_color='white',
                node_size=3000,
                with_labels=True)
        edge_labels = {(u, v): format_amount(data['amount'])
                       for u, v, data in optimized_graph.edges_iter(data=True)}
        nx.draw_networkx_edge_labels(optimized_graph,
                                     pos=pos,
                                     edge_labels=edge_labels)
        if show_balances:
            node_labels = {
                node: format_amount(data['orig_balance'])
                for node, data in optimized_graph.nodes_iter(data=True)
            }
            elevated_pos = {
                key: (x, y + 5 / 100)
                for key, (x, y) in pos.items()
            }
            nx.draw_networkx_labels(optimized_graph,
                                    pos=elevated_pos,
                                    labels=node_labels,
                                    font_weight='bold')
        plt.show()