Skip to main content

A lightweight Python library to automatically generate clean __repr__ methods for all your user-declared classes.

Project description

Custom Repr Library

A lightweight Python library designed to offer a custom representation for user-defined classes. This library enables users to visualize class attributes and methods with enhanced formatting using the Rich library. Please be aware that this library employs monkey patching so it should be use with caution.

Installation

To install the library, use pip:

pip install custom-repr

Usage

Basic Usage

To use the custom representation, simply import the library:

import custom_repr

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f"Hello, my name is {self.name}."

# Create an instance
person = Person("Alice", 30)

print(person) # The output will be: 
# Person => { name: "Alice", age: 30 }
# [ greet() ] 

Configuration

You can configure the output format of the custom representation using the custom_repr_config function. This function allows you to choose whether to display attributes, methods, or both.

Function Signature

from custom_repr import custom_repr_config

custom_repr_config(attributes=True, methods=True)

Examples

  • Show both attributes and methods (default behavior):

    custom_repr_config(True, True)
    
  • Show only attributes:

    custom_repr_config(True, False)
    
  • Show only methods:

    custom_repr_config(False, True)
    
  • Show only the class name:

    custom_repr_config(False, False)
    

Example

from custom_repr import custom_repr_config

# Configure to show only attributes
custom_repr_config(True, False)

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

    def drive(self):
        return "Vroom!"

car = Car("Toyota", "Corolla")
print(car) # Outputs: Car => { make: "Toyota", model: "Corolla" }

# Configure to show only the class name
custom_repr_config(False, False)
print(car)  # Outputs: Car

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

custom_repr-2.3.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

custom_repr-2.3.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file custom_repr-2.3.0.tar.gz.

File metadata

  • Download URL: custom_repr-2.3.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for custom_repr-2.3.0.tar.gz
Algorithm Hash digest
SHA256 30ade47d38cfab15096540615d89b02d425b3d951df26d95c5fb41b45ac4ef62
MD5 48c94ac6f1c50a94e60a2d9431d86ace
BLAKE2b-256 df0f15cd7d58a66c1506fa3f60a3e56e87133cf93aafa0174e9554a375d4ce2d

See more details on using hashes here.

File details

Details for the file custom_repr-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: custom_repr-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for custom_repr-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a86e16900b05e8192c95d94b22da7bba0c3b764efb257953bb2df15d2e4f1c2a
MD5 11b7c73e7652ec018999b2b28240f074
BLAKE2b-256 ff0341aa43e4aebca122146c00deaf42f635529b45003b9430cac6b00c7a3b3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page