Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dc28e51

Browse filesBrowse files
committed
typos
1 parent 74ff9ec commit dc28e51
Copy full SHA for dc28e51

File tree

Expand file treeCollapse file tree

12 files changed

+110
-154
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

12 files changed

+110
-154
lines changed
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+21-4Lines changed: 21 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
> "The only way to learn a new programming language is by writing programs in it." - Dennis Ritchie
44
5-
I believe you can learn serious things through silly games. I also think you will learn best by *doing*. This is a book of programming exercises. Each chapter includes a description of a program you should write with examples of how the program should work. Most importantly, each program includes with a test suite so that you know if your program is working well enough.
5+
I believe you can learn serious things through silly games. I also think you will learn best by *doing*. This is a book of programming exercises. Each chapter includes a description of a program you should write with examples of how the program should work. Most importantly, each program includes tests so that you know if your program is working well enough.
66

7-
I won't necessarily show you beforehand what you need to write a program. I'll describe what the program should do and provide some discussion about how to write it. I'll also create an appendix with short examples of how to do things like how to use `argparse`, how to read/write from/to a file, how to process all the files in a directory, how to extract k-mers from a string, etc. I'll provide some building blocks, but I want you to figure out how to put the pieces together.
7+
I won't necessarily show you beforehand how to write each program. I'll describe what the program should do and provide some discussion about how to write it. I'll also create an appendix with short examples of how to do things like how to use `argparse`, how to read/write from/to a file, how to process all the files in a directory, how to extract k-mers from a string, etc. I'll provide some building blocks, but I want you to figure out how to put the pieces together.
88

99
## Forking GitHub repo
1010

@@ -19,13 +19,30 @@ This will allow you to `git pull upstream master` in order to get updates. When
1919

2020
## new.py
2121

22-
I provide a program in the `bin` directory called `new.py` that will help you stub out new Python programs using the `argparse` module to parse the command line arguments and options for your programs. I recommend you start every new program with this program. For example, in the `article` directory the `README.md` wants you to create a program called `article.py`. You should go into the directory with `cd article` and then do:
22+
I provide some useful programs in the `bin` directory including one called `new.py` that will help you stub out new Python programs using the `argparse` module to parse the command line arguments and options for your programs. I recommend you start every new program with this program. For example, in the `article` directory the `README.md` wants you to create a program called `article.py`. You should do this:
2323

2424
````
25+
$ cd article
2526
$ new.py article
2627
````
2728

28-
This will create a new file called `article.py` (that has been made executable with `chmod +x`, if your operating system supports that) that has example code for you to start writing your program. It's best to put `new.py` into your `$PATH` or alter your `$PATH` to include the directory where it's located. I usually create a `$HOME/.local/bin` that I add to my `$PATH` for programs like this.
29+
This will create a new file called `article.py` (that has been made executable with `chmod +x`, if your operating system supports that) that has example code for you to start writing your program.
30+
31+
## $PATH
32+
33+
Your `$PATH` is a list of directories where your operating system will look for programs. To see what your `$PATH` looks like, do:
34+
35+
````
36+
$ echo $PATH
37+
````
38+
39+
Probably each directory is separated by a colon (`:`). *The order of the directories matters!* For instance, it's common to have more than one version of Python installed. When you type `python` on the command line, the directories in your `$PATH` are searched in order, and the first `python` found is the one that is used (and it's probably Python version 2!)
40+
41+
You could execute `new.py` by giving the full path to the program, e.g., `$HOME/work/playful_python/bin/new.py`, but that's really tedious. It's best to put `new.py` into one of the directories that is already in your `$PATH` like maybe `/usr/local/bin`. The problem is that you probably need administrator privileges to write to most of the directories that are in your `$PATH.`. If you are working on your laptop, this is probably not a problem, but if you are on a shared system, you probably won't be able to copy the program into your `$PATH` directories.
42+
43+
An alternative is to alter your `$PATH` to include the directory where `new.py` is located. E.g., if `new.py` is in `$HOME/work/playful_python/bin/`, then add this directory to your `$PATH` -- probably by editing `.bashrc` or `.bash_profile` located in your `$HOME` directory (if you use `bash`). See the documentation for your shell of choice to understand how to edit and persist your `$PATH`.
44+
45+
For what it's worth, I always create a `$HOME/.local` directory for local installations of software I need, so I add `$HOME/.local/bin` to my `$PATH`. Then I copy programs like `new.py` there and they are available to me anywhere on the system.
2946

