Permalink
Browse files

Added a rudimentary statistics tracking system.

  • Loading branch information...
1 parent 3959118 commit 918b4db3046db2fbca80ef6253f4c059e7769500 @christian-oudard committed Nov 1, 2011
Showing with 61 additions and 30 deletions.
  1. +23 −0 card_stats.py
  2. +16 −18 flashcard.py
  3. +22 −12 tests.py
View
@@ -0,0 +1,23 @@
+import pickle
+
+class CardStats:
+ def __init__(self, stats_dict=None):
+ if stats_dict is None:
+ stats_dict = {}
+ self.stats_dict = stats_dict
+
+ @staticmethod
+ def load(data):
+ return CardStats(pickle.loads(data))
+
+ def save(self):
+ return pickle.dumps(self.stats_dict)
+
+ def __eq__(self, other):
+ return self.stats_dict == other.stats_dict
+
+ def __repr__(self):
+ return repr(self.stats_dict)
+
+ def answer(self, card_sides, result):
+ pass #STUB
View
@@ -6,6 +6,8 @@
import sys
from collections import namedtuple
+from card_stats import CardStats
+
Card = namedtuple('Card', 'word pronunciation definition')
_card_regex = re.compile(
@@ -32,6 +34,7 @@ def load_cards(deck_string):
return cards
def main(args):
+ # Validate arguments.
if len(args) < 1:
print('Please specify a deck of flash cards.')
return
@@ -48,16 +51,21 @@ def main(args):
stats_filename = os.path.join(deck_folder, deck_name + '.stats')
if os.path.exists(stats_filename):
- with open(stats_filename) as f:
+ with open(stats_filename, 'rb') as f:
stats_data = f.read()
- old_stats = load_stats(stats_data)
+ stats = CardStats.load(stats_data)
else:
- old_stats = {}
+ stats = CardStats()
+
+ # Start the quiz.
+ stats = do_quiz(cards, stats)
- # Start a quiz.
- do_quiz(cards, old_stats)
+ # Save stats.
+ stats_data = stats.save()
+ with open(stats_filename, 'wb') as f:
+ f.write(stats_data)
-def do_quiz(cards, old_stats):
+def do_quiz(cards, stats):
print('Press Enter to flip card, Q to quit.')
random.shuffle(cards)
for card in cards:
@@ -66,14 +74,8 @@ def do_quiz(cards, old_stats):
result = ask(front, back)
if result == 'done':
break
- elif result is True:
- pass#STUB, correct answer
- elif result is False:
- pass#STUB, incorrect answer
-
- new_stats = {}
- #STUB
- return new_stats
+ stats.answer((front, back), result)
+ return stats
def ask(front, back):
print()
@@ -105,9 +107,5 @@ def ask(front, back):
if command == 'n':
return False
-def load_stats(stats_data):
- #STUB
- return {}
-
if __name__ == '__main__':
main(sys.argv[1:])
View
@@ -8,6 +8,7 @@
from nose.tools import assert_equal, with_setup
from flashcard import load_cards, main
+from card_stats import CardStats
# Testing utilities.
@@ -46,6 +47,11 @@
Were you correct? (Y/N)
''')
+test_stats = CardStats({
+ (('你好',), ('ni3hao3', 'hello')): [False, True, False, True, True],
+ (('ni3hao3', 'hello'), ('你好',)): [False, False, True],
+})
+
def fix_whitespace(s):
return '\n'.join(line.rstrip() for line in s.splitlines())
@@ -71,15 +77,14 @@ def setup_fake_cards():
with open('fake_cards', 'w') as f:
f.write(test_data)
-def teardown_fake_cards():
- os.remove('fake_cards')
-
def setup_fake_cards_stats():
- with open('fake_cards.stats', 'w') as f:
- f.write('\n')
+ with open('fake_cards.stats', 'wb') as f:
+ f.write(test_stats.save())
-def teardown_fake_cards_stats():
- os.remove('fake_cards.stats')
+def teardown():
+ for filename in ['fake_cards', 'fake_cards.stats']:
+ if os.path.exists(filename):
+ os.remove(filename)
# Test functions.
@@ -97,26 +102,31 @@ def test_load_cards_error():
with assert_output(''):
assert_equal(load_cards('bad line'), [])
+def test_save_load_stats():
+ data = test_stats.save()
+ stats = CardStats.load(data)
+ assert_equal(test_stats, stats)
+
def test_main_error():
with assert_output('Please specify a deck of flash cards.'):
main([])
-@with_setup(setup_fake_cards, teardown_fake_cards)
+@with_setup(setup_fake_cards, teardown)
def test_main():
random.seed(0);
with fake_input(test_input):
with assert_output(test_output):
main(['fake_cards'])
-@with_setup(setup_fake_cards, teardown_fake_cards)
-@with_setup(setup_fake_cards_stats, teardown_fake_cards_stats)
-def test_main_load_stats():
+@with_setup(setup_fake_cards, teardown)
+@with_setup(setup_fake_cards_stats, teardown)
+def test_main_load_empty_stats_file():
random.seed(0);
with fake_input(test_input):
with assert_output(test_output):
main(['fake_cards'])
-@with_setup(setup_fake_cards, teardown_fake_cards)
+@with_setup(setup_fake_cards, teardown)
def test_main_quit():
random.seed(0);
with fake_input('q\n'):

0 comments on commit 918b4db

Please sign in to comment.