Skip to content

Commit fad4c81

Browse files
committed
Chapter 32
1 parent fca7c95 commit fad4c81

File tree

4 files changed

+430
-8
lines changed

4 files changed

+430
-8
lines changed
2.08 MB
Binary file not shown.

assets/notebooks/32_modules.ipynb

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 32. How can I reuse functions?\n",
8+
"\n",
9+
"## Working with Jupyter Notebooks and Python Modules\n",
10+
"\n",
11+
"[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n",
12+
"Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/). Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) \n",
13+
"\n",
14+
"---"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## 1. Creating a module\n",
22+
"\n",
23+
"- Learn the steps in Chapter 32!"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"---\n",
31+
"## 2. Importing a module and running a function\n",
32+
" \n",
33+
"- Import the module as is, then call the function:"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"import setup_database\n",
43+
"\n",
44+
"username = setup_database.create_username (\"John\", \"Gelb\") \n",
45+
"print (username)"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"- Import a module and create an alias, then call the function:"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"import setup_database as sdb \n",
62+
"\n",
63+
"username = sdb.create_username (\"John\", \"Gelb\") \n",
64+
"print (username)"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"- Import one single function from a module, then call the function: "
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"from setup_database import create_username \n",
81+
"\n",
82+
"username = create_username (\"John\", \"Gelb\") \n",
83+
"print (username)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"- Import the function from a module and create an alias, then call the function: "
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"from setup_database import create_username as cu\n",
100+
"\n",
101+
"username = cu (\"John\", \"Gelb\") \n",
102+
"print (username)"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"---\n",
110+
"## 3. Importing a module from a different folder \n",
111+
"- Restart the kernel, then add the module folder and import the module:"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": null,
117+
"metadata": {},
118+
"outputs": [],
119+
"source": [
120+
"import sys\n",
121+
"sys.path.append(\"\") # write the module location in between quotes\n",
122+
"import setup_database"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"- Call the function:"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"metadata": {},
136+
"outputs": [],
137+
"source": [
138+
"username = setup_database.create_username (\"John\", \"Gelb\") \n",
139+
"print (username)"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"metadata": {},
145+
"source": [
146+
"---\n",
147+
"## 4. Changing a function in a module and calling it in a Notebook"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"- Restart the kernel, then call the function *before* the change:"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": null,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"import setup_database\n",
164+
"\n",
165+
"username = setup_database.create_username (\"John\", \"Gelb\") \n",
166+
"print (username)"
167+
]
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"metadata": {},
172+
"source": [
173+
"- Call the function *after* the change:"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {},
180+
"outputs": [],
181+
"source": [
182+
"username = setup_database.create_username (\"John\", \"Gelb\") \n",
183+
"print (username)"
184+
]
185+
},
186+
{
187+
"cell_type": "markdown",
188+
"metadata": {},
189+
"source": [
190+
"- Restart the kernel, then run autoreload:"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": null,
196+
"metadata": {},
197+
"outputs": [],
198+
"source": [
199+
"%load_ext autoreload\n",
200+
"%autoreload 2"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {},
206+
"source": [
207+
"- Call the function *before* the change:"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": null,
213+
"metadata": {},
214+
"outputs": [],
215+
"source": [
216+
"import setup_database\n",
217+
"\n",
218+
"username = setup_database.create_username (\"John\", \"Gelb\") \n",
219+
"print (username)"
220+
]
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"metadata": {},
225+
"source": [
226+
"- Call the function *after* the change:"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": null,
232+
"metadata": {},
233+
"outputs": [],
234+
"source": [
235+
"username = setup_database.create_username (\"John\", \"Gelb\") \n",
236+
"print (username)"
237+
]
238+
},
239+
{
240+
"cell_type": "markdown",
241+
"metadata": {},
242+
"source": [
243+
"--- \n",
244+
"## 5. Adding functions to a module "
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {},
250+
"source": [
251+
"- Call `create_password()`:"
252+
]
253+
},
254+
{
255+
"cell_type": "code",
256+
"execution_count": null,
257+
"metadata": {},
258+
"outputs": [],
259+
"source": [
260+
"password = setup_database.create_password()\n",
261+
"print (password)"
262+
]
263+
},
264+
{
265+
"cell_type": "markdown",
266+
"metadata": {},
267+
"source": [
268+
"- Call `create_database()`:"
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": null,
274+
"metadata": {},
275+
"outputs": [],
276+
"source": [
277+
"customers = [[\"Maria\", \"Lopez\"], [\"Julia\", \"Smith\"], [\"Mohammed\", \"Seid\"]] \n",
278+
"db, n = setup_database.create_database(customers)\n",
279+
"print (\"Database:\", db)\n",
280+
"print (\"Number of customers:\", n) "
281+
]
282+
}
283+
],
284+
"metadata": {
285+
"kernelspec": {
286+
"display_name": "Python 3 (ipykernel)",
287+
"language": "python",
288+
"name": "python3"
289+
},
290+
"language_info": {
291+
"codemirror_mode": {
292+
"name": "ipython",
293+
"version": 3
294+
},
295+
"file_extension": ".py",
296+
"mimetype": "text/x-python",
297+
"name": "python",
298+
"nbconvert_exporter": "python",
299+
"pygments_lexer": "ipython3",
300+
"version": "3.9.6"
301+
},
302+
"widgets": {
303+
"application/vnd.jupyter.widget-state+json": {
304+
"state": {},
305+
"version_major": 2,
306+
"version_minor": 0
307+
}
308+
}
309+
},
310+
"nbformat": 4,
311+
"nbformat_minor": 4
312+
}

0 commit comments

Comments
 (0)