Skip to main content

Adding colours to Python simplified into a single class

Project description

build python3.8 pypi mit black

Object-oriented library for stylizing terminal output


$ pip install object-colors


Args can be provided as strings or as indices corresponding to their index in an ANSI escape sequence

The following would yield the same result

>>> from object_colors import Color
>>> c = Color(effect="bold", fore="red", back="green")
>>> print(c)
Color(effect=1, fore=1, back=2, objects())
>>> c = Color(effect=1, fore=1, back=2)
>>> print(c)
Color(effect=1, fore=1, back=2, objects())

The above options are part of the below mapping

>>> from object_colors import Color
>>> for i, c in enumerate(Color.colors):
...     print(i, c)
0 black
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 white
>>> for i, e in enumerate(Color.effects):
...     print(i, e)
0 none
1 bold
2 dim
3 italic
4 underline
5 blink
6 blinking
7 negative
8 empty
9 strikethrough


To configure the current object either ``effect``, ``fore``, or ``back`` can be provided and they must be an ``int``, ``str``, or ``None``

Create new objects with by providing a ``dict`` object with any keyword argument

Use ``set`` to set multiple parameters

>>> from object_colors import Color
>>> c = Color()
>>> c.set(effect="bold", fore="red", back="red")
>>> print(c)
Color(effect=1, fore=1, back=1, objects())
>>> c.set(bold_green={"effect": "bold", "fore": "green"})
>>> print(c)
Color(effect=1, fore=1, back=1, objects(bold_green))

Return ``str`` or ``tuple`` using ``get``

>>> from object_colors import Color
>>> c = Color()
>>> c.set(red={"fore": "red"})
>>> c.set(yellow={"fore": "yellow"})
>>> print("*") + " " + c.yellow.get("Warning"))
'\u001b[31m*\u001b[0;0m \u001b[33mWarning\u001b[0;0m'
>>> x, y, z ="x", "y", "z")
>>> print(x, y, z)
'\u001b[31mx\u001b[0;0m \u001b[31my\u001b[0;0m \u001b[31mz\u001b[0;0m'

Print the result using ``print``

>>> from object_colors import Color
>>> c = Color(effect="bold", fore="cyan")
>>> c.print("bold cyan")
'\u001b[1;36mbold cyan\u001b[0;0m'

Load all ``effect``, ``fore``, or ``back`` elements using ``populate``

>>> from object_colors import Color
>>> c = Color()
>>> c.populate("fore")
>>> print(c)
Color(effect=None, fore=None, back=None, objects(black, red, green, yellow, blue, magenta, cyan, white))
>>> print(
Color(effect=None, fore=1, back=None, objects(none, bold, dim, italic, underline, blink, blinking, negative, empty, strikethrough))
>>>"strikethrough red")
'\u001b[9;31mstrikethrough red\u001b[0;0m'

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

object_colors-2.0.0.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

object_colors-2.0.0-py3-none-any.whl (6.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page