Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 1.38 KB

ooze_magic.rst

File metadata and controls

43 lines (32 loc) · 1.38 KB

The @ooze.magic decorator

Overview

Ooze has the ability to inject SOME arguments into a function, allowing you to specify others. This is done using the @ooze.magic decorator. When you decorate a function with the @ooze.magic decorator, can you later call that function with only a portion of the arguments it expects. Ooze will see the attempted call and try to inject any remaining (missing) function arguments that you didn't supply. Here's a quick example:

A quick example may make it more clear:

 1 import ooze
 2 import socket
 3 
 4 ooze.provide_static('version', '1.0.0')
 5 
 6 @ooze.magic
 7 def app_title(hostname, version):
 8     print(f"App version {version} running on {hostname}")
 9 
10 app_title(socket.gethostname())

Running this script will output:

1 $ python script.py
2 App version 1.0.0 running on macbook.local
3 $

You'll notice the definition of app_title takes 2 arguments (hostname and version) but that the call made on line 13 only specifies 1 (hostname). The @ooze.magic decorator informs Ooze that it is responsible for injecting the remaining arguments into the function call. If Ooze is unable to locate a dependency that matches the argument name, it will raise an InjectionError exception.