Skip to content

Commit 67b7a50

Browse files
committed
Turn pseudo code to actual code
1 parent c979d82 commit 67b7a50

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

parse_readme.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,64 @@
11
import pprint
22

33
fname = "README.md"
4-
snipepts = []
4+
snippets = []
55

66
with open(fname, 'r') as f:
7-
lines = f.readlines()
8-
iterator
9-
while iterator:
10-
# check if it's a H3
11-
if line.startswith("###"):
12-
title = line.replace("### ", "")
13-
description = ''
14-
next_line = itertor.next
15-
while not next_line.startswith("#### "):
16-
# store lines till an H4 (explanation) is encountered
17-
description.append(next_line)
18-
next_line = iterator.next
19-
# store lines again until --- or another H3 is encountered
20-
snippets.append({
21-
"title":,
22-
"description":,
23-
"explanation":
24-
})
25-
# repeat until EOL is encoutered
7+
lines = iter(f.readlines())
8+
line = lines.next()
9+
10+
try:
11+
while True:
12+
# check if it's a H3
13+
if line.startswith("### "):
14+
title = line.replace("### ", "")
15+
# print(title, "found")
16+
description = []
17+
next_line = lines.next()
18+
19+
# store lines till an H4 (explanation) is encountered
20+
while not next_line.startswith("#### "):
21+
description.append(next_line)
22+
next_line = lines.next()
23+
24+
# print("Description captured", description[:10])
25+
26+
explanation = []
27+
# store lines again until --- or another H3 is encountered
28+
while not (next_line.startswith("---") or
29+
next_line.startswith("### ")):
30+
explanation.append(next_line)
31+
next_line = lines.next()
32+
33+
# print("explanation captured", explanation[:10])
34+
35+
36+
# Store the results finally
37+
snippets.append({
38+
"title": title,
39+
"description": '\n'.join(description),
40+
"explanation": '\n'.join(explanation)
41+
})
42+
43+
line = next_line
44+
45+
else:
46+
line = lines.next()
47+
48+
except StopIteration:
49+
snippets.append({
50+
"title": title,
51+
"description": '\n'.join(description),
52+
"explanation": '\n'.join(explanation)
53+
})
2654

2755
# separating by category
2856
categories = ["a", "b", "c"]
2957

3058
snips_by_cat = {k:[] for k in categories}
3159

3260
for snip in snippets:
33-
cat = raw_input(snip["title"])
34-
snips_by_cat[cat].append(snip)
61+
cat = raw_input(snip["title"])
62+
snips_by_cat[cat].append(snip)
3563

36-
pprint.pprint(snips_by_cat)
64+
pprint.pprint("hail", snippets)

0 commit comments

Comments
 (0)