新手,好多地方都不懂,做了点测试,弄了两天,感觉自己好鶸。

  1 # -*- coding:utf-8 -*-
  2 import random
  3 # random:http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html
  4 from urllib import urlopen
  5 import sys
  6 
  7 WORD_URL = "http://192.168.186.129/words.txt"
  8 # 源地址为 http://learncodethehardway.org/words.txt 不过打不开 只好下下来放server上了
  9 WORDS = []
 10 
 11 # 一个大字典
 12 PHRASES = {
 13     "class %%%(%%%):":
 14       "Make a class named %%% that is-a %%%.",
 15     "class %%%(object):\n\tdef __init__(self, ***)" :
 16       "class %%% has-a __init__ that takes self and *** parameters.",
 17     "class %%%(object):\n\tdef ***(self, @@@)":
 18       "class %%% has-a function named *** that takes self and @@@ parameters.",
 19     "*** = %%%()":
 20       "Set *** to an instance of class %%%.",
 21     "***.***(@@@)":
 22       "From *** get the *** function, and call it with parameters self, @@@.",
 23     "***.*** = '***'":
 24       "From *** get the *** attribute and set it to '***'."
 25 }
 26 # @@@ 可以是多个变量。
 27 
 28 # do they want to drill phrases first
 29 if len(sys.argv) == 2 and sys.argv[1] == "english":
 30     PHRASE_FIRST = True
 31 else:
 32     PHRASE_FIRST = False
 33 # 判断有无后缀english
 34 
 35 # load up the words from the website
 36 for word in urlopen(WORD_URL).readlines():
 37     WORDS.append(word.strip())
 38 # 将words.txt里面的单词打散,存入列表WORDS中。
 39 
 40 # ******************************************************************************
 41 # test 1
 42 # for word in urlopen(WORD_URL).readlines():
 43 #     WORDS.append(word)
 44 # conclusion: every words is like :    apple\r\n   banana\r\n
 45 # so the function "strip()" is necessary
 46 # end test 1
 47 # ******************************************************************************
 48 # print WORDS
 49 
 50 
 51 def convert(snippet, phrase):
 52     # 注意:此时snippet, phrase 都已经确定。
 53     class_names = [w.capitalize() for w in    # capitalize apple -> Apple
 54                    random.sample(WORDS, snippet.count("%%%"))]
 55                     # 列表解析( List comprehensions) :  
 56                     # http://www.cnblogs.com/moinmoin/archive/2011/03/10/lsit-comprehensions-generators.html
 57                     # 将随机产生的列表元素首字母大写产生新的列表。
 58                     # 随机产生个数为“%%%”个数个元素。
 59                     # count:http://www.tutorialspoint.com/python/list_count.htm
 60                     # capitalize:http://www.ziqiangxuetang.com/python/att-string-capitalize.html
 61 # ********* test 2 ***************************
 62     # 注意tab
 63     # print random.sample(WORDS, snippet.count("%%%"))
 64     # 测试结果:输出为列表,内涵 0 - 2 个元素,为 snippet 内单词。
 65 # ********* test 2 end ***********************
 66     other_names = random.sample(WORDS, snippet.count("***"))
 67                     # 从WORDS中抽取与“***”个数相等的元素。
 68                     # 与上面语句区别,上面大写了。
 69     results = []
 70     param_names = []
 71 
 72     for i in range(0, snippet.count("@@@")): # 进行与“@@@”个数相等次循环。
 73         param_count = random.randint(1,3) # 1-2 随机数。
 74         param_names.append(', '.join(random.sample(WORDS, param_count)))
 75     # 从WORDS中随机抽取 1 - 2 个元素,逗号隔开放在param_count中。
 76 
 77     for sentence in snippet, phrase:
 78         # 此处 snippet,phrase 仅为类似两个变量,因为在下面传递值时两个变量已经固定。
 79         # 换完问题,换答案。
 80         result = sentence[:]
 81         # 注意:sentence是keys或者value 不是word
 82         # 列表复制方法。
 83 
 84         # fake class names
 85         for word in class_names:
 86             result = result.replace("%%%", word, 1)
 87             # class_names中有“%%%”个元素,由上面count产生。
 88             # 将字符串中的"%%%"替换成列表class_name中的元素,一个一个的换。
 89             # replace()    :  http://www.w3cschool.cc/python/att-string-replace.html  
 90         # fake other names
 91         for word in other_names:
 92             result = result.replace("***", word, 1)
 93 
 94         # fake parameter lists
 95         for word in param_names:
 96             result = result.replace("@@@", word, 1)
 97 
 98         results.append(result)
 99         # 通过循环两次将 snippets 和 phrase 写入results
