• Stars
    star
    287
  • Rank 144,232 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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, FORMAT_GENERATOR_COLS
from termcolor import colored

header, rows = format_table(data, fmt=FORMAT_GENERATOR_COLS)
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:

from rapidtables import format_table, FORMAT_GENERATOR

header, rows = format_table(data, fmt=FORMAT_GENERATOR)
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=rapidtables.FORMAT_RAW raw string
  • fmt=rapidtables.FORMAT_GENERATOR generator of strings
  • fmt=rapidtables.FORMAT_GENERATOR_COLS generator of tuples of strings

Align columns:

  • align=rapidtables.ALIGN_LEFT align all columns to left
  • align=rapidtables.ALIGN_NUMBERS_RIGHT align numbers to right (default)
  • align=rapidtables.ALIGN_RIGHT align all columns to right
  • align=rapidtables.ALIGN_CENTER align all columns to center
  • align=rapidtables.ALIGN_HOMOGENEOUS_NUMBERS_RIGHT align numbers to right but consider the table is homogeneous and check col values only to first number or string (works slightly faster)

To predefine aligns, set align to tuple or list:

align=(rapidtables.ALIGN_LEFT, rapidtables.ALIGN_RIGHT, ....)

number of items in list must match number of columns in table.

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

make_table

Generates a ready to output table. Supports basic formats:

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

print_table

The same as make_table, but prints table to stdout.

Benchmarks

(Python 3.7)

benchmarks

Enjoy!

More Repositories

1

rmodbus

Modbus framework for Rust
Rust
117
star
2

psrt

PSRT - an industrial pub/sub server
Rust
112
star
3

busrt

BUS/RT - Modern, fast, Rust-native IPC broker
Rust
106
star
4

pptop

Open, extensible Python injector/profiler/analyzer
Python
63
star
5

myval

Lightweight Apache Arrow data frame for Rust
Rust
62
star
6

eva3

Industrial/Enterprise and home IoT automation platform
Python
49
star
7

shd

Show pretty HDD/SSD list
Rust
48
star
8

finac

Finac - financial accounting for humans
Python
39
star
9

latencymon

TCP/UDP/ICMP latency monitoring
Rust
24
star
10

pulr

pull devices and transform data into events
Rust
22
star
11

yedb-rs

Rugged embedded and client/server key-value database (Rust implementation)
Rust
19
star
12

secureshare

secure file sharing personal server
Python
18
star
13

submap

Rust B-Tree map for pub/sub services
Rust
17
star
14

stm32f1-modbus-example

Rust Modbus context example with stm32 (stm32f103c8t6) & RS485
Rust
16
star
15

atasker

Python library for modern thread / multiprocessing pooling and task processing via asyncio
Python
15
star
16

roboger

Universal event notification broker/manager
Python
13
star
17

icli

interactive command line interfaces for Python
Python
11
star
18

simple-pool

Simple async pool for any kind of resources
Rust
11
star
19

tpm-km

yet another pack of scripts for TPM2+Luks
Shell
11
star
20

bma-benchmark

Benchmark for Rust and humans
Rust
11
star
21

eva-js-framework

Legacy JS Framework for EVA ICS. Supports web browser and CLI (e.g. Node.js)
JavaScript
10
star
22

modbusgw

Simple TCP<->RTU Modbus Gateway Server
Rust
10
star
23

neotermcolor

modern ANSII Color formatting for output in terminal
Python
9
star
24

gmg

Super-lightweight git manager
Rust
9
star
25

smartobject

Simple, powerful Python object manipulation library
Python
5
star
26

tokio-task-pool

Task pool for Tokio Runtime
Rust
5
star
27

owfs-rs

Rust API for OWFS (1-Wire file system)
Rust
5
star
28

aiosched

Python asyncio jobs scheduler
Python
4
star
29

eva-hmi-block_ui

HMI web interface application for EVA ICS
CSS
4
star
30

eva-demo-smarthome

EVA ICS smart home / office demo
Python
4
star
31

pime

Rust Python Integration Made Easy
Rust
3
star
32

yedb

Simple and reliable key-value database
Jinja
3
star
33

hyper-static

A static file handler for Rust/Hyper with minimal logic
Rust
3
star
34

timeouter

Python timeout helper library, works as thread-local or dedicated object
Python
3
star
35

neotasker

Lightweight workers and task management library for Python
Python
3
star
36

psrt-py

Python client for PSRT
Python
2
star
37

sshare

secure share client
Python
2
star
38

tebot

Telegram bot library for Python and humans
Python
2
star
39

idc-core

IDC core module
TypeScript
2
star
40

tagmap

PLC-like lightweight-typing tag map
Rust
2
star
41

object-id

Rust unique object ID
Rust
2
star
42

weatherbroker

Weather broker for Python
Python
2
star
43

bma-jrpc

JSON RPC Client for Rust
Rust
2
star
44

docker-pytpl

Dockerfile
1
star
45

yml2mp

YAML to msgpack CLI converter
Rust
1
star
46

eva-ics-docker

EVA ICS docker image builder and tools
Shell
1
star
47

bma-jrpc-derive

Derive macros for JSON RPC Client
Rust
1
star
48

_common

1
star
49

eva-weatherapp

Weather App for EVA ICS v3
Shell
1
star
50

evaHI

Universal, customizable android app template for web-based interfaces
Java
1
star
51

robogerctl

CLI tool for Roboger server management
Python
1
star
52

yedb-py

Rugged embedded and client/server key-value database (Python implementation)
Python
1
star
53

pyaltt

Various tools for functions, looped and queued workers etc.
Python
1
star
54

eva-phi

Physical interface modules for EVA ICS
Python
1
star
55

eva-demo-farm

EVA ICS virtual farm demo
CSS
1
star
56

bma-benchmark-py

Python benchmark library
Python
1
star
57

eva-plugin-cmon

Controller state monitor plugin for EVA ICS
Python
1
star
58

bma-ts

A timestamp object
Rust
1
star
59

filemap

Python module for processing Kubernetes-style config maps and secrets
Python
1
star
60

evacc

EVA ICS Control Center for Android
Makefile
1
star
61

grafana-fes-datasource

Grafana datasource plugin for Finac Enterprise Server
JavaScript
1
star
62

pyrpush

Push client Python library for Roboger event pager
Python
1
star
63

pyaltt2

Python
1
star
64

eva-telegram

Telegram extension for EVA ICS LM PLC
Python
1
star
65

bitman

A tiny tool to manipulate PLC bits
Rust
1
star
66

snmptools

SNMP tools for Rust
Rust
1
star
67

eva-plugin-alarmer

Alarms plugin for EVA ICS
Python
1
star
68

rsatool

RSA signer / verifier
Rust
1
star
69

jsaltt

TypeScript
1
star
70

eva-plugin-userinfo

User info plugin for EVA ICS
Python
1
star
71

ttsbroker

Simple TTS (Text-To-Speech) broker for Python
Python
1
star
72

webauth

flask auth/oauth wrapper
Python
1
star
73

echannel

Async channel for mixed events
Rust
1
star