In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use the Python pluggy module". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1 introduction to pluggy
Pluggy function: provides a simple and convenient plug-in system, which can achieve loose coupling between plug-ins and theme functions
Pluggy is the core framework of pytest,tox,devpi
2 installation
Just execute the following command
Pip install pluggy3 uses first-time import pluggy# HookspecMarker and HookimplMarker to be essentially a decorator class with parameters, and its function is to add additional property settings hookspec = pluggy.HookspecMarker ("myproject") hookimpl = pluggy.HookimplMarker ("myproject") # to define its own Spec. Here it can be understood as defining the interface class class MySpec: # hookspec is a decorator for methods in the class, adding additional property settings to the method. Here myhook can be understood as defining an interface @ hookspec def myhook (self, arg1, arg2): pass# defines a plug-in class Plugin_1: # plug-in that implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function returns the sum of two parameters and @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_1.myhook ()") return arg1 + arg2# defines the second plug-in class Plugin_2: # plug-in implements the interface defined above, the same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_2.myhook ()") return arg1-arg2# instantiates an object managed by a plug-in Note that the name here should be the same as when the decorator is defined at the beginning of the file pm = pluggy.PluginManager ("myproject") # add the custom interface class to the hook definition to pm.add_hookspecs (MySpec) # register the two plug-ins defined pm.register (Plugin_1 ()) pm.register (Plugin_2 ()) # call the method through the hook of the plug-in management object, and this method will be executed in both plug-ins And follow the principle that LIFO is executed first after registration. The result handout list of the two plug-ins returns results = pm.hook.myhook (arg1=1, arg2=2) print (results)
The implementation results are as follows:
Inside Plugin_2.myhook ()
Inside Plugin_1.myhook ()
[- 1,3]
4 detailed explanation
From the point of view of the code, in fact, it is quite simple to understand that it is actually quite simple, which can be understood as defining an interface class first, and then defining many plug-in classes. the plug-in class defines as many interfaces as it wants, and the interface class defines an interface in the interface class. in fact, many interfaces can be defined. In each plug-in class, you need to select the interface in the interface class to implement. Of course, there is no need for each to achieve, you can choose to achieve according to your own needs.
For example:
For example, an interface class is defined, 10 interfaces are defined in the interface class, and 3 classes are defined at the same time. These three classes implement 3 interfaces, 6 interfaces and 10 interfaces in the interface class, respectively, and then instantiate an object managed by a plug-in. Instantiate the object to add the interface class to the definition template, and then register the three classes. You can call each method in the interface class through the hook of the plug-in management object, such as calling the first three methods, because each plug-in is implemented, so there will be three results. When calling the interface of the next 4-6, because only two plug-ins are implemented, only two results will be returned. Calling the interface of 7-10 because only one plug-in class has implemented, only one result will be returned. In this way, the plug-in is very flexible to use and can really be "hot-swappable".
The following is demonstrated with a code example:
Import pluggy# HookspecMarker and HookimplMarker are essentially a decorator class with parameters, which add additional property settings hookspec = pluggy.HookspecMarker ("myproject") hookimpl = pluggy.HookimplMarker ("myproject") # to the function to define its own Spec. Here it can be understood as defining the interface class class MySpec: # hookspec is a decorator for methods in the class, adding additional property settings to the method. Here myhook can be understood as defining three interfaces @ hookspec def myhook1 (self, arg1, arg2): pass @ hookspec def myhook2 (self, arg1, arg2): pass @ hookspec def myhook3 (self, arg1, arg2): pass# defines a plug-in that implements the interface defined above in class Plugin_1: # plug-in, which is also decorated with a hookimpl decorator The function returns the sum of @ hookimpl def myhook1 (self, arg1, arg2): print ("inside Plugin_1.myhook1 ()") return arg1 + arg2 @ hookimpl def myhook2 (self, arg1, arg2): print ("inside Plugin_1.myhook2 ()") return arg1 + arg2 + 1 @ hookimpl def myhook4 (self, arg1) Arg2): print ("inside Plugin_1.myhook4 ()") return arg1 + arg2 + definitions defines the second plug-in class Plugin_2: # the interface defined above is implemented in the plug-in The same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl def myhook1 (self, arg1, arg2): print ("inside Plugin_2.myhook1 ()") return arg1-arg2 @ hookimpl def myhook2 (self, arg1, arg2): print ("inside Plugin_2.myhook2 ()") return arg1-arg2-1 @ hookimpl def myhook3 (self, arg1) Arg2): print ("inside Plugin_2.myhook3 ()") return arg1-arg2-instantiate an object managed by a plug-in Note that the name here should be the same as when the decorator is defined at the beginning of the file pm = pluggy.PluginManager ("myproject") # add the custom interface class to the hook definition to pm.add_hookspecs (MySpec) # register the two plug-ins defined pm.register (Plugin_1 ()) pm.register (Plugin_2 ()) # call the method through the hook of the plug-in management object, and this method will be executed in both plug-ins And follow the principle of LIFO first after registration. The result handout list of the two plug-ins returns results = pm.hook.myhook1 (arg1=1, arg2=2) print (results) results = pm.hook.myhook2 (arg1=1, arg2=2) print (results) results = pm.hook.myhook3 (arg1=1, arg2=2) print (results) results = pm.hook.myhook4 (arg1=1, arg2=2) print (results)
The implementation results are as follows:
Inside Plugin_2.myhook1 ()
Inside Plugin_1.myhook1 ()
[- 1,3]
Inside Plugin_2.myhook2 ()
Inside Plugin_1.myhook2 ()
[- 2,4]
Inside Plugin_2.myhook3 ()
[- 3]
Inside Plugin_1.myhook4 ()
[5]
As you can see from the code example above:
1) myhook1 and myhook2 are implemented because both plug-ins return two results, and they are in reverse order.
2) since only plug-in 2 has been implemented in myhook3, only one result has been returned
3) myhook4 is not defined in spec, but there is also a result here. At present, it may be the bug of pluggy, which will be explained after looking at the source code later.
5 HookspeckMarker decorator supports passing in some specific parameters
When firstresult=True is passed in, the execution is stopped after the first plugin execution result is obtained.
Import pluggy# HookspecMarker and HookimplMarker are essentially a decorator class with parameters, which add additional property settings hookspec = pluggy.HookspecMarker ("myproject") hookimpl = pluggy.HookimplMarker ("myproject") # to the function to define its own Spec. Here it can be understood as defining the interface class class MySpec: # hookspec is a decorator for methods in the class, adding additional property settings to the method. Here myhook can be understood as defining an interface @ hookspec (firstresult=True) def myhook (self, arg1, arg2): pass# defines a plug-in class Plugin_1: # the plug-in implements the interface defined above, and this method of implementing the interface is decorated with a hookimpl decorator The function returns the sum of two parameters and @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_1.myhook ()") return arg1 + arg2# defines the second plug-in class Plugin_2: # plug-in implements the interface defined above, the same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_2.myhook ()") return arg1-arg2# instantiates an object managed by a plug-in Note that the name here should be the same as when the decorator is defined at the beginning of the file pm = pluggy.PluginManager ("myproject") # add the custom interface class to the hook definition to pm.add_hookspecs (MySpec) # register the two plug-ins defined pm.register (Plugin_1 ()) pm.register (Plugin_2 ()) # call the method through the hook of the plug-in management object, and this method will be executed in both plug-ins And follow the principle that LIFO is executed first after registration. The result handout list of the two plug-ins returns results = pm.hook.myhook (arg1=1, arg2=2) print (results)
The implementation results are as follows:
Inside Plugin_2.myhook ()
-1
6 HookImplMarker decorator also supports passing in some specific parameters
The commonly used ones are tryfirst, trylast and hookwrapper
When tryfirst=True is passed in, the hook function of this class will be executed first, while others will still be executed in LIFO order.
Import pluggy# HookspecMarker and HookimplMarker are essentially a decorator class with parameters, which add additional property settings hookspec = pluggy.HookspecMarker ("myproject") hookimpl = pluggy.HookimplMarker ("myproject") # to the function to define its own Spec. Here it can be understood as defining the interface class class MySpec: # hookspec is a decorator for methods in the class, adding additional property settings to the method. Here myhook can be understood as defining an interface @ hookspec def myhook (self, arg1, arg2): pass# defines a plug-in class Plugin_1: # plug-in that implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function returns the sum of two parameters and @ hookimpl (tryfirst=True) def myhook (self, arg1, arg2): print ("inside Plugin_1.myhook ()") return arg1 + arg2# defines the second plug-in class Plugin_2: # plug-in implements the interface defined above, and this method of implementing the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_2.myhook ()") return arg1-arg2# defines the third plug-in class Plugin_3: # plug-in implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_3.myhook ()") return arg1-arg2+10# instantiates an object managed by a plug-in Note that the name here should be the same as when the decorator is defined at the beginning of the file pm = pluggy.PluginManager ("myproject") # add the custom interface class to the hook definition to pm.add_hookspecs (MySpec) # register the definition of the two plug-ins pm.register (Plugin_1 ()) pm.register (Plugin_2 ()) pm.register (Plugin_3 ()) # manage the hook call method of the object through the plug-in At this time, this method in both plug-ins will be executed, and the principle of LIFO after registration is followed. The result handout list of the two plug-ins returns results = pm.hook.myhook (arg1=1, arg2=2) print (results).
The execution results are as follows:
Inside Plugin_1.myhook ()
Inside Plugin_3.myhook ()
Inside Plugin_2.myhook ()
[3,9,-1]
When trylast=True is passed in, the hook function of the current plug-in will be executed as late as possible, while the rest will still be executed in LIFO order.
Import pluggy# HookspecMarker and HookimplMarker are essentially a decorator class with parameters, which add additional property settings hookspec = pluggy.HookspecMarker ("myproject") hookimpl = pluggy.HookimplMarker ("myproject") # to the function to define its own Spec. Here it can be understood as defining the interface class class MySpec: # hookspec is a decorator for methods in the class, adding additional property settings to the method. Here myhook can be understood as defining an interface @ hookspec def myhook (self, arg1, arg2): pass# defines a plug-in class Plugin_1: # plug-in that implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function returns the sum of two parameters and @ hookimpl () def myhook (self, arg1, arg2): print ("inside Plugin_1.myhook ()") return arg1 + arg2# defines the second plug-in class Plugin_2: # plug-in implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl (trylast=True) def myhook (self, arg1, arg2): print ("inside Plugin_2.myhook ()") return arg1-arg2# defines the third plug-in class Plugin_3: # plug-in implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_3.myhook ()") return arg1-arg2+10# instantiates an object managed by a plug-in Note that the name here should be the same as when the decorator is defined at the beginning of the file pm = pluggy.PluginManager ("myproject") # add the custom interface class to the hook definition to pm.add_hookspecs (MySpec) # register the definition of the two plug-ins pm.register (Plugin_1 ()) pm.register (Plugin_2 ()) pm.register (Plugin_3 ()) # manage the hook call method of the object through the plug-in At this time, this method in both plug-ins will be executed, and the principle of LIFO after registration is followed. The result handout list of the two plug-ins returns results = pm.hook.myhook (arg1=1, arg2=2) print (results).
The implementation results are as follows:
Inside Plugin_3.myhook ()
Inside Plugin_1.myhook ()
Inside Plugin_2.myhook ()
[9, 3,-1]
When hookwrapper=True is passed in, you need to implement a yield,plugin in this plugin to execute yield first.
The previous code, then execute other pluggin, and then come back to execute the code after yield. At the same time, you can get the results executed by other plug-ins through yield.
Import pluggy# HookspecMarker and HookimplMarker are essentially a decorator class with parameters, which add additional property settings hookspec = pluggy.HookspecMarker ("myproject") hookimpl = pluggy.HookimplMarker ("myproject") # to the function to define its own Spec. Here it can be understood as defining the interface class class MySpec: # hookspec is a decorator for methods in the class, adding additional property settings to the method. Here myhook can be understood as defining an interface @ hookspec def myhook (self, arg1, arg2): pass# defines a plug-in class Plugin_1: # plug-in that implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function returns the sum of two parameters and @ hookimpl () def myhook (self, arg1, arg2): print ("inside Plugin_1.myhook ()") return arg1 + arg2# defines the second plug-in class Plugin_2: # plug-in implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between the two parameters @ hookimpl (hookwrapper=True) def myhook (self, arg1, arg2): print ("inside Plugin_2.myhook () before yield...") Output=yield result=output.get_result () print (result) print ("inside Plugin_2.myhook () after yield...") # defines the third plug-in class Plugin_3: # the plug-in implements the interface defined above, and the same way to implement the interface is decorated with a hookimpl decorator The function is to return the difference between two parameters @ hookimpl def myhook (self, arg1, arg2): print ("inside Plugin_3.myhook ()") return arg1-arg2+10# instantiates an object managed by a plug-in Note that the name here should be the same as when the decorator is defined at the beginning of the file pm = pluggy.PluginManager ("myproject") # add the custom interface class to the hook definition to pm.add_hookspecs (MySpec) # register the definition of the two plug-ins pm.register (Plugin_1 ()) pm.register (Plugin_2 ()) pm.register (Plugin_3 ()) # manage the hook call method of the object through the plug-in At this time, this method in both plug-ins will be executed, and the principle of LIFO after registration is followed. The result handout list of the two plug-ins returns results = pm.hook.myhook (arg1=1, arg2=2) print (results).
The implementation results are as follows:
Inside Plugin_2.myhook () before yield...
Inside Plugin_3.myhook ()
Inside Plugin_1.myhook ()
[9, 3]
Inside Plugin_2.myhook () after yield...
[9, 3]
This is the end of the content of "how to use the Python pluggy module". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.