Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. #!/usr/bin/env python2.7
  2.  
  3. def query(c):
  4. return sum(c.itervalues())
  5.  
  6. def merge(c1, c2):
  7. return {k: max(c1.get(k, 0), c2.get(k, 0)) for k in set(c1).union(c2)}
  8.  
  9. class tx(object):
  10. def __init__(self, id):
  11. self.id = id
  12.  
  13. def add(self, c, n=1):
  14. assert n >= 0
  15. c = dict(c)
  16. c.setdefault(self.id, 0)
  17. c[self.id] += n
  18. return c
  19.  
  20. c1 = dict(tx1=7)
  21. print "c1", query(c1)
  22.  
  23. c2 = tx(2).add(c1)
  24. print "c2", query(c2)
  25.  
  26. c3 = tx(3).add(c1)
  27. print "c3", query(c3)
  28.  
  29. c4 = merge(c2, c3)
  30. print "c4", query(c4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement