From 621477c97b20e59f44fe5d70fd956071d4a361fa Mon Sep 17 00:00:00 2001 From: Boris Baldassari Date: Sun, 7 Jan 2024 15:25:19 +0100 Subject: [PATCH] Init template for scripts. --- scripts/templates/my_script.py | 70 +++++++++++++++++++++++++++++ scripts/templates/test_my_script.py | 31 +++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 scripts/templates/my_script.py create mode 100644 scripts/templates/test_my_script.py diff --git a/scripts/templates/my_script.py b/scripts/templates/my_script.py new file mode 100644 index 0000000..e21950b --- /dev/null +++ b/scripts/templates/my_script.py @@ -0,0 +1,70 @@ +###################################################################### +# Copyright (c) Boris Baldassari, Adrien Luxey-Bitri +# +# This program and the accompanying materials are made +# available under the terms of the Eclipse Public License 2.0 +# which is available at https://www.eclipse.org/legal/epl-2.0/ +# +# SPDX-License-Identifier: EPL-2.0 +###################################################################### + +import argparse +from pprint import pprint + +""" +This module provides a set of example functions. + +See also the tests for the module, located in test_my_script.py. +Run tests with `pytest test_my_script`: +$ pytest test_my_script.py -vvvv +======================== test session starts ======================== +platform linux -- Python 3.11.2, pytest-7.2.1, pluggy-1.0.0+repack -- /usr/bin/python3 +cachedir: .pytest_cache +rootdir: /home/user/project/scripts/templates +collected 1 item + +test_my_script.py::test_add_integers_case0 PASSED [100%] + +========================= 1 passed in 0.00s ========================= + +""" + + +def _parse_args(): + """ + Parse arguments from command line. + """ + parser = argparse.ArgumentParser(description="A script that does something.") + parser.add_argument('-wg', '--working_group', + help='Retrieve information for all projects in a working group.') + parser.add_argument('-p', '--project', + help='Retrieve information for a single project ID.') + parser.add_argument('-c', '--conf', + help='Configuration file for opensearch access.') + args = parser.parse_args() + return args + + +def add_ints(a: int, b: int): + """ + Adds two integers and returns the outcome. + + Returns + + Parameters: + + - `a`: First integer to add. + - `b`: Second integer to add. + """ + print('Printed from add_ints function.') + return a + b + + +if __name__ == '__main__': + + # Parse command-line arguments + args = _parse_args() + pprint(args) + + + diff --git a/scripts/templates/test_my_script.py b/scripts/templates/test_my_script.py new file mode 100644 index 0000000..f641340 --- /dev/null +++ b/scripts/templates/test_my_script.py @@ -0,0 +1,31 @@ +###################################################################### +# Copyright (c) Boris Baldassari, Adrien Luxey-Bitri +# +# This program and the accompanying materials are made +# available under the terms of the Eclipse Public License 2.0 +# which is available at https://www.eclipse.org/legal/epl-2.0/ +# +# SPDX-License-Identifier: EPL-2.0 +###################################################################### + +import pytest +from pprint import pprint + +import my_script as target + +""" +Script to test the my_script.py module. + +Use with `pytest test_my_script.py -v`. +""" + +def test_add_integers_case1(): + """ Tests add_integer. """ + assert target.add_ints(2, 3) == 5 + assert target.add_ints(6, 0) == 6 + assert target.add_ints(0, 7) == 7 + + + + +