changeset 155:c14a95ed1f58 core-javascript.closure-exercises

Add script to strip answers from exercises file.
author jf3243
date Wed, 09 Nov 2011 15:30:33 +0000
parents ee0bf994ab95
children b774913eb242
files core-javascript/py/doit.py
diffstat 1 files changed, 63 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core-javascript/py/doit.py	Wed Nov 09 15:30:33 2011 +0000
@@ -0,0 +1,63 @@
+'''Remove answers from exercises file.'''
+
+import sys
+
+NORMAL, SKIPPING, ERROR = range(3)
+LINE, SOMETHING, START, STOP = range(4)
+
+lookup = {}
+
+# All other states are an error.
+lookup[NORMAL, LINE] = NORMAL
+lookup[NORMAL, SOMETHING] = NORMAL
+lookup[NORMAL, START] = SKIPPING
+lookup[SKIPPING, LINE] = SKIPPING
+# lookup[NORMAL, STOP] = ERROR
+# lookup[SKIPPING, SOMETHING] = ERROR
+lookup[SKIPPING, STOP] = NORMAL
+
+
+filename = 'core-javascript-work/closure-exercises.js'
+
+write = sys.stdout.write
+with open(filename) as lines:
+
+    new_state = NORMAL
+
+    for lineno, line in enumerate(lines):
+
+        state = new_state
+
+
+        stripped_line = line.strip()
+
+        if stripped_line.startswith('something'):
+            event = SOMETHING
+
+        elif stripped_line == '// start':
+            event = START
+
+        elif stripped_line == '// stop':
+            event = STOP
+
+        else:
+            event = LINE
+
+        new_state = lookup[state, event]
+
+        if event == SOMETHING:
+            pass
+
+        elif event == STOP:
+            pass
+
+        elif event == START:
+            indent = len(line) - len(line.lstrip())
+            spaces = line[0:indent]
+            write(spaces + '// Please write your answer here.\r\n')
+
+        elif state == SKIPPING:
+            pass
+
+        elif event == LINE:
+            write(line.rstrip() + '\r\n')