Overview
Build the pattern behind Flask routes and pytest fixtures: a decorator that registers functions, and a generator that runs them lazily.
- Decorator factories (decorators with arguments)
- Registering functions in a dict
- Generators yielding tuples
Short Course
A decorator with arguments is a function that returns a decorator. Use it
to record functions in a registry as they're defined, like @app.route("/")
in Flask.
HANDLERS = {}
def on(event): # decorator factory
def register(func): # the actual decorator
HANDLERS[event] = func
return func # keep the function usable
return register
@on("start")
def hello():
return "hi"
HANDLERS["start"]() # 'hi'
Task
Write plugin(name), a decorator that stores the function in PLUGINS under
name and returns it unchanged, and run_all(value), a generator that yields
(name, result) for each plugin in registration order.
Test Suites
Pending
Stdout