Merge pull request #3 from nirum/contextmanager

Creates a contextmanager for live updating of tables
This commit is contained in:
Niru Maheswaranathan
2017-05-26 13:39:31 -07:00
committed by GitHub
4 changed files with 60 additions and 7 deletions

View File

@@ -49,6 +49,7 @@ Hosted at Read The Docs: [tableprint.readthedocs.org](http://tableprint.readthed
- `six` - `six`
## Version ## Version
- 0.7.0 (May 26 2017) Adds a TableContext context manager for easy creation of dynamic tables (tables that update periodically). Adds the ability to pass a list or tuple of widths to specify different widths for different columns
- 0.6.9 (May 25 2017) Splitting the tableprint.py module into a pacakge with multiple files - 0.6.9 (May 25 2017) Splitting the tableprint.py module into a pacakge with multiple files
- 0.6.7 (May 25 2017) Fixes some bugs with ANSI escape sequences - 0.6.7 (May 25 2017) Fixes some bugs with ANSI escape sequences
- 0.5.0 (Sept 29 2016) Better handling of ANSI escape sequences in table rows - 0.5.0 (Sept 29 2016) Better handling of ANSI escape sequences in table rows

View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Version info # Version info
__version__ = '0.6.9' __version__ = '0.7.0'
__license__ = 'MIT' __license__ = 'MIT'
# Project description(s) # Project description(s)

View File

@@ -21,13 +21,56 @@ from six import string_types
from .style import LineStyle, STYLES from .style import LineStyle, STYLES
from .utils import ansi_len, format_line, parse_width from .utils import ansi_len, format_line, parse_width
__all__ = ('table', 'header', 'row', 'hrule', 'top', 'bottom', 'banner', 'dataframe') __all__ = ('table', 'header', 'row', 'hrule', 'top', 'bottom', 'banner', 'dataframe', 'TableContext')
STYLE = 'round' STYLE = 'round'
WIDTH = 11 WIDTH = 11
FMT = '5g' FMT = '5g'
class TableContext:
def __init__(self, headers, width=WIDTH, style=STYLE, add_hr=True, out=sys.stdout):
"""Context manager for table printing
Parameters
----------
headers : array_like
A list of N strings consisting of the header of each of the N columns
width : int or array_like, optional
The width of each column in the table (Default: 11)
style : string or tuple, optional
A formatting style. (Default: 'round')
add_hr : boolean, optional
Whether or not to add a horizontal rule (hr) after the headers
Usage
-----
>>> with TableContext("ABC") as t:
for k in range(10):
t.row(np.random.randn(3))
"""
self.out = out
self.config = {'width': width, 'style': style}
self.headers = header(headers, add_hr=add_hr, **self.config)
self.bottom = bottom(len(headers), **self.config)
def __call__(self, data):
self.out.write(row(data, **self.config) + '\n')
self.out.flush()
def __enter__(self):
self.out.write(self.headers + '\n')
self.out.flush()
return self
def __exit__(self, *exc):
self.out.write(self.bottom + '\n')
self.out.flush()
def table(data, headers=None, format_spec=FMT, width=WIDTH, style=STYLE, out=sys.stdout): def table(data, headers=None, format_spec=FMT, width=WIDTH, style=STYLE, out=sys.stdout):
"""Print a table with the given data """Print a table with the given data

View File

@@ -1,12 +1,21 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from tableprint import table, banner, dataframe, hrule from tableprint import table, banner, dataframe, hrule, TableContext
from io import StringIO from io import StringIO
import numpy as np import numpy as np
def test_table(): def test_context():
"""Tests the table context manager"""
output = StringIO()
with TableContext('ABC', style='round', width=5, out=output) as t:
t([1, 2, 3])
t([4, 5, 6])
assert output.getvalue() == '╭─────┬─────┬─────╮\n│ A │ B │ C │\n├─────┼─────┼─────┤\n│ 1│ 2│ 3│\n│ 4│ 5│ 6│\n╰─────┴─────┴─────╯\n'
def test_table():
"""Tests the table function"""
output = StringIO() output = StringIO()
table([[1, 2, 3], [4, 5, 6]], 'ABC', style='round', width=5, out=output) table([[1, 2, 3], [4, 5, 6]], 'ABC', style='round', width=5, out=output)
assert output.getvalue() == '╭─────┬─────┬─────╮\n│ A │ B │ C │\n├─────┼─────┼─────┤\n│ 1│ 2│ 3│\n│ 4│ 5│ 6│\n╰─────┴─────┴─────╯\n' assert output.getvalue() == '╭─────┬─────┬─────╮\n│ A │ B │ C │\n├─────┼─────┼─────┤\n│ 1│ 2│ 3│\n│ 4│ 5│ 6│\n╰─────┴─────┴─────╯\n'
@@ -17,7 +26,7 @@ def test_table():
def test_frame(): def test_frame():
"""Tests the dataframe function"""
# mock of a pandas DataFrame # mock of a pandas DataFrame
class DataFrame: class DataFrame:
def __init__(self, data, headers): def __init__(self, data, headers):
@@ -35,7 +44,7 @@ def test_frame():
def test_banner(): def test_banner():
"""Tests the banner function"""
output = StringIO() output = StringIO()
banner('hello world', style='clean', width=11, out=output) banner('hello world', style='clean', width=11, out=output)
assert output.getvalue() == ' ─────────── \n hello world \n ─────────── \n' assert output.getvalue() == ' ─────────── \n hello world \n ─────────── \n'
@@ -46,7 +55,7 @@ def test_banner():
def test_hrule(): def test_hrule():
"""Tests the hrule function"""
output = hrule(1, width=11) output = hrule(1, width=11)
assert len(output) == 11 assert len(output) == 11
assert '───────────' assert '───────────'