1、專有詞彙python
2、措辭練習web
3、練習用代碼api
1 import random 2 from urllib.request import urlopen 3 import sys 4 5 WORD_URL = "http://learncodethehardway.org/words.txt" 6 WORDS = [] 7 8 PHRASES = { 9 "class %%%(%%%):": 10 "Make a class named %%% that is-a %%%.", 11 "class %%%(object):\n\tdef __init__(self, ***)": 12 "class %%% has-a __init__ that takes self and *** params.", 13 "class %%%(object):\n\tdef ***(self, @@@)": 14 "class %%% has-a function *** that takes self and @@@ params.", 15 "*** = %%%()": 16 "Set *** to an instance of class %%%.", 17 "***.***(@@@)": 18 "From *** get the *** function, call it with params self, @@@.", 19 "***.*** = '***'": 20 "From *** get the *** attribute and set it to '***'." 21 } 22 23 # do they want to drill phrases first 24 if len(sys.argv) == 2 and sys.argv[1] =="english": 25 PHRASES_FIRST = True 26 else: 27 PHRASES_FIRST = False 28 29 # load up the words from the website 30 for word in urlopen(WORD_URL).readlines(): 31 WORDS.append(str(word.strip(),encoding="utf-8")) 32 33 def convert(snippet, phrase): 34 class_names = [w.capitalize() for w in 35 random.sample(WORDS, snippet.count("%%%"))] 36 other_names = random.sample(WORDS, snippet.count("***")) 37 results = [] 38 param_names = [] 39 40 for i in range(0, snippet.count("@@@")): 41 param_count = random.randint(1,3) 42 param_names.append(', '.join( 43 random.sample(WORDS, param_count))) 44 45 for sentence in snippet, phrase: 46 result = sentence[:] 47 48 # fake class class_names 49 for word in class_names: 50 result = result.replace("%%%", word, 1) 51 52 # fake other class_names 53 for word in other_names: 54 result = result.replace("***", word, 1) 55 56 # fake parameter lists 57 for word in param_names: 58 result = result.replace("@@@", word, 1) 59 60 results.append(result) 61 62 return results 63 64 # keep going until they hit CTRL+D 65 try: 66 while True: 67 snippets = list(PHRASES.keys()) 68 random.shuffle(snippets) 69 70 for snippet in snippets: 71 phrase = PHRASES[snippet] 72 question, answer = convert(snippet, phrase) 73 if PHRASES_FIRST: 74 question, answer = answer, question 75 76 print(question) 77 78 input("> ") 79 print(f"ANSWER: {answer}\n\n") 80 except EOFError: 81 print("\nBye.")
4、閱讀更多代碼app
找到更多的代碼,用前述措辭閱讀,找到全部帶類的文件,而後完成下列步驟:dom
1. 針對每個類,指出它的名稱,以及它是繼承於哪些類的。ide
2. 列出每一個類中的全部函數,以及這些函數的參數。函數
3. 列出類中用在self上的全部屬性。url
4. 針對每個屬性,指出它是來自哪一個類。spa