diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-03-14 20:56:04 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-03-14 20:56:04 -0400 |
commit | 5c73e15508e54c233b9fc77daf0d00156b195c3f (patch) | |
tree | a0710088a8849afbf8e422f3dcee2f70e29d1fb6 | |
parent | 6a52f64d52d35a83640e7e319a27f1ce8edd0ea2 (diff) | |
download | cmd2-git-5c73e15508e54c233b9fc77daf0d00156b195c3f.tar.gz |
Added table_display.py
Added example showing how to display tabular data in a cmd2 application.
Also:
- Updated CHANGELOG with data on all PRs since last release
- Bumped version to 0.8.2 in preparation for next release
-rw-r--r-- | CHANGELOG.md | 11 | ||||
-rwxr-xr-x | cmd2.py | 2 | ||||
-rw-r--r-- | docs/conf.py | 2 | ||||
-rwxr-xr-x | examples/table_display.py | 64 | ||||
-rwxr-xr-x | setup.py | 2 | ||||
-rw-r--r-- | tests/test_cmd2.py | 2 |
6 files changed, 79 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e3dd5a53..d82d191d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +## 0.8.2 (March TBD, 2018) + +* Bug Fixes + * Fixed a bug in tab-completion of command names within sub-menus + * Fixed a bug when using persistent readline history in Python 2.7 + * Fixed a bug where the ``AddSubmenu`` decorator didn't work with a default value for ``shared_attributes`` +* Enhancements + * Added [quit_on_sigint](http://cmd2.readthedocs.io/en/latest/settingchanges.html#quit-on-sigint) attribute to enable canceling current line instead of quitting when Ctrl+C is typed + * Added possibility of having readline history preservation in a SubMenu + * Added [table_display.py](https://github.com/python-cmd2/cmd2/blob/master/examples/table_display.py) example to demonstrate how to display tabular data + ## 0.8.1 (March 9, 2018) * Bug Fixes @@ -113,7 +113,7 @@ if six.PY2 and sys.platform.startswith('lin'): except ImportError: pass -__version__ = '0.8.1' +__version__ = '0.8.2' # Pyparsing enablePackrat() can greatly speed up parsing, but problems have been seen in Python 3 in the past pyparsing.ParserElement.enablePackrat() diff --git a/docs/conf.py b/docs/conf.py index 09d68b9c..da1fc965 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -62,7 +62,7 @@ author = 'Catherine Devlin and Todd Leonhardt' # The short X.Y version. version = '0.8' # The full version, including alpha/beta/rc tags. -release = '0.8.1' +release = '0.8.2' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/examples/table_display.py b/examples/table_display.py new file mode 100755 index 00000000..68b73d0f --- /dev/null +++ b/examples/table_display.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# coding=utf-8 +"""A simple example demonstrating the following: + 1) How to display tabular data within a cmd2 application + 2) How to display output using a pager + +NOTE: IF the table does not entirely fit within the screen of your terminal, then it will be displayed using a pager. +You can use the arrow keys (left, right, up, and down) to scroll around the table as well as the PageUp/PageDown keys. +You can quit out of the pager by typing "q". You can also search for text within the pager using "/". + +WARNING: This example requires the tabulate module. +""" +import functools + +import cmd2 +import tabulate + +# Format to use with tabulate module when displaying tables +TABLE_FORMAT = 'grid' + +# Create a function to format a fixed-width table for pretty-printing using the desired table format +table = functools.partial(tabulate.tabulate, tablefmt=TABLE_FORMAT) + +# Population data from Wikipedia: https://en.wikipedia.org/wiki/List_of_cities_proper_by_population +EXAMPLE_DATA = [['Shanghai', 'Shanghai', 'China', 'Asia', 24183300, 6340.5, 3814], + ['Beijing', 'Hebei', 'China', 'Asia', 20794000, 1749.57, 11885], + ['Karachi', 'Sindh', 'Pakistan', 'Asia', 14910352, 615.58, 224221], + ['Shenzen', 'Guangdong', 'China', 'Asia', 13723000, 1493.32, 9190], + ['Guangzho', 'Guangdong', 'China', 'Asia', 13081000, 1347.81, 9705], + ['Mumbai', ' Maharashtra', 'India', 'Asia', 12442373, 465.78, 27223], + ['Istanbul', 'Istanbul', 'Turkey', 'Eurasia', 12661000, 620.29, 20411], + ] +EXAMPLE_HEADERS = ['City', 'Province', 'Country', 'Continent', 'Population', 'Area (km^2)', 'Pop. Density (/km^2)'] + + +class TableDisplay(cmd2.Cmd): + """Example cmd2 application showing how you can display tabular data.""" + + def __init__(self): + cmd2.Cmd.__init__(self) + + def ptable(self, tabular_data, headers=()): + """Format tabular data for pretty-printing as a fixed-width table and then display it using a pager. + + :param tabular_data: required argument - can be a list-of-lists (or another iterable of iterables), a list of + named tuples, a dictionary of iterables, an iterable of dictionaries, a two-dimensional + NumPy array, NumPy record array, or a Pandas dataframe. + :param headers: (optional) - to print nice column headers, supply this argument: + - headers can be an explicit list of column headers + - if `headers="firstrow"`, then the first row of data is used + - if `headers="keys"`, then dictionary keys or column indices are used + - Otherwise, a headerless table is produced + """ + formatted_table = table(tabular_data, headers=headers) + self.ppaged(formatted_table) + + def do_table(self, _): + """Display data on the Earth's most populated cities in a table.""" + self.ptable(tabular_data=EXAMPLE_DATA, headers=EXAMPLE_HEADERS) + + +if __name__ == '__main__': + app = TableDisplay() + app.cmdloop() @@ -6,7 +6,7 @@ Setuptools setup file, used to install or test 'cmd2' import sys from setuptools import setup -VERSION = '0.8.1' +VERSION = '0.8.2' DESCRIPTION = "cmd2 - a tool for building interactive command line applications in Python" LONG_DESCRIPTION = """cmd2 is a tool for building interactive command line applications in Python. Its goal is to make it quick and easy for developers to build feature-rich and user-friendly interactive command line applications. It diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index e22212d4..fd37e25e 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -25,7 +25,7 @@ from conftest import run_cmd, normalize, BASE_HELP, HELP_HISTORY, SHORTCUTS_TXT, def test_ver(): - assert cmd2.__version__ == '0.8.1' + assert cmd2.__version__ == '0.8.2' def test_empty_statement(base_app): |