Pigmento: Colorize and Trace Printing
Due to the limitation of Github and Pypi Markdown, please visit Pigmento for a better reading experience.
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
Metadata
Release files for pigmento 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pigmento-0.2.3.tar.gz | 8.4 kB | Without provenance | Details for pigmento-0.2.3.tar.gz |
Release files / pigmento-0.2.3.tar.gz
| Download URL | pigmento-0.2.3.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2a94380bd08786aeabb889206064f25ca76e5ca0c2d742192daf13e2343b30e6
|
|
BLAKE2b-256 checksum How to use checksums |
e22784c5bbae1b53267d239b85c721aa09f49c2f0ae98943405e98b9491088e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Security for pigmento 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.