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

Uploaded Source

Built Distribution

single_line-1.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file single_line-1.1.0.tar.gz.

File metadata

  • Download URL: single_line-1.1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for single_line-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ecadb9cf5f5c7f6eb38af102923e4d293b1601dfe107c3fc4195ffa886ac395f
MD5 139b90c9633ff0fe5293ce0fba85642e
BLAKE2b-256 e40dca7df92f99caa7386121620990ff02ff1a832169632744fb947824f42051

See more details on using hashes here.

File details

Details for the file single_line-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: single_line-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for single_line-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0817d33125450b63f3a311aa9eaa5a2f6450b61208f492242bf2fd2c3dfce296
MD5 60b8788d1a26e1495317eff4653f5ca0
BLAKE2b-256 cc4f98c672d53f5fdc49728e30405d9085c240522efa15896c00d9e50a6cfd89

See more details on using hashes here.

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