From 206aec57e4da0a6ea31e9fca6fbb23bfa6f56ded Mon Sep 17 00:00:00 2001 From: Corey Schafer Date: Tue, 19 Dec 2017 21:43:13 -0700 Subject: [PATCH] Python Context Managers code snippets --- .../Sample-Dir-One/mydoc.txt | 0 .../Sample-Dir-One/todo.txt | 0 .../Sample-Dir-One/work.txt | 0 .../Sample-Dir-Two/demo.txt | 0 .../Sample-Dir-Two/sample.txt | 0 .../Sample-Dir-Two/test.txt | 0 Python-Context-Managers/cm_demo.py | 19 +++++++++ Python-Context-Managers/sample.txt | 1 + Python-Context-Managers/snippets.txt | 41 +++++++++++++++++++ 9 files changed, 61 insertions(+) create mode 100644 Python-Context-Managers/Sample-Dir-One/mydoc.txt create mode 100644 Python-Context-Managers/Sample-Dir-One/todo.txt create mode 100644 Python-Context-Managers/Sample-Dir-One/work.txt create mode 100644 Python-Context-Managers/Sample-Dir-Two/demo.txt create mode 100644 Python-Context-Managers/Sample-Dir-Two/sample.txt create mode 100644 Python-Context-Managers/Sample-Dir-Two/test.txt create mode 100644 Python-Context-Managers/cm_demo.py create mode 100644 Python-Context-Managers/sample.txt create mode 100644 Python-Context-Managers/snippets.txt diff --git a/Python-Context-Managers/Sample-Dir-One/mydoc.txt b/Python-Context-Managers/Sample-Dir-One/mydoc.txt new file mode 100644 index 0000000..e69de29 diff --git a/Python-Context-Managers/Sample-Dir-One/todo.txt b/Python-Context-Managers/Sample-Dir-One/todo.txt new file mode 100644 index 0000000..e69de29 diff --git a/Python-Context-Managers/Sample-Dir-One/work.txt b/Python-Context-Managers/Sample-Dir-One/work.txt new file mode 100644 index 0000000..e69de29 diff --git a/Python-Context-Managers/Sample-Dir-Two/demo.txt b/Python-Context-Managers/Sample-Dir-Two/demo.txt new file mode 100644 index 0000000..e69de29 diff --git a/Python-Context-Managers/Sample-Dir-Two/sample.txt b/Python-Context-Managers/Sample-Dir-Two/sample.txt new file mode 100644 index 0000000..e69de29 diff --git a/Python-Context-Managers/Sample-Dir-Two/test.txt b/Python-Context-Managers/Sample-Dir-Two/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/Python-Context-Managers/cm_demo.py b/Python-Context-Managers/cm_demo.py new file mode 100644 index 0000000..823866a --- /dev/null +++ b/Python-Context-Managers/cm_demo.py @@ -0,0 +1,19 @@ +import os +from contextlib import contextmanager + + +@contextmanager +def change_dir(destination): + try: + cwd = os.getcwd() + os.chdir(destination) + yield + finally: + os.chdir(cwd) + + +with change_dir('Sample-Dir-One'): + print(os.listdir()) + +with change_dir('Sample-Dir-Two'): + print(os.listdir()) diff --git a/Python-Context-Managers/sample.txt b/Python-Context-Managers/sample.txt new file mode 100644 index 0000000..d863439 --- /dev/null +++ b/Python-Context-Managers/sample.txt @@ -0,0 +1 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. \ No newline at end of file diff --git a/Python-Context-Managers/snippets.txt b/Python-Context-Managers/snippets.txt new file mode 100644 index 0000000..3d64909 --- /dev/null +++ b/Python-Context-Managers/snippets.txt @@ -0,0 +1,41 @@ + +class Open_File(): + + def __init__(self, destination): + pass + + def __enter__(self): + pass + + def __exit__(self, exc_type, exc_val, traceback): + pass + + +#### Using contextlib #### + +@contextmanager +def open_file(file, mode): + f = open(file, mode) + yield f + f.close() + + +with open_file('sample.txt', 'w') as f: + f.write('Lorem ipsum dolor sit amet, consectetur adipiscing elit.') + +print(f.closed) + + +#### CD Example #### + +cwd = os.getcwd() +os.chdir('Sample-Dir-One') +print(os.listdir()) +os.chdir(cwd) + +cwd = os.getcwd() +os.chdir('Sample-Dir-Two') +print(os.listdir()) +os.chdir(cwd) + +