reprint
reprint is a module for Python 2/3 to binding variables and refresh multi line output in terminal.
The solution for calculating Unicode char width is from urwid
Feature
- Support Python 2/3
- variables binding, automatically refresh command line output when variables changed
- multi line contents flush-able, every line of output comes from different variable in output object, changes on variable will refresh output
- thread safe, using threading.Lock to do that
Setup
pip install reprint
DEMO
Guidance
-
Import the
output
objectfrom reprint import output
-
Use
with
block to control the initalization ,output
object contain these following parameters:output_type
:"list"
or"dict"
, indicating list mode and dict mode, default as"list"
initial_len
:int
, only work in list mode, indicating the initial length of the list, for do some modification on the content without initialization, default as1
interval
:int
, the interval of refresh,only greater than this interval will trigger the refresh function, default as0
with output(output_type="list", initial_len=1, interval=0) as output_list:
-
Change the variables in
output_list
will trigger the refresh of the command line output
Note
-
Within
with
block, anyprint
/logging
/Exception
that do output on terminal would cause the format of reprint output abnormal. If you need to append some content to the end of output, useappend
function ofoutput
instance (works both in list or dict mode) -
Don't assign a new
list
ordict
tooutput
instance. If you want to entirely change the list or dict, usechange
function ofoutput
instance (works both in list or dict mode) -
If the lines of output exceed the height of terminal windows, that will cause the former output remained and keep adding new lines to the terminal. So maybe you should control the length of your
output
instance.- or you can use
force_single_line
mode, to force the output stay in single line
with output(output_type="list", initial_len=1, interval=0, force_single_line=True) as output_list:
- or you can use
-
The initialization of threading should be within the
with
block if you use reprint in threading -
When using non-terminal output, reprint will use normal build-in
print
function.