Skip to content

Commit 68176c7

Browse files
committed
now a book!
1 parent f2e6046 commit 68176c7

File tree

59 files changed

+19016
-670
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+19016
-670
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
clean:
44
find . -type d -name \*cache\* -maxdepth 2 -exec rm -rf {} \;
55
find . -name .log -delete
6+
7+
book:
8+
./bin/compile.py

abuse/solution.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#!/usr/bin/env python3
2-
"""
3-
Author: Ken Youens-Clark <kyclark@gmail.com>
4-
Date : 2019-05-17
5-
Purpose: Shakespearean insult generator
6-
"""
72

83
import argparse
94
import random
@@ -56,19 +51,6 @@ def get_args():
5651
return parser.parse_args()
5752

5853

59-
# --------------------------------------------------
60-
def warn(msg):
61-
"""Print a message to STDERR"""
62-
print(msg, file=sys.stderr)
63-
64-
65-
# --------------------------------------------------
66-
def die(msg='Something bad happened'):
67-
"""warn() and exit with error"""
68-
warn(msg)
69-
sys.exit(1)
70-
71-
7254
# --------------------------------------------------
7355
def main():
7456
"""Make a jazz noise here"""

anagram/solution.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#!/usr/bin/env python3
2-
"""
3-
Author : Ken Youens-Clark <kyclark@gmail.com>
4-
Date : 2019-05-19
5-
Purpose: Find anagrams
6-
"""
72

83
import argparse
94
import logging

apples_and_bananas/solution.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#!/usr/bin/env python3
2-
"""
3-
Author : Ken Youens-Clark <kyclark@gmail.com>
4-
Date : 2019-05-18
5-
Purpose: Apples and bananas
6-
"""
72

83
import argparse
94
import os
@@ -31,19 +26,6 @@ def get_args():
3126
return parser.parse_args()
3227

3328

34-
# --------------------------------------------------
35-
def warn(msg):
36-
"""Print a message to STDERR"""
37-
print(msg, file=sys.stderr)
38-
39-
40-
# --------------------------------------------------
41-
def die(msg='Something bad happened'):
42-
"""warn() and exit with error"""
43-
warn(msg)
44-
sys.exit(1)
45-
46-
4729
# --------------------------------------------------
4830
def main():
4931
"""Make a jazz noise here"""

bacronym/solution.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#!/usr/bin/env python3
2-
"""
3-
Purpose: Make guesses about acronyms
4-
Author: Ken Youens-Clark <kyclark@gmail.com>
5-
Date: 2019-05-19
6-
"""
2+
"""Make guesses about acronyms"""
73

84
import argparse
95
import sys
@@ -56,6 +52,7 @@ def get_args():
5652
# --------------------------------------------------
5753
def main():
5854
"""main"""
55+
5956
args = get_args()
6057
acronym = args.acronym
6158
wordlist = args.wordlist

