Skip to content

Commit cbaa077

Browse files
committed
Restructure and update README-md
1 parent c1c2cdc commit cbaa077

File tree

15 files changed

+112
-81
lines changed

15 files changed

+112
-81
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
strategy:
2323
matrix:
2424
os: [ubuntu-latest, windows-latest]
25-
python-version: ["3.7", "3.8", "3.9", "3.10"]
25+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
2626

2727
runs-on: ${{ matrix.os }}
2828

.vscode/settings.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"[python]": {
3+
"editor.detectIndentation": false,
4+
"editor.formatOnSave": true,
5+
"editor.formatOnPaste": false, // not supported by black
6+
"editor.insertSpaces": true,
7+
"editor.tabSize": 4
8+
},
9+
"python.analysis.diagnosticSeverityOverrides": {
10+
"reportPrivateImportUsage": "information"
11+
},
12+
"python.defaultInterpreterPath": "${env:CONDA_PREFIX}/envs/base/bin/python",
13+
"python.formatting.provider": "black",
14+
"python.linting.enabled": true,
15+
"python.linting.pylintEnabled": true,
16+
"python.linting.pylintArgs": ["--indent-string=' '"],
17+
"python.testing.pytestArgs": [],
18+
"python.testing.unittestEnabled": false,
19+
"python.testing.pytestEnabled": true
20+
}

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,43 @@ Finally, some handy features of pytest you should be aware of:
6464

6565
- fixtures: common setup for multiple tests (e.g., reading file or database connection)
6666
- parametrize: multiple test cases for single function
67-
- expected fails: testing if the code handles wrong inputs (`pytest.raises(Exception)` or `@pytest.mark.xfail`)
67+
- expected fails: testing if the code handles wrong inputs (`with pytest.raises(Exception): ...` or `@pytest.mark.xfail`)
68+
- check for [test pollution](https://github.com/asottile/detect-test-pollution) by randomizing the order of tests ([pytest-plugin](https://pypi.org/project/pytest-random-order/))
6869

6970
<br>
7071

71-
Further links:
72+
## Setup files and Packaging
7273

73-
- check for [test pollution](https://github.com/asottile/detect-test-pollution) by randomizing the order of tests ([pytest-plugin](https://pypi.org/project/pytest-random-order/))
74+
Packaging is done with [`setuptools`](https://setuptools.pypa.io/en/latest/index.html), which is configured through the `pyproject.toml` and/or `setup.cfg`/`setup.py` files.
7475

75-
<br>
76+
<details><summary>`pyproject.toml` vs. `setup.cfg` vs `setup.py`</summary>
7677

77-
## Setup files and Packaging
78+
The `setup.py` file is a Python script and configuration is passed through keyword arguments of `setuptools.setup()`. This is not recommended due to possible security and parsing issues. The same setup can be accomplished in a declarative style within `setup.cfg` and `setup.py` remains mostly empty only calling `setuptools.setup()`.
79+
The `pyproject.toml` file aims to unify configuration files including various tools like black or pytest. For packaging, it is very similar to `setup.cfg`. However, `pyproject.toml` has not been adopted as the default yet and many projects still use `setup.cfg` to declare the packaging setup. Note that `setup.py` is not necessary if a `pyproject.toml` is present.
7880

79-
...
81+
</details>
8082

81-
Additional points:
82-
- `setup.py` is not necessary if `pyproject.toml` is present
83+
#### `pyproject.toml`
84+
85+
- minimal build specification to use with setuptools
86+
- configuration of other tools (black, pytest, mypy, ...)
87+
88+
[](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#using-a-src-layout)
89+
90+
#### [`setup.cfg`](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html)
91+
92+
- declarative configuration for setuptools
93+
- [_metadata_](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#metadata): must at least contain _name_ and _version_
94+
- [_options_](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#options): package discovery, dependencies
95+
- [additional setup](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#using-a-src-layout) required for `src/` layout
96+
- [_options.extras_require_](https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies): optional dependencies (dev tools, docs, ...)
97+
- [_options.package_data_](https://setuptools.pypa.io/en/latest/userguide/datafiles.html#package-data): inclusion of other, non-Python files (marker files, data, ...)
98+
- alternative: `MANIFEST.in`
99+
- [_options.entry_points_](https://setuptools.pypa.io/en/latest/userguide/entry_point.html): entry point for command line interface
100+
101+
<br>
83102

103+
The package can be installed with `pip install .` or something like `pip install .[dev]` to also install additional dependencies specified in `setup.cfg`'s _options.extras_require_. Pass the `-e` flag for editable mode, which loads the package from the source directory, i.e., changing the source code does not require a new installation.
84104

85105
<br>
86106

environment.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ channels:
33
- defaults
44
- conda-forge
55
dependencies:
6+
- black
67
- numpy
8+
- pre-commit
9+
- pylint
10+
- pytest
11+
- pytest-cov
12+
- tox

setup.cfg

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ package_dir =
2424

2525
[options.packages.find]
2626
where = src
27-
exclude = test
27+
exclude =
28+
test*
29+
docs*
30+
examples*
2831

2932
[options.entry_points]
3033
console_scripts =
31-
squarer = squarer:main
34+
squarer = squarer.cli:console_entry_point
3235

3336
[options.extras_require]
3437
dev =
@@ -39,3 +42,7 @@ dev =
3942
pytest
4043
pytest-cov
4144
tox
45+
46+
[options.package_data]
47+
squarer =
48+
py.typed

src/squarer/__init__.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
11
"""
2-
Entry point for command line interface.
3-
"""
4-
5-
from __future__ import annotations
2+
Squarer
3+
=======
64
7-
import argparse
8-
import sys
9-
from collections.abc import Sequence
5+
Dummy command line tool to square a number.
6+
"""
107

118
from .__version__ import __version__
9+
from .cli import console_entry_point
1210
from .maths import square_a_number as square
13-
14-
__all__ = ["__version__", "maths", "square"]
15-
16-
17-
def main(argv: Sequence[str] | None = None) -> int:
18-
# get command line argument
19-
parser = argparse.ArgumentParser()
20-
parser.add_argument("number", type=float, help="Number to square.")
21-
args = parser.parse_args(argv)
22-
23-
# print result
24-
print(square(args.number))
25-
26-
return 0
27-
28-
29-
if __name__ == "__main__":
30-
sys.exit(main())

src/squarer/__main__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
Entry point for command line interface via `python -m <prog>`.
33
"""
44

5-
import sys
6-
7-
from . import main
5+
from .cli import console_entry_point
86

97
if __name__ == "__main__":
10-
sys.exit(main())
8+
raise SystemExit(console_entry_point())

src/squarer/cli/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Command line interface
3+
======================
4+
5+
This module contains functionality for the CLI.
6+
"""
7+
8+
from .entrypoint import console_entry_point

src/squarer/cli/entrypoint.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Entrypoint for command line interface.
3+
"""
4+
5+
from __future__ import annotations
6+
7+
import argparse
8+
from collections.abc import Sequence
9+
10+
from ..maths import square_a_number as square
11+
12+
13+
def console_entry_point(argv: Sequence[str] | None = None) -> int:
14+
# get command line argument
15+
parser = argparse.ArgumentParser()
16+
parser.add_argument("number", type=float, help="Number to square.")
17+
args = parser.parse_args(argv)
18+
19+
# print result
20+
print(square(args.number))
21+
22+
return 0

src/squarer/maths/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
"""
2+
Math
3+
====
4+
5+
This module contains all mathematical functions.
6+
"""
7+
18
from .calc import square_a_number

0 commit comments

Comments
 (0)