Skip to main content

A shortcut to generate __repr__ methods for python classes

Project description

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”:

__repr__ = make_repr('foo', 'bar')

And to specify a function to create a value for an attribute, using keywords:

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.org/

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.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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

repr-0.2.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

repr-0.2.0-py2.py3-none-any.whl (7.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file repr-0.2.0.tar.gz.

File metadata

  • Download URL: repr-0.2.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for repr-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e250fee2090abb41df12488beb925fb7f1532c50276abb7e0e83b6770b23ee58
MD5 16c8053130e2280c70bbdcbb13987b68
BLAKE2b-256 65adc6dd480616ab193bf0dc9898022f3e506ba478d6dc17c0adeb5e55b6718d

See more details on using hashes here.

File details

Details for the file repr-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for repr-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 00a4090e7a7385cae77eed838cce8a3ffd3f9c38c34db9211a62906f84be68b6
MD5 1595c7039839344eecbd551aed4bd8b3
BLAKE2b-256 45f14171b6845a2dbd95342a31fe66bc7dd6efc528fe04800bd874c28d622ed0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page