Permalink
Browse files

stat getter with tests

  • Loading branch information...
baverman committed Feb 5, 2011
1 parent b62bea7 commit 693d33ddda8bc12062b243d2627e4130cdb7c2ce
Showing with 72 additions and 3 deletions.
  1. +35 −0 tests/test_stat.py
  2. +37 −3 typetrainer/stat.py
View
@@ -0,0 +1,35 @@
+from typetrainer.stat import FileStatistic
+from datetime import datetime, timedelta
+
+def test_stat_must_collect_and_get(tmpdir):
+ stat = FileStatistic(str(tmpdir.join()))
+
+ dt = datetime.now()
+ stat.log('name', 150, 100, dt)
+
+ result = stat.get('name', 100)
+ assert result == {dt.date():(150, 1)}
+
+def test_stat_result_must_not_contain_records_with_low_accuracies(tmpdir):
+ stat = FileStatistic(str(tmpdir.join()))
+
+ dt = datetime.now()
+ stat.log('name', 150, 100, dt)
+ stat.log('name', 140, 99, dt)
+
+ result = stat.get('name', 100)
+ assert result == {dt.date():(150, 1)}
+
+def test_stat_must_return_records_averaged_by_day(tmpdir):
+ stat = FileStatistic(str(tmpdir.join()))
+
+ dt1 = datetime.now()
+ dt2 = dt1 + timedelta(days=1)
+
+ stat.log('name', 150, 100, dt1)
+ stat.log('name', 140, 100, dt1)
+ stat.log('name', 100, 100, dt2)
+ stat.log('name', 120, 100, dt2)
+
+ result = stat.get('name', 100)
+ assert result == {dt1.date():(145, 2), dt2.date():(110, 2)}
View
@@ -2,12 +2,15 @@
from datetime import datetime
from hashlib import md5
+DATE_FORMAT = '%Y-%m-%d %H:%M'
+
class FileStatistic(object):
+
def __init__(self, root):
self.root = root
def get_filename(self, name):
- return os.path.join(self.root, md5(name).hexdigest())
+ return os.path.join(self.root, md5(name + 'v1').hexdigest())
def log(self, name, cpm, accuracy, dt=None):
dt = dt or datetime.now()
@@ -19,5 +22,36 @@ def log(self, name, cpm, accuracy, dt=None):
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()
+ f.write("{0} {1} {2}\n".format(dt.strftime(DATE_FORMAT), cpm, accuracy))
+ f.close()
+
+ def get(self, name, accuracy):
+ result = {}
+
+ with open(self.get_filename(name)) as f:
+ skip_first = True
+ for l in f:
+ if skip_first:
+ skip_first = False
+ continue
+
+ try:
+ dt, cpm, acc = l.strip().rsplit(' ', 2)
+ except ValueError:
+ continue
+
+ acc = float(acc)
+ if acc < accuracy:
+ continue
+
+ cpm = float(cpm)
+ dt = datetime.strptime(dt, DATE_FORMAT).date()
+
+ try:
+ avg, cnt = result[dt]
+ except KeyError:
+ avg, cnt = 0.0, 0
+
+ result[dt] = ( avg * cnt + cpm ) / (cnt + 1.0), cnt + 1
+
+ return result

0 comments on commit 693d33d

Please sign in to comment.