Skip to main content

Handful of utilities to do printing tricks to the terminal.

Project description

printbuddies

A few utilities to do terminal printing tricks.
Install with:

pip install printbuddies

Contains two classes and three functions: ProgBar, Spinner, print_in_place, ticker, and clear.

ProgBar

ProgBar is a self-incrementing, dynamically sized progress bar.
The progress counter and completion values can be manually overriden if desired.
The width of the progress bar is set according to a ratio of the terminal width so it will be resized automatically if the terminal width is changed.

from printbuddies import ProgBar
total = 100
bar = ProgBar(total=total)
for _ in range(total):
    bar.display()
bar.reset()
my_list = [bar.display(return_object=i) for i in range(total)]

The display function has a 'return_object' parameter, allowing ProgBar to be used in comprehensions.

bar = ProgBar(10)
def square(x: int | float)->int|float:
    return x * x
myList = [bar.display(return_object=square(i)) for i in range(10)]
{progress bar gets displayed}
print(myList)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

ProgBar also supports being used with a context manager.

PoolBar

A convenience class to integrate concurrent.futures.ThreadPoolExecutor and concurrent.futures.ProcessPoolExecutor with ProgBar.
Constructor takes the pool executor type, a list of functions to execute, and an optional list of args for those functions.
The execute() method returns a list of whatever those functions return.
execute() can also take any optional ProgBar constructor arguments.

def my_func(page: int)->str:
    return requests.get(f"https://somesite.com/pages/{page}").text
pool = PoolBar("thread", [my_func for _ in range(10)], [(i,) for i in range(10)])
pages = pool.execute(width_ratio=0.75)

Spinner

This class will print the next character from a sequence every time it's display method is called, clearing whatever is currently on the line.
The characters will be cycled through indefinitely.

from printbuddies import Spinner
spinner = Spinner()
for _ in range(10):
    spinner.display()

The default character sequence can be overridden:

spinner = Spinner(sequence=["~_~_~_~_~_~_", "_~_~_~_~_~_~"])
for _ in range(10):
    spinner.display()

When used with a context manager, the last character printed will be cleared from the terminal upon exiting.

print_in_place

'print_in_place' erases the current line in the terminal and then writes the value of the 'string' param to the terminal.

from printbuddies import print_in_place
import time
#This will print numbers 0-99 to the terminal with each digit overwriting the last.
for i in range(100):
    print_in_place(i)
    time.sleep(0.1)

ticker

'ticker' prints a list of strings to the terminal with empty lines above and below such that previous text in the terminal is no longer visible.
Visually, It functions as a multi-line version of print_in_place.

from printbuddies import ticker
import time
#This will produce visually the same output as the above example
for i in range(100):
    ticker([i])
    time.sleep(0.1)

clear

A call to printbuddies.clear() simply clears the current line from the terminal.

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

printbuddies-1.4.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

printbuddies-1.4.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file printbuddies-1.4.0.tar.gz.

File metadata

  • Download URL: printbuddies-1.4.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for printbuddies-1.4.0.tar.gz
Algorithm Hash digest
SHA256 2cc65b3dbd5e54967997547da0ad6c5d0c65c57c15b13877f582c34cb8168c10
MD5 e2e5033e29e8c2271c626d17d7dd19a0
BLAKE2b-256 0da0e92cdd34c5a1c95d015b3e446831de6fff67599952b04da158dc0a3d32bf

See more details on using hashes here.

Provenance

File details

Details for the file printbuddies-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for printbuddies-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c8f73b62e6a14830ee43f76abe56c2028c029ef33a48ffa7dd5fe817e315acf1
MD5 5a25fc83070e2dce4e475455c30107de
BLAKE2b-256 3eaf340b3be51a0e91b6fa7f6785bd4cbd031ffc88c4b76706a4856880800e61

See more details on using hashes here.

Provenance

Supported by

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