Package: hprint
hprintPrint python object as table/json format in an easy way based onpython-tabulate.The main use cases of the library are:table print list of records (dict)customize table header namecustomize missing valueautomatically print json string when fail to print as tablesupport expanded output, like postgresInstallationTo install the Python library and the command line utility, run:pipinstall-UhprintLibrary usageThe module provides just one function,hprintorpretty_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.>>>fromhprintimportpretty_print>>>data=[{'name':'John Doe','age':18},{'name':'Jane Doe','age':20}]>>>pretty_print(data)name        age--------  -----John Doe     18Jane Doe     20Show index>>>pretty_print(data,numbered=True)No  name        age    _no----  --------  -----  -----1  John Doe     18      12  Jane Doe     20      2>>>pretty_print(data,numbered=True,offset=-1)No  name        age    _no----  --------  -----  -----0  John Doe     18      01  Jane Doe     20      1Expanded output>>>pretty_print(data,x=True)-[ RECORD 1 ]--+---------name           | John Doeage            | 18-[ RECORD 2 ]--+---------name           | Jane Doeage            | 20Customize Headers>>>pretty_print(data,mappings={'NAME':'name','AGE':'age'})NAME        AGE--------  -----John Doe     18Jane Doe     20>>>pretty_print(data,mappings={'NAME':'name','AGE':'age'},header=False)--------  --John Doe  18Jane Doe  20--------  -->>>pretty_print(data,mappings={'NAME':('name',lambdan:n.upper()),'AGE':'age'})NAME        AGE--------  -----JOHN DOE     18JANE DOE     20>>>pretty_print(data,mappings={'Aggregate':('',lambdaperson:person['name']+": "+str(person['age']))})Aggregate------------John Doe: 18Jane Doe: 20>>>pretty_print(data,mappings={'NAME':'name','AGE':'age'},header=False,x=True)NAME           | John DoeAGE            | 18NAME           | Jane DoeAGE            | 20Missing value>>>pretty_print(data,mappings={'NAME':'name','AGE':'age','GENDER':'gender'})NAME        AGE  GENDER--------  -----  --------John Doe     18  n/aJane Doe     20  n/a>>>pretty_print(data,mappings={'NAME':'name','AGE':'age','GENDER':'gender'},missing_value='unknown')NAME        AGE  GENDER--------  -----  --------John Doe     18  unknownJane Doe     20  unknownTable formatSupported format are the same with those supported bytabulate.>>>pretty_print(data,tf='plain')name        ageJohn Doe     18Jane 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'