Skip to content

Commit 77d4bf4

Browse files
authored
Merge pull request #193 from UCL/week-07-notes-tidying
Week 07 notes tidying
2 parents 9675ecf + f2f55f4 commit 77d4bf4

File tree

4 files changed

+907
-1233
lines changed

4 files changed

+907
-1233
lines changed

ch04packaging/010Installation.ipynb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"outputs": [],
121121
"source": [
122122
"import geopy\n",
123-
"geocoder = geopy.geocoders.Yandex(lang=\"en_US\")"
123+
"geocoder = geopy.geocoders.Nominatim(user_agent=\"mphy0021\") "
124124
]
125125
},
126126
{
@@ -131,16 +131,16 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"[Location(Cambridge, Cambridgeshire County, United Kingdom, (52.208145, 0.133023, 0.0)),\n",
135-
" Location(Cambridge, Ontario, Canada, (43.370599, -80.318989, 0.0)),\n",
136-
" Location(Cambridge, Middlesex County, Massachusetts, United States of America, (42.385899, -71.141684, 0.0)),\n",
137-
" Location(Cambridge, Washington County, State of Idaho, United States of America, (44.572039, -116.678507, 0.0)),\n",
138-
" Location(Cambridge, Isanti County, Minnesota, United States of America, (45.572595, -93.223783, 0.0)),\n",
139-
" Location(Cambridge, East London, Republic of South Africa, (-32.978267, 27.884695, 0.0)),\n",
140-
" Location(Cambridge, Waikato, New Zealand, (-37.889307, 175.465014, 0.0)),\n",
141-
" Location(Cambridge, Cambridgeshire County, United Kingdom, (52.210303, 0.176447, 0.0)),\n",
142-
" Location(Cambridge Bay, Nunavut, Canada, (69.11673, -105.067782, 0.0)),\n",
143-
" Location(Cambridge, Saint James, Jamaica, (18.291699, -77.895883, 0.0))]"
134+
"[Location(Cambridge, Cambridgeshire, East of England, England, United Kingdom, (52.2034823, 0.1235817, 0.0)),\n",
135+
" Location(Cambridge, Cambridgeshire, East of England, England, United Kingdom, (52.2034823, 0.1235817, 0.0)),\n",
136+
" Location(Cambridge, Middlesex County, Massachusetts, United States of America, (42.3750997, -71.1056157, 0.0)),\n",
137+
" Location(Cambridge, Region of Waterloo, Southwestern Ontario, Ontario, N1R 1T7, Canada, (43.3600536, -80.3123023, 0.0)),\n",
138+
" Location(Cambridge, Henry County, Illinois, United States of America, (41.3036472, -90.1928971, 0.0)),\n",
139+
" Location(Cambridge, Isanti County, Minnesota, 55008, United States of America, (45.5727408, -93.2243921, 0.0)),\n",
140+
" Location(Town Of Cambridge, Western Australia, Australia, (-31.935404, 115.79574791389207, 0.0)),\n",
141+
" Location(Cambridge, Story County, Iowa, 50046, United States of America, (41.8990768, -93.5294029, 0.0)),\n",
142+
" Location(Cambridge, Dorchester County, Maryland, 21613, United States of America, (38.5714624, -76.0763177, 0.0)),\n",
143+
" Location(Cambridge, Guernsey County, Ohio, 43725, United States of America, (40.031183, -81.5884561, 0.0))]"
144144
]
145145
},
146146
"execution_count": 2,

ch04packaging/01Libraries.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
" * emails to developer list\n",
9595
" * personal emails\n",
9696
" * tweets\n",
97-
" * [irc](https://freenode.net)/[gitter](http://gitter.im/)/[slack](http://slack.com/)/[[matrix]](http://riot.im/)\n",
97+
" * [irc](https://freenode.net)/[gitter](https://gitter.im/)/[slack](https://slack.com/)/[[matrix]](https://element.io/)\n",
9898
" * issues raised on GitHub\n",
9999
"* Are there contributors other than the lead contributor?\n",
100100
"* Is there discussion of the library on Stack Exchange?\n",

ch04packaging/02Argparse.ipynb

