diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..7da1f96 --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 100 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..b21b4a5 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,64 @@ +name: Docs + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + name: Docs + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Autogenerate new documentation + continue-on-error: true + run: | + pip install -e . + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + pip install pyyaml + python automate_mkdocs.py + git add . + - name: Update and Build GH Pages + run: | + python -m pip install --upgrade pip + pip install mkdocs==1.2.3 + pip install mkgendocs==0.9.0 + pip install markupsafe==2.0.1 + pip install jinja2==2.11 + gendocs --config mkgendocs.yml + - name: deploy + run: | + mkdocs gh-deploy --force --clean --verbose + + - name: Commit any changes to docs + continue-on-error: true + run: | + git config --local user.name "github-actions[bot]" + git add ./docs + git commit -m "Auto-updating the docs" + + + - name: Push the changes to master + continue-on-error: true + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: main + force: true + + + diff --git a/.github/workflows/lint_action.yml b/.github/workflows/lint_action.yml new file mode 100644 index 0000000..5169118 --- /dev/null +++ b/.github/workflows/lint_action.yml @@ -0,0 +1,37 @@ +name: Lint + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + run-linters: + name: Run linters + runs-on: ubuntu-latest + + steps: + - name: Check out Git repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v1 + with: + python-version: 3.9 + + - name: Install Python dependencies + run: pip install black flake8 + + - name: Run linters + uses: wearerequired/lint-action@v2 + with: + auto_fix: true + black: true + black_auto_fix: true + flake8: true + flake8_auto_fix: false diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..db8f299 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,35 @@ +name: Tests + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + name: Running Tests + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest + pip install -e . + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Test with pytest + run: | + pytest + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index 71b6167..ad7b9d1 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,120 @@ -# Github-Training-Repository -This repository is meant to be used to train new hires/volunteers on how to use Github. Reading the material and finishing the exercise should take around 6-8 hours. - -Before you get started, please check that your actual name is visible on your Github profile. This way we know which account belongs to whom and when we make repositories citable your real name will be used and not your nickname. - -# Preparation Material -Please look at all those materials before you start the exercise. - -* [Good enough practices in scientific computing](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005510): This gives a good overview of why we need to adhere to good coding practices and what the general idea is -* [Good Research Code Handbook](https://goodresearch.dev/): Explanation of how to implement good coding practices in Python -* [Fundamentals of Git and Github](https://www.youtube.com/watch?v=8Dd7KRpKeaE) -* [How to use Github with Github Desktop](https://www.youtube.com/watch?v=8Dd7KRpKeaE) -* [Getting started with anaconda and conda](https://youtu.be/YJC6ldI3hWk) -* [ALLFED Repository Template Readme](https://github.com/allfed/ALLFED-Repository-Template) -* [Overview of Python](https://www.youtube.com/watch?v=kqtD5dpn9C8) -* [Automated Testing](https://blog.deepjyoti30.dev/tests-github-python) -* The [OpenAI Chat Bot](https://chat.openai.com/chat) is pretty good at answering programming questions -* It is recommended to use [VS Code](https://www.youtube.com/watch?v=B-s71n0dHUk) as your code editor -* The [Seaweed Growth Model Repository](https://github.com/allfed/Seaweed-Growth-Model) can be seen as an example of how ALLFED repositories should look like. You can check how things are done there, if something here confuses you - -# Exercise -1. Fork this repository -2. Clone it to your local computer -3. Recreate the folder structure as described in the [ALLFED Guidelines](https://github.com/allfed/ALLFED-Repository-Template) -4. Create a local virtual environment for the repository - * When you try to install/change things make sure are [activating it first!](https://goodresearch.dev/setup.html?highlight=activate#conda). If something related to virtual environments isn't working, always make sure that it is really activated. -5. Create two files in the src folder: numerical.py and plotting.py -6. Write a function in numerical.py that takes at least one argument and returns a numerical value -7. Write a function in plotting.py that creates a scatter plot and uses the [ALLFED Style Sheet](https://github.com/allfed/ALLFED-matplotlib-style-sheet) -8. Make your repository an installable package as described in [Good Research Code Handbook](https://goodresearch.dev/setup.html) -9. Add a Jupyter Notebook in your scripts folder and import numerical.py and call it -11. Write two test for numerical.py -12. Make sure that the documenation of all code follows the [ALLFED Guidelines](https://github.com/allfed/ALLFED-Repository-Template#allfed-python-style-guide) - * If you set-up automated documentation, you can see the status in the [pages setting](https://github.com/allfed/Seaweed-Growth-Model/settings/pages) -14. Automate the tests, so they run on every commit (you can just copy the files needed for that from [the template](https://github.com/allfed/ALLFED-Repository-Template) - * The files used for testing in Github Actions are hidden files. You might need to change the settings of your operation system to show you the hidden files. - * you can play around with pytest in your terminal in VS Code -15. Create an environment.yml that specifies how your virtual environment can be recreated and save it in the repository -16. Send back a pull request -17. Check back in with Morgan or Florian if you have any questions (florian@allfed.info or morgan@allfed.info) - -If you get stuck at any point please reach out to one of the data scientists (either florian@allfed.info or morgan@allfed.info). + +# ALLFED-Repository-Template + +**If this is the first time you want to use this template, please go through the [Github training](https://github.com/allfed/Github-Training-Repository/blob/main/README.md) first. This will give you the neccesary background knowledge to understand what is happening here. + +## How to use this template +Use this as template when you start a new project by clicking "Use this template" in the top right. + +Then follow all the descriptions below. + +## ALLFED Python Style Guide +All code written for ALLFED should follow the [PEP 8 Style Guide for Python](https://peps.python.org/pep-0008/). Especially important are: +* Keep the code well documented +* Every function needs a docstring in this form: +``` +def count_line(f, line): + """ + Counts the number of times a line occurs. Case-sensitive. + + Arguments: + f (file): the file to scan + line (str): the line to count + + Returns: + int: the number of times the line occurs. + """ +``` +* Write [decoupled code](https://goodresearch.dev/decoupled.html), e.g. Functions should do exactly one thing and be as short as possible +* Naming conventions: + - Snake case for variables and module: variable_name, my_module.py + - Camel case for class name: MyClass + - Camel case with spaces for jupyter notebook: Analyze Brain Data.ipynb +* Delete dead code! Don't outcomment code you don't use anymore, but delete it instead. If you need to find it again, that's what we have git for. +* Use Jupyter Notebooks only for explanations and visualization. The actual programming should be happening in `.py` files. +* This repository is automatically set up with Github Actions/[Lint Action](https://github.com/marketplace/actions/lint-action) that will format your code using black and check for problems with flake8 ([without E203, W503](https://black.readthedocs.io/en/stable/faq.html#why-are-flake8-s-e203-and-w503-violated)). If either of them fails, you will not be able to merge your files unless you fix it. If this creates problem you cannot solve contact: florian@allfed.info +* You can deactivate the branch protection that makes sure that only correct and styled code can be merged, but it is not recommended. + + +To make this easier you can use linter (auto-formatter) that change your code to be formatted in PEP 8 when you safe it. E.g. [here for Spyder](https://stackoverflow.com/questions/51463223/how-to-use-pep8-module-using-spyder) or [VS Code](https://code.visualstudio.com/docs/python/linting). + +## Testing +We want to create reliable code. This means, as much of the code needs to be automatically tested, to make sure that everything runs as intended. Therefore, every possible function should have some kind of `assert` that tests if it works. We use pytest [pytest](https://docs.pytest.org/en/7.1.x/) as our main test suit. All tests that you put in the tests folder in here are automatically run every time you push code. You can read more about testing [here](https://goodresearch.dev/testing.html). To adapt the testing.yml to your repository you just have to adapt the requirements in requirements.txt. Everything else should work on its own. You can find an example of how a test file should look like in the tests folder. Once you go the testing set up, also make sure to add the testing badge to the readme of your repository. Simply change the URL to your repository in this badge: + +![Testing](https://github.com/allfed/allfed-repository-template/actions/workflows/testing.yml/badge.svg) + + +## Documenting in small projects +Documenting your code is only one part of the documentation we want to create. Every larger repository needs: +* a readme file that explains what the repository is for and how it is organized, which should contain: + - A one-sentence description of your project + - A longer description of your project + - Installation instructions + - General orientation to the codebase and usage instructions + - Links to papers + - Links to extended docs + - License + +* a tutorial Jupyter Notebook to explain how the repository is supposed to be used + + +## Documenting in big projects +All the things for the small projects, but also: +* An automated documentation via Gitub Actions. This is already set up in this repository. Is uses the code from [this post](https://towardsdatascience.com/easily-automate-and-never-touch-your-documentation-again-a98c91ce1b95) and combines it with [this one](https://blog.elmah.io/deploying-a-mkdocs-documentation-site-with-github-actions/). It also is setup that it will only look for python files in the src folder. So, make sure that everything is in there (you can changes this behavior in the main function of `automate_mkdocs.py`). To get it running do the following + * Change the names in `mkgendocs.yml` and `mkdocs.yml` so they fit your repository + * go to Settings --> Pages + * select deploy from a branch as source + * select gh-pages as branch at root (for this option to pop up the `docs.yml` file has to have run succesfully at least once, this should happen when you push anything in your repository) + * The end result will look something [like this](https://florianjehn.github.io/Seaweed-Growth-Model/) + * The automated documentation part is still a bit wip, if you run into problems contact florian@allfed.info +* Create a [visual representation](https://goodresearch.dev/_images/pcbi.1007358.g002.PNG_L.png) of how the different files interact with each other + +## Issues +Every time you come across a problem that you do not plan to fix in the next day, please open an issue in the repository to make sure that it does not get lost. This also allows you to assign tasks to other coders on the team. For a short intro to issues, see [here](https://www.youtube.com/watch?v=TKJ4RdhyB5Y). + +## Making the repository citable +All ALLFED repositories should be citable by release. For this we use [Zenodo](https://zenodo.org/). This has to be activated by an Admin (so either Florian or Morgan). Once you have a manuscript where you need to cite the repository let them know and they will activate it. This will also create a doi badge, which should be included in the readme, like this: + +--- + + +[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6865646.svg)](https://doi.org/10.5281/zenodo.6865646) + + +--- + +## ALLFED Plotting Style +All plots created for ALLFED should look and feel the same. You can activate the ALLFED style by simply starting your code with: + +`plt.style.use("https://raw.githubusercontent.com/allfed/ALLFED-matplotlib-style-sheet/main/ALLFED.mplstyle")` + +If you need to create your plots in ALLFED style, while being offline just download the file and change the path to local. + +## Project Skeleton +This repository already has the folder structure we use for repositories. Every folder has an additional readme, to tell you what needs to go in there. + +## Making the repository a pip installable Python package +For some repositories it makes sense to make them installable via pip (e.g. a model we want to share easily). In this case you can use the explanation [here](https://goodresearch.dev/setup.html). This repository already contains a setup.py that can be used for that. If you want to install it to your local environment just run `pip install -e .` while being in the folder that contains setup.py. + +## Environment +Every ALLFED project is run in its own virtual environment. Therefore, every project needs an `environment.yml` file. The one in this repository is only an example and should not be used for any actual project. To create and organize virtual environments we use [conda](https://docs.conda.io/en/latest/miniconda.html). + +## Requirements +Every ALLFED project needs a requirements file that specifies what packages are needed to run the project. You can find an example file in the repository. If you use a lot of packages you can use [pipreqg](https://allfed.github.io/Seaweed-Growth-Model/) to find them all. + +## License +ALLFED publishes its code in Open Access. For this we use the [**Apache 2.0 License**](https://www.planetcrust.com/what-does-apache-2-0-license-mean). This license allows very free use of the code, but makes sure that ALLFED cannot be sued if something goes wrong with the code. The license template in this repository needs to be adapted when a new project is created. You can include the following license badge in your readme: + +[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) + +## Gitignore +The [.gitignore file](https://git-scm.com/docs/gitignore) is the default one for Python. Make sure you change it when using another programming language. + +## Further reading/Misc +* [Best Practices for Scientific Computing](https://journals.plos.org/plosbiology/article?id=10.1371/journal.pbio.1001745) +* [General course to get to know the Python skills needed to the work at ALLFED](https://github.com/florianjehn/python-for-environmental-science) +* We are using [Github Copilot](https://github.com/features/copilot) to write faster and cleaner code. If you would also like to have a license contact finance@allfed.info + +## Acknowledgment +This is strongly based on the ["Good Research Code Handbook"](https://goodresearch.dev/index.html). If something here confuses you, it makes sense to read about it there. Pretty good explanations. + diff --git a/automate_mkdocs.py b/automate_mkdocs.py new file mode 100644 index 0000000..5555004 --- /dev/null +++ b/automate_mkdocs.py @@ -0,0 +1,216 @@ +"""Automates Python scripts formatting, linting and Mkdocs documentation.""" + +import ast +import importlib +import json +import yaml +from collections import defaultdict +from pathlib import Path +from typing import Union + + +def add_val(indices, value, data): + if not len(indices): + return + element = data + for index in indices[:-1]: + element = element[index] + element[indices[-1]] = value + + +def automate_mkdocs_from_docstring( + mkdocs_dir: Union[str, Path], mkgendocs_f: str, repo_dir: Path, match_string: str +) -> dict: + """Automates the -pages for mkgendocs package by adding all Python functions in a + directory to the mkgendocs config. + Args: + mkdocs_dir (typing.Union[str, pathlib.Path]): textual directory for + the hierarchical directory & navigation in Mkdocs + mkgendocs_f (str): The configurations file for the mkgendocs package + repo_dir (pathlib.Path): textual directory to search for Python functions in + match_string (str): the text to be matches, after which the functions will be + added in mkgendocs format + Example: + >>> + >>> automate_mkdocs_from_docstring('scripts', repo_dir=Path.cwd(), match_string='pages:') + Returns: + list: list of created markdown files and their relative paths + + """ + p = repo_dir.glob("**/*.py") + scripts = [x for x in p if x.is_file()] + + if ( + Path.cwd() != repo_dir + ): # look for mkgendocs.yml in the parent file if a subdirectory is used + repo_dir = repo_dir.parent + + functions = defaultdict(dict) + structure = fix(defaultdict)() + full_repo_dir = str(repo_dir) + "/" + for script in scripts: + with open(script, "r") as source: + tree = ast.parse(source.read()) + funcs = {"classes": [], "functions": []} + for child in ast.iter_child_nodes(tree): + try: + if isinstance( + child, (ast.FunctionDef, ast.ClassDef, ast.AsyncFunctionDef) + ): + if child.name not in ["main"]: + relative_path = ( + str(script) + .replace(full_repo_dir, "") + .replace("/", ".") + .replace(".py", "") + ) + module = importlib.import_module(relative_path) + f_ = getattr(module, child.name) + function = f_.__name__ + if isinstance(child, (ast.ClassDef)): + funcs["classes"].append(function) + if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)): + funcs["functions"].append(function) + + except Exception as e: + print("trouble on importing " + script.stem) + print("did not document " + child.name) + print(str(e)) + if not funcs["classes"]: + funcs.pop("classes") + if not funcs["functions"]: + funcs.pop("functions") + if funcs: + functions[script] = funcs + with open(f"{repo_dir}/{mkgendocs_f}", "r+") as mkgen_config: + insert_string = "" + for path, function_names in functions.items(): + relative_path = str(path).replace(full_repo_dir, "").replace(".py", "") + insert_string += ( + f' - page: "{mkdocs_dir}/{relative_path}.md"\n ' + f'source: "{relative_path}.py"\n' # functions:\n' + ) + page = f"{mkdocs_dir}/{relative_path}" + split_page = page.split("/") + split_page = [" - " + s for s in split_page] + page += f".md" + + add_val(split_page, page, structure) + for class_name, class_list in function_names.items(): + insert_string += f" {class_name}:\n" + f_string = "" + for f in class_list: + insert_f_string = f" - {f}\n" + f_string += insert_f_string + + insert_string += f_string + insert_string += "\n" + + contents = mkgen_config.readlines() + if match_string in contents[-1]: + contents.append(insert_string) + else: + for index, line in enumerate(contents): + if match_string in line and insert_string not in contents[index + 1]: + contents = contents[: index + 1] + contents.append(insert_string) + break + + with open(f"{repo_dir}/{mkgendocs_f}", "w") as mkgen_config: + mkgen_config.writelines(contents) + + return structure + + +def automate_nav_structure( + mkdocs_dir: Union[str, Path], + mkdocs_f: str, + repo_dir: Path, + match_string: str, + structure: dict, +) -> str: + """Automates the -pages for mkgendocs package by adding all Python + functions in a directory to the mkgendocs config. + Args: + mkdocs_dir (typing.Union[str, pathlib.Path]): textual directory for + the hierarchical directory & navigation in Mkdocs + mkgendocs_f (str): The configurations file for the mkgendocs package + repo_dir (pathlib.Path): textual directory to search for Python functions in + match_string (str): the text to be matches, after which the functions + will be added in mkgendocs format + Example: + >>> + >>> automate_mkdocs_from_docstring('scripts', repo_dir=Path.cwd(), match_string='pages:') + Returns: + str: feedback message + + """ + insert_string = yaml.safe_dump(json.loads(json.dumps(structure, indent=4))).replace( + "'", "" + ) + # print(structure) + with open(f"{repo_dir}/{mkdocs_f}", "r+") as mkgen_config: + assert mkgen_config is not None + contents = mkgen_config.readlines() + if match_string in contents[-1]: + contents.append(insert_string) + else: + for index, line in enumerate(contents): + if match_string in line and insert_string not in contents[index + 1]: + contents = contents[: index + 1] + contents.append(insert_string) + break + + with open(f"{repo_dir}/{mkdocs_f}", "w") as mkgen_config: + mkgen_config.writelines(contents) + + +def fix(f): + """Allows creation of arbitrary length dict item + + Args: + f (type): Description of parameter `f`. + + Returns: + type: Description of returned object. + + """ + return lambda *args, **kwargs: f(fix(f), *args, **kwargs) + + +def indent(string: str) -> int: + """Count the indentation in whitespace characters. + Args: + string (str): text with indents + Returns: + int: Number of whitespace indentations + + """ + return sum(4 if char == "\t" else 1 for char in string[: -len(string.lstrip())]) + + +def main(): + """Execute when running this script.""" + # This is the path the script will look for code to document + python_tips_dir = Path.cwd().joinpath("src") + # This is the path the script will look for mkgendocs.yml + root_dir = Path.cwd() + + structure = automate_mkdocs_from_docstring( + mkdocs_dir="modules", + mkgendocs_f="mkgendocs.yml", + repo_dir=python_tips_dir, + match_string="pages:\n", + ) + + automate_nav_structure( + mkdocs_dir="modules", + mkdocs_f="mkdocs.yml", + repo_dir=root_dir, + match_string="- Home: index.md\n", + structure=structure, + ) + + +if __name__ == "__main__": + main() diff --git a/data/readme.txt b/data/readme.txt new file mode 100644 index 0000000..1b7b100 --- /dev/null +++ b/data/readme.txt @@ -0,0 +1 @@ +data: Where you put raw data for your project. You usually won’t sync this to source control, unless you use very small, text-based datasets (< 10 MBs). diff --git a/docs/readme.txt b/docs/readme.txt new file mode 100644 index 0000000..8128cbf --- /dev/null +++ b/docs/readme.txt @@ -0,0 +1 @@ +docs: Where you put documentation, including Markdown and reStructuredText (reST). Calling it docs makes it easy to publish documentation online through Github pages. diff --git a/docs/sources/index.md b/docs/sources/index.md new file mode 100644 index 0000000..450c370 --- /dev/null +++ b/docs/sources/index.md @@ -0,0 +1,102 @@ +# ALLFED-Repository-Template +Use this as template when you start a new project by clicking "Use this template" in the top right. + + +## ALLFED Python Style Guide +All code written for ALLFED should follow the [PEP 8 Style Guide for Python](https://peps.python.org/pep-0008/). Especially important are: +* Keep the code well documented +* Every function needs a docstring in this form: +``` +def count_line(f, line): + """ + Counts the number of times a line occurs. Case-sensitive. + + Arguments: + f (file): the file to scan + line (str): the line to count + + Returns: + int: the number of times the line occurs. + """ +``` +* Write [decoupled code](https://goodresearch.dev/decoupled.html), e.g. Functions should do exactly one thing and be as short as possible +* Naming conventions: + - Snake case for variables and module: variable_name, my_module.py + - Camel case for class name: MyClass + - Camel case with spaces for jupyter notebook: Analyze Brain Data.ipynb +* Delete dead code! Don't outcomment code you don't use anymore, but delete it instead. If you need to find it again, that's what we have git for. +* Use Jupyter Notebooks only for explanations and visualization. The actual programming should be happening in `.py` files. +* This repository is automatically set up with Github Actions/[Lint Action](https://github.com/marketplace/actions/lint-action) that will format your code using black and check for problems with flake8 ([without E203, W503](https://black.readthedocs.io/en/stable/faq.html#why-are-flake8-s-e203-and-w503-violated)). If either of them fails, you will not be able to merge your files unless you fix it. If this creates problem you cannot solve contact: florian@allfed.info +* You can deactivate the branch protection that makes sure that only correct and styled code can be merged, but it is not recommended. + + +To make this easier you can use linter (auto-formatter) that change your code to be formatted in PEP 8 when you safe it. E.g. [here for Spyder](https://stackoverflow.com/questions/51463223/how-to-use-pep8-module-using-spyder) or [VS Code](https://code.visualstudio.com/docs/python/linting). + +## Testing +We want to create reliable code. This means, as much of the code needs to be automatically tested, to make sure that everything runs as intended. Therefore, every possible function should have some kind of `assert` that tests if it works. We use pytest [pytest](https://docs.pytest.org/en/7.1.x/) as our main test suit. All tests that you put in the tests folder in here are automatically run every time you push code. You can read more about testing [here](https://goodresearch.dev/testing.html). To adapt the testing.yml to your repository you just have to adapt the requirements in requirements.txt. Everything else should work on its own. You can find an example of how a test file should look like in the tests folder. Once you go the testing set up, also make sure to add the testing badge to the readme of your repository. Simply change the URL to your repository in this badge: + +![Testing](https://github.com/allfed/allfed-repository-template/actions/workflows/testing.yml/badge.svg) + + +## Documenting in small projects +Documenting your code is only one part of the documentation we want to create. Every larger repository needs: +* a readme file that explains what the repository is for and how it is organized, which should contain: + - A one-sentence description of your project + - A longer description of your project + - Installation instructions + - General orientation to the codebase and usage instructions + - Links to papers + - Links to extended docs + - License + +* a tutorial Jupyter Notebook to explain how the repository is supposed to be used + + +## Documenting in big projects +All the things for the small projects, but also: +* An automated documentation via Gitub Actions. This is already set up in this repository. Is uses the code from [this post](https://towardsdatascience.com/easily-automate-and-never-touch-your-documentation-again-a98c91ce1b95) and combines it with [this one](https://blog.elmah.io/deploying-a-mkdocs-documentation-site-with-github-actions/). It also is setup that it will only look for python files in the src folder. So, make sure that everything is in there (you can changes this behavior in the main function of `automate_mkdocs.py`). To get it running do the following + * go to Settings --> Pages + * select deploy from a branch as source + * select gh-pages as branch at root (for this option to pop up the `docs.yml` file has to have run succesfully at least once) + * The end result will look something [like this](https://florianjehn.github.io/Seaweed-Growth-Model/) + * The automated documentation part is still a bit wip, if you run into problems contact florian@allfed.info +* Create a [visual representation](https://goodresearch.dev/_images/pcbi.1007358.g002.PNG_L.png) of how the different files interact with each other + +## Making the repository citable +All ALLFED repositories should be citable by release. For this we use [Zenodo](https://zenodo.org/). This has to be activated by an Admin (so either Florian or Morgan). Once you have a manuscript where you need to cite the repository let them know and they will activate it. This will also create a doi badge, which should be included in the readme, like this: + +--- + + +[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6865646.svg)](https://doi.org/10.5281/zenodo.6865646) + + +--- + +## Project Skeleton +This repository already has the folder structure we use for repositories. Every folder has an additional readme, to tell you what needs to go in there. + +## Making the repository a pip installable Python package +For some repositories it makes sense to make them installable via pip (e.g. a model we want to share easily). In this case you can use the explanation [here](https://goodresearch.dev/setup.html). This repository already contains a setup.py that can be used for that. + +## Environment +Every ALLFED project is run in its own virtual environment. Therefore, every project needs an `environment.yml` file. The one in this repository is only an example and should not be used for any actual project. To create and organize virtual environments we use [conda](https://docs.conda.io/en/latest/miniconda.html). + +## Requirements +Every ALLFED project needs a requirements file that specifies what packages are needed to run the project. You can find an example file in the repository. If you use a lot of packages you can use [pipreqg](https://allfed.github.io/Seaweed-Growth-Model/) to find them all. + +## License +ALLFED publishes its code in Open Access. For this we use the [**Apache 2.0 License**](https://www.planetcrust.com/what-does-apache-2-0-license-mean). This license allows very free use of the code, but makes sure that ALLFED cannot be sued if something goes wrong with the code. The license template in this repository needs to be adapted when a new project is created. You can include the following license badge in your readme: + +[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) + +## Gitignore +The [.gitignore file](https://git-scm.com/docs/gitignore) is the default one for Python. Make sure you change it when using another programming language. + +## Further reading/Misc +* [Best Practices for Scientific Computing](https://journals.plos.org/plosbiology/article?id=10.1371/journal.pbio.1001745) +* [General course to get to know the Python skills needed to the work at ALLFED](https://github.com/florianjehn/python-for-environmental-science) +* We are using [Github Copilot](https://github.com/features/copilot) to write faster and cleaner code. If you would also like to have a license contact finance@allfed.info + +## Acknowledgment +This is strongly based on the ["Good Research Code Handbook"](https://goodresearch.dev/index.html). If something here confuses you, it makes sense to read about it there. Pretty good explanations. diff --git a/environment.yml b/environment.yml new file mode 100644 index 0000000..70ce4e7 --- /dev/null +++ b/environment.yml @@ -0,0 +1,134 @@ +name: env +channels: + - conda-forge + - defaults +dependencies: + - asttokens=2.4.0=pyhd8ed1ab_0 + - backcall=0.2.0=pyh9f0ad1d_0 + - backports=1.0=pyhd8ed1ab_3 + - backports.functools_lru_cache=1.6.5=pyhd8ed1ab_0 + - brotli=1.1.0=hcfcfb64_1 + - brotli-bin=1.1.0=hcfcfb64_1 + - bzip2=1.0.8=he774522_0 + - ca-certificates=2023.7.22=h56e8100_0 + - certifi=2023.7.22=pyhd8ed1ab_0 + - colorama=0.4.6=pyhd8ed1ab_0 + - comm=0.1.4=pyhd8ed1ab_0 + - contourpy=1.1.1=py312h0d7def4_1 + - cycler=0.12.1=pyhd8ed1ab_0 + - debugpy=1.8.0=py312h53d5487_1 + - decorator=5.1.1=pyhd8ed1ab_0 + - exceptiongroup=1.1.3=pyhd8ed1ab_0 + - executing=1.2.0=pyhd8ed1ab_0 + - expat=2.5.0=hd77b12b_0 + - fonttools=4.43.1=py312he70551f_0 + - freetype=2.12.1=hdaf720e_2 + - gettext=0.21.1=h5728263_0 + - glib=2.78.0=h12be248_0 + - glib-tools=2.78.0=h12be248_0 + - gst-plugins-base=1.22.6=h001b923_2 + - gstreamer=1.22.6=hb4038d2_2 + - icu=73.2=h63175ca_0 + - importlib-metadata=6.8.0=pyha770c72_0 + - importlib_metadata=6.8.0=hd8ed1ab_0 + - intel-openmp=2023.2.0=h57928b3_50496 + - ipykernel=6.25.2=pyh60829e3_0 + - ipython=8.16.1=pyh5737063_0 + - jedi=0.19.1=pyhd8ed1ab_0 + - jupyter_client=8.4.0=pyhd8ed1ab_0 + - jupyter_core=5.4.0=py312h2e8e312_0 + - kiwisolver=1.4.5=py312h0d7def4_1 + - krb5=1.21.2=heb0366b_0 + - lcms2=2.15=h67d730c_3 + - lerc=4.0.0=h63175ca_0 + - libblas=3.9.0=19_win64_mkl + - libbrotlicommon=1.1.0=hcfcfb64_1 + - libbrotlidec=1.1.0=hcfcfb64_1 + - libbrotlienc=1.1.0=hcfcfb64_1 + - libcblas=3.9.0=19_win64_mkl + - libclang=15.0.7=default_h77d9078_3 + - libclang13=15.0.7=default_h77d9078_3 + - libdeflate=1.19=hcfcfb64_0 + - libexpat=2.5.0=h63175ca_1 + - libffi=3.4.4=hd77b12b_0 + - libglib=2.78.0=he8f3873_0 + - libhwloc=2.9.3=default_haede6df_1009 + - libiconv=1.17=h8ffe710_0 + - libjpeg-turbo=3.0.0=hcfcfb64_1 + - liblapack=3.9.0=19_win64_mkl + - libogg=1.3.4=h8ffe710_1 + - libpng=1.6.39=h19919ed_0 + - libsodium=1.0.18=h8d14728_1 + - libsqlite=3.43.2=hcfcfb64_0 + - libtiff=4.6.0=h6e2ebb7_2 + - libvorbis=1.3.7=h0e60522_0 + - libwebp-base=1.3.2=hcfcfb64_0 + - libxcb=1.15=hcd874cb_0 + - libxml2=2.11.5=hc3477c8_1 + - libzlib=1.2.13=hcfcfb64_5 + - m2w64-gcc-libgfortran=5.3.0=6 + - m2w64-gcc-libs=5.3.0=7 + - m2w64-gcc-libs-core=5.3.0=7 + - m2w64-gmp=6.1.0=2 + - m2w64-libwinpthread-git=5.0.0.4634.697f757=2 + - matplotlib=3.8.0=py312h2e8e312_2 + - matplotlib-base=3.8.0=py312h26ecaf7_2 + - matplotlib-inline=0.1.6=pyhd8ed1ab_0 + - mkl=2023.2.0=h6a75c08_50496 + - msys2-conda-epoch=20160418=1 + - munkres=1.1.4=pyh9f0ad1d_0 + - nest-asyncio=1.5.8=pyhd8ed1ab_0 + - numpy=1.26.0=py312h8753938_0 + - openjpeg=2.5.0=h3d672ee_3 + - openssl=3.1.3=hcfcfb64_0 + - packaging=23.2=pyhd8ed1ab_0 + - parso=0.8.3=pyhd8ed1ab_0 + - pcre2=10.40=h17e33f8_0 + - pickleshare=0.7.5=py_1003 + - pillow=10.1.0=py312he768995_0 + - pip=23.3=py312haa95532_0 + - platformdirs=3.11.0=pyhd8ed1ab_0 + - ply=3.11=py_1 + - prompt-toolkit=3.0.39=pyha770c72_0 + - prompt_toolkit=3.0.39=hd8ed1ab_0 + - psutil=5.9.5=py312he70551f_1 + - pthread-stubs=0.4=hcd874cb_1001 + - pthreads-win32=2.9.1=hfa6e2cd_3 + - pure_eval=0.2.2=pyhd8ed1ab_0 + - pygments=2.16.1=pyhd8ed1ab_0 + - pyparsing=3.1.1=pyhd8ed1ab_0 + - pyqt=5.15.9=py312he09f080_5 + - pyqt5-sip=12.12.2=py312h53d5487_5 + - python=3.12.0=h2628c8c_0_cpython + - python-dateutil=2.8.2=pyhd8ed1ab_0 + - python_abi=3.12=4_cp312 + - pywin32=306=py312h53d5487_2 + - pyzmq=25.1.1=py312h8139593_1 + - qt-main=5.15.8=h9e85ed6_17 + - setuptools=68.0.0=py312haa95532_0 + - sip=6.7.12=py312h53d5487_0 + - six=1.16.0=pyh6c4a22f_0 + - sqlite=3.41.2=h2bbff1b_0 + - stack_data=0.6.2=pyhd8ed1ab_0 + - tbb=2021.10.0=h91493d7_2 + - tk=8.6.13=hcfcfb64_0 + - toml=0.10.2=pyhd8ed1ab_0 + - tomli=2.0.1=pyhd8ed1ab_0 + - tornado=6.3.3=py312he70551f_1 + - traitlets=5.11.2=pyhd8ed1ab_0 + - typing-extensions=4.8.0=hd8ed1ab_0 + - typing_extensions=4.8.0=pyha770c72_0 + - tzdata=2023c=h04d1e81_0 + - ucrt=10.0.22621.0=h57928b3_0 + - vc=14.2=h21ff451_1 + - vc14_runtime=14.36.32532=hdcecf7f_17 + - vs2015_runtime=14.36.32532=h05e6639_17 + - wcwidth=0.2.8=pyhd8ed1ab_0 + - wheel=0.37.1=pyhd3eb1b0_0 + - xorg-libxau=1.0.11=hcd874cb_0 + - xorg-libxdmcp=1.1.3=hcd874cb_0 + - xz=5.4.2=h8cc25b3_0 + - zeromq=4.3.4=h0e60522_1 + - zipp=3.17.0=pyhd8ed1ab_0 + - zstd=1.5.5=h12be248_0 +prefix: C:\Users\zaris\.conda\envs\env diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..0b58c4a --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,28 @@ +site_name: My Super Cool Respository + + +# Repository +repo_name: My Super Cool Respository +repo_url: https://github.com/allfed/My Super-Cool-Respository +edit_uri: "" +# Copyright +copyright: Me + +docs_dir: "docs/sources" + +plugins: + - search + +theme: + name: 'readthedocs' + custom_dir: 'docs' + palette: + primary: 'white' + logo: 'assets/images/logo_full.svg' + favicon: 'assets/images/favicon.svg' + features: + - navigation.tabs + - navigation.tabs.sticky + - instant +nav: + diff --git a/mkgendocs.yml b/mkgendocs.yml new file mode 100644 index 0000000..8d9a562 --- /dev/null +++ b/mkgendocs.yml @@ -0,0 +1,11 @@ +site_name: My Super Cool Respository + + +# Repository +repo_name: My Super Cool Respository +repo: https://github.com/allfed/My-Super-Cool-Respository +# Copyright +copyright: ALLFED + +#This specifies the autogeneration portion +pages: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4355f56 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +pandas>=1.4.3 +setuptools>=61.2.0 +pytest>=7.1.2 +numpy>=1.23.1 +matplotlib>=3.5.1 +mkdocs diff --git a/results/readme.txt b/results/readme.txt new file mode 100644 index 0000000..aede27e --- /dev/null +++ b/results/readme.txt @@ -0,0 +1 @@ +results: Where you put results, including checkpoints, hdf5 files, pickle files, as well as figures and tables. If these files are heavy, you won’t put these under source control. diff --git a/scripts/Calculate Area.ipynb b/scripts/Calculate Area.ipynb new file mode 100644 index 0000000..4c377fa --- /dev/null +++ b/scripts/Calculate Area.ipynb @@ -0,0 +1,53 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#import calculating area equation\n", + "from src.numerical import calculate_circle_area" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "78.53981633974483\n" + ] + } + ], + "source": [ + "result = calculate_circle_area(5)\n", + "print(result)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "env", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/scripts/readme.txt b/scripts/readme.txt new file mode 100644 index 0000000..e7c528c --- /dev/null +++ b/scripts/readme.txt @@ -0,0 +1 @@ +scripts: Where you put scripts - Python and bash alike - as well as .ipynb notebooks. diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..161f66e --- /dev/null +++ b/setup.py @@ -0,0 +1,6 @@ +from setuptools import find_packages, setup + +setup( + name="src", + packages=find_packages(), +) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/numerical.py b/src/numerical.py new file mode 100644 index 0000000..f91ca00 --- /dev/null +++ b/src/numerical.py @@ -0,0 +1,17 @@ +"""Module providing a function that calculate the area of a circle.""" +import math + + +def calculate_circle_area(radius): + """ + Calculates the area of a circle from its radius. + + Arguments: + radius (float): The radius of the circle. + + Returns: + float: The area of the circle. + """ + + area = math.pi * radius * radius + return area diff --git a/src/plotting.py b/src/plotting.py new file mode 100644 index 0000000..37cc1ee --- /dev/null +++ b/src/plotting.py @@ -0,0 +1,34 @@ +"""Function creating a scatterplot""" + +import matplotlib.pyplot as plt + +plt.style.use( + "https://raw.githubusercontent.com/allfed/ALLFED-matplotlib-style-sheet/main/ALLFED.mplstyle" +) + + +def create_scatter_plot(x_data, y_data, title="", xlabel="", ylabel=""): + """ + Create a scatter plot with the provided x and y data. + + Arguments: + x_data (list): The data for the x-axis. + y_data (list): The data for the y-axis. + title (str): The title of the plot. + xlabel (str): The label for the x-axis. + ylabel (str): The label for the y-axis. + """ + + # Create a new figure and axes + plt.figure() + + # Create the scatter plot + plt.scatter(x_data, y_data) + + # Set the title and labels + plt.title(title) + plt.xlabel(xlabel) + plt.ylabel(ylabel) + + # Show the plot + plt.show() diff --git a/src/readme.txt b/src/readme.txt new file mode 100644 index 0000000..503277b --- /dev/null +++ b/src/readme.txt @@ -0,0 +1 @@ +src: Where you put reusable Python modules for your project. This is the kind of python code that you import. diff --git a/tests/readme.txt b/tests/readme.txt new file mode 100644 index 0000000..2cbb868 --- /dev/null +++ b/tests/readme.txt @@ -0,0 +1 @@ +tests: Where you put tests for your code. diff --git a/tests/test_calculate_circle_area.py b/tests/test_calculate_circle_area.py new file mode 100644 index 0000000..777389d --- /dev/null +++ b/tests/test_calculate_circle_area.py @@ -0,0 +1,16 @@ +"""Tests to calculate_circle_area function""" + +import math +from src.numerical import calculate_circle_area + + +def test_calculate_circle_area(): + """ + Test with a positive radius, typical input + Test with radius 0, edge case + """ + radius = 5 + expected_result = math.pi * radius**2 + + assert calculate_circle_area(radius) == expected_result + assert calculate_circle_area(0) == 0