Skip to main content

Print objects with data beautifully

Project description

logo

Downloads Downloads Coverage Status Lines of code Hits-of-Code Test-Package Python versions PyPI version Checked with mypy Ruff

There is an implicit agreement among pythonists to create special __repr__ methods for classes that return text as similar as possible to the piece of code where the specific object was constructed. __repr__ of 1 returns "1", and repr of None returns "None". With this library, you can create your own classes, the objects of which will obey this rule.

Table of contents

Installation

You can install dirstree using pip:

pip install printo

You can also quickly try out this and other packages without having to install using instld.

Basic usage

The main function in this library is descript_data_object, it returns a string representing what your object's initialization code should look like. There are 3 required positional parameters:

  • The name of the class for which we are creating a representation.
  • A list or tuple of positional arguments.
  • A dict with named 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 descript_data_object

print(descript_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 fields from being displayed. To do this, pass a dict as the filters parameter, in which the argument numbers (counting starts from 0) for positional arguments or the argument names for named arguments will be used as keys, and returning bool functions (each of them answers the question "whether to display this argument", where True means "yes" and False means "no") will be used as values:

print(descript_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 save a few characters by specifying a function as a filter that automatically filters None of the values:

from printo import not_none

print(descript_data_object('MyClassName', (1, None), {}, filters={1: not_none}))
#> MyClassName(1)

Custom display of objects

By default, all your objects are serialized in the same way as the standard repr function does. There are only 2 exceptions:

  • Ordinary functions, in their case, instead of the usual text, just the function name is displayed.
  • Lambda functions, just the λ symbol is displayed instead. This is done because there is no reliable way to display the source code of a lambda function in Python.

You can use your own function as a repr for all your objects, use the serializator parameter for this:

from printo import descript_data_object

print(
    descript_data_object(
        'MyClassName',
        (1, 2, 'lol'),
        {'variable_name': 1, 'second_variable_name': 'kek'},
        serializator=lambda x: repr(x * 2),
    )
)
#> MyClassName(2, 4, 'lollol', variable_name=2, second_variable_name='kekkek')

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

printo-0.0.7.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.

printo-0.0.7-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file printo-0.0.7.tar.gz.

File metadata

  • Download URL: printo-0.0.7.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for printo-0.0.7.tar.gz
Algorithm Hash digest
SHA256 29914683b81a6da05d06de803f8c37b8fdf88fe4cb3dea5847220cf03b35278c
MD5 a7737f4c92e9ee453e70d7670419e47f
BLAKE2b-256 33e39ea8e8e6f86a5f739177fbed0cd2a1c270d0eb20d22c8f4b4c90676d48e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for printo-0.0.7.tar.gz:

Publisher: release.yml on pomponchik/printo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file printo-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: printo-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for printo-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b3103956d5bfb54bc09ea3738eaced25e6a9d541abc6dd437cd7274c5584a6e9
MD5 11021ee7e0688e3ecded76016d5349a2
BLAKE2b-256 8917003b074c18fe9ab32e65b6041b05830a90ae637a3bc9f3d3422fc0554e01

See more details on using hashes here.

Provenance

The following attestation bundles were made for printo-0.0.7-py3-none-any.whl:

Publisher: release.yml on pomponchik/printo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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