Skip to main content

The printio library in Python.

Project description

printio is a MIT licensed pretty printing library implemented in Python.

Format a value(s) for printing or display using Python’s built-in String Format library.

Most of the string formatting options from the Format Specification Mini-Language are available. See the formatting options here:

http://docs.python.org/release/3.1.2/library/string.html#format-specification-mini-language

Features

  • Align values to the left, right, or center.

  • Pad and fill values.

  • Sign numerical values.

  • Convert integers to float.

  • Specify precision for floating values - values are rounded up.

  • Convert numbers to percentage formats. Ex. 0.10 ~ 10.0000%

  • Add column headers or utilize default headers for your values.

  • Column widths are automatically sized based on maximum width of the values.

  • Choose which columns to format in your values.

  • Ability to print to ‘text’ similar to how MySQL displays output to the console.

Overview

The major functions of printio:

  • PrettyValue():

    Formats a single value to a string.

  • PrettyValues():
    Formats a list of lists or dicts.
    • format: will return a list of strings including the header.

    • text: will return a string similar to MySQL’s console display format.

License

Made available under the MIT License.

Usage

First, some housekeeping items…

Import the library

>>> from printio import PrettyValue
>>> from printio import PrettyValues

Create a list of values you wish to format.

>>> lol = []
>>> lol.append([0, 'yhoo', 23.45])
>>> lol.append([1, 'goog', 200.4565])
>>> lol.append([2, 't', 1.00])

Let’s also create a list of dictionaries to format as well.

>>> keys = ['bar', 'symbol', 'close']
>>> lod = [dict(zip(keys, x)) for x in lol]

Now, let’s get down to business…

Format a string with a width of 10, center-aligned, and filled with ‘-‘.

>>> value = 'yhoo'
>>> pv = PrettyValue('^10', fill='-')
>>> pv.format(value)
---yhoo---

Format a float with a decimal precision of 1.

>>> value = 23.45599
>>> pv = PrettyValue('.1f')
>>> pv.format(value)
'23.5'

Format a float into a percentage.

>>> value = 0.025
>>> pv = PrettyValue('.2%')
>>> pv.format(value)
'2.50%'

Format a list.

>>> pv = PrettyValues()
>>> for row in pv.format([lol[0]]): print row
['0', '1   ', '2    ']
['0', 'yhoo', '23.45']

Format a dict.

>>> pv = PrettyValues()
>>> pv.newcol('bar')
>>> pv.newcol('symbol')
>>> pv.newcol('close')
>>> for row in pv.format([lod[0]]): print row
['bar', 'symbol', 'close']
['0  ', 'yhoo  ', '23.45']

Format a list of lists.

>>> pv = PrettyValues()
>>> for row in pv.format(lol): print row
['0', '1   ', '2       ']
['0', 'yhoo', '23.45   ']
['1', 'goog', '200.4565']
['2', 't   ', '1.0     ']

Same as above but this time in text.

>>> pv = PrettyValues()
>>> results = pv.text(lol)
>>> print results
+---+------+----------+
| 0 | 1    | 2        |
+---+------+----------+
| 0 | yhoo | 23.45    |
| 1 | goog | 200.4565 |
| 2 | t    | 1.0      |
+---+------+----------+

Want to add better column names?

>>> pv.newcol(0, cname='Bar')
>>> pv.newcol(1, cname='Symbol')
>>> pv.newcol(2, cname='Close')
>>> print pv.text(lol)
+-----+--------+----------+
| Bar | Symbol | Close    |
+-----+--------+----------+
| 0   | yhoo   | 23.45    |
| 1   | goog   | 200.4565 |
| 2   | t      | 1.0      |
+-----+--------+----------+

Print only the Close column, always show sign, and format with decimal precision of 2.

>>> pv = PrettyValues()
>>> pv.newcol(2, '+.2f', cname='Close')
>>> print pv.text(lol)
+---------+
| Close   |
+---------+
| + 23.45 |
| +200.46 |
| +  1.00 |
+---------+

Print list of dictionaries with the numerical settings for the bar & close.

>>> pv = PrettyValues()
>>> pv.newcol('bar', 'i')
>>> pv.newcol('symbol')
>>> pv.newcol('close', '.2f')
>>> print pv.text(lod)
+-----+--------+--------+
| bar | symbol | close  |
+-----+--------+--------+
|   0 | yhoo   |  23.45 |
|   1 | goog   | 200.46 |
|   2 | t      |   1.00 |
+-----+--------+--------+

Roadmap

  • Add option to display title in addition to column headings.

  • Add autonum column ability.

  • Add tb_html to format to a html table.

  • Add pre_html to format <pre> html </pre>.

For additional information, please email:

mike@taylortree.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

printio-0.0.2.zip (16.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

printio-0.0.2.win32.exe (212.3 kB view details)

Uploaded Source

printio-0.0.2-py2.6.egg (22.8 kB view details)

Uploaded Egg

File details

Details for the file printio-0.0.2.zip.

File metadata

  • Download URL: printio-0.0.2.zip
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for printio-0.0.2.zip
Algorithm Hash digest
SHA256 ae246ea624b6fe6c4d5264ac967edfeca556bbb2c10321b670380d3cb1b425ac
MD5 52545a04a25f2ad824bf57165855a449
BLAKE2b-256 0013ecd786a57df2e5fa0344b94fbe5c5f2cacbc7389e451b6f5c633de6cca41

See more details on using hashes here.

File details

Details for the file printio-0.0.2.win32.exe.

File metadata

  • Download URL: printio-0.0.2.win32.exe
  • Upload date:
  • Size: 212.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for printio-0.0.2.win32.exe
Algorithm Hash digest
SHA256 c4e777576e47422f7c2fb77a55fcc51e86c3f91bb1493cc32ff9cbd7bfe10a87
MD5 fb656a38eb960f9e03f289e7ca8988bf
BLAKE2b-256 8ccbc497fe88e172b1eb8da011b93011840fcc122c5b62f0a2bc6576ae6b8124

See more details on using hashes here.

File details

Details for the file printio-0.0.2-py2.6.egg.

File metadata

  • Download URL: printio-0.0.2-py2.6.egg
  • Upload date:
  • Size: 22.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for printio-0.0.2-py2.6.egg
Algorithm Hash digest
SHA256 aabc61e3e96f13492f6a08b717fd4a2e1894ef22e2befd31da2c338fa44d5ab9
MD5 7359ff1cefc750e6cb9a7cec3ec0b45d
BLAKE2b-256 319c77cd5a426c1311ed54c867004aa612144f43ae246ea3d519a1064eb0c24a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page