Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- def query(c):
- return sum(c.itervalues())
- def merge(c1, c2):
- return {k: max(c1.get(k, 0), c2.get(k, 0)) for k in set(c1).union(c2)}
- class tx(object):
- def __init__(self, id):
- self.id = id
- def add(self, c, n=1):
- assert n >= 0
- c = dict(c)
- c.setdefault(self.id, 0)
- c[self.id] += n
- return c
- c1 = dict(tx1=7)
- print "c1", query(c1)
- c2 = tx(2).add(c1)
- print "c2", query(c2)
- c3 = tx(3).add(c1)
- print "c3", query(c3)
- c4 = merge(c2, c3)
- print "c4", query(c4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement