Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
yield tuple(outrow)
elif leftouter:
outrow = list(lrow)
if not anti:
outrow.extend([missing] * len(rflds))
yield tuple(outrow)
else:
# build interval lookup for right table
lookup = facetintervallookup(right, key=rkey, start=rstart,
stop=rstop, include_stop=include_stop)
search = dict()
for f in lookup:
search[f] = lookup[f].search
# getter for facet key values in left table
getlkey = itemgetter(*asindices(lflds, lkey))
# main loop
for lrow in lit:
lkey = getlkey(lrow)
start = getlstart(lrow)
stop = getlstop(lrow)
try:
rrows = search[lkey](start, stop)
except KeyError:
rrows = None
except AttributeError:
rrows = None
if rrows:
if not anti:
for rrow in rrows:
def recordlookupone(table, key, dictionary=None, strict=False):
"""
Load a dictionary with data from the given table, mapping to record objects,
assuming there is at most one row for each key.
"""
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 itermelt(source, key, variables, variablefield, valuefield):
if key is None and variables is None:
raise ValueError('either key or variables must be specified')
it = iter(source)
hdr = next(it)
# determine key and variable field indices
key_indices = variables_indices = None
if key is not None:
key_indices = asindices(hdr, key)
if variables is not None:
if not isinstance(variables, (list, tuple)):
variables = (variables,)
variables_indices = asindices(hdr, variables)
if key is None:
# assume key is fields not in variables
key_indices = [i for i in range(len(hdr))
if i not in variables_indices]
if variables is None:
# assume variables are fields not in key
variables_indices = [i for i in range(len(hdr))
if i not in key_indices]
variables = [hdr[i] for i in variables_indices]
getkey = rowgetter(*key_indices)
def iterintervalsubtract(left, right, lstart, lstop, rstart, rstop, lkey, rkey,
include_stop):
# create iterators and obtain fields
lit = iter(left)
lhdr = next(lit)
lflds = list(map(text_type, lhdr))
rit = iter(right)
rhdr = next(rit)
# check fields via petl.util.asindices (raises FieldSelectionError if spec
# is not valid)
asindices(lhdr, lstart)
asindices(lhdr, lstop)
if lkey is not None:
asindices(lhdr, lkey)
asindices(rhdr, rstart)
asindices(rhdr, rstop)
if rkey is not None:
asindices(rhdr, rkey)
# determine output fields
outhdr = list(lflds)
yield tuple(outhdr)
# create getters for start and stop positions
lstartidx, lstopidx = asindices(lhdr, (lstart, lstop))
getlcoords = itemgetter(lstartidx, lstopidx)
getrcoords = itemgetter(*asindices(rhdr, (rstart, rstop)))
if rkey is None:
# build interval lookup for right table
def iterhashjoin(left, right, lkey, rkey, rlookup, lprefix, rprefix):
lit = iter(left)
rit = iter(right)
lhdr = next(lit)
rhdr = next(rit)
# determine indices of the key fields in left and right tables
lkind = asindices(lhdr, lkey)
rkind = asindices(rhdr, rkey)
# construct functions to extract key values from left table
lgetk = operator.itemgetter(*lkind)
# determine indices of non-key fields in the right table
# (in the output, we only include key fields from the left table - we
# don't want to duplicate fields)
rvind = [i for i in range(len(rhdr)) if i not in rkind]
rgetv = rowgetter(*rvind)
# determine the output fields
if lprefix is None:
outhdr = list(lhdr)
else:
outhdr = [(str(lprefix) + str(f))
for f in lhdr]
# obtain iterator and header row
it = iter(table)
hdr = next(it)
# prepare key getter
keyindices = asindices(hdr, key)
assert len(keyindices) > 0, 'no key selected'
getkey = operator.itemgetter(*keyindices)
# prepare value getter
if value is None:
# default value is complete row
getvalue = rowgetter(*range(len(hdr)))
else:
valueindices = asindices(hdr, value)
assert len(valueindices) > 0, 'no value selected'
getvalue = operator.itemgetter(*valueindices)
return it, getkey, getvalue
def iterantijoin(left, right, lkey, rkey):
lit = iter(left)
rit = iter(right)
lhdr = next(lit)
rhdr = next(rit)
yield tuple(lhdr)
# determine indices of the key fields in left and right tables
lkind = asindices(lhdr, lkey)
rkind = asindices(rhdr, rkey)
# construct functions to extract key values from both tables
lgetk = comparable_itemgetter(*lkind)
rgetk = comparable_itemgetter(*rkind)
# construct group iterators for both tables
lgit = itertools.groupby(lit, key=lgetk)
rgit = itertools.groupby(rit, key=rgetk)
lrowgrp = []
# loop until *either* of the iterators is exhausted
lkval, rkval = Comparable(None), Comparable(None)
try:
# pick off initial row groups
lkval, lrowgrp = next(lgit)
>>> lkp.close()
>>> lkp = shelve.open('example.dat', flag='r')
>>> lkp['a']
{'foo': 'a', 'bar': 1}
>>> lkp['b']
{'foo': 'b', 'bar': 2}
"""
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 = asdict(flds, row)
dictionary[k] = d
return dictionary
# create iterators and obtain fields
lit = iter(left)
lhdr = next(lit)
lflds = list(map(text_type, lhdr))
rit = iter(right)
rhdr = next(rit)
rflds = list(map(text_type, rhdr))
# check fields via petl.util.asindices (raises FieldSelectionError if spec
# is not valid)
asindices(lhdr, lstart)
asindices(lhdr, lstop)
if lkey is not None:
asindices(lhdr, lkey)
asindices(rhdr, rstart)
asindices(rhdr, rstop)
if rkey is not None:
asindices(rhdr, rkey)
# determine output fields
if lprefix is None:
outhdr = list(lflds)
if not anti:
outhdr.extend(rflds)
else:
outhdr = list(lprefix + f for f in lflds)
if not anti:
outhdr.extend(rprefix + f for f in rflds)
yield tuple(outhdr)
# create getters for start and stop positions
getlstart = itemgetter(lflds.index(lstart))
the tree is a row of the table.
"""
import intervaltree
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
assert start in flds, 'start field not recognised'
assert stop in flds, 'stop field not recognised'
getstart = itemgetter(flds.index(start))
getstop = itemgetter(flds.index(stop))
if value is None:
getvalue = tuple
else:
valueindices = asindices(hdr, value)
assert len(valueindices) > 0, 'invalid value field specification'
getvalue = itemgetter(*valueindices)
keyindices = asindices(hdr, key)
assert len(keyindices) > 0, 'invalid key'
getkey = itemgetter(*keyindices)
trees = dict()
for row in it:
k = getkey(row)
if k not in trees:
trees[k] = intervaltree.IntervalTree()
trees[k].addi(getstart(row), getstop(row), getvalue(row))
return trees