%title: Plug-ins: Adding Flexibility to Your Apps %author: PyCon 2019 @gahjelle %date: May 5, 2019
-> # Geir Arne Hjelle <-
-
Motivation
-
Example: Plotter app
- Different data formats
- Choosing what to plot
- More plotting styles
-
Plug-ins
Modularized code:
- is less complex ^
- is easier to maintain and test ^
- is easier to extend ^
With plug-ins, your app:
- can be controlled by configuration settings ^
- can be extended and customized for and by users ^
- can be better structured, and developed faster
We'll build a simple plotter app: ^
- Command line application
- Read data from a CSV file
- Plot data in a simple line graph
A decorator is a function that wraps another function:
decorated = decorator(original)
-
Decorators are typically used to add some common functionality across many functions or methods ^
-
In most cases, decorators are applied using @-syntax:
@click.command() def main(file_path): ... ^
-> realpython.com/primer-on-python-decorators <-
^
Let's expand our plotter app: ^
- Support more file formats, like JSON
- Support more kinds of plots
- Control which data to plot
A simple decorator based plug-in architecture for Python
$ pip install pyplugs
Three levels:
- Plug-in packages: Directories containing files with plug-ins
- Plug-ins: Modules containing registered functions or classes
- Plug-in funcs: Several registered functions in the same file
Example use cases for plug-ins:
- Readers for different file formats
- Models for flexible calculations
- Filters for filtering your data
- Writers for storing your data in different formats
- Notifiers for sending you data to different devices
- Components for building your GUI
-> # Thank You For Your Attention <-
^
-> - Me: @gahjelle <- -> - Code: github.com/gahjelle/talks <- -> - PyPlugs: pyplugs.readthedocs.io <- -> - Real Python: realpython.com <-