Skip to main content

A context manager to facilitate printing messages to the same line

Project description

single-line

build coverage complexity vulnerabilities python

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)

example1

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)

example2

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)

example3

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))

example4

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

single_line-1.0.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

single_line-1.0.0-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

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