Newer
Older
# python-template
[](https://gitlab.cc-asp.fraunhofer.de/babu/python-template/-/commits/main)
[](https://gitlab.cc-asp.fraunhofer.de/babu/python-template/-/commits/main)
[](https://gitlab.cc-asp.fraunhofer.de/babu/python-template/-/releases)
This is a template for a project with the following features:
- Setuptools for Python project
- Sphinx documentation
- Pytest unit tests with coverage
- Pylint code quality checks
- Type checking with mypy
- Black code formatting
- Gitlab CI/CD
## Documentation 📖
Auto generated documentation can be found _[here](http://python-template-babu-5d849e1f859ea5533ef4f19b88fd2c5db39a37f7ed.pages.fraunhofer.de/)_. :point_left:
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
## Setup Sphinx
Sphinx is a documentation generator that can be used to generate documentation from docstrings in the code.
**All commands are to be run from the project directory.**
<details>
<summary>Setup Sphinx for a New Project</summary>
1. Delete the existing `docs` directory if it exists.
```bash
rm -rf docs
```
1. Create Sphinx Project
We will use the `sphinx-quickstart` command to create a new Sphinx project.
The files will be created in the `docs` directory.
```bash
sphinx-quickstart docs
```
1. Edit `conf.py`
Edit the `docs/conf.py` file to add the following lines to the top of the file:
```python
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
```
Add the following lines to the `extensions` list:
```python
extensions = [
"sphinx.ext.autodoc", # for autodoc
"sphinx.ext.autosummary", # for autosummary
"sphinx.ext.viewcode", # for source code
"sphinx.ext.napoleon", # for google style docstrings
"sphinx_autodoc_typehints", # for type hints
"sphinx_copybutton", # for copy button
"sphinx-prompt", # for prompt
"recommonmark", # for markdown
]
```
Change the theme to `sphinx_rtd_theme` or another theme of your choice if you prefer:
```python
html_theme = "sphinx_rtd_theme"
```
Add the following lines to the end of the file:
```python
# generate autosummary even if no references
autosummary_generate = True
autosummary_imported_members = True
```
1. Generate Documentation
Run the following command to generate the documentation:
```bash
sphinx-apidoc -f -e -M -o docs/ sample_project_ivi/
```
The above command will generate the documentation for the `sample_project_ivi` package. There will be a `modules.rst` file in the output directory. This file will be used to generate the table of contents for the documentation.
1. Edit `index.rst`
Add modules to the table of contents by adding the following lines to the `docs/index.rst` file:
```rst
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
```
1. Build Documentation
Run the following command to build the documentation:
```bash
# for html documentation
sphinx-build -b html docs docs/_build
# for pdf documentation (requires latex to be configured)
sphinx-build -M latexpdf docs docs/_build
```
The documentation will be generated in the `docs/_build` directory.
</details>
## Setuptools
<details>
<summary>Setup Setuptools for a New Project</summary>
- Use **setuptools** to package your code.
- Setuptools lets you easily download, build, install, upgrade, and uninstall Python packages.
- Setuptools can be configured using the following files:
- `setup.py`
- `setup.cfg`
- Setuptools lets you install your code using pip.
```bash
pip install git+REPO_URL
# or
pip install .
# or in editable mode
pip install -e .
```
- You can also save configurations for your project in the `setup.cfg` file.
- Sample `setup.py` file:
```python
from setuptools import setup, find_packages
setup(
name="project_name",
version="0.1.0",
description="Project description",
author="Author name",
author_email="Author email",
url="Project url",
license="License name",
# find_packages() finds all the packages in the src directory
# package_dir={"": "src"} can be used to specify the source directory
packages=find_packages(),
# package_data={"": ["data/*.txt"]} can be used to include data files
# the following command will include all txt files in the data directory
# hence hardcoded paths in the code can be avoided and the code can be made more portable
package_data={"src": ["data/*.txt"]},
# install_requires can be used to specify the dependencies of the project
# will be installed automatically when the project is installed
install_requires=[
"package1",
"package2",
],
# extras_require can be used to specify the dependencies of the project
# will not be installed automatically when the project is installed
extras_require={
"dev": [
"package3",
"package4",
],
},
# entry_points can be used to specify the command line scripts
entry_points={
"console_scripts": [
"script_name=package.module:main",
],
},
)
```
- Check the [Setuptools documentation](https://setuptools.pypa.io/) for more information.
</details>
## Unit testing with Pytest
<details>
<summary>Setup Pytest for a New Project</summary>
- Use **pytest** to write and run tests.
- Unittesting makes sure that your code works as expected.
- Pytest can automatically find and run tests in files named `test_*.py` or `*_test.py`.
- Pytest can be run using the following command:
```bash
pytest test_file.py
# or
python -m pytest test_file.py
# or to run all tests in a directory 'tests'
pytest tests/
```
- Here is a sample test file:
```python
import pytest
def test_function():
assert 1 == 1
```
- Some sample tests can be found in the [tests](https://gitlab.cc-asp.fraunhofer.de/babu/python-template/-/tree/main/tests).
- Check the [Pytest documentation](https://docs.pytest.org/) for more information.
- Use **coverage** to check the test coverage of your code.
- Coverage identifies which parts of your code are executed during testing and reports can be generated that show which parts of your code are missed by the tests.
- Coverage can be run using the following command:
```bash
coverage run -m pytest file.py
coverage report
```
- Check the [Coverage documentation](https://coverage.readthedocs.io/) for more information.
- **tox** can be used to run all the above tools in one command.
- Tox creates a virtual environment for each Python version you want to test.
- Tox can be run using the following command:
```bash
tox
```
- Configuration for tox can be specified in the following files:
- `tox.ini`
- `pyproject.toml`
- `setup.cfg`
- A sample tox configuration file:
```ini
[tox]
envlist = py{38,311} # specify the python versions to test
[testenv]
deps =
pytest
coverage
commands =
pytest
coverage run -m pytest
coverage report
```
- Check the [Tox documentation](https://tox.readthedocs.io/) for more information.
- Sample tox configuration files can be found in the [tox](https://gitlab.cc-asp.fraunhofer.de/babu/python-template/-/blob/main/tox.ini) directory.
</details>
## Code Quality Checks
<details>
<summary>Setup Code Quality Checks for a New Project</summary>
### Pylint
- Use **linters** to check your code for errors and style.
- Pylint is one of the tools that can be used to lint your code.
- [flake8](https://flake8.pycqa.org/) is another popular tool that can be used to lint your code.
- Code can be linted using the following command:
```bash
# recommended (pylint and flake8)
pylint file.py
# or
flake8 file.py
```
- Pylint can provide you with a score for your code. This score can be used to track the quality of your code and also be used to enforce a minimum score. In the template repository, the minimum score is set to 9.0. If the score is below 9.0, the CI pipeline will fail. This is done with the following command:
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
pylint --fail-under=9 uam
```
- It can be used to enforce a coding style, find bugs and unused code.
- Check the [Pylint documentation](https://pylint.pycqa.org/en/stable/index.html) for more information.
- [ruff](https://github.com/charliermarsh/ruff) can also be used to lint your code. ruff is based on rust is much faster than pylint and flake8.
- Example with pylint:
```python
# Example code test.py
def calculate_average(numbers):
total = sum(numbers)
number_of_items = len(numbers)
average = total / len(numbers)
print("The average is:", average)
numbers = [1, 2, 3, 4, 5]
calculate_average(numbers)
```
```bash
pylint test.py
# Output
************* Module example
test.py:10:0: C0304: Final newline missing (missing-final-newline)
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:2:0: C0116: Missing function or method docstring (missing-function-docstring)
test.py:2:22: W0621: Redefining name 'numbers' from outer scope (line 9) (redefined-outer-name)
test.py:4:4: W0612: Unused variable 'number_of_items' (unused-variable)
------------------------------------------------------------------
Your code has been rated at 2.86/10 (previous run: 4.29/10, -1.43)
```
The same suggestions can be seen in VSCode as well once the extensions are installed.
### Mypy
- Use **type hints** to specify the type of variables and arguments.
- Type hints are optional in Python, but they are recommended.
- Type hints can be very useful for documentation and debugging.
- Type hints are specified using the following syntax:
```python
variable: type
def function(argument: type) -> type:
pass
class Class:
def method(self, argument: type) -> type:
pass
```
- Check the [PEP 484](https://www.python.org/dev/peps/pep-0484/) for more information.
- Tools like [mypy](http://mypy-lang.org/) can be used to check your code for type errors.
```bash
mypy file.py
mypy src/
```
- It is a static type checker that can identify type errors in your code.
```python
def function(argument: int) -> int:
return argument + "1"
```
```bash
mypy file.py
error: Unsupported operand types for + ("int" and "str")
```
- Check the [Mypy documentation](http://mypy-lang.org/) for more information.
- Example 1
```python
# Example code test_1.py
def add_numbers(a: int, b: int) -> int:
return a + b
result = add_numbers(5, "10") # Type mismatch: 'str' cannot be added to 'int'
print(result)
```
```bash
mypy test_1.py
# Output
test.py:5: error: Argument 2 to "add_numbers" has incompatible type "str"; expected "int" [arg-type]
Found 1 error in 1 file (checked 1 source file)
```
- Example 2
```python
# Example code test_2.py
import numpy as np
def calculate_mean(data: np.ndarray) -> float:
return np.mean(data)
numbers = [1, 2, 3, 4, 5] # Incorrect type: List[int] instead of np.ndarray
mean = calculate_mean(numbers)
print(mean)
```
```bash
mypy test_2.py
# Output
test.py:8: error: Argument 1 to "calculate_mean" has incompatible type "List[int]"; expected "ndarray[Any, Any]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
```
- **You need to provide type hints for your code to be checked by mypy. Else it will not be able to check the type of the variables.**
- Mypy will also check for type errors in third-party libraries.
### Black
- Use **black** to format your code.
- Black lets you format your code in a consistent way.
- Black can be run using the following command:
```bash
black --check --diff file.py # will show the changes that will be made without making them
# or
black file.py # will format the file in place
```
- It can be integrated with your editor to format your code on save.
- Check the [Black documentation](https://black.readthedocs.io/) for more information.
- Example:
```python
# before
def my_function():
x=1
y = 2
z=3
if x>y:
print("x is greater than y")
else:
print("y is greater than or equal to x")
```
<div align="center">
<font size="5">↓</font>
</div>
```python
# after
def my_function():
x = 1
y = 2
z = 3
if x > y:
print("x is greater than y")
else:
print("y is greater than or equal to x")
```
- Use **isort** to sort your imports.
- Isort does sort import in the following order:
- standard library imports
- related third party imports
- local application/library specific imports
- Isort can be run using the following command:
```bash
isort --check-only --diff file.py # will show the changes that will be made without making them
# or
isort file.py # will format the file in place
```
- It can also be integrated with your editor to sort your imports on save.
- Here is an example of how isort sorts imports:
```python
# before
import os
import sys
import django
import requests
import uam
```
<div align="center">
<font size="5">↓</font>
</div>
```python
# after
import os
import sys
import django
import requests
import uam
```
- Check the [Isort documentation](https://pycqa.github.io/isort/) for more information.
</details>