Skip to content

Commit

Permalink
Add chapter 5 code companion (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
daneah authored Nov 21, 2021
1 parent 92f97f9 commit cf1092f
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ch05/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 ch05/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 ch05/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
```
3 changes: 3 additions & 0 deletions ch05/first-python-package/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel", "cython"]
build-backend = "setuptools.build_meta"
63 changes: 63 additions & 0 deletions ch05/first-python-package/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[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 #
######################

[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}
7 changes: 7 additions & 0 deletions ch05/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 ch05/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)
21 changes: 21 additions & 0 deletions ch05/first-python-package/src/imppkg/harmony.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys

from termcolor import cprint

from imppkg.harmonic_mean import harmonic_mean


def main():
result = 0.0

try:
nums = [float(num) for num in sys.argv[1:]]
except ValueError:
nums = []

try:
result = harmonic_mean(nums)
except ZeroDivisionError:
pass

cprint(result, 'red', 'on_cyan', attrs=['bold'])
Empty file.
32 changes: 32 additions & 0 deletions ch05/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 cf1092f

Please sign in to comment.