Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example pyproject and package. #11

Merged
merged 19 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,22 @@ jobs:
shell: bash
run: |
pip3 install numpy \
pytest
pytest \
build

- name: Test
shell: bash
run: |
pytest -v .


- name: build
run: python -m build .

- name: install
shell: bash
run: |
pip install dist/*.whl
do_thing -h



2 changes: 2 additions & 0 deletions acme_corp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# make piano a top-level class
from .piano import Piano
19 changes: 19 additions & 0 deletions acme_corp/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from . import Piano

"""
This is a special file that makes this package executable.

It will only be run when this package is run from the command line, i.e.,

``python -m acme_corp``
"""


def set_trap():
piano = Piano()
piano.lift()
return piano


if __name__ == "__main__":
trap = set_trap()
44 changes: 18 additions & 26 deletions do_task.py → acme_corp/do_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

logger = logging.getLogger(__name__)

# using Sphinx docstrings style: <https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did we decide to switch to numpy docstring style?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we decided to make that a separate PR #12.


def perform_action(args, input_data):
"""Perform some action

Following the principle of separation of concerns, this method only
performs the action, but does not have any input or output.

inputs
------
args : argparse object with many possible members based on argparse configuration
input_data : dictionary of input data read from YAML file

outputs
--------
string that describes the input quantities

:param args: argparse object with many possible members based on argparse configuration
:type args: argparse.ArgumentParser
:param input_data: dictionary of input data read from YAML file
:type input_data: dict
:returns: string that describes the input quantities
:rtype: str
"""

return f"Some results based on args:\n{args}\n and input_data:\n{input_data}\n"
Expand All @@ -31,16 +29,12 @@ def report_results(args, input_data, results):
Following the principle of separation of concerns, this method only
performs output and does not do any actions.

inputs
------
args : argparse object with many possible members based on argparse configuration
input_data : dictionary of input data read from YAML file
results : the results of the action

outputs
--------
None
:param args: argparse object with many possible members based on argparse configuration
:type args: argparse.ArgumentParser
:param input_data: dictionary of input data read from YAML file
:type input_data: dict

:returns: None
"""

logger.info(
Expand All @@ -56,13 +50,8 @@ def task_args():
`filename` : required positional argument
`verbose` : optional keyword argument

inputs
-------
None

outputs
--------
argparse object with various members depending on configuration
:returns: argparse object with various members depending on configuration
:rtype: argparse.ArgumentParser
"""

parser = argparse.ArgumentParser(
Expand All @@ -86,7 +75,10 @@ def read_input(input_filename):

inputs
-------
input_filename : a string with a filename/path accessible from the current location
:param input_filename: a string with a filename/path accessible from the current location
:type input_filename: str
:returns: the data from the YAML file.
:rtype: dict
"""

with open(input_filename, "r") as yaml_file:
Expand Down
42 changes: 42 additions & 0 deletions acme_corp/piano.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Piano:
"""
A grand piano proudly built by ACME corporation.

Weighing half a ton be careful when hoisting this precariously over
sidewalks.

.. warning::
Always use proper rigging techniques when hoisting this above
above any paths that may be occupied by any Road Runners.
"""

def __init__(self):
self._weight = 1_000
# start on ground level
self._height = 0

def lift(self):
self._height = 3 # [m]

def drop(self):
pass

@property
def weight(self):
"""
The current weight of the piano.

:returns: the current weight.
:rtype: float
"""
return self._weight

@property
def height(self):
"""
The current height of the piano.

:returns: the current height.
:rtype: float
"""
return self._height
35 changes: 35 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# reference: <https://packaging.python.org/en/latest/guides/writing-pyproject-toml/>
[project]
name = "acme_corp"
description = "A package, or box, created by the Acme Corporation for all of your Road Runner hunting needs."
# use semantic versioning: <https://semver.org/>
version = "0.0.1"

readme = "README.md"
requires-python = ">=3.9"
maintainers = [{name = "G. C. Runner", email = "[email protected]"}]
authors = [{name = "G. C. Runner", email = "[email protected]"}]

keywords = ["Piano", "Anvil", "Instant Tunnel"]

# these will be install with `pip install .`
dependencies = ["pyyaml"]

# these will only be installed with `pip install .[test]`
[project.optional-dependencies]
test =["pytest"]

[project.scripts]
do_thing = "acme_corp:do_task.do_task"

# these show up on the left bar on pypi.org
[project.urls]
Homepage = "https://github.com/cnerg/py-template"

[build-system]
requires = ["setuptools>=64.0.0"]
build-backend = "setuptools.build_meta"

[tools.pytest.ini_options]
minversion = "6.0"
junit_logging = "all"
Empty file added tests/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions test_do_task.py → tests/test_do_task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import do_task
import acme_corp.do_task as do_task

def test_perform_action():
args = 'foo'
Expand All @@ -10,4 +10,4 @@ def test_perform_action():

assert exp == obs



22 changes: 22 additions & 0 deletions tests/test_piano.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import acme_corp

import pytest


@pytest.fixture
def blank_piano():
return acme_corp.Piano()


def test_piano_init(blank_piano):
assert blank_piano.weight == pytest.approx(1_000.0)
assert blank_piano.height == pytest.approx(0.0)


def test_lifting_piano(blank_piano):
blank_piano.lift()
assert blank_piano.height == pytest.approx(3)


def test_dropping_piano(blank_piano):
pass