oop_test.py --- ex41.py 关于该程序架构/设计方法的思考

。_饼干妹妹 提交于 2019-12-06 14:05:43

关于该程序架构/设计方法的思考

有想法就暂时记录起来?

 

目标,也就是测试用例main函数想要实现的功能:

1. 执行程序后能看到一行语句(代码),可随机表示以下6种操作:

(1)某类的创建;

(2)某类的某个实例对象的初始化函数;

(3)某类的类函数的定义;

(4)创建某个类的实例对象;

(5)实例对象调用类函数;

(6)给实例对象的属性赋值。

2. 用户再按一下回车键,屏幕上可显示出对上述语句的具体解释。

 

具体代码的实现:

关于main函数,即测试用例:

1. 随机产生一行语句代码(格式),保存为变量snippet,并得到该语句对应的描述(格式)phrase;

2. 传参给convert(snippet, phrase),得到具体的snippet和phrase(有具体类名函数名);

3. 打印到屏幕;

4. 可加保护try...except。

 

关于其他函数和变量:

1. 建立“语句--解释”的字典。某种语句对应有固定的某种解释语句。用户看到语句(key)后再按回车,屏幕显示语句解释(value);该字典为全局变量。

{"语句格式": "语句解释", 
"class %%%(object):\n\tdef __init__(self, ***)":
"class %%% has-a __init__ that takes self and *** parameters."} 

# 建立6个key-value,把随机的变量名(类名,函数名等)格式化表示(如%%%,***等)。

2. 将网页中字符串读出来,放入新建的数组中;

3. 创建convert(snippet, phrase),接收snippet格式,输出具体phrase解释。

snippet格式——>(1)随机得到类名(2中的),并首字母大写;(2)随机得到函数名/属性名/变量名/参数名,保持小写。(3)用得到的具体单词替换掉相应snippet中的格式符(%%%, ***等),得到具体的phrase——>输出phrase。

 

 

 


源代码:

import random
from urllib.request import urlopen
import sys

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = {
    "class %%%(%%%):":
        "Make a class named %%% that is-a %%%.",
    "class %%%(object):\n\tdef __init__(self, ***)":
        "class %%% has-a __init__ that takes self and *** parameters.",
    "class %%%(object):\n\tdef ***(self, @@@)":
        "class %%% has-a function named *** that takes self and @@@ parameters.",
    "***.***(@@@)":
        "From *** get the *** function, and call it with parameters self, @@@.",
    "***.*** = '***'":
        "From *** get the *** attribute and set it to '***'."
}

# do they want to drill phrases first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
    PHRASE_FIRST = True

# load up the words from the website
for word in urlopen(WORD_URL).readlines():
    WORDS.append(word.decode('utf-8').strip())

def convert(snippet, phrase):
    class_names = [w.capitalize() for w in random.sample(WORDS, snippet.count("%%%"))]
    other_names = random.sample(WORDS, snippet.count("***"))
    results = []
    param_names = []

    for i in range(0, snippet.count("@@@")):
        param_count = random.randint(1, 3)
        param_names.append(', '.join(random.sample(WORDS, param_count)))

    for sentence in snippet, phrase:
        result = sentence[:]

        # fake class names
        for word in class_names:
            result = result.replace("%%%", word, 1)

        # fake other names
        for word in other_names:
            result = result.replace("***", word, 1)

        # fake parameter lists
        for word in param_names:
            result = result.replace("@@@", word, 1)

        results.append(result)

    return results

# keep going until they hit CTRL-D
if __name__ == "__main__":
    try:
        while True:
            snippets = PHRASES.keys()
            random.shuffle(list(snippets))

            for snippet in snippets:
                phrase = PHRASES[snippet]
                question, answer = convert(snippet, phrase)
                if PHRASE_FIRST:
                    question, answer = answer, question
                print (question)

                user = input("> ")
                while(user != "" and user[-1] == ":"):
                    user = input("> ")

                print("ANSWER: %s\n\n" % answer)

    except EOFError:
        print("\nBye")

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!