Permalink
Browse files

Merge pull request #1 from MeirKriheli/master

Parse the quotes with generator (yield) func
  • Loading branch information...
ik5 committed Feb 5, 2013
2 parents 3bfb2e5 + b2355f8 commit 544de16a4954113b4fe73ccae525bd0c4770d0ae
Showing with 31 additions and 0 deletions.
  1. +31 −0 parse_quotes.py
View
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+
+QUOTES_FILE = 'quotes.txt'
+SEPARATOR = '----\n'
+AUTHOR_MARK = ' '
+
+
+def iter_quotes(quotes_file=QUOTES_FILE):
+ "Walks the quotes file, yields (quote, author) tuples for each quote"
+
+ with open(quotes_file) as f:
+ quote = []
+
+ for line in f:
+ if line != SEPARATOR:
+ quote.append(line)
+ else:
+ if quote[-1].startswith(AUTHOR_MARK):
+ author = quote.pop().strip()
+ else:
+ author = None
+
+ yield ''.join(quote).rstrip(), author
+ quote = []
+
+
+if __name__ == '__main__':
+
+ for quote, author in iter_quotes():
+ print quote, author

0 comments on commit 544de16

Please sign in to comment.