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
29 changes: 20 additions & 9 deletions notebook/context manager/hands_on/1_microscope_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def do_stuff_under_vacuum(microscope_state):
finally:
deactivate_vacuum_pump(microscope_state)

@contextmanager
def sample_inserted(microscope_state):
insert_sample(microscope_state)
try:
yield # sample_image = scan_sample(microscope_state)
finally:
remove_sample(microscope_state)

# Write here the `sample_inserted` context manager that inserts a sample
# in the microscope before executing a block of code, and safely removes
Expand All @@ -33,14 +40,18 @@ 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:
sample_image = scan_sample(microscope_state)
finally:
remove_sample(microscope_state)
finally:
deactivate_vacuum_pump(microscope_state)
with do_stuff_under_vacuum(microscope_state):
with sample_inserted(microscope_state):
sample_image = scan_sample(microscope_state)

# activate_vacuum_pump(microscope_state)
#try:
# insert_sample(microscope_state)
# try:
# sample_image = scan_sample(microscope_state)
# finally:
# remove_sample(microscope_state)
#finally:
# deactivate_vacuum_pump(microscope_state)

release_microscope(microscope_state)