3047
## Testing your programs
3148

Collapse file

‎appendix/argparse/README.md‎

Copy file name to clipboardExpand all lines: appendix/argparse/README.md
+33Lines changed: 33 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,39 @@ number = 2
254254
files = foo, bar
255255
````
256256

257+
## Choices
258+
259+
Sometimes you want to limit the values of an argument. You can pass in a `list` of valid values to the `choices` option.
260+
261+
````
262+
$ cat appendix/argparse/choices.py
263+
#!/usr/bin/env python3
264+
"""Choices"""
265+
266+
import argparse
267+
268+
parser = argparse.ArgumentParser(
269+
description='Choices',
270+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
271+
272+
parser.add_argument('color', metavar='str', help='Color', choices=['red', 'yellow', 'blue'])
273+
274+
args = parser.parse_args()
275+
276+
print('color =', args.color)
277+
````
278+
279+
Any value not present in the list will be rejected and the user will be shown the valid choices:
280+
281+
````
282+
$ ./choices.py
283+
usage: choices.py [-h] str
284+
choices.py: error: the following arguments are required: str
285+
$ ./choices.py purple
286+
usage: choices.py [-h] str
287+
choices.py: error: argument str: invalid choice: 'purple' (choose from 'red', 'yellow', 'blue')
288+
````
289+
257290
## Automatic help
258291

259292
The `argparse` module reserves the `-h` and `--help` flags for generating help documentation. You do not need to add these nor are you allowed to use these flags for other purposes. Using the above definition, this is the help that `argparse` will generate:
Collapse file

‎bin/compile.py‎

Copy file name to clipboardExpand all lines: bin/compile.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def main():
109109

110110
solution_py = os.path.join(in_dir, dir_name, 'solution.py')
111111
if os.path.isfile(solution_py):
112-
print('\tSOLUTION PY')
112+
print('\tSOLUTION')
113113
fh.write('## Solution\n\n')
114114
fh.write('````\n')
115115
numbered = getoutput('cat -n {}'.format(solution_py))
Collapse file

‎book.md‎

Copy file name to clipboardExpand all lines: book.md
+27-7Lines changed: 27 additions & 7 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
> "The only way to learn a new programming language is by writing programs in it." - Dennis Ritchie
77
8-
I believe you can learn serious things through silly games. I also think you will learn best by *doing*. This is a book of programming exercises. Each chapter includes a description of a program you should write with examples of how the program should work. Most importantly, each program includes with a test suite so that you know if your program is working well enough.
8+
I believe you can learn serious things through silly games. I also think you will learn best by *doing*. This is a book of programming exercises. Each chapter includes a description of a program you should write with examples of how the program should work. Most importantly, each program includes tests so that you know if your program is working well enough.
99

10-
I won't necessarily show you beforehand what you need to write a program. I'll describe what the program should do and provide some discussion about how to write it. I'll also create an appendix with short examples of how to do things like how to use `argparse`, how to read/write from/to a file, how to process all the files in a directory, how to extract k-mers from a string, etc. I'll provide some building blocks, but I want you to figure out how to put the pieces together.
10+
I won't necessarily show you beforehand how to write each program. I'll describe what the program should do and provide some discussion about how to write it. I'll also create an appendix with short examples of how to do things like how to use `argparse`, how to read/write from/to a file, how to process all the files in a directory, how to extract k-mers from a string, etc. I'll provide some building blocks, but I want you to figure out how to put the pieces together.
1111

1212
## Forking GitHub repo
1313

@@ -22,13 +22,30 @@ This will allow you to `git pull upstream master` in order to get updates. When
2222

2323
## new.py
2424

25-
I provide a program in the `bin` directory called `new.py` that will help you stub out new Python programs using the `argparse` module to parse the command line arguments and options for your programs. I recommend you start every new program with this program. For example, in the `article` directory the `README.md` wants you to create a program called `article.py`. You should go into the directory with `cd article` and then do:
25+
I provide some useful programs in the `bin` directory including one called `new.py` that will help you stub out new Python programs using the `argparse` module to parse the command line arguments and options for your programs. I recommend you start every new program with this program. For example, in the `article` directory the `README.md` wants you to create a program called `article.py`. You should do this:
2626

2727
````
28+
$ cd article
2829
$ new.py article
2930
````
3031

31-
This will create a new file called `article.py` (that has been made executable with `chmod +x`, if your operating system supports that) that has example code for you to start writing your program. It's best to put `new.py` into your `$PATH` or alter your `$PATH` to include the directory where it's located. I usually create a `$HOME/.local/bin` that I add to my `$PATH` for programs like this.
32+
This will create a new file called `article.py` (that has been made executable with `chmod +x`, if your operating system supports that) that has example code for you to start writing your program.
33+
34+
## $PATH
35+
36+
Your `$PATH` is a list of directories where your operating system will look for programs. To see what your `$PATH` looks like, do:
37+
38+
````
39+
$ echo $PATH
40+
````
41+
42+
Probably each directory is separated by a colon (`:`). *The order of the directories matters!* For instance, it's common to have more than one version of Python installed. When you type `python` on the command line, the directories in your `$PATH` are searched in order, and the first `python` found is the one that is used (and it's probably Python version 2!)
43+
44+
You could execute `new.py` by giving the full path to the program, e.g., `$HOME/work/playful_python/bin/new.py`, but that's really tedious. It's best to put `new.py` into one of the directories that is already in your `$PATH` like maybe `/usr/local/bin`. The problem is that you probably need administrator privileges to write to most of the directories that are in your `$PATH.`. If you are working on your laptop, this is probably not a problem, but if you are on a shared system, you probably won't be able to copy the program into your `$PATH` directories.
45+
46+
An alternative is to alter your `$PATH` to include the directory where `new.py` is located. E.g., if `new.py` is in `$HOME/work/playful_python/bin/`, then add this directory to your `$PATH` -- probably by editing `.bashrc` or `.bash_profile` located in your `$HOME` directory (if you use `bash`). See the documentation for your shell of choice to understand how to edit and persist your `$PATH`.
47+
48+
For what it's worth, I always create a `$HOME/.local` directory for local installations of software I need, so I add `$HOME/.local/bin` to my `$PATH`. Then I copy programs like `new.py` there and they are available to me anywhere on the system.
3249

