Contents¶
Overview¶
docs | |
---|---|
tests | |
package |
A magic shortcut to generate __repr__ methods for your classes.
- Free software: BSD license
Installation¶
pip install repr
This package contains a single module magic_repr
called so
to not conflict with standart python’s repr
.
Reasoning¶
What do you think each time, writing such code?
def __repr__(self):
return """
Issue(changelog={self.changelog},
type={self.type},
comment={self.comment},
created_at={self.created_at},
resolved_at={self.resolved_at})""".format(self=self).strip().encode('utf-8')
Isn’t this much better and readable?
__repr__ = make_repr('changelog', 'type', 'comment', 'created_at', 'resolved_at')
And this produces much nicer output:
<Issue changelog=<Changelog namespace=u'python'
name=u'geocoder'
source=u'https://github.com/DenisCarriere/geocoder'>
type=u'wrong-version-content'
comment=u'AllMyChanges should take release notes from the web site.'
created_at=datetime.datetime(2016, 6, 17, 6, 44, 52, 16760, tzinfo=<UTC>)
resolved_at=None>
Another advantage of the magic_repr¶
Is it’s recursiveness. If you use magic_repr
for your objects and they
include each other, then representation of the parent object will include
child objects properly nested:
<Foo bars={1: <Bar first=1
second=2
third=3>,
2: <Bar first=1
second=2
third=3>,
u'три': <Bar first=1
second=2
third=3>}>
And all this for free! Just do __repr__ = make_repr()
.
Usage¶
For simple cases it is enough to call make_repr
without arguments. It will figure out
which attributes object has and will output them sorted alphabetically.
You can also specify which attributes you want to include in “representaion”:
from magic_repr import make_repr
__repr__ = make_repr('foo', 'bar')
And to specify a function to create a value for an attribute, using keywords:
from magic_repr import make_repr
class Some(object):
def is_active(self):
return True
Some.__repr__ = make_repr(active=Some.is_active)
Pay attention, that in this case __repr__
was created after the class definition.
This is because inside of the class it can’t reference itself.
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
|
Reference¶
magic_repr¶
-
magic_repr.
make_repr
(*args, **kwargs)[source]¶ Returns __repr__ method which returns ASCII representaion of the object with given fields.
Without arguments,
make_repr
generates a method which outputs all object’s non-protected (non-undercored) arguments which are not callables.Accepts
*args
, which should be a names of object’s attributes to be included in the output:__repr__ = make_repr('foo', 'bar')
If you want to generate attribute’s content on the fly, then you should use keyword arguments and pass a callable of one argument:
__repr__ = make_repr(foo=lambda obj: obj.blah + 100500)
Contributing¶
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
Bug reports¶
When reporting a bug please include:
- Your operating system name and version.
- Any details about your local setup that might be helpful in troubleshooting.
- Detailed steps to reproduce the bug.
Documentation improvements¶
repr could always use more documentation, whether as part of the official repr docs, in docstrings, or even on the web in blog posts, articles, and such.
Feature requests and feedback¶
The best way to send feedback is to file an issue at https://github.com/svetlyak40wt/python-repr/issues.
If you are proposing a feature:
- Explain in detail how it would work.
- Keep the scope as narrow as possible, to make it easier to implement.
- Remember that this is a volunteer-driven project, and that code contributions are welcome :)
Development¶
To set up python-repr for local development:
Fork python-repr (look for the “Fork” button).
Clone your fork locally:
git clone git@github.com:your_name_here/python-repr.git
Create a branch for local development:
git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When you’re done making changes, run all the checks, doc builder and spell checker with tox one command:
tox
Commit your changes and push your branch to GitHub:
git add . git commit -m "Your detailed description of your changes." git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines¶
If you need some code review or feedback while you’re developing the code just make the pull request.
For merging, you should:
- Include passing tests (run
tox
) [1]. - Update documentation when there’s new API, functionality etc.
- Add a note to
CHANGELOG.rst
about the changes. - Add yourself to
AUTHORS.rst
.
[1] | If you don’t have all the necessary python versions available locally you can rely on Travis - it will run the tests for each change you add in the pull request. It will be slower though ... |
Tips¶
To run a subset of tests:
tox -e envname -- py.test -k test_myfeature
To run all the test environments in parallel (you need to pip install detox
):
detox
Authors¶
- Alexander Artemenko - http://dev.svetlyak.ru
Changelog¶
0.3.1 (2016-06-22)¶
- Fixed issue #1 prevented generated __repr__ methods to work in mutithreaded programs.
0.3.0 (2016-06-20)¶
- Now
make_repr
can be used for recursive datastructures.
0.2.1 (2016-06-19)¶
- Documentation improved.
0.2.0 (2016-06-19)¶
- Better handling of nested datastructure.
- Callables as source of the attribute’s value.
- Some documentation.
0.1.0 (2016-06-09)¶
- First release on PyPI.