Skip to main content

Create __repr__ automatically or declaratively.

Project description

Installation

$ pip install represent

Automatic Generation

from represent import RepresentationMixin


class Rectangle(RepresentationMixin, object):
    def __init__(self, name, color, width, height):
        self.name = name
        self.color = color
        self.width = width
        self.height = height

        super(Rectangle, self).__init__()

rect = Rectangle('Timothy', 'red', 15, 4.5)
print(rect)
Rectangle(name='Timothy', color='red', width=15, height=4.5)

Declarative Generation

from represent import RepresentationHelper


class ContrivedExample(object):
    def __init__(self, description, radians, shape, color, miles):
        self.description = description
        self.degrees = radians * 180 / 3.141592654
        self.shape = shape
        self._color = color
        self.km = 1.60934 * miles

    def __repr__(self):
        r = RepresentationHelper(self)
        r.positional_from_attr('description')
        r.positional_with_value(self.degrees * 3.141592654 / 180)
        r.keyword_from_attr('shape')
        r.keyword_from_attr('color', '_color')
        r.keyword_with_value('miles', self.km / 1.60934)
        return str(r)

ce = ContrivedExample('does something', 0.345, 'square', 'red', 22)
print(ce)
ContrivedExample('does something', 0.345, shape='square', color='red', miles=22.0)

For pretty printer support and more information, visit the documentation!

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

Represent-1.0.1.tar.gz (4.9 kB view details)

Uploaded Source

File details

Details for the file Represent-1.0.1.tar.gz.

File metadata

  • Download URL: Represent-1.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Represent-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4d8651591027fa4c096d4a7b5b2d5885a7d1742ca1e62fb17a2d1b46a26eeca7
MD5 9cc646bea2a7df1f9454200a16b81a90
BLAKE2b-256 8c4f6520af1fc6c9bc55f7b7c021a51fb1ff4286b38101441aa8157216889b83

See more details on using hashes here.

Provenance

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