Skip to content

Reference

placeholder.F

Bases: partial

Singleton for creating composite functions.

Parameters:

Name Type Description Default
*funcs Callable

ordered callables

required
Source code in placeholder/__init__.py
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
class F(partial):
    """Singleton for creating composite functions.

    Args:
        *funcs (Callable): ordered callables
    """

    def __new__(cls, *funcs):
        funcs = (func if isinstance(func, cls) else [func] for func in funcs)
        funcs = tuple(itertools.chain(*funcs))
        return partial.__new__(cls, *(funcs if len(funcs) == 1 else (pipe, funcs)))

    def __iter__(self) -> Iterator[Callable]:
        """Return composed functions in order."""
        args = super().__getattribute__('args')
        return iter(args[0] if args else [super().__getattribute__('func')])

    def __getattribute__(self, attr: str) -> 'F':
        """Return `attrgetter`."""
        if attr.startswith('__') and attr.endswith('__'):
            return super().__getattribute__(attr)
        return type(self)(self, operator.attrgetter(attr))

    def __getitem__(self, item) -> 'F':
        """Return `itemgetter`."""
        return type(self)(self, operator.itemgetter(item))

    def __round__(self, ndigits: Optional[int] = None) -> 'F':
        """Return `round(...)`."""
        return type(self)(self, round if ndigits is None else partial(round, ndigits=ndigits))

    __neg__ = unary(operator.neg)
    __pos__ = unary(operator.pos)
    __invert__ = unary(operator.invert)

    __abs__ = unary(abs)
    __reversed__ = unary(reversed)

    __trunc__ = unary(math.trunc)
    __floor__ = unary(math.floor)
    __ceil__ = unary(math.ceil)

    __add__, __radd__ = methods(operator.add)
    __sub__, __rsub__ = methods(operator.sub)
    __mul__, __rmul__ = methods(operator.mul)
    __floordiv__, __rfloordiv__ = methods(operator.floordiv)
    __truediv__, __rtruediv__ = methods(operator.truediv)

    __mod__, __rmod__ = methods(operator.mod)
    __divmod__, __rdivmod__ = methods(divmod)
    __pow__, __rpow__ = methods(operator.pow)
    __matmul__, __rmatmul__ = methods(operator.matmul)

    __lshift__, __rlshift__ = methods(operator.lshift)
    __rshift__, __rrshift__ = methods(operator.rshift)

    __and__, __rand__ = methods(operator.and_)
    __xor__, __rxor__ = methods(operator.xor)
    __or__, __ror__ = methods(operator.or_)

    __eq__ = methods(operator.eq)[0]
    __ne__ = methods(operator.ne)[0]
    __lt__, __gt__ = methods(operator.lt)
    __le__, __ge__ = methods(operator.le)

__getattribute__(attr)

Return attrgetter.

Source code in placeholder/__init__.py
59
60
61
62
63
def __getattribute__(self, attr: str) -> 'F':
    """Return `attrgetter`."""
    if attr.startswith('__') and attr.endswith('__'):
        return super().__getattribute__(attr)
    return type(self)(self, operator.attrgetter(attr))

__getitem__(item)

Return itemgetter.

Source code in placeholder/__init__.py
65
66
67
def __getitem__(self, item) -> 'F':
    """Return `itemgetter`."""
    return type(self)(self, operator.itemgetter(item))

__iter__()

Return composed functions in order.

Source code in placeholder/__init__.py
54
55
56
57
def __iter__(self) -> Iterator[Callable]:
    """Return composed functions in order."""
    args = super().__getattribute__('args')
    return iter(args[0] if args else [super().__getattribute__('func')])

__round__(ndigits=None)

Return round(...).

Source code in placeholder/__init__.py
69
70
71
def __round__(self, ndigits: Optional[int] = None) -> 'F':
    """Return `round(...)`."""
    return type(self)(self, round if ndigits is None else partial(round, ndigits=ndigits))

placeholder.M

Singleton for creating method callers and multi-valued getters.

Source code in placeholder/__init__.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class M:
    """Singleton for creating method callers and multi-valued getters."""

    def __getattr__(cls, name: str) -> F:
        """Return a `methodcaller` constructor."""
        return F(partial(operator.methodcaller, name), F)

    def __call__(self, *names: str) -> F:
        """Return a tupled `attrgetter`."""
        return F(operator.attrgetter(*names))

    def __getitem__(self, keys: Iterable) -> F:
        """Return a tupled `itemgetter`."""
        return F(operator.itemgetter(*keys))

__call__(*names)

Return a tupled attrgetter.

Source code in placeholder/__init__.py
115
116
117
def __call__(self, *names: str) -> F:
    """Return a tupled `attrgetter`."""
    return F(operator.attrgetter(*names))

__getattr__(name)

Return a methodcaller constructor.

Source code in placeholder/__init__.py
111
112
113
def __getattr__(cls, name: str) -> F:
    """Return a `methodcaller` constructor."""
    return F(partial(operator.methodcaller, name), F)

__getitem__(keys)

Return a tupled itemgetter.

Source code in placeholder/__init__.py
119
120
121
def __getitem__(self, keys: Iterable) -> F:
    """Return a tupled `itemgetter`."""
    return F(operator.itemgetter(*keys))