Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Latest PyPI version Travis build status

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(list(1 ^until^ 10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> @pow_infix
... def pow(a, b):
...     return a**b
...
>>> print(3 **pow** 2)
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).

Compatibility

Works on all major Python versions (2.6, 2.7, 3.2, 3.3, 3.4).

Tests

The tests in this README files are run using doctest. To run the tests, run ‘’python infix.py’’ - alternatively, use tox to run the tests on all compatible Python versions.

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.

Release files for infix 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for infix 1.1
File Size Uploaded
infix-1.1.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for infix 1.1
File Interpreter ABI Platform
infix-1.1-py2-none-any.whl Python 2 none any Details

Total release size: 9.5 kB

Release files / infix-1.1.tar.gz

Download URL infix-1.1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
689d1670a45cb4b8318a8d140c5dc002b457ed53f1693e59ca39b2f314c27e93
BLAKE2b-256 checksum
How to use checksums
7c4e8d8879c894b347a79ba4fd461f8bdcc2d0f348d361398c5c604ef19e3817
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / infix-1.1-py2-none-any.whl

Download URL infix-1.1-py2-none-any.whl
Size 6.0 kB
Tags Python 2
SHA-256 checksum
How to use checksums
8c074b6326e3b06ce5566b96c02b605153a45bf778ac4d887617d619172f7962
BLAKE2b-256 checksum
How to use checksums
2bb45d2089d4d260e84f2a989692873ae0a3b6a1d9bc6b162a005ad04083d1c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page