Adding colours to Python simplified into a single class
Project description
Pypi | Read the Docs | Github Pages
Installation
pip install object-colors
Setup
from object_colors import Color
c = Color()
Usage
- key-values
Match effects with effect and colors with text and background
keys = ["text", "effect", "background"]
values = {
"colors": ["black", "red", "green", "yellow", "blue", "purple", "cyan", "white"]
"effects": ['none', "bold", 'bright', "underline", "negative"]
}
For most versatile usage simply instantiate the class with the string argument “colors”
This will populate the instance with a subclass for every key in the “colors” object
>>> from object_colors import Color
>>> c = Color("colors")
>>> print(c.__dict__)
{
'text': 7,
'effect': 0,
'background': 0,
'black': <object_colors.Color object at 0x7f3302cf4b10>,
'red': <object_colors.Color object at 0x7f3303aa5d10>,
'green': <object_colors.Color object at 0x7f33037a7710>,
'yellow': <object_colors.Color object at 0x7f3302bd4710>,
'blue': <object_colors.Color object at 0x7f3302bd4d50>,
'purple': <object_colors.Color object at 0x7f3302ce0910>,
'cyan': <object_colors.Color object at 0x7f33037ddc10>,
'white': <object_colors.Color object at 0x7f33005e4c10>,
'bold': <object_colors.Color object at 0x7f3303b09a90>
}
This can be further enhanced with the Color.bold object, initialized when bold has not been activated
>>> c.red.print("not bold")
>>> c.red.bold.print("bold")
All colors, effects and objects can be manipulated however the user pleases, provided a keyword is not used outside its purpose
Methods
- set()
Set values with keyword arguments
>>> c.set(text="red", effect="bold", background="blue")
>>> c.set("red", "bold", "blue")
>>> c.set(114)
>>> print(c.__dict__)
{'text': 1, 'effect': 1, 'background': 4}
Set a new class with a keyword (any key) followed by a dict value
>>> c.set(
red={"text": "red"},
bold_red={"text": "red", "effect": "bold"},
yellow={"text": "yellow"}
)
>>> c.yellow.set(effect="bold")
>>> print(c.__dict__)
{
'text': 1,
'effect': 1,
'background': 4,
'red': <object_colors.Color object at 0x0000020C10D06080>,
'bold_red': <object_colors.Color object at 0x0000020C10D066D8>,
'yellow': <object_colors.Color object at 0x0000020C10D06B00>
}
>>> print(c.yellow.__dict__)
{'text': 3, 'effect': 1, 'background': 0}
- get()
Store values for multicolored printing
>>> bullet = c.red.get("[!] ")
>>> warning = c.yellow("Warning")
>>> print(bullet + warning)
returns a string or a tuple
>>> a, b, c = c.red.get("a", "b", "c")
- print()
Instance includes enhanced print() function for color output
>>> c.print("no color print")
>>> c.red.print("red print", flush=True)
>>> c.yellow.print("yellow print", end="")
- pop()
Remove unused attributes
>>> c.pop("bold_red")
>>> print(c.__dict__)
{
'text': 1,
'effect': 1,
'background': 4,
'red': <object_colors.Color object at 0x0000020C10D06080>,
'yellow': <object_colors.Color object at 0x0000020C10D06B00>
}
Or create new instances
>>> red = c.pop("red")
>>> print(c.__dict__)
{
'text': 1,
'effect': 1,
'background': 4,
'yellow': <object_colors.Color object at 0x0000020C10D06B00>
}
>>> print(red.__dict__)
{'text': 1, 'effect': 0, 'background': 0}
>>> red.print()
>>> red.get()
>>> red.set()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for object_colors-1.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b09bad1d0173b5cb0489c15bf2db19c60dbed31d9b4a74748eac473a8e838f1a |
|
MD5 | 3f200ad0b0d4e643cdaea517a182b9db |
|
BLAKE2b-256 | 123c45f22fd6615c2eb2c6f57e9fd5d700f38c47919ec17bf12aa986fc9a48db |