Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def convertall(table, *args, **kwargs):
"""
Convenience function to convert all fields in the table using a common
function or mapping. See also :func:`convert`.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
# TODO don't read the data twice!
return convert(table, header(table), *args, **kwargs)
def natural_key(left, right):
# determine key field or fields
lhdr = header(left)
lflds = list(map(str, lhdr))
rhdr = header(right)
rflds = list(map(str, rhdr))
key = [f for f in lflds if f in rflds]
assert len(key) > 0, 'no fields in common'
if len(key) == 1:
key = key[0] # deal with singletons
return key
def itertranspose(source):
hdr = header(source)
its = [iter(source) for _ in hdr]
for i in range(len(hdr)):
yield tuple(row[i] for row in its[i])
def natural_key(left, right):
# determine key field or fields
lhdr = header(left)
lflds = list(map(str, lhdr))
rhdr = header(right)
rflds = list(map(str, rhdr))
key = [f for f in lflds if f in rflds]
assert len(key) > 0, 'no fields in common'
if len(key) == 1:
key = key[0] # deal with singletons
return key
def itercrossjoin(sources, prefix):
# construct fields
outhdr = list()
for i, s in enumerate(sources):
if prefix:
# use one-based numbering
outhdr.extend([text_type(i+1) + '_' + text_type(f) for f in header(s)])
else:
outhdr.extend(header(s))
yield tuple(outhdr)
datasrcs = [data(src) for src in sources]
for prod in itertools.product(*datasrcs):
outrow = list()
for row in prod:
outrow.extend(row)
yield tuple(outrow)
+-----+-----+-------+
| 9 | 'A' | False |
+-----+-----+-------+
Note that both tables must have the same set of fields, but that the order
of the fields does not matter. See also the
:func:`petl.transform.setops.complement` function.
See also the discussion of the `buffersize`, `tempdir` and `cache` arguments
under the :func:`petl.transform.sorts.sort` function.
"""
# TODO possible with only one pass?
ha = header(a)
hb = header(b)
assert set(ha) == set(hb), 'both tables must have the same set of fields'
# make sure fields are in the same order
bv = cut(b, *ha)
return complement(a, bv, buffersize=buffersize, tempdir=tempdir,
cache=cache, strict=strict)