[Python-ideas] Multi-index Containers Library

David Wilson dw+python-ideas at hmmz.org
Mon Dec 1 16:03:28 CET 2014


> before it would be useful to include in the stdlib, along with some
> motivating use cases

One example would be a continuous auction, like a stock exchange order
book. In that case, efficient enumeration is desirable by all of
account ID, order ID, or (price, time).

    book = IndexedList()
    book.add_unique_index('order_id', lambda o: o.id)
    book.add_index('account_id', lambda o: o.account_id)
    book.add_index('price_time', lambda o: (o.price, o.time))

    def add_order(order):
        book.append(order)

    def del_order(order):
        del book.indices['order_id'][order.id]

    def close_account(account_id):
        del book.indices['account_id'][account_id]

    def iter_matches(limit_price):
        for (price, time), order in book.indices['price_time']:
            if price > limit_price:
                break
            yield order

Again it's not very clear how the magical 'indices' object slices would
work.


David


More information about the Python-ideas mailing list