From 62a08886b81d2a0f8515e83eed6ec9e977b80af8 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Wed, 4 Sep 2019 12:33:02 +0200 Subject: [PATCH 1/2] We are the champions --- notebook/generators/hands_on/recognize_smell.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 notebook/generators/hands_on/recognize_smell.py diff --git a/notebook/generators/hands_on/recognize_smell.py b/notebook/generators/hands_on/recognize_smell.py new file mode 100644 index 0000000..344f3f0 --- /dev/null +++ b/notebook/generators/hands_on/recognize_smell.py @@ -0,0 +1,16 @@ +def generator_useful(n,divider): + """Wonderful comments""" + + for i in range(n): + if i % divider == 0: + continue + yield i + +for i in generator_useful(9,3): + print('Square is', i ** 2) + +for i in generator_useful(5,2): + print('Cube is', i ** 3) + +for i in generator_useful(13,5): + print('A' * i) From 0067a7b075fd7cbd5b8c1a4de1fb93a42eea6709 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Wed, 4 Sep 2019 13:14:44 +0200 Subject: [PATCH 2/2] added context managers --- .../hands_on/1_microscope_script.py | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/notebook/context manager/hands_on/1_microscope_script.py b/notebook/context manager/hands_on/1_microscope_script.py index 78e59f1..c92a515 100644 --- a/notebook/context manager/hands_on/1_microscope_script.py +++ b/notebook/context manager/hands_on/1_microscope_script.py @@ -4,15 +4,22 @@ connect_to_microscope, # Connect to microscope and get a state object release_microscope, - activate_vacuum_pump, # Make vacuum inside the microscope + activate_vacuum_pump, # Make vacuum inside the microscope deactivate_vacuum_pump, - + insert_sample, # Insert sample for scanning remove_sample, - + calibrate, # Calibrate microscope 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 @@ -26,21 +33,16 @@ def do_stuff_under_vacuum(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 +# in the microscope before executing a block of code, and safely removes # it at the end of the block. # 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)