I have a very large class that serves as the base class for many AdapterClasses. The base class consists of methods that can be classified as follows:
- Translation : The basic translation methods used by all AdapterClasses. The class attribute from AdapterClasses is required (internal / external is used). The tricky part for this is that I would like to maintain class level access from AdapterClasses, but that eluded me. Now I have access level access from AdapterClasses and level access directly from Translator.
- operations : fundamental methods used by all adapters (internal)
- adapter methods : methods that can be overwritten by AdapterClasses for special needs. (Interior)
- api methods : used only externally, but access to all internal methods (external) is required
I thought that sharing as described below would make development easier and make the public api a little clearer. Before I take this refactoring, I would like for some channel to come back. Thoughts, comments, suggestions are welcome.
This is an extremely simplified abstraction of what I am doing, and is not even close to my actual code. I was just trying to tell the structure.
One problem: see comments for accessing the to_adapter class level. I would like to access DislexiaAadpter.tr.to_adapter() , but since this is a class attribute in Translator , you cannot work with multiple adapters at the same time. When the Translator methods were part of BaseClass, this was not a problem.
class Translator(object): ac = None dictionaries = {'english_spanish':{'hello world':'hola mundo', "i'm just backwards":'Solo soy al reves'}} def __init__(self, c): self.ac = c def to_adapter(self, phrase): r = self.by_lang(phrase, self.ac.lang) return r @classmethod def by_lang(cls, phrase, lang=None): d = cls.get_dictionary(lang) if isinstance(d, dict): for k, v in d.items(): if phrase in k: return v return phrase else: return d @classmethod def get_dictionary(cls, lang='english'): for k, v in cls.dictionaries.items(): if lang in k: k = k.split('_') if lang == k[0]: return {v:k for k,v in v.items()} else: return v return "We don't know that language." class BaseApi(object): def __init__(self, c): self.c = c def say(self, phrase): return self.c.translate(phrase) class BaseClass(object): tr = Translator api = BaseApi lang = 'english' id = 'baseclass' slogan = 'base slogan' def __init__(self): self.tr = Translator(self) self.api = self.api(self)
Using:
# Translator print Translator.by_lang('hello world', 'spanish') hola mundo print Translator.by_lang('hola mundo', 'english') hello world
python
arctelix
source share