Dynamically create name recognition methods from a list

I am trying to write a simple console color utility that uses a class full of ANSI codes and generates some helper methods in my console utility, so console.add('text', 'blue')I can do it instead of doing it console.blue('text').

I know that I can define all of these statically (for example, def blue(self, s):), but it does not scale very well if I want to add 100 or more helpers (not what I would like, but if ...)

Here is a simple ANSI card:

class _AnsiColors:
    def __init__(self):
        self.green = 35
        self.red = 1
        self.blue = 32
        self.yellow = 214
        self.amber = 208
        self.olive = 106
        self.orange = 166
        self.purple = 18
        self.pink = 197
        self.gray = 243
        self.dark_gray = 238
        self.light_gray = 248
        self.black = 0
        self.white = 255
        self.debug = 24

ansi = _AnsiColors()

And a console utility (which proxies methods before pyfancyand uses colors):

import copy 

from colors import color    
from pyfancy import *

from ansi import ansi

class console(object):
    def __init__(self, s):
        self._s = pyfancy(s)

    def add(self, s, c='white'):
        if hasattr(ansi, self.add.__name__):
            c = self.add.__name__
        self._s.add(color(s, fg=getattr(ansi, c)))
        return self

    def bold(self, s):
        self._s.bold(s)
        return self

    def raw(self, s):
        self._s.raw(s)  
        return self

    def dim(self, s):
        self._s.dim(s)      
        return self

    def print(self):
        self._s.output()

# Inject ansi color convenience methods
for c in vars(ansi):
    setattr(console, c, copy.deepcopy(console.add))
    getattr(console, c).__name__ = c

Then I can use it like this:

console('raw').bold(' bold').raw(' raw').blue(' blue').red(' red').print()

, blue red , add() , ( , copy.deepcopy), , __name__ , add, (ansi.debug).

, , ?


MCVE /pyfancy:

import copy 

from ansi import ansi

class console(object):
    def __init__(self, s):
        self._s = s

    def add(self, s, c='white'):
        if hasattr(ansi, self.add.__name__):
            c = self.add.__name__
        self._s += '%s(%s)' % (s, c)
        return self

    def print(self):
        print(self._s)

# Inject ansi color convenience methods
for c in vars(ansi):
    setattr(console, c, copy.deepcopy(console.add))
    getattr(console, c).__name__ = c

console('white').blue(' blue').red(' red').print()

# white blue(debug) red(debug)
+4
1

:

class console(object):
    def __init__(self, s):
        self._s = s
        for color in vars(ansi):
            self._colorizer(color)

    def _colorizer(self, c):        
        def add(s):
            self._s += '%s(%s)' % (s, c)
            return self

        self.__setattr__(c, add)

    def print(self):
        print(self._s)

console('white').blue(' blue').red(' red').print()
# white blue(blue) red(red)
+1

Source: https://habr.com/ru/post/1694369/