Context managers · 15 / 15 Exercise Hard

Error-aware log

Objective

Create a Python program with a context manager that decides according to the type of error: the parameters of __exit__ tell whether there was an exception and of which class.

Instructions

  • Create the class Log with a list of notes, a note(text) method that adds a text, and the __enter__ (returns the object itself) and __exit__ methods.
  • On exit, show Log OK: k entries if there was no error, or Log with error (Type): k entries if there was (the name of the error class is kind.__name__), and then each note as - text.
  • __exit__ must return True only when the error is ZeroDivisionError (that suppresses it). Any other error would propagate.
  • Read an integer n and then n lines with two integers x and y. Inside the block note "x / y" and then "= result" (integer division). When finished, show End of the program.
Input
3
6 3
1 0
8 2
Output
Log with error (ZeroDivisionError): 3 entries
  - 6 / 3
  - = 2
  - 1 / 0
End of the program

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise