Contextmanagers schrijven

Functies schrijven in Python

Shayne Miel

Software Architect @ Duo Security

Twee manieren om een contextmanager te definiëren

  • Op basis van classes
  • Op basis van functies
Functies schrijven in Python

Twee manieren om een contextmanager te definiëren

  • Op basis van classes
  • Op basis van functies *
Functies schrijven in Python

Hoe maak je een contextmanager


def my_context():

# Add any set up code you need
yield
# Add any teardown code you need
  1. Definieer een functie.
  2. (optioneel) Voeg setup-code toe die je context nodig heeft.
  3. Gebruik het sleutelwoord "yield".
  4. (optioneel) Voeg teardown-code toe die je context nodig heeft.
Functies schrijven in Python

Hoe maak je een contextmanager

@contextlib.contextmanager
def my_context():

# Add any set up code you need
yield
# Add any teardown code you need
  1. Definieer een functie.
  2. (optioneel) Voeg setup-code toe die je context nodig heeft.
  3. Gebruik het sleutelwoord "yield".
  4. (optioneel) Voeg teardown-code toe die je context nodig heeft.
  5. Voeg de decorator `@contextlib.contextmanager` toe.
Functies schrijven in Python

Het sleutelwoord "yield"

@contextlib.contextmanager
def my_context():
  print('hello')
  yield 42
  print('goodbye')
with my_context() as foo:
  print('foo is {}'.format(foo))
hello
foo is 42
goodbye
Functies schrijven in Python

Setup en teardown

@contextlib.contextmanager
def database(url):
  # set up database connection
  db = postgres.connect(url)

  yield db

  # tear down database connection
  db.disconnect()
url = 'http://datacamp.com/data'
with database(url) as my_db:
  course_list = my_db.execute(
    'SELECT * FROM courses'
  )

lege ruimte

pijl

Functies schrijven in Python

Setup en teardown

@contextlib.contextmanager
def database(url):
  # set up database connection
  db = postgres.connect(url)

  yield db

  # tear down database connection
  db.disconnect()
url = 'http://datacamp.com/data'
with database(url) as my_db:
  course_list = my_db.execute(
    'SELECT * FROM courses'
  )

lege ruimte

lege ruimte

lege ruimte

pijl

Functies schrijven in Python

Setup en teardown

@contextlib.contextmanager
def database(url):
  # set up database connection
  db = postgres.connect(url)

  yield db

  # tear down database connection
  db.disconnect()
url = 'http://datacamp.com/data'
with database(url) as my_db:
  course_list = my_db.execute(
    'SELECT * FROM courses'
  )

lege ruimte

lege ruimte

lege ruimte

lege ruimte

lege ruimte

lege ruimte

pijl

Functies schrijven in Python

Een waarde of None yielden

@contextlib.contextmanager
def database(url):
  # set up database connection
  db = postgres.connect(url)

  yield db

  # tear down database connection
  db.disconnect()
url = 'http://datacamp.com/data'
with database(url) as my_db:
  course_list = my_db.execute(
    'SELECT * FROM courses'
  )
@contextlib.contextmanager
def in_dir(path):
  # save current working directory
  old_dir = os.getcwd()

  # switch to new working directory
  os.chdir(path)

  yield

  # change back to previous
  # working directory
  os.chdir(old_dir)
with in_dir('/data/project_1/'):
  project_files = os.listdir()
Functies schrijven in Python

Laten we oefenen!

Functies schrijven in Python

Preparing Video For Download...