Skip to main content

A library that can print Python objects in human readable format

Project description

objprint

build coverage pypi support-version license commit

A library that can print Python objects in human readable format

Install

pip install objprint

Usage

objprint

Use objprint() to print objects.

from objprint import objprint

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)

objprint(Player())
<Player
  .age = 18,
  .coins = {'bronze': 57, 'gold': 1, 'silver': 33},
  .items = ['axe', 'armor'],
  .name = 'Alice',
  .position = <Position
    .x = 3,
    .y = 5
  >
>

add_objprint

If you want to use print() to print your object, you can also use the class decorator add_objprint to add __str__ method for your class.

from objprint import add_objprint

class Position:
    def __init__(self, x, y):
        self.x = x
        self.y = y

@add_objprint
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)

# This will print the same thing as above
print(Player())

objstr

If you want the str representation of the object, instead of printing it on the screen, you can use objstr function

from objprint import objstr

s = objstr(my_object)

include/exclude attributes

You can include/exclude attributes using regular expression so objprint will only print out the attributes you are interested in.

objprint(Player(), include=["name"])
<Player
  .name = 'Alice'
>
objprint(Player(), exclude=[".*s"])
<Player
  .name = 'Alice',
  .age = 18,
  .position = <Position
    .x = 3,
    .y = 5
  >
>

If you specify both include and exclude, it will do a inclusive check first, then filter out the attributes that match exclusive check.

include and exclude arguments work on objprint, objstr and @add_objprint.

config

objprint formats the output based on some configs

  • depth determines how deep objprint goes into nested data structures
  • indent determines the indentation
  • width determines the maximum width a data structure will be presented as a single line
  • elements determines the maximum number of elements that will be displayed

You can set the configs globally using config function

from objprint import config

config(indent=4)

Or you can do a one time config by passing the arguments into objprint function

from objprint import objprint

objprint(var, indent=4)

install

Maybe you don't want to import objprint in every single file that you want to use. You can use install to make it globally accessible

from objprint import install

# Now you can use objprint() in any file
install()

# You can specify a name for objprint()
install("op")
op(my_object)

Bugs/Requests

Please send bug reports and feature requests through github issue tracker.

License

Copyright Tian Gao, 2020.

Distributed under the terms of the Apache 2.0 license.

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

objprint-0.0.4.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

objprint-0.0.4-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file objprint-0.0.4.tar.gz.

File metadata

  • Download URL: objprint-0.0.4.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for objprint-0.0.4.tar.gz
Algorithm Hash digest
SHA256 0651c3e5b24c5750410cea011396fa55dc15b9dd8452bbbcb40e81dbde751487
MD5 aa81745ae961c9294ee93e816c1173fb
BLAKE2b-256 9220fcac05fae5489e1431962a05826a239a50827420484c3e11c06669a1c907

See more details on using hashes here.

File details

Details for the file objprint-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: objprint-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for objprint-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1266db92b0e3068a5781551fb771709646915e346a22d0aa73f9e3c4bbfbd017
MD5 a8d01b8388916aa5753b600a4585419c
BLAKE2b-256 dcb37334406255577bb984b23d1a6269c9210d343efb7f3a7470631cde4b4cdc

See more details on using hashes here.

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