def _trim(self, book, balances, side):
new_book = []<EOL>for service, balance in balances.items():<EOL><INDENT>accumulation = <NUM_LIT:0><EOL>for order in book:<EOL><INDENT>if order[<NUM_LIT:2>].name == service.name:<EOL><INDENT>if side == '<STR_LIT>':<EOL><INDENT>size = order[<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>size = order[<NUM_LIT:1>] * order[<NUM_LIT:0>] <EOL><DEDENT>if size + accumulation <= balance:<EOL><INDENT>new_book.append(order)<EOL>accumulation += size<EOL><DEDENT>else:<EOL><INDENT>remaining = balance - accumulation<EOL>new_book.append([order[<NUM_LIT:0>], float("<STR_LIT>" % remaining), order[<NUM_LIT:2>]])<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return sorted(new_book, key=lambda x: x[<NUM_LIT:0>], reverse=side == '<STR_LIT>')<EOL>
>>> m = MultiOrderbook()
>>> book = [
    [7800, 1.1, GDAX()],
    [7805, 3.2, Poloniex()],
    [7810, 0.3, GDAX()],
    [7900, 7.0, GDAX()]
]
>>> m._trim(book, {GDAX(): 1.2, Poloniex(): 1.0}, 'crypto')
[[7800, 1.1, <Service: GDAX (0 in cache)>],
 [7805, 1.0, <Service: Poloniex (0 in cache)>],
 [7810, 0.1, <Service: GDAX (0 in cache)>]]