Pythonistas follow an implicit convention 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 to follow this convention.
You can install printo with pip:
pip install printoYou can also use instld to quickly try this package and others without installing them.
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
listortupleof positional arguments. - A
dictof 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')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 bool — 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 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)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')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 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.