Skip to main content

A context manager to facilitate printing messages to the same line

Project description

single-line

build coverage complexity vulnerabilities PyPI version 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 write method will print the message to the same line. A common use case is to use it in conjuction with a for loop.

from time import sleep
from faker import Faker
from single_line import SingleLine

with SingleLine() as line:
    for _ in range(25):
        line.write(Faker().sentence())
        sleep(.15)

example1

Setting the exit_message parameter will print the designated message when the context exits. The write method also supports colored messages via the colorama module (so long as the stream is interactive); pass an optional color parameter with a dictionary containing the fore, back and style values.

from time import sleep
from faker import Faker
from colorama import Fore
from single_line import SingleLine

with SingleLine(exit_message='done') as line:
    for _ in range(25):
        line.write(Faker().sentence(), color={'fore': Fore.YELLOW})
        sleep(.15)

example2

By default messages will be printed out to the sys.stdout stream but you can designate sys.stderr by setting the stream parameter. Note if stream is not connected to an interactive terminal device 'SingleLine` will simply print the message, color and cursor directives will be ignored. 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.write(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.write(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(exit_message='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.1.0.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

single_line-1.1.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