Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Here's the way common lisp works: You place code that actually recovers from
- # errors into 'restarts' and condition handlers can then handle a condition by
- # invoking an appropriate restart. You can place restart code in mid- or
- # low-level functions, such as 'parse_log_file' or 'parse_log_entry', while
- # moving the condition handlers into the upper levels of the application.
- def parse_line(line):
- if is_parsable(line):
- ...
- else:
- raise UnparsableLineException("...")
- def parse_log_file(f):
- with open(f) as file:
- results = []
- for line in file:
- # here, some new syntax may be required:
- restart_case parse_line(line):
- skip_entry:
- pass
- log_error:
- print "Unparsable line: "+line
- stop_here:
- return
- # New you need to set up a condition handler that will invoke the skip_log_entry
- # restart, for example. You can set up the handler anywhere in the chain of
- # calls leading to parse_log_file. This may be quite high up in your
- # application, not necessarily in parse_log_file's direct caller. For instance,
- # suppose the main entry point to your application is a function, log_analyzer,
- # that finds a bunch of logs and analyzes them with the function analyze_log,
- # which eventually leads to a call to parse_log_file.
- def log_analyzer():
- for log in ["mailman.log", "termcap.log", "rctab.d", ...]:
- try:
- parse_log_file(log)
- except UnparsableLineException as ex:
- ex.skip_entry()
- # for example
- # alternatively, maybe ask the user what to do
- ex.invoke_restart("stop_here");
Add Comment
Please, Sign In to add comment