def swap(args): """Swap-create a new flashcard deck. Create a new flashcard deck by swapping questions and answers. Args: args (argparse.Namespace): command line arguments. """ print('Swapping questions and answers from {} and saving to {}.'.format( args.deck, args.dest)) src = Deck.load(args.deck) dest = Deck(src.name) for c in src: dest.add_card(Flashcard(c.answer, c.question)) dest.save(args.dest)
def create(args): """Create a new flashcard deck. Args: args (argparse.Namespace): command line arguments. """ _logger.info('Creating deck {}.'.format(args.deck)) if os.path.exists(args.deck): raise ValueError('{} already exists.'.format(args.deck)) name = input('Deck Name: ') deck = Deck(name) print('Enter an empty question to finish.') for idx in itertools.count(1): q = input('Question #{}: '.format(idx)) if not q: break a = input('Answer #{}: '.format(idx)) deck.add_card(Flashcard(q, a)) deck.save(args.deck)
class DeckManipulationTestCase(unittest.TestCase): def setUp(self): self.deck_name = 'test deck' self.deck = Deck(self.deck_name) self.cards = [ Flashcard('q0', 'a0', 0, 0, str(datetime.datetime.utcnow())), Flashcard('q1', 'a1', 0, 0, str(datetime.datetime.utcnow())), Flashcard('q2', 'a2', 0, 0, str(datetime.datetime.utcnow())) ] for c in self.cards: self.deck.add_card(c) def test_iterate(self): """Iteration tests.""" self.assertEqual( frozenset(c for c in self.deck), frozenset(self.cards) ) def test_len(self): """__len__() tests.""" self.assertEqual(len(self.cards), len(self.deck)) def test_equality(self): """__eq__() tests.""" other_deck = Deck('other deck') for c in self.cards: other_deck.add_card(c) other_other_deck = Deck(self.deck_name) self.assertEqual(self.deck, self.deck) self.assertNotEqual(self.deck, other_deck) self.assertNotEqual(self.deck, other_other_deck) self.assertNotEqual(self.deck, 'not a deck') def test_load_and_save(self): """load() and save() interface tests.""" # Save and load the deck. with tempfile.NamedTemporaryFile(mode='w', newline='') as tf: self.deck.save(tf.name, overwrite=True) saved_deck = Deck.load(tf.name) # Test. self.assertEqual(saved_deck, self.deck) def test_sample_deck_file(self): """Load sample deck from README.md test.""" sample = """Name: Sample Deck Quiz: Question,Answer,Attempts,Correct,Last Shown 1 + 1, 2 1 + 2, 3 1 + 3, 4 1 + 4, 5 1 + 5, 6 1 + 6, 7 1 + 7, 8 1 + 8, 9 1 + 9, 10 """ with tempfile.NamedTemporaryFile(mode='w', newline='') as tf: with open(tf.name, 'w') as f: f.write(sample) saved_deck = Deck.load(tf.name) self.assertEqual(saved_deck.name, 'Sample Deck') self.assertEqual(len(saved_deck), 9) def test_answers(self): """answers() interface tests.""" expected_answers = frozenset([c.answer for c in self.cards]) self.assertEqual(frozenset(self.deck.answers()), expected_answers)