Exporting / Copying

as_dict method

Register.as_dict()[source]

Export color register as dict.

Return type

Dict[str, str]

Example:

from sty import fg as fg_obj

fg = fg_obj.as_dict()

a = fg['red'] + "I have a red fg." + fg['rs']

print(a)

as_namedtuple method

Register.as_namedtuple()[source]

Export color register as namedtuple.

Example:

from sty import fg as fg_obj

fg = fg_obj.as_namedtuple()

a = fg.yellow + "I have a yellow fg." + fg.rs

print(a)

copy method

The copy method allows you to create deep copies of register-objects. This can be very useful in case you don’t want to mess with sty’s global register-classes (sty.fg., sty.bg, sty.ef, sty.rs).

Register.copy()[source]

Make a deepcopy of a register-object.

Example:

from sty import fg as fg_a, RgbFg, FgRegister

fg_b = fg_a

fg_c = fg_a.copy()

fg_a.set_style('orange', RgbFg(240, 100, 100))

a = fg_a.orange + "I have an orange fg." + fg.rs
b = fg_b.orange + "I have an orange fg too." + fg.rs

print(a, b, sep="\n")

try:
    msg = "But I have no orange fg, because I'm an earlier copy."
    fg_c.orange + msg + fg.rs
except AttributeError:
    print(msg)