[Yum] Future feature request...
carwyn at carwyn.com
Thu Nov 13 14:23:51 UTC 2003
I know you were just joking (the smiley gave it away) but there is a
tidy solution to the "endless if" problem.
Make a dict of names->functions. This makes the whole thing rather
modular and tidy.
I don't see why you need a huge "if block" or a dict map to do this?
Either way you end up with hard coded mappings. The best way to do this
is with a plugin system that uses a dynamic class/module loader. See:
http://mjoc.sig.lt/python/py_plugins.html - This method uses exec.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/223972 - This
(I like the second one better)
Basically you have a class/module per [command] which you instantiate
based on the value of [command] and pass all the subsequent arguments
too. The core yum main() entry code doesn't even have to know what
commands are available.
Adding new commands would be a case of dropping in new classes/modules
with the catch-all set to spew out the usage/list of valid command options.
You don't need an if block or a dict at all - hell you can even build it
in such a way that the command documentation is dynamically pulled from
the class the command is in:
bash$ yum list help # calls commands.list.usage()
GIVEN A LONG LIST of name->function pairs, how do we more cleanly
deal with them?
Answer: you don't have a long list. You use introspection/reflection.
Why build a mapping when you can derive it at runtime?
More information about the Yum