Commit c7b6908b authored by Georg Brandl's avatar Georg Brandl

Merged revisions...

Merged revisions 82262,82269,82434,82480-82481,82484-82485,82487-82488,82594,82599,82615 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k

................
  r82262 | georg.brandl | 2010-06-27 12:17:12 +0200 (So, 27 Jun 2010) | 1 line

  #9078: fix some Unicode C API descriptions, in comments and docs.
................
  r82269 | georg.brandl | 2010-06-27 12:59:19 +0200 (So, 27 Jun 2010) | 1 line

  Wording fix.
................
  r82434 | georg.brandl | 2010-07-02 09:41:51 +0200 (Fr, 02 Jul 2010) | 9 lines

  Merged revisions 82433 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r82433 | georg.brandl | 2010-07-02 09:33:50 +0200 (Fr, 02 Jul 2010) | 1 line

    Grammar and markup fixes.
  ........
................
  r82480 | georg.brandl | 2010-07-03 12:21:50 +0200 (Sa, 03 Jul 2010) | 1 line

  Wrap and use the correct directive.
................
  r82481 | georg.brandl | 2010-07-03 12:22:10 +0200 (Sa, 03 Jul 2010) | 1 line

  Use the right role.
................
  r82484 | georg.brandl | 2010-07-03 12:26:17 +0200 (Sa, 03 Jul 2010) | 9 lines

  Recorded merge of revisions 82474 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r82474 | georg.brandl | 2010-07-03 10:40:13 +0200 (Sa, 03 Jul 2010) | 1 line

    Fix role name.
  ........
................
  r82485 | georg.brandl | 2010-07-03 12:26:54 +0200 (Sa, 03 Jul 2010) | 9 lines

  Merged revisions 82483 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk

  ........
    r82483 | georg.brandl | 2010-07-03 12:25:54 +0200 (Sa, 03 Jul 2010) | 1 line

    Add link to bytecode docs.
  ........
................
  r82487 | georg.brandl | 2010-07-03 12:33:26 +0200 (Sa, 03 Jul 2010) | 1 line

  Fix markup.
................
  r82488 | georg.brandl | 2010-07-03 12:41:33 +0200 (Sa, 03 Jul 2010) | 1 line

  Remove the need for a "()" empty argument list after opcodes.
................
  r82594 | georg.brandl | 2010-07-05 22:13:41 +0200 (Mo, 05 Jul 2010) | 1 line

  Update Vec class constructor, remove indirection via function, use operator module.
................
  r82599 | alexander.belopolsky | 2010-07-05 23:44:05 +0200 (Mo, 05 Jul 2010) | 1 line

  "Modernized" the demo a little.
................
  r82615 | georg.brandl | 2010-07-07 00:58:50 +0200 (Mi, 07 Jul 2010) | 1 line

  Fix typo.
