279x Filetype PDF File size 0.33 MB Source: dbdmg.polito.it
Data Science Lab
Structuring Python projects
DataBaseand Data Mining Group Andrea Pasini, Elena Baralis
Structuring Python projects
▪ The code of a Python project is organized in
packages and modules
▪ Package:
▪ Represented with a directory in the file system
▪ Collects a set of Python modules
▪ A folder must contain a __init__.py file to be
considered a Python package
▪ Module
▪ Represented with a Python file (.py)
▪ Contain attributes, functions and class definitions
2
Structuring Python projects
▪ Example: project structure
MyProject Project main folder
algorithms Package
__init__.py Init file
utils.py Modules
classifier.py
graphics
__init__.py
charts.py
main_script.py Main script: run with command
python main_script.py
3
Structuring Python projects
▪ Example: content of Python module
graphics charts.py
__init__.py import matplotlib.pyplot as plt
charts.py import os
def plot_result():
Function ...
definition plt.plot(...)
...
plot_path = "../Data/plots"
Attribute if not os.path.exists(plot_path):
Initialization os.makedirs(plot_path)
operations
4
no reviews yet
Please Login to review.