我正在尝试理解以下代码并且遇到困难。有人可以回答几个问题来帮助我理解吗?
我知道PHRASES(一个变量)包含一个字典,所以“class %%%(%%%)”映射到“创建一个名为%%%的类,它是一个%%%。”
线条(第2-13行)是否也实际创建了类,或者它们就像“字符串”一样?因为看起来它可能正在创建类,但我不确定。
我也知道%%%, * 和@@@实际上会被代码中某处的实际单词替换,但不知道如何或在哪里,因为它只是看起来如此混乱。
那么,任何人都可以帮助我吗?谢谢!
1 PHRASES = {
2 "class %%%(%%%):":
3 "Make a class named %%% that is-a %%%.",
4 "class %%%(object):\n\tdef __init__(self, ***)" :
5 "class %%% has-a __init__ that takes self and *** parameters.",
6 "class %%%(object):\n\tdef ***(self, @@@)":
7 "class %%% has-a function named *** that takes self and @@@ parameters.",
8 "*** = %%%()":
9 "Set *** to an instance of class %%%.",
10 "***.***(@@@)":
11 "From *** get the *** function, and call it with parameters self, @@@.",
12 "***.*** = '***'":
13 "From *** get the *** attribute and set it to '***'."
14 }
哦,这是完整的代码,以备你需要的时候:
import random
from urllib 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.",
"*** = %%%()":
"Set *** to an instance of class %%%.",
"***.***(@@@)":
"From *** get the *** function, and call it with parameters self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'."
}
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.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[:]
for word in class_names:
result = result.replace("%%%", word, 1)
for word in other_names:
result = result.replace("***", word, 1)
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question, answer = answer, question
print question
raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"
答案 0 :(得分:1)
这些行正在创建一个字符串字典,其中的键具有与有效Python类定义类似的语法,以及描述它们正在执行的操作的值。
例如,"class %%%(object):\n\tdef __init__(self, ***)"
变为
class %%%(object):
def __init__(self, ***)
我也知道
%%%
,***
和@@@
实际上会被代码中某处的实际单词取代,但不知道如何或在哪里,因为它只是所有看起来太混乱了。
这一点非常明显,例如:
result = result.replace("@@@", word, 1)
这些字词来自WORD_URL = "http://learncodethehardway.org/words.txt"
。
答案 1 :(得分:0)
基本上脚本的作用是:
如果您调用脚本(例如从命令行python oop_test.py
),系统会以抽象的方式提示您问题,然后答案是英文的。
但是,如果您通过将argv定义为英语python oop_test.py english
来调用脚本,那么您将获得英语问题,并且必须采用抽象方式。
功能块为流程提供逻辑。最后一个块try / except调用函数和字典+随机模块来随机化从url导入的单词。