Permalink
Browse files

statistic collector

  • Loading branch information...
1 parent 001b304 commit ce6991019d0c4f3f4e47c5771aac1c24000ff39d @baverman committed Feb 4, 2011
Showing with 45 additions and 6 deletions.
  1. +8 −1 typetrainer/run.py
  2. +23 −0 typetrainer/stat.py
  3. +10 −5 typetrainer/ui/main.py
  4. +4 −0 typetrainer/util.py
View
9 typetrainer/run.py
@@ -34,13 +34,20 @@ def run():
filler = tutors.help.get_filler()
import gtk
+ import os.path
from typetrainer.ui import idle
from typetrainer.ui.main import Main
from typetrainer.ui import kbd
+ from typetrainer.stat import FileStatistic
+ from typetrainer.util import make_missing_dirs, join_to_data_dir
+
+ fake_stat = join_to_data_dir('fake_stat')
+ make_missing_dirs(fake_stat)
+ stat = FileStatistic(os.path.dirname(fake_stat))
kbd_layout = getattr(kbd, config['KEYBOARD'] + '_keyboard')
- app = Main(config, filler, kbd.KeyboardDrawer(kbd_layout))
+ app = Main(config, filler, stat, kbd.KeyboardDrawer(kbd_layout))
app.window.show()
idle(app.fill)
View
23 typetrainer/stat.py
@@ -0,0 +1,23 @@
+import os.path
+from datetime import datetime
+from hashlib import md5
+
+class FileStatistic(object):
+ def __init__(self, root):
+ self.root = root
+
+ def get_filename(self, name):
+ return os.path.join(self.root, md5(name).hexdigest())
+
+ def log(self, name, cpm, accuracy, dt=None):
+ dt = dt or datetime.now()
+
+ fname = self.get_filename(name)
+ existed_before = os.path.exists(fname)
+ f = open(fname, 'a')
+
+ if not existed_before:
+ f.write(name + '\n')
+
+ f.write("{0} {1} {2}\n".format(dt.strftime('%Y-%d-%m %H:%M'), cpm, accuracy))
+ f.close()
View
15 typetrainer/ui/main.py
@@ -26,11 +26,12 @@
class Main(BuilderAware):
"""glade-file: main.glade"""
- def __init__(self, config, filler, kbd_drawer):
+ def __init__(self, config, filler, stat, kbd_drawer):
BuilderAware.__init__(self, join_to_file_dir(__file__, 'main.glade'))
self.config = config
self.filler = filler
+ self.stat = stat
self.kbd_drawer = kbd_drawer
self.typed_chars = deque([], CHARS_HISTORY_LENGTH)
self.errors = defaultdict(float)
@@ -118,9 +119,12 @@ def on_type_entry_activate(self, *args):
self.filler.reset_distribution()
self.retype_lb.set_text('')
- self.stat_lb.set_text(
- '%d / %d%%' % (int((len(cur_text) + 1) * 60.0 / delta),
- int((len(self.totype_text) - errors) * 100.0 / len(self.totype_text))))
+ cpm = int((len(cur_text) + 1) * 60.0 / delta)
+ accuracy = int((len(self.totype_text) - errors) * 100.0 / len(self.totype_text))
+ self.stat_lb.set_text('%d / %d%%' % (cpm, accuracy))
+
+ if self.filler.name:
+ self.stat.log(self.filler.name, cpm, accuracy)
self.fill()
@@ -156,6 +160,8 @@ def get_error(self, typed):
max_value = value
err_char, prevs = ch, r['prevs']
+ #print
+
if max_value > RETYPE_THRESHOLD:
pchar = ''
mx = max(prevs.values()) if prevs else 0
@@ -164,7 +170,6 @@ def get_error(self, typed):
if len(pchars) == 1:
pchar = pchars[0]
- #print '!!!', pchar, err_char, '\n\n'
return pchar + err_char
else:
return None
View
4 typetrainer/util.py
@@ -8,6 +8,10 @@ def join_to_settings_dir(*args):
config_dir = os.getenv('XDG_CONFIG_HOME', expanduser('~/.config'))
return join(config_dir, 'typetrainer', *args)
+def join_to_data_dir(*args):
+ config_dir = os.getenv('XDG_DATA_HOME', expanduser('~/.local/share'))
+ return join(config_dir, 'typetrainer', *args)
+
def make_missing_dirs(filename):
path = dirname(filename)
if not exists(path):

0 comments on commit ce69910

Please sign in to comment.