如何通过使用“动态”代码减少此示例中的条件开销

时间:2013-01-16 20:51:31

标签: python

在这个天真的代码示例中,我遇到的问题是,每次调用.do()时都需要处理一个条件列表,以使类按预期运行。这不是很有效,而且我很确定还有另外一种方法,但我不能指责它。

我能采取什么方法让这个课程表现得更好,但效率更高?

class translate():
    def __init__(self, EN=True, FR=False, DE=False, SP=False):
        self.EN=EN
        self.FR=FR
        self.DE=DE
        self.SP=SP

    def do(self, word):
        if self.EN:
            self.en(word)
        if self.FR:
            self.fr(word)
        if self.DE:
            self.de(word)
        if self.SP:
            self.sp(word)

    def en(self, word):
        print "In English: %s"%(word)

    def fr(self, word):
        print "In French: %s"%(word)

    def de(self, word):
        print "In German: %s"%(word)    

    def sp(self, word):
        print "In Spanish: %s"%(word)

tr=translate(FR=True)
tr.do("blah")

我可以这样,但我只能做一种语言:

    class translate():
    def __init__(self, EN=False, FR=False, DE=False, SP=False):
        if EN:
            self.do=self.en
        elif FR:
            self.do=self.fr
        elif DE:
            self.do=self.de
        elif SP:
            self.do=self.sp
        else:
            self.do=self.unkown

    def en(self, word):
        print "In English: %s"%(word)

    def fr(self, word):
        print "In French: %s"%(word)

    def de(self, word):
        print "In German: %s"%(word)    

    def sp(self, word):
        print "In Spanish: %s"%(word)

    def unknown(self, word):
        print "Unknown: %s"%(word)

tr=translate(FR=True)
tr.do("blah")

2 个答案:

答案 0 :(得分:4)

在原始代码中,do可以调用多种翻译方法。 因此,我们应该跟踪一个列表(或可能是一组)翻译方法:

class Translate():
    def __init__(self, EN=True, FR=False, DE=False, SP=False):
        self.translators = [method for lang, method in
                            zip((EN, FR, DE, SP),
                                (self.en, self.fr, self.de, self.sp))
                            if lang]

    def do(self, word):
        for method in self.translators:
            method(word)

    def en(self, word):
        print "In English: %s" % (word)

    def fr(self, word):
        print "In French: %s" % (word)

    def de(self, word):
        print "In German: %s" % (word)

    def sp(self, word):
        print "In Spanish: %s" % (word)

tr = Translate(FR=True)
tr.do("blah")

产量

In English: blah
In French: blah

答案 1 :(得分:0)

您实际上并没有将翻译存储在任何地方,所以当您为每个初始化对象的语言输入单词时,您只是打印拼写单词。您需要一个系统,可以存储和检索翻译,并为您提供为给定单词设置翻译的所有值。这可行:

class WordTranslationMap(dict):

    def __init__(self):
        dict.__init__(self, languages)
        for language in languages:
            self[language] = None

    def all(self):
        for language, translation in self.iteritems():
            if translation is not None:
               yield (language, translation)

class TranslationDictionary(dict):

    def __init__(supported_languages=['EN', 'SP', 'FR', 'DE']):
        dict.__init__(self)
        self.supported_languages = supported_languages

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            word = WordTranslationMap(self.supported_languages)
            self[key] = word
            return word

words = TranslationDictionary()
words['hello']['EN'] = 'hello'
words['hello']['FR'] = 'bonjour'
for translation in words['hello'].all:
     print translation

或者,首先按语言组织:

class WordToken(object):
    def __init__(value, translation_dictionary, word_id=None):
        self.value = value
        self.word_id = translation_dictionary.next_id.next() if word_id is None else word_id
        self.translation_dictionary= translation_dictionary

    def __getitem__(self, key):
        try:
            return self.translation_dictionary[key][self.word_id]
        except KeyError:
            return None

    def __setitem__(self, key, value):
        self.translation_dictionary[key][self.word_id] = value

    def all(self):
        for language, translations in self.translation_dictionary.language_dictionaries.iteritems():
            try:
                yield (language, str(translations[self.word_id]))
            except KeyError:
                pass

    def __str__(self):
        return self.value

class LanguageDictionary(dict):

    def __init__(self, translation_dictionary):
        dict.__init__(self)
        self.translation_dictionary = translation_dictionary

    def add_word(self, word):
        word = WordToken(word, self.translation_dictionary)
        self[word.word_id] = word

    def __setitem__(self, key, value):
        try:
            if value.word_id != key:
                raise ValueError('Tried to set translation to word object that does not match language dictionary key.')
        except AttributeError:
            value = WordToken(value, self.translation_dictionary, key)
        dict.__setitem__(self, key, value)

class TranslationDictionary(object):

    def __init__(self):
        self.next_id = itertools.count(start=0, step=1)
        self.language_dictionaries = defaultdict(LanguageDictionary)

    def __getitem__(self, key):
        return self.language_dictionaries[key]

dictionary = TranslationDictionary()
dictionary['EN'].add_word('hello')
dictionary['EN']['hello']['FR'] = 'bonjour'
for translation in dictionary['EN']['hello'].all():
    print translation