Skip to content

Commit d966837

Browse files
created "Lesson-free" index page for all modules and exercises
1 parent 3d38428 commit d966837

10 files changed

Lines changed: 205 additions & 59 deletions

File tree

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55

66
class Row:
7-
"""This class represents a single row with ID, first name, last name and state attributes"""
8-
def __init__(self, fname: str, lname: str, state: str):
7+
"""
8+
This class represents a single row
9+
with ID, first name, last name and state attributes
10+
"""
11+
def __init__(self, fname, lname, state):
912
self.row_id = str(uuid4()) # randomly generated unique ID
1013
self.fname = fname
1114
self.lname = lname
@@ -33,13 +36,14 @@ def size(self) -> int:
3336
pass
3437

3538
def get_number_of_pages(self) -> int:
36-
"""Get how many pages the report has; this will be based on limit variable.
39+
"""
40+
Get how many pages the report has; this will be based on limit variable.
3741
If your limit=4 and rows list has 6 records then there are two pages: page1 has 4 records, page2 has 2 records
3842
hint: you'll want to round up
3943
"""
4044
pass
4145

42-
def get_paged_rows(self, sort_field: str, page: int) -> list:
46+
def get_paged_rows(self, sort_field, page):
4347
"""Return a list of rows for a specific page number
4448
:param sort_field: field to sort on, "name" or "-name" (descending)
4549
:param page: specific page for returning data

‎source/exercises/index.rst‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ Lesson 9: Multiple Inheritance
106106
Lesson 10: Catch up
107107
-------------------
108108

109-
It's now time to catch up on everything you haven't quite finished.
110-
In particular, you should finish an object oriented mailroom that is fully complete and unit tested.
111109

112110

113111
Extra Exercises
@@ -118,8 +116,6 @@ The following are not required, but can be useful learning tools. Feel free to t
118116
.. toctree::
119117
:maxdepth: 1
120118

121-
roman.rst
122-
123119
rot13.rst
124120

125121
lambda_magic.rst

‎source/exercises/mailroom-part2.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Example:
4141
4242
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."
4343

44+
4445
Update mailroom with file writing.
4546
----------------------------------
4647

‎source/exercises/oo_intro.rst‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,31 @@ This assignment uses Object Oriented Programming to design a class that can be u
88

99
We have done some reporting in our mailroom program, but that report was pretty simple and used simple functions to accomplish the work.
1010

11-
We will explore here, how one can utilize OO to improve and enhance reporting capabilities.
11+
We will explore here how one can utilize OO to improve and enhance reporting capabilities.
1212

1313

1414
Procedure
1515
=========
1616

17-
You will use :download:`oo_intro.py` file as a starting point for your code.
17+
You will use :download:`../examples/oo_intro/oo_intro.py` file as a starting point for your code.
1818

19-
You will notice that ``Report`` class will have attributes and methods defined for you, including input parameters (and their types) as well as expected output. You will need to fill out the code for each defined method and docstrings contain additional information on what is expected.
19+
You will notice that ``Report`` class will have attributes and methods defined for you, including input parameters (and their types) as well as expected output. You will need to fill out the code for each defined method and docstrings containing additional information on what is expected.
2020

