Python Automation Cookbook
上QQ阅读APP看书,第一时间看更新

Preparing a task

It all starts with defining exactly what task needs to be run, and designing it in a way that doesn't require human intervention to run.

Some ideal characteristic points are as follows:

  1. Single, clear entry point: No confusion on what the task to run is.
  2. Clear parameters: If there are any parameters, they should be very explicit.
  3. No interactivity: Stopping the execution to request information from the user is not possible.
  4. The result should be stored: To be able to be checked at a different time than when it runs.
  5. Clear result: If we are working interactively in a result, we accept more verbose results, or progress reports. But, for an automated task, the final result should be as concise and to the point as possible.
  6. Errors should be logged: To analyze what went wrong. 

A command-line program has a lot of those characteristics already. It has a clear way of running, with defined parameters, and the result can be stored, even if just in text format. But, it can be improved with a config file to clarify the parameters, and an output file.

Note that point 6 is the objective of the Capturing errors and problems recipe, and will be covered there.

To avoid interactivity, do not use any command that stops for the user to input, like input. Remember to delete breakpoints for debugging!