def slice(*args, close_all=False): spec = builtins.slice(*args) start, stop, step = spec.start, spec.stop, spec.step if start is not None and start < 0: raise ValueError('slice requires start >= 0') if stop is not None and stop < 0: raise ValueError('slice requires stop >= 0') if step is not None and step <= 0: raise ValueError('slice requires step > 0') if start is None: start = 0 if step is None: step = 1 if stop is None: stopper = it.count() else: stopper = range((stop - start + step - 1) // step) @coroutine def slice_loop(target): with closing(target): for _ in range(start): yield for _ in stopper: target.send((yield)) for _ in range(step - 1): yield if close_all: raise StopPipeline while True: yield return slice_loop
def slice(source, *args): """Slice an asynchronous sequence. The arguments are the same as the builtin type slice. There are two limitations compare to regular slices: - Positive stop index with negative start index is not supported - Negative step is not supported """ s = builtins.slice(*args) start, stop, step = s.start or 0, s.stop, s.step or 1 # Filter the first items if start < 0: source = takelast.raw(source, abs(start)) elif start > 0: source = skip.raw(source, start) # Filter the last items if stop is not None: if stop >= 0 and start < 0: raise ValueError("Positive stop with negative start is not supported") elif stop >= 0: source = take.raw(source, stop - start) else: source = skiplast.raw(source, abs(stop)) # Filter step items if step is not None: if step > 1: source = filterindex.raw(source, lambda i: i % step == 0) elif step < 0: raise ValueError("Negative step not supported") # Return return source
class Operator(Base, tuple): def __eq__(self, other): return hash(self) == hash(other) def __hash__(self): return hash(cat(self, self.__class__)) @mathify(piece=slice(1, None, None)) def __new__(cls, *args): return super().__new__(cls, args) def __repr__(self): r = lambda x, y: str(x) + self.sign + str(y) return "(" + str(reduce(r, self)) + ")" def __getitem__(self, arg): item = super().__getitem__(arg) if isinstance(arg, slice): try: return type(self)(*item) except TypeError: return tuple(item) else: return item def append(self, *node): return self.__class__(*(self + tuple(node))) def insert(self, index, *items): return self.__class__(*(self[:index] + tuple(items) + self[index + 1:]))
def slice(source, *args): """Slice an asynchronous sequence. The arguments are the same as the builtin type slice. There are two limitations compare to regular slices: - Positive stop index with negative start index is not supported - Negative step is not supported """ s = builtins.slice(*args) start, stop, step = s.start or 0, s.stop, s.step or 1 # Filter the first items if start < 0: source = takelast.raw(source, abs(start)) elif start > 0: source = skip.raw(source, start) # Filter the last items if stop is not None: if stop >= 0 and start < 0: raise ValueError( "Positive stop with negative start is not supported") elif stop >= 0: source = take.raw(source, stop - start) else: source = skiplast.raw(source, abs(stop)) # Filter step items if step is not None: if step > 1: source = filterindex.raw(source, lambda i: i % step == 0) elif step < 0: raise ValueError("Negative step not supported") # Return return source
def slice_head( _data: DataFrame, n: int = None, prop: float = None, ) -> Tibble: """Select first rows Args: _data: The dataframe. n: and prop: Provide either n, the number of rows, or prop, the proportion of rows to select. If neither are supplied, n = 1 will be used. If n is greater than the number of rows in the group (or prop > 1), the result will be silently truncated to the group size. If the proportion of a group size is not an integer, it is rounded down. Returns: The sliced dataframe """ n = _n_from_prop(_data.shape[0], n, prop) return regcall( slice, _data, builtins.slice(None, n), )
def read_block(self, block, slice=None): # Print("Block:", block) if block[1:] != (0, 0): raise IndexError(block) arr = self.read(builtins.slice(block[0], block[0] + 1)) if slice is not None: arr = arr[slice] return arr
def __init__(self, *args: ty.Optional[int]): s = builtins.slice(*args) start, stop, step = ( s.start or 0, s.stop or builtins.min(s.stop, MAX_RANGE), s.step or 1, ) self._it = builtins.iter(builtins.range(start, stop, step))
def __init__(self, *args: ty.Optional[int], elements: int = 1): s = builtins.slice(*args) start, stop, step = ( s.start or 0, s.stop or builtins.min(s.stop, MAX_RANGE), s.step or 1, ) self._it = builtins.tuple( builtins.iter(builtins.range(start, stop, step)) for _ in builtins.range(elements) )
def slice_tail( _data: DataFrame, n: int = 1, prop: float = None, ) -> Tibble: """Select last rows See Also: [`slice_head()`](datar.dplyr.slice.slice_head) """ n = _n_from_prop(_data.shape[0], n, prop) return regcall( slice, _data, builtins.slice(-n, None), )
def slice(*args): """ slice([start], end [,step]) nd version of slice, where each arg can be a vector of the same length Parameters: [start] (vector): the start """ # if passed in scalars call the built-in range if not isinstance(args[0], (list, tuple, np.ndarray)): return builtins.slice(*args) start, end, step = _prep_range(*args) # prepare idx = [slice(start[i], end[i], step[i]) for i in range(len(end))] return idx
def mathify(piece=slice(0, None, None)): """Wrapper to ensure all arguments are math types. Only necessary on public functions that take arbitrary inputs. Only acts on args, not kwargs. Slice can be used to specify which arguments to mathify.""" def _(f): """What the hell why didn't they just make the first argument for a decorator function the function and not require this double nesting nonsense? """ @wraps(f) def __(*args, **kwargs): args = list(args) mathifyargs = args.__getitem__(piece) mathifyargs = list(map(_mathify, mathifyargs)) args.__setitem__(piece, mathifyargs) return f(*args, **kwargs) return __ return _
def get_job_output(self, job, slice=None): """ Helper method for getting output from job `job` must be finished. :param job: :class:`~.jobs.base.JobBase` instance :param slice: :class:`int` to get a specific item from `job`'s output, `None` to return all output as a list or a :class:`slice` object :raise RuntimeError: if `job` is not finished or getting `slice` from :attr:`~.base.JobBase.output` raises an :class:`IndexError` :return: :class:`list` or :class:`str` """ if not job.is_finished: raise RuntimeError(f'Unfinished job: {job.name}') if slice is None: slice = builtins.slice(None, None) try: return job.output[slice] except IndexError: raise RuntimeError(f'Job finished with insufficient output: {job.name}: {job.output}')
lambda *args, **kwargs: builtins.round(*args, **kwargs), builtins.round) round._ = functools.update_wrapper( lambda *args, **kwargs: wrap(builtins.round)(*args, **kwargs), builtins.round) set = functools.update_wrapper( lambda *args, **kwargs: builtins.set(*args, **kwargs), builtins.set) set._ = functools.update_wrapper( lambda *args, **kwargs: wrap(builtins.set)(*args, **kwargs), builtins.set) setattr = functools.update_wrapper( lambda *args, **kwargs: builtins.setattr(*args, **kwargs), builtins.setattr) setattr._ = functools.update_wrapper( lambda *args, **kwargs: wrap(builtins.setattr)(*args, **kwargs), builtins.setattr) slice = functools.update_wrapper( lambda *args, **kwargs: builtins.slice(*args, **kwargs), builtins.slice) slice._ = functools.update_wrapper( lambda *args, **kwargs: wrap(builtins.slice)(*args, **kwargs), builtins.slice) sorted = functools.update_wrapper( lambda *args, **kwargs: builtins.sorted(*args, **kwargs), builtins.sorted) sorted._ = functools.update_wrapper( lambda *args, **kwargs: wrap(builtins.sorted)(*args, **kwargs), builtins.sorted) str = functools.update_wrapper( lambda *args, **kwargs: builtins.str(*args, **kwargs), builtins.str) str._ = functools.update_wrapper( lambda *args, **kwargs: wrap(builtins.str)(*args, **kwargs), builtins.str) sum = functools.update_wrapper( lambda *args, **kwargs: builtins.sum(*args, **kwargs), builtins.sum) sum._ = functools.update_wrapper(