From 10abd4ce96b4952a2b0d1c5221815f2f3aff8646 Mon Sep 17 00:00:00 2001 From: eneller Date: Mon, 14 Apr 2025 00:42:01 +0200 Subject: [PATCH] feat: prettier grades calculation --- src/uulm_utils/main.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/uulm_utils/main.py b/src/uulm_utils/main.py index 8883ee6..bb71d68 100644 --- a/src/uulm_utils/main.py +++ b/src/uulm_utils/main.py @@ -6,8 +6,11 @@ import pandas as pd from enum import Enum import asyncio from time import sleep +import logging +logger = logging.getLogger(__name__) + CORONANG_VERSION='v1.8.00' Selection = Enum('Selection', ['TREE_WALK', 'TREE_LEAF', 'ITEM_SELECTED']) @@ -32,8 +35,11 @@ async def run_playwright(headless: bool): @click.option('--username','-u') @click.option('--password','-p') @click.option('--headful', is_flag=True, help='Show the browser window') +@click.option('--debug', '-d', is_flag=True, help='Set the log level to DEBUG') @click.pass_context -async def cli(ctx, username, password, headful): +async def cli(ctx, username, password, headful, debug): + logging.basicConfig(level=logging.ERROR,format='%(asctime)s - %(levelname)s - %(message)s') + if(debug): logger.setLevel(logging.DEBUG) ctx.ensure_object(dict) if ctx.invoked_subcommand != 'grades': ctx.obj['USERNAME'] = username or await questionary.text('Enter your kiz username:').ask_async() @@ -76,27 +82,27 @@ async def coronang(ctx): await page.reload() click.echo("Finished coronang.") -@cli.command(help='Calculate your weighted grade from a CSV using the best n ECTS') +@cli.command(help='''Calculate your weighted grade using the best n credits. Expects a csv with the columns name, grade, credits''') @click.argument('filename', type=click.Path(exists=True)) -@click.option('--target_lp', '-t', type=int, default=74, help='Target number of n ECTS needed') +@click.option('--target_lp', '-t', type=int, default=74, help='Target number of n credits needed') def grades(filename, target_lp:int): data = pd.read_csv(filename) - data.sort_values(by='note', inplace=True) + data.sort_values(by='grade', inplace=True) acc_note: float = 0.0 acc_lp = 0 # the use of iterrows and all iteration over dataframes is discouraged for performance reasons for _ , row in data.iterrows(): - if acc_lp + row['lp'] < target_lp: - acc_lp += row['lp'] - acc_note: float = acc_note + row['lp'] * row['note'] + if acc_lp + row['credits'] < target_lp: + weight = row['credits'] else: weight = target_lp - acc_lp - acc_lp += weight - acc_note = acc_note + weight * row['note'] break + acc_lp += weight + acc_note = acc_note + weight * row['grade'] + logger.debug('Added "%s" with %d/%d credits and grade %.1f', row['name'], weight, row['credits'], row['grade']) acc_note: float = acc_note / acc_lp - print(acc_note) + print(f'Final Grade: {acc_note}') if __name__ == "__main__": asyncio.run(cli.main()) \ No newline at end of file