Skip to main content

Colorful and Prefix-supported Print Toolkit

Project description

Pigmento: Colorize and Trace Printing

Due to the limitation of Github and Pypi Markdown, please visit Pigmento for a better reading experience.

Pigmento

Installation

pip install pigmento

Quick Start

from pigmento import pnt

class Test:
    @classmethod
    def class_method(cls):
        pnt('Hello World')

    def instance_method(self):
        pnt('Hello World')

    @staticmethod
    def static_method():
        pnt('Hello World')
        

def global_function():
    pnt('Hello World')

Test.class_method()
Test().instance_method()
Test.static_method()
global_function()
|Test| (class_method) Hello World
|Test| (instance_method) Hello World
|Test| (static_method) Hello World
(global_function) Hello World

Style Customization

from pigmento import pnt, Color

pnt.set_display_style(
    method_color=Color.RED,
    method_bracket=('<', '>'),
    class_color=Color.BLUE,
    class_bracket=('[', ']'),
)

Test.class_method()
Test().instance_method()
Test.static_method()
[Test] <class_method> Hello World
[Test] <instance_method> Hello World
[Test] <static_method> Hello World

Display Mode Customization

from pigmento import pnt

pnt.set_display_mode(
    display_method_name=False,
)

Test.class_method()
|Test| Hello World

Prefixes

Pigmento supports customized prefixes for each print. It is important to note that all prefixes are in first-in-first-print order.

from pigmento import pnt, Prefix, Color, Bracket

pnt.add_prefix(Prefix('DEBUG', bracket=Bracket.DEFAULT, color=Color.GREEN))

global_function()
[DEBUG] (global_function) Hello World

Dynamic Prefix

Texts inside prefix can be dynamically generated.

from pigmento import pnt, Prefix, Color, Bracket

class System:
    STATUS = 'TRAINING'
    
    @classmethod
    def get_status(cls):
        return cls.STATUS
    
    
pnt.add_prefix(Prefix(System.get_status, bracket=Bracket.DEFAULT, color=Color.GREEN))

global_function()
System.STATUS = 'TESTING'
global_function()
[TRAINING] (global_function) Hello World
[TESTING] (global_function) Hello World

Build-in Time Prefix

TimePrefix is a build-in prefix that can be used to display time.

import time
import pigmento
from pigmento import pnt

pigmento.add_time_prefix()

Test.class_method()
time.sleep(1)
Test.class_method()
[00:00:00] |Test| (class_method) Hello World
[00:00:01] |Test| (class_method) Hello World

Plugins

Pigmento supports plugins to extend its functionalities.

Build-in Logger

Everytime you print something, it will be logged to a file.

import pigmento
from pigmento import pnt

pigmento.add_log_plugin('log.txt')

global_function()
(global_function) Hello World

The log file will be created in the current working directory and the content will be removed the color codes.

cat log.txt
[00:00:00] (global_function) Hello World

Build-in Dynamic Color

DynamicColor will map caller class names to colors.

import pigmento
from pigmento import pnt


class A:
    @staticmethod
    def print():
        pnt(f'Hello from A')


class B:
    @staticmethod
    def print():
        pnt(f'Hello from B')


class D:
    @staticmethod
    def print():
        pnt(f'Hello from C')


A().print()
B().print()
D().print()

pigmento.add_dynamic_color_plugin()

A().print()
B().print()
D().print()
|A| (print) Hello from A
|B| (print) Hello from B
|D| (print) Hello from C
|A| (print) Hello from A
|B| (print) Hello from B
|D| (print) Hello from C

Plugin Customization

from pigmento import pnt, BasePlugin


class RenamePlugin(BasePlugin):
    def middleware_before_class_prefix(self, name, bracket, color):
        return name.lower(), bracket, color

    def middleware_before_method_prefix(self, name, bracket, color):
        return name.replace('_', '-'), bracket, color


pnt.add_plugin(RenamePlugin())

Test.class_method()
Test().instance_method()
Test.static_method()
|test| (class-method) Hello World
|test| (instance-method) Hello World
|test| (static-method) Hello World

Basic Printer Customization

import sys
import time

from pigmento import pnt


def flush_printer(prefix_s, prefix_s_with_color, text, **kwargs):
    sys.stdout.write(f'\r{prefix_s_with_color} {text}')
    sys.stdout.flush()


pnt.set_basic_printer(flush_printer)

def progress(total):
    for i in range(total):
        time.sleep(0.1)  # 模拟工作
        bar = f"Progress: |{'#' * (i + 1)}{'-' * (total - i - 1)}| {i + 1}/{total}"
        pnt(bar)

progress(30)
(progress) Progress: |#############-----------------| 13/30

Multiple Printers

Pigmento supports multiple printers.

from pigmento import Pigmento, Bracket, Color, Prefix

debug = Pigmento()
debug.add_prefix(Prefix('DEBUG', bracket=Bracket.DEFAULT, color=Color.GREEN))

info = Pigmento()
info.add_prefix(Prefix('INFO', bracket=Bracket.DEFAULT, color=Color.BLUE))

error = Pigmento()
error.add_prefix(Prefix('ERROR', bracket=Bracket.DEFAULT, color=Color.RED))


def divide(a, b):
    if not isinstance(a, int) or not isinstance(b, int):
        error('Inputs must be integers')
        return

    if b == 0:
        debug('Cannot divide by zero')
        return

    info(f'{a} / {b} = {a / b}')


divide(1, 2)
divide(1, 0)
divide('x', 'y')
[INFO] (divide) 1 / 2 = 0.5
[DEBUG] (divide) Cannot divide by zero
[ERROR] (divide) Inputs must be integers

License

MIT License

Author

Jyonn

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

pigmento-0.2.2.tar.gz (8.2 kB view details)

Uploaded Source

File details

Details for the file pigmento-0.2.2.tar.gz.

File metadata

  • Download URL: pigmento-0.2.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for pigmento-0.2.2.tar.gz
Algorithm Hash digest
SHA256 0f1f55ecdb93ada70b75a8987a5a085923cef10099342ff6792019d7eb530886
MD5 af30f81b931060cccefc52c8e2aeb00c
BLAKE2b-256 d88630e567753bee8a6994c515c5540168fd50de101cec6536a3581e9d07b88d

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