From b79b54fb2d95894667a59363ab7ea3546daa5ba5 Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:35:08 +0100 Subject: [PATCH 1/9] add pyright type checking --- .github/workflows/pyright.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/pyright.yml diff --git a/.github/workflows/pyright.yml b/.github/workflows/pyright.yml new file mode 100644 index 0000000..2899fc0 --- /dev/null +++ b/.github/workflows/pyright.yml @@ -0,0 +1,25 @@ +name: Type-checking with Pyright + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + pyright: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install pyright + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Type-check with Pyright + run: | + pyright --warnings From 1972554b04ce86aea7d533ca7673e5890b116cb3 Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:36:32 +0100 Subject: [PATCH 2/9] add ruff linting and formatting --- .github/workflows/ruff.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/ruff.yml diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml new file mode 100644 index 0000000..0e616dc --- /dev/null +++ b/.github/workflows/ruff.yml @@ -0,0 +1,28 @@ +name: Ruff Linting and Formatting + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install ruff + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with Ruff + run: | + ruff check + - name: Check format with Ruff + run: | + ruff format --check From 1c3b072ba56309fb51e173ebeb67d7c8c8800daf Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:38:49 +0100 Subject: [PATCH 3/9] ruff fmt --- docs_src/conf.py | 27 ++++++++-------- gui/streamlit_app.py | 73 ++++++++++++++++++++++++-------------------- main.py | 36 +++++++++++++--------- tests/tests.py | 5 +-- 4 files changed, 79 insertions(+), 62 deletions(-) diff --git a/docs_src/conf.py b/docs_src/conf.py index 910477b..bb2f934 100644 --- a/docs_src/conf.py +++ b/docs_src/conf.py @@ -5,28 +5,29 @@ import os import sys -sys.path.insert(0, os.path.abspath('..')) + +sys.path.insert(0, os.path.abspath("..")) # -- Project information ----------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information -project = 'Example' -copyright = '2024, Micha Johannes Birklbauer' -author = 'Micha Johannes Birklbauer' -release = '1.0.0' +project = "Example" +copyright = "2024, Micha Johannes Birklbauer" +author = "Micha Johannes Birklbauer" +release = "1.0.0" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.autosummary', - 'sphinx.ext.napoleon', - 'sphinx.ext.viewcode' + "sphinx.ext.autodoc", + "sphinx.ext.autosummary", + "sphinx.ext.napoleon", + "sphinx.ext.viewcode", ] -templates_path = ['_templates'] -exclude_patterns = ['build'] +templates_path = ["_templates"] +exclude_patterns = ["build"] root_doc = "index" autosummary_generate = True autodoc_default_options = {"members": True, "inherited-members": True} @@ -34,8 +35,8 @@ # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output -html_theme = 'pydata_sphinx_theme' -html_static_path = ['_static'] +html_theme = "pydata_sphinx_theme" +html_static_path = ["_static"] html_theme_options = { "icon_links": [ { diff --git a/gui/streamlit_app.py b/gui/streamlit_app.py index 502380d..72338ef 100644 --- a/gui/streamlit_app.py +++ b/gui/streamlit_app.py @@ -16,62 +16,69 @@ import streamlit as st from main import my_product + # main page content def main_page(): - title = st.title("TITLE") - general_description = \ - """ + general_description = """ DESCRIPTION """ description = st.markdown(general_description) - header_1 = st.subheader("Product", divider = "rainbow") - - f1 = st.number_input("Number 1:", - min_value = 1, - max_value = 10000, - value = 1, - step = 1, - help = "First factor of multiplication.") - - f2 = st.number_input("Number 2:", - min_value = 1, - max_value = 10000, - value = 2, - step = 1, - help = "Second factor of multiplication.") + header_1 = st.subheader("Product", divider="rainbow") + + f1 = st.number_input( + "Number 1:", + min_value=1, + max_value=10000, + value=1, + step=1, + help="First factor of multiplication.", + ) + + f2 = st.number_input( + "Number 2:", + min_value=1, + max_value=10000, + value=2, + step=1, + help="Second factor of multiplication.", + ) l1, l2, center_button, r1, r2 = st.columns(5) with center_button: - compute = st.button("Compute!", type = "primary") + compute = st.button("Compute!", type="primary") if compute: st.success(f"The product of {f1} and {f2} is {my_product(f1, f2)}") + # side bar and main page loader def main(): - - about_str = \ - """ + about_str = """ ABOUT """ - st.set_page_config(page_title = "TITLE", - page_icon = ":test_tube:", - layout = "wide", - initial_sidebar_state = "expanded", - menu_items = {"Get Help": "https://github.com/YOUR_REPO/discussions", - "Report a bug": "https://github.com/YOUR_REPO/issues", - "About": about_str} - ) + st.set_page_config( + page_title="TITLE", + page_icon=":test_tube:", + layout="wide", + initial_sidebar_state="expanded", + menu_items={ + "Get Help": "https://github.com/YOUR_REPO/discussions", + "Report a bug": "https://github.com/YOUR_REPO/issues", + "About": about_str, + }, + ) title = st.sidebar.title("TITLE") - logo = st.sidebar.image("https://user-images.githubusercontent.com/7164864/217935870-c0bc60a3-6fc0-4047-b011-7b4c59488c91.png", - caption = "CAPTION") + logo = st.sidebar.image( + "https://user-images.githubusercontent.com/7164864/217935870-c0bc60a3-6fc0-4047-b011-7b4c59488c91.png", + caption="CAPTION", + ) doc = st.sidebar.markdown(about_str) @@ -89,6 +96,6 @@ def main(): main_page() -if __name__ == "__main__": +if __name__ == "__main__": main() diff --git a/main.py b/main.py index 1a56c9a..998e98d 100644 --- a/main.py +++ b/main.py @@ -19,8 +19,7 @@ ######################### -docs = \ -""" +docs = """ DESCRIPTION: A description of the script [multiplies two integers]. USAGE: @@ -50,6 +49,7 @@ # these examples use the numpy docstring style # https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard + def my_product(x: int, y: int) -> int: """Returns the product of two integer numbers. @@ -75,9 +75,11 @@ def my_product(x: int, y: int) -> int: return x * y + ##### MAIN FUNCTION ##### -def main(argv = None) -> int: + +def main(argv=None) -> int: """Main function. Parameters @@ -102,16 +104,22 @@ def main(argv = None) -> int: """ parser = argparse.ArgumentParser() - parser.add_argument("-f1", "--factor1", - dest = "f1", - required = True, - help = "First factor of multiplication.", - type = int) - parser.add_argument("-f2", "--factor2", - dest = "f2", - default = 2, - help = "Second factor of multiplication.", - type = int) + parser.add_argument( + "-f1", + "--factor1", + dest="f1", + required=True, + help="First factor of multiplication.", + type=int, + ) + parser.add_argument( + "-f2", + "--factor2", + dest="f2", + default=2, + help="Second factor of multiplication.", + type=int, + ) args = parser.parse_args(argv) p = my_product(args.f1, args.f2) @@ -119,8 +127,8 @@ def main(argv = None) -> int: return p + ######## SCRIPT ######### if __name__ == "__main__": - m = main() diff --git a/tests/tests.py b/tests/tests.py index 6b6254d..cfff804 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -5,9 +5,10 @@ # https://github.com/michabirklbauer/ # micha.birklbauer@gmail.com -def test1(): +def test1(): import pandas as pd + df = pd.read_csv("example_data.csv") from main import my_product @@ -15,8 +16,8 @@ def test1(): for i, row in df.iterrows(): assert my_product(row["x"], row["y"]) == row["x"] * row["y"] -def test2(): +def test2(): from main import main assert main(["-f1", "20"]) == 40 From 1452c7fadcf4531d535e5e1cb12d6b23ea26f03d Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:48:01 +0100 Subject: [PATCH 4/9] disable ruff checks for F841 --- gui/streamlit_app.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gui/streamlit_app.py b/gui/streamlit_app.py index 72338ef..77786d7 100644 --- a/gui/streamlit_app.py +++ b/gui/streamlit_app.py @@ -5,6 +5,10 @@ # https://github.com/michabirklbauer/ # micha.birklbauer@gmail.com +## disable unused variable checks for streamlit variables + +# ruff: noqa: F841 + """ ##################################################### ## ## From dce0f4e1cbff2d2c02f1b8db183a021ae02a8fe7 Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:48:10 +0100 Subject: [PATCH 5/9] fix structure --- main.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 998e98d..aa0220d 100644 --- a/main.py +++ b/main.py @@ -5,13 +5,23 @@ # https://github.com/michabirklbauer/ # micha.birklbauer@gmail.com + +##### REQUIREMENTS ###### + +# pip install pandas + +######################### + +# import packages +import argparse +# import pandas as pd + +######## VERSION ######## + # version tracking __version = "1.0.0" __date = "2024-03-11" -# REQUIREMENTS -# pip install pandas - ###### PARAMETERS ####### param_1 = 1 @@ -38,12 +48,6 @@ Show program's version number and exit. """ -######################### - -# import packages -import argparse -import pandas as pd - ####### FUNCTIONS ####### # these examples use the numpy docstring style From 9c34cf92861ac01c061e945201799aadf2f80e67 Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:56:33 +0100 Subject: [PATCH 6/9] add ruff config --- ruff.toml | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ruff.toml diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..44b862f --- /dev/null +++ b/ruff.toml @@ -0,0 +1,61 @@ +# Exclude a variety of commonly ignored directories. +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] + +# Same as Black. +line-length = 88 +indent-width = 4 + +# Assume Python 3.7 +target-version = "py37" + +[lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +select = ["E4", "E7", "E9", "F"] +ignore = [] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" From dec2df20e9fb192b7451e1ff0ee0cab4c5382b2d Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:56:56 +0100 Subject: [PATCH 7/9] add pyright config --- pyrightconfig.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 pyrightconfig.json diff --git a/pyrightconfig.json b/pyrightconfig.json new file mode 100644 index 0000000..e15cfc7 --- /dev/null +++ b/pyrightconfig.json @@ -0,0 +1,12 @@ +{ + "include": [ + "." + ], + + "exclude": [ + "**/__pycache__" + ], + + "pythonVersion": "3.7", + "pythonPlatform": "Linux" +} From 1fe03e35427f590598ed87b1aa873f2969752f60 Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Tue, 19 Nov 2024 18:57:13 +0100 Subject: [PATCH 8/9] add type casts --- gui/streamlit_app.py | 2 +- tests/tests.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gui/streamlit_app.py b/gui/streamlit_app.py index 77786d7..30e4e20 100644 --- a/gui/streamlit_app.py +++ b/gui/streamlit_app.py @@ -56,7 +56,7 @@ def main_page(): compute = st.button("Compute!", type="primary") if compute: - st.success(f"The product of {f1} and {f2} is {my_product(f1, f2)}") + st.success(f"The product of {f1} and {f2} is {my_product(int(f1), int(f2))}") # side bar and main page loader diff --git a/tests/tests.py b/tests/tests.py index cfff804..5617c8e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -14,7 +14,7 @@ def test1(): from main import my_product for i, row in df.iterrows(): - assert my_product(row["x"], row["y"]) == row["x"] * row["y"] + assert my_product(int(row["x"]), int(row["y"])) == int(row["x"]) * int(row["y"]) def test2(): From 02da1b48787cb73ee580aab48a3c9fbafce2514b Mon Sep 17 00:00:00 2001 From: Micha Birklbauer Date: Wed, 20 Nov 2024 12:08:03 +0100 Subject: [PATCH 9/9] add streamlit to req --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 1411a4a..339397a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -pandas \ No newline at end of file +pandas +streamlit \ No newline at end of file