7
\$\begingroup\$

I made a small command line tool to help my Italian studies. Any thoughts on how it could be improved? I can't think of much but would like to be proved wrong.

import random

class RegularVerb(object):
    def __init__(self, name):
        self._name = name

    def name(self):
        return self._name

    def _make(self, verb_end):
        return self.name()[:-3] + verb_end

    def _build_condizionale_semplice(self, verb_forms, prefix):
        def verb_base(prefix):
            return (prefix + condizionale_semplice_base
                    for condizionale_semplice_base in 
                    ['rei', 'resti', 'rebbe', 'remmo', 'reste', 'rebbero']
                   )
        return dict(zip(verb_forms, verb_base(prefix)))


class EreVerb(RegularVerb):
    def __init__(self, name, verb_forms):
        super(EreVerb, self).__init__(name)
        self._condizionale_semplice = self._build_condizionale_semplice(verb_forms, 'e')

    def condizionale_semplice(self, verb_form):
        return self._make(self._condizionale_semplice[verb_form])


class IreVerb(RegularVerb):
    def __init__(self, name, verb_forms):
        super(IreVerb, self).__init__(name)
        self._condizionale_semplice = self._build_condizionale_semplice(verb_forms, 'i')

    def condizionale_semplice(self, verb_form):
        return self._make(self._condizionale_semplice[verb_form])


class AreVerb(RegularVerb):
    def __init__(self, name, verb_forms):
        super(AreVerb, self).__init__(name)
        self._condizionale_semplice = self._build_condizionale_semplice(verb_forms, 'e')

    def condizionale_semplice(self, verb_form):
        return self._make(self._condizionale_semplice[verb_form])


class Questioner():

    def __init__(self, verb_forms, verbs):
        self._verb_forms = verb_forms
        self._verbs = verbs

    def _handle_answer(self, user_answer, correct_answer):
        if user_answer == correct_answer:
            print "bravo!"
        else:
            print "no, correct form is:", correct_answer

    def ask_random_condizionale_semplice(self):
        def ask(verb_form, verb): 
            print "condizionale semplice for {0} in {1}?".format(verb.name(), verb_form),
            self._handle_answer(raw_input(), verb.condizionale_semplice(verb_form))
        ask(random.choice(self._verb_forms), random.choice(self._verbs))


def build_regular_verbs(verbs, verb_forms):
    def make(verb):
        if verb[-3:] == 'are':
            return AreVerb(verb, verb_forms)
        elif verb[-3:] == 'ire':
            return IreVerb(verb, verb_forms)
        elif verb[-3:] == 'ere':
            return EreVerb(verb, verb_forms)
        else:
            raise Exception("not a regular verb")
    return [make(v) for v in verbs]


def build_questioner(verb_forms, regular_verbs):
    return Questioner(verb_forms, 
                      build_regular_verbs(
                        regular_verbs,    
                        verb_forms
                      )
                     )

if __name__ == "__main__":
    questioner = build_questioner( 
                        ['I sg', 'II sg', 'III sg', 'I pl', 'II pl', 'III pl'],
                        ['passare', 'mettere', 'sentire'],
                       )
    while True: 
        questioner.ask_random_condizionale_semplice()
\$\endgroup\$
1
  • 1
    \$\begingroup\$ hmm now after posting.. I see that the class RegularCondizionaleSemplice is actually useless and could easily be refactored into each type of verb. \$\endgroup\$
    – j-a
    Commented Nov 29, 2011 at 16:04

2 Answers 2

3
\$\begingroup\$

Maybe this could provide of a more neat way of relating verbs with veb_forms:

verb_binder = dict(are=AreVerb, ire=IreVerb, ere=EreVerb)

def build_regular_verbs(verbs, verb_forms):
    def make(verb):
        try:
            return verb_binder[verb[-3:]](verb, verb_forms)
        except KeyError:
            raise Exception("not a regular verb")

    return [make(v) for v in verbs]

not sure if verb_binder dictionary should be a module global or local to the function or maybe it should be located elsewhere, but this is the idea.

\$\endgroup\$
2
\$\begingroup\$

You should be able to delete this method:

def condizionale_semplice(self, verb_form):
    return self._make(self._condizionale_semplice[verb_form])

from the Ere, Are, and Ire classes and add it to the RegularVerb class, since it is the same for all three.

\$\endgroup\$
2
  • \$\begingroup\$ And what would you do with self._condizionale_semplice? (I am not too found of the idea having it used in the super class and let the basic class set it?). \$\endgroup\$
    – j-a
    Commented Dec 2, 2011 at 10:51
  • \$\begingroup\$ I don't have time to look too closely, but I would assume you could merge it together with condizionale_semplice. There's probably more that can be done here. If I get a chance, I'll edit my post with some more suggestions tonight. \$\endgroup\$ Commented Dec 2, 2011 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.