summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrabhu Ramachandran2018-05-19 20:33:36 +0530
committerPrabhu Ramachandran2018-05-19 20:33:36 +0530
commit3170893245a922fce640a64f0c3bc3f6009ae1e3 (patch)
treea7e11e934620b3381e4a0b4980417b610294fa2a
parent89de5dd28d80d20ff41a70594d930d5adaf3f57e (diff)
downloadpython-workshops-3170893245a922fce640a64f0c3bc3f6009ae1e3.tar.gz
python-workshops-3170893245a922fce640a64f0c3bc3f6009ae1e3.tar.bz2
python-workshops-3170893245a922fce640a64f0c3bc3f6009ae1e3.zip
Add basic material on with statements.
-rw-r--r--advanced_python/18_with_statement.ipyml167
-rw-r--r--advanced_python/data/test.txt3
-rw-r--r--advanced_python/rise.css5
3 files changed, 170 insertions, 5 deletions
diff --git a/advanced_python/18_with_statement.ipyml b/advanced_python/18_with_statement.ipyml
new file mode 100644
index 0000000..b231d39
--- /dev/null
+++ b/advanced_python/18_with_statement.ipyml
@@ -0,0 +1,167 @@
+cells:
+
+- markdown: |
+ # Advanced Python: `with` statements
+
+ ### Prabhu Ramachandran
+ ### The FOSSEE Python group &
+ ### Department of Aerospace Engineering
+ ### IIT Bombay
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Introduction
+
+ - `with` statement is used as follows
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ with open('data/test.txt') as f:
+ for line in f:
+ print(line, end='')
+
+ id: 0
+
+- code: |
+ f.closed
+
+ id: 1
+ metadata:
+ slideshow:
+ slide_type: fragment
+
+- markdown: |
+ ## Observations
+
+ - Creates a context
+ - `f` is automatically closed
+ - Works even if there was an exception!
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Use cases
+
+ - Use it with locks (for threaded computing)
+ - Database connections
+ - File objects
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Creating your own
+
+ - Relatively easy to do
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- code: |
+ from contextlib import contextmanager
+
+ @contextmanager
+ def context():
+ print("setup")
+ try:
+ yield
+ finally:
+ print("done")
+
+- code: |
+ with context():
+ print("do something")
+
+- markdown: |
+ ## A more complex example
+
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+- code: |
+ import os
+ from contextlib import contextmanager
+
+ @contextmanager
+ def tempfile(filename):
+ f = open(filename, 'w')
+ try:
+ yield f
+ finally:
+ f.close()
+ os.remove(filename)
+
+- code: |
+ with tempfile('junk.txt') as f:
+ f.write('hello world\n')
+ f.flush()
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+- markdown: |
+ ## Comments
+
+ - Useful feature
+ - Can define an object supporting this
+ - Overload, `__enter__` and `__exit__`
+ - Not covered here
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+
+
+- markdown: |
+ ## More information
+
+ - https://www.python.org/dev/peps/pep-0343
+ - [With statement](https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers)
+ - [Context manager types](https://docs.python.org/3/library/stdtypes.html#typecontextmanager)
+
+ metadata:
+ slideshow:
+ slide_type: slide
+
+# The lines below here may be deleted if you do not need them.
+# ---------------------------------------------------------------------------
+metadata:
+ kernelspec:
+ display_name: Python 3
+ language: python
+ name: python3
+ language_info:
+ codemirror_mode:
+ name: ipython
+ version: 3
+ file_extension: .py
+ mimetype: text/x-python
+ name: python
+ nbconvert_exporter: python
+ pygments_lexer: ipython3
+ version: 3.6.0
+ rise:
+ scroll: true
+ transition: none
+nbformat: 4
+nbformat_minor: 2
+
+# ---------------------------------------------------------------------------
+data:
+ [{execution_count: null, outputs: []}, {execution_count: null, outputs: []}, {execution_count: null,
+ outputs: []}, {execution_count: null, outputs: []}, {execution_count: null, outputs: []}]
diff --git a/advanced_python/data/test.txt b/advanced_python/data/test.txt
new file mode 100644
index 0000000..c100072
--- /dev/null
+++ b/advanced_python/data/test.txt
@@ -0,0 +1,3 @@
+hello
+world
+
diff --git a/advanced_python/rise.css b/advanced_python/rise.css
index 11d733d..0846e50 100644
--- a/advanced_python/rise.css
+++ b/advanced_python/rise.css
@@ -8,11 +8,6 @@ div.cell.code_cell {
font-size: 150%;
}
-/*
-div.CodeMirror-lines {
- font-size: 125%;
-}*/
-
/* Tables were rendered out as tiny values
since the font-size was set to 12px somehow.
*/