Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions notebook/context manager/hands_on/1_microscope_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
scan_sample, # Scan sample currently in the microscope
)

@contextmanager
def sample_inserted(microscope_state):
insert_sample(microscope_state)
try:
yield
finally:
remove_sample(microscope_state)

@contextmanager
def do_stuff_under_vacuum(microscope_state):
Expand All @@ -33,14 +40,8 @@ def do_stuff_under_vacuum(microscope_state):
# Rewrite this script using the two context managers.
microscope_state = connect_to_microscope()

activate_vacuum_pump(microscope_state)
try:
insert_sample(microscope_state)
try:
with do_stuff_under_vacuum(microscope_state):
with sample_inserted(microscope_state):
sample_image = scan_sample(microscope_state)
finally:
remove_sample(microscope_state)
finally:
deactivate_vacuum_pump(microscope_state)

release_microscope(microscope_state)