pyyeti.pp.PP

class pyyeti.pp.PP(var=<object object>, depth=1, tab=4, keylen=40, strlen=80, show_hidden=False)[source]

A simple class for pretty printing data structures.

__init__(var=<object object>, depth=1, tab=4, keylen=40, strlen=80, show_hidden=False)[source]

Initializer for PP.

Parameters:
  • var (any; optional) – If not PP_None, the variable to pretty print.

  • depth (integer; optional) – Maximum number of levels to print.

  • tab (integer; optional) – Number of additional indent spaces for each level.

  • keylen (integer; optional) – Maximum length for dictionary (and similar) keys.

  • strlen (integer; optional) – Maximum length for dictionary (and similar) values.

  • show_hidden (bool; optional) – Many objects (classes, class instances, namespaces, etc) are printed via looping over the .__dict__ method. For those variables, if show_hidden is True, show members that start with ‘_’. Default is to not show them.

Notes

A variable can be printed during instantiation or via PP.pp(). For example:

PP(var, depth=3, tab=8)

gives the same output as:

p = PP(depth=3, tab=8)
p.pp(var)

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from types import SimpleNamespace
>>> from pyyeti.pp import PP
>>> r = np.arange(4, dtype=np.int16)
>>> s = np.zeros((4, 4, 4))
>>> t = np.array(9, dtype=np.uint8)
>>> d = {'asdf': 4,
...      '34': 'value',
...      'r': r,
...      'dataframe': pd.DataFrame(np.ones((3, 3))),
...      'series': pd.Series(np.ones(8)),
...      'longer name': {1: 2,
...                      2: 3,
...                      3: s,
...                      4: SimpleNamespace(a=6,
...                                         b=[1, 23],
...                                         var='string',
...                                         t=(s,))
...      }
... }
>>> PP(d)
<class 'dict'>[n=6]
    'asdf'       : 4
    '34'         : 'value'
    'r'          : int16 ndarray 4 elems: (4,) [0 1 2 3]
    'dataframe'  : pandas DataFrame: (3, 3)
    'series'     : pandas Series: (8,)
    'longer name': <class 'dict'>[n=4]

<...>
>>> PP(d, 5)
<class 'dict'>[n=6]
    'asdf'       : 4
    '34'         : 'value'
    'r'          : int16 ndarray 4 elems: (4,) [0 1 2 3]
    'dataframe'  : pandas DataFrame: (3, 3)
    'series'     : pandas Series: (8,)
    'longer name': <class 'dict'>[n=4]
        1: 2
        2: 3
        3: float64 ndarray 64 elems: (4, 4, 4)
        4: <class 'types.SimpleNamespace'>[n=4]
            .a  : 6
            .b  : [n=2]: [1, 23]
            .var: 'string'
            .t  : [n=1]: (float64 ndarray: (4, 4, 4),)

<...>

To demonstrate the show_hidden option:

>>> class A:
...     a = 9
...     b = {'variable': [1, 2, 3]}
>>> PP(A, 2)
<class 'A'>[n=...]
    .a: 9
    .b: <class 'dict'>[n=1]
        'variable': [n=3]: [1, 2, 3]

<...>
>>> PP(A, 2, show_hidden=1)
<class 'A'>[n=6]
    .__module__ : 'pyyeti.pp'
    .a          : 9
    .b          : <class 'dict'>[n=1]
        'variable': [n=3]: [1, 2, 3]
    .__dict__   : <attribute '__dict__' of 'A' objects>
    .__weakref__: <attribute '__weakref__' of 'A' objects>
    .__doc__    : None

<...>

Methods

__init__([var, depth, tab, keylen, strlen, ...])

Initializer for PP.

pp(var)

Pretty print variable var.