bronx.fancies.display

Various tools designed for interactive scripts.

Functions

bronx.fancies.display.dict_as_str(adict, prefix=0, indent=3)[source]

Nicely aligned string representation of a dictionary.

Parameters:
  • adict (dict) – the dictionary to format

  • prefix (int) – number of leading spaces

  • indent (int) – number of additionnal spaces when recurring

Return str:

the formatted string representation

>>> d = dict(a=1, abracadabra=42, more=dict(b=2, babylou=69))
>>> print(dict_as_str(d, prefix=4, indent=2), end='')
    a          : 1
    abracadabra: 42
    more
      b      : 2
      babylou: 69
bronx.fancies.display.join_list_in_proper_english(a_list, l_fmt='{!s}')[source]

Join a list using commas + a final ‘and’ word if needed.

Parameters:
  • a_list – any iterable object to be concatenated

  • l_fmt (str) – The Python’s format applied to each a_list item

Example:

Ask something to someone…

>>> print(join_list_in_proper_english(['a', 'b', 'c']))
a, b and c
>>> print(join_list_in_proper_english('abc'))  # Any iterable can be used
a, b and c
>>> print(join_list_in_proper_english(['a', 'b']))
a and b
>>> print(join_list_in_proper_english(['a', ]))
a
>>> print(join_list_in_proper_english([]))
bronx.fancies.display.print_tablelike(fmt, *args, **kwargs)[source]

Left align all strings in order to have a well aligned output.

Parameters:
  • fmt (str) – The format string used for each of the output lines

  • output_callback (str) – The function to call for each output line (default: print)

  • preserve_last (str) – If True and if the last column is of string type, leave it untouched (i.e. unaligned) (default: True)

Simple examples:

>>> print_tablelike('{:s} = {:d}', ['a', 'bcde', 'f'], [1, 2, 3])
a    = 1
bcde = 2
f    = 3

If one wants to write somewhere else:

>>> outlist = list()
>>> print_tablelike('{:s} = {:d}', ['a', 'bcde', 'f'], [1, 2, 3], output_callback=outlist.append)
>>> for l in outlist:
...     print(l)
a    = 1
bcde = 2
f    = 3

By default, if the last column consists of string, it is un-aligned:

>>> print_tablelike('{:s} = << {:s} >>', ['a', 'bcde', 'f'], ['a', 'bcde', 'f'])
a    = << a >>
bcde = << bcde >>
f    = << f >>

If one wants to align it:

>>> print_tablelike('{:s} = << {:s} >>', ['a', 'bcde', 'f'], ['a', 'bcde', 'f'], preserve_last=False)
a    = << a    >>
bcde = << bcde >>
f    = << f    >>
bronx.fancies.display.printstatus(step, end, refresh_freq=1)[source]

Print percentage of the loop it is in.

Parameters:
  • step – the current loop step

  • end – the final loop step

  • refresh_freq – the frequency in % at which reprinting status.

bronx.fancies.display.query_yes_no_quit(question, default='yes')[source]

Ask a yes/no/quit question via input() and return their answer.

Parameters:
  • question (str) – String that is presented to the user.

  • default (str) – The presumed answer if the user just hits <Enter>. It must be “yes” (the default), “no”, “quit” or None (meaning an answer is required).

Returns:

‘yes’, ‘no’ or ‘quit’ depending on the user’s answer.

Return type:

A unicode string.

Example:

Ask something to someone…

>>> answer = query_yes_no_quit('Do you want to continue ?', default='yes') 
Do you want to continue ? [Y/n/q] Y
>>> answer 
u'yes'

from: http://code.activestate.com/recipes/577097/