From 25546dad1f508f264b3224dd50dd42426d8b6aba Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Wed, 25 Sep 2024 18:20:58 +0200 Subject: Implemented test session --- src/flashcard_cli.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/src/flashcard_cli.py b/src/flashcard_cli.py index 9e9633d..95da182 100644 --- a/src/flashcard_cli.py +++ b/src/flashcard_cli.py @@ -1,32 +1,92 @@ import click -from json import load -from os.path import isfile +from json import load, dump +from random import shuffle from card import Card from scheduler import getSchedulerClass, SCHEDULERS from parser import parseFiles - @click.group() def cli(): pass @cli.command() -@click.option("--scheduler", "scheduler_name", default="brutal", type=click.Choice(SCHEDULERS, case_sensitive=False)) @click.argument("state_file", nargs=1, type=click.Path()) @click.argument("card_files", nargs=-1, type=click.Path(exists=True)) -def practice(scheduler_name, state_file, card_files): +@click.option("--scheduler", "scheduler_name", default="brutal", type=click.Choice(SCHEDULERS, case_sensitive=False), help="Name of desired scheduler") +@click.option("--count", default=20, type=int, help="Number of cards to show during the session") +@click.option("--random_flip", is_flag=True, help="Prompt with card front or back randomly instead of always front") +def practice(state_file, card_files, scheduler_name, count, random_flip): """ Run a practice session with the specified scheduler, using the provided state and card files. """ - cards = parseFiles(card_files) - state = json.load(state_file) if isfile(state_file) else {} + all_cards = parseFiles(card_files) + + try: + state = json.load(state_file) + except: + click.echo(f"Warning: could not load state from {state_file}, starting with clean state") + state = {} - scheduler = getSchedulerClass(scheduler_name)(cards, state) + scheduler = getSchedulerClass(scheduler_name)(all_cards, state) + cards = scheduler.practice(count) + + for i, id in enumerate(cards): + click.echo(click.style(f"{i + 1}/{len(cards)} ===========================================================", fg="blue")) + + faces = [all_cards[id].front, all_cards[id].back] + + if random_flip: + shuffle(faces) + + click.echo(click.style(faces.pop(0), fg="yellow")) + input() + click.echo(faces.pop(0)) + +@cli.command() +@click.argument("state_file", nargs=1, type=click.Path()) +@click.argument("card_files", nargs=-1, type=click.Path(exists=True)) +@click.option("--scheduler", "scheduler_name", default="brutal", type=click.Choice(SCHEDULERS, case_sensitive=False), help="Name of desired scheduler") +@click.option("--count", default=20, type=int, help="Number of cards to show during the session") +@click.option("--random_flip", is_flag=True, help="Prompt with card front or back randomly instead of always front") +def test(state_file, card_files, scheduler_name, count, random_flip): + """ + Run a test session with the specified scheduler, using the provided state and card files. + """ + all_cards = parseFiles(card_files) + + try: + with open(state_file, "r") as f: + state = load(f) + except: + click.echo(f"Warning: could not load state from {state_file}, starting with clean state") + state = {} + + scheduler = getSchedulerClass(scheduler_name)(all_cards, state) + + cards = scheduler.test(count) + results = {} + + for i, id in enumerate(cards): + click.echo(click.style(f"{i + 1}/{len(cards)} ===========================================================", fg="blue")) + + faces = [all_cards[id].front, all_cards[id].back] + + if random_flip: + shuffle(faces) + + click.echo(click.style(faces.pop(0), fg="yellow")) + input() + click.echo(faces.pop(0)) + click.echo() + results[id] = int(click.confirm(click.style("Correct?", bold=True))) + scheduler.update(results) + with open(state_file, "w") as f: + dump(scheduler.getState(), f) if __name__ == '__main__': cli() -- cgit v1.2.3