Chapter 1
Chapter 1
D E V E L O P I N G P Y T H O N PA C K A G E S
James Fulton
Climate informatics researcher
Why build a package anyway?
To make your code easier to reuse.
To avoid lots of copying and pasting.
File layout
Import structure
Making your package installable
Module - A Python file inside a package which stores the package code.
e.g. example coming in next 2 slide.
mysimplepackage/
|-- simplemodule.py
|-- __init__.py
def cool_function():
...
Empty file return cool_result
...
def another_cool_function():
...
return another_cool_result
mysklearn/
|-- __init__.py
|-- preprocessing
| |-- __init__.py
| |-- normalize.py
| |-- standardize.py
|-- regression
| |-- __init__.py
| |-- regression.py
|-- utils.py
James Fulton
Climate informatics researcher
Why include documentation?
Helps your users use your code import numpy as np
help(np.sum)
Document each
Function ...
sum(a, axis=None, dtype=None, out=None)
Class Sum of array elements over a given axis.
Function
...
Class mean(...) method of numpy.ndarray instance
a.mean(axis=None, dtype=None, out=None)
Class method
Returns the average of the array elements
along given axis.
[what is returned]
"""
Args: Parameters
arg1 (int): Description of arg1 ----------
arg2 (str): Description of arg2 arg1 : int
Description of arg1 ...
numpy
scipy
pandas
sklearn
matplotlib
dask
etc.
Parameters
----------
a : array_like
Input array or object that can be converted to an array.
Other types include - int , float , bool , str , dict , numpy.array , etc.
Raises
See Also
Notes
References
Examples
1 https://numpydoc.readthedocs.io/en/latest/format.html
Numpydoc
-w - overwrite file
Parameters
----------
filepath :
words_list :
Returns
-------
type
"""
Parameters
----------
filepath : str
Path to text file.
words_list : list of str
Count the total number of appearances of these words.
Returns
-------
Args:
filepath(str): Path to text file.
words_list(list of str): Count the total number of appearances of these words.
Returns:
"""
""" """
Linear regression for Python A subpackage for standard preprocessing operations.
============================ """
mysklearn/preprocessing/normalize.py
"""
A module for normalizing data.
"""
James Fulton
Climate informatics researcher
Without package imports
import mysklearn Directory tree for package with subpackages
mysklearn/
help(mysklearn.preprocessing)
|-- __init__.py
|-- preprocessing
Traceback (most recent call last):
| |-- __init__.py
File "<stdin>", line 1, in <module>
| |-- normalize.py
AttributeError: module 'mysklearn' has no
| |-- standardize.py
attribute 'preprocessing'
|-- regression
| |-- __init__.py
| |-- regression.py
|-- utils.py
mysklearn/
help(mysklearn.preprocessing)
|-- __init__.py
|-- preprocessing
Help on package mysklearn.preprocessing in
| |-- __init__.py
mysklearn:
| |-- normalize.py
| |-- standardize.py
NAME
|-- regression
mysklearn.preprocessing - A subpackage
| |-- __init__.py
for standard preprocessing operations.
| |-- regression.py
|-- utils.py
mysklearn/
help(mysklearn.preprocessing.normalize)
|-- __init__.py
|-- preprocessing
Traceback (most recent call last):
| |-- __init__.py
File "<stdin>", line 1, in <module>
| |-- normalize.py
AttributeError: module
| |-- standardize.py
'mysklearn.preprocessing' has no attribute
|-- regression
'normalize'
| |-- __init__.py
| |-- regression.py
|-- utils.py
mysklearn/
help(mysklearn.preprocessing.normalize)
|-- __init__.py
|-- preprocessing
Help on module mysklearn.preprocessing.normalize
| |-- __init__.py
in mysklearn.preprocessing:
| |-- normalize.py
| |-- standardize.py
NAME
|-- regression
mysklearn.preprocessing.normalize - A module
| |-- __init__.py
for normalizing data.
| |-- regression.py
|-- utils.py
help(mysklearn.preprocessing.normalize.normalize_data)
normalize_data(x)
Normalize the data array.
help(mysklearn.preprocessing.normalize_data)
normalize_data(x)
Normalize the data array.