From 076b3cb38e3e87c71ddd9fd999d012286782c65c Mon Sep 17 00:00:00 2001 From: Liam Keegan Date: Tue, 16 Jan 2024 11:00:21 +0100 Subject: [PATCH] add dice roll, tests, click cli --- calculate/README.md | 2 ++ calculate/pyproject.toml | 19 +++++++++++++++++-- calculate/src/calculate/__init__.py | 3 ++- calculate/src/calculate/cli.py | 19 +++++++++++++++++++ calculate/src/calculate/stats.py | 9 ++++++--- calculate/test/__init__.py | 0 calculate/test/test_stats.py | 17 +++++++++++++++++ 7 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 calculate/src/calculate/cli.py create mode 100644 calculate/test/__init__.py create mode 100644 calculate/test/test_stats.py diff --git a/calculate/README.md b/calculate/README.md index 4db68e4..521ccca 100644 --- a/calculate/README.md +++ b/calculate/README.md @@ -12,4 +12,6 @@ A simple example of a Python package. from calculate import stats print(stats.flip_coin()) + +print(stats.roll_dice(n_dice=2, n_sides=12)) ``` diff --git a/calculate/pyproject.toml b/calculate/pyproject.toml index 8fe796e..61f55f2 100644 --- a/calculate/pyproject.toml +++ b/calculate/pyproject.toml @@ -5,7 +5,22 @@ build-backend = "hatchling.build" [project] name = "calculate" version = "0.0.1" -dependencies = ["click"] +dependencies = ["click", "numpy"] +authors = [ + { name="Liam Keegan", email="liam@keegan.ch" }, +] +description = "A simple package to flip a coin or roll dice" +readme = "README.md" +requires-python = ">=3.8" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", +] + +[project.optional-dependencies] +test = ["pytest"] [project.scripts] -coinflip = "calculate.stats:flip_coin" +flip-coin = "calculate.cli:flip_coin_cli" +roll-dice = "calculate.cli:roll_dice_cli" diff --git a/calculate/src/calculate/__init__.py b/calculate/src/calculate/__init__.py index f6d6b4d..90c53af 100644 --- a/calculate/src/calculate/__init__.py +++ b/calculate/src/calculate/__init__.py @@ -1 +1,2 @@ -import stats +from .stats import flip_coin +from .stats import roll_dice diff --git a/calculate/src/calculate/cli.py b/calculate/src/calculate/cli.py new file mode 100644 index 0000000..9ea5322 --- /dev/null +++ b/calculate/src/calculate/cli.py @@ -0,0 +1,19 @@ +import click +from .stats import flip_coin +from .stats import roll_dice + + +@click.command() +def flip_coin_cli(): + click.echo(flip_coin()) + + +@click.command() +@click.option( + "-d", "--n-dice", default=1, show_default=True, help="Number of dice to roll" +) +@click.option("-s", "--n-sides", default=6, show_default=True, help="Number of sides") +def roll_dice_cli(n_dice, n_sides): + click.echo("") + for d in roll_dice(n_dice, n_sides): + click.secho(f" {d} \n", bold=True, bg="red") diff --git a/calculate/src/calculate/stats.py b/calculate/src/calculate/stats.py index 85a83c1..cf4c362 100644 --- a/calculate/src/calculate/stats.py +++ b/calculate/src/calculate/stats.py @@ -1,7 +1,10 @@ import random -import click +import numpy as np -@click.command() -def flip_coin(): +def flip_coin() -> str: return random.choice(["Heads", "Tails"]) + + +def roll_dice(n_dice: int, n_sides: int): + return np.random.randint(1, n_sides, n_dice) diff --git a/calculate/test/__init__.py b/calculate/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/calculate/test/test_stats.py b/calculate/test/test_stats.py new file mode 100644 index 0000000..bc1c388 --- /dev/null +++ b/calculate/test/test_stats.py @@ -0,0 +1,17 @@ +import pytest +import numpy as np +from calculate import stats + + +def test_flip_coin(): + coin = stats.flip_coin() + assert coin in ["Heads", "Tails"] + + +@pytest.mark.parametrize("n_dice", [1, 2, 5, 17]) +@pytest.mark.parametrize("n_sides", [2, 3, 6, 20]) +def test_roll_dice(n_dice, n_sides): + dice = stats.roll_dice(n_dice, n_sides) + assert len(dice) == n_dice + assert np.all(dice >= 1) + assert np.all(dice <= n_sides)