Overview
Easily make named infix binary operators in Python.
Demo time:
# the only useful function in the module
>>> from betwixt import infix_operator
# any function of 2 arguments would do
>>> from fnmatch import fnmatch
# make the binary function into an operator, delimited by `*`
>>> matches = infix_operator('*', fnmatch)
# use it
>>> 'foo.txt' *matches* '*.txt'
True
# other delimiters can be used
>>> matches = infix_operator('|', fnmatch)
>>> 'foo.txt' |matches| '*.txt'
True
A decorator form can also be used:
>>> @infix_operator('|')
... def contains(left, right):
... return right in left
>>> [1, 2, 3] |contains| 1
True
>>> [1, 2, 3] |contains| 0
False
Finally, betwixt is provided as a shorter (and, perhaps, more expressive) alias to infix_operator:
>>> from betwixt import betwixt
>>> @betwixt('*')
... def joining(left, right):
... return left.join(right)
>>> '_' *joining* ['a', 'b', 'c']
'a_b_c'
>>> split_at = betwixt('//', lambda lhs, rhs: lhs.split(rhs))
>>> 'a_b_c' //split_at// '_'
['a', 'b', 'c']
All these example operators and a few more are available in module betwixt.examples.
The idea was taken from http://code.activestate.com/recipes/384122-infix-operators/ and by a similar C++ hack whose code on the web I cannot find any more, but no actual code has been stolen.
Installation
pip install betwixt
Documentation
Development
To run the all tests run:
tox
Note, to combine the coverage data from all the tox environments run:
Windows |
set PYTEST_ADDOPTS=--cov-append tox |
|---|---|
Other |
PYTEST_ADDOPTS=--cov-append tox |
Copyright and license
Copyright (c) 2016-2020 Riccardo Murri <riccardo.murri@gmail.com>
This is free software, available under the terms and conditions of the GNU LGPL – see file LICENSE for details.
Changelog
1.0.0 (2019-01-31)
First release on PyPI.
Metadata
Release files for betwixt 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| betwixt-1.0.0.tar.gz | 21.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| betwixt-1.0.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 30.1 kB
Release files / betwixt-1.0.0.tar.gz
| Download URL | betwixt-1.0.0.tar.gz |
|---|---|
| Size | 21.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
625611d7942b6a2b8019e79a37dd78c984b93104cfe3d855e52a62d53fcf1d13
|
|
BLAKE2b-256 checksum How to use checksums |
5e5e78aae7e53783c1923ba00856a5ace08eb00dd209ba90b75962b9af066986
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.5
|
Release files / betwixt-1.0.0-py2.py3-none-any.whl
| Download URL | betwixt-1.0.0-py2.py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
a93199915a150628648662b7b561d41731244d83a53c721266988ccf91989f11
|
|
BLAKE2b-256 checksum How to use checksums |
c80ca205e3b045e5ec2465401d6030bd9e5d6f8ba114103461bd5c33a9f159ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.7.5
|
Security for betwixt 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.