Lab, Decorators and Context Managers
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints, including what happens when the block raises.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write a context manager class Tag(name) that prints <name> when the block starts and </name> when it ends, even if the block raises. Use it to print:
<ul> <li> tea </li> </ul>
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so the block runs between the two prints and receives the list, and the program prints start, ['a'] and stop.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The lock must always be released, but when the block fails, released never prints. Fix locked so the program prints:
acquired released caught: disk full
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows level and steps each time one of them changes, once both exist. indent raises the level on entry and lowers it on exit.
from contextlib import contextmanager
level = 0
@contextmanager
def indent():
global level
level = level + 1
try:
yield
finally:
level = level - 1
steps = 0
with indent():
steps = steps + 1
with indent():
steps = steps + 1
steps = steps + 1Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Both jobs use with job(...); the only try is inside the manager.
This code works, but it repeats the same try and finally around every job. Write a context manager job(name) with @contextmanager that prints the start and finish lines, and use with job(...) for both jobs, with no try outside the manager. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write a context manager class Budget(limit). Inside the block, the code calls b.spend(n), which adds n to b.used and raises RuntimeError("over budget") if used goes above limit. On exit, it always prints used U of L, and it suppresses only RuntimeError, so the program carries on; any other error passes through. The program should print:
used 7 of 10 used 12 of 10 after the second block used 1 of 5 KeyError passed through
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.