Skip to content

Commit

Permalink
add dice roll, tests, click cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lkeegan committed Jan 16, 2024
1 parent c4c8162 commit 076b3cb
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
2 changes: 2 additions & 0 deletions calculate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
```
19 changes: 17 additions & 2 deletions calculate/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion calculate/src/calculate/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import stats
from .stats import flip_coin
from .stats import roll_dice
19 changes: 19 additions & 0 deletions calculate/src/calculate/cli.py
Original file line number Diff line number Diff line change
@@ -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")
9 changes: 6 additions & 3 deletions calculate/src/calculate/stats.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file added calculate/test/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions calculate/test/test_stats.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 076b3cb

Please sign in to comment.