Skip to main content
https://travis-ci.org/wolever/autorepr.svg?branch=master

Now with Python 3 support!

Overview

Python makes classes easy, but __repr__ methods hard. Did you forget to reference self again? Probably. Did have you thought to yourself “eh, this class is real simple, it doesn’t need a repr”? Without a doubt. Was production taken down three times last week because your __str__ returned unicode? … no? Maybe that’s just me.

autorepr makes it simple to build expressive, safe, and correct, __repr__, __str__, __unicode__, and __bytes__ methods in a single line each.

With autorepr, you get the repers you want, without worrying about the fiddly bits (like encoding and decoding), leaving you to focus on your project:

>>> from autorepr import autorepr, autotext
>>> class Person(object):
...     name = u"Alex ☃"
...     height = 123.456
...
...     __repr__ = autorepr(["name", "height:0.1f"])
...     __str__, __unicode__ = autotext("{self.name} ({self.height:0.0f} cm)")
...
>>> p = Person()
>>> repr(p)
"<__main__.Person name=u'Alex \\u2603' height=123.5 at 0x...>"
>>> unicode(p)
u'Alex \u2603 (123 cm)'
>>> str(p)
'Alex \xe2\x98\x83 (123 cm)'

Installation

$ pip install autorepr

Usage

autorepr exposes two main functions:

  • autorepr, which builds a Python-esque __repr__ string by passing either a str.format-style string, or a list of attributes which should be included in a name=value list:

    autorepr(["name", "height:0.1f"]) -->
        "<pkg.Person name=u'Alex \u2603' height=123.5 at 0x...>"
    autorepr("{self.id} name={self.name!r}") -->
        "<pkg.Person 123 name=u'Alex \u2603' at 0x...>"
  • autotext, which uses autostr and autounicode to create __str__ and __unicode__ methods in a Python 2 + 3 friendly way:

    __str__, __unicode__ = autotext("{self.name} ({self.height!d} cm)") -->
        str: 'Alex \xe2\x98\x83 (123cm)'
        unicode: u'Alex \u2603 (123cm)'

And three secondary functions - autostr, autounicode, and autobytes - which build __str__, __unicode__, and __bytes__ functions, respectively. The functions will do their best to avoid Unicode encoding / decoding errors, and will generally Do The Right Thing, even if the inputs aren’t necessarily sensible.

Note: the examples shown here are Python 2, but everything works equally well under Python 3.

>>> from autorepr import autorepr, autotext, autostr, autounicode
>>> class Person(object):
...     name = u"Alex ☃"
...     height = 123.456
...
...     __repr__ = autorepr(["name", "height:0.1f"])
...     __str__, __unicode__ = autotext("{self.name} ({self.height:0.0f} cm)")
...
>>> p = Person()
>>> repr(p)
"<__main__.Person name=u'Alex \\u2603' height=123.5 at 0x...>"
>>> unicode(p)
u'Alex \u2603 (123 cm)'
>>> str(p)
'Alex \xe2\x98\x83 (123 cm)'

Notice that autostr and autorepr (as called here through autotext) are intelligent about converting to/from unicode (decoding/encoding as UTF-8) as necessary:

>>> p.name = u"unicode: ☃"
>>> unicode(p)
u'unicode: \u2603 (123 cm)'
>>> str(p)
'unicode: \xe2\x98\x83 (123 cm)'
>>> p.name = 'utf-8 bytes: \xe2\x98\x83'
>>> unicode(p)
u'utf-8 bytes: \u2603 (123 cm)'
>>> str(p)
'utf-8 bytes: \xe2\x98\x83 (123 cm)'

Note: autostr and autorepr won’t crash on invalid UTF-8 (for example, if autounicode is asked to turn binary data into unicode), but the result is undefined and may not be desirable.

Additional properties can be passed in as kwargs, which will be called with the instance as a parameter:

>>> name_with_len = autostr("{self.name} length={len}",
...                         len=lambda self: len(self.name))
...
>>> p.name = 'Alex'
>>> name_with_len(p)
'Alex length=4'

This works with autorepr’s list mode too:

>>> repr_with_len = autorepr(["name", "len"],
...                          len=lambda self: len(self.name))
...
>>> repr_with_len(p)
"<__main__.Person name='Alex' len=4 at 0x...>"

If a regular format string is passed to autorepr, it will use that instead of the generated string:

>>> repr_with_str = autorepr("{self.name!r}")
>>> repr_with_str(p)
"<__main__.Person 'Alex' at 0x...>"

And of course, if you don’t want your __repr__ to be wrapped in <ClassName ...>, you can use autostr:

>>> repr_with_autostr = autostr("Person({self.name!r})")
>>> repr_with_autostr(p)
"Person('Alex')"

Format specifications can also be passed to autorepr if the default of !r is undesirable (for example, truncating floats):

>>> with_fmt_spec = autorepr(["duration:0.1f", "addr:x", "type!s"],
...                          duration=lambda x: 123.456,
...                          addr=lambda x: 0xabc123,
...                          type=lambda x: "foo")
>>> with_fmt_spec(None)
'<....NoneType duration=123.5 addr=abc123 type=foo at 0x...>'

Metadata

Release files for autorepr 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autorepr 0.3.0
File Size Uploaded
autorepr-0.3.0.tar.gz 8.7 kB Details

Built distributions (wheels)

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

Total release size: 25.9 kB

Release files / autorepr-0.3.0.tar.gz

Download URL autorepr-0.3.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ef770b84793d5433e6bb893054973b8c7ce6b487274f9c3f734f678cae11e85e
BLAKE2b-256 checksum
How to use checksums
15d1d33a2f69b85af4ac67298bf8d8b41db94363e91a48dbdc95aaf49eca5504
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / autorepr-0.3.0-py2.py3-none-any.whl

Download URL autorepr-0.3.0-py2.py3-none-any.whl
Size 8.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1d9010d14fb325d3961e3aa73692685563f97d6ba4a2f0f735329fb37422599c
BLAKE2b-256 checksum
How to use checksums
f7a5339ac779f32f7ed627c84311bb93274906212e5285d5bb80aa5e087161f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / autorepr-0.3.0-py2-none-any.whl

Download URL autorepr-0.3.0-py2-none-any.whl
Size 8.6 kB
Tags Python 2
SHA-256 checksum
How to use checksums
c34567e4073630feb52d9c788fc198085e9e9de4817e3b93b7c4c534fc689f11
BLAKE2b-256 checksum
How to use checksums
0fc6ef25daa81f3f8eb1c714f1e3d538faf18004440ed624e2f91298e43d1bae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.0 This release

3 release files

0.2.0

2 release files

0.1.1

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