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.1.1.tar.gz (4.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.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: custom_repr-2.1.1.tar.gz
  • Upload date:
  • Size: 4.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.1.1.tar.gz
Algorithm Hash digest
SHA256 19d9ad83d96f0e0fa8f73d911edabd1dc46d362993b5c13be6cbbf45bcc98550
MD5 b96951b5c9d92822e580c3de4e6420af
BLAKE2b-256 c1d759ba288613b39195800eec2bd96e6437cd4f4fa453e7be16a71bb3303b2c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: custom_repr-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a529c160b2daaa72dc8b4ae20ebf2676df518cd5a38c312f2ee14ea45dd9b374
MD5 5b251e3daa2d625d6ae922addb4ef40e
BLAKE2b-256 abd9cf6a0557feccab8262a3b805e44b8f9e249c82b7da3de14b7475dc9e92fb

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