Robot Chef all in one package with lml

Now let us bring in lml and see see how the lml package can be used to rewrite Robot Chef but in a single package. This chapter introduces two classes: lml.plugin.PluginManager and lml.plugin.PluginInfo. And show how those classes can be used to make factory pattern. Meanwhile, it demonstrates that the lml based plugins can be made to load immediately and in a single package. And this sections helps you to understand the next section where we will make the plugins to be loaded later.

Demo

Please navigate to lml/examples, you would find robotchef_allinone_lml and its packages. Do the following:

$ cd robotchef_allinone_lml
$ python setup.py install

And then you could try:

$ robotchef_allinone_lml "Fish and Chips"
I can fry Fish and Chips

Lml plugins and plugin manager

_static/images/robotchef_allinone_lml.svg

plugin.py

CuisineManager inherits from PluginManager class and replaces the static registry PLUGINS and the modular function get_a_plugin. Please note that CuisineManager declares that it is a manager for plugin_type named cuisine.

Next, the PluginInfo decorates all Chef’s subclasses as cuisine plugins and register the decorated classes with the manager class for cuisine, CuisineManager. The food names become the tags which will be used to look up the classes.

Here is the code difference with Robot Chef All In One solution: plugin.py.

main.py

The main code has been updated to reflect the changes in plugin.py. CuisineManager has to be instantiated to be the a factory manager.

Remember this interaction:

$ robotchef "Portable Battery"
I can cook Portable Battery for robots

The response comes from class Boost. It is obtained via CuisineManager when user types ‘Portable Battery’. And the food parameter was passed to the instance of Boost. make method was called and it prints ‘I can cook Portable Battery for robots’.

See also

  1. pyexcel-chart: use lml to refactor existing plugins