Print objects with data beautifully
Project description
There is an implicit agreement among Pythonistas to create special __repr__ methods that return text closely resembling the code used to construct the object. __repr__ of 1 returns "1", and __repr__ of None returns "None". With this library, you can easily implement __repr__ for your own classes following this convention.
Table of contents
Installation
You can install printo with pip:
pip install printo
You can also use instld to quickly try this package and others without installing them.
Basic usage
The main function in this library is describe_data_object; it returns a string representing what your object's initialization code should look like. There are three required positional parameters:
- The name of the class for which you are creating a representation.
- A
listortupleof positional arguments. - A
dictwith keyword arguments, where the keys are the names of the arguments, and the values are any objects.
Here's a simple example of how it works:
from printo import describe_data_object
print(
describe_data_object(
'MyClassName',
(1, 2, 'some text'),
{'variable_name': 1, 'second_variable_name': 'kek'},
)
)
#> MyClassName(1, 2, 'some text', variable_name=1, second_variable_name='kek')
Filtering
You can prevent individual parameters from being displayed. To do this, pass a dict to the filters parameter. Keys identify arguments by index or name. Values are functions returning bool, where True keeps the argument and False skips it:
print(
describe_data_object(
'MyClassName',
(1, 2, 'some text'),
{'variable_name': 1, 'second_variable_name': 'kek'},
filters={1: lambda x: False if x == 2 else True, 'second_variable_name': lambda x: False},
)
)
#> MyClassName(1, 'some text', variable_name=1)
You can also use the built-in not_none filter to automatically exclude None values:
from printo import not_none
print(
describe_data_object(
'MyClassName',
(1, None),
{},
filters={1: not_none},
)
)
#> MyClassName(1)
Custom display of objects
By default, all argument values are represented in the same way as the standard repr function. There are only three exceptions:
- For regular functions, the function name is displayed.
- For classes, the class name is displayed.
- For lambda functions, just the
λsymbol is displayed, because there is no reliable way to display the source code of a lambda function in Python.
You can provide a custom repr function for each argument value; use the serializer parameter for this:
print(
describe_data_object(
'MyClassName',
(1, 2, 'lol'),
{'variable_name': 1, 'second_variable_name': 'kek'},
serializer=lambda x: repr(x * 2),
)
)
#> MyClassName(2, 4, 'lollol', variable_name=2, second_variable_name='kekkek')
Placeholders
For individual parameters, you can pass predefined strings that will be displayed instead of the actual values. This can be useful, for example, to hide the values of secret fields when serializing objects.
Use the placeholders parameter for this by passing a dictionary, where the keys are argument names (for keyword arguments) or indices (for positional parameters, zero-indexed), and the values are strings:
print(
describe_data_object(
'MySuperClass',
(1, 2, 'lol'),
{'variable_name': 1, 'second_variable_name': 'kek'},
placeholders={
1: '***',
'variable_name': '***',
},
)
)
#> MySuperClass(1, ***, 'lol', variable_name=***, second_variable_name='kek')
🤓 If you set a placeholder for a parameter, the custom serializer will not be applied to it.
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 printo-0.0.15.tar.gz.
File metadata
- Download URL: printo-0.0.15.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7b714e29347fe434e5bb1af33270b09919abc0f3b705eee96bfe84141fe90a1
|
|
| MD5 |
b17bfcf213416acc012e91d31f27bf47
|
|
| BLAKE2b-256 |
572b3da8098a99035ea6b7e1e7d0e9af491abd963e5f6ea6875b641e9e6cf9f8
|
Provenance
The following attestation bundles were made for printo-0.0.15.tar.gz:
Publisher:
release.yml on mutating/printo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
printo-0.0.15.tar.gz -
Subject digest:
d7b714e29347fe434e5bb1af33270b09919abc0f3b705eee96bfe84141fe90a1 - Sigstore transparency entry: 1101627912
- Sigstore integration time:
-
Permalink:
mutating/printo@e5be636cd0f5b3f9c5b6f5a5d219be92c4d62f7f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/mutating
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e5be636cd0f5b3f9c5b6f5a5d219be92c4d62f7f -
Trigger Event:
push
-
Statement type:
File details
Details for the file printo-0.0.15-py3-none-any.whl.
File metadata
- Download URL: printo-0.0.15-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
750923917f396642aae9486c5392b8b87206d8e5c558e7ca1af2407f92102a34
|
|
| MD5 |
2834645f3ac0c59ee916c2966775ee25
|
|
| BLAKE2b-256 |
7b777dab8c7c1cbb4e67fa85fe4f3a3c51df786ad4f553702f79a2a21083e733
|
Provenance
The following attestation bundles were made for printo-0.0.15-py3-none-any.whl:
Publisher:
release.yml on mutating/printo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
printo-0.0.15-py3-none-any.whl -
Subject digest:
750923917f396642aae9486c5392b8b87206d8e5c558e7ca1af2407f92102a34 - Sigstore transparency entry: 1101627914
- Sigstore integration time:
-
Permalink:
mutating/printo@e5be636cd0f5b3f9c5b6f5a5d219be92c4d62f7f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/mutating
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e5be636cd0f5b3f9c5b6f5a5d219be92c4d62f7f -
Trigger Event:
push
-
Statement type: