Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
elif callable(m):
mapfuns[outfld] = m
elif isinstance(m, (tuple, list)) and len(m) == 2:
srcfld = m[0]
fm = m[1]
if callable(fm):
mapfuns[outfld] = composefun(fm, srcfld)
elif isinstance(fm, dict):
mapfuns[outfld] = composedict(fm, srcfld)
else:
raise ArgumentError('expected callable or dict')
else:
raise ArgumentError('invalid mapping %r: %r' % (outfld, m))
# wrap rows as records
it = (Record(row, flds) for row in it)
for row in it:
outrow = list()
for outfld in outhdr:
try:
val = mapfuns[outfld](row)
except Exception as e:
if failonerror:
raise e
else:
val = errorvalue
outrow.append(val)
yield tuple(outrow)
def accept(self, row):
row = Record(row, self.fields)
key = self.discriminator(row)
self.broadcast(key, row)
def iterselectusingcontext(table, query):
it = iter(table)
hdr = tuple(next(it))
flds = list(map(text_type, hdr))
yield hdr
it = (Record(row, flds) for row in it)
prv = None
cur = next(it)
for nxt in it:
if query(prv, cur, nxt):
yield cur
prv = cur
cur = nxt
# handle last row
if query(prv, cur, None):
yield cur
def iterrowmap(source, rowmapper, header, failonerror):
it = iter(source)
hdr = next(it)
flds = list(map(text_type, hdr))
yield tuple(header)
it = (Record(row, flds) for row in it)
for row in it:
try:
outrow = rowmapper(row)
yield tuple(outrow)
except Exception as e:
if failonerror:
raise e
if dictionary is None:
dictionary = dict()
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
keyindices = asindices(hdr, key)
assert len(keyindices) > 0, 'no key selected'
getkey = operator.itemgetter(*keyindices)
for row in it:
k = getkey(row)
if strict and k in dictionary:
raise DuplicateKeyError(k)
elif k not in dictionary:
d = Record(row, flds)
dictionary[k] = d
return dictionary
def iteraddfieldusingcontext(table, field, query):
it = iter(table)
hdr = tuple(next(it))
flds = list(map(text_type, hdr))
yield hdr + (field,)
flds.append(field)
it = (Record(row, flds) for row in it)
prv = None
cur = next(it)
for nxt in it:
v = query(prv, cur, nxt)
yield tuple(cur) + (v,)
prv = Record(tuple(cur) + (v,), flds)
cur = nxt
# handle last row
v = query(prv, cur, None)
yield tuple(cur) + (v,)
def iterrecords(table, *sliceargs, **kwargs):
missing = kwargs.get('missing', None)
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
if sliceargs:
it = islice(it, *sliceargs)
for row in it:
yield Record(row, flds, missing=missing)
it = iter(source)
hdr = next(it)
flds = list(map(text_type, hdr))
# determine index of new field
if index is None:
index = len(hdr)
# construct output fields
outhdr = list(hdr)
outhdr.insert(index, field)
yield tuple(outhdr)
if callable(value):
# wrap rows as records if using calculated value
it = (Record(row, flds) for row in it)
for row in it:
outrow = list(row)
v = value(row)
outrow.insert(index, v)
yield tuple(outrow)
else:
for row in it:
outrow = list(row)
outrow.insert(index, value)
yield tuple(outrow)
else:
def transform_row(_row):
return tuple(transform_value(i, v)
for i, v in enumerate(_row))
# prepare where function
if isinstance(where, string_types):
where = expr(where)
elif where is not None:
assert callable(where), 'expected callable for "where" argument, ' \
'found %r' % where
# prepare iterator
if pass_row or where:
# wrap rows as records
it = (Record(row, flds) for row in it)
# construct the data rows
if where is None:
# simple case, transform all rows
for row in it:
yield transform_row(row)
else:
# conditionally transform rows
for row in it:
if where(row):
yield transform_row(row)
else:
yield row