def test_records(): table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 3)) actual = records(table) # access items it = iter(actual) o = next(it) eq_('a', o['foo']) eq_(1, o['bar']) o = next(it) eq_('b', o['foo']) eq_(2, o['bar']) # access attributes it = iter(actual) o = next(it) eq_('a', o.foo) eq_(1, o.bar) o = next(it) eq_('b', o.foo) eq_(2, o.bar) # access with get() method it = iter(actual) o = next(it) eq_('a', o.get('foo')) eq_(1, o.get('bar')) eq_(None, o.get('baz')) eq_('qux', o.get('baz', default='qux'))
def recordtree(table, start='start', stop='stop'): """ Construct an interval tree for the given table, where each node in the tree is a row of the table represented as a record object. """ import intervaltree getstart = attrgetter(start) getstop = attrgetter(stop) tree = intervaltree.IntervalTree() for rec in records(table): tree.addi(getstart(rec), getstop(rec), rec) return tree
def test_records_errors(): table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = records(table) # access items it = iter(actual) o = next(it) try: o['baz'] except KeyError: pass else: raise Exception('expected exception not raised') try: o.baz except AttributeError: pass else: raise Exception('expected exception not raised')
def facetrecordtrees(table, key, start='start', stop='stop'): """ Construct faceted interval trees for the given table, where each node in the tree is a record. """ import intervaltree getstart = attrgetter(start) getstop = attrgetter(stop) getkey = attrgetter(key) trees = dict() for rec in records(table): k = getkey(rec) if k not in trees: trees[k] = intervaltree.IntervalTree() trees[k].addi(getstart(rec), getstop(rec), rec) return trees
def test_records(): table = (('foo', 'bar'), ('a', 1), ('b', 2)) actual = records(table) # access items it = iter(actual) o = next(it) eq_('a', o['foo']) eq_(1, o['bar']) o = next(it) eq_('b', o['foo']) eq_(2, o['bar']) # access attributes it = iter(actual) o = next(it) eq_('a', o.foo) eq_(1, o.bar) o = next(it) eq_('b', o.foo) eq_(2, o.bar)
def test_records_unevenrows(): table = (('foo', 'bar'), ('a', 1, True), ('b', )) actual = records(table) # access items it = iter(actual) o = next(it) eq_('a', o['foo']) eq_(1, o['bar']) o = next(it) eq_('b', o['foo']) eq_(None, o['bar']) # access attributes it = iter(actual) o = next(it) eq_('a', o.foo) eq_(1, o.bar) o = next(it) eq_('b', o.foo) eq_(None, o.bar)
def test_records_unevenrows(): table = (('foo', 'bar'), ('a', 1, True), ('b',)) actual = records(table) # access items it = iter(actual) o = next(it) eq_('a', o['foo']) eq_(1, o['bar']) o = next(it) eq_('b', o['foo']) eq_(None, o['bar']) # access attributes it = iter(actual) o = next(it) eq_('a', o.foo) eq_(1, o.bar) o = next(it) eq_('b', o.foo) eq_(None, o.bar)