A context manager to facilitate printing messages to the same line
Project description
single-line
A context manager to facilitate printing messages to the same line.
Installation
pip install single-line
Usage
Using the SingleLine
context manager all calls to its print
method will print the message to the same line. A common use case will be to use it in conjuction with a for loop as follows.
from time import sleep
from faker import Faker
from single_line import SingleLine
with SingleLine() as line:
for _ in range(25):
line.print(Faker().sentence())
sleep(.15)
Setting message_when_done
parameter will print a prescribed message when the context is done. The print
method also supports printing colored messages via the colorama module, just pass the method an optional color
parameter consiting of a dictionary describing the fore
, back
and style
you wish to the message to be printed with.
from time import sleep
from faker import Faker
from colorama import Fore
from single_line import SingleLine
with SingleLine(message_when_done='done') as line:
for _ in range(25):
line.print(Faker().sentence(), color={'fore': Fore.YELLOW})
sleep(.15)
By default messages will be printed out to sys.stdout
but you can print to any object with a write(string) method. This example also shows the extent of using colors when printing messages.
import sys
import random
from time import sleep
from faker import Faker
from single_line import SingleLine
from colorama import Fore, Back, Style
def get_random_fore():
return random.choice([Fore.BLACK, Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE])
def get_random_back():
return random.choice([Back.BLACK, Back.RED, Back.GREEN, Back.YELLOW, Back.BLUE, Back.MAGENTA, Back.CYAN, Back.WHITE])
def get_random_style():
return random.choice([Style.NORMAL, Style.DIM, Style.BRIGHT])
with SingleLine(stream=sys.stderr) as line:
for _ in range(25):
line.print(Faker().sentence(), color={'fore': get_random_fore(), 'back': get_random_back(), 'style': get_random_style()})
sleep(.15)
You can also use the SingleLine
context manager to display messages when executing asyncio methods.
import asyncio
import random
from faker import Faker
from single_line import SingleLine
async def do_some_work(worker, fake, line):
for index in range(random.randint(10, 35)):
await asyncio.sleep(random.choice([.5, .1, .25]))
line.print(f'worker{worker} {fake.sentence()}')
async def run(line):
await asyncio.gather(*(do_some_work(worker, Faker(), line) for worker in range(5)))
with SingleLine(message_when_done='done with asyncio') as line:
asyncio.run(run(line))
Development
Clone the repository and ensure the latest version of Docker is installed on your development server.
Build the Docker image:
docker image build \
-t single-line:latest .
Run the Docker container:
docker container run \
--rm \
-it \
-v $PWD:/code \
single-line:latest \
bash
Execute the build:
pyb -X
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for single_line-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea16730aaebed0d207a3f63e9a853ede1021b961517cc9c23fd6b82c46705bbc |
|
MD5 | 90be9d2881a389fe1597b138d806652a |
|
BLAKE2b-256 | 3b819361f86b60017c4f40f73f46d9fa46b2591da924cb8a1b31d83e24be0b36 |