Introduction Pigmento is a Python package that can colorize and trace printing. It can be used to quickly locate the source of the print statement. Moreover, it supports customizing the style of printing, prefixes, and powerful plugins.
Installation
Quick Start 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 from pigmento import pntclass 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 1 2 3 4 5 6 7 8 9 10 11 12 from pigmento import pnt, Colorpnt.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 1 2 3 4 5 6 7 from pigmento import pntpnt.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.
1 2 3 4 5 from pigmento import pnt, Prefix, Color, Bracketpnt.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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 from pigmento import pnt, Prefix, Color, Bracketclass 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.
1 2 3 4 5 6 7 8 9 import timeimport pigmentofrom pigmento import pntpigmento.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.
1 2 3 4 5 6 import pigmentofrom pigmento import pntpigmento.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.
[00:00:00] (global_function) Hello World
Build-in Dynamic Color DynamicColor will map caller class names to colors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import pigmentofrom pigmento import pntclass 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 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from pigmento import pnt, BasePluginclass 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
Multiple Printers Pigmento supports multiple printers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 from pigmento import Pigmento, Bracket, Color, Prefixdebug = 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
Links