Tvorba kontextových manažerů

Writing Functions in Python

Shayne Miel

Software Architect @ Duo Security

Dva způsoby definice kontextového manažeru

  • Třídní
  • Funkční
Writing Functions in Python

Dva způsoby definice kontextového manažeru

  • Třídní
  • Funkční *
Writing Functions in Python

Jak vytvořit kontextový manažer


def my_context():

# Add any set up code you need
yield
# Add any teardown code you need
  1. Definujte funkci.
  2. (volitelné) Přidejte inicializační kód.
  3. Použijte klíčové slovo "yield".
  4. (volitelné) Přidejte ukončovací kód.
Writing Functions in Python

Jak vytvořit kontextový manažer

@contextlib.contextmanager
def my_context():

# Add any set up code you need
yield
# Add any teardown code you need
  1. Definujte funkci.
  2. (volitelné) Přidejte inicializační kód.
  3. Použijte klíčové slovo "yield".
  4. (volitelné) Přidejte ukončovací kód.
  5. Přidejte dekorátor `@contextlib.contextmanager`.
Writing Functions in Python

Klíčové slovo "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
Writing Functions in Python

Inicializace a ukončení

@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'
  )

blankspace

arrow

Writing Functions in Python

Inicializace a ukončení

@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'
  )

blankspace

blankspace

blankspace

arrow

Writing Functions in Python

Inicializace a ukončení

@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'
  )

blankspace

blankspace

blankspace

blankspace

blankspace

blankspace

arrow

Writing Functions in Python

Vrácení hodnoty nebo None

@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()
Writing Functions in Python

Pojďme cvičit!

Writing Functions in Python

Preparing Video For Download...