Python: the best way to break a large class

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) # operations methods def split(self, string): l = string.split(':') prefix = l[0] try: phrase = l[1] except: phrase = prefix prefix = '' return (phrase, prefix) def process_translate(self, phrase, prefix): return prefix+': '+phrase if prefix else phrase # adapter methods def translate(self, phrase): phrase, prefix = self.split(phrase) phrase = self.tr.to_adapter(phrase) return (phrase, prefix) def get_slogan(self): slogan = self.slogan#self.translate(self.slogan) return self.id+" slogan is: "+slogan # AdapterClass class DislexiaApi(BaseApi): def yas(self, phrase): hdys = super(DislexiaApi, self).say(phrase) return hdys class DislexiaClass(BaseClass): id = "dislexia" lang = 'english' slogan = "sdrawkcab tsuj m'i" api= DislexiaApi def make_dislexic(self, string): return string[::-1] def translate(self, phrase): phrase, prefix = super(DislexiaClass, self).translate(phrase) phrase = self.make_dislexic(phrase) return self.process_translate(phrase, prefix) # Wrapper class returns the api of all Adapters. Nice and clean. class WrapperClass(): adapters = [DislexiaClass] def __init__(self): for adapter in self.adapters: setattr(self, adapter.id, adapter().api) 

Using:

 # Translator print Translator.by_lang('hello world', 'spanish') hola mundo print Translator.by_lang('hola mundo', 'english') hello world # AdapterClass d = DislexiaClass() print d.get_slogan() dislexia slogan is: sdrawkcab tsuj m'i d.translate(slogan) dislexia slogan is: i'm just backwards # WrapperClass w = WrapperClass() wd = w.dislexia print wd.yas('hello world') dlrow olleh print wd.yas('hola mundo') dlrow olleh print wd.c.translate(wd.yas('hola mundo')) hello world 
+7
python
source share

No one has answered this question yet.

See related questions:

5504
Does Python have a ternary conditional operator?
5231
What are metaclasses in Python?
4473
Calling an external command in Python
3790
How can I safely create a subdirectory?
3602
Does Python have a "contains" substring method?
3119
What is the difference between Python list methods that are added and expanded?
2818
Finding an index of an element with a list containing it in Python
2601
How can I make a time delay in Python?
2568
How to find out the current time in Python
2097
Is there a way to run Python on Android?

All Articles