Guest User

Untitled

a guest
Jun 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. # Here's the way common lisp works: You place code that actually recovers from
  2. # errors into 'restarts' and condition handlers can then handle a condition by
  3. # invoking an appropriate restart. You can place restart code in mid- or
  4. # low-level functions, such as 'parse_log_file' or 'parse_log_entry', while
  5. # moving the condition handlers into the upper levels of the application.
  6.  
  7. def parse_line(line):
  8. if is_parsable(line):
  9. ...
  10. else:
  11. raise UnparsableLineException("...")
  12.  
  13. def parse_log_file(f):
  14. with open(f) as file:
  15. results = []
  16. for line in file:
  17. # here, some new syntax may be required:
  18. restart_case parse_line(line):
  19. skip_entry:
  20. pass
  21. log_error:
  22. print "Unparsable line: "+line
  23. stop_here:
  24. return
  25.  
  26. # New you need to set up a condition handler that will invoke the skip_log_entry
  27. # restart, for example. You can set up the handler anywhere in the chain of
  28. # calls leading to parse_log_file. This may be quite high up in your
  29. # application, not necessarily in parse_log_file's direct caller. For instance,
  30. # suppose the main entry point to your application is a function, log_analyzer,
  31. # that finds a bunch of logs and analyzes them with the function analyze_log,
  32. # which eventually leads to a call to parse_log_file.
  33.  
  34. def log_analyzer():
  35. for log in ["mailman.log", "termcap.log", "rctab.d", ...]:
  36. try:
  37. parse_log_file(log)
  38. except UnparsableLineException as ex:
  39. ex.skip_entry()
  40. # for example
  41. # alternatively, maybe ask the user what to do
  42. ex.invoke_restart("stop_here");
Add Comment
Please, Sign In to add comment