🎨 Decorators
Decorators to assist you in adjusting functions' behavior.
narrow_return
This decorator modifies a function which returns multiple elements to only return one, at a specified index of the returned tuple.
Consider plot_series
function
from the sktime
library, which returns two elements:
- A
matplotlib
figure:plt.Figure
- An axis object:
plt.Axis
original_workflow.py
from sktime.utils.plotting import plot_series
from sktime.datasets import load_airline
y = load_airline()
fig, ax = plot_series(y)
Oftentimes you only care about the former.
You can adjust this function to only return a fig
:
narrow_return.py
from sktime.utils.plotting import plot_series
from sktime.datasets import load_airline
from utilsx import narrow_return
plot_series_to_figure = narrow_return(0)(plot_series)
y = load_airline()
fig = plot_series_to_figure(y)
This can help while working with orchestration frameworks like Kedro, where unused elements in the returned tuple clutter the directed acyclic graph.