binaryfunc nb_inplace_add;
binaryfunc nb_inplace_subtract;
binaryfunc nb_inplace_multiply;
+ binaryfunc nb_inplace_divide;
binaryfunc nb_inplace_remainder;
ternaryfunc nb_inplace_power;
binaryfunc nb_inplace_lshift;
Regular Expression HOWTO
****************************
-:Author: A.M. Kuchling
+:Author: A.M. Kuchling <amk@amk.ca>
:Release: 0.05
.. TODO:
from contextlib import nested
- with nested(A, B, C) as (X, Y, Z):
+ with nested(A(), B(), C()) as (X, Y, Z):
do_something()
is equivalent to this::
- with A as X:
- with B as Y:
- with C as Z:
+ m1, m2, m3 = A(), B(), C()
+ with m1 as X:
+ with m2 as Y:
+ with m3 as Z:
do_something()
Note that if the :meth:`__exit__` method of one of the nested context managers
its :meth:`__next__` method; if the value returned is equal to *sentinel*,
:exc:`StopIteration` will be raised, otherwise the value will be returned.
+ One useful application of the second form of :func:`iter` is to read lines of
+ a file until a certain line is reached. The following example reads a file
+ until ``"STOP"`` is reached: ::
+
+ with open("mydata.txt") as fp:
+ for line in iter(fp.readline, "STOP"):
+ process_line(line)
+
.. function:: len(s)
black
+Startup
+-------
+
+Upon startup with the ``-s`` option, IDLE will execute the file referenced by
+the environment variables :envvar:`IDLESTARTUP` or :envvar:`PYTHONSTARTUP`.
+Idle first checks for ``IDLESTARTUP``; if ``IDLESTARTUP`` is present the file
+referenced is run. If ``IDLESTARTUP`` is not present, Idle checks for
+``PYTHONSTARTUP``. Files referenced by these environment variables are
+convenient places to store functions that are used frequently from the Idle
+shell, or for executing import statements to import common modules.
+
+In addition, ``Tk`` also loads a startup file if it is present. Note that the
+Tk file is loaded unconditionally. This additional file is ``.Idle.py`` and is
+looked for in the user's home directory. Statements in this file will be
+executed in the Tk namespace, so this file is not useful for importing functions
+to be used from Idle's Python shell.
+
+
Command line usage
^^^^^^^^^^^^^^^^^^
string)`` or ``re.search(pattern, string)``.
:func:`match` has an optional second parameter that gives an index in the string
-where the search is to start:
+where the search is to start::
>>> pattern = re.compile("o")
>>> pattern.match("dog") # No match as "o" is not at the start of "dog."
.. productionlist:: sf
replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}"
- field_name: (`identifier` | `integer`) ("." `attribute_name` | "[" element_index "]")*
+ field_name: (`identifier` | `integer`) ("." `attribute_name` | "[" `element_index` "]")*
attribute_name: `identifier`
element_index: `integer`
conversion: "r" | "s" | "a"
Same as equivalent method in the :class:`Document` class.
-.. method:: Element.getElementsByTagNameNS(tagName)
+.. method:: Element.getElementsByTagNameNS(namespaceURI, localName)
Same as equivalent method in the :class:`Document` class.
.. index:: single: __len__() (mapping object method)
- Called to implement truth value testing, and the built-in operation ``bool()``;
- should return ``False`` or ``True``. When this method is not defined,
- :meth:`__len__` is called, if it is defined (see below) and ``True`` is returned
- when the length is not zero. If a class defines neither :meth:`__len__` nor
- :meth:`__bool__`, all its instances are considered true.
+ Called to implement truth value testing and the built-in operation
+ ``bool()``; should return ``False`` or ``True``, or their integer equivalents
+ ``0`` or ``1``. When this method is not defined, :meth:`__len__` is called,
+ if it is defined, and the object is considered true if its result is nonzero.
+ If a class defines neither :meth:`__len__` nor :meth:`__bool__`, all its
+ instances are considered true.
.. _attribute-access:
if sys.platform != 'win32':
# this test is only for win32
return
+ from distutils.msvccompiler import get_build_version
+ if get_build_version() < 8.0:
+ # this test is only for MSVC8.0 or above
+ return
from distutils.msvc9compiler import Reg
self.assertRaises(KeyError, Reg.get_value, 'xxx', 'xxx')
import os
import stat
+import contextlib
from itertools import filterfalse
__all__ = ["cmp","dircmp","cmpfiles"]
def _do_cmp(f1, f2):
bufsize = BUFSIZE
- fp1 = open(f1, 'rb')
- fp2 = open(f2, 'rb')
- while True:
- b1 = fp1.read(bufsize)
- b2 = fp2.read(bufsize)
- if b1 != b2:
- return False
- if not b1:
- return True
+ with contextlib.nested(open(f1, 'rb'), open(f2, 'rb')) as (fp1, fp2):
+ while True:
+ b1 = fp1.read(bufsize)
+ b2 = fp2.read(bufsize)
+ if b1 != b2:
+ return False
+ if not b1:
+ return True
# Directory comparison class.
#
"lookup(encoding) -> CodecInfo\n\
\n\
Looks up a codec tuple in the Python codec registry and returns\n\
-a tuple of function (or a CodecInfo object).");
+a CodecInfo object.");
static
PyObject *codec_lookup(PyObject *self, PyObject *args)
else if (cmp < 0)
return NULL;
}
- PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in list");
+ PyErr_SetString(PyExc_ValueError, "tuple.index(x): x not in tuple");
return NULL;
}
PyObject *func, *args;
static PyObject *bool_str, *len_str;
int result = -1;
- int from_len = 0;
+ int using_len = 0;
func = lookup_maybe(self, "__bool__", &bool_str);
if (func == NULL) {
func = lookup_maybe(self, "__len__", &len_str);
if (func == NULL)
return PyErr_Occurred() ? -1 : 1;
- from_len = 1;
- }
+ using_len = 1;
+ }
args = PyTuple_New(0);
if (args != NULL) {
PyObject *temp = PyObject_Call(func, args, NULL);
Py_DECREF(args);
if (temp != NULL) {
- if (from_len) {
+ if (using_len) {
/* enforced by slot_nb_len */
result = PyObject_IsTrue(temp);
}
}
else {
PyErr_Format(PyExc_TypeError,
- "__bool__ should return "
- "bool, returned %s",
- Py_TYPE(temp)->tp_name);
+ "%s should return "
+ "bool or int, returned %s",
+ (using_len ? "__len__"
+ : "__bool__"),
+ Py_TYPE(temp)->tp_name);
result = -1;
}
Py_DECREF(temp);