Package: pigmento
Pigmento: Colorize and Trace PrintingDue to the limitation of Github and Pypi Markdown, please visitPigmentofor a better reading experience.InstallationpipinstallpigmentoQuick StartfrompigmentoimportpntclassTest:@classmethoddefclass_method(cls):pnt('Hello World')definstance_method(self):pnt('Hello World')@staticmethoddefstatic_method():pnt('Hello World')defglobal_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 WorldStyle Customizationfrompigmentoimportpnt,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 WorldDisplay Mode Customizationfrompigmentoimportpntpnt.set_display_mode(display_method_name=False,)Test.class_method()|Test|Hello WorldPrefixesPigmento supports customized prefixes for each print.
It is important to note that all prefixes are infirst-in-first-printorder.frompigmentoimportpnt,Prefix,Color,Bracketpnt.add_prefix(Prefix('DEBUG',bracket=Bracket.DEFAULT,color=Color.GREEN))global_function()[DEBUG](global_function)Hello WorldDynamic PrefixTexts inside prefix can be dynamically generated.frompigmentoimportpnt,Prefix,Color,BracketclassSystem:STATUS='TRAINING'@classmethoddefget_status(cls):returncls.STATUSpnt.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 WorldBuild-in Time PrefixTimePrefix is a build-in prefix that can be used to display time.importtimeimportpigmentofrompigmentoimportpntpigmento.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 WorldPluginsPigmento supports plugins to extend its functionalities.Build-in LoggerEverytime you print something, it will be logged to a file.importpigmentofrompigmentoimportpntpigmento.add_log_plugin('log.txt')global_function()(global_function)Hello WorldThe log file will be created in the current working directory and the content will be removed the color codes.catlog.txt[00:00:00] (global_function) Hello WorldBuild-in Dynamic ColorDynamicColor will map caller class names to colors.importpigmentofrompigmentoimportpntclassA:@staticmethoddefprint():pnt(f'Hello from A')classB:@staticmethoddefprint():pnt(f'Hello from B')classD:@staticmethoddefprint():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 CPlugin Customizationfrompigmentoimportpnt,BasePluginclassRenamePlugin(BasePlugin):defmiddleware_before_class_prefix(self,name,bracket,color):returnname.lower(),bracket,colordefmiddleware_before_method_prefix(self,name,bracket,color):returnname.replace('_','-'),bracket,colorpnt.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 WorldMultiple PrintersPigmento supports multiple printers.frompigmentoimportPigmento,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))defdivide(a,b):ifnotisinstance(a,int)ornotisinstance(b,int):error('Inputs must be integers')returnifb==0:debug('Cannot divide by zero')returninfo(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 integersLicenseMIT LicenseAuthorJyonn