3350
## Testing your programs
3451

@@ -481,7 +498,7 @@ Depending on your version of Python, you may be able to use *f-strings*:
481498

482499
# Chapter 4: Howler
483500

484-
Write a Python program `howler.py` that will uppercase all the text from the command line or from a file.
501+
Write a Python program `howler.py` that will uppercase all the text from the command line or from a file. The program should also take a named option of `-o|--outfile` to write the output. The default output should be *standard out* (STDOUT).
485502

486503
````
487504
$ ./howler.py
@@ -503,8 +520,10 @@ $ ./howler.py 'One word: Plastics!'
503520
ONE WORD: PLASTICS!
504521
$ ./howler.py ../inputs/fox.txt
505522
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
523+
$ ./howler.py -o out.txt ../inputs/fox.txt
524+
$ cat out.txt
525+
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
506526
````
507-
508527
\newpage
509528

510529
## Solution
@@ -582,8 +601,9 @@ if os.path.isfile(text):
582601
text = text.rstrip()
583602
````
584603

585-
On line 39, we decide where to put the output of our program. The `if` expression will open `out_file` for writing text if `out_file` has been defined. The default value for `out_file` is the empty string which is effectively `False` when evaluated in a Boolean content. Unless the user provides a value, the output file handle `out_fh` will be `sys.stdout`. T
604+
On line 39, we decide where to put the output of our program. The `if` expression will open `out_file` for writing text if `out_file` has been defined. The default value for `out_file` is the empty string which is effectively `False` when evaluated in a Boolean content. Unless the user provides a value, the output file handle `out_fh` will be `sys.stdout`.
586605

