From: Christian Heller Date: Sat, 16 Mar 2024 23:56:26 +0000 (+0100) Subject: Improve type hints sufficiently for mypy not to complain, and for Day.__eq__ check... X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/condition?a=commitdiff_plain;h=51cdb0020cf8f1c0a0c11cc80be98863fce2448b;p=plomtask Improve type hints sufficiently for mypy not to complain, and for Day.__eq__ check comparand's type. --- diff --git a/days.py b/days.py index 42fc4c1..8db9f15 100644 --- a/days.py +++ b/days.py @@ -8,7 +8,7 @@ DATE_FORMAT = '%Y-%m-%d' class Day: """Individual days defined by their dates.""" - def __init__(self, date:str): + def __init__(self, date: str): self.date = date self.datetime = datetime.strptime(date, DATE_FORMAT) @@ -17,8 +17,8 @@ class Day: """Return what weekday matches self.date.""" return self.datetime.strftime('%A') - def __eq__(self, other:object): + def __eq__(self, other: object): return isinstance(other, self.__class__) and self.date == other.date - def __lt__(self, other:Day): + def __lt__(self, other): return self.date < other.date diff --git a/task.py b/task.py index 4774c8c..9631434 100755 --- a/task.py +++ b/task.py @@ -15,16 +15,25 @@ class HandledException(Exception): """To identify Exceptions based on expected (if faulty) user behavior.""" +class TaskServer(HTTPServer): + """Variant of HTTPServer that knows .jinja as Jinja Environment.""" + + def __init__(self, *args, **kwargs): + super().__init__(self, *args, **kwargs) + self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR)) + + class TaskHandler(BaseHTTPRequestHandler): """Handles single HTTP request.""" + server: TaskServer - def send_html(self, html:str, code:int=200): + def send_html(self, html: str, code: int = 200): """Send HTML as proper HTTP response.""" self.send_response(code) self.end_headers() self.wfile.write(bytes(html, 'utf-8')) - def send_msg(self, msg:str, code:int=400): + def send_msg(self, msg: str, code: int = 400): """Send message in HTML formatting as HTTP response.""" html = self.server.jinja.get_template('msg.html').render(msg=msg) self.send_html(html, code) @@ -52,8 +61,7 @@ class TaskHandler(BaseHTTPRequestHandler): def main(): """Main loop.""" - server = HTTPServer(('localhost', HTTP_PORT), TaskHandler) - server.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR)) + server = TaskServer(('localhost', HTTP_PORT), TaskHandler) print(f'running at port {HTTP_PORT}') try: server.serve_forever()