summaryrefslogtreecommitdiff
path: root/advanced_python/18_with_statement.ipyml
blob: 57ccc387e23f1dcc2c0b23b4f1908bfc32f0416f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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='')


- code: |
    f.closed

  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: |
    %ls junk.txt


- code: |
    with tempfile('junk.txt') as f:
        f.write('hello world\n')
        f.flush()

  metadata:
    slideshow:
      slide_type: slide

- markdown: |
    ## Summary
    
    - 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.5.2
  rise:
    scroll: true
    transition: none
nbformat: 4
nbformat_minor: 2