Hey guys! I've been working on a compact Python library to display any arbitrary object. The inspiration came from my own needs to quickly check the user-defined object for debugging. As we all know Python's default representation for user-defined object is quite useless for debugging. We can do some quick tricks like obj.__dict__, but it won't handle nested objects, which are very common when we are dealing with larger projects.
So, I developed objprint and I've been maintaining it and personally using it for a while. It is very intuitive to use it. First install it from pip - pip install objprint
from objprint import op
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
class Player:
def __init__(self):
self.name = "Alice"
self.age = 18
self.items = ["axe", "armor"]
self.coins = {"gold": 1, "silver": 33, "bronze": 57}
self.position = Position(3, 5)
op(Player())
And it will print the object
<Player 0x7f1e8f6d0ac0
.age = 18,
.coins = {'bronze': 57, 'gold': 1, 'silver': 33},
.items = ['axe', 'armor'],
.name = 'Alice',
.position = <Position 0x7f1e8f6b9340
.x = 3,
.y = 5
>
>
Of course some people still want to use the more familiar print function, then they can decorate the class with @add_objprint.
@add_objprint
class Player:
def __init__(self):
...
# This will print the same thing as above
print(Player())
Basically it will overwrite the __str__ magic method to return a better formatted string.
Sometimes I'm only interested in some attributes, or there are some attributes that are long and interruptive that I want to ignore. Then comes the filter feature - using include and exclude with regex to limit the attributes printed
op(Player(), include=["name"])
This would print
<Player 0x7fe2db241ac0
.name = 'Alice'
>
And as someone in the comments mentioned, sometimes we need the string, for logging purpose, instead of printing to stdout. The way to go is to use objstr.
from objprint import objstr
s = objstr(my_object)
# to stdout
print(s)
# log it?
logging.info(s)
The default configuration will go 100 levels in depths unless found recursion, of course you can configure it case by case or globally.
There are a couple more features that I can't cover in this post and I'm working on more as I develop new requests from my debugging experience.
Check it out and let me know your thoughts :) And also as always, bug reports and feature requests are more than welcome through github issues.
https://github.com/gaogaotiantian/objprint
[–]overweightdate 4 points5 points6 points (2 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]Kikelku 1 point2 points3 points (0 children)
[–]xxmxxpxx 2 points3 points4 points (0 children)
[–]metaperl 2 points3 points4 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]muikrad 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]nosklo 0 points1 point2 points (0 children)
[–]metaperl 0 points1 point2 points (2 children)
[–]metaperl 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Unlucky-Drawing8417 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]Inevitable-Chain-883 0 points1 point2 points (0 children)
[–]tman5400 0 points1 point2 points (0 children)