From adb8522e262fd313534d8b3c6577b71d95efe9a1 Mon Sep 17 00:00:00 2001 From: Miguel Barão Date: Mon, 16 Nov 2020 10:02:59 +0000 Subject: [PATCH] add some type annotations --- BUGS.md | 2 -- perguntations/main.py | 4 ++-- perguntations/test.py | 37 +++++++++++++++++++------------------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/BUGS.md b/BUGS.md index b8298a6..e81d677 100644 --- a/BUGS.md +++ b/BUGS.md @@ -1,8 +1,6 @@ # BUGS -- no login, dar mensagem de erro se aluno nao existir?? -- Verificar o processo de logout. - permitir remover alunos que estão online para poderem comecar de novo. - grade gives internal server error - reload do teste recomeça a contagem no inicio do tempo. diff --git a/perguntations/main.py b/perguntations/main.py index bbf369c..38ea58e 100644 --- a/perguntations/main.py +++ b/perguntations/main.py @@ -55,7 +55,7 @@ def parse_cmdline_arguments(): help='port for the HTTPS server (default: 8443)') parser.add_argument('--version', action='version', - version=APP_VERSION, + version=f'{APP_VERSION} - python {sys.version}', help='Show version information and exit') return parser.parse_args() @@ -100,7 +100,7 @@ def get_logger_config(debug=False): }, } default_config['loggers'].update({ - APP_NAME+'.'+module: { + f'{APP_NAME}.{module}': { 'handlers': ['default'], 'level': level, 'propagate': False, diff --git a/perguntations/test.py b/perguntations/test.py index 1222fd2..2445ee1 100644 --- a/perguntations/test.py +++ b/perguntations/test.py @@ -10,6 +10,7 @@ import random from datetime import datetime import logging import re +from typing import Any, Dict # this project from perguntations.questions import QFactory, QuestionException @@ -33,7 +34,7 @@ class TestFactory(dict): ''' # ------------------------------------------------------------------------ - def __init__(self, conf): + def __init__(self, conf: Dict[str, Any]) -> None: ''' Loads configuration from yaml file, then overrides some configurations using the conf argument. @@ -123,7 +124,7 @@ class TestFactory(dict): raise TestFactoryException(f'Could not find questions {qmissing}.') # ------------------------------------------------------------------------ - def check_test_ref(self): + def check_test_ref(self) -> None: '''Test must have a `ref`''' if 'ref' not in self: raise TestFactoryException('Missing "ref" in configuration!') @@ -131,7 +132,7 @@ class TestFactory(dict): raise TestFactoryException('Test "ref" can only contain the ' 'characters a-zA-Z0-9_-') - def check_missing_database(self): + def check_missing_database(self) -> None: '''Test must have a database''' if 'database' not in self: raise TestFactoryException('Missing "database" in configuration') @@ -139,13 +140,13 @@ class TestFactory(dict): msg = f'Database "{self["database"]}" not found!' raise TestFactoryException(msg) - def check_missing_answers_directory(self): + def check_missing_answers_directory(self) -> None: '''Test must have a answers directory''' if 'answers_dir' not in self: msg = 'Missing "answers_dir" in configuration' raise TestFactoryException(msg) - def check_answers_directory_writable(self): + def check_answers_directory_writable(self) -> None: '''Answers directory must be writable''' testfile = path.join(path.expanduser(self['answers_dir']), 'REMOVE-ME') try: @@ -155,7 +156,7 @@ class TestFactory(dict): msg = f'Cannot write answers to directory "{self["answers_dir"]}"' raise TestFactoryException(msg) from exc - def check_questions_directory(self): + def check_questions_directory(self) -> None: '''Check if questions directory is missing or not accessible.''' if 'questions_dir' not in self: logger.warning('Missing "questions_dir". Using "%s"', @@ -165,7 +166,7 @@ class TestFactory(dict): raise TestFactoryException(f'Can\'t find questions directory ' f'"{self["questions_dir"]}"') - def check_import_files(self): + def check_import_files(self) -> None: '''Check if there are files to import (with questions)''' if 'files' not in self: msg = ('Missing "files" in configuration with the list of ' @@ -175,7 +176,7 @@ class TestFactory(dict): if isinstance(self['files'], str): self['files'] = [self['files']] - def check_question_list(self): + def check_question_list(self) -> None: '''normalize question list''' if 'questions' not in self: raise TestFactoryException('Missing "questions" in configuration') @@ -191,12 +192,12 @@ class TestFactory(dict): self['questions'][i] = question - def check_missing_title(self): + def check_missing_title(self) -> None: '''Warns if title is missing''' if not self['title']: logger.warning('Title is undefined!') - def check_grade_scaling(self): + def check_grade_scaling(self) -> None: '''Just informs the scale limits''' if 'scale_points' in self: msg = ('*** DEPRECATION WARNING: *** scale_points, scale_min, ' @@ -206,7 +207,7 @@ class TestFactory(dict): # ------------------------------------------------------------------------ - def sanity_checks(self): + def sanity_checks(self) -> None: ''' Checks for valid keys and sets default values. Also checks if some files and directories exist @@ -304,7 +305,7 @@ class Test(dict): # super().__init__(d) # ------------------------------------------------------------------------ - def register(self, student): + def register(self, student: dict) -> None: ''' Write student id in the test and register start time ''' @@ -315,18 +316,18 @@ class Test(dict): self['comment'] = '' # ------------------------------------------------------------------------ - def reset_answers(self): + def reset_answers(self) -> None: '''Removes all answers from the test (clean)''' for question in self['questions']: question['answer'] = None # ------------------------------------------------------------------------ - def update_answer(self, ref, ans): + def update_answer(self, ref: str, ans) -> None: '''updates one answer in the test''' self['questions'][ref].set_answer(ans) # ------------------------------------------------------------------------ - def update_answers(self, answers_dict): + def update_answers(self, answers_dict) -> None: ''' Given a dictionary ans={'ref': 'some answer'} updates the answers of multiple questions in the test. @@ -337,7 +338,7 @@ class Test(dict): # self['questions'][ref]['answer'] = ans # ------------------------------------------------------------------------ - async def correct(self): + async def correct(self) -> float: '''Corrects all the answers of the test and computes the final grade''' self['finish_time'] = datetime.now() self['state'] = 'FINISHED' @@ -354,7 +355,7 @@ class Test(dict): return self['grade'] # ------------------------------------------------------------------------ - def giveup(self): + def giveup(self) -> float: '''Test is marqued as QUIT and is not corrected''' self['finish_time'] = datetime.now() self['state'] = 'QUIT' @@ -363,7 +364,7 @@ class Test(dict): return self['grade'] # ------------------------------------------------------------------------ - def __str__(self): + def __str__(self) -> str: return ('Test:\n' f' student: {self.get("student", "--")}\n' f' start_time: {self.get("start_time", "--")}\n' -- libgit2 0.21.2