Skip to content

Instantly share code, notes, and snippets.

@moshez
Forked from afroisalreadyinu/sample.py
Last active August 25, 2016 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moshez/783d67a8e6f72b09a36a45cdefa5f110 to your computer and use it in GitHub Desktop.
Save moshez/783d67a8e6f72b09a36a45cdefa5f110 to your computer and use it in GitHub Desktop.
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