diff --git a/notebook/context manager/hands_on/1_microscope_script.py b/notebook/context manager/hands_on/1_microscope_script.py index 78e59f1..c417fcc 100644 --- a/notebook/context manager/hands_on/1_microscope_script.py +++ b/notebook/context manager/hands_on/1_microscope_script.py @@ -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 @@ -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)