mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Remove trailing whitespace.
This commit is contained in:
parent
3d3558a465
commit
48310cd3f2
127 changed files with 825 additions and 825 deletions
|
@ -399,8 +399,8 @@ string. It can optionally be limited to a fixed number of characters. ::
|
|||
|
||||
curses.echo() # Enable echoing of characters
|
||||
|
||||
# Get a 15-character string, with the cursor on the top line
|
||||
s = stdscr.getstr(0,0, 15)
|
||||
# Get a 15-character string, with the cursor on the top line
|
||||
s = stdscr.getstr(0,0, 15)
|
||||
|
||||
The Python :mod:`curses.textpad` module supplies something better. With it, you
|
||||
can turn a window into a text box that supports an Emacs-like set of
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
************************************
|
||||
Idioms and Anti-Idioms in Python
|
||||
Idioms and Anti-Idioms in Python
|
||||
************************************
|
||||
|
||||
:Author: Moshe Zadka
|
||||
|
@ -94,7 +94,7 @@ Bad example::
|
|||
# bar.py
|
||||
from foo import a
|
||||
if something():
|
||||
a = 2 # danger: foo.a != a
|
||||
a = 2 # danger: foo.a != a
|
||||
|
||||
Good example::
|
||||
|
||||
|
@ -271,6 +271,6 @@ It is usually much better to use the implicit continuation inside parenthesis:
|
|||
|
||||
This version is bulletproof::
|
||||
|
||||
value = (foo.bar()['first'][0]*baz.quux(1, 2)[5:9]
|
||||
value = (foo.bar()['first'][0]*baz.quux(1, 2)[5:9]
|
||||
+ calculate_number(10, 20)*forbulate(500, 360))
|
||||
|
||||
|
|
|
@ -145,7 +145,7 @@ than a large function that performs a complicated transformation. Small
|
|||
functions are also easier to read and to check for errors.
|
||||
|
||||
|
||||
Ease of debugging and testing
|
||||
Ease of debugging and testing
|
||||
-----------------------------
|
||||
|
||||
Testing and debugging a functional-style program is easier.
|
||||
|
@ -213,7 +213,7 @@ You can experiment with the iteration interface manually:
|
|||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in ?
|
||||
StopIteration
|
||||
>>>
|
||||
>>>
|
||||
|
||||
Python expects iterable objects in several different contexts, the most
|
||||
important being the ``for`` statement. In the statement ``for X in Y``, Y must
|
||||
|
@ -363,7 +363,7 @@ Generator expressions are surrounded by parentheses ("()") and list
|
|||
comprehensions are surrounded by square brackets ("[]"). Generator expressions
|
||||
have the form::
|
||||
|
||||
( expression for expr in sequence1
|
||||
( expression for expr in sequence1
|
||||
if condition1
|
||||
for expr2 in sequence2
|
||||
if condition2
|
||||
|
@ -405,7 +405,7 @@ equivalent to the following Python code::
|
|||
if not (conditionN):
|
||||
continue # Skip this element
|
||||
|
||||
# Output the value of
|
||||
# Output the value of
|
||||
# the expression.
|
||||
|
||||
This means that when there are multiple ``for...in`` clauses but no ``if``
|
||||
|
@ -419,8 +419,8 @@ list is 9 elements long:
|
|||
>>> seq1 = 'abc'
|
||||
>>> seq2 = (1,2,3)
|
||||
>>> [(x,y) for x in seq1 for y in seq2]
|
||||
[('a', 1), ('a', 2), ('a', 3),
|
||||
('b', 1), ('b', 2), ('b', 3),
|
||||
[('a', 1), ('a', 2), ('a', 3),
|
||||
('b', 1), ('b', 2), ('b', 3),
|
||||
('c', 1), ('c', 2), ('c', 3)]
|
||||
|
||||
To avoid introducing an ambiguity into Python's grammar, if ``expression`` is
|
||||
|
@ -627,7 +627,7 @@ Let's look in more detail at built-in functions often used with iterators.
|
|||
Two of Python's built-in functions, :func:`map` and :func:`filter` duplicate the
|
||||
features of generator expressions:
|
||||
|
||||
``map(f, iterA, iterB, ...)`` returns an iterator over the sequence
|
||||
``map(f, iterA, iterB, ...)`` returns an iterator over the sequence
|
||||
``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...``.
|
||||
|
||||
>>> def upper(s):
|
||||
|
@ -639,7 +639,7 @@ features of generator expressions:
|
|||
>>> [upper(s) for s in ['sentence', 'fragment']]
|
||||
['SENTENCE', 'FRAGMENT']
|
||||
|
||||
You can of course achieve the same effect with a list comprehension.
|
||||
You can of course achieve the same effect with a list comprehension.
|
||||
|
||||
``filter(predicate, iter)`` returns an iterator over all the sequence elements
|
||||
that meet a certain condition, and is similarly duplicated by list
|
||||
|
@ -709,7 +709,7 @@ values:
|
|||
True
|
||||
>>> all([0,1,0])
|
||||
False
|
||||
>>> all([0,0,0])
|
||||
>>> all([0,0,0])
|
||||
False
|
||||
>>> all([1,1,1])
|
||||
True
|
||||
|
@ -827,7 +827,7 @@ operators. Some examples are ``operator.add(a, b)`` (adds two values),
|
|||
``itertools.starmap(func, iter)`` assumes that the iterable will return a stream
|
||||
of tuples, and calls ``f()`` using these tuples as the arguments::
|
||||
|
||||
itertools.starmap(os.path.join,
|
||||
itertools.starmap(os.path.join,
|
||||
[('/usr', 'bin', 'java'), ('/bin', 'python'),
|
||||
('/usr', 'bin', 'perl'),('/usr', 'bin', 'ruby')])
|
||||
=>
|
||||
|
@ -887,9 +887,9 @@ value and an iterator for the elements with that key.
|
|||
|
||||
::
|
||||
|
||||
city_list = [('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL'),
|
||||
city_list = [('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL'),
|
||||
('Anchorage', 'AK'), ('Nome', 'AK'),
|
||||
('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ'),
|
||||
('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ'),
|
||||
...
|
||||
]
|
||||
|
||||
|
@ -904,7 +904,7 @@ value and an iterator for the elements with that key.
|
|||
where
|
||||
iterator-1 =>
|
||||
('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL')
|
||||
iterator-2 =>
|
||||
iterator-2 =>
|
||||
('Anchorage', 'AK'), ('Nome', 'AK')
|
||||
iterator-3 =>
|
||||
('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ')
|
||||
|
@ -1045,7 +1045,7 @@ is equivalent to ::
|
|||
|
||||
>>> double(add(5, 6))
|
||||
22
|
||||
|
||||
|
||||
The ``unpack`` keyword is provided to work around the fact that Python functions
|
||||
are not always `fully curried <http://en.wikipedia.org/wiki/Currying>`__. By
|
||||
default, it is expected that the ``inner`` function will return a single object
|
||||
|
@ -1054,15 +1054,15 @@ and that the ``outer`` function will take a single argument. Setting the
|
|||
will be expanded before being passed to ``outer``. Put simply, ::
|
||||
|
||||
compose(f, g)(5, 6)
|
||||
|
||||
|
||||
is equivalent to::
|
||||
|
||||
f(g(5, 6))
|
||||
|
||||
|
||||
while ::
|
||||
|
||||
compose(f, g, unpack=True)(5, 6)
|
||||
|
||||
|
||||
is equivalent to::
|
||||
|
||||
f(*g(5, 6))
|
||||
|
@ -1074,21 +1074,21 @@ provided by both ``functional`` and ``functools``). ::
|
|||
|
||||
from functional import compose, partial
|
||||
import functools
|
||||
|
||||
|
||||
|
||||
multi_compose = partial(functools.reduce, compose)
|
||||
|
||||
|
||||
|
||||
|
||||
We can also use ``map()``, ``compose()`` and ``partial()`` to craft a version of
|
||||
``"".join(...)`` that converts its arguments to string::
|
||||
|
||||
from functional import compose, partial
|
||||
|
||||
|
||||
join = compose("".join, partial(map, str))
|
||||
|
||||
|
||||
``flip(func)``
|
||||
|
||||
|
||||
``flip()`` wraps the callable in ``func`` and causes it to receive its
|
||||
non-keyword arguments in reverse order. ::
|
||||
|
||||
|
@ -1103,7 +1103,7 @@ non-keyword arguments in reverse order. ::
|
|||
(7, 6, 5)
|
||||
|
||||
``foldl(func, start, iterable)``
|
||||
|
||||
|
||||
``foldl()`` takes a binary function, a starting value (usually some kind of
|
||||
'zero'), and an iterable. The function is applied to the starting value and the
|
||||
first element of the list, then the result of that and the second element of the
|
||||
|
@ -1117,7 +1117,7 @@ is equivalent to::
|
|||
|
||||
f(f(f(0, 1), 2), 3)
|
||||
|
||||
|
||||
|
||||
``foldl()`` is roughly equivalent to the following recursive function::
|
||||
|
||||
def foldl(func, start, seq):
|
||||
|
@ -1224,7 +1224,7 @@ Fredrik Lundh once suggested the following set of rules for refactoring uses of
|
|||
4) Convert the lambda to a def statement, using that name.
|
||||
5) Remove the comment.
|
||||
|
||||
I really like these rules, but you're free to disagree
|
||||
I really like these rules, but you're free to disagree
|
||||
about whether this lambda-free style is better.
|
||||
|
||||
|
||||
|
@ -1282,7 +1282,7 @@ for text processing, in the section titled "Utilizing Higher-Order Functions in
|
|||
Text Processing".
|
||||
|
||||
Mertz also wrote a 3-part series of articles on functional programming
|
||||
for IBM's DeveloperWorks site; see
|
||||
for IBM's DeveloperWorks site; see
|
||||
`part 1 <http://www-128.ibm.com/developerworks/library/l-prog.html>`__,
|
||||
`part 2 <http://www-128.ibm.com/developerworks/library/l-prog2.html>`__, and
|
||||
`part 3 <http://www-128.ibm.com/developerworks/linux/library/l-prog3.html>`__,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
****************************
|
||||
Socket Programming HOWTO
|
||||
Socket Programming HOWTO
|
||||
****************************
|
||||
|
||||
:Author: Gordon McMillan
|
||||
|
@ -62,7 +62,7 @@ your browser did something like the following::
|
|||
|
||||
#create an INET, STREAMing socket
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
#now connect to the web server on port 80
|
||||
#now connect to the web server on port 80
|
||||
# - the normal http port
|
||||
s.connect(("www.mcmillan-inc.com", 80))
|
||||
|
||||
|
@ -77,7 +77,7 @@ creates a "server socket". ::
|
|||
#create an INET, STREAMing socket
|
||||
serversocket = socket.socket(
|
||||
socket.AF_INET, socket.SOCK_STREAM)
|
||||
#bind the socket to a public host,
|
||||
#bind the socket to a public host,
|
||||
# and a well-known port
|
||||
serversocket.bind((socket.gethostname(), 80))
|
||||
#become a server socket
|
||||
|
@ -184,7 +184,7 @@ Assuming you don't want to end the connection, the simplest solution is a fixed
|
|||
length message::
|
||||
|
||||
class mysocket:
|
||||
"""demonstration class only
|
||||
"""demonstration class only
|
||||
- coded for clarity, not efficiency
|
||||
"""
|
||||
|
||||
|
@ -340,9 +340,9 @@ you'll have little trouble with it in C. ::
|
|||
|
||||
ready_to_read, ready_to_write, in_error = \
|
||||
select.select(
|
||||
potential_readers,
|
||||
potential_writers,
|
||||
potential_errs,
|
||||
potential_readers,
|
||||
potential_writers,
|
||||
potential_errs,
|
||||
timeout)
|
||||
|
||||
You pass ``select`` three lists: the first contains all sockets that you might
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
HOWTO, available at `urllib2 - Le Manuel manquant
|
||||
<http://www.voidspace.org.uk/python/articles/urllib2_francais.shtml>`_.
|
||||
|
||||
|
||||
|
||||
|
||||
Introduction
|
||||
============
|
||||
|
@ -19,9 +19,9 @@ Introduction
|
|||
|
||||
You may also find useful the following article on fetching web resources
|
||||
with Python:
|
||||
|
||||
|
||||
* `Basic Authentication <http://www.voidspace.org.uk/python/articles/authentication.shtml>`_
|
||||
|
||||
|
||||
A tutorial on *Basic Authentication*, with examples in Python.
|
||||
|
||||
**urllib.request** is a `Python <http://www.python.org>`_ module for fetching URLs
|
||||
|
@ -98,7 +98,7 @@ argument. The encoding is done using a function from the :mod:`urllib.parse`
|
|||
library. ::
|
||||
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
import urllib.request
|
||||
|
||||
url = 'http://www.someserver.com/cgi-bin/register.cgi'
|
||||
values = {'name' : 'Michael Foord',
|
||||
|
@ -161,15 +161,15 @@ request as above, but identifies itself as a version of Internet
|
|||
Explorer [#]_. ::
|
||||
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
import urllib.request
|
||||
|
||||
url = 'http://www.someserver.com/cgi-bin/register.cgi'
|
||||
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
|
||||
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
|
||||
values = {'name' : 'Michael Foord',
|
||||
'location' : 'Northampton',
|
||||
'language' : 'Python' }
|
||||
headers = { 'User-Agent' : user_agent }
|
||||
|
||||
|
||||
data = urllib.parse.urlencode(values)
|
||||
req = urllib.request.Request(url, data, headers)
|
||||
response = urllib.request.urlopen(req)
|
||||
|
@ -183,7 +183,7 @@ Handling Exceptions
|
|||
===================
|
||||
|
||||
*urlopen* raises :exc:`URLError` when it cannot handle a response (though as usual
|
||||
with Python APIs, builtin exceptions such as
|
||||
with Python APIs, builtin exceptions such as
|
||||
:exc:`ValueError`, :exc:`TypeError` etc. may also
|
||||
be raised).
|
||||
|
||||
|
@ -311,18 +311,18 @@ page returned. This means that as well as the code attribute, it also has read,
|
|||
geturl, and info, methods as returned by the ``urllib.response`` module::
|
||||
|
||||
>>> req = urllib.request.Request('http://www.python.org/fish.html')
|
||||
>>> try:
|
||||
>>> try:
|
||||
>>> urllib.request.urlopen(req)
|
||||
>>> except urllib.error.URLError, e:
|
||||
>>> print(e.code)
|
||||
>>> print(e.read())
|
||||
>>>
|
||||
>>>
|
||||
404
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
"http://www.w3.org/TR/html4/loose.dtd">
|
||||
<?xml-stylesheet href="./css/ht2html.css"
|
||||
<?xml-stylesheet href="./css/ht2html.css"
|
||||
type="text/css"?>
|
||||
<html><head><title>Error 404: File Not Found</title>
|
||||
<html><head><title>Error 404: File Not Found</title>
|
||||
...... etc...
|
||||
|
||||
Wrapping it Up
|
||||
|
@ -376,7 +376,7 @@ Number 2
|
|||
print('Error code: ', e.code)
|
||||
else:
|
||||
# everything is fine
|
||||
|
||||
|
||||
|
||||
info and geturl
|
||||
===============
|
||||
|
@ -448,7 +448,7 @@ error code) requesting authentication. This specifies the authentication scheme
|
|||
and a 'realm'. The header looks like : ``Www-authenticate: SCHEME
|
||||
realm="REALM"``.
|
||||
|
||||
e.g. ::
|
||||
e.g. ::
|
||||
|
||||
Www-authenticate: Basic realm="cPanel Users"
|
||||
|
||||
|
@ -472,24 +472,24 @@ The top-level URL is the first URL that requires authentication. URLs "deeper"
|
|||
than the URL you pass to .add_password() will also match. ::
|
||||
|
||||
# create a password manager
|
||||
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
|
||||
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
|
||||
|
||||
# Add the username and password.
|
||||
# If we knew the realm, we could use it instead of ``None``.
|
||||
top_level_url = "http://example.com/foo/"
|
||||
password_mgr.add_password(None, top_level_url, username, password)
|
||||
|
||||
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
|
||||
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
|
||||
|
||||
# create "opener" (OpenerDirector instance)
|
||||
opener = urllib.request.build_opener(handler)
|
||||
opener = urllib.request.build_opener(handler)
|
||||
|
||||
# use the opener to fetch a URL
|
||||
opener.open(a_url)
|
||||
opener.open(a_url)
|
||||
|
||||
# Install the opener.
|
||||
# Now all calls to urllib.request.urlopen use our opener.
|
||||
urllib.request.install_opener(opener)
|
||||
urllib.request.install_opener(opener)
|
||||
|
||||
.. note::
|
||||
|
||||
|
@ -545,7 +545,7 @@ However, you can set the default timeout globally for all sockets using ::
|
|||
|
||||
# timeout in seconds
|
||||
timeout = 10
|
||||
socket.setdefaulttimeout(timeout)
|
||||
socket.setdefaulttimeout(timeout)
|
||||
|
||||
# this call to urllib.request.urlopen now uses the default timeout
|
||||
# we have set in the socket module
|
||||
|
@ -562,7 +562,7 @@ Footnotes
|
|||
This document was reviewed and revised by John Lee.
|
||||
|
||||
.. [#] For an introduction to the CGI protocol see
|
||||
`Writing Web Applications in Python <http://www.pyzine.com/Issue008/Section_Articles/article_CGIOne.html>`_.
|
||||
`Writing Web Applications in Python <http://www.pyzine.com/Issue008/Section_Articles/article_CGIOne.html>`_.
|
||||
.. [#] Like Google for example. The *proper* way to use google from a program
|
||||
is to use `PyGoogle <http://pygoogle.sourceforge.net>`_ of course. See
|
||||
`Voidspace Google <http://www.voidspace.org.uk/python/recipebook.shtml#google>`_
|
||||
|
@ -579,6 +579,6 @@ This document was reviewed and revised by John Lee.
|
|||
is set to use the proxy, which urllib picks up on. In order to test
|
||||
scripts with a localhost server, I have to prevent urllib from using
|
||||
the proxy.
|
||||
.. [#] urllib opener for SSL proxy (CONNECT method): `ASPN Cookbook Recipe
|
||||
.. [#] urllib opener for SSL proxy (CONNECT method): `ASPN Cookbook Recipe
|
||||
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/456195>`_.
|
||||
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ they can be run as CGI if no better option is available.
|
|||
<http://wiki.python.org/moin/CgiScripts>`_ with some additional information
|
||||
about CGI in Python.
|
||||
|
||||
|
||||
|
||||
Simple script for testing CGI
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
@ -387,7 +387,7 @@ compared with other web techniques.
|
|||
|
||||
You might be interested in some WSGI-supporting modules already contained in
|
||||
the standard library, namely:
|
||||
|
||||
|
||||
* :mod:`wsgiref` -- some tiny utilities and servers for WSGI
|
||||
|
||||
|
||||
|
@ -500,7 +500,7 @@ using these is a good idea.
|
|||
time in looking through the most popular ones. Some frameworks have their
|
||||
own template engine or have a recommentation for one. It's wise to use
|
||||
these.
|
||||
|
||||
|
||||
Popular template engines include:
|
||||
|
||||
* Mako
|
||||
|
@ -688,7 +688,7 @@ And that's still not everything. The most up-to-date information can always be
|
|||
found in the Python wiki.
|
||||
|
||||
.. seealso::
|
||||
|
||||
|
||||
The Python wiki contains an extensive list of `web frameworks
|
||||
<http://wiki.python.org/moin/WebFrameworks>`_.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue