Coverage for pandalone\xleash\_filter.py : 96%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python # -*- coding: UTF-8 -*- # # Copyright 2014 European Commission (JRC); # Licensed under the EUPL (the 'Licence'); # You may not use this work except in compliance with the Licence. # You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl The high-level functionality, the filtering and recursive :term:`lassoing`.
Prefer accessing the public members from the parent module.
.. currentmodule:: pandalone.xleash """
""" A :term:`bulk-filter` that applies all call-specifiers one after another on the :term:`capture-rect` values.
:param list filters: the json-parsed :term:`call-spec` """
""" Identifies rect from its edge-coordinates (row, col, 2d-table)..
:param Coords st: the top-left edge of capture-rect, inclusive :param Coords or None nd: the bottom-right edge of capture-rect, inclusive :return: in int based on the input like that:
- 0: only `st` given - 1: `st` and `nd` point the same cell - 2: row - 3: col - 4: 2d-table
Examples::
>>> _classify_rect_shape((1,1), None) 0 >>> _classify_rect_shape((2,2), (2,2)) 1 >>> _classify_rect_shape((2,2), (2,20)) 2 >>> _classify_rect_shape((2,2), (20,2)) 3 >>> _classify_rect_shape((2,2), (20,20)) 4 """
""" Append trivial dimensions to the left.
:param values: The scalar ot 2D-results of :meth:`Sheet.read_rect()` :param int new_dim: The new dimension the result should have """
""" Squeeze it, and then flatten it, before inflating it.
:param values: The scalar ot 2D-results of :meth:`Sheet.read_rect()` :param int new_dim: The new dimension the result should have """ else:
""" Reshapes the :term:`capture-rect` values of :func:`read_capture_rect()`.
:param values: The scalar ot 2D-results of :meth:`Sheet.read_rect()` :type values: (nested) list, * :param new_ndim: :type int, (int, bool) or None new_ndim:
:return: reshaped values :rtype: list of lists, list, *
Examples::
>>> _redim([1, 2], 2) [[1, 2]]
>>> _redim([[1, 2]], 1) [1, 2]
>>> _redim([], 2) [[]]
>>> _redim([[3.14]], 0) 3.14
>>> _redim([[11, 22]], 0) [11, 22]
>>> arr = [[[11], [22]]] >>> arr == _redim(arr, None) True
>>> _redim([[11, 22]], 0) [11, 22] """ return values
"""A list :term:`call-spec` for :meth:`_redim_filter` :term:`filter` that imitates results of *xlwings* library."""
scalar=None, cell=None, row=None, col=None, table=None): """ A :term:`bulk-filter` that reshapes sand/or transpose captured values, depending on rect's shape.
Each dimension might be a single int or None, or a pair [dim, transpose]. """
('sheet', 'st', 'nd', 'base_coords')) """ Fields denoting the position of a sheet/cell while running a :term:`element-wise-filter`.
Practically func:`run_filter_elementwise() preserves these fields if the processed ones were `None`. """
include=None, exclude=None, depth=-1, *args, **kwds): """ Runner of all :term:`element-wise` :term:`filters`.
It applies the `element_func` on elements extracted from ``lasso.values`` by treating the later first as "indexed" objects (Mappings, Series and Dataframes.), and if that fails, as nested lists.
- The `include`/`exclude` filter args work only for "indexed" objects with ``items()`` or ``iteritems()`` and indexing methods.
- If no filter arg specified, expands for all keys. - If only `include` specified, rejects all keys not explicitly contained in this filter arg. - If only `exclude` specified, expands all keys not explicitly contained in this filter arg. - When both `include`/`exclude` exist, only those explicitly included are accepted, unless also excluded.
- Lower the :mod:`logging` level to see other than syntax-errors on recursion reported on :data:`log`. - Only those in :class:`XLocation` are passed recursively.
:param list element_func: A function implementing the element-wise :term:`filter` and returning a 2-tuple ``(is_proccessed, new_val_or_lasso)``, like that::
def element_func(ranger, lasso, context, elval) proced = False try: elval = int(elval) proced = True except ValueError: pass return proced, elval
Its `kwds` may contain the `include`, `exclude` and `depth` args. Any exception raised from `element_func` will cancel the diving. :param list filters: Any :term:`filters` to apply after invoking the `element_func`. :param list or str include: Items to include when diving into "indexed" values. See description above. :param list or str exclude: Items to exclude when diving into "indexed" values. See description above. :param int or None depth: How deep to dive into nested structures, "indexed" or lists. If `< 0`, no limit. If 0, stops completely. :params args: To be relayed to 'element_func'. :params kwds: To be relayed to 'element_func'. """
except ImportError: if cdepth == 0: row += i elif cdepth == 1: col += + i else:
lasso._asdict()) *args, **kwds)
res_lasso = lasso._replace(values=res_lasso)
else: # Dict is not ordered, so cannot locate `base_coords`!
""" A :term:`element-wise-filter` that expand recursively any :term:`xl-ref` strings elements in :term:`capture-rect` values.
:param list filters: Any :term:`filters` to apply after invoking the `element_func`. :param list or str include: Items to include when diving into "indexed" values. See :func:`run_filter_elementwise()`. :param list or str exclude: Items to exclude when diving into "indexed" values. See :func:`run_filter_elementwise()`. :param int or None depth: How deep to dive into nested structures, "indexed" or lists. If `< 0`, no limit. If 0, stops completely. See :func:`run_filter_elementwise()`.
Note that in python-3 the signature would be::
def recursive_filter(ranger, lasso, element_func, filters, include=None, exclude=None, depth=-1): """ filters, include=include, exclude=exclude, depth=depth)
logging.INFO)
else: len(aeval.error)) + error else: if res.opts is None else res) else:
""" A :term:`element-wise-filter` that uses :mod:`asteval` to evaluate string values as python expressions.
The `expr` fecthed from `term:`capturing` may access read-write all :func:`locals()` of this method (ie: `ranger`, `lasso`), the :mod:`numpy` funcs, and the :mod:`pandalone.xleash` module under the `xleash` variable.
The `expr` may return either: - the processed values, or - an instance of the :class:`Lasso`, in which case only its `opt` field is checked and replaced with original if missing. So better use :func:`namedtuple._replace()` on the current `lasso` which exists in the expr's namespace.
:param bool eval_all: If `True` raise on 1st error and stop diving cells. Defaults to `False`. :param list filters: Any :term:`filters` to apply after invoking the `element_func`. :param list or str include: Items to include when diving into "indexed" values. See :func:`run_filter_elementwise()`. :param list or str exclude: Items to exclude when diving into "indexed" values. See :func:`run_filter_elementwise()`. :param int or None depth: How deep to dive into nested structures, "indexed" or lists. If `< 0`, no limit. If 0, stops completely. See :func:`run_filter_elementwise()`.
Note that in python-3 the signature woudl be::
def pyeval_filter(ranger, lasso, element_func, filters, include=None, exclude=None, depth=-1):
Example::
>>> expr = ''' ... res = array([[0.5, 0.3, 0.1, 0.1]]) ... res * res.T ... ''' >>> lasso = Lasso(values=expr, opts={}) >>> ranger = Ranger(None) >>> eval_filter(ranger, lasso).values array([[ 0.25, 0.15, 0.05, 0.05], [ 0.15, 0.09, 0.03, 0.03], [ 0.05, 0.03, 0.01, 0.01], [ 0.05, 0.03, 0.01, 0.01]]) """ filters, include=include, exclude=exclude, depth=depth, eval_all=eval_all)
""" A :term:`bulk-filter` that passes values through a python-expression using :mod:`asteval` library.
The `expr` may access read-write all :func:`locals()` of this method (`ranger`, `lasso`), the :mod:`numpy` funcs, and the :mod:`pandalone.xleash` module under the `xleash` variable.
The `expr` may return either: - the processed values, or - an instance of the :class:`Lasso`, in which case only its `opt` field is checked and replaced with original if missing. So better use :func:`namedtuple._replace()` on the current `lasso` which exists in the expr's namespace.
:param str expr: The python-expression, which may comprise of multiple statements. """ error = aeval.error[0].get_error() msg = "%i errors while py-evaluating %r: %s: %s" msg_args = (len(aeval.error), expr) + error raise ValueError(msg % msg_args) else: if res.opts is None else res) else: lasso = lasso._replace(values=res)
""" The default available :term:`filters` used by :func:`lasso()` when constructing its internal :class:`Ranger`.
:param dict or None overrides: Any items to update the default ones.
:return: a dict-of-dicts with 2 items:
- *func*: a function with args: ``(Ranger, Lasso, *args, **kwds)`` - *desc*: help-text replaced by ``func.__doc__`` if missing.
:rtype: dict """ 'pipe': { 'func': pipe_filter, }, 'pyeval': { 'func': pyeval_filter, }, 'py': { 'func': py_filter, }, 'recurse': { 'func': recursive_filter, }, 'redim': { 'func': redim_filter, }, 'numpy': { 'func': lambda ranger, lasso, * args, **kwds: lasso._replace( values=np.array(lasso.values, *args, **kwds)), 'desc': np.array.__doc__, }, 'dict': { 'func': lambda ranger, lasso, * args, **kwds: lasso._replace( values=dict(lasso.values, *args, **kwds)), 'desc': dict.__doc__, }, 'odict': { 'func': lambda ranger, lasso, * args, **kwds: lasso._replace( values=OrderedDict(lasso.values, *args, **kwds)), 'desc': OrderedDict.__doc__, }, 'sorted': { 'func': lambda ranger, lasso, * args, **kwds: lasso._replace( values=sorted(lasso.values, *args, **kwds)), 'desc': sorted.__doc__, }, }
'names') is None else None # , convert_float=True,
'df': { 'func': _df_filter, 'desc': parsers.TextParser.__doc__, }, 'series': { 'func': lambda ranger, lasso, *args, **kwds: pd.Series(OrderedDict(lasso.values), *args, **kwds), 'desc': ("Converts a 2-columns list-of-lists into pd.Series.\n" + pd.Series.__doc__), } }) except ImportError as ex: msg = "The 'df' and 'series' filters were not installed, due to: %s" log.info(msg, ex)
filters.update(overrides)
|