annotate py/gotcha.py @ 162:77e87f1b5811 core-javascript

$ hg merge core-javascript.closure-exercises # done, at least for now
author jf3243
date Wed, 09 Nov 2011 17:00:38 +0000
parents c9fe170162ee
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
1 import string
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
2
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
3 def split(s):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
4
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
5 for item in s.split('&&&'):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
6
20
c9fe170162ee gotcha.py: Replaced delimier '===' (common in js code) by '===\n'.
Jonathan Fine <jfine@pytex.org>
parents: 16
diff changeset
7 yield map(string.strip, item.split('===\n'))
16
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
8
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
9
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
10 QUESTION_TEMPLATE = '''
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
11 .. _%(label)s-gotcha:
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
12
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
13 **%(name)s**
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
14
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
15 .. code-block:: javascript
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
16
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
17 %(js)s
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
18
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
19 :ref:`Answer <%(label)s-gotcha-answer>`.
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
20 '''
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
21
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
22
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
23 ANSWER_TEMPLATE = '''
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
24 .. _%(label)s-gotcha-answer:
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
25
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
26 **%(name)s**
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
27
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
28 .. code-block:: javascript
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
29
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
30 %(js)s
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
31
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
32 %(answer)s
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
33
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
34 :ref:`Question <%(label)s-gotcha>`.
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
35 '''
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
36
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
37 class Gotcha(object):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
38
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
39 def __init__(self, name, js, answer):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
40
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
41 self.name = name
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
42 self.js = '\n'.join([' ' + line for line in js.split('\n')])
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
43 self.answer = answer
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
44 self.answer = '\n'.join([' ' + line for line in answer.split('\n')])
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
45 self.label = name.lower().replace(' ', '-')
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
46
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
47 def as_question(self):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
48 return QUESTION_TEMPLATE % (self.__dict__)
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
49
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
50 def as_answer(self):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
51 return ANSWER_TEMPLATE % (self.__dict__)
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
52
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
53 def __str__(self):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
54 return '<Gotcha %r>' % self.name
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
55
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
56 __repr__ = __str__
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
57
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
58
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
59 if __name__ == '__main__':
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
60
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
61 import os
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
62
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
63 s = open('txt/gotcha.txt').read()
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
64
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
65 # Prefix a sort if you wish.
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
66 gotchas = [Gotcha(*x) for x in split(s)]
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
67
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
68 for key in ('question', 'answer'):
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
69
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
70 f = open(os.path.join('generated', 'gotcha-%ss.txt' % key), 'wb')
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
71 for gotcha in gotchas:
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
72 f.write(getattr(gotcha, 'as_' + key)())
9410e9350bcd gotcha.py: Added generator for gotcha questions and answers.
Jonathan Fine <jfine@pytex.org>
parents:
diff changeset
73 f.close()