Skip to main content

Infix operators for Python

Project description

A decorator that allows functions to be used as infix functions.

Infix operators are created and applied as such, and do not stop the function from being called normally:

>>> from infix import shift_infix as infix
...
>>> @infix
... def plus(a, b):
...     return a + b
...
>>> print 40 <<plus>> 2
42
>>> print plus(5, 5)
10

Variant syntaxes

Multiple infix syntaxes are provided by the infix module:

>>> @and_infix
... def is_before(a, b):
...     return a < b
...
>>> print 1 &is_before& 5
True

>>> @or_infix
... def minus(a, b):
...     return a - b
...
>>> print 1 |minus| 2
-1

>>> @xor_infix
... def until(start, stop):
...     return range(start, stop)
...
>>> print 1 ^until^ 10
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Custom syntaxes

Python has a large set of operators than could be overridden to provide the infix syntax, so instead of providing them all a custom_infix function is provided. It takes two parameters, the names of the left and right operator functions:

>>> @custom_infix('__rmod__', '__mod__')
... def ate(a, b):
...     return (a == 7 and b == 9)
...
>>> print 7 %ate% 9
True
>>> print 6 %ate% 7
False

The left function should be a right operand, and the right functions should be a left operand.

You should be careful to avoid using operands that the objects your functions will take may provide themselves (as the initial right operand is only called if the previous object does not provide for that operand).

Tests

The tests are done using doctest and can be run with:

python infix.py

Licence

Copyright (C) 2013 Sam Clements

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

infix-0.1.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file infix-0.1.tar.gz.

File metadata

  • Download URL: infix-0.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for infix-0.1.tar.gz
Algorithm Hash digest
SHA256 5514b49660a17e569ae97a5816e1b70f95020060678e9544cce5773013ab72eb
MD5 28b70ed75f82f56942d44c65862e1d4a
BLAKE2b-256 6e25d129ce104ba5b7e35d9161aad9ab5fd6bae6d6dc30e9392b4ecd71c364ba

See more details on using hashes here.

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