Skip to main content

A package to enable the usage of operators on python functions

Project description

opfuncs

A package to enable the usage of operators on python functions.

>>> from opfuncs import opfunc
>>> @opfunc
... def add_7(x):
...     return x + 7
...
>>> @opfunc
... def times_4(x):
...     return x * 4
...
>>> (add_7 + times_4)(5)
32
>>> (add_7 * times_4)(3)
120
>>> times_4[add_7](4)  # supports composition
44

Installation

The opfuncs package can be installed from PyPI using pip:

pip install opfuncs

Features

Can be used alongside normal functions occupying the right-side argument of the operator.

>>> def line(x):
...     return 3 * x + 4
...
>>> (times_4 * line)(3)
156

You can restrict a function to only allow/disallow certain operators using the include and exclude parameters:

>>> @opfunc(include=('add', ))
>>> def mod_3(x):
...     return x % 3

Refer to the SUPPORTED_OPERATORS constant within the package for the list operator names supported by include and exclude.


You can customize an operator to use a function you define in place of the usual operator. Custom operators are supplied to the opfunc wrapper through the custom_ops parameter, which accepts a dictionary mapping operator names to operator function builders.

>>> def custom_xor(
...     f: Callable[[float], float],
...     g: Callable[[float], float],
... ) -> Callable[[float], float]:
...     def xor_as_pow(x: float) -> float:
...         return f(x) ** g(x)
...
...     return xor_as_pow
...
>>> custom_times_4 = opfunc(times_4, custom_ops={"xor": custom_xor})
>>> custom_add_7 = opfunc(add_7, custom_ops={"xor": custom_xor})
>>> (custom_times_4 ^ custom_add_7)(-5)
400

Refer to the SUPPORTED_OPERATORS constant within the package for the list operator names supported by custom_ops.


Supported Operators

The opfuncs package supports the following python operators and functions:

  • &
  • |
  • ^
  • +
  • -
  • *
  • @
  • /
  • //
  • %
  • divmod()
  • ** and pow()
  • <<
  • >>
  • -()
  • +()
  • abs()
  • ~
  • round()
  • math.trunc()
  • math.floor()
  • math.ceil()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opfuncs-1.0.1.tar.gz (6.7 kB view hashes)

Uploaded Source

Built Distribution

opfuncs-1.0.1-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page