GithubHelp home page GithubHelp logo

ptzagk / rapidtables Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alttch/rapidtables

0.0 0.0 0.0 218 KB

Super fast list of dicts to pre-formatted tables conversion library for Python 2/3

License: MIT License

Python 100.00%

rapidtables's Introduction

rapidtables

rapidtables is a module for Python 2/3, which does only one thing: converts lists of dictionaries to pre-formatted tables. And it does the job as fast as possible.

rapidtables is focused on speed and is useful for applications which dynamically refresh data in console. The module code is heavily optimized and written purely in Python.

And unlike other similar modules, rapidtables can output pre-formatted generators of strings or even generators of tuples of strings, which allows you to colorize every single column.

Install

pip install rapidtables

Example

# if you need to keep strict column ordering, use OrderedDict for the rows
data = [
    { 'name': 'John', 'salary': 2000, 'job': 'DevOps' },
    { 'name': 'Jack', 'salary': 2500, 'job': 'Architect' },
    { 'name': 'Diana', 'salary': None, 'job': 'Student' },
    { 'name': 'Ken', 'salary': 1800, 'job': 'Q/A' }
]

from rapidtables import format_table
from termcolor import colored

header, rows = format_table(data, fmt=2)
spacer = '  '
print(colored(spacer.join(header), color='blue'))
print(colored('-' * sum([(len(x) + 2) for x in header]), color='grey'))
for r in rows:
    print(colored(r[0], color='white', attrs=['bold']) + spacer, end='')
    print(colored(r[1], color='cyan') + spacer, end='')
    print(colored(r[2], color='yellow'))

colorized cols

Pretty cool, isn't it? Actually, it was the most complex example, you can work with header + table rows already joined:

header, rows = format_table(data, fmt=1)
print(colored(header, color='blue'))
print(colored('-' * len(header), color='grey'))
for r in rows:
    print(colored(r, color='yellow'))

colorized rows

Or you can use make_table function to return the table out-of-the-box (or print_table to instantly print it), and print it in raw:

print_table(data)
name  salary  job
----  ------  ---------
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A

Quick API reference

format_table

Formats a table. Outputs data in raw, generator of strings (one string per row) or generator of tuples of strings (one tuple per row, one string per column):

  • fmt=0 raw string
  • fmt=1 generator of strings
  • fmt=2 generator of tuples of strings

You may also customize headers, separators etc. Read pydoc for more info.

make_table

Generates a ready to output table. Support basic formats:

table = rapidtables.make_table(data, tablefmt='raw')
name  salary  job
-----------------------
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A
table = rapidtables.make_table(data, tablefmt='simple')
name  salary  job
----  ------  ---------
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A
table = rapidtables.make_table(data, tablefmt='md') # Markdown
| name | salary | job       |
|------|--------|-----------|
| John |   2000 | DevOps    |
| Jack |   2500 | Architect |
| Ken  |   1800 | Q/A       |
table = rapidtables.make_table(data, tablefmt='rst') # reStructured Text
====  ======  =========
name  salary  job
====  ======  =========
John    2000  DevOps
Jack    2500  Architect
Ken     1800  Q/A
====  ======  =========

print_table

The same as make_table, but prints table to stdout.

Benchmarks

(Python 3.7)

benchmark

Enjoy!

rapidtables's People

Contributors

divi255 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.