Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a3dd56b6
Commit
a3dd56b6
authored
Mar 11, 2011
by
Éric Araujo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use with statement where it improves the documentation (closes #10461)
parent
17b880a5
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
16 additions
and
8 deletions
+16
-8
Doc/library/atexit.rst
Doc/library/atexit.rst
+7
-2
Doc/library/cmd.rst
Doc/library/cmd.rst
+2
-2
Doc/library/collections.rst
Doc/library/collections.rst
+2
-1
Doc/library/difflib.rst
Doc/library/difflib.rst
+2
-2
Doc/tutorial/stdlib2.rst
Doc/tutorial/stdlib2.rst
+3
-1
No files found.
Doc/library/atexit.rst
View file @
a3dd56b6
...
@@ -61,17 +61,22 @@ from a file when it is imported and save the counter's updated value
...
@@ -61,17 +61,22 @@ from a file when it is imported and save the counter's updated value
automatically when the program terminates without relying on the application
automatically when the program terminates without relying on the application
making an explicit call into this module at termination. ::
making an explicit call into this module at termination. ::
infile = open("/tmp/counter")
try:
try:
_count = int(
open("/tmp/counter")
.read())
_count = int(
infile
.read())
except IOError:
except IOError:
_count = 0
_count = 0
finally:
infile.close()
def incrcounter(n):
def incrcounter(n):
global _count
global _count
_count = _count + n
_count = _count + n
def savecounter():
def savecounter():
open("/tmp/counter", "w").write("%d" % _count)
with open("/tmp/counter", "w") as outfile:
outfile.write("%d" % _count)
import atexit
import atexit
atexit.register(savecounter)
atexit.register(savecounter)
...
...
Doc/library/cmd.rst
View file @
a3dd56b6
...
@@ -282,8 +282,8 @@ immediate playback::
...
@@ -282,8 +282,8 @@ immediate playback::
def do_playback(self, arg):
def do_playback(self, arg):
'Playback commands from a file: PLAYBACK rose.cmd'
'Playback commands from a file: PLAYBACK rose.cmd'
self.close()
self.close()
cmds = open(arg).read().splitlines()
with open(arg) as f:
self.cmdqueue.extend(cmds
)
self.cmdqueue.extend(f.read().splitlines()
)
def precmd(self, line):
def precmd(self, line):
line = line.lower()
line = line.lower()
if self.file and 'playback' not in line:
if self.file and 'playback' not in line:
...
...
Doc/library/collections.rst
View file @
a3dd56b6
...
@@ -512,7 +512,8 @@ in Unix::
...
@@ -512,7 +512,8 @@ in Unix::
def tail(filename, n=10):
def tail(filename, n=10):
'Return the last n lines of a file'
'Return the last n lines of a file'
return deque(open(filename), n)
with open(filename) as f:
return deque(f, n)
Another approach to using deques is to maintain a sequence of recently
Another approach to using deques is to maintain a sequence of recently
added elements by appending to the right and popping to the left::
added elements by appending to the right and popping to the left::
...
...
Doc/library/difflib.rst
View file @
a3dd56b6
...
@@ -750,8 +750,8 @@ It is also contained in the Python source distribution, as
...
@@ -750,8 +750,8 @@ It is also contained in the Python source distribution, as
# we're passing these as arguments to the diff function
# we're passing these as arguments to the diff function
fromdate = time.ctime(os.stat(fromfile).st_mtime)
fromdate = time.ctime(os.stat(fromfile).st_mtime)
todate = time.ctime(os.stat(tofile).st_mtime)
todate = time.ctime(os.stat(tofile).st_mtime)
fromlines = open(fromfile, 'U').readlines()
with open(fromlines) as fromf, open(tofile) as tof:
tolines = open(tofile, 'U').readlines(
)
fromlines, tolines = list(fromf), list(tof
)
if options.u:
if options.u:
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
...
...
Doc/tutorial/stdlib2.rst
View file @
a3dd56b6
...
@@ -141,7 +141,9 @@ standard size and in little-endian byte order::
...
@@ -141,7 +141,9 @@ standard size and in little-endian byte order::
import struct
import struct
data = open('myfile.zip', 'rb').read()
with open('myfile.zip', 'rb') as f:
data = f.read()
start = 0
start = 0
for i in range(3): # show the first 3 file headers
for i in range(3): # show the first 3 file headers
start += 14
start += 14
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment