Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 14 additions & 12 deletions notebook/context manager/hands_on/1_microscope_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
16 changes: 16 additions & 0 deletions notebook/generators/hands_on/recognize_smell.py
Original file line number Diff line number Diff line change
@@ -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)