X Tutup
Skip to content

Commit b87a615

Browse files
committed
piggy discussion
1 parent 36b8872 commit b87a615

File tree

25 files changed

+773
-359
lines changed

25 files changed

+773
-359
lines changed

bin/chapters.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ kentucky_friar
2525
mad_libs
2626
license_plates
2727
gibberish
28-
piggie
28+
piggy
2929
soundex_rhymer
3030
substring
3131
tictactoe

book.md

Lines changed: 359 additions & 89 deletions
Large diffs are not rendered by default.

family_tree/Digraph.gv.pdf

10.2 KB
Binary file not shown.

family_tree/solution.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python3
2+
"""Display a family tree"""
3+
4+
import argparse
5+
import os
6+
import re
7+
from dire import die
8+
from graphviz import Digraph
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Display a family tree',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('file',
20+
metavar='FILE',
21+
type=argparse.FileType('r'),
22+
help='File input')
23+
24+
parser.add_argument('-o',
25+
'--outfile',
26+
help='Output filename',
27+
metavar='str',
28+
type=str,
29+
default='')
30+
31+
parser.add_argument('-v', '--view', help='View image', action='store_true')
32+
33+
return parser.parse_args()
34+
35+
36+
# --------------------------------------------------
37+
def main():
38+
"""Make a jazz noise here"""
39+
40+
args = get_args()
41+
fh = args.file
42+
out_file = args.outfile or os.path.basename(fh.name) + '.gv'
43+
44+
nodes, edges = parse_tree(fh)
45+
46+
if not nodes and not edges:
47+
die('No nodes or edges in "{}".'.format(fh.name))
48+
49+
dot = Digraph(comment='Tree')
50+
51+
# keys are initials which we don't need
52+
for _, name in nodes.items():
53+
dot.node(name)
54+
55+
for n1, n2 in edges:
56+
# see if node has alias in nodes, else use node itself
57+
n1 = nodes.get(n1, n1)
58+
n2 = nodes.get(n2, n2)
59+
dot.edge(n1, n2)
60+
61+
dot.render(out_file, view=args.view)
62+
63+
print('Done, see output in "{}".'.format(out_file))
64+
65+
66+
# --------------------------------------------------
67+
def parse_tree(fh):
68+
"""parse input file"""
69+
70+
name_patt = r'(.+)\s*=\s*(.+)'
71+
married_patt = r'(.+)\s+married\s+(.+)'
72+
begat_patt = r'(.+)\s+and\s+(.+)\s+begat\s+(.+)'
73+
74+
edges = set()
75+
nodes = {}
76+
77+
for line in fh:
78+
name_match = re.match(name_patt, line)
79+
begat_match = re.match(begat_patt, line)
80+
married_match = re.match(married_patt, line)
81+
82+
if name_match:
83+
initials, name = name_match.groups()
84+
nodes[initials.strip()] = name.strip()
85+
elif married_match:
86+
p1, p2 = married_match.groups()
87+
edges.add((p1.strip(), p2.strip()))
88+
elif begat_match:
89+
p1, p2, begat = begat_match.groups()
90+
children = re.split(r'\s*,\s*', begat)
91+
for parent in p1, p2:
92+
for child in children:
93+
edges.add((parent.strip(), child.strip()))
94+
95+
return nodes, edges
96+
97+
98+
# --------------------------------------------------
99+
if __name__ == '__main__':
100+
main()

inputs/nobody.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
I'm Nobody! Who are you?
22
Are you -- Nobody -- too?
33
Then there’s a pair of us!
4-
Dont tell! theyd advertise -- you know!
4+
Don't tell! they'd advertise -- you know!
55

66
How dreary -- to be -- Somebody!
77
How public -- like a Frog --

inputs/spiders.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Don’t worry, spiders,
2+
I keep house
3+
casually.

piggie/Makefile

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

piggie/README.md

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

piggie/mk-outs.sh

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

piggie/out-yay/61ndn

Whitespace-only changes.

0 commit comments

Comments
 (0)
X Tutup