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.0.0.tar.gz (4.8 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.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: custom_repr-2.0.0.tar.gz
  • Upload date:
  • Size: 4.8 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.0.0.tar.gz
Algorithm Hash digest
SHA256 8c5a5d588f040206a481829a251392269348f6068167f43cdc631966f647f9cc
MD5 d5824245da6163e46d2a9e4702958b0f
BLAKE2b-256 40c228371e8996b009c422b1c314743c03abecb773df1788e0e76e726890272a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: custom_repr-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aea979e5a40b9890ddc52b6c11573af2fcb56d36961da553643f110034476d25
MD5 81fdad282946e4aadf0d3e6a627c1626
BLAKE2b-256 5f14d9a0f7e9ab21579a430d8e470363c937859f471bdc58882b94002d4fd528

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