21-
The ``Report`` class uses another class that is fully defined for you, the ``Row`` class. This class represents a single row in your report, and the report class will hold a list of the row instances.
21+
The ``Report`` class uses another class that is fully defined for you,
22+
the ``Row`` class. This class represents a single row in your report, and the report class will hold a list of the row instances.
2223
There are big advantages to using a class like ``Row`` in contrast to a simple dictionary, this design creates a clear contract on what's expected to be as part of a row, where with a dictionary it is easy to misspell or miss a key.
23-
A class like ``Row`` is often called a dataclass (since it only holds "data" and doesn't actually have any logic to it).
24+
A class like ``Row`` is often called a "data class" (since it only holds "data" and doesn't actually have any logic to it).
2425

25-
This is such a popular pattern that python introduced dataclasses to make this even simpler and you can read more about them here:
26-
https://realpython.com/python-data-classes/. You do not use them in this assignment but you should know that they exist and why.
26+
This is such a popular pattern that python introduced ``dataclasses`` in
27+
`version 3.7 <https://www.python.org/dev/peps/pep-0557>`_ to make this even simpler. You do not use them in this assignment but you should know that they exist and why.
2728

29+
You can read more about ``dataclasses`` in the Python docs here:
2830

31+
https://docs.python.org/3/library/dataclasses.html
32+
33+
Or a more descriptive tutorial here:
34+
35+
https://realpython.com/python-data-classes/.
36+
37+
Reminder: use Test Driven Development and make sure you have complete unit tests for all of your class methods.
2938

30-
And at last, you will of course want to include unit tests covering all of your class methods.

‎source/exercises/roman.rst‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎source/exercises/unit_testing.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ with ``test_`` prepended.
110110

111111
.. note::
112112
Technically, you can name your test files anything you want. But there are two reasons to use standard naming conventions.
113-
One is that it is clear to anyone looking at the code what is and isn't a test module. The other is that pytest, and other testing systems, use `naming conventions <https://docs.pytest.org/en/latest/goodpractices.html#test-discovery>`_ to find your test files.
113+
One is that it is clear to anyone looking at the code what is and isn't a test module. The other is that pytest, and other testing systems, use
114+
`naming conventions <https://docs.pytest.org/en/latest/goodpractices.html#test-discovery>`_ to find your test files.
114115
If you name your test files: ``test_something.py`` then pytest will find them for you. And if you use the name of the module being tested:
115116
``test_name_of_tested_module.py`` then it will be clear which test files belong to which modules.
116117

‎source/index.rst‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ Course Materials
2121
.. toctree::
2222
:maxdepth: 2
2323

24-
modules/dev_environment/index
25-
2624
modules/index
2725

2826
exercises/index

‎source/modules/IteratorsAndGenerators.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Iteration is one of the main reasons Python code is so readable:
9999
for x in just_about_anything:
100100
do_stuff(x)
101101
102-
An iterable is anything that can be looped over sequentially, so it does not have to be
102+
An "iterable" is anything that can be looped over sequentially, so it does not have to be
103103
a "sequence": list, tuple, etc. For example, a string is iterable. So is a set.
104104

105105
An iterator is an iterable that remembers state. All sequences are iterable, but

‎source/modules/Strings.rst‎

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ Strings
2323

2424
Full of confidence, the string replies: "Nope, I'm a frayed knot."
2525

26-
A "String" is a computerese word for a piece of text -- a "string" of characters. Why "string"? "String" can be used to mean "(a linear sequence (as of characters, words, proteins, etc.)"
27-
(`definition of string <http://wordnetweb.princeton.edu/perl/webwn?s=string>`_)
26+
A "String" is a computerese word for a piece of text -- a "string" of characters.
27+
28+
Why "string"?
29+
30+
"String" can be used to mean "a linear sequence (as of characters, words, proteins, etc.)"
31+
32+
`Definition of string <http://wordnetweb.princeton.edu/perl/webwn?s=string>`_
2833

2934
So a string is a sequence of individual letters or characters.
3035

31-
In Python, each character can be a `Unicode <https://unicode.org/>`_ character -- that is, any character in any language in the world. Having this built in by default in Python (3) means that you can get very far simply ignoring it -- anything you can type on your computer can be used in strings in Python. If you do need to work with non-English characters, or data encoded in non-utf-8, particularly on Python 2, here are some notes about that: :ref:`unicode`. But for the most part, in Python3 -- strings are text, and text is string, and that's that.
36+
In Python, each character can be a `Unicode <https://unicode.org/>`_ character -- that is, any character in any language in the world. Having this built in by default in Python (3) means that you can get very far simply ignoring it -- anything you can type on your computer can be used in strings in Python. If you do need to work with non-English characters, or data encoded in non-utf-8, particularly on Python 2, here are some notes about that: :ref:`unicode`. But for the most part, in Python3 -- strings are text, and text is strings, and that's that. If you know how to type characters (accented, etc.) that are not used in English on your computer, they should "just work".
3237

3338
Creating strings:
3439
-----------------
@@ -47,11 +52,11 @@ A literal can be delineated with single or double quotes, alone or in triples.
4752

4853
"""and this also"""
4954

50-
'''and even this
51-
triple quotes preserve newlines
55+
'''and even this.
56+
Triple quotes preserve newlines
5257
so this is three lines'''
5358

54-
You can also use call the string object (``str()``) to "make" a string out of other data types.
59+
You can also call the string object (``str()``) to "make" a string out of other data types.
5560

5661
.. code-block:: ipython
5762
@@ -60,12 +65,13 @@ You can also use call the string object (``str()``) to "make" a string out of ot
6065
6166
Strings can also be read from files or other sources of I/O.
6267

68+
6369
String Methods
6470
===============
6571

6672
The python string object is very powerful with lots of methods for common text manipulation. "methods" are functions defined on an object itself (more on that when we get to OO programming). But it means that you have many ways to manipulate text built right into the string objects themselves.
6773

68-
Note that strings are "immutable" --they can not be changed once they have been created. So the string methods all return new objects, rather than change the string in place.
74+
Note that strings are "immutable" -- they can not be changed once they have been created. So the string methods all return new strings, rather than change the string in place. Which is kind of handy if you want to string multiple operations together (pun intended ...).
6975

7076
Here are just a few of the more common string methods:
7177

@@ -88,11 +94,11 @@ Splitting and Joining Strings
8894
8995
It may seem odd at first that ``.join()`` is a string method, rather than, say, a method on lists. But in fact, it makes a lot of sense. Lists (and tuples, and other sequences) can hold any type of data -- and "joining" arbitrary data types doesn't make any sense. Joining is strictly a string activity.
9096

91-
And you need a string so you can join the parts -- therefore, we need a string object in there somewhere anyway.
97+
And you need a string so you can join the parts with something (e.g. a space, or a comma, or ...) -- therefore, we need a string object in there somewhere anyway.
9298

9399
Lastly, having join() be a string method means that it can join strings in ANY iterable object -- not just lists or other built-in sequence types.
94100

95-
So it does make sense -- but even if doesn't make sense to you, that's the way it is -- so remember that you call ``.join()`` on the string you want to join things with.
101+
So it does make sense. But even if doesn't make sense to you, that's the way it is -- so remember that you call ``.join()`` on the string you want to join things with, not on the sequence.
96102

97103
So to be clear: if you have a bunch of strings in a sequence and you want to put them together, you create a string with the character (or characters) you want to join them with, and call join() on that object:
98104

@@ -115,6 +121,7 @@ Maybe not very common, but you can join with a longer string as well:
115121
In [5]: " --#-- ".join(["these", "are", "some", "strings"])
116122
Out[5]: 'these --#-- are --#-- some --#-- strings'
117123
124+
118125
Building up a Long String.
119126
--------------------------
120127

@@ -135,7 +142,18 @@ But: strings are immutable -- Python needs to create a new string each time you
135142
msg.append(piece)
136143
" ".join(msg)
137144
138-
appending to lists is efficient -- and so is the ``join()`` method of strings.
145+
appending to lists is efficient -- and so is the ``join()`` method of strings. In fact the `+=` approach is so inefficient that the `sum()` function explicitly forbids summing strings:
146+
147+
.. code-block:: ipython
148+
149+
In [2]: sum(stuff_to_join, "")
150+
---------------------------------------------------------------------------
151+
TypeError Traceback (most recent call last)
152+
<ipython-input-2-1c17e5fa4f59> in <module>
153+
----> 1 sum(stuff_to_join, "")
154+
155+
TypeError: sum() can't sum strings [use ''.join(seq) instead]
156+
139157
140158
Case Switching
141159
--------------
@@ -182,7 +200,7 @@ Testing for certain classes of characters
182200
String Literals
183201
-----------------
184202

185-
Sometimes when you are creating a string, you want to put an non-normal character in there --one that isn't strictly a letter or symbol, such as newlines, etc.
203+
Sometimes when you are creating a string, you want to put an non-normal character in there -- one that isn't strictly a letter or symbol, such as newlines, etc.
186204

187205
To do that, python support a set of "escape" sequences -- when a character follows a backslash, it gets interpreted as having a particular meaning.
188206

@@ -219,7 +237,7 @@ There are times when you want a literal backslash in your string: Windows file p
219237

220238
Add an ``r`` in front of the string literal:
221239

222-
**Escape Sequences Ignored**
240+
**Escape Sequences Are Ignored**
223241

224242
.. code-block:: ipython
225243
@@ -252,18 +270,18 @@ If you put two string literals next to each other in the code, Python will join
252270
Out[6]: 'thisthat'
253271
254272
(note: no comma in between!)
255-
THis may not look useful, but when combined with the fact that Python joins together lines when inside a parentheses, it can be a nice way to make larger string literals:
273+
This may not look useful, but when combined with the fact that Python joins together lines when inside a parentheses, it can be a nice way to make larger string literals:
256274

257275
.. code-block:: ipython
258276
259277
In [7]: print("This is the first line\n"
260278
...: "And here is another line\n"
261-
...: "If I don't put in a newline"
262-
...: "I can get an very long line in, without making the"
279+
...: "If I don't put in a newline "
280+
...: "I can get a very long line in, without making the"
263281
...: "line of code too long.")
264282
This is the first line
265283
And here is another line
266-
If I don't put in a newlineI can get an very long line in, without making the line of code too long.
284+
If I don't put in a newline I can get a very long line in, without making the line of code too long.
267285
268286
Ordinal values
269287
--------------
@@ -274,9 +292,9 @@ Characters in strings are stored as numeric values:
274292

275293
* Unicode "code points" -- 1 - 1,114,111 (!!!)
276294

277-
Unicode supports a LOT of characters -- every character in every language known to man -- and then some :-). The Unicode code poitns for the characters in the ASCII character set are the same as ASCII -- so handy for us English speakers.
295+
Unicode supports a LOT of characters -- every character in every language known to man -- and then some :-). The Unicode code points for the characters in the ASCII character set are the same as ASCII -- so handy for us English speakers.
278296

279-
To get the value, use ``ord()``:
297+
To get the code point value, use ``ord()``:
280298

281299
.. code-block:: ipython
282300
@@ -292,7 +310,8 @@ To get the character from the code point, use ``chr()``:
292310
.....: print(chr(i), end='')
293311
Chris
294312
295-
For the English language, stick with ASCII, otherwise use, full Unicode: it's easy with Python3
313+
For the English language, stick with ASCII, otherwise use full Unicode: it's easy with Python3
314+
296315

297316
Building Strings from Data
298317
--------------------------
@@ -332,7 +351,8 @@ This is very similar to C-style string formatting (`sprintf`).
332351

333352
It's still around, and handy --- but ...
334353

335-
The "new" ``format()`` method is more powerful and flexible, so we'll focus on that in this class. And there is now the newer "f-strings" (see below) which provide a lot of that "quick and dirty" convenience, while using the same formatting codes as ``.format()``
354+
The "new" ``format()`` method is more powerful and flexible, so we'll focus on that in this class. And there is now the newer "f-strings" (see below) which provide a lot of that "quick and dirty" convenience, while using the same formatting codes as ``.format()``. So there really isn't a reason to use the "old style" anymore.
355+
336356

337357
String Formatting
338358
-----------------
@@ -458,6 +478,8 @@ All the other ways to do this required a lot more typing:
458478
In [30]: "My name is %s %s" % (first, last)
459479
Out[30]: 'My name is Chris Barker'
460480
481+
Even more than the typing, it required keeping the string and the input data in sync when you changed things: maybe adding or removing an interpolated item.
482+
461483
f-string basics
462484
---------------
463485

@@ -522,12 +544,29 @@ And it has to be an expression, not a statement -- so you can't put a for loop o
522544

523545
You can see how this can be a very powerful and quick way to get things done.
524546

525-
F-string Use
547+
Formatting codes with f-strings
548+
-------------------------------
549+
550+
We've seen that f-strings will automatically "stringify" the results of the expression used.
551+
And that's often what you want.
552+
But if you do want to control how that is done, you can use all the same formatting codes used with the ``.format()`` method:
553+
554+
555+
.. code-block:: ipython
556+
557+
In [15]: f"One third with 4 digits is: {1/3:.3f}"
558+
Out[15]: 'One third with 4 digits is: 0.333'
559+
560+
you put the format specifier after a colon.
561+
562+
563+
564+
f-string Use
526565
------------
527566

528-
F-strings are a fairly new Python feature. They will cause a syntax error in any Python version older than 3.6 -- 3.6 was first released on December 23, 2016 -- only a couple years from this writing.
567+
f-strings are a fairly new Python feature. They will cause a syntax error in any Python version older than 3.6 -- 3.6 was first released on December 23, 2016 -- only a couple years from this writing.
529568

530-
So there is not much out there in the wild, and I have yet to see it in production code.
569+
So there is still not much out there in the wild, and it it still rare in production code.
531570

532571
They are not currently used in many of the examples in this course.
533572

@@ -540,10 +579,11 @@ To read all about the justification and syntax, read PEP 498:
540579

541580
https://www.python.org/dev/peps/pep-0498/
542581

582+
543583
Other resources for f-strings
544584
-----------------------------
545585

546-
f-strings are quite new, but there are a few introductions out there:
586+
f-strings have been around a while now, so there are a number of good introductions out there:
547587

548588
A short introduction:
549589

@@ -553,6 +593,8 @@ Another intro:
553593

554594
https://www.pydanny.com/python-f-strings-are-fun.html
555595

596+
http://zetcode.com/python/fstring/
597+
556598
One that gets into the technical details (bytecode! -- for the real geeks):
557599

558600
https://hackernoon.com/a-closer-look-at-how-python-f-strings-work-f197736b3bdb

0 commit comments

Comments
 (0)