................
parent 16489247
# A simple vector class class Vec:
""" A simple vector class
Instances of the Vec class can be constructed from numbers
def vec(*v): >>> a = Vec(1, 2, 3)
return Vec(*v) >>> b = Vec(3, 2, 1)
added
>>> a + b
Vec(4, 4, 4)
class Vec: subtracted
>>> a - b
Vec(-2, 0, 2)
and multiplied by a scalar on the left
>>> 3.0 * a
Vec(3.0, 6.0, 9.0)
or on the right
>>> a * 3.0
Vec(3.0, 6.0, 9.0)
"""
def __init__(self, *v): def __init__(self, *v):
self.v = list(v) self.v = list(v)
def fromlist(self, v): @classmethod
def fromlist(cls, v):
if not isinstance(v, list): if not isinstance(v, list):
raise TypeError raise TypeError
self.v = v[:] inst = cls()
return self inst.v = v
return inst
def __repr__(self): def __repr__(self):
return 'vec(' + repr(self.v)[1:-1] + ')' args = ', '.join(repr(x) for x in self.v)
return 'Vec({})'.format(args)
def __len__(self): def __len__(self):
return len(self.v) return len(self.v)
...@@ -27,28 +45,24 @@ class Vec: ...@@ -27,28 +45,24 @@ class Vec:
def __add__(self, other): def __add__(self, other):
# Element-wise addition # Element-wise addition
v = list(map(lambda x, y: x+y, self, other)) v = [x + y for x, y in zip(self.v, other.v)]
return Vec().fromlist(v) return Vec.fromlist(v)
def __sub__(self, other): def __sub__(self, other):
# Element-wise subtraction # Element-wise subtraction
v = list(map(lambda x, y: x-y, self, other)) v = [x - y for x, y in zip(self.v, other.v)]
return Vec().fromlist(v) return Vec.fromlist(v)
def __mul__(self, scalar): def __mul__(self, scalar):
# Multiply by scalar # Multiply by scalar
v = [x*scalar for x in self.v] v = [x * scalar for x in self.v]
return Vec().fromlist(v) return Vec.fromlist(v)
__rmul__ = __mul__
def test(): def test():
a = vec(1, 2, 3) import doctest
b = vec(3, 2, 1) doctest.testmod()
print(a)
print(b)
print(a+b)
print(a-b)
print(a*3.0)
test() test()
...@@ -328,10 +328,10 @@ APIs: ...@@ -328,10 +328,10 @@ APIs:
Coerce an encoded object *obj* to an Unicode object and return a reference with Coerce an encoded object *obj* to an Unicode object and return a reference with
incremented refcount. incremented refcount.
String and other char buffer compatible objects are decoded according to the :class:`bytes`, :class:`bytearray` and other char buffer compatible objects
given encoding and using the error handling defined by errors. Both can be are decoded according to the given encoding and using the error handling
*NULL* to have the interface use the default values (see the next section for defined by errors. Both can be *NULL* to have the interface use the default
details). values (see the next section for details).
All other objects, including Unicode objects, cause a :exc:`TypeError` to be All other objects, including Unicode objects, cause a :exc:`TypeError` to be
set. set.
......
...@@ -63,6 +63,9 @@ Glossary ...@@ -63,6 +63,9 @@ Glossary
"intermediate language" is said to run on a :term:`virtual machine` "intermediate language" is said to run on a :term:`virtual machine`
that executes the machine code corresponding to each bytecode. that executes the machine code corresponding to each bytecode.
A list of bytecode instructions can be found in the documentation for
:ref:`the dis module <bytecodes>`.
class class
A template for creating user-defined objects. Class definitions A template for creating user-defined objects. Class definitions
normally contain method definitions which operate on instances of the normally contain method definitions which operate on instances of the
......
This diff is collapsed.
...@@ -284,30 +284,30 @@ of which this module provides three different variants: ...@@ -284,30 +284,30 @@ of which this module provides three different variants:
For example usage, see the implementation of the :func:`test` function For example usage, see the implementation of the :func:`test` function
invocation in the :mod:`http.server` module. invocation in the :mod:`http.server` module.
The :class:`SimpleHTTPRequestHandler` class can be invoked the following manner
with the :mod:`http.server` to create a very basic webserver serving files
relative to the current directory.::
import http.server The :class:`SimpleHTTPRequestHandler` class can be used in the following
import socketserver manner in order to create a very basic webserver serving files relative to
the current directory. ::
PORT = 8000 import http.server
import socketserver
Handler = http.server.SimpleHTTPRequestHandler PORT = 8000
httpd = socketserver.TCPServer(("", PORT), Handler) Handler = http.server.SimpleHTTPRequestHandler
print("serving at port", PORT) httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()
:mod:`http.server` can also be invoked directly using the ``-m`` switch of print("serving at port", PORT)
interpreter a with ``port number`` argument which uses httpd.serve_forever()
:class:`SimpleHTTPRequestHandler` as the default request Handler. Similar to
the previous example, even this serves files relative to the current :mod:`http.server` can also be invoked directly using the :option:`-m`
directory.:: switch of the interpreter a with ``port number`` argument. Similar to
the previous example, this serves files relative to the current directory. ::
python -m http.server 8000 python -m http.server 8000
.. class:: CGIHTTPRequestHandler(request, client_address, server) .. class:: CGIHTTPRequestHandler(request, client_address, server)
This class is used to serve either files or output of CGI scripts from the This class is used to serve either files or output of CGI scripts from the
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
Sphinx extension with Python doc-specific markup. Sphinx extension with Python doc-specific markup.
:copyright: 2008, 2009 by Georg Brandl. :copyright: 2008, 2009, 2010 by Georg Brandl.
:license: Python license. :license: Python license.
""" """
...@@ -149,7 +149,7 @@ import suspicious ...@@ -149,7 +149,7 @@ import suspicious
import re import re
from sphinx import addnodes from sphinx import addnodes
opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)\s*\((.*)\)') opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')
def parse_opcode_signature(env, sig, signode): def parse_opcode_signature(env, sig, signode):
"""Transform an opcode signature into RST nodes.""" """Transform an opcode signature into RST nodes."""
...@@ -158,9 +158,10 @@ def parse_opcode_signature(env, sig, signode): ...@@ -158,9 +158,10 @@ def parse_opcode_signature(env, sig, signode):
raise ValueError raise ValueError
opname, arglist = m.groups() opname, arglist = m.groups()
signode += addnodes.desc_name(opname, opname) signode += addnodes.desc_name(opname, opname)
paramlist = addnodes.desc_parameterlist() if arglist is not None:
signode += paramlist paramlist = addnodes.desc_parameterlist()
paramlist += addnodes.desc_parameter(arglist, arglist) signode += paramlist
paramlist += addnodes.desc_parameter(arglist, arglist)
return opname.strip() return opname.strip()
......
...@@ -87,7 +87,6 @@ The :func:`range` Function ...@@ -87,7 +87,6 @@ The :func:`range` Function
If you do need to iterate over a sequence of numbers, the built-in function If you do need to iterate over a sequence of numbers, the built-in function
:func:`range` comes in handy. It generates arithmetic progressions:: :func:`range` comes in handy. It generates arithmetic progressions::
>>> for i in range(5): >>> for i in range(5):
... print(i) ... print(i)
... ...
...@@ -97,9 +96,7 @@ If you do need to iterate over a sequence of numbers, the built-in function ...@@ -97,9 +96,7 @@ If you do need to iterate over a sequence of numbers, the built-in function
3 3
4 4
The given end point is never part of the generated sequence; ``range(10)`` generates
The given end point is never part of the generated list; ``range(10)`` generates
10 values, the legal indices for items of a sequence of length 10. It 10 values, the legal indices for items of a sequence of length 10. It
is possible to let the range start at another number, or to specify a different is possible to let the range start at another number, or to specify a different
increment (even negative; sometimes this is called the 'step'):: increment (even negative; sometimes this is called the 'step')::
......
...@@ -58,7 +58,7 @@ source. ...@@ -58,7 +58,7 @@ source.
.. cmdoption:: -c <command> .. cmdoption:: -c <command>
Execute the Python code in *command*. *command* can be one ore more Execute the Python code in *command*. *command* can be one or more
statements separated by newlines, with significant leading whitespace as in statements separated by newlines, with significant leading whitespace as in
normal module code. normal module code.
......
...@@ -496,14 +496,14 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode( ...@@ -496,14 +496,14 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
Py_ssize_t size /* size of buffer */ Py_ssize_t size /* size of buffer */
); );
/* Similar to PyUnicode_FromUnicode(), but u points to Latin-1 encoded bytes */ /* Similar to PyUnicode_FromUnicode(), but u points to UTF-8 encoded bytes */
PyAPI_FUNC(PyObject*) PyUnicode_FromStringAndSize( PyAPI_FUNC(PyObject*) PyUnicode_FromStringAndSize(
const char *u, /* char buffer */ const char *u, /* char buffer */
Py_ssize_t size /* size of buffer */ Py_ssize_t size /* size of buffer */
); );
/* Similar to PyUnicode_FromUnicode(), but u points to null-terminated /* Similar to PyUnicode_FromUnicode(), but u points to null-terminated
Latin-1 encoded bytes */ UTF-8 encoded bytes */
PyAPI_FUNC(PyObject*) PyUnicode_FromString( PyAPI_FUNC(PyObject*) PyUnicode_FromString(
const char *u /* string */ const char *u /* string */
); );
...@@ -548,7 +548,7 @@ PyAPI_FUNC(int) PyUnicode_Resize( ...@@ -548,7 +548,7 @@ PyAPI_FUNC(int) PyUnicode_Resize(
Coercion is done in the following way: Coercion is done in the following way:
1. String and other char buffer compatible objects are decoded 1. bytes, bytearray and other char buffer compatible objects are decoded
under the assumptions that they contain data using the current under the assumptions that they contain data using the current
default encoding. Decoding is done in "strict" mode. default encoding. Decoding is done in "strict" mode.
...@@ -572,7 +572,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject( ...@@ -572,7 +572,7 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
Unicode objects are passed back as-is (subclasses are converted to Unicode objects are passed back as-is (subclasses are converted to
true Unicode objects), all other objects are delegated to true Unicode objects), all other objects are delegated to
PyUnicode_FromEncodedObject(obj, NULL, "strict") which results in PyUnicode_FromEncodedObject(obj, NULL, "strict") which results in
using the default encoding as basis for decoding the object. using UTF-8 encoding as basis for decoding the object.
The API returns NULL in case of an error. The caller is responsible The API returns NULL in case of an error. The caller is responsible
for decref'ing the returned objects. for decref'ing the returned objects.
...@@ -604,7 +604,7 @@ PyAPI_FUNC(void) _Py_ReleaseInternedUnicodeStrings(void); ...@@ -604,7 +604,7 @@ PyAPI_FUNC(void) _Py_ReleaseInternedUnicodeStrings(void);
#ifdef HAVE_WCHAR_H #ifdef HAVE_WCHAR_H
/* Create a Unicode Object from the whcar_t buffer w of the given /* Create a Unicode Object from the wchar_t buffer w of the given
size. size.
The buffer is copied into the new object. */ The buffer is copied into the new object. */
...@@ -663,7 +663,7 @@ PyAPI_FUNC(int) PyUnicode_ClearFreeList(void); ...@@ -663,7 +663,7 @@ PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
parameters encoding and errors have the same semantics as the ones parameters encoding and errors have the same semantics as the ones
of the builtin unicode() API. of the builtin unicode() API.
Setting encoding to NULL causes the default encoding to be used. Setting encoding to NULL causes the default encoding (UTF-8) to be used.
Error handling is set by errors which may also be set to NULL Error handling is set by errors which may also be set to NULL
meaning to use the default handling defined for the codec. Default meaning to use the default handling defined for the codec. Default
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment