# HG changeset patch
# User Igor Chubin <igor@chub.in>
# Date 1304274456 -7200
# Node ID abd4080ee5839e2b51b6849221d0cd1da0fb8ff1
# Parent  74e05d4436eef0318f8df9c5579c48e08a841a1d
logging (fixme: hardcoded log filename) + answers shorthand (you press key instead of fullanswer)

diff -r 74e05d4436ee -r abd4080ee583 misc/zubrator.py
--- a/misc/zubrator.py	Sun May 01 20:25:55 2011 +0200
+++ b/misc/zubrator.py	Sun May 01 20:27:36 2011 +0200
@@ -2,6 +2,7 @@
 
 import random
 import sys
+import time
 
 # TODO:
 # * persistent weight dict
@@ -11,6 +12,53 @@
 # DONE:
 # * correct quit (ctrl d)
 
+logfile = "/home/igor/Langs/Deutsch/training-scripts/geschlecht/zubrator.log"
+
+class _Getch:
+    """Gets a single character from standard input.  Does not echo to the
+screen."""
+    def __init__(self):
+        try:
+            self.impl = _GetchWindows()
+        except ImportError:
+            self.impl = _GetchUnix()
+
+    def __call__(self): return self.impl()
+
+
+class _GetchUnix:
+    def __init__(self):
+        import tty, sys
+
+    def __call__(self):
+        import sys, tty, termios
+        fd = sys.stdin.fileno()
+        old_settings = termios.tcgetattr(fd)
+        try:
+            tty.setraw(sys.stdin.fileno())
+            ch = sys.stdin.read(1)
+        finally:
+            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
+        return ch
+
+
+class _GetchWindows:
+    def __init__(self):
+        import msvcrt
+
+    def __call__(self):
+        import msvcrt
+        return msvcrt.getch()
+
+
+getch = _Getch()
+
+def log_answer(result, question, correct_answer, given_answer):
+    with open(logfile, "a") as f:
+        timestamp = time.strftime("%Y-%m-%d %H:%M")
+        f.write(" ".join([timestamp, result, question, correct_answer, given_answer])+"\n")
+
+
 def color_for_answer(answer):
     color_table = {
         'der':  'Blue',
@@ -124,10 +172,28 @@
 while 1:
     question = wrandom(weight)
     colorprint(question, 'Yellow')
-    answer = sys.stdin.readline().rstrip('\n')
+    #answer = sys.stdin.readline().rstrip('\n')
+    ch = getch()
+    codes = {
+        'q':    'der',
+        'w':    'das',
+        'e':    'die',
+
+        'p':    'der',
+        '[':    'das',
+        ']':    'die'
+    }
+    if ch in codes:
+        answer = codes[ch]
+    else:
+        answer = ''
+
     if not answer:
         break
+
+    result = "OK"
     if answer != correct_answer[question]:
+        result = "FAIL"
         colorprint(
             "%s %s" % (correct_answer[question], question),
             color_for_answer(correct_answer[question])
@@ -138,6 +204,8 @@
     else:
         weight = set_weight(weight, question, weight[question]*0.8)
 
+    log_answer(result, question, correct_answer[question], answer)
+
     stats['total_questions'] += 1
     stats['last_questions'] += 1