Skip to content

Commit

Permalink
Add package with changes from chapter 4 (#1)
Browse files Browse the repository at this point in the history
* Add package with changes from chapter 4

* Use Python syntax instead of Cython
  • Loading branch information
daneah authored Sep 17, 2021
1 parent f4057a9 commit c54772f
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Compiled extensions
*.c
7 changes: 7 additions & 0 deletions ch04/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 ch04/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 ch04/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 ch04/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"
30 changes: 30 additions & 0 deletions ch04/first-python-package/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[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
7 changes: 7 additions & 0 deletions ch04/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 ch04/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 ch04/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.

0 comments on commit c54772f

Please sign in to comment.