Skip to main content

Overview

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

https://python-repr.readthedocs.io/

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

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.

Metadata

Release files for repr 0.3.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 repr 0.3.1
File Size Uploaded
repr-0.3.1.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for repr 0.3.1
File Interpreter ABI Platform
repr-0.3.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 24.0 kB

Release files / repr-0.3.1.tar.gz

Download URL repr-0.3.1.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0684f8cbf452d0b4f4f7eaba2eeb3ea3478bbf67153df3afbc796bd9f3914a87
BLAKE2b-256 checksum
How to use checksums
cfd9891955bf9e29cd6ab1000b9c66cee157070ac74e172a738d3388719fb49a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / repr-0.3.1-py2.py3-none-any.whl

Download URL repr-0.3.1-py2.py3-none-any.whl
Size 7.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
29f4c3a73f3a6d98551fe47759dbccf8a4f5c188622914f0449dd7e455196e74
BLAKE2b-256 checksum
How to use checksums
e9fe6421e10e77d544079bda49cc06f481a1ef5950a51e638025bb6701bed6ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

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