From de5a3347ffbea243ef0a9e534d5c5c47031e1814 Mon Sep 17 00:00:00 2001 From: Kodi Arfer <Kodiologist@users.noreply.github.com> Date: Mon, 20 Nov 2023 03:09:35 -0500 Subject: [PATCH 1/4] Test and slightly extend the Python command-line interface (#447) * Add a `--times` argument to the Python CLI I meant to include this previously, but I forgot. * Test the Python command-line interface --- src/python/code/gnoll/__main__.py | 35 ++++++++++++++++++++++--------- tests/python/test_cli.py | 20 ++++++++++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 tests/python/test_cli.py diff --git a/src/python/code/gnoll/__main__.py b/src/python/code/gnoll/__main__.py index 3f3e83e4a..7ae6b78b5 100644 --- a/src/python/code/gnoll/__main__.py +++ b/src/python/code/gnoll/__main__.py @@ -34,6 +34,14 @@ def parse_cmdline_args(args): action='store_true', help='show a breakdown into individual dice' ) + g.add_argument( + '-n', + '--times', + metavar='N', + type=int, + default=1, + help='execute the entire expression N times' + ) g.add_argument( '--no-builtins', action='store_true', @@ -71,22 +79,29 @@ def parse_cmdline_args(args): return a -def main(EXPR, no_builtins, **kwargs): +def main(EXPR, times, no_builtins, **kwargs): """ The entry point for gnoll when called via `python -m gnoll` @param EXPR - the expression + @param times - number of times to execute @param no_builtins - a flag to disable builtins @param **kwargs - other key word arguments to be passed to gnoll.roll """ - _, [[result]], breakdown = gnoll.roll( - EXPR, - builtins=not no_builtins, - **kwargs) - if breakdown: - print(breakdown[0], '-->', result) - else: - print(result) + for _ in range(times): + _, [[result]], breakdown = gnoll.roll( + EXPR, + builtins=not no_builtins, + **kwargs) + yield (breakdown[0], '-->', result) if breakdown else (result,) + + +def main_with_args(args): + """Parse the commandline args and then run `main` + @param args - the arguments from the commandline (excluding the python3 call) + """ + yield from main(**vars(parse_cmdline_args(args))) if __name__ == '__main__': - main(**vars(parse_cmdline_args(sys.argv[1:]))) + for line in main_with_args(sys.argv[1:]): + print(*line) diff --git a/tests/python/test_cli.py b/tests/python/test_cli.py new file mode 100644 index 000000000..b5c4473f2 --- /dev/null +++ b/tests/python/test_cli.py @@ -0,0 +1,20 @@ +import gnoll.__main__ + + +m = lambda *x: list(gnoll.__main__.main_with_args(x)) + + +def test_cli(): + + [[r]] = m('1d4') + assert isinstance(r, int) + + [[(die1, die2), a, r]] = m('2d4', '--breakdown') + assert all((isinstance(x, int) for x in [die1, die2, r])) + assert a == '-->' + + executions = m('4d6kh3', '+', '1', '--breakdown', '--times', '6', '--no-builtins') + assert len(executions) == 6 + for (die1, die2, die3, die4), a, r in executions: + assert all((isinstance(x, int) for x in [die1, die2, die3, die4, r])) + assert a == '-->' From 9c92fd6901272fd838dda0f3a56807a9b15503e2 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Mon, 20 Nov 2023 08:13:38 +0000 Subject: [PATCH 2/4] style: format code with Autopep8, Black, Go fmt, Gofumpt, isort and Yapf (#448) This commit fixes the style issues introduced in de5a334 according to the output from Autopep8, Black, Go fmt, Gofumpt, isort and Yapf. Details: None Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- src/python/code/gnoll/__main__.py | 104 +++++++++++++----------------- tests/python/test_cli.py | 13 ++-- 2 files changed, 51 insertions(+), 66 deletions(-) diff --git a/src/python/code/gnoll/__main__.py b/src/python/code/gnoll/__main__.py index 7ae6b78b5..c63ac4dc7 100644 --- a/src/python/code/gnoll/__main__.py +++ b/src/python/code/gnoll/__main__.py @@ -1,7 +1,8 @@ """Roll some dice with GNOLL.""" -import sys import argparse +import sys + import gnoll @@ -9,73 +10,59 @@ def parse_cmdline_args(args): """Extract values from the commandline @param args - the arguments from the commandline (excluding the python3 call) """ - p = argparse.ArgumentParser( - description=__doc__, - usage='python3 -m gnoll [options] EXPR', - add_help=False) + p = argparse.ArgumentParser(description=__doc__, + usage="python3 -m gnoll [options] EXPR", + add_help=False) p.add_argument( - 'EXPR', - nargs='+', - help='a dice expression to evaluate' - '(multiple arguments will be joined with spaces)' + "EXPR", + nargs="+", + help="a dice expression to evaluate" + "(multiple arguments will be joined with spaces)", ) - g = p.add_argument_group('main options') - g.add_argument( - '-h', - '--help', - action='help', - help='show this help message and exit' - ) + g = p.add_argument_group("main options") + g.add_argument("-h", + "--help", + action="help", + help="show this help message and exit") g.add_argument( - '-b', - '--breakdown', - action='store_true', - help='show a breakdown into individual dice' + "-b", + "--breakdown", + action="store_true", + help="show a breakdown into individual dice", ) g.add_argument( - '-n', - '--times', - metavar='N', + "-n", + "--times", + metavar="N", type=int, default=1, - help='execute the entire expression N times' - ) - g.add_argument( - '--no-builtins', - action='store_true', - help='disable built-in macros' + help="execute the entire expression N times", ) + g.add_argument("--no-builtins", + action="store_true", + help="disable built-in macros") - g = p.add_argument_group('debugging options') + g = p.add_argument_group("debugging options") + g.add_argument("-v", + "--verbose", + action="store_true", + help="enable verbosity") g.add_argument( - '-v', - '--verbose', - action='store_true', - help='enable verbosity' - ) - g.add_argument( - '--keep-temp-file', - action='store_true', - help="don't delete the created temporary file" - ) - g.add_argument( - '--mock', - metavar='TYPE', - type=int, - help='mocking type' - ) - g.add_argument( - '--mock-const', - metavar='N', - type=int, - default=3, - help='mocking constant' + "--keep-temp-file", + action="store_true", + help="don't delete the created temporary file", ) + g.add_argument("--mock", metavar="TYPE", type=int, help="mocking type") + g.add_argument("--mock-const", + metavar="N", + type=int, + default=3, + help="mocking constant") a = p.parse_args(args) - a.EXPR = ' '.join(a.EXPR) + a.EXPR = " ".join(a.EXPR) return a @@ -88,11 +75,10 @@ def main(EXPR, times, no_builtins, **kwargs): @param **kwargs - other key word arguments to be passed to gnoll.roll """ for _ in range(times): - _, [[result]], breakdown = gnoll.roll( - EXPR, - builtins=not no_builtins, - **kwargs) - yield (breakdown[0], '-->', result) if breakdown else (result,) + _, [[result]], breakdown = gnoll.roll(EXPR, + builtins=not no_builtins, + **kwargs) + yield (breakdown[0], "-->", result) if breakdown else (result, ) def main_with_args(args): @@ -102,6 +88,6 @@ def main_with_args(args): yield from main(**vars(parse_cmdline_args(args))) -if __name__ == '__main__': +if __name__ == "__main__": for line in main_with_args(sys.argv[1:]): print(*line) diff --git a/tests/python/test_cli.py b/tests/python/test_cli.py index b5c4473f2..5833c3863 100644 --- a/tests/python/test_cli.py +++ b/tests/python/test_cli.py @@ -1,20 +1,19 @@ import gnoll.__main__ - m = lambda *x: list(gnoll.__main__.main_with_args(x)) def test_cli(): - - [[r]] = m('1d4') + [[r]] = m("1d4") assert isinstance(r, int) - [[(die1, die2), a, r]] = m('2d4', '--breakdown') + [[(die1, die2), a, r]] = m("2d4", "--breakdown") assert all((isinstance(x, int) for x in [die1, die2, r])) - assert a == '-->' + assert a == "-->" - executions = m('4d6kh3', '+', '1', '--breakdown', '--times', '6', '--no-builtins') + executions = m("4d6kh3", "+", "1", "--breakdown", "--times", "6", + "--no-builtins") assert len(executions) == 6 for (die1, die2, die3, die4), a, r in executions: assert all((isinstance(x, int) for x in [die1, die2, die3, die4, r])) - assert a == '-->' + assert a == "-->" From 4fb9d3604330c5f24c1201cc38fa1ff46a73e3e2 Mon Sep 17 00:00:00 2001 From: Ian Hunter <ianfhunter@gmail.com> Date: Sat, 25 Nov 2023 08:57:59 +0000 Subject: [PATCH 3/4] Update publish_PyPi.yml Signed-off-by: Ian Hunter <ianfhunter@gmail.com> --- .github/workflows/publish_PyPi.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/publish_PyPi.yml b/.github/workflows/publish_PyPi.yml index f23ed6854..1ce9fc8dc 100644 --- a/.github/workflows/publish_PyPi.yml +++ b/.github/workflows/publish_PyPi.yml @@ -26,6 +26,7 @@ jobs: python-version: '3.x' - name: Install dependencies run: | + sudo apt-get install -y python3-setuptools python -m pip install --upgrade pip pip install wheel make python From 370021d82a32c93b84487a0ecad01ff8fdaedf19 Mon Sep 17 00:00:00 2001 From: Ian Hunter <ianfhunter@gmail.com> Date: Sat, 25 Nov 2023 09:03:03 +0000 Subject: [PATCH 4/4] Update publish_PyPi.yml Signed-off-by: Ian Hunter <ianfhunter@gmail.com> --- .github/workflows/publish_PyPi.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish_PyPi.yml b/.github/workflows/publish_PyPi.yml index 1ce9fc8dc..fd90a39db 100644 --- a/.github/workflows/publish_PyPi.yml +++ b/.github/workflows/publish_PyPi.yml @@ -26,9 +26,8 @@ jobs: python-version: '3.x' - name: Install dependencies run: | - sudo apt-get install -y python3-setuptools python -m pip install --upgrade pip - pip install wheel + pip install wheel setuptools make python ls src/python echo "-------" @@ -99,7 +98,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install wheel twine + pip install wheel twine setuptools make python ls src/python echo "-------"