def each(self, updater, selector=slice(None)): rsel = sel(selector) func = ignore_extra_args(s2f(updater)) for i, row in enumerate(self): if rsel(row, i): self[i] = func(row, i) return self
def sel(rows): if type(rows) == int: return lambda val, i: i == rows elif type(rows) == str: return ignore_extra_args(s2f(rows)) elif type(rows) == slice: def in_slice(val, i): start = rows.start or 0 stop = sys.maxint if rows.stop == None else rows.stop r = start <= i < stop # print i, rows, r if rows.step: return r and ((i - rows.start) % rows.step == 0) else: return r return in_slice else: return ignore_extra_args(rows)
def __setitem__(self, selectors, updater): try: list.__setitem__(self, selectors, updater) except: func = ignore_extra_args(s2f(updater)) if type(selectors) == tuple: assert len(selectors) == 2 rows, cols = selectors rsel = sel(rows) csel = sel(cols) for i, row in enumerate(self): if rsel(row, i): for j, cell in enumerate(row): if csel(cell, j): row[j] = func(cell, i, j) else: rsel = sel(selectors) # return selected row for i, row in enumerate(self): if rsel(row, i): self[i] = func(row, i)
def _(self, f): return s2f(f)(self)
def map(self, updater, selector=slice(None)): rsel = sel(selector) func = ignore_extra_args(s2f(updater)) return itab([func(row, i) for i, row in enumerate(self) if rsel(row, i)])
def tab_untab(self, sep, func): extras = self._tablize(sep) s2f(func)(self) self._untablize(extras, sep) return self