def test_addfield(): table = (('foo', 'bar'), ('M', 12), ('F', 34), ('-', 56)) result = addfield(table, 'baz', 42) expectation = (('foo', 'bar', 'baz'), ('M', 12, 42), ('F', 34, 42), ('-', 56, 42)) ieq(expectation, result) ieq(expectation, result) result = addfield(table, 'baz', lambda row: '%s,%s' % (row.foo, row.bar)) expectation = (('foo', 'bar', 'baz'), ('M', 12, 'M,12'), ('F', 34, 'F,34'), ('-', 56, '-,56')) ieq(expectation, result) ieq(expectation, result) result = addfield(table, 'baz', lambda rec: rec['bar'] * 2) expectation = (('foo', 'bar', 'baz'), ('M', 12, 24), ('F', 34, 68), ('-', 56, 112)) ieq(expectation, result) ieq(expectation, result) result = addfield(table, 'baz', expr('{bar} * 2')) expectation = (('foo', 'bar', 'baz'), ('M', 12, 24), ('F', 34, 68), ('-', 56, 112)) ieq(expectation, result) ieq(expectation, result) result = addfield(table, 'baz', 42, index=0) expectation = (('baz', 'foo', 'bar'), (42, 'M', 12), (42, 'F', 34), (42, '-', 56)) ieq(expectation, result) ieq(expectation, result)
def test_addfield_coalesce(): table = (('foo', 'bar', 'baz', 'quux'), ('M', 12, 23, 44), ('F', None, 23, 11), ('-', None, None, 42)) result = addfield(table, 'spong', coalesce('bar', 'baz', 'quux')) expect = (('foo', 'bar', 'baz', 'quux', 'spong'), ('M', 12, 23, 44, 12), ('F', None, 23, 11, 23), ('-', None, None, 42, 42)) ieq(expect, result) ieq(expect, result) result = addfield(table, 'spong', coalesce(1, 2, 3)) expect = (('foo', 'bar', 'baz', 'quux', 'spong'), ('M', 12, 23, 44, 12), ('F', None, 23, 11, 23), ('-', None, None, 42, 42)) ieq(expect, result) ieq(expect, result)
def test_addfield_uneven_rows(): table = (('foo', 'bar'), ('M',), ('F', 34), ('-', 56, 'spong')) result = addfield(table, 'baz', 42) expectation = (('foo', 'bar', 'baz'), ('M', None, 42), ('F', 34, 42), ('-', 56, 42)) ieq(expectation, result) ieq(expectation, result)
def test_addfield_dupfield(): table = (('foo', 'foo'), ('M', 12), ('F', 34), ('-', 56)) result = addfield(table, 'bar', 42) expectation = (('foo', 'foo', 'bar'), ('M', 12, 42), ('F', 34, 42), ('-', 56, 42)) ieq(expectation, result) ieq(expectation, result)
def intervaljoinvalues(left, right, value, lstart='start', lstop='stop', rstart='start', rstop='stop', lkey=None, rkey=None, include_stop=False): """ Convenience function to join the left table with values from a specific field in the right hand table. Note start coordinates are included and stop coordinates are excluded from the interval. Use the `include_stop` keyword argument to include the upper bound of the interval when finding overlaps. """ assert (lkey is None) == (rkey is None), \ 'facet key field must be provided for both or neither table' if lkey is None: lkp = intervallookup(right, start=rstart, stop=rstop, value=value, include_stop=include_stop) f = lambda row: lkp.search(row[lstart], row[lstop]) else: lkp = facetintervallookup(right, rkey, start=rstart, stop=rstop, value=value, include_stop=include_stop) f = lambda row: lkp[row[lkey]].search(row[lstart], row[lstop]) return addfield(left, value, f)
def test_addfield_empty(): table = (('foo', 'bar'),) expect = (('foo', 'bar', 'baz'),) actual = addfield(table, 'baz', 42) ieq(expect, actual) ieq(expect, actual)
def test_addfield_empty(): table = (('foo', 'bar'), ) expect = (('foo', 'bar', 'baz'), ) actual = addfield(table, 'baz', 42) ieq(expect, actual) ieq(expect, actual)