typed_print
typed_print copied to clipboard
Easily beautify your logs with a drop-in replacement for the print function.
Example
If you print something in your terminal or notebook, e.g.
print('Epoch [4/300]', 3, 3231.32, 'loss=-22.4e-9 time=121mc')
you will see a very bleak output:
Typed_print gets your logs a little bit more cheerful and easy to read.
The output is colorized based on the argument's type. Additionally, some keywords can be highlighted (like Epoch
here). If you print a string, the numbers in the string will get highlighted.
- For a light background you will get:
- For a dark background:
Of course, everything is customizable. For example, you can override list printing like that:
print([131, 'I love cats', 'String with number 9'])
Light palette | Dark palette |
---|---|
![]() |
![]() |
Features
- Type-based coloring and printing layout.
- Automatic highlighting guided by regexp or list of keywords.
- Extensible and customizable framework: easily change representation for objects of any type including the built-in types like
int
,list
,dict
, etc.
Install
pip install typed_print
Tested with Ubuntu 14.04 and Python 3.6.
Usage
For the examples above the printing function is initialized as follows:
import typed_print as tp
print = tp.init(palette='light', # or 'dark'
str_mode=tp.HIGHLIGHT_NUMBERS,
highlight_word_list=['Epoch'])
Arguments
-
palette
: highlighting palette. Usedark
if the background of your terminal is dark andlight
otherwise -
str_mode
: what to highlight in strings and sting representations of objects of unknown types. Possible values are:-
tp.HIGHLIGHT_NOTHING
-
tp.HIGHLIGHT_DIGITS
-
tp.HIGHLIGHT_NUMBERS
-
tp.HIGHLIGHT_CUSTOM
: in this case you should pass a compiled regex extractor asre_custom
argument totp.init
, e.g.-
re_custom=re.compile("\d+")
-
-
-
custom_type_map
: a dictionary with correspondencetype:processor_fn
.processor_fn
should return string representation for the object, similarly to__str__
or__repr__
.-
highlight_word_list
: a list of words to highlight. In the example abovehighlight_word_list=['Epoch']
-
Why overriding print function?
I did not find a way to override __str__
or __repr__
methods for the built-in types. Thus the only way to change the way the objects of built-in type are printed is to override print
function.
UPDATE: @alex-bender mentioned here that there is actually a way to override built-in types.