hprint


Namehprint JSON
Version 2.1.0 PyPI version JSON
download
home_pagehttps://github.com/ruanhao/hprint
SummaryPrint python object in table/json format
upload_time2024-01-23 06:44:16
maintainer
docs_urlNone
authorHao Ruan
requires_python>=3.7, <4
licenseMIT
keywords utils print json
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            hprint
======

Print python object as table/json format in an easy way based on [python-tabulate](https://github.com/astanin/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:

```shell
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.

```pycon
>>> 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

```pycon
>>> 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

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


### Customize Headers

```pycon
>>> 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

```pycon
>>> 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](https://github.com/astanin/python-tabulate/blob/master/README.md#table-format).

```pycon
>>> 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

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

### Raw output

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




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ruanhao/hprint",
    "name": "hprint",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7, <4",
    "maintainer_email": "",
    "keywords": "utils,print,json",
    "author": "Hao Ruan",
    "author_email": "ruanhao1116@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d6/48/a908181c508f7f5ae38a42bd3ded7a06ea7849726c2783df06a277acfdcf/hprint-2.1.0.tar.gz",
    "platform": null,
    "description": "hprint\n======\n\nPrint python object as table/json format in an easy way based on [python-tabulate](https://github.com/astanin/python-tabulate).\n\n\nThe main use cases of the library are:\n\n-   table print list of records (dict)\n-   customize table header name\n-   customize missing value\n-   automatically print json string when fail to print as table\n-   support expanded output, like postgres\n\nInstallation\n------------\n\nTo install the Python library and the command line utility, run:\n\n```shell\npip install -U hprint\n```\n\nLibrary usage\n-------------\n\nThe module provides just one function, `hprint` or `pretty_print`(which is just alias of hprint), which takes a list of\ndict or just a single dict as the first argument, and outputs a\nnicely formatted plain-text table or json string as fallback.\n\n```pycon\n>>> from hprint import pretty_print\n\n>>> data = [{'name': 'John Doe', 'age': 18}, {'name': 'Jane Doe', 'age': 20}]\n\n>>> pretty_print(data)\nname        age\n--------  -----\nJohn Doe     18\nJane Doe     20\n```\n\n### Show index\n\n```pycon\n>>> pretty_print(data, numbered=True)\n  No  name        age    _no\n----  --------  -----  -----\n   1  John Doe     18      1\n   2  Jane Doe     20      2\n>>> pretty_print(data, numbered=True, offset=-1)\n  No  name        age    _no\n----  --------  -----  -----\n   0  John Doe     18      0\n   1  Jane Doe     20      1\n```\n\n## Expanded output\n\n```pycon\n>>> pretty_print(data, x=True)\n-[ RECORD 1 ]--+---------\nname           | John Doe\nage            | 18\n-[ RECORD 2 ]--+---------\nname           | Jane Doe\nage            | 20\n```\n\n\n### Customize Headers\n\n```pycon\n>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'})\nNAME        AGE\n--------  -----\nJohn Doe     18\nJane Doe     20\n>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'}, header=False)\n--------  --\nJohn Doe  18\nJane Doe  20\n--------  --\n>>> pretty_print(data, mappings={'NAME': ('name', lambda n: n.upper()), 'AGE': 'age'})\nNAME        AGE\n--------  -----\nJOHN DOE     18\nJANE DOE     20\n>>> pretty_print(data, mappings={'Aggregate': ('', lambda person: person['name'] + \": \" + str(person['age']))})\nAggregate\n------------\nJohn Doe: 18\nJane Doe: 20\n>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age'}, header=False, x=True)\nNAME           | John Doe\nAGE            | 18\nNAME           | Jane Doe\nAGE            | 20\n```\n\n### Missing value\n\n```pycon\n>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age', 'GENDER': 'gender'})\nNAME        AGE  GENDER\n--------  -----  --------\nJohn Doe     18  n/a\nJane Doe     20  n/a\n>>> pretty_print(data, mappings={'NAME': 'name', 'AGE': 'age', 'GENDER': 'gender'}, missing_value='unknown')\nNAME        AGE  GENDER\n--------  -----  --------\nJohn Doe     18  unknown\nJane Doe     20  unknown\n```\n\n### Table format\n\nSupported format are the same with those supported by [tabulate](https://github.com/astanin/python-tabulate/blob/master/README.md#table-format).\n\n```pycon\n>>> pretty_print(data, tf='plain')\nname        age\nJohn Doe     18\nJane Doe     20\n>>> pretty_print(data, tf='github')\n| name     |   age |\n|----------|-------|\n| John Doe |    18 |\n| Jane Doe |    20 |\n>>> pretty_print(data, tf='pretty')\n+----------+-----+\n|   name   | age |\n+----------+-----+\n| John Doe | 18  |\n| Jane Doe | 20  |\n+----------+-----+\n>>> pretty_print(data, tf='psql')\n+----------+-------+\n| name     |   age |\n|----------+-------|\n| John Doe |    18 |\n| Jane Doe |    20 |\n+----------+-------+\n>>> pretty_print(data, tf='orgtbl')\n| name     |   age |\n|----------+-------|\n| John Doe |    18 |\n| Jane Doe |    20 |\n>>> pretty_print(data, tf='html')\n<table>\n<thead>\n<tr><th>name    </th><th style=\"text-align: right;\">  age</th></tr>\n</thead>\n<tbody>\n<tr><td>John Doe</td><td style=\"text-align: right;\">   18</td></tr>\n<tr><td>Jane Doe</td><td style=\"text-align: right;\">   20</td></tr>\n</tbody>\n</table>\n```\n\n### Print as JSON\n\n```pycon\n>>> pretty_print(data, as_json=True)\n[\n    {\n        \"age\": 18,\n        \"name\": \"John Doe\"\n    },\n    {\n        \"age\": 20,\n        \"name\": \"Jane Doe\"\n    }\n]\n```\n\n### Raw output\n\n```pycon\n>>> s = pretty_print(data, raw=True)\n>>> s\n'name        age\\n--------  -----\\nJohn Doe     18\\nJane Doe     20'\n```\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Print python object in table/json format",
    "version": "2.1.0",
    "project_urls": {
        "Homepage": "https://github.com/ruanhao/hprint"
    },
    "split_keywords": [
        "utils",
        "print",
        "json"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fdcaba3ffe9a0f65d4a7f6b8f177d13b573990f242328192e45dd7c8305401b7",
                "md5": "063ed951408e256f6b00d1317e86f92c",
                "sha256": "4c812fd4bd309f4bfaab4a98472ae6195975896d582ed4b7a1d9436c5c46a477"
            },
            "downloads": -1,
            "filename": "hprint-2.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "063ed951408e256f6b00d1317e86f92c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7, <4",
            "size": 6042,
            "upload_time": "2024-01-23T06:44:14",
            "upload_time_iso_8601": "2024-01-23T06:44:14.233310Z",
            "url": "https://files.pythonhosted.org/packages/fd/ca/ba3ffe9a0f65d4a7f6b8f177d13b573990f242328192e45dd7c8305401b7/hprint-2.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d648a908181c508f7f5ae38a42bd3ded7a06ea7849726c2783df06a277acfdcf",
                "md5": "b43e2432882eda49bfb8c9a03a521ae9",
                "sha256": "bcae092991ff4875bf8b2a6daad1e8ca0777743d5e0d968f9e4341bfa80c7c3d"
            },
            "downloads": -1,
            "filename": "hprint-2.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b43e2432882eda49bfb8c9a03a521ae9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7, <4",
            "size": 5889,
            "upload_time": "2024-01-23T06:44:16",
            "upload_time_iso_8601": "2024-01-23T06:44:16.114095Z",
            "url": "https://files.pythonhosted.org/packages/d6/48/a908181c508f7f5ae38a42bd3ded7a06ea7849726c2783df06a277acfdcf/hprint-2.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-23 06:44:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ruanhao",
    "github_project": "hprint",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "hprint"
}
        
Elapsed time: 0.38429s