bin/compile.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Ken Youens-Clark <kyclark@gmail.com>
4+
Date : 2019-05-29
5+
Purpose: Compile my book
6+
"""
7+
8+
import argparse
9+
import os
10+
import sys
11+
from subprocess import getstatusoutput
12+
from dire import die
13+
14+
15+
# --------------------------------------------------
16+
def get_args():
17+
"""Get command-line arguments"""
18+
19+
parser = argparse.ArgumentParser(
20+
description='Compile my book',
21+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
22+
23+
# parser.add_argument('positional',
24+
# metavar='str',
25+
# help='A positional argument')
26+
27+
parser.add_argument('-i',
28+
'--dir',
29+
help='Input dir',
30+
metavar='str',
31+
type=str,
32+
default=os.getcwd())
33+
34+
parser.add_argument('-o',
35+
'--outdir',
36+
help='Output dir',
37+
metavar='str',
38+
type=str,
39+
default=os.getcwd())
40+
41+
parser.add_argument('-c',
42+
'--contents',
43+
help='Table of contents',
44+
metavar='str',
45+
type=str,
46+
default=None)
47+
48+
# parser.add_argument('-i',
49+
# '--int',
50+
# help='A named integer argument',
51+
# metavar='int',
52+
# type=int,
53+
# default=0)
54+
55+
# parser.add_argument('-f',
56+
# '--flag',
57+
# help='A boolean flag',
58+
# action='store_true')
59+
60+
return parser.parse_args()
61+
62+
63+
# --------------------------------------------------
64+
def main():
65+
"""Make a jazz noise here"""
66+
67+
args = get_args()
68+
in_dir = args.dir
69+
out_dir = args.outdir
70+
contents = args.contents
71+
72+
if not contents:
73+
cur_dir = os.path.dirname(sys.argv[0])
74+
contents = os.path.join(cur_dir, 'contents.txt')
75+
76+
if not os.path.isfile(contents):
77+
die('Bad --contents "{}"'.format(contents))
78+
79+
book_file = os.path.join(out_dir, 'book.md')
80+
with open(book_file, 'wt') as fh:
81+
for i, dir_name in enumerate(map(str.rstrip, open(contents)), 1):
82+
print('{:3}: {}'.format(i, dir_name))
83+
readme = os.path.join(in_dir, dir_name, 'README.md')
84+
if os.path.isfile(readme):
85+
print('\tREADME')
86+
fh.write('# Chapter {}\n\n'.format(i))
87+
fh.write(open(readme).read())
88+
fh.write('\n\\pagebreak\n\n')
89+
90+
solution = os.path.join(in_dir, dir_name, 'solution.py')
91+
if os.path.isfile(solution):
92+
print('\tSOLUTION')
93+
fh.write('# {} Solution\n\n'.format(dir_name))
94+
fh.write('````\n')
95+
fh.write(open(solution).read())
96+
fh.write('````\n')
97+
fh.write('\n\\pagebreak\n\n')
98+
99+
cmd = 'pandoc {} --pdf-engine=xelatex -o book.pdf'
100+
rv, out = getstatusoutput(cmd.format(book_file))
101+
102+
if rv != 0:
103+
die('Error: {}'.format(out))
104+
105+
# --------------------------------------------------
106+
if __name__ == '__main__':
107+
main()

bin/contents.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
howler
2+
jump_the_five
3+
bottles_of_beer
4+
picnic
5+
apples_and_bananas
6+
gashlycrumb
7+
movie_reader
8+
palindromes
9+
ransom_note
10+
rhymer
11+
rock_paper_scissors
12+
abuse
13+
bacronym
14+
blackjack
15+
family_tree
16+
gematria
17+
guess
18+
kentucky_fryer
19+
mad_libs
20+
markov_words
21+
piggie
22+
sound
23+
substring
24+
tictactoe
25+
twelve_days_of_christmas
26+
war
27+
anagram
28+
hangman
29+
markov_chain
30+
morse
31+
rot13

blackjack/README.md

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,3 @@ D [19]: ♠10 ♦9
4343
P [21]: ♣10 ♥8 ♠3
4444
Player wins. You probably cheated.
4545
````
46-
47-
# Test Suite
48-
49-
A passing test suite looks like this:
50-
51-
````
52-
$ make test
53-
pytest -v test.py
54-
============================= test session starts ==============================
55-
platform darwin -- Python 3.6.8, pytest-4.2.0, py-1.7.0, pluggy-0.8.1 -- /anaconda3/bin/python
56-
cachedir: .pytest_cache
57-
rootdir: /Users/kyclark/work/python/practical_python_for_data_science/ch09-python-games/exercises/blackjack_b, inifile:
58-
plugins: remotedata-0.3.1, openfiles-0.3.2, doctestplus-0.2.0, arraydiff-0.3
59-
collected 13 items
60-
61-
test.py::test_usage PASSED [ 7%]
62-
test.py::test_play01 PASSED [ 15%]
63-
test.py::test_play02 PASSED [ 23%]
64-
test.py::test_play03 PASSED [ 30%]
65-
test.py::test_play04 PASSED [ 38%]
66-
test.py::test_play05 PASSED [ 46%]
67-
test.py::test_play06 PASSED [ 53%]
68-
test.py::test_play07 PASSED [ 61%]
69-
test.py::test_play08 PASSED [ 69%]
70-
test.py::test_play09 PASSED [ 76%]
71-
test.py::test_play10 PASSED [ 84%]
72-
test.py::test_play11 PASSED [ 92%]
73-
test.py::test_play12 PASSED [100%]
74-
75-
========================== 13 passed in 0.82 seconds ===========================
76-
````

blackjack/solution.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#!/usr/bin/env python3
2-
"""
3-
Author : Ken Youens-Clark <kyclark@gmail.com>
4-
Date : 2019-03-15
5-
Purpose: Rock the Casbah
6-
"""
72

83
import argparse
94
import random
105
import re
116
import sys
127
from itertools import product
8+
from dire import die
139

1410

1511
# --------------------------------------------------
@@ -42,18 +38,6 @@ def get_args():
4238
return parser.parse_args()
4339

4440

45-
# --------------------------------------------------
46-
def warn(msg):
47-
"""Print a message to STDERR"""
48-
print(msg, file=sys.stderr)
49-
50-
51-
# --------------------------------------------------
52-
def die(msg='Something bad happened'):
53-
"""warn() and exit with error"""
54-
warn(msg)
55-
sys.exit(1)
56-
5741
# --------------------------------------------------
5842
def bail(msg):
5943
"""print() and exit(0)"""

0 commit comments

Comments
 (0)