urllib
库安装
先切换到pip所在目录
D:Program FilesJetBrainsPyCharm 2017.3.3untitled>cd /d c:
c:>cd c:UsersceshiAppDataLocalProgramsPythonPython36Scripts
执行下载安装指令【哦 这里装了个exe打包工具 要装urllib的话 直接把名字改了就成
c:UsersceshiAppDataLocalProgramsPythonPython36Scripts>pip install Pyinstaller
以下是打包exe的,与urllib无关
c:UsersceshiAppDataLocalProgramsPythonPython36Scripts>pyinstaller.exe -F d:pytestgai.py
=============================================
课文内容待补
====================================
回来了。。
要被这节课搞疯 python2和python3的鸿沟 对于刚入门的小辣鸡来说实在是。。 心疼自己(ಥ_ಥ)
装的是python3.6.4
1 import random
2 from urllib import request #from urllib 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):
def __init__(self, ***)" :
12 "class %%% has-a __init__ that takes seulf and *** parameters.",
13 "class %%%(object):
def ***(self, @@@)":
14 "class %%% has-a function named *** that takes self and @@@ parameters.",
15 "*** = %%%()":
16 "Set *** to an instance of class %%%.",
17 "***.***(@@@)":
18 "From *** get the *** function, and call it with parameters 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 PHRASE_FIRST = True
26 else:
27 PHRASE_FIRST = False
28
29 # load up the words from the website
30 for word in request.urlopen(WORD_URL).readlines(): #for word in urlopen(WORD_URL).readlines():
31 WORDS.append(word.strip())
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(str(random.sample(WORDS, param_count)))) #param_names.append(', '.join(random.sample(WORDS, param_count)))
43
44 for sentence in snippet, phrase:
45 result = sentence[:]
46
47 # fake class names
48 for word in class_names:
49 result = result.replace("%%%", "word", 1) #result = result.replace("%%%", word, 1)
50
51 # fake other names
52 for word in other_names:
53 result = result.replace("***", "word", 1) #result = result.replace("***", word, 1)
54
55 # fake parameter lists
56 for word in param_names:
57 result = result.replace("@@@", word, 1)
58
59 results.append(result)
60
61 return results
62
63 # keep going until they hit CTRL-D
64 try:
65 while True:
66 snippets = list(PHRASES.keys())
67 random.shuffle(snippets)
68
69 for snippet in snippets:
70 phrase = PHRASES[snippet]
71 question, answer = convert(snippet, phrase)
72 if PHRASE_FIRST:
73 question, answer = answer, question
74
75 print(question)
76
77 input("> ")
78 print("ANSWER: %s
" % answer)
79 except EOFError:
80 print("
Bye")
========================
绕得晕乎乎。。 最近转做发行运营公司的测试。 感觉原本仅存的脑子也被吃掉了 写在小本本上背吧。。
在左边有一个Python代码片段列表,右面是他们的解释
class X(Y)
:创建一个叫X的类,并继承Y
class X(object): def __init__(self, J)
:类X有一个__init__
方法,该方法有self和J两个参数
class X(object): def M(self, J)
:类X有一个叫M的函数,该函数有self和J两个参数
foo = X()
:给foo赋值为类X的一个实例
foo.M(J)
:从foo里调用M函数,传递的参数为self和J
foo.K = Q
:从foo里调用K属性,并将其设置为Q。