Skip to content

Instantly share code, notes, and snippets.

@shutej
Created January 31, 2015 17:34
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 shutej/2c327545dfcfe6a138ca to your computer and use it in GitHub Desktop.
Save shutej/2c327545dfcfe6a138ca to your computer and use it in GitHub Desktop.
#!/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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment