new-words

diff misc/zubrator.py @ 56:6d6655d4cab6

new-words.pl stub removed
author Igor Chubin <igor@chub.in>
date Thu Nov 03 16:01:53 2011 +0100 (2011-11-03)
parents bf0aa8e3c1ce
children
line diff
     1.1 --- a/misc/zubrator.py	Fri Feb 04 06:39:25 2011 +0100
     1.2 +++ b/misc/zubrator.py	Thu Nov 03 16:01:53 2011 +0100
     1.3 @@ -2,6 +2,7 @@
     1.4  
     1.5  import random
     1.6  import sys
     1.7 +import time
     1.8  
     1.9  # TODO:
    1.10  # * persistent weight dict
    1.11 @@ -11,6 +12,53 @@
    1.12  # DONE:
    1.13  # * correct quit (ctrl d)
    1.14  
    1.15 +logfile = "/home/igor/Langs/Deutsch/training-scripts/geschlecht/zubrator.log"
    1.16 +
    1.17 +class _Getch:
    1.18 +    """Gets a single character from standard input.  Does not echo to the
    1.19 +screen."""
    1.20 +    def __init__(self):
    1.21 +        try:
    1.22 +            self.impl = _GetchWindows()
    1.23 +        except ImportError:
    1.24 +            self.impl = _GetchUnix()
    1.25 +
    1.26 +    def __call__(self): return self.impl()
    1.27 +
    1.28 +
    1.29 +class _GetchUnix:
    1.30 +    def __init__(self):
    1.31 +        import tty, sys
    1.32 +
    1.33 +    def __call__(self):
    1.34 +        import sys, tty, termios
    1.35 +        fd = sys.stdin.fileno()
    1.36 +        old_settings = termios.tcgetattr(fd)
    1.37 +        try:
    1.38 +            tty.setraw(sys.stdin.fileno())
    1.39 +            ch = sys.stdin.read(1)
    1.40 +        finally:
    1.41 +            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    1.42 +        return ch
    1.43 +
    1.44 +
    1.45 +class _GetchWindows:
    1.46 +    def __init__(self):
    1.47 +        import msvcrt
    1.48 +
    1.49 +    def __call__(self):
    1.50 +        import msvcrt
    1.51 +        return msvcrt.getch()
    1.52 +
    1.53 +
    1.54 +getch = _Getch()
    1.55 +
    1.56 +def log_answer(result, question, correct_answer, given_answer):
    1.57 +    with open(logfile, "a") as f:
    1.58 +        timestamp = time.strftime("%Y-%m-%d %H:%M")
    1.59 +        f.write(" ".join([timestamp, result, question, correct_answer, given_answer])+"\n")
    1.60 +
    1.61 +
    1.62  def color_for_answer(answer):
    1.63      color_table = {
    1.64          'der':  'Blue',
    1.65 @@ -124,10 +172,28 @@
    1.66  while 1:
    1.67      question = wrandom(weight)
    1.68      colorprint(question, 'Yellow')
    1.69 -    answer = sys.stdin.readline().rstrip('\n')
    1.70 +    #answer = sys.stdin.readline().rstrip('\n')
    1.71 +    ch = getch()
    1.72 +    codes = {
    1.73 +        'q':    'der',
    1.74 +        'w':    'das',
    1.75 +        'e':    'die',
    1.76 +
    1.77 +        'p':    'der',
    1.78 +        '[':    'das',
    1.79 +        ']':    'die'
    1.80 +    }
    1.81 +    if ch in codes:
    1.82 +        answer = codes[ch]
    1.83 +    else:
    1.84 +        answer = ''
    1.85 +
    1.86      if not answer:
    1.87          break
    1.88 +
    1.89 +    result = "OK"
    1.90      if answer != correct_answer[question]:
    1.91 +        result = "FAIL"
    1.92          colorprint(
    1.93              "%s %s" % (correct_answer[question], question),
    1.94              color_for_answer(correct_answer[question])
    1.95 @@ -138,6 +204,8 @@
    1.96      else:
    1.97          weight = set_weight(weight, question, weight[question]*0.8)
    1.98  
    1.99 +    log_answer(result, question, correct_answer[question], answer)
   1.100 +
   1.101      stats['total_questions'] += 1
   1.102      stats['last_questions'] += 1
   1.103