Skip to main content

Adding colours to Python simplified into a single class

Project description

build python3.8 pypi codecov.io readthedocs.org mit black

Object-oriented library for stylizing terminal output

Installation

$ pip install object-colors

Options

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

Usage

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.red.get("*") + " " + c.yellow.get("Warning"))
'\u001b[31m*\u001b[0;0m \u001b[33mWarning\u001b[0;0m'
>>> x, y, z = c.red.get("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))
>>> c.red.populate("effect")
>>> print(c.red)
Color(effect=None, fore=1, back=None, objects(none, bold, dim, italic, underline, blink, blinking, negative, empty, strikethrough))
>>> c.red.strikethrough.print("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.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

object_colors-2.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file object_colors-2.0.1.tar.gz.

File metadata

  • Download URL: object_colors-2.0.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.1 requests/2.26.0 setuptools/57.1.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.11+

File hashes

Hashes for object_colors-2.0.1.tar.gz
Algorithm Hash digest
SHA256 5a70dc0927cbbeb45ae9534b6688ad947af6d56636421ff661727e5eef0455f2
MD5 19343363c3f1397b6f2c4fde840e303f
BLAKE2b-256 33df1f49213f3453509608e0e720c9a701f9ee14fb3f1777bc8082198a2962f4

See more details on using hashes here.

File details

Details for the file object_colors-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: object_colors-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.1 requests/2.26.0 setuptools/57.1.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.11+

File hashes

Hashes for object_colors-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd62c7cecd0360a73286609f12b92e09b557a50ea0bb367c2bc31e25e077feb4
MD5 a6bae3f5ce906be2fdd27675ccc2d62f
BLAKE2b-256 da005c9e10a2eaa560439e7c5ddcdb2a7a9672d73dbb410a9fc43a0fb52d145c

See more details on using hashes here.

Supported by

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