You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d966837
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: source/exercises/mailroom-part2.rst
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,6 +41,7 @@ Example:
41
41
42
42
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."
Copy file name to clipboardExpand all lines: source/exercises/oo_intro.rst
+16-8Lines changed: 16 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,23 +8,31 @@ This assignment uses Object Oriented Programming to design a class that can be u
8
8
9
9
We have done some reporting in our mailroom program, but that report was pretty simple and used simple functions to accomplish the work.
10
10
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.
12
12
13
13
14
14
Procedure
15
15
=========
16
16
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.
18
18
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.
20
20
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.
22
23
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).
24
25
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.
27
28
29
+
You can read more about ``dataclasses`` in the Python docs here:
Copy file name to clipboardExpand all lines: source/exercises/unit_testing.rst
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,7 +110,8 @@ with ``test_`` prepended.
110
110
111
111
.. note::
112
112
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.
114
115
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:
115
116
``test_name_of_tested_module.py`` then it will be clear which test files belong to which modules.
Copy file name to clipboardExpand all lines: source/modules/Strings.rst
+66-24Lines changed: 66 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,12 +23,17 @@ Strings
23
23
24
24
Full of confidence, the string replies: "Nope, I'm a frayed knot."
25
25
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>`_
28
33
29
34
So a string is a sequence of individual letters or characters.
30
35
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".
32
37
33
38
Creating strings:
34
39
-----------------
@@ -47,11 +52,11 @@ A literal can be delineated with single or double quotes, alone or in triples.
47
52
48
53
"""and this also"""
49
54
50
-
'''and even this
51
-
triple quotes preserve newlines
55
+
'''and even this.
56
+
Triple quotes preserve newlines
52
57
so this is three lines'''
53
58
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.
55
60
56
61
.. code-block:: ipython
57
62
@@ -60,12 +65,13 @@ You can also use call the string object (``str()``) to "make" a string out of ot
60
65
61
66
Strings can also be read from files or other sources of I/O.
62
67
68
+
63
69
String Methods
64
70
===============
65
71
66
72
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.
67
73
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 ...).
69
75
70
76
Here are just a few of the more common string methods:
71
77
@@ -88,11 +94,11 @@ Splitting and Joining Strings
88
94
89
95
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.
90
96
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.
92
98
93
99
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.
94
100
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.
96
102
97
103
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:
98
104
@@ -115,6 +121,7 @@ Maybe not very common, but you can join with a longer string as well:
115
121
In [5]: " --#-- ".join(["these", "are", "some", "strings"])
116
122
Out[5]: 'these --#-- are --#-- some --#-- strings'
117
123
124
+
118
125
Building up a Long String.
119
126
--------------------------
120
127
@@ -135,7 +142,18 @@ But: strings are immutable -- Python needs to create a new string each time you
135
142
msg.append(piece)
136
143
"".join(msg)
137
144
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:
TypeError: sum() can't sum strings [use ''.join(seq) instead]
156
+
139
157
140
158
Case Switching
141
159
--------------
@@ -182,7 +200,7 @@ Testing for certain classes of characters
182
200
String Literals
183
201
-----------------
184
202
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.
186
204
187
205
To do that, python support a set of "escape" sequences -- when a character follows a backslash, it gets interpreted as having a particular meaning.
188
206
@@ -219,7 +237,7 @@ There are times when you want a literal backslash in your string: Windows file p
219
237
220
238
Add an ``r`` in front of the string literal:
221
239
222
-
**Escape Sequences Ignored**
240
+
**Escape Sequences Are Ignored**
223
241
224
242
.. code-block:: ipython
225
243
@@ -252,18 +270,18 @@ If you put two string literals next to each other in the code, Python will join
252
270
Out[6]: 'thisthat'
253
271
254
272
(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:
256
274
257
275
.. code-block:: ipython
258
276
259
277
In [7]: print("This is the first line\n"
260
278
...: "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"
263
281
...: "line of code too long.")
264
282
This is the first line
265
283
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.
267
285
268
286
Ordinal values
269
287
--------------
@@ -274,9 +292,9 @@ Characters in strings are stored as numeric values:
274
292
275
293
* Unicode "code points" -- 1 - 1,114,111 (!!!)
276
294
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.
278
296
279
-
To get the value, use ``ord()``:
297
+
To get the code point value, use ``ord()``:
280
298
281
299
.. code-block:: ipython
282
300
@@ -292,7 +310,8 @@ To get the character from the code point, use ``chr()``:
292
310
.....: print(chr(i), end='')
293
311
Chris
294
312
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
+
296
315
297
316
Building Strings from Data
298
317
--------------------------
@@ -332,7 +351,8 @@ This is very similar to C-style string formatting (`sprintf`).
332
351
333
352
It's still around, and handy --- but ...
334
353
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
+
336
356
337
357
String Formatting
338
358
-----------------
@@ -458,6 +478,8 @@ All the other ways to do this required a lot more typing:
458
478
In [30]: "My name is %s %s" % (first, last)
459
479
Out[30]: 'My name is Chris Barker'
460
480
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
+
461
483
f-string basics
462
484
---------------
463
485
@@ -522,12 +544,29 @@ And it has to be an expression, not a statement -- so you can't put a for loop o
522
544
523
545
You can see how this can be a very powerful and quick way to get things done.
524
546
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
526
565
------------
527
566
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.
529
568
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.
531
570
532
571
They are not currently used in many of the examples in this course.
533
572
@@ -540,10 +579,11 @@ To read all about the justification and syntax, read PEP 498:
540
579
541
580
https://www.python.org/dev/peps/pep-0498/
542
581
582
+
543
583
Other resources for f-strings
544
584
-----------------------------
545
585
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:
0 commit comments