index
execution_count
cell_type
toolbar
started_at
completed_at
source
loc
metadata
outputs
1
unexecuted
In
[
]
markdown
# fumbling around with `pluggy`
https://pluggy.readthedocs.io/
* register/unregister plugins
* define specs and implementations at the same time.
metadata
6
https://pluggy.readthedocs.io/
register/unregister plugins
define specs and implementations at the same time.
2
executed
In
[
1
]
code
metadata
1
0 outputs.
Out
[
1
]
3
unexecuted
In
[
]
markdown
we're going to think about a sample `PROG` ram that has an interface.
metadata
1
we're going to think about a sample
PROG
ram that has an interface.
4
executed
In
[
2
]
code
metadata
1
0 outputs.
Out
[
2
]
5
unexecuted
In
[
]
markdown
the `specification` decorates the functions and signatures of our interface.
the `implementation` uses the `specification` for consistency when it actually defines computational work.
metadata
2
the
specification
decorates the functions and signatures of our interface.
the
implementation
uses the
specification
for consistency when it actually defines computational work.
6
executed
In
[
3
]
code
implementation , specification = pluggy . HookimplMarker ( PROG ), pluggy . HookspecMarker ( PROG )
metadata
1
0 outputs.
Out
[
3
]
7
unexecuted
In
[
]
markdown
`PROG` s `specification` for `my_plugin` provides a default interface.
metadata
1
PROG
s
specification
for
my_plugin
provides a default interface.
8
executed
In
[
4
]
code
@implementation
@specification ( firstresult = False )
def my_plugin ( a , b , c ): return "" . join ( map ( str , ( a , b , c )))
metadata
3
0 outputs.
Out
[
4
]
9
unexecuted
In
[
]
markdown
we register the `specifaction` onto a plugin `manager`
metadata
1
we register the
specifaction
onto a plugin
manager
10
executed
In
[
5
]
code
manager = pluggy . PluginManager ( PROG )
metadata
1
0 outputs.
Out
[
5
]
11
unexecuted
In
[
]
markdown
our `specification` lives in the `import __main__` or `MAIN` namespace.
metadata
1
our
specification
lives in the
import __main__
or
MAIN
namespace.
12
executed
In
[
6
]
code
manager . add_hookspecs ( __main__ := __import__ ( __name__ ))
assert "my_plugin" in dir ( manager . hook ), "the plugin is registered"
metadata
2
0 outputs.
Out
[
6
]
13
unexecuted
In
[
]
markdown
now we can add our `implementation` of `my_plugin` in the `__main__` module
metadata
1
now we can add our
implementation
of
my_plugin
in the
__main__
module
14
executed
In
[
7
]
code
manager . register ( __main__ );
metadata
1
0 outputs.
Out
[
7
]
15
unexecuted
In
[
]
markdown
now we can execute our `manager.hook.my_plugin` method
metadata
1
now we can execute our
manager.hook.my_plugin
method
16
executed
In
[
8
]
code
manager . hook . my_plugin ( a = 10 , b = 20 , c = 30 )
metadata
1
1 outputs.
Out
[
8
]
17
unexecuted
In
[
]
markdown
let's add another implementation. we can't name it `my_plugin` otherwise we'll lose the scope of our previous method. here we name our method whatever we want and explicitly define the `implementation:specname` it refers to.
metadata
1
let's add another implementation. we can't name it
my_plugin
otherwise we'll lose the scope of our previous method. here we name our method whatever we want and explicitly define the
implementation:specname
it refers to.
18
executed
In
[
9
]
code
@implementation ( specname = "my_plugin" )
def another_plugin ( a , b ): return ( a , b )
metadata
2
0 outputs.
Out
[
9
]
19
unexecuted
In
[
]
markdown
now seems like a good time test what `async` functions do
metadata
1
now seems like a good time test what
async
functions do
20
executed
In
[
10
]
code
@implementation ( specname = "my_plugin" , trylast = True )
async def async_my_plugin ( a , b ): return ( a , b )
metadata
2
0 outputs.
Out
[
10
]
21
unexecuted
In
[
]
markdown
reregistering the `__main__` gives an error because `pluggy` doesn't allow someone register the module twice
metadata
1
reregistering the
__main__
gives an error because
pluggy
doesn't allow someone register the module twice
22
executed
In
[
11
]
code
try : manager . register ( __main__ ); assert False , "can't register the module twice"
except ValueError : assert True
metadata
2
0 outputs.
Out
[
11
]
23
unexecuted
In
[
]
markdown
the proper registration requires we unregister the module first.
metadata
1
the proper registration requires we unregister the module first.
24
executed
In
[
12
]
code
manager . unregister ( __main__ )
manager . register ( __main__ );
metadata
2
0 outputs.
Out
[
12
]
25
unexecuted
In
[
]
markdown
now our invocation finds both implementations.
metadata
1
now our invocation finds both implementations.
26
executed
In
[
13
]
code
( results := manager . hook . my_plugin ( a = 10 , b = 20 , c = 30 ))
metadata
1
1 outputs.
Out
[
13
]
['102030', (10, 20), <coroutine object async_my_plugin at 0x7feab0168040>]
27
unexecuted
In
[
]
markdown
`async_my_plugin` evaluates to a coroutine that we'd have to handle properly.
metadata
1
async_my_plugin
evaluates to a coroutine that we'd have to handle properly.
28
executed
In
[
14
]
code
assert __import__ ( "inspect" ) . iscoroutine ( results [ - 1 ])
metadata
1
0 outputs.
Out
[
14
]