pycharmers.utils.print_utils module

pycharmers.utils.print_utils.format_spec_create(align='>', sign='', zero_padding=False, width=0, grouping_option='', fmt='')[source]

Create a function which returns a formatted text.

format_spec = [[fill]align][sign][#][0][width][grouping_option][.precision][type]

Parameters
  • width (int) –

  • align (str) – [[fill]align] One of ["<", ">", "=", "^"]

  • sign (str) – [sign] One of ["+", "-", " ", ""]

  • zero_padding (bool) – [0]

  • width – [width]

  • grouping_option (str) – [grouping_option] One of ["_", ",", ""]

  • fmt (str) – [.precision][type] One of ["b", "c", "d", "e", "E", "f", "F", "g", "G", "n", "o", "s", "x", "X", "%"]

Returns

lambda (<function __main__.<lambda>(fill)>)

Return type

format_spec

References

Examples

>>> from pycharmers.utils import format_spec_create
>>> format_spec = format_spec_create(width=10, align="^")
>>> format_spec("hoge")
'   hoge   '
>>> format_spec = format_spec_create(align="<", fmt=".1%")
>>> format_spec(1/3)
'33.3%'
>>> format_spec = format_spec_create(align=">", zero_padding=True, fmt="b")
>>> format_spec(20)
'10100'
pycharmers.utils.print_utils.align_text(string, align='left', width=0)[source]

Align text

Parameters
  • string (str) – Strig.

  • align (str) – How to align the string.

  • width (int) – Width.

Returns

Aligned text.

Return type

string (str)

Examples

>>> from pycharmers.utils import align_text, toBLUE
>>> print(align_text("Hello world!", align=">", width=15))
   Hello world!
>>> print(align_text(toBLUE("Hello world!"), align=">", width=15))
   Hello world!
pycharmers.utils.print_utils.print_func_create(align='>', sign='', zero_padding=False, width=0, grouping_option='', fmt='', color='', is_bg=False, left_side_bar='', right_side_bar='', left_margin=0, right_margin=0, end='\n')[source]

Create a function which prints a formatted text. Please see also the function format_spec_create.

Parameters
  • color (str) – color.

  • is_bg (bool) – Whether to add color to the background or not.

  • left (right)_margin (int) – Characters to output to the Left/Right side.

  • left – Left/Right side margin

  • end (str) – string appended after the last value, default a newline.

Returns

Function that can be used like print

Return type

print_func

Examples

>>> from pycharmers.utils import print_func_create
>>> print_func = print_func_create(width=8, align="^", left_side_bar="[", right_side_bar="]")
>>> print_func("hoge")
[  hoge  ]
>>> print_func = print_func_create(align="<", left_side_bar="$ ")
>>> print_func("git clone https://github.com/iwasakishuto/Python-utils.git")
$ git clone https://github.com/iwasakishuto/Python-utils.git
>>> print_func("cd Python-utils")
$ cd Python-utils
>>> print_func("sudo python setup.py install")
$ sudo python setup.py install
class pycharmers.utils.print_utils.Table(tablefmt='rst', enable_colspan=True, mincolwidth=3)[source]

Bases: object

Create a beautiful table and show.

Parameters
  • tablefmt (str) – The format of tabel.

  • enable_colspan (bool) – Whether to enable colspan or not.

  • mincolwidth (int) – The minimum width of each column.

Properties:

ncols(int) : the number of columns.

set_cols()[source]

Set values to a table.

show()[source]

Show a table.

Examples

>>> from pycharmers.utils import Table, toBLUE
>>> table = Table(enable_colspan=True)
>>> table.set_cols([1,2,""], colname="id")
>>> table.set_cols([toBLUE("abc"), "", "de"], color="GREEN")
>>> table.show()
+----+-------+
| id | col.2 |
+====+=======+
|  1 |   abc |
+----+       +
|  2 |       |
+    +-------+
|    |    de |
+----+-------+
SUPPORTED_FORMATS = ['github', 'rst']
property ncols
show(head=None, table_width=None, tablefmt=None)[source]

Show a table

Parameters
  • head (str) – Show the first head rows for the table.

  • table_width (int) – The table width.

show_github(head=None, table_width=None)[source]

Show a table with github format.

Parameters
  • head (str) – Show the first head rows for the table.

  • table_width (int) – The table width.

Examples

>>> from pycharmers.utils import Table
>>> table = Table()
>>> table.set_cols(values=range(3), colname="Number")
>>> table.set_cols(values=["iwa", "saki", "shuto"], colname="Name")
>>> table.show_github()
| Number | Name  |
|:------:|:-----:|
|      0 |   iwa |
|      1 |  saki |
|      2 | shuto |
show_rst(head=None, table_width=None)[source]

Show a table with rst format.

Parameters
  • head (str) – Show the first head rows for the table.

  • table_width (int) – The table width.

Examples

>>> from pycharmers.utils import Table
>>> table = Table()
>>> table.set_cols(values=range(3), colname="Number")
>>> table.set_cols(values=["iwa", "saki", "shuto"], colname="Name")
>>> table.show_rst()
+--------+-------+
| Number | Name  |
+========+=======+
|      0 |   iwa |
+--------+-------+
|      1 |  saki |
+--------+-------+
|      2 | shuto |
+--------+-------+
set_cols(values, colname=None, width=0, align='>', sign='', zero_padding=False, grouping_option='', fmt='', color='', left_margin=1, right_margin=1)[source]

Set values to a table.

Parameters
  • values (array) – The array-like data.

  • colname (str) – The colname for values.

  • **kwargs – See also print_func_create

pycharmers.utils.print_utils.tabulate(tabular_data=[[]], headers=[], tablefmt='rst', aligns='left')[source]

Format a fixed width table for pretty printing.

Parameters
  • tabular_data (list) – tbody contents. Must be a dual list.

  • headers (list) – thead contents.

  • tablefmt (str) – Table format for Table

  • aligns (list) – How to align values in each col.

Examples

>>> from pycharmers.utils import tabulate
>>> tabulate([[i*j for i in range(1,4)] for j in range(1,4)])
+-------+-------+-------+
| col.1 | col.2 | col.3 |
+=======+=======+=======+
|     1 |     2 |     3 |
+-------+-------+-------+
|     2 |     4 |     6 |
+-------+-------+-------+
|     3 |     6 |     9 |
+-------+-------+-------+
pycharmers.utils.print_utils.print_dict_tree(dictionary, indent=4, rank=0, marks=['-', '*', '#'])[source]

Print Dictionary as a Tree.

Parameters
  • dictionary (dict) – An input dictionary.

  • indent (int) – Indent.

  • rank (int) – A current rank.

  • marks (list) – List mark types.

Examples

>>> from pycharmers.utils import print_dict_tree
>>> print_dict_tree({"a": 0, "b": 1})
- a: 0
- b: 1
>>> print_dict_tree({"a": 0, "b": {"b1": 1, "b2": 2}})
- a: 0
- b:
  * b1: 1
  * b2: 2
>>> print_dict_tree({"a": 0, "b": {"b1": 1, "b2": {"b21": 0, "b22": 1}}, "c": 3})
- a: 0
- b:
  * b1: 1
  * b2:
    # b21: 0
    # b22: 1
- c: 3
pycharmers.utils.print_utils.pretty_3quote(*value, indent=0)[source]

pretty 3 quote string.

Parameters

indent (int) – If indent is a non-negative integer, then multiple lines will be pretty-printed with that indent level.

Examples

>>> from pycharmers.utils import pretty_3quote
>>> print(*pretty_3quote("""
...     When I was 17, I read a quote that went something like:
...     “If you live each day as if it was your last, someday you’ll most certainly be right.”
...     It made an impression on me, and since then, for the past 33 years,
>>> """))
When I was 17, I read a quote that went something like:
“If you live each day as if it was your last, someday you’ll most certainly be right.”
It made an impression on me, and since then, for the past 33 years,
pycharmers.utils.print_utils.strip_invisible(s)[source]

Remove invisible ANSI color codes.

Parameters

s (str) – String.

Returns

String with invisible code removed.

Return type

s (str)

Examples

>>> from pycharmers.utils import strip_invisible, toBLUE
>>> strip_invisible("hello")
'hello'
>>> strip_invisible(toBLUE("hello"))
'hello'
>>> strip_invisible("hello")
'hello'
pycharmers.utils.print_utils.visible_width(s)[source]

Visible width of a printed string. ANSI color codes are removed.

Parameters

s (str) – String.

Returns

Visible width

Return type

width (int)

Examples

>>> from pycharmers.utils import visible_width, toBLUE
>>> visible_width(toBLUE("hello"))
5
>>> visible_width("こんにちは")
10
>>> visible_width("hello 世界。")
12
pycharmers.utils.print_utils.str2pyexample(string)[source]

Create a python example code.

Parameters

string (str) – A string of Python Example Code.

Examples

>>> from pycharmers.utils import str2pyexample
>>> WINDOW_NAME = "string2python"
>>> str2pyexample("""
... import cv2
... import numpy as np
... frame = np.zeros(shape=(50, 100, 3), dtype=np.uint8)
... while (True):
...     cv2.imshow(WINDOW_NAME, frame)
...     if cv2.waitKey(0) == 27: break
... cv2.destroyAllWindows()
... """)
>>> import cv2
>>> import numpy as np
>>> frame = np.zeros(shape=(50, 100, 3), dtype=np.uint8)
>>> while (True):
...     cv2.imshow(WINDOW_NAME, frame)
...     if cv2.waitKey(0) == 27: break
>>> cv2.destroyAllWindows()