100 
101     return results
102     # 返回时用的列表形式,results内含有两个元素,分别返给question和answer
103 
104 
105 # keep going until they hit CTRL-D
106 try:
107     while True:
108         snippets = PHRASES.keys() # snippets = 大字典keys
109         random.shuffle(snippets) # 打乱snippets顺序
110         # print snippets[0]    //print the key   just for fun.
111         for snippet in snippets: # 逐个读取列表snippets的元素,目测作用是不重复。
112             phrase = PHRASES[snippet] # 注意:WORDS只在函数中涉及,此部分都是对大字典的操作。
113             question, answer = convert(snippet, phrase) # 送入函数。
114             if PHRASE_FIRST: #如有english question与answer对调
115                 question, answer = answer, question
116 
117             print question
118 
119             raw_input("> ")
120             print "ANSWER:  %s\n\n" % answer
121 except EOFError:
122     print "\nBye"
123 # 错误退出判断
124 
125 
126 
127 
128 
129 # 下面是做的一些测试
130 # import sys
131 
132 # print sys.argv[1]
133 
134 # import random
135 
136 # print random.randint(1,3)
137 
138 # list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
139 # list2 = random.sample(list1, 5)
140 # print list1
141 # print list2
142 
143 # str1 = ['as', 'bs', 'cs']
144 # str2 = random.sample(str1, 2)
145 # print str1
146 # print str2
147 
148 # **************************************************************************
149 # test2
150 
151 # from urllib import urlopen
152 
153 # url = "http://192.168.186.129/words.txt"
154 
155 # WORDS = []
156 
157 # for word in urlopen(url).readlines():
158 #   print word
159 
160 # print WORDS
161 
162 
163 
164 # ***************************************************************************
165 #  a test for 'for'
166 # print "enter the your each value."
167 
168 # value = []
169 
170 # for num in range(0, 6):
171 #   print num
172 #   i = raw_input("> ")
173 #   value.append(i)
174 
175 # print value
176 
177 # ************************************** 
178 # for test 2
179 # for i in range(0, 0):
180 #   print "sb"
181 # for i in range(0, 1):
182 #   print "2b"
183 
184 # ***************************************
185 # for test 3
186 # a = 'asd'
187 # b = 'bnm'
188 # a = 1
189 # b = 2
190 # for i in a, b:
191 #   print i
192 # print "so ga"
193 
194 # **************************************
195 #  a test for "replace"
196 # words = ['I', 'am', 'sb']
197 # sentence = "***,***,***"
198 # sentence_test1 = sentence
199 # sentence_test2 = sentence
200 # for word in words:
201 #   sentence_test1 = sentence_test1.replace('***', word, 1)
202 # for word in words:
203 #   sentence_test2 = sentence_test2.replace('***', word, 2)
204 # print sentence
205 # print sentence_test1
206 # print sentence_test2
207 
208 # more test
209 # nums = ['1', '2', '3', '4', '5', '6']
210 # strs = "*,*,*,*"
211 # str1 = strs
212 # str2 = strs
213 # str3 = strs
214 # for num in nums:
215 #   str1 = str1.replace('*', num, 1)
216 #   str2 = str2.replace('*', num, 2)
217 #   str3 = str3.replace('*', num)
218 # print str1, "\n", str2, "\n", str3
219 
220 # *************************************************
221 # a test for def(return)
222 # def test(a, b):
223 #   str0 = []
224 #   for i in a, b:
225 #       str0.append(i)
226 #   return str0
227 # a = 'aaa'
228 # b = 'bbb'
229 # a1, b1 = test(a, b)
230 # print a1, b1

 

posted on 2015-05-03 14:41  雷欧altman  阅读(283)  评论(0)    收藏  举报