CONFIG Module

This module contains the decorators and the definition of useful variables that are imported by the __init__ script.

class klassez.config.XtermColors(dic_xcolor)[source]

Bases: object

Container for the pretty customization of printed text, in terms of colors and style. Uses the xterm version of the standard matplotlib colors.

Usage:

from klassez import textcolor

text = 'my text'
print(textcolor(text, 'red')

Use

print(klassez.textcolor)

to see the supported colors and styles.

Attributes:
colorsdict

Name of the supported colors and correspondant formatting string.

stylesdict

Name of the supported font styles

resetstr

String to reset the output to normal

Methods

__call__(text[, color, style])

Formats the text.

__call__(text: str, color=None, style=None)[source]

Formats the text.

Parameters:
textstr

Text to format

color, cstr or None

Color to format the text. If None, the default color is kept

style, sstr or None

Style to format the text. If None, the default style is kept

Returns:
fmt_textstr

Formatted text with chosen style and color

__init__(dic_xcolor)[source]

Store dic_xcolor in self.colors.

Parameters:
dic_xcolordict

Name of the colors and rendering strings

reset = '\x1b[0m'[source]
styles = {'bold': '\x1b[1m', 'bold_italic': '\x1b[1;3m', 'italic': '\x1b[3m', 'underline': '\x1b[4m'}[source]
klassez.config.cprint(*args, c=None, s=None, sep=' ', end='\n', file=None, flush=False)[source]

This function can override the default print() function, allowing to format the text easily. Calls klassez.textcolor() with color=c and style=s.

Parameters:
argssequence

Things to print, will be converted in text.

cstr or None

Color of the text

sstr or None

Style of the text

All the rest are the parameters of the print function
klassez.config.cron(f)[source]

Decorator: use it to monitor the runtime of a function.

klassez.config.safe_kws(f)[source]

Decorator.

Let us assume we want to run the following code:

def f(a, b=1):
    print(a, b)

kws = {'a': 1, 'b': 2, 'c': 3}
f(**kws)

This will raise an error, because there is not a parameter c in the signature of f. Decorating f with @safe_kws will filter the passed kws dictionary to include only the parameters that are also present in f, in order to avoid this error to appear.