Helper Functions for `__repr__` Methods
Project description
This is a simple Python module for reducing the boilerplate code involved in writing __repr__ methods. This code works for all Python versions from 2.6 to 3.3.
This software was written by Matthew Lefavor (mclefavor _at_ gmail.com). The license for this software can be found in the LICENSE text file accompanying this distribution.
Installation
Download the reprutils source archive from PyPI. After downloading, unpack the archive and run the standard “python setup.py install” with your favorite Python interpreter:
my-shell$ tar -xf reprutils-1.0.tar.gz my-shell$ cd reprutils-1.0 my-shell$ python setup.py install
You may need to use sudo for the last step, depending on the location of your python interpreter. This package is also installable via pip, and Windows users can use a graphical installer found on the PyPI page.
Overview and Examples
In Python, an object’s __repr__ method is supposed to return a string giving a standard representation of the object. By convention, the strings returned by a __repr__ method should be (whenever possible) a valid Python expression that could return an equivalent object. This means returning a string that looks like a call to the object’s constructor (fully qualified with the module name). But the code to do so is typically repetitive and hard to read, particularly if you wish to want to dynamically access the object’s module and class name. This module provides functions and descriptors for creating some common __repr__ patterns to save time, improve code readability, and promote the good coding practice of creating __repr__ methods for objects (don’t debug without one!).
The standard_repr function is a general-purpose function for creating a constructor-formatted repr as described above. The user can pass any arbitrary values to appear as arguments or keyword arguments to the constructor. That said, most use cases don’t require arbitrary values; the values to appear in the resulting string are attributes of the object. This case is simplified by the GetattrRepr descriptor. Example usages of both are given below; for more information about nitty-gritty details (like the ordering of keyword arguments) and some syntactic sugar, consult the docstrings for standard_repr and GetattrRepr.
Here are two examples of basic usages of both standard_repr and GetattrRepr:
>>> from reprutils import standard_repr, GetattrRepr >>> class DataPoint(object): ... """Represents a single point of data.""" ... def __init__(self, time, value, units=None): ... """Initialize the DataPoint. ... ... @param time: Time (in seconds) of the observation ... @param value: Recorded value at the time of the observation. ... @param units: Units of the measurement ... ... """ ... self.time = time ... self.value = value ... self.units = units ... ... __repr__ = GetattrRepr('time', 'value', units='units') ... >>> dp = DataPoint(1200, 5.3, units="Newtons") >>> dp "__main__.DataPoint(1200, 5.3, units='Newtons')" >>> class DataPoint2(object): ... """Represents a single point of data.""" ... def __init__(self, time, value, units=None): ... """Initialize the DataPoint. ... ... @param time: Time (in seconds) of the observation ... @param value: Recorded value at the time of the observation. ... @param units: Units of the measurement ... ... """ ... self.time = time + 1800 # Correct timezone ... self.value = value - 2.5 # Correct for widget offset ... self.units = units ... ... def __repr__(self): ... """Return a standard representation of the object.""" ... return standard_repr(self, [self.time - 7000, value + 2.5], ... [('units', self.units)]) ... >>> dp = DataPoint2(24000, 2.6, units="Newtons") >>> dp "__main__.DataPoint2(24000, 2.6, units='Newtons')"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file reprutils-1.0.zip
.
File metadata
- Download URL: reprutils-1.0.zip
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e1775d14ca493ad9819401ed5c641af35dffadaadd49715aac11b6d3301d7e7 |
|
MD5 | f68b7e8e6af2415c7af65511dee02da5 |
|
BLAKE2b-256 | 56f518bb40d46e3cc55c3a46cdadd5a74fc9df0119d8e6de415da7301bea2db4 |
File details
Details for the file reprutils-1.0.tar.gz
.
File metadata
- Download URL: reprutils-1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e234bfa606f19d1b0d0e1ce29ddaa2c50450bb4e9e3aa8446bb416e5fde8a92b |
|
MD5 | 7e2f21db6100d71f8a2e29c7e0c9bb9f |
|
BLAKE2b-256 | 9f56b2210b182dc8f95ee8d0fe2352fe1a3b386ae88203eed6d8e529649a7c64 |
File details
Details for the file reprutils-1.0.macosx-10.6-intel.exe
.
File metadata
- Download URL: reprutils-1.0.macosx-10.6-intel.exe
- Upload date:
- Size: 71.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8493be32ca482f202a36f075b1f179d19aaa5ccfa7ddf060dc29972518c2486b |
|
MD5 | 2631db24e19952fc074ec718336abc59 |
|
BLAKE2b-256 | 7fe83ad0cb15b9e7a79f9011c3e057477aaf92f8279faef333035cf5a67f33d5 |