Skip to content

Commit 4f7fb46

Browse files
committed
updated homework, etc. for session4
1 parent 53eb778 commit 4f7fb46

File tree

5 files changed

+128
-78
lines changed

5 files changed

+128
-78
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.. _exercise_file_lab:
2+
3+
********
4+
File LAB
5+
********
6+
7+
A bit of practice with files
8+
============================
9+
10+
Goal:
11+
-----
12+
13+
Get a little bit of practice with handling files and parsing simple text.
14+
15+
16+
Paths and File Processing
17+
--------------------------
18+
19+
* write a program which prints the full path to all files in the current
20+
directory, one per line
21+
22+
* write a program which copies a file from a source, to a destination
23+
(without using shutil, or the OS copy command)
24+
25+
- advanced: make it work for any size file: i.e. don't read the entire
26+
contents of the file into memory at once.
27+
28+
- Note that if you want it to do any kind of file, you need to open the files in binary mode:
29+
``open(filename, 'rb')`` (or ``'wb'`` for writing.)
30+
31+
32+
File reading and parsing
33+
------------------------
34+
35+
36+
In the class repo, in:
37+
38+
``Examples/Session01/students.txt``
39+
40+
You will find the list I generated in the first class of all the students in the class, and what programming languages they have used in the past.
41+
42+
Write a little script that reads that file, and generates a list of all
43+
the languages that have been used.
44+
45+
Extra credit: keep track of how many students specified each language.
46+
47+
If you've got git set up right, ``git pull upstream master`` should update
48+
your repo. Otherwise, you can get it from gitHub:
49+
50+
https://github.com/UWPCE-PythonCert/IntroPython2016/blob/master/Examples/Session01/students.txt
51+

slides_sources/source/exercises/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Session 4:
3434
:maxdepth: 1
3535

3636
dict_lab
37+
file_lab
3738
kata_fourteen
3839

3940
Session 5:

slides_sources/source/exercises/mailroom.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,60 @@ directory, and add it to your clone early. Make frequent commits with
9999
good, clear messages about what you are doing and why.
100100

101101
When you are done, push your changes and make a pull request.
102+
103+
.. _exercise_mailroom_plus:
104+
105+
Adding dicts...
106+
---------------
107+
108+
109+
For the next week (after Session04)
110+
111+
You should have been able to do all that with the basic data types:
112+
113+
numbers, strings, lists and tuples.
114+
115+
But once you've learned about dictionaries (Session04) you may be able to re-write it a bit more simply and efficiently.
116+
117+
* Update mailroom from last week to:
118+
119+
- Use dicts where appropriate
120+
- Write a full set of letters to everyone to individual files on disk
121+
- See if you can use a dict to switch between the users selections
122+
- Try to use a dict and the .format() method to do the letter as one
123+
big template -- rather than building up a big string in parts.
124+
125+
Example:
126+
127+
.. code-block:: ipython
128+
129+
In [3]: d
130+
Out[3]: {'first_name': 'Chris', 'last_name': 'Barker'}
131+
132+
133+
In [5]: "My name is {first_name} {last_name}".format(**d)
134+
Out[5]: 'My name is Chris Barker'
135+
136+
Don't worry too much about the "**" -- we'll get into the details later, but for now, it means, more or less -- pass this whole dict in as a bunch of keyword arguments.
137+
138+
139+
.. _exercise_mailroom_exeptions:
140+
141+
Adding Exceptions
142+
-----------------
143+
144+
**After Session05:**
145+
146+
* Exceptions:
147+
148+
Now that you've learned about Exception handling, you can update your code to handle errors better -- like when a user inputs bad data.
149+
150+
* Comprehensions:
151+
152+
Can you use comprehensions to clean up your code a bit?
153+
154+
* Tests
155+
156+
Add some tests..
157+
158+

slides_sources/source/session04.rst

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -980,25 +980,8 @@ LAB
980980

981981
Files Lab: If there is time.
982982

983-
Files Lab
984-
---------
983+
:ref:`exercise_file_lab`
985984

986-
In the class repo, in:
987-
988-
``Examples\students.txt``
989-
990-
You will find the list I generated of all the students in the class, and
991-
what programming languages they have used in the past.
992-
993-
Write a little script that reads that file, and generates a list of all
994-
the languages that have been used.
995-
996-
Extra credit: keep track of how many students specified each language.
997-
998-
If you've got git set up right, ``git pull upstream master`` should update
999-
your repo. Otherwise, you can get it from gitHub:
1000-
1001-
``https://github.com/UWPCE-PythonCert/IntroPython2015/blob/master/Examples/students.txt``
1002985

1003986

1004987
=========
@@ -1007,67 +990,21 @@ Homework
1007990

