如何为一组可扩展的定义/术语对建模?

时间:2011-10-06 02:11:10

标签: dictionary scalability python

现在我的flashcard游戏正在使用prepvocab()方法,我

  • 将一周的术语的术语和翻译定义为字典
  • 添加该周的条款说明
  • 将它们整理到一个词典列表中,用户选择他们的“周”来学习

每次我添加新的一周的术语和翻译时,我都会在可用词典列表中添加另一个元素。我绝对可以看出这不是一件好事。

class Vocab(object):

def __init__(self):
    vocab = {}
    self.new_vocab = vocab
    self.prepvocab()

def prepvocab(self):
    week01 = {"term":"translation"} #and many more...
    week01d = "Simple Latvian words"

    week02 = {"term":"translation"}
    week02d = "Simple Latvian colors"

    week03 = {"I need to add this":"to self.selvocab below"}
    week03d = "Body parts"

    self.selvocab = [week01, week02] #, week03, weekn]
    self.descs = [week01d, week02d] #, week03, weekn]
    Vocab.selvocab(self)

def selvocab(self):
    """I like this because as long as I maintain self.selvocab,
    the for loop cycles through the options just fine"""
    for x in range(self.selvocab):
        YN = input("Would you like to add week " \
                   + repr(x + 1) + " vocab? (y or n) \n" \
                   "Description: " + self.descs[x] + " ").lower()
        if YN in "yes":
            self.new_vocab.update(self.selvocab[x])
    self.makevocab()

我可以肯定地看到这将是一个痛苦的20+是没有问题。我现在正在阅读诅咒,并且正在考虑立即打印所有描述,并让用户选择他们想要为该回合学习的所有内容。

如何更好地维护我的代码部分?任何人都进行了根本性的彻底改革......程序性的?

2 个答案:

答案 0 :(得分:0)

您应该以某种方式将您的术语:翻译对和描述存储在文本文件中。然后,您的程序应该解析文本文件并发现所有可用的课程。这将允许您扩展可用的课程集,而无需编辑任何代码。

至于您选择的课程,请编写一个print_lesson_choices函数,向用户显示可用的课程和说明,然后在选择它们时询问他们的输入。不要在每节课中询问问题,为什么不提出类似的提示:

self.selected_weeks = []

def selvocab(self):
    self.print_lesson_choices()
    selection = input("Select a lesson number or leave blank if done selecting: ")
    if selection == "": #Done selecting
        self.makevocab()
    elif selection in self.available_lessons:
        if selection not in self.selected_weeks:
            self.selected_weeks.append(selection)
            print "Added lesson %s"%selection
        self.selvocab() #Display the list of options so the user can select again
    else:
        print "Bad selection, try again."
        self.selvocab()

答案 1 :(得分:0)

Pickling objects into a database意味着需要花费一些精力来创建一个界面来修改前端的每周课程,但是值得花时间。