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')"
Metadata
Release files for reprutils 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distributions (sdists)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| reprutils-1.0.zip | 8.4 kB | Without provenance | Details | |
| reprutils-1.0.tar.gz | 6.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| reprutils-1.0.macosx-10.6-intel.exe | Details |
Total release size: 86.4 kB
Release files / reprutils-1.0.zip
| Download URL | reprutils-1.0.zip |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7e1775d14ca493ad9819401ed5c641af35dffadaadd49715aac11b6d3301d7e7
|
|
BLAKE2b-256 checksum How to use checksums |
56f518bb40d46e3cc55c3a46cdadd5a74fc9df0119d8e6de415da7301bea2db4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / reprutils-1.0.tar.gz
| Download URL | reprutils-1.0.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e234bfa606f19d1b0d0e1ce29ddaa2c50450bb4e9e3aa8446bb416e5fde8a92b
|
|
BLAKE2b-256 checksum How to use checksums |
9f56b2210b182dc8f95ee8d0fe2352fe1a3b386ae88203eed6d8e529649a7c64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / reprutils-1.0.macosx-10.6-intel.exe
| Download URL | reprutils-1.0.macosx-10.6-intel.exe |
|---|---|
| Size | 71.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8493be32ca482f202a36f075b1f179d19aaa5ccfa7ddf060dc29972518c2486b
|
|
BLAKE2b-256 checksum How to use checksums |
7fe83ad0cb15b9e7a79f9011c3e057477aaf92f8279faef333035cf5a67f33d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for reprutils 1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.