multiple inheritance of a dynamic list of classes?
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. My idea was: - Load a list of plugin names (i.e. from the config file, or from the plugins directory) - Import all plugins found dynamically: and this is easy, since I can do, for example: PLUGIN_NAMES=['foo', 'bar'] PLUGIN_MODULES = map(__import__, PLUGIN_NAMES) PLUGINS = [item.Commands for item in PLUGIN_MODULES] Now, what I have to do is to define my command line class. This is usually done by subclassing cmd.Cmd: class MyCli(cmd.Cmd): Now I want to add the commands defined in foo.Commands and bar.Commands. foo.Commands contains the functions corresponding to the new commands this way: #foo.py class Commands def do_this(self,args): ... def do_that(self,args): ... I've seen I can do it by explicitely import them and using multiple inheritance: class MyCli(cmd.Cmd , foo.Commands, bar.Commands) so that do_this and do_that are now methods of a Cmd command line. Now: - how can I instead have MyCli inherit from a dynamic list of modules? - is there a better way than using multiple inheritance to plug-in dynamically commands in a Cmd command line? I hope it's all clear. Thanks, Massimo -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple inheritance of a dynamic list of classes?
> Most of the above should be straight-forward. I used type(cmd.Cmd)(name, > bases, classdict) instead of just type(name, bases, classdict) because > cmd.Cmd is a classic class. It seems it works. It is not so straight-forward to me because I don't know about new-style types and classes very well, but I'm looking at type() and it makes full sense. I'll also look into new style classes. Thanks a lot. Anyway, just for the sake of asking: since I have no formal CS education (I'm a biophysics ph.d. student), I wonder if there is some kind of design pattern/coding tool that could work better/help me along simple inheritance for coding plugin architectures (inheritance probably works well in this case, but I'd like to know what's in the swiss army knife). Thanks again, Massimo -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple inheritance of a dynamic list of classes?
Thanks both for suggestions. I still think that using inheritance is somehow cleanest in this case (I always hear the mantra "avoid multiple inheritance!", but this is one of the cases it seems to make a lot of sense to me), but it's nice food for thought/code anyway. Other suggestions are always welcome, if there are! Massimo -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple inheritance of a dynamic list of classes?
On 13 Feb, 09:14, Peter Otten <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Thanks both for suggestions. I still think that using inheritance is > > somehow cleanest in this case (I always hear the mantra "avoid > > multiple inheritance!", but this is one of the cases it seems to make > > a lot of sense to me), but it's nice food for thought/code anyway. > > "Avoid inheritance" would be almost as justified :-) Why? > Problems that may arise with this case of multiple inheritance: > > - If you need initializers, ensure that they are all invoked Yes, I figured it out. This should be easy in this case. > - What would you do about name clashes? To avoid them your plugins need to > know about each other. Yes, I know, but I can't see any simple solution to this (if you can, please share it with me!). The cmd module works by interpreting any method starting with "do_" as a command, so "do_blah" becomes the "blah" command. If two people write a "do_blah" command, and both plugins are used, I see no easy way to solve the issue (apart rewriting a cmd module). Perhaps there can be some previous sanity check in each modules dict to see if there are obvious namespace clashings, and in this case issue a warning. I don't know. > - State (instance attributes) is shared among all your plugins. Since you > call all base classes Commands, Python's double-underscore hack won't work. What double-underscore hack are you referring to? (sigh, my python limits are all arising...) I can call all base classes PluginNameCommand, however, this wouldn't break the thing (I'm still at the very early planning stage) and would maybe work. m. -- http://mail.python.org/mailman/listinfo/python-list