Hard
Nested Transactions
Write a class
Store holding a dictionary, with a method transaction() that returns a context manager: on entry it saves a copy of the data; if the block raises, it restores the copy and swallows the error, printing rolled back. Transactions can be nested. Run the commands: set K V, begin, end, fail (raise an error) — use recursion so each begin opens a with block that runs until its matching end (a failure skips the rest of that block). Print the data sorted at the end as K=V, or empty.Input
Commands, then END.
Output
Lines.
Sample 1
Input
set a 1 begin set a 2 set b 3 end begin set c 4 fail set d 5 end END
Output
rolled back a=2 b=3
Sample 2
Input
begin fail end END
Output
rolled back empty
Output appears here