1008991
Recommended Reading:
1009992
---------------------
1010-
* Dive Into Python: Chapt. 13,14
1011-
1012-
Assignments:
1013-
-------------
1014-
1015-
* Finish the dict/sets lab
1016-
* Coding kata: trigrams
1017-
* Paths and files
1018-
* Update mailroom with dicts
1019-
1020-
1021-
Text and files and dicts, and...
1022-
---------------------------------
1023-
1024-
* Coding Kata 14 - Dave Thomas
1025-
1026-
http://codekata.com/kata/kata14-tom-swift-under-the-milkwood/
1027-
1028-
and in this doc:
1029993

1030-
:doc:`./exercises/kata_fourteen`
994+
* Dive Into Python 3: Chapt. 2.7 (and 4 if you haven't already)
1031995

1032-
and on github here
996+
http://www.diveintopython3.net/native-datatypes.html#dictionaries
1033997

1034-
http://uwpce-pythoncert.github.io/IntroToPython/exercises/kata_fourteen.html
998+
* Dive Into Python 3: Chapt. 11
1035999

1036-
.. nextslide::
1037-
1038-
* Use The Adventures of Sherlock Holmes as input:
1039-
1040-
:download:`./exercises/sherlock.txt`
1041-
1042-
and on github here:
1000+
http://www.diveintopython3.net/files.html
10431001

1044-
http://uwpce-pythoncert.github.io/IntroToPython/_downloads/sherlock.txt
10451002

1046-
* This is intentionally open-ended and underspecified. There are many interesting decisions to make.
1047-
1048-
* Experiment with different lengths for the lookup key. (3 words, 4 words, 3 letters, etc)
1049-
1050-
1051-
Paths and File Processing
1052-
--------------------------
1053-
1054-
* write a program which prints the full path to all files in the current
1055-
directory, one per line
1056-
1057-
* write a program which copies a file from a source, to a destination
1058-
(without using shutil, or the OS copy command)
1059-
1060-
- advanced: make it work for any size file: i.e. don't read the entire
1061-
contents of the file into memory at once.
1062-
1063-
- Note that if you want it to do any kind of file, you need to open the files in binary mode:
1064-
``open(filename, 'rb')`` (or ``'wb'`` for writing.)
1065-
1066-
* update mailroom from last week to:
1003+
Assignments:
1004+
-------------
10671005

1068-
- Use dicts where appropriate
1069-
- Write a full set of letters to everyone to individual files on disk
1070-
- See if you can use a dict to switch between the users selections
1071-
- Try to use a dict and the .format() method to do the letter as one
1072-
big template -- rather than building up a big string in parts.
1006+
* Finish the dict/sets lab: :ref:`exercise_dict_lab`
1007+
* Finish the files lab: :ref:`exercise_file_lab`
1008+
* Coding kata: trigrams: :ref:`exercise_trigrams`
10731009

1010+
* update mailroom with dicts :ref:`exercise_mailroom_plus`

slides_sources/source/session05.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ So use names that make sense to the reader.
128128
Naming Guidelines
129129
-----------------
130130

131-
Only use single-letter names for things with limited scope: indexes and teh like:
131+
Only use single-letter names for things with limited scope: indexes and the like:
132132

133133
.. code-block:: python
134134
@@ -201,6 +201,7 @@ Another Branching structure:
201201
202202
Exceptions
203203
----------
204+
204205
Never Do this:
205206

206207
.. code-block:: python
@@ -212,6 +213,8 @@ Never Do this:
212213
except:
213214
print "couldn't open missing.txt"
214215
216+
**always** capture the particular Exception you know how to handle.
217+
215218

216219
Exceptions
217220
----------
@@ -225,7 +228,7 @@ Don't do this:
225228
do_something()
226229
if os.path.exists('missing.txt'):
227230
f = open('missing.txt')
228-
process(f) # never called if file missing
231+
process(f)
229232
230233
It will almost always work -- but the almost will drive you crazy
231234

@@ -252,7 +255,8 @@ So you can do
252255
Or let the Exception be raised....
253256

254257

255-
.. nextslide:: EAFP
258+
EAFP
259+
----
256260

257261

258262
"it's Easier to Ask Forgiveness than Permission"
@@ -264,6 +268,7 @@ http://www.youtube.com/watch?v=AZDWveIdqjY
264268

265269
(PyCon talk by Alex Martelli)
266270

271+
267272
.. nextslide:: Do you catch all Exceptions?
268273

269274
For simple scripts, let exceptions happen.
@@ -390,7 +395,6 @@ LAB
390395

391396
Exceptions Lab:
392397

393-
A number of you already did this -- so do it at home if you haven't
394398

395399
:ref:`exercise_exceptions_lab`
396400

0 commit comments

Comments
 (0)