Giter Site home page Giter Site logo

reprint's Introduction

reprint reprint

reprint is a Python 2/3 module for binding variables and refreshing multi-line output in terminal.

The solution for calculating Unicode char width is from urwid

中文版README

Features

  • Python 2/3 support
  • Simple variable bindings and automatic command line refresh upon variable change
  • Simultaneous multi-line refresh with each line binded to different variables
  • Robust thread safety with threading.Lock

Setup

pip install reprint

DEMO

Demo_gif

Guidance

  1. Import the output object
from reprint import output
  1. Use with block to control the initialization, output object contains the following parameters:

    • output_type: "list" or "dict" (default: "list"), indicating the list mode or the dict mode.
    • initial_len: int (default: 1), only works in the list mode, indicating the initial length of the list. It's for modifying the content by index without initialization.
    • interval: int (default: 0), the minimal refresh interval (in millisecond). The refresh function call will be ignored unless at least this amount of time has passed since last refresh.
    with output(output_type="list", initial_len=1, interval=0) as output_list:
  2. Changing the variables in output_list will trigger the refresh of the command line output.

Note

  • In the with block, any print/logging/Exception commands that print texts on terminal would ruin the format of the reprint output. If you need to append some content to the end of the output, use append function in the output object (works both in the list or the dict mode).

  • Don't assign a new list or dict to the output object. If you want to change the whole list or dict, use change function in the output object (works both in the list or the dict mode).

  • Old messages will not be fully wiped out if the height of the output is larger than the height of the terminal window. So you should control the length of your output.

    • Or you may use the force_single_line mode to force the output to stay in single line.
    with output(output_type="list", initial_len=1, interval=0, force_single_line=True) as output_list:
  • The initialization of threading should be in the with block if you use reprint in threading.

  • When used in non-terminal environment, reprint will use the built-in print function.

  • Does not work in the IDLE terminal, and any other environment that doesn't provide terminal_size.

FAQ

Q: I want to use customize 'sort function' for dict mode to sort output lines, what should I do?

A: You can use that parameter name sort_key when initializing the output object, such as:

with output(output_type='dict', sort_key=lambda x:x[1]) as output_lines:

then reprint will use that function as the key parameter in the sorted function

elif isinstance(content, dict):
    for k, v in sorted(content.items(), key=sort_key):
        print("{}: {}".format(k, v))

Q: How to disable all warning?

A: You can use that parameter name no_warning when initializing the output object, such as:

with output(no_warning=True) as output_list:

reprint's People

Contributors

yinzo avatar zeruniverse avatar

Watchers

James Cloos avatar leavesosoon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.