├── .gitignore ├── .travis.yml ├── README.rst ├── infix.py ├── setup.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | *.egg 3 | *.egg-info 4 | dist 5 | build 6 | eggs 7 | parts 8 | bin 9 | var 10 | sdist 11 | develop-eggs 12 | .installed.cfg 13 | lib 14 | lib64 15 | __pycache__ 16 | pip-log.txt 17 | .tox 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: python 3 | python: 4 | - "2.6" 5 | - "2.7" 6 | - "3.2" 7 | - "3.3" 8 | - "3.4" 9 | script: "python infix.py" 10 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ===== 2 | Infix 3 | ===== 4 | 5 | .. image:: http://img.shields.io/pypi/v/infix.svg 6 | :target: https://pypi.python.org/pypi/infix/ 7 | :alt: Latest PyPI version 8 | 9 | .. image:: https://travis-ci.org/borntyping/python-infix.svg 10 | :target: https://travis-ci.org/borntyping/python-infix 11 | :alt: Travis build status 12 | 13 | **This project is an unmaintained experiment. Don't use it for production code.** 14 | 15 | A decorator that allows functions to be used as infix functions. 16 | 17 | Infix operators are created and applied as such, and do not stop the function from being called normally:: 18 | 19 | >>> from infix import shift_infix as infix 20 | ... 21 | >>> @infix 22 | ... def plus(a, b): 23 | ... return a + b 24 | ... 25 | >>> print(40 <> 2) 26 | 42 27 | >>> print(plus(5, 5)) 28 | 10 29 | 30 | Variant syntaxes 31 | ---------------- 32 | 33 | Multiple infix syntaxes are provided by the ``infix`` module:: 34 | 35 | >>> @and_infix 36 | ... def is_before(a, b): 37 | ... return a < b 38 | ... 39 | >>> print(1 &is_before& 5) 40 | True 41 | 42 | >>> @or_infix 43 | ... def minus(a, b): 44 | ... return a - b 45 | ... 46 | >>> print(1 |minus| 2) 47 | -1 48 | 49 | >>> @xor_infix 50 | ... def until(start, stop): 51 | ... return range(start, stop) 52 | ... 53 | >>> print(list(1 ^until^ 10)) 54 | [1, 2, 3, 4, 5, 6, 7, 8, 9] 55 | 56 | >>> @pow_infix 57 | ... def pow(a, b): 58 | ... return a**b 59 | ... 60 | >>> print(3 **pow** 2) 61 | 9 62 | 63 | >>> dpow = div_infix(pow) 64 | >>> print(3 /dpow/ 2) 65 | 9 66 | 67 | Custom syntaxes 68 | ---------------- 69 | 70 | Python has a large set of operators than could be overridden to provide the infix syntax, so instead of providing them all a ``make_infix`` function is provided. To use it, pass the names of the operatorsyou want to support:: 71 | 72 | >>> @make_infix('mod','pow','rshift') 73 | ... def until(start, stop): 74 | ... return list(range(start, stop)) 75 | >>> print(1 %until% 3) 76 | [1, 2] 77 | >>> print(2 <> 4) 78 | [2, 3] 79 | >>> print(5 **until** 8) 80 | [5, 6, 7] 81 | >>> print(until(10,12)) 82 | [10, 11] 83 | 84 | .. versionadded:: 1.2 85 | 86 | The old ``custom_infix`` is still supported. It takes two parameters, the names of the left and right operator functions, though only the first is important:: 87 | 88 | >>> @custom_infix('__rmod__', '__mod__') 89 | ... def ate(a, b): 90 | ... return (a == 7 and b == 9) 91 | ... 92 | >>> print(7 %ate% 9) 93 | True 94 | >>> print(6 %ate% 7) 95 | False 96 | 97 | The left function should be a `right operand `_, and the right functions should be a `left operand `_. 98 | 99 | 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). 100 | 101 | Binding 102 | ------- 103 | 104 | The library now uses a binding feature to generate a temporary object halfway through the process. That means that half expressions now work:: 105 | 106 | >>> 3**pow 107 | 108 | 109 | 110 | .. versionadded:: 1.2 111 | 112 | Example: Currying 113 | ----------------- 114 | 115 | One possible use is in curring functions in Python. You can easily define a 116 | curry function:: 117 | 118 | >>> from functools import partial 119 | >>> curry = or_infix(partial) 120 | >>> def volume(x, y, z): 121 | ... return x * y * z 122 | >>> tot = volume |curry| 2 |curry| 3 |curry| 4 123 | >>> tot() 124 | 24 125 | 126 | Compatibility 127 | ------------- 128 | 129 | Works on all major Python versions (2.6, 2.7, 3.2, 3.3, 3.4). 130 | 131 | Tests 132 | ----- 133 | 134 | 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. 135 | 136 | .. _doctest: http://docs.python.org/3/library/doctest.html 137 | .. _tox: http://testrun.org/tox/ 138 | 139 | Licence 140 | ------- 141 | 142 | Copyright (C) 2013 Sam Clements 143 | 144 | 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: 145 | 146 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 147 | 148 | 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. 149 | -------------------------------------------------------------------------------- /infix.py: -------------------------------------------------------------------------------- 1 | """The infix operator decorator""" 2 | 3 | from __future__ import print_function 4 | 5 | __all__ = ['shift_infix', 'and_infix', 'or_infix', 'xor_infix', 'pow_infix', 6 | 'mul_infix', 'add_infix', 'sub_infix', 'mod_infix', 'make_infix', 7 | 'all_infix','div_infix', 'floordiv_infix'] 8 | 9 | from functools import update_wrapper 10 | import sys 11 | 12 | def lname(op): 13 | """The left name of an op""" 14 | return '__' + op + '__' 15 | 16 | def rname(op): 17 | """The right name of an op, switches shifts""" 18 | shifts = set(['lshift', 'rshift']) 19 | if op in shifts: 20 | op = list(shifts - set([op]))[0] 21 | return '__r' + op + '__' 22 | 23 | class base_infix(object): 24 | """The base infix class""" 25 | 26 | op = () # The operations to use 27 | 28 | def __init__(self, function): 29 | """Creates the decorated function""" 30 | self._function = function 31 | update_wrapper(self, self._function) 32 | ldict = dict() 33 | rdict = dict() 34 | for op in self.op: 35 | ldict[lname(op)] = lbind.__call__ 36 | rdict[rname(op)] = rbind.__call__ 37 | 38 | self.lbind = type('_'.join(self.op)+'_lbind', (lbind,), ldict) 39 | self.rbind = type('_'.join(self.op)+'_rbind', (rbind,), rdict) 40 | 41 | @property 42 | def __call__(self): 43 | """Wraps self""" 44 | return self._function 45 | 46 | def left(self, other): 47 | """Returns a partially applied infix operator""" 48 | return self.rbind(self._function, other) 49 | 50 | def right(self, other): 51 | return self.lbind(self._function, other) 52 | 53 | # Allows binding 54 | # Idea from http://code.activestate.com/recipes/384122-infix-operators/ 55 | 56 | class rbind(object): 57 | def __init__(self, function, binded): 58 | self._function = function 59 | update_wrapper(self, self._function) 60 | self.binded = binded 61 | def __call__(self, other): 62 | return self._function(other, self.binded) 63 | def reverse(self, other): 64 | return self._function(self.binded, other) 65 | def __repr__(self): 66 | return "<{0.__class__.__name__}: Waiting for left side>".format(self) 67 | 68 | class lbind(object): 69 | def __init__(self, function, binded): 70 | self._function = function 71 | update_wrapper(self, self._function) 72 | self.binded = binded 73 | def __call__(self, other): 74 | return self._function(self.binded, other) 75 | def reverse(self, other): 76 | return self._function(other, self.binded) 77 | def __repr__(self): 78 | return "<{0.__class__.__name__}: Waiting for right side>".format(self) 79 | 80 | def make_infix(*ops): 81 | """Returns a custom infix type, using the given operation.""" 82 | ops = list(ops) 83 | if 'div' in ops: 84 | ops += ['truediv'] 85 | opdict = dict(op=ops) 86 | for op in ops: 87 | opdict[lname(op)] = base_infix.left 88 | opdict[rname(op)] = base_infix.right 89 | return type('_'.join(ops)+'_infix', (base_infix,), opdict) 90 | 91 | def custom_infix(left, right = None): 92 | """Legacy support for old notation.""" 93 | op = left[3:-2] 94 | return make_infix(op) 95 | 96 | or_infix = make_infix('or') 97 | mul_infix = make_infix('mul') 98 | pow_infix = make_infix('pow') 99 | shift_infix = make_infix('rshift') 100 | and_infix = make_infix('and') 101 | add_infix = make_infix('add') 102 | sub_infix = make_infix('sub') 103 | mod_infix = make_infix('mod') 104 | xor_infix = make_infix('xor') 105 | div_infix = make_infix('div') 106 | floordiv_infix = make_infix('floor_div') 107 | all_infix = make_infix('or', 'and', 'pow', 'mul', 'xor', 'lshift', 108 | 'rshift', 'sub', 'mod', 'floordiv', 'div') 109 | 110 | if __name__ == "__main__": 111 | from doctest import testfile 112 | failure_count, test_count = testfile('README.rst', globs=locals()) 113 | if failure_count > 0: 114 | sys.exit(1) 115 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from setuptools import setup 4 | 5 | setup( 6 | name = 'infix', 7 | version = '1.2', 8 | license = 'MIT License', 9 | url = 'https://github.com/borntyping/python-infix', 10 | 11 | author = 'Sam Clements', 12 | author_email = 'sam@borntyping.co.uk', 13 | 14 | description = 'Infix operators for Python', 15 | long_description = open('README.rst').read(), 16 | 17 | py_modules = ['infix'], 18 | 19 | classifiers = [ 20 | 'Development Status :: 5 - Production/Stable', 21 | 'Intended Audience :: Developers', 22 | 'License :: OSI Approved :: MIT License', 23 | 'Programming Language :: Python', 24 | 'Programming Language :: Python :: 2', 25 | 'Programming Language :: Python :: 2.6', 26 | 'Programming Language :: Python :: 2.7', 27 | 'Programming Language :: Python :: 3', 28 | 'Programming Language :: Python :: 3.2', 29 | 'Programming Language :: Python :: 3.3', 30 | 'Programming Language :: Python :: 3.4', 31 | 'Topic :: Software Development :: Libraries', 32 | 'Topic :: Utilities', 33 | ], 34 | ) 35 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py26,py27,py32,py33,py34 3 | 4 | [testenv] 5 | commands=python infix.py 6 | 7 | [testenv:release] 8 | commands=python setup.py sdist bdist_wheel upload 9 | skip_sdist=true 10 | deps=wheel 11 | 12 | --------------------------------------------------------------------------------