Skip to main content

Print objects with data beautifully

Project description

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

logo

Pythonistas follow an implicit convention to create special __repr__ methods that return text closely resembling the code used to construct the object. With this library, you can easily implement __repr__ for your own classes to follow 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 the initialization code for your object. There are three required positional parameters:

  • A class name.
  • A list or tuple of positional arguments.
  • A dict of keyword arguments, where the keys are the names of the arguments, and the values are arbitrary 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. The keys identify arguments by index or name. The values are functions that return a boolTrue 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 provided 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 would show them. There are only three exceptions:

  • For regular functions, the function name is displayed.
  • For classes, the class name is displayed.
  • For lambda functions, the complete source code is displayed. However, if a single line of source code contains more than one lambda function, only the λ symbol is displayed (this is a technical limitation of source code reflection in Python).

You can provide a custom serialization function for each argument value via the serializer parameter:

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 arbitrary strings that will be displayed instead of the actual values. This can be useful, for example, to hide the values of sensitive fields when serializing objects.

Pass a dict to the placeholders parameter, 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.

Auto mode

You can remove the boilerplate code by using the @repred decorator for your class:

from printo import repred

@repred
class SomeClass:
    def __init__(self, a, b, c, *args, **kwargs):
        self.a = a
        self.b = b
        self.c = c
        self.args = args
        self.kwargs = kwargs

print(SomeClass(1, 2, 3))
#> SomeClass(1, 2, 3)
print(SomeClass(1, 2, 3, 4, 5))
#> SomeClass(1, 2, 3, 4, 5)
print(SomeClass(1, 2, 3, 4, 5, d=lambda x: x))
#> SomeClass(1, 2, 3, 4, 5, d=lambda x: x)

How does it work? Behind the scenes, the decorator uses AST analysis to generate code. The program attempts to determine which arguments passed to __init__ are stored in which attributes. In other words, it looks for direct assignments of the form self.a = a in the __init__ method.

If there is no direct assignment of a specific argument, an exception will be raised:

@repred
class SomeClass:
    def __init__(self, a):
        ...

#> ...
#> printo.errors.ParameterMappingNotFoundError: No internal object property or custom getter was found for the parameter a.

↑ The error occurs when the class is decorated.

If, for some reason, you are unable to specify this mapping in the body of the __init__ method, you can pass a function for a specific parameter that will extract it:

@repred(getters={'a': lambda x: x.a})
class SomeClass:
    def __init__(self, a):
        self.a = self.convert_a(a)

    def convert_a(self, a):
        return a

print(SomeClass(123))
#> SomeClass(a=123)

By default, @repred displays all arguments as keywords in most cases. However, you can pass the prefer_positional argument to the decorator, which will cause it to prefer omitting argument names in such cases:

@repred
class Class1:
    def __init__(self, a, b):
        self.a = a
        self.b = b

@repred(prefer_positional=True)
class Class2:
    def __init__(self, a, b):
        self.a = a
        self.b = b

print(Class1(123, 456))
#> Class1(a=123, b=456)
print(Class2(123, 456))
#> Class2(123, 456)

⚠️ Automatic mode is currently experimental, so there may be some bugs.

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.17.tar.gz (10.9 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.17-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: printo-0.0.17.tar.gz
  • Upload date:
  • Size: 10.9 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.17.tar.gz
Algorithm Hash digest
SHA256 87b5a2ce79cacb669c4e5f28c7cb5c54f1f8866a4fdde742d6db258f426d920a
MD5 93b92889068c076ddf1f8bb920f7b81b
BLAKE2b-256 f13def158021ee8f93a7f94f355c10c09e1d21622cfba9e07f891cad73fd906a

See more details on using hashes here.

Provenance

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

Publisher: release.yml on mutating/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.17-py3-none-any.whl.

File metadata

  • Download URL: printo-0.0.17-py3-none-any.whl
  • Upload date:
  • Size: 8.9 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.17-py3-none-any.whl
Algorithm Hash digest
SHA256 8994318fc5e87a331a9d1cc21483f2c66632b1a9e60d465cdd6a6e4747a35185
MD5 3293f55b7df5882b747894482e127f25
BLAKE2b-256 596eff87eaecfc891a6fe782797158941c468c311a0e56d2a02c5c553ba9186a

See more details on using hashes here.

Provenance

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

Publisher: release.yml on mutating/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