From 7c6e90a7c66cd9782ab83d5ea43b680067293b11 Mon Sep 17 00:00:00 2001 From: 6058aa27-6ced-4d77-952a-bf55d59351c2 Date: Sun, 30 Mar 2025 18:39:40 -0500 Subject: [PATCH] Seperate components and add basic Flask UI --- .gitignore | 1 + month_num_convert.py | 39 +++++++++++++++++++++++++++++++++ MonthNumConvert.py => tests.py | 40 ++-------------------------------- wsgi.py | 23 +++++++++++++++++++ 4 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 .gitignore create mode 100644 month_num_convert.py rename MonthNumConvert.py => tests.py (61%) create mode 100644 wsgi.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/month_num_convert.py b/month_num_convert.py new file mode 100644 index 0000000..0f92ab3 --- /dev/null +++ b/month_num_convert.py @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: CC0-1.0 + + +class MonthNumConvert: + __months = ( + "january", + "february", + "march", + "april", + "may", + "june", + "july", + "august", + "september", + "october", + "november", + "december", + ) + + # https://example.com/1 -> January + def __num_to_name(self, num): + if (num > 12) or (num < 1): + return None + return self.__months[num - 1].title() + + def __name_to_num(self, name): + for i, month in enumerate(self.__months): + if name[0:3].lower() == month[0:3]: + return i + 1 + + # a is a string + def convert(self, a): + try: + return self.__num_to_name(int(a)) + except ValueError: + pass + + # So it's not a number + return self.__name_to_num(a) diff --git a/MonthNumConvert.py b/tests.py similarity index 61% rename from MonthNumConvert.py rename to tests.py index 4379c89..0ae8335 100644 --- a/MonthNumConvert.py +++ b/tests.py @@ -1,43 +1,7 @@ # SPDX-License-Identifier: CC0-1.0 + import unittest - - -class MonthNumConvert: - __months = ( - "january", - "february", - "march", - "april", - "may", - "june", - "july", - "august", - "september", - "october", - "november", - "december", - ) - - # https://example.com/1 -> January - def __num_to_name(self, num): - if (num > 12) or (num < 1): - return None - return self.__months[num - 1].title() - - def __name_to_num(self, name): - for i, month in enumerate(self.__months): - if name[0:3].lower() == month[0:3]: - return i + 1 - - # a is a string - def convert(self, a): - try: - return self.__num_to_name(int(a)) - except ValueError: - pass - - # So it's not a number - return self.__name_to_num(a) +from month_num_convert import MonthNumConvert class TestMonthNumConvert(unittest.TestCase): diff --git a/wsgi.py b/wsgi.py new file mode 100644 index 0000000..722f28e --- /dev/null +++ b/wsgi.py @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: CC0-1.0 + +from flask import abort, Flask +from month_num_convert import MonthNumConvert + +app = Flask(__name__) +mnc = MonthNumConvert() + + +@app.route("/") +def hello_world(): + return "

Hello, World!

" + + +@app.route("/") +def handle_input(something): + a = mnc.convert(something) + if a == None: + abort(404) + if type(a) == type(1): + a = str(a) + + return a