Adding and packaging more features into an application

Previously, we noted one common approach to supporting multiple features. Some applications use multiple top-level main programs in separate .py script files. If we do this, then combining commands from separate files forces us to write a shell script. It doesn't seem optimal to introduce yet another tool and another language to do programming in-the-large (PITL).

A slightly more flexible alternative to creating separate script files is using a positional parameter to select a specific top-level Command object. For our example, we'd like to select either the simulation or the analysis command. To do this, we would add a parameter to the command-line argument parsing the following code:

parser.add_argument(
"command", action="store", default='simulate',
choices=['simulate', 'analyze']) parser.add_argument("outputfile", action="store", metavar="output")

This would change the command-line API to add the top-level verb to the command line. We can then map our argument values to class names that implement the desired command, as shown in the following code:

command_map = {
'simulate': Simulate_Command,
'analyze': Analyze_Command
}
command = command_map[options.command]
command.configure(options)
command.run()

This allows us to create even higher-level composite features. For example, we might want to combine simulation and analysis into a single, overall program. We also might like to do this without resorting to using the shell.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset