Skip to content

Commit

Permalink
Add ch06 code companion (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
daneah authored Dec 18, 2021
1 parent cf1092f commit 9c6b086
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ch06/first-python-package/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright © 2021 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions ch06/first-python-package/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
graft src
recursive-exclude __pycache__ *.py[cod]
9 changes: 9 additions & 0 deletions ch06/first-python-package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# first-python-package

This package does amazing things.

## Installation

```shell
$ python -m pip install first-python-package
```
7 changes: 7 additions & 0 deletions ch06/first-python-package/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = ["setuptools", "wheel", "cython"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 120
target-version = ['py38', 'py39']
95 changes: 95 additions & 0 deletions ch06/first-python-package/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[metadata]
name = first-python-package
version = 0.0.1
description = This package does amazing things.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/daneah/publishing-python-packages
author = Dane Hillard
author_email = "Dane Hillard" <github@danehillard.com>
license = MIT
license_files = LICENSE
classifiers =
License :: OSI Approved :: MIT License

[options]
package_dir =
=src
packages = find:
include_package_data = True
install_requires =
termcolor>=1.1.0,<2

[options.packages.find]
where = src
exclude =
test*

[options.entry_points]
console_scripts =
harmony = imppkg.harmony:main

######################
# Tool configuration #
######################

[mypy]
python_version = 3.10
warn_unused_configs = True
show_error_context = True
pretty = True
namespace_packages = True

[flake8]
max-line-length = 120

[tool:pytest]
testpaths = test
addopts = --cov --strict-markers
xfail_strict = True

[coverage:run]
source = imppkg
branch = True

[coverage:report]
show_missing = True
skip_covered = True

[coverage:paths]
source =
src/imppkg
.venv/*/site-packages/imppkg

[tox:tox]
envlist = py39,py310
isolated_build = True

[testenv]
deps =
pytest
pytest-cov
commands =
pytest {posargs}

[testenv:typecheck]
deps =
mypy
pytest
types-termcolor
commands =
mypy --ignore-missing-imports {posargs:src test}

[testenv:format]
skip_install = True
deps =
black
commands =
black {posargs:--check --diff src test}

[testenv:lint]
skip_install = True
deps =
flake8
commands =
flake8 {posargs:src test}
7 changes: 7 additions & 0 deletions ch06/first-python-package/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from setuptools import setup

from Cython.Build import cythonize

setup(
ext_modules=cythonize("src/imppkg/harmonic_mean.pyx"),
)
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions ch06/first-python-package/src/imppkg/harmonic_mean.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def harmonic_mean(nums):
return len(nums) / sum(1 / num for num in nums)
29 changes: 29 additions & 0 deletions ch06/first-python-package/src/imppkg/harmony.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys

from termcolor import colored

from imppkg.harmonic_mean import harmonic_mean


def _parse_nums(inputs: str) -> list[float]:
try:
return [float(num) for num in inputs]
except ValueError:
return []


def _calculate_result(nums: list[float]) -> float:
try:
return harmonic_mean(nums)
except ZeroDivisionError:
return 0.0


def _format_output(result: float) -> str:
return colored(str(result), "red", "on_cyan", attrs=["bold"])


def main():
nums = _parse_nums(sys.argv[1:])
result = _calculate_result(nums)
print(_format_output(result))
Empty file.
32 changes: 32 additions & 0 deletions ch06/first-python-package/test/test_harmonic_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys

import pytest
from termcolor import colored

from imppkg.harmony import main


@pytest.mark.parametrize(
"inputs, expected",
[
(["3", "3", "3"], 3.0),
([], 0.0),
(["foo", "bar"], 0.0),
],
)
def test_harmony_parametrized(inputs, monkeypatch, capsys, expected):
monkeypatch.setattr(sys, "argv", ["harmony"] + inputs)
main()
assert capsys.readouterr().out.strip() == colored(expected, "red", "on_cyan", attrs=["bold"])


FRUITS = ["apple"]


def test_len():
assert len(FRUITS) == 1


def test_append():
FRUITS.append("banana")
assert FRUITS == ["apple", "banana"]

0 comments on commit 9c6b086

Please sign in to comment.