Lines changed: 188 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,68 @@
1414
"This is the standard library for building programs with a command-line interface. Here we show a short introduction to it, but we recommend to read the [official tutorial](https://docs.python.org/3/howto/argparse.html)."
1515
]
1616
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"Let's start by creating a simple `greet` function that accepts some parameters."
22+
]
23+
},
1724
{
1825
"cell_type": "code",
1926
"execution_count": 1,
20-
"metadata": {
21-
"collapsed": false
22-
},
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def greet(personal, family, title=\"\", polite=False):\n",
31+
" greeting = \"How do you do, \" if polite else \"Hey, \"\n",
32+
" if title:\n",
33+
" greeting += f\"{title} \"\n",
34+
"\n",
35+
" greeting += f\"{personal} {family}.\"\n",
36+
" return greeting"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"Now we have a function that greets whoever we want."
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 2,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"data": {
53+
"text/plain": [
54+
"'How do you do, John Cleese.'"
55+
]
56+
},
57+
"execution_count": 2,
58+
"metadata": {},
59+
"output_type": "execute_result"
60+
}
61+
],
62+
"source": [
63+
"greet(\"John\", \"Cleese\", polite=True)"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"If we want to create a command line interface for this function, we need to save it on its own file. To add the capability to accept inputs from the command line we are going to use `argparse`.\n",
71+
"\n",
72+
"Rememer, what's under the `if __name__ == \"__main__\":` block is what's get executed when you run the file!"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 3,
78+
"metadata": {},
2379
"outputs": [
2480
{
2581
"name": "stdout",
@@ -34,6 +90,14 @@
3490
"#!/usr/bin/env python\n",
3591
"from argparse import ArgumentParser\n",
3692
"\n",
93+
"def greet(personal, family, title=\"\", polite=False):\n",
94+
" greeting = \"How do you do, \" if polite else \"Hey, \"\n",
95+
" if title:\n",
96+
" greeting += f\"{title} \"\n",
97+
"\n",
98+
" greeting += f\"{personal} {family}.\"\n",
99+
" return greeting\n",
100+
"\n",
37101
"if __name__ == \"__main__\":\n",
38102
" parser = ArgumentParser(description=\"Generate appropriate greetings\")\n",
39103
" parser.add_argument('--title', '-t')\n",
@@ -42,26 +106,29 @@
42106
" parser.add_argument('family')\n",
43107
" arguments= parser.parse_args()\n",
44108
" \n",
45-
" greeting = \"How do you do, \" if arguments.polite else \"Hey, \"\n",
46-
" if arguments.title:\n",
47-
" greeting += f\"{arguments.title} \"\n",
48-
" greeting += f\"{arguments.personal} {arguments.family}.\"\n",
49-
" print(greeting)\n"
109+
" message = greet(arguments.personal, arguments.family,\n",
110+
" arguments.title, arguments.polite)\n",
111+
" print(message)"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {},
117+
"source": [
118+
"Note that we've created arguments for each argument `greet` accepts and kept what's optional in the function (the keyword arguments) to be also optional for its command-line interface (can you spot how?)."
50119
]
51120
},
52121
{
53122
"cell_type": "markdown",
54123
"metadata": {},
55124
"source": [
56-
"If you are using MacOS or Linux, you do the following to create an executable:"
125+
"We need to tell the computer that this file can be executed to be able to run this script without calling it with `python` everytime. The computer will know what to use by reading the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) `#!`. If you are using MacOS or Linux, you do the following to create an executable:"
57126
]
58127
},
59128
{
60129
"cell_type": "code",
61-
"execution_count": 2,
62-
"metadata": {
63-
"collapsed": true
64-
},
130+
"execution_count": 4,
131+
"metadata": {},
65132
"outputs": [],
66133
"source": [
67134
"%%bash\n",
@@ -77,40 +144,29 @@
77144
},
78145
{
79146
"cell_type": "code",
80-
"execution_count": 3,
81-
"metadata": {
82-
"collapsed": false
83-
},
147+
"execution_count": 5,
148+
"metadata": {},
84149
"outputs": [
85150
{
86-
"name": "stdout",
151+
"name": "stderr",
87152
"output_type": "stream",
88153
"text": [
89154
"usage: greeter.py [-h] [--title TITLE] [--polite] personal family\n",
90-
"\n",
91-
"Generate appropriate greetings\n",
92-
"\n",
93-
"positional arguments:\n",
94-
" personal\n",
95-
" family\n",
96-
"\n",
97-
"optional arguments:\n",
98-
" -h, --help show this help message and exit\n",
99-
" --title TITLE, -t TITLE\n",
100-
" --polite, -p\n"
155+
"greeter.py: error: the following arguments are required: personal, family\n"
101156
]
102157
}
103158
],
104159
"source": [
105-
"%%bash\n",
106-
"./greeter.py --help"
160+
"%%bash --no-raise-error\n",
161+
"./greeter.py"
107162
]
108163
},
109164
{
110165
"cell_type": "markdown",
111166
"metadata": {},
112167
"source": [
113-
"if you are using Windows, change `bash` by `cmd`, and prepend the commands by `python`\n",
168+
"if you are using Windows' commands or powershell terminal (instead of git-bash), then the shebang is ignored and you will have to call `python` explicitily. Additionally, for the notebooks cells, you need to change `bash` by `cmd`.\n",
169+
"\n",
114170
"```\n",
115171
"%%cmd\n",
116172
"python greeter.py John Cleese\n",
@@ -119,10 +175,8 @@
119175
},
120176
{
121177
"cell_type": "code",
122-
"execution_count": 4,
123-
"metadata": {
124-
"collapsed": false
125-
},
178+
"execution_count": 6,
179+
"metadata": {},
126180
"outputs": [
127181
{
128182
"name": "stdout",
@@ -137,12 +191,17 @@
137191
"./greeter.py John Cleese"
138192
]
139193
},
194+
{
195+
"cell_type": "markdown",
196+
"metadata": {},
197+
"source": [
198+
"We can then use the optional arguments as:"
199+
]
200+
},
140201
{
141202
"cell_type": "code",
142-
"execution_count": 5,
143-
"metadata": {
144-
"collapsed": false
145-
},
203+
"execution_count": 7,
204+
"metadata": {},
146205
"outputs": [
147206
{
148207
"name": "stdout",
@@ -159,10 +218,8 @@
159218
},
160219
{
161220
"cell_type": "code",
162-
"execution_count": 6,
163-
"metadata": {
164-
"collapsed": false
165-
},
221+
"execution_count": 8,
222+
"metadata": {},
166223
"outputs": [
167224
{
168225
"name": "stdout",
@@ -183,6 +240,92 @@
183240
"source": [
184241
"Yes, [he is](https://en.wikipedia.org/wiki/John_Cleese#Honours_and_tributes)!"
185242
]
243+
},
244+
{
245+
"cell_type": "markdown",
246+
"metadata": {},
247+
"source": [
248+
"From the error we got above when we called `greeter.py` without arguments, you may have noticed that in the usage message there's also a `-h` optional argument. We know it's optional because it's shown within square brackes, like for `[--polite]`. This new argument, as the usage message seen above, is generated automatically by argparse and you can use it to see the help."
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": 9,
254+
"metadata": {},
255+
"outputs": [
256+
{
257+
"name": "stdout",
258+
"output_type": "stream",
259+
"text": [
260+
"usage: greeter.py [-h] [--title TITLE] [--polite] personal family\n",
261+
"\n",
262+
"Generate appropriate greetings\n",
263+
"\n",
264+
"positional arguments:\n",
265+
" personal\n",
266+
" family\n",
267+
"\n",
268+
"optional arguments:\n",
269+
" -h, --help show this help message and exit\n",
270+
" --title TITLE, -t TITLE\n",
271+
" --polite, -p\n"
272+
]
273+
}
274+
],
275+
"source": [
276+
"%%bash\n",
277+
"./greeter.py --help"
278+
]
279+
},
280+
{
281+
"cell_type": "markdown",
282+
"metadata": {},
283+
"source": [
284+
"Before we move into the next section, let's clean up our `if __name__ == \"__main__\":` block by creating a function that keeps the `argparse` magic. We will call that function `process`."
285+
]
286+
},
287+
{
288+
"cell_type": "code",
289+
"execution_count": 10,
290+
"metadata": {},
291+
"outputs": [
292+
{
293+
"name": "stdout",
294+
"output_type": "stream",
295+
"text": [
296+
"Overwriting greeter.py\n"
297+
]
298+
}
299+
],
300+
"source": [
301+
"%%writefile greeter.py\n",
302+
"#!/usr/bin/env python\n",
303+
"from argparse import ArgumentParser\n",
304+
"\n",
305+
"def greet(personal, family, title=\"\", polite=False):\n",
306+
" greeting = \"How do you do, \" if polite else \"Hey, \"\n",
307+
" if title:\n",
308+
" greeting += f\"{title} \"\n",
309+
"\n",
310+
" greeting += f\"{personal} {family}.\"\n",
311+
" return greeting\n",
312+
"\n",
313+
"def process():\n",
314+
" parser = ArgumentParser(description=\"Generate appropriate greetings\")\n",
315+
"\n",
316+
" parser.add_argument('--title', '-t')\n",
317+
" parser.add_argument('--polite', '-p', action=\"store_true\")\n",
318+
" parser.add_argument('personal')\n",
319+
" parser.add_argument('family')\n",
320+
"\n",
321+
" arguments = parser.parse_args()\n",
322+
"\n",
323+
" print(greet(arguments.personal, arguments.family,\n",
324+
" arguments.title, arguments.polite)) \n",
325+
"\n",
326+
"if __name__ == \"__main__\":\n",
327+
" process()"
328+
]
186329
}
187330
],
188331
"metadata": {
@@ -204,7 +347,7 @@
204347
"name": "python",
205348
"nbconvert_exporter": "python",
206349
"pygments_lexer": "ipython3",
207-
"version": "3.5.2"
350+
"version": "3.8.5"
208351
},
209352
"widgets": {
210353
"state": {},

0 commit comments

Comments
 (0)