कॉन्टेक्स्ट मैनेजर्स लिखना

Python में Functions लिखना

Shayne Miel

Software Architect @ Duo Security

कॉन्टेक्स्ट मैनेजर परिभाषित करने के दो तरीके

  • क्लास-आधारित
  • फंक्शन-आधारित
Python में Functions लिखना

कॉन्टेक्स्ट मैनेजर परिभाषित करने के दो तरीके

  • क्लास-आधारित
  • फंक्शन-आधारित *
Python में Functions लिखना

कॉन्टेक्स्ट मैनेजर कैसे बनाएँ


def my_context():

# Add any set up code you need
yield
# Add any teardown code you need
  1. एक फंक्शन परिभाषित करें।
  2. (वैकल्पिक) ज़रूरत हो तो कोई सेटअप कोड जोड़ें।
  3. "yield" कीवर्ड का उपयोग करें।
  4. (वैकल्पिक) ज़रूरत हो तो कोई टियरडाउन कोड जोड़ें।
Python में Functions लिखना

कॉन्टेक्स्ट मैनेजर कैसे बनाएँ

@contextlib.contextmanager
def my_context():

# Add any set up code you need
yield
# Add any teardown code you need
  1. एक फंक्शन परिभाषित करें।
  2. (वैकल्पिक) ज़रूरत हो तो कोई सेटअप कोड जोड़ें।
  3. "yield" कीवर्ड का उपयोग करें।
  4. (वैकल्पिक) ज़रूरत हो तो कोई टियरडाउन कोड जोड़ें।
  5. `@contextlib.contextmanager` डेकोरेटर जोड़ें।
Python में Functions लिखना

"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
Python में Functions लिखना

सेटअप और टियरडाउन

@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

Python में Functions लिखना

सेटअप और टियरडाउन

@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

Python में Functions लिखना

सेटअप और टियरडाउन

@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

Python में Functions लिखना

किसी वैल्यू या None को yield करना

@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()
Python में Functions लिखना

अभ्यास करते हैं!

Python में Functions लिखना

Preparing Video For Download...