-
-
Save moshez/783d67a8e6f72b09a36a45cdefa5f110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deepdict(): | |
return collections.defaultdict(deepdict) | |
def deepdictify(tupdict): | |
res = deepdict() | |
for key, val in tupdict.items(): | |
tempres = res | |
for kpart in key[:-1]: | |
tempres = tempres[kpart] | |
tempres[key[-1]] = val | |
@attr.s | |
class Book(object): | |
shop_label = attr.ib() | |
cell_label = attr.ib() | |
book_id = attr.ib() | |
count = attr.ib() | |
def build_book_inventory(book_ids, shops): | |
shop_labels = [shop['label'] for shop in shops] | |
book_list = Persistency.books_table.read( | |
shops=shop_labels, | |
books=book_ids) | |
inventory = {} | |
for item in book_list: | |
key = tuple(map(functools.partial(operator.getitem, item), | |
('shop_label', 'cell_label', 'book_id'))) | |
inventory[key] = item['count'] | |
return deepdictify(inventory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment