2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -1,6 +1,6 @@
# with 语句和上下文管理器
```
```py
# create/aquire some resource
...
try:
@@ -18,7 +18,7 @@ finally:
In [1]:
```
```py
with open('my_file', 'w') as fp:
# do stuff with fp
data = fp.write("Hello world")
@@ -29,7 +29,7 @@ with open('my_file', 'w') as fp:
In [2]:
```
```py
fp = open('my_file', 'w')
try:
# do stuff with f
@@ -43,7 +43,7 @@ finally:
其基本用法如下:
```
```py
with <expression>:
<block>
```
@@ -52,13 +52,13 @@ with <expression>:
In [3]:
```
```py
print fp.__enter__
print fp.__exit__
```
```
```py
<built-in method __enter__ of file object at 0x0000000003C1D540>
<built-in method __exit__ of file object at 0x0000000003C1D540>
@@ -70,7 +70,7 @@ print fp.__exit__
In [4]:
```
```py
class ContextManager(object):
def __enter__(self):
@@ -85,13 +85,13 @@ class ContextManager(object):
In [5]:
```
```py
with ContextManager():
print " Inside the with statement"
```
```
```py
Entering
Inside the with statement
Exiting
@@ -102,19 +102,19 @@ Exiting
In [6]:
```
```py
with ContextManager():
print 1/0
```
```
```py
Entering
Exiting
```
```
```py
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-6-b509c97cb388> in <module>()
@@ -130,7 +130,7 @@ ZeroDivisionError: integer division or modulo by zero
In [7]:
```
```py
class ContextManager(object):
def __enter__(self):
@@ -146,13 +146,13 @@ class ContextManager(object):
In [8]:
```
```py
with ContextManager() as value:
print value
```
```
```py
Entering
my value
Exiting
@@ -163,21 +163,21 @@ Exiting
In [9]:
```
```py
fp = open('my_file', 'r')
print fp.__enter__()
fp.close()
```
```
```py
<open file 'my_file', mode 'r' at 0x0000000003B63030>
```
In [10]:
```
```py
import os
os.remove('my_file')
@@ -187,7 +187,7 @@ os.remove('my_file')
In [11]:
```
```py
class ContextManager(object):
def __enter__(self):
@@ -201,13 +201,13 @@ class ContextManager(object):
In [12]:
```
```py
with ContextManager() as value:
print value
```
```
```py
Entering
<__main__.ContextManager object at 0x0000000003D48828>
Exiting
@@ -220,7 +220,7 @@ Exiting
In [13]:
```
```py
class ContextManager(object):
def __enter__(self):
@@ -237,20 +237,20 @@ class ContextManager(object):
In [14]:
```
```py
with ContextManager():
print 1/0
```
```
```py
Entering
Exiting
Exception: integer division or modulo by zero
```
```
```py
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-14-b509c97cb388> in <module>()
@@ -264,7 +264,7 @@ ZeroDivisionError: integer division or modulo by zero
In [15]:
```
```py
class ContextManager(object):
def __enter__(self):
@@ -280,13 +280,13 @@ class ContextManager(object):
In [16]:
```
```py
with ContextManager():
print 1/0
```
```
```py
Entering
Exiting
Exception suppresed: integer division or modulo by zero
@@ -301,7 +301,7 @@ Exiting
In [17]:
```
```py
class Transaction(object):
def __init__(self, connection):
@@ -324,7 +324,7 @@ class Transaction(object):
In [18]:
```
```py
import sqlite3 as db
connection = db.connect(":memory:")
@@ -344,7 +344,7 @@ with Transaction(connection) as cursor:
In [19]:
```
```py
with Transaction(connection) as cursor:
cursor.executemany("""INSERT OR REPLACE INTO addresses VALUES (?, ?, ?, ?, ?, ?)""", [
(0, '515 Congress Ave', 'Austin', 'Texas', 'USA', '78701'),
@@ -359,7 +359,7 @@ with Transaction(connection) as cursor:
In [20]:
```
```py
with Transaction(connection) as cursor:
cursor.execute("""INSERT OR REPLACE INTO addresses VALUES (?, ?, ?, ?, ?, ?)""",
(4, '2100 Pennsylvania Ave', 'Washington', 'DC', 'USA', '78701'),
@@ -368,7 +368,7 @@ with Transaction(connection) as cursor:
```
```
```py
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-20-ed8abdd56558> in <module>()
@@ -383,14 +383,14 @@ Exception: out of addresses
In [21]:
```
```py
cursor.execute("SELECT * FROM addresses")
for row in cursor:
print row
```
```
```py
(0, u'515 Congress Ave', u'Austin', u'Texas', u'USA', u'78701')
(1, u'245 Park Avenue', u'New York', u'New York', u'USA', u'10167')
(2, u'21 J.J. Thompson Ave.', u'Cambridge', None, u'UK', u'CB3 0FA')
@@ -406,7 +406,7 @@ for row in cursor:
In [23]:
```
```py
from contextlib import closing
import urllib
@@ -417,7 +417,7 @@ print html[:100]
```
```
```py
<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charse
```
@@ -426,7 +426,7 @@ print html[:100]
In [24]:
```
```py
from contextlib import contextmanager
@contextmanager
@@ -440,7 +440,7 @@ with my_contextmanager():
```
```
```py
Enter
Inside the with statement
Exit
@@ -453,7 +453,7 @@ Exit
In [25]:
```
```py
@contextmanager
def my_contextmanager():
print "Enter"
@@ -465,7 +465,7 @@ with my_contextmanager() as value:
```
```
```py
Enter
my value
Exit
@@ -476,7 +476,7 @@ Exit
In [26]:
```
```py
@contextmanager
def my_contextmanager():
print "Enter"
@@ -491,13 +491,13 @@ def my_contextmanager():
In [27]:
```
```py
with my_contextmanager():
print 1/0
```
```
```py
Enter
Error: integer division or modulo by zero
Exit
@@ -508,7 +508,7 @@ Exit
In [28]:
```
```py
@contextmanager
def transaction(connection):
cursor = connection.cursor()