OOP Paradigm in Practice: Classes as an Abstraction Tool

In software design – especially in minimalist environments where every line of code must be justified – Python classes evolve from mere data structures into tools for building clean interfaces (APIs). Instead of operating on raw values, we define logical entities that carry specific context.


Implementing Constants via Classes

A prime example of high-level resource management is using classes to define ANSI control codes (terminal colors). Rather than scattering "magic strings" like \033[91m throughout a project, we encapsulate them within a structure that serves as a color palette.

The following example illustrates how the Colors class separates the presentation layer from the business logic:

(...)
class Colors:(...)
    RED = '\033[91m'(...)
def system_report(status: str):
    color = Colors.GREEN if status == "OK" else Colors.RED
    print(f"Operation status: {color}{status}{Colors.RESET}")(...)

Dynamic Classes: State Management

While the above example demonstrates a class as a static container, classes can also manage dynamic state using the __init__ method. This allows for creating instances that "remember" their own data.

(...)
class Process:
    def __init__(self, name: str):
        self.name = name
        self.start = time.time()

    def log_status(self, message: str):
        elapsed = round(time.time() - self.start, 2)
        print(f"[{self.name}] {Colors.YELLOW}{elapsed}s{Colors.RESET}: {message}")(...)
Source: Name: Requirements:
Download Center post_44_2en post_44_1en

How to Properly Construct Classes in Python

Writing classes requires discipline. Here are the key principles for building professional structures:

Declaration and Attributes: A class is defined using the class keyword. Attributes defined directly within the class (as in Colors) are shared across all instances. Attributes inside self (within __init__) are unique to each object.
Methods: Functions inside a class (methods) must take self as the first argument, allowing them to access the specific object's data.
Encapsulation: We group data and the methods that operate on that data in one place. This ensures the rest of the program doesn't need to know how something works internally, only what it needs to do.
Type Hinting: In professional code, always specify argument types (e.g., name: str). This simplifies debugging and makes the code more readable for others.

Why is this approach optimal

Single Source of Truth (SSoT): Modifying a color shade across the entire application is reduced to changing a single constant within the class.
Code Semantics: Calling Colors.GREEN is instantly readable, eliminating the need for a developer to decode ASCII sequences.
System Minimalism: Utilizing Python's built-in mechanisms avoids external dependencies, which is critical for performance-driven projects and clean code.

Module: Full ANSI Color Palette (Kajotte Tools)

To fully control the CLI interface, we must understand the structure of control codes. The \033[X;Ym code consists of an escape identifier, a type (text or background color), and a specific color number. The Colors class below aggregates these values into a readable system of constants.

Class as an Automation Engine: Building an Interactive Focus Timer

The true power of Object-Oriented Programming is revealed when a class stops being just a static container and becomes a process control engine. In this example, we will build a deep work tool that doesn't just store data but actively manages time and user interaction through the terminal.

Source: Name: Requirements:
Download Center post_44_4en post_44_3en

We utilized the technique of dynamic terminal line overwriting using the carriage return character (\r). This allows our progress bar to "stay alive" in a single line instead of cluttering the screen with dozens of new messages. The class handles all the heavy lifting: calculating percentages, selecting the right visual characters (█, ░), and changing colors based on the session status. This is a perfect example of encapsulation, where the class user calls a simple .start() method, while all complex computational logic remains hidden.


Installation and Implementation of Python-colors Module

Terminal automation requires clarity. The python-colors module is designed to simplify console color management using a clean Python class structure.

Package Installation

The package is available directly from the GitHub repository. It can be installed globally or within a virtual environment using pip:
pip install git+https://github.com/Kajotte-studio/Python-colors.git
or (recommended)
pip install kajotte-studio-tools


Documentation and Source Code

The complete source code, pyproject.toml structure, and implementation examples are available in the official project repository:

🔗 GitHub Repository: Kajotte-studio/Python-colors

🔗 Official PyPI Registry: kajotte-studio-tools

The module supports standard ANSI codes, providing tools for both foreground and background styling in the terminal.


👇

▒ Blog Guide: All Programming Posts in One Place. ▹ See