Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. def deepdict():
  2. return collections.defaultdict(deepdict)
  3.  
  4. def deepdictify(tupdict):
  5. res = deepdict()
  6. for key, val in tupdict.items():
  7. tempres = res
  8. for kpart in key[:-1]:
  9. tempres = tempres[kpart]
  10. tempres[key[-1]] = val
  11.  
  12. @attr.s
  13. class Book(object):
  14. shop_label = attr.ib()
  15. cell_label = attr.ib()
  16. book_id = attr.ib()
  17. count = attr.ib()
  18.  
  19. def build_book_inventory(book_ids, shops):
  20. shop_labels = [shop['label'] for shop in shops]
  21. book_list = Persistency.books_table.read(
  22. shops=shop_labels,
  23. books=book_ids)
  24. inventory = {}
  25. for item in book_list:
  26. key = tuple(map(functools.partial(operator.getitem, item),
  27. ('shop_label', 'cell_label', 'book_id')))
  28. inventory[key] = item['count']
  29. return deepdictify(inventory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement