Introduce how to develop python package with easy framework
Developing Python Packages
Lesson 1
- reuse
- key function up to date
-
sharing
- Terms
- script : python file
- package : a directory full of python code to be imported, such as numpy
- subpackage, such as numpy.random
- Module : a python file inside a package which stores the package code
- library : a package or collection of package
- Directory tree of package
- simplemodule.py
- init.py :
- from scrip to package
Documentation
- Function
- Class
- Class method
- docstring templates
` pyment -w -o google textanalysis.py `
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def inches_to_feet(x, reverse=False):
"""Convert lengths between inches and feet.
Parameters
----------
x : numpy.ndarray
Lengths in feet.
reverse : bool, optional
If true this function converts from feet to inches
instead of the default behavior of inches to feet.
(Default value = False)
Returns
-------
numpy.ndarray
"""
if reverse:
return x * INCHES_PER_FOOT
else:
return x / INCHES_PER_FOOT
Structuring imports
1
2
3
import mysklearn.preprocessing.normalize
from mysklearn import preprocessing
1
2
3
4
5
6
7
8
"""User-facing functions."""
from impyrial.length.core import (
UNITS,
inches_to_feet,
inches_to_yards
)
Lesson 2 : install your own package
-
Package directory structure
-
setup.py
- find_package
[image:3ECF5F03-2C74-4140-9642-8F67E1DFE5B9-23475-00000614EA62826E/截圖 2021-12-20 下午11.15.26.png]
1
2
pip install -e .
dealing with dependencies
1
2
3
pip freeze > requirements.txt
pip install - r requirements.txt
Including licences and writing READMEs
- license
- to give others permission to use your code
- README
- Title
- Description
- Installation
- Usage examples
- Contributing
- License
- MANIFEST.in
- List all the extra files to include in your package distribution
- include README.md
- include LICENSE
Publishing your package
- PyPI
- Distributions
- source distribution : a distribution package which is mostly your source code
- wheel distribution : a distribution package which has been processed to make it faster to install
1
2
python setup.py sdist bdist_wheel
- upload ```python twine upload dist/*
twine upload -r testpypi dist/*
1
2
3
4
5
6
7
8
9
10
11
12
## Lesson 3 : Testing your package
* Writing tests
![](https://i.imgur.com/jiPxE8c.png)
* Organizing tests inside your package
![](https://i.imgur.com/OSHL0IY.png)
pytest
會執行所有test開頭的module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
![](https://i.imgur.com/BPigsJn.png)
### Testing your package with different environments
* Testing multiple versions of Python
* tox
* tox.ini
![](https://i.imgur.com/vALbdCL.png)
![](https://i.imgur.com/bTIJBB0.png)
![](https://i.imgur.com/0PpE2eL.png)
### keeping your package stylish
* standard python style is described in PEP8
* a style guide dictates how code should be laid out
* flake8
` flake8 features.py `
![](https://i.imgur.com/73NKpmS.png)
* breaking the rule on purpose
![](https://i.imgur.com/AfsVTH7.png)
* find/ignore specific rule
![](https://i.imgur.com/pYW9znU.png)
* setup.cfg
![](https://i.imgur.com/QULqZxU.png)
[flake8]
Ignore F401 violations in the main init.py file
per-file-ignores =
impyrial/init.py : F401
Ignore all violations in the tests directoory
exclude = tests/*
```
Lesson 4: Rapid Package Development
- Faster package development with templates
- cookiecutter ` cookiecutter https://github.com/audreyr/cookiecutter-pypackage `
version numbers and history
- CONTRIBUTING.md
- HISTORY.md
- e.g. NumPy release notes
History
0.3.0
Changed
- ….
Deprecated
- …
0.2.1
Fixed
- …
0.2.0
Added
- ….
Deprecated
- …
- …
- ….
- e.g. NumPy release notes
- Version number
- setup.py
- init.py
- bumpversion
- major
- minor
- patch
Makefiles and classifier
- Classifiers
- inside
setup.py
of mysklearn
- inside
- Makefiles
- used to automate parts of building your package
- make clean
- make test
- make dist
- used to automate parts of building your package
- Advanced:
- UT in DS
- Package website : ReadtheDocs and Sphinx