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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file custom_repr-2.1.0.tar.gz.
File metadata
- Download URL: custom_repr-2.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d46eb308f68272fdbe7cc7812f340af677b3e629cd27867872be5ff4b74b0ac
|
|
| MD5 |
25e46775e8e44eaa30e54fea31c41038
|
|
| BLAKE2b-256 |
83a01cbcb56012a1c0fa8a66bebfb423446485f8287dfb287299e371e49ca5ef
|
File details
Details for the file custom_repr-2.1.0-py3-none-any.whl.
File metadata
- Download URL: custom_repr-2.1.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0bbd7a6492e8259616533ff558012432df77f56d80ca186832ebed502d6aa4ae
|
|
| MD5 |
e79dd3330a8c0fc96c10292e74e4097f
|
|
| BLAKE2b-256 |
3e083e610dd331e54a8139ab79163339c1b0156bec2c3d9dc424773170536bbd
|