606+
To get uppercase, we can use the `text.upper` method. You can either `out_fh.write` this new text or use `print(..., file=...)`, noting which needs a newline and which does not. You can use `fh.close()` to close the file handle, but it's not entirely necessary as the program immediately ends after this. Still, it's good practice to close your file handles.
587607
\newpage
588608

589609
# Chapter 5: Apples and Bananas
Collapse file

‎coin_combos/README.md‎

Copy file name to clipboardExpand all lines: coin_combos/README.md
-50Lines changed: 0 additions & 50 deletions
This file was deleted.
Collapse file

‎coin_combos/solution.py‎

Copy file name to clipboardExpand all lines: coin_combos/solution.py
-75Lines changed: 0 additions & 75 deletions
This file was deleted.
Collapse file

‎first_bank_of_change/README.md‎

Copy file name to clipboardExpand all lines: first_bank_of_change/README.md
+18-12Lines changed: 18 additions & 12 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
# First Bank of Change
22

3-
Write a Python program that will figure out all the different combinations of pennies, nickels, dimes, and quarters in a given `value` provided as a single positional argument. The value must be greater than 0 and less than or equal to 100.
3+
Write a Python program called `fboc.py` that will figure out all the different combinations of pennies, nickels, dimes, and quarters in a given `value` provided as a single positional argument. The value must be greater than 0 and less than or equal to 100.
44

55
````
6-
$ ./combos.py
7-
usage: combos.py [-h] int
8-
combos.py: error: the following arguments are required: int
9-
$ ./combos.py -h
10-
usage: combos.py [-h] int
6+
$ ./fboc.py
7+
usage: fboc.py [-h] int
8+
fboc.py: error: the following arguments are required: int
9+
$ ./fboc.py -h
10+
usage: fboc.py [-h] int
1111
12-
Coin combos for value
12+
First Bank of Change
1313
1414
positional arguments:
1515
int Sum
1616
1717
optional arguments:
1818
-h, --help show this help message and exit
19-
$ ./combos.py 1
19+
$ ./fboc.py 0
20+
usage: fboc.py [-h] int
21+
fboc.py: error: value "0" must be > 0 and <= 100
22+
$ ./fboc.py 124
23+
usage: fboc.py [-h] int
24+
fboc.py: error: value "124" must be > 0 and <= 100
25+
$ ./fboc.py 1
2026
If you give me 1 cent, I can give you:
2127
1: 1 penny
22-
$ ./combos.py 4
28+
$ ./fboc.py 4
2329
If you give me 4 cents, I can give you:
2430
1: 4 pennies
25-
$ ./combos.py 6
31+
$ ./fboc.py 6
2632
If you give me 6 cents, I can give you:
2733
1: 6 pennies
2834
2: 1 nickel, 1 penny
29-
$ ./combos.py 13
35+
$ ./fboc.py 13
3036
If you give me 13 cents, I can give you:
3137
1: 13 pennies
3238
2: 1 dime, 3 pennies
3339
3: 1 nickel, 8 pennies
3440
4: 2 nickels, 3 pennies
35-
$ ./combos.py 27
41+
$ ./fboc.py 27
3642
If you give me 27 cents, I can give you:
3743
1: 27 pennies
3844
2: 1 quarter, 2 pennies
Collapse file

‎first_bank_of_change/solution.py‎

Copy file name to clipboardExpand all lines: first_bank_of_change/solution.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def get_args():
1111
"""Get command-line arguments"""
1212

1313
parser = argparse.ArgumentParser(
14-
description='Coin combos for value',
14+
description='First Bank of Change',
1515
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
1616

1717
parser.add_argument('value', metavar='int', type=int, help='Sum')
1818

1919
args = parser.parse_args()
2020

2121
if not 0 < args.value <= 100:
22-
parser.error('value "{}" must be > 1 and <= 100'.format(args.value))
22+
parser.error('value "{}" must be > 0 and <= 100'.format(args.value))
2323

2424
return args
2525

Collapse file

‎hamming_chain/.log‎

Copy file name to clipboardExpand all lines: hamming_chain/.log
Whitespace-only changes.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.