Skip to content

trivrepr - a helper for generating repr() strings

[Home | GitLab | PyPI | ReadTheDocs]

Overview

The trivrepr module contains the TrivialRepr class that may be derived from to provide an implementation of __repr__() for simple classes that have all their attributes passed directly to the constructor.

import trivrepr

class KeyValue(trivrepr.TrivialRepr):
    def __init__(self, key, value, more='hi'):
        super(KeyValue, self).__init__()
        self.key = key
        self.value = value
        self.more = more

kv = KeyValue(key='key', value='val')
print(repr(kv))

This program will output:

KeyValue(key='key', value='val', more='hi')

The TrivialReprWithJson class adds a .to_json() method that returns the same fields that __repr__() will output as a dictionary.

Contact

The trivrepr library was written by Peter Pentchev. It is developed in a GitLab repository. This documentation is hosted at Ringlet with a copy at ReadTheDocs.