Skip to main content

Print python object in table/json format

Project description

hprint

Print python object as table/json format in an easy way based on python-tabulate.

The main use cases of the library are:

  • table print list of records (dict)
  • customize table header name
  • customize missing value
  • automatically print json string when fail to print as table
  • support expanded output, like postgres

Installation

To install the Python library and the command line utility, run:

pip install -U hprint

Library usage

The module provides just one function, hprint or pretty_print(which is just alias of hprint), which takes a list of dict or just a single dict as the first argument, and outputs a nicely formatted plain-text table or json string as fallback.

>>> from hprint import pretty_print

>>> data = [{'name': 'John Doe', 'age': 18}, {'name': 'Jane Doe', 'age': 20}]

>>> pretty_print(data)
name        age
--------  -----
John Doe     18
Jane Doe     20

Show index

>>> pretty_print(data, numbered=True)
  No  name        age    _no
----  --------  -----  -----
   1  John Doe     18      1
   2  Jane Doe     20      2
>>> pretty_print(data, numbered=True, offset=-1)
  No  name        age    _no
----  --------  -----  -----
   0  John Doe     18      0
   1  Jane Doe     20      1

Expanded output

>>> pretty_print(data, x=True)
-[ RECORD 1 ]--+---------
name           | John Doe
age            | 18
-[ RECORD 2 ]--+---------
name           | Jane Doe
age            | 20

Customize Headers

>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'})
NAME        AGE
--------  -----
John Doe     18
Jane Doe     20
>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'}, header=False)
--------  --
John Doe  18
Jane Doe  20
--------  --
>>> pretty_print(data, mappings={'NAME': ('name', lambda n: n.upper()), 'AGE': 'age'})
NAME        AGE
--------  -----
JOHN DOE     18
JANE DOE     20
>>> pretty_print(data, mappings={'Aggregate': ('', lambda person: person['name'] + ": " + str(person['age']))})
Aggregate
------------
John Doe: 18
Jane Doe: 20
>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'}, header=False, x=True)
NAME           | John Doe
AGE            | 18
NAME           | Jane Doe
AGE            | 20

Missing value

>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age', 'GENDER': 'gender'})
NAME        AGE  GENDER
--------  -----  --------
John Doe     18  n/a
Jane Doe     20  n/a
>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age', 'GENDER': 'gender'}, missing_value='unknown')
NAME        AGE  GENDER
--------  -----  --------
John Doe     18  unknown
Jane Doe     20  unknown

Table format

Supported format are the same with those supported by tabulate.

>>> pretty_print(data, tf='plain')
name        age
John Doe     18
Jane Doe     20
>>> pretty_print(data, tf='github')
| name     |   age |
|----------|-------|
| John Doe |    18 |
| Jane Doe |    20 |
>>> pretty_print(data, tf='pretty')
+----------+-----+
|   name   | age |
+----------+-----+
| John Doe | 18  |
| Jane Doe | 20  |
+----------+-----+
>>> pretty_print(data, tf='psql')
+----------+-------+
| name     |   age |
|----------+-------|
| John Doe |    18 |
| Jane Doe |    20 |
+----------+-------+
>>> pretty_print(data, tf='orgtbl')
| name     |   age |
|----------+-------|
| John Doe |    18 |
| Jane Doe |    20 |
>>> pretty_print(data, tf='html')
<table>
<thead>
<tr><th>name    </th><th style="text-align: right;">  age</th></tr>
</thead>
<tbody>
<tr><td>John Doe</td><td style="text-align: right;">   18</td></tr>
<tr><td>Jane Doe</td><td style="text-align: right;">   20</td></tr>
</tbody>
</table>

Print as JSON

>>> pretty_print(data, as_json=True)
[
    {
        "age": 18,
        "name": "John Doe"
    },
    {
        "age": 20,
        "name": "Jane Doe"
    }
]

Raw output

>>> s = pretty_print(data, raw=True)
>>> s
'name        age\n--------  -----\nJohn Doe     18\nJane Doe     20'

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

hprint-2.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

hprint-2.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file hprint-2.1.0.tar.gz.

File metadata

  • Download URL: hprint-2.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for hprint-2.1.0.tar.gz
Algorithm Hash digest
SHA256 bcae092991ff4875bf8b2a6daad1e8ca0777743d5e0d968f9e4341bfa80c7c3d
MD5 b43e2432882eda49bfb8c9a03a521ae9
BLAKE2b-256 d648a908181c508f7f5ae38a42bd3ded7a06ea7849726c2783df06a277acfdcf

See more details on using hashes here.

File details

Details for the file hprint-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: hprint-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for hprint-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c812fd4bd309f4bfaab4a98472ae6195975896d582ed4b7a1d9436c5c46a477
MD5 063ed951408e256f6b00d1317e86f92c
BLAKE2b-256 fdcaba3ffe9a0f65d4a7f6b8f177d13b573990f242328192e45dd7c8305401b7

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