Easily implement __repr__, __str__, and __unicode__ methods
Project description
Installation
$ pip install autorepr
Usage
The autorepr function can be used to build a Python-esque __repr__ string by passing either a str.format-style string which will be formatted with self, or a list of attributes which should be included in a name=value list. The autostr and autounicode functions are similar, except they accept only a format string, not a list.
>>> class Person(object):
... name = "Alex"
...
... __repr__ = autorepr(["name"])
... __str__ = autostr("{self.name}")
... __unicode__ = autounicode(__str__)
...
>>> p = Person()
>>> repr(p)
'<__main__.Person name="Alex" 0x...>'
>>> str(p)
'Alex'
>>> unicode(p)
u'Alex'
The autostr and autounicode functions will also be intelligent about converting their input to / from unicode (encoding / decoding as UTF-8) as necessary:
>>> p.name = u"☃"
>>> unicode(p)
u'☃'
>>> str(p)
'\xe2\x98\x83'
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 desierable.
Additional properties can be passed in as kwargs, which will be called with the instance as a paramter:
>>> 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 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}")
>>> repr_with_str(p)
'<__main__.Person "Alex" 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")'
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
Built Distribution
File details
Details for the file autorepr-0.1.1.tar.gz
.
File metadata
- Download URL: autorepr-0.1.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95661d4a36b2115f901362ee8c518ccac7daa505e31721f77dc26e49436434c7 |
|
MD5 | b443749fce97995d678c3c54d05c6432 |
|
BLAKE2b-256 | f891a1039358032d7a89f7550aedf6085e2f3b569c4baff1f175923f188ac8fd |
Provenance
File details
Details for the file autorepr-0.1.1-py2-none-any.whl
.
File metadata
- Download URL: autorepr-0.1.1-py2-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ef7a6bc30cbd49b53579002ee38846e5a03d04697ed3b9a62854d02ee93dd71 |
|
MD5 | 68acf76cbc3b386882cfbab573575161 |
|
BLAKE2b-256 | 0aa7dc3be08b1e4ca8b987c943906dac7f642b29a35ad6731b72223550536744 |