├── .gitignore ├── LICENSE ├── README.rst ├── closurepr.py ├── setup.py └── test └── test_closurepr.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | *.egg-info/ 22 | .installed.cfg 23 | *.egg 24 | 25 | # PyInstaller 26 | # Usually these files are written by a python script from a template 27 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 28 | *.manifest 29 | *.spec 30 | 31 | # Installer logs 32 | pip-log.txt 33 | pip-delete-this-directory.txt 34 | 35 | # Unit test / coverage reports 36 | htmlcov/ 37 | .tox/ 38 | .coverage 39 | .cache 40 | nosetests.xml 41 | coverage.xml 42 | 43 | # Translations 44 | *.mo 45 | *.pot 46 | 47 | # Django stuff: 48 | *.log 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | 53 | # PyBuilder 54 | target/ 55 | 56 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2016 Eeo Jun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | closurepr: debuggable closures 2 | ============================== 3 | 4 | Provides a super lightweight and magical wrapper around functions 5 | that return functions, to make functional programming in Python more 6 | friendly: 7 | 8 | .. code-block:: python 9 | 10 | >>> from closurepr import rich_repr 11 | >>> @rich_repr 12 | def adder(a): 13 | return lambda b: a+b 14 | 15 | >>> adder(1) 16 | '' 17 | 18 | You don't strictly need it. But you want it. 19 | 20 | Previous literature: 21 | -------------------- 22 | 23 | - http://stackoverflow.com/questions/32284395/should-extra-layers-be-added-just-to-support-repr/32285982 24 | - https://bugs.python.org/issue24056 25 | -------------------------------------------------------------------------------- /closurepr.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from functools import wraps 3 | 4 | 5 | IS_PY3 = sys.version_info[0] == 3 6 | 7 | 8 | def format_funcname(name, args, kwargs): 9 | varargs = ','.join(repr(v) for v in args) 10 | kwdargs = ','.join('%s=%r' % (k, kwargs[k]) for k in kwargs) 11 | kwdargs = ',%s' % (kwdargs) if (varargs and kwdargs) else kwdargs 12 | 13 | return '%s(%s%s)' % (name, varargs, kwdargs) 14 | 15 | 16 | def rich_repr(fn): 17 | name = fn.__name__ 18 | 19 | @wraps(fn) 20 | def wrapper(*args, **kwargs): 21 | closure = fn(*args, **kwargs) 22 | string = format_funcname(name, args, kwargs) 23 | if IS_PY3: 24 | closure.__qualname__ = string 25 | else: 26 | closure.func_name = string 27 | return closure 28 | return wrapper 29 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | setup( 3 | name='closurepr', 4 | version='0.0.1', 5 | py_modules=['closurepr'], 6 | ) 7 | -------------------------------------------------------------------------------- /test/test_closurepr.py: -------------------------------------------------------------------------------- 1 | from re import search 2 | from unittest import TestCase 3 | from closurepr import rich_repr 4 | 5 | 6 | @rich_repr 7 | def adder(a, b): 8 | return lambda c: (a + b + c) 9 | 10 | 11 | def rr(obj): 12 | match = search(r'', repr(obj)) 13 | assert match 14 | name, _ = match.groups() 15 | return name 16 | 17 | 18 | class TestRichRepr(TestCase): 19 | def test_simple(self): 20 | add = adder(1, 2) 21 | assert rr(add) == 'adder(1,2)' 22 | assert add(3) == 6 23 | 24 | def test_complex(self): 25 | add = adder(1, b=2) 26 | assert rr(add) == 'adder(1,b=2)' 27 | assert add(3) == 6 28 | 29 | def test_kwargsonly(self): 30 | add = adder(a=1, b=2) 31 | assert rr(add) in [ 32 | 'adder(a=1,b=2)', 33 | 'adder(b=2,a=1)', 34 | ] 35 | assert add(3) == 6 36 | --------------------------------------------------------------------------------