Skip to main content

A pretty printer for SimpleNamespace

Project description

Pretty SimpleNamespace


Table of Contents


What is it?

  • A stringifier and formatter for SimpleNamespace which attempts to make the data as readable as possible.

Why create it?

  • I use SimpleNamespace often to hold state and needed a way to print it out for debugging purposes.

Simple usage

from pretty_simple_namespace import pprint
from types import SimpleNamespace as o

joe = o(
    name={"first": "joe", "last": "schmo"},
    age=30,
    favoriteFoods=["apples", "steak"],
)

pprint(joe)
# prints
# {
#   name: {
#     first: 'joe'
#     last: 'schmo'
#   }
#   age: 30
#   favoriteFoods: [
#     'apples'
#     'steak'
#   ]
# }

Features

  • handles recursive structures by tracking and printing references nicely
  • recurses into types list, dict and SimpleNamespace for now
  • has special-case printing for types bool, str, callable and None
    • booleans and None are printed lowercase
    • strings are wrapped in single quotes
    • callable appends () e.g. myMethod(). Arguments aren't represented
  • all other types are printed by wrapping it in str e.g. str(userDefinedType)

Limitations

  • multi-line strings look ugly
  • doesn't have a way to recurse into structures other than what's listed above

Related projects


Api

format(something, indent=2) => str

pprint(something, indent=2) => None

  • just prints the formated something

wrapWith(*, indent) => Wrapped module

  • use this when you want to call format or pprint with a different default indent value so you don't have to pass it manually all the time.

    e.g.

    from pretty_simple_namespace import wrapWith
    
    pprint = wrapWith(indent=4).pprint
    pprint(o(tabbed4spaces=True))
    # {
    #     tabbed4spaces: true
    # }
    

Wrapped module

  • just an instance of SimpleNamespace with two attributes format and pprint.

Test

#
# you must have poetry installed
#
$ poetry shell
$ poetry install
$ python runTests.py

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

pretty_simple_namespace-0.1.1.tar.gz (914.4 kB view details)

Uploaded Source

Built Distribution

pretty_simple_namespace-0.1.1-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file pretty_simple_namespace-0.1.1.tar.gz.

File metadata

  • Download URL: pretty_simple_namespace-0.1.1.tar.gz
  • Upload date:
  • Size: 914.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.15 CPython/3.7.3 Linux/5.1.2-arch1-1-ARCH

File hashes

Hashes for pretty_simple_namespace-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7579dcb10091defe84d3702aa0680b8ccf4ee00f93e8b0bf85195a7b93aef4af
MD5 efb4f8fc0f391588fad309f74f2e5c47
BLAKE2b-256 1edf54830ecc0bc254b7a53e160f734fcad933a69c901c42cadaca71892c5369

See more details on using hashes here.

File details

Details for the file pretty_simple_namespace-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pretty_simple_namespace-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 17cb165fa3a3e55605c6f3204bf077d62fc53a7005afe2de3cb9c4ed6c2718a5
MD5 c3d1d72716c1debc3799b1070e8a56e9
BLAKE2b-256 b28c461c20ac66fe60e912985ec578fe9cebad62033198e559482d6fc6311ba7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page