Skip to main content

Makes civilized __repr__, __str__, and __unicode__ methods

Project description

Python makes it easy to make a class, but annoying to specify how that class is represented as a string. Did you forget to reference self again? Probably. Did you just spend an hour trying to remember how to handle unicode? Almost certainly.

autorepr lets you customize __repr__, __str__, and __unicode__ methods in a single line each. They’ll let you see any number of attributes, just the way you want to see them.

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.

Installation

$ pip install autorepr

Usage

autorepr consists of three functions. autorepr 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. The autostr and autounicode functions are similar, and will do their best to avoid Unicode encoding / decoding errors.

>>> from autorepr import autorepr, autostr, autounicode
>>> class Person(object):
...     name = u"Alex ☃"
...     height = 123.456
...
...     __repr__ = autorepr(["name", "height:0.1f"])
...     __str__ = autostr("{self.name} ({self.height:0.0f} cm)")
...     __unicode__ = autounicode(__str__)
...
>>> 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 autounicode are intelligent about converting their input 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 undesierable (for example, turncating 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)
'<__builtin__.NoneType duration=123.5 addr=abc123 type=foo at 0x...>'

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

autorepr-0.2.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

autorepr-0.2.0-py2-none-any.whl (7.1 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for autorepr-0.2.0.tar.gz
Algorithm Hash digest
SHA256 645f0e09c08736bfc877b439746ce48d21ee4a17f7be3f5ad87808593bc26ec3
MD5 662d6922b8cf10913bc2c15c7c6c257b
BLAKE2b-256 6133e02d21ab85b1bb162bc3420e5c33c0dd7a8dfc8436f68219bbcfa2fa01f0

See more details on using hashes here.

File details

Details for the file autorepr-0.2.0-py2-none-any.whl.

File metadata

File hashes

Hashes for autorepr-0.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 83a9c26d9c09bfb65b2d26ea0fbc1775ce0d8ac6a9750e3222f7c45cf71dbd1c
MD5 2bf8337c60df8ce014a6808b204029c7
BLAKE2b-256 44a16a26ce41988ac36ad4fcc8fb4203b51216571b153993a05ee82ee2cc8618

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