Skip to main content

Adding colours to Python simplified into a single class

Project description

Pypi | Read the Docs | Github Pages

Build Status codecov.io PyPi Version Licence

Installation

pip install object-colors

Setup

>>> from object_colors import Color
>>> color = Color()

Without keywords args are positional like so:

>>> Color("text", "effect", "background")
  • colors:

    • black: 0

    • red: 1

    • green: 2

    • yellow: 3

    • blue: 4

    • purple: 5

    • cyan: 6

    • white: 7

  • effects:

    • None: 0

    • bold: 1

    • bright: 2

    • underline: 3

    • negative: 4

>>> color = Color()
>>> color.set(2, 1, 1)
  • text: green

  • effect: bold

  • background: red

>>> color = Color()
>>> # instance attributes
>>> color.set(
...     text="green",
...     effect="bold",
...     background="red"
... )
>>> # subclasses -  set like those for
>>> # original class only keyword arguments
>>> # are expressed as dictionary
>>> color.set(
...     sub_color={
...         "text": "green",
...         "effect": "bold",
...         "background": "red"
...     }
... )

For most versatile usage simply instantiate the class with the populate_colors() method This will populate the instance with a subclass for every key in the “colors” object

>>> color = Color()
>>> color.populate_colors()
>>> print(color.__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

>>> color.red.print("not bold")
>>> color.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
>>> color.set(text="red", effect="bold", background="blue")
>>> color.set("red", "bold", "blue")
>>> color.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
>>> color.set(red={"text": "red"}, yellow={"text": "yellow"})
>>> print(c.__dict__)
'text': 1,
'effect': 0,
'background': 4,
'red': <object_colors.Color object at 0x0000020C10D06080>,
'yellow': <object_colors.Color object at 0x0000020C10D06B00>
>>> print(color.yellow.__dict__)
{'text': 3, 'effect': 1, 'background': 0}
  • get():

>>> # store values
>>> # useful for multicolored printing
>>> bullet = color.red.get("[!] ")
>>> warning = color.yellow("Warning")
>>> print(bullet + warning)
"\u001b[0;31;40m[!]\u001b[0;0m\u001b[0;33;40mWarning\u001b[0;0m"
>>> # returns a string or a tuple
>>> a, b, c = color.red.get("a", "b", "c")
  • print():

>>> # Instance includes enhanced print() function for color output
>>> color.print("no color print")
>>> color.red.print("red print", flush=True)
>>> color.yellow.print("yellow print", end="")
  • pop():

>>> # remove unused attributes
>>> color.pop("bold_red")
>>> print(color.__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 = color.pop("red")
>>> print(color.__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


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-1.0.8.tar.gz (12.0 kB view hashes)

Uploaded Source

Built Distribution

object_colors-1.0.8-py3-none-any.whl (12.4 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