Skip to content

Commit 35b3375

Browse files
committed
updated Ch12
1 parent 19624df commit 35b3375

File tree

3 files changed

+89
-70
lines changed

3 files changed

+89
-70
lines changed

Ch12-Modules.ipynb

+76-62
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
{
2020
"cell_type": "code",
2121
"execution_count": null,
22-
"metadata": {
23-
"collapsed": true
24-
},
22+
"metadata": {},
2523
"outputs": [],
2624
"source": [
2725
"# import math module into the global namespace\n",
@@ -33,9 +31,7 @@
3331
{
3432
"cell_type": "code",
3533
"execution_count": null,
36-
"metadata": {
37-
"collapsed": true
38-
},
34+
"metadata": {},
3935
"outputs": [],
4036
"source": [
4137
"import random\n",
@@ -45,9 +41,34 @@
4541
{
4642
"cell_type": "code",
4743
"execution_count": null,
48-
"metadata": {
49-
"collapsed": true
50-
},
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"from random import choice"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"print(choice([1, 2, 3, 4]))"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"help(math)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
5172
"outputs": [],
5273
"source": [
5374
"from math import * # Import all the identifiers from math\n",
@@ -58,9 +79,7 @@
5879
{
5980
"cell_type": "code",
6081
"execution_count": null,
61-
"metadata": {
62-
"collapsed": true
63-
},
82+
"metadata": {},
6483
"outputs": [],
6584
"source": [
6685
"from math import radians, sin\n",
@@ -78,20 +97,18 @@
7897
},
7998
{
8099
"cell_type": "code",
81-
"execution_count": 1,
82-
"metadata": {
83-
"collapsed": true
84-
},
100+
"execution_count": null,
101+
"metadata": {},
85102
"outputs": [],
86103
"source": [
87104
"def isUpper(letter):\n",
88-
" import string\n",
105+
" import string # string name is local\n",
89106
" return letter in string.ascii_uppercase"
90107
]
91108
},
92109
{
93110
"cell_type": "code",
94-
"execution_count": 2,
111+
"execution_count": 15,
95112
"metadata": {},
96113
"outputs": [
97114
{
@@ -108,7 +125,7 @@
108125
},
109126
{
110127
"cell_type": "code",
111-
"execution_count": 3,
128+
"execution_count": 2,
112129
"metadata": {},
113130
"outputs": [
114131
{
@@ -118,7 +135,7 @@
118135
"traceback": [
119136
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
120137
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
121-
"\u001b[0;32m<ipython-input-3-50b10b339d5b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# can we use string module outside isUpper function?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstring\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdigits\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
138+
"\u001b[0;32m<ipython-input-2-1f51304bf154>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# can we use string module outside isUpper function?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstring\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdigits\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
122139
"\u001b[0;31mNameError\u001b[0m: name 'string' is not defined"
123140
]
124141
}
@@ -148,13 +165,11 @@
148165
},
149166
{
150167
"cell_type": "code",
151-
"execution_count": 4,
152-
"metadata": {
153-
"collapsed": true
154-
},
168+
"execution_count": 1,
169+
"metadata": {},
155170
"outputs": [],
156171
"source": [
157-
"def test():\n",
172+
"def testLocalScope():\n",
158173
" k = 5\n",
159174
" for i in range(2):\n",
160175
" for j in range(2):\n",
@@ -166,7 +181,7 @@
166181
},
167182
{
168183
"cell_type": "code",
169-
"execution_count": 5,
184+
"execution_count": 20,
170185
"metadata": {},
171186
"outputs": [
172187
{
@@ -183,33 +198,36 @@
183198
"test()"
184199
]
185200
},
186-
{
187-
"cell_type": "markdown",
188-
"metadata": {},
189-
"source": [
190-
"## use provided module1.py, module2.py modules to demonstrate user defined modules"
191-
]
192-
},
193201
{
194202
"cell_type": "code",
195-
"execution_count": null,
196-
"metadata": {
197-
"collapsed": true
198-
},
199-
"outputs": [],
203+
"execution_count": 21,
204+
"metadata": {},
205+
"outputs": [
206+
{
207+
"ename": "NameError",
208+
"evalue": "name 'k' is not defined",
209+
"output_type": "error",
210+
"traceback": [
211+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
212+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
213+
"\u001b[0;32m<ipython-input-21-eb2fa875d160>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
214+
"\u001b[0;31mNameError\u001b[0m: name 'k' is not defined"
215+
]
216+
}
217+
],
200218
"source": [
201-
"import module1"
219+
"print(k)"
202220
]
203221
},
204222
{
205-
"cell_type": "code",
206-
"execution_count": null,
207-
"metadata": {
208-
"collapsed": true
209-
},
210-
"outputs": [],
223+
"cell_type": "markdown",
224+
"metadata": {},
211225
"source": [
212-
"help(module1)"
226+
"## User-defined modules\n",
227+
"#### use module1.py, module2.py inside modules folder to demonstrate user defined modules and importance of:\n",
228+
"```if __name__ == '__main__':\n",
229+
" ...\n",
230+
"```"
213231
]
214232
},
215233
{
@@ -219,8 +237,8 @@
219237
"# Packages\n",
220238
"- folder with module(s)\n",
221239
"- must define \\__init\\__.py empty module to initialize as package\n",
222-
"- can't import package itself but only module(s) or identifiers in the modules\n",
223-
"- https://docs.python.org/3/tutorial/modules.html#packages\n"
240+
"- can't import package itself (in a useful way) but only module(s) or identifiers in the modules\n",
241+
"- https://docs.python.org/3/tutorial/modules.html#packages"
224242
]
225243
},
226244
{
@@ -232,18 +250,16 @@
232250
},
233251
{
234252
"cell_type": "code",
235-
"execution_count": 6,
236-
"metadata": {
237-
"collapsed": true
238-
},
253+
"execution_count": 3,
254+
"metadata": {},
239255
"outputs": [],
240256
"source": [
241257
"import fibos"
242258
]
243259
},
244260
{
245261
"cell_type": "code",
246-
"execution_count": 7,
262+
"execution_count": 4,
247263
"metadata": {},
248264
"outputs": [
249265
{
@@ -259,7 +275,7 @@
259275
" fibo\n",
260276
"\n",
261277
"FILE\n",
262-
" /Volumes/Storage/Google Drive/CMU/Python/thinkpythonnotebooks/fibos/__init__.py\n",
278+
" /Volumes/Storage/CMU/Sp2019/CS0/thinkpythonnotebooks/fibos/__init__.py\n",
263279
"\n",
264280
"\n"
265281
]
@@ -271,7 +287,7 @@
271287
},
272288
{
273289
"cell_type": "code",
274-
"execution_count": 8,
290+
"execution_count": 5,
275291
"metadata": {},
276292
"outputs": [
277293
{
@@ -281,7 +297,7 @@
281297
"traceback": [
282298
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
283299
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
284-
"\u001b[0;32m<ipython-input-8-e05a5c206d74>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfibos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfibo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfib\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
300+
"\u001b[0;32m<ipython-input-5-94afb397843e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfibos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfibo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfib\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
285301
"\u001b[0;31mAttributeError\u001b[0m: module 'fibos' has no attribute 'fibo'"
286302
]
287303
}
@@ -292,7 +308,7 @@
292308
},
293309
{
294310
"cell_type": "code",
295-
"execution_count": 9,
311+
"execution_count": 6,
296312
"metadata": {},
297313
"outputs": [
298314
{
@@ -310,7 +326,7 @@
310326
},
311327
{
312328
"cell_type": "code",
313-
"execution_count": 10,
329+
"execution_count": 7,
314330
"metadata": {},
315331
"outputs": [
316332
{
@@ -329,9 +345,7 @@
329345
{
330346
"cell_type": "code",
331347
"execution_count": null,
332-
"metadata": {
333-
"collapsed": true
334-
},
348+
"metadata": {},
335349
"outputs": [],
336350
"source": []
337351
}
@@ -352,7 +366,7 @@
352366
"name": "python",
353367
"nbconvert_exporter": "python",
354368
"pygments_lexer": "ipython3",
355-
"version": "3.6.1"
369+
"version": "3.7.2"
356370
}
357371
},
358372
"nbformat": 4,

modules/module1.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
question = "What is the meaning of Life, the Universe, and Everything?"
99
answer = 42
1010

11+
1112
def areaOfCircle(radius):
1213
"""
1314
Given radius of a circle, finds and returns its area.
1415
"""
1516
return math.pi*radius**2
1617

1718

18-
print(question)
19-
print(answer)
20-
area = areaOfCircle(3)
21-
print("area = {:.2f}".format(area))
19+
if __name__ == '__main__':
20+
print(question)
21+
print(answer)
22+
area = areaOfCircle(3)
23+
print("area = {:.2f}".format(area))

modules/module2.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
circumference function
44
"""
55
import math
6-
import module1
6+
#import module1
77

88
question = "What is your quest?"
99
answer = "To seek the holy grail."
1010

11+
1112
def circumference(radius):
1213
"""
1314
Given radius of a circle, finds and returns its circumference.
1415
"""
1516
return math.pi*radius*2
1617

17-
print(question)
18-
print(answer)
19-
print("circumference = {:.2f}".format(circumference(3)))
18+
19+
if __name__ == '__main__':
20+
print(question)
21+
print(answer)
22+
print("circumference = {:.2f}".format(circumference(3)))

0 commit comments

Comments
 (0)