[ { "url": "http://docs.python.org/library/struct.html", "title": "struct", "html": "
\n

7.3. struct — Interpret strings as packed binary data

\n

This module performs conversions between Python values and C structs represented\nas Python strings. This can be used in handling binary data stored in files or\nfrom network connections, among other sources. It uses\nFormat Strings as compact descriptions of the layout of the C\nstructs and the intended conversion to/from Python values.

\n
\n

Note

\n

By default, the result of packing a given C struct includes pad bytes in\norder to maintain proper alignment for the C types involved; similarly,\nalignment is taken into account when unpacking. This behavior is chosen so\nthat the bytes of a packed struct correspond exactly to the layout in memory\nof the corresponding C struct. To handle platform-independent data formats\nor omit implicit pad bytes, use standard size and alignment instead of\nnative size and alignment: see Byte Order, Size, and Alignment for details.

\n
\n
\n

7.3.1. Functions and Exceptions

\n

The module defines the following exception and functions:

\n
\n
\nexception struct.error
\n
Exception raised on various occasions; argument is a string describing what\nis wrong.
\n\n
\n
\nstruct.pack(fmt, v1, v2, ...)
\n
Return a string containing the values v1, v2, ... packed according to the\ngiven format. The arguments must match the values required by the format\nexactly.
\n\n
\n
\nstruct.pack_into(fmt, buffer, offset, v1, v2, ...)
\n

Pack the values v1, v2, ... according to the given format, write the\npacked bytes into the writable buffer starting at offset. Note that the\noffset is a required argument.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nstruct.unpack(fmt, string)
\n
Unpack the string (presumably packed by pack(fmt, ...)) according to the\ngiven format. The result is a tuple even if it contains exactly one item.\nThe string must contain exactly the amount of data required by the format\n(len(string) must equal calcsize(fmt)).
\n\n
\n
\nstruct.unpack_from(fmt, buffer[, offset=0])
\n

Unpack the buffer according to the given format. The result is a tuple even\nif it contains exactly one item. The buffer must contain at least the\namount of data required by the format (len(buffer[offset:]) must be at\nleast calcsize(fmt)).

\n

\nNew in version 2.5.

\n
\n\n
\n
\nstruct.calcsize(fmt)
\n
Return the size of the struct (and hence of the string) corresponding to the\ngiven format.
\n\n
\n
\n

7.3.2. Format Strings

\n

Format strings are the mechanism used to specify the expected layout when\npacking and unpacking data. They are built up from Format Characters,\nwhich specify the type of data being packed/unpacked. In addition, there are\nspecial characters for controlling the Byte Order, Size, and Alignment.

\n
\n

7.3.2.1. Byte Order, Size, and Alignment

\n

By default, C types are represented in the machine’s native format and byte\norder, and properly aligned by skipping pad bytes if necessary (according to the\nrules used by the C compiler).

\n

Alternatively, the first character of the format string can be used to indicate\nthe byte order, size and alignment of the packed data, according to the\nfollowing table:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CharacterByte orderSizeAlignment
@nativenativenative
=nativestandardnone
<little-endianstandardnone
>big-endianstandardnone
!network (= big-endian)standardnone
\n

If the first character is not one of these, '@' is assumed.

\n

Native byte order is big-endian or little-endian, depending on the host\nsystem. For example, Intel x86 and AMD64 (x86-64) are little-endian;\nMotorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature\nswitchable endianness (bi-endian). Use sys.byteorder to check the\nendianness of your system.

\n

Native size and alignment are determined using the C compiler’s\nsizeof expression. This is always combined with native byte order.

\n

Standard size depends only on the format character; see the table in\nthe Format Characters section.

\n

Note the difference between '@' and '=': both use native byte order, but\nthe size and alignment of the latter is standardized.

\n

The form '!' is available for those poor souls who claim they can’t remember\nwhether network byte order is big-endian or little-endian.

\n

There is no way to indicate non-native byte order (force byte-swapping); use the\nappropriate choice of '<' or '>'.

\n

Notes:

\n
    \n
  1. Padding is only automatically added between successive structure members.\nNo padding is added at the beginning or the end of the encoded struct.
  2. \n
  3. No padding is added when using non-native size and alignment, e.g.\nwith ‘<’, ‘>’, ‘=’, and ‘!’.
  4. \n
  5. To align the end of a structure to the alignment requirement of a\nparticular type, end the format with the code for that type with a repeat\ncount of zero. See Examples.
  6. \n
\n
\n
\n

7.3.2.2. Format Characters

\n

Format characters have the following meaning; the conversion between C and\nPython values should be obvious given their types. The ‘Standard size’ column\nrefers to the size of the packed value in bytes when using standard size; that\nis, when the format string starts with one of '<', '>', '!' or\n'='. When using native size, the size of the packed value is\nplatform-dependent.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FormatC TypePython typeStandard sizeNotes
xpad byteno value  
ccharstring of length 11 
bsigned charinteger1(3)
Bunsigned charinteger1(3)
?_Boolbool1(1)
hshortinteger2(3)
Hunsigned shortinteger2(3)
iintinteger4(3)
Iunsigned intinteger4(3)
llonginteger4(3)
Lunsigned longinteger4(3)
qlong longinteger8(2), (3)
Qunsigned long\nlonginteger8(2), (3)
ffloatfloat4(4)
ddoublefloat8(4)
schar[]string  
pchar[]string  
Pvoid *integer (5), (3)
\n

Notes:

\n
    \n
  1. The '?' conversion code corresponds to the _Bool type defined by\nC99. If this type is not available, it is simulated using a char. In\nstandard mode, it is always represented by one byte.

    \n

    \nNew in version 2.6.

    \n
  2. \n
  3. The 'q' and 'Q' conversion codes are available in native mode only if\nthe platform C compiler supports C long long, or, on Windows,\n__int64. They are always available in standard modes.

    \n

    \nNew in version 2.2.

    \n
  4. \n
  5. When attempting to pack a non-integer using any of the integer conversion\ncodes, if the non-integer has a __index__() method then that method is\ncalled to convert the argument to an integer before packing. If no\n__index__() method exists, or the call to __index__() raises\nTypeError, then the __int__() method is tried. However, the use\nof __int__() is deprecated, and will raise DeprecationWarning.

    \n

    \nChanged in version 2.7: Use of the __index__() method for non-integers is new in 2.7.

    \n

    \nChanged in version 2.7: Prior to version 2.7, not all integer conversion codes would use the\n__int__() method to convert, and DeprecationWarning was\nraised only for float arguments.

    \n
  6. \n
  7. For the 'f' and 'd' conversion codes, the packed representation uses\nthe IEEE 754 binary32 (for 'f') or binary64 (for 'd') format,\nregardless of the floating-point format used by the platform.

    \n
  8. \n
  9. The 'P' format character is only available for the native byte ordering\n(selected as the default or with the '@' byte order character). The byte\norder character '=' chooses to use little- or big-endian ordering based\non the host system. The struct module does not interpret this as native\nordering, so the 'P' format is not available.

    \n
  10. \n
\n

A format character may be preceded by an integral repeat count. For example,\nthe format string '4h' means exactly the same as 'hhhh'.

\n

Whitespace characters between formats are ignored; a count and its format must\nnot contain whitespace though.

\n

For the 's' format character, the count is interpreted as the size of the\nstring, not a repeat count like for the other format characters; for example,\n'10s' means a single 10-byte string, while '10c' means 10 characters.\nIf a count is not given, it defaults to 1. For packing, the string is\ntruncated or padded with null bytes as appropriate to make it fit. For\nunpacking, the resulting string always has exactly the specified number of\nbytes. As a special case, '0s' means a single, empty string (while\n'0c' means 0 characters).

\n

The 'p' format character encodes a “Pascal string”, meaning a short\nvariable-length string stored in a fixed number of bytes, given by the count.\nThe first byte stored is the length of the string, or 255, whichever is smaller.\nThe bytes of the string follow. If the string passed in to pack() is too\nlong (longer than the count minus 1), only the leading count-1 bytes of the\nstring are stored. If the string is shorter than count-1, it is padded with\nnull bytes so that exactly count bytes in all are used. Note that for\nunpack(), the 'p' format character consumes count bytes, but that the\nstring returned can never contain more than 255 characters.

\n

For the 'P' format character, the return value is a Python integer or long\ninteger, depending on the size needed to hold a pointer when it has been cast to\nan integer type. A NULL pointer will always be returned as the Python integer\n0. When packing pointer-sized values, Python integer or long integer objects\nmay be used. For example, the Alpha and Merced processors use 64-bit pointer\nvalues, meaning a Python long integer will be used to hold the pointer; other\nplatforms use 32-bit pointers and will use a Python integer.

\n

For the '?' format character, the return value is either True or\nFalse. When packing, the truth value of the argument object is used.\nEither 0 or 1 in the native or standard bool representation will be packed, and\nany non-zero value will be True when unpacking.

\n
\n
\n

7.3.2.3. Examples

\n
\n

Note

\n

All examples assume a native byte order, size, and alignment with a\nbig-endian machine.

\n
\n

A basic example of packing/unpacking three integers:

\n
>>> from struct import *\n>>> pack('hhl', 1, 2, 3)\n'\\x00\\x01\\x00\\x02\\x00\\x00\\x00\\x03'\n>>> unpack('hhl', '\\x00\\x01\\x00\\x02\\x00\\x00\\x00\\x03')\n(1, 2, 3)\n>>> calcsize('hhl')\n8\n
\n
\n

Unpacked fields can be named by assigning them to variables or by wrapping\nthe result in a named tuple:

\n
>>> record = 'raymond   \\x32\\x12\\x08\\x01\\x08'\n>>> name, serialnum, school, gradelevel = unpack('<10sHHb', record)\n\n>>> from collections import namedtuple\n>>> Student = namedtuple('Student', 'name serialnum school gradelevel')\n>>> Student._make(unpack('<10sHHb', record))\nStudent(name='raymond   ', serialnum=4658, school=264, gradelevel=8)\n
\n
\n

The ordering of format characters may have an impact on size since the padding\nneeded to satisfy alignment requirements is different:

\n
>>> pack('ci', '*', 0x12131415)\n'*\\x00\\x00\\x00\\x12\\x13\\x14\\x15'\n>>> pack('ic', 0x12131415, '*')\n'\\x12\\x13\\x14\\x15*'\n>>> calcsize('ci')\n8\n>>> calcsize('ic')\n5\n
\n
\n

The following format 'llh0l' specifies two pad bytes at the end, assuming\nlongs are aligned on 4-byte boundaries:

\n
>>> pack('llh0l', 1, 2, 3)\n'\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x02\\x00\\x03\\x00\\x00'\n
\n
\n

This only works when native size and alignment are in effect; standard size and\nalignment does not enforce any alignment.

\n
\n

See also

\n
\n
Module array
\n
Packed binary storage of homogeneous data.
\n
Module xdrlib
\n
Packing and unpacking of XDR data.
\n
\n
\n
\n
\n
\n

7.3.3. Classes

\n

The struct module also defines the following type:

\n
\n
\nclass struct.Struct(format)
\n

Return a new Struct object which writes and reads binary data according to\nthe format string format. Creating a Struct object once and calling its\nmethods is more efficient than calling the struct functions with the\nsame format since the format string only needs to be compiled once.

\n

\nNew in version 2.5.

\n

Compiled Struct objects support the following methods and attributes:

\n
\n
\npack(v1, v2, ...)
\n
Identical to the pack() function, using the compiled format.\n(len(result) will equal self.size.)
\n\n
\n
\npack_into(buffer, offset, v1, v2, ...)
\n
Identical to the pack_into() function, using the compiled format.
\n\n
\n
\nunpack(string)
\n
Identical to the unpack() function, using the compiled format.\n(len(string) must equal self.size).
\n\n
\n
\nunpack_from(buffer[, offset=0])
\n
Identical to the unpack_from() function, using the compiled format.\n(len(buffer[offset:]) must be at least self.size).
\n\n
\n
\nformat
\n
The format string used to construct this Struct object.
\n\n
\n
\nsize
\n
The calculated size of the struct (and hence of the string) corresponding\nto format.
\n\n
\n\n
\n
", "searchableItems": [ { "name": "struct.calcsize", "domId": "struct_struct.calcsize" }, { "name": "struct.pack", "domId": "struct_struct.pack" }, { "name": "struct.pack_into", "domId": "struct_struct.pack_into" }, { "name": "struct.Struct", "domId": "struct_struct.Struct" }, { "name": "struct.Struct.pack", "domId": "struct_struct.Struct.pack" }, { "name": "struct.Struct.pack_into", "domId": "struct_struct.Struct.pack_into" }, { "name": "struct.Struct.unpack", "domId": "struct_struct.Struct.unpack" }, { "name": "struct.Struct.unpack_from", "domId": "struct_struct.Struct.unpack_from" }, { "name": "struct.unpack", "domId": "struct_struct.unpack" }, { "name": "struct.unpack_from", "domId": "struct_struct.unpack_from" } ] }, { "url": "http://docs.python.org/library/exceptions.html", "title": "", "html": "
\n

6. Built-in Exceptions

\n

Exceptions should be class objects. The exceptions are defined in the module\nexceptions. This module never needs to be imported explicitly: the\nexceptions are provided in the built-in namespace as well as the\nexceptions module.

\n

For class exceptions, in a try statement with an except\nclause that mentions a particular class, that clause also handles any exception\nclasses derived from that class (but not exception classes from which it is\nderived). Two exception classes that are not related via subclassing are never\nequivalent, even if they have the same name.

\n

The built-in exceptions listed below can be generated by the interpreter or\nbuilt-in functions. Except where mentioned, they have an “associated value”\nindicating the detailed cause of the error. This may be a string or a tuple\ncontaining several items of information (e.g., an error code and a string\nexplaining the code). The associated value is the second argument to the\nraise statement. If the exception class is derived from the standard\nroot class BaseException, the associated value is present as the\nexception instance’s args attribute.

\n

User code can raise built-in exceptions. This can be used to test an exception\nhandler or to report an error condition “just like” the situation in which the\ninterpreter raises the same exception; but beware that there is nothing to\nprevent user code from raising an inappropriate error.

\n

The built-in exception classes can be sub-classed to define new exceptions;\nprogrammers are encouraged to at least derive new exceptions from the\nException class and not BaseException. More information on\ndefining exceptions is available in the Python Tutorial under\nUser-defined Exceptions.

\n

The following exceptions are only used as base classes for other exceptions.

\n
\n
\nexception BaseException
\n

The base class for all built-in exceptions. It is not meant to be directly\ninherited by user-defined classes (for that, use Exception). If\nstr() or unicode() is called on an instance of this class, the\nrepresentation of the argument(s) to the instance are returned, or the empty\nstring when there were no arguments.

\n

\nNew in version 2.5.

\n
\n
\nargs
\n
The tuple of arguments given to the exception constructor. Some built-in\nexceptions (like IOError) expect a certain number of arguments and\nassign a special meaning to the elements of this tuple, while others are\nusually called only with a single string giving an error message.
\n\n
\n\n
\n
\nexception Exception
\n

All built-in, non-system-exiting exceptions are derived from this class. All\nuser-defined exceptions should also be derived from this class.

\n

\nChanged in version 2.5: Changed to inherit from BaseException.

\n
\n\n
\n
\nexception StandardError
\n
The base class for all built-in exceptions except StopIteration,\nGeneratorExit, KeyboardInterrupt and SystemExit.\nStandardError itself is derived from Exception.
\n\n
\n
\nexception ArithmeticError
\n
The base class for those built-in exceptions that are raised for various\narithmetic errors: OverflowError, ZeroDivisionError,\nFloatingPointError.
\n\n
\n
\nexception BufferError
\n
Raised when a buffer related operation cannot be\nperformed.
\n\n
\n
\nexception LookupError
\n
The base class for the exceptions that are raised when a key or index used on\na mapping or sequence is invalid: IndexError, KeyError. This\ncan be raised directly by codecs.lookup().
\n\n
\n
\nexception EnvironmentError
\n

The base class for exceptions that can occur outside the Python system:\nIOError, OSError. When exceptions of this type are created with a\n2-tuple, the first item is available on the instance’s errno attribute\n(it is assumed to be an error number), and the second item is available on the\nstrerror attribute (it is usually the associated error message). The\ntuple itself is also available on the args attribute.

\n

\nNew in version 1.5.2.

\n

When an EnvironmentError exception is instantiated with a 3-tuple, the\nfirst two items are available as above, while the third item is available on the\nfilename attribute. However, for backwards compatibility, the\nargs attribute contains only a 2-tuple of the first two constructor\narguments.

\n

The filename attribute is None when this exception is created with\nother than 3 arguments. The errno and strerror attributes are\nalso None when the instance was created with other than 2 or 3 arguments.\nIn this last case, args contains the verbatim constructor arguments as a\ntuple.

\n
\n\n

The following exceptions are the exceptions that are actually raised.

\n
\n
\nexception AssertionError
\n

Raised when an assert statement fails.

\n
\n\n
\n
\nexception AttributeError
\n
Raised when an attribute reference (see Attribute references) or\nassignment fails. (When an object does not support attribute references or\nattribute assignments at all, TypeError is raised.)
\n\n
\n
\nexception EOFError
\n
Raised when one of the built-in functions (input() or raw_input())\nhits an end-of-file condition (EOF) without reading any data. (N.B.: the\nfile.read() and file.readline() methods return an empty string\nwhen they hit EOF.)
\n\n
\n
\nexception FloatingPointError
\n
Raised when a floating point operation fails. This exception is always defined,\nbut can only be raised when Python is configured with the\n--with-fpectl option, or the WANT_SIGFPE_HANDLER symbol is\ndefined in the pyconfig.h file.
\n\n
\n
\nexception GeneratorExit
\n

Raise when a generator‘s close() method is called. It\ndirectly inherits from BaseException instead of StandardError since\nit is technically not an error.

\n

\nNew in version 2.5.

\n

\nChanged in version 2.6: Changed to inherit from BaseException.

\n
\n\n
\n
\nexception IOError
\n

Raised when an I/O operation (such as a print statement, the built-in\nopen() function or a method of a file object) fails for an I/O-related\nreason, e.g., “file not found” or “disk full”.

\n

This class is derived from EnvironmentError. See the discussion above\nfor more information on exception instance attributes.

\n

\nChanged in version 2.6: Changed socket.error to use this as a base class.

\n
\n\n
\n
\nexception ImportError
\n
Raised when an import statement fails to find the module definition\nor when a from ... import fails to find a name that is to be imported.
\n\n
\n
\nexception IndexError
\n
Raised when a sequence subscript is out of range. (Slice indices are silently\ntruncated to fall in the allowed range; if an index is not a plain integer,\nTypeError is raised.)
\n\n
\n
\nexception KeyError
\n
Raised when a mapping (dictionary) key is not found in the set of existing keys.
\n\n
\n
\nexception KeyboardInterrupt
\n

Raised when the user hits the interrupt key (normally Control-C or\nDelete). During execution, a check for interrupts is made regularly.\nInterrupts typed when a built-in function input() or raw_input() is\nwaiting for input also raise this exception. The exception inherits from\nBaseException so as to not be accidentally caught by code that catches\nException and thus prevent the interpreter from exiting.

\n

\nChanged in version 2.5: Changed to inherit from BaseException.

\n
\n\n
\n
\nexception MemoryError
\n
Raised when an operation runs out of memory but the situation may still be\nrescued (by deleting some objects). The associated value is a string indicating\nwhat kind of (internal) operation ran out of memory. Note that because of the\nunderlying memory management architecture (C’s malloc() function), the\ninterpreter may not always be able to completely recover from this situation; it\nnevertheless raises an exception so that a stack traceback can be printed, in\ncase a run-away program was the cause.
\n\n
\n
\nexception NameError
\n
Raised when a local or global name is not found. This applies only to\nunqualified names. The associated value is an error message that includes the\nname that could not be found.
\n\n
\n
\nexception NotImplementedError
\n

This exception is derived from RuntimeError. In user defined base\nclasses, abstract methods should raise this exception when they require derived\nclasses to override the method.

\n

\nNew in version 1.5.2.

\n
\n\n
\n
\nexception OSError
\n

This exception is derived from EnvironmentError. It is raised when a\nfunction returns a system-related error (not for illegal argument types or\nother incidental errors). The errno attribute is a numeric error\ncode from errno, and the strerror attribute is the\ncorresponding string, as would be printed by the C function perror().\nSee the module errno, which contains names for the error codes defined\nby the underlying operating system.

\n

For exceptions that involve a file system path (such as chdir() or\nunlink()), the exception instance will contain a third attribute,\nfilename, which is the file name passed to the function.

\n

\nNew in version 1.5.2.

\n
\n\n
\n
\nexception OverflowError
\n
Raised when the result of an arithmetic operation is too large to be\nrepresented. This cannot occur for long integers (which would rather raise\nMemoryError than give up) and for most operations with plain integers,\nwhich return a long integer instead. Because of the lack of standardization\nof floating point exception handling in C, most floating point operations\nalso aren’t checked.
\n\n
\n
\nexception ReferenceError
\n

This exception is raised when a weak reference proxy, created by the\nweakref.proxy() function, is used to access an attribute of the referent\nafter it has been garbage collected. For more information on weak references,\nsee the weakref module.

\n

\nNew in version 2.2: Previously known as the weakref.ReferenceError exception.

\n
\n\n
\n
\nexception RuntimeError
\n
Raised when an error is detected that doesn’t fall in any of the other\ncategories. The associated value is a string indicating what precisely went\nwrong. (This exception is mostly a relic from a previous version of the\ninterpreter; it is not used very much any more.)
\n\n
\n
\nexception StopIteration
\n

Raised by an iterator‘s next() method to signal that\nthere are no further values. This is derived from Exception rather\nthan StandardError, since this is not considered an error in its\nnormal application.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nexception SyntaxError
\n

Raised when the parser encounters a syntax error. This may occur in an\nimport statement, in an exec statement, in a call to the\nbuilt-in function eval() or input(), or when reading the initial\nscript or standard input (also interactively).

\n

Instances of this class have attributes filename, lineno,\noffset and text for easier access to the details. str()\nof the exception instance returns only the message.

\n
\n\n
\n
\nexception IndentationError
\n
Base class for syntax errors related to incorrect indentation. This is a\nsubclass of SyntaxError.
\n\n
\n
\nexception TabError
\n
Raised when indentation contains an inconsistent use of tabs and spaces.\nThis is a subclass of IndentationError.
\n\n
\n
\nexception SystemError
\n

Raised when the interpreter finds an internal error, but the situation does not\nlook so serious to cause it to abandon all hope. The associated value is a\nstring indicating what went wrong (in low-level terms).

\n

You should report this to the author or maintainer of your Python interpreter.\nBe sure to report the version of the Python interpreter (sys.version; it is\nalso printed at the start of an interactive Python session), the exact error\nmessage (the exception’s associated value) and if possible the source of the\nprogram that triggered the error.

\n
\n\n
\n
\nexception SystemExit
\n

This exception is raised by the sys.exit() function. When it is not\nhandled, the Python interpreter exits; no stack traceback is printed. If the\nassociated value is a plain integer, it specifies the system exit status (passed\nto C’s exit() function); if it is None, the exit status is zero; if\nit has another type (such as a string), the object’s value is printed and the\nexit status is one.

\n

Instances have an attribute code which is set to the proposed exit\nstatus or error message (defaulting to None). Also, this exception derives\ndirectly from BaseException and not StandardError, since it is not\ntechnically an error.

\n

A call to sys.exit() is translated into an exception so that clean-up\nhandlers (finally clauses of try statements) can be\nexecuted, and so that a debugger can execute a script without running the risk\nof losing control. The os._exit() function can be used if it is\nabsolutely positively necessary to exit immediately (for example, in the child\nprocess after a call to fork()).

\n

The exception inherits from BaseException instead of StandardError\nor Exception so that it is not accidentally caught by code that catches\nException. This allows the exception to properly propagate up and cause\nthe interpreter to exit.

\n

\nChanged in version 2.5: Changed to inherit from BaseException.

\n
\n\n
\n
\nexception TypeError
\n
Raised when an operation or function is applied to an object of inappropriate\ntype. The associated value is a string giving details about the type mismatch.
\n\n
\n
\nexception UnboundLocalError
\n

Raised when a reference is made to a local variable in a function or method, but\nno value has been bound to that variable. This is a subclass of\nNameError.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception UnicodeError
\n

Raised when a Unicode-related encoding or decoding error occurs. It is a\nsubclass of ValueError.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception UnicodeEncodeError
\n

Raised when a Unicode-related error occurs during encoding. It is a subclass of\nUnicodeError.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception UnicodeDecodeError
\n

Raised when a Unicode-related error occurs during decoding. It is a subclass of\nUnicodeError.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception UnicodeTranslateError
\n

Raised when a Unicode-related error occurs during translating. It is a subclass\nof UnicodeError.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception ValueError
\n
Raised when a built-in operation or function receives an argument that has the\nright type but an inappropriate value, and the situation is not described by a\nmore precise exception such as IndexError.
\n\n
\n
\nexception VMSError
\n
Only available on VMS. Raised when a VMS-specific error occurs.
\n\n
\n
\nexception WindowsError
\n

Raised when a Windows-specific error occurs or when the error number does not\ncorrespond to an errno value. The winerror and\nstrerror values are created from the return values of the\nGetLastError() and FormatMessage() functions from the Windows\nPlatform API. The errno value maps the winerror value to\ncorresponding errno.h values. This is a subclass of OSError.

\n

\nNew in version 2.0.

\n

\nChanged in version 2.5: Previous versions put the GetLastError() codes into errno.

\n
\n\n
\n
\nexception ZeroDivisionError
\n
Raised when the second argument of a division or modulo operation is zero. The\nassociated value is a string indicating the type of the operands and the\noperation.
\n\n

The following exceptions are used as warning categories; see the warnings\nmodule for more information.

\n
\n
\nexception Warning
\n
Base class for warning categories.
\n\n
\n
\nexception UserWarning
\n
Base class for warnings generated by user code.
\n\n
\n
\nexception DeprecationWarning
\n
Base class for warnings about deprecated features.
\n\n
\n
\nexception PendingDeprecationWarning
\n
Base class for warnings about features which will be deprecated in the future.
\n\n
\n
\nexception SyntaxWarning
\n
Base class for warnings about dubious syntax
\n\n
\n
\nexception RuntimeWarning
\n
Base class for warnings about dubious runtime behavior.
\n\n
\n
\nexception FutureWarning
\n
Base class for warnings about constructs that will change semantically in the\nfuture.
\n\n
\n
\nexception ImportWarning
\n

Base class for warnings about probable mistakes in module imports.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nexception UnicodeWarning
\n

Base class for warnings related to Unicode.

\n

\nNew in version 2.5.

\n
\n\n
\n

6.1. Exception hierarchy

\n

The class hierarchy for built-in exceptions is:

\n
BaseException\n +-- SystemExit\n +-- KeyboardInterrupt\n +-- GeneratorExit\n +-- Exception\n      +-- StopIteration\n      +-- StandardError\n      |    +-- BufferError\n      |    +-- ArithmeticError\n      |    |    +-- FloatingPointError\n      |    |    +-- OverflowError\n      |    |    +-- ZeroDivisionError\n      |    +-- AssertionError\n      |    +-- AttributeError\n      |    +-- EnvironmentError\n      |    |    +-- IOError\n      |    |    +-- OSError\n      |    |         +-- WindowsError (Windows)\n      |    |         +-- VMSError (VMS)\n      |    +-- EOFError\n      |    +-- ImportError\n      |    +-- LookupError\n      |    |    +-- IndexError\n      |    |    +-- KeyError\n      |    +-- MemoryError\n      |    +-- NameError\n      |    |    +-- UnboundLocalError\n      |    +-- ReferenceError\n      |    +-- RuntimeError\n      |    |    +-- NotImplementedError\n      |    +-- SyntaxError\n      |    |    +-- IndentationError\n      |    |         +-- TabError\n      |    +-- SystemError\n      |    +-- TypeError\n      |    +-- ValueError\n      |         +-- UnicodeError\n      |              +-- UnicodeDecodeError\n      |              +-- UnicodeEncodeError\n      |              +-- UnicodeTranslateError\n      +-- Warning\n           +-- DeprecationWarning\n           +-- PendingDeprecationWarning\n           +-- RuntimeWarning\n           +-- SyntaxWarning\n           +-- UserWarning\n           +-- FutureWarning\n\t   +-- ImportWarning\n\t   +-- UnicodeWarning\n\t   +-- BytesWarning\n
\n
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/string.html", "title": "string", "html": "
\n

7.1. string — Common string operations

\n

Source code: Lib/string.py

\n
\n

The string module contains a number of useful constants and\nclasses, as well as some deprecated legacy functions that are also\navailable as methods on strings. In addition, Python’s built-in string\nclasses support the sequence type methods described in the\nSequence Types — str, unicode, list, tuple, bytearray, buffer, xrange section, and also the string-specific methods described\nin the String Methods section. To output formatted strings use\ntemplate strings or the operator described in the\nString Formatting Operations section. Also, see the re module for\nstring functions based on regular expressions.

\n
\n

7.1.1. String constants

\n

The constants defined in this module are:

\n
\n
\nstring.ascii_letters
\n
The concatenation of the ascii_lowercase and ascii_uppercase\nconstants described below. This value is not locale-dependent.
\n\n
\n
\nstring.ascii_lowercase
\n
The lowercase letters 'abcdefghijklmnopqrstuvwxyz'. This value is not\nlocale-dependent and will not change.
\n\n
\n
\nstring.ascii_uppercase
\n
The uppercase letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. This value is not\nlocale-dependent and will not change.
\n\n
\n
\nstring.digits
\n
The string '0123456789'.
\n\n
\n
\nstring.hexdigits
\n
The string '0123456789abcdefABCDEF'.
\n\n
\n
\nstring.letters
\n
The concatenation of the strings lowercase and uppercase\ndescribed below. The specific value is locale-dependent, and will be updated\nwhen locale.setlocale() is called.
\n\n
\n
\nstring.lowercase
\n
A string containing all the characters that are considered lowercase letters.\nOn most systems this is the string 'abcdefghijklmnopqrstuvwxyz'. The\nspecific value is locale-dependent, and will be updated when\nlocale.setlocale() is called.
\n\n
\n
\nstring.octdigits
\n
The string '01234567'.
\n\n
\n
\nstring.punctuation
\n
String of ASCII characters which are considered punctuation characters in the\nC locale.
\n\n
\n
\nstring.printable
\n
String of characters which are considered printable. This is a combination of\ndigits, letters, punctuation, and\nwhitespace.
\n\n
\n
\nstring.uppercase
\n
A string containing all the characters that are considered uppercase letters.\nOn most systems this is the string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The\nspecific value is locale-dependent, and will be updated when\nlocale.setlocale() is called.
\n\n
\n
\nstring.whitespace
\n
A string containing all characters that are considered whitespace. On most\nsystems this includes the characters space, tab, linefeed, return, formfeed, and\nvertical tab.
\n\n
\n
\n

7.1.2. String Formatting

\n

\nNew in version 2.6.

\n

The built-in str and unicode classes provide the ability\nto do complex variable substitutions and value formatting via the\nstr.format() method described in PEP 3101. The Formatter\nclass in the string module allows you to create and customize your own\nstring formatting behaviors using the same implementation as the built-in\nformat() method.

\n
\n
\nclass string.Formatter
\n

The Formatter class has the following public methods:

\n
\n
\nformat(format_string, *args, **kwargs)
\n
format() is the primary API method. It takes a format template\nstring, and an arbitrary set of positional and keyword argument.\nformat() is just a wrapper that calls vformat().
\n\n
\n
\nvformat(format_string, args, kwargs)
\n
This function does the actual work of formatting. It is exposed as a\nseparate function for cases where you want to pass in a predefined\ndictionary of arguments, rather than unpacking and repacking the\ndictionary as individual arguments using the *args and **kwds\nsyntax. vformat() does the work of breaking up the format template\nstring into character data and replacement fields. It calls the various\nmethods described below.
\n\n

In addition, the Formatter defines a number of methods that are\nintended to be replaced by subclasses:

\n
\n
\nparse(format_string)
\n

Loop over the format_string and return an iterable of tuples\n(literal_text, field_name, format_spec, conversion). This is used\nby vformat() to break the string into either literal text, or\nreplacement fields.

\n

The values in the tuple conceptually represent a span of literal text\nfollowed by a single replacement field. If there is no literal text\n(which can happen if two replacement fields occur consecutively), then\nliteral_text will be a zero-length string. If there is no replacement\nfield, then the values of field_name, format_spec and conversion\nwill be None.

\n
\n\n
\n
\nget_field(field_name, args, kwargs)
\n
Given field_name as returned by parse() (see above), convert it to\nan object to be formatted. Returns a tuple (obj, used_key). The default\nversion takes strings of the form defined in PEP 3101, such as\n“0[name]” or “label.title”. args and kwargs are as passed in to\nvformat(). The return value used_key has the same meaning as the\nkey parameter to get_value().
\n\n
\n
\nget_value(key, args, kwargs)
\n

Retrieve a given field value. The key argument will be either an\ninteger or a string. If it is an integer, it represents the index of the\npositional argument in args; if it is a string, then it represents a\nnamed argument in kwargs.

\n

The args parameter is set to the list of positional arguments to\nvformat(), and the kwargs parameter is set to the dictionary of\nkeyword arguments.

\n

For compound field names, these functions are only called for the first\ncomponent of the field name; Subsequent components are handled through\nnormal attribute and indexing operations.

\n

So for example, the field expression ‘0.name’ would cause\nget_value() to be called with a key argument of 0. The name\nattribute will be looked up after get_value() returns by calling the\nbuilt-in getattr() function.

\n

If the index or keyword refers to an item that does not exist, then an\nIndexError or KeyError should be raised.

\n
\n\n
\n
\ncheck_unused_args(used_args, args, kwargs)
\n
Implement checking for unused arguments if desired. The arguments to this\nfunction is the set of all argument keys that were actually referred to in\nthe format string (integers for positional arguments, and strings for\nnamed arguments), and a reference to the args and kwargs that was\npassed to vformat. The set of unused args can be calculated from these\nparameters. check_unused_args() is assumed to raise an exception if\nthe check fails.
\n\n
\n
\nformat_field(value, format_spec)
\n
format_field() simply calls the global format() built-in. The\nmethod is provided so that subclasses can override it.
\n\n
\n
\nconvert_field(value, conversion)
\n
Converts the value (returned by get_field()) given a conversion type\n(as in the tuple returned by the parse() method). The default\nversion understands ‘r’ (repr) and ‘s’ (str) conversion types.
\n\n
\n\n
\n
\n

7.1.3. Format String Syntax

\n

The str.format() method and the Formatter class share the same\nsyntax for format strings (although in the case of Formatter,\nsubclasses can define their own format string syntax).

\n

Format strings contain “replacement fields” surrounded by curly braces {}.\nAnything that is not contained in braces is considered literal text, which is\ncopied unchanged to the output. If you need to include a brace character in the\nliteral text, it can be escaped by doubling: {{ and }}.

\n

The grammar for a replacement field is as follows:

\n
\n
\nreplacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"\nfield_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*\narg_name          ::=  [identifier | integer]\nattribute_name    ::=  identifier\nelement_index     ::=  integer | index_string\nindex_string      ::=  <any source character except "]"> +\nconversion        ::=  "r" | "s"\nformat_spec       ::=  <described in the next section>\n
\n
\n

In less formal terms, the replacement field can start with a field_name that specifies\nthe object whose value is to be formatted and inserted\ninto the output instead of the replacement field.\nThe field_name is optionally followed by a conversion field, which is\npreceded by an exclamation point '!', and a format_spec, which is preceded\nby a colon ':'. These specify a non-default format for the replacement value.

\n

See also the Format Specification Mini-Language section.

\n

The field_name itself begins with an arg_name that is either a number or a\nkeyword. If it’s a number, it refers to a positional argument, and if it’s a keyword,\nit refers to a named keyword argument. If the numerical arg_names in a format string\nare 0, 1, 2, ... in sequence, they can all be omitted (not just some)\nand the numbers 0, 1, 2, ... will be automatically inserted in that order.\nBecause arg_name is not quote-delimited, it is not possible to specify arbitrary\ndictionary keys (e.g., the strings '10' or ':-]') within a format string.\nThe arg_name can be followed by any number of index or\nattribute expressions. An expression of the form '.name' selects the named\nattribute using getattr(), while an expression of the form '[index]'\ndoes an index lookup using __getitem__().

\n

\nChanged in version 2.7: The positional argument specifiers can be omitted, so '{} {}' is\nequivalent to '{0} {1}'.

\n

Some simple format string examples:

\n
"First, thou shalt count to {0}" # References first positional argument\n"Bring me a {}"                  # Implicitly references the first positional argument\n"From {} to {}"                  # Same as "From {0} to {1}"\n"My quest is {name}"             # References keyword argument 'name'\n"Weight in tons {0.weight}"      # 'weight' attribute of first positional arg\n"Units destroyed: {players[0]}"  # First element of keyword argument 'players'.\n
\n
\n

The conversion field causes a type coercion before formatting. Normally, the\njob of formatting a value is done by the __format__() method of the value\nitself. However, in some cases it is desirable to force a type to be formatted\nas a string, overriding its own definition of formatting. By converting the\nvalue to a string before calling __format__(), the normal formatting logic\nis bypassed.

\n

Two conversion flags are currently supported: '!s' which calls str()\non the value, and '!r' which calls repr().

\n

Some examples:

\n
"Harold's a clever {0!s}"        # Calls str() on the argument first\n"Bring out the holy {name!r}"    # Calls repr() on the argument first\n
\n
\n

The format_spec field contains a specification of how the value should be\npresented, including such details as field width, alignment, padding, decimal\nprecision and so on. Each value type can define its own “formatting\nmini-language” or interpretation of the format_spec.

\n

Most built-in types support a common formatting mini-language, which is\ndescribed in the next section.

\n

A format_spec field can also include nested replacement fields within it.\nThese nested replacement fields can contain only a field name; conversion flags\nand format specifications are not allowed. The replacement fields within the\nformat_spec are substituted before the format_spec string is interpreted.\nThis allows the formatting of a value to be dynamically specified.

\n

See the Format examples section for some examples.

\n
\n

7.1.3.1. Format Specification Mini-Language

\n

“Format specifications” are used within replacement fields contained within a\nformat string to define how individual values are presented (see\nFormat String Syntax). They can also be passed directly to the built-in\nformat() function. Each formattable type may define how the format\nspecification is to be interpreted.

\n

Most built-in types implement the following options for format specifications,\nalthough some of the formatting options are only supported by the numeric types.

\n

A general convention is that an empty format string ("") produces\nthe same result as if you had called str() on the value. A\nnon-empty format string typically modifies the result.

\n

The general form of a standard format specifier is:

\n
\nformat_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]\nfill        ::=  <a character other than '}'>\nalign       ::=  "<" | ">" | "=" | "^"\nsign        ::=  "+" | "-" | " "\nwidth       ::=  integer\nprecision   ::=  integer\ntype        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n
\n

The fill character can be any character other than ‘{‘ or ‘}’. The presence\nof a fill character is signaled by the character following it, which must be\none of the alignment options. If the second character of format_spec is not\na valid alignment option, then it is assumed that both the fill character and\nthe alignment option are absent.

\n

The meaning of the various alignment options is as follows:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OptionMeaning
'<'Forces the field to be left-aligned within the available\nspace (this is the default for most objects).
'>'Forces the field to be right-aligned within the\navailable space (this is the default for numbers).
'='Forces the padding to be placed after the sign (if any)\nbut before the digits. This is used for printing fields\nin the form ‘+000000120’. This alignment option is only\nvalid for numeric types.
'^'Forces the field to be centered within the available\nspace.
\n
\n

Note that unless a minimum field width is defined, the field width will always\nbe the same size as the data to fill it, so that the alignment option has no\nmeaning in this case.

\n

The sign option is only valid for number types, and can be one of the\nfollowing:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OptionMeaning
'+'indicates that a sign should be used for both\npositive as well as negative numbers.
'-'indicates that a sign should be used only for negative\nnumbers (this is the default behavior).
spaceindicates that a leading space should be used on\npositive numbers, and a minus sign on negative numbers.
\n
\n

The '#' option is only valid for integers, and only for binary, octal, or\nhexadecimal output. If present, it specifies that the output will be prefixed\nby '0b', '0o', or '0x', respectively.

\n

The ',' option signals the use of a comma for a thousands separator.\nFor a locale aware separator, use the 'n' integer presentation type\ninstead.

\n

\nChanged in version 2.7: Added the ',' option (see also PEP 378).

\n

width is a decimal integer defining the minimum field width. If not\nspecified, then the field width will be determined by the content.

\n

If the width field is preceded by a zero ('0') character, this enables\nzero-padding. This is equivalent to an alignment type of '=' and a fill\ncharacter of '0'.

\n

The precision is a decimal number indicating how many digits should be\ndisplayed after the decimal point for a floating point value formatted with\n'f' and 'F', or before and after the decimal point for a floating point\nvalue formatted with 'g' or 'G'. For non-number types the field\nindicates the maximum field size - in other words, how many characters will be\nused from the field content. The precision is not allowed for integer values.

\n

Finally, the type determines how the data should be presented.

\n

The available string presentation types are:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
TypeMeaning
's'String format. This is the default type for strings and\nmay be omitted.
NoneThe same as 's'.
\n
\n

The available integer presentation types are:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
TypeMeaning
'b'Binary format. Outputs the number in base 2.
'c'Character. Converts the integer to the corresponding\nunicode character before printing.
'd'Decimal Integer. Outputs the number in base 10.
'o'Octal format. Outputs the number in base 8.
'x'Hex format. Outputs the number in base 16, using lower-\ncase letters for the digits above 9.
'X'Hex format. Outputs the number in base 16, using upper-\ncase letters for the digits above 9.
'n'Number. This is the same as 'd', except that it uses\nthe current locale setting to insert the appropriate\nnumber separator characters.
NoneThe same as 'd'.
\n
\n

In addition to the above presentation types, integers can be formatted\nwith the floating point presentation types listed below (except\n'n' and None). When doing so, float() is used to convert the\ninteger to a floating point number before formatting.

\n

The available presentation types for floating point and decimal values are:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
TypeMeaning
'e'Exponent notation. Prints the number in scientific\nnotation using the letter ‘e’ to indicate the exponent.
'E'Exponent notation. Same as 'e' except it uses an\nupper case ‘E’ as the separator character.
'f'Fixed point. Displays the number as a fixed-point\nnumber.
'F'Fixed point. Same as 'f'.
'g'

General format. For a given precision p >= 1,\nthis rounds the number to p significant digits and\nthen formats the result in either fixed-point format\nor in scientific notation, depending on its magnitude.

\n

The precise rules are as follows: suppose that the\nresult formatted with presentation type 'e' and\nprecision p-1 would have exponent exp. Then\nif -4 <= exp < p, the number is formatted\nwith presentation type 'f' and precision\np-1-exp. Otherwise, the number is formatted\nwith presentation type 'e' and precision p-1.\nIn both cases insignificant trailing zeros are removed\nfrom the significand, and the decimal point is also\nremoved if there are no remaining digits following it.

\n

Positive and negative infinity, positive and negative\nzero, and nans, are formatted as inf, -inf,\n0, -0 and nan respectively, regardless of\nthe precision.

\n

A precision of 0 is treated as equivalent to a\nprecision of 1.

\n
'G'General format. Same as 'g' except switches to\n'E' if the number gets too large. The\nrepresentations of infinity and NaN are uppercased, too.
'n'Number. This is the same as 'g', except that it uses\nthe current locale setting to insert the appropriate\nnumber separator characters.
'%'Percentage. Multiplies the number by 100 and displays\nin fixed ('f') format, followed by a percent sign.
NoneThe same as 'g'.
\n
\n
\n
\n

7.1.3.2. Format examples

\n

This section contains examples of the new format syntax and comparison with\nthe old -formatting.

\n

In most of the cases the syntax is similar to the old -formatting, with the\naddition of the {} and with : used instead of .\nFor example, '%03.2f' can be translated to '{:03.2f}'.

\n

The new format syntax also supports new and different options, shown in the\nfollow examples.

\n

Accessing arguments by position:

\n
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n'a, b, c'\n>>> '{}, {}, {}'.format('a', 'b', 'c')  # 2.7+ only\n'a, b, c'\n>>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n'c, b, a'\n>>> '{2}, {1}, {0}'.format(*'abc')      # unpacking argument sequence\n'c, b, a'\n>>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated\n'abracadabra'\n
\n
\n

Accessing arguments by name:

\n
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')\n'Coordinates: 37.24N, -115.81W'\n>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}\n>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)\n'Coordinates: 37.24N, -115.81W'\n
\n
\n

Accessing arguments’ attributes:

\n
>>> c = 3-5j\n>>> ('The complex number {0} is formed from the real part {0.real} '\n...  'and the imaginary part {0.imag}.').format(c)\n'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'\n>>> class Point(object):\n...     def __init__(self, x, y):\n...         self.x, self.y = x, y\n...     def __str__(self):\n...         return 'Point({self.x}, {self.y})'.format(self=self)\n...\n>>> str(Point(4, 2))\n'Point(4, 2)'\n
\n
\n

Accessing arguments’ items:

\n
>>> coord = (3, 5)\n>>> 'X: {0[0]};  Y: {0[1]}'.format(coord)\n'X: 3;  Y: 5'\n
\n
\n

Replacing %s and %r:

\n
>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')\n"repr() shows quotes: 'test1'; str() doesn't: test2"\n
\n
\n

Aligning the text and specifying a width:

\n
>>> '{:<30}'.format('left aligned')\n'left aligned                  '\n>>> '{:>30}'.format('right aligned')\n'                 right aligned'\n>>> '{:^30}'.format('centered')\n'           centered           '\n>>> '{:*^30}'.format('centered')  # use '*' as a fill char\n'***********centered***********'\n
\n
\n

Replacing %+f, %-f, and f and specifying a sign:

\n
>>> '{:+f}; {:+f}'.format(3.14, -3.14)  # show it always\n'+3.140000; -3.140000'\n>>> '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers\n' 3.140000; -3.140000'\n>>> '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the minus -- same as '{:f}; {:f}'\n'3.140000; -3.140000'\n
\n
\n

Replacing %x and %o and converting the value to different bases:

\n
>>> # format also supports binary numbers\n>>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)\n'int: 42;  hex: 2a;  oct: 52;  bin: 101010'\n>>> # with 0x, 0o, or 0b as prefix:\n>>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)\n'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'\n
\n
\n

Using the comma as a thousands separator:

\n
>>> '{:,}'.format(1234567890)\n'1,234,567,890'\n
\n
\n

Expressing a percentage:

\n
>>> points = 19.5\n>>> total = 22\n>>> 'Correct answers: {:.2%}'.format(points/total)\n'Correct answers: 88.64%'\n
\n
\n

Using type-specific formatting:

\n
>>> import datetime\n>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n'2010-07-04 12:15:58'\n
\n
\n

Nesting arguments and more complex examples:

\n
>>> for align, text in zip('<^>', ['left', 'center', 'right']):\n...     '{0:{fill}{align}16}'.format(text, fill=align, align=align)\n...\n'left<<<<<<<<<<<<'\n'^^^^^center^^^^^'\n'>>>>>>>>>>>right'\n>>>\n>>> octets = [192, 168, 0, 1]\n>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n'C0A80001'\n>>> int(_, 16)\n3232235521\n>>>\n>>> width = 5\n>>> for num in range(5,12):\n...     for base in 'dXob':\n...         print '{0:{width}{base}}'.format(num, base=base, width=width),\n...     print\n...\n    5     5     5   101\n    6     6     6   110\n    7     7     7   111\n    8     8    10  1000\n    9     9    11  1001\n   10     A    12  1010\n   11     B    13  1011\n
\n
\n
\n
\n
\n

7.1.4. Template strings

\n

\nNew in version 2.4.

\n

Templates provide simpler string substitutions as described in PEP 292.\nInstead of the normal -based substitutions, Templates support $-based substitutions, using the following rules:

\n\n

Any other appearance of $ in the string will result in a ValueError\nbeing raised.

\n

The string module provides a Template class that implements\nthese rules. The methods of Template are:

\n
\n
\nclass string.Template(template)
\n

The constructor takes a single argument which is the template string.

\n
\n
\nsubstitute(mapping[, **kws])
\n
Performs the template substitution, returning a new string. mapping is\nany dictionary-like object with keys that match the placeholders in the\ntemplate. Alternatively, you can provide keyword arguments, where the\nkeywords are the placeholders. When both mapping and kws are given\nand there are duplicates, the placeholders from kws take precedence.
\n\n
\n
\nsafe_substitute(mapping[, **kws])
\n

Like substitute(), except that if placeholders are missing from\nmapping and kws, instead of raising a KeyError exception, the\noriginal placeholder will appear in the resulting string intact. Also,\nunlike with substitute(), any other appearances of the $ will\nsimply return $ instead of raising ValueError.

\n

While other exceptions may still occur, this method is called “safe”\nbecause substitutions always tries to return a usable string instead of\nraising an exception. In another sense, safe_substitute() may be\nanything other than safe, since it will silently ignore malformed\ntemplates containing dangling delimiters, unmatched braces, or\nplaceholders that are not valid Python identifiers.

\n
\n\n

Template instances also provide one public data attribute:

\n
\n
\ntemplate
\n
This is the object passed to the constructor’s template argument. In\ngeneral, you shouldn’t change it, but read-only access is not enforced.
\n\n
\n\n

Here is an example of how to use a Template:

\n
>>> from string import Template\n>>> s = Template('$who likes $what')\n>>> s.substitute(who='tim', what='kung pao')\n'tim likes kung pao'\n>>> d = dict(who='tim')\n>>> Template('Give $who $100').substitute(d)\nTraceback (most recent call last):\n[...]\nValueError: Invalid placeholder in string: line 1, col 10\n>>> Template('$who likes $what').substitute(d)\nTraceback (most recent call last):\n[...]\nKeyError: 'what'\n>>> Template('$who likes $what').safe_substitute(d)\n'tim likes $what'
\n
\n

Advanced usage: you can derive subclasses of Template to customize the\nplaceholder syntax, delimiter character, or the entire regular expression used\nto parse template strings. To do this, you can override these class attributes:

\n\n

Alternatively, you can provide the entire regular expression pattern by\noverriding the class attribute pattern. If you do this, the value must be a\nregular expression object with four named capturing groups. The capturing\ngroups correspond to the rules given above, along with the invalid placeholder\nrule:

\n\n
\n
\n

7.1.5. String functions

\n

The following functions are available to operate on string and Unicode objects.\nThey are not available as string methods.

\n
\n
\nstring.capwords(s[, sep])
\n
Split the argument into words using str.split(), capitalize each word\nusing str.capitalize(), and join the capitalized words using\nstr.join(). If the optional second argument sep is absent\nor None, runs of whitespace characters are replaced by a single space\nand leading and trailing whitespace are removed, otherwise sep is used to\nsplit and join the words.
\n\n
\n
\nstring.maketrans(from, to)
\n

Return a translation table suitable for passing to translate(), that will\nmap each character in from into the character at the same position in to;\nfrom and to must have the same length.

\n
\n

Note

\n

Don’t use strings derived from lowercase and uppercase as\narguments; in some locales, these don’t have the same length. For case\nconversions, always use str.lower() and str.upper().

\n
\n
\n\n
\n
\n

7.1.6. Deprecated string functions

\n

The following list of functions are also defined as methods of string and\nUnicode objects; see section String Methods for more information on\nthose. You should consider these functions as deprecated, although they will\nnot be removed until Python 3.0. The functions defined in this module are:

\n
\n
\nstring.atof(s)
\n

\nDeprecated since version 2.0: Use the float() built-in function.

\n

Convert a string to a floating point number. The string must have the standard\nsyntax for a floating point literal in Python, optionally preceded by a sign\n(+ or -). Note that this behaves identical to the built-in function\nfloat() when passed a string.

\n
\n

Note

\n

When passing in a string, values for NaN and Infinity may be returned, depending\non the underlying C library. The specific set of strings accepted which cause\nthese values to be returned depends entirely on the C library and is known to\nvary.

\n
\n
\n\n
\n
\nstring.atoi(s[, base])
\n

\nDeprecated since version 2.0: Use the int() built-in function.

\n

Convert string s to an integer in the given base. The string must consist\nof one or more digits, optionally preceded by a sign (+ or -). The\nbase defaults to 10. If it is 0, a default base is chosen depending on the\nleading characters of the string (after stripping the sign): 0x or 0X\nmeans 16, 0 means 8, anything else means 10. If base is 16, a leading\n0x or 0X is always accepted, though not required. This behaves\nidentically to the built-in function int() when passed a string. (Also\nnote: for a more flexible interpretation of numeric literals, use the built-in\nfunction eval().)

\n
\n\n
\n
\nstring.atol(s[, base])
\n

\nDeprecated since version 2.0: Use the long() built-in function.

\n

Convert string s to a long integer in the given base. The string must\nconsist of one or more digits, optionally preceded by a sign (+ or -).\nThe base argument has the same meaning as for atoi(). A trailing l\nor L is not allowed, except if the base is 0. Note that when invoked\nwithout base or with base set to 10, this behaves identical to the built-in\nfunction long() when passed a string.

\n
\n\n
\n
\nstring.capitalize(word)
\n
Return a copy of word with only its first character capitalized.
\n\n
\n
\nstring.expandtabs(s[, tabsize])
\n
Expand tabs in a string replacing them by one or more spaces, depending on the\ncurrent column and the given tab size. The column number is reset to zero after\neach newline occurring in the string. This doesn’t understand other non-printing\ncharacters or escape sequences. The tab size defaults to 8.
\n\n
\n
\nstring.find(s, sub[, start[, end]])
\n
Return the lowest index in s where the substring sub is found such that\nsub is wholly contained in s[start:end]. Return -1 on failure.\nDefaults for start and end and interpretation of negative values is the same\nas for slices.
\n\n
\n
\nstring.rfind(s, sub[, start[, end]])
\n
Like find() but find the highest index.
\n\n
\n
\nstring.index(s, sub[, start[, end]])
\n
Like find() but raise ValueError when the substring is not found.
\n\n
\n
\nstring.rindex(s, sub[, start[, end]])
\n
Like rfind() but raise ValueError when the substring is not found.
\n\n
\n
\nstring.count(s, sub[, start[, end]])
\n
Return the number of (non-overlapping) occurrences of substring sub in string\ns[start:end]. Defaults for start and end and interpretation of negative\nvalues are the same as for slices.
\n\n
\n
\nstring.lower(s)
\n
Return a copy of s, but with upper case letters converted to lower case.
\n\n
\n
\nstring.split(s[, sep[, maxsplit]])
\n

Return a list of the words of the string s. If the optional second argument\nsep is absent or None, the words are separated by arbitrary strings of\nwhitespace characters (space, tab, newline, return, formfeed). If the second\nargument sep is present and not None, it specifies a string to be used as\nthe word separator. The returned list will then have one more item than the\nnumber of non-overlapping occurrences of the separator in the string. The\noptional third argument maxsplit defaults to 0. If it is nonzero, at most\nmaxsplit number of splits occur, and the remainder of the string is returned\nas the final element of the list (thus, the list will have at most\nmaxsplit+1 elements).

\n

The behavior of split on an empty string depends on the value of sep. If sep\nis not specified, or specified as None, the result will be an empty list.\nIf sep is specified as any string, the result will be a list containing one\nelement which is an empty string.

\n
\n\n
\n
\nstring.rsplit(s[, sep[, maxsplit]])
\n

Return a list of the words of the string s, scanning s from the end. To all\nintents and purposes, the resulting list of words is the same as returned by\nsplit(), except when the optional third argument maxsplit is explicitly\nspecified and nonzero. When maxsplit is nonzero, at most maxsplit number of\nsplits – the rightmost ones – occur, and the remainder of the string is\nreturned as the first element of the list (thus, the list will have at most\nmaxsplit+1 elements).

\n

\nNew in version 2.4.

\n
\n\n
\n
\nstring.splitfields(s[, sep[, maxsplit]])
\n
This function behaves identically to split(). (In the past, split()\nwas only used with one argument, while splitfields() was only used with\ntwo arguments.)
\n\n
\n
\nstring.join(words[, sep])
\n
Concatenate a list or tuple of words with intervening occurrences of sep.\nThe default value for sep is a single space character. It is always true that\nstring.join(string.split(s, sep), sep) equals s.
\n\n
\n
\nstring.joinfields(words[, sep])
\n
This function behaves identically to join(). (In the past, join()\nwas only used with one argument, while joinfields() was only used with two\narguments.) Note that there is no joinfields() method on string objects;\nuse the join() method instead.
\n\n
\n
\nstring.lstrip(s[, chars])
\n

Return a copy of the string with leading characters removed. If chars is\nomitted or None, whitespace characters are removed. If given and not\nNone, chars must be a string; the characters in the string will be\nstripped from the beginning of the string this method is called on.

\n

\nChanged in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in\nearlier 2.2 versions.

\n
\n\n
\n
\nstring.rstrip(s[, chars])
\n

Return a copy of the string with trailing characters removed. If chars is\nomitted or None, whitespace characters are removed. If given and not\nNone, chars must be a string; the characters in the string will be\nstripped from the end of the string this method is called on.

\n

\nChanged in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in\nearlier 2.2 versions.

\n
\n\n
\n
\nstring.strip(s[, chars])
\n

Return a copy of the string with leading and trailing characters removed. If\nchars is omitted or None, whitespace characters are removed. If given and\nnot None, chars must be a string; the characters in the string will be\nstripped from the both ends of the string this method is called on.

\n

\nChanged in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in\nearlier 2.2 versions.

\n
\n\n
\n
\nstring.swapcase(s)
\n
Return a copy of s, but with lower case letters converted to upper case and\nvice versa.
\n\n
\n
\nstring.translate(s, table[, deletechars])
\n
Delete all characters from s that are in deletechars (if present), and then\ntranslate the characters using table, which must be a 256-character string\ngiving the translation for each character value, indexed by its ordinal. If\ntable is None, then only the character deletion step is performed.
\n\n
\n
\nstring.upper(s)
\n
Return a copy of s, but with lower case letters converted to upper case.
\n\n
\n
\nstring.ljust(s, width[, fillchar])
\n
\nstring.rjust(s, width[, fillchar])
\n
\nstring.center(s, width[, fillchar])
\n
These functions respectively left-justify, right-justify and center a string in\na field of given width. They return a string that is at least width\ncharacters wide, created by padding the string s with the character fillchar\n(default is a space) until the given width on the right, left or both sides.\nThe string is never truncated.
\n\n
\n
\nstring.zfill(s, width)
\n
Pad a numeric string on the left with zero digits until the given width is\nreached. Strings starting with a sign are handled correctly.
\n\n
\n
\nstring.replace(str, old, new[, maxreplace])
\n
Return a copy of string str with all occurrences of substring old replaced\nby new. If the optional argument maxreplace is given, the first\nmaxreplace occurrences are replaced.
\n\n
\n
", "searchableItems": [ { "name": "string.atof", "domId": "string_string.atof" }, { "name": "string.atoi", "domId": "string_string.atoi" }, { "name": "string.atol", "domId": "string_string.atol" }, { "name": "string.capitalize", "domId": "string_string.capitalize" }, { "name": "string.capwords", "domId": "string_string.capwords" }, { "name": "string.count", "domId": "string_string.count" }, { "name": "string.expandtabs", "domId": "string_string.expandtabs" }, { "name": "string.find", "domId": "string_string.find" }, { "name": "string.Formatter", "domId": "string_string.Formatter" }, { "name": "string.Formatter.check_unused_args", "domId": "string_string.Formatter.check_unused_args" }, { "name": "string.Formatter.convert_field", "domId": "string_string.Formatter.convert_field" }, { "name": "string.Formatter.format", "domId": "string_string.Formatter.format" }, { "name": "string.Formatter.format_field", "domId": "string_string.Formatter.format_field" }, { "name": "string.Formatter.get_field", "domId": "string_string.Formatter.get_field" }, { "name": "string.Formatter.get_value", "domId": "string_string.Formatter.get_value" }, { "name": "string.Formatter.parse", "domId": "string_string.Formatter.parse" }, { "name": "string.Formatter.vformat", "domId": "string_string.Formatter.vformat" }, { "name": "string.index", "domId": "string_string.index" }, { "name": "string.join", "domId": "string_string.join" }, { "name": "string.joinfields", "domId": "string_string.joinfields" }, { "name": "string.ljust", "domId": "string_string.ljust" }, { "name": "string.lower", "domId": "string_string.lower" }, { "name": "string.lstrip", "domId": "string_string.lstrip" }, { "name": "string.maketrans", "domId": "string_string.maketrans" }, { "name": "string.replace", "domId": "string_string.replace" }, { "name": "string.rfind", "domId": "string_string.rfind" }, { "name": "string.rindex", "domId": "string_string.rindex" }, { "name": "string.rsplit", "domId": "string_string.rsplit" }, { "name": "string.rstrip", "domId": "string_string.rstrip" }, { "name": "string.split", "domId": "string_string.split" }, { "name": "string.splitfields", "domId": "string_string.splitfields" }, { "name": "string.strip", "domId": "string_string.strip" }, { "name": "string.swapcase", "domId": "string_string.swapcase" }, { "name": "string.Template", "domId": "string_string.Template" }, { "name": "string.Template.safe_substitute", "domId": "string_string.Template.safe_substitute" }, { "name": "string.Template.substitute", "domId": "string_string.Template.substitute" }, { "name": "string.translate", "domId": "string_string.translate" }, { "name": "string.upper", "domId": "string_string.upper" }, { "name": "string.zfill", "domId": "string_string.zfill" } ] }, { "url": "http://docs.python.org/library/re.html", "title": "re", "html": "
\n

7.2. re — Regular expression operations

\n

This module provides regular expression matching operations similar to\nthose found in Perl. Both patterns and strings to be searched can be\nUnicode strings as well as 8-bit strings.

\n

Regular expressions use the backslash character ('\\') to indicate\nspecial forms or to allow special characters to be used without invoking\ntheir special meaning. This collides with Python’s usage of the same\ncharacter for the same purpose in string literals; for example, to match\na literal backslash, one might have to write '\\\\\\\\' as the pattern\nstring, because the regular expression must be \\\\, and each\nbackslash must be expressed as \\\\ inside a regular Python string\nliteral.

\n

The solution is to use Python’s raw string notation for regular expression\npatterns; backslashes are not handled in any special way in a string literal\nprefixed with 'r'. So r"\\n" is a two-character string containing\n'\\' and 'n', while "\\n" is a one-character string containing a\nnewline. Usually patterns will be expressed in Python code using this raw\nstring notation.

\n

It is important to note that most regular expression operations are available as\nmodule-level functions and RegexObject methods. The functions are\nshortcuts that don’t require you to compile a regex object first, but miss some\nfine-tuning parameters.

\n
\n

See also

\n
\n
Mastering Regular Expressions
\n
Book on regular expressions by Jeffrey Friedl, published by O’Reilly. The\nsecond edition of the book no longer covers Python at all, but the first\nedition covered writing good regular expression patterns in great detail.
\n
\n
\n
\n

7.2.1. Regular Expression Syntax

\n

A regular expression (or RE) specifies a set of strings that matches it; the\nfunctions in this module let you check if a particular string matches a given\nregular expression (or if a given regular expression matches a particular\nstring, which comes down to the same thing).

\n

Regular expressions can be concatenated to form new regular expressions; if A\nand B are both regular expressions, then AB is also a regular expression.\nIn general, if a string p matches A and another string q matches B, the\nstring pq will match AB. This holds unless A or B contain low precedence\noperations; boundary conditions between A and B; or have numbered group\nreferences. Thus, complex expressions can easily be constructed from simpler\nprimitive expressions like the ones described here. For details of the theory\nand implementation of regular expressions, consult the Friedl book referenced\nabove, or almost any textbook about compiler construction.

\n

A brief explanation of the format of regular expressions follows. For further\ninformation and a gentler presentation, consult the Regular Expression HOWTO.

\n

Regular expressions can contain both special and ordinary characters. Most\nordinary characters, like 'A', 'a', or '0', are the simplest regular\nexpressions; they simply match themselves. You can concatenate ordinary\ncharacters, so last matches the string 'last'. (In the rest of this\nsection, we’ll write RE’s in this special style, usually without quotes, and\nstrings to be matched 'in single quotes'.)

\n

Some characters, like '|' or '(', are special. Special\ncharacters either stand for classes of ordinary characters, or affect\nhow the regular expressions around them are interpreted. Regular\nexpression pattern strings may not contain null bytes, but can specify\nthe null byte using the \\number notation, e.g., '\\x00'.

\n

The special characters are:

\n
\n
'.'
\n
(Dot.) In the default mode, this matches any character except a newline. If\nthe DOTALL flag has been specified, this matches any character\nincluding a newline.
\n
'^'
\n
(Caret.) Matches the start of the string, and in MULTILINE mode also\nmatches immediately after each newline.
\n
'$'
\n
Matches the end of the string or just before the newline at the end of the\nstring, and in MULTILINE mode also matches before a newline. foo\nmatches both ‘foo’ and ‘foobar’, while the regular expression foo$ matches\nonly ‘foo’. More interestingly, searching for foo.$ in 'foo1\\nfoo2\\n'\nmatches ‘foo2’ normally, but ‘foo1’ in MULTILINE mode; searching for\na single $ in 'foo\\n' will find two (empty) matches: one just before\nthe newline, and one at the end of the string.
\n
'*'
\n
Causes the resulting RE to match 0 or more repetitions of the preceding RE, as\nmany repetitions as are possible. ab* will match ‘a’, ‘ab’, or ‘a’ followed\nby any number of ‘b’s.
\n
'+'
\n
Causes the resulting RE to match 1 or more repetitions of the preceding RE.\nab+ will match ‘a’ followed by any non-zero number of ‘b’s; it will not\nmatch just ‘a’.
\n
'?'
\n
Causes the resulting RE to match 0 or 1 repetitions of the preceding RE.\nab? will match either ‘a’ or ‘ab’.
\n
*?, +?, ??
\n
The '*', '+', and '?' qualifiers are all greedy; they match\nas much text as possible. Sometimes this behaviour isn’t desired; if the RE\n<.*> is matched against '<H1>title</H1>', it will match the entire\nstring, and not just '<H1>'. Adding '?' after the qualifier makes it\nperform the match in non-greedy or minimal fashion; as few\ncharacters as possible will be matched. Using .*? in the previous\nexpression will match only '<H1>'.
\n
{m}
\n
Specifies that exactly m copies of the previous RE should be matched; fewer\nmatches cause the entire RE not to match. For example, a{6} will match\nexactly six 'a' characters, but not five.
\n
{m,n}
\n
Causes the resulting RE to match from m to n repetitions of the preceding\nRE, attempting to match as many repetitions as possible. For example,\na{3,5} will match from 3 to 5 'a' characters. Omitting m specifies a\nlower bound of zero, and omitting n specifies an infinite upper bound. As an\nexample, a{4,}b will match aaaab or a thousand 'a' characters\nfollowed by a b, but not aaab. The comma may not be omitted or the\nmodifier would be confused with the previously described form.
\n
{m,n}?
\n
Causes the resulting RE to match from m to n repetitions of the preceding\nRE, attempting to match as few repetitions as possible. This is the\nnon-greedy version of the previous qualifier. For example, on the\n6-character string 'aaaaaa', a{3,5} will match 5 'a' characters,\nwhile a{3,5}? will only match 3 characters.
\n
'\\'
\n

Either escapes special characters (permitting you to match characters like\n'*', '?', and so forth), or signals a special sequence; special\nsequences are discussed below.

\n

If you’re not using a raw string to express the pattern, remember that Python\nalso uses the backslash as an escape sequence in string literals; if the escape\nsequence isn’t recognized by Python’s parser, the backslash and subsequent\ncharacter are included in the resulting string. However, if Python would\nrecognize the resulting sequence, the backslash should be repeated twice. This\nis complicated and hard to understand, so it’s highly recommended that you use\nraw strings for all but the simplest expressions.

\n
\n
[]
\n

Used to indicate a set of characters. In a set:

\n
    \n
  • Characters can be listed individually, e.g. [amk] will match 'a',\n'm', or 'k'.
  • \n
  • Ranges of characters can be indicated by giving two characters and separating\nthem by a '-', for example [a-z] will match any lowercase ASCII letter,\n[0-5][0-9] will match all the two-digits numbers from 00 to 59, and\n[0-9A-Fa-f] will match any hexadecimal digit. If - is escaped (e.g.\n[a\\-z]) or if it’s placed as the first or last character (e.g. [a-]),\nit will match a literal '-'.
  • \n
  • Special characters lose their special meaning inside sets. For example,\n[(+*)] will match any of the literal characters '(', '+',\n'*', or ')'.
  • \n
  • Character classes such as \\w or \\S (defined below) are also accepted\ninside a set, although the characters they match depends on whether\nLOCALE or UNICODE mode is in force.
  • \n
  • Characters that are not within a range can be matched by complementing\nthe set. If the first character of the set is '^', all the characters\nthat are not in the set will be matched. For example, [^5] will match\nany character except '5', and [^^] will match any character except\n'^'. ^ has no special meaning if it’s not the first character in\nthe set.
  • \n
  • To match a literal ']' inside a set, precede it with a backslash, or\nplace it at the beginning of the set. For example, both [()[\\]{}] and\n[]()[{}] will both match a parenthesis.
  • \n
\n
\n
'|'
\n
A|B, where A and B can be arbitrary REs, creates a regular expression that\nwill match either A or B. An arbitrary number of REs can be separated by the\n'|' in this way. This can be used inside groups (see below) as well. As\nthe target string is scanned, REs separated by '|' are tried from left to\nright. When one pattern completely matches, that branch is accepted. This means\nthat once A matches, B will not be tested further, even if it would\nproduce a longer overall match. In other words, the '|' operator is never\ngreedy. To match a literal '|', use \\|, or enclose it inside a\ncharacter class, as in [|].
\n
(...)
\n
Matches whatever regular expression is inside the parentheses, and indicates the\nstart and end of a group; the contents of a group can be retrieved after a match\nhas been performed, and can be matched later in the string with the \\number\nspecial sequence, described below. To match the literals '(' or ')',\nuse \\( or \\), or enclose them inside a character class: [(] [)].
\n
(?...)
\n
This is an extension notation (a '?' following a '(' is not meaningful\notherwise). The first character after the '?' determines what the meaning\nand further syntax of the construct is. Extensions usually do not create a new\ngroup; (?P<name>...) is the only exception to this rule. Following are the\ncurrently supported extensions.
\n
(?iLmsux)
\n

(One or more letters from the set 'i', 'L', 'm', 's',\n'u', 'x'.) The group matches the empty string; the letters\nset the corresponding flags: re.I (ignore case),\nre.L (locale dependent), re.M (multi-line),\nre.S (dot matches all), re.U (Unicode dependent),\nand re.X (verbose), for the entire regular expression. (The\nflags are described in Module Contents.) This\nis useful if you wish to include the flags as part of the regular\nexpression, instead of passing a flag argument to the\nre.compile() function.

\n

Note that the (?x) flag changes how the expression is parsed. It should be\nused first in the expression string, or after one or more whitespace characters.\nIf there are non-whitespace characters before the flag, the results are\nundefined.

\n
\n
(?:...)
\n
A non-capturing version of regular parentheses. Matches whatever regular\nexpression is inside the parentheses, but the substring matched by the group\ncannot be retrieved after performing a match or referenced later in the\npattern.
\n
(?P<name>...)
\n

Similar to regular parentheses, but the substring matched by the group is\naccessible within the rest of the regular expression via the symbolic group\nname name. Group names must be valid Python identifiers, and each group\nname must be defined only once within a regular expression. A symbolic group\nis also a numbered group, just as if the group were not named. So the group\nnamed id in the example below can also be referenced as the numbered group\n1.

\n

For example, if the pattern is (?P<id>[a-zA-Z_]\\w*), the group can be\nreferenced by its name in arguments to methods of match objects, such as\nm.group('id') or m.end('id'), and also by name in the regular\nexpression itself (using (?P=id)) and replacement text given to\n.sub() (using \\g<id>).

\n
\n
(?P=name)
\n
Matches whatever text was matched by the earlier group named name.
\n
(?#...)
\n
A comment; the contents of the parentheses are simply ignored.
\n
(?=...)
\n
Matches if ... matches next, but doesn’t consume any of the string. This is\ncalled a lookahead assertion. For example, Isaac (?=Asimov) will match\n'Isaac ' only if it’s followed by 'Asimov'.
\n
(?!...)
\n
Matches if ... doesn’t match next. This is a negative lookahead assertion.\nFor example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not\nfollowed by 'Asimov'.
\n
(?<=...)
\n

Matches if the current position in the string is preceded by a match for ...\nthat ends at the current position. This is called a positive lookbehind\nassertion. (?<=abc)def will find a match in abcdef, since the\nlookbehind will back up 3 characters and check if the contained pattern matches.\nThe contained pattern must only match strings of some fixed length, meaning that\nabc or a|b are allowed, but a* and a{3,4} are not. Note that\npatterns which start with positive lookbehind assertions will never match at the\nbeginning of the string being searched; you will most likely want to use the\nsearch() function rather than the match() function:

\n
>>> import re\n>>> m = re.search('(?<=abc)def', 'abcdef')\n>>> m.group(0)\n'def'\n
\n
\n

This example looks for a word following a hyphen:

\n
>>> m = re.search('(?<=-)\\w+', 'spam-egg')\n>>> m.group(0)\n'egg'\n
\n
\n
\n
(?<!...)
\n
Matches if the current position in the string is not preceded by a match for\n.... This is called a negative lookbehind assertion. Similar to\npositive lookbehind assertions, the contained pattern must only match strings of\nsome fixed length. Patterns which start with negative lookbehind assertions may\nmatch at the beginning of the string being searched.
\n
(?(id/name)yes-pattern|no-pattern)
\n

Will try to match with yes-pattern if the group with given id or name\nexists, and with no-pattern if it doesn’t. no-pattern is optional and\ncan be omitted. For example, (<)?(\\w+@\\w+(?:\\.\\w+)+)(?(1)>) is a poor email\nmatching pattern, which will match with '<user@host.com>' as well as\n'user@host.com', but not with '<user@host.com'.

\n

\nNew in version 2.4.

\n
\n
\n

The special sequences consist of '\\' and a character from the list below.\nIf the ordinary character is not on the list, then the resulting RE will match\nthe second character. For example, \\$ matches the character '$'.

\n
\n
\\number
\n
Matches the contents of the group of the same number. Groups are numbered\nstarting from 1. For example, (.+) \\1 matches 'the the' or '55 55',\nbut not 'the end' (note the space after the group). This special sequence\ncan only be used to match one of the first 99 groups. If the first digit of\nnumber is 0, or number is 3 octal digits long, it will not be interpreted as\na group match, but as the character with octal value number. Inside the\n'[' and ']' of a character class, all numeric escapes are treated as\ncharacters.
\n
\\A
\n
Matches only at the start of the string.
\n
\\b
\n
Matches the empty string, but only at the beginning or end of a word. A word is\ndefined as a sequence of alphanumeric or underscore characters, so the end of a\nword is indicated by whitespace or a non-alphanumeric, non-underscore character.\nNote that \\b is defined as the boundary between \\w and \\W, so the\nprecise set of characters deemed to be alphanumeric depends on the values of the\nUNICODE and LOCALE flags. Inside a character range, \\b represents\nthe backspace character, for compatibility with Python’s string literals.
\n
\\B
\n
Matches the empty string, but only when it is not at the beginning or end of a\nword. This is just the opposite of \\b, so is also subject to the settings\nof LOCALE and UNICODE.
\n
\\d
\n
When the UNICODE flag is not specified, matches any decimal digit; this\nis equivalent to the set [0-9]. With UNICODE, it will match\nwhatever is classified as a decimal digit in the Unicode character properties\ndatabase.
\n
\\D
\n
When the UNICODE flag is not specified, matches any non-digit\ncharacter; this is equivalent to the set [^0-9]. With UNICODE, it\nwill match anything other than character marked as digits in the Unicode\ncharacter properties database.
\n
\\s
\n
When the LOCALE and UNICODE flags are not specified, matches\nany whitespace character; this is equivalent to the set [ \\t\\n\\r\\f\\v]. With\nLOCALE, it will match this set plus whatever characters are defined as\nspace for the current locale. If UNICODE is set, this will match the\ncharacters [ \\t\\n\\r\\f\\v] plus whatever is classified as space in the Unicode\ncharacter properties database.
\n
\\S
\n
When the LOCALE and UNICODE flags are not specified, matches\nany non-whitespace character; this is equivalent to the set [^ \\t\\n\\r\\f\\v]\nWith LOCALE, it will match any character not in this set, and not\ndefined as space in the current locale. If UNICODE is set, this will\nmatch anything other than [ \\t\\n\\r\\f\\v] and characters marked as space in\nthe Unicode character properties database.
\n
\\w
\n
When the LOCALE and UNICODE flags are not specified, matches\nany alphanumeric character and the underscore; this is equivalent to the set\n[a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus\nwhatever characters are defined as alphanumeric for the current locale. If\nUNICODE is set, this will match the characters [0-9_] plus whatever\nis classified as alphanumeric in the Unicode character properties database.
\n
\\W
\n
When the LOCALE and UNICODE flags are not specified, matches\nany non-alphanumeric character; this is equivalent to the set [^a-zA-Z0-9_].\nWith LOCALE, it will match any character not in the set [0-9_], and\nnot defined as alphanumeric for the current locale. If UNICODE is set,\nthis will match anything other than [0-9_] and characters marked as\nalphanumeric in the Unicode character properties database.
\n
\\Z
\n
Matches only at the end of the string.
\n
\n

Most of the standard escapes supported by Python string literals are also\naccepted by the regular expression parser:

\n
\\a      \\b      \\f      \\n\n\\r      \\t      \\v      \\x\n\\\\
\n
\n

Octal escapes are included in a limited form: If the first digit is a 0, or if\nthere are three octal digits, it is considered an octal escape. Otherwise, it is\na group reference. As for string literals, octal escapes are always at most\nthree digits in length.

\n
\n
\n

7.2.2. Matching vs Searching

\n

Python offers two different primitive operations based on regular expressions:\nmatch checks for a match only at the beginning of the string, while\nsearch checks for a match anywhere in the string (this is what Perl does\nby default).

\n

Note that match may differ from search even when using a regular expression\nbeginning with '^': '^' matches only at the start of the string, or in\nMULTILINE mode also immediately following a newline. The “match”\noperation succeeds only if the pattern matches at the start of the string\nregardless of mode, or at the starting position given by the optional pos\nargument regardless of whether a newline precedes it.

\n
>>> re.match("c", "abcdef")  # No match\n>>> re.search("c", "abcdef") # Match\n<_sre.SRE_Match object at ...>\n
\n
\n
\n
\n

7.2.3. Module Contents

\n

The module defines several functions, constants, and an exception. Some of the\nfunctions are simplified versions of the full featured methods for compiled\nregular expressions. Most non-trivial applications always use the compiled\nform.

\n
\n
\nre.compile(pattern, flags=0)
\n

Compile a regular expression pattern into a regular expression object, which\ncan be used for matching using its match() and search() methods,\ndescribed below.

\n

The expression’s behaviour can be modified by specifying a flags value.\nValues can be any of the following variables, combined using bitwise OR (the\n| operator).

\n

The sequence

\n
prog = re.compile(pattern)\nresult = prog.match(string)\n
\n
\n

is equivalent to

\n
result = re.match(pattern, string)\n
\n
\n

but using re.compile() and saving the resulting regular expression\nobject for reuse is more efficient when the expression will be used several\ntimes in a single program.

\n
\n

Note

\n

The compiled versions of the most recent patterns passed to\nre.match(), re.search() or re.compile() are cached, so\nprograms that use only a few regular expressions at a time needn’t worry\nabout compiling regular expressions.

\n
\n
\n\n
\n
\nre.DEBUG
\n
Display debug information about compiled expression.
\n\n
\n
\nre.I
\n
\nre.IGNORECASE
\n
Perform case-insensitive matching; expressions like [A-Z] will match\nlowercase letters, too. This is not affected by the current locale.
\n\n
\n
\nre.L
\n
\nre.LOCALE
\n
Make \\w, \\W, \\b, \\B, \\s and \\S dependent on the\ncurrent locale.
\n\n
\n
\nre.M
\n
\nre.MULTILINE
\n
When specified, the pattern character '^' matches at the beginning of the\nstring and at the beginning of each line (immediately following each newline);\nand the pattern character '$' matches at the end of the string and at the\nend of each line (immediately preceding each newline). By default, '^'\nmatches only at the beginning of the string, and '$' only at the end of the\nstring and immediately before the newline (if any) at the end of the string.
\n\n
\n
\nre.S
\n
\nre.DOTALL
\n
Make the '.' special character match any character at all, including a\nnewline; without this flag, '.' will match anything except a newline.
\n\n
\n
\nre.U
\n
\nre.UNICODE
\n

Make \\w, \\W, \\b, \\B, \\d, \\D, \\s and \\S dependent\non the Unicode character properties database.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nre.X
\n
\nre.VERBOSE
\n

This flag allows you to write regular expressions that look nicer. Whitespace\nwithin the pattern is ignored, except when in a character class or preceded by\nan unescaped backslash, and, when a line contains a '#' neither in a\ncharacter class or preceded by an unescaped backslash, all characters from the\nleftmost such '#' through the end of the line are ignored.

\n

That means that the two following regular expression objects that match a\ndecimal number are functionally equal:

\n
a = re.compile(r"""\\d +  # the integral part\n                   \\.    # the decimal point\n                   \\d *  # some fractional digits""", re.X)\nb = re.compile(r"\\d+\\.\\d*")\n
\n
\n
\n\n
\n
\nre.search(pattern, string, flags=0)
\n
Scan through string looking for a location where the regular expression\npattern produces a match, and return a corresponding MatchObject\ninstance. Return None if no position in the string matches the pattern; note\nthat this is different from finding a zero-length match at some point in the\nstring.
\n\n
\n
\nre.match(pattern, string, flags=0)
\n

If zero or more characters at the beginning of string match the regular\nexpression pattern, return a corresponding MatchObject instance.\nReturn None if the string does not match the pattern; note that this is\ndifferent from a zero-length match.

\n
\n

Note

\n

If you want to locate a match anywhere in string, use search()\ninstead.

\n
\n
\n\n
\n
\nre.split(pattern, string, maxsplit=0, flags=0)
\n

Split string by the occurrences of pattern. If capturing parentheses are\nused in pattern, then the text of all groups in the pattern are also returned\nas part of the resulting list. If maxsplit is nonzero, at most maxsplit\nsplits occur, and the remainder of the string is returned as the final element\nof the list. (Incompatibility note: in the original Python 1.5 release,\nmaxsplit was ignored. This has been fixed in later releases.)

\n
>>> re.split('\\W+', 'Words, words, words.')\n['Words', 'words', 'words', '']\n>>> re.split('(\\W+)', 'Words, words, words.')\n['Words', ', ', 'words', ', ', 'words', '.', '']\n>>> re.split('\\W+', 'Words, words, words.', 1)\n['Words', 'words, words.']\n>>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)\n['0', '3', '9']\n
\n
\n

If there are capturing groups in the separator and it matches at the start of\nthe string, the result will start with an empty string. The same holds for\nthe end of the string:

\n
>>> re.split('(\\W+)', '...words, words...')\n['', '...', 'words', ', ', 'words', '...', '']\n
\n
\n

That way, separator components are always found at the same relative\nindices within the result list (e.g., if there’s one capturing group\nin the separator, the 0th, the 2nd and so forth).

\n

Note that split will never split a string on an empty pattern match.\nFor example:

\n
>>> re.split('x*', 'foo')\n['foo']\n>>> re.split("(?m)^$", "foo\\n\\nbar\\n")\n['foo\\n\\nbar\\n']\n
\n
\n

\nChanged in version 2.7: Added the optional flags argument.

\n
\n\n
\n
\nre.findall(pattern, string, flags=0)
\n

Return all non-overlapping matches of pattern in string, as a list of\nstrings. The string is scanned left-to-right, and matches are returned in\nthe order found. If one or more groups are present in the pattern, return a\nlist of groups; this will be a list of tuples if the pattern has more than\none group. Empty matches are included in the result unless they touch the\nbeginning of another match.

\n

\nNew in version 1.5.2.

\n

\nChanged in version 2.4: Added the optional flags argument.

\n
\n\n
\n
\nre.finditer(pattern, string, flags=0)
\n

Return an iterator yielding MatchObject instances over all\nnon-overlapping matches for the RE pattern in string. The string is\nscanned left-to-right, and matches are returned in the order found. Empty\nmatches are included in the result unless they touch the beginning of another\nmatch.

\n

\nNew in version 2.2.

\n

\nChanged in version 2.4: Added the optional flags argument.

\n
\n\n
\n
\nre.sub(pattern, repl, string, count=0, flags=0)
\n

Return the string obtained by replacing the leftmost non-overlapping occurrences\nof pattern in string by the replacement repl. If the pattern isn’t found,\nstring is returned unchanged. repl can be a string or a function; if it is\na string, any backslash escapes in it are processed. That is, \\n is\nconverted to a single newline character, \\r is converted to a carriage return, and\nso forth. Unknown escapes such as \\j are left alone. Backreferences, such\nas \\6, are replaced with the substring matched by group 6 in the pattern.\nFor example:

\n
>>> re.sub(r'def\\s+([a-zA-Z_][a-zA-Z_0-9]*)\\s*\\(\\s*\\):',\n...        r'static PyObject*\\npy_\\1(void)\\n{',\n...        'def myfunc():')\n'static PyObject*\\npy_myfunc(void)\\n{'\n
\n
\n

If repl is a function, it is called for every non-overlapping occurrence of\npattern. The function takes a single match object argument, and returns the\nreplacement string. For example:

\n
>>> def dashrepl(matchobj):\n...     if matchobj.group(0) == '-': return ' '\n...     else: return '-'\n>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')\n'pro--gram files'\n>>> re.sub(r'\\sAND\\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)\n'Baked Beans & Spam'\n
\n
\n

The pattern may be a string or an RE object.

\n

The optional argument count is the maximum number of pattern occurrences to be\nreplaced; count must be a non-negative integer. If omitted or zero, all\noccurrences will be replaced. Empty matches for the pattern are replaced only\nwhen not adjacent to a previous match, so sub('x*', '-', 'abc') returns\n'-a-b-c-'.

\n

In addition to character escapes and backreferences as described above,\n\\g<name> will use the substring matched by the group named name, as\ndefined by the (?P<name>...) syntax. \\g<number> uses the corresponding\ngroup number; \\g<2> is therefore equivalent to \\2, but isn’t ambiguous\nin a replacement such as \\g<2>0. \\20 would be interpreted as a\nreference to group 20, not a reference to group 2 followed by the literal\ncharacter '0'. The backreference \\g<0> substitutes in the entire\nsubstring matched by the RE.

\n

\nChanged in version 2.7: Added the optional flags argument.

\n
\n\n
\n
\nre.subn(pattern, repl, string, count=0, flags=0)
\n

Perform the same operation as sub(), but return a tuple (new_string,\nnumber_of_subs_made).

\n

\nChanged in version 2.7: Added the optional flags argument.

\n
\n\n
\n
\nre.escape(string)
\n
Return string with all non-alphanumerics backslashed; this is useful if you\nwant to match an arbitrary literal string that may have regular expression\nmetacharacters in it.
\n\n
\n
\nre.purge()
\n
Clear the regular expression cache.
\n\n
\n
\nexception re.error
\n
Exception raised when a string passed to one of the functions here is not a\nvalid regular expression (for example, it might contain unmatched parentheses)\nor when some other error occurs during compilation or matching. It is never an\nerror if a string contains no match for a pattern.
\n\n
\n
\n

7.2.4. Regular Expression Objects

\n
\n
\nclass re.RegexObject
\n

The RegexObject class supports the following methods and attributes:

\n
\n
\nsearch(string[, pos[, endpos]])
\n

Scan through string looking for a location where this regular expression\nproduces a match, and return a corresponding MatchObject instance.\nReturn None if no position in the string matches the pattern; note that this\nis different from finding a zero-length match at some point in the string.

\n

The optional second parameter pos gives an index in the string where the\nsearch is to start; it defaults to 0. This is not completely equivalent to\nslicing the string; the '^' pattern character matches at the real beginning\nof the string and at positions just after a newline, but not necessarily at the\nindex where the search is to start.

\n

The optional parameter endpos limits how far the string will be searched; it\nwill be as if the string is endpos characters long, so only the characters\nfrom pos to endpos - 1 will be searched for a match. If endpos is less\nthan pos, no match will be found, otherwise, if rx is a compiled regular\nexpression object, rx.search(string, 0, 50) is equivalent to\nrx.search(string[:50], 0).

\n
>>> pattern = re.compile("d")\n>>> pattern.search("dog")     # Match at index 0\n<_sre.SRE_Match object at ...>\n>>> pattern.search("dog", 1)  # No match; search doesn't include the "d"\n
\n
\n
\n\n
\n
\nmatch(string[, pos[, endpos]])
\n

If zero or more characters at the beginning of string match this regular\nexpression, return a corresponding MatchObject instance. Return\nNone if the string does not match the pattern; note that this is different\nfrom a zero-length match.

\n

The optional pos and endpos parameters have the same meaning as for the\nsearch() method.

\n
\n

Note

\n

If you want to locate a match anywhere in string, use\nsearch() instead.

\n
\n
>>> pattern = re.compile("o")\n>>> pattern.match("dog")      # No match as "o" is not at the start of "dog".\n>>> pattern.match("dog", 1)   # Match as "o" is the 2nd character of "dog".\n<_sre.SRE_Match object at ...>\n
\n
\n
\n\n
\n
\nsplit(string, maxsplit=0)
\n
Identical to the split() function, using the compiled pattern.
\n\n
\n
\nfindall(string[, pos[, endpos]])
\n
Similar to the findall() function, using the compiled pattern, but\nalso accepts optional pos and endpos parameters that limit the search\nregion like for match().
\n\n
\n
\nfinditer(string[, pos[, endpos]])
\n
Similar to the finditer() function, using the compiled pattern, but\nalso accepts optional pos and endpos parameters that limit the search\nregion like for match().
\n\n
\n
\nsub(repl, string, count=0)
\n
Identical to the sub() function, using the compiled pattern.
\n\n
\n
\nsubn(repl, string, count=0)
\n
Identical to the subn() function, using the compiled pattern.
\n\n
\n
\nflags
\n
The flags argument used when the RE object was compiled, or 0 if no flags\nwere provided.
\n\n
\n
\ngroups
\n
The number of capturing groups in the pattern.
\n\n
\n
\ngroupindex
\n
A dictionary mapping any symbolic group names defined by (?P<id>) to group\nnumbers. The dictionary is empty if no symbolic groups were used in the\npattern.
\n\n
\n
\npattern
\n
The pattern string from which the RE object was compiled.
\n\n
\n\n
\n
\n

7.2.5. Match Objects

\n
\n
\nclass re.MatchObject
\n

Match Objects always have a boolean value of True, so that you can test\nwhether e.g. match() resulted in a match with a simple if statement. They\nsupport the following methods and attributes:

\n
\n
\nexpand(template)
\n
Return the string obtained by doing backslash substitution on the template\nstring template, as done by the sub() method. Escapes\nsuch as \\n are converted to the appropriate characters, and numeric\nbackreferences (\\1, \\2) and named backreferences (\\g<1>,\n\\g<name>) are replaced by the contents of the corresponding group.
\n\n
\n
\ngroup([group1, ...])
\n

Returns one or more subgroups of the match. If there is a single argument, the\nresult is a single string; if there are multiple arguments, the result is a\ntuple with one item per argument. Without arguments, group1 defaults to zero\n(the whole match is returned). If a groupN argument is zero, the corresponding\nreturn value is the entire matching string; if it is in the inclusive range\n[1..99], it is the string matching the corresponding parenthesized group. If a\ngroup number is negative or larger than the number of groups defined in the\npattern, an IndexError exception is raised. If a group is contained in a\npart of the pattern that did not match, the corresponding result is None.\nIf a group is contained in a part of the pattern that matched multiple times,\nthe last match is returned.

\n
>>> m = re.match(r"(\\w+) (\\w+)", "Isaac Newton, physicist")\n>>> m.group(0)       # The entire match\n'Isaac Newton'\n>>> m.group(1)       # The first parenthesized subgroup.\n'Isaac'\n>>> m.group(2)       # The second parenthesized subgroup.\n'Newton'\n>>> m.group(1, 2)    # Multiple arguments give us a tuple.\n('Isaac', 'Newton')\n
\n
\n

If the regular expression uses the (?P<name>...) syntax, the groupN\narguments may also be strings identifying groups by their group name. If a\nstring argument is not used as a group name in the pattern, an IndexError\nexception is raised.

\n

A moderately complicated example:

\n
>>> m = re.match(r"(?P<first_name>\\w+) (?P<last_name>\\w+)", "Malcolm Reynolds")\n>>> m.group('first_name')\n'Malcolm'\n>>> m.group('last_name')\n'Reynolds'\n
\n
\n

Named groups can also be referred to by their index:

\n
>>> m.group(1)\n'Malcolm'\n>>> m.group(2)\n'Reynolds'\n
\n
\n

If a group matches multiple times, only the last match is accessible:

\n
>>> m = re.match(r"(..)+", "a1b2c3")  # Matches 3 times.\n>>> m.group(1)                        # Returns only the last match.\n'c3'\n
\n
\n
\n\n
\n
\ngroups([default])
\n

Return a tuple containing all the subgroups of the match, from 1 up to however\nmany groups are in the pattern. The default argument is used for groups that\ndid not participate in the match; it defaults to None. (Incompatibility\nnote: in the original Python 1.5 release, if the tuple was one element long, a\nstring would be returned instead. In later versions (from 1.5.1 on), a\nsingleton tuple is returned in such cases.)

\n

For example:

\n
>>> m = re.match(r"(\\d+)\\.(\\d+)", "24.1632")\n>>> m.groups()\n('24', '1632')\n
\n
\n

If we make the decimal place and everything after it optional, not all groups\nmight participate in the match. These groups will default to None unless\nthe default argument is given:

\n
>>> m = re.match(r"(\\d+)\\.?(\\d+)?", "24")\n>>> m.groups()      # Second group defaults to None.\n('24', None)\n>>> m.groups('0')   # Now, the second group defaults to '0'.\n('24', '0')\n
\n
\n
\n\n
\n
\ngroupdict([default])
\n

Return a dictionary containing all the named subgroups of the match, keyed by\nthe subgroup name. The default argument is used for groups that did not\nparticipate in the match; it defaults to None. For example:

\n
>>> m = re.match(r"(?P<first_name>\\w+) (?P<last_name>\\w+)", "Malcolm Reynolds")\n>>> m.groupdict()\n{'first_name': 'Malcolm', 'last_name': 'Reynolds'}\n
\n
\n
\n\n
\n
\nstart([group])
\n
\nend([group])
\n

Return the indices of the start and end of the substring matched by group;\ngroup defaults to zero (meaning the whole matched substring). Return -1 if\ngroup exists but did not contribute to the match. For a match object m, and\na group g that did contribute to the match, the substring matched by group g\n(equivalent to m.group(g)) is

\n
m.string[m.start(g):m.end(g)]\n
\n
\n

Note that m.start(group) will equal m.end(group) if group matched a\nnull string. For example, after m = re.search('b(c?)', 'cba'),\nm.start(0) is 1, m.end(0) is 2, m.start(1) and m.end(1) are both\n2, and m.start(2) raises an IndexError exception.

\n

An example that will remove remove_this from email addresses:

\n
>>> email = "tony@tiremove_thisger.net"\n>>> m = re.search("remove_this", email)\n>>> email[:m.start()] + email[m.end():]\n'tony@tiger.net'\n
\n
\n
\n\n
\n
\nspan([group])
\n
For MatchObject m, return the 2-tuple (m.start(group),\nm.end(group)). Note that if group did not contribute to the match, this is\n(-1, -1). group defaults to zero, the entire match.
\n\n
\n
\npos
\n
The value of pos which was passed to the search() or\nmatch() method of the RegexObject. This is the\nindex into the string at which the RE engine started looking for a match.
\n\n
\n
\nendpos
\n
The value of endpos which was passed to the search() or\nmatch() method of the RegexObject. This is the\nindex into the string beyond which the RE engine will not go.
\n\n
\n
\nlastindex
\n
The integer index of the last matched capturing group, or None if no group\nwas matched at all. For example, the expressions (a)b, ((a)(b)), and\n((ab)) will have lastindex == 1 if applied to the string 'ab', while\nthe expression (a)(b) will have lastindex == 2, if applied to the same\nstring.
\n\n
\n
\nlastgroup
\n
The name of the last matched capturing group, or None if the group didn’t\nhave a name, or if no group was matched at all.
\n\n
\n
\nre
\n
The regular expression object whose match() or\nsearch() method produced this MatchObject\ninstance.
\n\n
\n
\nstring
\n
The string passed to match() or\nsearch().
\n\n
\n\n
\n
\n

7.2.6. Examples

\n
\n

7.2.6.1. Checking For a Pair

\n

In this example, we’ll use the following helper function to display match\nobjects a little more gracefully:

\n
def displaymatch(match):\n    if match is None:\n        return None\n    return '<Match: %r, groups=%r>'  (match.group(), match.groups())\n
\n
\n

Suppose you are writing a poker program where a player’s hand is represented as\na 5-character string with each character representing a card, “a” for ace, “k”\nfor king, “q” for queen, “j” for jack, “t” for 10, and “2” through “9”\nrepresenting the card with that value.

\n

To see if a given string is a valid hand, one could do the following:

\n
>>> valid = re.compile(r"^[a2-9tjqk]{5}$")\n>>> displaymatch(valid.match("akt5q"))  # Valid.\n"<Match: 'akt5q', groups=()>"\n>>> displaymatch(valid.match("akt5e"))  # Invalid.\n>>> displaymatch(valid.match("akt"))    # Invalid.\n>>> displaymatch(valid.match("727ak"))  # Valid.\n"<Match: '727ak', groups=()>"\n
\n
\n

That last hand, "727ak", contained a pair, or two of the same valued cards.\nTo match this with a regular expression, one could use backreferences as such:

\n
>>> pair = re.compile(r".*(.).*\\1")\n>>> displaymatch(pair.match("717ak"))     # Pair of 7s.\n"<Match: '717', groups=('7',)>"\n>>> displaymatch(pair.match("718ak"))     # No pairs.\n>>> displaymatch(pair.match("354aa"))     # Pair of aces.\n"<Match: '354aa', groups=('a',)>"\n
\n
\n

To find out what card the pair consists of, one could use the\ngroup() method of MatchObject in the following\nmanner:

\n
>>> pair.match("717ak").group(1)\n'7'\n\n# Error because re.match() returns None, which doesn't have a group() method:\n>>> pair.match("718ak").group(1)\nTraceback (most recent call last):\n  File "<pyshell#23>", line 1, in <module>\n    re.match(r".*(.).*\\1", "718ak").group(1)\nAttributeError: 'NoneType' object has no attribute 'group'\n\n>>> pair.match("354aa").group(1)\n'a'\n
\n
\n
\n
\n

7.2.6.2. Simulating scanf()

\n

Python does not currently have an equivalent to scanf(). Regular\nexpressions are generally more powerful, though also more verbose, than\nscanf() format strings. The table below offers some more-or-less\nequivalent mappings between scanf() format tokens and regular\nexpressions.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
scanf() TokenRegular Expression
%c.
%5c.{5}
%d[-+]?\\d+
%e, %E, %f, %g[-+]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?
%i[-+]?(0[xX][\\dA-Fa-f]+|0[0-7]*|\\d+)
%o0[0-7]*
%s\\S+
%u\\d+
%x, %X0[xX][\\dA-Fa-f]+
\n

To extract the filename and numbers from a string like

\n
/usr/sbin/sendmail - 0 errors, 4 warnings
\n
\n

you would use a scanf() format like

\n
%s - %d errors, %d warnings
\n
\n

The equivalent regular expression would be

\n
(\\S+) - (\\d+) errors, (\\d+) warnings
\n
\n
\n
\n

7.2.6.3. Avoiding recursion

\n

If you create regular expressions that require the engine to perform a lot of\nrecursion, you may encounter a RuntimeError exception with the message\nmaximum recursion limit exceeded. For example,

\n
>>> s = 'Begin ' + 1000*'a very long string ' + 'end'\n>>> re.match('Begin (\\w| )*? end', s).end()\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\n  File "/usr/local/lib/python2.5/re.py", line 132, in match\n    return _compile(pattern, flags).match(string)\nRuntimeError: maximum recursion limit exceeded\n
\n
\n

You can often restructure your regular expression to avoid recursion.

\n

Starting with Python 2.3, simple uses of the *? pattern are special-cased to\navoid recursion. Thus, the above regular expression can avoid recursion by\nbeing recast as Begin [a-zA-Z0-9_ ]*?end. As a further benefit, such\nregular expressions will run faster than their recursive equivalents.

\n
\n
\n

7.2.6.4. search() vs. match()

\n

In a nutshell, match() only attempts to match a pattern at the beginning\nof a string where search() will match a pattern anywhere in a string.\nFor example:

\n
>>> re.match("o", "dog")  # No match as "o" is not the first letter of "dog".\n>>> re.search("o", "dog") # Match as search() looks everywhere in the string.\n<_sre.SRE_Match object at ...>\n
\n
\n
\n

Note

\n

The following applies only to regular expression objects like those created\nwith re.compile("pattern"), not the primitives re.match(pattern,\nstring) or re.search(pattern, string).

\n
\n

match() has an optional second parameter that gives an index in the string\nwhere the search is to start:

\n
>>> pattern = re.compile("o")\n>>> pattern.match("dog")      # No match as "o" is not at the start of "dog."\n\n# Equivalent to the above expression as 0 is the default starting index:\n>>> pattern.match("dog", 0)\n\n# Match as "o" is the 2nd character of "dog" (index 0 is the first):\n>>> pattern.match("dog", 1)\n<_sre.SRE_Match object at ...>\n>>> pattern.match("dog", 2)   # No match as "o" is not the 3rd character of "dog."\n
\n
\n
\n
\n

7.2.6.5. Making a Phonebook

\n

split() splits a string into a list delimited by the passed pattern. The\nmethod is invaluable for converting textual data into data structures that can be\neasily read and modified by Python as demonstrated in the following example that\ncreates a phonebook.

\n

First, here is the input. Normally it may come from a file, here we are using\ntriple-quoted string syntax:

\n
>>> input = """Ross McFluff: 834.345.1254 155 Elm Street\n...\n... Ronald Heathmore: 892.345.3428 436 Finley Avenue\n... Frank Burger: 925.541.7625 662 South Dogwood Way\n...\n...\n... Heather Albrecht: 548.326.4584 919 Park Place"""\n
\n
\n

The entries are separated by one or more newlines. Now we convert the string\ninto a list with each nonempty line having its own entry:

\n
>>> entries = re.split("\\n+", input)\n>>> entries\n['Ross McFluff: 834.345.1254 155 Elm Street',\n'Ronald Heathmore: 892.345.3428 436 Finley Avenue',\n'Frank Burger: 925.541.7625 662 South Dogwood Way',\n'Heather Albrecht: 548.326.4584 919 Park Place']\n
\n
\n

Finally, split each entry into a list with first name, last name, telephone\nnumber, and address. We use the maxsplit parameter of split()\nbecause the address has spaces, our splitting pattern, in it:

\n
>>> [re.split(":? ", entry, 3) for entry in entries]\n[['Ross', 'McFluff', '834.345.1254', '155 Elm Street'],\n['Ronald', 'Heathmore', '892.345.3428', '436 Finley Avenue'],\n['Frank', 'Burger', '925.541.7625', '662 South Dogwood Way'],\n['Heather', 'Albrecht', '548.326.4584', '919 Park Place']]\n
\n
\n

The :? pattern matches the colon after the last name, so that it does not\noccur in the result list. With a maxsplit of 4, we could separate the\nhouse number from the street name:

\n
>>> [re.split(":? ", entry, 4) for entry in entries]\n[['Ross', 'McFluff', '834.345.1254', '155', 'Elm Street'],\n['Ronald', 'Heathmore', '892.345.3428', '436', 'Finley Avenue'],\n['Frank', 'Burger', '925.541.7625', '662', 'South Dogwood Way'],\n['Heather', 'Albrecht', '548.326.4584', '919', 'Park Place']]\n
\n
\n
\n
\n

7.2.6.6. Text Munging

\n

sub() replaces every occurrence of a pattern with a string or the\nresult of a function. This example demonstrates using sub() with\na function to “munge” text, or randomize the order of all the characters\nin each word of a sentence except for the first and last characters:

\n
>>> def repl(m):\n...   inner_word = list(m.group(2))\n...   random.shuffle(inner_word)\n...   return m.group(1) + "".join(inner_word) + m.group(3)\n>>> text = "Professor Abdolmalek, please report your absences promptly."\n>>> re.sub(r"(\\w)(\\w+)(\\w)", repl, text)\n'Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.'\n>>> re.sub(r"(\\w)(\\w+)(\\w)", repl, text)\n'Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.'\n
\n
\n
\n
\n

7.2.6.7. Finding all Adverbs

\n

findall() matches all occurrences of a pattern, not just the first\none as search() does. For example, if one was a writer and wanted to\nfind all of the adverbs in some text, he or she might use findall() in\nthe following manner:

\n
>>> text = "He was carefully disguised but captured quickly by police."\n>>> re.findall(r"\\w+ly", text)\n['carefully', 'quickly']\n
\n
\n
\n
\n

7.2.6.8. Finding all Adverbs and their Positions

\n

If one wants more information about all matches of a pattern than the matched\ntext, finditer() is useful as it provides instances of\nMatchObject instead of strings. Continuing with the previous example,\nif one was a writer who wanted to find all of the adverbs and their positions\nin some text, he or she would use finditer() in the following manner:

\n
>>> text = "He was carefully disguised but captured quickly by police."\n>>> for m in re.finditer(r"\\w+ly", text):\n...     print '%02d-%02d: %s'  (m.start(), m.end(), m.group(0))\n07-16: carefully\n40-47: quickly\n
\n
\n
\n
\n

7.2.6.9. Raw String Notation

\n

Raw string notation (r"text") keeps regular expressions sane. Without it,\nevery backslash ('\\') in a regular expression would have to be prefixed with\nanother one to escape it. For example, the two following lines of code are\nfunctionally identical:

\n
>>> re.match(r"\\W(.)\\1\\W", " ff ")\n<_sre.SRE_Match object at ...>\n>>> re.match("\\\\W(.)\\\\1\\\\W", " ff ")\n<_sre.SRE_Match object at ...>\n
\n
\n

When one wants to match a literal backslash, it must be escaped in the regular\nexpression. With raw string notation, this means r"\\\\". Without raw string\nnotation, one must use "\\\\\\\\", making the following lines of code\nfunctionally identical:

\n
>>> re.match(r"\\\\", r"\\\\")\n<_sre.SRE_Match object at ...>\n>>> re.match("\\\\\\\\", r"\\\\")\n<_sre.SRE_Match object at ...>\n
\n
\n
\n
\n
", "searchableItems": [ { "name": "re.compile", "domId": "re_re.compile" }, { "name": "re.escape", "domId": "re_re.escape" }, { "name": "re.findall", "domId": "re_re.findall" }, { "name": "re.finditer", "domId": "re_re.finditer" }, { "name": "re.match", "domId": "re_re.match" }, { "name": "re.MatchObject", "domId": "re_re.MatchObject" }, { "name": "re.MatchObject.expand", "domId": "re_re.MatchObject.expand" }, { "name": "re.MatchObject.group", "domId": "re_re.MatchObject.group" }, { "name": "re.MatchObject.groupdict", "domId": "re_re.MatchObject.groupdict" }, { "name": "re.MatchObject.groups", "domId": "re_re.MatchObject.groups" }, { "name": "re.MatchObject.span", "domId": "re_re.MatchObject.span" }, { "name": "re.MatchObject.start", "domId": "re_re.MatchObject.start" }, { "name": "re.purge", "domId": "re_re.purge" }, { "name": "re.RegexObject", "domId": "re_re.RegexObject" }, { "name": "re.RegexObject.findall", "domId": "re_re.RegexObject.findall" }, { "name": "re.RegexObject.finditer", "domId": "re_re.RegexObject.finditer" }, { "name": "re.RegexObject.match", "domId": "re_re.RegexObject.match" }, { "name": "re.RegexObject.search", "domId": "re_re.RegexObject.search" }, { "name": "re.RegexObject.split", "domId": "re_re.RegexObject.split" }, { "name": "re.RegexObject.sub", "domId": "re_re.RegexObject.sub" }, { "name": "re.RegexObject.subn", "domId": "re_re.RegexObject.subn" }, { "name": "re.search", "domId": "re_re.search" }, { "name": "re.split", "domId": "re_re.split" }, { "name": "re.sub", "domId": "re_re.sub" }, { "name": "re.subn", "domId": "re_re.subn" } ] }, { "url": "http://docs.python.org/library/stringio.html", "title": "StringIO", "html": "
\n

7.5. StringIO — Read and write strings as files

\n

This module implements a file-like class, StringIO, that reads and\nwrites a string buffer (also known as memory files). See the description of\nfile objects for operations (section File Objects). (For\nstandard strings, see str and unicode.)

\n
\n
\nclass StringIO.StringIO([buffer])
\n

When a StringIO object is created, it can be initialized to an existing\nstring by passing the string to the constructor. If no string is given, the\nStringIO will start empty. In both cases, the initial file position\nstarts at zero.

\n

The StringIO object can accept either Unicode or 8-bit strings, but\nmixing the two may take some care. If both are used, 8-bit strings that cannot\nbe interpreted as 7-bit ASCII (that use the 8th bit) will cause a\nUnicodeError to be raised when getvalue() is called.

\n
\n\n

The following methods of StringIO objects require special mention:

\n
\n
\nStringIO.getvalue()
\n
Retrieve the entire contents of the “file” at any time before the\nStringIO object’s close() method is called. See the note above\nfor information about mixing Unicode and 8-bit strings; such mixing can cause\nthis method to raise UnicodeError.
\n\n
\n
\nStringIO.close()
\n
Free the memory buffer. Attempting to do further operations with a closed\nStringIO object will raise a ValueError.
\n\n

Example usage:

\n
import StringIO\n\noutput = StringIO.StringIO()\noutput.write('First line.\\n')\nprint >>output, 'Second line.'\n\n# Retrieve file contents -- this will be\n# 'First line.\\nSecond line.\\n'\ncontents = output.getvalue()\n\n# Close object and discard memory buffer --\n# .getvalue() will now raise an exception.\noutput.close()\n
\n
\n
\n

7.6. cStringIO — Faster version of StringIO

\n

The module cStringIO provides an interface similar to that of the\nStringIO module. Heavy use of StringIO.StringIO objects can be\nmade more efficient by using the function StringIO() from this module\ninstead.

\n
\n
\ncStringIO.StringIO([s])
\n

Return a StringIO-like stream for reading or writing.

\n

Since this is a factory function which returns objects of built-in types,\nthere’s no way to build your own version using subclassing. It’s not\npossible to set attributes on it. Use the original StringIO module in\nthose cases.

\n

Unlike the StringIO module, this module is not able to accept Unicode\nstrings that cannot be encoded as plain ASCII strings.

\n

Another difference from the StringIO module is that calling\nStringIO() with a string parameter creates a read-only object. Unlike an\nobject created without a string parameter, it does not have write methods.\nThese objects are not generally visible. They turn up in tracebacks as\nStringI and StringO.

\n
\n\n

The following data objects are provided as well:

\n
\n
\ncStringIO.InputType
\n
The type object of the objects created by calling StringIO() with a string\nparameter.
\n\n
\n
\ncStringIO.OutputType
\n
The type object of the objects returned by calling StringIO() with no\nparameters.
\n\n

There is a C API to the module as well; refer to the module source for more\ninformation.

\n

Example usage:

\n
import cStringIO\n\noutput = cStringIO.StringIO()\noutput.write('First line.\\n')\nprint >>output, 'Second line.'\n\n# Retrieve file contents -- this will be\n# 'First line.\\nSecond line.\\n'\ncontents = output.getvalue()\n\n# Close object and discard memory buffer --\n# .getvalue() will now raise an exception.\noutput.close()\n
\n
\n
", "searchableItems": [ { "name": "cStringIO.StringIO", "domId": "StringIO_cStringIO.StringIO" }, { "name": "StringIO.StringIO", "domId": "StringIO_StringIO.StringIO" }, { "name": "StringIO.StringIO.close", "domId": "StringIO_StringIO.StringIO.close" }, { "name": "StringIO.StringIO.getvalue", "domId": "StringIO_StringIO.StringIO.getvalue" } ] }, { "url": "http://docs.python.org/library/stdtypes.html", "title": "", "html": "
\n

5. Built-in Types

\n

The following sections describe the standard types that are built into the\ninterpreter.

\n
\n

Note

\n

Historically (until release 2.2), Python’s built-in types have differed from\nuser-defined types because it was not possible to use the built-in types as the\nbasis for object-oriented inheritance. This limitation no longer\nexists.

\n
\n

The principal built-in types are numerics, sequences, mappings, files, classes,\ninstances and exceptions.

\n

Some operations are supported by several object types; in particular,\npractically all objects can be compared, tested for truth value, and converted\nto a string (with the repr() function or the slightly different\nstr() function). The latter function is implicitly used when an object is\nwritten by the print() function.

\n
\n

5.1. Truth Value Testing

\n

Any object can be tested for truth value, for use in an if or\nwhile condition or as operand of the Boolean operations below. The\nfollowing values are considered false:

\n
\n
\n\n

All other values are considered true — so objects of many types are always\ntrue.

\n

Operations and built-in functions that have a Boolean result always return 0\nor False for false and 1 or True for true, unless otherwise stated.\n(Important exception: the Boolean operations or and and always return\none of their operands.)

\n
\n
\n

5.2. Boolean Operations — and, or, not

\n

These are the Boolean operations, ordered by ascending priority:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResultNotes
x or yif x is false, then y, else\nx(1)
x and yif x is false, then x, else\ny(2)
not xif x is false, then True,\nelse False(3)
\n

Notes:

\n
    \n
  1. This is a short-circuit operator, so it only evaluates the second\nargument if the first one is False.
  2. \n
  3. This is a short-circuit operator, so it only evaluates the second\nargument if the first one is True.
  4. \n
  5. not has a lower priority than non-Boolean operators, so not a == b is\ninterpreted as not (a == b), and a == not b is a syntax error.
  6. \n
\n
\n
\n

5.3. Comparisons

\n

Comparison operations are supported by all objects. They all have the same\npriority (which is higher than that of the Boolean operations). Comparisons can\nbe chained arbitrarily; for example, x < y <= z is equivalent to x < y and\ny <= z, except that y is evaluated only once (but in both cases z is not\nevaluated at all when x < y is found to be false).

\n

This table summarizes the comparison operations:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationMeaningNotes
<strictly less than 
<=less than or equal 
>strictly greater than 
>=greater than or equal 
==equal 
!=not equal(1)
isobject identity 
is notnegated object identity 
\n

Notes:

\n
    \n
  1. != can also be written <>, but this is an obsolete usage\nkept for backwards compatibility only. New code should always use\n!=.
  2. \n
\n

Objects of different types, except different numeric types and different string\ntypes, never compare equal; such objects are ordered consistently but\narbitrarily (so that sorting a heterogeneous array yields a consistent result).\nFurthermore, some types (for example, file objects) support only a degenerate\nnotion of comparison where any two objects of that type are unequal. Again,\nsuch objects are ordered arbitrarily but consistently. The <, <=, >\nand >= operators will raise a TypeError exception when any operand is\na complex number.

\n

Instances of a class normally compare as non-equal unless the class defines the\n__cmp__() method. Refer to Basic customization) for information on the\nuse of this method to effect object comparisons.

\n
\n

CPython implementation detail: Objects of different types except numbers are ordered by their type names;\nobjects of the same types that don’t support proper comparison are ordered by\ntheir address.

\n
\n

Two more operations with the same syntactic priority, in and not in, are\nsupported only by sequence types (below).

\n
\n
\n

5.4. Numeric Types — int, float, long, complex

\n

There are four distinct numeric types: plain integers, long\nintegers, floating point numbers, and complex numbers. In\naddition, Booleans are a subtype of plain integers. Plain integers (also just\ncalled integers) are implemented using long in C, which gives\nthem at least 32 bits of precision (sys.maxint is always set to the maximum\nplain integer value for the current platform, the minimum value is\n-sys.maxint - 1). Long integers have unlimited precision. Floating point\nnumbers are usually implemented using double in C; information about\nthe precision and internal representation of floating point numbers for the\nmachine on which your program is running is available in\nsys.float_info. Complex numbers have a real and imaginary part, which\nare each a floating point number. To extract these parts from a complex number\nz, use z.real and z.imag. (The standard library includes additional\nnumeric types, fractions that hold rationals, and decimal that\nhold floating-point numbers with user-definable precision.)

\n

Numbers are created by numeric literals or as the result of built-in functions\nand operators. Unadorned integer literals (including binary, hex, and octal\nnumbers) yield plain integers unless the value they denote is too large to be\nrepresented as a plain integer, in which case they yield a long integer.\nInteger literals with an 'L' or 'l' suffix yield long integers ('L'\nis preferred because 1l looks too much like eleven!). Numeric literals\ncontaining a decimal point or an exponent sign yield floating point numbers.\nAppending 'j' or 'J' to a numeric literal yields a complex number with a\nzero real part. A complex numeric literal is the sum of a real and an imaginary\npart.

\n

Python fully supports mixed arithmetic: when a binary arithmetic operator has\noperands of different numeric types, the operand with the “narrower” type is\nwidened to that of the other, where plain integer is narrower than long integer\nis narrower than floating point is narrower than complex. Comparisons between\nnumbers of mixed type use the same rule. [2] The constructors int(),\nlong(), float(), and complex() can be used to produce numbers\nof a specific type.

\n

All built-in numeric types support the following operations. See\nThe power operator and later sections for the operators’ priorities.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResultNotes
x + ysum of x and y 
x - ydifference of x and y 
x * yproduct of x and y 
x / yquotient of x and y(1)
x // y(floored) quotient of x and\ny(4)(5)
x yremainder of x / y(4)
-xx negated 
+xx unchanged 
abs(x)absolute value or magnitude of\nx(3)
int(x)x converted to integer(2)
long(x)x converted to long integer(2)
float(x)x converted to floating point(6)
complex(re,im)a complex number with real part\nre, imaginary part im.\nim defaults to zero. 
c.conjugate()conjugate of the complex number\nc. (Identity on real numbers) 
divmod(x, y)the pair (x // y, x y)(3)(4)
pow(x, y)x to the power y(3)(7)
x ** yx to the power y(7)
\n

Notes:

\n
    \n
  1. For (plain or long) integer division, the result is an integer. The result is\nalways rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and\n(-1)/(-2) is 0. Note that the result is a long integer if either operand is a\nlong integer, regardless of the numeric value.

    \n
  2. \n
  3. Conversion from floats using int() or long() truncates toward\nzero like the related function, math.trunc(). Use the function\nmath.floor() to round downward and math.ceil() to round\nupward.

    \n
  4. \n
  5. See Built-in Functions for a full description.

    \n
  6. \n
  7. \nDeprecated since version 2.3: The floor division operator, the modulo operator, and the divmod()\nfunction are no longer defined for complex numbers. Instead, convert to\na floating point number using the abs() function if appropriate.

    \n
  8. \n
  9. Also referred to as integer division. The resultant value is a whole integer,\nthough the result’s type is not necessarily int.

    \n
  10. \n
  11. float also accepts the strings “nan” and “inf” with an optional prefix “+”\nor “-” for Not a Number (NaN) and positive or negative infinity.

    \n

    \nNew in version 2.6.

    \n
  12. \n
  13. Python defines pow(0, 0) and 0 ** 0 to be 1, as is common for\nprogramming languages.

    \n
  14. \n
\n

All numbers.Real types (int, long, and\nfloat) also include the following operations:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResultNotes
math.trunc(x)x truncated to Integral 
round(x[, n])x rounded to n digits,\nrounding half to even. If n is\nomitted, it defaults to 0. 
math.floor(x)the greatest integral float <= x 
math.ceil(x)the least integral float >= x 
\n
\n

5.4.1. Bit-string Operations on Integer Types

\n

Plain and long integer types support additional operations that make sense only\nfor bit-strings. Negative numbers are treated as their 2’s complement value\n(for long integers, this assumes a sufficiently large number of bits that no\noverflow occurs during the operation).

\n

The priorities of the binary bitwise operations are all lower than the numeric\noperations and higher than the comparisons; the unary operation ~ has the\nsame priority as the other unary numeric operations (+ and -).

\n

This table lists the bit-string operations sorted in ascending priority:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResultNotes
x | ybitwise or of x and\ny 
x ^ ybitwise exclusive or of\nx and y 
x & ybitwise and of x and\ny 
x << nx shifted left by n bits(1)(2)
x >> nx shifted right by n bits(1)(3)
~xthe bits of x inverted 
\n

Notes:

\n
    \n
  1. Negative shift counts are illegal and cause a ValueError to be raised.
  2. \n
  3. A left shift by n bits is equivalent to multiplication by pow(2, n). A\nlong integer is returned if the result exceeds the range of plain integers.
  4. \n
  5. A right shift by n bits is equivalent to division by pow(2, n).
  6. \n
\n
\n
\n

5.4.2. Additional Methods on Integer Types

\n

The integer types implement the numbers.Integral abstract base\nclass. In addition, they provide one more method:

\n
\n
\nint.bit_length()
\n
\n\n
\n
\nlong.bit_length()
\n

Return the number of bits necessary to represent an integer in binary,\nexcluding the sign and leading zeros:

\n
>>> n = -37\n>>> bin(n)\n'-0b100101'\n>>> n.bit_length()\n6\n
\n
\n

More precisely, if x is nonzero, then x.bit_length() is the\nunique positive integer k such that 2**(k-1) <= abs(x) < 2**k.\nEquivalently, when abs(x) is small enough to have a correctly\nrounded logarithm, then k = 1 + int(log(abs(x), 2)).\nIf x is zero, then x.bit_length() returns 0.

\n

Equivalent to:

\n
def bit_length(self):\n    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'\n    s = s.lstrip('-0b') # remove leading zeros and minus sign\n    return len(s)       # len('100101') --> 6\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\n

5.4.3. Additional Methods on Float

\n

The float type implements the numbers.Real abstract base\nclass. float also has the following additional methods.

\n
\n
\nfloat.as_integer_ratio()
\n

Return a pair of integers whose ratio is exactly equal to the\noriginal float and with a positive denominator. Raises\nOverflowError on infinities and a ValueError on\nNaNs.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nfloat.is_integer()
\n

Return True if the float instance is finite with integral\nvalue, and False otherwise:

\n
>>> (-2.0).is_integer()\nTrue\n>>> (3.2).is_integer()\nFalse\n
\n
\n

\nNew in version 2.6.

\n
\n\n

Two methods support conversion to\nand from hexadecimal strings. Since Python’s floats are stored\ninternally as binary numbers, converting a float to or from a\ndecimal string usually involves a small rounding error. In\ncontrast, hexadecimal strings allow exact representation and\nspecification of floating-point numbers. This can be useful when\ndebugging, and in numerical work.

\n
\n
\nfloat.hex()
\n

Return a representation of a floating-point number as a hexadecimal\nstring. For finite floating-point numbers, this representation\nwill always include a leading 0x and a trailing p and\nexponent.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nfloat.fromhex(s)
\n

Class method to return the float represented by a hexadecimal\nstring s. The string s may have leading and trailing\nwhitespace.

\n

\nNew in version 2.6.

\n
\n\n

Note that float.hex() is an instance method, while\nfloat.fromhex() is a class method.

\n

A hexadecimal string takes the form:

\n
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
\n
\n

where the optional sign may by either + or -, integer\nand fraction are strings of hexadecimal digits, and exponent\nis a decimal integer with an optional leading sign. Case is not\nsignificant, and there must be at least one hexadecimal digit in\neither the integer or the fraction. This syntax is similar to the\nsyntax specified in section 6.4.4.2 of the C99 standard, and also to\nthe syntax used in Java 1.5 onwards. In particular, the output of\nfloat.hex() is usable as a hexadecimal floating-point literal in\nC or Java code, and hexadecimal strings produced by C’s %a format\ncharacter or Java’s Double.toHexString are accepted by\nfloat.fromhex().

\n

Note that the exponent is written in decimal rather than hexadecimal,\nand that it gives the power of 2 by which to multiply the coefficient.\nFor example, the hexadecimal string 0x3.a7p10 represents the\nfloating-point number (3 + 10./16 + 7./16**2) * 2.0**10, or\n3740.0:

\n
>>> float.fromhex('0x3.a7p10')\n3740.0\n
\n
\n

Applying the reverse conversion to 3740.0 gives a different\nhexadecimal string representing the same number:

\n
>>> float.hex(3740.0)\n'0x1.d380000000000p+11'\n
\n
\n
\n
\n
\n

5.5. Iterator Types

\n

\nNew in version 2.2.

\n

Python supports a concept of iteration over containers. This is implemented\nusing two distinct methods; these are used to allow user-defined classes to\nsupport iteration. Sequences, described below in more detail, always support\nthe iteration methods.

\n

One method needs to be defined for container objects to provide iteration\nsupport:

\n
\n
\ncontainer.__iter__()
\n
Return an iterator object. The object is required to support the iterator\nprotocol described below. If a container supports different types of\niteration, additional methods can be provided to specifically request\niterators for those iteration types. (An example of an object supporting\nmultiple forms of iteration would be a tree structure which supports both\nbreadth-first and depth-first traversal.) This method corresponds to the\ntp_iter slot of the type structure for Python objects in the Python/C\nAPI.
\n\n

The iterator objects themselves are required to support the following two\nmethods, which together form the iterator protocol:

\n
\n
\niterator.__iter__()
\n
Return the iterator object itself. This is required to allow both containers\nand iterators to be used with the for and in statements.\nThis method corresponds to the tp_iter slot of the type structure for\nPython objects in the Python/C API.
\n\n
\n
\niterator.next()
\n
Return the next item from the container. If there are no further items, raise\nthe StopIteration exception. This method corresponds to the\ntp_iternext slot of the type structure for Python objects in the\nPython/C API.
\n\n

Python defines several iterator objects to support iteration over general and\nspecific sequence types, dictionaries, and other more specialized forms. The\nspecific types are not important beyond their implementation of the iterator\nprotocol.

\n

The intention of the protocol is that once an iterator’s next() method\nraises StopIteration, it will continue to do so on subsequent calls.\nImplementations that do not obey this property are deemed broken. (This\nconstraint was added in Python 2.3; in Python 2.2, various iterators are broken\naccording to this rule.)

\n
\n

5.5.1. Generator Types

\n

Python’s generators provide a convenient way to implement the iterator\nprotocol. If a container object’s __iter__() method is implemented as a\ngenerator, it will automatically return an iterator object (technically, a\ngenerator object) supplying the __iter__() and next() methods. More\ninformation about generators can be found in the documentation for the\nyield expression.

\n
\n
\n
\n

5.6. Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange

\n

There are seven sequence types: strings, Unicode strings, lists, tuples,\nbytearrays, buffers, and xrange objects.

\n

For other containers see the built in dict and set classes,\nand the collections module.

\n

String literals are written in single or double quotes: 'xyzzy',\n"frobozz". See String literals for more about string literals.\nUnicode strings are much like strings, but are specified in the syntax\nusing a preceding 'u' character: u'abc', u"def". In addition\nto the functionality described here, there are also string-specific\nmethods described in the String Methods section. Lists are\nconstructed with square brackets, separating items with commas: [a, b, c].\nTuples are constructed by the comma operator (not within square\nbrackets), with or without enclosing parentheses, but an empty tuple\nmust have the enclosing parentheses, such as a, b, c or (). A\nsingle item tuple must have a trailing comma, such as (d,).

\n

Bytearray objects are created with the built-in function bytearray().

\n

Buffer objects are not directly supported by Python syntax, but can be created\nby calling the built-in function buffer(). They don’t support\nconcatenation or repetition.

\n

Objects of type xrange are similar to buffers in that there is no specific syntax to\ncreate them, but they are created using the xrange() function. They don’t\nsupport slicing, concatenation or repetition, and using in, not in,\nmin() or max() on them is inefficient.

\n

Most sequence types support the following operations. The in and not in\noperations have the same priorities as the comparison operations. The + and\n* operations have the same priority as the corresponding numeric operations.\n[3] Additional methods are provided for Mutable Sequence Types.

\n

This table lists the sequence operations sorted in ascending priority\n(operations in the same box have the same priority). In the table, s and t\nare sequences of the same type; n, i and j are integers:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResultNotes
x in sTrue if an item of s is\nequal to x, else False(1)
x not in sFalse if an item of s is\nequal to x, else True(1)
s + tthe concatenation of s and\nt(6)
s * n, n * sn shallow copies of s\nconcatenated(2)
s[i]i‘th item of s, origin 0(3)
s[i:j]slice of s from i to j(3)(4)
s[i:j:k]slice of s from i to j\nwith step k(3)(5)
len(s)length of s 
min(s)smallest item of s 
max(s)largest item of s 
s.index(i)index of the first occurence\nof i in s 
s.count(i)total number of occurences of\ni in s 
\n

Sequence types also support comparisons. In particular, tuples and lists\nare compared lexicographically by comparing corresponding\nelements. This means that to compare equal, every element must compare\nequal and the two sequences must be of the same type and have the same\nlength. (For full details see Comparisons in the language\nreference.)

\n

Notes:

\n
    \n
  1. When s is a string or Unicode string object the in and not in\noperations act like a substring test. In Python versions before 2.3, x had to\nbe a string of length 1. In Python 2.3 and beyond, x may be a string of any\nlength.

    \n
  2. \n
  3. Values of n less than 0 are treated as 0 (which yields an empty\nsequence of the same type as s). Note also that the copies are shallow;\nnested structures are not copied. This often haunts new Python programmers;\nconsider:

    \n
    >>> lists = [[]] * 3\n>>> lists\n[[], [], []]\n>>> lists[0].append(3)\n>>> lists\n[[3], [3], [3]]\n
    \n
    \n

    What has happened is that [[]] is a one-element list containing an empty\nlist, so all three elements of [[]] * 3 are (pointers to) this single empty\nlist. Modifying any of the elements of lists modifies this single list.\nYou can create a list of different lists this way:

    \n
    >>> lists = [[] for i in range(3)]\n>>> lists[0].append(3)\n>>> lists[1].append(5)\n>>> lists[2].append(7)\n>>> lists\n[[3], [5], [7]]\n
    \n
    \n
  4. \n
  5. If i or j is negative, the index is relative to the end of the string:\nlen(s) + i or len(s) + j is substituted. But note that -0 is still\n0.

    \n
  6. \n
  7. The slice of s from i to j is defined as the sequence of items with index\nk such that i <= k < j. If i or j is greater than len(s), use\nlen(s). If i is omitted or None, use 0. If j is omitted or\nNone, use len(s). If i is greater than or equal to j, the slice is\nempty.

    \n
  8. \n
  9. The slice of s from i to j with step k is defined as the sequence of\nitems with index x = i + n*k such that 0 <= n < (j-i)/k. In other words,\nthe indices are i, i+k, i+2*k, i+3*k and so on, stopping when\nj is reached (but never including j). If i or j is greater than\nlen(s), use len(s). If i or j are omitted or None, they become\n“end” values (which end depends on the sign of k). Note, k cannot be zero.\nIf k is None, it is treated like 1.

    \n
  10. \n
  11. \n

    CPython implementation detail: If s and t are both strings, some Python implementations such as\nCPython can usually perform an in-place optimization for assignments of\nthe form s = s + t or s += t. When applicable, this optimization\nmakes quadratic run-time much less likely. This optimization is both\nversion and implementation dependent. For performance sensitive code, it\nis preferable to use the str.join() method which assures consistent\nlinear concatenation performance across versions and implementations.

    \n
    \n

    \nChanged in version 2.4: Formerly, string concatenation never occurred in-place.

    \n
  12. \n
\n
\n

5.6.1. String Methods

\n

Below are listed the string methods which both 8-bit strings and\nUnicode objects support. Some of them are also available on bytearray\nobjects.

\n

In addition, Python’s strings support the sequence type methods\ndescribed in the Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange section. To output formatted strings\nuse template strings or the operator described in the\nString Formatting Operations section. Also, see the re module for\nstring functions based on regular expressions.

\n
\n
\nstr.capitalize()
\n

Return a copy of the string with its first character capitalized and the\nrest lowercased.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.center(width[, fillchar])
\n

Return centered in a string of length width. Padding is done using the\nspecified fillchar (default is a space).

\n

\nChanged in version 2.4: Support for the fillchar argument.

\n
\n\n
\n
\nstr.count(sub[, start[, end]])
\n
Return the number of non-overlapping occurrences of substring sub in the\nrange [start, end]. Optional arguments start and end are\ninterpreted as in slice notation.
\n\n
\n
\nstr.decode([encoding[, errors]])
\n

Decodes the string using the codec registered for encoding. encoding\ndefaults to the default string encoding. errors may be given to set a\ndifferent error handling scheme. The default is 'strict', meaning that\nencoding errors raise UnicodeError. Other possible values are\n'ignore', 'replace' and any other name registered via\ncodecs.register_error(), see section Codec Base Classes.

\n

\nNew in version 2.2.

\n

\nChanged in version 2.3: Support for other error handling schemes added.

\n

\nChanged in version 2.7: Support for keyword arguments added.

\n
\n\n
\n
\nstr.encode([encoding[, errors]])
\n

Return an encoded version of the string. Default encoding is the current\ndefault string encoding. errors may be given to set a different error\nhandling scheme. The default for errors is 'strict', meaning that\nencoding errors raise a UnicodeError. Other possible values are\n'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and\nany other name registered via codecs.register_error(), see section\nCodec Base Classes. For a list of possible encodings, see section\nStandard Encodings.

\n

\nNew in version 2.0.

\n

\nChanged in version 2.3: Support for 'xmlcharrefreplace' and 'backslashreplace' and other error\nhandling schemes added.

\n

\nChanged in version 2.7: Support for keyword arguments added.

\n
\n\n
\n
\nstr.endswith(suffix[, start[, end]])
\n

Return True if the string ends with the specified suffix, otherwise return\nFalse. suffix can also be a tuple of suffixes to look for. With optional\nstart, test beginning at that position. With optional end, stop comparing\nat that position.

\n

\nChanged in version 2.5: Accept tuples as suffix.

\n
\n\n
\n
\nstr.expandtabs([tabsize])
\n
Return a copy of the string where all tab characters are replaced by one or\nmore spaces, depending on the current column and the given tab size. The\ncolumn number is reset to zero after each newline occurring in the string.\nIf tabsize is not given, a tab size of 8 characters is assumed. This\ndoesn’t understand other non-printing characters or escape sequences.
\n\n
\n
\nstr.find(sub[, start[, end]])
\n

Return the lowest index in the string where substring sub is found, such\nthat sub is contained in the slice s[start:end]. Optional arguments\nstart and end are interpreted as in slice notation. Return -1 if\nsub is not found.

\n
\n

Note

\n

The find() method should be used only if you need to know the\nposition of sub. To check if sub is a substring or not, use the\nin operator:

\n
>>> 'Py' in 'Python'\nTrue\n
\n
\n
\n
\n\n
\n
\nstr.format(*args, **kwargs)
\n

Perform a string formatting operation. The string on which this method is\ncalled can contain literal text or replacement fields delimited by braces\n{}. Each replacement field contains either the numeric index of a\npositional argument, or the name of a keyword argument. Returns a copy of\nthe string where each replacement field is replaced with the string value of\nthe corresponding argument.

\n
>>> "The sum of 1 + 2 is {0}".format(1+2)\n'The sum of 1 + 2 is 3'\n
\n
\n

See Format String Syntax for a description of the various formatting options\nthat can be specified in format strings.

\n

This method of string formatting is the new standard in Python 3.0, and\nshould be preferred to the formatting described in\nString Formatting Operations in new code.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nstr.index(sub[, start[, end]])
\n
Like find(), but raise ValueError when the substring is not found.
\n\n
\n
\nstr.isalnum()
\n

Return true if all characters in the string are alphanumeric and there is at\nleast one character, false otherwise.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.isalpha()
\n

Return true if all characters in the string are alphabetic and there is at least\none character, false otherwise.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.isdigit()
\n

Return true if all characters in the string are digits and there is at least one\ncharacter, false otherwise.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.islower()
\n

Return true if all cased characters [4] in the string are lowercase and there is at\nleast one cased character, false otherwise.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.isspace()
\n

Return true if there are only whitespace characters in the string and there is\nat least one character, false otherwise.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.istitle()
\n

Return true if the string is a titlecased string and there is at least one\ncharacter, for example uppercase characters may only follow uncased characters\nand lowercase characters only cased ones. Return false otherwise.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.isupper()
\n

Return true if all cased characters [4] in the string are uppercase and there is at\nleast one cased character, false otherwise.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.join(iterable)
\n
Return a string which is the concatenation of the strings in the\niterable iterable. The separator between elements is the string\nproviding this method.
\n\n
\n
\nstr.ljust(width[, fillchar])
\n

Return the string left justified in a string of length width. Padding is done\nusing the specified fillchar (default is a space). The original string is\nreturned if width is less than or equal to len(s).

\n

\nChanged in version 2.4: Support for the fillchar argument.

\n
\n\n
\n
\nstr.lower()
\n

Return a copy of the string with all the cased characters [4] converted to\nlowercase.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.lstrip([chars])
\n

Return a copy of the string with leading characters removed. The chars\nargument is a string specifying the set of characters to be removed. If omitted\nor None, the chars argument defaults to removing whitespace. The chars\nargument is not a prefix; rather, all combinations of its values are stripped:

\n
>>> '   spacious   '.lstrip()\n'spacious   '\n>>> 'www.example.com'.lstrip('cmowz.')\n'example.com'\n
\n
\n

\nChanged in version 2.2.2: Support for the chars argument.

\n
\n\n
\n
\nstr.partition(sep)
\n

Split the string at the first occurrence of sep, and return a 3-tuple\ncontaining the part before the separator, the separator itself, and the part\nafter the separator. If the separator is not found, return a 3-tuple containing\nthe string itself, followed by two empty strings.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nstr.replace(old, new[, count])
\n
Return a copy of the string with all occurrences of substring old replaced by\nnew. If the optional argument count is given, only the first count\noccurrences are replaced.
\n\n
\n
\nstr.rfind(sub[, start[, end]])
\n
Return the highest index in the string where substring sub is found, such\nthat sub is contained within s[start:end]. Optional arguments start\nand end are interpreted as in slice notation. Return -1 on failure.
\n\n
\n
\nstr.rindex(sub[, start[, end]])
\n
Like rfind() but raises ValueError when the substring sub is not\nfound.
\n\n
\n
\nstr.rjust(width[, fillchar])
\n

Return the string right justified in a string of length width. Padding is done\nusing the specified fillchar (default is a space). The original string is\nreturned if width is less than or equal to len(s).

\n

\nChanged in version 2.4: Support for the fillchar argument.

\n
\n\n
\n
\nstr.rpartition(sep)
\n

Split the string at the last occurrence of sep, and return a 3-tuple\ncontaining the part before the separator, the separator itself, and the part\nafter the separator. If the separator is not found, return a 3-tuple containing\ntwo empty strings, followed by the string itself.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nstr.rsplit([sep[, maxsplit]])
\n

Return a list of the words in the string, using sep as the delimiter string.\nIf maxsplit is given, at most maxsplit splits are done, the rightmost\nones. If sep is not specified or None, any whitespace string is a\nseparator. Except for splitting from the right, rsplit() behaves like\nsplit() which is described in detail below.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nstr.rstrip([chars])
\n

Return a copy of the string with trailing characters removed. The chars\nargument is a string specifying the set of characters to be removed. If omitted\nor None, the chars argument defaults to removing whitespace. The chars\nargument is not a suffix; rather, all combinations of its values are stripped:

\n
>>> '   spacious   '.rstrip()\n'   spacious'\n>>> 'mississippi'.rstrip('ipz')\n'mississ'\n
\n
\n

\nChanged in version 2.2.2: Support for the chars argument.

\n
\n\n
\n
\nstr.split([sep[, maxsplit]])
\n

Return a list of the words in the string, using sep as the delimiter\nstring. If maxsplit is given, at most maxsplit splits are done (thus,\nthe list will have at most maxsplit+1 elements). If maxsplit is not\nspecified, then there is no limit on the number of splits (all possible\nsplits are made).

\n

If sep is given, consecutive delimiters are not grouped together and are\ndeemed to delimit empty strings (for example, '1,,2'.split(',') returns\n['1', '', '2']). The sep argument may consist of multiple characters\n(for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']).\nSplitting an empty string with a specified separator returns [''].

\n

If sep is not specified or is None, a different splitting algorithm is\napplied: runs of consecutive whitespace are regarded as a single separator,\nand the result will contain no empty strings at the start or end if the\nstring has leading or trailing whitespace. Consequently, splitting an empty\nstring or a string consisting of just whitespace with a None separator\nreturns [].

\n

For example, ' 1  2   3  '.split() returns ['1', '2', '3'], and\n'  1  2   3  '.split(None, 1) returns ['1', '2   3  '].

\n
\n\n
\n
\nstr.splitlines([keepends])
\n
Return a list of the lines in the string, breaking at line boundaries. Line\nbreaks are not included in the resulting list unless keepends is given and\ntrue.
\n\n
\n
\nstr.startswith(prefix[, start[, end]])
\n

Return True if string starts with the prefix, otherwise return False.\nprefix can also be a tuple of prefixes to look for. With optional start,\ntest string beginning at that position. With optional end, stop comparing\nstring at that position.

\n

\nChanged in version 2.5: Accept tuples as prefix.

\n
\n\n
\n
\nstr.strip([chars])
\n

Return a copy of the string with the leading and trailing characters removed.\nThe chars argument is a string specifying the set of characters to be removed.\nIf omitted or None, the chars argument defaults to removing whitespace.\nThe chars argument is not a prefix or suffix; rather, all combinations of its\nvalues are stripped:

\n
>>> '   spacious   '.strip()\n'spacious'\n>>> 'www.example.com'.strip('cmowz.')\n'example'\n
\n
\n

\nChanged in version 2.2.2: Support for the chars argument.

\n
\n\n
\n
\nstr.swapcase()
\n

Return a copy of the string with uppercase characters converted to lowercase and\nvice versa.

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.title()
\n

Return a titlecased version of the string where words start with an uppercase\ncharacter and the remaining characters are lowercase.

\n

The algorithm uses a simple language-independent definition of a word as\ngroups of consecutive letters. The definition works in many contexts but\nit means that apostrophes in contractions and possessives form word\nboundaries, which may not be the desired result:

\n
>>> "they're bill's friends from the UK".title()\n"They'Re Bill'S Friends From The Uk"\n
\n
\n

A workaround for apostrophes can be constructed using regular expressions:

\n
>>> import re\n>>> def titlecase(s):\n        return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",\n                      lambda mo: mo.group(0)[0].upper() +\n                                 mo.group(0)[1:].lower(),\n                      s)\n\n>>> titlecase("they're bill's friends.")\n"They're Bill's Friends."\n
\n
\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.translate(table[, deletechars])
\n

Return a copy of the string where all characters occurring in the optional\nargument deletechars are removed, and the remaining characters have been\nmapped through the given translation table, which must be a string of length\n256.

\n

You can use the maketrans() helper function in the string\nmodule to create a translation table. For string objects, set the table\nargument to None for translations that only delete characters:

\n
>>> 'read this short text'.translate(None, 'aeiou')\n'rd ths shrt txt'\n
\n
\n

\nNew in version 2.6: Support for a None table argument.

\n

For Unicode objects, the translate() method does not accept the optional\ndeletechars argument. Instead, it returns a copy of the s where all\ncharacters have been mapped through the given translation table which must be a\nmapping of Unicode ordinals to Unicode ordinals, Unicode strings or None.\nUnmapped characters are left untouched. Characters mapped to None are\ndeleted. Note, a more flexible approach is to create a custom character mapping\ncodec using the codecs module (see encodings.cp1251 for an\nexample).

\n
\n\n
\n
\nstr.upper()
\n

Return a copy of the string with all the cased characters [4] converted to\nuppercase. Note that str.upper().isupper() might be False if s\ncontains uncased characters or if the Unicode category of the resulting\ncharacter(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase).

\n

For 8-bit strings, this method is locale-dependent.

\n
\n\n
\n
\nstr.zfill(width)
\n

Return the numeric string left filled with zeros in a string of length\nwidth. A sign prefix is handled correctly. The original string is\nreturned if width is less than or equal to len(s).

\n

\nNew in version 2.2.2.

\n
\n\n

The following methods are present only on unicode objects:

\n
\n
\nunicode.isnumeric()
\n
Return True if there are only numeric characters in S, False\notherwise. Numeric characters include digit characters, and all characters\nthat have the Unicode numeric value property, e.g. U+2155,\nVULGAR FRACTION ONE FIFTH.
\n\n
\n
\nunicode.isdecimal()
\n
Return True if there are only decimal characters in S, False\notherwise. Decimal characters include digit characters, and all characters\nthat can be used to form decimal-radix numbers, e.g. U+0660,\nARABIC-INDIC DIGIT ZERO.
\n\n
\n
\n

5.6.2. String Formatting Operations

\n

String and Unicode objects have one unique built-in operation: the \noperator (modulo). This is also known as the string formatting or\ninterpolation operator. Given format values (where format is a string\nor Unicode object), conversion specifications in format are replaced\nwith zero or more elements of values. The effect is similar to the using\nsprintf() in the C language. If format is a Unicode object, or if any\nof the objects being converted using the %s conversion are Unicode objects,\nthe result will also be a Unicode object.

\n

If format requires a single argument, values may be a single non-tuple\nobject. [5] Otherwise, values must be a tuple with exactly the number of\nitems specified by the format string, or a single mapping object (for example, a\ndictionary).

\n

A conversion specifier contains two or more characters and has the following\ncomponents, which must occur in this order:

\n
    \n
  1. The '%' character, which marks the start of the specifier.
  2. \n
  3. Mapping key (optional), consisting of a parenthesised sequence of characters\n(for example, (somename)).
  4. \n
  5. Conversion flags (optional), which affect the result of some conversion\ntypes.
  6. \n
  7. Minimum field width (optional). If specified as an '*' (asterisk), the\nactual width is read from the next element of the tuple in values, and the\nobject to convert comes after the minimum field width and optional precision.
  8. \n
  9. Precision (optional), given as a '.' (dot) followed by the precision. If\nspecified as '*' (an asterisk), the actual width is read from the next\nelement of the tuple in values, and the value to convert comes after the\nprecision.
  10. \n
  11. Length modifier (optional).
  12. \n
  13. Conversion type.
  14. \n
\n

When the right argument is a dictionary (or other mapping type), then the\nformats in the string must include a parenthesised mapping key into that\ndictionary inserted immediately after the '%' character. The mapping key\nselects the value to be formatted from the mapping. For example:

\n
>>> print '%(language)s has %(number)03d quote types.'  \\\n...       {"language": "Python", "number": 2}\nPython has 002 quote types.\n
\n
\n

In this case no * specifiers may occur in a format (since they require a\nsequential parameter list).

\n

The conversion flag characters are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FlagMeaning
'#'The value conversion will use the “alternate form” (where defined\nbelow).
'0'The conversion will be zero padded for numeric values.
'-'The converted value is left adjusted (overrides the '0'\nconversion if both are given).
' '(a space) A blank should be left before a positive number (or empty\nstring) produced by a signed conversion.
'+'A sign character ('+' or '-') will precede the conversion\n(overrides a “space” flag).
\n

A length modifier (h, l, or L) may be present, but is ignored as it\nis not necessary for Python – so e.g. %ld is identical to %d.

\n

The conversion types are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConversionMeaningNotes
'd'Signed integer decimal. 
'i'Signed integer decimal. 
'o'Signed octal value.(1)
'u'Obsolete type – it is identical to 'd'.(7)
'x'Signed hexadecimal (lowercase).(2)
'X'Signed hexadecimal (uppercase).(2)
'e'Floating point exponential format (lowercase).(3)
'E'Floating point exponential format (uppercase).(3)
'f'Floating point decimal format.(3)
'F'Floating point decimal format.(3)
'g'Floating point format. Uses lowercase exponential\nformat if exponent is less than -4 or not less than\nprecision, decimal format otherwise.(4)
'G'Floating point format. Uses uppercase exponential\nformat if exponent is less than -4 or not less than\nprecision, decimal format otherwise.(4)
'c'Single character (accepts integer or single\ncharacter string). 
'r'String (converts any Python object using\nrepr()).(5)
's'String (converts any Python object using\nstr()).(6)
'%'No argument is converted, results in a '%'\ncharacter in the result. 
\n

Notes:

\n
    \n
  1. The alternate form causes a leading zero ('0') to be inserted between\nleft-hand padding and the formatting of the number if the leading character\nof the result is not already a zero.

    \n
  2. \n
  3. The alternate form causes a leading '0x' or '0X' (depending on whether\nthe 'x' or 'X' format was used) to be inserted between left-hand padding\nand the formatting of the number if the leading character of the result is not\nalready a zero.

    \n
  4. \n
  5. The alternate form causes the result to always contain a decimal point, even if\nno digits follow it.

    \n

    The precision determines the number of digits after the decimal point and\ndefaults to 6.

    \n
  6. \n
  7. The alternate form causes the result to always contain a decimal point, and\ntrailing zeroes are not removed as they would otherwise be.

    \n

    The precision determines the number of significant digits before and after the\ndecimal point and defaults to 6.

    \n
  8. \n
  9. The %r conversion was added in Python 2.0.

    \n

    The precision determines the maximal number of characters used.

    \n
  10. \n
  11. If the object or format provided is a unicode string, the resulting\nstring will also be unicode.

    \n

    The precision determines the maximal number of characters used.

    \n
  12. \n
  13. See PEP 237.

    \n
  14. \n
\n

Since Python strings have an explicit length, %s conversions do not assume\nthat '\\0' is the end of the string.

\n

\nChanged in version 2.7: %f conversions for numbers whose absolute value is over 1e50 are no\nlonger replaced by %g conversions.

\n

Additional string operations are defined in standard modules string and\nre.

\n
\n
\n

5.6.3. XRange Type

\n

The xrange type is an immutable sequence which is commonly used for\nlooping. The advantage of the xrange type is that an xrange\nobject will always take the same amount of memory, no matter the size of the\nrange it represents. There are no consistent performance advantages.

\n

XRange objects have very little behavior: they only support indexing, iteration,\nand the len() function.

\n
\n
\n

5.6.4. Mutable Sequence Types

\n

List and bytearray objects support additional operations that allow\nin-place modification of the object. Other mutable sequence types (when added\nto the language) should also support these operations. Strings and tuples\nare immutable sequence types: such objects cannot be modified once created.\nThe following operations are defined on mutable sequence types (where x is\nan arbitrary object):

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResultNotes
s[i] = xitem i of s is replaced by\nx 
s[i:j] = tslice of s from i to j\nis replaced by the contents of\nthe iterable t 
del s[i:j]same as s[i:j] = [] 
s[i:j:k] = tthe elements of s[i:j:k]\nare replaced by those of t(1)
del s[i:j:k]removes the elements of\ns[i:j:k] from the list 
s.append(x)same as s[len(s):len(s)] =\n[x](2)
s.extend(x)same as s[len(s):len(s)] =\nx(3)
s.count(x)return number of i‘s for\nwhich s[i] == x 
s.index(x[, i[, j]])return smallest k such that\ns[k] == x and i <= k <\nj(4)
s.insert(i, x)same as s[i:i] = [x](5)
s.pop([i])same as x = s[i]; del s[i];\nreturn x(6)
s.remove(x)same as del s[s.index(x)](4)
s.reverse()reverses the items of s in\nplace(7)
s.sort([cmp[, key[,\nreverse]]])sort the items of s in place(7)(8)(9)(10)
\n

Notes:

\n
    \n
  1. t must have the same length as the slice it is replacing.

    \n
  2. \n
  3. The C implementation of Python has historically accepted multiple parameters and\nimplicitly joined them into a tuple; this no longer works in Python 2.0. Use of\nthis misfeature has been deprecated since Python 1.4.

    \n
  4. \n
  5. x can be any iterable object.

    \n
  6. \n
  7. Raises ValueError when x is not found in s. When a negative index is\npassed as the second or third parameter to the index() method, the list\nlength is added, as for slice indices. If it is still negative, it is truncated\nto zero, as for slice indices.

    \n

    \nChanged in version 2.3: Previously, index() didn’t have arguments for specifying start and stop\npositions.

    \n
  8. \n
  9. When a negative index is passed as the first parameter to the insert()\nmethod, the list length is added, as for slice indices. If it is still\nnegative, it is truncated to zero, as for slice indices.

    \n

    \nChanged in version 2.3: Previously, all negative indices were truncated to zero.

    \n
  10. \n
  11. The pop() method is only supported by the list and array types. The\noptional argument i defaults to -1, so that by default the last item is\nremoved and returned.

    \n
  12. \n
  13. The sort() and reverse() methods modify the list in place for\neconomy of space when sorting or reversing a large list. To remind you that\nthey operate by side effect, they don’t return the sorted or reversed list.

    \n
  14. \n
  15. The sort() method takes optional arguments for controlling the\ncomparisons.

    \n

    cmp specifies a custom comparison function of two arguments (list items) which\nshould return a negative, zero or positive number depending on whether the first\nargument is considered smaller than, equal to, or larger than the second\nargument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value\nis None.

    \n

    key specifies a function of one argument that is used to extract a comparison\nkey from each list element: key=str.lower. The default value is None.

    \n

    reverse is a boolean value. If set to True, then the list elements are\nsorted as if each comparison were reversed.

    \n

    In general, the key and reverse conversion processes are much faster than\nspecifying an equivalent cmp function. This is because cmp is called\nmultiple times for each list element while key and reverse touch each\nelement only once. Use functools.cmp_to_key() to convert an\nold-style cmp function to a key function.

    \n

    \nChanged in version 2.3: Support for None as an equivalent to omitting cmp was added.

    \n

    \nChanged in version 2.4: Support for key and reverse was added.

    \n
  16. \n
  17. Starting with Python 2.3, the sort() method is guaranteed to be stable. A\nsort is stable if it guarantees not to change the relative order of elements\nthat compare equal — this is helpful for sorting in multiple passes (for\nexample, sort by department, then by salary grade).

    \n
  18. \n
  19. \n

    CPython implementation detail: While a list is being sorted, the effect of attempting to mutate, or even\ninspect, the list is undefined. The C implementation of Python 2.3 and\nnewer makes the list appear empty for the duration, and raises\nValueError if it can detect that the list has been mutated during a\nsort.

    \n
    \n
  20. \n
\n
\n
\n
\n

5.7. Set Types — set, frozenset

\n

A set object is an unordered collection of distinct hashable objects.\nCommon uses include membership testing, removing duplicates from a sequence, and\ncomputing mathematical operations such as intersection, union, difference, and\nsymmetric difference.\n(For other containers see the built in dict, list,\nand tuple classes, and the collections module.)

\n

\nNew in version 2.4.

\n

Like other collections, sets support x in set, len(set), and for x in\nset. Being an unordered collection, sets do not record element position or\norder of insertion. Accordingly, sets do not support indexing, slicing, or\nother sequence-like behavior.

\n

There are currently two built-in set types, set and frozenset.\nThe set type is mutable — the contents can be changed using methods\nlike add() and remove(). Since it is mutable, it has no hash value\nand cannot be used as either a dictionary key or as an element of another set.\nThe frozenset type is immutable and hashable — its contents\ncannot be altered after it is created; it can therefore be used as a dictionary\nkey or as an element of another set.

\n

As of Python 2.7, non-empty sets (not frozensets) can be created by placing a\ncomma-separated list of elements within braces, for example: {'jack',\n'sjoerd'}, in addition to the set constructor.

\n

The constructors for both classes work the same:

\n
\n
\nclass set([iterable])
\n
\nclass frozenset([iterable])
\n

Return a new set or frozenset object whose elements are taken from\niterable. The elements of a set must be hashable. To represent sets of\nsets, the inner sets must be frozenset objects. If iterable is\nnot specified, a new empty set is returned.

\n

Instances of set and frozenset provide the following\noperations:

\n
\n
\nlen(s)
\n
Return the cardinality of set s.
\n\n
\n
\nx in s
\n
Test x for membership in s.
\n\n
\n
\nx not in s
\n
Test x for non-membership in s.
\n\n
\n
\nisdisjoint(other)
\n

Return True if the set has no elements in common with other. Sets are\ndisjoint if and only if their intersection is the empty set.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nissubset(other)
\n
\nset <= other
\n
Test whether every element in the set is in other.
\n\n
\n
\nset < other
\n
Test whether the set is a true subset of other, that is,\nset <= other and set != other.
\n\n
\n
\nissuperset(other)
\n
\nset >= other
\n
Test whether every element in other is in the set.
\n\n
\n
\nset > other
\n
Test whether the set is a true superset of other, that is, set >=\nother and set != other.
\n\n
\n
\nunion(other, ...)
\n
\nset | other | ...
\n

Return a new set with elements from the set and all others.

\n

\nChanged in version 2.6: Accepts multiple input iterables.

\n
\n\n
\n
\nintersection(other, ...)
\n
\nset & other & ...
\n

Return a new set with elements common to the set and all others.

\n

\nChanged in version 2.6: Accepts multiple input iterables.

\n
\n\n
\n
\ndifference(other, ...)
\n
\nset - other - ...
\n

Return a new set with elements in the set that are not in the others.

\n

\nChanged in version 2.6: Accepts multiple input iterables.

\n
\n\n
\n
\nsymmetric_difference(other)
\n
\nset ^ other
\n
Return a new set with elements in either the set or other but not both.
\n\n
\n
\ncopy()
\n
Return a new set with a shallow copy of s.
\n\n

Note, the non-operator versions of union(), intersection(),\ndifference(), and symmetric_difference(), issubset(), and\nissuperset() methods will accept any iterable as an argument. In\ncontrast, their operator based counterparts require their arguments to be\nsets. This precludes error-prone constructions like set('abc') & 'cbs'\nin favor of the more readable set('abc').intersection('cbs').

\n

Both set and frozenset support set to set comparisons. Two\nsets are equal if and only if every element of each set is contained in the\nother (each is a subset of the other). A set is less than another set if and\nonly if the first set is a proper subset of the second set (is a subset, but\nis not equal). A set is greater than another set if and only if the first set\nis a proper superset of the second set (is a superset, but is not equal).

\n

Instances of set are compared to instances of frozenset\nbased on their members. For example, set('abc') == frozenset('abc')\nreturns True and so does set('abc') in set([frozenset('abc')]).

\n

The subset and equality comparisons do not generalize to a complete ordering\nfunction. For example, any two disjoint sets are not equal and are not\nsubsets of each other, so all of the following return False: a<b,\na==b, or a>b. Accordingly, sets do not implement the __cmp__()\nmethod.

\n

Since sets only define partial ordering (subset relationships), the output of\nthe list.sort() method is undefined for lists of sets.

\n

Set elements, like dictionary keys, must be hashable.

\n

Binary operations that mix set instances with frozenset\nreturn the type of the first operand. For example: frozenset('ab') |\nset('bc') returns an instance of frozenset.

\n

The following table lists operations available for set that do not\napply to immutable instances of frozenset:

\n
\n
\nupdate(other, ...)
\n
\nset |= other | ...
\n

Update the set, adding elements from all others.

\n

\nChanged in version 2.6: Accepts multiple input iterables.

\n
\n\n
\n
\nintersection_update(other, ...)
\n
\nset &= other & ...
\n

Update the set, keeping only elements found in it and all others.

\n

\nChanged in version 2.6: Accepts multiple input iterables.

\n
\n\n
\n
\ndifference_update(other, ...)
\n
\nset -= other | ...
\n

Update the set, removing elements found in others.

\n

\nChanged in version 2.6: Accepts multiple input iterables.

\n
\n\n
\n
\nsymmetric_difference_update(other)
\n
\nset ^= other
\n
Update the set, keeping only elements found in either set, but not in both.
\n\n
\n
\nadd(elem)
\n
Add element elem to the set.
\n\n
\n
\nremove(elem)
\n
Remove element elem from the set. Raises KeyError if elem is\nnot contained in the set.
\n\n
\n
\ndiscard(elem)
\n
Remove element elem from the set if it is present.
\n\n
\n
\npop()
\n
Remove and return an arbitrary element from the set. Raises\nKeyError if the set is empty.
\n\n
\n
\nclear()
\n
Remove all elements from the set.
\n\n

Note, the non-operator versions of the update(),\nintersection_update(), difference_update(), and\nsymmetric_difference_update() methods will accept any iterable as an\nargument.

\n

Note, the elem argument to the __contains__(), remove(), and\ndiscard() methods may be a set. To support searching for an equivalent\nfrozenset, the elem set is temporarily mutated during the search and then\nrestored. During the search, the elem set should not be read or mutated\nsince it does not have a meaningful value.

\n
\n\n
\n

See also

\n
\n
Comparison to the built-in set types
\n
Differences between the sets module and the built-in set types.
\n
\n
\n
\n
\n

5.8. Mapping Types — dict

\n

A mapping object maps hashable values to arbitrary objects.\nMappings are mutable objects. There is currently only one standard mapping\ntype, the dictionary. (For other containers see the built in\nlist, set, and tuple classes, and the\ncollections module.)

\n

A dictionary’s keys are almost arbitrary values. Values that are not\nhashable, that is, values containing lists, dictionaries or other\nmutable types (that are compared by value rather than by object identity) may\nnot be used as keys. Numeric types used for keys obey the normal rules for\nnumeric comparison: if two numbers compare equal (such as 1 and 1.0)\nthen they can be used interchangeably to index the same dictionary entry. (Note\nhowever, that since computers store floating-point numbers as approximations it\nis usually unwise to use them as dictionary keys.)

\n

Dictionaries can be created by placing a comma-separated list of key: value\npairs within braces, for example: {'jack': 4098, 'sjoerd': 4127} or {4098:\n'jack', 4127: 'sjoerd'}, or by the dict constructor.

\n
\n
\nclass dict([arg])
\n

Return a new dictionary initialized from an optional positional argument or from\na set of keyword arguments. If no arguments are given, return a new empty\ndictionary. If the positional argument arg is a mapping object, return a\ndictionary mapping the same keys to the same values as does the mapping object.\nOtherwise the positional argument must be a sequence, a container that supports\niteration, or an iterator object. The elements of the argument must each also\nbe of one of those kinds, and each must in turn contain exactly two objects.\nThe first is used as a key in the new dictionary, and the second as the key’s\nvalue. If a given key is seen more than once, the last value associated with it\nis retained in the new dictionary.

\n

If keyword arguments are given, the keywords themselves with their associated\nvalues are added as items to the dictionary. If a key is specified both in the\npositional argument and as a keyword argument, the value associated with the\nkeyword is retained in the dictionary. For example, these all return a\ndictionary equal to {"one": 1, "two": 2}:

\n
    \n
  • dict(one=1, two=2)
  • \n
  • dict({'one': 1, 'two': 2})
  • \n
  • dict(zip(('one', 'two'), (1, 2)))
  • \n
  • dict([['two', 2], ['one', 1]])
  • \n
\n

The first example only works for keys that are valid Python\nidentifiers; the others work with any valid keys.

\n

\nNew in version 2.2.

\n

\nChanged in version 2.3: Support for building a dictionary from keyword arguments added.

\n

These are the operations that dictionaries support (and therefore, custom\nmapping types should support too):

\n
\n
\nlen(d)
\n
Return the number of items in the dictionary d.
\n\n
\n
\nd[key]
\n

Return the item of d with key key. Raises a KeyError if key\nis not in the map.

\n

\nNew in version 2.5: If a subclass of dict defines a method __missing__(), if the key\nkey is not present, the d[key] operation calls that method with\nthe key key as argument. The d[key] operation then returns or\nraises whatever is returned or raised by the __missing__(key) call\nif the key is not present. No other operations or methods invoke\n__missing__(). If __missing__() is not defined,\nKeyError is raised. __missing__() must be a method; it\ncannot be an instance variable. For an example, see\ncollections.defaultdict.

\n
\n\n
\n
\nd[key] = value
\n
Set d[key] to value.
\n\n
\n
\ndel d[key]
\n
Remove d[key] from d. Raises a KeyError if key is not in the\nmap.
\n\n
\n
\nkey in d
\n

Return True if d has a key key, else False.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nkey not in d
\n

Equivalent to not key in d.

\n

\nNew in version 2.2.

\n
\n\n
\n
\niter(d)
\n
Return an iterator over the keys of the dictionary. This is a shortcut\nfor iterkeys().
\n\n
\n
\nclear()
\n
Remove all items from the dictionary.
\n\n
\n
\ncopy()
\n
Return a shallow copy of the dictionary.
\n\n
\n
\nfromkeys(seq[, value])
\n

Create a new dictionary with keys from seq and values set to value.

\n

fromkeys() is a class method that returns a new dictionary. value\ndefaults to None.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nget(key[, default])
\n
Return the value for key if key is in the dictionary, else default.\nIf default is not given, it defaults to None, so that this method\nnever raises a KeyError.
\n\n
\n
\nhas_key(key)
\n
Test for the presence of key in the dictionary. has_key() is\ndeprecated in favor of key in d.
\n\n
\n
\nitems()
\n

Return a copy of the dictionary’s list of (key, value) pairs.

\n
\n

CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random,\nvaries across Python implementations, and depends on the dictionary’s\nhistory of insertions and deletions.

\n
\n

If items(), keys(), values(), iteritems(),\niterkeys(), and itervalues() are called with no intervening\nmodifications to the dictionary, the lists will directly correspond. This\nallows the creation of (value, key) pairs using zip(): pairs =\nzip(d.values(), d.keys()). The same relationship holds for the\niterkeys() and itervalues() methods: pairs =\nzip(d.itervalues(), d.iterkeys()) provides the same value for\npairs. Another way to create the same list is pairs = [(v, k) for\n(k, v) in d.iteritems()].

\n
\n\n
\n
\niteritems()
\n

Return an iterator over the dictionary’s (key, value) pairs. See the\nnote for dict.items().

\n

Using iteritems() while adding or deleting entries in the dictionary\nmay raise a RuntimeError or fail to iterate over all entries.

\n

\nNew in version 2.2.

\n
\n\n
\n
\niterkeys()
\n

Return an iterator over the dictionary’s keys. See the note for\ndict.items().

\n

Using iterkeys() while adding or deleting entries in the dictionary\nmay raise a RuntimeError or fail to iterate over all entries.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nitervalues()
\n

Return an iterator over the dictionary’s values. See the note for\ndict.items().

\n

Using itervalues() while adding or deleting entries in the\ndictionary may raise a RuntimeError or fail to iterate over all\nentries.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nkeys()
\n
Return a copy of the dictionary’s list of keys. See the note for\ndict.items().
\n\n
\n
\npop(key[, default])
\n

If key is in the dictionary, remove it and return its value, else return\ndefault. If default is not given and key is not in the dictionary,\na KeyError is raised.

\n

\nNew in version 2.3.

\n
\n\n
\n
\npopitem()
\n

Remove and return an arbitrary (key, value) pair from the dictionary.

\n

popitem() is useful to destructively iterate over a dictionary, as\noften used in set algorithms. If the dictionary is empty, calling\npopitem() raises a KeyError.

\n
\n\n
\n
\nsetdefault(key[, default])
\n
If key is in the dictionary, return its value. If not, insert key\nwith a value of default and return default. default defaults to\nNone.
\n\n
\n
\nupdate([other])
\n

Update the dictionary with the key/value pairs from other, overwriting\nexisting keys. Return None.

\n

update() accepts either another dictionary object or an iterable of\nkey/value pairs (as tuples or other iterables of length two). If keyword\narguments are specified, the dictionary is then updated with those\nkey/value pairs: d.update(red=1, blue=2).

\n

\nChanged in version 2.4: Allowed the argument to be an iterable of key/value pairs and allowed\nkeyword arguments.

\n
\n\n
\n
\nvalues()
\n
Return a copy of the dictionary’s list of values. See the note for\ndict.items().
\n\n
\n
\nviewitems()
\n

Return a new view of the dictionary’s items ((key, value) pairs). See\nbelow for documentation of view objects.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nviewkeys()
\n

Return a new view of the dictionary’s keys. See below for documentation of\nview objects.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nviewvalues()
\n

Return a new view of the dictionary’s values. See below for documentation of\nview objects.

\n

\nNew in version 2.7.

\n
\n\n
\n\n
\n

5.8.1. Dictionary view objects

\n

The objects returned by dict.viewkeys(), dict.viewvalues() and\ndict.viewitems() are view objects. They provide a dynamic view on the\ndictionary’s entries, which means that when the dictionary changes, the view\nreflects these changes.

\n

Dictionary views can be iterated over to yield their respective data, and\nsupport membership tests:

\n
\n
\nlen(dictview)
\n
Return the number of entries in the dictionary.
\n\n
\n
\niter(dictview)
\n

Return an iterator over the keys, values or items (represented as tuples of\n(key, value)) in the dictionary.

\n

Keys and values are iterated over in an arbitrary order which is non-random,\nvaries across Python implementations, and depends on the dictionary’s history\nof insertions and deletions. If keys, values and items views are iterated\nover with no intervening modifications to the dictionary, the order of items\nwill directly correspond. This allows the creation of (value, key) pairs\nusing zip(): pairs = zip(d.values(), d.keys()). Another way to\ncreate the same list is pairs = [(v, k) for (k, v) in d.items()].

\n

Iterating views while adding or deleting entries in the dictionary may raise\na RuntimeError or fail to iterate over all entries.

\n
\n\n
\n
\nx in dictview
\n
Return True if x is in the underlying dictionary’s keys, values or\nitems (in the latter case, x should be a (key, value) tuple).
\n\n

Keys views are set-like since their entries are unique and hashable. If all\nvalues are hashable, so that (key, value) pairs are unique and hashable, then\nthe items view is also set-like. (Values views are not treated as set-like\nsince the entries are generally not unique.) Then these set operations are\navailable (“other” refers either to another view or a set):

\n
\n
\ndictview & other
\n
Return the intersection of the dictview and the other object as a new set.
\n\n
\n
\ndictview | other
\n
Return the union of the dictview and the other object as a new set.
\n\n
\n
\ndictview - other
\n
Return the difference between the dictview and the other object (all elements\nin dictview that aren’t in other) as a new set.
\n\n
\n
\ndictview ^ other
\n
Return the symmetric difference (all elements either in dictview or\nother, but not in both) of the dictview and the other object as a new set.
\n\n

An example of dictionary view usage:

\n
>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}\n>>> keys = dishes.viewkeys()\n>>> values = dishes.viewvalues()\n\n>>> # iteration\n>>> n = 0\n>>> for val in values:\n...     n += val\n>>> print(n)\n504\n\n>>> # keys and values are iterated over in the same order\n>>> list(keys)\n['eggs', 'bacon', 'sausage', 'spam']\n>>> list(values)\n[2, 1, 1, 500]\n\n>>> # view objects are dynamic and reflect dict changes\n>>> del dishes['eggs']\n>>> del dishes['sausage']\n>>> list(keys)\n['spam', 'bacon']\n\n>>> # set operations\n>>> keys & {'eggs', 'bacon', 'salad'}\n{'bacon'}\n
\n
\n
\n
\n
\n

5.9. File Objects

\n

File objects are implemented using C’s stdio package and can be\ncreated with the built-in open() function. File\nobjects are also returned by some other built-in functions and methods,\nsuch as os.popen() and os.fdopen() and the makefile()\nmethod of socket objects. Temporary files can be created using the\ntempfile module, and high-level file operations such as copying,\nmoving, and deleting files and directories can be achieved with the\nshutil module.

\n

When a file operation fails for an I/O-related reason, the exception\nIOError is raised. This includes situations where the operation is not\ndefined for some reason, like seek() on a tty device or writing a file\nopened for reading.

\n

Files have the following methods:

\n
\n
\nfile.close()
\n

Close the file. A closed file cannot be read or written any more. Any operation\nwhich requires that the file be open will raise a ValueError after the\nfile has been closed. Calling close() more than once is allowed.

\n

As of Python 2.5, you can avoid having to call this method explicitly if you use\nthe with statement. For example, the following code will\nautomatically close f when the with block is exited:

\n
from __future__ import with_statement # This isn't required in Python 2.6\n\nwith open("hello.txt") as f:\n    for line in f:\n        print line\n
\n
\n

In older versions of Python, you would have needed to do this to get the same\neffect:

\n
f = open("hello.txt")\ntry:\n    for line in f:\n        print line\nfinally:\n    f.close()\n
\n
\n
\n

Note

\n

Not all “file-like” types in Python support use as a context manager for the\nwith statement. If your code is intended to work with any file-like\nobject, you can use the function contextlib.closing() instead of using\nthe object directly.

\n
\n
\n\n
\n
\nfile.flush()
\n

Flush the internal buffer, like stdio‘s fflush(). This may be a\nno-op on some file-like objects.

\n
\n

Note

\n

flush() does not necessarily write the file’s data to disk. Use\nflush() followed by os.fsync() to ensure this behavior.

\n
\n
\n\n
\n
\nfile.fileno()
\n

Return the integer “file descriptor” that is used by the underlying\nimplementation to request I/O operations from the operating system. This can be\nuseful for other, lower level interfaces that use file descriptors, such as the\nfcntl module or os.read() and friends.

\n
\n

Note

\n

File-like objects which do not have a real file descriptor should not provide\nthis method!

\n
\n
\n\n
\n
\nfile.isatty()
\n

Return True if the file is connected to a tty(-like) device, else False.

\n
\n

Note

\n

If a file-like object is not associated with a real file, this method should\nnot be implemented.

\n
\n
\n\n
\n
\nfile.next()
\n

A file object is its own iterator, for example iter(f) returns f (unless\nf is closed). When a file is used as an iterator, typically in a\nfor loop (for example, for line in f: print line), the\nnext() method is called repeatedly. This method returns the next input\nline, or raises StopIteration when EOF is hit when the file is open for\nreading (behavior is undefined when the file is open for writing). In order to\nmake a for loop the most efficient way of looping over the lines of a\nfile (a very common operation), the next() method uses a hidden read-ahead\nbuffer. As a consequence of using a read-ahead buffer, combining next()\nwith other file methods (like readline()) does not work right. However,\nusing seek() to reposition the file to an absolute position will flush the\nread-ahead buffer.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nfile.read([size])
\n

Read at most size bytes from the file (less if the read hits EOF before\nobtaining size bytes). If the size argument is negative or omitted, read\nall data until EOF is reached. The bytes are returned as a string object. An\nempty string is returned when EOF is encountered immediately. (For certain\nfiles, like ttys, it makes sense to continue reading after an EOF is hit.) Note\nthat this method may call the underlying C function fread() more than\nonce in an effort to acquire as close to size bytes as possible. Also note\nthat when in non-blocking mode, less data than was requested may be\nreturned, even if no size parameter was given.

\n
\n

Note

\n

This function is simply a wrapper for the underlying\nfread() C function, and will behave the same in corner cases,\nsuch as whether the EOF value is cached.

\n
\n
\n\n
\n
\nfile.readline([size])
\n

Read one entire line from the file. A trailing newline character is kept in\nthe string (but may be absent when a file ends with an incomplete line). [6]\nIf the size argument is present and non-negative, it is a maximum byte\ncount (including the trailing newline) and an incomplete line may be\nreturned. When size is not 0, an empty string is returned only when EOF\nis encountered immediately.

\n
\n

Note

\n

Unlike stdio‘s fgets(), the returned string contains null characters\n('\\0') if they occurred in the input.

\n
\n
\n\n
\n
\nfile.readlines([sizehint])
\n
Read until EOF using readline() and return a list containing the lines\nthus read. If the optional sizehint argument is present, instead of\nreading up to EOF, whole lines totalling approximately sizehint bytes\n(possibly after rounding up to an internal buffer size) are read. Objects\nimplementing a file-like interface may choose to ignore sizehint if it\ncannot be implemented, or cannot be implemented efficiently.
\n\n
\n
\nfile.xreadlines()
\n

This method returns the same thing as iter(f).

\n

\nNew in version 2.1.

\n

\nDeprecated since version 2.3: Use for line in file instead.

\n
\n\n
\n
\nfile.seek(offset[, whence])
\n

Set the file’s current position, like stdio‘s fseek(). The whence\nargument is optional and defaults to os.SEEK_SET or 0 (absolute file\npositioning); other values are os.SEEK_CUR or 1 (seek relative to the\ncurrent position) and os.SEEK_END or 2 (seek relative to the file’s\nend). There is no return value.

\n

For example, f.seek(2, os.SEEK_CUR) advances the position by two and\nf.seek(-3, os.SEEK_END) sets the position to the third to last.

\n

Note that if the file is opened for appending\n(mode 'a' or 'a+'), any seek() operations will be undone at the\nnext write. If the file is only opened for writing in append mode (mode\n'a'), this method is essentially a no-op, but it remains useful for files\nopened in append mode with reading enabled (mode 'a+'). If the file is\nopened in text mode (without 'b'), only offsets returned by tell() are\nlegal. Use of other offsets causes undefined behavior.

\n

Note that not all file objects are seekable.

\n

\nChanged in version 2.6: Passing float values as offset has been deprecated.

\n
\n\n
\n
\nfile.tell()
\n

Return the file’s current position, like stdio‘s ftell().

\n
\n

Note

\n

On Windows, tell() can return illegal values (after an fgets())\nwhen reading files with Unix-style line-endings. Use binary mode ('rb') to\ncircumvent this problem.

\n
\n
\n\n
\n
\nfile.truncate([size])
\n
Truncate the file’s size. If the optional size argument is present, the file\nis truncated to (at most) that size. The size defaults to the current position.\nThe current file position is not changed. Note that if a specified size exceeds\nthe file’s current size, the result is platform-dependent: possibilities\ninclude that the file may remain unchanged, increase to the specified size as if\nzero-filled, or increase to the specified size with undefined new content.\nAvailability: Windows, many Unix variants.
\n\n
\n
\nfile.write(str)
\n
Write a string to the file. There is no return value. Due to buffering, the\nstring may not actually show up in the file until the flush() or\nclose() method is called.
\n\n
\n
\nfile.writelines(sequence)
\n
Write a sequence of strings to the file. The sequence can be any iterable\nobject producing strings, typically a list of strings. There is no return value.\n(The name is intended to match readlines(); writelines() does not\nadd line separators.)
\n\n

Files support the iterator protocol. Each iteration returns the same result as\nfile.readline(), and iteration ends when the readline() method returns\nan empty string.

\n

File objects also offer a number of other interesting attributes. These are not\nrequired for file-like objects, but should be implemented if they make sense for\nthe particular object.

\n
\n
\nfile.closed
\n
bool indicating the current state of the file object. This is a read-only\nattribute; the close() method changes the value. It may not be available\non all file-like objects.
\n\n
\n
\nfile.encoding
\n

The encoding that this file uses. When Unicode strings are written to a file,\nthey will be converted to byte strings using this encoding. In addition, when\nthe file is connected to a terminal, the attribute gives the encoding that the\nterminal is likely to use (that information might be incorrect if the user has\nmisconfigured the terminal). The attribute is read-only and may not be present\non all file-like objects. It may also be None, in which case the file uses\nthe system default encoding for converting Unicode strings.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nfile.errors
\n

The Unicode error handler used along with the encoding.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nfile.mode
\n
The I/O mode for the file. If the file was created using the open()\nbuilt-in function, this will be the value of the mode parameter. This is a\nread-only attribute and may not be present on all file-like objects.
\n\n
\n
\nfile.name
\n
If the file object was created using open(), the name of the file.\nOtherwise, some string that indicates the source of the file object, of the\nform <...>. This is a read-only attribute and may not be present on all\nfile-like objects.
\n\n
\n
\nfile.newlines
\n
If Python was built with universal newlines enabled (the default) this\nread-only attribute exists, and for files opened in universal newline read\nmode it keeps track of the types of newlines encountered while reading the\nfile. The values it can take are '\\r', '\\n', '\\r\\n', None\n(unknown, no newlines read yet) or a tuple containing all the newline types\nseen, to indicate that multiple newline conventions were encountered. For\nfiles not opened in universal newline read mode the value of this attribute\nwill be None.
\n\n
\n
\nfile.softspace
\n

Boolean that indicates whether a space character needs to be printed before\nanother value when using the print statement. Classes that are trying\nto simulate a file object should also have a writable softspace\nattribute, which should be initialized to zero. This will be automatic for most\nclasses implemented in Python (care may be needed for objects that override\nattribute access); types implemented in C will have to provide a writable\nsoftspace attribute.

\n
\n

Note

\n

This attribute is not used to control the print statement, but to\nallow the implementation of print to keep track of its internal\nstate.

\n
\n
\n\n
\n
\n

5.10. memoryview type

\n

\nNew in version 2.7.

\n

memoryview objects allow Python code to access the internal data\nof an object that supports the buffer protocol without copying. Memory\nis generally interpreted as simple bytes.

\n
\n
\nclass memoryview(obj)
\n

Create a memoryview that references obj. obj must support the\nbuffer protocol. Built-in objects that support the buffer protocol include\nstr and bytearray (but not unicode).

\n

A memoryview has the notion of an element, which is the\natomic memory unit handled by the originating object obj. For many\nsimple types such as str and bytearray, an element\nis a single byte, but other third-party types may expose larger elements.

\n

len(view) returns the total number of elements in the memoryview,\nview. The itemsize attribute will give you the\nnumber of bytes in a single element.

\n

A memoryview supports slicing to expose its data. Taking a single\nindex will return a single element as a str object. Full\nslicing will result in a subview:

\n
>>> v = memoryview('abcefg')\n>>> v[1]\n'b'\n>>> v[-1]\n'g'\n>>> v[1:4]\n<memory at 0x77ab28>\n>>> v[1:4].tobytes()\n'bce'\n
\n
\n

If the object the memoryview is over supports changing its data, the\nmemoryview supports slice assignment:

\n
>>> data = bytearray('abcefg')\n>>> v = memoryview(data)\n>>> v.readonly\nFalse\n>>> v[0] = 'z'\n>>> data\nbytearray(b'zbcefg')\n>>> v[1:4] = '123'\n>>> data\nbytearray(b'z123fg')\n>>> v[2] = 'spam'\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\nValueError: cannot modify size of memoryview object\n
\n
\n

Notice how the size of the memoryview object cannot be changed.

\n

memoryview has two methods:

\n
\n
\ntobytes()
\n

Return the data in the buffer as a bytestring (an object of class\nstr).

\n
>>> m = memoryview("abc")\n>>> m.tobytes()\n'abc'\n
\n
\n
\n\n
\n
\ntolist()
\n

Return the data in the buffer as a list of integers.

\n
>>> memoryview("abc").tolist()\n[97, 98, 99]\n
\n
\n
\n\n

There are also several readonly attributes available:

\n
\n
\nformat
\n
A string containing the format (in struct module style) for each\nelement in the view. This defaults to 'B', a simple bytestring.
\n\n
\n
\nitemsize
\n
The size in bytes of each element of the memoryview.
\n\n
\n
\nshape
\n
A tuple of integers the length of ndim giving the shape of the\nmemory as a N-dimensional array.
\n\n
\n
\nndim
\n
An integer indicating how many dimensions of a multi-dimensional array the\nmemory represents.
\n\n
\n
\nstrides
\n
A tuple of integers the length of ndim giving the size in bytes to\naccess each element for each dimension of the array.
\n\n
\n
\nreadonly
\n
A bool indicating whether the memory is read only.
\n\n
\n\n
\n
\n

5.11. Context Manager Types

\n

\nNew in version 2.5.

\n

Python’s with statement supports the concept of a runtime context\ndefined by a context manager. This is implemented using two separate methods\nthat allow user-defined classes to define a runtime context that is entered\nbefore the statement body is executed and exited when the statement ends.

\n

The context management protocol consists of a pair of methods that need\nto be provided for a context manager object to define a runtime context:

\n
\n
\ncontextmanager.__enter__()
\n

Enter the runtime context and return either this object or another object\nrelated to the runtime context. The value returned by this method is bound to\nthe identifier in the as clause of with statements using\nthis context manager.

\n

An example of a context manager that returns itself is a file object. File\nobjects return themselves from __enter__() to allow open() to be used as\nthe context expression in a with statement.

\n

An example of a context manager that returns a related object is the one\nreturned by decimal.localcontext(). These managers set the active\ndecimal context to a copy of the original decimal context and then return the\ncopy. This allows changes to be made to the current decimal context in the body\nof the with statement without affecting code outside the\nwith statement.

\n
\n\n
\n
\ncontextmanager.__exit__(exc_type, exc_val, exc_tb)
\n

Exit the runtime context and return a Boolean flag indicating if any exception\nthat occurred should be suppressed. If an exception occurred while executing the\nbody of the with statement, the arguments contain the exception type,\nvalue and traceback information. Otherwise, all three arguments are None.

\n

Returning a true value from this method will cause the with statement\nto suppress the exception and continue execution with the statement immediately\nfollowing the with statement. Otherwise the exception continues\npropagating after this method has finished executing. Exceptions that occur\nduring execution of this method will replace any exception that occurred in the\nbody of the with statement.

\n

The exception passed in should never be reraised explicitly - instead, this\nmethod should return a false value to indicate that the method completed\nsuccessfully and does not want to suppress the raised exception. This allows\ncontext management code (such as contextlib.nested) to easily detect whether\nor not an __exit__() method has actually failed.

\n
\n\n

Python defines several context managers to support easy thread synchronisation,\nprompt closure of files or other objects, and simpler manipulation of the active\ndecimal arithmetic context. The specific types are not treated specially beyond\ntheir implementation of the context management protocol. See the\ncontextlib module for some examples.

\n

Python’s generators and the contextlib.contextmanager decorator\nprovide a convenient way to implement these protocols. If a generator function is\ndecorated with the contextlib.contextmanager decorator, it will return a\ncontext manager implementing the necessary __enter__() and\n__exit__() methods, rather than the iterator produced by an undecorated\ngenerator function.

\n

Note that there is no specific slot for any of these methods in the type\nstructure for Python objects in the Python/C API. Extension types wanting to\ndefine these methods must provide them as a normal Python accessible method.\nCompared to the overhead of setting up the runtime context, the overhead of a\nsingle class dictionary lookup is negligible.

\n
\n
\n

5.12. Other Built-in Types

\n

The interpreter supports several other kinds of objects. Most of these support\nonly one or two operations.

\n
\n

5.12.1. Modules

\n

The only special operation on a module is attribute access: m.name, where\nm is a module and name accesses a name defined in m‘s symbol table.\nModule attributes can be assigned to. (Note that the import\nstatement is not, strictly speaking, an operation on a module object; import\nfoo does not require a module object named foo to exist, rather it requires\nan (external) definition for a module named foo somewhere.)

\n

A special attribute of every module is __dict__. This is the dictionary\ncontaining the module’s symbol table. Modifying this dictionary will actually\nchange the module’s symbol table, but direct assignment to the __dict__\nattribute is not possible (you can write m.__dict__['a'] = 1, which defines\nm.a to be 1, but you can’t write m.__dict__ = {}). Modifying\n__dict__ directly is not recommended.

\n

Modules built into the interpreter are written like this: <module 'sys'\n(built-in)>. If loaded from a file, they are written as <module 'os' from\n'/usr/local/lib/pythonX.Y/os.pyc'>.

\n
\n
\n

5.12.2. Classes and Class Instances

\n

See Objects, values and types and Class definitions for these.

\n
\n
\n

5.12.3. Functions

\n

Function objects are created by function definitions. The only operation on a\nfunction object is to call it: func(argument-list).

\n

There are really two flavors of function objects: built-in functions and\nuser-defined functions. Both support the same operation (to call the function),\nbut the implementation is different, hence the different object types.

\n

See Function definitions for more information.

\n
\n
\n

5.12.4. Methods

\n

Methods are functions that are called using the attribute notation. There are\ntwo flavors: built-in methods (such as append() on lists) and class\ninstance methods. Built-in methods are described with the types that support\nthem.

\n

The implementation adds two special read-only attributes to class instance\nmethods: m.im_self is the object on which the method operates, and\nm.im_func is the function implementing the method. Calling m(arg-1,\narg-2, ..., arg-n) is completely equivalent to calling m.im_func(m.im_self,\narg-1, arg-2, ..., arg-n).

\n

Class instance methods are either bound or unbound, referring to whether the\nmethod was accessed through an instance or a class, respectively. When a method\nis unbound, its im_self attribute will be None and if called, an\nexplicit self object must be passed as the first argument. In this case,\nself must be an instance of the unbound method’s class (or a subclass of\nthat class), otherwise a TypeError is raised.

\n

Like function objects, methods objects support getting arbitrary attributes.\nHowever, since method attributes are actually stored on the underlying function\nobject (meth.im_func), setting method attributes on either bound or unbound\nmethods is disallowed. Attempting to set a method attribute results in a\nTypeError being raised. In order to set a method attribute, you need to\nexplicitly set it on the underlying function object:

\n
class C:\n    def method(self):\n        pass\n\nc = C()\nc.method.im_func.whoami = 'my name is c'\n
\n
\n

See The standard type hierarchy for more information.

\n
\n
\n

5.12.5. Code Objects

\n

Code objects are used by the implementation to represent “pseudo-compiled”\nexecutable Python code such as a function body. They differ from function\nobjects because they don’t contain a reference to their global execution\nenvironment. Code objects are returned by the built-in compile() function\nand can be extracted from function objects through their func_code\nattribute. See also the code module.

\n

A code object can be executed or evaluated by passing it (instead of a source\nstring) to the exec statement or the built-in eval() function.

\n

See The standard type hierarchy for more information.

\n
\n
\n

5.12.6. Type Objects

\n

Type objects represent the various object types. An object’s type is accessed\nby the built-in function type(). There are no special operations on\ntypes. The standard module types defines names for all standard built-in\ntypes.

\n

Types are written like this: <type 'int'>.

\n
\n
\n

5.12.7. The Null Object

\n

This object is returned by functions that don’t explicitly return a value. It\nsupports no special operations. There is exactly one null object, named\nNone (a built-in name).

\n

It is written as None.

\n
\n
\n

5.12.8. The Ellipsis Object

\n

This object is used by extended slice notation (see Slicings). It\nsupports no special operations. There is exactly one ellipsis object, named\nEllipsis (a built-in name).

\n

It is written as Ellipsis. When in a subscript, it can also be written as\n..., for example seq[...].

\n
\n
\n

5.12.9. The NotImplemented Object

\n

This object is returned from comparisons and binary operations when they are\nasked to operate on types they don’t support. See Comparisons for more\ninformation.

\n

It is written as NotImplemented.

\n
\n
\n

5.12.10. Boolean Values

\n

Boolean values are the two constant objects False and True. They are\nused to represent truth values (although other values can also be considered\nfalse or true). In numeric contexts (for example when used as the argument to\nan arithmetic operator), they behave like the integers 0 and 1, respectively.\nThe built-in function bool() can be used to convert any value to a\nBoolean, if the value can be interpreted as a truth value (see section\nTruth Value Testing above).

\n

They are written as False and True, respectively.

\n
\n
\n

5.12.11. Internal Objects

\n

See The standard type hierarchy for this information. It describes stack frame objects,\ntraceback objects, and slice objects.

\n
\n
\n
\n

5.13. Special Attributes

\n

The implementation adds a few special read-only attributes to several object\ntypes, where they are relevant. Some of these are not reported by the\ndir() built-in function.

\n
\n
\nobject.__dict__
\n
A dictionary or other mapping object used to store an object’s (writable)\nattributes.
\n\n
\n
\nobject.__methods__
\n

\nDeprecated since version 2.2: Use the built-in function dir() to get a list of an object’s attributes.\nThis attribute is no longer available.

\n
\n\n
\n
\nobject.__members__
\n

\nDeprecated since version 2.2: Use the built-in function dir() to get a list of an object’s attributes.\nThis attribute is no longer available.

\n
\n\n
\n
\ninstance.__class__
\n
The class to which a class instance belongs.
\n\n
\n
\nclass.__bases__
\n
The tuple of base classes of a class object.
\n\n
\n
\nclass.__name__
\n
The name of the class or type.
\n\n

The following attributes are only supported by new-style classes.

\n
\n
\nclass.__mro__
\n
This attribute is a tuple of classes that are considered when looking for\nbase classes during method resolution.
\n\n
\n
\nclass.mro()
\n
This method can be overridden by a metaclass to customize the method\nresolution order for its instances. It is called at class instantiation, and\nits result is stored in __mro__.
\n\n
\n
\nclass.__subclasses__()
\n

Each new-style class keeps a list of weak references to its immediate\nsubclasses. This method returns a list of all those references still alive.\nExample:

\n
>>> int.__subclasses__()\n[<type 'bool'>]\n
\n
\n
\n\n

Footnotes

\n\n\n\n\n\n
[1]Additional information on these special methods may be found in the Python\nReference Manual (Basic customization).
\n\n\n\n\n\n
[2]As a consequence, the list [1, 2] is considered equal to [1.0, 2.0], and\nsimilarly for tuples.
\n\n\n\n\n\n
[3]They must have since the parser can’t tell the type of the operands.
\n\n\n\n\n\n
[4](1, 2, 3, 4) Cased characters are those with general category property being one of\n“Lu” (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase).
\n\n\n\n\n\n
[5]To format only a tuple you should therefore provide a singleton tuple whose only\nelement is the tuple to be formatted.
\n\n\n\n\n\n
[6]The advantage of leaving the newline on is that returning an empty string is\nthen an unambiguous EOF indication. It is also possible (in cases where it\nmight matter, for example, if you want to make an exact copy of a file while\nscanning its lines) to tell whether the last line of a file ended in a newline\nor not (yes this happens!).
\n
\n
", "searchableItems": [ { "name": "class.__subclasses__", "domId": "_class.__subclasses__" }, { "name": "class.mro", "domId": "_class.mro" }, { "name": "container.__iter__", "domId": "_container.__iter__" }, { "name": "contextmanager.__enter__", "domId": "_contextmanager.__enter__" }, { "name": "contextmanager.__exit__", "domId": "_contextmanager.__exit__" }, { "name": "dict", "domId": "_dict" }, { "name": "dict.clear", "domId": "_dict.clear" }, { "name": "dict.copy", "domId": "_dict.copy" }, { "name": "dict.fromkeys", "domId": "_dict.fromkeys" }, { "name": "dict.get", "domId": "_dict.get" }, { "name": "dict.has_key", "domId": "_dict.has_key" }, { "name": "dict.items", "domId": "_dict.items" }, { "name": "dict.iteritems", "domId": "_dict.iteritems" }, { "name": "dict.iterkeys", "domId": "_dict.iterkeys" }, { "name": "dict.itervalues", "domId": "_dict.itervalues" }, { "name": "dict.keys", "domId": "_dict.keys" }, { "name": "dict.pop", "domId": "_dict.pop" }, { "name": "dict.popitem", "domId": "_dict.popitem" }, { "name": "dict.setdefault", "domId": "_dict.setdefault" }, { "name": "dict.update", "domId": "_dict.update" }, { "name": "dict.values", "domId": "_dict.values" }, { "name": "dict.viewitems", "domId": "_dict.viewitems" }, { "name": "dict.viewkeys", "domId": "_dict.viewkeys" }, { "name": "dict.viewvalues", "domId": "_dict.viewvalues" }, { "name": "file.close", "domId": "_file.close" }, { "name": "file.fileno", "domId": "_file.fileno" }, { "name": "file.flush", "domId": "_file.flush" }, { "name": "file.isatty", "domId": "_file.isatty" }, { "name": "file.next", "domId": "_file.next" }, { "name": "file.read", "domId": "_file.read" }, { "name": "file.readline", "domId": "_file.readline" }, { "name": "file.readlines", "domId": "_file.readlines" }, { "name": "file.seek", "domId": "_file.seek" }, { "name": "file.tell", "domId": "_file.tell" }, { "name": "file.truncate", "domId": "_file.truncate" }, { "name": "file.write", "domId": "_file.write" }, { "name": "file.writelines", "domId": "_file.writelines" }, { "name": "file.xreadlines", "domId": "_file.xreadlines" }, { "name": "float.as_integer_ratio", "domId": "_float.as_integer_ratio" }, { "name": "float.fromhex", "domId": "_float.fromhex" }, { "name": "float.hex", "domId": "_float.hex" }, { "name": "float.is_integer", "domId": "_float.is_integer" }, { "name": "int.bit_length", "domId": "_int.bit_length" }, { "name": "iterator.__iter__", "domId": "_iterator.__iter__" }, { "name": "iterator.next", "domId": "_iterator.next" }, { "name": "long.bit_length", "domId": "_long.bit_length" }, { "name": "memoryview", "domId": "_memoryview" }, { "name": "memoryview.tobytes", "domId": "_memoryview.tobytes" }, { "name": "memoryview.tolist", "domId": "_memoryview.tolist" }, { "name": "set", "domId": "_set" }, { "name": "set.add", "domId": "_set.add" }, { "name": "set.clear", "domId": "_set.clear" }, { "name": "set.copy", "domId": "_set.copy" }, { "name": "set.difference", "domId": "_set.difference" }, { "name": "set.difference_update", "domId": "_set.difference_update" }, { "name": "set.discard", "domId": "_set.discard" }, { "name": "set.intersection", "domId": "_set.intersection" }, { "name": "set.intersection_update", "domId": "_set.intersection_update" }, { "name": "set.isdisjoint", "domId": "_set.isdisjoint" }, { "name": "set.issubset", "domId": "_set.issubset" }, { "name": "set.issuperset", "domId": "_set.issuperset" }, { "name": "set.pop", "domId": "_set.pop" }, { "name": "set.remove", "domId": "_set.remove" }, { "name": "set.symmetric_difference", "domId": "_set.symmetric_difference" }, { "name": "set.symmetric_difference_update", "domId": "_set.symmetric_difference_update" }, { "name": "set.union", "domId": "_set.union" }, { "name": "set.update", "domId": "_set.update" }, { "name": "str.capitalize", "domId": "_str.capitalize" }, { "name": "str.center", "domId": "_str.center" }, { "name": "str.count", "domId": "_str.count" }, { "name": "str.decode", "domId": "_str.decode" }, { "name": "str.encode", "domId": "_str.encode" }, { "name": "str.endswith", "domId": "_str.endswith" }, { "name": "str.expandtabs", "domId": "_str.expandtabs" }, { "name": "str.find", "domId": "_str.find" }, { "name": "str.format", "domId": "_str.format" }, { "name": "str.index", "domId": "_str.index" }, { "name": "str.isalnum", "domId": "_str.isalnum" }, { "name": "str.isalpha", "domId": "_str.isalpha" }, { "name": "str.isdigit", "domId": "_str.isdigit" }, { "name": "str.islower", "domId": "_str.islower" }, { "name": "str.isspace", "domId": "_str.isspace" }, { "name": "str.istitle", "domId": "_str.istitle" }, { "name": "str.isupper", "domId": "_str.isupper" }, { "name": "str.join", "domId": "_str.join" }, { "name": "str.ljust", "domId": "_str.ljust" }, { "name": "str.lower", "domId": "_str.lower" }, { "name": "str.lstrip", "domId": "_str.lstrip" }, { "name": "str.partition", "domId": "_str.partition" }, { "name": "str.replace", "domId": "_str.replace" }, { "name": "str.rfind", "domId": "_str.rfind" }, { "name": "str.rindex", "domId": "_str.rindex" }, { "name": "str.rjust", "domId": "_str.rjust" }, { "name": "str.rpartition", "domId": "_str.rpartition" }, { "name": "str.rsplit", "domId": "_str.rsplit" }, { "name": "str.rstrip", "domId": "_str.rstrip" }, { "name": "str.split", "domId": "_str.split" }, { "name": "str.splitlines", "domId": "_str.splitlines" }, { "name": "str.startswith", "domId": "_str.startswith" }, { "name": "str.strip", "domId": "_str.strip" }, { "name": "str.swapcase", "domId": "_str.swapcase" }, { "name": "str.title", "domId": "_str.title" }, { "name": "str.translate", "domId": "_str.translate" }, { "name": "str.upper", "domId": "_str.upper" }, { "name": "str.zfill", "domId": "_str.zfill" }, { "name": "unicode.isdecimal", "domId": "_unicode.isdecimal" }, { "name": "unicode.isnumeric", "domId": "_unicode.isnumeric" } ] }, { "url": "http://docs.python.org/library/difflib.html", "title": "difflib", "html": "
\n

7.4. difflib — Helpers for computing deltas

\n

\nNew in version 2.1.

\n

This module provides classes and functions for comparing sequences. It\ncan be used for example, for comparing files, and can produce difference\ninformation in various formats, including HTML and context and unified\ndiffs. For comparing directories and files, see also, the filecmp module.

\n
\n
\nclass difflib.SequenceMatcher
\n

This is a flexible class for comparing pairs of sequences of any type, so long\nas the sequence elements are hashable. The basic algorithm predates, and is a\nlittle fancier than, an algorithm published in the late 1980’s by Ratcliff and\nObershelp under the hyperbolic name “gestalt pattern matching.” The idea is to\nfind the longest contiguous matching subsequence that contains no “junk”\nelements (the Ratcliff and Obershelp algorithm doesn’t address junk). The same\nidea is then applied recursively to the pieces of the sequences to the left and\nto the right of the matching subsequence. This does not yield minimal edit\nsequences, but does tend to yield matches that “look right” to people.

\n

Timing: The basic Ratcliff-Obershelp algorithm is cubic time in the worst\ncase and quadratic time in the expected case. SequenceMatcher is\nquadratic time for the worst case and has expected-case behavior dependent in a\ncomplicated way on how many elements the sequences have in common; best case\ntime is linear.

\n

Automatic junk heuristic: SequenceMatcher supports a heuristic that\nautomatically treats certain sequence items as junk. The heuristic counts how many\ntimes each individual item appears in the sequence. If an item’s duplicates (after\nthe first one) account for more than 1% of the sequence and the sequence is at least\n200 items long, this item is marked as “popular” and is treated as junk for\nthe purpose of sequence matching. This heuristic can be turned off by setting\nthe autojunk argument to False when creating the SequenceMatcher.

\n

\nNew in version 2.7.1: The autojunk parameter.

\n
\n\n
\n
\nclass difflib.Differ
\n

This is a class for comparing sequences of lines of text, and producing\nhuman-readable differences or deltas. Differ uses SequenceMatcher\nboth to compare sequences of lines, and to compare sequences of characters\nwithin similar (near-matching) lines.

\n

Each line of a Differ delta begins with a two-letter code:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CodeMeaning
'- 'line unique to sequence 1
'+ 'line unique to sequence 2
'  'line common to both sequences
'? 'line not present in either input sequence
\n

Lines beginning with ‘?‘ attempt to guide the eye to intraline differences,\nand were not present in either input sequence. These lines can be confusing if\nthe sequences contain tab characters.

\n
\n\n
\n
\nclass difflib.HtmlDiff
\n

This class can be used to create an HTML table (or a complete HTML file\ncontaining the table) showing a side by side, line by line comparison of text\nwith inter-line and intra-line change highlights. The table can be generated in\neither full or contextual difference mode.

\n

The constructor for this class is:

\n
\n
\n__init__([tabsize][, wrapcolumn][, linejunk][, charjunk])
\n

Initializes instance of HtmlDiff.

\n

tabsize is an optional keyword argument to specify tab stop spacing and\ndefaults to 8.

\n

wrapcolumn is an optional keyword to specify column number where lines are\nbroken and wrapped, defaults to None where lines are not wrapped.

\n

linejunk and charjunk are optional keyword arguments passed into ndiff()\n(used by HtmlDiff to generate the side by side HTML differences). See\nndiff() documentation for argument default values and descriptions.

\n
\n\n

The following methods are public:

\n
\n
\nmake_file(fromlines, tolines[, fromdesc][, todesc][, context][, numlines])
\n

Compares fromlines and tolines (lists of strings) and returns a string which\nis a complete HTML file containing a table showing line by line differences with\ninter-line and intra-line changes highlighted.

\n

fromdesc and todesc are optional keyword arguments to specify from/to file\ncolumn header strings (both default to an empty string).

\n

context and numlines are both optional keyword arguments. Set context to\nTrue when contextual differences are to be shown, else the default is\nFalse to show the full files. numlines defaults to 5. When context\nis True numlines controls the number of context lines which surround the\ndifference highlights. When context is False numlines controls the\nnumber of lines which are shown before a difference highlight when using the\n“next” hyperlinks (setting to zero would cause the “next” hyperlinks to place\nthe next difference highlight at the top of the browser without any leading\ncontext).

\n
\n\n
\n
\nmake_table(fromlines, tolines[, fromdesc][, todesc][, context][, numlines])
\n

Compares fromlines and tolines (lists of strings) and returns a string which\nis a complete HTML table showing line by line differences with inter-line and\nintra-line changes highlighted.

\n

The arguments for this method are the same as those for the make_file()\nmethod.

\n
\n\n

Tools/scripts/diff.py is a command-line front-end to this class and\ncontains a good example of its use.

\n

\nNew in version 2.4.

\n
\n\n
\n
\ndifflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
\n

Compare a and b (lists of strings); return a delta (a generator\ngenerating the delta lines) in context diff format.

\n

Context diffs are a compact way of showing just the lines that have changed plus\na few lines of context. The changes are shown in a before/after style. The\nnumber of context lines is set by n which defaults to three.

\n

By default, the diff control lines (those with *** or ---) are created\nwith a trailing newline. This is helpful so that inputs created from\nfile.readlines() result in diffs that are suitable for use with\nfile.writelines() since both the inputs and outputs have trailing\nnewlines.

\n

For inputs that do not have trailing newlines, set the lineterm argument to\n"" so that the output will be uniformly newline free.

\n

The context diff format normally has a header for filenames and modification\ntimes. Any or all of these may be specified using strings for fromfile,\ntofile, fromfiledate, and tofiledate. The modification times are normally\nexpressed in the ISO 8601 format. If not specified, the\nstrings default to blanks.

\n
>>> s1 = ['bacon\\n', 'eggs\\n', 'ham\\n', 'guido\\n']\n>>> s2 = ['python\\n', 'eggy\\n', 'hamster\\n', 'guido\\n']\n>>> for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'):\n...     sys.stdout.write(line)  # doctest: +NORMALIZE_WHITESPACE\n*** before.py\n--- after.py\n***************\n*** 1,4 ****\n! bacon\n! eggs\n! ham\n  guido\n--- 1,4 ----\n! python\n! eggy\n! hamster\n  guido\n
\n
\n

See A command-line interface to difflib for a more detailed example.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ndifflib.get_close_matches(word, possibilities[, n][, cutoff])
\n

Return a list of the best “good enough” matches. word is a sequence for which\nclose matches are desired (typically a string), and possibilities is a list of\nsequences against which to match word (typically a list of strings).

\n

Optional argument n (default 3) is the maximum number of close matches to\nreturn; n must be greater than 0.

\n

Optional argument cutoff (default 0.6) is a float in the range [0, 1].\nPossibilities that don’t score at least that similar to word are ignored.

\n

The best (no more than n) matches among the possibilities are returned in a\nlist, sorted by similarity score, most similar first.

\n
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])\n['apple', 'ape']\n>>> import keyword\n>>> get_close_matches('wheel', keyword.kwlist)\n['while']\n>>> get_close_matches('apple', keyword.kwlist)\n[]\n>>> get_close_matches('accept', keyword.kwlist)\n['except']\n
\n
\n
\n\n
\n
\ndifflib.ndiff(a, b[, linejunk][, charjunk])
\n

Compare a and b (lists of strings); return a Differ-style\ndelta (a generator generating the delta lines).

\n

Optional keyword parameters linejunk and charjunk are for filter functions\n(or None):

\n

linejunk: A function that accepts a single string argument, and returns true\nif the string is junk, or false if not. The default is (None), starting with\nPython 2.3. Before then, the default was the module-level function\nIS_LINE_JUNK(), which filters out lines without visible characters, except\nfor at most one pound character ('#'). As of Python 2.3, the underlying\nSequenceMatcher class does a dynamic analysis of which lines are so\nfrequent as to constitute noise, and this usually works better than the pre-2.3\ndefault.

\n

charjunk: A function that accepts a character (a string of length 1), and\nreturns if the character is junk, or false if not. The default is module-level\nfunction IS_CHARACTER_JUNK(), which filters out whitespace characters (a\nblank or tab; note: bad idea to include newline in this!).

\n

Tools/scripts/ndiff.py is a command-line front-end to this function.

\n
>>> diff = ndiff('one\\ntwo\\nthree\\n'.splitlines(1),\n...              'ore\\ntree\\nemu\\n'.splitlines(1))\n>>> print ''.join(diff),\n- one\n?  ^\n+ ore\n?  ^\n- two\n- three\n?  -\n+ tree\n+ emu\n
\n
\n
\n\n
\n
\ndifflib.restore(sequence, which)
\n

Return one of the two sequences that generated a delta.

\n

Given a sequence produced by Differ.compare() or ndiff(), extract\nlines originating from file 1 or 2 (parameter which), stripping off line\nprefixes.

\n

Example:

\n
>>> diff = ndiff('one\\ntwo\\nthree\\n'.splitlines(1),\n...              'ore\\ntree\\nemu\\n'.splitlines(1))\n>>> diff = list(diff) # materialize the generated delta into a list\n>>> print ''.join(restore(diff, 1)),\none\ntwo\nthree\n>>> print ''.join(restore(diff, 2)),\nore\ntree\nemu\n
\n
\n
\n\n
\n
\ndifflib.unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
\n

Compare a and b (lists of strings); return a delta (a generator\ngenerating the delta lines) in unified diff format.

\n

Unified diffs are a compact way of showing just the lines that have changed plus\na few lines of context. The changes are shown in a inline style (instead of\nseparate before/after blocks). The number of context lines is set by n which\ndefaults to three.

\n

By default, the diff control lines (those with ---, +++, or @@) are\ncreated with a trailing newline. This is helpful so that inputs created from\nfile.readlines() result in diffs that are suitable for use with\nfile.writelines() since both the inputs and outputs have trailing\nnewlines.

\n

For inputs that do not have trailing newlines, set the lineterm argument to\n"" so that the output will be uniformly newline free.

\n

The context diff format normally has a header for filenames and modification\ntimes. Any or all of these may be specified using strings for fromfile,\ntofile, fromfiledate, and tofiledate. The modification times are normally\nexpressed in the ISO 8601 format. If not specified, the\nstrings default to blanks.

\n
>>> s1 = ['bacon\\n', 'eggs\\n', 'ham\\n', 'guido\\n']\n>>> s2 = ['python\\n', 'eggy\\n', 'hamster\\n', 'guido\\n']\n>>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'):\n...     sys.stdout.write(line)   # doctest: +NORMALIZE_WHITESPACE\n--- before.py\n+++ after.py\n@@ -1,4 +1,4 @@\n-bacon\n-eggs\n-ham\n+python\n+eggy\n+hamster\n guido\n
\n
\n

See A command-line interface to difflib for a more detailed example.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ndifflib.IS_LINE_JUNK(line)
\n
Return true for ignorable lines. The line line is ignorable if line is\nblank or contains a single '#', otherwise it is not ignorable. Used as a\ndefault for parameter linejunk in ndiff() before Python 2.3.
\n\n
\n
\ndifflib.IS_CHARACTER_JUNK(ch)
\n
Return true for ignorable characters. The character ch is ignorable if ch\nis a space or tab, otherwise it is not ignorable. Used as a default for\nparameter charjunk in ndiff().
\n\n
\n

See also

\n
\n
Pattern Matching: The Gestalt Approach
\n
Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener. This\nwas published in Dr. Dobb’s Journal in July, 1988.
\n
\n
\n
\n

7.4.1. SequenceMatcher Objects

\n

The SequenceMatcher class has this constructor:

\n
\n
\nclass difflib.SequenceMatcher([isjunk[, a[, b[, autojunk=True]]]])
\n

Optional argument isjunk must be None (the default) or a one-argument\nfunction that takes a sequence element and returns true if and only if the\nelement is “junk” and should be ignored. Passing None for isjunk is\nequivalent to passing lambda x: 0; in other words, no elements are ignored.\nFor example, pass:

\n
lambda x: x in " \\t"\n
\n
\n

if you’re comparing lines as sequences of characters, and don’t want to synch up\non blanks or hard tabs.

\n

The optional arguments a and b are sequences to be compared; both default to\nempty strings. The elements of both sequences must be hashable.

\n

The optional argument autojunk can be used to disable the automatic junk\nheuristic.

\n

\nNew in version 2.7.1: The autojunk parameter.

\n

SequenceMatcher objects have the following methods:

\n
\n
\nset_seqs(a, b)
\n
Set the two sequences to be compared.
\n\n

SequenceMatcher computes and caches detailed information about the\nsecond sequence, so if you want to compare one sequence against many\nsequences, use set_seq2() to set the commonly used sequence once and\ncall set_seq1() repeatedly, once for each of the other sequences.

\n
\n
\nset_seq1(a)
\n
Set the first sequence to be compared. The second sequence to be compared\nis not changed.
\n\n
\n
\nset_seq2(b)
\n
Set the second sequence to be compared. The first sequence to be compared\nis not changed.
\n\n
\n
\nfind_longest_match(alo, ahi, blo, bhi)
\n

Find longest matching block in a[alo:ahi] and b[blo:bhi].

\n

If isjunk was omitted or None, find_longest_match() returns\n(i, j, k) such that a[i:i+k] is equal to b[j:j+k], where alo\n<= i <= i+k <= ahi and blo <= j <= j+k <= bhi. For all (i', j',\nk') meeting those conditions, the additional conditions k >= k', i\n<= i', and if i == i', j <= j' are also met. In other words, of\nall maximal matching blocks, return one that starts earliest in a, and\nof all those maximal matching blocks that start earliest in a, return\nthe one that starts earliest in b.

\n
>>> s = SequenceMatcher(None, " abcd", "abcd abcd")\n>>> s.find_longest_match(0, 5, 0, 9)\nMatch(a=0, b=4, size=5)\n
\n
\n

If isjunk was provided, first the longest matching block is determined\nas above, but with the additional restriction that no junk element appears\nin the block. Then that block is extended as far as possible by matching\n(only) junk elements on both sides. So the resulting block never matches\non junk except as identical junk happens to be adjacent to an interesting\nmatch.

\n

Here’s the same example as before, but considering blanks to be junk. That\nprevents ' abcd' from matching the ' abcd' at the tail end of the\nsecond sequence directly. Instead only the 'abcd' can match, and\nmatches the leftmost 'abcd' in the second sequence:

\n
>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")\n>>> s.find_longest_match(0, 5, 0, 9)\nMatch(a=1, b=0, size=4)\n
\n
\n

If no blocks match, this returns (alo, blo, 0).

\n

\nChanged in version 2.6: This method returns a named tuple Match(a, b, size).

\n
\n\n
\n
\nget_matching_blocks()
\n

Return list of triples describing matching subsequences. Each triple is of\nthe form (i, j, n), and means that a[i:i+n] == b[j:j+n]. The\ntriples are monotonically increasing in i and j.

\n

The last triple is a dummy, and has the value (len(a), len(b), 0). It\nis the only triple with n == 0. If (i, j, n) and (i', j', n')\nare adjacent triples in the list, and the second is not the last triple in\nthe list, then i+n != i' or j+n != j'; in other words, adjacent\ntriples always describe non-adjacent equal blocks.

\n

\nChanged in version 2.5: The guarantee that adjacent triples always describe non-adjacent blocks\nwas implemented.

\n
>>> s = SequenceMatcher(None, "abxcd", "abcd")\n>>> s.get_matching_blocks()\n[Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]\n
\n
\n
\n\n
\n
\nget_opcodes()
\n

Return list of 5-tuples describing how to turn a into b. Each tuple is\nof the form (tag, i1, i2, j1, j2). The first tuple has i1 == j1 ==\n0, and remaining tuples have i1 equal to the i2 from the preceding\ntuple, and, likewise, j1 equal to the previous j2.

\n

The tag values are strings, with these meanings:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueMeaning
'replace'a[i1:i2] should be replaced by\nb[j1:j2].
'delete'a[i1:i2] should be deleted. Note that\nj1 == j2 in this case.
'insert'b[j1:j2] should be inserted at\na[i1:i1]. Note that i1 == i2 in\nthis case.
'equal'a[i1:i2] == b[j1:j2] (the sub-sequences\nare equal).
\n

For example:

\n
>>> a = "qabxcd"\n>>> b = "abycdf"\n>>> s = SequenceMatcher(None, a, b)\n>>> for tag, i1, i2, j1, j2 in s.get_opcodes():\n...    print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" \n...           (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))\n delete a[0:1] (q) b[0:0] ()\n  equal a[1:3] (ab) b[0:2] (ab)\nreplace a[3:4] (x) b[2:3] (y)\n  equal a[4:6] (cd) b[3:5] (cd)\n insert a[6:6] () b[5:6] (f)\n
\n
\n
\n\n
\n
\nget_grouped_opcodes([n])
\n

Return a generator of groups with up to n lines of context.

\n

Starting with the groups returned by get_opcodes(), this method\nsplits out smaller change clusters and eliminates intervening ranges which\nhave no changes.

\n

The groups are returned in the same format as get_opcodes().

\n

\nNew in version 2.3.

\n
\n\n
\n
\nratio()
\n

Return a measure of the sequences’ similarity as a float in the range [0,\n1].

\n

Where T is the total number of elements in both sequences, and M is the\nnumber of matches, this is 2.0*M / T. Note that this is 1.0 if the\nsequences are identical, and 0.0 if they have nothing in common.

\n

This is expensive to compute if get_matching_blocks() or\nget_opcodes() hasn’t already been called, in which case you may want\nto try quick_ratio() or real_quick_ratio() first to get an\nupper bound.

\n
\n\n
\n
\nquick_ratio()
\n
Return an upper bound on ratio() relatively quickly.
\n\n
\n
\nreal_quick_ratio()
\n
Return an upper bound on ratio() very quickly.
\n\n
\n\n

The three methods that return the ratio of matching to total characters can give\ndifferent results due to differing levels of approximation, although\nquick_ratio() and real_quick_ratio() are always at least as large as\nratio():

\n
>>> s = SequenceMatcher(None, "abcd", "bcde")\n>>> s.ratio()\n0.75\n>>> s.quick_ratio()\n0.75\n>>> s.real_quick_ratio()\n1.0\n
\n
\n
\n
\n

7.4.2. SequenceMatcher Examples

\n

This example compares two strings, considering blanks to be “junk:”

\n
>>> s = SequenceMatcher(lambda x: x == " ",\n...                     "private Thread currentThread;",\n...                     "private volatile Thread currentThread;")\n
\n
\n

ratio() returns a float in [0, 1], measuring the similarity of the\nsequences. As a rule of thumb, a ratio() value over 0.6 means the\nsequences are close matches:

\n
>>> print round(s.ratio(), 3)\n0.866\n
\n
\n

If you’re only interested in where the sequences match,\nget_matching_blocks() is handy:

\n
>>> for block in s.get_matching_blocks():\n...     print "a[%d] and b[%d] match for %d elements"  block\na[0] and b[0] match for 8 elements\na[8] and b[17] match for 21 elements\na[29] and b[38] match for 0 elements\n
\n
\n

Note that the last tuple returned by get_matching_blocks() is always a\ndummy, (len(a), len(b), 0), and this is the only case in which the last\ntuple element (number of elements matched) is 0.

\n

If you want to know how to change the first sequence into the second, use\nget_opcodes():

\n
>>> for opcode in s.get_opcodes():\n...     print "%6s a[%d:%d] b[%d:%d]"  opcode\n equal a[0:8] b[0:8]\ninsert a[8:8] b[8:17]\n equal a[8:29] b[17:38]\n
\n
\n
\n

See also

\n\n
\n
\n
\n

7.4.3. Differ Objects

\n

Note that Differ-generated deltas make no claim to be minimal\ndiffs. To the contrary, minimal diffs are often counter-intuitive, because they\nsynch up anywhere possible, sometimes accidental matches 100 pages apart.\nRestricting synch points to contiguous matches preserves some notion of\nlocality, at the occasional cost of producing a longer diff.

\n

The Differ class has this constructor:

\n
\n
\nclass difflib.Differ([linejunk[, charjunk]])
\n

Optional keyword parameters linejunk and charjunk are for filter functions\n(or None):

\n

linejunk: A function that accepts a single string argument, and returns true\nif the string is junk. The default is None, meaning that no line is\nconsidered junk.

\n

charjunk: A function that accepts a single character argument (a string of\nlength 1), and returns true if the character is junk. The default is None,\nmeaning that no character is considered junk.

\n

Differ objects are used (deltas generated) via a single method:

\n
\n
\ncompare(a, b)
\n

Compare two sequences of lines, and generate the delta (a sequence of lines).

\n

Each sequence must contain individual single-line strings ending with newlines.\nSuch sequences can be obtained from the readlines() method of file-like\nobjects. The delta generated also consists of newline-terminated strings, ready\nto be printed as-is via the writelines() method of a file-like object.

\n
\n\n
\n\n
\n
\n

7.4.4. Differ Example

\n

This example compares two texts. First we set up the texts, sequences of\nindividual single-line strings ending with newlines (such sequences can also be\nobtained from the readlines() method of file-like objects):

\n
>>> text1 = '''  1. Beautiful is better than ugly.\n...   2. Explicit is better than implicit.\n...   3. Simple is better than complex.\n...   4. Complex is better than complicated.\n... '''.splitlines(1)\n>>> len(text1)\n4\n>>> text1[0][-1]\n'\\n'\n>>> text2 = '''  1. Beautiful is better than ugly.\n...   3.   Simple is better than complex.\n...   4. Complicated is better than complex.\n...   5. Flat is better than nested.\n... '''.splitlines(1)\n
\n
\n

Next we instantiate a Differ object:

\n
>>> d = Differ()\n
\n
\n

Note that when instantiating a Differ object we may pass functions to\nfilter out line and character “junk.” See the Differ() constructor for\ndetails.

\n

Finally, we compare the two:

\n
>>> result = list(d.compare(text1, text2))\n
\n
\n

result is a list of strings, so let’s pretty-print it:

\n
>>> from pprint import pprint\n>>> pprint(result)\n['    1. Beautiful is better than ugly.\\n',\n '-   2. Explicit is better than implicit.\\n',\n '-   3. Simple is better than complex.\\n',\n '+   3.   Simple is better than complex.\\n',\n '?     ++\\n',\n '-   4. Complex is better than complicated.\\n',\n '?            ^                     ---- ^\\n',\n '+   4. Complicated is better than complex.\\n',\n '?           ++++ ^                      ^\\n',\n '+   5. Flat is better than nested.\\n']\n
\n
\n

As a single multi-line string it looks like this:

\n
>>> import sys\n>>> sys.stdout.writelines(result)\n    1. Beautiful is better than ugly.\n-   2. Explicit is better than implicit.\n-   3. Simple is better than complex.\n+   3.   Simple is better than complex.\n?     ++\n-   4. Complex is better than complicated.\n?            ^                     ---- ^\n+   4. Complicated is better than complex.\n?           ++++ ^                      ^\n+   5. Flat is better than nested.\n
\n
\n
\n
\n

7.4.5. A command-line interface to difflib

\n

This example shows how to use difflib to create a diff-like utility.\nIt is also contained in the Python source distribution, as\nTools/scripts/diff.py.

\n
""" Command line interface to difflib.py providing diffs in four formats:\n\n* ndiff:    lists every line and highlights interline changes.\n* context:  highlights clusters of changes in a before/after format.\n* unified:  highlights clusters of changes in an inline format.\n* html:     generates side by side comparison with change highlights.\n\n"""\n\nimport sys, os, time, difflib, optparse\n\ndef main():\n     # Configure the option parser\n    usage = "usage: %prog [options] fromfile tofile"\n    parser = optparse.OptionParser(usage)\n    parser.add_option("-c", action="store_true", default=False,\n                      help='Produce a context format diff (default)')\n    parser.add_option("-u", action="store_true", default=False,\n                      help='Produce a unified format diff')\n    hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)'\n    parser.add_option("-m", action="store_true", default=False, help=hlp)\n    parser.add_option("-n", action="store_true", default=False,\n                      help='Produce a ndiff format diff')\n    parser.add_option("-l", "--lines", type="int", default=3,\n                      help='Set number of context lines (default 3)')\n    (options, args) = parser.parse_args()\n\n    if len(args) == 0:\n        parser.print_help()\n        sys.exit(1)\n    if len(args) != 2:\n        parser.error("need to specify both a fromfile and tofile")\n\n    n = options.lines\n    fromfile, tofile = args # as specified in the usage string\n\n    # we're passing these as arguments to the diff function\n    fromdate = time.ctime(os.stat(fromfile).st_mtime)\n    todate = time.ctime(os.stat(tofile).st_mtime)\n    fromlines = open(fromfile, 'U').readlines()\n    tolines = open(tofile, 'U').readlines()\n\n    if options.u:\n        diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,\n                                    fromdate, todate, n=n)\n    elif options.n:\n        diff = difflib.ndiff(fromlines, tolines)\n    elif options.m:\n        diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile,\n                                            tofile, context=options.c,\n                                            numlines=n)\n    else:\n        diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,\n                                    fromdate, todate, n=n)\n\n    # we're using writelines because diff is a generator\n    sys.stdout.writelines(diff)\n\nif __name__ == '__main__':\n    main()\n
\n
\n
\n
", "searchableItems": [ { "name": "difflib.context_diff", "domId": "difflib_difflib.context_diff" }, { "name": "difflib.Differ", "domId": "difflib_difflib.Differ" }, { "name": "difflib.Differ.compare", "domId": "difflib_difflib.Differ.compare" }, { "name": "difflib.get_close_matches", "domId": "difflib_difflib.get_close_matches" }, { "name": "difflib.HtmlDiff", "domId": "difflib_difflib.HtmlDiff" }, { "name": "difflib.HtmlDiff.__init__", "domId": "difflib_difflib.HtmlDiff.__init__" }, { "name": "difflib.HtmlDiff.make_file", "domId": "difflib_difflib.HtmlDiff.make_file" }, { "name": "difflib.HtmlDiff.make_table", "domId": "difflib_difflib.HtmlDiff.make_table" }, { "name": "difflib.IS_CHARACTER_JUNK", "domId": "difflib_difflib.IS_CHARACTER_JUNK" }, { "name": "difflib.IS_LINE_JUNK", "domId": "difflib_difflib.IS_LINE_JUNK" }, { "name": "difflib.ndiff", "domId": "difflib_difflib.ndiff" }, { "name": "difflib.restore", "domId": "difflib_difflib.restore" }, { "name": "difflib.SequenceMatcher", "domId": "difflib_difflib.SequenceMatcher" }, { "name": "difflib.SequenceMatcher.find_longest_match", "domId": "difflib_difflib.SequenceMatcher.find_longest_match" }, { "name": "difflib.SequenceMatcher.get_grouped_opcodes", "domId": "difflib_difflib.SequenceMatcher.get_grouped_opcodes" }, { "name": "difflib.SequenceMatcher.get_matching_blocks", "domId": "difflib_difflib.SequenceMatcher.get_matching_blocks" }, { "name": "difflib.SequenceMatcher.get_opcodes", "domId": "difflib_difflib.SequenceMatcher.get_opcodes" }, { "name": "difflib.SequenceMatcher.quick_ratio", "domId": "difflib_difflib.SequenceMatcher.quick_ratio" }, { "name": "difflib.SequenceMatcher.ratio", "domId": "difflib_difflib.SequenceMatcher.ratio" }, { "name": "difflib.SequenceMatcher.real_quick_ratio", "domId": "difflib_difflib.SequenceMatcher.real_quick_ratio" }, { "name": "difflib.SequenceMatcher.set_seq1", "domId": "difflib_difflib.SequenceMatcher.set_seq1" }, { "name": "difflib.SequenceMatcher.set_seq2", "domId": "difflib_difflib.SequenceMatcher.set_seq2" }, { "name": "difflib.SequenceMatcher.set_seqs", "domId": "difflib_difflib.SequenceMatcher.set_seqs" }, { "name": "difflib.unified_diff", "domId": "difflib_difflib.unified_diff" } ] }, { "url": "http://docs.python.org/library/textwrap.html", "title": "textwrap", "html": "
\n

7.7. textwrap — Text wrapping and filling

\n

\nNew in version 2.3.

\n

Source code: Lib/textwrap.py

\n
\n

The textwrap module provides two convenience functions, wrap() and\nfill(), as well as TextWrapper, the class that does all the work,\nand a utility function dedent(). If you’re just wrapping or filling one\nor two text strings, the convenience functions should be good enough;\notherwise, you should use an instance of TextWrapper for efficiency.

\n
\n
\ntextwrap.wrap(text[, width[, ...]])
\n

Wraps the single paragraph in text (a string) so every line is at most width\ncharacters long. Returns a list of output lines, without final newlines.

\n

Optional keyword arguments correspond to the instance attributes of\nTextWrapper, documented below. width defaults to 70.

\n
\n\n
\n
\ntextwrap.fill(text[, width[, ...]])
\n

Wraps the single paragraph in text, and returns a single string containing the\nwrapped paragraph. fill() is shorthand for

\n
"\\n".join(wrap(text, ...))\n
\n
\n

In particular, fill() accepts exactly the same keyword arguments as\nwrap().

\n
\n\n

Both wrap() and fill() work by creating a TextWrapper\ninstance and calling a single method on it. That instance is not reused, so for\napplications that wrap/fill many text strings, it will be more efficient for you\nto create your own TextWrapper object.

\n

Text is preferably wrapped on whitespaces and right after the hyphens in\nhyphenated words; only then will long words be broken if necessary, unless\nTextWrapper.break_long_words is set to false.

\n

An additional utility function, dedent(), is provided to remove\nindentation from strings that have unwanted whitespace to the left of the text.

\n
\n
\ntextwrap.dedent(text)
\n

Remove any common leading whitespace from every line in text.

\n

This can be used to make triple-quoted strings line up with the left edge of the\ndisplay, while still presenting them in the source code in indented form.

\n

Note that tabs and spaces are both treated as whitespace, but they are not\nequal: the lines "  hello" and "\\thello" are considered to have no\ncommon leading whitespace. (This behaviour is new in Python 2.5; older versions\nof this module incorrectly expanded tabs before searching for common leading\nwhitespace.)

\n

For example:

\n
def test():\n    # end first line with \\ to avoid the empty line!\n    s = '''\\\n    hello\n      world\n    '''\n    print repr(s)          # prints '    hello\\n      world\\n    '\n    print repr(dedent(s))  # prints 'hello\\n  world\\n'\n
\n
\n
\n\n
\n
\nclass textwrap.TextWrapper(...)
\n

The TextWrapper constructor accepts a number of optional keyword\narguments. Each argument corresponds to one instance attribute, so for example

\n
wrapper = TextWrapper(initial_indent="* ")\n
\n
\n

is the same as

\n
wrapper = TextWrapper()\nwrapper.initial_indent = "* "\n
\n
\n

You can re-use the same TextWrapper object many times, and you can\nchange any of its options through direct assignment to instance attributes\nbetween uses.

\n

The TextWrapper instance attributes (and keyword arguments to the\nconstructor) are as follows:

\n
\n
\nwidth
\n
(default: 70) The maximum length of wrapped lines. As long as there\nare no individual words in the input text longer than width,\nTextWrapper guarantees that no output line will be longer than\nwidth characters.
\n\n
\n
\nexpand_tabs
\n
(default: True) If true, then all tab characters in text will be\nexpanded to spaces using the expandtabs() method of text.
\n\n
\n
\nreplace_whitespace
\n

(default: True) If true, each whitespace character (as defined by\nstring.whitespace) remaining after tab expansion will be replaced by a\nsingle space.

\n
\n

Note

\n

If expand_tabs is false and replace_whitespace is true,\neach tab character will be replaced by a single space, which is not\nthe same as tab expansion.

\n
\n
\n

Note

\n

If replace_whitespace is false, newlines may appear in the\nmiddle of a line and cause strange output. For this reason, text should\nbe split into paragraphs (using str.splitlines() or similar)\nwhich are wrapped separately.

\n
\n
\n\n
\n
\ndrop_whitespace
\n

(default: True) If true, whitespace that, after wrapping, happens to\nend up at the beginning or end of a line is dropped (leading whitespace in\nthe first line is always preserved, though).

\n

\nNew in version 2.6: Whitespace was always dropped in earlier versions.

\n
\n\n
\n
\ninitial_indent
\n
(default: '') String that will be prepended to the first line of\nwrapped output. Counts towards the length of the first line.
\n\n
\n
\nsubsequent_indent
\n
(default: '') String that will be prepended to all lines of wrapped\noutput except the first. Counts towards the length of each line except\nthe first.
\n\n
\n
\nfix_sentence_endings
\n

(default: False) If true, TextWrapper attempts to detect\nsentence endings and ensure that sentences are always separated by exactly\ntwo spaces. This is generally desired for text in a monospaced font.\nHowever, the sentence detection algorithm is imperfect: it assumes that a\nsentence ending consists of a lowercase letter followed by one of '.',\n'!', or '?', possibly followed by one of '"' or "'",\nfollowed by a space. One problem with this is algorithm is that it is\nunable to detect the difference between “Dr.” in

\n
[...] Dr. Frankenstein's monster [...]
\n
\n

and “Spot.” in

\n
[...] See Spot. See Spot run [...]
\n
\n

fix_sentence_endings is false by default.

\n

Since the sentence detection algorithm relies on string.lowercase for\nthe definition of “lowercase letter,” and a convention of using two spaces\nafter a period to separate sentences on the same line, it is specific to\nEnglish-language texts.

\n
\n\n
\n
\nbreak_long_words
\n
(default: True) If true, then words longer than width will be\nbroken in order to ensure that no lines are longer than width. If\nit is false, long words will not be broken, and some lines may be longer\nthan width. (Long words will be put on a line by themselves, in\norder to minimize the amount by which width is exceeded.)
\n\n
\n
\nbreak_on_hyphens
\n

(default: True) If true, wrapping will occur preferably on whitespaces\nand right after hyphens in compound words, as it is customary in English.\nIf false, only whitespaces will be considered as potentially good places\nfor line breaks, but you need to set break_long_words to false if\nyou want truly insecable words. Default behaviour in previous versions\nwas to always allow breaking hyphenated words.

\n

\nNew in version 2.6.

\n
\n\n

TextWrapper also provides two public methods, analogous to the\nmodule-level convenience functions:

\n
\n
\nwrap(text)
\n
Wraps the single paragraph in text (a string) so every line is at most\nwidth characters long. All wrapping options are taken from\ninstance attributes of the TextWrapper instance. Returns a list\nof output lines, without final newlines.
\n\n
\n
\nfill(text)
\n
Wraps the single paragraph in text, and returns a single string\ncontaining the wrapped paragraph.
\n\n
\n\n
", "searchableItems": [ { "name": "textwrap.dedent", "domId": "textwrap_textwrap.dedent" }, { "name": "textwrap.fill", "domId": "textwrap_textwrap.fill" }, { "name": "textwrap.TextWrapper", "domId": "textwrap_textwrap.TextWrapper" }, { "name": "textwrap.TextWrapper.fill", "domId": "textwrap_textwrap.TextWrapper.fill" }, { "name": "textwrap.TextWrapper.wrap", "domId": "textwrap_textwrap.TextWrapper.wrap" }, { "name": "textwrap.wrap", "domId": "textwrap_textwrap.wrap" } ] }, { "url": "http://docs.python.org/library/unicodedata.html", "title": "unicodedata", "html": "
\n

7.9. unicodedata — Unicode Database

\n

This module provides access to the Unicode Character Database which defines\ncharacter properties for all Unicode characters. The data in this database is\nbased on the UnicodeData.txt file version 5.2.0 which is publicly\navailable from ftp://ftp.unicode.org/.

\n

The module uses the same names and symbols as defined by the UnicodeData File\nFormat 5.2.0 (see http://www.unicode.org/reports/tr44/tr44-4.html).\nIt defines the following functions:

\n
\n
\nunicodedata.lookup(name)
\n
Look up character by name. If a character with the given name is found, return\nthe corresponding Unicode character. If not found, KeyError is raised.
\n\n
\n
\nunicodedata.name(unichr[, default])
\n
Returns the name assigned to the Unicode character unichr as a string. If no\nname is defined, default is returned, or, if not given, ValueError is\nraised.
\n\n
\n
\nunicodedata.decimal(unichr[, default])
\n
Returns the decimal value assigned to the Unicode character unichr as integer.\nIf no such value is defined, default is returned, or, if not given,\nValueError is raised.
\n\n
\n
\nunicodedata.digit(unichr[, default])
\n
Returns the digit value assigned to the Unicode character unichr as integer.\nIf no such value is defined, default is returned, or, if not given,\nValueError is raised.
\n\n
\n
\nunicodedata.numeric(unichr[, default])
\n
Returns the numeric value assigned to the Unicode character unichr as float.\nIf no such value is defined, default is returned, or, if not given,\nValueError is raised.
\n\n
\n
\nunicodedata.category(unichr)
\n
Returns the general category assigned to the Unicode character unichr as\nstring.
\n\n
\n
\nunicodedata.bidirectional(unichr)
\n
Returns the bidirectional category assigned to the Unicode character unichr as\nstring. If no such value is defined, an empty string is returned.
\n\n
\n
\nunicodedata.combining(unichr)
\n
Returns the canonical combining class assigned to the Unicode character unichr\nas integer. Returns 0 if no combining class is defined.
\n\n
\n
\nunicodedata.east_asian_width(unichr)
\n

Returns the east asian width assigned to the Unicode character unichr as\nstring.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nunicodedata.mirrored(unichr)
\n
Returns the mirrored property assigned to the Unicode character unichr as\ninteger. Returns 1 if the character has been identified as a “mirrored”\ncharacter in bidirectional text, 0 otherwise.
\n\n
\n
\nunicodedata.decomposition(unichr)
\n
Returns the character decomposition mapping assigned to the Unicode character\nunichr as string. An empty string is returned in case no such mapping is\ndefined.
\n\n
\n
\nunicodedata.normalize(form, unistr)
\n

Return the normal form form for the Unicode string unistr. Valid values for\nform are ‘NFC’, ‘NFKC’, ‘NFD’, and ‘NFKD’.

\n

The Unicode standard defines various normalization forms of a Unicode string,\nbased on the definition of canonical equivalence and compatibility equivalence.\nIn Unicode, several characters can be expressed in various way. For example, the\ncharacter U+00C7 (LATIN CAPITAL LETTER C WITH CEDILLA) can also be expressed as\nthe sequence U+0327 (COMBINING CEDILLA) U+0043 (LATIN CAPITAL LETTER C).

\n

For each character, there are two normal forms: normal form C and normal form D.\nNormal form D (NFD) is also known as canonical decomposition, and translates\neach character into its decomposed form. Normal form C (NFC) first applies a\ncanonical decomposition, then composes pre-combined characters again.

\n

In addition to these two forms, there are two additional normal forms based on\ncompatibility equivalence. In Unicode, certain characters are supported which\nnormally would be unified with other characters. For example, U+2160 (ROMAN\nNUMERAL ONE) is really the same thing as U+0049 (LATIN CAPITAL LETTER I).\nHowever, it is supported in Unicode for compatibility with existing character\nsets (e.g. gb2312).

\n

The normal form KD (NFKD) will apply the compatibility decomposition, i.e.\nreplace all compatibility characters with their equivalents. The normal form KC\n(NFKC) first applies the compatibility decomposition, followed by the canonical\ncomposition.

\n

Even if two unicode strings are normalized and look the same to\na human reader, if one has combining characters and the other\ndoesn’t, they may not compare equal.

\n

\nNew in version 2.3.

\n
\n\n

In addition, the module exposes the following constant:

\n
\n
\nunicodedata.unidata_version
\n

The version of the Unicode database used in this module.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nunicodedata.ucd_3_2_0
\n

This is an object that has the same methods as the entire module, but uses the\nUnicode database version 3.2 instead, for applications that require this\nspecific version of the Unicode database (such as IDNA).

\n

\nNew in version 2.5.

\n
\n\n

Examples:

\n
>>> import unicodedata\n>>> unicodedata.lookup('LEFT CURLY BRACKET')\nu'{'\n>>> unicodedata.name(u'/')\n'SOLIDUS'\n>>> unicodedata.decimal(u'9')\n9\n>>> unicodedata.decimal(u'a')\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: not a decimal\n>>> unicodedata.category(u'A')  # 'L'etter, 'u'ppercase\n'Lu'\n>>> unicodedata.bidirectional(u'\\u0660') # 'A'rabic, 'N'umber\n'AN'\n
\n
\n
", "searchableItems": [ { "name": "unicodedata.bidirectional", "domId": "unicodedata_unicodedata.bidirectional" }, { "name": "unicodedata.category", "domId": "unicodedata_unicodedata.category" }, { "name": "unicodedata.combining", "domId": "unicodedata_unicodedata.combining" }, { "name": "unicodedata.decimal", "domId": "unicodedata_unicodedata.decimal" }, { "name": "unicodedata.decomposition", "domId": "unicodedata_unicodedata.decomposition" }, { "name": "unicodedata.digit", "domId": "unicodedata_unicodedata.digit" }, { "name": "unicodedata.east_asian_width", "domId": "unicodedata_unicodedata.east_asian_width" }, { "name": "unicodedata.lookup", "domId": "unicodedata_unicodedata.lookup" }, { "name": "unicodedata.mirrored", "domId": "unicodedata_unicodedata.mirrored" }, { "name": "unicodedata.name", "domId": "unicodedata_unicodedata.name" }, { "name": "unicodedata.normalize", "domId": "unicodedata_unicodedata.normalize" }, { "name": "unicodedata.numeric", "domId": "unicodedata_unicodedata.numeric" } ] }, { "url": "http://docs.python.org/library/stringprep.html", "title": "stringprep", "html": "
\n

7.10. stringprep — Internet String Preparation

\n

\nNew in version 2.3.

\n

When identifying things (such as host names) in the internet, it is often\nnecessary to compare such identifications for “equality”. Exactly how this\ncomparison is executed may depend on the application domain, e.g. whether it\nshould be case-insensitive or not. It may be also necessary to restrict the\npossible identifications, to allow only identifications consisting of\n“printable” characters.

\n

RFC 3454 defines a procedure for “preparing” Unicode strings in internet\nprotocols. Before passing strings onto the wire, they are processed with the\npreparation procedure, after which they have a certain normalized form. The RFC\ndefines a set of tables, which can be combined into profiles. Each profile must\ndefine which tables it uses, and what other optional parts of the stringprep\nprocedure are part of the profile. One example of a stringprep profile is\nnameprep, which is used for internationalized domain names.

\n

The module stringprep only exposes the tables from RFC 3454. As these\ntables would be very large to represent them as dictionaries or lists, the\nmodule uses the Unicode character database internally. The module source code\nitself was generated using the mkstringprep.py utility.

\n

As a result, these tables are exposed as functions, not as data structures.\nThere are two kinds of tables in the RFC: sets and mappings. For a set,\nstringprep provides the “characteristic function”, i.e. a function that\nreturns true if the parameter is part of the set. For mappings, it provides the\nmapping function: given the key, it returns the associated value. Below is a\nlist of all functions available in the module.

\n
\n
\nstringprep.in_table_a1(code)
\n
Determine whether code is in tableA.1 (Unassigned code points in Unicode 3.2).
\n\n
\n
\nstringprep.in_table_b1(code)
\n
Determine whether code is in tableB.1 (Commonly mapped to nothing).
\n\n
\n
\nstringprep.map_table_b2(code)
\n
Return the mapped value for code according to tableB.2 (Mapping for\ncase-folding used with NFKC).
\n\n
\n
\nstringprep.map_table_b3(code)
\n
Return the mapped value for code according to tableB.3 (Mapping for\ncase-folding used with no normalization).
\n\n
\n
\nstringprep.in_table_c11(code)
\n
Determine whether code is in tableC.1.1 (ASCII space characters).
\n\n
\n
\nstringprep.in_table_c12(code)
\n
Determine whether code is in tableC.1.2 (Non-ASCII space characters).
\n\n
\n
\nstringprep.in_table_c11_c12(code)
\n
Determine whether code is in tableC.1 (Space characters, union of C.1.1 and\nC.1.2).
\n\n
\n
\nstringprep.in_table_c21(code)
\n
Determine whether code is in tableC.2.1 (ASCII control characters).
\n\n
\n
\nstringprep.in_table_c22(code)
\n
Determine whether code is in tableC.2.2 (Non-ASCII control characters).
\n\n
\n
\nstringprep.in_table_c21_c22(code)
\n
Determine whether code is in tableC.2 (Control characters, union of C.2.1 and\nC.2.2).
\n\n
\n
\nstringprep.in_table_c3(code)
\n
Determine whether code is in tableC.3 (Private use).
\n\n
\n
\nstringprep.in_table_c4(code)
\n
Determine whether code is in tableC.4 (Non-character code points).
\n\n
\n
\nstringprep.in_table_c5(code)
\n
Determine whether code is in tableC.5 (Surrogate codes).
\n\n
\n
\nstringprep.in_table_c6(code)
\n
Determine whether code is in tableC.6 (Inappropriate for plain text).
\n\n
\n
\nstringprep.in_table_c7(code)
\n
Determine whether code is in tableC.7 (Inappropriate for canonical\nrepresentation).
\n\n
\n
\nstringprep.in_table_c8(code)
\n
Determine whether code is in tableC.8 (Change display properties or are\ndeprecated).
\n\n
\n
\nstringprep.in_table_c9(code)
\n
Determine whether code is in tableC.9 (Tagging characters).
\n\n
\n
\nstringprep.in_table_d1(code)
\n
Determine whether code is in tableD.1 (Characters with bidirectional property\n“R” or “AL”).
\n\n
\n
\nstringprep.in_table_d2(code)
\n
Determine whether code is in tableD.2 (Characters with bidirectional property\n“L”).
\n\n
", "searchableItems": [ { "name": "stringprep.in_table_a1", "domId": "stringprep_stringprep.in_table_a1" }, { "name": "stringprep.in_table_b1", "domId": "stringprep_stringprep.in_table_b1" }, { "name": "stringprep.in_table_c11", "domId": "stringprep_stringprep.in_table_c11" }, { "name": "stringprep.in_table_c11_c12", "domId": "stringprep_stringprep.in_table_c11_c12" }, { "name": "stringprep.in_table_c12", "domId": "stringprep_stringprep.in_table_c12" }, { "name": "stringprep.in_table_c21", "domId": "stringprep_stringprep.in_table_c21" }, { "name": "stringprep.in_table_c21_c22", "domId": "stringprep_stringprep.in_table_c21_c22" }, { "name": "stringprep.in_table_c22", "domId": "stringprep_stringprep.in_table_c22" }, { "name": "stringprep.in_table_c3", "domId": "stringprep_stringprep.in_table_c3" }, { "name": "stringprep.in_table_c4", "domId": "stringprep_stringprep.in_table_c4" }, { "name": "stringprep.in_table_c5", "domId": "stringprep_stringprep.in_table_c5" }, { "name": "stringprep.in_table_c6", "domId": "stringprep_stringprep.in_table_c6" }, { "name": "stringprep.in_table_c7", "domId": "stringprep_stringprep.in_table_c7" }, { "name": "stringprep.in_table_c8", "domId": "stringprep_stringprep.in_table_c8" }, { "name": "stringprep.in_table_c9", "domId": "stringprep_stringprep.in_table_c9" }, { "name": "stringprep.in_table_d1", "domId": "stringprep_stringprep.in_table_d1" }, { "name": "stringprep.in_table_d2", "domId": "stringprep_stringprep.in_table_d2" }, { "name": "stringprep.map_table_b2", "domId": "stringprep_stringprep.map_table_b2" }, { "name": "stringprep.map_table_b3", "domId": "stringprep_stringprep.map_table_b3" } ] }, { "url": "http://docs.python.org/library/codecs.html", "title": "codecs", "html": "
\n

7.8. codecs — Codec registry and base classes

\n

This module defines base classes for standard Python codecs (encoders and\ndecoders) and provides access to the internal Python codec registry which\nmanages the codec and error handling lookup process.

\n

It defines the following functions:

\n
\n
\ncodecs.register(search_function)
\n

Register a codec search function. Search functions are expected to take one\nargument, the encoding name in all lower case letters, and return a\nCodecInfo object having the following attributes:

\n
    \n
  • name The name of the encoding;
  • \n
  • encode The stateless encoding function;
  • \n
  • decode The stateless decoding function;
  • \n
  • incrementalencoder An incremental encoder class or factory function;
  • \n
  • incrementaldecoder An incremental decoder class or factory function;
  • \n
  • streamwriter A stream writer class or factory function;
  • \n
  • streamreader A stream reader class or factory function.
  • \n
\n

The various functions or classes take the following arguments:

\n

encode and decode: These must be functions or methods which have the same\ninterface as the encode()/decode() methods of Codec instances (see\nCodec Interface). The functions/methods are expected to work in a stateless\nmode.

\n

incrementalencoder and incrementaldecoder: These have to be factory\nfunctions providing the following interface:

\n
\nfactory(errors='strict')
\n

The factory functions must return objects providing the interfaces defined by\nthe base classes IncrementalEncoder and IncrementalDecoder,\nrespectively. Incremental codecs can maintain state.

\n

streamreader and streamwriter: These have to be factory functions providing\nthe following interface:

\n
\nfactory(stream, errors='strict')
\n

The factory functions must return objects providing the interfaces defined by\nthe base classes StreamWriter and StreamReader, respectively.\nStream codecs can maintain state.

\n

Possible values for errors are

\n
    \n
  • 'strict': raise an exception in case of an encoding error
  • \n
  • 'replace': replace malformed data with a suitable replacement marker,\nsuch as '?' or '\\ufffd'
  • \n
  • 'ignore': ignore malformed data and continue without further notice
  • \n
  • 'xmlcharrefreplace': replace with the appropriate XML character\nreference (for encoding only)
  • \n
  • 'backslashreplace': replace with backslashed escape sequences (for\nencoding only)
  • \n
\n

as well as any other error handling name defined via register_error().

\n

In case a search function cannot find a given encoding, it should return\nNone.

\n
\n\n
\n
\ncodecs.lookup(encoding)
\n

Looks up the codec info in the Python codec registry and returns a\nCodecInfo object as defined above.

\n

Encodings are first looked up in the registry’s cache. If not found, the list of\nregistered search functions is scanned. If no CodecInfo object is\nfound, a LookupError is raised. Otherwise, the CodecInfo object\nis stored in the cache and returned to the caller.

\n
\n\n

To simplify access to the various codecs, the module provides these additional\nfunctions which use lookup() for the codec lookup:

\n
\n
\ncodecs.getencoder(encoding)
\n

Look up the codec for the given encoding and return its encoder function.

\n

Raises a LookupError in case the encoding cannot be found.

\n
\n\n
\n
\ncodecs.getdecoder(encoding)
\n

Look up the codec for the given encoding and return its decoder function.

\n

Raises a LookupError in case the encoding cannot be found.

\n
\n\n
\n
\ncodecs.getincrementalencoder(encoding)
\n

Look up the codec for the given encoding and return its incremental encoder\nclass or factory function.

\n

Raises a LookupError in case the encoding cannot be found or the codec\ndoesn’t support an incremental encoder.

\n

\nNew in version 2.5.

\n
\n\n
\n
\ncodecs.getincrementaldecoder(encoding)
\n

Look up the codec for the given encoding and return its incremental decoder\nclass or factory function.

\n

Raises a LookupError in case the encoding cannot be found or the codec\ndoesn’t support an incremental decoder.

\n

\nNew in version 2.5.

\n
\n\n
\n
\ncodecs.getreader(encoding)
\n

Look up the codec for the given encoding and return its StreamReader class or\nfactory function.

\n

Raises a LookupError in case the encoding cannot be found.

\n
\n\n
\n
\ncodecs.getwriter(encoding)
\n

Look up the codec for the given encoding and return its StreamWriter class or\nfactory function.

\n

Raises a LookupError in case the encoding cannot be found.

\n
\n\n
\n
\ncodecs.register_error(name, error_handler)
\n

Register the error handling function error_handler under the name name.\nerror_handler will be called during encoding and decoding in case of an error,\nwhen name is specified as the errors parameter.

\n

For encoding error_handler will be called with a UnicodeEncodeError\ninstance, which contains information about the location of the error. The error\nhandler must either raise this or a different exception or return a tuple with a\nreplacement for the unencodable part of the input and a position where encoding\nshould continue. The encoder will encode the replacement and continue encoding\nthe original input at the specified position. Negative position values will be\ntreated as being relative to the end of the input string. If the resulting\nposition is out of bound an IndexError will be raised.

\n

Decoding and translating works similar, except UnicodeDecodeError or\nUnicodeTranslateError will be passed to the handler and that the\nreplacement from the error handler will be put into the output directly.

\n
\n\n
\n
\ncodecs.lookup_error(name)
\n

Return the error handler previously registered under the name name.

\n

Raises a LookupError in case the handler cannot be found.

\n
\n\n
\n
\ncodecs.strict_errors(exception)
\n
Implements the strict error handling: each encoding or decoding error\nraises a UnicodeError.
\n\n
\n
\ncodecs.replace_errors(exception)
\n
Implements the replace error handling: malformed data is replaced with a\nsuitable replacement character such as '?' in bytestrings and\n'\\ufffd' in Unicode strings.
\n\n
\n
\ncodecs.ignore_errors(exception)
\n
Implements the ignore error handling: malformed data is ignored and\nencoding or decoding is continued without further notice.
\n\n
\n
\ncodecs.xmlcharrefreplace_errors(exception)
\n
Implements the xmlcharrefreplace error handling (for encoding only): the\nunencodable character is replaced by an appropriate XML character reference.
\n\n
\n
\ncodecs.backslashreplace_errors(exception)
\n
Implements the backslashreplace error handling (for encoding only): the\nunencodable character is replaced by a backslashed escape sequence.
\n\n

To simplify working with encoded files or stream, the module also defines these\nutility functions:

\n
\n
\ncodecs.open(filename, mode[, encoding[, errors[, buffering]]])
\n

Open an encoded file using the given mode and return a wrapped version\nproviding transparent encoding/decoding. The default file mode is 'r'\nmeaning to open the file in read mode.

\n
\n

Note

\n

The wrapped version will only accept the object format defined by the codecs,\ni.e. Unicode objects for most built-in codecs. Output is also codec-dependent\nand will usually be Unicode as well.

\n
\n
\n

Note

\n

Files are always opened in binary mode, even if no binary mode was\nspecified. This is done to avoid data loss due to encodings using 8-bit\nvalues. This means that no automatic conversion of '\\n' is done\non reading and writing.

\n
\n

encoding specifies the encoding which is to be used for the file.

\n

errors may be given to define the error handling. It defaults to 'strict'\nwhich causes a ValueError to be raised in case an encoding error occurs.

\n

buffering has the same meaning as for the built-in open() function. It\ndefaults to line buffered.

\n
\n\n
\n
\ncodecs.EncodedFile(file, input[, output[, errors]])
\n

Return a wrapped version of file which provides transparent encoding\ntranslation.

\n

Strings written to the wrapped file are interpreted according to the given\ninput encoding and then written to the original file as strings using the\noutput encoding. The intermediate encoding will usually be Unicode but depends\non the specified codecs.

\n

If output is not given, it defaults to input.

\n

errors may be given to define the error handling. It defaults to 'strict',\nwhich causes ValueError to be raised in case an encoding error occurs.

\n
\n\n
\n
\ncodecs.iterencode(iterable, encoding[, errors])
\n

Uses an incremental encoder to iteratively encode the input provided by\niterable. This function is a generator. errors (as well as any\nother keyword argument) is passed through to the incremental encoder.

\n

\nNew in version 2.5.

\n
\n\n
\n
\ncodecs.iterdecode(iterable, encoding[, errors])
\n

Uses an incremental decoder to iteratively decode the input provided by\niterable. This function is a generator. errors (as well as any\nother keyword argument) is passed through to the incremental decoder.

\n

\nNew in version 2.5.

\n
\n\n

The module also provides the following constants which are useful for reading\nand writing to platform dependent files:

\n
\n
\ncodecs.BOM
\n
\ncodecs.BOM_BE
\n
\ncodecs.BOM_LE
\n
\ncodecs.BOM_UTF8
\n
\ncodecs.BOM_UTF16
\n
\ncodecs.BOM_UTF16_BE
\n
\ncodecs.BOM_UTF16_LE
\n
\ncodecs.BOM_UTF32
\n
\ncodecs.BOM_UTF32_BE
\n
\ncodecs.BOM_UTF32_LE
\n
These constants define various encodings of the Unicode byte order mark (BOM)\nused in UTF-16 and UTF-32 data streams to indicate the byte order used in the\nstream or file and in UTF-8 as a Unicode signature. BOM_UTF16 is either\nBOM_UTF16_BE or BOM_UTF16_LE depending on the platform’s\nnative byte order, BOM is an alias for BOM_UTF16,\nBOM_LE for BOM_UTF16_LE and BOM_BE for\nBOM_UTF16_BE. The others represent the BOM in UTF-8 and UTF-32\nencodings.
\n\n
\n

7.8.1. Codec Base Classes

\n

The codecs module defines a set of base classes which define the\ninterface and can also be used to easily write your own codecs for use in\nPython.

\n

Each codec has to define four interfaces to make it usable as codec in Python:\nstateless encoder, stateless decoder, stream reader and stream writer. The\nstream reader and writers typically reuse the stateless encoder/decoder to\nimplement the file protocols.

\n

The Codec class defines the interface for stateless encoders/decoders.

\n

To simplify and standardize error handling, the encode() and\ndecode() methods may implement different error handling schemes by\nproviding the errors string argument. The following string values are defined\nand implemented by all standard Python codecs:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueMeaning
'strict'Raise UnicodeError (or a subclass);\nthis is the default.
'ignore'Ignore the character and continue with the\nnext.
'replace'Replace with a suitable replacement\ncharacter; Python will use the official\nU+FFFD REPLACEMENT CHARACTER for the built-in\nUnicode codecs on decoding and ‘?’ on\nencoding.
'xmlcharrefreplace'Replace with the appropriate XML character\nreference (only for encoding).
'backslashreplace'Replace with backslashed escape sequences\n(only for encoding).
\n

The set of allowed values can be extended via register_error().

\n
\n

7.8.1.1. Codec Objects

\n

The Codec class defines these methods which also define the function\ninterfaces of the stateless encoder and decoder:

\n
\n
\nCodec.encode(input[, errors])
\n

Encodes the object input and returns a tuple (output object, length consumed).\nWhile codecs are not restricted to use with Unicode, in a Unicode context,\nencoding converts a Unicode object to a plain string using a particular\ncharacter set encoding (e.g., cp1252 or iso-8859-1).

\n

errors defines the error handling to apply. It defaults to 'strict'\nhandling.

\n

The method may not store state in the Codec instance. Use\nStreamCodec for codecs which have to keep state in order to make\nencoding/decoding efficient.

\n

The encoder must be able to handle zero length input and return an empty object\nof the output object type in this situation.

\n
\n\n
\n
\nCodec.decode(input[, errors])
\n

Decodes the object input and returns a tuple (output object, length consumed).\nIn a Unicode context, decoding converts a plain string encoded using a\nparticular character set encoding to a Unicode object.

\n

input must be an object which provides the bf_getreadbuf buffer slot.\nPython strings, buffer objects and memory mapped files are examples of objects\nproviding this slot.

\n

errors defines the error handling to apply. It defaults to 'strict'\nhandling.

\n

The method may not store state in the Codec instance. Use\nStreamCodec for codecs which have to keep state in order to make\nencoding/decoding efficient.

\n

The decoder must be able to handle zero length input and return an empty object\nof the output object type in this situation.

\n
\n\n

The IncrementalEncoder and IncrementalDecoder classes provide\nthe basic interface for incremental encoding and decoding. Encoding/decoding the\ninput isn’t done with one call to the stateless encoder/decoder function, but\nwith multiple calls to the encode()/decode() method of the\nincremental encoder/decoder. The incremental encoder/decoder keeps track of the\nencoding/decoding process during method calls.

\n

The joined output of calls to the encode()/decode() method is the\nsame as if all the single inputs were joined into one, and this input was\nencoded/decoded with the stateless encoder/decoder.

\n
\n
\n

7.8.1.2. IncrementalEncoder Objects

\n

\nNew in version 2.5.

\n

The IncrementalEncoder class is used for encoding an input in multiple\nsteps. It defines the following methods which every incremental encoder must\ndefine in order to be compatible with the Python codec registry.

\n
\n
\nclass codecs.IncrementalEncoder([errors])
\n

Constructor for an IncrementalEncoder instance.

\n

All incremental encoders must provide this constructor interface. They are free\nto add additional keyword arguments, but only the ones defined here are used by\nthe Python codec registry.

\n

The IncrementalEncoder may implement different error handling schemes\nby providing the errors keyword argument. These parameters are predefined:

\n
    \n
  • 'strict' Raise ValueError (or a subclass); this is the default.
  • \n
  • 'ignore' Ignore the character and continue with the next.
  • \n
  • 'replace' Replace with a suitable replacement character
  • \n
  • 'xmlcharrefreplace' Replace with the appropriate XML character reference
  • \n
  • 'backslashreplace' Replace with backslashed escape sequences.
  • \n
\n

The errors argument will be assigned to an attribute of the same name.\nAssigning to this attribute makes it possible to switch between different error\nhandling strategies during the lifetime of the IncrementalEncoder\nobject.

\n

The set of allowed values for the errors argument can be extended with\nregister_error().

\n
\n
\nencode(object[, final])
\n
Encodes object (taking the current state of the encoder into account)\nand returns the resulting encoded object. If this is the last call to\nencode() final must be true (the default is false).
\n\n
\n
\nreset()
\n
Reset the encoder to the initial state.
\n\n
\n\n
\n
\n

7.8.1.3. IncrementalDecoder Objects

\n

The IncrementalDecoder class is used for decoding an input in multiple\nsteps. It defines the following methods which every incremental decoder must\ndefine in order to be compatible with the Python codec registry.

\n
\n
\nclass codecs.IncrementalDecoder([errors])
\n

Constructor for an IncrementalDecoder instance.

\n

All incremental decoders must provide this constructor interface. They are free\nto add additional keyword arguments, but only the ones defined here are used by\nthe Python codec registry.

\n

The IncrementalDecoder may implement different error handling schemes\nby providing the errors keyword argument. These parameters are predefined:

\n
    \n
  • 'strict' Raise ValueError (or a subclass); this is the default.
  • \n
  • 'ignore' Ignore the character and continue with the next.
  • \n
  • 'replace' Replace with a suitable replacement character.
  • \n
\n

The errors argument will be assigned to an attribute of the same name.\nAssigning to this attribute makes it possible to switch between different error\nhandling strategies during the lifetime of the IncrementalDecoder\nobject.

\n

The set of allowed values for the errors argument can be extended with\nregister_error().

\n
\n
\ndecode(object[, final])
\n
Decodes object (taking the current state of the decoder into account)\nand returns the resulting decoded object. If this is the last call to\ndecode() final must be true (the default is false). If final is\ntrue the decoder must decode the input completely and must flush all\nbuffers. If this isn’t possible (e.g. because of incomplete byte sequences\nat the end of the input) it must initiate error handling just like in the\nstateless case (which might raise an exception).
\n\n
\n
\nreset()
\n
Reset the decoder to the initial state.
\n\n
\n\n

The StreamWriter and StreamReader classes provide generic\nworking interfaces which can be used to implement new encoding submodules very\neasily. See encodings.utf_8 for an example of how this is done.

\n
\n
\n

7.8.1.4. StreamWriter Objects

\n

The StreamWriter class is a subclass of Codec and defines the\nfollowing methods which every stream writer must define in order to be\ncompatible with the Python codec registry.

\n
\n
\nclass codecs.StreamWriter(stream[, errors])
\n

Constructor for a StreamWriter instance.

\n

All stream writers must provide this constructor interface. They are free to add\nadditional keyword arguments, but only the ones defined here are used by the\nPython codec registry.

\n

stream must be a file-like object open for writing binary data.

\n

The StreamWriter may implement different error handling schemes by\nproviding the errors keyword argument. These parameters are predefined:

\n
    \n
  • 'strict' Raise ValueError (or a subclass); this is the default.
  • \n
  • 'ignore' Ignore the character and continue with the next.
  • \n
  • 'replace' Replace with a suitable replacement character
  • \n
  • 'xmlcharrefreplace' Replace with the appropriate XML character reference
  • \n
  • 'backslashreplace' Replace with backslashed escape sequences.
  • \n
\n

The errors argument will be assigned to an attribute of the same name.\nAssigning to this attribute makes it possible to switch between different error\nhandling strategies during the lifetime of the StreamWriter object.

\n

The set of allowed values for the errors argument can be extended with\nregister_error().

\n
\n
\nwrite(object)
\n
Writes the object’s contents encoded to the stream.
\n\n
\n
\nwritelines(list)
\n
Writes the concatenated list of strings to the stream (possibly by reusing\nthe write() method).
\n\n
\n
\nreset()
\n

Flushes and resets the codec buffers used for keeping state.

\n

Calling this method should ensure that the data on the output is put into\na clean state that allows appending of new fresh data without having to\nrescan the whole stream to recover state.

\n
\n\n
\n\n

In addition to the above methods, the StreamWriter must also inherit\nall other methods and attributes from the underlying stream.

\n
\n
\n

7.8.1.5. StreamReader Objects

\n

The StreamReader class is a subclass of Codec and defines the\nfollowing methods which every stream reader must define in order to be\ncompatible with the Python codec registry.

\n
\n
\nclass codecs.StreamReader(stream[, errors])
\n

Constructor for a StreamReader instance.

\n

All stream readers must provide this constructor interface. They are free to add\nadditional keyword arguments, but only the ones defined here are used by the\nPython codec registry.

\n

stream must be a file-like object open for reading (binary) data.

\n

The StreamReader may implement different error handling schemes by\nproviding the errors keyword argument. These parameters are defined:

\n
    \n
  • 'strict' Raise ValueError (or a subclass); this is the default.
  • \n
  • 'ignore' Ignore the character and continue with the next.
  • \n
  • 'replace' Replace with a suitable replacement character.
  • \n
\n

The errors argument will be assigned to an attribute of the same name.\nAssigning to this attribute makes it possible to switch between different error\nhandling strategies during the lifetime of the StreamReader object.

\n

The set of allowed values for the errors argument can be extended with\nregister_error().

\n
\n
\nread([size[, chars[, firstline]]])
\n

Decodes data from the stream and returns the resulting object.

\n

chars indicates the number of characters to read from the\nstream. read() will never return more than chars characters, but\nit might return less, if there are not enough characters available.

\n

size indicates the approximate maximum number of bytes to read from the\nstream for decoding purposes. The decoder can modify this setting as\nappropriate. The default value -1 indicates to read and decode as much as\npossible. size is intended to prevent having to decode huge files in\none step.

\n

firstline indicates that it would be sufficient to only return the first\nline, if there are decoding errors on later lines.

\n

The method should use a greedy read strategy meaning that it should read\nas much data as is allowed within the definition of the encoding and the\ngiven size, e.g. if optional encoding endings or state markers are\navailable on the stream, these should be read too.

\n

\nChanged in version 2.4: chars argument added.

\n

\nChanged in version 2.4.2: firstline argument added.

\n
\n\n
\n
\nreadline([size[, keepends]])
\n

Read one line from the input stream and return the decoded data.

\n

size, if given, is passed as size argument to the stream’s\nreadline() method.

\n

If keepends is false line-endings will be stripped from the lines\nreturned.

\n

\nChanged in version 2.4: keepends argument added.

\n
\n\n
\n
\nreadlines([sizehint[, keepends]])
\n

Read all lines available on the input stream and return them as a list of\nlines.

\n

Line-endings are implemented using the codec’s decoder method and are\nincluded in the list entries if keepends is true.

\n

sizehint, if given, is passed as the size argument to the stream’s\nread() method.

\n
\n\n
\n
\nreset()
\n

Resets the codec buffers used for keeping state.

\n

Note that no stream repositioning should take place. This method is\nprimarily intended to be able to recover from decoding errors.

\n
\n\n
\n\n

In addition to the above methods, the StreamReader must also inherit\nall other methods and attributes from the underlying stream.

\n

The next two base classes are included for convenience. They are not needed by\nthe codec registry, but may provide useful in practice.

\n
\n
\n

7.8.1.6. StreamReaderWriter Objects

\n

The StreamReaderWriter allows wrapping streams which work in both read\nand write modes.

\n

The design is such that one can use the factory functions returned by the\nlookup() function to construct the instance.

\n
\n
\nclass codecs.StreamReaderWriter(stream, Reader, Writer, errors)
\n
Creates a StreamReaderWriter instance. stream must be a file-like\nobject. Reader and Writer must be factory functions or classes providing the\nStreamReader and StreamWriter interface resp. Error handling\nis done in the same way as defined for the stream readers and writers.
\n\n

StreamReaderWriter instances define the combined interfaces of\nStreamReader and StreamWriter classes. They inherit all other\nmethods and attributes from the underlying stream.

\n
\n
\n

7.8.1.7. StreamRecoder Objects

\n

The StreamRecoder provide a frontend - backend view of encoding data\nwhich is sometimes useful when dealing with different encoding environments.

\n

The design is such that one can use the factory functions returned by the\nlookup() function to construct the instance.

\n
\n
\nclass codecs.StreamRecoder(stream, encode, decode, Reader, Writer, errors)
\n

Creates a StreamRecoder instance which implements a two-way conversion:\nencode and decode work on the frontend (the input to read() and output\nof write()) while Reader and Writer work on the backend (reading and\nwriting to the stream).

\n

You can use these objects to do transparent direct recodings from e.g. Latin-1\nto UTF-8 and back.

\n

stream must be a file-like object.

\n

encode, decode must adhere to the Codec interface. Reader,\nWriter must be factory functions or classes providing objects of the\nStreamReader and StreamWriter interface respectively.

\n

encode and decode are needed for the frontend translation, Reader and\nWriter for the backend translation. The intermediate format used is\ndetermined by the two sets of codecs, e.g. the Unicode codecs will use Unicode\nas the intermediate encoding.

\n

Error handling is done in the same way as defined for the stream readers and\nwriters.

\n
\n\n

StreamRecoder instances define the combined interfaces of\nStreamReader and StreamWriter classes. They inherit all other\nmethods and attributes from the underlying stream.

\n
\n
\n
\n

7.8.2. Encodings and Unicode

\n

Unicode strings are stored internally as sequences of codepoints (to be precise\nas Py_UNICODE arrays). Depending on the way Python is compiled (either\nvia --enable-unicode=ucs2 or --enable-unicode=ucs4, with the\nformer being the default) Py_UNICODE is either a 16-bit or 32-bit data\ntype. Once a Unicode object is used outside of CPU and memory, CPU endianness\nand how these arrays are stored as bytes become an issue. Transforming a\nunicode object into a sequence of bytes is called encoding and recreating the\nunicode object from the sequence of bytes is known as decoding. There are many\ndifferent methods for how this transformation can be done (these methods are\nalso called encodings). The simplest method is to map the codepoints 0-255 to\nthe bytes 0x0-0xff. This means that a unicode object that contains\ncodepoints above U+00FF can’t be encoded with this method (which is called\n'latin-1' or 'iso-8859-1'). unicode.encode() will raise a\nUnicodeEncodeError that looks like this: UnicodeEncodeError: 'latin-1'\ncodec can't encode character u'\\u1234' in position 3: ordinal not in\nrange(256).

\n

There’s another group of encodings (the so called charmap encodings) that choose\na different subset of all unicode code points and how these codepoints are\nmapped to the bytes 0x0-0xff. To see how this is done simply open\ne.g. encodings/cp1252.py (which is an encoding that is used primarily on\nWindows). There’s a string constant with 256 characters that shows you which\ncharacter is mapped to which byte value.

\n

All of these encodings can only encode 256 of the 1114112 codepoints\ndefined in unicode. A simple and straightforward way that can store each Unicode\ncode point, is to store each codepoint as four consecutive bytes. There are two\npossibilities: store the bytes in big endian or in little endian order. These\ntwo encodings are called UTF-32-BE and UTF-32-LE respectively. Their\ndisadvantage is that if e.g. you use UTF-32-BE on a little endian machine you\nwill always have to swap bytes on encoding and decoding. UTF-32 avoids this\nproblem: bytes will always be in natural endianness. When these bytes are read\nby a CPU with a different endianness, then bytes have to be swapped though. To\nbe able to detect the endianness of a UTF-16 or UTF-32 byte sequence,\nthere’s the so called BOM (“Byte Order Mark”). This is the Unicode character\nU+FEFF. This character can be prepended to every UTF-16 or UTF-32\nbyte sequence. The byte swapped version of this character (0xFFFE) is an\nillegal character that may not appear in a Unicode text. So when the\nfirst character in an UTF-16 or UTF-32 byte sequence\nappears to be a U+FFFE the bytes have to be swapped on decoding.\nUnfortunately the character U+FEFF had a second purpose as\na ZERO WIDTH NO-BREAK SPACE: a character that has no width and doesn’t allow\na word to be split. It can e.g. be used to give hints to a ligature algorithm.\nWith Unicode 4.0 using U+FEFF as a ZERO WIDTH NO-BREAK SPACE has been\ndeprecated (with U+2060 (WORD JOINER) assuming this role). Nevertheless\nUnicode software still must be able to handle U+FEFF in both roles: as a BOM\nit’s a device to determine the storage layout of the encoded bytes, and vanishes\nonce the byte sequence has been decoded into a Unicode string; as a ZERO WIDTH\nNO-BREAK SPACE it’s a normal character that will be decoded like any other.

\n

There’s another encoding that is able to encoding the full range of Unicode\ncharacters: UTF-8. UTF-8 is an 8-bit encoding, which means there are no issues\nwith byte order in UTF-8. Each byte in a UTF-8 byte sequence consists of two\nparts: marker bits (the most significant bits) and payload bits. The marker bits\nare a sequence of zero to four 1 bits followed by a 0 bit. Unicode characters are\nencoded like this (with x being payload bits, which when concatenated give the\nUnicode character):

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
RangeEncoding
U-00000000 ... U-0000007F0xxxxxxx
U-00000080 ... U-000007FF110xxxxx 10xxxxxx
U-00000800 ... U-0000FFFF1110xxxx 10xxxxxx 10xxxxxx
U-00010000 ... U-0010FFFF11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
\n

The least significant bit of the Unicode character is the rightmost x bit.

\n

As UTF-8 is an 8-bit encoding no BOM is required and any U+FEFF character in\nthe decoded Unicode string (even if it’s the first character) is treated as a\nZERO WIDTH NO-BREAK SPACE.

\n

Without external information it’s impossible to reliably determine which\nencoding was used for encoding a Unicode string. Each charmap encoding can\ndecode any random byte sequence. However that’s not possible with UTF-8, as\nUTF-8 byte sequences have a structure that doesn’t allow arbitrary byte\nsequences. To increase the reliability with which a UTF-8 encoding can be\ndetected, Microsoft invented a variant of UTF-8 (that Python 2.5 calls\n"utf-8-sig") for its Notepad program: Before any of the Unicode characters\nis written to the file, a UTF-8 encoded BOM (which looks like this as a byte\nsequence: 0xef, 0xbb, 0xbf) is written. As it’s rather improbable\nthat any charmap encoded file starts with these byte values (which would e.g.\nmap to

\n
\n
\n
LATIN SMALL LETTER I WITH DIAERESIS
\n
RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
\n
INVERTED QUESTION MARK
\n
\n
\n

in iso-8859-1), this increases the probability that a utf-8-sig encoding can be\ncorrectly guessed from the byte sequence. So here the BOM is not used to be able\nto determine the byte order used for generating the byte sequence, but as a\nsignature that helps in guessing the encoding. On encoding the utf-8-sig codec\nwill write 0xef, 0xbb, 0xbf as the first three bytes to the file. On\ndecoding utf-8-sig will skip those three bytes if they appear as the first\nthree bytes in the file. In UTF-8, the use of the BOM is discouraged and\nshould generally be avoided.

\n
\n
\n

7.8.3. Standard Encodings

\n

Python comes with a number of codecs built-in, either implemented as C functions\nor with dictionaries as mapping tables. The following table lists the codecs by\nname, together with a few common aliases, and the languages for which the\nencoding is likely used. Neither the list of aliases nor the list of languages\nis meant to be exhaustive. Notice that spelling alternatives that only differ in\ncase or use a hyphen instead of an underscore are also valid aliases; therefore,\ne.g. 'utf-8' is a valid alias for the 'utf_8' codec.

\n

Many of the character sets support the same languages. They vary in individual\ncharacters (e.g. whether the EURO SIGN is supported or not), and in the\nassignment of characters to code positions. For the European languages in\nparticular, the following variants typically exist:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CodecAliasesLanguages
ascii646, us-asciiEnglish
big5big5-tw, csbig5Traditional Chinese
big5hkscsbig5-hkscs, hkscsTraditional Chinese
cp037IBM037, IBM039English
cp424EBCDIC-CP-HE, IBM424Hebrew
cp437437, IBM437English
cp500EBCDIC-CP-BE, EBCDIC-CP-CH,\nIBM500Western Europe
cp720 Arabic
cp737 Greek
cp775IBM775Baltic languages
cp850850, IBM850Western Europe
cp852852, IBM852Central and Eastern Europe
cp855855, IBM855Bulgarian, Byelorussian,\nMacedonian, Russian, Serbian
cp856 Hebrew
cp857857, IBM857Turkish
cp858858, IBM858Western Europe
cp860860, IBM860Portuguese
cp861861, CP-IS, IBM861Icelandic
cp862862, IBM862Hebrew
cp863863, IBM863Canadian
cp864IBM864Arabic
cp865865, IBM865Danish, Norwegian
cp866866, IBM866Russian
cp869869, CP-GR, IBM869Greek
cp874 Thai
cp875 Greek
cp932932, ms932, mskanji, ms-kanjiJapanese
cp949949, ms949, uhcKorean
cp950950, ms950Traditional Chinese
cp1006 Urdu
cp1026ibm1026Turkish
cp1140ibm1140Western Europe
cp1250windows-1250Central and Eastern Europe
cp1251windows-1251Bulgarian, Byelorussian,\nMacedonian, Russian, Serbian
cp1252windows-1252Western Europe
cp1253windows-1253Greek
cp1254windows-1254Turkish
cp1255windows-1255Hebrew
cp1256windows-1256Arabic
cp1257windows-1257Baltic languages
cp1258windows-1258Vietnamese
euc_jpeucjp, ujis, u-jisJapanese
euc_jis_2004jisx0213, eucjis2004Japanese
euc_jisx0213eucjisx0213Japanese
euc_kreuckr, korean, ksc5601,\nks_c-5601, ks_c-5601-1987,\nksx1001, ks_x-1001Korean
gb2312chinese, csiso58gb231280, euc-\ncn, euccn, eucgb2312-cn,\ngb2312-1980, gb2312-80, iso-\nir-58Simplified Chinese
gbk936, cp936, ms936Unified Chinese
gb18030gb18030-2000Unified Chinese
hzhzgb, hz-gb, hz-gb-2312Simplified Chinese
iso2022_jpcsiso2022jp, iso2022jp,\niso-2022-jpJapanese
iso2022_jp_1iso2022jp-1, iso-2022-jp-1Japanese
iso2022_jp_2iso2022jp-2, iso-2022-jp-2Japanese, Korean, Simplified\nChinese, Western Europe, Greek
iso2022_jp_2004iso2022jp-2004,\niso-2022-jp-2004Japanese
iso2022_jp_3iso2022jp-3, iso-2022-jp-3Japanese
iso2022_jp_extiso2022jp-ext, iso-2022-jp-extJapanese
iso2022_krcsiso2022kr, iso2022kr,\niso-2022-krKorean
latin_1iso-8859-1, iso8859-1, 8859,\ncp819, latin, latin1, L1West Europe
iso8859_2iso-8859-2, latin2, L2Central and Eastern Europe
iso8859_3iso-8859-3, latin3, L3Esperanto, Maltese
iso8859_4iso-8859-4, latin4, L4Baltic languages
iso8859_5iso-8859-5, cyrillicBulgarian, Byelorussian,\nMacedonian, Russian, Serbian
iso8859_6iso-8859-6, arabicArabic
iso8859_7iso-8859-7, greek, greek8Greek
iso8859_8iso-8859-8, hebrewHebrew
iso8859_9iso-8859-9, latin5, L5Turkish
iso8859_10iso-8859-10, latin6, L6Nordic languages
iso8859_13iso-8859-13, latin7, L7Baltic languages
iso8859_14iso-8859-14, latin8, L8Celtic languages
iso8859_15iso-8859-15, latin9, L9Western Europe
iso8859_16iso-8859-16, latin10, L10South-Eastern Europe
johabcp1361, ms1361Korean
koi8_r Russian
koi8_u Ukrainian
mac_cyrillicmaccyrillicBulgarian, Byelorussian,\nMacedonian, Russian, Serbian
mac_greekmacgreekGreek
mac_icelandmacicelandIcelandic
mac_latin2maclatin2, maccentraleuropeCentral and Eastern Europe
mac_romanmacromanWestern Europe
mac_turkishmacturkishTurkish
ptcp154csptcp154, pt154, cp154,\ncyrillic-asianKazakh
shift_jiscsshiftjis, shiftjis, sjis,\ns_jisJapanese
shift_jis_2004shiftjis2004, sjis_2004,\nsjis2004Japanese
shift_jisx0213shiftjisx0213, sjisx0213,\ns_jisx0213Japanese
utf_32U32, utf32all languages
utf_32_beUTF-32BEall languages
utf_32_leUTF-32LEall languages
utf_16U16, utf16all languages
utf_16_beUTF-16BEall languages (BMP only)
utf_16_leUTF-16LEall languages (BMP only)
utf_7U7, unicode-1-1-utf-7all languages
utf_8U8, UTF, utf8all languages
utf_8_sig all languages
\n

A number of codecs are specific to Python, so their codec names have no meaning\noutside Python. Some of them don’t convert from Unicode strings to byte strings,\nbut instead use the property of the Python codecs machinery that any bijective\nfunction with one argument can be considered as an encoding.

\n

For the codecs listed below, the result in the “encoding” direction is always a\nbyte string. The result of the “decoding” direction is listed as operand type in\nthe table.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CodecAliasesOperand typePurpose
base64_codecbase64, base-64byte stringConvert operand to MIME\nbase64
bz2_codecbz2byte stringCompress the operand\nusing bz2
hex_codechexbyte stringConvert operand to\nhexadecimal\nrepresentation, with two\ndigits per byte
idna Unicode stringImplements RFC 3490,\nsee also\nencodings.idna
mbcsdbcsUnicode stringWindows only: Encode\noperand according to the\nANSI codepage (CP_ACP)
palmos Unicode stringEncoding of PalmOS 3.5
punycode Unicode stringImplements RFC 3492
quopri_codecquopri, quoted-printable,\nquotedprintablebyte stringConvert operand to MIME\nquoted printable
raw_unicode_escape Unicode stringProduce a string that is\nsuitable as raw Unicode\nliteral in Python source\ncode
rot_13rot13Unicode stringReturns the Caesar-cypher\nencryption of the operand
string_escape byte stringProduce a string that is\nsuitable as string\nliteral in Python source\ncode
undefined anyRaise an exception for\nall conversions. Can be\nused as the system\nencoding if no automatic\ncoercion between\nbyte and Unicode strings\nis desired.
unicode_escape Unicode stringProduce a string that is\nsuitable as Unicode\nliteral in Python source\ncode
unicode_internal Unicode stringReturn the internal\nrepresentation of the\noperand
uu_codecuubyte stringConvert the operand using\nuuencode
zlib_codeczip, zlibbyte stringCompress the operand\nusing gzip
\n

\nNew in version 2.3: The idna and punycode encodings.

\n
\n
\n

7.8.4. encodings.idna — Internationalized Domain Names in Applications

\n

\nNew in version 2.3.

\n

This module implements RFC 3490 (Internationalized Domain Names in\nApplications) and RFC 3492 (Nameprep: A Stringprep Profile for\nInternationalized Domain Names (IDN)). It builds upon the punycode encoding\nand stringprep.

\n

These RFCs together define a protocol to support non-ASCII characters in domain\nnames. A domain name containing non-ASCII characters (such as\nwww.Alliancefrançaise.nu) is converted into an ASCII-compatible encoding\n(ACE, such as www.xn--alliancefranaise-npb.nu). The ACE form of the domain\nname is then used in all places where arbitrary characters are not allowed by\nthe protocol, such as DNS queries, HTTP Host fields, and so\non. This conversion is carried out in the application; if possible invisible to\nthe user: The application should transparently convert Unicode domain labels to\nIDNA on the wire, and convert back ACE labels to Unicode before presenting them\nto the user.

\n

Python supports this conversion in several ways: the idna codec performs\nconversion between Unicode and ACE, separating an input string into labels\nbased on the separator characters defined in section 3.1 (1) of RFC 3490\nand converting each label to ACE as required, and conversely separating an input\nbyte string into labels based on the . separator and converting any ACE\nlabels found into unicode. Furthermore, the socket module\ntransparently converts Unicode host names to ACE, so that applications need not\nbe concerned about converting host names themselves when they pass them to the\nsocket module. On top of that, modules that have host names as function\nparameters, such as httplib and ftplib, accept Unicode host names\n(httplib then also transparently sends an IDNA hostname in the\nHost field if it sends that field at all).

\n

When receiving host names from the wire (such as in reverse name lookup), no\nautomatic conversion to Unicode is performed: Applications wishing to present\nsuch host names to the user should decode them to Unicode.

\n

The module encodings.idna also implements the nameprep procedure, which\nperforms certain normalizations on host names, to achieve case-insensitivity of\ninternational domain names, and to unify similar characters. The nameprep\nfunctions can be used directly if desired.

\n
\n
\nencodings.idna.nameprep(label)
\n
Return the nameprepped version of label. The implementation currently assumes\nquery strings, so AllowUnassigned is true.
\n\n
\n
\nencodings.idna.ToASCII(label)
\n
Convert a label to ASCII, as specified in RFC 3490. UseSTD3ASCIIRules is\nassumed to be false.
\n\n
\n
\nencodings.idna.ToUnicode(label)
\n
Convert a label to Unicode, as specified in RFC 3490.
\n\n
\n
\n

7.8.5. encodings.utf_8_sig — UTF-8 codec with BOM signature

\n

\nNew in version 2.5.

\n

This module implements a variant of the UTF-8 codec: On encoding a UTF-8 encoded\nBOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this\nis only done once (on the first write to the byte stream). For decoding an\noptional UTF-8 encoded BOM at the start of the data will be skipped.

\n
\n
", "searchableItems": [ { "name": "codecs.backslashreplace_errors", "domId": "codecs_codecs.backslashreplace_errors" }, { "name": "codecs.Codec.decode", "domId": "codecs_codecs.Codec.decode" }, { "name": "codecs.Codec.encode", "domId": "codecs_codecs.Codec.encode" }, { "name": "codecs.EncodedFile", "domId": "codecs_codecs.EncodedFile" }, { "name": "codecs.getdecoder", "domId": "codecs_codecs.getdecoder" }, { "name": "codecs.getencoder", "domId": "codecs_codecs.getencoder" }, { "name": "codecs.getincrementaldecoder", "domId": "codecs_codecs.getincrementaldecoder" }, { "name": "codecs.getincrementalencoder", "domId": "codecs_codecs.getincrementalencoder" }, { "name": "codecs.getreader", "domId": "codecs_codecs.getreader" }, { "name": "codecs.getwriter", "domId": "codecs_codecs.getwriter" }, { "name": "codecs.ignore_errors", "domId": "codecs_codecs.ignore_errors" }, { "name": "codecs.IncrementalDecoder", "domId": "codecs_codecs.IncrementalDecoder" }, { "name": "codecs.IncrementalDecoder.decode", "domId": "codecs_codecs.IncrementalDecoder.decode" }, { "name": "codecs.IncrementalDecoder.reset", "domId": "codecs_codecs.IncrementalDecoder.reset" }, { "name": "codecs.IncrementalEncoder", "domId": "codecs_codecs.IncrementalEncoder" }, { "name": "codecs.IncrementalEncoder.encode", "domId": "codecs_codecs.IncrementalEncoder.encode" }, { "name": "codecs.IncrementalEncoder.reset", "domId": "codecs_codecs.IncrementalEncoder.reset" }, { "name": "codecs.iterdecode", "domId": "codecs_codecs.iterdecode" }, { "name": "codecs.iterencode", "domId": "codecs_codecs.iterencode" }, { "name": "codecs.lookup", "domId": "codecs_codecs.lookup" }, { "name": "codecs.lookup_error", "domId": "codecs_codecs.lookup_error" }, { "name": "codecs.open", "domId": "codecs_codecs.open" }, { "name": "codecs.register", "domId": "codecs_codecs.register" }, { "name": "codecs.register_error", "domId": "codecs_codecs.register_error" }, { "name": "codecs.replace_errors", "domId": "codecs_codecs.replace_errors" }, { "name": "codecs.StreamReader", "domId": "codecs_codecs.StreamReader" }, { "name": "codecs.StreamReader.read", "domId": "codecs_codecs.StreamReader.read" }, { "name": "codecs.StreamReader.readline", "domId": "codecs_codecs.StreamReader.readline" }, { "name": "codecs.StreamReader.readlines", "domId": "codecs_codecs.StreamReader.readlines" }, { "name": "codecs.StreamReader.reset", "domId": "codecs_codecs.StreamReader.reset" }, { "name": "codecs.StreamReaderWriter", "domId": "codecs_codecs.StreamReaderWriter" }, { "name": "codecs.StreamRecoder", "domId": "codecs_codecs.StreamRecoder" }, { "name": "codecs.StreamWriter", "domId": "codecs_codecs.StreamWriter" }, { "name": "codecs.StreamWriter.reset", "domId": "codecs_codecs.StreamWriter.reset" }, { "name": "codecs.StreamWriter.write", "domId": "codecs_codecs.StreamWriter.write" }, { "name": "codecs.StreamWriter.writelines", "domId": "codecs_codecs.StreamWriter.writelines" }, { "name": "codecs.strict_errors", "domId": "codecs_codecs.strict_errors" }, { "name": "codecs.xmlcharrefreplace_errors", "domId": "codecs_codecs.xmlcharrefreplace_errors" }, { "name": "encodings.idna.nameprep", "domId": "codecs_encodings.idna.nameprep" }, { "name": "encodings.idna.ToASCII", "domId": "codecs_encodings.idna.ToASCII" }, { "name": "encodings.idna.ToUnicode", "domId": "codecs_encodings.idna.ToUnicode" } ] }, { "url": "http://docs.python.org/library/fpformat.html", "title": "fpformat", "html": "
\n

7.11. fpformat — Floating point conversions

\n

\nDeprecated since version 2.6: The fpformat module has been removed in Python 3.0.

\n

The fpformat module defines functions for dealing with floating point\nnumbers representations in 100% pure Python.

\n
\n

Note

\n

This module is unnecessary: everything here can be done using the string\ninterpolation operator described in the String Formatting Operations section.

\n
\n

The fpformat module defines the following functions and an exception:

\n
\n
\nfpformat.fix(x, digs)
\n

Format x as [-]ddd.ddd with digs digits after the point and at least one\ndigit before. If digs <= 0, the decimal point is suppressed.

\n

x can be either a number or a string that looks like one. digs is an\ninteger.

\n

Return value is a string.

\n
\n\n
\n
\nfpformat.sci(x, digs)
\n

Format x as [-]d.dddE[+-]ddd with digs digits after the point and\nexactly one digit before. If digs <= 0, one digit is kept and the point is\nsuppressed.

\n

x can be either a real number, or a string that looks like one. digs is an\ninteger.

\n

Return value is a string.

\n
\n\n
\n
\nexception fpformat.NotANumber
\n
Exception raised when a string passed to fix() or sci() as the x\nparameter does not look like a number. This is a subclass of ValueError\nwhen the standard exceptions are strings. The exception value is the improperly\nformatted string that caused the exception to be raised.
\n\n

Example:

\n
>>> import fpformat\n>>> fpformat.fix(1.23, 1)\n'1.2'\n
\n
\n
", "searchableItems": [ { "name": "fpformat.fix", "domId": "fpformat_fpformat.fix" }, { "name": "fpformat.sci", "domId": "fpformat_fpformat.sci" } ] }, { "url": "http://docs.python.org/library/calendar.html", "title": "calendar", "html": "
\n

8.2. calendar — General calendar-related functions

\n

Source code: Lib/calendar.py

\n
\n

This module allows you to output calendars like the Unix cal program,\nand provides additional useful functions related to the calendar. By default,\nthese calendars have Monday as the first day of the week, and Sunday as the last\n(the European convention). Use setfirstweekday() to set the first day of\nthe week to Sunday (6) or to any other weekday. Parameters that specify dates\nare given as integers. For related\nfunctionality, see also the datetime and time modules.

\n

Most of these functions and classes rely on the datetime module which\nuses an idealized calendar, the current Gregorian calendar indefinitely extended\nin both directions. This matches the definition of the “proleptic Gregorian”\ncalendar in Dershowitz and Reingold’s book “Calendrical Calculations”, where\nit’s the base calendar for all computations.

\n
\n
\nclass calendar.Calendar([firstweekday])
\n

Creates a Calendar object. firstweekday is an integer specifying the\nfirst day of the week. 0 is Monday (the default), 6 is Sunday.

\n

A Calendar object provides several methods that can be used for\npreparing the calendar data for formatting. This class doesn’t do any formatting\nitself. This is the job of subclasses.

\n

\nNew in version 2.5.

\n

Calendar instances have the following methods:

\n
\n
\niterweekdays()
\n
Return an iterator for the week day numbers that will be used for one\nweek. The first value from the iterator will be the same as the value of\nthe firstweekday property.
\n\n
\n
\nitermonthdates(year, month)
\n
Return an iterator for the month month (1-12) in the year year. This\niterator will return all days (as datetime.date objects) for the\nmonth and all days before the start of the month or after the end of the\nmonth that are required to get a complete week.
\n\n
\n
\nitermonthdays2(year, month)
\n
Return an iterator for the month month in the year year similar to\nitermonthdates(). Days returned will be tuples consisting of a day\nnumber and a week day number.
\n\n
\n
\nitermonthdays(year, month)
\n
Return an iterator for the month month in the year year similar to\nitermonthdates(). Days returned will simply be day numbers.
\n\n
\n
\nmonthdatescalendar(year, month)
\n
Return a list of the weeks in the month month of the year as full\nweeks. Weeks are lists of seven datetime.date objects.
\n\n
\n
\nmonthdays2calendar(year, month)
\n
Return a list of the weeks in the month month of the year as full\nweeks. Weeks are lists of seven tuples of day numbers and weekday\nnumbers.
\n\n
\n
\nmonthdayscalendar(year, month)
\n
Return a list of the weeks in the month month of the year as full\nweeks. Weeks are lists of seven day numbers.
\n\n
\n
\nyeardatescalendar(year[, width])
\n
Return the data for the specified year ready for formatting. The return\nvalue is a list of month rows. Each month row contains up to width\nmonths (defaulting to 3). Each month contains between 4 and 6 weeks and\neach week contains 1–7 days. Days are datetime.date objects.
\n\n
\n
\nyeardays2calendar(year[, width])
\n
Return the data for the specified year ready for formatting (similar to\nyeardatescalendar()). Entries in the week lists are tuples of day\nnumbers and weekday numbers. Day numbers outside this month are zero.
\n\n
\n
\nyeardayscalendar(year[, width])
\n
Return the data for the specified year ready for formatting (similar to\nyeardatescalendar()). Entries in the week lists are day numbers. Day\nnumbers outside this month are zero.
\n\n
\n\n
\n
\nclass calendar.TextCalendar([firstweekday])
\n

This class can be used to generate plain text calendars.

\n

\nNew in version 2.5.

\n

TextCalendar instances have the following methods:

\n
\n
\nformatmonth(theyear, themonth[, w[, l]])
\n
Return a month’s calendar in a multi-line string. If w is provided, it\nspecifies the width of the date columns, which are centered. If l is\ngiven, it specifies the number of lines that each week will use. Depends\non the first weekday as specified in the constructor or set by the\nsetfirstweekday() method.
\n\n
\n
\nprmonth(theyear, themonth[, w[, l]])
\n
Print a month’s calendar as returned by formatmonth().
\n\n
\n
\nformatyear(theyear[, w[, l[, c[, m]]]])
\n
Return a m-column calendar for an entire year as a multi-line string.\nOptional parameters w, l, and c are for date column width, lines per\nweek, and number of spaces between month columns, respectively. Depends on\nthe first weekday as specified in the constructor or set by the\nsetfirstweekday() method. The earliest year for which a calendar\ncan be generated is platform-dependent.
\n\n
\n
\npryear(theyear[, w[, l[, c[, m]]]])
\n
Print the calendar for an entire year as returned by formatyear().
\n\n
\n\n
\n
\nclass calendar.HTMLCalendar([firstweekday])
\n

This class can be used to generate HTML calendars.

\n

\nNew in version 2.5.

\n

HTMLCalendar instances have the following methods:

\n
\n
\nformatmonth(theyear, themonth[, withyear])
\n
Return a month’s calendar as an HTML table. If withyear is true the year\nwill be included in the header, otherwise just the month name will be\nused.
\n\n
\n
\nformatyear(theyear[, width])
\n
Return a year’s calendar as an HTML table. width (defaulting to 3)\nspecifies the number of months per row.
\n\n
\n
\nformatyearpage(theyear[, width[, css[, encoding]]])
\n
Return a year’s calendar as a complete HTML page. width (defaulting to\n3) specifies the number of months per row. css is the name for the\ncascading style sheet to be used. None can be passed if no style\nsheet should be used. encoding specifies the encoding to be used for the\noutput (defaulting to the system default encoding).
\n\n
\n\n
\n
\nclass calendar.LocaleTextCalendar([firstweekday[, locale]])
\n

This subclass of TextCalendar can be passed a locale name in the\nconstructor and will return month and weekday names in the specified locale.\nIf this locale includes an encoding all strings containing month and weekday\nnames will be returned as unicode.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nclass calendar.LocaleHTMLCalendar([firstweekday[, locale]])
\n

This subclass of HTMLCalendar can be passed a locale name in the\nconstructor and will return month and weekday names in the specified\nlocale. If this locale includes an encoding all strings containing month and\nweekday names will be returned as unicode.

\n

\nNew in version 2.5.

\n
\n\n
\n

Note

\n

The formatweekday() and formatmonthname() methods of these two\nclasses temporarily change the current locale to the given locale. Because\nthe current locale is a process-wide setting, they are not thread-safe.

\n
\n

For simple text calendars this module provides the following functions.

\n
\n
\ncalendar.setfirstweekday(weekday)
\n

Sets the weekday (0 is Monday, 6 is Sunday) to start each week. The\nvalues MONDAY, TUESDAY, WEDNESDAY, THURSDAY,\nFRIDAY, SATURDAY, and SUNDAY are provided for\nconvenience. For example, to set the first weekday to Sunday:

\n
import calendar\ncalendar.setfirstweekday(calendar.SUNDAY)\n
\n
\n

\nNew in version 2.0.

\n
\n\n
\n
\ncalendar.firstweekday()
\n

Returns the current setting for the weekday to start each week.

\n

\nNew in version 2.0.

\n
\n\n
\n
\ncalendar.isleap(year)
\n
Returns True if year is a leap year, otherwise False.
\n\n
\n
\ncalendar.leapdays(y1, y2)
\n

Returns the number of leap years in the range from y1 to y2 (exclusive),\nwhere y1 and y2 are years.

\n

\nChanged in version 2.0: This function didn’t work for ranges spanning a century change in Python\n1.5.2.

\n
\n\n
\n
\ncalendar.weekday(year, month, day)
\n
Returns the day of the week (0 is Monday) for year (1970–...),\nmonth (112), day (131).
\n\n
\n
\ncalendar.weekheader(n)
\n
Return a header containing abbreviated weekday names. n specifies the width in\ncharacters for one weekday.
\n\n
\n
\ncalendar.monthrange(year, month)
\n
Returns weekday of first day of the month and number of days in month, for the\nspecified year and month.
\n\n
\n
\ncalendar.monthcalendar(year, month)
\n
Returns a matrix representing a month’s calendar. Each row represents a week;\ndays outside of the month a represented by zeros. Each week begins with Monday\nunless set by setfirstweekday().
\n\n
\n
\ncalendar.prmonth(theyear, themonth[, w[, l]])
\n
Prints a month’s calendar as returned by month().
\n\n
\n
\ncalendar.month(theyear, themonth[, w[, l]])
\n

Returns a month’s calendar in a multi-line string using the formatmonth()\nof the TextCalendar class.

\n

\nNew in version 2.0.

\n
\n\n
\n
\ncalendar.prcal(year[, w[, l[, c]]])
\n
Prints the calendar for an entire year as returned by calendar().
\n\n
\n
\ncalendar.calendar(year[, w[, l[, c]]])
\n

Returns a 3-column calendar for an entire year as a multi-line string using the\nformatyear() of the TextCalendar class.

\n

\nNew in version 2.0.

\n
\n\n
\n
\ncalendar.timegm(tuple)
\n

An unrelated but handy function that takes a time tuple such as returned by the\ngmtime() function in the time module, and returns the corresponding\nUnix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In\nfact, time.gmtime() and timegm() are each others’ inverse.

\n

\nNew in version 2.0.

\n
\n\n

The calendar module exports the following data attributes:

\n
\n
\ncalendar.day_name
\n
An array that represents the days of the week in the current locale.
\n\n
\n
\ncalendar.day_abbr
\n
An array that represents the abbreviated days of the week in the current locale.
\n\n
\n
\ncalendar.month_name
\n
An array that represents the months of the year in the current locale. This\nfollows normal convention of January being month number 1, so it has a length of\n13 and month_name[0] is the empty string.
\n\n
\n
\ncalendar.month_abbr
\n
An array that represents the abbreviated months of the year in the current\nlocale. This follows normal convention of January being month number 1, so it\nhas a length of 13 and month_abbr[0] is the empty string.
\n\n
\n

See also

\n
\n
Module datetime
\n
Object-oriented interface to dates and times with similar functionality to the\ntime module.
\n
Module time
\n
Low-level time related functions.
\n
\n
\n
", "searchableItems": [ { "name": "calendar.calendar", "domId": "calendar_calendar_calendar.calendar" }, { "name": "calendar.Calendar", "domId": "calendar_calendar.Calendar" }, { "name": "calendar.calendar", "domId": "calendar_calendar_calendar.calendar" }, { "name": "calendar.Calendar", "domId": "calendar_calendar.Calendar" }, { "name": "calendar.Calendar.itermonthdates", "domId": "calendar_calendar.Calendar.itermonthdates" }, { "name": "calendar.Calendar.itermonthdays", "domId": "calendar_calendar.Calendar.itermonthdays" }, { "name": "calendar.Calendar.itermonthdays2", "domId": "calendar_calendar.Calendar.itermonthdays2" }, { "name": "calendar.Calendar.iterweekdays", "domId": "calendar_calendar.Calendar.iterweekdays" }, { "name": "calendar.Calendar.monthdatescalendar", "domId": "calendar_calendar.Calendar.monthdatescalendar" }, { "name": "calendar.Calendar.monthdays2calendar", "domId": "calendar_calendar.Calendar.monthdays2calendar" }, { "name": "calendar.Calendar.monthdayscalendar", "domId": "calendar_calendar.Calendar.monthdayscalendar" }, { "name": "calendar.Calendar.yeardatescalendar", "domId": "calendar_calendar.Calendar.yeardatescalendar" }, { "name": "calendar.Calendar.yeardays2calendar", "domId": "calendar_calendar.Calendar.yeardays2calendar" }, { "name": "calendar.Calendar.yeardayscalendar", "domId": "calendar_calendar.Calendar.yeardayscalendar" }, { "name": "calendar.firstweekday", "domId": "calendar_calendar.firstweekday" }, { "name": "calendar.HTMLCalendar", "domId": "calendar_calendar.HTMLCalendar" }, { "name": "calendar.HTMLCalendar.formatmonth", "domId": "calendar_calendar.HTMLCalendar.formatmonth" }, { "name": "calendar.HTMLCalendar.formatyear", "domId": "calendar_calendar.HTMLCalendar.formatyear" }, { "name": "calendar.HTMLCalendar.formatyearpage", "domId": "calendar_calendar.HTMLCalendar.formatyearpage" }, { "name": "calendar.isleap", "domId": "calendar_calendar.isleap" }, { "name": "calendar.leapdays", "domId": "calendar_calendar.leapdays" }, { "name": "calendar.LocaleHTMLCalendar", "domId": "calendar_calendar.LocaleHTMLCalendar" }, { "name": "calendar.LocaleTextCalendar", "domId": "calendar_calendar.LocaleTextCalendar" }, { "name": "calendar.month", "domId": "calendar_calendar.month" }, { "name": "calendar.monthcalendar", "domId": "calendar_calendar.monthcalendar" }, { "name": "calendar.monthrange", "domId": "calendar_calendar.monthrange" }, { "name": "calendar.prcal", "domId": "calendar_calendar.prcal" }, { "name": "calendar.prmonth", "domId": "calendar_calendar.prmonth" }, { "name": "calendar.setfirstweekday", "domId": "calendar_calendar.setfirstweekday" }, { "name": "calendar.TextCalendar", "domId": "calendar_calendar.TextCalendar" }, { "name": "calendar.TextCalendar.formatmonth", "domId": "calendar_calendar.TextCalendar.formatmonth" }, { "name": "calendar.TextCalendar.formatyear", "domId": "calendar_calendar.TextCalendar.formatyear" }, { "name": "calendar.TextCalendar.prmonth", "domId": "calendar_calendar.TextCalendar.prmonth" }, { "name": "calendar.TextCalendar.pryear", "domId": "calendar_calendar.TextCalendar.pryear" }, { "name": "calendar.timegm", "domId": "calendar_calendar.timegm" }, { "name": "calendar.weekday", "domId": "calendar_calendar.weekday" }, { "name": "calendar.weekheader", "domId": "calendar_calendar.weekheader" } ] }, { "url": "http://docs.python.org/library/heapq.html", "title": "heapq", "html": "
\n

8.4. heapq — Heap queue algorithm

\n

\nNew in version 2.3.

\n

Source code: Lib/heapq.py

\n
\n

This module provides an implementation of the heap queue algorithm, also known\nas the priority queue algorithm.

\n

Heaps are binary trees for which every parent node has a value less than or\nequal to any of its children. This implementation uses arrays for which\nheap[k] <= heap[2*k+1] and heap[k] <= heap[2*k+2] for all k, counting\nelements from zero. For the sake of comparison, non-existing elements are\nconsidered to be infinite. The interesting property of a heap is that its\nsmallest element is always the root, heap[0].

\n

The API below differs from textbook heap algorithms in two aspects: (a) We use\nzero-based indexing. This makes the relationship between the index for a node\nand the indexes for its children slightly less obvious, but is more suitable\nsince Python uses zero-based indexing. (b) Our pop method returns the smallest\nitem, not the largest (called a “min heap” in textbooks; a “max heap” is more\ncommon in texts because of its suitability for in-place sorting).

\n

These two make it possible to view the heap as a regular Python list without\nsurprises: heap[0] is the smallest item, and heap.sort() maintains the\nheap invariant!

\n

To create a heap, use a list initialized to [], or you can transform a\npopulated list into a heap via function heapify().

\n

The following functions are provided:

\n
\n
\nheapq.heappush(heap, item)
\n
Push the value item onto the heap, maintaining the heap invariant.
\n\n
\n
\nheapq.heappop(heap)
\n
Pop and return the smallest item from the heap, maintaining the heap\ninvariant. If the heap is empty, IndexError is raised.
\n\n
\n
\nheapq.heappushpop(heap, item)
\n

Push item on the heap, then pop and return the smallest item from the\nheap. The combined action runs more efficiently than heappush()\nfollowed by a separate call to heappop().

\n

\nNew in version 2.6.

\n
\n\n
\n
\nheapq.heapify(x)
\n
Transform list x into a heap, in-place, in linear time.
\n\n
\n
\nheapq.heapreplace(heap, item)
\n

Pop and return the smallest item from the heap, and also push the new item.\nThe heap size doesn’t change. If the heap is empty, IndexError is raised.

\n

This one step operation is more efficient than a heappop() followed by\nheappush() and can be more appropriate when using a fixed-size heap.\nThe pop/push combination always returns an element from the heap and replaces\nit with item.

\n

The value returned may be larger than the item added. If that isn’t\ndesired, consider using heappushpop() instead. Its push/pop\ncombination returns the smaller of the two values, leaving the larger value\non the heap.

\n
\n\n

The module also offers three general purpose functions based on heaps.

\n
\n
\nheapq.merge(*iterables)
\n

Merge multiple sorted inputs into a single sorted output (for example, merge\ntimestamped entries from multiple log files). Returns an iterator\nover the sorted values.

\n

Similar to sorted(itertools.chain(*iterables)) but returns an iterable, does\nnot pull the data into memory all at once, and assumes that each of the input\nstreams is already sorted (smallest to largest).

\n

\nNew in version 2.6.

\n
\n\n
\n
\nheapq.nlargest(n, iterable[, key])
\n

Return a list with the n largest elements from the dataset defined by\niterable. key, if provided, specifies a function of one argument that is\nused to extract a comparison key from each element in the iterable:\nkey=str.lower Equivalent to: sorted(iterable, key=key,\nreverse=True)[:n]

\n

\nNew in version 2.4.

\n

\nChanged in version 2.5: Added the optional key argument.

\n
\n\n
\n
\nheapq.nsmallest(n, iterable[, key])
\n

Return a list with the n smallest elements from the dataset defined by\niterable. key, if provided, specifies a function of one argument that is\nused to extract a comparison key from each element in the iterable:\nkey=str.lower Equivalent to: sorted(iterable, key=key)[:n]

\n

\nNew in version 2.4.

\n

\nChanged in version 2.5: Added the optional key argument.

\n
\n\n

The latter two functions perform best for smaller values of n. For larger\nvalues, it is more efficient to use the sorted() function. Also, when\nn==1, it is more efficient to use the built-in min() and max()\nfunctions.

\n
\n

8.4.1. Basic Examples

\n

A heapsort can be implemented by\npushing all values onto a heap and then popping off the smallest values one at a\ntime:

\n
>>> def heapsort(iterable):\n...     'Equivalent to sorted(iterable)'\n...     h = []\n...     for value in iterable:\n...         heappush(h, value)\n...     return [heappop(h) for i in range(len(h))]\n...\n>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])\n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n
\n
\n

Heap elements can be tuples. This is useful for assigning comparison values\n(such as task priorities) alongside the main record being tracked:

\n
>>> h = []\n>>> heappush(h, (5, 'write code'))\n>>> heappush(h, (7, 'release product'))\n>>> heappush(h, (1, 'write spec'))\n>>> heappush(h, (3, 'create tests'))\n>>> heappop(h)\n(1, 'write spec')\n
\n
\n
\n
\n

8.4.2. Priority Queue Implementation Notes

\n

A priority queue is common use\nfor a heap, and it presents several implementation challenges:

\n\n

A solution to the first two challenges is to store entries as 3-element list\nincluding the priority, an entry count, and the task. The entry count serves as\na tie-breaker so that two tasks with the same priority are returned in the order\nthey were added. And since no two entry counts are the same, the tuple\ncomparison will never attempt to directly compare two tasks.

\n

The remaining challenges revolve around finding a pending task and making\nchanges to its priority or removing it entirely. Finding a task can be done\nwith a dictionary pointing to an entry in the queue.

\n

Removing the entry or changing its priority is more difficult because it would\nbreak the heap structure invariants. So, a possible solution is to mark the\nexisting entry as removed and add a new entry with the revised priority:

\n
pq = []                         # list of entries arranged in a heap\nentry_finder = {}               # mapping of tasks to entries\nREMOVED = '<removed-task>'      # placeholder for a removed task\ncounter = itertools.count()     # unique sequence count\n\ndef add_task(task, priority=0):\n    'Add a new task or update the priority of an existing task'\n    if task in entry_finder:\n        remove_task(task)\n    count = next(counter)\n    entry = [priority, count, task]\n    entry_finder[task] = entry\n    heappush(pq, entry)\n\ndef remove_task(task):\n    'Mark an existing task as REMOVED.  Raise KeyError if not found.'\n    entry = entry_finder.pop(task)\n    entry[-1] = REMOVED\n\ndef pop_task():\n    'Remove and return the lowest priority task. Raise KeyError if empty.'\n    while pq:\n        priority, count, task = heappop(pq)\n        if task is not REMOVED:\n            del entry_finder[task]\n            return task\n    raise KeyError('pop from an empty priority queue')\n
\n
\n
\n
\n

8.4.3. Theory

\n

Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all\nk, counting elements from 0. For the sake of comparison, non-existing\nelements are considered to be infinite. The interesting property of a heap is\nthat a[0] is always its smallest element.

\n

The strange invariant above is meant to be an efficient memory representation\nfor a tournament. The numbers below are k, not a[k]:

\n
                               0\n\n              1                                 2\n\n      3               4                5               6\n\n  7       8       9       10      11      12      13      14\n\n15 16   17 18   19 20   21 22   23 24   25 26   27 28   29 30
\n
\n

In the tree above, each cell k is topping 2*k+1 and 2*k+2. In an usual\nbinary tournament we see in sports, each cell is the winner over the two cells\nit tops, and we can trace the winner down the tree to see all opponents s/he\nhad. However, in many computer applications of such tournaments, we do not need\nto trace the history of a winner. To be more memory efficient, when a winner is\npromoted, we try to replace it by something else at a lower level, and the rule\nbecomes that a cell and the two cells it tops contain three different items, but\nthe top cell “wins” over the two topped cells.

\n

If this heap invariant is protected at all time, index 0 is clearly the overall\nwinner. The simplest algorithmic way to remove it and find the “next” winner is\nto move some loser (let’s say cell 30 in the diagram above) into the 0 position,\nand then percolate this new 0 down the tree, exchanging values, until the\ninvariant is re-established. This is clearly logarithmic on the total number of\nitems in the tree. By iterating over all items, you get an O(n log n) sort.

\n

A nice feature of this sort is that you can efficiently insert new items while\nthe sort is going on, provided that the inserted items are not “better” than the\nlast 0’th element you extracted. This is especially useful in simulation\ncontexts, where the tree holds all incoming events, and the “win” condition\nmeans the smallest scheduled time. When an event schedule other events for\nexecution, they are scheduled into the future, so they can easily go into the\nheap. So, a heap is a good structure for implementing schedulers (this is what\nI used for my MIDI sequencer :-).

\n

Various structures for implementing schedulers have been extensively studied,\nand heaps are good for this, as they are reasonably speedy, the speed is almost\nconstant, and the worst case is not much different than the average case.\nHowever, there are other representations which are more efficient overall, yet\nthe worst cases might be terrible.

\n

Heaps are also very useful in big disk sorts. You most probably all know that a\nbig sort implies producing “runs” (which are pre-sorted sequences, which size is\nusually related to the amount of CPU memory), followed by a merging passes for\nthese runs, which merging is often very cleverly organised [1]. It is very\nimportant that the initial sort produces the longest runs possible. Tournaments\nare a good way to that. If, using all the memory available to hold a\ntournament, you replace and percolate items that happen to fit the current run,\nyou’ll produce runs which are twice the size of the memory for random input, and\nmuch better for input fuzzily ordered.

\n

Moreover, if you output the 0’th item on disk and get an input which may not fit\nin the current tournament (because the value “wins” over the last output value),\nit cannot fit in the heap, so the size of the heap decreases. The freed memory\ncould be cleverly reused immediately for progressively building a second heap,\nwhich grows at exactly the same rate the first heap is melting. When the first\nheap completely vanishes, you switch heaps and start a new run. Clever and\nquite effective!

\n

In a word, heaps are useful memory structures to know. I use them in a few\napplications, and I think it is good to keep a ‘heap’ module around. :-)

\n

Footnotes

\n\n\n\n\n\n
[1]The disk balancing algorithms which are current, nowadays, are more annoying\nthan clever, and this is a consequence of the seeking capabilities of the disks.\nOn devices which cannot seek, like big tape drives, the story was quite\ndifferent, and one had to be very clever to ensure (far in advance) that each\ntape movement will be the most effective possible (that is, will best\nparticipate at “progressing” the merge). Some tapes were even able to read\nbackwards, and this was also used to avoid the rewinding time. Believe me, real\ngood tape sorts were quite spectacular to watch! From all times, sorting has\nalways been a Great Art! :-)
\n
\n
", "searchableItems": [ { "name": "heapq.heapify", "domId": "heapq_heapq.heapify" }, { "name": "heapq.heappop", "domId": "heapq_heapq.heappop" }, { "name": "heapq.heappush", "domId": "heapq_heapq.heappush" }, { "name": "heapq.heappushpop", "domId": "heapq_heapq.heappushpop" }, { "name": "heapq.heapreplace", "domId": "heapq_heapq.heapreplace" }, { "name": "heapq.merge", "domId": "heapq_heapq.merge" }, { "name": "heapq.nlargest", "domId": "heapq_heapq.nlargest" }, { "name": "heapq.nsmallest", "domId": "heapq_heapq.nsmallest" } ] }, { "url": "http://docs.python.org/library/bisect.html", "title": "bisect", "html": "
\n

8.5. bisect — Array bisection algorithm

\n

\nNew in version 2.1.

\n

Source code: Lib/bisect.py

\n
\n

This module provides support for maintaining a list in sorted order without\nhaving to sort the list after each insertion. For long lists of items with\nexpensive comparison operations, this can be an improvement over the more common\napproach. The module is called bisect because it uses a basic bisection\nalgorithm to do its work. The source code may be most useful as a working\nexample of the algorithm (the boundary conditions are already right!).

\n

The following functions are provided:

\n
\n
\nbisect.bisect_left(a, x, lo=0, hi=len(a))
\n

Locate the insertion point for x in a to maintain sorted order.\nThe parameters lo and hi may be used to specify a subset of the list\nwhich should be considered; by default the entire list is used. If x is\nalready present in a, the insertion point will be before (to the left of)\nany existing entries. The return value is suitable for use as the first\nparameter to list.insert() assuming that a is already sorted.

\n

The returned insertion point i partitions the array a into two halves so\nthat all(val < x for val in a[lo:i]) for the left side and\nall(val >= x for val in a[i:hi]) for the right side.

\n
\n\n
\n
\nbisect.bisect_right(a, x, lo=0, hi=len(a))
\n
\nbisect.bisect(a, x, lo=0, hi=len(a))
\n

Similar to bisect_left(), but returns an insertion point which comes\nafter (to the right of) any existing entries of x in a.

\n

The returned insertion point i partitions the array a into two halves so\nthat all(val <= x for val in a[lo:i]) for the left side and\nall(val > x for val in a[i:hi]) for the right side.

\n
\n\n
\n
\nbisect.insort_left(a, x, lo=0, hi=len(a))
\n
Insert x in a in sorted order. This is equivalent to\na.insert(bisect.bisect_left(a, x, lo, hi), x) assuming that a is\nalready sorted. Keep in mind that the O(log n) search is dominated by\nthe slow O(n) insertion step.
\n\n
\n
\nbisect.insort_right(a, x, lo=0, hi=len(a))
\n
\nbisect.insort(a, x, lo=0, hi=len(a))
\n
Similar to insort_left(), but inserting x in a after any existing\nentries of x.
\n\n
\n

See also

\n

SortedCollection recipe that uses\nbisect to build a full-featured collection class with straight-forward search\nmethods and support for a key-function. The keys are precomputed to save\nunnecessary calls to the key function during searches.

\n
\n
\n

8.5.1. Searching Sorted Lists

\n

The above bisect() functions are useful for finding insertion points but\ncan be tricky or awkward to use for common searching tasks. The following five\nfunctions show how to transform them into the standard lookups for sorted\nlists:

\n
def index(a, x):\n    'Locate the leftmost value exactly equal to x'\n    i = bisect_left(a, x)\n    if i != len(a) and a[i] == x:\n        return i\n    raise ValueError\n\ndef find_lt(a, x):\n    'Find rightmost value less than x'\n    i = bisect_left(a, x)\n    if i:\n        return a[i-1]\n    raise ValueError\n\ndef find_le(a, x):\n    'Find rightmost value less than or equal to x'\n    i = bisect_right(a, x)\n    if i:\n        return a[i-1]\n    raise ValueError\n\ndef find_gt(a, x):\n    'Find leftmost value greater than x'\n    i = bisect_right(a, x)\n    if i != len(a):\n        return a[i]\n    raise ValueError\n\ndef find_ge(a, x):\n    'Find leftmost item greater than or equal to x'\n    i = bisect_left(a, x)\n    if i != len(a):\n        return a[i]\n    raise ValueError\n
\n
\n
\n
\n

8.5.2. Other Examples

\n

The bisect() function can be useful for numeric table lookups. This\nexample uses bisect() to look up a letter grade for an exam score (say)\nbased on a set of ordered numeric breakpoints: 90 and up is an ‘A’, 80 to 89 is\na ‘B’, and so on:

\n
>>> def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):\n...     i = bisect(breakpoints, score)\n...     return grades[i]\n...\n>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]\n['F', 'A', 'C', 'C', 'B', 'A', 'A']\n
\n
\n

Unlike the sorted() function, it does not make sense for the bisect()\nfunctions to have key or reversed arguments because that would lead to an\ninefficient design (successive calls to bisect functions would not “remember”\nall of the previous key lookups).

\n

Instead, it is better to search a list of precomputed keys to find the index\nof the record in question:

\n
>>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]\n>>> data.sort(key=lambda r: r[1])\n>>> keys = [r[1] for r in data]         # precomputed list of keys\n>>> data[bisect_left(keys, 0)]\n('black', 0)\n>>> data[bisect_left(keys, 1)]\n('blue', 1)\n>>> data[bisect_left(keys, 5)]\n('red', 5)\n>>> data[bisect_left(keys, 8)]\n('yellow', 8)\n
\n
\n
\n
", "searchableItems": [ { "name": "bisect.bisect_left", "domId": "bisect_bisect.bisect_left" }, { "name": "bisect.bisect_right", "domId": "bisect_bisect.bisect_right" }, { "name": "bisect.insort_left", "domId": "bisect_bisect.insort_left" }, { "name": "bisect.insort_right", "domId": "bisect_bisect.insort_right" } ] }, { "url": "http://docs.python.org/library/array.html", "title": "array", "html": "
\n

8.6. array — Efficient arrays of numeric values

\n

This module defines an object type which can compactly represent an array of\nbasic values: characters, integers, floating point numbers. Arrays are sequence\ntypes and behave very much like lists, except that the type of objects stored in\nthem is constrained. The type is specified at object creation time by using a\ntype code, which is a single character. The following type codes are\ndefined:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Type codeC TypePython TypeMinimum size in bytes
'c'charcharacter1
'b'signed charint1
'B'unsigned charint1
'u'Py_UNICODEUnicode character2 (see note)
'h'signed shortint2
'H'unsigned shortint2
'i'signed intint2
'I'unsigned intlong2
'l'signed longint4
'L'unsigned longlong4
'f'floatfloat4
'd'doublefloat8
\n
\n

Note

\n

The 'u' typecode corresponds to Python’s unicode character. On narrow\nUnicode builds this is 2-bytes, on wide builds this is 4-bytes.

\n
\n

The actual representation of values is determined by the machine architecture\n(strictly speaking, by the C implementation). The actual size can be accessed\nthrough the itemsize attribute. The values stored for 'L' and\n'I' items will be represented as Python long integers when retrieved,\nbecause Python’s plain integer type cannot represent the full range of C’s\nunsigned (long) integers.

\n

The module defines the following type:

\n
\n
\nclass array.array(typecode[, initializer])
\n

A new array whose items are restricted by typecode, and initialized\nfrom the optional initializer value, which must be a list, string, or iterable\nover elements of the appropriate type.

\n

\nChanged in version 2.4: Formerly, only lists or strings were accepted.

\n

If given a list or string, the initializer is passed to the new array’s\nfromlist(), fromstring(), or fromunicode() method (see below)\nto add initial items to the array. Otherwise, the iterable initializer is\npassed to the extend() method.

\n
\n\n
\n
\narray.ArrayType
\n
Obsolete alias for array.
\n\n

Array objects support the ordinary sequence operations of indexing, slicing,\nconcatenation, and multiplication. When using slice assignment, the assigned\nvalue must be an array object with the same type code; in all other cases,\nTypeError is raised. Array objects also implement the buffer interface,\nand may be used wherever buffer objects are supported.

\n

The following data items and methods are also supported:

\n
\n
\narray.typecode
\n
The typecode character used to create the array.
\n\n
\n
\narray.itemsize
\n
The length in bytes of one array item in the internal representation.
\n\n
\n
\narray.append(x)
\n
Append a new item with value x to the end of the array.
\n\n
\n
\narray.buffer_info()
\n

Return a tuple (address, length) giving the current memory address and the\nlength in elements of the buffer used to hold array’s contents. The size of the\nmemory buffer in bytes can be computed as array.buffer_info()[1] *\narray.itemsize. This is occasionally useful when working with low-level (and\ninherently unsafe) I/O interfaces that require memory addresses, such as certain\nioctl() operations. The returned numbers are valid as long as the array\nexists and no length-changing operations are applied to it.

\n
\n

Note

\n

When using array objects from code written in C or C++ (the only way to\neffectively make use of this information), it makes more sense to use the buffer\ninterface supported by array objects. This method is maintained for backward\ncompatibility and should be avoided in new code. The buffer interface is\ndocumented in Buffers and Memoryview Objects.

\n
\n
\n\n
\n
\narray.byteswap()
\n
“Byteswap” all items of the array. This is only supported for values which are\n1, 2, 4, or 8 bytes in size; for other types of values, RuntimeError is\nraised. It is useful when reading data from a file written on a machine with a\ndifferent byte order.
\n\n
\n
\narray.count(x)
\n
Return the number of occurrences of x in the array.
\n\n
\n
\narray.extend(iterable)
\n

Append items from iterable to the end of the array. If iterable is another\narray, it must have exactly the same type code; if not, TypeError will\nbe raised. If iterable is not an array, it must be iterable and its elements\nmust be the right type to be appended to the array.

\n

\nChanged in version 2.4: Formerly, the argument could only be another array.

\n
\n\n
\n
\narray.fromfile(f, n)
\n
Read n items (as machine values) from the file object f and append them to\nthe end of the array. If less than n items are available, EOFError is\nraised, but the items that were available are still inserted into the array.\nf must be a real built-in file object; something else with a read()\nmethod won’t do.
\n\n
\n
\narray.fromlist(list)
\n
Append items from the list. This is equivalent to for x in list:\na.append(x) except that if there is a type error, the array is unchanged.
\n\n
\n
\narray.fromstring(s)
\n
Appends items from the string, interpreting the string as an array of machine\nvalues (as if it had been read from a file using the fromfile() method).
\n\n
\n
\narray.fromunicode(s)
\n
Extends this array with data from the given unicode string. The array must\nbe a type 'u' array; otherwise a ValueError is raised. Use\narray.fromstring(unicodestring.encode(enc)) to append Unicode data to an\narray of some other type.
\n\n
\n
\narray.index(x)
\n
Return the smallest i such that i is the index of the first occurrence of\nx in the array.
\n\n
\n
\narray.insert(i, x)
\n
Insert a new item with value x in the array before position i. Negative\nvalues are treated as being relative to the end of the array.
\n\n
\n
\narray.pop([i])
\n
Removes the item with the index i from the array and returns it. The optional\nargument defaults to -1, so that by default the last item is removed and\nreturned.
\n\n
\n
\narray.read(f, n)
\n

\nDeprecated since version 1.5.1: Use the fromfile() method.

\n

Read n items (as machine values) from the file object f and append them to\nthe end of the array. If less than n items are available, EOFError is\nraised, but the items that were available are still inserted into the array.\nf must be a real built-in file object; something else with a read()\nmethod won’t do.

\n
\n\n
\n
\narray.remove(x)
\n
Remove the first occurrence of x from the array.
\n\n
\n
\narray.reverse()
\n
Reverse the order of the items in the array.
\n\n
\n
\narray.tofile(f)
\n
Write all items (as machine values) to the file object f.
\n\n
\n
\narray.tolist()
\n
Convert the array to an ordinary list with the same items.
\n\n
\n
\narray.tostring()
\n
Convert the array to an array of machine values and return the string\nrepresentation (the same sequence of bytes that would be written to a file by\nthe tofile() method.)
\n\n
\n
\narray.tounicode()
\n
Convert the array to a unicode string. The array must be a type 'u' array;\notherwise a ValueError is raised. Use array.tostring().decode(enc) to\nobtain a unicode string from an array of some other type.
\n\n
\n
\narray.write(f)
\n

\nDeprecated since version 1.5.1: Use the tofile() method.

\n

Write all items (as machine values) to the file object f.

\n
\n\n

When an array object is printed or converted to a string, it is represented as\narray(typecode, initializer). The initializer is omitted if the array is\nempty, otherwise it is a string if the typecode is 'c', otherwise it is a\nlist of numbers. The string is guaranteed to be able to be converted back to an\narray with the same type and value using eval(), so long as the\narray() function has been imported using from array import array.\nExamples:

\n
array('l')\narray('c', 'hello world')\narray('u', u'hello \\u2641')\narray('l', [1, 2, 3, 4, 5])\narray('d', [1.0, 2.0, 3.14])\n
\n
\n
\n

See also

\n
\n
Module struct
\n
Packing and unpacking of heterogeneous binary data.
\n
Module xdrlib
\n
Packing and unpacking of External Data Representation (XDR) data as used in some\nremote procedure call systems.
\n
The Numerical Python Manual
\n
The Numeric Python extension (NumPy) defines another array type; see\nhttp://numpy.sourceforge.net/ for further information about Numerical Python.\n(A PDF version of the NumPy manual is available at\nhttp://numpy.sourceforge.net/numdoc/numdoc.pdf).
\n
\n
\n
", "searchableItems": [ { "name": "array.array", "domId": "array_array.array" }, { "name": "array.array.append", "domId": "array_array.array.append" }, { "name": "array.array.buffer_info", "domId": "array_array.array.buffer_info" }, { "name": "array.array.byteswap", "domId": "array_array.array.byteswap" }, { "name": "array.array.count", "domId": "array_array.array.count" }, { "name": "array.array.extend", "domId": "array_array.array.extend" }, { "name": "array.array.fromfile", "domId": "array_array.array.fromfile" }, { "name": "array.array.fromlist", "domId": "array_array.array.fromlist" }, { "name": "array.array.fromstring", "domId": "array_array.array.fromstring" }, { "name": "array.array.fromunicode", "domId": "array_array.array.fromunicode" }, { "name": "array.array.index", "domId": "array_array.array.index" }, { "name": "array.array.insert", "domId": "array_array.array.insert" }, { "name": "array.array.pop", "domId": "array_array.array.pop" }, { "name": "array.array.read", "domId": "array_array.array.read" }, { "name": "array.array.remove", "domId": "array_array.array.remove" }, { "name": "array.array.reverse", "domId": "array_array.array.reverse" }, { "name": "array.array.tofile", "domId": "array_array.array.tofile" }, { "name": "array.array.tolist", "domId": "array_array.array.tolist" }, { "name": "array.array.tostring", "domId": "array_array.array.tostring" }, { "name": "array.array.tounicode", "domId": "array_array.array.tounicode" }, { "name": "array.array.write", "domId": "array_array.array.write" } ] }, { "url": "http://docs.python.org/library/datetime.html", "title": "datetime", "html": "
\n

8.1. datetime — Basic date and time types

\n

\nNew in version 2.3.

\n

The datetime module supplies classes for manipulating dates and times in\nboth simple and complex ways. While date and time arithmetic is supported, the\nfocus of the implementation is on efficient attribute extraction for output\nformatting and manipulation. For related\nfunctionality, see also the time and calendar modules.

\n

There are two kinds of date and time objects: “naive” and “aware”. This\ndistinction refers to whether the object has any notion of time zone, daylight\nsaving time, or other kind of algorithmic or political time adjustment. Whether\na naive datetime object represents Coordinated Universal Time (UTC),\nlocal time, or time in some other timezone is purely up to the program, just\nlike it’s up to the program whether a particular number represents metres,\nmiles, or mass. Naive datetime objects are easy to understand and to\nwork with, at the cost of ignoring some aspects of reality.

\n

For applications requiring more, datetime and time objects\nhave an optional time zone information attribute, tzinfo, that can be\nset to an instance of a subclass of the abstract tzinfo class. These\ntzinfo objects capture information about the offset from UTC time, the\ntime zone name, and whether Daylight Saving Time is in effect. Note that no\nconcrete tzinfo classes are supplied by the datetime module.\nSupporting timezones at whatever level of detail is required is up to the\napplication. The rules for time adjustment across the world are more political\nthan rational, and there is no standard suitable for every application.

\n

The datetime module exports the following constants:

\n
\n
\ndatetime.MINYEAR
\n
The smallest year number allowed in a date or datetime object.\nMINYEAR is 1.
\n\n
\n
\ndatetime.MAXYEAR
\n
The largest year number allowed in a date or datetime object.\nMAXYEAR is 9999.
\n\n
\n

See also

\n
\n
Module calendar
\n
General calendar related functions.
\n
Module time
\n
Time access and conversions.
\n
\n
\n
\n

8.1.1. Available Types

\n
\n
\nclass datetime.date
\n
An idealized naive date, assuming the current Gregorian calendar always was, and\nalways will be, in effect. Attributes: year, month, and\nday.
\n\n
\n
\nclass datetime.time
\n
An idealized time, independent of any particular day, assuming that every day\nhas exactly 24*60*60 seconds (there is no notion of “leap seconds” here).\nAttributes: hour, minute, second, microsecond,\nand tzinfo.
\n\n
\n
\nclass datetime.datetime
\n
A combination of a date and a time. Attributes: year, month,\nday, hour, minute, second, microsecond,\nand tzinfo.
\n\n
\n
\nclass datetime.timedelta
\n
A duration expressing the difference between two date, time,\nor datetime instances to microsecond resolution.
\n\n
\n
\nclass datetime.tzinfo
\n
An abstract base class for time zone information objects. These are used by the\ndatetime and time classes to provide a customizable notion of\ntime adjustment (for example, to account for time zone and/or daylight saving\ntime).
\n\n

Objects of these types are immutable.

\n

Objects of the date type are always naive.

\n

An object d of type time or datetime may be naive or aware.\nd is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does\nnot return None. If d.tzinfo is None, or if d.tzinfo is not\nNone but d.tzinfo.utcoffset(d) returns None, d is naive.

\n

The distinction between naive and aware doesn’t apply to timedelta\nobjects.

\n

Subclass relationships:

\n
object\n    timedelta\n    tzinfo\n    time\n    date\n        datetime
\n
\n
\n
\n

8.1.2. timedelta Objects

\n

A timedelta object represents a duration, the difference between two\ndates or times.

\n
\n
\nclass datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
\n

All arguments are optional and default to 0. Arguments may be ints, longs,\nor floats, and may be positive or negative.

\n

Only days, seconds and microseconds are stored internally. Arguments are\nconverted to those units:

\n
    \n
  • A millisecond is converted to 1000 microseconds.
  • \n
  • A minute is converted to 60 seconds.
  • \n
  • An hour is converted to 3600 seconds.
  • \n
  • A week is converted to 7 days.
  • \n
\n

and days, seconds and microseconds are then normalized so that the\nrepresentation is unique, with

\n
    \n
  • 0 <= microseconds < 1000000
  • \n
  • 0 <= seconds < 3600*24 (the number of seconds in one day)
  • \n
  • -999999999 <= days <= 999999999
  • \n
\n

If any argument is a float and there are fractional microseconds, the fractional\nmicroseconds left over from all arguments are combined and their sum is rounded\nto the nearest microsecond. If no argument is a float, the conversion and\nnormalization processes are exact (no information is lost).

\n

If the normalized value of days lies outside the indicated range,\nOverflowError is raised.

\n

Note that normalization of negative values may be surprising at first. For\nexample,

\n
>>> from datetime import timedelta\n>>> d = timedelta(microseconds=-1)\n>>> (d.days, d.seconds, d.microseconds)\n(-1, 86399, 999999)\n
\n
\n
\n\n

Class attributes are:

\n
\n
\ntimedelta.min
\n
The most negative timedelta object, timedelta(-999999999).
\n\n
\n
\ntimedelta.max
\n
The most positive timedelta object, timedelta(days=999999999,\nhours=23, minutes=59, seconds=59, microseconds=999999).
\n\n
\n
\ntimedelta.resolution
\n
The smallest possible difference between non-equal timedelta objects,\ntimedelta(microseconds=1).
\n\n

Note that, because of normalization, timedelta.max > -timedelta.min.\n-timedelta.max is not representable as a timedelta object.

\n

Instance attributes (read-only):

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeValue
daysBetween -999999999 and 999999999 inclusive
secondsBetween 0 and 86399 inclusive
microsecondsBetween 0 and 999999 inclusive
\n

Supported operations:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResult
t1 = t2 + t3Sum of t2 and t3. Afterwards t1-t2 ==\nt3 and t1-t3 == t2 are true. (1)
t1 = t2 - t3Difference of t2 and t3. Afterwards t1\n== t2 - t3 and t2 == t1 + t3 are\ntrue. (1)
t1 = t2 * i or t1 = i * t2Delta multiplied by an integer or long.\nAfterwards t1 // i == t2 is true,\nprovided i != 0.
 In general, t1 * i == t1 * (i-1) + t1\nis true. (1)
t1 = t2 // iThe floor is computed and the remainder (if\nany) is thrown away. (3)
+t1Returns a timedelta object with the\nsame value. (2)
-t1equivalent to timedelta(-t1.days, -t1.seconds,\n-t1.microseconds), and to t1* -1. (1)(4)
abs(t)equivalent to +t when t.days >= 0, and\nto -t when t.days < 0. (2)
str(t)Returns a string in the form\n[D day[s], ][H]H:MM:SS[.UUUUUU], where D\nis negative for negative t. (5)
repr(t)Returns a string in the form\ndatetime.timedelta(D[, S[, U]]), where D\nis negative for negative t. (5)
\n

Notes:

\n
    \n
  1. This is exact, but may overflow.

    \n
  2. \n
  3. This is exact, and cannot overflow.

    \n
  4. \n
  5. Division by 0 raises ZeroDivisionError.

    \n
  6. \n
  7. -timedelta.max is not representable as a timedelta object.

    \n
  8. \n
  9. String representations of timedelta objects are normalized\nsimilarly to their internal representation. This leads to somewhat\nunusual results for negative timedeltas. For example:

    \n
    >>> timedelta(hours=-5)\ndatetime.timedelta(-1, 68400)\n>>> print(_)\n-1 day, 19:00:00\n
    \n
    \n
  10. \n
\n

In addition to the operations listed above timedelta objects support\ncertain additions and subtractions with date and datetime\nobjects (see below).

\n

Comparisons of timedelta objects are supported with the\ntimedelta object representing the smaller duration considered to be the\nsmaller timedelta. In order to stop mixed-type comparisons from falling back to\nthe default comparison by object address, when a timedelta object is\ncompared to an object of a different type, TypeError is raised unless the\ncomparison is == or !=. The latter cases return False or\nTrue, respectively.

\n

timedelta objects are hashable (usable as dictionary keys), support\nefficient pickling, and in Boolean contexts, a timedelta object is\nconsidered to be true if and only if it isn’t equal to timedelta(0).

\n

Instance methods:

\n
\n
\ntimedelta.total_seconds()
\n

Return the total number of seconds contained in the duration.\nEquivalent to (td.microseconds + (td.seconds + td.days * 24 *\n3600) * 10**6) / 10**6 computed with true division enabled.

\n

Note that for very large time intervals (greater than 270 years on\nmost platforms) this method will lose microsecond accuracy.

\n

\nNew in version 2.7.

\n
\n\n

Example usage:

\n
>>> from datetime import timedelta\n>>> year = timedelta(days=365)\n>>> another_year = timedelta(weeks=40, days=84, hours=23,\n...                          minutes=50, seconds=600)  # adds up to 365 days\n>>> year.total_seconds()\n31536000.0\n>>> year == another_year\nTrue\n>>> ten_years = 10 * year\n>>> ten_years, ten_years.days // 365\n(datetime.timedelta(3650), 10)\n>>> nine_years = ten_years - year\n>>> nine_years, nine_years.days // 365\n(datetime.timedelta(3285), 9)\n>>> three_years = nine_years // 3;\n>>> three_years, three_years.days // 365\n(datetime.timedelta(1095), 3)\n>>> abs(three_years - ten_years) == 2 * three_years + year\nTrue\n
\n
\n
\n
\n

8.1.3. date Objects

\n

A date object represents a date (year, month and day) in an idealized\ncalendar, the current Gregorian calendar indefinitely extended in both\ndirections. January 1 of year 1 is called day number 1, January 2 of year 1 is\ncalled day number 2, and so on. This matches the definition of the “proleptic\nGregorian” calendar in Dershowitz and Reingold’s book Calendrical Calculations,\nwhere it’s the base calendar for all computations. See the book for algorithms\nfor converting between proleptic Gregorian ordinals and many other calendar\nsystems.

\n
\n
\nclass datetime.date(year, month, day)
\n

All arguments are required. Arguments may be ints or longs, in the following\nranges:

\n
    \n
  • MINYEAR <= year <= MAXYEAR
  • \n
  • 1 <= month <= 12
  • \n
  • 1 <= day <= number of days in the given month and year
  • \n
\n

If an argument outside those ranges is given, ValueError is raised.

\n
\n\n

Other constructors, all class methods:

\n
\n
\nclassmethod date.today()
\n
Return the current local date. This is equivalent to\ndate.fromtimestamp(time.time()).
\n\n
\n
\nclassmethod date.fromtimestamp(timestamp)
\n
Return the local date corresponding to the POSIX timestamp, such as is returned\nby time.time(). This may raise ValueError, if the timestamp is out\nof the range of values supported by the platform C localtime() function.\nIt’s common for this to be restricted to years from 1970 through 2038. Note\nthat on non-POSIX systems that include leap seconds in their notion of a\ntimestamp, leap seconds are ignored by fromtimestamp().
\n\n
\n
\nclassmethod date.fromordinal(ordinal)
\n
Return the date corresponding to the proleptic Gregorian ordinal, where January\n1 of year 1 has ordinal 1. ValueError is raised unless 1 <= ordinal <=\ndate.max.toordinal(). For any date d, date.fromordinal(d.toordinal()) ==\nd.
\n\n

Class attributes:

\n
\n
\ndate.min
\n
The earliest representable date, date(MINYEAR, 1, 1).
\n\n
\n
\ndate.max
\n
The latest representable date, date(MAXYEAR, 12, 31).
\n\n
\n
\ndate.resolution
\n
The smallest possible difference between non-equal date objects,\ntimedelta(days=1).
\n\n

Instance attributes (read-only):

\n
\n
\ndate.year
\n
Between MINYEAR and MAXYEAR inclusive.
\n\n
\n
\ndate.month
\n
Between 1 and 12 inclusive.
\n\n
\n
\ndate.day
\n
Between 1 and the number of days in the given month of the given year.
\n\n

Supported operations:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResult
date2 = date1 + timedeltadate2 is timedelta.days days removed\nfrom date1. (1)
date2 = date1 - timedeltaComputes date2 such that date2 +\ntimedelta == date1. (2)
timedelta = date1 - date2(3)
date1 < date2date1 is considered less than date2 when\ndate1 precedes date2 in time. (4)
\n

Notes:

\n
    \n
  1. date2 is moved forward in time if timedelta.days > 0, or backward if\ntimedelta.days < 0. Afterward date2 - date1 == timedelta.days.\ntimedelta.seconds and timedelta.microseconds are ignored.\nOverflowError is raised if date2.year would be smaller than\nMINYEAR or larger than MAXYEAR.
  2. \n
  3. This isn’t quite equivalent to date1 + (-timedelta), because -timedelta in\nisolation can overflow in cases where date1 - timedelta does not.\ntimedelta.seconds and timedelta.microseconds are ignored.
  4. \n
  5. This is exact, and cannot overflow. timedelta.seconds and\ntimedelta.microseconds are 0, and date2 + timedelta == date1 after.
  6. \n
  7. In other words, date1 < date2 if and only if date1.toordinal() <\ndate2.toordinal(). In order to stop comparison from falling back to the\ndefault scheme of comparing object addresses, date comparison normally raises\nTypeError if the other comparand isn’t also a date object.\nHowever, NotImplemented is returned instead if the other comparand has a\ntimetuple() attribute. This hook gives other kinds of date objects a\nchance at implementing mixed-type comparison. If not, when a date\nobject is compared to an object of a different type, TypeError is raised\nunless the comparison is == or !=. The latter cases return\nFalse or True, respectively.
  8. \n
\n

Dates can be used as dictionary keys. In Boolean contexts, all date\nobjects are considered to be true.

\n

Instance methods:

\n
\n
\ndate.replace(year, month, day)
\n
Return a date with the same value, except for those parameters given new\nvalues by whichever keyword arguments are specified. For example, if d ==\ndate(2002, 12, 31), then d.replace(day=26) == date(2002, 12, 26).
\n\n
\n
\ndate.timetuple()
\n
Return a time.struct_time such as returned by time.localtime().\nThe hours, minutes and seconds are 0, and the DST flag is -1. d.timetuple()\nis equivalent to time.struct_time((d.year, d.month, d.day, 0, 0, 0,\nd.weekday(), yday, -1)), where yday = d.toordinal() - date(d.year, 1,\n1).toordinal() + 1 is the day number within the current year starting with\n1 for January 1st.
\n\n
\n
\ndate.toordinal()
\n
Return the proleptic Gregorian ordinal of the date, where January 1 of year 1\nhas ordinal 1. For any date object d,\ndate.fromordinal(d.toordinal()) == d.
\n\n
\n
\ndate.weekday()
\n
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.\nFor example, date(2002, 12, 4).weekday() == 2, a Wednesday. See also\nisoweekday().
\n\n
\n
\ndate.isoweekday()
\n
Return the day of the week as an integer, where Monday is 1 and Sunday is 7.\nFor example, date(2002, 12, 4).isoweekday() == 3, a Wednesday. See also\nweekday(), isocalendar().
\n\n
\n
\ndate.isocalendar()
\n

Return a 3-tuple, (ISO year, ISO week number, ISO weekday).

\n

The ISO calendar is a widely used variant of the Gregorian calendar. See\nhttp://www.phys.uu.nl/~vgent/calendar/isocalendar.htm for a good\nexplanation.

\n

The ISO year consists of 52 or 53 full weeks, and where a week starts on a\nMonday and ends on a Sunday. The first week of an ISO year is the first\n(Gregorian) calendar week of a year containing a Thursday. This is called week\nnumber 1, and the ISO year of that Thursday is the same as its Gregorian year.

\n

For example, 2004 begins on a Thursday, so the first week of ISO year 2004\nbegins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so that\ndate(2003, 12, 29).isocalendar() == (2004, 1, 1) and date(2004, 1,\n4).isocalendar() == (2004, 1, 7).

\n
\n\n
\n
\ndate.isoformat()
\n
Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For\nexample, date(2002, 12, 4).isoformat() == '2002-12-04'.
\n\n
\n
\ndate.__str__()
\n
For a date d, str(d) is equivalent to d.isoformat().
\n\n
\n
\ndate.ctime()
\n
Return a string representing the date, for example date(2002, 12,\n4).ctime() == 'Wed Dec 4 00:00:00 2002'. d.ctime() is equivalent to\ntime.ctime(time.mktime(d.timetuple())) on platforms where the native C\nctime() function (which time.ctime() invokes, but which\ndate.ctime() does not invoke) conforms to the C standard.
\n\n
\n
\ndate.strftime(format)
\n
Return a string representing the date, controlled by an explicit format string.\nFormat codes referring to hours, minutes or seconds will see 0 values. See\nsection strftime() and strptime() Behavior.
\n\n

Example of counting days to an event:

\n
>>> import time\n>>> from datetime import date\n>>> today = date.today()\n>>> today\ndatetime.date(2007, 12, 5)\n>>> today == date.fromtimestamp(time.time())\nTrue\n>>> my_birthday = date(today.year, 6, 24)\n>>> if my_birthday < today:\n...     my_birthday = my_birthday.replace(year=today.year + 1)\n>>> my_birthday\ndatetime.date(2008, 6, 24)\n>>> time_to_birthday = abs(my_birthday - today)\n>>> time_to_birthday.days\n202\n
\n
\n

Example of working with date:

\n
>>> from datetime import date\n>>> d = date.fromordinal(730920) # 730920th day after 1. 1. 0001\n>>> d\ndatetime.date(2002, 3, 11)\n>>> t = d.timetuple()\n>>> for i in t:     \n...     print i\n2002                # year\n3                   # month\n11                  # day\n0\n0\n0\n0                   # weekday (0 = Monday)\n70                  # 70th day in the year\n-1\n>>> ic = d.isocalendar()\n>>> for i in ic:    \n...     print i\n2002                # ISO year\n11                  # ISO week number\n1                   # ISO day number ( 1 = Monday )\n>>> d.isoformat()\n'2002-03-11'\n>>> d.strftime("%d/%m/%y")\n'11/03/02'\n>>> d.strftime("%A %d. %B %Y")\n'Monday 11. March 2002'\n
\n
\n
\n
\n

8.1.4. datetime Objects

\n

A datetime object is a single object containing all the information\nfrom a date object and a time object. Like a date\nobject, datetime assumes the current Gregorian calendar extended in\nboth directions; like a time object, datetime assumes there are exactly\n3600*24 seconds in every day.

\n

Constructor:

\n
\n
\nclass datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
\n

The year, month and day arguments are required. tzinfo may be None, or an\ninstance of a tzinfo subclass. The remaining arguments may be ints or\nlongs, in the following ranges:

\n
    \n
  • MINYEAR <= year <= MAXYEAR
  • \n
  • 1 <= month <= 12
  • \n
  • 1 <= day <= number of days in the given month and year
  • \n
  • 0 <= hour < 24
  • \n
  • 0 <= minute < 60
  • \n
  • 0 <= second < 60
  • \n
  • 0 <= microsecond < 1000000
  • \n
\n

If an argument outside those ranges is given, ValueError is raised.

\n
\n\n

Other constructors, all class methods:

\n
\n
\nclassmethod datetime.today()
\n
Return the current local datetime, with tzinfo None. This is\nequivalent to datetime.fromtimestamp(time.time()). See also now(),\nfromtimestamp().
\n\n
\n
\nclassmethod datetime.now([tz])
\n

Return the current local date and time. If optional argument tz is None\nor not specified, this is like today(), but, if possible, supplies more\nprecision than can be gotten from going through a time.time() timestamp\n(for example, this may be possible on platforms supplying the C\ngettimeofday() function).

\n

Else tz must be an instance of a class tzinfo subclass, and the\ncurrent date and time are converted to tz‘s time zone. In this case the\nresult is equivalent to tz.fromutc(datetime.utcnow().replace(tzinfo=tz)).\nSee also today(), utcnow().

\n
\n\n
\n
\nclassmethod datetime.utcnow()
\n
Return the current UTC date and time, with tzinfo None. This is like\nnow(), but returns the current UTC date and time, as a naive\ndatetime object. See also now().
\n\n
\n
\nclassmethod datetime.fromtimestamp(timestamp[, tz])
\n

Return the local date and time corresponding to the POSIX timestamp, such as is\nreturned by time.time(). If optional argument tz is None or not\nspecified, the timestamp is converted to the platform’s local date and time, and\nthe returned datetime object is naive.

\n

Else tz must be an instance of a class tzinfo subclass, and the\ntimestamp is converted to tz‘s time zone. In this case the result is\nequivalent to\ntz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz)).

\n

fromtimestamp() may raise ValueError, if the timestamp is out of\nthe range of values supported by the platform C localtime() or\ngmtime() functions. It’s common for this to be restricted to years in\n1970 through 2038. Note that on non-POSIX systems that include leap seconds in\ntheir notion of a timestamp, leap seconds are ignored by fromtimestamp(),\nand then it’s possible to have two timestamps differing by a second that yield\nidentical datetime objects. See also utcfromtimestamp().

\n
\n\n
\n
\nclassmethod datetime.utcfromtimestamp(timestamp)
\n
Return the UTC datetime corresponding to the POSIX timestamp, with\ntzinfo None. This may raise ValueError, if the timestamp is\nout of the range of values supported by the platform C gmtime() function.\nIt’s common for this to be restricted to years in 1970 through 2038. See also\nfromtimestamp().
\n\n
\n
\nclassmethod datetime.fromordinal(ordinal)
\n
Return the datetime corresponding to the proleptic Gregorian ordinal,\nwhere January 1 of year 1 has ordinal 1. ValueError is raised unless 1\n<= ordinal <= datetime.max.toordinal(). The hour, minute, second and\nmicrosecond of the result are all 0, and tzinfo is None.
\n\n
\n
\nclassmethod datetime.combine(date, time)
\n
Return a new datetime object whose date components are equal to the\ngiven date object’s, and whose time components and tzinfo\nattributes are equal to the given time object’s. For any\ndatetime object d,\nd == datetime.combine(d.date(), d.timetz()). If date is a\ndatetime object, its time components and tzinfo attributes\nare ignored.
\n\n
\n
\nclassmethod datetime.strptime(date_string, format)
\n

Return a datetime corresponding to date_string, parsed according to\nformat. This is equivalent to datetime(*(time.strptime(date_string,\nformat)[0:6])). ValueError is raised if the date_string and format\ncan’t be parsed by time.strptime() or if it returns a value which isn’t a\ntime tuple. See section strftime() and strptime() Behavior.

\n

\nNew in version 2.5.

\n
\n\n

Class attributes:

\n
\n
\ndatetime.min
\n
The earliest representable datetime, datetime(MINYEAR, 1, 1,\ntzinfo=None).
\n\n
\n
\ndatetime.max
\n
The latest representable datetime, datetime(MAXYEAR, 12, 31, 23, 59,\n59, 999999, tzinfo=None).
\n\n
\n
\ndatetime.resolution
\n
The smallest possible difference between non-equal datetime objects,\ntimedelta(microseconds=1).
\n\n

Instance attributes (read-only):

\n
\n
\ndatetime.year
\n
Between MINYEAR and MAXYEAR inclusive.
\n\n
\n
\ndatetime.month
\n
Between 1 and 12 inclusive.
\n\n
\n
\ndatetime.day
\n
Between 1 and the number of days in the given month of the given year.
\n\n
\n
\ndatetime.hour
\n
In range(24).
\n\n
\n
\ndatetime.minute
\n
In range(60).
\n\n
\n
\ndatetime.second
\n
In range(60).
\n\n
\n
\ndatetime.microsecond
\n
In range(1000000).
\n\n
\n
\ndatetime.tzinfo
\n
The object passed as the tzinfo argument to the datetime constructor,\nor None if none was passed.
\n\n

Supported operations:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResult
datetime2 = datetime1 + timedelta(1)
datetime2 = datetime1 - timedelta(2)
timedelta = datetime1 - datetime2(3)
datetime1 < datetime2Compares datetime to\ndatetime. (4)
\n
    \n
  1. datetime2 is a duration of timedelta removed from datetime1, moving forward in\ntime if timedelta.days > 0, or backward if timedelta.days < 0. The\nresult has the same tzinfo attribute as the input datetime, and\ndatetime2 - datetime1 == timedelta after. OverflowError is raised if\ndatetime2.year would be smaller than MINYEAR or larger than\nMAXYEAR. Note that no time zone adjustments are done even if the\ninput is an aware object.

    \n
  2. \n
  3. Computes the datetime2 such that datetime2 + timedelta == datetime1. As for\naddition, the result has the same tzinfo attribute as the input\ndatetime, and no time zone adjustments are done even if the input is aware.\nThis isn’t quite equivalent to datetime1 + (-timedelta), because -timedelta\nin isolation can overflow in cases where datetime1 - timedelta does not.

    \n
  4. \n
  5. Subtraction of a datetime from a datetime is defined only if\nboth operands are naive, or if both are aware. If one is aware and the other is\nnaive, TypeError is raised.

    \n

    If both are naive, or both are aware and have the same tzinfo attribute,\nthe tzinfo attributes are ignored, and the result is a timedelta\nobject t such that datetime2 + t == datetime1. No time zone adjustments\nare done in this case.

    \n

    If both are aware and have different tzinfo attributes, a-b acts\nas if a and b were first converted to naive UTC datetimes first. The\nresult is (a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None)\n- b.utcoffset()) except that the implementation never overflows.

    \n
  6. \n
  7. datetime1 is considered less than datetime2 when datetime1 precedes\ndatetime2 in time.

    \n

    If one comparand is naive and the other is aware, TypeError is raised.\nIf both comparands are aware, and have the same tzinfo attribute, the\ncommon tzinfo attribute is ignored and the base datetimes are\ncompared. If both comparands are aware and have different tzinfo\nattributes, the comparands are first adjusted by subtracting their UTC\noffsets (obtained from self.utcoffset()).

    \n
    \n

    Note

    \n

    In order to stop comparison from falling back to the default scheme of comparing\nobject addresses, datetime comparison normally raises TypeError if the\nother comparand isn’t also a datetime object. However,\nNotImplemented is returned instead if the other comparand has a\ntimetuple() attribute. This hook gives other kinds of date objects a\nchance at implementing mixed-type comparison. If not, when a datetime\nobject is compared to an object of a different type, TypeError is raised\nunless the comparison is == or !=. The latter cases return\nFalse or True, respectively.

    \n
    \n
  8. \n
\n

datetime objects can be used as dictionary keys. In Boolean contexts,\nall datetime objects are considered to be true.

\n

Instance methods:

\n
\n
\ndatetime.date()
\n
Return date object with same year, month and day.
\n\n
\n
\ndatetime.time()
\n
Return time object with same hour, minute, second and microsecond.\ntzinfo is None. See also method timetz().
\n\n
\n
\ndatetime.timetz()
\n
Return time object with same hour, minute, second, microsecond, and\ntzinfo attributes. See also method time().
\n\n
\n
\ndatetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
\n
Return a datetime with the same attributes, except for those attributes given\nnew values by whichever keyword arguments are specified. Note that\ntzinfo=None can be specified to create a naive datetime from an aware\ndatetime with no conversion of date and time data.
\n\n
\n
\ndatetime.astimezone(tz)
\n

Return a datetime object with new tzinfo attribute tz,\nadjusting the date and time data so the result is the same UTC time as\nself, but in tz‘s local time.

\n

tz must be an instance of a tzinfo subclass, and its\nutcoffset() and dst() methods must not return None. self must\nbe aware (self.tzinfo must not be None, and self.utcoffset() must\nnot return None).

\n

If self.tzinfo is tz, self.astimezone(tz) is equal to self: no\nadjustment of date or time data is performed. Else the result is local\ntime in time zone tz, representing the same UTC time as self: after\nastz = dt.astimezone(tz), astz - astz.utcoffset() will usually have\nthe same date and time data as dt - dt.utcoffset(). The discussion\nof class tzinfo explains the cases at Daylight Saving Time transition\nboundaries where this cannot be achieved (an issue only if tz models both\nstandard and daylight time).

\n

If you merely want to attach a time zone object tz to a datetime dt without\nadjustment of date and time data, use dt.replace(tzinfo=tz). If you\nmerely want to remove the time zone object from an aware datetime dt without\nconversion of date and time data, use dt.replace(tzinfo=None).

\n

Note that the default tzinfo.fromutc() method can be overridden in a\ntzinfo subclass to affect the result returned by astimezone().\nIgnoring error cases, astimezone() acts like:

\n
def astimezone(self, tz):\n    if self.tzinfo is tz:\n        return self\n    # Convert self to UTC, and attach the new time zone object.\n    utc = (self - self.utcoffset()).replace(tzinfo=tz)\n    # Convert from UTC to tz's local time.\n    return tz.fromutc(utc)\n
\n
\n
\n\n
\n
\ndatetime.utcoffset()
\n
If tzinfo is None, returns None, else returns\nself.tzinfo.utcoffset(self), and raises an exception if the latter doesn’t\nreturn None, or a timedelta object representing a whole number of\nminutes with magnitude less than one day.
\n\n
\n
\ndatetime.dst()
\n
If tzinfo is None, returns None, else returns\nself.tzinfo.dst(self), and raises an exception if the latter doesn’t return\nNone, or a timedelta object representing a whole number of minutes\nwith magnitude less than one day.
\n\n
\n
\ndatetime.tzname()
\n
If tzinfo is None, returns None, else returns\nself.tzinfo.tzname(self), raises an exception if the latter doesn’t return\nNone or a string object,
\n\n
\n
\ndatetime.timetuple()
\n
Return a time.struct_time such as returned by time.localtime().\nd.timetuple() is equivalent to time.struct_time((d.year, d.month, d.day,\nd.hour, d.minute, d.second, d.weekday(), yday, dst)), where yday =\nd.toordinal() - date(d.year, 1, 1).toordinal() + 1 is the day number within\nthe current year starting with 1 for January 1st. The tm_isdst flag\nof the result is set according to the dst() method: tzinfo is\nNone or dst() returns None, tm_isdst is set to -1;\nelse if dst() returns a non-zero value, tm_isdst is set to 1;\nelse tm_isdst is set to 0.
\n\n
\n
\ndatetime.utctimetuple()
\n

If datetime instance d is naive, this is the same as\nd.timetuple() except that tm_isdst is forced to 0 regardless of what\nd.dst() returns. DST is never in effect for a UTC time.

\n

If d is aware, d is normalized to UTC time, by subtracting\nd.utcoffset(), and a time.struct_time for the normalized time is\nreturned. tm_isdst is forced to 0. Note that the result’s\ntm_year member may be MINYEAR-1 or MAXYEAR+1, if\nd.year was MINYEAR or MAXYEAR and UTC adjustment spills over a year\nboundary.

\n
\n\n
\n
\ndatetime.toordinal()
\n
Return the proleptic Gregorian ordinal of the date. The same as\nself.date().toordinal().
\n\n
\n
\ndatetime.weekday()
\n
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.\nThe same as self.date().weekday(). See also isoweekday().
\n\n
\n
\ndatetime.isoweekday()
\n
Return the day of the week as an integer, where Monday is 1 and Sunday is 7.\nThe same as self.date().isoweekday(). See also weekday(),\nisocalendar().
\n\n
\n
\ndatetime.isocalendar()
\n
Return a 3-tuple, (ISO year, ISO week number, ISO weekday). The same as\nself.date().isocalendar().
\n\n
\n
\ndatetime.isoformat([sep])
\n

Return a string representing the date and time in ISO 8601 format,\nYYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0,\nYYYY-MM-DDTHH:MM:SS

\n

If utcoffset() does not return None, a 6-character string is\nappended, giving the UTC offset in (signed) hours and minutes:\nYYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if microsecond is 0\nYYYY-MM-DDTHH:MM:SS+HH:MM

\n

The optional argument sep (default 'T') is a one-character separator,\nplaced between the date and time portions of the result. For example,

\n
>>> from datetime import tzinfo, timedelta, datetime\n>>> class TZ(tzinfo):\n...     def utcoffset(self, dt): return timedelta(minutes=-399)\n...\n>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')\n'2002-12-25 00:00:00-06:39'\n
\n
\n
\n\n
\n
\ndatetime.__str__()
\n
For a datetime instance d, str(d) is equivalent to\nd.isoformat(' ').
\n\n
\n
\ndatetime.ctime()
\n
Return a string representing the date and time, for example datetime(2002, 12,\n4, 20, 30, 40).ctime() == 'Wed Dec  4 20:30:40 2002'. d.ctime() is\nequivalent to time.ctime(time.mktime(d.timetuple())) on platforms where the\nnative C ctime() function (which time.ctime() invokes, but which\ndatetime.ctime() does not invoke) conforms to the C standard.
\n\n
\n
\ndatetime.strftime(format)
\n
Return a string representing the date and time, controlled by an explicit format\nstring. See section strftime() and strptime() Behavior.
\n\n

Examples of working with datetime objects:

\n
>>> from datetime import datetime, date, time\n>>> # Using datetime.combine()\n>>> d = date(2005, 7, 14)\n>>> t = time(12, 30)\n>>> datetime.combine(d, t)\ndatetime.datetime(2005, 7, 14, 12, 30)\n>>> # Using datetime.now() or datetime.utcnow()\n>>> datetime.now()   \ndatetime.datetime(2007, 12, 6, 16, 29, 43, 79043)   # GMT +1\n>>> datetime.utcnow()   \ndatetime.datetime(2007, 12, 6, 15, 29, 43, 79060)\n>>> # Using datetime.strptime()\n>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")\n>>> dt\ndatetime.datetime(2006, 11, 21, 16, 30)\n>>> # Using datetime.timetuple() to get tuple of all attributes\n>>> tt = dt.timetuple()\n>>> for it in tt:   \n...     print it\n...\n2006    # year\n11      # month\n21      # day\n16      # hour\n30      # minute\n0       # second\n1       # weekday (0 = Monday)\n325     # number of days since 1st January\n-1      # dst - method tzinfo.dst() returned None\n>>> # Date in ISO format\n>>> ic = dt.isocalendar()\n>>> for it in ic:   \n...     print it\n...\n2006    # ISO year\n47      # ISO week\n2       # ISO weekday\n>>> # Formatting datetime\n>>> dt.strftime("%A, %d. %B %Y %I:%M%p")\n'Tuesday, 21. November 2006 04:30PM'\n
\n
\n

Using datetime with tzinfo:

\n
>>> from datetime import timedelta, datetime, tzinfo\n>>> class GMT1(tzinfo):\n...     def __init__(self):         # DST starts last Sunday in March\n...         d = datetime(dt.year, 4, 1)   # ends last Sunday in October\n...         self.dston = d - timedelta(days=d.weekday() + 1)\n...         d = datetime(dt.year, 11, 1)\n...         self.dstoff = d - timedelta(days=d.weekday() + 1)\n...     def utcoffset(self, dt):\n...         return timedelta(hours=1) + self.dst(dt)\n...     def dst(self, dt):\n...         if self.dston <=  dt.replace(tzinfo=None) < self.dstoff:\n...             return timedelta(hours=1)\n...         else:\n...             return timedelta(0)\n...     def tzname(self,dt):\n...          return "GMT +1"\n...\n>>> class GMT2(tzinfo):\n...     def __init__(self):\n...         d = datetime(dt.year, 4, 1)\n...         self.dston = d - timedelta(days=d.weekday() + 1)\n...         d = datetime(dt.year, 11, 1)\n...         self.dstoff = d - timedelta(days=d.weekday() + 1)\n...     def utcoffset(self, dt):\n...         return timedelta(hours=1) + self.dst(dt)\n...     def dst(self, dt):\n...         if self.dston <=  dt.replace(tzinfo=None) < self.dstoff:\n...             return timedelta(hours=2)\n...         else:\n...             return timedelta(0)\n...     def tzname(self,dt):\n...         return "GMT +2"\n...\n>>> gmt1 = GMT1()\n>>> # Daylight Saving Time\n>>> dt1 = datetime(2006, 11, 21, 16, 30, tzinfo=gmt1)\n>>> dt1.dst()\ndatetime.timedelta(0)\n>>> dt1.utcoffset()\ndatetime.timedelta(0, 3600)\n>>> dt2 = datetime(2006, 6, 14, 13, 0, tzinfo=gmt1)\n>>> dt2.dst()\ndatetime.timedelta(0, 3600)\n>>> dt2.utcoffset()\ndatetime.timedelta(0, 7200)\n>>> # Convert datetime to another time zone\n>>> dt3 = dt2.astimezone(GMT2())\n>>> dt3     # doctest: +ELLIPSIS\ndatetime.datetime(2006, 6, 14, 14, 0, tzinfo=<GMT2 object at 0x...>)\n>>> dt2     # doctest: +ELLIPSIS\ndatetime.datetime(2006, 6, 14, 13, 0, tzinfo=<GMT1 object at 0x...>)\n>>> dt2.utctimetuple() == dt3.utctimetuple()\nTrue\n
\n
\n
\n
\n

8.1.5. time Objects

\n

A time object represents a (local) time of day, independent of any particular\nday, and subject to adjustment via a tzinfo object.

\n
\n
\nclass datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])
\n

All arguments are optional. tzinfo may be None, or an instance of a\ntzinfo subclass. The remaining arguments may be ints or longs, in the\nfollowing ranges:

\n
    \n
  • 0 <= hour < 24
  • \n
  • 0 <= minute < 60
  • \n
  • 0 <= second < 60
  • \n
  • 0 <= microsecond < 1000000.
  • \n
\n

If an argument outside those ranges is given, ValueError is raised. All\ndefault to 0 except tzinfo, which defaults to None.

\n
\n\n

Class attributes:

\n
\n
\ntime.min
\n
The earliest representable time, time(0, 0, 0, 0).
\n\n
\n
\ntime.max
\n
The latest representable time, time(23, 59, 59, 999999).
\n\n
\n
\ntime.resolution
\n
The smallest possible difference between non-equal time objects,\ntimedelta(microseconds=1), although note that arithmetic on\ntime objects is not supported.
\n\n

Instance attributes (read-only):

\n
\n
\ntime.hour
\n
In range(24).
\n\n
\n
\ntime.minute
\n
In range(60).
\n\n
\n
\ntime.second
\n
In range(60).
\n\n
\n
\ntime.microsecond
\n
In range(1000000).
\n\n
\n
\ntime.tzinfo
\n
The object passed as the tzinfo argument to the time constructor, or\nNone if none was passed.
\n\n

Supported operations:

\n\n

Instance methods:

\n
\n
\ntime.replace([hour[, minute[, second[, microsecond[, tzinfo]]]]])
\n
Return a time with the same value, except for those attributes given\nnew values by whichever keyword arguments are specified. Note that\ntzinfo=None can be specified to create a naive time from an\naware time, without conversion of the time data.
\n\n
\n
\ntime.isoformat()
\n
Return a string representing the time in ISO 8601 format, HH:MM:SS.mmmmmm or, if\nself.microsecond is 0, HH:MM:SS If utcoffset() does not return None, a\n6-character string is appended, giving the UTC offset in (signed) hours and\nminutes: HH:MM:SS.mmmmmm+HH:MM or, if self.microsecond is 0, HH:MM:SS+HH:MM
\n\n
\n
\ntime.__str__()
\n
For a time t, str(t) is equivalent to t.isoformat().
\n\n
\n
\ntime.strftime(format)
\n
Return a string representing the time, controlled by an explicit format string.\nSee section strftime() and strptime() Behavior.
\n\n
\n
\ntime.utcoffset()
\n
If tzinfo is None, returns None, else returns\nself.tzinfo.utcoffset(None), and raises an exception if the latter doesn’t\nreturn None or a timedelta object representing a whole number of\nminutes with magnitude less than one day.
\n\n
\n
\ntime.dst()
\n
If tzinfo is None, returns None, else returns\nself.tzinfo.dst(None), and raises an exception if the latter doesn’t return\nNone, or a timedelta object representing a whole number of minutes\nwith magnitude less than one day.
\n\n
\n
\ntime.tzname()
\n
If tzinfo is None, returns None, else returns\nself.tzinfo.tzname(None), or raises an exception if the latter doesn’t\nreturn None or a string object.
\n\n

Example:

\n
>>> from datetime import time, tzinfo\n>>> class GMT1(tzinfo):\n...     def utcoffset(self, dt):\n...         return timedelta(hours=1)\n...     def dst(self, dt):\n...         return timedelta(0)\n...     def tzname(self,dt):\n...         return "Europe/Prague"\n...\n>>> t = time(12, 10, 30, tzinfo=GMT1())\n>>> t                               # doctest: +ELLIPSIS\ndatetime.time(12, 10, 30, tzinfo=<GMT1 object at 0x...>)\n>>> gmt = GMT1()\n>>> t.isoformat()\n'12:10:30+01:00'\n>>> t.dst()\ndatetime.timedelta(0)\n>>> t.tzname()\n'Europe/Prague'\n>>> t.strftime("%H:%M:%S %Z")\n'12:10:30 Europe/Prague'\n
\n
\n
\n
\n

8.1.6. tzinfo Objects

\n

tzinfo is an abstract base class, meaning that this class should not be\ninstantiated directly. You need to derive a concrete subclass, and (at least)\nsupply implementations of the standard tzinfo methods needed by the\ndatetime methods you use. The datetime module does not supply\nany concrete subclasses of tzinfo.

\n

An instance of (a concrete subclass of) tzinfo can be passed to the\nconstructors for datetime and time objects. The latter objects\nview their attributes as being in local time, and the tzinfo object\nsupports methods revealing offset of local time from UTC, the name of the time\nzone, and DST offset, all relative to a date or time object passed to them.

\n

Special requirement for pickling: A tzinfo subclass must have an\n__init__() method that can be called with no arguments, else it can be\npickled but possibly not unpickled again. This is a technical requirement that\nmay be relaxed in the future.

\n

A concrete subclass of tzinfo may need to implement the following\nmethods. Exactly which methods are needed depends on the uses made of aware\ndatetime objects. If in doubt, simply implement all of them.

\n
\n
\ntzinfo.utcoffset(self, dt)
\n

Return offset of local time from UTC, in minutes east of UTC. If local time is\nwest of UTC, this should be negative. Note that this is intended to be the\ntotal offset from UTC; for example, if a tzinfo object represents both\ntime zone and DST adjustments, utcoffset() should return their sum. If\nthe UTC offset isn’t known, return None. Else the value returned must be a\ntimedelta object specifying a whole number of minutes in the range\n-1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset must be less\nthan one day). Most implementations of utcoffset() will probably look\nlike one of these two:

\n
return CONSTANT                 # fixed-offset class\nreturn CONSTANT + self.dst(dt)  # daylight-aware class\n
\n
\n

If utcoffset() does not return None, dst() should not return\nNone either.

\n

The default implementation of utcoffset() raises\nNotImplementedError.

\n
\n\n
\n
\ntzinfo.dst(self, dt)
\n

Return the daylight saving time (DST) adjustment, in minutes east of UTC, or\nNone if DST information isn’t known. Return timedelta(0) if DST is not\nin effect. If DST is in effect, return the offset as a timedelta object\n(see utcoffset() for details). Note that DST offset, if applicable, has\nalready been added to the UTC offset returned by utcoffset(), so there’s\nno need to consult dst() unless you’re interested in obtaining DST info\nseparately. For example, datetime.timetuple() calls its tzinfo\nattribute’s dst() method to determine how the tm_isdst flag\nshould be set, and tzinfo.fromutc() calls dst() to account for\nDST changes when crossing time zones.

\n

An instance tz of a tzinfo subclass that models both standard and\ndaylight times must be consistent in this sense:

\n

tz.utcoffset(dt) - tz.dst(dt)

\n

must return the same result for every datetime dt with dt.tzinfo ==\ntz For sane tzinfo subclasses, this expression yields the time\nzone’s “standard offset”, which should not depend on the date or the time, but\nonly on geographic location. The implementation of datetime.astimezone()\nrelies on this, but cannot detect violations; it’s the programmer’s\nresponsibility to ensure it. If a tzinfo subclass cannot guarantee\nthis, it may be able to override the default implementation of\ntzinfo.fromutc() to work correctly with astimezone() regardless.

\n

Most implementations of dst() will probably look like one of these two:

\n
def dst(self, dt):\n    # a fixed-offset class:  doesn't account for DST\n    return timedelta(0)\n
\n
\n

or

\n
def dst(self, dt):\n    # Code to set dston and dstoff to the time zone's DST\n    # transition times based on the input dt.year, and expressed\n    # in standard local time.  Then\n\n    if dston <= dt.replace(tzinfo=None) < dstoff:\n        return timedelta(hours=1)\n    else:\n        return timedelta(0)\n
\n
\n

The default implementation of dst() raises NotImplementedError.

\n
\n\n
\n
\ntzinfo.tzname(self, dt)
\n

Return the time zone name corresponding to the datetime object dt, as\na string. Nothing about string names is defined by the datetime module,\nand there’s no requirement that it mean anything in particular. For example,\n“GMT”, “UTC”, “-500”, “-5:00”, “EDT”, “US/Eastern”, “America/New York” are all\nvalid replies. Return None if a string name isn’t known. Note that this is\na method rather than a fixed string primarily because some tzinfo\nsubclasses will wish to return different names depending on the specific value\nof dt passed, especially if the tzinfo class is accounting for\ndaylight time.

\n

The default implementation of tzname() raises NotImplementedError.

\n
\n\n

These methods are called by a datetime or time object, in\nresponse to their methods of the same names. A datetime object passes\nitself as the argument, and a time object passes None as the\nargument. A tzinfo subclass’s methods should therefore be prepared to\naccept a dt argument of None, or of class datetime.

\n

When None is passed, it’s up to the class designer to decide the best\nresponse. For example, returning None is appropriate if the class wishes to\nsay that time objects don’t participate in the tzinfo protocols. It\nmay be more useful for utcoffset(None) to return the standard UTC offset, as\nthere is no other convention for discovering the standard offset.

\n

When a datetime object is passed in response to a datetime\nmethod, dt.tzinfo is the same object as self. tzinfo methods can\nrely on this, unless user code calls tzinfo methods directly. The\nintent is that the tzinfo methods interpret dt as being in local\ntime, and not need worry about objects in other timezones.

\n

There is one more tzinfo method that a subclass may wish to override:

\n
\n
\ntzinfo.fromutc(self, dt)
\n

This is called from the default datetime.astimezone()\nimplementation. When called from that, dt.tzinfo is self, and dt‘s\ndate and time data are to be viewed as expressing a UTC time. The purpose\nof fromutc() is to adjust the date and time data, returning an\nequivalent datetime in self‘s local time.

\n

Most tzinfo subclasses should be able to inherit the default\nfromutc() implementation without problems. It’s strong enough to handle\nfixed-offset time zones, and time zones accounting for both standard and\ndaylight time, and the latter even if the DST transition times differ in\ndifferent years. An example of a time zone the default fromutc()\nimplementation may not handle correctly in all cases is one where the standard\noffset (from UTC) depends on the specific date and time passed, which can happen\nfor political reasons. The default implementations of astimezone() and\nfromutc() may not produce the result you want if the result is one of the\nhours straddling the moment the standard offset changes.

\n

Skipping code for error cases, the default fromutc() implementation acts\nlike:

\n
def fromutc(self, dt):\n    # raise ValueError error if dt.tzinfo is not self\n    dtoff = dt.utcoffset()\n    dtdst = dt.dst()\n    # raise ValueError if dtoff is None or dtdst is None\n    delta = dtoff - dtdst  # this is self's standard offset\n    if delta:\n        dt += delta   # convert to standard local time\n        dtdst = dt.dst()\n        # raise ValueError if dtdst is None\n    if dtdst:\n        return dt + dtdst\n    else:\n        return dt\n
\n
\n
\n\n

Example tzinfo classes:

\n
from datetime import tzinfo, timedelta, datetime\n\nZERO = timedelta(0)\nHOUR = timedelta(hours=1)\n\n# A UTC class.\n\nclass UTC(tzinfo):\n    """UTC"""\n\n    def utcoffset(self, dt):\n        return ZERO\n\n    def tzname(self, dt):\n        return "UTC"\n\n    def dst(self, dt):\n        return ZERO\n\nutc = UTC()\n\n# A class building tzinfo objects for fixed-offset time zones.\n# Note that FixedOffset(0, "UTC") is a different way to build a\n# UTC tzinfo object.\n\nclass FixedOffset(tzinfo):\n    """Fixed offset in minutes east from UTC."""\n\n    def __init__(self, offset, name):\n        self.__offset = timedelta(minutes = offset)\n        self.__name = name\n\n    def utcoffset(self, dt):\n        return self.__offset\n\n    def tzname(self, dt):\n        return self.__name\n\n    def dst(self, dt):\n        return ZERO\n\n# A class capturing the platform's idea of local time.\n\nimport time as _time\n\nSTDOFFSET = timedelta(seconds = -_time.timezone)\nif _time.daylight:\n    DSTOFFSET = timedelta(seconds = -_time.altzone)\nelse:\n    DSTOFFSET = STDOFFSET\n\nDSTDIFF = DSTOFFSET - STDOFFSET\n\nclass LocalTimezone(tzinfo):\n\n    def utcoffset(self, dt):\n        if self._isdst(dt):\n            return DSTOFFSET\n        else:\n            return STDOFFSET\n\n    def dst(self, dt):\n        if self._isdst(dt):\n            return DSTDIFF\n        else:\n            return ZERO\n\n    def tzname(self, dt):\n        return _time.tzname[self._isdst(dt)]\n\n    def _isdst(self, dt):\n        tt = (dt.year, dt.month, dt.day,\n              dt.hour, dt.minute, dt.second,\n              dt.weekday(), 0, 0)\n        stamp = _time.mktime(tt)\n        tt = _time.localtime(stamp)\n        return tt.tm_isdst > 0\n\nLocal = LocalTimezone()\n\n\n# A complete implementation of current DST rules for major US time zones.\n\ndef first_sunday_on_or_after(dt):\n    days_to_go = 6 - dt.weekday()\n    if days_to_go:\n        dt += timedelta(days_to_go)\n    return dt\n\n\n# US DST Rules\n#\n# This is a simplified (i.e., wrong for a few cases) set of rules for US\n# DST start and end times. For a complete and up-to-date set of DST rules\n# and timezone definitions, visit the Olson Database (or try pytz):\n# http://www.twinsun.com/tz/tz-link.htm\n# http://sourceforge.net/projects/pytz/ (might not be up-to-date)\n#\n# In the US, since 2007, DST starts at 2am (standard time) on the second\n# Sunday in March, which is the first Sunday on or after Mar 8.\nDSTSTART_2007 = datetime(1, 3, 8, 2)\n# and ends at 2am (DST time; 1am standard time) on the first Sunday of Nov.\nDSTEND_2007 = datetime(1, 11, 1, 1)\n# From 1987 to 2006, DST used to start at 2am (standard time) on the first\n# Sunday in April and to end at 2am (DST time; 1am standard time) on the last\n# Sunday of October, which is the first Sunday on or after Oct 25.\nDSTSTART_1987_2006 = datetime(1, 4, 1, 2)\nDSTEND_1987_2006 = datetime(1, 10, 25, 1)\n# From 1967 to 1986, DST used to start at 2am (standard time) on the last\n# Sunday in April (the one on or after April 24) and to end at 2am (DST time;\n# 1am standard time) on the last Sunday of October, which is the first Sunday\n# on or after Oct 25.\nDSTSTART_1967_1986 = datetime(1, 4, 24, 2)\nDSTEND_1967_1986 = DSTEND_1987_2006\n\nclass USTimeZone(tzinfo):\n\n    def __init__(self, hours, reprname, stdname, dstname):\n        self.stdoffset = timedelta(hours=hours)\n        self.reprname = reprname\n        self.stdname = stdname\n        self.dstname = dstname\n\n    def __repr__(self):\n        return self.reprname\n\n    def tzname(self, dt):\n        if self.dst(dt):\n            return self.dstname\n        else:\n            return self.stdname\n\n    def utcoffset(self, dt):\n        return self.stdoffset + self.dst(dt)\n\n    def dst(self, dt):\n        if dt is None or dt.tzinfo is None:\n            # An exception may be sensible here, in one or both cases.\n            # It depends on how you want to treat them.  The default\n            # fromutc() implementation (called by the default astimezone()\n            # implementation) passes a datetime with dt.tzinfo is self.\n            return ZERO\n        assert dt.tzinfo is self\n\n        # Find start and end times for US DST. For years before 1967, return\n        # ZERO for no DST.\n        if 2006 < dt.year:\n            dststart, dstend = DSTSTART_2007, DSTEND_2007\n        elif 1986 < dt.year < 2007:\n            dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006\n        elif 1966 < dt.year < 1987:\n            dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986\n        else:\n            return ZERO\n\n        start = first_sunday_on_or_after(dststart.replace(year=dt.year))\n        end = first_sunday_on_or_after(dstend.replace(year=dt.year))\n\n        # Can't compare naive to aware objects, so strip the timezone from\n        # dt first.\n        if start <= dt.replace(tzinfo=None) < end:\n            return HOUR\n        else:\n            return ZERO\n\nEastern  = USTimeZone(-5, "Eastern",  "EST", "EDT")\nCentral  = USTimeZone(-6, "Central",  "CST", "CDT")\nMountain = USTimeZone(-7, "Mountain", "MST", "MDT")\nPacific  = USTimeZone(-8, "Pacific",  "PST", "PDT")\n
\n
\n

Note that there are unavoidable subtleties twice per year in a tzinfo\nsubclass accounting for both standard and daylight time, at the DST transition\npoints. For concreteness, consider US Eastern (UTC -0500), where EDT begins the\nminute after 1:59 (EST) on the second Sunday in March, and ends the minute after\n1:59 (EDT) on the first Sunday in November:

\n
  UTC   3:MM  4:MM  5:MM  6:MM  7:MM  8:MM\n  EST  22:MM 23:MM  0:MM  1:MM  2:MM  3:MM\n  EDT  23:MM  0:MM  1:MM  2:MM  3:MM  4:MM\n\nstart  22:MM 23:MM  0:MM  1:MM  3:MM  4:MM\n\n  end  23:MM  0:MM  1:MM  1:MM  2:MM  3:MM
\n
\n

When DST starts (the “start” line), the local wall clock leaps from 1:59 to\n3:00. A wall time of the form 2:MM doesn’t really make sense on that day, so\nastimezone(Eastern) won’t deliver a result with hour == 2 on the day DST\nbegins. In order for astimezone() to make this guarantee, the\nrzinfo.dst() method must consider times in the “missing hour” (2:MM for\nEastern) to be in daylight time.

\n

When DST ends (the “end” line), there’s a potentially worse problem: there’s an\nhour that can’t be spelled unambiguously in local wall time: the last hour of\ndaylight time. In Eastern, that’s times of the form 5:MM UTC on the day\ndaylight time ends. The local wall clock leaps from 1:59 (daylight time) back\nto 1:00 (standard time) again. Local times of the form 1:MM are ambiguous.\nastimezone() mimics the local clock’s behavior by mapping two adjacent UTC\nhours into the same local hour then. In the Eastern example, UTC times of the\nform 5:MM and 6:MM both map to 1:MM when converted to Eastern. In order for\nastimezone() to make this guarantee, the tzinfo.dst() method must\nconsider times in the “repeated hour” to be in standard time. This is easily\narranged, as in the example, by expressing DST switch times in the time zone’s\nstandard local time.

\n

Applications that can’t bear such ambiguities should avoid using hybrid\ntzinfo subclasses; there are no ambiguities when using UTC, or any\nother fixed-offset tzinfo subclass (such as a class representing only\nEST (fixed offset -5 hours), or only EDT (fixed offset -4 hours)).

\n
\n
\n

8.1.7. strftime() and strptime() Behavior

\n

date, datetime, and time objects all support a\nstrftime(format) method, to create a string representing the time under the\ncontrol of an explicit format string. Broadly speaking, d.strftime(fmt)\nacts like the time module’s time.strftime(fmt, d.timetuple())\nalthough not all objects support a timetuple() method.

\n

Conversely, the datetime.strptime() class method creates a\ndatetime object from a string representing a date and time and a\ncorresponding format string. datetime.strptime(date_string, format) is\nequivalent to datetime(*(time.strptime(date_string, format)[0:6])).

\n

For time objects, the format codes for year, month, and day should not\nbe used, as time objects have no such values. If they’re used anyway, 1900\nis substituted for the year, and 1 for the month and day.

\n

For date objects, the format codes for hours, minutes, seconds, and\nmicroseconds should not be used, as date objects have no such\nvalues. If they’re used anyway, 0 is substituted for them.

\n

\nNew in version 2.6: time and datetime objects support a %f format code\nwhich expands to the number of microseconds in the object, zero-padded on\nthe left to six places.

\n

For a naive object, the %z and %Z format codes are replaced by empty\nstrings.

\n

For an aware object:

\n
\n
%z
\n
utcoffset() is transformed into a 5-character string of the form +HHMM or\n-HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and\nMM is a 2-digit string giving the number of UTC offset minutes. For example, if\nutcoffset() returns timedelta(hours=-3, minutes=-30), %z is\nreplaced with the string '-0330'.
\n
%Z
\n
If tzname() returns None, %Z is replaced by an empty string.\nOtherwise %Z is replaced by the returned value, which must be a string.
\n
\n

The full set of format codes supported varies across platforms, because Python\ncalls the platform C library’s strftime() function, and platform\nvariations are common.

\n

The following is a list of all the format codes that the C standard (1989\nversion) requires, and these work on all platforms with a standard C\nimplementation. Note that the 1999 version of the C standard added additional\nformat codes.

\n

The exact range of years for which strftime() works also varies across\nplatforms. Regardless of platform, years before 1900 cannot be used.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
DirectiveMeaningNotes
%aLocale’s abbreviated weekday\nname. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month\nname. 
%BLocale’s full month name. 
%cLocale’s appropriate date and\ntime representation. 
%dDay of the month as a decimal\nnumber [01,31]. 
%fMicrosecond as a decimal\nnumber [0,999999], zero-padded\non the left(1)
%HHour (24-hour clock) as a\ndecimal number [00,23]. 
%IHour (12-hour clock) as a\ndecimal number [01,12]. 
%jDay of the year as a decimal\nnumber [001,366]. 
%mMonth as a decimal number\n[01,12]. 
%MMinute as a decimal number\n[00,59]. 
%pLocale’s equivalent of either\nAM or PM.(2)
%SSecond as a decimal number\n[00,61].(3)
%UWeek number of the year\n(Sunday as the first day of\nthe week) as a decimal number\n[00,53]. All days in a new\nyear preceding the first\nSunday are considered to be in\nweek 0.(4)
%wWeekday as a decimal number\n[0(Sunday),6]. 
%WWeek number of the year\n(Monday as the first day of\nthe week) as a decimal number\n[00,53]. All days in a new\nyear preceding the first\nMonday are considered to be in\nweek 0.(4)
%xLocale’s appropriate date\nrepresentation. 
%XLocale’s appropriate time\nrepresentation. 
%yYear without century as a\ndecimal number [00,99]. 
%YYear with century as a decimal\nnumber. 
%zUTC offset in the form +HHMM\nor -HHMM (empty string if the\nthe object is naive).(5)
%ZTime zone name (empty string\nif the object is naive). 
A literal '%' character. 
\n

Notes:

\n
    \n
  1. When used with the strptime() method, the %f directive\naccepts from one to six digits and zero pads on the right. %f is\nan extension to the set of format characters in the C standard (but\nimplemented separately in datetime objects, and therefore always\navailable).
  2. \n
  3. When used with the strptime() method, the %p directive only affects\nthe output hour field if the %I directive is used to parse the hour.
  4. \n
  5. The range really is 0 to 61; according to the Posix standard this\naccounts for leap seconds and the (very rare) double leap seconds.\nThe time module may produce and does accept leap seconds since\nit is based on the Posix standard, but the datetime module\ndoes not accept leap seconds in strptime() input nor will it\nproduce them in strftime() output.
  6. \n
  7. When used with the strptime() method, %U and %W are only used in\ncalculations when the day of the week and the year are specified.
  8. \n
  9. For example, if utcoffset() returns timedelta(hours=-3, minutes=-30),\n%z is replaced with the string '-0330'.
  10. \n
\n
\n
", "searchableItems": [ { "name": "datetime.date", "domId": "datetime_datetime.date" }, { "name": "datetime.date.__str__", "domId": "datetime_datetime.date.__str__" }, { "name": "datetime.date.ctime", "domId": "datetime_datetime.date.ctime" }, { "name": "datetime.date.isocalendar", "domId": "datetime_datetime.date.isocalendar" }, { "name": "datetime.date.isoformat", "domId": "datetime_datetime.date.isoformat" }, { "name": "datetime.date.isoweekday", "domId": "datetime_datetime.date.isoweekday" }, { "name": "datetime.date.replace", "domId": "datetime_datetime.date.replace" }, { "name": "datetime.date.strftime", "domId": "datetime_datetime.date.strftime" }, { "name": "datetime.date.timetuple", "domId": "datetime_datetime.date.timetuple" }, { "name": "datetime.date.toordinal", "domId": "datetime_datetime.date.toordinal" }, { "name": "datetime.date.weekday", "domId": "datetime_datetime.date.weekday" }, { "name": "datetime.datetime", "domId": "datetime_datetime.datetime" }, { "name": "datetime.datetime.__str__", "domId": "datetime_datetime.datetime.__str__" }, { "name": "datetime.datetime.astimezone", "domId": "datetime_datetime.datetime.astimezone" }, { "name": "datetime.datetime.ctime", "domId": "datetime_datetime.datetime.ctime" }, { "name": "datetime.datetime.date", "domId": "datetime_datetime.datetime.date" }, { "name": "datetime.datetime.dst", "domId": "datetime_datetime.datetime.dst" }, { "name": "datetime.datetime.isocalendar", "domId": "datetime_datetime.datetime.isocalendar" }, { "name": "datetime.datetime.isoformat", "domId": "datetime_datetime.datetime.isoformat" }, { "name": "datetime.datetime.isoweekday", "domId": "datetime_datetime.datetime.isoweekday" }, { "name": "datetime.datetime.replace", "domId": "datetime_datetime.datetime.replace" }, { "name": "datetime.datetime.strftime", "domId": "datetime_datetime.datetime.strftime" }, { "name": "datetime.datetime.time", "domId": "datetime_datetime.datetime.time" }, { "name": "datetime.datetime.timetuple", "domId": "datetime_datetime.datetime.timetuple" }, { "name": "datetime.datetime.timetz", "domId": "datetime_datetime.datetime.timetz" }, { "name": "datetime.datetime.toordinal", "domId": "datetime_datetime.datetime.toordinal" }, { "name": "datetime.datetime.tzname", "domId": "datetime_datetime.datetime.tzname" }, { "name": "datetime.datetime.utcoffset", "domId": "datetime_datetime.datetime.utcoffset" }, { "name": "datetime.datetime.utctimetuple", "domId": "datetime_datetime.datetime.utctimetuple" }, { "name": "datetime.datetime.weekday", "domId": "datetime_datetime.datetime.weekday" }, { "name": "datetime.time", "domId": "datetime_datetime.time" }, { "name": "datetime.time.__str__", "domId": "datetime_datetime.time.__str__" }, { "name": "datetime.time.dst", "domId": "datetime_datetime.time.dst" }, { "name": "datetime.time.isoformat", "domId": "datetime_datetime.time.isoformat" }, { "name": "datetime.time.replace", "domId": "datetime_datetime.time.replace" }, { "name": "datetime.time.strftime", "domId": "datetime_datetime.time.strftime" }, { "name": "datetime.time.tzname", "domId": "datetime_datetime.time.tzname" }, { "name": "datetime.time.utcoffset", "domId": "datetime_datetime.time.utcoffset" }, { "name": "datetime.timedelta", "domId": "datetime_datetime.timedelta" }, { "name": "datetime.timedelta.total_seconds", "domId": "datetime_datetime.timedelta.total_seconds" }, { "name": "datetime.tzinfo", "domId": "datetime_datetime.tzinfo" }, { "name": "datetime.tzinfo.dst", "domId": "datetime_datetime.tzinfo.dst" }, { "name": "datetime.tzinfo.fromutc", "domId": "datetime_datetime.tzinfo.fromutc" }, { "name": "datetime.tzinfo.tzname", "domId": "datetime_datetime.tzinfo.tzname" }, { "name": "datetime.tzinfo.utcoffset", "domId": "datetime_datetime.tzinfo.utcoffset" } ] }, { "url": "http://docs.python.org/library/collections.html", "title": "collections", "html": "
\n

8.3. collections — High-performance container datatypes

\n

\nNew in version 2.4.

\n

Source code: Lib/collections.py and Lib/_abcoll.py

\n
\n

This module implements specialized container datatypes providing alternatives to\nPython’s general purpose built-in containers, dict, list,\nset, and tuple.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
namedtuple()factory function for creating tuple subclasses with named fields

\nNew in version 2.6.

\n
dequelist-like container with fast appends and pops on either end

\nNew in version 2.4.

\n
Counterdict subclass for counting hashable objects

\nNew in version 2.7.

\n
OrderedDictdict subclass that remembers the order entries were added

\nNew in version 2.7.

\n
defaultdictdict subclass that calls a factory function to supply missing values

\nNew in version 2.5.

\n
\n

In addition to the concrete container classes, the collections module provides\nabstract base classes that can be\nused to test whether a class provides a particular interface, for example,\nwhether it is hashable or a mapping.

\n
\n

8.3.1. Counter objects

\n

A counter tool is provided to support convenient and rapid tallies.\nFor example:

\n
>>> # Tally occurrences of words in a list\n>>> cnt = Counter()\n>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:\n...     cnt[word] += 1\n>>> cnt\nCounter({'blue': 3, 'red': 2, 'green': 1})\n\n>>> # Find the ten most common words in Hamlet\n>>> import re\n>>> words = re.findall('\\w+', open('hamlet.txt').read().lower())\n>>> Counter(words).most_common(10)\n[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),\n ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]\n
\n
\n
\n
\nclass collections.Counter([iterable-or-mapping])
\n

A Counter is a dict subclass for counting hashable objects.\nIt is an unordered collection where elements are stored as dictionary keys\nand their counts are stored as dictionary values. Counts are allowed to be\nany integer value including zero or negative counts. The Counter\nclass is similar to bags or multisets in other languages.

\n

Elements are counted from an iterable or initialized from another\nmapping (or counter):

\n
>>> c = Counter()                           # a new, empty counter\n>>> c = Counter('gallahad')                 # a new counter from an iterable\n>>> c = Counter({'red': 4, 'blue': 2})      # a new counter from a mapping\n>>> c = Counter(cats=4, dogs=8)             # a new counter from keyword args\n
\n
\n

Counter objects have a dictionary interface except that they return a zero\ncount for missing items instead of raising a KeyError:

\n
>>> c = Counter(['eggs', 'ham'])\n>>> c['bacon']                              # count of a missing element is zero\n0\n
\n
\n

Setting a count to zero does not remove an element from a counter.\nUse del to remove it entirely:

\n
>>> c['sausage'] = 0                        # counter entry with a zero count\n>>> del c['sausage']                        # del actually removes the entry\n
\n
\n

\nNew in version 2.7.

\n

Counter objects support three methods beyond those available for all\ndictionaries:

\n
\n
\nelements()
\n

Return an iterator over elements repeating each as many times as its\ncount. Elements are returned in arbitrary order. If an element’s count\nis less than one, elements() will ignore it.

\n
>>> c = Counter(a=4, b=2, c=0, d=-2)\n>>> list(c.elements())\n['a', 'a', 'a', 'a', 'b', 'b']\n
\n
\n
\n\n
\n
\nmost_common([n])
\n

Return a list of the n most common elements and their counts from the\nmost common to the least. If n is not specified, most_common()\nreturns all elements in the counter. Elements with equal counts are\nordered arbitrarily:

\n
>>> Counter('abracadabra').most_common(3)\n[('a', 5), ('r', 2), ('b', 2)]\n
\n
\n
\n\n
\n
\nsubtract([iterable-or-mapping])
\n

Elements are subtracted from an iterable or from another mapping\n(or counter). Like dict.update() but subtracts counts instead\nof replacing them. Both inputs and outputs may be zero or negative.

\n
>>> c = Counter(a=4, b=2, c=0, d=-2)\n>>> d = Counter(a=1, b=2, c=3, d=4)\n>>> c.subtract(d)\nCounter({'a': 3, 'b': 0, 'c': -3, 'd': -6})\n
\n
\n
\n\n

The usual dictionary methods are available for Counter objects\nexcept for two which work differently for counters.

\n
\n
\nfromkeys(iterable)
\n
This class method is not implemented for Counter objects.
\n\n
\n
\nupdate([iterable-or-mapping])
\n
Elements are counted from an iterable or added-in from another\nmapping (or counter). Like dict.update() but adds counts\ninstead of replacing them. Also, the iterable is expected to be a\nsequence of elements, not a sequence of (key, value) pairs.
\n\n
\n\n

Common patterns for working with Counter objects:

\n
sum(c.values())                 # total of all counts\nc.clear()                       # reset all counts\nlist(c)                         # list unique elements\nset(c)                          # convert to a set\ndict(c)                         # convert to a regular dictionary\nc.items()                       # convert to a list of (elem, cnt) pairs\nCounter(dict(list_of_pairs))    # convert from a list of (elem, cnt) pairs\nc.most_common()[:-n:-1]         # n least common elements\nc += Counter()                  # remove zero and negative counts\n
\n
\n

Several mathematical operations are provided for combining Counter\nobjects to produce multisets (counters that have counts greater than zero).\nAddition and subtraction combine counters by adding or subtracting the counts\nof corresponding elements. Intersection and union return the minimum and\nmaximum of corresponding counts. Each operation can accept inputs with signed\ncounts, but the output will exclude results with counts of zero or less.

\n
>>> c = Counter(a=3, b=1)\n>>> d = Counter(a=1, b=2)\n>>> c + d                       # add two counters together:  c[x] + d[x]\nCounter({'a': 4, 'b': 3})\n>>> c - d                       # subtract (keeping only positive counts)\nCounter({'a': 2})\n>>> c & d                       # intersection:  min(c[x], d[x])\nCounter({'a': 1, 'b': 1})\n>>> c | d                       # union:  max(c[x], d[x])\nCounter({'a': 3, 'b': 2})\n
\n
\n
\n

Note

\n

Counters were primarily designed to work with positive integers to represent\nrunning counts; however, care was taken to not unnecessarily preclude use\ncases needing other types or negative values. To help with those use cases,\nthis section documents the minimum range and type restrictions.

\n
    \n
  • The Counter class itself is a dictionary subclass with no\nrestrictions on its keys and values. The values are intended to be numbers\nrepresenting counts, but you could store anything in the value field.
  • \n
  • The most_common() method requires only that the values be orderable.
  • \n
  • For in-place operations such as c[key] += 1, the value type need only\nsupport addition and subtraction. So fractions, floats, and decimals would\nwork and negative values are supported. The same is also true for\nupdate() and subtract() which allow negative and zero values\nfor both inputs and outputs.
  • \n
  • The multiset methods are designed only for use cases with positive values.\nThe inputs may be negative or zero, but only outputs with positive values\nare created. There are no type restrictions, but the value type needs to\nsupport addition, subtraction, and comparison.
  • \n
  • The elements() method requires integer counts. It ignores zero and\nnegative counts.
  • \n
\n
\n
\n

See also

\n
    \n
  • Counter class\nadapted for Python 2.5 and an early Bag recipe for Python 2.4.

    \n
  • \n
  • Bag class\nin Smalltalk.

    \n
  • \n
  • Wikipedia entry for Multisets.

    \n
  • \n
  • C++ multisets\ntutorial with examples.

    \n
  • \n
  • For mathematical operations on multisets and their use cases, see\nKnuth, Donald. The Art of Computer Programming Volume II,\nSection 4.6.3, Exercise 19.

    \n
  • \n
  • To enumerate all distinct multisets of a given size over a given set of\nelements, see itertools.combinations_with_replacement().

    \n
    \n

    map(Counter, combinations_with_replacement(‘ABC’, 2)) –> AA AB AC BB BC CC

    \n
    \n
  • \n
\n
\n
\n
\n

8.3.2. deque objects

\n
\n
\nclass collections.deque([iterable[, maxlen]])
\n

Returns a new deque object initialized left-to-right (using append()) with\ndata from iterable. If iterable is not specified, the new deque is empty.

\n

Deques are a generalization of stacks and queues (the name is pronounced “deck”\nand is short for “double-ended queue”). Deques support thread-safe, memory\nefficient appends and pops from either side of the deque with approximately the\nsame O(1) performance in either direction.

\n

Though list objects support similar operations, they are optimized for\nfast fixed-length operations and incur O(n) memory movement costs for\npop(0) and insert(0, v) operations which change both the size and\nposition of the underlying data representation.

\n

\nNew in version 2.4.

\n

If maxlen is not specified or is None, deques may grow to an\narbitrary length. Otherwise, the deque is bounded to the specified maximum\nlength. Once a bounded length deque is full, when new items are added, a\ncorresponding number of items are discarded from the opposite end. Bounded\nlength deques provide functionality similar to the tail filter in\nUnix. They are also useful for tracking transactions and other pools of data\nwhere only the most recent activity is of interest.

\n

\nChanged in version 2.6: Added maxlen parameter.

\n

Deque objects support the following methods:

\n
\n
\nappend(x)
\n
Add x to the right side of the deque.
\n\n
\n
\nappendleft(x)
\n
Add x to the left side of the deque.
\n\n
\n
\nclear()
\n
Remove all elements from the deque leaving it with length 0.
\n\n
\n
\ncount(x)
\n

Count the number of deque elements equal to x.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nextend(iterable)
\n
Extend the right side of the deque by appending elements from the iterable\nargument.
\n\n
\n
\nextendleft(iterable)
\n
Extend the left side of the deque by appending elements from iterable.\nNote, the series of left appends results in reversing the order of\nelements in the iterable argument.
\n\n
\n
\npop()
\n
Remove and return an element from the right side of the deque. If no\nelements are present, raises an IndexError.
\n\n
\n
\npopleft()
\n
Remove and return an element from the left side of the deque. If no\nelements are present, raises an IndexError.
\n\n
\n
\nremove(value)
\n

Removed the first occurrence of value. If not found, raises a\nValueError.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nreverse()
\n

Reverse the elements of the deque in-place and then return None.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nrotate(n)
\n
Rotate the deque n steps to the right. If n is negative, rotate to\nthe left. Rotating one step to the right is equivalent to:\nd.appendleft(d.pop()).
\n\n

Deque objects also provide one read-only attribute:

\n
\n
\nmaxlen
\n

Maximum size of a deque or None if unbounded.

\n

\nNew in version 2.7.

\n
\n\n
\n\n

In addition to the above, deques support iteration, pickling, len(d),\nreversed(d), copy.copy(d), copy.deepcopy(d), membership testing with\nthe in operator, and subscript references such as d[-1]. Indexed\naccess is O(1) at both ends but slows to O(n) in the middle. For fast random\naccess, use lists instead.

\n

Example:

\n
>>> from collections import deque\n>>> d = deque('ghi')                 # make a new deque with three items\n>>> for elem in d:                   # iterate over the deque's elements\n...     print elem.upper()\nG\nH\nI\n\n>>> d.append('j')                    # add a new entry to the right side\n>>> d.appendleft('f')                # add a new entry to the left side\n>>> d                                # show the representation of the deque\ndeque(['f', 'g', 'h', 'i', 'j'])\n\n>>> d.pop()                          # return and remove the rightmost item\n'j'\n>>> d.popleft()                      # return and remove the leftmost item\n'f'\n>>> list(d)                          # list the contents of the deque\n['g', 'h', 'i']\n>>> d[0]                             # peek at leftmost item\n'g'\n>>> d[-1]                            # peek at rightmost item\n'i'\n\n>>> list(reversed(d))                # list the contents of a deque in reverse\n['i', 'h', 'g']\n>>> 'h' in d                         # search the deque\nTrue\n>>> d.extend('jkl')                  # add multiple elements at once\n>>> d\ndeque(['g', 'h', 'i', 'j', 'k', 'l'])\n>>> d.rotate(1)                      # right rotation\n>>> d\ndeque(['l', 'g', 'h', 'i', 'j', 'k'])\n>>> d.rotate(-1)                     # left rotation\n>>> d\ndeque(['g', 'h', 'i', 'j', 'k', 'l'])\n\n>>> deque(reversed(d))               # make a new deque in reverse order\ndeque(['l', 'k', 'j', 'i', 'h', 'g'])\n>>> d.clear()                        # empty the deque\n>>> d.pop()                          # cannot pop from an empty deque\nTraceback (most recent call last):\n  File "<pyshell#6>", line 1, in -toplevel-\n    d.pop()\nIndexError: pop from an empty deque\n\n>>> d.extendleft('abc')              # extendleft() reverses the input order\n>>> d\ndeque(['c', 'b', 'a'])\n
\n
\n
\n

8.3.2.1. deque Recipes

\n

This section shows various approaches to working with deques.

\n

Bounded length deques provide functionality similar to the tail filter\nin Unix:

\n
def tail(filename, n=10):\n    'Return the last n lines of a file'\n    return deque(open(filename), n)\n
\n
\n

Another approach to using deques is to maintain a sequence of recently\nadded elements by appending to the right and popping to the left:

\n
def moving_average(iterable, n=3):\n    # moving_average([40, 30, 50, 46, 39, 44]) --> 40.0 42.0 45.0 43.0\n    # http://en.wikipedia.org/wiki/Moving_average\n    it = iter(iterable)\n    d = deque(itertools.islice(it, n-1))\n    d.appendleft(0)\n    s = sum(d)\n    for elem in it:\n        s += elem - d.popleft()\n        d.append(elem)\n        yield s / float(n)\n
\n
\n

The rotate() method provides a way to implement deque slicing and\ndeletion. For example, a pure Python implementation of del d[n] relies on\nthe rotate() method to position elements to be popped:

\n
def delete_nth(d, n):\n    d.rotate(-n)\n    d.popleft()\n    d.rotate(n)\n
\n
\n

To implement deque slicing, use a similar approach applying\nrotate() to bring a target element to the left side of the deque. Remove\nold entries with popleft(), add new entries with extend(), and then\nreverse the rotation.\nWith minor variations on that approach, it is easy to implement Forth style\nstack manipulations such as dup, drop, swap, over, pick,\nrot, and roll.

\n
\n
\n
\n

8.3.3. defaultdict objects

\n
\n
\nclass collections.defaultdict([default_factory[, ...]])
\n

Returns a new dictionary-like object. defaultdict is a subclass of the\nbuilt-in dict class. It overrides one method and adds one writable\ninstance variable. The remaining functionality is the same as for the\ndict class and is not documented here.

\n

The first argument provides the initial value for the default_factory\nattribute; it defaults to None. All remaining arguments are treated the same\nas if they were passed to the dict constructor, including keyword\narguments.

\n

\nNew in version 2.5.

\n

defaultdict objects support the following method in addition to the\nstandard dict operations:

\n
\n
\n__missing__(key)
\n

If the default_factory attribute is None, this raises a\nKeyError exception with the key as argument.

\n

If default_factory is not None, it is called without arguments\nto provide a default value for the given key, this value is inserted in\nthe dictionary for the key, and returned.

\n

If calling default_factory raises an exception this exception is\npropagated unchanged.

\n

This method is called by the __getitem__() method of the\ndict class when the requested key is not found; whatever it\nreturns or raises is then returned or raised by __getitem__().

\n
\n\n

defaultdict objects support the following instance variable:

\n
\n
\ndefault_factory
\n
This attribute is used by the __missing__() method; it is\ninitialized from the first argument to the constructor, if present, or to\nNone, if absent.
\n\n
\n\n
\n

8.3.3.1. defaultdict Examples

\n

Using list as the default_factory, it is easy to group a\nsequence of key-value pairs into a dictionary of lists:

\n
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]\n>>> d = defaultdict(list)\n>>> for k, v in s:\n...     d[k].append(v)\n...\n>>> d.items()\n[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]\n
\n
\n

When each key is encountered for the first time, it is not already in the\nmapping; so an entry is automatically created using the default_factory\nfunction which returns an empty list. The list.append()\noperation then attaches the value to the new list. When keys are encountered\nagain, the look-up proceeds normally (returning the list for that key) and the\nlist.append() operation adds another value to the list. This technique is\nsimpler and faster than an equivalent technique using dict.setdefault():

\n
>>> d = {}\n>>> for k, v in s:\n...     d.setdefault(k, []).append(v)\n...\n>>> d.items()\n[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]\n
\n
\n

Setting the default_factory to int makes the\ndefaultdict useful for counting (like a bag or multiset in other\nlanguages):

\n
>>> s = 'mississippi'\n>>> d = defaultdict(int)\n>>> for k in s:\n...     d[k] += 1\n...\n>>> d.items()\n[('i', 4), ('p', 2), ('s', 4), ('m', 1)]\n
\n
\n

When a letter is first encountered, it is missing from the mapping, so the\ndefault_factory function calls int() to supply a default count of\nzero. The increment operation then builds up the count for each letter.

\n

The function int() which always returns zero is just a special case of\nconstant functions. A faster and more flexible way to create constant functions\nis to use itertools.repeat() which can supply any constant value (not just\nzero):

\n
>>> def constant_factory(value):\n...     return itertools.repeat(value).next\n>>> d = defaultdict(constant_factory('<missing>'))\n>>> d.update(name='John', action='ran')\n>>> '%(name)s %(action)s to %(object)s'  d\n'John ran to <missing>'\n
\n
\n

Setting the default_factory to set makes the\ndefaultdict useful for building a dictionary of sets:

\n
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]\n>>> d = defaultdict(set)\n>>> for k, v in s:\n...     d[k].add(v)\n...\n>>> d.items()\n[('blue', set([2, 4])), ('red', set([1, 3]))]\n
\n
\n
\n
\n
\n

8.3.4. namedtuple() Factory Function for Tuples with Named Fields

\n

Named tuples assign meaning to each position in a tuple and allow for more readable,\nself-documenting code. They can be used wherever regular tuples are used, and\nthey add the ability to access fields by name instead of position index.

\n
\n
\ncollections.namedtuple(typename, field_names[, verbose=False][, rename=False])
\n

Returns a new tuple subclass named typename. The new subclass is used to\ncreate tuple-like objects that have fields accessible by attribute lookup as\nwell as being indexable and iterable. Instances of the subclass also have a\nhelpful docstring (with typename and field_names) and a helpful __repr__()\nmethod which lists the tuple contents in a name=value format.

\n

The field_names are a sequence of strings such as ['x', 'y'].\nAlternatively, field_names can be a single string with each fieldname\nseparated by whitespace and/or commas, for example 'x y' or 'x, y'.

\n

Any valid Python identifier may be used for a fieldname except for names\nstarting with an underscore. Valid identifiers consist of letters, digits,\nand underscores but do not start with a digit or underscore and cannot be\na keyword such as class, for, return, global, pass, print,\nor raise.

\n

If rename is true, invalid fieldnames are automatically replaced\nwith positional names. For example, ['abc', 'def', 'ghi', 'abc'] is\nconverted to ['abc', '_1', 'ghi', '_3'], eliminating the keyword\ndef and the duplicate fieldname abc.

\n

If verbose is true, the class definition is printed just before being built.

\n

Named tuple instances do not have per-instance dictionaries, so they are\nlightweight and require no more memory than regular tuples.

\n

\nNew in version 2.6.

\n

\nChanged in version 2.7: added support for rename.

\n
\n\n

Example:

\n
>>> Point = namedtuple('Point', ['x', 'y'], verbose=True)\nclass Point(tuple):\n        'Point(x, y)'\n\n        __slots__ = ()\n\n        _fields = ('x', 'y')\n\n        def __new__(_cls, x, y):\n            'Create a new instance of Point(x, y)'\n            return _tuple.__new__(_cls, (x, y))\n\n        @classmethod\n        def _make(cls, iterable, new=tuple.__new__, len=len):\n            'Make a new Point object from a sequence or iterable'\n            result = new(cls, iterable)\n            if len(result) != 2:\n                raise TypeError('Expected 2 arguments, got %d' % len(result))\n            return result\n\n        def __repr__(self):\n            'Return a nicely formatted representation string'\n            return 'Point(x=%r, y=%r)' % self\n\n        def _asdict(self):\n            'Return a new OrderedDict which maps field names to their values'\n            return OrderedDict(zip(self._fields, self))\n\n       __dict__ = property(_asdict)\n\n       def _replace(_self, **kwds):\n            'Return a new Point object replacing specified fields with new values'\n            result = _self._make(map(kwds.pop, ('x', 'y'), _self))\n            if kwds:\n                raise ValueError('Got unexpected field names: %r' % kwds.keys())\n            return result\n\n        def __getnewargs__(self):\n            'Return self as a plain tuple.   Used by copy and pickle.'\n            return tuple(self)\n\n        x = _property(_itemgetter(0), doc='Alias for field number 0')\n        y = _property(_itemgetter(1), doc='Alias for field number 1')\n\n>>> p = Point(11, y=22)     # instantiate with positional or keyword arguments\n>>> p[0] + p[1]             # indexable like the plain tuple (11, 22)\n33\n>>> x, y = p                # unpack like a regular tuple\n>>> x, y\n(11, 22)\n>>> p.x + p.y               # fields also accessible by name\n33\n>>> p                       # readable __repr__ with a name=value style\nPoint(x=11, y=22)\n
\n
\n

Named tuples are especially useful for assigning field names to result tuples returned\nby the csv or sqlite3 modules:

\n
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')\n\nimport csv\nfor emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):\n    print emp.name, emp.title\n\nimport sqlite3\nconn = sqlite3.connect('/companydata')\ncursor = conn.cursor()\ncursor.execute('SELECT name, age, title, department, paygrade FROM employees')\nfor emp in map(EmployeeRecord._make, cursor.fetchall()):\n    print emp.name, emp.title\n
\n
\n

In addition to the methods inherited from tuples, named tuples support\nthree additional methods and one attribute. To prevent conflicts with\nfield names, the method and attribute names start with an underscore.

\n
\n
\nclassmethod somenamedtuple._make(iterable)
\n

Class method that makes a new instance from an existing sequence or iterable.

\n
>>> t = [11, 22]\n>>> Point._make(t)\nPoint(x=11, y=22)\n
\n
\n
\n\n
\n
\nsomenamedtuple._asdict()
\n

Return a new OrderedDict which maps field names to their corresponding\nvalues:

\n
>>> p._asdict()\nOrderedDict([('x', 11), ('y', 22)])\n
\n
\n

\nChanged in version 2.7: Returns an OrderedDict instead of a regular dict.

\n
\n\n
\n
\nsomenamedtuple._replace(kwargs)
\n

Return a new instance of the named tuple replacing specified fields with new\nvalues:

\n
>>> p = Point(x=11, y=22)\n>>> p._replace(x=33)\nPoint(x=33, y=22)\n\n>>> for partnum, record in inventory.items():\n        inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())\n
\n
\n
\n\n
\n
\nsomenamedtuple._fields
\n

Tuple of strings listing the field names. Useful for introspection\nand for creating new named tuple types from existing named tuples.

\n
>>> p._fields            # view the field names\n('x', 'y')\n\n>>> Color = namedtuple('Color', 'red green blue')\n>>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)\n>>> Pixel(11, 22, 128, 255, 0)\nPixel(x=11, y=22, red=128, green=255, blue=0)\n
\n
\n
\n\n

To retrieve a field whose name is stored in a string, use the getattr()\nfunction:

\n
>>> getattr(p, 'x')\n11\n
\n
\n

To convert a dictionary to a named tuple, use the double-star-operator\n(as described in Unpacking Argument Lists):

\n
>>> d = {'x': 11, 'y': 22}\n>>> Point(**d)\nPoint(x=11, y=22)\n
\n
\n

Since a named tuple is a regular Python class, it is easy to add or change\nfunctionality with a subclass. Here is how to add a calculated field and\na fixed-width print format:

\n
\n
>>> class Point(namedtuple('Point', 'x y')):\n        __slots__ = ()\n        @property\n        def hypot(self):\n            return (self.x ** 2 + self.y ** 2) ** 0.5\n        def __str__(self):\n            return 'Point: x=%6.3f  y=%6.3f  hypot=%6.3f' % (self.x, self.y, self.hypot)\n
\n
\n
>>> for p in Point(3, 4), Point(14, 5/7.):\n        print p\nPoint: x= 3.000  y= 4.000  hypot= 5.000\nPoint: x=14.000  y= 0.714  hypot=14.018\n
\n
\n
\n

The subclass shown above sets __slots__ to an empty tuple. This helps\nkeep memory requirements low by preventing the creation of instance dictionaries.

\n

Subclassing is not useful for adding new, stored fields. Instead, simply\ncreate a new named tuple type from the _fields attribute:

\n
>>> Point3D = namedtuple('Point3D', Point._fields + ('z',))\n
\n
\n

Default values can be implemented by using _replace() to\ncustomize a prototype instance:

\n
>>> Account = namedtuple('Account', 'owner balance transaction_count')\n>>> default_account = Account('<owner name>', 0.0, 0)\n>>> johns_account = default_account._replace(owner='John')\n
\n
\n

Enumerated constants can be implemented with named tuples, but it is simpler\nand more efficient to use a simple class declaration:

\n
>>> Status = namedtuple('Status', 'open pending closed')._make(range(3))\n>>> Status.open, Status.pending, Status.closed\n(0, 1, 2)\n>>> class Status:\n        open, pending, closed = range(3)\n
\n
\n
\n

See also

\n

Named tuple recipe\nadapted for Python 2.4.

\n
\n
\n
\n

8.3.5. OrderedDict objects

\n

Ordered dictionaries are just like regular dictionaries but they remember the\norder that items were inserted. When iterating over an ordered dictionary,\nthe items are returned in the order their keys were first added.

\n
\n
\nclass collections.OrderedDict([items])
\n

Return an instance of a dict subclass, supporting the usual dict\nmethods. An OrderedDict is a dict that remembers the order that keys\nwere first inserted. If a new entry overwrites an existing entry, the\noriginal insertion position is left unchanged. Deleting an entry and\nreinserting it will move it to the end.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nOrderedDict.popitem(last=True)
\n
The popitem() method for ordered dictionaries returns and removes\na (key, value) pair. The pairs are returned in LIFO order if last is\ntrue or FIFO order if false.
\n\n

In addition to the usual mapping methods, ordered dictionaries also support\nreverse iteration using reversed().

\n

Equality tests between OrderedDict objects are order-sensitive\nand are implemented as list(od1.items())==list(od2.items()).\nEquality tests between OrderedDict objects and other\nMapping objects are order-insensitive like regular dictionaries.\nThis allows OrderedDict objects to be substituted anywhere a\nregular dictionary is used.

\n

The OrderedDict constructor and update() method both accept\nkeyword arguments, but their order is lost because Python’s function call\nsemantics pass-in keyword arguments using a regular unordered dictionary.

\n
\n

See also

\n

Equivalent OrderedDict recipe\nthat runs on Python 2.4 or later.

\n
\n
\n

8.3.5.1. OrderedDict Examples and Recipes

\n

Since an ordered dictionary remembers its insertion order, it can be used\nin conjuction with sorting to make a sorted dictionary:

\n
>>> # regular unsorted dictionary\n>>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}\n\n>>> # dictionary sorted by key\n>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))\nOrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])\n\n>>> # dictionary sorted by value\n>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))\nOrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])\n\n>>> # dictionary sorted by length of the key string\n>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))\nOrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])\n
\n
\n

The new sorted dictionaries maintain their sort order when entries\nare deleted. But when new keys are added, the keys are appended\nto the end and the sort is not maintained.

\n

It is also straight-forward to create an ordered dictionary variant\nthat the remembers the order the keys were last inserted.\nIf a new entry overwrites an existing entry, the\noriginal insertion position is changed and moved to the end:

\n
class LastUpdatedOrderedDict(OrderedDict):\n    'Store items in the order the keys were last added'\n\n    def __setitem__(self, key, value):\n        if key in self:\n            del self[key]\n        OrderedDict.__setitem__(self, key, value)\n
\n
\n

An ordered dictionary can be combined with the Counter class\nso that the counter remembers the order elements are first encountered:

\n
class OrderedCounter(Counter, OrderedDict):\n     'Counter that remembers the order elements are first encountered'\n\n     def __repr__(self):\n         return '%s(%r)'  (self.__class__.__name__, OrderedDict(self))\n\n     def __reduce__(self):\n         return self.__class__, (OrderedDict(self),)\n
\n
\n
\n
\n
\n

8.3.6. Collections Abstract Base Classes

\n

The collections module offers the following ABCs:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ABCInherits fromAbstract MethodsMixin Methods
Container __contains__ 
Hashable __hash__ 
Iterable __iter__ 
IteratorIterablenext__iter__
Sized __len__ 
Callable __call__ 
SequenceSized,\nIterable,\nContainer__getitem____contains__, __iter__, __reversed__,\nindex, and count
MutableSequenceSequence__setitem__,\n__delitem__,\ninsertInherited Sequence methods and\nappend, reverse, extend, pop,\nremove, and __iadd__
SetSized,\nIterable,\nContainer __le__, __lt__, __eq__, __ne__,\n__gt__, __ge__, __and__, __or__,\n__sub__, __xor__, and isdisjoint
MutableSetSetadd,\ndiscardInherited Set methods and\nclear, pop, remove, __ior__,\n__iand__, __ixor__, and __isub__
MappingSized,\nIterable,\nContainer__getitem____contains__, keys, items, values,\nget, __eq__, and __ne__
MutableMappingMapping__setitem__,\n__delitem__Inherited Mapping methods and\npop, popitem, clear, update,\nand setdefault
MappingViewSized __len__
ItemsViewMappingView,\nSet __contains__,\n__iter__
KeysViewMappingView,\nSet __contains__,\n__iter__
ValuesViewMappingView __contains__, __iter__
\n
\n
\nclass collections.Container
\n
\nclass collections.Hashable
\n
\nclass collections.Sized
\n
\nclass collections.Callable
\n
ABCs for classes that provide respectively the methods __contains__(),\n__hash__(), __len__(), and __call__().
\n\n
\n
\nclass collections.Iterable
\n
ABC for classes that provide the __iter__() method.\nSee also the definition of iterable.
\n\n
\n
\nclass collections.Iterator
\n
ABC for classes that provide the __iter__() and next() methods.\nSee also the definition of iterator.
\n\n
\n
\nclass collections.Sequence
\n
\nclass collections.MutableSequence
\n
ABCs for read-only and mutable sequences.
\n\n
\n
\nclass collections.Set
\n
\nclass collections.MutableSet
\n
ABCs for read-only and mutable sets.
\n\n
\n
\nclass collections.Mapping
\n
\nclass collections.MutableMapping
\n
ABCs for read-only and mutable mappings.
\n\n
\n
\nclass collections.MappingView
\n
\nclass collections.ItemsView
\n
\nclass collections.KeysView
\n
\nclass collections.ValuesView
\n
ABCs for mapping, items, keys, and values views.
\n\n

These ABCs allow us to ask classes or instances if they provide\nparticular functionality, for example:

\n
size = None\nif isinstance(myvar, collections.Sized):\n    size = len(myvar)\n
\n
\n

Several of the ABCs are also useful as mixins that make it easier to develop\nclasses supporting container APIs. For example, to write a class supporting\nthe full Set API, it only necessary to supply the three underlying\nabstract methods: __contains__(), __iter__(), and __len__().\nThe ABC supplies the remaining methods such as __and__() and\nisdisjoint()

\n
class ListBasedSet(collections.Set):\n     ''' Alternate set implementation favoring space over speed\n         and not requiring the set elements to be hashable. '''\n     def __init__(self, iterable):\n         self.elements = lst = []\n         for value in iterable:\n             if value not in lst:\n                 lst.append(value)\n     def __iter__(self):\n         return iter(self.elements)\n     def __contains__(self, value):\n         return value in self.elements\n     def __len__(self):\n         return len(self.elements)\n\ns1 = ListBasedSet('abcdef')\ns2 = ListBasedSet('defghi')\noverlap = s1 & s2            # The __and__() method is supported automatically\n
\n
\n

Notes on using Set and MutableSet as a mixin:

\n
    \n
  1. Since some set operations create new sets, the default mixin methods need\na way to create new instances from an iterable. The class constructor is\nassumed to have a signature in the form ClassName(iterable).\nThat assumption is factored-out to an internal classmethod called\n_from_iterable() which calls cls(iterable) to produce a new set.\nIf the Set mixin is being used in a class with a different\nconstructor signature, you will need to override _from_iterable()\nwith a classmethod that can construct new instances from\nan iterable argument.
  2. \n
  3. To override the comparisons (presumably for speed, as the\nsemantics are fixed), redefine __le__() and\nthen the other operations will automatically follow suit.
  4. \n
  5. The Set mixin provides a _hash() method to compute a hash value\nfor the set; however, __hash__() is not defined because not all sets\nare hashable or immutable. To add set hashabilty using mixins,\ninherit from both Set() and Hashable(), then define\n__hash__ = Set._hash.
  6. \n
\n
\n

See also

\n\n
\n
\n
", "searchableItems": [ { "name": "collections.Container", "domId": "collections_collections.Container" }, { "name": "collections.Counter", "domId": "collections_collections.Counter" }, { "name": "collections.Counter.elements", "domId": "collections_collections.Counter.elements" }, { "name": "collections.Counter.fromkeys", "domId": "collections_collections.Counter.fromkeys" }, { "name": "collections.Counter.most_common", "domId": "collections_collections.Counter.most_common" }, { "name": "collections.Counter.subtract", "domId": "collections_collections.Counter.subtract" }, { "name": "collections.Counter.update", "domId": "collections_collections.Counter.update" }, { "name": "collections.defaultdict", "domId": "collections_collections.defaultdict" }, { "name": "collections.defaultdict.__missing__", "domId": "collections_collections.defaultdict.__missing__" }, { "name": "collections.deque", "domId": "collections_collections.deque" }, { "name": "collections.deque.append", "domId": "collections_collections.deque.append" }, { "name": "collections.deque.appendleft", "domId": "collections_collections.deque.appendleft" }, { "name": "collections.deque.clear", "domId": "collections_collections.deque.clear" }, { "name": "collections.deque.count", "domId": "collections_collections.deque.count" }, { "name": "collections.deque.extend", "domId": "collections_collections.deque.extend" }, { "name": "collections.deque.extendleft", "domId": "collections_collections.deque.extendleft" }, { "name": "collections.deque.pop", "domId": "collections_collections.deque.pop" }, { "name": "collections.deque.popleft", "domId": "collections_collections.deque.popleft" }, { "name": "collections.deque.remove", "domId": "collections_collections.deque.remove" }, { "name": "collections.deque.reverse", "domId": "collections_collections.deque.reverse" }, { "name": "collections.deque.rotate", "domId": "collections_collections.deque.rotate" }, { "name": "collections.Iterable", "domId": "collections_collections.Iterable" }, { "name": "collections.Iterator", "domId": "collections_collections.Iterator" }, { "name": "collections.Mapping", "domId": "collections_collections.Mapping" }, { "name": "collections.MappingView", "domId": "collections_collections.MappingView" }, { "name": "collections.namedtuple", "domId": "collections_collections.namedtuple" }, { "name": "collections.OrderedDict", "domId": "collections_collections.OrderedDict" }, { "name": "collections.OrderedDict.popitem", "domId": "collections_collections.OrderedDict.popitem" }, { "name": "collections.Sequence", "domId": "collections_collections.Sequence" }, { "name": "collections.Set", "domId": "collections_collections.Set" }, { "name": "collections.somenamedtuple._asdict", "domId": "collections_collections.somenamedtuple._asdict" }, { "name": "collections.somenamedtuple._replace", "domId": "collections_collections.somenamedtuple._replace" } ] }, { "url": "http://docs.python.org/library/sched.html", "title": "sched", "html": "
\n

8.8. sched — Event scheduler

\n

Source code: Lib/sched.py

\n
\n

The sched module defines a class which implements a general purpose event\nscheduler:

\n
\n
\nclass sched.scheduler(timefunc, delayfunc)
\n
The scheduler class defines a generic interface to scheduling events.\nIt needs two functions to actually deal with the “outside world” — timefunc\nshould be callable without arguments, and return a number (the “time”, in any\nunits whatsoever). The delayfunc function should be callable with one\nargument, compatible with the output of timefunc, and should delay that many\ntime units. delayfunc will also be called with the argument 0 after each\nevent is run to allow other threads an opportunity to run in multi-threaded\napplications.
\n\n

Example:

\n
>>> import sched, time\n>>> s = sched.scheduler(time.time, time.sleep)\n>>> def print_time(): print "From print_time", time.time()\n...\n>>> def print_some_times():\n...     print time.time()\n...     s.enter(5, 1, print_time, ())\n...     s.enter(10, 1, print_time, ())\n...     s.run()\n...     print time.time()\n...\n>>> print_some_times()\n930343690.257\nFrom print_time 930343695.274\nFrom print_time 930343700.273\n930343700.276\n
\n
\n

In multi-threaded environments, the scheduler class has limitations\nwith respect to thread-safety, inability to insert a new task before\nthe one currently pending in a running scheduler, and holding up the main\nthread until the event queue is empty. Instead, the preferred approach\nis to use the threading.Timer class instead.

\n

Example:

\n
>>> import time\n>>> from threading import Timer\n>>> def print_time():\n...     print "From print_time", time.time()\n...\n>>> def print_some_times():\n...     print time.time()\n...     Timer(5, print_time, ()).start()\n...     Timer(10, print_time, ()).start()\n...     time.sleep(11)  # sleep while time-delay events execute\n...     print time.time()\n...\n>>> print_some_times()\n930343690.257\nFrom print_time 930343695.274\nFrom print_time 930343700.273\n930343701.301\n
\n
\n
\n

8.8.1. Scheduler Objects

\n

scheduler instances have the following methods and attributes:

\n
\n
\nscheduler.enterabs(time, priority, action, argument)
\n

Schedule a new event. The time argument should be a numeric type compatible\nwith the return value of the timefunc function passed to the constructor.\nEvents scheduled for the same time will be executed in the order of their\npriority.

\n

Executing the event means executing action(*argument). argument must be a\nsequence holding the parameters for action.

\n

Return value is an event which may be used for later cancellation of the event\n(see cancel()).

\n
\n\n
\n
\nscheduler.enter(delay, priority, action, argument)
\n
Schedule an event for delay more time units. Other than the relative time, the\nother arguments, the effect and the return value are the same as those for\nenterabs().
\n\n
\n
\nscheduler.cancel(event)
\n
Remove the event from the queue. If event is not an event currently in the\nqueue, this method will raise a ValueError.
\n\n
\n
\nscheduler.empty()
\n
Return true if the event queue is empty.
\n\n
\n
\nscheduler.run()
\n

Run all scheduled events. This function will wait (using the delayfunc()\nfunction passed to the constructor) for the next event, then execute it and so\non until there are no more scheduled events.

\n

Either action or delayfunc can raise an exception. In either case, the\nscheduler will maintain a consistent state and propagate the exception. If an\nexception is raised by action, the event will not be attempted in future calls\nto run().

\n

If a sequence of events takes longer to run than the time available before the\nnext event, the scheduler will simply fall behind. No events will be dropped;\nthe calling code is responsible for canceling events which are no longer\npertinent.

\n
\n\n
\n
\nscheduler.queue
\n

Read-only attribute returning a list of upcoming events in the order they\nwill be run. Each event is shown as a named tuple with the\nfollowing fields: time, priority, action, argument.

\n

\nNew in version 2.6.

\n
\n\n
\n
", "searchableItems": [ { "name": "sched.scheduler", "domId": "sched_sched.scheduler" }, { "name": "sched.scheduler.cancel", "domId": "sched_sched.scheduler.cancel" }, { "name": "sched.scheduler.empty", "domId": "sched_sched.scheduler.empty" }, { "name": "sched.scheduler.enter", "domId": "sched_sched.scheduler.enter" }, { "name": "sched.scheduler.enterabs", "domId": "sched_sched.scheduler.enterabs" }, { "name": "sched.scheduler.run", "domId": "sched_sched.scheduler.run" } ] }, { "url": "http://docs.python.org/library/mutex.html", "title": "mutex", "html": "
\n

8.9. mutex — Mutual exclusion support

\n

\nDeprecated since version 2.6: The mutex module has been removed in Python 3.0.

\n

The mutex module defines a class that allows mutual-exclusion via\nacquiring and releasing locks. It does not require (or imply)\nthreading or multi-tasking, though it could be useful for those\npurposes.

\n

The mutex module defines the following class:

\n
\n
\nclass mutex.mutex
\n

Create a new (unlocked) mutex.

\n

A mutex has two pieces of state — a “locked” bit and a queue. When the mutex\nis not locked, the queue is empty. Otherwise, the queue contains zero or more\n(function, argument) pairs representing functions (or methods) waiting to\nacquire the lock. When the mutex is unlocked while the queue is not empty, the\nfirst queue entry is removed and its function(argument) pair called,\nimplying it now has the lock.

\n

Of course, no multi-threading is implied – hence the funny interface for\nlock(), where a function is called once the lock is acquired.

\n
\n\n
\n

8.9.1. Mutex Objects

\n

mutex objects have following methods:

\n
\n
\nmutex.test()
\n
Check whether the mutex is locked.
\n\n
\n
\nmutex.testandset()
\n
“Atomic” test-and-set, grab the lock if it is not set, and return True,\notherwise, return False.
\n\n
\n
\nmutex.lock(function, argument)
\n
Execute function(argument), unless the mutex is locked. In the case it is\nlocked, place the function and argument on the queue. See unlock() for\nexplanation of when function(argument) is executed in that case.
\n\n
\n
\nmutex.unlock()
\n
Unlock the mutex if queue is empty, otherwise execute the first element in the\nqueue.
\n\n
\n
", "searchableItems": [ { "name": "mutex.mutex", "domId": "mutex_mutex.mutex" }, { "name": "mutex.mutex.lock", "domId": "mutex_mutex.mutex.lock" }, { "name": "mutex.mutex.test", "domId": "mutex_mutex.mutex.test" }, { "name": "mutex.mutex.testandset", "domId": "mutex_mutex.mutex.testandset" }, { "name": "mutex.mutex.unlock", "domId": "mutex_mutex.mutex.unlock" } ] }, { "url": "http://docs.python.org/library/sets.html", "title": "sets", "html": "
\n

8.7. sets — Unordered collections of unique elements

\n

\nNew in version 2.3.

\n

\nDeprecated since version 2.6: The built-in set/frozenset types replace this module.

\n

The sets module provides classes for constructing and manipulating\nunordered collections of unique elements. Common uses include membership\ntesting, removing duplicates from a sequence, and computing standard math\noperations on sets such as intersection, union, difference, and symmetric\ndifference.

\n

Like other collections, sets support x in set, len(set), and for x in\nset. Being an unordered collection, sets do not record element position or\norder of insertion. Accordingly, sets do not support indexing, slicing, or\nother sequence-like behavior.

\n

Most set applications use the Set class which provides every set method\nexcept for __hash__(). For advanced applications requiring a hash method,\nthe ImmutableSet class adds a __hash__() method but omits methods\nwhich alter the contents of the set. Both Set and ImmutableSet\nderive from BaseSet, an abstract class useful for determining whether\nsomething is a set: isinstance(obj, BaseSet).

\n

The set classes are implemented using dictionaries. Accordingly, the\nrequirements for set elements are the same as those for dictionary keys; namely,\nthat the element defines both __eq__() and __hash__(). As a result,\nsets cannot contain mutable elements such as lists or dictionaries. However,\nthey can contain immutable collections such as tuples or instances of\nImmutableSet. For convenience in implementing sets of sets, inner sets\nare automatically converted to immutable form, for example,\nSet([Set(['dog'])]) is transformed to Set([ImmutableSet(['dog'])]).

\n
\n
\nclass sets.Set([iterable])
\n
Constructs a new empty Set object. If the optional iterable\nparameter is supplied, updates the set with elements obtained from iteration.\nAll of the elements in iterable should be immutable or be transformable to an\nimmutable using the protocol described in section Protocol for automatic conversion to immutable.
\n\n
\n
\nclass sets.ImmutableSet([iterable])
\n

Constructs a new empty ImmutableSet object. If the optional iterable\nparameter is supplied, updates the set with elements obtained from iteration.\nAll of the elements in iterable should be immutable or be transformable to an\nimmutable using the protocol described in section Protocol for automatic conversion to immutable.

\n

Because ImmutableSet objects provide a __hash__() method, they\ncan be used as set elements or as dictionary keys. ImmutableSet\nobjects do not have methods for adding or removing elements, so all of the\nelements must be known when the constructor is called.

\n
\n\n
\n

8.7.1. Set Objects

\n

Instances of Set and ImmutableSet both provide the following\noperations:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationEquivalentResult
len(s) cardinality of set s
x in s test x for membership in s
x not in s test x for non-membership in\ns
s.issubset(t)s <= ttest whether every element in\ns is in t
s.issuperset(t)s >= ttest whether every element in\nt is in s
s.union(t)s | tnew set with elements from both\ns and t
s.intersection(t)s & tnew set with elements common to\ns and t
s.difference(t)s - tnew set with elements in s\nbut not in t
s.symmetric_difference(t)s ^ tnew set with elements in either\ns or t but not both
s.copy() new set with a shallow copy of\ns
\n

Note, the non-operator versions of union(), intersection(),\ndifference(), and symmetric_difference() will accept any iterable as\nan argument. In contrast, their operator based counterparts require their\narguments to be sets. This precludes error-prone constructions like\nSet('abc') & 'cbs' in favor of the more readable\nSet('abc').intersection('cbs').

\n

\nChanged in version 2.3.1: Formerly all arguments were required to be sets.

\n

In addition, both Set and ImmutableSet support set to set\ncomparisons. Two sets are equal if and only if every element of each set is\ncontained in the other (each is a subset of the other). A set is less than\nanother set if and only if the first set is a proper subset of the second set\n(is a subset, but is not equal). A set is greater than another set if and only\nif the first set is a proper superset of the second set (is a superset, but is\nnot equal).

\n

The subset and equality comparisons do not generalize to a complete ordering\nfunction. For example, any two disjoint sets are not equal and are not subsets\nof each other, so all of the following return False: a<b, a==b,\nor a>b. Accordingly, sets do not implement the __cmp__() method.

\n

Since sets only define partial ordering (subset relationships), the output of\nthe list.sort() method is undefined for lists of sets.

\n

The following table lists operations available in ImmutableSet but not\nfound in Set:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationResult
hash(s)returns a hash value for s
\n

The following table lists operations available in Set but not found in\nImmutableSet:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationEquivalentResult
s.update(t)s |= treturn set s with elements\nadded from t
s.intersection_update(t)s &= treturn set s keeping only\nelements also found in t
s.difference_update(t)s -= treturn set s after removing\nelements found in t
s.symmetric_difference_update(t)s ^= treturn set s with elements\nfrom s or t but not both
s.add(x) add element x to set s
s.remove(x) remove x from set s; raises\nKeyError if not present
s.discard(x) removes x from set s if\npresent
s.pop() remove and return an arbitrary\nelement from s; raises\nKeyError if empty
s.clear() remove all elements from set\ns
\n

Note, the non-operator versions of update(), intersection_update(),\ndifference_update(), and symmetric_difference_update() will accept\nany iterable as an argument.

\n

\nChanged in version 2.3.1: Formerly all arguments were required to be sets.

\n

Also note, the module also includes a union_update() method which is an\nalias for update(). The method is included for backwards compatibility.\nProgrammers should prefer the update() method because it is supported by\nthe built-in set() and frozenset() types.

\n
\n
\n

8.7.2. Example

\n
>>> from sets import Set\n>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])\n>>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])\n>>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])\n>>> employees = engineers | programmers | managers           # union\n>>> engineering_management = engineers & managers            # intersection\n>>> fulltime_management = managers - engineers - programmers # difference\n>>> engineers.add('Marvin')                                  # add element\n>>> print engineers # doctest: +SKIP\nSet(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])\n>>> employees.issuperset(engineers)     # superset test\nFalse\n>>> employees.update(engineers)         # update from another set\n>>> employees.issuperset(engineers)\nTrue\n>>> for group in [engineers, programmers, managers, employees]: # doctest: +SKIP\n...     group.discard('Susan')          # unconditionally remove element\n...     print group\n...\nSet(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])\nSet(['Janice', 'Jack', 'Sam'])\nSet(['Jane', 'Zack', 'Jack'])\nSet(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])\n
\n
\n
\n
\n

8.7.3. Protocol for automatic conversion to immutable

\n

Sets can only contain immutable elements. For convenience, mutable Set\nobjects are automatically copied to an ImmutableSet before being added\nas a set element.

\n

The mechanism is to always add a hashable element, or if it is not\nhashable, the element is checked to see if it has an __as_immutable__()\nmethod which returns an immutable equivalent.

\n

Since Set objects have a __as_immutable__() method returning an\ninstance of ImmutableSet, it is possible to construct sets of sets.

\n

A similar mechanism is needed by the __contains__() and remove()\nmethods which need to hash an element to check for membership in a set. Those\nmethods check an element for hashability and, if not, check for a\n__as_temporarily_immutable__() method which returns the element wrapped by\na class that provides temporary methods for __hash__(), __eq__(),\nand __ne__().

\n

The alternate mechanism spares the need to build a separate copy of the original\nmutable object.

\n

Set objects implement the __as_temporarily_immutable__() method\nwhich returns the Set object wrapped by a new class\n_TemporarilyImmutableSet.

\n

The two mechanisms for adding hashability are normally invisible to the user;\nhowever, a conflict can arise in a multi-threaded environment where one thread\nis updating a set while another has temporarily wrapped it in\n_TemporarilyImmutableSet. In other words, sets of mutable sets are not\nthread-safe.

\n
\n
\n

8.7.4. Comparison to the built-in set types

\n

The built-in set and frozenset types were designed based on\nlessons learned from the sets module. The key differences are:

\n\n
\n
", "searchableItems": [ { "name": "sets.ImmutableSet", "domId": "sets_sets.ImmutableSet" }, { "name": "sets.Set", "domId": "sets_sets.Set" } ] }, { "url": "http://docs.python.org/library/queue.html", "title": "Queue", "html": "
\n

8.10. Queue — A synchronized queue class

\n
\n

Note

\n

The Queue module has been renamed to queue in Python 3.0. The\n2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n

Source code: Lib/Queue.py

\n
\n

The Queue module implements multi-producer, multi-consumer queues.\nIt is especially useful in threaded programming when information must be\nexchanged safely between multiple threads. The Queue class in this\nmodule implements all the required locking semantics. It depends on the\navailability of thread support in Python; see the threading\nmodule.

\n

Implements three types of queue whose only difference is the order that\nthe entries are retrieved. In a FIFO queue, the first tasks added are\nthe first retrieved. In a LIFO queue, the most recently added entry is\nthe first retrieved (operating like a stack). With a priority queue,\nthe entries are kept sorted (using the heapq module) and the\nlowest valued entry is retrieved first.

\n

The Queue module defines the following classes and exceptions:

\n
\n
\nclass Queue.Queue(maxsize=0)
\n
Constructor for a FIFO queue. maxsize is an integer that sets the upperbound\nlimit on the number of items that can be placed in the queue. Insertion will\nblock once this size has been reached, until queue items are consumed. If\nmaxsize is less than or equal to zero, the queue size is infinite.
\n\n
\n
\nclass Queue.LifoQueue(maxsize=0)
\n

Constructor for a LIFO queue. maxsize is an integer that sets the upperbound\nlimit on the number of items that can be placed in the queue. Insertion will\nblock once this size has been reached, until queue items are consumed. If\nmaxsize is less than or equal to zero, the queue size is infinite.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nclass Queue.PriorityQueue(maxsize=0)
\n

Constructor for a priority queue. maxsize is an integer that sets the upperbound\nlimit on the number of items that can be placed in the queue. Insertion will\nblock once this size has been reached, until queue items are consumed. If\nmaxsize is less than or equal to zero, the queue size is infinite.

\n

The lowest valued entries are retrieved first (the lowest valued entry is the\none returned by sorted(list(entries))[0]). A typical pattern for entries\nis a tuple in the form: (priority_number, data).

\n

\nNew in version 2.6.

\n
\n\n
\n
\nexception Queue.Empty
\n
Exception raised when non-blocking get() (or get_nowait()) is called\non a Queue object which is empty.
\n\n
\n
\nexception Queue.Full
\n
Exception raised when non-blocking put() (or put_nowait()) is called\non a Queue object which is full.
\n\n
\n

See also

\n

collections.deque is an alternative implementation of unbounded\nqueues with fast atomic append() and popleft() operations that\ndo not require locking.

\n
\n
\n

8.10.1. Queue Objects

\n

Queue objects (Queue, LifoQueue, or PriorityQueue)\nprovide the public methods described below.

\n
\n
\nQueue.qsize()
\n
Return the approximate size of the queue. Note, qsize() > 0 doesn’t\nguarantee that a subsequent get() will not block, nor will qsize() < maxsize\nguarantee that put() will not block.
\n\n
\n
\nQueue.empty()
\n
Return True if the queue is empty, False otherwise. If empty()\nreturns True it doesn’t guarantee that a subsequent call to put()\nwill not block. Similarly, if empty() returns False it doesn’t\nguarantee that a subsequent call to get() will not block.
\n\n
\n
\nQueue.full()
\n
Return True if the queue is full, False otherwise. If full()\nreturns True it doesn’t guarantee that a subsequent call to get()\nwill not block. Similarly, if full() returns False it doesn’t\nguarantee that a subsequent call to put() will not block.
\n\n
\n
\nQueue.put(item[, block[, timeout]])
\n

Put item into the queue. If optional args block is true and timeout is\nNone (the default), block if necessary until a free slot is available. If\ntimeout is a positive number, it blocks at most timeout seconds and raises\nthe Full exception if no free slot was available within that time.\nOtherwise (block is false), put an item on the queue if a free slot is\nimmediately available, else raise the Full exception (timeout is\nignored in that case).

\n

\nNew in version 2.3: The timeout parameter.

\n
\n\n
\n
\nQueue.put_nowait(item)
\n
Equivalent to put(item, False).
\n\n
\n
\nQueue.get([block[, timeout]])
\n

Remove and return an item from the queue. If optional args block is true and\ntimeout is None (the default), block if necessary until an item is available.\nIf timeout is a positive number, it blocks at most timeout seconds and\nraises the Empty exception if no item was available within that time.\nOtherwise (block is false), return an item if one is immediately available,\nelse raise the Empty exception (timeout is ignored in that case).

\n

\nNew in version 2.3: The timeout parameter.

\n
\n\n
\n
\nQueue.get_nowait()
\n
Equivalent to get(False).
\n\n

Two methods are offered to support tracking whether enqueued tasks have been\nfully processed by daemon consumer threads.

\n
\n
\nQueue.task_done()
\n

Indicate that a formerly enqueued task is complete. Used by queue consumer\nthreads. For each get() used to fetch a task, a subsequent call to\ntask_done() tells the queue that the processing on the task is complete.

\n

If a join() is currently blocking, it will resume when all items have been\nprocessed (meaning that a task_done() call was received for every item\nthat had been put() into the queue).

\n

Raises a ValueError if called more times than there were items placed in\nthe queue.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nQueue.join()
\n

Blocks until all items in the queue have been gotten and processed.

\n

The count of unfinished tasks goes up whenever an item is added to the queue.\nThe count goes down whenever a consumer thread calls task_done() to\nindicate that the item was retrieved and all work on it is complete. When the\ncount of unfinished tasks drops to zero, join() unblocks.

\n

\nNew in version 2.5.

\n
\n\n

Example of how to wait for enqueued tasks to be completed:

\n
def worker():\n    while True:\n        item = q.get()\n        do_work(item)\n        q.task_done()\n\nq = Queue()\nfor i in range(num_worker_threads):\n     t = Thread(target=worker)\n     t.daemon = True\n     t.start()\n\nfor item in source():\n    q.put(item)\n\nq.join()       # block until all tasks are done\n
\n
\n
\n
", "searchableItems": [ { "name": "Queue.LifoQueue", "domId": "Queue_Queue.LifoQueue" }, { "name": "Queue.PriorityQueue", "domId": "Queue_Queue.PriorityQueue" }, { "name": "Queue.Queue", "domId": "Queue_Queue.Queue" }, { "name": "Queue.Queue.empty", "domId": "Queue_Queue.Queue.empty" }, { "name": "Queue.Queue.full", "domId": "Queue_Queue.Queue.full" }, { "name": "Queue.Queue.get", "domId": "Queue_Queue.Queue.get" }, { "name": "Queue.Queue.get_nowait", "domId": "Queue_Queue.Queue.get_nowait" }, { "name": "Queue.Queue.join", "domId": "Queue_Queue.Queue.join" }, { "name": "Queue.Queue.put", "domId": "Queue_Queue.Queue.put" }, { "name": "Queue.Queue.put_nowait", "domId": "Queue_Queue.Queue.put_nowait" }, { "name": "Queue.Queue.qsize", "domId": "Queue_Queue.Queue.qsize" }, { "name": "Queue.Queue.task_done", "domId": "Queue_Queue.Queue.task_done" } ] }, { "url": "http://docs.python.org/library/new.html", "title": "new", "html": "
\n

8.16. new — Creation of runtime internal objects

\n

\nDeprecated since version 2.6: The new module has been removed in Python 3.0. Use the types\nmodule’s classes instead.

\n

The new module allows an interface to the interpreter object creation\nfunctions. This is for use primarily in marshal-type functions, when a new\nobject needs to be created “magically” and not by using the regular creation\nfunctions. This module provides a low-level interface to the interpreter, so\ncare must be exercised when using this module. It is possible to supply\nnon-sensical arguments which crash the interpreter when the object is used.

\n

The new module defines the following functions:

\n
\n
\nnew.instance(class[, dict])
\n
This function creates an instance of class with dictionary dict without\ncalling the __init__() constructor. If dict is omitted or None, a\nnew, empty dictionary is created for the new instance. Note that there are no\nguarantees that the object will be in a consistent state.
\n\n
\n
\nnew.instancemethod(function, instance, class)
\n
This function will return a method object, bound to instance, or unbound if\ninstance is None. function must be callable.
\n\n
\n
\nnew.function(code, globals[, name[, argdefs[, closure]]])
\n
Returns a (Python) function with the given code and globals. If name is given,\nit must be a string or None. If it is a string, the function will have the\ngiven name, otherwise the function name will be taken from code.co_name. If\nargdefs is given, it must be a tuple and will be used to determine the default\nvalues of parameters. If closure is given, it must be None or a tuple of\ncell objects containing objects to bind to the names in code.co_freevars.
\n\n
\n
\nnew.code(argcount, nlocals, stacksize, flags, codestring, constants, names, varnames, filename, name, firstlineno, lnotab)
\n
This function is an interface to the PyCode_New() C function.
\n\n
\n
\nnew.module(name[, doc])
\n
This function returns a new module object with name name. name must be a\nstring. The optional doc argument can have any type.
\n\n
\n
\nnew.classobj(name, baseclasses, dict)
\n
This function returns a new class object, with name name, derived from\nbaseclasses (which should be a tuple of classes) and with namespace dict.
\n\n
", "searchableItems": [ { "name": "new.classobj", "domId": "new_new.classobj" }, { "name": "new.code", "domId": "new_new.code" }, { "name": "new.function", "domId": "new_new.function" }, { "name": "new.instance", "domId": "new_new.instance" }, { "name": "new.instancemethod", "domId": "new_new.instancemethod" }, { "name": "new.module", "domId": "new_new.module" } ] }, { "url": "http://docs.python.org/library/types.html", "title": "types", "html": "
\n

8.15. types — Names for built-in types

\n

Source code: Lib/types.py

\n
\n

This module defines names for some object types that are used by the standard\nPython interpreter, but not for the types defined by various extension modules.\nAlso, it does not include some of the types that arise during processing such as\nthe listiterator type. It is safe to use from types import * — the\nmodule does not export any names besides the ones listed here. New names\nexported by future versions of this module will all end in Type.

\n

Typical use is for functions that do different things depending on their\nargument types, like the following:

\n
from types import *\ndef delete(mylist, item):\n    if type(item) is IntType:\n       del mylist[item]\n    else:\n       mylist.remove(item)\n
\n
\n

Starting in Python 2.2, built-in factory functions such as int() and\nstr() are also names for the corresponding types. This is now the\npreferred way to access the type instead of using the types module.\nAccordingly, the example above should be written as follows:

\n
def delete(mylist, item):\n    if isinstance(item, int):\n       del mylist[item]\n    else:\n       mylist.remove(item)\n
\n
\n

The module defines the following names:

\n
\n
\ntypes.NoneType
\n
The type of None.
\n\n
\n
\ntypes.TypeType
\n

The type of type objects (such as returned by type()); alias of the\nbuilt-in type.

\n
\n\n
\n
\ntypes.BooleanType
\n

The type of the bool values True and False; alias of the\nbuilt-in bool.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ntypes.IntType
\n
The type of integers (e.g. 1); alias of the built-in int.
\n\n
\n
\ntypes.LongType
\n
The type of long integers (e.g. 1L); alias of the built-in long.
\n\n
\n
\ntypes.FloatType
\n
The type of floating point numbers (e.g. 1.0); alias of the built-in\nfloat.
\n\n
\n
\ntypes.ComplexType
\n
The type of complex numbers (e.g. 1.0j). This is not defined if Python was\nbuilt without complex number support.
\n\n
\n
\ntypes.StringType
\n
The type of character strings (e.g. 'Spam'); alias of the built-in\nstr.
\n\n
\n
\ntypes.UnicodeType
\n
The type of Unicode character strings (e.g. u'Spam'). This is not defined\nif Python was built without Unicode support. It’s an alias of the built-in\nunicode.
\n\n
\n
\ntypes.TupleType
\n
The type of tuples (e.g. (1, 2, 3, 'Spam')); alias of the built-in\ntuple.
\n\n
\n
\ntypes.ListType
\n
The type of lists (e.g. [0, 1, 2, 3]); alias of the built-in\nlist.
\n\n
\n
\ntypes.DictType
\n
The type of dictionaries (e.g. {'Bacon': 1, 'Ham': 0}); alias of the\nbuilt-in dict.
\n\n
\n
\ntypes.DictionaryType
\n
An alternate name for DictType.
\n\n
\n
\ntypes.FunctionType
\n
\ntypes.LambdaType
\n
The type of user-defined functions and functions created by lambda\nexpressions.
\n\n
\n
\ntypes.GeneratorType
\n

The type of generator-iterator objects, produced by calling a\ngenerator function.

\n

\nNew in version 2.2.

\n
\n\n
\n
\ntypes.CodeType
\n

The type for code objects such as returned by compile().

\n
\n\n
\n
\ntypes.ClassType
\n
The type of user-defined old-style classes.
\n\n
\n
\ntypes.InstanceType
\n
The type of instances of user-defined classes.
\n\n
\n
\ntypes.MethodType
\n
The type of methods of user-defined class instances.
\n\n
\n
\ntypes.UnboundMethodType
\n
An alternate name for MethodType.
\n\n
\n
\ntypes.BuiltinFunctionType
\n
\ntypes.BuiltinMethodType
\n
The type of built-in functions like len() or sys.exit(), and\nmethods of built-in classes. (Here, the term “built-in” means “written in\nC”.)
\n\n
\n
\ntypes.ModuleType
\n
The type of modules.
\n\n
\n
\ntypes.FileType
\n
The type of open file objects such as sys.stdout; alias of the built-in\nfile.
\n\n
\n
\ntypes.XRangeType
\n

The type of range objects returned by xrange(); alias of the built-in\nxrange.

\n
\n\n
\n
\ntypes.SliceType
\n

The type of objects returned by slice(); alias of the built-in\nslice.

\n
\n\n
\n
\ntypes.EllipsisType
\n
The type of Ellipsis.
\n\n
\n
\ntypes.TracebackType
\n
The type of traceback objects such as found in sys.exc_traceback.
\n\n
\n
\ntypes.FrameType
\n
The type of frame objects such as found in tb.tb_frame if tb is a\ntraceback object.
\n\n
\n
\ntypes.BufferType
\n

The type of buffer objects created by the buffer() function.

\n
\n\n
\n
\ntypes.DictProxyType
\n
The type of dict proxies, such as TypeType.__dict__.
\n\n
\n
\ntypes.NotImplementedType
\n
The type of NotImplemented
\n\n
\n
\ntypes.GetSetDescriptorType
\n

The type of objects defined in extension modules with PyGetSetDef, such\nas FrameType.f_locals or array.array.typecode. This type is used as\ndescriptor for object attributes; it has the same purpose as the\nproperty type, but for classes defined in extension modules.

\n

\nNew in version 2.5.

\n
\n\n
\n
\ntypes.MemberDescriptorType
\n

The type of objects defined in extension modules with PyMemberDef, such\nas datetime.timedelta.days. This type is used as descriptor for simple C\ndata members which use standard conversion functions; it has the same purpose\nas the property type, but for classes defined in extension modules.

\n
\n

CPython implementation detail: In other implementations of Python, this type may be identical to\nGetSetDescriptorType.

\n
\n

\nNew in version 2.5.

\n
\n\n
\n
\ntypes.StringTypes
\n

A sequence containing StringType and UnicodeType used to facilitate\neasier checking for any string object. Using this is more portable than using a\nsequence of the two string types constructed elsewhere since it only contains\nUnicodeType if it has been built in the running version of Python. For\nexample: isinstance(s, types.StringTypes).

\n

\nNew in version 2.2.

\n
\n\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/userdict.html", "title": "UserDict", "html": "
\n

8.12. UserDict — Class wrapper for dictionary objects

\n

Source code: Lib/UserDict.py

\n
\n

The module defines a mixin, DictMixin, defining all dictionary methods\nfor classes that already have a minimum mapping interface. This greatly\nsimplifies writing classes that need to be substitutable for dictionaries (such\nas the shelve module).

\n

This module also defines a class, UserDict, that acts as a wrapper\naround dictionary objects. The need for this class has been largely supplanted\nby the ability to subclass directly from dict (a feature that became\navailable starting with Python version 2.2). Prior to the introduction of\ndict, the UserDict class was used to create dictionary-like\nsub-classes that obtained new behaviors by overriding existing methods or adding\nnew ones.

\n

The UserDict module defines the UserDict class and\nDictMixin:

\n
\n
\nclass UserDict.UserDict([initialdata])
\n

Class that simulates a dictionary. The instance’s contents are kept in a\nregular dictionary, which is accessible via the data attribute of\nUserDict instances. If initialdata is provided, data is\ninitialized with its contents; note that a reference to initialdata will not\nbe kept, allowing it be used for other purposes.

\n
\n

Note

\n

For backward compatibility, instances of UserDict are not iterable.

\n
\n
\n\n
\n
\nclass UserDict.IterableUserDict([initialdata])
\n
Subclass of UserDict that supports direct iteration (e.g. for key in\nmyDict).
\n\n

In addition to supporting the methods and operations of mappings (see section\nMapping Types — dict), UserDict and IterableUserDict instances\nprovide the following attribute:

\n
\n
\nIterableUserDict.data
\n
A real dictionary used to store the contents of the UserDict class.
\n\n
\n
\nclass UserDict.DictMixin
\n

Mixin defining all dictionary methods for classes that already have a minimum\ndictionary interface including __getitem__(), __setitem__(),\n__delitem__(), and keys().

\n

This mixin should be used as a superclass. Adding each of the above methods\nadds progressively more functionality. For instance, defining all but\n__delitem__() will preclude only pop() and popitem() from the\nfull interface.

\n

In addition to the four base methods, progressively more efficiency comes with\ndefining __contains__(), __iter__(), and iteritems().

\n

Since the mixin has no knowledge of the subclass constructor, it does not define\n__init__() or copy().

\n

Starting with Python version 2.6, it is recommended to use\ncollections.MutableMapping instead of DictMixin.

\n
\n\n
\n

8.13. UserList — Class wrapper for list objects

\n
\n

Note

\n

This module is available for backward compatibility only. If you are writing\ncode that does not need to work with versions of Python earlier than Python 2.2,\nplease consider subclassing directly from the built-in list type.

\n
\n

This module defines a class that acts as a wrapper around list objects. It is a\nuseful base class for your own list-like classes, which can inherit from them\nand override existing methods or add new ones. In this way one can add new\nbehaviors to lists.

\n

The UserList module defines the UserList class:

\n
\n
\nclass UserList.UserList([list])
\n

Class that simulates a list. The instance’s contents are kept in a regular\nlist, which is accessible via the data attribute of UserList\ninstances. The instance’s contents are initially set to a copy of list,\ndefaulting to the empty list []. list can be any iterable, e.g. a\nreal Python list or a UserList object.

\n
\n

Note

\n

The UserList class has been moved to the collections\nmodule in Python 3.0. The 2to3 tool will automatically adapt\nimports when converting your sources to 3.0.

\n
\n
\n\n

In addition to supporting the methods and operations of mutable sequences (see\nsection Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange), UserList instances provide the following\nattribute:

\n
\n
\nUserList.data
\n
A real Python list object used to store the contents of the UserList\nclass.
\n\n

Subclassing requirements: Subclasses of UserList are expect to\noffer a constructor which can be called with either no arguments or one\nargument. List operations which return a new sequence attempt to create an\ninstance of the actual implementation class. To do so, it assumes that the\nconstructor can be called with a single parameter, which is a sequence object\nused as a data source.

\n

If a derived class does not wish to comply with this requirement, all of the\nspecial methods supported by this class will need to be overridden; please\nconsult the sources for information about the methods which need to be provided\nin that case.

\n

\nChanged in version 2.0: Python versions 1.5.2 and 1.6 also required that the constructor be callable\nwith no parameters, and offer a mutable data attribute. Earlier\nversions of Python did not attempt to create instances of the derived class.

\n
\n

8.14. UserString — Class wrapper for string objects

\n
\n

Note

\n

This UserString class from this module is available for backward\ncompatibility only. If you are writing code that does not need to work with\nversions of Python earlier than Python 2.2, please consider subclassing directly\nfrom the built-in str type instead of using UserString (there\nis no built-in equivalent to MutableString).

\n
\n

This module defines a class that acts as a wrapper around string objects. It is\na useful base class for your own string-like classes, which can inherit from\nthem and override existing methods or add new ones. In this way one can add new\nbehaviors to strings.

\n

It should be noted that these classes are highly inefficient compared to real\nstring or Unicode objects; this is especially the case for\nMutableString.

\n

The UserString module defines the following classes:

\n
\n
\nclass UserString.UserString([sequence])
\n

Class that simulates a string or a Unicode string object. The instance’s\ncontent is kept in a regular string or Unicode string object, which is\naccessible via the data attribute of UserString instances. The\ninstance’s contents are initially set to a copy of sequence. sequence can\nbe either a regular Python string or Unicode string, an instance of\nUserString (or a subclass) or an arbitrary sequence which can be\nconverted into a string using the built-in str() function.

\n
\n

Note

\n

The UserString class has been moved to the collections\nmodule in Python 3.0. The 2to3 tool will automatically adapt\nimports when converting your sources to 3.0.

\n
\n
\n\n
\n
\nclass UserString.MutableString([sequence])
\n

This class is derived from the UserString above and redefines strings\nto be mutable. Mutable strings can’t be used as dictionary keys, because\ndictionaries require immutable objects as keys. The main intention of this\nclass is to serve as an educational example for inheritance and necessity to\nremove (override) the __hash__() method in order to trap attempts to use a\nmutable object as dictionary key, which would be otherwise very error prone and\nhard to track down.

\n

\nDeprecated since version 2.6: The MutableString class has been removed in Python 3.0.

\n
\n\n

In addition to supporting the methods and operations of string and Unicode\nobjects (see section String Methods), UserString instances\nprovide the following attribute:

\n
\n
\nMutableString.data
\n
A real Python string or Unicode object used to store the content of the\nUserString class.
\n\n
", "searchableItems": [ { "name": "UserDict.DictMixin", "domId": "UserDict_UserDict.DictMixin" }, { "name": "UserDict.IterableUserDict", "domId": "UserDict_UserDict.IterableUserDict" }, { "name": "UserDict.UserDict", "domId": "UserDict_UserDict.UserDict" }, { "name": "UserList.UserList", "domId": "UserDict_UserList.UserList" }, { "name": "UserString.MutableString", "domId": "UserDict_UserString.MutableString" }, { "name": "UserString.UserString", "domId": "UserDict_UserString.UserString" } ] }, { "url": "http://docs.python.org/library/weakref.html", "title": "weakref", "html": "
\n

8.11. weakref — Weak references

\n

\nNew in version 2.1.

\n

Source code: Lib/weakref.py

\n
\n

The weakref module allows the Python programmer to create weak\nreferences to objects.

\n

In the following, the term referent means the object which is referred to\nby a weak reference.

\n

A weak reference to an object is not enough to keep the object alive: when the\nonly remaining references to a referent are weak references,\ngarbage collection is free to destroy the referent and reuse its memory\nfor something else. A primary use for weak references is to implement caches or\nmappings holding large objects, where it’s desired that a large object not be\nkept alive solely because it appears in a cache or mapping.

\n

For example, if you have a number of large binary image objects, you may wish to\nassociate a name with each. If you used a Python dictionary to map names to\nimages, or images to names, the image objects would remain alive just because\nthey appeared as values or keys in the dictionaries. The\nWeakKeyDictionary and WeakValueDictionary classes supplied by\nthe weakref module are an alternative, using weak references to construct\nmappings that don’t keep objects alive solely because they appear in the mapping\nobjects. If, for example, an image object is a value in a\nWeakValueDictionary, then when the last remaining references to that\nimage object are the weak references held by weak mappings, garbage collection\ncan reclaim the object, and its corresponding entries in weak mappings are\nsimply deleted.

\n

WeakKeyDictionary and WeakValueDictionary use weak references\nin their implementation, setting up callback functions on the weak references\nthat notify the weak dictionaries when a key or value has been reclaimed by\ngarbage collection. Most programs should find that using one of these weak\ndictionary types is all they need – it’s not usually necessary to create your\nown weak references directly. The low-level machinery used by the weak\ndictionary implementations is exposed by the weakref module for the\nbenefit of advanced uses.

\n
\n

Note

\n

Weak references to an object are cleared before the object’s __del__()\nis called, to ensure that the weak reference callback (if any) finds the\nobject still alive.

\n
\n

Not all objects can be weakly referenced; those objects which can include class\ninstances, functions written in Python (but not in C), methods (both bound and\nunbound), sets, frozensets, file objects, generators, type objects,\nDBcursor objects from the bsddb module, sockets, arrays, deques,\nregular expression pattern objects, and code objects.

\n

\nChanged in version 2.4: Added support for files, sockets, arrays, and patterns.

\n

\nChanged in version 2.7: Added support for thread.lock, threading.Lock, and code objects.

\n

Several built-in types such as list and dict do not directly\nsupport weak references but can add support through subclassing:

\n
class Dict(dict):\n    pass\n\nobj = Dict(red=1, green=2, blue=3)   # this object is weak referenceable\n
\n
\n
\n

CPython implementation detail: Other built-in types such as tuple and long do not support\nweak references even when subclassed.

\n
\n

Extension types can easily be made to support weak references; see\nWeak Reference Support.

\n
\n
\nclass weakref.ref(object[, callback])
\n

Return a weak reference to object. The original object can be retrieved by\ncalling the reference object if the referent is still alive; if the referent is\nno longer alive, calling the reference object will cause None to be\nreturned. If callback is provided and not None, and the returned\nweakref object is still alive, the callback will be called when the object is\nabout to be finalized; the weak reference object will be passed as the only\nparameter to the callback; the referent will no longer be available.

\n

It is allowable for many weak references to be constructed for the same object.\nCallbacks registered for each weak reference will be called from the most\nrecently registered callback to the oldest registered callback.

\n

Exceptions raised by the callback will be noted on the standard error output,\nbut cannot be propagated; they are handled in exactly the same way as exceptions\nraised from an object’s __del__() method.

\n

Weak references are hashable if the object is hashable. They will maintain\ntheir hash value even after the object was deleted. If hash() is called\nthe first time only after the object was deleted, the call will raise\nTypeError.

\n

Weak references support tests for equality, but not ordering. If the referents\nare still alive, two references have the same equality relationship as their\nreferents (regardless of the callback). If either referent has been deleted,\nthe references are equal only if the reference objects are the same object.

\n

\nChanged in version 2.4: This is now a subclassable type rather than a factory function; it derives from\nobject.

\n
\n\n
\n
\nweakref.proxy(object[, callback])
\n
Return a proxy to object which uses a weak reference. This supports use of\nthe proxy in most contexts instead of requiring the explicit dereferencing used\nwith weak reference objects. The returned object will have a type of either\nProxyType or CallableProxyType, depending on whether object is\ncallable. Proxy objects are not hashable regardless of the referent; this\navoids a number of problems related to their fundamentally mutable nature, and\nprevent their use as dictionary keys. callback is the same as the parameter\nof the same name to the ref() function.
\n\n
\n
\nweakref.getweakrefcount(object)
\n
Return the number of weak references and proxies which refer to object.
\n\n
\n
\nweakref.getweakrefs(object)
\n
Return a list of all weak reference and proxy objects which refer to object.
\n\n
\n
\nclass weakref.WeakKeyDictionary([dict])
\n

Mapping class that references keys weakly. Entries in the dictionary will be\ndiscarded when there is no longer a strong reference to the key. This can be\nused to associate additional data with an object owned by other parts of an\napplication without adding attributes to those objects. This can be especially\nuseful with objects that override attribute accesses.

\n
\n

Note

\n

Caution: Because a WeakKeyDictionary is built on top of a Python\ndictionary, it must not change size when iterating over it. This can be\ndifficult to ensure for a WeakKeyDictionary because actions\nperformed by the program during iteration may cause items in the\ndictionary to vanish “by magic” (as a side effect of garbage collection).

\n
\n
\n\n

WeakKeyDictionary objects have the following additional methods. These\nexpose the internal references directly. The references are not guaranteed to\nbe “live” at the time they are used, so the result of calling the references\nneeds to be checked before being used. This can be used to avoid creating\nreferences that will cause the garbage collector to keep the keys around longer\nthan needed.

\n
\n
\nWeakKeyDictionary.iterkeyrefs()
\n

Return an iterator that yields the weak references to the keys.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nWeakKeyDictionary.keyrefs()
\n

Return a list of weak references to the keys.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nclass weakref.WeakValueDictionary([dict])
\n

Mapping class that references values weakly. Entries in the dictionary will be\ndiscarded when no strong reference to the value exists any more.

\n
\n

Note

\n

Caution: Because a WeakValueDictionary is built on top of a Python\ndictionary, it must not change size when iterating over it. This can be\ndifficult to ensure for a WeakValueDictionary because actions performed\nby the program during iteration may cause items in the dictionary to vanish “by\nmagic” (as a side effect of garbage collection).

\n
\n
\n\n

WeakValueDictionary objects have the following additional methods.\nThese method have the same issues as the iterkeyrefs() and keyrefs()\nmethods of WeakKeyDictionary objects.

\n
\n
\nWeakValueDictionary.itervaluerefs()
\n

Return an iterator that yields the weak references to the values.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nWeakValueDictionary.valuerefs()
\n

Return a list of weak references to the values.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nclass weakref.WeakSet([elements])
\n

Set class that keeps weak references to its elements. An element will be\ndiscarded when no strong reference to it exists any more.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nweakref.ReferenceType
\n
The type object for weak references objects.
\n\n
\n
\nweakref.ProxyType
\n
The type object for proxies of objects which are not callable.
\n\n
\n
\nweakref.CallableProxyType
\n
The type object for proxies of callable objects.
\n\n
\n
\nweakref.ProxyTypes
\n
Sequence containing all the type objects for proxies. This can make it simpler\nto test if an object is a proxy without being dependent on naming both proxy\ntypes.
\n\n
\n
\nexception weakref.ReferenceError
\n
Exception raised when a proxy object is used but the underlying object has been\ncollected. This is the same as the standard ReferenceError exception.
\n\n
\n

See also

\n
\n
PEP 0205 - Weak References
\n
The proposal and rationale for this feature, including links to earlier\nimplementations and information about similar features in other languages.
\n
\n
\n
\n

8.11.1. Weak Reference Objects

\n

Weak reference objects have no attributes or methods, but do allow the referent\nto be obtained, if it still exists, by calling it:

\n
>>> import weakref\n>>> class Object:\n...     pass\n...\n>>> o = Object()\n>>> r = weakref.ref(o)\n>>> o2 = r()\n>>> o is o2\nTrue\n
\n
\n

If the referent no longer exists, calling the reference object returns\nNone:

\n
>>> del o, o2\n>>> print r()\nNone\n
\n
\n

Testing that a weak reference object is still live should be done using the\nexpression ref() is not None. Normally, application code that needs to use\na reference object should follow this pattern:

\n
# r is a weak reference object\no = r()\nif o is None:\n    # referent has been garbage collected\n    print "Object has been deallocated; can't frobnicate."\nelse:\n    print "Object is still live!"\n    o.do_something_useful()\n
\n
\n

Using a separate test for “liveness” creates race conditions in threaded\napplications; another thread can cause a weak reference to become invalidated\nbefore the weak reference is called; the idiom shown above is safe in threaded\napplications as well as single-threaded applications.

\n

Specialized versions of ref objects can be created through subclassing.\nThis is used in the implementation of the WeakValueDictionary to reduce\nthe memory overhead for each entry in the mapping. This may be most useful to\nassociate additional information with a reference, but could also be used to\ninsert additional processing on calls to retrieve the referent.

\n

This example shows how a subclass of ref can be used to store\nadditional information about an object and affect the value that’s returned when\nthe referent is accessed:

\n
import weakref\n\nclass ExtendedRef(weakref.ref):\n    def __init__(self, ob, callback=None, **annotations):\n        super(ExtendedRef, self).__init__(ob, callback)\n        self.__counter = 0\n        for k, v in annotations.iteritems():\n            setattr(self, k, v)\n\n    def __call__(self):\n        """Return a pair containing the referent and the number of\n        times the reference has been called.\n        """\n        ob = super(ExtendedRef, self).__call__()\n        if ob is not None:\n            self.__counter += 1\n            ob = (ob, self.__counter)\n        return ob\n
\n
\n
\n
\n

8.11.2. Example

\n

This simple example shows how an application can use objects IDs to retrieve\nobjects that it has seen before. The IDs of the objects can then be used in\nother data structures without forcing the objects to remain alive, but the\nobjects can still be retrieved by ID if they do.

\n
import weakref\n\n_id2obj_dict = weakref.WeakValueDictionary()\n\ndef remember(obj):\n    oid = id(obj)\n    _id2obj_dict[oid] = obj\n    return oid\n\ndef id2obj(oid):\n    return _id2obj_dict[oid]\n
\n
\n
\n
", "searchableItems": [ { "name": "weakref.getweakrefcount", "domId": "weakref_weakref.getweakrefcount" }, { "name": "weakref.getweakrefs", "domId": "weakref_weakref.getweakrefs" }, { "name": "weakref.proxy", "domId": "weakref_weakref.proxy" }, { "name": "weakref.ref", "domId": "weakref_weakref.ref" }, { "name": "weakref.WeakKeyDictionary", "domId": "weakref_weakref.WeakKeyDictionary" }, { "name": "weakref.WeakKeyDictionary.iterkeyrefs", "domId": "weakref_weakref.WeakKeyDictionary.iterkeyrefs" }, { "name": "weakref.WeakKeyDictionary.keyrefs", "domId": "weakref_weakref.WeakKeyDictionary.keyrefs" }, { "name": "weakref.WeakSet", "domId": "weakref_weakref.WeakSet" }, { "name": "weakref.WeakValueDictionary", "domId": "weakref_weakref.WeakValueDictionary" }, { "name": "weakref.WeakValueDictionary.itervaluerefs", "domId": "weakref_weakref.WeakValueDictionary.itervaluerefs" }, { "name": "weakref.WeakValueDictionary.valuerefs", "domId": "weakref_weakref.WeakValueDictionary.valuerefs" } ] }, { "url": "http://docs.python.org/library/copy.html", "title": "copy", "html": "
\n

8.17. copy — Shallow and deep copy operations

\n

This module provides generic (shallow and deep) copying operations.

\n

Interface summary:

\n
\n
\ncopy.copy(x)
\n
Return a shallow copy of x.
\n\n
\n
\ncopy.deepcopy(x)
\n
Return a deep copy of x.
\n\n
\n
\nexception copy.error
\n
Raised for module specific errors.
\n\n

The difference between shallow and deep copying is only relevant for compound\nobjects (objects that contain other objects, like lists or class instances):

\n\n

Two problems often exist with deep copy operations that don’t exist with shallow\ncopy operations:

\n\n

The deepcopy() function avoids these problems by:

\n\n

This module does not copy types like module, method, stack trace, stack frame,\nfile, socket, window, array, or any similar types. It does “copy” functions and\nclasses (shallow and deeply), by returning the original object unchanged; this\nis compatible with the way these are treated by the pickle module.

\n

Shallow copies of dictionaries can be made using dict.copy(), and\nof lists by assigning a slice of the entire list, for example,\ncopied_list = original_list[:].

\n

\nChanged in version 2.5: Added copying functions.

\n

Classes can use the same interfaces to control copying that they use to control\npickling. See the description of module pickle for information on these\nmethods. The copy module does not use the copy_reg registration\nmodule.

\n

In order for a class to define its own copy implementation, it can define\nspecial methods __copy__() and __deepcopy__(). The former is called\nto implement the shallow copy operation; no additional arguments are passed.\nThe latter is called to implement the deep copy operation; it is passed one\nargument, the memo dictionary. If the __deepcopy__() implementation needs\nto make a deep copy of a component, it should call the deepcopy() function\nwith the component as first argument and the memo dictionary as second argument.

\n
\n

See also

\n
\n
Module pickle
\n
Discussion of the special methods used to support object state retrieval and\nrestoration.
\n
\n
\n
", "searchableItems": [ { "name": "copy.copy", "domId": "copy_copy.copy" }, { "name": "copy.deepcopy", "domId": "copy_copy.deepcopy" } ] }, { "url": "http://docs.python.org/library/repr.html", "title": "repr", "html": "
\n

8.19. repr — Alternate repr() implementation

\n
\n

Note

\n

The repr module has been renamed to reprlib in Python 3.0. The\n2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n

Source code: Lib/repr.py

\n
\n

The repr module provides a means for producing object representations\nwith limits on the size of the resulting strings. This is used in the Python\ndebugger and may be useful in other contexts as well.

\n

This module provides a class, an instance, and a function:

\n
\n
\nclass repr.Repr
\n
Class which provides formatting services useful in implementing functions\nsimilar to the built-in repr(); size limits for different object types\nare added to avoid the generation of representations which are excessively long.
\n\n
\n
\nrepr.aRepr
\n
This is an instance of Repr which is used to provide the repr()\nfunction described below. Changing the attributes of this object will affect\nthe size limits used by repr() and the Python debugger.
\n\n
\n
\nrepr.repr(obj)
\n
This is the repr() method of aRepr. It returns a string\nsimilar to that returned by the built-in function of the same name, but with\nlimits on most sizes.
\n\n
\n

8.19.1. Repr Objects

\n

Repr instances provide several attributes which can be used to provide\nsize limits for the representations of different object types, and methods\nwhich format specific object types.

\n
\n
\nRepr.maxlevel
\n
Depth limit on the creation of recursive representations. The default is 6.
\n\n
\n
\nRepr.maxdict
\n
\nRepr.maxlist
\n
\nRepr.maxtuple
\n
\nRepr.maxset
\n
\nRepr.maxfrozenset
\n
\nRepr.maxdeque
\n
\nRepr.maxarray
\n

Limits on the number of entries represented for the named object type. The\ndefault is 4 for maxdict, 5 for maxarray, and 6 for\nthe others.

\n

\nNew in version 2.4: maxset, maxfrozenset, and set.

\n
\n\n
\n
\nRepr.maxlong
\n
Maximum number of characters in the representation for a long integer. Digits\nare dropped from the middle. The default is 40.
\n\n
\n
\nRepr.maxstring
\n
Limit on the number of characters in the representation of the string. Note\nthat the “normal” representation of the string is used as the character source:\nif escape sequences are needed in the representation, these may be mangled when\nthe representation is shortened. The default is 30.
\n\n
\n
\nRepr.maxother
\n
This limit is used to control the size of object types for which no specific\nformatting method is available on the Repr object. It is applied in a\nsimilar manner as maxstring. The default is 20.
\n\n
\n
\nRepr.repr(obj)
\n
The equivalent to the built-in repr() that uses the formatting imposed by\nthe instance.
\n\n
\n
\nRepr.repr1(obj, level)
\n
Recursive implementation used by repr(). This uses the type of obj to\ndetermine which formatting method to call, passing it obj and level. The\ntype-specific methods should call repr1() to perform recursive formatting,\nwith level - 1 for the value of level in the recursive call.
\n\n
\n
\nRepr.repr_TYPE(obj, level)
\n
Formatting methods for specific types are implemented as methods with a name\nbased on the type name. In the method name, TYPE is replaced by\nstring.join(string.split(type(obj).__name__, '_')). Dispatch to these\nmethods is handled by repr1(). Type-specific methods which need to\nrecursively format a value should call self.repr1(subobj, level - 1).
\n\n
\n
\n

8.19.2. Subclassing Repr Objects

\n

The use of dynamic dispatching by Repr.repr1() allows subclasses of\nRepr to add support for additional built-in object types or to modify\nthe handling of types already supported. This example shows how special support\nfor file objects could be added:

\n
import repr as reprlib\nimport sys\n\nclass MyRepr(reprlib.Repr):\n    def repr_file(self, obj, level):\n        if obj.name in ['<stdin>', '<stdout>', '<stderr>']:\n            return obj.name\n        else:\n            return repr(obj)\n\naRepr = MyRepr()\nprint aRepr.repr(sys.stdin)          # prints '<stdin>'\n
\n
\n
\n
", "searchableItems": [ { "name": "repr.Repr", "domId": "repr_repr_repr.Repr" }, { "name": "repr.repr", "domId": "repr_repr.repr" }, { "name": "repr.Repr", "domId": "repr_repr_repr.Repr" }, { "name": "repr.Repr.repr", "domId": "repr_repr.Repr.repr" }, { "name": "repr.Repr.repr1", "domId": "repr_repr.Repr.repr1" } ] }, { "url": "http://docs.python.org/library/pprint.html", "title": "pprint", "html": "
\n

8.18. pprint — Data pretty printer

\n

Source code: Lib/pprint.py

\n
\n

The pprint module provides a capability to “pretty-print” arbitrary\nPython data structures in a form which can be used as input to the interpreter.\nIf the formatted structures include objects which are not fundamental Python\ntypes, the representation may not be loadable. This may be the case if objects\nsuch as files, sockets, classes, or instances are included, as well as many\nother built-in objects which are not representable as Python constants.

\n

The formatted representation keeps objects on a single line if it can, and\nbreaks them onto multiple lines if they don’t fit within the allowed width.\nConstruct PrettyPrinter objects explicitly if you need to adjust the\nwidth constraint.

\n

\nChanged in version 2.5: Dictionaries are sorted by key before the display is computed; before 2.5, a\ndictionary was sorted only if its display required more than one line, although\nthat wasn’t documented.

\n

\nChanged in version 2.6: Added support for set and frozenset.

\n

The pprint module defines one class:

\n
\n
\nclass pprint.PrettyPrinter(...)
\n

Construct a PrettyPrinter instance. This constructor understands\nseveral keyword parameters. An output stream may be set using the stream\nkeyword; the only method used on the stream object is the file protocol’s\nwrite() method. If not specified, the PrettyPrinter adopts\nsys.stdout. Three additional parameters may be used to control the\nformatted representation. The keywords are indent, depth, and width. The\namount of indentation added for each recursive level is specified by indent;\nthe default is one. Other values can cause output to look a little odd, but can\nmake nesting easier to spot. The number of levels which may be printed is\ncontrolled by depth; if the data structure being printed is too deep, the next\ncontained level is replaced by .... By default, there is no constraint on\nthe depth of the objects being formatted. The desired output width is\nconstrained using the width parameter; the default is 80 characters. If a\nstructure cannot be formatted within the constrained width, a best effort will\nbe made.

\n
>>> import pprint\n>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']\n>>> stuff.insert(0, stuff[:])\n>>> pp = pprint.PrettyPrinter(indent=4)\n>>> pp.pprint(stuff)\n[   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],\n    'spam',\n    'eggs',\n    'lumberjack',\n    'knights',\n    'ni']\n>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',\n... ('parrot', ('fresh fruit',))))))))\n>>> pp = pprint.PrettyPrinter(depth=6)\n>>> pp.pprint(tup)\n('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))\n
\n
\n
\n\n

The PrettyPrinter class supports several derivative functions:

\n
\n
\npprint.pformat(object[, indent[, width[, depth]]])
\n

Return the formatted representation of object as a string. indent, width\nand depth will be passed to the PrettyPrinter constructor as\nformatting parameters.

\n

\nChanged in version 2.4: The parameters indent, width and depth were added.

\n
\n\n
\n
\npprint.pprint(object[, stream[, indent[, width[, depth]]]])
\n

Prints the formatted representation of object on stream, followed by a\nnewline. If stream is omitted, sys.stdout is used. This may be used in\nthe interactive interpreter instead of a print statement for\ninspecting values. indent, width and depth will be passed to the\nPrettyPrinter constructor as formatting parameters.

\n
>>> import pprint\n>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']\n>>> stuff.insert(0, stuff)\n>>> pprint.pprint(stuff)\n[<Recursion on list with id=...>,\n 'spam',\n 'eggs',\n 'lumberjack',\n 'knights',\n 'ni']\n
\n
\n

\nChanged in version 2.4: The parameters indent, width and depth were added.

\n
\n\n
\n
\npprint.isreadable(object)
\n

Determine if the formatted representation of object is “readable,” or can be\nused to reconstruct the value using eval(). This always returns False\nfor recursive objects.

\n
>>> pprint.isreadable(stuff)\nFalse\n
\n
\n
\n\n
\n
\npprint.isrecursive(object)
\n
Determine if object requires a recursive representation.
\n\n

One more support function is also defined:

\n
\n
\npprint.saferepr(object)
\n

Return a string representation of object, protected against recursive data\nstructures. If the representation of object exposes a recursive entry, the\nrecursive reference will be represented as <Recursion on typename with\nid=number>. The representation is not otherwise formatted.

\n
>>> pprint.saferepr(stuff)\n"[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"\n
\n
\n
\n\n
\n

8.18.1. PrettyPrinter Objects

\n

PrettyPrinter instances have the following methods:

\n
\n
\nPrettyPrinter.pformat(object)
\n
Return the formatted representation of object. This takes into account the\noptions passed to the PrettyPrinter constructor.
\n\n
\n
\nPrettyPrinter.pprint(object)
\n
Print the formatted representation of object on the configured stream,\nfollowed by a newline.
\n\n

The following methods provide the implementations for the corresponding\nfunctions of the same names. Using these methods on an instance is slightly\nmore efficient since new PrettyPrinter objects don’t need to be\ncreated.

\n
\n
\nPrettyPrinter.isreadable(object)
\n

Determine if the formatted representation of the object is “readable,” or can be\nused to reconstruct the value using eval(). Note that this returns\nFalse for recursive objects. If the depth parameter of the\nPrettyPrinter is set and the object is deeper than allowed, this\nreturns False.

\n
\n\n
\n
\nPrettyPrinter.isrecursive(object)
\n
Determine if the object requires a recursive representation.
\n\n

This method is provided as a hook to allow subclasses to modify the way objects\nare converted to strings. The default implementation uses the internals of the\nsaferepr() implementation.

\n
\n
\nPrettyPrinter.format(object, context, maxlevels, level)
\n

Returns three values: the formatted version of object as a string, a flag\nindicating whether the result is readable, and a flag indicating whether\nrecursion was detected. The first argument is the object to be presented. The\nsecond is a dictionary which contains the id() of objects that are part of\nthe current presentation context (direct and indirect containers for object\nthat are affecting the presentation) as the keys; if an object needs to be\npresented which is already represented in context, the third return value\nshould be True. Recursive calls to the format() method should add\nadditional entries for containers to this dictionary. The third argument,\nmaxlevels, gives the requested limit to recursion; this will be 0 if there\nis no requested limit. This argument should be passed unmodified to recursive\ncalls. The fourth argument, level, gives the current level; recursive calls\nshould be passed a value less than that of the current call.

\n

\nNew in version 2.3.

\n
\n\n
\n
\n

8.18.2. pprint Example

\n

This example demonstrates several uses of the pprint() function and its parameters.

\n
>>> import pprint\n>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',\n... ('parrot', ('fresh fruit',))))))))\n>>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]\n>>> pprint.pprint(stuff)\n['aaaaaaaaaa',\n ('spam',\n  ('eggs',\n   ('lumberjack',\n    ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),\n ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],\n ['cccccccccccccccccccc', 'dddddddddddddddddddd']]\n>>> pprint.pprint(stuff, depth=3)\n['aaaaaaaaaa',\n ('spam', ('eggs', (...))),\n ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],\n ['cccccccccccccccccccc', 'dddddddddddddddddddd']]\n>>> pprint.pprint(stuff, width=60)\n['aaaaaaaaaa',\n ('spam',\n  ('eggs',\n   ('lumberjack',\n    ('knights',\n     ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),\n ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',\n  'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],\n ['cccccccccccccccccccc', 'dddddddddddddddddddd']]\n
\n
\n
\n
", "searchableItems": [ { "name": "pprint.isreadable", "domId": "pprint_pprint.isreadable" }, { "name": "pprint.isrecursive", "domId": "pprint_pprint.isrecursive" }, { "name": "pprint.pformat", "domId": "pprint_pprint.pformat" }, { "name": "pprint.pprint", "domId": "pprint_pprint.pprint" }, { "name": "pprint.PrettyPrinter", "domId": "pprint_pprint.PrettyPrinter" }, { "name": "pprint.PrettyPrinter.format", "domId": "pprint_pprint.PrettyPrinter.format" }, { "name": "pprint.PrettyPrinter.isreadable", "domId": "pprint_pprint.PrettyPrinter.isreadable" }, { "name": "pprint.PrettyPrinter.isrecursive", "domId": "pprint_pprint.PrettyPrinter.isrecursive" }, { "name": "pprint.PrettyPrinter.pformat", "domId": "pprint_pprint.PrettyPrinter.pformat" }, { "name": "pprint.PrettyPrinter.pprint", "domId": "pprint_pprint.PrettyPrinter.pprint" }, { "name": "pprint.saferepr", "domId": "pprint_pprint.saferepr" } ] }, { "url": "http://docs.python.org/library/numbers.html", "title": "numbers", "html": "
\n

9.1. numbers — Numeric abstract base classes

\n

\nNew in version 2.6.

\n

The numbers module (PEP 3141) defines a hierarchy of numeric\nabstract base classes which progressively define\nmore operations. None of the types defined in this module can be instantiated.

\n
\n
\nclass numbers.Number
\n
The root of the numeric hierarchy. If you just want to check if an argument\nx is a number, without caring what kind, use isinstance(x, Number).
\n\n
\n

9.1.1. The numeric tower

\n
\n
\nclass numbers.Complex
\n

Subclasses of this type describe complex numbers and include the operations\nthat work on the built-in complex type. These are: conversions to\ncomplex and bool, real, imag, +,\n-, *, /, abs(), conjugate(), ==, and !=. All\nexcept - and != are abstract.

\n
\n
\nreal
\n
Abstract. Retrieves the real component of this number.
\n\n
\n
\nimag
\n
Abstract. Retrieves the imaginary component of this number.
\n\n
\n
\nconjugate()
\n
Abstract. Returns the complex conjugate. For example, (1+3j).conjugate()\n== (1-3j).
\n\n
\n\n
\n
\nclass numbers.Real
\n

To Complex, Real adds the operations that work on real\nnumbers.

\n

In short, those are: a conversion to float, math.trunc(),\nround(), math.floor(), math.ceil(), divmod(), //,\n, <, <=, >, and >=.

\n

Real also provides defaults for complex(), real,\nimag, and conjugate().

\n
\n\n
\n
\nclass numbers.Rational
\n

Subtypes Real and adds\nnumerator and denominator properties, which\nshould be in lowest terms. With these, it provides a default for\nfloat().

\n
\n
\nnumerator
\n
Abstract.
\n\n
\n
\ndenominator
\n
Abstract.
\n\n
\n\n
\n
\nclass numbers.Integral
\n
Subtypes Rational and adds a conversion to int.\nProvides defaults for float(), numerator, and\ndenominator, and bit-string operations: <<,\n>>, &, ^, |, ~.
\n\n
\n
\n

9.1.2. Notes for type implementors

\n

Implementors should be careful to make equal numbers equal and hash\nthem to the same values. This may be subtle if there are two different\nextensions of the real numbers. For example, fractions.Fraction\nimplements hash() as follows:

\n
def __hash__(self):\n    if self.denominator == 1:\n        # Get integers right.\n        return hash(self.numerator)\n    # Expensive check, but definitely correct.\n    if self == float(self):\n        return hash(float(self))\n    else:\n        # Use tuple's hash to avoid a high collision rate on\n        # simple fractions.\n        return hash((self.numerator, self.denominator))\n
\n
\n
\n

9.1.2.1. Adding More Numeric ABCs

\n

There are, of course, more possible ABCs for numbers, and this would\nbe a poor hierarchy if it precluded the possibility of adding\nthose. You can add MyFoo between Complex and\nReal with:

\n
class MyFoo(Complex): ...\nMyFoo.register(Real)\n
\n
\n
\n
\n

9.1.2.2. Implementing the arithmetic operations

\n

We want to implement the arithmetic operations so that mixed-mode\noperations either call an implementation whose author knew about the\ntypes of both arguments, or convert both to the nearest built in type\nand do the operation there. For subtypes of Integral, this\nmeans that __add__() and __radd__() should be defined as:

\n
class MyIntegral(Integral):\n\n    def __add__(self, other):\n        if isinstance(other, MyIntegral):\n            return do_my_adding_stuff(self, other)\n        elif isinstance(other, OtherTypeIKnowAbout):\n            return do_my_other_adding_stuff(self, other)\n        else:\n            return NotImplemented\n\n    def __radd__(self, other):\n        if isinstance(other, MyIntegral):\n            return do_my_adding_stuff(other, self)\n        elif isinstance(other, OtherTypeIKnowAbout):\n            return do_my_other_adding_stuff(other, self)\n        elif isinstance(other, Integral):\n            return int(other) + int(self)\n        elif isinstance(other, Real):\n            return float(other) + float(self)\n        elif isinstance(other, Complex):\n            return complex(other) + complex(self)\n        else:\n            return NotImplemented\n
\n
\n

There are 5 different cases for a mixed-type operation on subclasses\nof Complex. I’ll refer to all of the above code that doesn’t\nrefer to MyIntegral and OtherTypeIKnowAbout as\n“boilerplate”. a will be an instance of A, which is a subtype\nof Complex (a : A <: Complex), and b : B <:\nComplex. I’ll consider a + b:

\n
\n
    \n
  1. If A defines an __add__() which accepts b, all is\nwell.
  2. \n
  3. If A falls back to the boilerplate code, and it were to\nreturn a value from __add__(), we’d miss the possibility\nthat B defines a more intelligent __radd__(), so the\nboilerplate should return NotImplemented from\n__add__(). (Or A may not implement __add__() at\nall.)
  4. \n
  5. Then B‘s __radd__() gets a chance. If it accepts\na, all is well.
  6. \n
  7. If it falls back to the boilerplate, there are no more possible\nmethods to try, so this is where the default implementation\nshould live.
  8. \n
  9. If B <: A, Python tries B.__radd__ before\nA.__add__. This is ok, because it was implemented with\nknowledge of A, so it can handle those instances before\ndelegating to Complex.
  10. \n
\n
\n

If A <: Complex and B <: Real without sharing any other knowledge,\nthen the appropriate shared operation is the one involving the built\nin complex, and both __radd__() s land there, so a+b\n== b+a.

\n

Because most of the operations on any given type will be very similar,\nit can be useful to define a helper function which generates the\nforward and reverse instances of any given operator. For example,\nfractions.Fraction uses:

\n
def _operator_fallbacks(monomorphic_operator, fallback_operator):\n    def forward(a, b):\n        if isinstance(b, (int, long, Fraction)):\n            return monomorphic_operator(a, b)\n        elif isinstance(b, float):\n            return fallback_operator(float(a), b)\n        elif isinstance(b, complex):\n            return fallback_operator(complex(a), b)\n        else:\n            return NotImplemented\n    forward.__name__ = '__' + fallback_operator.__name__ + '__'\n    forward.__doc__ = monomorphic_operator.__doc__\n\n    def reverse(b, a):\n        if isinstance(a, Rational):\n            # Includes ints.\n            return monomorphic_operator(a, b)\n        elif isinstance(a, numbers.Real):\n            return fallback_operator(float(a), float(b))\n        elif isinstance(a, numbers.Complex):\n            return fallback_operator(complex(a), complex(b))\n        else:\n            return NotImplemented\n    reverse.__name__ = '__r' + fallback_operator.__name__ + '__'\n    reverse.__doc__ = monomorphic_operator.__doc__\n\n    return forward, reverse\n\ndef _add(a, b):\n    """a + b"""\n    return Fraction(a.numerator * b.denominator +\n                    b.numerator * a.denominator,\n                    a.denominator * b.denominator)\n\n__add__, __radd__ = _operator_fallbacks(_add, operator.add)\n\n# ...\n
\n
\n
\n
\n
", "searchableItems": [ { "name": "numbers.Complex", "domId": "numbers_numbers.Complex" }, { "name": "numbers.Complex.conjugate", "domId": "numbers_numbers.Complex.conjugate" }, { "name": "numbers.Integral", "domId": "numbers_numbers.Integral" }, { "name": "numbers.Number", "domId": "numbers_numbers.Number" }, { "name": "numbers.Rational", "domId": "numbers_numbers.Rational" }, { "name": "numbers.Real", "domId": "numbers_numbers.Real" } ] }, { "url": "http://docs.python.org/library/math.html", "title": "math", "html": "
\n

9.2. math — Mathematical functions

\n

This module is always available. It provides access to the mathematical\nfunctions defined by the C standard.

\n

These functions cannot be used with complex numbers; use the functions of the\nsame name from the cmath module if you require support for complex\nnumbers. The distinction between functions which support complex numbers and\nthose which don’t is made since most users do not want to learn quite as much\nmathematics as required to understand complex numbers. Receiving an exception\ninstead of a complex result allows earlier detection of the unexpected complex\nnumber used as a parameter, so that the programmer can determine how and why it\nwas generated in the first place.

\n

The following functions are provided by this module. Except when explicitly\nnoted otherwise, all return values are floats.

\n
\n

9.2.1. Number-theoretic and representation functions

\n
\n
\nmath.ceil(x)
\n
Return the ceiling of x as a float, the smallest integer value greater than or\nequal to x.
\n\n
\n
\nmath.copysign(x, y)
\n

Return x with the sign of y. On a platform that supports\nsigned zeros, copysign(1.0, -0.0) returns -1.0.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.fabs(x)
\n
Return the absolute value of x.
\n\n
\n
\nmath.factorial(x)
\n

Return x factorial. Raises ValueError if x is not integral or\nis negative.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.floor(x)
\n
Return the floor of x as a float, the largest integer value less than or equal\nto x.
\n\n
\n
\nmath.fmod(x, y)
\n
Return fmod(x, y), as defined by the platform C library. Note that the\nPython expression x y may not return the same result. The intent of the C\nstandard is that fmod(x, y) be exactly (mathematically; to infinite\nprecision) equal to x - n*y for some integer n such that the result has\nthe same sign as x and magnitude less than abs(y). Python’s x y\nreturns a result with the sign of y instead, and may not be exactly computable\nfor float arguments. For example, fmod(-1e-100, 1e100) is -1e-100, but\nthe result of Python’s -1e-100 1e100 is 1e100-1e-100, which cannot be\nrepresented exactly as a float, and rounds to the surprising 1e100. For\nthis reason, function fmod() is generally preferred when working with\nfloats, while Python’s x y is preferred when working with integers.
\n\n
\n
\nmath.frexp(x)
\n
Return the mantissa and exponent of x as the pair (m, e). m is a float\nand e is an integer such that x == m * 2**e exactly. If x is zero,\nreturns (0.0, 0), otherwise 0.5 <= abs(m) < 1. This is used to “pick\napart” the internal representation of a float in a portable way.
\n\n
\n
\nmath.fsum(iterable)
\n

Return an accurate floating point sum of values in the iterable. Avoids\nloss of precision by tracking multiple intermediate partial sums:

\n
>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])\n0.9999999999999999\n>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])\n1.0\n
\n
\n

The algorithm’s accuracy depends on IEEE-754 arithmetic guarantees and the\ntypical case where the rounding mode is half-even. On some non-Windows\nbuilds, the underlying C library uses extended precision addition and may\noccasionally double-round an intermediate sum causing it to be off in its\nleast significant bit.

\n

For further discussion and two alternative approaches, see the ASPN cookbook\nrecipes for accurate floating point summation.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.isinf(x)
\n

Check if the float x is positive or negative infinity.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.isnan(x)
\n

Check if the float x is a NaN (not a number). For more information\non NaNs, see the IEEE 754 standards.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.ldexp(x, i)
\n
Return x * (2**i). This is essentially the inverse of function\nfrexp().
\n\n
\n
\nmath.modf(x)
\n
Return the fractional and integer parts of x. Both results carry the sign\nof x and are floats.
\n\n
\n
\nmath.trunc(x)
\n

Return the Real value x truncated to an Integral (usually\na long integer). Uses the __trunc__ method.

\n

\nNew in version 2.6.

\n
\n\n

Note that frexp() and modf() have a different call/return pattern\nthan their C equivalents: they take a single argument and return a pair of\nvalues, rather than returning their second return value through an ‘output\nparameter’ (there is no such thing in Python).

\n

For the ceil(), floor(), and modf() functions, note that all\nfloating-point numbers of sufficiently large magnitude are exact integers.\nPython floats typically carry no more than 53 bits of precision (the same as the\nplatform C double type), in which case any float x with abs(x) >= 2**52\nnecessarily has no fractional bits.

\n
\n
\n

9.2.2. Power and logarithmic functions

\n
\n
\nmath.exp(x)
\n
Return e**x.
\n\n
\n
\nmath.expm1(x)
\n

Return e**x - 1. For small floats x, the subtraction in\nexp(x) - 1 can result in a significant loss of precision; the\nexpm1() function provides a way to compute this quantity to\nfull precision:

\n
>>> from math import exp, expm1\n>>> exp(1e-5) - 1  # gives result accurate to 11 places\n1.0000050000069649e-05\n>>> expm1(1e-5)    # result accurate to full precision\n1.0000050000166668e-05\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nmath.log(x[, base])
\n

With one argument, return the natural logarithm of x (to base e).

\n

With two arguments, return the logarithm of x to the given base,\ncalculated as log(x)/log(base).

\n

\nChanged in version 2.3: base argument added.

\n
\n\n
\n
\nmath.log1p(x)
\n

Return the natural logarithm of 1+x (base e). The\nresult is calculated in a way which is accurate for x near zero.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.log10(x)
\n
Return the base-10 logarithm of x. This is usually more accurate\nthan log(x, 10).
\n\n
\n
\nmath.pow(x, y)
\n

Return x raised to the power y. Exceptional cases follow\nAnnex ‘F’ of the C99 standard as far as possible. In particular,\npow(1.0, x) and pow(x, 0.0) always return 1.0, even\nwhen x is a zero or a NaN. If both x and y are finite,\nx is negative, and y is not an integer then pow(x, y)\nis undefined, and raises ValueError.

\n

\nChanged in version 2.6: The outcome of 1**nan and nan**0 was undefined.

\n
\n\n
\n
\nmath.sqrt(x)
\n
Return the square root of x.
\n\n
\n
\n

9.2.3. Trigonometric functions

\n
\n
\nmath.acos(x)
\n
Return the arc cosine of x, in radians.
\n\n
\n
\nmath.asin(x)
\n
Return the arc sine of x, in radians.
\n\n
\n
\nmath.atan(x)
\n
Return the arc tangent of x, in radians.
\n\n
\n
\nmath.atan2(y, x)
\n
Return atan(y / x), in radians. The result is between -pi and pi.\nThe vector in the plane from the origin to point (x, y) makes this angle\nwith the positive X axis. The point of atan2() is that the signs of both\ninputs are known to it, so it can compute the correct quadrant for the angle.\nFor example, atan(1) and atan2(1, 1) are both pi/4, but atan2(-1,\n-1) is -3*pi/4.
\n\n
\n
\nmath.cos(x)
\n
Return the cosine of x radians.
\n\n
\n
\nmath.hypot(x, y)
\n
Return the Euclidean norm, sqrt(x*x + y*y). This is the length of the vector\nfrom the origin to point (x, y).
\n\n
\n
\nmath.sin(x)
\n
Return the sine of x radians.
\n\n
\n
\nmath.tan(x)
\n
Return the tangent of x radians.
\n\n
\n
\n

9.2.4. Angular conversion

\n
\n
\nmath.degrees(x)
\n
Converts angle x from radians to degrees.
\n\n
\n
\nmath.radians(x)
\n
Converts angle x from degrees to radians.
\n\n
\n
\n

9.2.5. Hyperbolic functions

\n
\n
\nmath.acosh(x)
\n

Return the inverse hyperbolic cosine of x.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.asinh(x)
\n

Return the inverse hyperbolic sine of x.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.atanh(x)
\n

Return the inverse hyperbolic tangent of x.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmath.cosh(x)
\n
Return the hyperbolic cosine of x.
\n\n
\n
\nmath.sinh(x)
\n
Return the hyperbolic sine of x.
\n\n
\n
\nmath.tanh(x)
\n
Return the hyperbolic tangent of x.
\n\n
\n
\n

9.2.6. Special functions

\n
\n
\nmath.erf(x)
\n

Return the error function at x.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nmath.erfc(x)
\n

Return the complementary error function at x.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nmath.gamma(x)
\n

Return the Gamma function at x.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nmath.lgamma(x)
\n

Return the natural logarithm of the absolute value of the Gamma\nfunction at x.

\n

\nNew in version 2.7.

\n
\n\n
\n
\n

9.2.7. Constants

\n
\n
\nmath.pi
\n
The mathematical constant π = 3.141592..., to available precision.
\n\n
\n
\nmath.e
\n
The mathematical constant e = 2.718281..., to available precision.
\n\n
\n

CPython implementation detail: The math module consists mostly of thin wrappers around the platform C\nmath library functions. Behavior in exceptional cases follows Annex F of\nthe C99 standard where appropriate. The current implementation will raise\nValueError for invalid operations like sqrt(-1.0) or log(0.0)\n(where C99 Annex F recommends signaling invalid operation or divide-by-zero),\nand OverflowError for results that overflow (for example,\nexp(1000.0)). A NaN will not be returned from any of the functions\nabove unless one or more of the input arguments was a NaN; in that case,\nmost functions will return a NaN, but (again following C99 Annex F) there\nare some exceptions to this rule, for example pow(float('nan'), 0.0) or\nhypot(float('nan'), float('inf')).

\n

Note that Python makes no effort to distinguish signaling NaNs from\nquiet NaNs, and behavior for signaling NaNs remains unspecified.\nTypical behavior is to treat all NaNs as though they were quiet.

\n

\nChanged in version 2.6: Behavior in special cases now aims to follow C99 Annex F. In earlier\nversions of Python the behavior in special cases was loosely specified.

\n
\n
\n

See also

\n
\n
Module cmath
\n
Complex number versions of many of these functions.
\n
\n
\n
\n
", "searchableItems": [ { "name": "math.acos", "domId": "math_math.acos" }, { "name": "math.acosh", "domId": "math_math.acosh" }, { "name": "math.asin", "domId": "math_math.asin" }, { "name": "math.asinh", "domId": "math_math.asinh" }, { "name": "math.atan", "domId": "math_math.atan" }, { "name": "math.atan2", "domId": "math_math.atan2" }, { "name": "math.atanh", "domId": "math_math.atanh" }, { "name": "math.ceil", "domId": "math_math.ceil" }, { "name": "math.copysign", "domId": "math_math.copysign" }, { "name": "math.cos", "domId": "math_math.cos" }, { "name": "math.cosh", "domId": "math_math.cosh" }, { "name": "math.degrees", "domId": "math_math.degrees" }, { "name": "math.erf", "domId": "math_math.erf" }, { "name": "math.erfc", "domId": "math_math.erfc" }, { "name": "math.exp", "domId": "math_math.exp" }, { "name": "math.expm1", "domId": "math_math.expm1" }, { "name": "math.fabs", "domId": "math_math.fabs" }, { "name": "math.factorial", "domId": "math_math.factorial" }, { "name": "math.floor", "domId": "math_math.floor" }, { "name": "math.fmod", "domId": "math_math.fmod" }, { "name": "math.frexp", "domId": "math_math.frexp" }, { "name": "math.fsum", "domId": "math_math.fsum" }, { "name": "math.gamma", "domId": "math_math.gamma" }, { "name": "math.hypot", "domId": "math_math.hypot" }, { "name": "math.isinf", "domId": "math_math.isinf" }, { "name": "math.isnan", "domId": "math_math.isnan" }, { "name": "math.ldexp", "domId": "math_math.ldexp" }, { "name": "math.lgamma", "domId": "math_math.lgamma" }, { "name": "math.log", "domId": "math_math.log" }, { "name": "math.log10", "domId": "math_math.log10" }, { "name": "math.log1p", "domId": "math_math.log1p" }, { "name": "math.modf", "domId": "math_math.modf" }, { "name": "math.pow", "domId": "math_math.pow" }, { "name": "math.radians", "domId": "math_math.radians" }, { "name": "math.sin", "domId": "math_math.sin" }, { "name": "math.sinh", "domId": "math_math.sinh" }, { "name": "math.sqrt", "domId": "math_math.sqrt" }, { "name": "math.tan", "domId": "math_math.tan" }, { "name": "math.tanh", "domId": "math_math.tanh" }, { "name": "math.trunc", "domId": "math_math.trunc" } ] }, { "url": "http://docs.python.org/library/fractions.html", "title": "fractions", "html": "
\n

9.5. fractions — Rational numbers

\n

\nNew in version 2.6.

\n

Source code: Lib/fractions.py

\n
\n

The fractions module provides support for rational number arithmetic.

\n

A Fraction instance can be constructed from a pair of integers, from\nanother rational number, or from a string.

\n
\n
\nclass fractions.Fraction(numerator=0, denominator=1)
\n
\nclass fractions.Fraction(other_fraction)
\n
\nclass fractions.Fraction(float)
\n
\nclass fractions.Fraction(decimal)
\n
\nclass fractions.Fraction(string)
\n

The first version requires that numerator and denominator are instances\nof numbers.Rational and returns a new Fraction instance\nwith value numerator/denominator. If denominator is 0, it\nraises a ZeroDivisionError. The second version requires that\nother_fraction is an instance of numbers.Rational and returns a\nFraction instance with the same value. The next two versions accept\neither a float or a decimal.Decimal instance, and return a\nFraction instance with exactly the same value. Note that due to the\nusual issues with binary floating-point (see Floating Point Arithmetic: Issues and Limitations), the\nargument to Fraction(1.1) is not exactly equal to 11/10, and so\nFraction(1.1) does not return Fraction(11, 10) as one might expect.\n(But see the documentation for the limit_denominator() method below.)\nThe last version of the constructor expects a string or unicode instance.\nThe usual form for this instance is:

\n
[sign] numerator ['/' denominator]
\n
\n

where the optional sign may be either ‘+’ or ‘-‘ and\nnumerator and denominator (if present) are strings of\ndecimal digits. In addition, any string that represents a finite\nvalue and is accepted by the float constructor is also\naccepted by the Fraction constructor. In either form the\ninput string may also have leading and/or trailing whitespace.\nHere are some examples:

\n
>>> from fractions import Fraction\n>>> Fraction(16, -10)\nFraction(-8, 5)\n>>> Fraction(123)\nFraction(123, 1)\n>>> Fraction()\nFraction(0, 1)\n>>> Fraction('3/7')\nFraction(3, 7)\n[40794 refs]\n>>> Fraction(' -3/7 ')\nFraction(-3, 7)\n>>> Fraction('1.414213 \\t\\n')\nFraction(1414213, 1000000)\n>>> Fraction('-.125')\nFraction(-1, 8)\n>>> Fraction('7e-6')\nFraction(7, 1000000)\n>>> Fraction(2.25)\nFraction(9, 4)\n>>> Fraction(1.1)\nFraction(2476979795053773, 2251799813685248)\n>>> from decimal import Decimal\n>>> Fraction(Decimal('1.1'))\nFraction(11, 10)\n
\n
\n

The Fraction class inherits from the abstract base class\nnumbers.Rational, and implements all of the methods and\noperations from that class. Fraction instances are hashable,\nand should be treated as immutable. In addition,\nFraction has the following methods:

\n

\nChanged in version 2.7: The Fraction constructor now accepts float and\ndecimal.Decimal instances.

\n
\n
\nfrom_float(flt)
\n

This class method constructs a Fraction representing the exact\nvalue of flt, which must be a float. Beware that\nFraction.from_float(0.3) is not the same value as Fraction(3, 10)

\n
\n

Note

\n

From Python 2.7 onwards, you can also construct a\nFraction instance directly from a float.

\n
\n
\n\n
\n
\nfrom_decimal(dec)
\n

This class method constructs a Fraction representing the exact\nvalue of dec, which must be a decimal.Decimal.

\n
\n

Note

\n

From Python 2.7 onwards, you can also construct a\nFraction instance directly from a decimal.Decimal\ninstance.

\n
\n
\n\n
\n
\nlimit_denominator(max_denominator=1000000)
\n

Finds and returns the closest Fraction to self that has\ndenominator at most max_denominator. This method is useful for finding\nrational approximations to a given floating-point number:

\n
>>> from fractions import Fraction\n>>> Fraction('3.1415926535897932').limit_denominator(1000)\nFraction(355, 113)\n
\n
\n

or for recovering a rational number that’s represented as a float:

\n
>>> from math import pi, cos\n>>> Fraction(cos(pi/3))\nFraction(4503599627370497, 9007199254740992)\n>>> Fraction(cos(pi/3)).limit_denominator()\nFraction(1, 2)\n>>> Fraction(1.1).limit_denominator()\nFraction(11, 10)\n
\n
\n
\n\n
\n\n
\n
\nfractions.gcd(a, b)
\n
Return the greatest common divisor of the integers a and b. If either\na or b is nonzero, then the absolute value of gcd(a, b) is the\nlargest integer that divides both a and b. gcd(a,b) has the same\nsign as b if b is nonzero; otherwise it takes the sign of a. gcd(0,\n0) returns 0.
\n\n
\n

See also

\n
\n
Module numbers
\n
The abstract base classes making up the numeric tower.
\n
\n
\n
", "searchableItems": [ { "name": "fractions.Fraction", "domId": "fractions_fractions.Fraction" }, { "name": "fractions.Fraction.from_decimal", "domId": "fractions_fractions.Fraction.from_decimal" }, { "name": "fractions.Fraction.from_float", "domId": "fractions_fractions.Fraction.from_float" }, { "name": "fractions.Fraction.limit_denominator", "domId": "fractions_fractions.Fraction.limit_denominator" }, { "name": "fractions.gcd", "domId": "fractions_fractions.gcd" } ] }, { "url": "http://docs.python.org/library/cmath.html", "title": "cmath", "html": "
\n

9.3. cmath — Mathematical functions for complex numbers

\n

This module is always available. It provides access to mathematical functions\nfor complex numbers. The functions in this module accept integers,\nfloating-point numbers or complex numbers as arguments. They will also accept\nany Python object that has either a __complex__() or a __float__()\nmethod: these methods are used to convert the object to a complex or\nfloating-point number, respectively, and the function is then applied to the\nresult of the conversion.

\n
\n

Note

\n

On platforms with hardware and system-level support for signed\nzeros, functions involving branch cuts are continuous on both\nsides of the branch cut: the sign of the zero distinguishes one\nside of the branch cut from the other. On platforms that do not\nsupport signed zeros the continuity is as specified below.

\n
\n
\n

9.3.1. Conversions to and from polar coordinates

\n

A Python complex number z is stored internally using rectangular\nor Cartesian coordinates. It is completely determined by its real\npart z.real and its imaginary part z.imag. In other\nwords:

\n
z == z.real + z.imag*1j\n
\n
\n

Polar coordinates give an alternative way to represent a complex\nnumber. In polar coordinates, a complex number z is defined by the\nmodulus r and the phase angle phi. The modulus r is the distance\nfrom z to the origin, while the phase phi is the counterclockwise\nangle, measured in radians, from the positive x-axis to the line\nsegment that joins the origin to z.

\n

The following functions can be used to convert from the native\nrectangular coordinates to polar coordinates and back.

\n
\n
\ncmath.phase(x)
\n

Return the phase of x (also known as the argument of x), as a\nfloat. phase(x) is equivalent to math.atan2(x.imag,\nx.real). The result lies in the range [-π, π], and the branch\ncut for this operation lies along the negative real axis,\ncontinuous from above. On systems with support for signed zeros\n(which includes most systems in current use), this means that the\nsign of the result is the same as the sign of x.imag, even when\nx.imag is zero:

\n
>>> phase(complex(-1.0, 0.0))\n3.1415926535897931\n>>> phase(complex(-1.0, -0.0))\n-3.1415926535897931\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n

Note

\n

The modulus (absolute value) of a complex number x can be\ncomputed using the built-in abs() function. There is no\nseparate cmath module function for this operation.

\n
\n
\n
\ncmath.polar(x)
\n

Return the representation of x in polar coordinates. Returns a\npair (r, phi) where r is the modulus of x and phi is the\nphase of x. polar(x) is equivalent to (abs(x),\nphase(x)).

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncmath.rect(r, phi)
\n

Return the complex number x with polar coordinates r and phi.\nEquivalent to r * (math.cos(phi) + math.sin(phi)*1j).

\n

\nNew in version 2.6.

\n
\n\n
\n
\n

9.3.2. Power and logarithmic functions

\n
\n
\ncmath.exp(x)
\n
Return the exponential value e**x.
\n\n
\n
\ncmath.log(x[, base])
\n

Returns the logarithm of x to the given base. If the base is not\nspecified, returns the natural logarithm of x. There is one branch cut, from 0\nalong the negative real axis to -∞, continuous from above.

\n

\nChanged in version 2.4: base argument added.

\n
\n\n
\n
\ncmath.log10(x)
\n
Return the base-10 logarithm of x. This has the same branch cut as\nlog().
\n\n
\n
\ncmath.sqrt(x)
\n
Return the square root of x. This has the same branch cut as log().
\n\n
\n
\n

9.3.3. Trigonometric functions

\n
\n
\ncmath.acos(x)
\n
Return the arc cosine of x. There are two branch cuts: One extends right from\n1 along the real axis to ∞, continuous from below. The other extends left from\n-1 along the real axis to -∞, continuous from above.
\n\n
\n
\ncmath.asin(x)
\n
Return the arc sine of x. This has the same branch cuts as acos().
\n\n
\n
\ncmath.atan(x)
\n

Return the arc tangent of x. There are two branch cuts: One extends from\n1j along the imaginary axis to ∞j, continuous from the right. The\nother extends from -1j along the imaginary axis to -∞j, continuous\nfrom the left.

\n

\nChanged in version 2.6: direction of continuity of upper cut reversed

\n
\n\n
\n
\ncmath.cos(x)
\n
Return the cosine of x.
\n\n
\n
\ncmath.sin(x)
\n
Return the sine of x.
\n\n
\n
\ncmath.tan(x)
\n
Return the tangent of x.
\n\n
\n
\n

9.3.4. Hyperbolic functions

\n
\n
\ncmath.acosh(x)
\n
Return the hyperbolic arc cosine of x. There is one branch cut, extending left\nfrom 1 along the real axis to -∞, continuous from above.
\n\n
\n
\ncmath.asinh(x)
\n

Return the hyperbolic arc sine of x. There are two branch cuts:\nOne extends from 1j along the imaginary axis to ∞j,\ncontinuous from the right. The other extends from -1j along\nthe imaginary axis to -∞j, continuous from the left.

\n

\nChanged in version 2.6: branch cuts moved to match those recommended by the C99 standard

\n
\n\n
\n
\ncmath.atanh(x)
\n

Return the hyperbolic arc tangent of x. There are two branch cuts: One\nextends from 1 along the real axis to , continuous from below. The\nother extends from -1 along the real axis to -∞, continuous from\nabove.

\n

\nChanged in version 2.6: direction of continuity of right cut reversed

\n
\n\n
\n
\ncmath.cosh(x)
\n
Return the hyperbolic cosine of x.
\n\n
\n
\ncmath.sinh(x)
\n
Return the hyperbolic sine of x.
\n\n
\n
\ncmath.tanh(x)
\n
Return the hyperbolic tangent of x.
\n\n
\n
\n

9.3.5. Classification functions

\n
\n
\ncmath.isinf(x)
\n

Return True if the real or the imaginary part of x is positive\nor negative infinity.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncmath.isnan(x)
\n

Return True if the real or imaginary part of x is not a number (NaN).

\n

\nNew in version 2.6.

\n
\n\n
\n
\n

9.3.6. Constants

\n
\n
\ncmath.pi
\n
The mathematical constant π, as a float.
\n\n
\n
\ncmath.e
\n
The mathematical constant e, as a float.
\n\n

Note that the selection of functions is similar, but not identical, to that in\nmodule math. The reason for having two modules is that some users aren’t\ninterested in complex numbers, and perhaps don’t even know what they are. They\nwould rather have math.sqrt(-1) raise an exception than return a complex\nnumber. Also note that the functions defined in cmath always return a\ncomplex number, even if the answer can be expressed as a real number (in which\ncase the complex number has an imaginary part of zero).

\n

A note on branch cuts: They are curves along which the given function fails to\nbe continuous. They are a necessary feature of many complex functions. It is\nassumed that if you need to compute with complex functions, you will understand\nabout branch cuts. Consult almost any (not too elementary) book on complex\nvariables for enlightenment. For information of the proper choice of branch\ncuts for numerical purposes, a good reference should be the following:

\n
\n

See also

\n

Kahan, W: Branch cuts for complex elementary functions; or, Much ado about\nnothing’s sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art\nin numerical analysis. Clarendon Press (1987) pp165-211.

\n
\n
\n
", "searchableItems": [ { "name": "cmath.acos", "domId": "cmath_cmath.acos" }, { "name": "cmath.acosh", "domId": "cmath_cmath.acosh" }, { "name": "cmath.asin", "domId": "cmath_cmath.asin" }, { "name": "cmath.asinh", "domId": "cmath_cmath.asinh" }, { "name": "cmath.atan", "domId": "cmath_cmath.atan" }, { "name": "cmath.atanh", "domId": "cmath_cmath.atanh" }, { "name": "cmath.cos", "domId": "cmath_cmath.cos" }, { "name": "cmath.cosh", "domId": "cmath_cmath.cosh" }, { "name": "cmath.exp", "domId": "cmath_cmath.exp" }, { "name": "cmath.isinf", "domId": "cmath_cmath.isinf" }, { "name": "cmath.isnan", "domId": "cmath_cmath.isnan" }, { "name": "cmath.log", "domId": "cmath_cmath.log" }, { "name": "cmath.log10", "domId": "cmath_cmath.log10" }, { "name": "cmath.phase", "domId": "cmath_cmath.phase" }, { "name": "cmath.polar", "domId": "cmath_cmath.polar" }, { "name": "cmath.rect", "domId": "cmath_cmath.rect" }, { "name": "cmath.sin", "domId": "cmath_cmath.sin" }, { "name": "cmath.sinh", "domId": "cmath_cmath.sinh" }, { "name": "cmath.sqrt", "domId": "cmath_cmath.sqrt" }, { "name": "cmath.tan", "domId": "cmath_cmath.tan" }, { "name": "cmath.tanh", "domId": "cmath_cmath.tanh" } ] }, { "url": "http://docs.python.org/library/functools.html", "title": "functools", "html": "
\n

9.8. functools — Higher order functions and operations on callable objects

\n

\nNew in version 2.5.

\n

Source code: Lib/functools.py

\n
\n

The functools module is for higher-order functions: functions that act on\nor return other functions. In general, any callable object can be treated as a\nfunction for the purposes of this module.

\n

The functools module defines the following functions:

\n
\n
\nfunctools.cmp_to_key(func)
\n

Transform an old-style comparison function to a key-function. Used with\ntools that accept key functions (such as sorted(), min(),\nmax(), heapq.nlargest(), heapq.nsmallest(),\nitertools.groupby()). This function is primarily used as a transition\ntool for programs being converted to Py3.x where comparison functions are no\nlonger supported.

\n

A compare function is any callable that accept two arguments, compares them,\nand returns a negative number for less-than, zero for equality, or a positive\nnumber for greater-than. A key function is a callable that accepts one\nargument and returns another value that indicates the position in the desired\ncollation sequence.

\n

Example:

\n
sorted(iterable, key=cmp_to_key(locale.strcoll))  # locale-aware sort order\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nfunctools.total_ordering(cls)
\n

Given a class defining one or more rich comparison ordering methods, this\nclass decorator supplies the rest. This simplifies the effort involved\nin specifying all of the possible rich comparison operations:

\n

The class must define one of __lt__(), __le__(),\n__gt__(), or __ge__().\nIn addition, the class should supply an __eq__() method.

\n

For example:

\n
@total_ordering\nclass Student:\n    def __eq__(self, other):\n        return ((self.lastname.lower(), self.firstname.lower()) ==\n                (other.lastname.lower(), other.firstname.lower()))\n    def __lt__(self, other):\n        return ((self.lastname.lower(), self.firstname.lower()) <\n                (other.lastname.lower(), other.firstname.lower()))\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nfunctools.reduce(function, iterable[, initializer])
\n

This is the same function as reduce(). It is made available in this module\nto allow writing code more forward-compatible with Python 3.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nfunctools.partial(func[, *args][, **keywords])
\n

Return a new partial object which when called will behave like func\ncalled with the positional arguments args and keyword arguments keywords. If\nmore arguments are supplied to the call, they are appended to args. If\nadditional keyword arguments are supplied, they extend and override keywords.\nRoughly equivalent to:

\n
def partial(func, *args, **keywords):\n    def newfunc(*fargs, **fkeywords):\n        newkeywords = keywords.copy()\n        newkeywords.update(fkeywords)\n        return func(*(args + fargs), **newkeywords)\n    newfunc.func = func\n    newfunc.args = args\n    newfunc.keywords = keywords\n    return newfunc\n
\n
\n

The partial() is used for partial function application which “freezes”\nsome portion of a function’s arguments and/or keywords resulting in a new object\nwith a simplified signature. For example, partial() can be used to create\na callable that behaves like the int() function where the base argument\ndefaults to two:

\n
>>> from functools import partial\n>>> basetwo = partial(int, base=2)\n>>> basetwo.__doc__ = 'Convert base 2 string to an int.'\n>>> basetwo('10010')\n18\n
\n
\n
\n\n
\n
\nfunctools.update_wrapper(wrapper, wrapped[, assigned][, updated])
\n

Update a wrapper function to look like the wrapped function. The optional\narguments are tuples to specify which attributes of the original function are\nassigned directly to the matching attributes on the wrapper function and which\nattributes of the wrapper function are updated with the corresponding attributes\nfrom the original function. The default values for these arguments are the\nmodule level constants WRAPPER_ASSIGNMENTS (which assigns to the wrapper\nfunction’s __name__, __module__ and __doc__, the documentation string) and\nWRAPPER_UPDATES (which updates the wrapper function’s __dict__, i.e. the\ninstance dictionary).

\n

The main intended use for this function is in decorator functions which\nwrap the decorated function and return the wrapper. If the wrapper function is\nnot updated, the metadata of the returned function will reflect the wrapper\ndefinition rather than the original function definition, which is typically less\nthan helpful.

\n
\n\n
\n
\nfunctools.wraps(wrapped[, assigned][, updated])
\n

This is a convenience function for invoking partial(update_wrapper,\nwrapped=wrapped, assigned=assigned, updated=updated) as a function decorator\nwhen defining a wrapper function. For example:

\n
>>> from functools import wraps\n>>> def my_decorator(f):\n...     @wraps(f)\n...     def wrapper(*args, **kwds):\n...         print 'Calling decorated function'\n...         return f(*args, **kwds)\n...     return wrapper\n...\n>>> @my_decorator\n... def example():\n...     """Docstring"""\n...     print 'Called example function'\n...\n>>> example()\nCalling decorated function\nCalled example function\n>>> example.__name__\n'example'\n>>> example.__doc__\n'Docstring'\n
\n
\n

Without the use of this decorator factory, the name of the example function\nwould have been 'wrapper', and the docstring of the original example()\nwould have been lost.

\n
\n\n
\n

9.8.1. partial Objects

\n

partial objects are callable objects created by partial(). They\nhave three read-only attributes:

\n
\n
\npartial.func
\n
A callable object or function. Calls to the partial object will be\nforwarded to func with new arguments and keywords.
\n\n
\n
\npartial.args
\n
The leftmost positional arguments that will be prepended to the positional\narguments provided to a partial object call.
\n\n
\n
\npartial.keywords
\n
The keyword arguments that will be supplied when the partial object is\ncalled.
\n\n

partial objects are like function objects in that they are\ncallable, weak referencable, and can have attributes. There are some important\ndifferences. For instance, the __name__ and __doc__ attributes\nare not created automatically. Also, partial objects defined in\nclasses behave like static methods and do not transform into bound methods\nduring instance attribute look-up.

\n
\n
", "searchableItems": [ { "name": "functools.cmp_to_key", "domId": "functools_functools.cmp_to_key" }, { "name": "functools.partial", "domId": "functools_functools.partial" }, { "name": "functools.reduce", "domId": "functools_functools.reduce" }, { "name": "functools.total_ordering", "domId": "functools_functools.total_ordering" }, { "name": "functools.update_wrapper", "domId": "functools_functools.update_wrapper" }, { "name": "functools.wraps", "domId": "functools_functools.wraps" } ] }, { "url": "http://docs.python.org/library/itertools.html", "title": "itertools", "html": "
\n

9.7. itertools — Functions creating iterators for efficient looping

\n

\nNew in version 2.3.

\n

This module implements a number of iterator building blocks inspired\nby constructs from APL, Haskell, and SML. Each has been recast in a form\nsuitable for Python.

\n

The module standardizes a core set of fast, memory efficient tools that are\nuseful by themselves or in combination. Together, they form an “iterator\nalgebra” making it possible to construct specialized tools succinctly and\nefficiently in pure Python.

\n

For instance, SML provides a tabulation tool: tabulate(f) which produces a\nsequence f(0), f(1), .... The same effect can be achieved in Python\nby combining imap() and count() to form imap(f, count()).

\n

These tools and their built-in counterparts also work well with the high-speed\nfunctions in the operator module. For example, the multiplication\noperator can be mapped across two vectors to form an efficient dot-product:\nsum(imap(operator.mul, vector1, vector2)).

\n

Infinite Iterators:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
IteratorArgumentsResultsExample
count()start, [step]start, start+step, start+2*step, ...count(10) --> 10 11 12 13 14 ...
cycle()pp0, p1, ... plast, p0, p1, ...cycle('ABCD') --> A B C D A B C D ...
repeat()elem [,n]elem, elem, elem, ... endlessly or up to n timesrepeat(10, 3) --> 10 10 10
\n

Iterators terminating on the shortest input sequence:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
IteratorArgumentsResultsExample
chain()p, q, ...p0, p1, ... plast, q0, q1, ...chain('ABC', 'DEF') --> A B C D E F
compress()data, selectors(d[0] if s[0]), (d[1] if s[1]), ...compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
dropwhile()pred, seqseq[n], seq[n+1], starting when pred failsdropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
groupby()iterable[, keyfunc]sub-iterators grouped by value of keyfunc(v) 
ifilter()pred, seqelements of seq where pred(elem) is Trueifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9
ifilterfalse()pred, seqelements of seq where pred(elem) is Falseifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
islice()seq, [start,] stop [, step]elements from seq[start:stop:step]islice('ABCDEFG', 2, None) --> C D E F G
imap()func, p, q, ...func(p0, q0), func(p1, q1), ...imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000
starmap()func, seqfunc(*seq[0]), func(*seq[1]), ...starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
tee()it, nit1, it2 , ... itn splits one iterator into n 
takewhile()pred, seqseq[0], seq[1], until pred failstakewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
izip()p, q, ...(p[0], q[0]), (p[1], q[1]), ...izip('ABCD', 'xy') --> Ax By
izip_longest()p, q, ...(p[0], q[0]), (p[1], q[1]), ...izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
\n

Combinatoric generators:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
IteratorArgumentsResults
product()p, q, ... [repeat=1]cartesian product, equivalent to a nested for-loop
permutations()p[, r]r-length tuples, all possible orderings, no repeated elements
combinations()p, rr-length tuples, in sorted order, no repeated elements
combinations_with_replacement()p, rr-length tuples, in sorted order, with repeated elements
product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC
combinations('ABCD', 2) AB AC AD BC BD CD
combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD
\n
\n

9.7.1. Itertool functions

\n

The following module functions all construct and return iterators. Some provide\nstreams of infinite length, so they should only be accessed by functions or\nloops that truncate the stream.

\n
\n
\nitertools.chain(*iterables)
\n

Make an iterator that returns elements from the first iterable until it is\nexhausted, then proceeds to the next iterable, until all of the iterables are\nexhausted. Used for treating consecutive sequences as a single sequence.\nEquivalent to:

\n
def chain(*iterables):\n    # chain('ABC', 'DEF') --> A B C D E F\n    for it in iterables:\n        for element in it:\n            yield element\n
\n
\n
\n\n
\n
\nclassmethod chain.from_iterable(iterable)
\n

Alternate constructor for chain(). Gets chained inputs from a\nsingle iterable argument that is evaluated lazily. Equivalent to:

\n
@classmethod\ndef from_iterable(iterables):\n    # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F\n    for it in iterables:\n        for element in it:\n            yield element\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nitertools.combinations(iterable, r)
\n

Return r length subsequences of elements from the input iterable.

\n

Combinations are emitted in lexicographic sort order. So, if the\ninput iterable is sorted, the combination tuples will be produced\nin sorted order.

\n

Elements are treated as unique based on their position, not on their\nvalue. So if the input elements are unique, there will be no repeat\nvalues in each combination.

\n

Equivalent to:

\n
def combinations(iterable, r):\n    # combinations('ABCD', 2) --> AB AC AD BC BD CD\n    # combinations(range(4), 3) --> 012 013 023 123\n    pool = tuple(iterable)\n    n = len(pool)\n    if r > n:\n        return\n    indices = range(r)\n    yield tuple(pool[i] for i in indices)\n    while True:\n        for i in reversed(range(r)):\n            if indices[i] != i + n - r:\n                break\n        else:\n            return\n        indices[i] += 1\n        for j in range(i+1, r):\n            indices[j] = indices[j-1] + 1\n        yield tuple(pool[i] for i in indices)\n
\n
\n

The code for combinations() can be also expressed as a subsequence\nof permutations() after filtering entries where the elements are not\nin sorted order (according to their position in the input pool):

\n
def combinations(iterable, r):\n    pool = tuple(iterable)\n    n = len(pool)\n    for indices in permutations(range(n), r):\n        if sorted(indices) == list(indices):\n            yield tuple(pool[i] for i in indices)\n
\n
\n

The number of items returned is n! / r! / (n-r)! when 0 <= r <= n\nor zero when r > n.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nitertools.combinations_with_replacement(iterable, r)
\n

Return r length subsequences of elements from the input iterable\nallowing individual elements to be repeated more than once.

\n

Combinations are emitted in lexicographic sort order. So, if the\ninput iterable is sorted, the combination tuples will be produced\nin sorted order.

\n

Elements are treated as unique based on their position, not on their\nvalue. So if the input elements are unique, the generated combinations\nwill also be unique.

\n

Equivalent to:

\n
def combinations_with_replacement(iterable, r):\n    # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC\n    pool = tuple(iterable)\n    n = len(pool)\n    if not n and r:\n        return\n    indices = [0] * r\n    yield tuple(pool[i] for i in indices)\n    while True:\n        for i in reversed(range(r)):\n            if indices[i] != n - 1:\n                break\n        else:\n            return\n        indices[i:] = [indices[i] + 1] * (r - i)\n        yield tuple(pool[i] for i in indices)\n
\n
\n

The code for combinations_with_replacement() can be also expressed as\na subsequence of product() after filtering entries where the elements\nare not in sorted order (according to their position in the input pool):

\n
def combinations_with_replacement(iterable, r):\n    pool = tuple(iterable)\n    n = len(pool)\n    for indices in product(range(n), repeat=r):\n        if sorted(indices) == list(indices):\n            yield tuple(pool[i] for i in indices)\n
\n
\n

The number of items returned is (n+r-1)! / r! / (n-1)! when n > 0.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nitertools.compress(data, selectors)
\n

Make an iterator that filters elements from data returning only those that\nhave a corresponding element in selectors that evaluates to True.\nStops when either the data or selectors iterables has been exhausted.\nEquivalent to:

\n
def compress(data, selectors):\n    # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F\n    return (d for d, s in izip(data, selectors) if s)\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nitertools.count(start=0, step=1)
\n

Make an iterator that returns evenly spaced values starting with n. Often\nused as an argument to imap() to generate consecutive data points.\nAlso, used with izip() to add sequence numbers. Equivalent to:

\n
def count(start=0, step=1):\n    # count(10) --> 10 11 12 13 14 ...\n    # count(2.5, 0.5) -> 2.5 3.0 3.5 ...\n    n = start\n    while True:\n        yield n\n        n += step\n
\n
\n

When counting with floating point numbers, better accuracy can sometimes be\nachieved by substituting multiplicative code such as: (start + step * i\nfor i in count()).

\n

\nChanged in version 2.7: added step argument and allowed non-integer arguments.

\n
\n\n
\n
\nitertools.cycle(iterable)
\n

Make an iterator returning elements from the iterable and saving a copy of each.\nWhen the iterable is exhausted, return elements from the saved copy. Repeats\nindefinitely. Equivalent to:

\n
def cycle(iterable):\n    # cycle('ABCD') --> A B C D A B C D A B C D ...\n    saved = []\n    for element in iterable:\n        yield element\n        saved.append(element)\n    while saved:\n        for element in saved:\n              yield element\n
\n
\n

Note, this member of the toolkit may require significant auxiliary storage\n(depending on the length of the iterable).

\n
\n\n
\n
\nitertools.dropwhile(predicate, iterable)
\n

Make an iterator that drops elements from the iterable as long as the predicate\nis true; afterwards, returns every element. Note, the iterator does not produce\nany output until the predicate first becomes false, so it may have a lengthy\nstart-up time. Equivalent to:

\n
def dropwhile(predicate, iterable):\n    # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1\n    iterable = iter(iterable)\n    for x in iterable:\n        if not predicate(x):\n            yield x\n            break\n    for x in iterable:\n        yield x\n
\n
\n
\n\n
\n
\nitertools.groupby(iterable[, key])
\n

Make an iterator that returns consecutive keys and groups from the iterable.\nThe key is a function computing a key value for each element. If not\nspecified or is None, key defaults to an identity function and returns\nthe element unchanged. Generally, the iterable needs to already be sorted on\nthe same key function.

\n

The operation of groupby() is similar to the uniq filter in Unix. It\ngenerates a break or new group every time the value of the key function changes\n(which is why it is usually necessary to have sorted the data using the same key\nfunction). That behavior differs from SQL’s GROUP BY which aggregates common\nelements regardless of their input order.

\n

The returned group is itself an iterator that shares the underlying iterable\nwith groupby(). Because the source is shared, when the groupby()\nobject is advanced, the previous group is no longer visible. So, if that data\nis needed later, it should be stored as a list:

\n
groups = []\nuniquekeys = []\ndata = sorted(data, key=keyfunc)\nfor k, g in groupby(data, keyfunc):\n    groups.append(list(g))      # Store group iterator as a list\n    uniquekeys.append(k)\n
\n
\n

groupby() is equivalent to:

\n
class groupby(object):\n    # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B\n    # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D\n    def __init__(self, iterable, key=None):\n        if key is None:\n            key = lambda x: x\n        self.keyfunc = key\n        self.it = iter(iterable)\n        self.tgtkey = self.currkey = self.currvalue = object()\n    def __iter__(self):\n        return self\n    def next(self):\n        while self.currkey == self.tgtkey:\n            self.currvalue = next(self.it)    # Exit on StopIteration\n            self.currkey = self.keyfunc(self.currvalue)\n        self.tgtkey = self.currkey\n        return (self.currkey, self._grouper(self.tgtkey))\n    def _grouper(self, tgtkey):\n        while self.currkey == tgtkey:\n            yield self.currvalue\n            self.currvalue = next(self.it)    # Exit on StopIteration\n            self.currkey = self.keyfunc(self.currvalue)\n
\n
\n

\nNew in version 2.4.

\n
\n\n
\n
\nitertools.ifilter(predicate, iterable)
\n

Make an iterator that filters elements from iterable returning only those for\nwhich the predicate is True. If predicate is None, return the items\nthat are true. Equivalent to:

\n
def ifilter(predicate, iterable):\n    # ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9\n    if predicate is None:\n        predicate = bool\n    for x in iterable:\n        if predicate(x):\n            yield x\n
\n
\n
\n\n
\n
\nitertools.ifilterfalse(predicate, iterable)
\n

Make an iterator that filters elements from iterable returning only those for\nwhich the predicate is False. If predicate is None, return the items\nthat are false. Equivalent to:

\n
def ifilterfalse(predicate, iterable):\n    # ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8\n    if predicate is None:\n        predicate = bool\n    for x in iterable:\n        if not predicate(x):\n            yield x\n
\n
\n
\n\n
\n
\nitertools.imap(function, *iterables)
\n

Make an iterator that computes the function using arguments from each of the\niterables. If function is set to None, then imap() returns the\narguments as a tuple. Like map() but stops when the shortest iterable is\nexhausted instead of filling in None for shorter iterables. The reason for\nthe difference is that infinite iterator arguments are typically an error for\nmap() (because the output is fully evaluated) but represent a common and\nuseful way of supplying arguments to imap(). Equivalent to:

\n
def imap(function, *iterables):\n    # imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000\n    iterables = map(iter, iterables)\n    while True:\n        args = [next(it) for it in iterables]\n        if function is None:\n            yield tuple(args)\n        else:\n            yield function(*args)\n
\n
\n
\n\n
\n
\nitertools.islice(iterable[, start], stop[, step])
\n

Make an iterator that returns selected elements from the iterable. If start is\nnon-zero, then elements from the iterable are skipped until start is reached.\nAfterward, elements are returned consecutively unless step is set higher than\none which results in items being skipped. If stop is None, then iteration\ncontinues until the iterator is exhausted, if at all; otherwise, it stops at the\nspecified position. Unlike regular slicing, islice() does not support\nnegative values for start, stop, or step. Can be used to extract related\nfields from data where the internal structure has been flattened (for example, a\nmulti-line report may list a name field on every third line). Equivalent to:

\n
def islice(iterable, *args):\n    # islice('ABCDEFG', 2) --> A B\n    # islice('ABCDEFG', 2, 4) --> C D\n    # islice('ABCDEFG', 2, None) --> C D E F G\n    # islice('ABCDEFG', 0, None, 2) --> A C E G\n    s = slice(*args)\n    it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))\n    nexti = next(it)\n    for i, element in enumerate(iterable):\n        if i == nexti:\n            yield element\n            nexti = next(it)\n
\n
\n

If start is None, then iteration starts at zero. If step is None,\nthen the step defaults to one.

\n

\nChanged in version 2.5: accept None values for default start and step.

\n
\n\n
\n
\nitertools.izip(*iterables)
\n

Make an iterator that aggregates elements from each of the iterables. Like\nzip() except that it returns an iterator instead of a list. Used for\nlock-step iteration over several iterables at a time. Equivalent to:

\n
def izip(*iterables):\n    # izip('ABCD', 'xy') --> Ax By\n    iterators = map(iter, iterables)\n    while iterators:\n        yield tuple(map(next, iterators))\n
\n
\n

\nChanged in version 2.4: When no iterables are specified, returns a zero length iterator instead of\nraising a TypeError exception.

\n

The left-to-right evaluation order of the iterables is guaranteed. This\nmakes possible an idiom for clustering a data series into n-length groups\nusing izip(*[iter(s)]*n).

\n

izip() should only be used with unequal length inputs when you don’t\ncare about trailing, unmatched values from the longer iterables. If those\nvalues are important, use izip_longest() instead.

\n
\n\n
\n
\nitertools.izip_longest(*iterables[, fillvalue])
\n

Make an iterator that aggregates elements from each of the iterables. If the\niterables are of uneven length, missing values are filled-in with fillvalue.\nIteration continues until the longest iterable is exhausted. Equivalent to:

\n
class ZipExhausted(Exception):\n    pass\n\ndef izip_longest(*args, **kwds):\n    # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-\n    fillvalue = kwds.get('fillvalue')\n    counter = [len(args) - 1]\n    def sentinel():\n        if not counter[0]:\n            raise ZipExhausted\n        counter[0] -= 1\n        yield fillvalue\n    fillers = repeat(fillvalue)\n    iterators = [chain(it, sentinel(), fillers) for it in args]\n    try:\n        while iterators:\n            yield tuple(map(next, iterators))\n    except ZipExhausted:\n        pass\n
\n
\n

If one of the iterables is potentially infinite, then the\nizip_longest() function should be wrapped with something that limits\nthe number of calls (for example islice() or takewhile()). If\nnot specified, fillvalue defaults to None.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nitertools.permutations(iterable[, r])
\n

Return successive r length permutations of elements in the iterable.

\n

If r is not specified or is None, then r defaults to the length\nof the iterable and all possible full-length permutations\nare generated.

\n

Permutations are emitted in lexicographic sort order. So, if the\ninput iterable is sorted, the permutation tuples will be produced\nin sorted order.

\n

Elements are treated as unique based on their position, not on their\nvalue. So if the input elements are unique, there will be no repeat\nvalues in each permutation.

\n

Equivalent to:

\n
def permutations(iterable, r=None):\n    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC\n    # permutations(range(3)) --> 012 021 102 120 201 210\n    pool = tuple(iterable)\n    n = len(pool)\n    r = n if r is None else r\n    if r > n:\n        return\n    indices = range(n)\n    cycles = range(n, n-r, -1)\n    yield tuple(pool[i] for i in indices[:r])\n    while n:\n        for i in reversed(range(r)):\n            cycles[i] -= 1\n            if cycles[i] == 0:\n                indices[i:] = indices[i+1:] + indices[i:i+1]\n                cycles[i] = n - i\n            else:\n                j = cycles[i]\n                indices[i], indices[-j] = indices[-j], indices[i]\n                yield tuple(pool[i] for i in indices[:r])\n                break\n        else:\n            return\n
\n
\n

The code for permutations() can be also expressed as a subsequence of\nproduct(), filtered to exclude entries with repeated elements (those\nfrom the same position in the input pool):

\n
def permutations(iterable, r=None):\n    pool = tuple(iterable)\n    n = len(pool)\n    r = n if r is None else r\n    for indices in product(range(n), repeat=r):\n        if len(set(indices)) == r:\n            yield tuple(pool[i] for i in indices)\n
\n
\n

The number of items returned is n! / (n-r)! when 0 <= r <= n\nor zero when r > n.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nitertools.product(*iterables[, repeat])
\n

Cartesian product of input iterables.

\n

Equivalent to nested for-loops in a generator expression. For example,\nproduct(A, B) returns the same as ((x,y) for x in A for y in B).

\n

The nested loops cycle like an odometer with the rightmost element advancing\non every iteration. This pattern creates a lexicographic ordering so that if\nthe input’s iterables are sorted, the product tuples are emitted in sorted\norder.

\n

To compute the product of an iterable with itself, specify the number of\nrepetitions with the optional repeat keyword argument. For example,\nproduct(A, repeat=4) means the same as product(A, A, A, A).

\n

This function is equivalent to the following code, except that the\nactual implementation does not build up intermediate results in memory:

\n
def product(*args, **kwds):\n    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy\n    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111\n    pools = map(tuple, args) * kwds.get('repeat', 1)\n    result = [[]]\n    for pool in pools:\n        result = [x+[y] for x in result for y in pool]\n    for prod in result:\n        yield tuple(prod)\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nitertools.repeat(object[, times])
\n

Make an iterator that returns object over and over again. Runs indefinitely\nunless the times argument is specified. Used as argument to imap() for\ninvariant function parameters. Also used with izip() to create constant\nfields in a tuple record. Equivalent to:

\n
def repeat(object, times=None):\n    # repeat(10, 3) --> 10 10 10\n    if times is None:\n        while True:\n            yield object\n    else:\n        for i in xrange(times):\n            yield object\n
\n
\n
\n\n
\n
\nitertools.starmap(function, iterable)
\n

Make an iterator that computes the function using arguments obtained from\nthe iterable. Used instead of imap() when argument parameters are already\ngrouped in tuples from a single iterable (the data has been “pre-zipped”). The\ndifference between imap() and starmap() parallels the distinction\nbetween function(a,b) and function(*c). Equivalent to:

\n
def starmap(function, iterable):\n    # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000\n    for args in iterable:\n        yield function(*args)\n
\n
\n

\nChanged in version 2.6: Previously, starmap() required the function arguments to be tuples.\nNow, any iterable is allowed.

\n
\n\n
\n
\nitertools.takewhile(predicate, iterable)
\n

Make an iterator that returns elements from the iterable as long as the\npredicate is true. Equivalent to:

\n
def takewhile(predicate, iterable):\n    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4\n    for x in iterable:\n        if predicate(x):\n            yield x\n        else:\n            break\n
\n
\n
\n\n
\n
\nitertools.tee(iterable[, n=2])
\n

Return n independent iterators from a single iterable. Equivalent to:

\n
def tee(iterable, n=2):\n    it = iter(iterable)\n    deques = [collections.deque() for i in range(n)]\n    def gen(mydeque):\n        while True:\n            if not mydeque:             # when the local deque is empty\n                newval = next(it)       # fetch a new value and\n                for d in deques:        # load it to all the deques\n                    d.append(newval)\n            yield mydeque.popleft()\n    return tuple(gen(d) for d in deques)\n
\n
\n

Once tee() has made a split, the original iterable should not be\nused anywhere else; otherwise, the iterable could get advanced without\nthe tee objects being informed.

\n

This itertool may require significant auxiliary storage (depending on how\nmuch temporary data needs to be stored). In general, if one iterator uses\nmost or all of the data before another iterator starts, it is faster to use\nlist() instead of tee().

\n

\nNew in version 2.4.

\n
\n\n
\n
\n

9.7.2. Recipes

\n

This section shows recipes for creating an extended toolset using the existing\nitertools as building blocks.

\n

The extended tools offer the same high performance as the underlying toolset.\nThe superior memory performance is kept by processing elements one at a time\nrather than bringing the whole iterable into memory all at once. Code volume is\nkept small by linking the tools together in a functional style which helps\neliminate temporary variables. High speed is retained by preferring\n“vectorized” building blocks over the use of for-loops and generators\nwhich incur interpreter overhead.

\n
def take(n, iterable):\n    "Return first n items of the iterable as a list"\n    return list(islice(iterable, n))\n\ndef tabulate(function, start=0):\n    "Return function(0), function(1), ..."\n    return imap(function, count(start))\n\ndef consume(iterator, n):\n    "Advance the iterator n-steps ahead. If n is none, consume entirely."\n    # Use functions that consume iterators at C speed.\n    if n is None:\n        # feed the entire iterator into a zero-length deque\n        collections.deque(iterator, maxlen=0)\n    else:\n        # advance to the empty slice starting at position n\n        next(islice(iterator, n, n), None)\n\ndef nth(iterable, n, default=None):\n    "Returns the nth item or a default value"\n    return next(islice(iterable, n, None), default)\n\ndef quantify(iterable, pred=bool):\n    "Count how many times the predicate is true"\n    return sum(imap(pred, iterable))\n\ndef padnone(iterable):\n    """Returns the sequence elements and then returns None indefinitely.\n\n    Useful for emulating the behavior of the built-in map() function.\n    """\n    return chain(iterable, repeat(None))\n\ndef ncycles(iterable, n):\n    "Returns the sequence elements n times"\n    return chain.from_iterable(repeat(tuple(iterable), n))\n\ndef dotproduct(vec1, vec2):\n    return sum(imap(operator.mul, vec1, vec2))\n\ndef flatten(listOfLists):\n    "Flatten one level of nesting"\n    return chain.from_iterable(listOfLists)\n\ndef repeatfunc(func, times=None, *args):\n    """Repeat calls to func with specified arguments.\n\n    Example:  repeatfunc(random.random)\n    """\n    if times is None:\n        return starmap(func, repeat(args))\n    return starmap(func, repeat(args, times))\n\ndef pairwise(iterable):\n    "s -> (s0,s1), (s1,s2), (s2, s3), ..."\n    a, b = tee(iterable)\n    next(b, None)\n    return izip(a, b)\n\ndef grouper(n, iterable, fillvalue=None):\n    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"\n    args = [iter(iterable)] * n\n    return izip_longest(fillvalue=fillvalue, *args)\n\ndef roundrobin(*iterables):\n    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"\n    # Recipe credited to George Sakkis\n    pending = len(iterables)\n    nexts = cycle(iter(it).next for it in iterables)\n    while pending:\n        try:\n            for next in nexts:\n                yield next()\n        except StopIteration:\n            pending -= 1\n            nexts = cycle(islice(nexts, pending))\n\ndef powerset(iterable):\n    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"\n    s = list(iterable)\n    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))\n\ndef unique_everseen(iterable, key=None):\n    "List unique elements, preserving order. Remember all elements ever seen."\n    # unique_everseen('AAAABBBCCDAABBB') --> A B C D\n    # unique_everseen('ABBCcAD', str.lower) --> A B C D\n    seen = set()\n    seen_add = seen.add\n    if key is None:\n        for element in ifilterfalse(seen.__contains__, iterable):\n            seen_add(element)\n            yield element\n    else:\n        for element in iterable:\n            k = key(element)\n            if k not in seen:\n                seen_add(k)\n                yield element\n\ndef unique_justseen(iterable, key=None):\n    "List unique elements, preserving order. Remember only the element just seen."\n    # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B\n    # unique_justseen('ABBCcAD', str.lower) --> A B C A D\n    return imap(next, imap(itemgetter(1), groupby(iterable, key)))\n\ndef iter_except(func, exception, first=None):\n    """ Call a function repeatedly until an exception is raised.\n\n    Converts a call-until-exception interface to an iterator interface.\n    Like __builtin__.iter(func, sentinel) but uses an exception instead\n    of a sentinel to end the loop.\n\n    Examples:\n        bsddbiter = iter_except(db.next, bsddb.error, db.first)\n        heapiter = iter_except(functools.partial(heappop, h), IndexError)\n        dictiter = iter_except(d.popitem, KeyError)\n        dequeiter = iter_except(d.popleft, IndexError)\n        queueiter = iter_except(q.get_nowait, Queue.Empty)\n        setiter = iter_except(s.pop, KeyError)\n\n    """\n    try:\n        if first is not None:\n            yield first()\n        while 1:\n            yield func()\n    except exception:\n        pass\n\ndef random_product(*args, **kwds):\n    "Random selection from itertools.product(*args, **kwds)"\n    pools = map(tuple, args) * kwds.get('repeat', 1)\n    return tuple(random.choice(pool) for pool in pools)\n\ndef random_permutation(iterable, r=None):\n    "Random selection from itertools.permutations(iterable, r)"\n    pool = tuple(iterable)\n    r = len(pool) if r is None else r\n    return tuple(random.sample(pool, r))\n\ndef random_combination(iterable, r):\n    "Random selection from itertools.combinations(iterable, r)"\n    pool = tuple(iterable)\n    n = len(pool)\n    indices = sorted(random.sample(xrange(n), r))\n    return tuple(pool[i] for i in indices)\n\ndef random_combination_with_replacement(iterable, r):\n    "Random selection from itertools.combinations_with_replacement(iterable, r)"\n    pool = tuple(iterable)\n    n = len(pool)\n    indices = sorted(random.randrange(n) for i in xrange(r))\n    return tuple(pool[i] for i in indices)\n
\n
\n

Note, many of the above recipes can be optimized by replacing global lookups\nwith local variables defined as default values. For example, the\ndotproduct recipe can be written as:

\n
def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul):\n    return sum(imap(mul, vec1, vec2))\n
\n
\n
\n
", "searchableItems": [ { "name": "itertools.chain", "domId": "itertools_itertools.chain" }, { "name": "itertools.combinations", "domId": "itertools_itertools.combinations" }, { "name": "itertools.combinations_with_replacement", "domId": "itertools_itertools.combinations_with_replacement" }, { "name": "itertools.compress", "domId": "itertools_itertools.compress" }, { "name": "itertools.count", "domId": "itertools_itertools.count" }, { "name": "itertools.cycle", "domId": "itertools_itertools.cycle" }, { "name": "itertools.dropwhile", "domId": "itertools_itertools.dropwhile" }, { "name": "itertools.groupby", "domId": "itertools_itertools.groupby" }, { "name": "itertools.ifilter", "domId": "itertools_itertools.ifilter" }, { "name": "itertools.ifilterfalse", "domId": "itertools_itertools.ifilterfalse" }, { "name": "itertools.imap", "domId": "itertools_itertools.imap" }, { "name": "itertools.islice", "domId": "itertools_itertools.islice" }, { "name": "itertools.izip", "domId": "itertools_itertools.izip" }, { "name": "itertools.izip_longest", "domId": "itertools_itertools.izip_longest" }, { "name": "itertools.permutations", "domId": "itertools_itertools.permutations" }, { "name": "itertools.product", "domId": "itertools_itertools.product" }, { "name": "itertools.repeat", "domId": "itertools_itertools.repeat" }, { "name": "itertools.starmap", "domId": "itertools_itertools.starmap" }, { "name": "itertools.takewhile", "domId": "itertools_itertools.takewhile" }, { "name": "itertools.tee", "domId": "itertools_itertools.tee" } ] }, { "url": "http://docs.python.org/library/decimal.html", "title": "decimal", "html": "
\n

9.4. decimal — Decimal fixed point and floating point arithmetic

\n

\nNew in version 2.4.

\n

The decimal module provides support for decimal floating point\narithmetic. It offers several advantages over the float datatype:

\n\n

The module design is centered around three concepts: the decimal number, the\ncontext for arithmetic, and signals.

\n

A decimal number is immutable. It has a sign, coefficient digits, and an\nexponent. To preserve significance, the coefficient digits do not truncate\ntrailing zeros. Decimals also include special values such as\nInfinity, -Infinity, and NaN. The standard also\ndifferentiates -0 from +0.

\n

The context for arithmetic is an environment specifying precision, rounding\nrules, limits on exponents, flags indicating the results of operations, and trap\nenablers which determine whether signals are treated as exceptions. Rounding\noptions include ROUND_CEILING, ROUND_DOWN,\nROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN,\nROUND_HALF_UP, ROUND_UP, and ROUND_05UP.

\n

Signals are groups of exceptional conditions arising during the course of\ncomputation. Depending on the needs of the application, signals may be ignored,\nconsidered as informational, or treated as exceptions. The signals in the\ndecimal module are: Clamped, InvalidOperation,\nDivisionByZero, Inexact, Rounded, Subnormal,\nOverflow, and Underflow.

\n

For each signal there is a flag and a trap enabler. When a signal is\nencountered, its flag is set to one, then, if the trap enabler is\nset to one, an exception is raised. Flags are sticky, so the user needs to\nreset them before monitoring a calculation.

\n
\n

See also

\n\n
\n
\n

9.4.1. Quick-start Tutorial

\n

The usual start to using decimals is importing the module, viewing the current\ncontext with getcontext() and, if necessary, setting new values for\nprecision, rounding, or enabled traps:

\n
>>> from decimal import *\n>>> getcontext()\nContext(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,\n        capitals=1, flags=[], traps=[Overflow, DivisionByZero,\n        InvalidOperation])\n\n>>> getcontext().prec = 7       # Set a new precision\n
\n
\n

Decimal instances can be constructed from integers, strings, floats, or tuples.\nConstruction from an integer or a float performs an exact conversion of the\nvalue of that integer or float. Decimal numbers include special values such as\nNaN which stands for “Not a number”, positive and negative\nInfinity, and -0.

\n
>>> getcontext().prec = 28\n>>> Decimal(10)\nDecimal('10')\n>>> Decimal('3.14')\nDecimal('3.14')\n>>> Decimal(3.14)\nDecimal('3.140000000000000124344978758017532527446746826171875')\n>>> Decimal((0, (3, 1, 4), -2))\nDecimal('3.14')\n>>> Decimal(str(2.0 ** 0.5))\nDecimal('1.41421356237')\n>>> Decimal(2) ** Decimal('0.5')\nDecimal('1.414213562373095048801688724')\n>>> Decimal('NaN')\nDecimal('NaN')\n>>> Decimal('-Infinity')\nDecimal('-Infinity')\n
\n
\n

The significance of a new Decimal is determined solely by the number of digits\ninput. Context precision and rounding only come into play during arithmetic\noperations.

\n
>>> getcontext().prec = 6\n>>> Decimal('3.0')\nDecimal('3.0')\n>>> Decimal('3.1415926535')\nDecimal('3.1415926535')\n>>> Decimal('3.1415926535') + Decimal('2.7182818285')\nDecimal('5.85987')\n>>> getcontext().rounding = ROUND_UP\n>>> Decimal('3.1415926535') + Decimal('2.7182818285')\nDecimal('5.85988')\n
\n
\n

Decimals interact well with much of the rest of Python. Here is a small decimal\nfloating point flying circus:

\n
>>> data = map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split())\n>>> max(data)\nDecimal('9.25')\n>>> min(data)\nDecimal('0.03')\n>>> sorted(data)\n[Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'),\n Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]\n>>> sum(data)\nDecimal('19.29')\n>>> a,b,c = data[:3]\n>>> str(a)\n'1.34'\n>>> float(a)\n1.34\n>>> round(a, 1)     # round() first converts to binary floating point\n1.3\n>>> int(a)\n1\n>>> a * 5\nDecimal('6.70')\n>>> a * b\nDecimal('2.5058')\n>>> c  a\nDecimal('0.77')\n
\n
\n

And some mathematical functions are also available to Decimal:

\n
>>> getcontext().prec = 28\n>>> Decimal(2).sqrt()\nDecimal('1.414213562373095048801688724')\n>>> Decimal(1).exp()\nDecimal('2.718281828459045235360287471')\n>>> Decimal('10').ln()\nDecimal('2.302585092994045684017991455')\n>>> Decimal('10').log10()\nDecimal('1')\n
\n
\n

The quantize() method rounds a number to a fixed exponent. This method is\nuseful for monetary applications that often round results to a fixed number of\nplaces:

\n
>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)\nDecimal('7.32')\n>>> Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP)\nDecimal('8')\n
\n
\n

As shown above, the getcontext() function accesses the current context and\nallows the settings to be changed. This approach meets the needs of most\napplications.

\n

For more advanced work, it may be useful to create alternate contexts using the\nContext() constructor. To make an alternate active, use the setcontext()\nfunction.

\n

In accordance with the standard, the Decimal module provides two ready to\nuse standard contexts, BasicContext and ExtendedContext. The\nformer is especially useful for debugging because many of the traps are\nenabled:

\n
>>> myothercontext = Context(prec=60, rounding=ROUND_HALF_DOWN)\n>>> setcontext(myothercontext)\n>>> Decimal(1) / Decimal(7)\nDecimal('0.142857142857142857142857142857142857142857142857142857142857')\n\n>>> ExtendedContext\nContext(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,\n        capitals=1, flags=[], traps=[])\n>>> setcontext(ExtendedContext)\n>>> Decimal(1) / Decimal(7)\nDecimal('0.142857143')\n>>> Decimal(42) / Decimal(0)\nDecimal('Infinity')\n\n>>> setcontext(BasicContext)\n>>> Decimal(42) / Decimal(0)\nTraceback (most recent call last):\n  File "<pyshell#143>", line 1, in -toplevel-\n    Decimal(42) / Decimal(0)\nDivisionByZero: x / 0\n
\n
\n

Contexts also have signal flags for monitoring exceptional conditions\nencountered during computations. The flags remain set until explicitly cleared,\nso it is best to clear the flags before each set of monitored computations by\nusing the clear_flags() method.

\n
>>> setcontext(ExtendedContext)\n>>> getcontext().clear_flags()\n>>> Decimal(355) / Decimal(113)\nDecimal('3.14159292')\n>>> getcontext()\nContext(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,\n        capitals=1, flags=[Rounded, Inexact], traps=[])\n
\n
\n

The flags entry shows that the rational approximation to Pi was\nrounded (digits beyond the context precision were thrown away) and that the\nresult is inexact (some of the discarded digits were non-zero).

\n

Individual traps are set using the dictionary in the traps field of a\ncontext:

\n
>>> setcontext(ExtendedContext)\n>>> Decimal(1) / Decimal(0)\nDecimal('Infinity')\n>>> getcontext().traps[DivisionByZero] = 1\n>>> Decimal(1) / Decimal(0)\nTraceback (most recent call last):\n  File "<pyshell#112>", line 1, in -toplevel-\n    Decimal(1) / Decimal(0)\nDivisionByZero: x / 0\n
\n
\n

Most programs adjust the current context only once, at the beginning of the\nprogram. And, in many applications, data is converted to Decimal with\na single cast inside a loop. With context set and decimals created, the bulk of\nthe program manipulates the data no differently than with other Python numeric\ntypes.

\n
\n
\n

9.4.2. Decimal objects

\n
\n
\nclass decimal.Decimal([value[, context]])
\n

Construct a new Decimal object based from value.

\n

value can be an integer, string, tuple, float, or another Decimal\nobject. If no value is given, returns Decimal('0'). If value is a\nstring, it should conform to the decimal numeric string syntax after leading\nand trailing whitespace characters are removed:

\n
sign           ::=  '+' | '-'\ndigit          ::=  '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'\nindicator      ::=  'e' | 'E'\ndigits         ::=  digit [digit]...\ndecimal-part   ::=  digits '.' [digits] | ['.'] digits\nexponent-part  ::=  indicator [sign] digits\ninfinity       ::=  'Infinity' | 'Inf'\nnan            ::=  'NaN' [digits] | 'sNaN' [digits]\nnumeric-value  ::=  decimal-part [exponent-part] | infinity\nnumeric-string ::=  [sign] numeric-value | [sign] nan
\n
\n

If value is a unicode string then other Unicode decimal digits\nare also permitted where digit appears above. These include\ndecimal digits from various other alphabets (for example,\nArabic-Indic and Devanāgarī digits) along with the fullwidth digits\nu'\\uff10' through u'\\uff19'.

\n

If value is a tuple, it should have three components, a sign\n(0 for positive or 1 for negative), a tuple of\ndigits, and an integer exponent. For example, Decimal((0, (1, 4, 1, 4), -3))\nreturns Decimal('1.414').

\n

If value is a float, the binary floating point value is losslessly\nconverted to its exact decimal equivalent. This conversion can often require\n53 or more digits of precision. For example, Decimal(float('1.1'))\nconverts to\nDecimal('1.100000000000000088817841970012523233890533447265625').

\n

The context precision does not affect how many digits are stored. That is\ndetermined exclusively by the number of digits in value. For example,\nDecimal('3.00000') records all five zeros even if the context precision is\nonly three.

\n

The purpose of the context argument is determining what to do if value is a\nmalformed string. If the context traps InvalidOperation, an exception\nis raised; otherwise, the constructor returns a new Decimal with the value of\nNaN.

\n

Once constructed, Decimal objects are immutable.

\n

\nChanged in version 2.6: leading and trailing whitespace characters are permitted when\ncreating a Decimal instance from a string.

\n

\nChanged in version 2.7: The argument to the constructor is now permitted to be a float instance.

\n

Decimal floating point objects share many properties with the other built-in\nnumeric types such as float and int. All of the usual math\noperations and special methods apply. Likewise, decimal objects can be\ncopied, pickled, printed, used as dictionary keys, used as set elements,\ncompared, sorted, and coerced to another type (such as float or\nlong).

\n

Decimal objects cannot generally be combined with floats in\narithmetic operations: an attempt to add a Decimal to a\nfloat, for example, will raise a TypeError.\nThere’s one exception to this rule: it’s possible to use Python’s\ncomparison operators to compare a float instance x\nwith a Decimal instance y. Without this exception,\ncomparisons between Decimal and float instances\nwould follow the general rules for comparing objects of different\ntypes described in the Expressions section of the reference\nmanual, leading to confusing results.

\n

\nChanged in version 2.7: A comparison between a float instance x and a\nDecimal instance y now returns a result based on\nthe values of x and y. In earlier versions x < y\nreturned the same (arbitrary) result for any Decimal\ninstance x and any float instance y.

\n

In addition to the standard numeric properties, decimal floating point\nobjects also have a number of specialized methods:

\n
\n
\nadjusted()
\n
Return the adjusted exponent after shifting out the coefficient’s\nrightmost digits until only the lead digit remains:\nDecimal('321e+5').adjusted() returns seven. Used for determining the\nposition of the most significant digit with respect to the decimal point.
\n\n
\n
\nas_tuple()
\n

Return a named tuple representation of the number:\nDecimalTuple(sign, digits, exponent).

\n

\nChanged in version 2.6: Use a named tuple.

\n
\n\n
\n
\ncanonical()
\n

Return the canonical encoding of the argument. Currently, the encoding of\na Decimal instance is always canonical, so this operation returns\nits argument unchanged.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncompare(other[, context])
\n

Compare the values of two Decimal instances. This operation behaves in\nthe same way as the usual comparison method __cmp__(), except that\ncompare() returns a Decimal instance rather than an integer, and if\neither operand is a NaN then the result is a NaN:

\n
a or b is a NaN ==> Decimal('NaN')\na < b           ==> Decimal('-1')\na == b          ==> Decimal('0')\na > b           ==> Decimal('1')
\n
\n
\n\n
\n
\ncompare_signal(other[, context])
\n

This operation is identical to the compare() method, except that all\nNaNs signal. That is, if neither operand is a signaling NaN then any\nquiet NaN operand is treated as though it were a signaling NaN.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncompare_total(other)
\n

Compare two operands using their abstract representation rather than their\nnumerical value. Similar to the compare() method, but the result\ngives a total ordering on Decimal instances. Two\nDecimal instances with the same numeric value but different\nrepresentations compare unequal in this ordering:

\n
>>> Decimal('12.0').compare_total(Decimal('12'))\nDecimal('-1')\n
\n
\n

Quiet and signaling NaNs are also included in the total ordering. The\nresult of this function is Decimal('0') if both operands have the same\nrepresentation, Decimal('-1') if the first operand is lower in the\ntotal order than the second, and Decimal('1') if the first operand is\nhigher in the total order than the second operand. See the specification\nfor details of the total order.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncompare_total_mag(other)
\n

Compare two operands using their abstract representation rather than their\nvalue as in compare_total(), but ignoring the sign of each operand.\nx.compare_total_mag(y) is equivalent to\nx.copy_abs().compare_total(y.copy_abs()).

\n

\nNew in version 2.6.

\n
\n\n
\n
\nconjugate()
\n

Just returns self, this method is only to comply with the Decimal\nSpecification.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncopy_abs()
\n

Return the absolute value of the argument. This operation is unaffected\nby the context and is quiet: no flags are changed and no rounding is\nperformed.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncopy_negate()
\n

Return the negation of the argument. This operation is unaffected by the\ncontext and is quiet: no flags are changed and no rounding is performed.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ncopy_sign(other)
\n

Return a copy of the first operand with the sign set to be the same as the\nsign of the second operand. For example:

\n
>>> Decimal('2.3').copy_sign(Decimal('-1.5'))\nDecimal('-2.3')\n
\n
\n

This operation is unaffected by the context and is quiet: no flags are\nchanged and no rounding is performed.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nexp([context])
\n

Return the value of the (natural) exponential function e**x at the\ngiven number. The result is correctly rounded using the\nROUND_HALF_EVEN rounding mode.

\n
>>> Decimal(1).exp()\nDecimal('2.718281828459045235360287471')\n>>> Decimal(321).exp()\nDecimal('2.561702493119680037517373933E+139')\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nfrom_float(f)
\n

Classmethod that converts a float to a decimal number, exactly.

\n

Note Decimal.from_float(0.1) is not the same as Decimal(‘0.1’).\nSince 0.1 is not exactly representable in binary floating point, the\nvalue is stored as the nearest representable value which is\n0x1.999999999999ap-4. That equivalent value in decimal is\n0.1000000000000000055511151231257827021181583404541015625.

\n
\n

Note

\n

From Python 2.7 onwards, a Decimal instance\ncan also be constructed directly from a float.

\n
\n
>>> Decimal.from_float(0.1)\nDecimal('0.1000000000000000055511151231257827021181583404541015625')\n>>> Decimal.from_float(float('nan'))\nDecimal('NaN')\n>>> Decimal.from_float(float('inf'))\nDecimal('Infinity')\n>>> Decimal.from_float(float('-inf'))\nDecimal('-Infinity')\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nfma(other, third[, context])
\n

Fused multiply-add. Return self*other+third with no rounding of the\nintermediate product self*other.

\n
>>> Decimal(2).fma(3, 5)\nDecimal('11')\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_canonical()
\n

Return True if the argument is canonical and False\notherwise. Currently, a Decimal instance is always canonical, so\nthis operation always returns True.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_finite()
\n

Return True if the argument is a finite number, and\nFalse if the argument is an infinity or a NaN.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_infinite()
\n

Return True if the argument is either positive or negative\ninfinity and False otherwise.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_nan()
\n

Return True if the argument is a (quiet or signaling) NaN and\nFalse otherwise.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_normal()
\n

Return True if the argument is a normal finite non-zero\nnumber with an adjusted exponent greater than or equal to Emin.\nReturn False if the argument is zero, subnormal, infinite or a\nNaN. Note, the term normal is used here in a different sense with\nthe normalize() method which is used to create canonical values.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_qnan()
\n

Return True if the argument is a quiet NaN, and\nFalse otherwise.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_signed()
\n

Return True if the argument has a negative sign and\nFalse otherwise. Note that zeros and NaNs can both carry signs.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_snan()
\n

Return True if the argument is a signaling NaN and False\notherwise.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_subnormal()
\n

Return True if the argument is subnormal, and False\notherwise. A number is subnormal is if it is nonzero, finite, and has an\nadjusted exponent less than Emin.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_zero()
\n

Return True if the argument is a (positive or negative) zero and\nFalse otherwise.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nln([context])
\n

Return the natural (base e) logarithm of the operand. The result is\ncorrectly rounded using the ROUND_HALF_EVEN rounding mode.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nlog10([context])
\n

Return the base ten logarithm of the operand. The result is correctly\nrounded using the ROUND_HALF_EVEN rounding mode.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nlogb([context])
\n

For a nonzero number, return the adjusted exponent of its operand as a\nDecimal instance. If the operand is a zero then\nDecimal('-Infinity') is returned and the DivisionByZero flag\nis raised. If the operand is an infinity then Decimal('Infinity') is\nreturned.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nlogical_and(other[, context])
\n

logical_and() is a logical operation which takes two logical\noperands (see Logical operands). The result is the\ndigit-wise and of the two operands.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nlogical_invert([context])
\n

logical_invert() is a logical operation. The\nresult is the digit-wise inversion of the operand.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nlogical_or(other[, context])
\n

logical_or() is a logical operation which takes two logical\noperands (see Logical operands). The result is the\ndigit-wise or of the two operands.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nlogical_xor(other[, context])
\n

logical_xor() is a logical operation which takes two logical\noperands (see Logical operands). The result is the\ndigit-wise exclusive or of the two operands.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmax(other[, context])
\n
Like max(self, other) except that the context rounding rule is applied\nbefore returning and that NaN values are either signaled or\nignored (depending on the context and whether they are signaling or\nquiet).
\n\n
\n
\nmax_mag(other[, context])
\n

Similar to the max() method, but the comparison is done using the\nabsolute values of the operands.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nmin(other[, context])
\n
Like min(self, other) except that the context rounding rule is applied\nbefore returning and that NaN values are either signaled or\nignored (depending on the context and whether they are signaling or\nquiet).
\n\n
\n
\nmin_mag(other[, context])
\n

Similar to the min() method, but the comparison is done using the\nabsolute values of the operands.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nnext_minus([context])
\n

Return the largest number representable in the given context (or in the\ncurrent thread’s context if no context is given) that is smaller than the\ngiven operand.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nnext_plus([context])
\n

Return the smallest number representable in the given context (or in the\ncurrent thread’s context if no context is given) that is larger than the\ngiven operand.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nnext_toward(other[, context])
\n

If the two operands are unequal, return the number closest to the first\noperand in the direction of the second operand. If both operands are\nnumerically equal, return a copy of the first operand with the sign set to\nbe the same as the sign of the second operand.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nnormalize([context])
\n
Normalize the number by stripping the rightmost trailing zeros and\nconverting any result equal to Decimal('0') to\nDecimal('0e0'). Used for producing canonical values for attributes\nof an equivalence class. For example, Decimal('32.100') and\nDecimal('0.321000e+2') both normalize to the equivalent value\nDecimal('32.1').
\n\n
\n
\nnumber_class([context])
\n

Return a string describing the class of the operand. The returned value\nis one of the following ten strings.

\n
    \n
  • "-Infinity", indicating that the operand is negative infinity.
  • \n
  • "-Normal", indicating that the operand is a negative normal number.
  • \n
  • "-Subnormal", indicating that the operand is negative and subnormal.
  • \n
  • "-Zero", indicating that the operand is a negative zero.
  • \n
  • "+Zero", indicating that the operand is a positive zero.
  • \n
  • "+Subnormal", indicating that the operand is positive and subnormal.
  • \n
  • "+Normal", indicating that the operand is a positive normal number.
  • \n
  • "+Infinity", indicating that the operand is positive infinity.
  • \n
  • "NaN", indicating that the operand is a quiet NaN (Not a Number).
  • \n
  • "sNaN", indicating that the operand is a signaling NaN.
  • \n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nquantize(exp[, rounding[, context[, watchexp]]])
\n

Return a value equal to the first operand after rounding and having the\nexponent of the second operand.

\n
>>> Decimal('1.41421356').quantize(Decimal('1.000'))\nDecimal('1.414')\n
\n
\n

Unlike other operations, if the length of the coefficient after the\nquantize operation would be greater than precision, then an\nInvalidOperation is signaled. This guarantees that, unless there\nis an error condition, the quantized exponent is always equal to that of\nthe right-hand operand.

\n

Also unlike other operations, quantize never signals Underflow, even if\nthe result is subnormal and inexact.

\n

If the exponent of the second operand is larger than that of the first\nthen rounding may be necessary. In this case, the rounding mode is\ndetermined by the rounding argument if given, else by the given\ncontext argument; if neither argument is given the rounding mode of\nthe current thread’s context is used.

\n

If watchexp is set (default), then an error is returned whenever the\nresulting exponent is greater than Emax or less than\nEtiny.

\n
\n\n
\n
\nradix()
\n

Return Decimal(10), the radix (base) in which the Decimal\nclass does all its arithmetic. Included for compatibility with the\nspecification.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nremainder_near(other[, context])
\n

Compute the modulo as either a positive or negative value depending on\nwhich is closest to zero. For instance, Decimal(10).remainder_near(6)\nreturns Decimal('-2') which is closer to zero than Decimal('4').

\n

If both are equally close, the one chosen will have the same sign as\nself.

\n
\n\n
\n
\nrotate(other[, context])
\n

Return the result of rotating the digits of the first operand by an amount\nspecified by the second operand. The second operand must be an integer in\nthe range -precision through precision. The absolute value of the second\noperand gives the number of places to rotate. If the second operand is\npositive then rotation is to the left; otherwise rotation is to the right.\nThe coefficient of the first operand is padded on the left with zeros to\nlength precision if necessary. The sign and exponent of the first operand\nare unchanged.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsame_quantum(other[, context])
\n
Test whether self and other have the same exponent or whether both are\nNaN.
\n\n
\n
\nscaleb(other[, context])
\n

Return the first operand with exponent adjusted by the second.\nEquivalently, return the first operand multiplied by 10**other. The\nsecond operand must be an integer.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nshift(other[, context])
\n

Return the result of shifting the digits of the first operand by an amount\nspecified by the second operand. The second operand must be an integer in\nthe range -precision through precision. The absolute value of the second\noperand gives the number of places to shift. If the second operand is\npositive then the shift is to the left; otherwise the shift is to the\nright. Digits shifted into the coefficient are zeros. The sign and\nexponent of the first operand are unchanged.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsqrt([context])
\n
Return the square root of the argument to full precision.
\n\n
\n
\nto_eng_string([context])
\n

Convert to an engineering-type string.

\n

Engineering notation has an exponent which is a multiple of 3, so there\nare up to 3 digits left of the decimal place. For example, converts\nDecimal('123E+1') to Decimal('1.23E+3')

\n
\n\n
\n
\nto_integral([rounding[, context]])
\n
Identical to the to_integral_value() method. The to_integral\nname has been kept for compatibility with older versions.
\n\n
\n
\nto_integral_exact([rounding[, context]])
\n

Round to the nearest integer, signaling Inexact or\nRounded as appropriate if rounding occurs. The rounding mode is\ndetermined by the rounding parameter if given, else by the given\ncontext. If neither parameter is given then the rounding mode of the\ncurrent context is used.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nto_integral_value([rounding[, context]])
\n

Round to the nearest integer without signaling Inexact or\nRounded. If given, applies rounding; otherwise, uses the\nrounding method in either the supplied context or the current context.

\n

\nChanged in version 2.6: renamed from to_integral to to_integral_value. The old name\nremains valid for compatibility.

\n
\n\n
\n\n
\n

9.4.2.1. Logical operands

\n

The logical_and(), logical_invert(), logical_or(),\nand logical_xor() methods expect their arguments to be logical\noperands. A logical operand is a Decimal instance whose\nexponent and sign are both zero, and whose digits are all either\n0 or 1.

\n
\n
\n
\n

9.4.3. Context objects

\n

Contexts are environments for arithmetic operations. They govern precision, set\nrules for rounding, determine which signals are treated as exceptions, and limit\nthe range for exponents.

\n

Each thread has its own current context which is accessed or changed using the\ngetcontext() and setcontext() functions:

\n
\n
\ndecimal.getcontext()
\n
Return the current context for the active thread.
\n\n
\n
\ndecimal.setcontext(c)
\n
Set the current context for the active thread to c.
\n\n

Beginning with Python 2.5, you can also use the with statement and\nthe localcontext() function to temporarily change the active context.

\n
\n
\ndecimal.localcontext([c])
\n

Return a context manager that will set the current context for the active thread\nto a copy of c on entry to the with-statement and restore the previous context\nwhen exiting the with-statement. If no context is specified, a copy of the\ncurrent context is used.

\n

\nNew in version 2.5.

\n

For example, the following code sets the current decimal precision to 42 places,\nperforms a calculation, and then automatically restores the previous context:

\n
from decimal import localcontext\n\nwith localcontext() as ctx:\n    ctx.prec = 42   # Perform a high precision calculation\n    s = calculate_something()\ns = +s  # Round the final result back to the default precision\n
\n
\n
\n\n

New contexts can also be created using the Context constructor\ndescribed below. In addition, the module provides three pre-made contexts:

\n
\n
\nclass decimal.BasicContext
\n

This is a standard context defined by the General Decimal Arithmetic\nSpecification. Precision is set to nine. Rounding is set to\nROUND_HALF_UP. All flags are cleared. All traps are enabled (treated\nas exceptions) except Inexact, Rounded, and\nSubnormal.

\n

Because many of the traps are enabled, this context is useful for debugging.

\n
\n\n
\n
\nclass decimal.ExtendedContext
\n

This is a standard context defined by the General Decimal Arithmetic\nSpecification. Precision is set to nine. Rounding is set to\nROUND_HALF_EVEN. All flags are cleared. No traps are enabled (so that\nexceptions are not raised during computations).

\n

Because the traps are disabled, this context is useful for applications that\nprefer to have result value of NaN or Infinity instead of\nraising exceptions. This allows an application to complete a run in the\npresence of conditions that would otherwise halt the program.

\n
\n\n
\n
\nclass decimal.DefaultContext
\n

This context is used by the Context constructor as a prototype for new\ncontexts. Changing a field (such a precision) has the effect of changing the\ndefault for new contexts created by the Context constructor.

\n

This context is most useful in multi-threaded environments. Changing one of the\nfields before threads are started has the effect of setting system-wide\ndefaults. Changing the fields after threads have started is not recommended as\nit would require thread synchronization to prevent race conditions.

\n

In single threaded environments, it is preferable to not use this context at\nall. Instead, simply create contexts explicitly as described below.

\n

The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled traps\nfor Overflow, InvalidOperation, and DivisionByZero.

\n
\n\n

In addition to the three supplied contexts, new contexts can be created with the\nContext constructor.

\n
\n
\nclass decimal.Context(prec=None, rounding=None, traps=None, flags=None, Emin=None, Emax=None, capitals=1)
\n

Creates a new context. If a field is not specified or is None, the\ndefault values are copied from the DefaultContext. If the flags\nfield is not specified or is None, all flags are cleared.

\n

The prec field is a positive integer that sets the precision for arithmetic\noperations in the context.

\n

The rounding option is one of:

\n
    \n
  • ROUND_CEILING (towards Infinity),
  • \n
  • ROUND_DOWN (towards zero),
  • \n
  • ROUND_FLOOR (towards -Infinity),
  • \n
  • ROUND_HALF_DOWN (to nearest with ties going towards zero),
  • \n
  • ROUND_HALF_EVEN (to nearest with ties going to nearest even integer),
  • \n
  • ROUND_HALF_UP (to nearest with ties going away from zero), or
  • \n
  • ROUND_UP (away from zero).
  • \n
  • ROUND_05UP (away from zero if last digit after rounding towards zero\nwould have been 0 or 5; otherwise towards zero)
  • \n
\n

The traps and flags fields list any signals to be set. Generally, new\ncontexts should only set traps and leave the flags clear.

\n

The Emin and Emax fields are integers specifying the outer limits allowable\nfor exponents.

\n

The capitals field is either 0 or 1 (the default). If set to\n1, exponents are printed with a capital E; otherwise, a\nlowercase e is used: Decimal('6.02e+23').

\n

\nChanged in version 2.6: The ROUND_05UP rounding mode was added.

\n

The Context class defines several general purpose methods as well as\na large number of methods for doing arithmetic directly in a given context.\nIn addition, for each of the Decimal methods described above (with\nthe exception of the adjusted() and as_tuple() methods) there is\na corresponding Context method. For example, for a Context\ninstance C and Decimal instance x, C.exp(x) is\nequivalent to x.exp(context=C). Each Context method accepts a\nPython integer (an instance of int or long) anywhere that a\nDecimal instance is accepted.

\n
\n
\nclear_flags()
\n
Resets all of the flags to 0.
\n\n
\n
\ncopy()
\n
Return a duplicate of the context.
\n\n
\n
\ncopy_decimal(num)
\n
Return a copy of the Decimal instance num.
\n\n
\n
\ncreate_decimal(num)
\n

Creates a new Decimal instance from num but using self as\ncontext. Unlike the Decimal constructor, the context precision,\nrounding method, flags, and traps are applied to the conversion.

\n

This is useful because constants are often given to a greater precision\nthan is needed by the application. Another benefit is that rounding\nimmediately eliminates unintended effects from digits beyond the current\nprecision. In the following example, using unrounded inputs means that\nadding zero to a sum can change the result:

\n
>>> getcontext().prec = 3\n>>> Decimal('3.4445') + Decimal('1.0023')\nDecimal('4.45')\n>>> Decimal('3.4445') + Decimal(0) + Decimal('1.0023')\nDecimal('4.44')\n
\n
\n

This method implements the to-number operation of the IBM specification.\nIf the argument is a string, no leading or trailing whitespace is\npermitted.

\n
\n\n
\n
\ncreate_decimal_from_float(f)
\n

Creates a new Decimal instance from a float f but rounding using self\nas the context. Unlike the Decimal.from_float() class method,\nthe context precision, rounding method, flags, and traps are applied to\nthe conversion.

\n
>>> context = Context(prec=5, rounding=ROUND_DOWN)\n>>> context.create_decimal_from_float(math.pi)\nDecimal('3.1415')\n>>> context = Context(prec=5, traps=[Inexact])\n>>> context.create_decimal_from_float(math.pi)\nTraceback (most recent call last):\n    ...\nInexact: None\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nEtiny()
\n
Returns a value equal to Emin - prec + 1 which is the minimum exponent\nvalue for subnormal results. When underflow occurs, the exponent is set\nto Etiny.
\n\n
\n
\nEtop()
\n
Returns a value equal to Emax - prec + 1.
\n\n

The usual approach to working with decimals is to create Decimal\ninstances and then apply arithmetic operations which take place within the\ncurrent context for the active thread. An alternative approach is to use\ncontext methods for calculating within a specific context. The methods are\nsimilar to those for the Decimal class and are only briefly\nrecounted here.

\n
\n
\nabs(x)
\n
Returns the absolute value of x.
\n\n
\n
\nadd(x, y)
\n
Return the sum of x and y.
\n\n
\n
\ncanonical(x)
\n
Returns the same Decimal object x.
\n\n
\n
\ncompare(x, y)
\n
Compares x and y numerically.
\n\n
\n
\ncompare_signal(x, y)
\n
Compares the values of the two operands numerically.
\n\n
\n
\ncompare_total(x, y)
\n
Compares two operands using their abstract representation.
\n\n
\n
\ncompare_total_mag(x, y)
\n
Compares two operands using their abstract representation, ignoring sign.
\n\n
\n
\ncopy_abs(x)
\n
Returns a copy of x with the sign set to 0.
\n\n
\n
\ncopy_negate(x)
\n
Returns a copy of x with the sign inverted.
\n\n
\n
\ncopy_sign(x, y)
\n
Copies the sign from y to x.
\n\n
\n
\ndivide(x, y)
\n
Return x divided by y.
\n\n
\n
\ndivide_int(x, y)
\n
Return x divided by y, truncated to an integer.
\n\n
\n
\ndivmod(x, y)
\n
Divides two numbers and returns the integer part of the result.
\n\n
\n
\nexp(x)
\n
Returns e ** x.
\n\n
\n
\nfma(x, y, z)
\n
Returns x multiplied by y, plus z.
\n\n
\n
\nis_canonical(x)
\n
Returns True if x is canonical; otherwise returns False.
\n\n
\n
\nis_finite(x)
\n
Returns True if x is finite; otherwise returns False.
\n\n
\n
\nis_infinite(x)
\n
Returns True if x is infinite; otherwise returns False.
\n\n
\n
\nis_nan(x)
\n
Returns True if x is a qNaN or sNaN; otherwise returns False.
\n\n
\n
\nis_normal(x)
\n
Returns True if x is a normal number; otherwise returns False.
\n\n
\n
\nis_qnan(x)
\n
Returns True if x is a quiet NaN; otherwise returns False.
\n\n
\n
\nis_signed(x)
\n
Returns True if x is negative; otherwise returns False.
\n\n
\n
\nis_snan(x)
\n
Returns True if x is a signaling NaN; otherwise returns False.
\n\n
\n
\nis_subnormal(x)
\n
Returns True if x is subnormal; otherwise returns False.
\n\n
\n
\nis_zero(x)
\n
Returns True if x is a zero; otherwise returns False.
\n\n
\n
\nln(x)
\n
Returns the natural (base e) logarithm of x.
\n\n
\n
\nlog10(x)
\n
Returns the base 10 logarithm of x.
\n\n
\n
\nlogb(x)
\n
Returns the exponent of the magnitude of the operand’s MSD.
\n\n
\n
\nlogical_and(x, y)
\n
Applies the logical operation and between each operand’s digits.
\n\n
\n
\nlogical_invert(x)
\n
Invert all the digits in x.
\n\n
\n
\nlogical_or(x, y)
\n
Applies the logical operation or between each operand’s digits.
\n\n
\n
\nlogical_xor(x, y)
\n
Applies the logical operation xor between each operand’s digits.
\n\n
\n
\nmax(x, y)
\n
Compares two values numerically and returns the maximum.
\n\n
\n
\nmax_mag(x, y)
\n
Compares the values numerically with their sign ignored.
\n\n
\n
\nmin(x, y)
\n
Compares two values numerically and returns the minimum.
\n\n
\n
\nmin_mag(x, y)
\n
Compares the values numerically with their sign ignored.
\n\n
\n
\nminus(x)
\n
Minus corresponds to the unary prefix minus operator in Python.
\n\n
\n
\nmultiply(x, y)
\n
Return the product of x and y.
\n\n
\n
\nnext_minus(x)
\n
Returns the largest representable number smaller than x.
\n\n
\n
\nnext_plus(x)
\n
Returns the smallest representable number larger than x.
\n\n
\n
\nnext_toward(x, y)
\n
Returns the number closest to x, in direction towards y.
\n\n
\n
\nnormalize(x)
\n
Reduces x to its simplest form.
\n\n
\n
\nnumber_class(x)
\n
Returns an indication of the class of x.
\n\n
\n
\nplus(x)
\n
Plus corresponds to the unary prefix plus operator in Python. This\noperation applies the context precision and rounding, so it is not an\nidentity operation.
\n\n
\n
\npower(x, y[, modulo])
\n

Return x to the power of y, reduced modulo modulo if given.

\n

With two arguments, compute x**y. If x is negative then y\nmust be integral. The result will be inexact unless y is integral and\nthe result is finite and can be expressed exactly in ‘precision’ digits.\nThe result should always be correctly rounded, using the rounding mode of\nthe current thread’s context.

\n

With three arguments, compute (x**y) modulo. For the three argument\nform, the following restrictions on the arguments hold:

\n
\n
    \n
  • all three arguments must be integral
  • \n
  • y must be nonnegative
  • \n
  • at least one of x or y must be nonzero
  • \n
  • modulo must be nonzero and have at most ‘precision’ digits
  • \n
\n
\n

The value resulting from Context.power(x, y, modulo) is\nequal to the value that would be obtained by computing (x**y)\n modulo with unbounded precision, but is computed more\nefficiently. The exponent of the result is zero, regardless of\nthe exponents of x, y and modulo. The result is\nalways exact.

\n

\nChanged in version 2.6: y may now be nonintegral in x**y.\nStricter requirements for the three-argument version.

\n
\n\n
\n
\nquantize(x, y)
\n
Returns a value equal to x (rounded), having the exponent of y.
\n\n
\n
\nradix()
\n
Just returns 10, as this is Decimal, :)
\n\n
\n
\nremainder(x, y)
\n

Returns the remainder from integer division.

\n

The sign of the result, if non-zero, is the same as that of the original\ndividend.

\n
\n\n
\n
\nremainder_near(x, y)
\n
Returns x - y * n, where n is the integer nearest the exact value\nof x / y (if the result is 0 then its sign will be the sign of x).
\n\n
\n
\nrotate(x, y)
\n
Returns a rotated copy of x, y times.
\n\n
\n
\nsame_quantum(x, y)
\n
Returns True if the two operands have the same exponent.
\n\n
\n
\nscaleb(x, y)
\n
Returns the first operand after adding the second value its exp.
\n\n
\n
\nshift(x, y)
\n
Returns a shifted copy of x, y times.
\n\n
\n
\nsqrt(x)
\n
Square root of a non-negative number to context precision.
\n\n
\n
\nsubtract(x, y)
\n
Return the difference between x and y.
\n\n
\n
\nto_eng_string(x)
\n
Converts a number to a string, using scientific notation.
\n\n
\n
\nto_integral_exact(x)
\n
Rounds to an integer.
\n\n
\n
\nto_sci_string(x)
\n
Converts a number to a string using scientific notation.
\n\n
\n\n
\n
\n

9.4.4. Signals

\n

Signals represent conditions that arise during computation. Each corresponds to\none context flag and one context trap enabler.

\n

The context flag is set whenever the condition is encountered. After the\ncomputation, flags may be checked for informational purposes (for instance, to\ndetermine whether a computation was exact). After checking the flags, be sure to\nclear all flags before starting the next computation.

\n

If the context’s trap enabler is set for the signal, then the condition causes a\nPython exception to be raised. For example, if the DivisionByZero trap\nis set, then a DivisionByZero exception is raised upon encountering the\ncondition.

\n
\n
\nclass decimal.Clamped
\n

Altered an exponent to fit representation constraints.

\n

Typically, clamping occurs when an exponent falls outside the context’s\nEmin and Emax limits. If possible, the exponent is reduced to\nfit by adding zeros to the coefficient.

\n
\n\n
\n
\nclass decimal.DecimalException
\n
Base class for other signals and a subclass of ArithmeticError.
\n\n
\n
\nclass decimal.DivisionByZero
\n

Signals the division of a non-infinite number by zero.

\n

Can occur with division, modulo division, or when raising a number to a negative\npower. If this signal is not trapped, returns Infinity or\n-Infinity with the sign determined by the inputs to the calculation.

\n
\n\n
\n
\nclass decimal.Inexact
\n

Indicates that rounding occurred and the result is not exact.

\n

Signals when non-zero digits were discarded during rounding. The rounded result\nis returned. The signal flag or trap is used to detect when results are\ninexact.

\n
\n\n
\n
\nclass decimal.InvalidOperation
\n

An invalid operation was performed.

\n

Indicates that an operation was requested that does not make sense. If not\ntrapped, returns NaN. Possible causes include:

\n
Infinity - Infinity\n0 * Infinity\nInfinity / Infinity\nx  0\nInfinity  x\nx._rescale( non-integer )\nsqrt(-x) and x > 0\n0 ** 0\nx ** (non-integer)\nx ** Infinity\n
\n
\n
\n\n
\n
\nclass decimal.Overflow
\n

Numerical overflow.

\n

Indicates the exponent is larger than Emax after rounding has\noccurred. If not trapped, the result depends on the rounding mode, either\npulling inward to the largest representable finite number or rounding outward\nto Infinity. In either case, Inexact and Rounded\nare also signaled.

\n
\n\n
\n
\nclass decimal.Rounded
\n

Rounding occurred though possibly no information was lost.

\n

Signaled whenever rounding discards digits; even if those digits are zero\n(such as rounding 5.00 to 5.0). If not trapped, returns\nthe result unchanged. This signal is used to detect loss of significant\ndigits.

\n
\n\n
\n
\nclass decimal.Subnormal
\n

Exponent was lower than Emin prior to rounding.

\n

Occurs when an operation result is subnormal (the exponent is too small). If\nnot trapped, returns the result unchanged.

\n
\n\n
\n
\nclass decimal.Underflow
\n

Numerical underflow with result rounded to zero.

\n

Occurs when a subnormal result is pushed to zero by rounding. Inexact\nand Subnormal are also signaled.

\n
\n\n

The following table summarizes the hierarchy of signals:

\n
exceptions.ArithmeticError(exceptions.StandardError)\n    DecimalException\n        Clamped\n        DivisionByZero(DecimalException, exceptions.ZeroDivisionError)\n        Inexact\n            Overflow(Inexact, Rounded)\n            Underflow(Inexact, Rounded, Subnormal)\n        InvalidOperation\n        Rounded\n        Subnormal
\n
\n
\n
\n

9.4.5. Floating Point Notes

\n
\n

9.4.5.1. Mitigating round-off error with increased precision

\n

The use of decimal floating point eliminates decimal representation error\n(making it possible to represent 0.1 exactly); however, some operations\ncan still incur round-off error when non-zero digits exceed the fixed precision.

\n

The effects of round-off error can be amplified by the addition or subtraction\nof nearly offsetting quantities resulting in loss of significance. Knuth\nprovides two instructive examples where rounded floating point arithmetic with\ninsufficient precision causes the breakdown of the associative and distributive\nproperties of addition:

\n
# Examples from Seminumerical Algorithms, Section 4.2.2.\n>>> from decimal import Decimal, getcontext\n>>> getcontext().prec = 8\n\n>>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')\n>>> (u + v) + w\nDecimal('9.5111111')\n>>> u + (v + w)\nDecimal('10')\n\n>>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')\n>>> (u*v) + (u*w)\nDecimal('0.01')\n>>> u * (v+w)\nDecimal('0.0060000')
\n
\n

The decimal module makes it possible to restore the identities by\nexpanding the precision sufficiently to avoid loss of significance:

\n
>>> getcontext().prec = 20\n>>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')\n>>> (u + v) + w\nDecimal('9.51111111')\n>>> u + (v + w)\nDecimal('9.51111111')\n>>>\n>>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')\n>>> (u*v) + (u*w)\nDecimal('0.0060000')\n>>> u * (v+w)\nDecimal('0.0060000')\n
\n
\n
\n
\n

9.4.5.2. Special values

\n

The number system for the decimal module provides special values\nincluding NaN, sNaN, -Infinity, Infinity,\nand two zeros, +0 and -0.

\n

Infinities can be constructed directly with: Decimal('Infinity'). Also,\nthey can arise from dividing by zero when the DivisionByZero signal is\nnot trapped. Likewise, when the Overflow signal is not trapped, infinity\ncan result from rounding beyond the limits of the largest representable number.

\n

The infinities are signed (affine) and can be used in arithmetic operations\nwhere they get treated as very large, indeterminate numbers. For instance,\nadding a constant to infinity gives another infinite result.

\n

Some operations are indeterminate and return NaN, or if the\nInvalidOperation signal is trapped, raise an exception. For example,\n0/0 returns NaN which means “not a number”. This variety of\nNaN is quiet and, once created, will flow through other computations\nalways resulting in another NaN. This behavior can be useful for a\nseries of computations that occasionally have missing inputs — it allows the\ncalculation to proceed while flagging specific results as invalid.

\n

A variant is sNaN which signals rather than remaining quiet after every\noperation. This is a useful return value when an invalid result needs to\ninterrupt a calculation for special handling.

\n

The behavior of Python’s comparison operators can be a little surprising where a\nNaN is involved. A test for equality where one of the operands is a\nquiet or signaling NaN always returns False (even when doing\nDecimal('NaN')==Decimal('NaN')), while a test for inequality always returns\nTrue. An attempt to compare two Decimals using any of the <,\n<=, > or >= operators will raise the InvalidOperation signal\nif either operand is a NaN, and return False if this signal is\nnot trapped. Note that the General Decimal Arithmetic specification does not\nspecify the behavior of direct comparisons; these rules for comparisons\ninvolving a NaN were taken from the IEEE 854 standard (see Table 3 in\nsection 5.7). To ensure strict standards-compliance, use the compare()\nand compare-signal() methods instead.

\n

The signed zeros can result from calculations that underflow. They keep the sign\nthat would have resulted if the calculation had been carried out to greater\nprecision. Since their magnitude is zero, both positive and negative zeros are\ntreated as equal and their sign is informational.

\n

In addition to the two signed zeros which are distinct yet equal, there are\nvarious representations of zero with differing precisions yet equivalent in\nvalue. This takes a bit of getting used to. For an eye accustomed to\nnormalized floating point representations, it is not immediately obvious that\nthe following calculation returns a value equal to zero:

\n
>>> 1 / Decimal('Infinity')\nDecimal('0E-1000000026')\n
\n
\n
\n
\n
\n

9.4.6. Working with threads

\n

The getcontext() function accesses a different Context object for\neach thread. Having separate thread contexts means that threads may make\nchanges (such as getcontext.prec=10) without interfering with other threads.

\n

Likewise, the setcontext() function automatically assigns its target to\nthe current thread.

\n

If setcontext() has not been called before getcontext(), then\ngetcontext() will automatically create a new context for use in the\ncurrent thread.

\n

The new context is copied from a prototype context called DefaultContext. To\ncontrol the defaults so that each thread will use the same values throughout the\napplication, directly modify the DefaultContext object. This should be done\nbefore any threads are started so that there won’t be a race condition between\nthreads calling getcontext(). For example:

\n
# Set applicationwide defaults for all threads about to be launched\nDefaultContext.prec = 12\nDefaultContext.rounding = ROUND_DOWN\nDefaultContext.traps = ExtendedContext.traps.copy()\nDefaultContext.traps[InvalidOperation] = 1\nsetcontext(DefaultContext)\n\n# Afterwards, the threads can be started\nt1.start()\nt2.start()\nt3.start()\n . . .
\n
\n
\n
\n

9.4.7. Recipes

\n

Here are a few recipes that serve as utility functions and that demonstrate ways\nto work with the Decimal class:

\n
def moneyfmt(value, places=2, curr='', sep=',', dp='.',\n             pos='', neg='-', trailneg=''):\n    """Convert Decimal to a money formatted string.\n\n    places:  required number of places after the decimal point\n    curr:    optional currency symbol before the sign (may be blank)\n    sep:     optional grouping separator (comma, period, space, or blank)\n    dp:      decimal point indicator (comma or period)\n             only specify as blank when places is zero\n    pos:     optional sign for positive numbers: '+', space or blank\n    neg:     optional sign for negative numbers: '-', '(', space or blank\n    trailneg:optional trailing minus indicator:  '-', ')', space or blank\n\n    >>> d = Decimal('-1234567.8901')\n    >>> moneyfmt(d, curr='$')\n    '-$1,234,567.89'\n    >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')\n    '1.234.568-'\n    >>> moneyfmt(d, curr='$', neg='(', trailneg=')')\n    '($1,234,567.89)'\n    >>> moneyfmt(Decimal(123456789), sep=' ')\n    '123 456 789.00'\n    >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')\n    '<0.02>'\n\n    """\n    q = Decimal(10) ** -places      # 2 places --> '0.01'\n    sign, digits, exp = value.quantize(q).as_tuple()\n    result = []\n    digits = map(str, digits)\n    build, next = result.append, digits.pop\n    if sign:\n        build(trailneg)\n    for i in range(places):\n        build(next() if digits else '0')\n    build(dp)\n    if not digits:\n        build('0')\n    i = 0\n    while digits:\n        build(next())\n        i += 1\n        if i == 3 and digits:\n            i = 0\n            build(sep)\n    build(curr)\n    build(neg if sign else pos)\n    return ''.join(reversed(result))\n\ndef pi():\n    """Compute Pi to the current precision.\n\n    >>> print pi()\n    3.141592653589793238462643383\n\n    """\n    getcontext().prec += 2  # extra digits for intermediate steps\n    three = Decimal(3)      # substitute "three=3.0" for regular floats\n    lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24\n    while s != lasts:\n        lasts = s\n        n, na = n+na, na+8\n        d, da = d+da, da+32\n        t = (t * n) / d\n        s += t\n    getcontext().prec -= 2\n    return +s               # unary plus applies the new precision\n\ndef exp(x):\n    """Return e raised to the power of x.  Result type matches input type.\n\n    >>> print exp(Decimal(1))\n    2.718281828459045235360287471\n    >>> print exp(Decimal(2))\n    7.389056098930650227230427461\n    >>> print exp(2.0)\n    7.38905609893\n    >>> print exp(2+0j)\n    (7.38905609893+0j)\n\n    """\n    getcontext().prec += 2\n    i, lasts, s, fact, num = 0, 0, 1, 1, 1\n    while s != lasts:\n        lasts = s\n        i += 1\n        fact *= i\n        num *= x\n        s += num / fact\n    getcontext().prec -= 2\n    return +s\n\ndef cos(x):\n    """Return the cosine of x as measured in radians.\n\n    >>> print cos(Decimal('0.5'))\n    0.8775825618903727161162815826\n    >>> print cos(0.5)\n    0.87758256189\n    >>> print cos(0.5+0j)\n    (0.87758256189+0j)\n\n    """\n    getcontext().prec += 2\n    i, lasts, s, fact, num, sign = 0, 0, 1, 1, 1, 1\n    while s != lasts:\n        lasts = s\n        i += 2\n        fact *= i * (i-1)\n        num *= x * x\n        sign *= -1\n        s += num / fact * sign\n    getcontext().prec -= 2\n    return +s\n\ndef sin(x):\n    """Return the sine of x as measured in radians.\n\n    >>> print sin(Decimal('0.5'))\n    0.4794255386042030002732879352\n    >>> print sin(0.5)\n    0.479425538604\n    >>> print sin(0.5+0j)\n    (0.479425538604+0j)\n\n    """\n    getcontext().prec += 2\n    i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1\n    while s != lasts:\n        lasts = s\n        i += 2\n        fact *= i * (i-1)\n        num *= x * x\n        sign *= -1\n        s += num / fact * sign\n    getcontext().prec -= 2\n    return +s\n
\n
\n
\n
\n

9.4.8. Decimal FAQ

\n

Q. It is cumbersome to type decimal.Decimal('1234.5'). Is there a way to\nminimize typing when using the interactive interpreter?

\n

A. Some users abbreviate the constructor to just a single letter:

\n
>>> D = decimal.Decimal\n>>> D('1.23') + D('3.45')\nDecimal('4.68')\n
\n
\n

Q. In a fixed-point application with two decimal places, some inputs have many\nplaces and need to be rounded. Others are not supposed to have excess digits\nand need to be validated. What methods should be used?

\n

A. The quantize() method rounds to a fixed number of decimal places. If\nthe Inexact trap is set, it is also useful for validation:

\n
\n
>>> TWOPLACES = Decimal(10) ** -2       # same as Decimal('0.01')\n
\n
\n
>>> # Round to two places\n>>> Decimal('3.214').quantize(TWOPLACES)\nDecimal('3.21')\n
\n
\n
>>> # Validate that a number does not exceed two places\n>>> Decimal('3.21').quantize(TWOPLACES, context=Context(traps=[Inexact]))\nDecimal('3.21')\n
\n
\n
>>> Decimal('3.214').quantize(TWOPLACES, context=Context(traps=[Inexact]))\nTraceback (most recent call last):\n   ...\nInexact: None\n
\n
\n
\n

Q. Once I have valid two place inputs, how do I maintain that invariant\nthroughout an application?

\n

A. Some operations like addition, subtraction, and multiplication by an integer\nwill automatically preserve fixed point. Others operations, like division and\nnon-integer multiplication, will change the number of decimal places and need to\nbe followed-up with a quantize() step:

\n
>>> a = Decimal('102.72')           # Initial fixed-point values\n>>> b = Decimal('3.17')\n>>> a + b                           # Addition preserves fixed-point\nDecimal('105.89')\n>>> a - b\nDecimal('99.55')\n>>> a * 42                          # So does integer multiplication\nDecimal('4314.24')\n>>> (a * b).quantize(TWOPLACES)     # Must quantize non-integer multiplication\nDecimal('325.62')\n>>> (b / a).quantize(TWOPLACES)     # And quantize division\nDecimal('0.03')\n
\n
\n

In developing fixed-point applications, it is convenient to define functions\nto handle the quantize() step:

\n
\n
>>> def mul(x, y, fp=TWOPLACES):\n...     return (x * y).quantize(fp)\n>>> def div(x, y, fp=TWOPLACES):\n...     return (x / y).quantize(fp)\n
\n
\n
>>> mul(a, b)                       # Automatically preserve fixed-point\nDecimal('325.62')\n>>> div(b, a)\nDecimal('0.03')\n
\n
\n
\n

Q. There are many ways to express the same value. The numbers 200,\n200.000, 2E2, and 02E+4 all have the same value at\nvarious precisions. Is there a way to transform them to a single recognizable\ncanonical value?

\n

A. The normalize() method maps all equivalent values to a single\nrepresentative:

\n
>>> values = map(Decimal, '200 200.000 2E2 .02E+4'.split())\n>>> [v.normalize() for v in values]\n[Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]\n
\n
\n

Q. Some decimal values always print with exponential notation. Is there a way\nto get a non-exponential representation?

\n

A. For some values, exponential notation is the only way to express the number\nof significant places in the coefficient. For example, expressing\n5.0E+3 as 5000 keeps the value constant but cannot show the\noriginal’s two-place significance.

\n

If an application does not care about tracking significance, it is easy to\nremove the exponent and trailing zeros, losing significance, but keeping the\nvalue unchanged:

\n
def remove_exponent(d):\n    '''Remove exponent and trailing zeros.\n\n    >>> remove_exponent(Decimal('5E+3'))\n    Decimal('5000')\n\n    '''\n    return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()\n
\n
\n

Q. Is there a way to convert a regular float to a Decimal?

\n

A. Yes, any binary floating point number can be exactly expressed as a\nDecimal though an exact conversion may take more precision than intuition would\nsuggest:

\n
>>> Decimal(math.pi)\nDecimal('3.141592653589793115997963468544185161590576171875')\n
\n
\n

Q. Within a complex calculation, how can I make sure that I haven’t gotten a\nspurious result because of insufficient precision or rounding anomalies.

\n

A. The decimal module makes it easy to test results. A best practice is to\nre-run calculations using greater precision and with various rounding modes.\nWidely differing results indicate insufficient precision, rounding mode issues,\nill-conditioned inputs, or a numerically unstable algorithm.

\n

Q. I noticed that context precision is applied to the results of operations but\nnot to the inputs. Is there anything to watch out for when mixing values of\ndifferent precisions?

\n

A. Yes. The principle is that all values are considered to be exact and so is\nthe arithmetic on those values. Only the results are rounded. The advantage\nfor inputs is that “what you type is what you get”. A disadvantage is that the\nresults can look odd if you forget that the inputs haven’t been rounded:

\n
>>> getcontext().prec = 3\n>>> Decimal('3.104') + Decimal('2.104')\nDecimal('5.21')\n>>> Decimal('3.104') + Decimal('0.000') + Decimal('2.104')\nDecimal('5.20')\n
\n
\n

The solution is either to increase precision or to force rounding of inputs\nusing the unary plus operation:

\n
>>> getcontext().prec = 3\n>>> +Decimal('1.23456789')      # unary plus triggers rounding\nDecimal('1.23')\n
\n
\n

Alternatively, inputs can be rounded upon creation using the\nContext.create_decimal() method:

\n
>>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678')\nDecimal('1.2345')\n
\n
\n
\n
", "searchableItems": [ { "name": "decimal.BasicContext", "domId": "decimal_decimal.BasicContext" }, { "name": "decimal.Clamped", "domId": "decimal_decimal.Clamped" }, { "name": "decimal.Context", "domId": "decimal_decimal.Context" }, { "name": "decimal.Context.abs", "domId": "decimal_decimal.Context.abs" }, { "name": "decimal.Context.add", "domId": "decimal_decimal.Context.add" }, { "name": "decimal.Context.canonical", "domId": "decimal_decimal.Context.canonical" }, { "name": "decimal.Context.clear_flags", "domId": "decimal_decimal.Context.clear_flags" }, { "name": "decimal.Context.compare", "domId": "decimal_decimal.Context.compare" }, { "name": "decimal.Context.compare_signal", "domId": "decimal_decimal.Context.compare_signal" }, { "name": "decimal.Context.compare_total", "domId": "decimal_decimal.Context.compare_total" }, { "name": "decimal.Context.compare_total_mag", "domId": "decimal_decimal.Context.compare_total_mag" }, { "name": "decimal.Context.copy", "domId": "decimal_decimal.Context.copy" }, { "name": "decimal.Context.copy_abs", "domId": "decimal_decimal.Context.copy_abs" }, { "name": "decimal.Context.copy_decimal", "domId": "decimal_decimal.Context.copy_decimal" }, { "name": "decimal.Context.copy_negate", "domId": "decimal_decimal.Context.copy_negate" }, { "name": "decimal.Context.copy_sign", "domId": "decimal_decimal.Context.copy_sign" }, { "name": "decimal.Context.create_decimal", "domId": "decimal_decimal.Context.create_decimal" }, { "name": "decimal.Context.create_decimal_from_float", "domId": "decimal_decimal.Context.create_decimal_from_float" }, { "name": "decimal.Context.divide", "domId": "decimal_decimal.Context.divide" }, { "name": "decimal.Context.divide_int", "domId": "decimal_decimal.Context.divide_int" }, { "name": "decimal.Context.divmod", "domId": "decimal_decimal.Context.divmod" }, { "name": "decimal.Context.Etiny", "domId": "decimal_decimal.Context.Etiny" }, { "name": "decimal.Context.Etop", "domId": "decimal_decimal.Context.Etop" }, { "name": "decimal.Context.exp", "domId": "decimal_decimal.Context.exp" }, { "name": "decimal.Context.fma", "domId": "decimal_decimal.Context.fma" }, { "name": "decimal.Context.is_canonical", "domId": "decimal_decimal.Context.is_canonical" }, { "name": "decimal.Context.is_finite", "domId": "decimal_decimal.Context.is_finite" }, { "name": "decimal.Context.is_infinite", "domId": "decimal_decimal.Context.is_infinite" }, { "name": "decimal.Context.is_nan", "domId": "decimal_decimal.Context.is_nan" }, { "name": "decimal.Context.is_normal", "domId": "decimal_decimal.Context.is_normal" }, { "name": "decimal.Context.is_qnan", "domId": "decimal_decimal.Context.is_qnan" }, { "name": "decimal.Context.is_signed", "domId": "decimal_decimal.Context.is_signed" }, { "name": "decimal.Context.is_snan", "domId": "decimal_decimal.Context.is_snan" }, { "name": "decimal.Context.is_subnormal", "domId": "decimal_decimal.Context.is_subnormal" }, { "name": "decimal.Context.is_zero", "domId": "decimal_decimal.Context.is_zero" }, { "name": "decimal.Context.ln", "domId": "decimal_decimal.Context.ln" }, { "name": "decimal.Context.log10", "domId": "decimal_decimal.Context.log10" }, { "name": "decimal.Context.logb", "domId": "decimal_decimal.Context.logb" }, { "name": "decimal.Context.logical_and", "domId": "decimal_decimal.Context.logical_and" }, { "name": "decimal.Context.logical_invert", "domId": "decimal_decimal.Context.logical_invert" }, { "name": "decimal.Context.logical_or", "domId": "decimal_decimal.Context.logical_or" }, { "name": "decimal.Context.logical_xor", "domId": "decimal_decimal.Context.logical_xor" }, { "name": "decimal.Context.max", "domId": "decimal_decimal.Context.max" }, { "name": "decimal.Context.max_mag", "domId": "decimal_decimal.Context.max_mag" }, { "name": "decimal.Context.min", "domId": "decimal_decimal.Context.min" }, { "name": "decimal.Context.min_mag", "domId": "decimal_decimal.Context.min_mag" }, { "name": "decimal.Context.minus", "domId": "decimal_decimal.Context.minus" }, { "name": "decimal.Context.multiply", "domId": "decimal_decimal.Context.multiply" }, { "name": "decimal.Context.next_minus", "domId": "decimal_decimal.Context.next_minus" }, { "name": "decimal.Context.next_plus", "domId": "decimal_decimal.Context.next_plus" }, { "name": "decimal.Context.next_toward", "domId": "decimal_decimal.Context.next_toward" }, { "name": "decimal.Context.normalize", "domId": "decimal_decimal.Context.normalize" }, { "name": "decimal.Context.number_class", "domId": "decimal_decimal.Context.number_class" }, { "name": "decimal.Context.plus", "domId": "decimal_decimal.Context.plus" }, { "name": "decimal.Context.power", "domId": "decimal_decimal.Context.power" }, { "name": "decimal.Context.quantize", "domId": "decimal_decimal.Context.quantize" }, { "name": "decimal.Context.radix", "domId": "decimal_decimal.Context.radix" }, { "name": "decimal.Context.remainder", "domId": "decimal_decimal.Context.remainder" }, { "name": "decimal.Context.remainder_near", "domId": "decimal_decimal.Context.remainder_near" }, { "name": "decimal.Context.rotate", "domId": "decimal_decimal.Context.rotate" }, { "name": "decimal.Context.same_quantum", "domId": "decimal_decimal.Context.same_quantum" }, { "name": "decimal.Context.scaleb", "domId": "decimal_decimal.Context.scaleb" }, { "name": "decimal.Context.shift", "domId": "decimal_decimal.Context.shift" }, { "name": "decimal.Context.sqrt", "domId": "decimal_decimal.Context.sqrt" }, { "name": "decimal.Context.subtract", "domId": "decimal_decimal.Context.subtract" }, { "name": "decimal.Context.to_eng_string", "domId": "decimal_decimal.Context.to_eng_string" }, { "name": "decimal.Context.to_integral_exact", "domId": "decimal_decimal.Context.to_integral_exact" }, { "name": "decimal.Context.to_sci_string", "domId": "decimal_decimal.Context.to_sci_string" }, { "name": "decimal.Decimal", "domId": "decimal_decimal.Decimal" }, { "name": "decimal.Decimal.adjusted", "domId": "decimal_decimal.Decimal.adjusted" }, { "name": "decimal.Decimal.as_tuple", "domId": "decimal_decimal.Decimal.as_tuple" }, { "name": "decimal.Decimal.canonical", "domId": "decimal_decimal.Decimal.canonical" }, { "name": "decimal.Decimal.compare", "domId": "decimal_decimal.Decimal.compare" }, { "name": "decimal.Decimal.compare_signal", "domId": "decimal_decimal.Decimal.compare_signal" }, { "name": "decimal.Decimal.compare_total", "domId": "decimal_decimal.Decimal.compare_total" }, { "name": "decimal.Decimal.compare_total_mag", "domId": "decimal_decimal.Decimal.compare_total_mag" }, { "name": "decimal.Decimal.conjugate", "domId": "decimal_decimal.Decimal.conjugate" }, { "name": "decimal.Decimal.copy_abs", "domId": "decimal_decimal.Decimal.copy_abs" }, { "name": "decimal.Decimal.copy_negate", "domId": "decimal_decimal.Decimal.copy_negate" }, { "name": "decimal.Decimal.copy_sign", "domId": "decimal_decimal.Decimal.copy_sign" }, { "name": "decimal.Decimal.exp", "domId": "decimal_decimal.Decimal.exp" }, { "name": "decimal.Decimal.fma", "domId": "decimal_decimal.Decimal.fma" }, { "name": "decimal.Decimal.from_float", "domId": "decimal_decimal.Decimal.from_float" }, { "name": "decimal.Decimal.is_canonical", "domId": "decimal_decimal.Decimal.is_canonical" }, { "name": "decimal.Decimal.is_finite", "domId": "decimal_decimal.Decimal.is_finite" }, { "name": "decimal.Decimal.is_infinite", "domId": "decimal_decimal.Decimal.is_infinite" }, { "name": "decimal.Decimal.is_nan", "domId": "decimal_decimal.Decimal.is_nan" }, { "name": "decimal.Decimal.is_normal", "domId": "decimal_decimal.Decimal.is_normal" }, { "name": "decimal.Decimal.is_qnan", "domId": "decimal_decimal.Decimal.is_qnan" }, { "name": "decimal.Decimal.is_signed", "domId": "decimal_decimal.Decimal.is_signed" }, { "name": "decimal.Decimal.is_snan", "domId": "decimal_decimal.Decimal.is_snan" }, { "name": "decimal.Decimal.is_subnormal", "domId": "decimal_decimal.Decimal.is_subnormal" }, { "name": "decimal.Decimal.is_zero", "domId": "decimal_decimal.Decimal.is_zero" }, { "name": "decimal.Decimal.ln", "domId": "decimal_decimal.Decimal.ln" }, { "name": "decimal.Decimal.log10", "domId": "decimal_decimal.Decimal.log10" }, { "name": "decimal.Decimal.logb", "domId": "decimal_decimal.Decimal.logb" }, { "name": "decimal.Decimal.logical_and", "domId": "decimal_decimal.Decimal.logical_and" }, { "name": "decimal.Decimal.logical_invert", "domId": "decimal_decimal.Decimal.logical_invert" }, { "name": "decimal.Decimal.logical_or", "domId": "decimal_decimal.Decimal.logical_or" }, { "name": "decimal.Decimal.logical_xor", "domId": "decimal_decimal.Decimal.logical_xor" }, { "name": "decimal.Decimal.max", "domId": "decimal_decimal.Decimal.max" }, { "name": "decimal.Decimal.max_mag", "domId": "decimal_decimal.Decimal.max_mag" }, { "name": "decimal.Decimal.min", "domId": "decimal_decimal.Decimal.min" }, { "name": "decimal.Decimal.min_mag", "domId": "decimal_decimal.Decimal.min_mag" }, { "name": "decimal.Decimal.next_minus", "domId": "decimal_decimal.Decimal.next_minus" }, { "name": "decimal.Decimal.next_plus", "domId": "decimal_decimal.Decimal.next_plus" }, { "name": "decimal.Decimal.next_toward", "domId": "decimal_decimal.Decimal.next_toward" }, { "name": "decimal.Decimal.normalize", "domId": "decimal_decimal.Decimal.normalize" }, { "name": "decimal.Decimal.number_class", "domId": "decimal_decimal.Decimal.number_class" }, { "name": "decimal.Decimal.quantize", "domId": "decimal_decimal.Decimal.quantize" }, { "name": "decimal.Decimal.radix", "domId": "decimal_decimal.Decimal.radix" }, { "name": "decimal.Decimal.remainder_near", "domId": "decimal_decimal.Decimal.remainder_near" }, { "name": "decimal.Decimal.rotate", "domId": "decimal_decimal.Decimal.rotate" }, { "name": "decimal.Decimal.same_quantum", "domId": "decimal_decimal.Decimal.same_quantum" }, { "name": "decimal.Decimal.scaleb", "domId": "decimal_decimal.Decimal.scaleb" }, { "name": "decimal.Decimal.shift", "domId": "decimal_decimal.Decimal.shift" }, { "name": "decimal.Decimal.sqrt", "domId": "decimal_decimal.Decimal.sqrt" }, { "name": "decimal.Decimal.to_eng_string", "domId": "decimal_decimal.Decimal.to_eng_string" }, { "name": "decimal.Decimal.to_integral", "domId": "decimal_decimal.Decimal.to_integral" }, { "name": "decimal.Decimal.to_integral_exact", "domId": "decimal_decimal.Decimal.to_integral_exact" }, { "name": "decimal.Decimal.to_integral_value", "domId": "decimal_decimal.Decimal.to_integral_value" }, { "name": "decimal.DecimalException", "domId": "decimal_decimal.DecimalException" }, { "name": "decimal.DefaultContext", "domId": "decimal_decimal.DefaultContext" }, { "name": "decimal.DivisionByZero", "domId": "decimal_decimal.DivisionByZero" }, { "name": "decimal.ExtendedContext", "domId": "decimal_decimal.ExtendedContext" }, { "name": "decimal.getcontext", "domId": "decimal_decimal.getcontext" }, { "name": "decimal.Inexact", "domId": "decimal_decimal.Inexact" }, { "name": "decimal.InvalidOperation", "domId": "decimal_decimal.InvalidOperation" }, { "name": "decimal.localcontext", "domId": "decimal_decimal.localcontext" }, { "name": "decimal.Overflow", "domId": "decimal_decimal.Overflow" }, { "name": "decimal.Rounded", "domId": "decimal_decimal.Rounded" }, { "name": "decimal.setcontext", "domId": "decimal_decimal.setcontext" }, { "name": "decimal.Subnormal", "domId": "decimal_decimal.Subnormal" }, { "name": "decimal.Underflow", "domId": "decimal_decimal.Underflow" } ] }, { "url": "http://docs.python.org/library/fileinput.html", "title": "fileinput", "html": "
\n

10.2. fileinput — Iterate over lines from multiple input streams

\n

Source code: Lib/fileinput.py

\n
\n

This module implements a helper class and functions to quickly write a\nloop over standard input or a list of files. If you just want to read or\nwrite one file see open().

\n

The typical use is:

\n
import fileinput\nfor line in fileinput.input():\n    process(line)\n
\n
\n

This iterates over the lines of all files listed in sys.argv[1:], defaulting\nto sys.stdin if the list is empty. If a filename is '-', it is also\nreplaced by sys.stdin. To specify an alternative list of filenames, pass it\nas the first argument to input(). A single file name is also allowed.

\n

All files are opened in text mode by default, but you can override this by\nspecifying the mode parameter in the call to input() or\nFileInput(). If an I/O error occurs during opening or reading a file,\nIOError is raised.

\n

If sys.stdin is used more than once, the second and further use will return\nno lines, except perhaps for interactive use, or if it has been explicitly reset\n(e.g. using sys.stdin.seek(0)).

\n

Empty files are opened and immediately closed; the only time their presence in\nthe list of filenames is noticeable at all is when the last file opened is\nempty.

\n

Lines are returned with any newlines intact, which means that the last line in\na file may not have one.

\n

You can control how files are opened by providing an opening hook via the\nopenhook parameter to fileinput.input() or FileInput(). The\nhook must be a function that takes two arguments, filename and mode, and\nreturns an accordingly opened file-like object. Two useful hooks are already\nprovided by this module.

\n

The following function is the primary interface of this module:

\n
\n
\nfileinput.input([files[, inplace[, backup[, mode[, openhook]]]]])
\n

Create an instance of the FileInput class. The instance will be used\nas global state for the functions of this module, and is also returned to use\nduring iteration. The parameters to this function will be passed along to the\nconstructor of the FileInput class.

\n

\nChanged in version 2.5: Added the mode and openhook parameters.

\n
\n\n

The following functions use the global state created by fileinput.input();\nif there is no active state, RuntimeError is raised.

\n
\n
\nfileinput.filename()
\n
Return the name of the file currently being read. Before the first line has\nbeen read, returns None.
\n\n
\n
\nfileinput.fileno()
\n

Return the integer “file descriptor” for the current file. When no file is\nopened (before the first line and between files), returns -1.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nfileinput.lineno()
\n
Return the cumulative line number of the line that has just been read. Before\nthe first line has been read, returns 0. After the last line of the last\nfile has been read, returns the line number of that line.
\n\n
\n
\nfileinput.filelineno()
\n
Return the line number in the current file. Before the first line has been\nread, returns 0. After the last line of the last file has been read,\nreturns the line number of that line within the file.
\n\n
\n
\nfileinput.isfirstline()
\n
Returns true if the line just read is the first line of its file, otherwise\nreturns false.
\n\n
\n
\nfileinput.isstdin()
\n
Returns true if the last line was read from sys.stdin, otherwise returns\nfalse.
\n\n
\n
\nfileinput.nextfile()
\n
Close the current file so that the next iteration will read the first line from\nthe next file (if any); lines not read from the file will not count towards the\ncumulative line count. The filename is not changed until after the first line\nof the next file has been read. Before the first line has been read, this\nfunction has no effect; it cannot be used to skip the first file. After the\nlast line of the last file has been read, this function has no effect.
\n\n
\n
\nfileinput.close()
\n
Close the sequence.
\n\n

The class which implements the sequence behavior provided by the module is\navailable for subclassing as well:

\n
\n
\nclass fileinput.FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
\n

Class FileInput is the implementation; its methods filename(),\nfileno(), lineno(), filelineno(), isfirstline(),\nisstdin(), nextfile() and close() correspond to the functions\nof the same name in the module. In addition it has a readline() method\nwhich returns the next input line, and a __getitem__() method which\nimplements the sequence behavior. The sequence must be accessed in strictly\nsequential order; random access and readline() cannot be mixed.

\n

With mode you can specify which file mode will be passed to open(). It\nmust be one of 'r', 'rU', 'U' and 'rb'.

\n

The openhook, when given, must be a function that takes two arguments,\nfilename and mode, and returns an accordingly opened file-like object. You\ncannot use inplace and openhook together.

\n

\nChanged in version 2.5: Added the mode and openhook parameters.

\n
\n\n

Optional in-place filtering: if the keyword argument inplace=1 is passed\nto fileinput.input() or to the FileInput constructor, the file is\nmoved to a backup file and standard output is directed to the input file (if a\nfile of the same name as the backup file already exists, it will be replaced\nsilently). This makes it possible to write a filter that rewrites its input\nfile in place. If the backup parameter is given (typically as\nbackup='.<some extension>'), it specifies the extension for the backup file,\nand the backup file remains around; by default, the extension is '.bak' and\nit is deleted when the output file is closed. In-place filtering is disabled\nwhen standard input is read.

\n
\n

Note

\n

The current implementation does not work for MS-DOS 8+3 filesystems.

\n
\n

The two following opening hooks are provided by this module:

\n
\n
\nfileinput.hook_compressed(filename, mode)
\n

Transparently opens files compressed with gzip and bzip2 (recognized by the\nextensions '.gz' and '.bz2') using the gzip and bz2\nmodules. If the filename extension is not '.gz' or '.bz2', the file is\nopened normally (ie, using open() without any decompression).

\n

Usage example: fi = fileinput.FileInput(openhook=fileinput.hook_compressed)

\n

\nNew in version 2.5.

\n
\n\n
\n
\nfileinput.hook_encoded(encoding)
\n

Returns a hook which opens each file with codecs.open(), using the given\nencoding to read the file.

\n

Usage example: fi =\nfileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))

\n
\n

Note

\n

With this hook, FileInput might return Unicode strings depending on the\nspecified encoding.

\n
\n

\nNew in version 2.5.

\n
\n\n
", "searchableItems": [ { "name": "fileinput.close", "domId": "fileinput_fileinput.close" }, { "name": "fileinput.FileInput", "domId": "fileinput_fileinput.FileInput" }, { "name": "fileinput.filelineno", "domId": "fileinput_fileinput.filelineno" }, { "name": "fileinput.filename", "domId": "fileinput_fileinput.filename" }, { "name": "fileinput.fileno", "domId": "fileinput_fileinput.fileno" }, { "name": "fileinput.hook_compressed", "domId": "fileinput_fileinput.hook_compressed" }, { "name": "fileinput.hook_encoded", "domId": "fileinput_fileinput.hook_encoded" }, { "name": "fileinput.input", "domId": "fileinput_fileinput.input" }, { "name": "fileinput.isfirstline", "domId": "fileinput_fileinput.isfirstline" }, { "name": "fileinput.isstdin", "domId": "fileinput_fileinput.isstdin" }, { "name": "fileinput.lineno", "domId": "fileinput_fileinput.lineno" }, { "name": "fileinput.nextfile", "domId": "fileinput_fileinput.nextfile" } ] }, { "url": "http://docs.python.org/library/random.html", "title": "random", "html": "
\n

9.6. random — Generate pseudo-random numbers

\n

Source code: Lib/random.py

\n
\n

This module implements pseudo-random number generators for various\ndistributions.

\n

For integers, uniform selection from a range. For sequences, uniform selection\nof a random element, a function to generate a random permutation of a list\nin-place, and a function for random sampling without replacement.

\n

On the real line, there are functions to compute uniform, normal (Gaussian),\nlognormal, negative exponential, gamma, and beta distributions. For generating\ndistributions of angles, the von Mises distribution is available.

\n

Almost all module functions depend on the basic function random(), which\ngenerates a random float uniformly in the semi-open range [0.0, 1.0). Python\nuses the Mersenne Twister as the core generator. It produces 53-bit precision\nfloats and has a period of 2**19937-1. The underlying implementation in C is\nboth fast and threadsafe. The Mersenne Twister is one of the most extensively\ntested random number generators in existence. However, being completely\ndeterministic, it is not suitable for all purposes, and is completely unsuitable\nfor cryptographic purposes.

\n

The functions supplied by this module are actually bound methods of a hidden\ninstance of the random.Random class. You can instantiate your own\ninstances of Random to get generators that don’t share state. This is\nespecially useful for multi-threaded programs, creating a different instance of\nRandom for each thread, and using the jumpahead() method to make\nit likely that the generated sequences seen by each thread don’t overlap.

\n

Class Random can also be subclassed if you want to use a different\nbasic generator of your own devising: in that case, override the random(),\nseed(), getstate(), setstate() and jumpahead() methods.\nOptionally, a new generator can supply a getrandbits() method — this\nallows randrange() to produce selections over an arbitrarily large range.

\n

\nNew in version 2.4: the getrandbits() method.

\n

As an example of subclassing, the random module provides the\nWichmannHill class that implements an alternative generator in pure\nPython. The class provides a backward compatible way to reproduce results from\nearlier versions of Python, which used the Wichmann-Hill algorithm as the core\ngenerator. Note that this Wichmann-Hill generator can no longer be recommended:\nits period is too short by contemporary standards, and the sequence generated is\nknown to fail some stringent randomness tests. See the references below for a\nrecent variant that repairs these flaws.

\n

\nChanged in version 2.3: MersenneTwister replaced Wichmann-Hill as the default generator.

\n

The random module also provides the SystemRandom class which\nuses the system function os.urandom() to generate random numbers\nfrom sources provided by the operating system.

\n

Bookkeeping functions:

\n
\n
\nrandom.seed([x])
\n

Initialize the basic random number generator. Optional argument x can be any\nhashable object. If x is omitted or None, current system time is used;\ncurrent system time is also used to initialize the generator when the module is\nfirst imported. If randomness sources are provided by the operating system,\nthey are used instead of the system time (see the os.urandom() function\nfor details on availability).

\n

\nChanged in version 2.4: formerly, operating system resources were not used.

\n
\n\n
\n
\nrandom.getstate()
\n

Return an object capturing the current internal state of the generator. This\nobject can be passed to setstate() to restore the state.

\n

\nNew in version 2.1.

\n

\nChanged in version 2.6: State values produced in Python 2.6 cannot be loaded into earlier versions.

\n
\n\n
\n
\nrandom.setstate(state)
\n

state should have been obtained from a previous call to getstate(), and\nsetstate() restores the internal state of the generator to what it was at\nthe time setstate() was called.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nrandom.jumpahead(n)
\n

Change the internal state to one different from and likely far away from the\ncurrent state. n is a non-negative integer which is used to scramble the\ncurrent state vector. This is most useful in multi-threaded programs, in\nconjunction with multiple instances of the Random class:\nsetstate() or seed() can be used to force all instances into the\nsame internal state, and then jumpahead() can be used to force the\ninstances’ states far apart.

\n

\nNew in version 2.1.

\n

\nChanged in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n)\njumps to another state likely to be separated by many steps.

\n
\n\n
\n
\nrandom.getrandbits(k)
\n

Returns a python long int with k random bits. This method is supplied\nwith the MersenneTwister generator and some other generators may also provide it\nas an optional part of the API. When available, getrandbits() enables\nrandrange() to handle arbitrarily large ranges.

\n

\nNew in version 2.4.

\n
\n\n

Functions for integers:

\n
\n
\nrandom.randrange([start], stop[, step])
\n

Return a randomly selected element from range(start, stop, step). This is\nequivalent to choice(range(start, stop, step)), but doesn’t actually build a\nrange object.

\n

\nNew in version 1.5.2.

\n
\n\n
\n
\nrandom.randint(a, b)
\n
Return a random integer N such that a <= N <= b.
\n\n

Functions for sequences:

\n
\n
\nrandom.choice(seq)
\n
Return a random element from the non-empty sequence seq. If seq is empty,\nraises IndexError.
\n\n
\n
\nrandom.shuffle(x[, random])
\n

Shuffle the sequence x in place. The optional argument random is a\n0-argument function returning a random float in [0.0, 1.0); by default, this is\nthe function random().

\n

Note that for even rather small len(x), the total number of permutations of\nx is larger than the period of most random number generators; this implies\nthat most permutations of a long sequence can never be generated.

\n
\n\n
\n
\nrandom.sample(population, k)
\n

Return a k length list of unique elements chosen from the population sequence.\nUsed for random sampling without replacement.

\n

\nNew in version 2.3.

\n

Returns a new list containing elements from the population while leaving the\noriginal population unchanged. The resulting list is in selection order so that\nall sub-slices will also be valid random samples. This allows raffle winners\n(the sample) to be partitioned into grand prize and second place winners (the\nsubslices).

\n

Members of the population need not be hashable or unique. If the population\ncontains repeats, then each occurrence is a possible selection in the sample.

\n

To choose a sample from a range of integers, use an xrange() object as an\nargument. This is especially fast and space efficient for sampling from a large\npopulation: sample(xrange(10000000), 60).

\n
\n\n

The following functions generate specific real-valued distributions. Function\nparameters are named after the corresponding variables in the distribution’s\nequation, as used in common mathematical practice; most of these equations can\nbe found in any statistics text.

\n
\n
\nrandom.random()
\n
Return the next random floating point number in the range [0.0, 1.0).
\n\n
\n
\nrandom.uniform(a, b)
\n

Return a random floating point number N such that a <= N <= b for\na <= b and b <= N <= a for b < a.

\n

The end-point value b may or may not be included in the range\ndepending on floating-point rounding in the equation a + (b-a) * random().

\n
\n\n
\n
\nrandom.triangular(low, high, mode)
\n

Return a random floating point number N such that low <= N <= high and\nwith the specified mode between those bounds. The low and high bounds\ndefault to zero and one. The mode argument defaults to the midpoint\nbetween the bounds, giving a symmetric distribution.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nrandom.betavariate(alpha, beta)
\n
Beta distribution. Conditions on the parameters are alpha > 0 and\nbeta > 0. Returned values range between 0 and 1.
\n\n
\n
\nrandom.expovariate(lambd)
\n
Exponential distribution. lambd is 1.0 divided by the desired\nmean. It should be nonzero. (The parameter would be called\n“lambda”, but that is a reserved word in Python.) Returned values\nrange from 0 to positive infinity if lambd is positive, and from\nnegative infinity to 0 if lambd is negative.
\n\n
\n
\nrandom.gammavariate(alpha, beta)
\n

Gamma distribution. (Not the gamma function!) Conditions on the\nparameters are alpha > 0 and beta > 0.

\n

The probability distribution function is:

\n
          x ** (alpha - 1) * math.exp(-x / beta)\npdf(x) =  --------------------------------------\n            math.gamma(alpha) * beta ** alpha
\n
\n
\n\n
\n
\nrandom.gauss(mu, sigma)
\n
Gaussian distribution. mu is the mean, and sigma is the standard\ndeviation. This is slightly faster than the normalvariate() function\ndefined below.
\n\n
\n
\nrandom.lognormvariate(mu, sigma)
\n
Log normal distribution. If you take the natural logarithm of this\ndistribution, you’ll get a normal distribution with mean mu and standard\ndeviation sigma. mu can have any value, and sigma must be greater than\nzero.
\n\n
\n
\nrandom.normalvariate(mu, sigma)
\n
Normal distribution. mu is the mean, and sigma is the standard deviation.
\n\n
\n
\nrandom.vonmisesvariate(mu, kappa)
\n
mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa\nis the concentration parameter, which must be greater than or equal to zero. If\nkappa is equal to zero, this distribution reduces to a uniform random angle\nover the range 0 to 2*pi.
\n\n
\n
\nrandom.paretovariate(alpha)
\n
Pareto distribution. alpha is the shape parameter.
\n\n
\n
\nrandom.weibullvariate(alpha, beta)
\n
Weibull distribution. alpha is the scale parameter and beta is the shape\nparameter.
\n\n

Alternative Generators:

\n
\n
\nclass random.WichmannHill([seed])
\n
Class that implements the Wichmann-Hill algorithm as the core generator. Has all\nof the same methods as Random plus the whseed() method described\nbelow. Because this class is implemented in pure Python, it is not threadsafe\nand may require locks between calls. The period of the generator is\n6,953,607,871,644 which is small enough to require care that two independent\nrandom sequences do not overlap.
\n\n
\n
\nrandom.whseed([x])
\n
This is obsolete, supplied for bit-level compatibility with versions of Python\nprior to 2.1. See seed() for details. whseed() does not guarantee\nthat distinct integer arguments yield distinct internal states, and can yield no\nmore than about 2**24 distinct internal states in all.
\n\n
\n
\nclass random.SystemRandom([seed])
\n

Class that uses the os.urandom() function for generating random numbers\nfrom sources provided by the operating system. Not available on all systems.\nDoes not rely on software state and sequences are not reproducible. Accordingly,\nthe seed() and jumpahead() methods have no effect and are ignored.\nThe getstate() and setstate() methods raise\nNotImplementedError if called.

\n

\nNew in version 2.4.

\n
\n\n

Examples of basic usage:

\n
>>> random.random()        # Random float x, 0.0 <= x < 1.0\n0.37444887175646646\n>>> random.uniform(1, 10)  # Random float x, 1.0 <= x < 10.0\n1.1800146073117523\n>>> random.randint(1, 10)  # Integer from 1 to 10, endpoints included\n7\n>>> random.randrange(0, 101, 2)  # Even integer from 0 to 100\n26\n>>> random.choice('abcdefghij')  # Choose a random element\n'c'\n\n>>> items = [1, 2, 3, 4, 5, 6, 7]\n>>> random.shuffle(items)\n>>> items\n[7, 3, 2, 5, 6, 4, 1]\n\n>>> random.sample([1, 2, 3, 4, 5],  3)  # Choose 3 elements\n[4, 1, 5]\n
\n
\n
\n

See also

\n

M. Matsumoto and T. Nishimura, “Mersenne Twister: A 623-dimensionally\nequidistributed uniform pseudorandom number generator”, ACM Transactions on\nModeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 1998.

\n

Wichmann, B. A. & Hill, I. D., “Algorithm AS 183: An efficient and portable\npseudo-random number generator”, Applied Statistics 31 (1982) 188-190.

\n

Complementary-Multiply-with-Carry recipe for a compatible alternative\nrandom number generator with a long period and comparatively simple update\noperations.

\n
\n
", "searchableItems": [ { "name": "random.betavariate", "domId": "random_random.betavariate" }, { "name": "random.choice", "domId": "random_random.choice" }, { "name": "random.expovariate", "domId": "random_random.expovariate" }, { "name": "random.gammavariate", "domId": "random_random.gammavariate" }, { "name": "random.gauss", "domId": "random_random.gauss" }, { "name": "random.getrandbits", "domId": "random_random.getrandbits" }, { "name": "random.getstate", "domId": "random_random.getstate" }, { "name": "random.jumpahead", "domId": "random_random.jumpahead" }, { "name": "random.lognormvariate", "domId": "random_random.lognormvariate" }, { "name": "random.normalvariate", "domId": "random_random.normalvariate" }, { "name": "random.paretovariate", "domId": "random_random.paretovariate" }, { "name": "random.randint", "domId": "random_random.randint" }, { "name": "random.random", "domId": "random_random.random" }, { "name": "random.randrange", "domId": "random_random.randrange" }, { "name": "random.sample", "domId": "random_random.sample" }, { "name": "random.seed", "domId": "random_random.seed" }, { "name": "random.setstate", "domId": "random_random.setstate" }, { "name": "random.shuffle", "domId": "random_random.shuffle" }, { "name": "random.SystemRandom", "domId": "random_random.SystemRandom" }, { "name": "random.triangular", "domId": "random_random.triangular" }, { "name": "random.uniform", "domId": "random_random.uniform" }, { "name": "random.vonmisesvariate", "domId": "random_random.vonmisesvariate" }, { "name": "random.weibullvariate", "domId": "random_random.weibullvariate" }, { "name": "random.whseed", "domId": "random_random.whseed" }, { "name": "random.WichmannHill", "domId": "random_random.WichmannHill" } ] }, { "url": "http://docs.python.org/library/statvfs.html", "title": "statvfs", "html": "
\n

10.4. statvfs — Constants used with os.statvfs()

\n

\nDeprecated since version 2.6: The statvfs module has been deprecated for removal in Python 3.0.

\n

The statvfs module defines constants so interpreting the result if\nos.statvfs(), which returns a tuple, can be made without remembering\n“magic numbers.” Each of the constants defined in this module is the index of\nthe entry in the tuple returned by os.statvfs() that contains the\nspecified information.

\n
\n
\nstatvfs.F_BSIZE
\n
Preferred file system block size.
\n\n
\n
\nstatvfs.F_FRSIZE
\n
Fundamental file system block size.
\n\n
\n
\nstatvfs.F_BLOCKS
\n
Total number of blocks in the filesystem.
\n\n
\n
\nstatvfs.F_BFREE
\n
Total number of free blocks.
\n\n
\n
\nstatvfs.F_BAVAIL
\n
Free blocks available to non-super user.
\n\n
\n
\nstatvfs.F_FILES
\n
Total number of file nodes.
\n\n
\n
\nstatvfs.F_FFREE
\n
Total number of free file nodes.
\n\n
\n
\nstatvfs.F_FAVAIL
\n
Free nodes available to non-super user.
\n\n
\n
\nstatvfs.F_FLAG
\n
Flags. System dependent: see statvfs() man page.
\n\n
\n
\nstatvfs.F_NAMEMAX
\n
Maximum file name length.
\n\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/operator.html", "title": "operator", "html": "
\n

9.9. operator — Standard operators as functions

\n

The operator module exports a set of efficient functions corresponding to\nthe intrinsic operators of Python. For example, operator.add(x, y) is\nequivalent to the expression x+y. The function names are those used for\nspecial class methods; variants without leading and trailing __ are also\nprovided for convenience.

\n

The functions fall into categories that perform object comparisons, logical\noperations, mathematical operations, sequence operations, and abstract type\ntests.

\n

The object comparison functions are useful for all objects, and are named after\nthe rich comparison operators they support:

\n
\n
\noperator.lt(a, b)
\n
\noperator.le(a, b)
\n
\noperator.eq(a, b)
\n
\noperator.ne(a, b)
\n
\noperator.ge(a, b)
\n
\noperator.gt(a, b)
\n
\noperator.__lt__(a, b)
\n
\noperator.__le__(a, b)
\n
\noperator.__eq__(a, b)
\n
\noperator.__ne__(a, b)
\n
\noperator.__ge__(a, b)
\n
\noperator.__gt__(a, b)
\n

Perform “rich comparisons” between a and b. Specifically, lt(a, b) is\nequivalent to a < b, le(a, b) is equivalent to a <= b, eq(a,\nb) is equivalent to a == b, ne(a, b) is equivalent to a != b,\ngt(a, b) is equivalent to a > b and ge(a, b) is equivalent to a\n>= b. Note that unlike the built-in cmp(), these functions can\nreturn any value, which may or may not be interpretable as a Boolean value.\nSee Comparisons for more information about rich comparisons.

\n

\nNew in version 2.2.

\n
\n\n

The logical operations are also generally applicable to all objects, and support\ntruth tests, identity tests, and boolean operations:

\n
\n
\noperator.not_(obj)
\n
\noperator.__not__(obj)
\n
Return the outcome of not obj. (Note that there is no\n__not__() method for object instances; only the interpreter core defines\nthis operation. The result is affected by the __nonzero__() and\n__len__() methods.)
\n\n
\n
\noperator.truth(obj)
\n
Return True if obj is true, and False otherwise. This is\nequivalent to using the bool constructor.
\n\n
\n
\noperator.is_(a, b)
\n

Return a is b. Tests object identity.

\n

\nNew in version 2.3.

\n
\n\n
\n
\noperator.is_not(a, b)
\n

Return a is not b. Tests object identity.

\n

\nNew in version 2.3.

\n
\n\n

The mathematical and bitwise operations are the most numerous:

\n
\n
\noperator.abs(obj)
\n
\noperator.__abs__(obj)
\n
Return the absolute value of obj.
\n\n
\n
\noperator.add(a, b)
\n
\noperator.__add__(a, b)
\n
Return a + b, for a and b numbers.
\n\n
\n
\noperator.and_(a, b)
\n
\noperator.__and__(a, b)
\n
Return the bitwise and of a and b.
\n\n
\n
\noperator.div(a, b)
\n
\noperator.__div__(a, b)
\n
Return a / b when __future__.division is not in effect. This is\nalso known as “classic” division.
\n\n
\n
\noperator.floordiv(a, b)
\n
\noperator.__floordiv__(a, b)
\n

Return a // b.

\n

\nNew in version 2.2.

\n
\n\n
\n
\noperator.index(a)
\n
\noperator.__index__(a)
\n

Return a converted to an integer. Equivalent to a.__index__().

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.inv(obj)
\n
\noperator.invert(obj)
\n
\noperator.__inv__(obj)
\n
\noperator.__invert__(obj)
\n

Return the bitwise inverse of the number obj. This is equivalent to ~obj.

\n

\nNew in version 2.0: The names invert() and __invert__().

\n
\n\n
\n
\noperator.lshift(a, b)
\n
\noperator.__lshift__(a, b)
\n
Return a shifted left by b.
\n\n
\n
\noperator.mod(a, b)
\n
\noperator.__mod__(a, b)
\n
Return a b.
\n\n
\n
\noperator.mul(a, b)
\n
\noperator.__mul__(a, b)
\n
Return a * b, for a and b numbers.
\n\n
\n
\noperator.neg(obj)
\n
\noperator.__neg__(obj)
\n
Return obj negated (-obj).
\n\n
\n
\noperator.or_(a, b)
\n
\noperator.__or__(a, b)
\n
Return the bitwise or of a and b.
\n\n
\n
\noperator.pos(obj)
\n
\noperator.__pos__(obj)
\n
Return obj positive (+obj).
\n\n
\n
\noperator.pow(a, b)
\n
\noperator.__pow__(a, b)
\n

Return a ** b, for a and b numbers.

\n

\nNew in version 2.3.

\n
\n\n
\n
\noperator.rshift(a, b)
\n
\noperator.__rshift__(a, b)
\n
Return a shifted right by b.
\n\n
\n
\noperator.sub(a, b)
\n
\noperator.__sub__(a, b)
\n
Return a - b.
\n\n
\n
\noperator.truediv(a, b)
\n
\noperator.__truediv__(a, b)
\n

Return a / b when __future__.division is in effect. This is also\nknown as “true” division.

\n

\nNew in version 2.2.

\n
\n\n
\n
\noperator.xor(a, b)
\n
\noperator.__xor__(a, b)
\n
Return the bitwise exclusive or of a and b.
\n\n

Operations which work with sequences (some of them with mappings too) include:

\n
\n
\noperator.concat(a, b)
\n
\noperator.__concat__(a, b)
\n
Return a + b for a and b sequences.
\n\n
\n
\noperator.contains(a, b)
\n
\noperator.__contains__(a, b)
\n

Return the outcome of the test b in a. Note the reversed operands.

\n

\nNew in version 2.0: The name __contains__().

\n
\n\n
\n
\noperator.countOf(a, b)
\n
Return the number of occurrences of b in a.
\n\n
\n
\noperator.delitem(a, b)
\n
\noperator.__delitem__(a, b)
\n
Remove the value of a at index b.
\n\n
\n
\noperator.delslice(a, b, c)
\n
\noperator.__delslice__(a, b, c)
\n

Delete the slice of a from index b to index c-1.

\n

\nDeprecated since version 2.6: This function is removed in Python 3.x. Use delitem() with a slice\nindex.

\n
\n\n
\n
\noperator.getitem(a, b)
\n
\noperator.__getitem__(a, b)
\n
Return the value of a at index b.
\n\n
\n
\noperator.getslice(a, b, c)
\n
\noperator.__getslice__(a, b, c)
\n

Return the slice of a from index b to index c-1.

\n

\nDeprecated since version 2.6: This function is removed in Python 3.x. Use getitem() with a slice\nindex.

\n
\n\n
\n
\noperator.indexOf(a, b)
\n
Return the index of the first of occurrence of b in a.
\n\n
\n
\noperator.repeat(a, b)
\n
\noperator.__repeat__(a, b)
\n

\nDeprecated since version 2.7: Use __mul__() instead.

\n

Return a * b where a is a sequence and b is an integer.

\n
\n\n
\n
\noperator.sequenceIncludes(...)
\n

\nDeprecated since version 2.0: Use contains() instead.

\n

Alias for contains().

\n
\n\n
\n
\noperator.setitem(a, b, c)
\n
\noperator.__setitem__(a, b, c)
\n
Set the value of a at index b to c.
\n\n
\n
\noperator.setslice(a, b, c, v)
\n
\noperator.__setslice__(a, b, c, v)
\n

Set the slice of a from index b to index c-1 to the sequence v.

\n

\nDeprecated since version 2.6: This function is removed in Python 3.x. Use setitem() with a slice\nindex.

\n
\n\n

Example use of operator functions:

\n
>>> # Elementwise multiplication\n>>> map(mul, [0, 1, 2, 3], [10, 20, 30, 40])\n[0, 20, 60, 120]\n\n>>> # Dot product\n>>> sum(map(mul, [0, 1, 2, 3], [10, 20, 30, 40]))\n200\n
\n
\n

Many operations have an “in-place” version. The following functions provide a\nmore primitive access to in-place operators than the usual syntax does; for\nexample, the statement x += y is equivalent to\nx = operator.iadd(x, y). Another way to put it is to say that\nz = operator.iadd(x, y) is equivalent to the compound statement\nz = x; z += y.

\n
\n
\noperator.iadd(a, b)
\n
\noperator.__iadd__(a, b)
\n

a = iadd(a, b) is equivalent to a += b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.iand(a, b)
\n
\noperator.__iand__(a, b)
\n

a = iand(a, b) is equivalent to a &= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.iconcat(a, b)
\n
\noperator.__iconcat__(a, b)
\n

a = iconcat(a, b) is equivalent to a += b for a and b sequences.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.idiv(a, b)
\n
\noperator.__idiv__(a, b)
\n

a = idiv(a, b) is equivalent to a /= b when __future__.division is\nnot in effect.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.ifloordiv(a, b)
\n
\noperator.__ifloordiv__(a, b)
\n

a = ifloordiv(a, b) is equivalent to a //= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.ilshift(a, b)
\n
\noperator.__ilshift__(a, b)
\n

a = ilshift(a, b) is equivalent to a <<= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.imod(a, b)
\n
\noperator.__imod__(a, b)
\n

a = imod(a, b) is equivalent to a %= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.imul(a, b)
\n
\noperator.__imul__(a, b)
\n

a = imul(a, b) is equivalent to a *= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.ior(a, b)
\n
\noperator.__ior__(a, b)
\n

a = ior(a, b) is equivalent to a |= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.ipow(a, b)
\n
\noperator.__ipow__(a, b)
\n

a = ipow(a, b) is equivalent to a **= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.irepeat(a, b)
\n
\noperator.__irepeat__(a, b)
\n

\nDeprecated since version 2.7: Use __imul__() instead.

\n

a = irepeat(a, b) is equivalent to a *= b where a is a sequence and\nb is an integer.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.irshift(a, b)
\n
\noperator.__irshift__(a, b)
\n

a = irshift(a, b) is equivalent to a >>= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.isub(a, b)
\n
\noperator.__isub__(a, b)
\n

a = isub(a, b) is equivalent to a -= b.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.itruediv(a, b)
\n
\noperator.__itruediv__(a, b)
\n

a = itruediv(a, b) is equivalent to a /= b when __future__.division\nis in effect.

\n

\nNew in version 2.5.

\n
\n\n
\n
\noperator.ixor(a, b)
\n
\noperator.__ixor__(a, b)
\n

a = ixor(a, b) is equivalent to a ^= b.

\n

\nNew in version 2.5.

\n
\n\n

The operator module also defines a few predicates to test the type of\nobjects; however, these are not all reliable. It is preferable to test\nabstract base classes instead (see collections and\nnumbers for details).

\n
\n
\noperator.isCallable(obj)
\n

\nDeprecated since version 2.0: Use isinstance(x, collections.Callable) instead.

\n

Returns true if the object obj can be called like a function, otherwise it\nreturns false. True is returned for functions, bound and unbound methods, class\nobjects, and instance objects which support the __call__() method.

\n
\n\n
\n
\noperator.isMappingType(obj)
\n

\nDeprecated since version 2.7: Use isinstance(x, collections.Mapping) instead.

\n

Returns true if the object obj supports the mapping interface. This is true for\ndictionaries and all instance objects defining __getitem__().

\n
\n\n
\n
\noperator.isNumberType(obj)
\n

\nDeprecated since version 2.7: Use isinstance(x, numbers.Number) instead.

\n

Returns true if the object obj represents a number. This is true for all\nnumeric types implemented in C.

\n
\n\n
\n
\noperator.isSequenceType(obj)
\n

\nDeprecated since version 2.7: Use isinstance(x, collections.Sequence) instead.

\n

Returns true if the object obj supports the sequence protocol. This returns true\nfor all objects which define sequence methods in C, and for all instance objects\ndefining __getitem__().

\n
\n\n

The operator module also defines tools for generalized attribute and item\nlookups. These are useful for making fast field extractors as arguments for\nmap(), sorted(), itertools.groupby(), or other functions that\nexpect a function argument.

\n
\n
\noperator.attrgetter(attr[, args...])
\n

Return a callable object that fetches attr from its operand. If more than one\nattribute is requested, returns a tuple of attributes. After,\nf = attrgetter('name'), the call f(b) returns b.name. After,\nf = attrgetter('name', 'date'), the call f(b) returns (b.name,\nb.date). Equivalent to:

\n
def attrgetter(*items):\n    if len(items) == 1:\n        attr = items[0]\n        def g(obj):\n            return resolve_attr(obj, attr)\n    else:\n        def g(obj):\n            return tuple(resolve_att(obj, attr) for attr in items)\n    return g\n\ndef resolve_attr(obj, attr):\n    for name in attr.split("."):\n        obj = getattr(obj, name)\n    return obj\n
\n
\n

The attribute names can also contain dots; after f = attrgetter('date.month'),\nthe call f(b) returns b.date.month.

\n

\nNew in version 2.4.

\n

\nChanged in version 2.5: Added support for multiple attributes.

\n

\nChanged in version 2.6: Added support for dotted attributes.

\n
\n\n
\n
\noperator.itemgetter(item[, args...])
\n

Return a callable object that fetches item from its operand using the\noperand’s __getitem__() method. If multiple items are specified,\nreturns a tuple of lookup values. Equivalent to:

\n
def itemgetter(*items):\n    if len(items) == 1:\n        item = items[0]\n        def g(obj):\n            return obj[item]\n    else:\n        def g(obj):\n            return tuple(obj[item] for item in items)\n    return g\n
\n
\n

The items can be any type accepted by the operand’s __getitem__()\nmethod. Dictionaries accept any hashable value. Lists, tuples, and\nstrings accept an index or a slice:

\n
>>> itemgetter(1)('ABCDEFG')\n'B'\n>>> itemgetter(1,3,5)('ABCDEFG')\n('B', 'D', 'F')\n>>> itemgetter(slice(2,None))('ABCDEFG')\n'CDEFG'\n
\n
\n

\nNew in version 2.4.

\n

\nChanged in version 2.5: Added support for multiple item extraction.

\n

Example of using itemgetter() to retrieve specific fields from a\ntuple record:

\n
>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]\n>>> getcount = itemgetter(1)\n>>> map(getcount, inventory)\n[3, 2, 5, 1]\n>>> sorted(inventory, key=getcount)\n[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]\n
\n
\n
\n\n
\n
\noperator.methodcaller(name[, args...])
\n

Return a callable object that calls the method name on its operand. If\nadditional arguments and/or keyword arguments are given, they will be given\nto the method as well. After f = methodcaller('name'), the call f(b)\nreturns b.name(). After f = methodcaller('name', 'foo', bar=1), the\ncall f(b) returns b.name('foo', bar=1). Equivalent to:

\n
def methodcaller(name, *args, **kwargs):\n    def caller(obj):\n        return getattr(obj, name)(*args, **kwargs)\n    return caller\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n

9.9.1. Mapping Operators to Functions

\n

This table shows how abstract operations correspond to operator symbols in the\nPython syntax and the functions in the operator module.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OperationSyntaxFunction
Additiona + badd(a, b)
Concatenationseq1 + seq2concat(seq1, seq2)
Containment Testobj in seqcontains(seq, obj)
Divisiona / bdiv(a, b) (without\n__future__.division)
Divisiona / btruediv(a, b) (with\n__future__.division)
Divisiona // bfloordiv(a, b)
Bitwise Anda & band_(a, b)
Bitwise Exclusive Ora ^ bxor(a, b)
Bitwise Inversion~ ainvert(a)
Bitwise Ora | bor_(a, b)
Exponentiationa ** bpow(a, b)
Identitya is bis_(a, b)
Identitya is not bis_not(a, b)
Indexed Assignmentobj[k] = vsetitem(obj, k, v)
Indexed Deletiondel obj[k]delitem(obj, k)
Indexingobj[k]getitem(obj, k)
Left Shifta << blshift(a, b)
Moduloa bmod(a, b)
Multiplicationa * bmul(a, b)
Negation (Arithmetic)- aneg(a)
Negation (Logical)not anot_(a)
Positive+ apos(a)
Right Shifta >> brshift(a, b)
Sequence Repetitionseq * irepeat(seq, i)
Slice Assignmentseq[i:j] = valuessetitem(seq, slice(i, j), values)
Slice Deletiondel seq[i:j]delitem(seq, slice(i, j))
Slicingseq[i:j]getitem(seq, slice(i, j))
String Formattings objmod(s, obj)
Subtractiona - bsub(a, b)
Truth Testobjtruth(obj)
Orderinga < blt(a, b)
Orderinga <= ble(a, b)
Equalitya == beq(a, b)
Differencea != bne(a, b)
Orderinga >= bge(a, b)
Orderinga > bgt(a, b)
\n
\n
", "searchableItems": [ { "name": "operator.abs", "domId": "operator_operator.abs" }, { "name": "operator.add", "domId": "operator_operator.add" }, { "name": "operator.and_", "domId": "operator_operator.and_" }, { "name": "operator.attrgetter", "domId": "operator_operator.attrgetter" }, { "name": "operator.concat", "domId": "operator_operator.concat" }, { "name": "operator.contains", "domId": "operator_operator.contains" }, { "name": "operator.countOf", "domId": "operator_operator.countOf" }, { "name": "operator.delitem", "domId": "operator_operator.delitem" }, { "name": "operator.delslice", "domId": "operator_operator.delslice" }, { "name": "operator.div", "domId": "operator_operator.div" }, { "name": "operator.floordiv", "domId": "operator_operator.floordiv" }, { "name": "operator.getitem", "domId": "operator_operator.getitem" }, { "name": "operator.getslice", "domId": "operator_operator.getslice" }, { "name": "operator.iadd", "domId": "operator_operator.iadd" }, { "name": "operator.iand", "domId": "operator_operator.iand" }, { "name": "operator.iconcat", "domId": "operator_operator.iconcat" }, { "name": "operator.idiv", "domId": "operator_operator.idiv" }, { "name": "operator.ifloordiv", "domId": "operator_operator.ifloordiv" }, { "name": "operator.ilshift", "domId": "operator_operator.ilshift" }, { "name": "operator.imod", "domId": "operator_operator.imod" }, { "name": "operator.imul", "domId": "operator_operator.imul" }, { "name": "operator.index", "domId": "operator_operator.index" }, { "name": "operator.indexOf", "domId": "operator_operator.indexOf" }, { "name": "operator.inv", "domId": "operator_operator.inv" }, { "name": "operator.ior", "domId": "operator_operator.ior" }, { "name": "operator.ipow", "domId": "operator_operator.ipow" }, { "name": "operator.irepeat", "domId": "operator_operator.irepeat" }, { "name": "operator.irshift", "domId": "operator_operator.irshift" }, { "name": "operator.is_", "domId": "operator_operator.is_" }, { "name": "operator.is_not", "domId": "operator_operator.is_not" }, { "name": "operator.isCallable", "domId": "operator_operator.isCallable" }, { "name": "operator.isMappingType", "domId": "operator_operator.isMappingType" }, { "name": "operator.isNumberType", "domId": "operator_operator.isNumberType" }, { "name": "operator.isSequenceType", "domId": "operator_operator.isSequenceType" }, { "name": "operator.isub", "domId": "operator_operator.isub" }, { "name": "operator.itemgetter", "domId": "operator_operator.itemgetter" }, { "name": "operator.itruediv", "domId": "operator_operator.itruediv" }, { "name": "operator.ixor", "domId": "operator_operator.ixor" }, { "name": "operator.lshift", "domId": "operator_operator.lshift" }, { "name": "operator.lt", "domId": "operator_operator.lt" }, { "name": "operator.methodcaller", "domId": "operator_operator.methodcaller" }, { "name": "operator.mod", "domId": "operator_operator.mod" }, { "name": "operator.mul", "domId": "operator_operator.mul" }, { "name": "operator.neg", "domId": "operator_operator.neg" }, { "name": "operator.not_", "domId": "operator_operator.not_" }, { "name": "operator.or_", "domId": "operator_operator.or_" }, { "name": "operator.pos", "domId": "operator_operator.pos" }, { "name": "operator.pow", "domId": "operator_operator.pow" }, { "name": "operator.repeat", "domId": "operator_operator.repeat" }, { "name": "operator.rshift", "domId": "operator_operator.rshift" }, { "name": "operator.sequenceIncludes", "domId": "operator_operator.sequenceIncludes" }, { "name": "operator.setitem", "domId": "operator_operator.setitem" }, { "name": "operator.setslice", "domId": "operator_operator.setslice" }, { "name": "operator.sub", "domId": "operator_operator.sub" }, { "name": "operator.truediv", "domId": "operator_operator.truediv" }, { "name": "operator.truth", "domId": "operator_operator.truth" }, { "name": "operator.xor", "domId": "operator_operator.xor" } ] }, { "url": "http://docs.python.org/library/os.path.html", "title": "os.path", "html": "
\n

10.1. os.path — Common pathname manipulations

\n

This module implements some useful functions on pathnames. To read or\nwrite files see open(), and for accessing the filesystem see the\nos module.

\n
\n

Note

\n

On Windows, many of these functions do not properly support UNC pathnames.\nsplitunc() and ismount() do handle them correctly.

\n
\n
\n

Note

\n

Since different operating systems have different path name conventions, there\nare several versions of this module in the standard library. The\nos.path module is always the path module suitable for the operating\nsystem Python is running on, and therefore usable for local paths. However,\nyou can also import and use the individual modules if you want to manipulate\na path that is always in one of the different formats. They all have the\nsame interface:

\n\n
\n
\n
\nos.path.abspath(path)
\n

Return a normalized absolutized version of the pathname path. On most\nplatforms, this is equivalent to normpath(join(os.getcwd(), path)).

\n

\nNew in version 1.5.2.

\n
\n\n
\n
\nos.path.basename(path)
\n
Return the base name of pathname path. This is the second half of the pair\nreturned by split(path). Note that the result of this function is different\nfrom the Unix basename program; where basename for\n'/foo/bar/' returns 'bar', the basename() function returns an\nempty string ('').
\n\n
\n
\nos.path.commonprefix(list)
\n
Return the longest path prefix (taken character-by-character) that is a prefix\nof all paths in list. If list is empty, return the empty string ('').\nNote that this may return invalid paths because it works a character at a time.
\n\n
\n
\nos.path.dirname(path)
\n
Return the directory name of pathname path. This is the first half of the\npair returned by split(path).
\n\n
\n
\nos.path.exists(path)
\n
Return True if path refers to an existing path. Returns False for\nbroken symbolic links. On some platforms, this function may return False if\npermission is not granted to execute os.stat() on the requested file, even\nif the path physically exists.
\n\n
\n
\nos.path.lexists(path)
\n

Return True if path refers to an existing path. Returns True for\nbroken symbolic links. Equivalent to exists() on platforms lacking\nos.lstat().

\n

\nNew in version 2.4.

\n
\n\n
\n
\nos.path.expanduser(path)
\n

On Unix and Windows, return the argument with an initial component of ~ or\n~user replaced by that user‘s home directory.

\n

On Unix, an initial ~ is replaced by the environment variable HOME\nif it is set; otherwise the current user’s home directory is looked up in the\npassword directory through the built-in module pwd. An initial ~user\nis looked up directly in the password directory.

\n

On Windows, HOME and USERPROFILE will be used if set,\notherwise a combination of HOMEPATH and HOMEDRIVE will be\nused. An initial ~user is handled by stripping the last directory component\nfrom the created user path derived above.

\n

If the expansion fails or if the path does not begin with a tilde, the path is\nreturned unchanged.

\n
\n\n
\n
\nos.path.expandvars(path)
\n

Return the argument with environment variables expanded. Substrings of the form\n$name or ${name} are replaced by the value of environment variable\nname. Malformed variable names and references to non-existing variables are\nleft unchanged.

\n

On Windows, expansions are supported in addition to $name and\n${name}.

\n
\n\n
\n
\nos.path.getatime(path)
\n

Return the time of last access of path. The return value is a number giving\nthe number of seconds since the epoch (see the time module). Raise\nos.error if the file does not exist or is inaccessible.

\n

\nNew in version 1.5.2.

\n

\nChanged in version 2.3: If os.stat_float_times() returns True, the result is a floating point\nnumber.

\n
\n\n
\n
\nos.path.getmtime(path)
\n

Return the time of last modification of path. The return value is a number\ngiving the number of seconds since the epoch (see the time module).\nRaise os.error if the file does not exist or is inaccessible.

\n

\nNew in version 1.5.2.

\n

\nChanged in version 2.3: If os.stat_float_times() returns True, the result is a floating point\nnumber.

\n
\n\n
\n
\nos.path.getctime(path)
\n

Return the system’s ctime which, on some systems (like Unix) is the time of the\nlast change, and, on others (like Windows), is the creation time for path.\nThe return value is a number giving the number of seconds since the epoch (see\nthe time module). Raise os.error if the file does not exist or\nis inaccessible.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.path.getsize(path)
\n

Return the size, in bytes, of path. Raise os.error if the file does\nnot exist or is inaccessible.

\n

\nNew in version 1.5.2.

\n
\n\n
\n
\nos.path.isabs(path)
\n
Return True if path is an absolute pathname. On Unix, that means it\nbegins with a slash, on Windows that it begins with a (back)slash after chopping\noff a potential drive letter.
\n\n
\n
\nos.path.isfile(path)
\n
Return True if path is an existing regular file. This follows symbolic\nlinks, so both islink() and isfile() can be true for the same path.
\n\n
\n
\nos.path.isdir(path)
\n
Return True if path is an existing directory. This follows symbolic\nlinks, so both islink() and isdir() can be true for the same path.
\n\n
\n
\nos.path.islink(path)
\n
Return True if path refers to a directory entry that is a symbolic link.\nAlways False if symbolic links are not supported.
\n\n
\n
\nos.path.ismount(path)
\n
Return True if pathname path is a mount point: a point in a file\nsystem where a different file system has been mounted. The function checks\nwhether path‘s parent, path/.., is on a different device than path,\nor whether path/.. and path point to the same i-node on the same\ndevice — this should detect mount points for all Unix and POSIX variants.
\n\n
\n
\nos.path.join(path1[, path2[, ...]])
\n
Join one or more path components intelligently. If any component is an absolute\npath, all previous components (on Windows, including the previous drive letter,\nif there was one) are thrown away, and joining continues. The return value is\nthe concatenation of path1, and optionally path2, etc., with exactly one\ndirectory separator (os.sep) following each non-empty part except the last.\n(This means that an empty last part will result in a path that ends with a\nseparator.) Note that on Windows, since there is a current directory for\neach drive, os.path.join("c:", "foo") represents a path relative to the\ncurrent directory on drive C: (c:foo), not c:\\foo.
\n\n
\n
\nos.path.normcase(path)
\n
Normalize the case of a pathname. On Unix and Mac OS X, this returns the\npath unchanged; on case-insensitive filesystems, it converts the path to\nlowercase. On Windows, it also converts forward slashes to backward slashes.
\n\n
\n
\nos.path.normpath(path)
\n

Normalize a pathname. This collapses redundant separators and up-level\nreferences so that A//B, A/B/, A/./B and A/foo/../B all become\nA/B.

\n

It does not normalize the case (use normcase() for that). On Windows, it\nconverts forward slashes to backward slashes. It should be understood that this\nmay change the meaning of the path if it contains symbolic links!

\n
\n\n
\n
\nos.path.realpath(path)
\n

Return the canonical path of the specified filename, eliminating any symbolic\nlinks encountered in the path (if they are supported by the operating system).

\n

\nNew in version 2.2.

\n
\n\n
\n
\nos.path.relpath(path[, start])
\n

Return a relative filepath to path either from the current directory or from\nan optional start point.

\n

start defaults to os.curdir.

\n

Availability: Windows, Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nos.path.samefile(path1, path2)
\n

Return True if both pathname arguments refer to the same file or directory\n(as indicated by device number and i-node number). Raise an exception if a\nos.stat() call on either pathname fails.

\n

Availability: Unix.

\n
\n\n
\n
\nos.path.sameopenfile(fp1, fp2)
\n

Return True if the file descriptors fp1 and fp2 refer to the same file.

\n

Availability: Unix.

\n
\n\n
\n
\nos.path.samestat(stat1, stat2)
\n

Return True if the stat tuples stat1 and stat2 refer to the same file.\nThese structures may have been returned by fstat(), lstat(), or\nstat(). This function implements the underlying comparison used by\nsamefile() and sameopenfile().

\n

Availability: Unix.

\n
\n\n
\n
\nos.path.split(path)
\n
Split the pathname path into a pair, (head, tail) where tail is the\nlast pathname component and head is everything leading up to that. The\ntail part will never contain a slash; if path ends in a slash, tail\nwill be empty. If there is no slash in path, head will be empty. If\npath is empty, both head and tail are empty. Trailing slashes are\nstripped from head unless it is the root (one or more slashes only). In\nall cases, join(head, tail) returns a path to the same location as path\n(but the strings may differ).
\n\n
\n
\nos.path.splitdrive(path)
\n

Split the pathname path into a pair (drive, tail) where drive is either\na drive specification or the empty string. On systems which do not use drive\nspecifications, drive will always be the empty string. In all cases, drive\n+ tail will be the same as path.

\n

\nNew in version 1.3.

\n
\n\n
\n
\nos.path.splitext(path)
\n

Split the pathname path into a pair (root, ext) such that root + ext ==\npath, and ext is empty or begins with a period and contains at most one\nperiod. Leading periods on the basename are ignored; splitext('.cshrc')\nreturns ('.cshrc', '').

\n

\nChanged in version 2.6: Earlier versions could produce an empty root when the only period was the\nfirst character.

\n
\n\n
\n
\nos.path.splitunc(path)
\n

Split the pathname path into a pair (unc, rest) so that unc is the UNC\nmount point (such as r'\\\\host\\mount'), if present, and rest the rest of\nthe path (such as r'\\path\\file.ext'). For paths containing drive letters,\nunc will always be the empty string.

\n

Availability: Windows.

\n
\n\n
\n
\nos.path.walk(path, visit, arg)
\n

Calls the function visit with arguments (arg, dirname, names) for each\ndirectory in the directory tree rooted at path (including path itself, if it\nis a directory). The argument dirname specifies the visited directory, the\nargument names lists the files in the directory (gotten from\nos.listdir(dirname)). The visit function may modify names to influence\nthe set of directories visited below dirname, e.g. to avoid visiting certain\nparts of the tree. (The object referred to by names must be modified in\nplace, using del or slice assignment.)

\n
\n

Note

\n

Symbolic links to directories are not treated as subdirectories, and that\nwalk() therefore will not visit them. To visit linked directories you must\nidentify them with os.path.islink(file) and os.path.isdir(file), and\ninvoke walk() as necessary.

\n
\n
\n

Note

\n

This function is deprecated and has been removed in 3.0 in favor of\nos.walk().

\n
\n
\n\n
\n
\nos.path.supports_unicode_filenames
\n

True if arbitrary Unicode strings can be used as file names (within limitations\nimposed by the file system).

\n

\nNew in version 2.3.

\n
\n\n
", "searchableItems": [ { "name": "os.path.abspath", "domId": "os.path_os.path.abspath" }, { "name": "os.path.basename", "domId": "os.path_os.path.basename" }, { "name": "os.path.commonprefix", "domId": "os.path_os.path.commonprefix" }, { "name": "os.path.dirname", "domId": "os.path_os.path.dirname" }, { "name": "os.path.exists", "domId": "os.path_os.path.exists" }, { "name": "os.path.expanduser", "domId": "os.path_os.path.expanduser" }, { "name": "os.path.expandvars", "domId": "os.path_os.path.expandvars" }, { "name": "os.path.getatime", "domId": "os.path_os.path.getatime" }, { "name": "os.path.getctime", "domId": "os.path_os.path.getctime" }, { "name": "os.path.getmtime", "domId": "os.path_os.path.getmtime" }, { "name": "os.path.getsize", "domId": "os.path_os.path.getsize" }, { "name": "os.path.isabs", "domId": "os.path_os.path.isabs" }, { "name": "os.path.isdir", "domId": "os.path_os.path.isdir" }, { "name": "os.path.isfile", "domId": "os.path_os.path.isfile" }, { "name": "os.path.islink", "domId": "os.path_os.path.islink" }, { "name": "os.path.ismount", "domId": "os.path_os.path.ismount" }, { "name": "os.path.join", "domId": "os.path_os.path.join" }, { "name": "os.path.lexists", "domId": "os.path_os.path.lexists" }, { "name": "os.path.normcase", "domId": "os.path_os.path.normcase" }, { "name": "os.path.normpath", "domId": "os.path_os.path.normpath" }, { "name": "os.path.realpath", "domId": "os.path_os.path.realpath" }, { "name": "os.path.relpath", "domId": "os.path_os.path.relpath" }, { "name": "os.path.samefile", "domId": "os.path_os.path.samefile" }, { "name": "os.path.sameopenfile", "domId": "os.path_os.path.sameopenfile" }, { "name": "os.path.samestat", "domId": "os.path_os.path.samestat" }, { "name": "os.path.split", "domId": "os.path_os.path.split" }, { "name": "os.path.splitdrive", "domId": "os.path_os.path.splitdrive" }, { "name": "os.path.splitext", "domId": "os.path_os.path.splitext" }, { "name": "os.path.splitunc", "domId": "os.path_os.path.splitunc" }, { "name": "os.path.walk", "domId": "os.path_os.path.walk" } ] }, { "url": "http://docs.python.org/library/glob.html", "title": "glob", "html": "
\n

10.7. glob — Unix style pathname pattern expansion

\n

Source code: Lib/glob.py

\n
\n

The glob module finds all the pathnames matching a specified pattern\naccording to the rules used by the Unix shell. No tilde expansion is done, but\n*, ?, and character ranges expressed with [] will be correctly\nmatched. This is done by using the os.listdir() and\nfnmatch.fnmatch() functions in concert, and not by actually invoking a\nsubshell. (For tilde and shell variable expansion, use\nos.path.expanduser() and os.path.expandvars().)

\n
\n
\nglob.glob(pathname)
\n
Return a possibly-empty list of path names that match pathname, which must be\na string containing a path specification. pathname can be either absolute\n(like /usr/src/Python-1.5/Makefile) or relative (like\n../../Tools/*/*.gif), and can contain shell-style wildcards. Broken\nsymlinks are included in the results (as in the shell).
\n\n
\n
\nglob.iglob(pathname)
\n

Return an iterator which yields the same values as glob()\nwithout actually storing them all simultaneously.

\n

\nNew in version 2.5.

\n
\n\n

For example, consider a directory containing only the following files:\n1.gif, 2.txt, and card.gif. glob() will produce\nthe following results. Notice how any leading components of the path are\npreserved.

\n
>>> import glob\n>>> glob.glob('./[0-9].*')\n['./1.gif', './2.txt']\n>>> glob.glob('*.gif')\n['1.gif', 'card.gif']\n>>> glob.glob('?.gif')\n['1.gif']\n
\n
\n
\n

See also

\n
\n
Module fnmatch
\n
Shell-style filename (not path) expansion
\n
\n
\n
", "searchableItems": [ { "name": "glob.glob", "domId": "glob_glob.glob" }, { "name": "glob.iglob", "domId": "glob_glob.iglob" } ] }, { "url": "http://docs.python.org/library/stat.html", "title": "stat", "html": "
\n

10.3. stat — Interpreting stat() results

\n

Source code: Lib/stat.py

\n
\n

The stat module defines constants and functions for interpreting the\nresults of os.stat(), os.fstat() and os.lstat() (if they\nexist). For complete details about the stat(), fstat() and\nlstat() calls, consult the documentation for your system.

\n

The stat module defines the following functions to test for specific file\ntypes:

\n
\n
\nstat.S_ISDIR(mode)
\n
Return non-zero if the mode is from a directory.
\n\n
\n
\nstat.S_ISCHR(mode)
\n
Return non-zero if the mode is from a character special device file.
\n\n
\n
\nstat.S_ISBLK(mode)
\n
Return non-zero if the mode is from a block special device file.
\n\n
\n
\nstat.S_ISREG(mode)
\n
Return non-zero if the mode is from a regular file.
\n\n
\n
\nstat.S_ISFIFO(mode)
\n
Return non-zero if the mode is from a FIFO (named pipe).
\n\n
\n
\nstat.S_ISLNK(mode)
\n
Return non-zero if the mode is from a symbolic link.
\n\n
\n
\nstat.S_ISSOCK(mode)
\n
Return non-zero if the mode is from a socket.
\n\n

Two additional functions are defined for more general manipulation of the file’s\nmode:

\n
\n
\nstat.S_IMODE(mode)
\n
Return the portion of the file’s mode that can be set by os.chmod()—that is, the file’s permission bits, plus the sticky bit, set-group-id, and\nset-user-id bits (on systems that support them).
\n\n
\n
\nstat.S_IFMT(mode)
\n
Return the portion of the file’s mode that describes the file type (used by the\nS_IS*() functions above).
\n\n

Normally, you would use the os.path.is*() functions for testing the type\nof a file; the functions here are useful when you are doing multiple tests of\nthe same file and wish to avoid the overhead of the stat() system call\nfor each test. These are also useful when checking for information about a file\nthat isn’t handled by os.path, like the tests for block and character\ndevices.

\n

Example:

\n
import os, sys\nfrom stat import *\n\ndef walktree(top, callback):\n    '''recursively descend the directory tree rooted at top,\n       calling the callback function for each regular file'''\n\n    for f in os.listdir(top):\n        pathname = os.path.join(top, f)\n        mode = os.stat(pathname).st_mode\n        if S_ISDIR(mode):\n            # It's a directory, recurse into it\n            walktree(pathname, callback)\n        elif S_ISREG(mode):\n            # It's a file, call the callback function\n            callback(pathname)\n        else:\n            # Unknown file type, print a message\n            print 'Skipping %s'  pathname\n\ndef visitfile(file):\n    print 'visiting', file\n\nif __name__ == '__main__':\n    walktree(sys.argv[1], visitfile)\n
\n
\n

All the variables below are simply symbolic indexes into the 10-tuple returned\nby os.stat(), os.fstat() or os.lstat().

\n
\n
\nstat.ST_MODE
\n
Inode protection mode.
\n\n
\n
\nstat.ST_INO
\n
Inode number.
\n\n
\n
\nstat.ST_DEV
\n
Device inode resides on.
\n\n
\n
\nstat.ST_NLINK
\n
Number of links to the inode.
\n\n
\n
\nstat.ST_UID
\n
User id of the owner.
\n\n
\n
\nstat.ST_GID
\n
Group id of the owner.
\n\n
\n
\nstat.ST_SIZE
\n
Size in bytes of a plain file; amount of data waiting on some special files.
\n\n
\n
\nstat.ST_ATIME
\n
Time of last access.
\n\n
\n
\nstat.ST_MTIME
\n
Time of last modification.
\n\n
\n
\nstat.ST_CTIME
\n
The “ctime” as reported by the operating system. On some systems (like Unix) is\nthe time of the last metadata change, and, on others (like Windows), is the\ncreation time (see platform documentation for details).
\n\n

The interpretation of “file size” changes according to the file type. For plain\nfiles this is the size of the file in bytes. For FIFOs and sockets under most\nflavors of Unix (including Linux in particular), the “size” is the number of\nbytes waiting to be read at the time of the call to os.stat(),\nos.fstat(), or os.lstat(); this can sometimes be useful, especially\nfor polling one of these special files after a non-blocking open. The meaning\nof the size field for other character and block devices varies more, depending\non the implementation of the underlying system call.

\n

The variables below define the flags used in the ST_MODE field.

\n

Use of the functions above is more portable than use of the first set of flags:

\n
\n
\nstat.S_IFMT
\n
Bit mask for the file type bit fields.
\n\n
\n
\nstat.S_IFSOCK
\n
Socket.
\n\n
\n
\nstat.S_IFLNK
\n
Symbolic link.
\n\n
\n
\nstat.S_IFREG
\n
Regular file.
\n\n
\n
\nstat.S_IFBLK
\n
Block device.
\n\n
\n
\nstat.S_IFDIR
\n
Directory.
\n\n
\n
\nstat.S_IFCHR
\n
Character device.
\n\n
\n
\nstat.S_IFIFO
\n
FIFO.
\n\n

The following flags can also be used in the mode argument of os.chmod():

\n
\n
\nstat.S_ISUID
\n
Set UID bit.
\n\n
\n
\nstat.S_ISGID
\n
Set-group-ID bit. This bit has several special uses. For a directory\nit indicates that BSD semantics is to be used for that directory:\nfiles created there inherit their group ID from the directory, not\nfrom the effective group ID of the creating process, and directories\ncreated there will also get the S_ISGID bit set. For a\nfile that does not have the group execution bit (S_IXGRP)\nset, the set-group-ID bit indicates mandatory file/record locking\n(see also S_ENFMT).
\n\n
\n
\nstat.S_ISVTX
\n
Sticky bit. When this bit is set on a directory it means that a file\nin that directory can be renamed or deleted only by the owner of the\nfile, by the owner of the directory, or by a privileged process.
\n\n
\n
\nstat.S_IRWXU
\n
Mask for file owner permissions.
\n\n
\n
\nstat.S_IRUSR
\n
Owner has read permission.
\n\n
\n
\nstat.S_IWUSR
\n
Owner has write permission.
\n\n
\n
\nstat.S_IXUSR
\n
Owner has execute permission.
\n\n
\n
\nstat.S_IRWXG
\n
Mask for group permissions.
\n\n
\n
\nstat.S_IRGRP
\n
Group has read permission.
\n\n
\n
\nstat.S_IWGRP
\n
Group has write permission.
\n\n
\n
\nstat.S_IXGRP
\n
Group has execute permission.
\n\n
\n
\nstat.S_IRWXO
\n
Mask for permissions for others (not in group).
\n\n
\n
\nstat.S_IROTH
\n
Others have read permission.
\n\n
\n
\nstat.S_IWOTH
\n
Others have write permission.
\n\n
\n
\nstat.S_IXOTH
\n
Others have execute permission.
\n\n
\n
\nstat.S_ENFMT
\n
System V file locking enforcement. This flag is shared with S_ISGID:\nfile/record locking is enforced on files that do not have the group\nexecution bit (S_IXGRP) set.
\n\n
\n
\nstat.S_IREAD
\n
Unix V7 synonym for S_IRUSR.
\n\n
\n
\nstat.S_IWRITE
\n
Unix V7 synonym for S_IWUSR.
\n\n
\n
\nstat.S_IEXEC
\n
Unix V7 synonym for S_IXUSR.
\n\n

The following flags can be used in the flags argument of os.chflags():

\n
\n
\nstat.UF_NODUMP
\n
Do not dump the file.
\n\n
\n
\nstat.UF_IMMUTABLE
\n
The file may not be changed.
\n\n
\n
\nstat.UF_APPEND
\n
The file may only be appended to.
\n\n
\n
\nstat.UF_OPAQUE
\n
The directory is opaque when viewed through a union stack.
\n\n
\n
\nstat.UF_NOUNLINK
\n
The file may not be renamed or deleted.
\n\n
\n
\nstat.UF_COMPRESSED
\n
The file is stored compressed (Mac OS X 10.6+).
\n\n
\n
\nstat.UF_HIDDEN
\n
The file should not be displayed in a GUI (Mac OS X 10.5+).
\n\n
\n
\nstat.SF_ARCHIVED
\n
The file may be archived.
\n\n
\n
\nstat.SF_IMMUTABLE
\n
The file may not be changed.
\n\n
\n
\nstat.SF_APPEND
\n
The file may only be appended to.
\n\n
\n
\nstat.SF_NOUNLINK
\n
The file may not be renamed or deleted.
\n\n
\n
\nstat.SF_SNAPSHOT
\n
The file is a snapshot file.
\n\n

See the *BSD or Mac OS systems man page chflags(2) for more information.

\n
", "searchableItems": [ { "name": "stat.S_IFMT", "domId": "stat_stat.S_IFMT" }, { "name": "stat.S_IMODE", "domId": "stat_stat.S_IMODE" }, { "name": "stat.S_ISBLK", "domId": "stat_stat.S_ISBLK" }, { "name": "stat.S_ISCHR", "domId": "stat_stat.S_ISCHR" }, { "name": "stat.S_ISDIR", "domId": "stat_stat.S_ISDIR" }, { "name": "stat.S_ISFIFO", "domId": "stat_stat.S_ISFIFO" }, { "name": "stat.S_ISLNK", "domId": "stat_stat.S_ISLNK" }, { "name": "stat.S_ISREG", "domId": "stat_stat.S_ISREG" }, { "name": "stat.S_ISSOCK", "domId": "stat_stat.S_ISSOCK" } ] }, { "url": "http://docs.python.org/library/tempfile.html", "title": "tempfile", "html": "
\n

10.6. tempfile — Generate temporary files and directories

\n

Source code: Lib/tempfile.py

\n
\n

This module generates temporary files and directories. It works on all\nsupported platforms.

\n

In version 2.3 of Python, this module was overhauled for enhanced security. It\nnow provides three new functions, NamedTemporaryFile(), mkstemp(),\nand mkdtemp(), which should eliminate all remaining need to use the\ninsecure mktemp() function. Temporary file names created by this module\nno longer contain the process ID; instead a string of six random characters is\nused.

\n

Also, all the user-callable functions now take additional arguments which\nallow direct control over the location and name of temporary files. It is\nno longer necessary to use the global tempdir and template variables.\nTo maintain backward compatibility, the argument order is somewhat odd; it\nis recommended to use keyword arguments for clarity.

\n

The module defines the following user-callable functions:

\n
\n
\ntempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]])
\n

Return a file-like object that can be used as a temporary storage area.\nThe file is created using mkstemp(). It will be destroyed as soon\nas it is closed (including an implicit close when the object is garbage\ncollected). Under Unix, the directory entry for the file is removed\nimmediately after the file is created. Other platforms do not support\nthis; your code should not rely on a temporary file created using this\nfunction having or not having a visible name in the file system.

\n

The mode parameter defaults to 'w+b' so that the file created can\nbe read and written without being closed. Binary mode is used so that it\nbehaves consistently on all platforms without regard for the data that is\nstored. bufsize defaults to -1, meaning that the operating system\ndefault is used.

\n

The dir, prefix and suffix parameters are passed to mkstemp().

\n

The returned object is a true file object on POSIX platforms. On other\nplatforms, it is a file-like object whose file attribute is the\nunderlying true file object. This file-like object can be used in a\nwith statement, just like a normal file.

\n
\n\n
\n
\ntempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])
\n

This function operates exactly as TemporaryFile() does, except that\nthe file is guaranteed to have a visible name in the file system (on\nUnix, the directory entry is not unlinked). That name can be retrieved\nfrom the name attribute of the file object. Whether the name can be\nused to open the file a second time, while the named temporary file is\nstill open, varies across platforms (it can be so used on Unix; it cannot\non Windows NT or later). If delete is true (the default), the file is\ndeleted as soon as it is closed.

\n

The returned object is always a file-like object whose file\nattribute is the underlying true file object. This file-like object can\nbe used in a with statement, just like a normal file.

\n

\nNew in version 2.3.

\n

\nNew in version 2.6: The delete parameter.

\n
\n\n
\n
\ntempfile.SpooledTemporaryFile([max_size=0[, mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]]])
\n

This function operates exactly as TemporaryFile() does, except that\ndata is spooled in memory until the file size exceeds max_size, or\nuntil the file’s fileno() method is called, at which point the\ncontents are written to disk and operation proceeds as with\nTemporaryFile().

\n

The resulting file has one additional method, rollover(), which\ncauses the file to roll over to an on-disk file regardless of its size.

\n

The returned object is a file-like object whose _file attribute\nis either a StringIO object or a true file object, depending on\nwhether rollover() has been called. This file-like object can be\nused in a with statement, just like a normal file.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ntempfile.mkstemp([suffix=''[, prefix='tmp'[, dir=None[, text=False]]]])
\n

Creates a temporary file in the most secure manner possible. There are\nno race conditions in the file’s creation, assuming that the platform\nproperly implements the os.O_EXCL flag for os.open(). The\nfile is readable and writable only by the creating user ID. If the\nplatform uses permission bits to indicate whether a file is executable,\nthe file is executable by no one. The file descriptor is not inherited\nby child processes.

\n

Unlike TemporaryFile(), the user of mkstemp() is responsible\nfor deleting the temporary file when done with it.

\n

If suffix is specified, the file name will end with that suffix,\notherwise there will be no suffix. mkstemp() does not put a dot\nbetween the file name and the suffix; if you need one, put it at the\nbeginning of suffix.

\n

If prefix is specified, the file name will begin with that prefix;\notherwise, a default prefix is used.

\n

If dir is specified, the file will be created in that directory;\notherwise, a default directory is used. The default directory is chosen\nfrom a platform-dependent list, but the user of the application can\ncontrol the directory location by setting the TMPDIR, TEMP or TMP\nenvironment variables. There is thus no guarantee that the generated\nfilename will have any nice properties, such as not requiring quoting\nwhen passed to external commands via os.popen().

\n

If text is specified, it indicates whether to open the file in binary\nmode (the default) or text mode. On some platforms, this makes no\ndifference.

\n

mkstemp() returns a tuple containing an OS-level handle to an open\nfile (as would be returned by os.open()) and the absolute pathname\nof that file, in that order.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ntempfile.mkdtemp([suffix=''[, prefix='tmp'[, dir=None]]])
\n

Creates a temporary directory in the most secure manner possible. There\nare no race conditions in the directory’s creation. The directory is\nreadable, writable, and searchable only by the creating user ID.

\n

The user of mkdtemp() is responsible for deleting the temporary\ndirectory and its contents when done with it.

\n

The prefix, suffix, and dir arguments are the same as for\nmkstemp().

\n

mkdtemp() returns the absolute pathname of the new directory.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ntempfile.mktemp([suffix=''[, prefix='tmp'[, dir=None]]])
\n

\nDeprecated since version 2.3: Use mkstemp() instead.

\n

Return an absolute pathname of a file that did not exist at the time the\ncall is made. The prefix, suffix, and dir arguments are the same\nas for mkstemp().

\n
\n

Warning

\n

Use of this function may introduce a security hole in your program. By\nthe time you get around to doing anything with the file name it returns,\nsomeone else may have beaten you to the punch. mktemp() usage can\nbe replaced easily with NamedTemporaryFile(), passing it the\ndelete=False parameter:

\n
>>> f = NamedTemporaryFile(delete=False)\n>>> f\n<open file '<fdopen>', mode 'w+b' at 0x384698>\n>>> f.name\n'/var/folders/5q/5qTPn6xq2RaWqk+1Ytw3-U+++TI/-Tmp-/tmpG7V1Y0'\n>>> f.write("Hello World!\\n")\n>>> f.close()\n>>> os.unlink(f.name)\n>>> os.path.exists(f.name)\nFalse\n
\n
\n
\n
\n\n

The module uses two global variables that tell it how to construct a\ntemporary name. They are initialized at the first call to any of the\nfunctions above. The caller may change them, but this is discouraged; use\nthe appropriate function arguments, instead.

\n
\n
\ntempfile.tempdir
\n

When set to a value other than None, this variable defines the\ndefault value for the dir argument to all the functions defined in this\nmodule.

\n

If tempdir is unset or None at any call to any of the above\nfunctions, Python searches a standard list of directories and sets\ntempdir to the first one which the calling user can create files in.\nThe list is:

\n
    \n
  1. The directory named by the TMPDIR environment variable.
  2. \n
  3. The directory named by the TEMP environment variable.
  4. \n
  5. The directory named by the TMP environment variable.
  6. \n
  7. A platform-specific location:
      \n
    • On RiscOS, the directory named by the Wimp$ScrapDir environment\nvariable.
    • \n
    • On Windows, the directories C:\\TEMP, C:\\TMP,\n\\TEMP, and \\TMP, in that order.
    • \n
    • On all other platforms, the directories /tmp, /var/tmp, and\n/usr/tmp, in that order.
    • \n
    \n
  8. \n
  9. As a last resort, the current working directory.
  10. \n
\n
\n\n
\n
\ntempfile.gettempdir()
\n

Return the directory currently selected to create temporary files in. If\ntempdir is not None, this simply returns its contents; otherwise,\nthe search described above is performed, and the result returned.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ntempfile.template
\n

\nDeprecated since version 2.0: Use gettempprefix() instead.

\n

When set to a value other than None, this variable defines the prefix of the\nfinal component of the filenames returned by mktemp(). A string of six\nrandom letters and digits is appended to the prefix to make the filename unique.\nThe default prefix is tmp.

\n

Older versions of this module used to require that template be set to\nNone after a call to os.fork(); this has not been necessary since\nversion 1.5.2.

\n
\n\n
\n
\ntempfile.gettempprefix()
\n

Return the filename prefix used to create temporary files. This does not\ncontain the directory component. Using this function is preferred over reading\nthe template variable directly.

\n

\nNew in version 1.5.2.

\n
\n\n
", "searchableItems": [ { "name": "tempfile.gettempdir", "domId": "tempfile_tempfile.gettempdir" }, { "name": "tempfile.gettempprefix", "domId": "tempfile_tempfile.gettempprefix" }, { "name": "tempfile.mkdtemp", "domId": "tempfile_tempfile.mkdtemp" }, { "name": "tempfile.mkstemp", "domId": "tempfile_tempfile.mkstemp" }, { "name": "tempfile.mktemp", "domId": "tempfile_tempfile.mktemp" }, { "name": "tempfile.NamedTemporaryFile", "domId": "tempfile_tempfile.NamedTemporaryFile" }, { "name": "tempfile.SpooledTemporaryFile", "domId": "tempfile_tempfile.SpooledTemporaryFile" }, { "name": "tempfile.TemporaryFile", "domId": "tempfile_tempfile.TemporaryFile" } ] }, { "url": "http://docs.python.org/library/fnmatch.html", "title": "fnmatch", "html": "
\n

10.8. fnmatch — Unix filename pattern matching

\n

Source code: Lib/fnmatch.py

\n
\n

This module provides support for Unix shell-style wildcards, which are not the\nsame as regular expressions (which are documented in the re module). The\nspecial characters used in shell-style wildcards are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
PatternMeaning
*matches everything
?matches any single character
[seq]matches any character in seq
[!seq]matches any character not in seq
\n

Note that the filename separator ('/' on Unix) is not special to this\nmodule. See module glob for pathname expansion (glob uses\nfnmatch() to match pathname segments). Similarly, filenames starting with\na period are not special for this module, and are matched by the * and ?\npatterns.

\n
\n
\nfnmatch.fnmatch(filename, pattern)
\n

Test whether the filename string matches the pattern string, returning\nTrue or False. If the operating system is case-insensitive,\nthen both parameters will be normalized to all lower- or upper-case before\nthe comparison is performed. fnmatchcase() can be used to perform a\ncase-sensitive comparison, regardless of whether that’s standard for the\noperating system.

\n

This example will print all file names in the current directory with the\nextension .txt:

\n
import fnmatch\nimport os\n\nfor file in os.listdir('.'):\n    if fnmatch.fnmatch(file, '*.txt'):\n        print file\n
\n
\n
\n\n
\n
\nfnmatch.fnmatchcase(filename, pattern)
\n
Test whether filename matches pattern, returning True or\nFalse; the comparison is case-sensitive.
\n\n
\n
\nfnmatch.filter(names, pattern)
\n

Return the subset of the list of names that match pattern. It is the same as\n[n for n in names if fnmatch(n, pattern)], but implemented more efficiently.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nfnmatch.translate(pattern)
\n

Return the shell-style pattern converted to a regular expression.

\n

Be aware there is no way to quote meta-characters.

\n

Example:

\n
>>> import fnmatch, re\n>>>\n>>> regex = fnmatch.translate('*.txt')\n>>> regex\n'.*\\\\.txt$'\n>>> reobj = re.compile(regex)\n>>> reobj.match('foobar.txt')\n<_sre.SRE_Match object at 0x...>\n
\n
\n
\n\n
\n

See also

\n
\n
Module glob
\n
Unix shell-style path expansion.
\n
\n
\n
", "searchableItems": [ { "name": "fnmatch.filter", "domId": "fnmatch_fnmatch.filter" }, { "name": "fnmatch.fnmatch", "domId": "fnmatch_fnmatch.fnmatch" }, { "name": "fnmatch.fnmatchcase", "domId": "fnmatch_fnmatch.fnmatchcase" }, { "name": "fnmatch.translate", "domId": "fnmatch_fnmatch.translate" } ] }, { "url": "http://docs.python.org/library/linecache.html", "title": "linecache", "html": "
\n

10.9. linecache — Random access to text lines

\n

Source code: Lib/linecache.py

\n
\n

The linecache module allows one to get any line from any file, while\nattempting to optimize internally, using a cache, the common case where many\nlines are read from a single file. This is used by the traceback module\nto retrieve source lines for inclusion in the formatted traceback.

\n

The linecache module defines the following functions:

\n
\n
\nlinecache.getline(filename, lineno[, module_globals])
\n

Get line lineno from file named filename. This function will never raise an\nexception — it will return '' on errors (the terminating newline character\nwill be included for lines that are found).

\n

If a file named filename is not found, the function will look for it in the\nmodule search path, sys.path, after first checking for a PEP 302\n__loader__ in module_globals, in case the module was imported from a\nzipfile or other non-filesystem import source.

\n

\nNew in version 2.5: The module_globals parameter was added.

\n
\n\n
\n
\nlinecache.clearcache()
\n
Clear the cache. Use this function if you no longer need lines from files\npreviously read using getline().
\n\n
\n
\nlinecache.checkcache([filename])
\n
Check the cache for validity. Use this function if files in the cache may have\nchanged on disk, and you require the updated version. If filename is omitted,\nit will check all the entries in the cache.
\n\n

Example:

\n
>>> import linecache\n>>> linecache.getline('/etc/passwd', 4)\n'sys:x:3:3:sys:/dev:/bin/sh\\n'\n
\n
\n
", "searchableItems": [ { "name": "linecache.checkcache", "domId": "linecache_linecache.checkcache" }, { "name": "linecache.clearcache", "domId": "linecache_linecache.clearcache" }, { "name": "linecache.getline", "domId": "linecache_linecache.getline" } ] }, { "url": "http://docs.python.org/library/filecmp.html", "title": "filecmp", "html": "
\n

10.5. filecmp — File and Directory Comparisons

\n

Source code: Lib/filecmp.py

\n
\n

The filecmp module defines functions to compare files and directories,\nwith various optional time/correctness trade-offs. For comparing files,\nsee also the difflib module.

\n

The filecmp module defines the following functions:

\n
\n
\nfilecmp.cmp(f1, f2[, shallow])
\n

Compare the files named f1 and f2, returning True if they seem equal,\nFalse otherwise.

\n

Unless shallow is given and is false, files with identical os.stat()\nsignatures are taken to be equal.

\n

Files that were compared using this function will not be compared again unless\ntheir os.stat() signature changes.

\n

Note that no external programs are called from this function, giving it\nportability and efficiency.

\n
\n\n
\n
\nfilecmp.cmpfiles(dir1, dir2, common[, shallow])
\n

Compare the files in the two directories dir1 and dir2 whose names are\ngiven by common.

\n

Returns three lists of file names: match, mismatch,\nerrors. match contains the list of files that match, mismatch contains\nthe names of those that don’t, and errors lists the names of files which\ncould not be compared. Files are listed in errors if they don’t exist in\none of the directories, the user lacks permission to read them or if the\ncomparison could not be done for some other reason.

\n

The shallow parameter has the same meaning and default value as for\nfilecmp.cmp().

\n

For example, cmpfiles('a', 'b', ['c', 'd/e']) will compare a/c with\nb/c and a/d/e with b/d/e. 'c' and 'd/e' will each be in\none of the three returned lists.

\n
\n\n

Example:

\n
>>> import filecmp\n>>> filecmp.cmp('undoc.rst', 'undoc.rst')\nTrue\n>>> filecmp.cmp('undoc.rst', 'index.rst')\nFalse\n
\n
\n
\n

10.5.1. The dircmp class

\n

dircmp instances are built using this constructor:

\n
\n
\nclass filecmp.dircmp(a, b[, ignore[, hide]])
\n

Construct a new directory comparison object, to compare the directories a and\nb. ignore is a list of names to ignore, and defaults to ['RCS', 'CVS',\n'tags']. hide is a list of names to hide, and defaults to [os.curdir,\nos.pardir].

\n

The dircmp class provides the following methods:

\n
\n
\nreport()
\n
Print (to sys.stdout) a comparison between a and b.
\n\n
\n
\nreport_partial_closure()
\n
Print a comparison between a and b and common immediate\nsubdirectories.
\n\n
\n
\nreport_full_closure()
\n
Print a comparison between a and b and common subdirectories\n(recursively).
\n\n

The dircmp offers a number of interesting attributes that may be\nused to get various bits of information about the directory trees being\ncompared.

\n

Note that via __getattr__() hooks, all attributes are computed lazily,\nso there is no speed penalty if only those attributes which are lightweight\nto compute are used.

\n
\n
\nleft_list
\n
Files and subdirectories in a, filtered by hide and ignore.
\n\n
\n
\nright_list
\n
Files and subdirectories in b, filtered by hide and ignore.
\n\n
\n
\ncommon
\n
Files and subdirectories in both a and b.
\n\n
\n
\nleft_only
\n
Files and subdirectories only in a.
\n\n
\n
\nright_only
\n
Files and subdirectories only in b.
\n\n
\n
\ncommon_dirs
\n
Subdirectories in both a and b.
\n\n
\n
\ncommon_files
\n
Files in both a and b
\n\n
\n
\ncommon_funny
\n
Names in both a and b, such that the type differs between the\ndirectories, or names for which os.stat() reports an error.
\n\n
\n
\nsame_files
\n
Files which are identical in both a and b.
\n\n
\n
\ndiff_files
\n
Files which are in both a and b, whose contents differ.
\n\n
\n
\nfunny_files
\n
Files which are in both a and b, but could not be compared.
\n\n
\n
\nsubdirs
\n
A dictionary mapping names in common_dirs to dircmp objects.
\n\n
\n\n
\n
", "searchableItems": [ { "name": "filecmp.cmp", "domId": "filecmp_filecmp.cmp" }, { "name": "filecmp.cmpfiles", "domId": "filecmp_filecmp.cmpfiles" }, { "name": "filecmp.dircmp", "domId": "filecmp_filecmp.dircmp" }, { "name": "filecmp.dircmp.report", "domId": "filecmp_filecmp.dircmp.report" }, { "name": "filecmp.dircmp.report_full_closure", "domId": "filecmp_filecmp.dircmp.report_full_closure" }, { "name": "filecmp.dircmp.report_partial_closure", "domId": "filecmp_filecmp.dircmp.report_partial_closure" } ] }, { "url": "http://docs.python.org/library/dircache.html", "title": "dircache", "html": "
\n

10.11. dircache — Cached directory listings

\n

\nDeprecated since version 2.6: The dircache module has been removed in Python 3.0.

\n

The dircache module defines a function for reading directory listing\nusing a cache, and cache invalidation using the mtime of the directory.\nAdditionally, it defines a function to annotate directories by appending a\nslash.

\n

The dircache module defines the following functions:

\n
\n
\ndircache.reset()
\n
Resets the directory cache.
\n\n
\n
\ndircache.listdir(path)
\n

Return a directory listing of path, as gotten from os.listdir(). Note\nthat unless path changes, further call to listdir() will not re-read the\ndirectory structure.

\n

Note that the list returned should be regarded as read-only. (Perhaps a future\nversion should change it to return a tuple?)

\n
\n\n
\n
\ndircache.opendir(path)
\n
Same as listdir(). Defined for backwards compatibility.
\n\n
\n
\ndircache.annotate(head, list)
\n
Assume list is a list of paths relative to head, and append, in place, a\n'/' to each path which points to a directory.
\n\n
>>> import dircache\n>>> a = dircache.listdir('/')\n>>> a = a[:] # Copy the return value so we can change 'a'\n>>> a\n['bin', 'boot', 'cdrom', 'dev', 'etc', 'floppy', 'home', 'initrd', 'lib', 'lost+\nfound', 'mnt', 'proc', 'root', 'sbin', 'tmp', 'usr', 'var', 'vmlinuz']\n>>> dircache.annotate('/', a)\n>>> a\n['bin/', 'boot/', 'cdrom/', 'dev/', 'etc/', 'floppy/', 'home/', 'initrd/', 'lib/\n', 'lost+found/', 'mnt/', 'proc/', 'root/', 'sbin/', 'tmp/', 'usr/', 'var/', 'vm\nlinuz']\n
\n
\n
", "searchableItems": [ { "name": "dircache.annotate", "domId": "dircache_dircache.annotate" }, { "name": "dircache.listdir", "domId": "dircache_dircache.listdir" }, { "name": "dircache.opendir", "domId": "dircache_dircache.opendir" }, { "name": "dircache.reset", "domId": "dircache_dircache.reset" } ] }, { "url": "http://docs.python.org/library/copy_reg.html", "title": "copy_reg", "html": "
\n

11.3. copy_reg — Register pickle support functions

\n
\n

Note

\n

The copy_reg module has been renamed to copyreg in Python 3.0.\nThe 2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n

The copy_reg module provides support for the pickle and\ncPickle modules. The copy module is likely to use this in the\nfuture as well. It provides configuration information about object constructors\nwhich are not classes. Such constructors may be factory functions or class\ninstances.

\n
\n
\ncopy_reg.constructor(object)
\n
Declares object to be a valid constructor. If object is not callable (and\nhence not valid as a constructor), raises TypeError.
\n\n
\n
\ncopy_reg.pickle(type, function[, constructor])
\n

Declares that function should be used as a “reduction” function for objects of\ntype type; type must not be a “classic” class object. (Classic classes are\nhandled differently; see the documentation for the pickle module for\ndetails.) function should return either a string or a tuple containing two or\nthree elements.

\n

The optional constructor parameter, if provided, is a callable object which\ncan be used to reconstruct the object when called with the tuple of arguments\nreturned by function at pickling time. TypeError will be raised if\nobject is a class or constructor is not callable.

\n

See the pickle module for more details on the interface expected of\nfunction and constructor.

\n
\n\n
", "searchableItems": [ { "name": "copy_reg.constructor", "domId": "copy_reg_copy_reg.constructor" }, { "name": "copy_reg.pickle", "domId": "copy_reg_copy_reg.pickle" } ] }, { "url": "http://docs.python.org/library/shutil.html", "title": "shutil", "html": "
\n

10.10. shutil — High-level file operations

\n

Source code: Lib/shutil.py

\n
\n

The shutil module offers a number of high-level operations on files and\ncollections of files. In particular, functions are provided which support file\ncopying and removal. For operations on individual files, see also the\nos module.

\n
\n

Warning

\n

Even the higher-level file copying functions (copy(), copy2())\ncan’t copy all file metadata.

\n

On POSIX platforms, this means that file owner and group are lost as well\nas ACLs. On Mac OS, the resource fork and other metadata are not used.\nThis means that resources will be lost and file type and creator codes will\nnot be correct. On Windows, file owners, ACLs and alternate data streams\nare not copied.

\n
\n
\n

10.10.1. Directory and files operations

\n
\n
\nshutil.copyfileobj(fsrc, fdst[, length])
\n
Copy the contents of the file-like object fsrc to the file-like object fdst.\nThe integer length, if given, is the buffer size. In particular, a negative\nlength value means to copy the data without looping over the source data in\nchunks; by default the data is read in chunks to avoid uncontrolled memory\nconsumption. Note that if the current file position of the fsrc object is not\n0, only the contents from the current file position to the end of the file will\nbe copied.
\n\n
\n
\nshutil.copyfile(src, dst)
\n
Copy the contents (no metadata) of the file named src to a file named dst.\ndst must be the complete target file name; look at copy() for a copy that\naccepts a target directory path. If src and dst are the same files,\nError is raised.\nThe destination location must be writable; otherwise, an IOError exception\nwill be raised. If dst already exists, it will be replaced. Special files\nsuch as character or block devices and pipes cannot be copied with this\nfunction. src and dst are path names given as strings.
\n\n
\n
\nshutil.copymode(src, dst)
\n
Copy the permission bits from src to dst. The file contents, owner, and\ngroup are unaffected. src and dst are path names given as strings.
\n\n
\n
\nshutil.copystat(src, dst)
\n
Copy the permission bits, last access time, last modification time, and flags\nfrom src to dst. The file contents, owner, and group are unaffected. src\nand dst are path names given as strings.
\n\n
\n
\nshutil.copy(src, dst)
\n
Copy the file src to the file or directory dst. If dst is a directory, a\nfile with the same basename as src is created (or overwritten) in the\ndirectory specified. Permission bits are copied. src and dst are path\nnames given as strings.
\n\n
\n
\nshutil.copy2(src, dst)
\n
Similar to copy(), but metadata is copied as well – in fact, this is just\ncopy() followed by copystat(). This is similar to the\nUnix command cp -p.
\n\n
\n
\nshutil.ignore_patterns(*patterns)
\n

This factory function creates a function that can be used as a callable for\ncopytree()‘s ignore argument, ignoring files and directories that\nmatch one of the glob-style patterns provided. See the example below.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nshutil.copytree(src, dst[, symlinks=False[, ignore=None]])
\n

Recursively copy an entire directory tree rooted at src. The destination\ndirectory, named by dst, must not already exist; it will be created as well\nas missing parent directories. Permissions and times of directories are\ncopied with copystat(), individual files are copied using\ncopy2().

\n

If symlinks is true, symbolic links in the source tree are represented as\nsymbolic links in the new tree, but the metadata of the original links is NOT\ncopied; if false or omitted, the contents and metadata of the linked files\nare copied to the new tree.

\n

If ignore is given, it must be a callable that will receive as its\narguments the directory being visited by copytree(), and a list of its\ncontents, as returned by os.listdir(). Since copytree() is\ncalled recursively, the ignore callable will be called once for each\ndirectory that is copied. The callable must return a sequence of directory\nand file names relative to the current directory (i.e. a subset of the items\nin its second argument); these names will then be ignored in the copy\nprocess. ignore_patterns() can be used to create such a callable that\nignores names based on glob-style patterns.

\n

If exception(s) occur, an Error is raised with a list of reasons.

\n

The source code for this should be considered an example rather than the\nultimate tool.

\n

\nChanged in version 2.3: Error is raised if any exceptions occur during copying, rather than\nprinting a message.

\n

\nChanged in version 2.5: Create intermediate directories needed to create dst, rather than raising an\nerror. Copy permissions and times of directories using copystat().

\n

\nChanged in version 2.6: Added the ignore argument to be able to influence what is being copied.

\n
\n\n
\n
\nshutil.rmtree(path[, ignore_errors[, onerror]])
\n

Delete an entire directory tree; path must point to a directory (but not a\nsymbolic link to a directory). If ignore_errors is true, errors resulting\nfrom failed removals will be ignored; if false or omitted, such errors are\nhandled by calling a handler specified by onerror or, if that is omitted,\nthey raise an exception.

\n

If onerror is provided, it must be a callable that accepts three\nparameters: function, path, and excinfo. The first parameter,\nfunction, is the function which raised the exception; it will be\nos.path.islink(), os.listdir(), os.remove() or\nos.rmdir(). The second parameter, path, will be the path name passed\nto function. The third parameter, excinfo, will be the exception\ninformation return by sys.exc_info(). Exceptions raised by onerror\nwill not be caught.

\n

\nChanged in version 2.6: Explicitly check for path being a symbolic link and raise OSError\nin that case.

\n
\n\n
\n
\nshutil.move(src, dst)
\n

Recursively move a file or directory (src) to another location (dst).

\n

If the destination is a directory or a symlink to a directory, then src is\nmoved inside that directory.

\n

The destination directory must not already exist. If the destination already\nexists but is not a directory, it may be overwritten depending on\nos.rename() semantics.

\n

If the destination is on the current filesystem, then os.rename() is\nused. Otherwise, src is copied (using copy2()) to dst and then\nremoved.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception shutil.Error
\n

This exception collects exceptions that are raised during a multi-file\noperation. For copytree(), the exception argument is a list of 3-tuples\n(srcname, dstname, exception).

\n

\nNew in version 2.3.

\n
\n\n
\n

10.10.1.1. copytree example

\n

This example is the implementation of the copytree() function, described\nabove, with the docstring omitted. It demonstrates many of the other functions\nprovided by this module.

\n
def copytree(src, dst, symlinks=False, ignore=None):\n    names = os.listdir(src)\n    if ignore is not None:\n        ignored_names = ignore(src, names)\n    else:\n        ignored_names = set()\n\n    os.makedirs(dst)\n    errors = []\n    for name in names:\n        if name in ignored_names:\n            continue\n        srcname = os.path.join(src, name)\n        dstname = os.path.join(dst, name)\n        try:\n            if symlinks and os.path.islink(srcname):\n                linkto = os.readlink(srcname)\n                os.symlink(linkto, dstname)\n            elif os.path.isdir(srcname):\n                copytree(srcname, dstname, symlinks, ignore)\n            else:\n                copy2(srcname, dstname)\n            # XXX What about devices, sockets etc.?\n        except (IOError, os.error), why:\n            errors.append((srcname, dstname, str(why)))\n        # catch the Error from the recursive copytree so that we can\n        # continue with other files\n        except Error, err:\n            errors.extend(err.args[0])\n    try:\n        copystat(src, dst)\n    except WindowsError:\n        # can't copy file access times on Windows\n        pass\n    except OSError, why:\n        errors.extend((src, dst, str(why)))\n    if errors:\n        raise Error(errors)\n
\n
\n

Another example that uses the ignore_patterns() helper:

\n
from shutil import copytree, ignore_patterns\n\ncopytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))\n
\n
\n

This will copy everything except .pyc files and files or directories whose\nname starts with tmp.

\n

Another example that uses the ignore argument to add a logging call:

\n
from shutil import copytree\nimport logging\n\ndef _logpath(path, names):\n    logging.info('Working in %s'  path)\n    return []   # nothing will be ignored\n\ncopytree(source, destination, ignore=_logpath)\n
\n
\n
\n
\n
\n

10.10.2. Archives operations

\n
\n
\nshutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]])
\n

Create an archive file (eg. zip or tar) and returns its name.

\n

base_name is the name of the file to create, including the path, minus\nany format-specific extension. format is the archive format: one of\n“zip”, “tar”, “bztar” or “gztar”.

\n

root_dir is a directory that will be the root directory of the\narchive; ie. we typically chdir into root_dir before creating the\narchive.

\n

base_dir is the directory where we start archiving from;\nie. base_dir will be the common prefix of all files and\ndirectories in the archive.

\n

root_dir and base_dir both default to the current directory.

\n

owner and group are used when creating a tar archive. By default,\nuses the current owner and group.

\n

logger is an instance of logging.Logger.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nshutil.get_archive_formats()
\n

Return a list of supported formats for archiving.\nEach element of the returned sequence is a tuple (name, description)

\n

By default shutil provides these formats:

\n
    \n
  • gztar: gzip’ed tar-file
  • \n
  • bztar: bzip2’ed tar-file
  • \n
  • tar: uncompressed tar file
  • \n
  • zip: ZIP file
  • \n
\n

You can register new formats or provide your own archiver for any existing\nformats, by using register_archive_format().

\n

\nNew in version 2.7.

\n
\n\n
\n
\nshutil.register_archive_format(name, function[, extra_args[, description]])
\n

Register an archiver for the format name. function is a callable that\nwill be used to invoke the archiver.

\n

If given, extra_args is a sequence of (name, value) that will be\nused as extra keywords arguments when the archiver callable is used.

\n

description is used by get_archive_formats() which returns the\nlist of archivers. Defaults to an empty list.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nshutil.unregister_archive_format(name)
\n

Remove the archive format name from the list of supported formats.

\n

\nNew in version 2.7.

\n
\n\n
\n

10.10.2.1. Archiving example

\n

In this example, we create a gzip’ed tar-file archive containing all files\nfound in the .ssh directory of the user:

\n
>>> from shutil import make_archive\n>>> import os\n>>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))\n>>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))\n>>> make_archive(archive_name, 'gztar', root_dir)\n'/Users/tarek/myarchive.tar.gz'\n
\n
\n

The resulting archive contains:

\n
$ tar -tzvf /Users/tarek/myarchive.tar.gz\ndrwx------ tarek/staff       0 2010-02-01 16:23:40 ./\n-rw-r--r-- tarek/staff     609 2008-06-09 13:26:54 ./authorized_keys\n-rwxr-xr-x tarek/staff      65 2008-06-09 13:26:54 ./config\n-rwx------ tarek/staff     668 2008-06-09 13:26:54 ./id_dsa\n-rwxr-xr-x tarek/staff     609 2008-06-09 13:26:54 ./id_dsa.pub\n-rw------- tarek/staff    1675 2008-06-09 13:26:54 ./id_rsa\n-rw-r--r-- tarek/staff     397 2008-06-09 13:26:54 ./id_rsa.pub\n-rw-r--r-- tarek/staff   37192 2010-02-06 18:23:10 ./known_hosts
\n
\n
\n
\n
", "searchableItems": [ { "name": "shutil.copy", "domId": "shutil_shutil.copy" }, { "name": "shutil.copy2", "domId": "shutil_shutil.copy2" }, { "name": "shutil.copyfile", "domId": "shutil_shutil.copyfile" }, { "name": "shutil.copyfileobj", "domId": "shutil_shutil.copyfileobj" }, { "name": "shutil.copymode", "domId": "shutil_shutil.copymode" }, { "name": "shutil.copystat", "domId": "shutil_shutil.copystat" }, { "name": "shutil.copytree", "domId": "shutil_shutil.copytree" }, { "name": "shutil.get_archive_formats", "domId": "shutil_shutil.get_archive_formats" }, { "name": "shutil.ignore_patterns", "domId": "shutil_shutil.ignore_patterns" }, { "name": "shutil.make_archive", "domId": "shutil_shutil.make_archive" }, { "name": "shutil.move", "domId": "shutil_shutil.move" }, { "name": "shutil.register_archive_format", "domId": "shutil_shutil.register_archive_format" }, { "name": "shutil.rmtree", "domId": "shutil_shutil.rmtree" }, { "name": "shutil.unregister_archive_format", "domId": "shutil_shutil.unregister_archive_format" } ] }, { "url": "http://docs.python.org/library/macpath.html", "title": "macpath", "html": "
\n

10.12. macpath — Mac OS 9 path manipulation functions

\n

This module is the Mac OS 9 (and earlier) implementation of the os.path\nmodule. It can be used to manipulate old-style Macintosh pathnames on Mac OS X\n(or any other platform).

\n

The following functions are available in this module: normcase(),\nnormpath(), isabs(), join(), split(), isdir(),\nisfile(), walk(), exists(). For other functions available in\nos.path dummy counterparts are available.

\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/marshal.html", "title": "marshal", "html": "
\n

11.5. marshal — Internal Python object serialization

\n

This module contains functions that can read and write Python values in a binary\nformat. The format is specific to Python, but independent of machine\narchitecture issues (e.g., you can write a Python value to a file on a PC,\ntransport the file to a Sun, and read it back there). Details of the format are\nundocumented on purpose; it may change between Python versions (although it\nrarely does). [1]

\n

This is not a general “persistence” module. For general persistence and\ntransfer of Python objects through RPC calls, see the modules pickle and\nshelve. The marshal module exists mainly to support reading and\nwriting the “pseudo-compiled” code for Python modules of .pyc files.\nTherefore, the Python maintainers reserve the right to modify the marshal format\nin backward incompatible ways should the need arise. If you’re serializing and\nde-serializing Python objects, use the pickle module instead – the\nperformance is comparable, version independence is guaranteed, and pickle\nsupports a substantially wider range of objects than marshal.

\n
\n

Warning

\n

The marshal module is not intended to be secure against erroneous or\nmaliciously constructed data. Never unmarshal data received from an\nuntrusted or unauthenticated source.

\n
\n

Not all Python object types are supported; in general, only objects whose value\nis independent from a particular invocation of Python can be written and read by\nthis module. The following types are supported: booleans, integers, long\nintegers, floating point numbers, complex numbers, strings, Unicode objects,\ntuples, lists, sets, frozensets, dictionaries, and code objects, where it should\nbe understood that tuples, lists, sets, frozensets and dictionaries are only\nsupported as long as the values contained therein are themselves supported; and\nrecursive lists, sets and dictionaries should not be written (they will cause\ninfinite loops). The singletons None, Ellipsis and\nStopIteration can also be marshalled and unmarshalled.

\n
\n

Warning

\n

On machines where C’s long int type has more than 32 bits (such as the\nDEC Alpha), it is possible to create plain Python integers that are longer\nthan 32 bits. If such an integer is marshaled and read back in on a machine\nwhere C’s long int type has only 32 bits, a Python long integer object\nis returned instead. While of a different type, the numeric value is the\nsame. (This behavior is new in Python 2.2. In earlier versions, all but the\nleast-significant 32 bits of the value were lost, and a warning message was\nprinted.)

\n
\n

There are functions that read/write files as well as functions operating on\nstrings.

\n

The module defines these functions:

\n
\n
\nmarshal.dump(value, file[, version])
\n

Write the value on the open file. The value must be a supported type. The\nfile must be an open file object such as sys.stdout or returned by\nopen() or os.popen(). It must be opened in binary mode ('wb'\nor 'w+b').

\n

If the value has (or contains an object that has) an unsupported type, a\nValueError exception is raised — but garbage data will also be written\nto the file. The object will not be properly read back by load().

\n

\nNew in version 2.4: The version argument indicates the data format that dump should use\n(see below).

\n
\n\n
\n
\nmarshal.load(file)
\n

Read one value from the open file and return it. If no valid value is read\n(e.g. because the data has a different Python version’s incompatible marshal\nformat), raise EOFError, ValueError or TypeError. The\nfile must be an open file object opened in binary mode ('rb' or\n'r+b').

\n
\n

Note

\n

If an object containing an unsupported type was marshalled with dump(),\nload() will substitute None for the unmarshallable type.

\n
\n
\n\n
\n
\nmarshal.dumps(value[, version])
\n

Return the string that would be written to a file by dump(value, file). The\nvalue must be a supported type. Raise a ValueError exception if value\nhas (or contains an object that has) an unsupported type.

\n

\nNew in version 2.4: The version argument indicates the data format that dumps should use\n(see below).

\n
\n\n
\n
\nmarshal.loads(string)
\n
Convert the string to a value. If no valid value is found, raise\nEOFError, ValueError or TypeError. Extra characters in the\nstring are ignored.
\n\n

In addition, the following constants are defined:

\n
\n
\nmarshal.version
\n

Indicates the format that the module uses. Version 0 is the historical format,\nversion 1 (added in Python 2.4) shares interned strings and version 2 (added in\nPython 2.5) uses a binary format for floating point numbers. The current version\nis 2.

\n

\nNew in version 2.4.

\n
\n\n

Footnotes

\n\n\n\n\n\n
[1]The name of this module stems from a bit of terminology used by the designers of\nModula-3 (amongst others), who use the term “marshalling” for shipping of data\naround in a self-contained form. Strictly speaking, “to marshal” means to\nconvert some data from internal to external form (in an RPC buffer for instance)\nand “unmarshalling” for the reverse process.
\n
", "searchableItems": [ { "name": "marshal.dump", "domId": "marshal_marshal.dump" }, { "name": "marshal.dumps", "domId": "marshal_marshal.dumps" }, { "name": "marshal.load", "domId": "marshal_marshal.load" }, { "name": "marshal.loads", "domId": "marshal_marshal.loads" } ] }, { "url": "http://docs.python.org/library/anydbm.html", "title": "anydbm", "html": "
\n

11.6. anydbm — Generic access to DBM-style databases

\n
\n

Note

\n

The anydbm module has been renamed to dbm in Python 3.0. The\n2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n

anydbm is a generic interface to variants of the DBM database —\ndbhash (requires bsddb), gdbm, or dbm. If none of\nthese modules is installed, the slow-but-simple implementation in module\ndumbdbm will be used.

\n
\n
\nanydbm.open(filename[, flag[, mode]])
\n

Open the database file filename and return a corresponding object.

\n

If the database file already exists, the whichdb module is used to\ndetermine its type and the appropriate module is used; if it does not exist,\nthe first module listed above that can be imported is used.

\n

The optional flag argument must be one of these values:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueMeaning
'r'Open existing database for reading only\n(default)
'w'Open existing database for reading and\nwriting
'c'Open database for reading and writing,\ncreating it if it doesn’t exist
'n'Always create a new, empty database, open\nfor reading and writing
\n

If not specified, the default value is 'r'.

\n

The optional mode argument is the Unix mode of the file, used only when the\ndatabase has to be created. It defaults to octal 0666 (and will be\nmodified by the prevailing umask).

\n
\n\n
\n
\nexception anydbm.error
\n
A tuple containing the exceptions that can be raised by each of the supported\nmodules, with a unique exception also named anydbm.error as the first\nitem — the latter is used when anydbm.error is raised.
\n\n

The object returned by open() supports most of the same functionality as\ndictionaries; keys and their corresponding values can be stored, retrieved, and\ndeleted, and the has_key() and keys() methods are available. Keys\nand values must always be strings.

\n

The following example records some hostnames and a corresponding title, and\nthen prints out the contents of the database:

\n
import anydbm\n\n# Open database, creating it if necessary.\ndb = anydbm.open('cache', 'c')\n\n# Record some values\ndb['www.python.org'] = 'Python Website'\ndb['www.cnn.com'] = 'Cable News Network'\n\n# Loop through contents.  Other dictionary methods\n# such as .keys(), .values() also work.\nfor k, v in db.iteritems():\n    print k, '\\t', v\n\n# Storing a non-string key or value will raise an exception (most\n# likely a TypeError).\ndb['www.yahoo.com'] = 4\n\n# Close when done.\ndb.close()\n
\n
\n
\n

See also

\n
\n
Module dbhash
\n
BSD db database interface.
\n
Module dbm
\n
Standard Unix database interface.
\n
Module dumbdbm
\n
Portable implementation of the dbm interface.
\n
Module gdbm
\n
GNU database interface, based on the dbm interface.
\n
Module shelve
\n
General object persistence built on top of the Python dbm interface.
\n
Module whichdb
\n
Utility module used to determine the type of an existing database.
\n
\n
\n
", "searchableItems": [ { "name": "anydbm.open", "domId": "anydbm_anydbm.open" } ] }, { "url": "http://docs.python.org/library/shelve.html", "title": "shelve", "html": "
\n

11.4. shelve — Python object persistence

\n

Source code: Lib/shelve.py

\n
\n

A “shelf” is a persistent, dictionary-like object. The difference with “dbm”\ndatabases is that the values (not the keys!) in a shelf can be essentially\narbitrary Python objects — anything that the pickle module can handle.\nThis includes most class instances, recursive data types, and objects containing\nlots of shared sub-objects. The keys are ordinary strings.

\n
\n
\nshelve.open(filename[, flag='c'[, protocol=None[, writeback=False]]])
\n

Open a persistent dictionary. The filename specified is the base filename for\nthe underlying database. As a side-effect, an extension may be added to the\nfilename and more than one file may be created. By default, the underlying\ndatabase file is opened for reading and writing. The optional flag parameter\nhas the same interpretation as the flag parameter of anydbm.open().

\n

By default, version 0 pickles are used to serialize values. The version of the\npickle protocol can be specified with the protocol parameter.

\n

\nChanged in version 2.3: The protocol parameter was added.

\n

Because of Python semantics, a shelf cannot know when a mutable\npersistent-dictionary entry is modified. By default modified objects are\nwritten only when assigned to the shelf (see Example). If the\noptional writeback parameter is set to True, all entries accessed are also\ncached in memory, and written back on sync() and\nclose(); this can make it handier to mutate mutable entries in\nthe persistent dictionary, but, if many entries are accessed, it can consume\nvast amounts of memory for the cache, and it can make the close operation\nvery slow since all accessed entries are written back (there is no way to\ndetermine which accessed entries are mutable, nor which ones were actually\nmutated).

\n

Like file objects, shelve objects should be closed explicitly to ensure\nthat the persistent data is flushed to disk.

\n

Since the shelve module stores objects using pickle, the same\nsecurity precautions apply. Accordingly, you should avoid loading a shelf\nfrom an untrusted source.

\n
\n\n

Shelf objects support all methods supported by dictionaries. This eases the\ntransition from dictionary based scripts to those requiring persistent storage.

\n

Two additional methods are supported:

\n
\n
\nShelf.sync()
\n
Write back all entries in the cache if the shelf was opened with writeback\nset to True. Also empty the cache and synchronize the persistent\ndictionary on disk, if feasible. This is called automatically when the shelf\nis closed with close().
\n\n
\n
\nShelf.close()
\n
Synchronize and close the persistent dict object. Operations on a closed\nshelf will fail with a ValueError.
\n\n
\n

See also

\n

Persistent dictionary recipe\nwith widely supported storage formats and having the speed of native\ndictionaries.

\n
\n
\n

11.4.1. Restrictions

\n
\n
\n\n
\n
\nclass shelve.Shelf(dict[, protocol=None[, writeback=False]])
\n

A subclass of UserDict.DictMixin which stores pickled values in the\ndict object.

\n

By default, version 0 pickles are used to serialize values. The version of the\npickle protocol can be specified with the protocol parameter. See the\npickle documentation for a discussion of the pickle protocols.

\n

\nChanged in version 2.3: The protocol parameter was added.

\n

If the writeback parameter is True, the object will hold a cache of all\nentries accessed and write them back to the dict at sync and close times.\nThis allows natural operations on mutable entries, but can consume much more\nmemory and make sync and close take a long time.

\n
\n\n
\n
\nclass shelve.BsdDbShelf(dict[, protocol=None[, writeback=False]])
\n
A subclass of Shelf which exposes first(), next(),\nprevious(), last() and set_location() which are available in\nthe bsddb module but not in other database modules. The dict object\npassed to the constructor must support those methods. This is generally\naccomplished by calling one of bsddb.hashopen(), bsddb.btopen() or\nbsddb.rnopen(). The optional protocol and writeback parameters have\nthe same interpretation as for the Shelf class.
\n\n
\n
\nclass shelve.DbfilenameShelf(filename[, flag='c'[, protocol=None[, writeback=False]]])
\n
A subclass of Shelf which accepts a filename instead of a dict-like\nobject. The underlying file will be opened using anydbm.open(). By\ndefault, the file will be created and opened for both read and write. The\noptional flag parameter has the same interpretation as for the open()\nfunction. The optional protocol and writeback parameters have the same\ninterpretation as for the Shelf class.
\n\n
\n
\n

11.4.2. Example

\n

To summarize the interface (key is a string, data is an arbitrary\nobject):

\n
import shelve\n\nd = shelve.open(filename) # open -- file may get suffix added by low-level\n                          # library\n\nd[key] = data   # store data at key (overwrites old data if\n                # using an existing key)\ndata = d[key]   # retrieve a COPY of data at key (raise KeyError if no\n                # such key)\ndel d[key]      # delete data stored at key (raises KeyError\n                # if no such key)\nflag = d.has_key(key)   # true if the key exists\nklist = d.keys() # a list of all existing keys (slow!)\n\n# as d was opened WITHOUT writeback=True, beware:\nd['xx'] = range(4)  # this works as expected, but...\nd['xx'].append(5)   # *this doesn't!* -- d['xx'] is STILL range(4)!\n\n# having opened d without writeback=True, you need to code carefully:\ntemp = d['xx']      # extracts the copy\ntemp.append(5)      # mutates the copy\nd['xx'] = temp      # stores the copy right back, to persist it\n\n# or, d=shelve.open(filename,writeback=True) would let you just code\n# d['xx'].append(5) and have it work as expected, BUT it would also\n# consume more memory and make the d.close() operation slower.\n\nd.close()       # close it\n
\n
\n
\n

See also

\n
\n
Module anydbm
\n
Generic interface to dbm-style databases.
\n
Module bsddb
\n
BSD db database interface.
\n
Module dbhash
\n
Thin layer around the bsddb which provides an open()\nfunction like the other database modules.
\n
Module dbm
\n
Standard Unix database interface.
\n
Module dumbdbm
\n
Portable implementation of the dbm interface.
\n
Module gdbm
\n
GNU database interface, based on the dbm interface.
\n
Module pickle
\n
Object serialization used by shelve.
\n
Module cPickle
\n
High-performance version of pickle.
\n
\n
\n
\n
", "searchableItems": [ { "name": "shelve.BsdDbShelf", "domId": "shelve_shelve.BsdDbShelf" }, { "name": "shelve.DbfilenameShelf", "domId": "shelve_shelve.DbfilenameShelf" }, { "name": "shelve.open", "domId": "shelve_shelve.open" }, { "name": "shelve.Shelf", "domId": "shelve_shelve.Shelf" }, { "name": "shelve.Shelf.close", "domId": "shelve_shelve.Shelf.close" }, { "name": "shelve.Shelf.sync", "domId": "shelve_shelve.Shelf.sync" } ] }, { "url": "http://docs.python.org/library/pickle.html", "title": "pickle", "html": "
\n

11.1. pickle — Python object serialization

\n

The pickle module implements a fundamental, but powerful algorithm for\nserializing and de-serializing a Python object structure. “Pickling” is the\nprocess whereby a Python object hierarchy is converted into a byte stream, and\n“unpickling” is the inverse operation, whereby a byte stream is converted back\ninto an object hierarchy. Pickling (and unpickling) is alternatively known as\n“serialization”, “marshalling,” [1] or “flattening”, however, to avoid\nconfusion, the terms used here are “pickling” and “unpickling”.

\n

This documentation describes both the pickle module and the\ncPickle module.

\n
\n

Warning

\n

The pickle module is not intended to be secure against erroneous or\nmaliciously constructed data. Never unpickle data received from an untrusted\nor unauthenticated source.

\n
\n
\n

11.1.1. Relationship to other Python modules

\n

The pickle module has an optimized cousin called the cPickle\nmodule. As its name implies, cPickle is written in C, so it can be up to\n1000 times faster than pickle. However it does not support subclassing\nof the Pickler() and Unpickler() classes, because in cPickle\nthese are functions, not classes. Most applications have no need for this\nfunctionality, and can benefit from the improved performance of cPickle.\nOther than that, the interfaces of the two modules are nearly identical; the\ncommon interface is described in this manual and differences are pointed out\nwhere necessary. In the following discussions, we use the term “pickle” to\ncollectively describe the pickle and cPickle modules.

\n

The data streams the two modules produce are guaranteed to be interchangeable.

\n

Python has a more primitive serialization module called marshal, but in\ngeneral pickle should always be the preferred way to serialize Python\nobjects. marshal exists primarily to support Python’s .pyc\nfiles.

\n

The pickle module differs from marshal in several significant ways:

\n\n

Note that serialization is a more primitive notion than persistence; although\npickle reads and writes file objects, it does not handle the issue of\nnaming persistent objects, nor the (even more complicated) issue of concurrent\naccess to persistent objects. The pickle module can transform a complex\nobject into a byte stream and it can transform the byte stream into an object\nwith the same internal structure. Perhaps the most obvious thing to do with\nthese byte streams is to write them onto a file, but it is also conceivable to\nsend them across a network or store them in a database. The module\nshelve provides a simple interface to pickle and unpickle objects on\nDBM-style database files.

\n
\n
\n

11.1.2. Data stream format

\n

The data format used by pickle is Python-specific. This has the\nadvantage that there are no restrictions imposed by external standards such as\nXDR (which can’t represent pointer sharing); however it means that non-Python\nprograms may not be able to reconstruct pickled Python objects.

\n

By default, the pickle data format uses a printable ASCII representation.\nThis is slightly more voluminous than a binary representation. The big\nadvantage of using printable ASCII (and of some other characteristics of\npickle‘s representation) is that for debugging or recovery purposes it is\npossible for a human to read the pickled file with a standard text editor.

\n

There are currently 3 different protocols which can be used for pickling.

\n\n

Refer to PEP 307 for more information.

\n

If a protocol is not specified, protocol 0 is used. If protocol is specified\nas a negative value or HIGHEST_PROTOCOL, the highest protocol version\navailable will be used.

\n

\nChanged in version 2.3: Introduced the protocol parameter.

\n

A binary format, which is slightly more efficient, can be chosen by specifying a\nprotocol version >= 1.

\n
\n
\n

11.1.3. Usage

\n

To serialize an object hierarchy, you first create a pickler, then you call the\npickler’s dump() method. To de-serialize a data stream, you first create\nan unpickler, then you call the unpickler’s load() method. The\npickle module provides the following constant:

\n
\n
\npickle.HIGHEST_PROTOCOL
\n

The highest protocol version available. This value can be passed as a\nprotocol value.

\n

\nNew in version 2.3.

\n
\n\n
\n

Note

\n

Be sure to always open pickle files created with protocols >= 1 in binary mode.\nFor the old ASCII-based pickle protocol 0 you can use either text mode or binary\nmode as long as you stay consistent.

\n

A pickle file written with protocol 0 in binary mode will contain lone linefeeds\nas line terminators and therefore will look “funny” when viewed in Notepad or\nother editors which do not support this format.

\n
\n

The pickle module provides the following functions to make the pickling\nprocess more convenient:

\n
\n
\npickle.dump(obj, file[, protocol])
\n

Write a pickled representation of obj to the open file object file. This is\nequivalent to Pickler(file, protocol).dump(obj).

\n

If the protocol parameter is omitted, protocol 0 is used. If protocol is\nspecified as a negative value or HIGHEST_PROTOCOL, the highest protocol\nversion will be used.

\n

\nChanged in version 2.3: Introduced the protocol parameter.

\n

file must have a write() method that accepts a single string argument.\nIt can thus be a file object opened for writing, a StringIO object, or\nany other custom object that meets this interface.

\n
\n\n
\n
\npickle.load(file)
\n

Read a string from the open file object file and interpret it as a pickle data\nstream, reconstructing and returning the original object hierarchy. This is\nequivalent to Unpickler(file).load().

\n

file must have two methods, a read() method that takes an integer\nargument, and a readline() method that requires no arguments. Both\nmethods should return a string. Thus file can be a file object opened for\nreading, a StringIO object, or any other custom object that meets this\ninterface.

\n

This function automatically determines whether the data stream was written in\nbinary mode or not.

\n
\n\n
\n
\npickle.dumps(obj[, protocol])
\n

Return the pickled representation of the object as a string, instead of writing\nit to a file.

\n

If the protocol parameter is omitted, protocol 0 is used. If protocol is\nspecified as a negative value or HIGHEST_PROTOCOL, the highest protocol\nversion will be used.

\n

\nChanged in version 2.3: The protocol parameter was added.

\n
\n\n
\n
\npickle.loads(string)
\n
Read a pickled object hierarchy from a string. Characters in the string past\nthe pickled object’s representation are ignored.
\n\n

The pickle module also defines three exceptions:

\n
\n
\nexception pickle.PickleError
\n
A common base class for the other exceptions defined below. This inherits from\nException.
\n\n
\n
\nexception pickle.PicklingError
\n
This exception is raised when an unpicklable object is passed to the\ndump() method.
\n\n
\n
\nexception pickle.UnpicklingError
\n
This exception is raised when there is a problem unpickling an object. Note that\nother exceptions may also be raised during unpickling, including (but not\nnecessarily limited to) AttributeError, EOFError,\nImportError, and IndexError.
\n\n

The pickle module also exports two callables [2], Pickler and\nUnpickler:

\n
\n
\nclass pickle.Pickler(file[, protocol])
\n

This takes a file-like object to which it will write a pickle data stream.

\n

If the protocol parameter is omitted, protocol 0 is used. If protocol is\nspecified as a negative value or HIGHEST_PROTOCOL, the highest\nprotocol version will be used.

\n

\nChanged in version 2.3: Introduced the protocol parameter.

\n

file must have a write() method that accepts a single string argument.\nIt can thus be an open file object, a StringIO object, or any other\ncustom object that meets this interface.

\n

Pickler objects define one (or two) public methods:

\n
\n
\ndump(obj)
\n
Write a pickled representation of obj to the open file object given in the\nconstructor. Either the binary or ASCII format will be used, depending on the\nvalue of the protocol argument passed to the constructor.
\n\n
\n
\nclear_memo()
\n

Clears the pickler’s “memo”. The memo is the data structure that remembers\nwhich objects the pickler has already seen, so that shared or recursive objects\npickled by reference and not by value. This method is useful when re-using\npicklers.

\n
\n

Note

\n

Prior to Python 2.3, clear_memo() was only available on the picklers\ncreated by cPickle. In the pickle module, picklers have an\ninstance variable called memo which is a Python dictionary. So to clear\nthe memo for a pickle module pickler, you could do the following:

\n
mypickler.memo.clear()\n
\n
\n

Code that does not need to support older versions of Python should simply use\nclear_memo().

\n
\n
\n\n
\n\n

It is possible to make multiple calls to the dump() method of the same\nPickler instance. These must then be matched to the same number of\ncalls to the load() method of the corresponding Unpickler\ninstance. If the same object is pickled by multiple dump() calls, the\nload() will all yield references to the same object. [3]

\n

Unpickler objects are defined as:

\n
\n
\nclass pickle.Unpickler(file)
\n

This takes a file-like object from which it will read a pickle data stream.\nThis class automatically determines whether the data stream was written in\nbinary mode or not, so it does not need a flag as in the Pickler\nfactory.

\n

file must have two methods, a read() method that takes an integer\nargument, and a readline() method that requires no arguments. Both\nmethods should return a string. Thus file can be a file object opened for\nreading, a StringIO object, or any other custom object that meets this\ninterface.

\n

Unpickler objects have one (or two) public methods:

\n
\n
\nload()
\n

Read a pickled object representation from the open file object given in\nthe constructor, and return the reconstituted object hierarchy specified\ntherein.

\n

This method automatically determines whether the data stream was written\nin binary mode or not.

\n
\n\n
\n
\nnoload()
\n

This is just like load() except that it doesn’t actually create any\nobjects. This is useful primarily for finding what’s called “persistent\nids” that may be referenced in a pickle data stream. See section\nThe pickle protocol below for more details.

\n

Note: the noload() method is currently only available on\nUnpickler objects created with the cPickle module.\npickle module Unpicklers do not have the noload()\nmethod.

\n
\n\n
\n\n
\n
\n

11.1.4. What can be pickled and unpickled?

\n

The following types can be pickled:

\n\n

Attempts to pickle unpicklable objects will raise the PicklingError\nexception; when this happens, an unspecified number of bytes may have already\nbeen written to the underlying file. Trying to pickle a highly recursive data\nstructure may exceed the maximum recursion depth, a RuntimeError will be\nraised in this case. You can carefully raise this limit with\nsys.setrecursionlimit().

\n

Note that functions (built-in and user-defined) are pickled by “fully qualified”\nname reference, not by value. This means that only the function name is\npickled, along with the name of the module the function is defined in. Neither the\nfunction’s code, nor any of its function attributes are pickled. Thus the\ndefining module must be importable in the unpickling environment, and the module\nmust contain the named object, otherwise an exception will be raised. [4]

\n

Similarly, classes are pickled by named reference, so the same restrictions in\nthe unpickling environment apply. Note that none of the class’s code or data is\npickled, so in the following example the class attribute attr is not\nrestored in the unpickling environment:

\n
class Foo:\n    attr = 'a class attr'\n\npicklestring = pickle.dumps(Foo)\n
\n
\n

These restrictions are why picklable functions and classes must be defined in\nthe top level of a module.

\n

Similarly, when class instances are pickled, their class’s code and data are not\npickled along with them. Only the instance data are pickled. This is done on\npurpose, so you can fix bugs in a class or add methods to the class and still\nload objects that were created with an earlier version of the class. If you\nplan to have long-lived objects that will see many versions of a class, it may\nbe worthwhile to put a version number in the objects so that suitable\nconversions can be made by the class’s __setstate__() method.

\n
\n
\n

11.1.5. The pickle protocol

\n

This section describes the “pickling protocol” that defines the interface\nbetween the pickler/unpickler and the objects that are being serialized. This\nprotocol provides a standard way for you to define, customize, and control how\nyour objects are serialized and de-serialized. The description in this section\ndoesn’t cover specific customizations that you can employ to make the unpickling\nenvironment slightly safer from untrusted pickle data streams; see section\nSubclassing Unpicklers for more details.

\n
\n

11.1.5.1. Pickling and unpickling normal class instances

\n
\n
\nobject.__getinitargs__()
\n
When a pickled class instance is unpickled, its __init__() method is\nnormally not invoked. If it is desirable that the __init__() method\nbe called on unpickling, an old-style class can define a method\n__getinitargs__(), which should return a tuple containing the\narguments to be passed to the class constructor (__init__() for\nexample). The __getinitargs__() method is called at pickle time; the\ntuple it returns is incorporated in the pickle for the instance.
\n\n
\n
\nobject.__getnewargs__()
\n

New-style types can provide a __getnewargs__() method that is used for\nprotocol 2. Implementing this method is needed if the type establishes some\ninternal invariants when the instance is created, or if the memory allocation\nis affected by the values passed to the __new__() method for the type\n(as it is for tuples and strings). Instances of a new-style class\nC are created using

\n
obj = C.__new__(C, *args)\n
\n
\n

where args is the result of calling __getnewargs__() on the original\nobject; if there is no __getnewargs__(), an empty tuple is assumed.

\n
\n\n
\n
\nobject.__getstate__()
\n
Classes can further influence how their instances are pickled; if the class\ndefines the method __getstate__(), it is called and the return state is\npickled as the contents for the instance, instead of the contents of the\ninstance’s dictionary. If there is no __getstate__() method, the\ninstance’s __dict__ is pickled.
\n\n
\n
\nobject.__setstate__(state)
\n

Upon unpickling, if the class also defines the method __setstate__(),\nit is called with the unpickled state. [5] If there is no\n__setstate__() method, the pickled state must be a dictionary and its\nitems are assigned to the new instance’s dictionary. If a class defines both\n__getstate__() and __setstate__(), the state object needn’t be a\ndictionary and these methods can do what they want. [6]

\n
\n

Note

\n

For new-style classes, if __getstate__() returns a false\nvalue, the __setstate__() method will not be called.

\n
\n
\n\n
\n

Note

\n

At unpickling time, some methods like __getattr__(),\n__getattribute__(), or __setattr__() may be called upon the\ninstance. In case those methods rely on some internal invariant being\ntrue, the type should implement either __getinitargs__() or\n__getnewargs__() to establish such an invariant; otherwise, neither\n__new__() nor __init__() will be called.

\n
\n
\n
\n

11.1.5.2. Pickling and unpickling extension types

\n
\n
\nobject.__reduce__()
\n

When the Pickler encounters an object of a type it knows nothing\nabout — such as an extension type — it looks in two places for a hint of\nhow to pickle it. One alternative is for the object to implement a\n__reduce__() method. If provided, at pickling time __reduce__()\nwill be called with no arguments, and it must return either a string or a\ntuple.

\n

If a string is returned, it names a global variable whose contents are\npickled as normal. The string returned by __reduce__() should be the\nobject’s local name relative to its module; the pickle module searches the\nmodule namespace to determine the object’s module.

\n

When a tuple is returned, it must be between two and five elements long.\nOptional elements can either be omitted, or None can be provided as their\nvalue. The contents of this tuple are pickled as normal and used to\nreconstruct the object at unpickling time. The semantics of each element\nare:

\n
    \n
  • A callable object that will be called to create the initial version of the\nobject. The next element of the tuple will provide arguments for this\ncallable, and later elements provide additional state information that will\nsubsequently be used to fully reconstruct the pickled data.

    \n

    In the unpickling environment this object must be either a class, a\ncallable registered as a “safe constructor” (see below), or it must have an\nattribute __safe_for_unpickling__ with a true value. Otherwise, an\nUnpicklingError will be raised in the unpickling environment. Note\nthat as usual, the callable itself is pickled by name.

    \n
  • \n
  • A tuple of arguments for the callable object.

    \n

    \nChanged in version 2.5: Formerly, this argument could also be None.

    \n
  • \n
  • Optionally, the object’s state, which will be passed to the object’s\n__setstate__() method as described in section Pickling and unpickling normal class instances. If\nthe object has no __setstate__() method, then, as above, the value\nmust be a dictionary and it will be added to the object’s __dict__.

    \n
  • \n
  • Optionally, an iterator (and not a sequence) yielding successive list\nitems. These list items will be pickled, and appended to the object using\neither obj.append(item) or obj.extend(list_of_items). This is\nprimarily used for list subclasses, but may be used by other classes as\nlong as they have append() and extend() methods with the\nappropriate signature. (Whether append() or extend() is used\ndepends on which pickle protocol version is used as well as the number of\nitems to append, so both must be supported.)

    \n
  • \n
  • Optionally, an iterator (not a sequence) yielding successive dictionary\nitems, which should be tuples of the form (key, value). These items\nwill be pickled and stored to the object using obj[key] = value. This\nis primarily used for dictionary subclasses, but may be used by other\nclasses as long as they implement __setitem__().

    \n
  • \n
\n
\n\n
\n
\nobject.__reduce_ex__(protocol)
\n

It is sometimes useful to know the protocol version when implementing\n__reduce__(). This can be done by implementing a method named\n__reduce_ex__() instead of __reduce__(). __reduce_ex__(),\nwhen it exists, is called in preference over __reduce__() (you may\nstill provide __reduce__() for backwards compatibility). The\n__reduce_ex__() method will be called with a single integer argument,\nthe protocol version.

\n

The object class implements both __reduce__() and\n__reduce_ex__(); however, if a subclass overrides __reduce__()\nbut not __reduce_ex__(), the __reduce_ex__() implementation\ndetects this and calls __reduce__().

\n
\n\n

An alternative to implementing a __reduce__() method on the object to be\npickled, is to register the callable with the copy_reg module. This\nmodule provides a way for programs to register “reduction functions” and\nconstructors for user-defined types. Reduction functions have the same\nsemantics and interface as the __reduce__() method described above, except\nthat they are called with a single argument, the object to be pickled.

\n

The registered constructor is deemed a “safe constructor” for purposes of\nunpickling as described above.

\n
\n
\n

11.1.5.3. Pickling and unpickling external objects

\n

For the benefit of object persistence, the pickle module supports the\nnotion of a reference to an object outside the pickled data stream. Such\nobjects are referenced by a “persistent id”, which is just an arbitrary string\nof printable ASCII characters. The resolution of such names is not defined by\nthe pickle module; it will delegate this resolution to user defined\nfunctions on the pickler and unpickler. [7]

\n

To define external persistent id resolution, you need to set the\npersistent_id attribute of the pickler object and the\npersistent_load attribute of the unpickler object.

\n

To pickle objects that have an external persistent id, the pickler must have a\ncustom persistent_id() method that takes an object as an argument and\nreturns either None or the persistent id for that object. When None is\nreturned, the pickler simply pickles the object as normal. When a persistent id\nstring is returned, the pickler will pickle that string, along with a marker so\nthat the unpickler will recognize the string as a persistent id.

\n

To unpickle external objects, the unpickler must have a custom\npersistent_load() function that takes a persistent id string and returns\nthe referenced object.

\n

Here’s a silly example that might shed more light:

\n
import pickle\nfrom cStringIO import StringIO\n\nsrc = StringIO()\np = pickle.Pickler(src)\n\ndef persistent_id(obj):\n    if hasattr(obj, 'x'):\n        return 'the value %d'  obj.x\n    else:\n        return None\n\np.persistent_id = persistent_id\n\nclass Integer:\n    def __init__(self, x):\n        self.x = x\n    def __str__(self):\n        return 'My name is integer %d'  self.x\n\ni = Integer(7)\nprint i\np.dump(i)\n\ndatastream = src.getvalue()\nprint repr(datastream)\ndst = StringIO(datastream)\n\nup = pickle.Unpickler(dst)\n\nclass FancyInteger(Integer):\n    def __str__(self):\n        return 'I am the integer %d'  self.x\n\ndef persistent_load(persid):\n    if persid.startswith('the value '):\n        value = int(persid.split()[2])\n        return FancyInteger(value)\n    else:\n        raise pickle.UnpicklingError, 'Invalid persistent id'\n\nup.persistent_load = persistent_load\n\nj = up.load()\nprint j\n
\n
\n

In the cPickle module, the unpickler’s persistent_load attribute\ncan also be set to a Python list, in which case, when the unpickler reaches a\npersistent id, the persistent id string will simply be appended to this list.\nThis functionality exists so that a pickle data stream can be “sniffed” for\nobject references without actually instantiating all the objects in a pickle.\n[8] Setting persistent_load to a list is usually used in conjunction\nwith the noload() method on the Unpickler.

\n
\n
\n
\n

11.1.6. Subclassing Unpicklers

\n

By default, unpickling will import any class that it finds in the pickle data.\nYou can control exactly what gets unpickled and what gets called by customizing\nyour unpickler. Unfortunately, exactly how you do this is different depending\non whether you’re using pickle or cPickle. [9]

\n

In the pickle module, you need to derive a subclass from\nUnpickler, overriding the load_global() method.\nload_global() should read two lines from the pickle data stream where the\nfirst line will the name of the module containing the class and the second line\nwill be the name of the instance’s class. It then looks up the class, possibly\nimporting the module and digging out the attribute, then it appends what it\nfinds to the unpickler’s stack. Later on, this class will be assigned to the\n__class__ attribute of an empty class, as a way of magically creating an\ninstance without calling its class’s __init__(). Your job (should you\nchoose to accept it), would be to have load_global() push onto the\nunpickler’s stack, a known safe version of any class you deem safe to unpickle.\nIt is up to you to produce such a class. Or you could raise an error if you\nwant to disallow all unpickling of instances. If this sounds like a hack,\nyou’re right. Refer to the source code to make this work.

\n

Things are a little cleaner with cPickle, but not by much. To control\nwhat gets unpickled, you can set the unpickler’s find_global attribute\nto a function or None. If it is None then any attempts to unpickle\ninstances will raise an UnpicklingError. If it is a function, then it\nshould accept a module name and a class name, and return the corresponding class\nobject. It is responsible for looking up the class and performing any necessary\nimports, and it may raise an error to prevent instances of the class from being\nunpickled.

\n

The moral of the story is that you should be really careful about the source of\nthe strings your application unpickles.

\n
\n
\n

11.1.7. Example

\n

For the simplest code, use the dump() and load() functions. Note\nthat a self-referencing list is pickled and restored correctly.

\n
import pickle\n\ndata1 = {'a': [1, 2.0, 3, 4+6j],\n         'b': ('string', u'Unicode string'),\n         'c': None}\n\nselfref_list = [1, 2, 3]\nselfref_list.append(selfref_list)\n\noutput = open('data.pkl', 'wb')\n\n# Pickle dictionary using protocol 0.\npickle.dump(data1, output)\n\n# Pickle the list using the highest protocol available.\npickle.dump(selfref_list, output, -1)\n\noutput.close()\n
\n
\n

The following example reads the resulting pickled data. When reading a\npickle-containing file, you should open the file in binary mode because you\ncan’t be sure if the ASCII or binary format was used.

\n
import pprint, pickle\n\npkl_file = open('data.pkl', 'rb')\n\ndata1 = pickle.load(pkl_file)\npprint.pprint(data1)\n\ndata2 = pickle.load(pkl_file)\npprint.pprint(data2)\n\npkl_file.close()\n
\n
\n

Here’s a larger example that shows how to modify pickling behavior for a class.\nThe TextReader class opens a text file, and returns the line number and\nline contents each time its readline() method is called. If a\nTextReader instance is pickled, all attributes except the file object\nmember are saved. When the instance is unpickled, the file is reopened, and\nreading resumes from the last location. The __setstate__() and\n__getstate__() methods are used to implement this behavior.

\n
#!/usr/local/bin/python\n\nclass TextReader:\n    """Print and number lines in a text file."""\n    def __init__(self, file):\n        self.file = file\n        self.fh = open(file)\n        self.lineno = 0\n\n    def readline(self):\n        self.lineno = self.lineno + 1\n        line = self.fh.readline()\n        if not line:\n            return None\n        if line.endswith("\\n"):\n            line = line[:-1]\n        return "%d: %s"  (self.lineno, line)\n\n    def __getstate__(self):\n        odict = self.__dict__.copy() # copy the dict since we change it\n        del odict['fh']              # remove filehandle entry\n        return odict\n\n    def __setstate__(self, dict):\n        fh = open(dict['file'])      # reopen file\n        count = dict['lineno']       # read from file...\n        while count:                 # until line count is restored\n            fh.readline()\n            count = count - 1\n        self.__dict__.update(dict)   # update attributes\n        self.fh = fh                 # save the file object\n
\n
\n

A sample usage might be something like this:

\n
>>> import TextReader\n>>> obj = TextReader.TextReader("TextReader.py")\n>>> obj.readline()\n'1: #!/usr/local/bin/python'\n>>> obj.readline()\n'2: '\n>>> obj.readline()\n'3: class TextReader:'\n>>> import pickle\n>>> pickle.dump(obj, open('save.p', 'wb'))\n
\n
\n

If you want to see that pickle works across Python processes, start\nanother Python session, before continuing. What follows can happen from either\nthe same process or a new process.

\n
>>> import pickle\n>>> reader = pickle.load(open('save.p', 'rb'))\n>>> reader.readline()\n'4:     """Print and number lines in a text file."""'\n
\n
\n
\n

See also

\n
\n
Module copy_reg
\n
Pickle interface constructor registration for extension types.
\n
Module shelve
\n
Indexed databases of objects; uses pickle.
\n
Module copy
\n
Shallow and deep object copying.
\n
Module marshal
\n
High-performance serialization of built-in types.
\n
\n
\n
\n
\n

11.2. cPickle — A faster pickle

\n

The cPickle module supports serialization and de-serialization of Python\nobjects, providing an interface and functionality nearly identical to the\npickle module. There are several differences, the most important being\nperformance and subclassability.

\n

First, cPickle can be up to 1000 times faster than pickle because\nthe former is implemented in C. Second, in the cPickle module the\ncallables Pickler() and Unpickler() are functions, not classes.\nThis means that you cannot use them to derive custom pickling and unpickling\nsubclasses. Most applications have no need for this functionality and should\nbenefit from the greatly improved performance of the cPickle module.

\n

The pickle data stream produced by pickle and cPickle are\nidentical, so it is possible to use pickle and cPickle\ninterchangeably with existing pickles. [10]

\n

There are additional minor differences in API between cPickle and\npickle, however for most applications, they are interchangeable. More\ndocumentation is provided in the pickle module documentation, which\nincludes a list of the documented differences.

\n

Footnotes

\n\n\n\n\n\n
[1]Don’t confuse this with the marshal module
\n\n\n\n\n\n
[2]In the pickle module these callables are classes, which you could\nsubclass to customize the behavior. However, in the cPickle module these\ncallables are factory functions and so cannot be subclassed. One common reason\nto subclass is to control what objects can actually be unpickled. See section\nSubclassing Unpicklers for more details.
\n\n\n\n\n\n
[3]Warning: this is intended for pickling multiple objects without intervening\nmodifications to the objects or their parts. If you modify an object and then\npickle it again using the same Pickler instance, the object is not\npickled again — a reference to it is pickled and the Unpickler will\nreturn the old value, not the modified one. There are two problems here: (1)\ndetecting changes, and (2) marshalling a minimal set of changes. Garbage\nCollection may also become a problem here.
\n\n\n\n\n\n
[4]The exception raised will likely be an ImportError or an\nAttributeError but it could be something else.
\n\n\n\n\n\n
[5]These methods can also be used to implement copying class instances.
\n\n\n\n\n\n
[6]This protocol is also used by the shallow and deep copying operations defined in\nthe copy module.
\n\n\n\n\n\n
[7]The actual mechanism for associating these user defined functions is slightly\ndifferent for pickle and cPickle. The description given here\nworks the same for both implementations. Users of the pickle module\ncould also use subclassing to effect the same results, overriding the\npersistent_id() and persistent_load() methods in the derived\nclasses.
\n\n\n\n\n\n
[8]We’ll leave you with the image of Guido and Jim sitting around sniffing pickles\nin their living rooms.
\n\n\n\n\n\n
[9]A word of caution: the mechanisms described here use internal attributes and\nmethods, which are subject to change in future versions of Python. We intend to\nsomeday provide a common interface for controlling this behavior, which will\nwork in either pickle or cPickle.
\n\n\n\n\n\n
[10]Since the pickle data format is actually a tiny stack-oriented programming\nlanguage, and some freedom is taken in the encodings of certain objects, it is\npossible that the two modules produce different data streams for the same input\nobjects. However it is guaranteed that they will always be able to read each\nother’s data streams.
\n
", "searchableItems": [ { "name": "object.__getinitargs__", "domId": "pickle_object.__getinitargs__" }, { "name": "object.__getnewargs__", "domId": "pickle_object.__getnewargs__" }, { "name": "object.__getstate__", "domId": "pickle_object.__getstate__" }, { "name": "object.__reduce__", "domId": "pickle_object.__reduce__" }, { "name": "object.__reduce_ex__", "domId": "pickle_object.__reduce_ex__" }, { "name": "object.__setstate__", "domId": "pickle_object.__setstate__" }, { "name": "pickle.dump", "domId": "pickle_pickle.dump" }, { "name": "pickle.dumps", "domId": "pickle_pickle.dumps" }, { "name": "pickle.load", "domId": "pickle_pickle.load" }, { "name": "pickle.loads", "domId": "pickle_pickle.loads" }, { "name": "pickle.Pickler", "domId": "pickle_pickle.Pickler" }, { "name": "pickle.Pickler.clear_memo", "domId": "pickle_pickle.Pickler.clear_memo" }, { "name": "pickle.Pickler.dump", "domId": "pickle_pickle.Pickler.dump" }, { "name": "pickle.Unpickler", "domId": "pickle_pickle.Unpickler" }, { "name": "pickle.Unpickler.load", "domId": "pickle_pickle.Unpickler.load" }, { "name": "pickle.Unpickler.noload", "domId": "pickle_pickle.Unpickler.noload" } ] }, { "url": "http://docs.python.org/library/whichdb.html", "title": "whichdb", "html": "
\n

11.7. whichdb — Guess which DBM module created a database

\n
\n

Note

\n

The whichdb module’s only function has been put into the dbm\nmodule in Python 3.0. The 2to3 tool will automatically adapt imports\nwhen converting your sources to 3.0.

\n
\n

The single function in this module attempts to guess which of the several simple\ndatabase modules available–dbm, gdbm, or dbhash–should be used to open a given file.

\n
\n
\nwhichdb.whichdb(filename)
\n
Returns one of the following values: None if the file can’t be opened\nbecause it’s unreadable or doesn’t exist; the empty string ('') if the\nfile’s format can’t be guessed; or a string containing the required module name,\nsuch as 'dbm' or 'gdbm'.
\n\n
", "searchableItems": [ { "name": "whichdb.whichdb", "domId": "whichdb_whichdb.whichdb" } ] }, { "url": "http://docs.python.org/library/dbm.html", "title": "dbm", "html": "
\n

11.8. dbm — Simple “database” interface

\n

Platforms: Unix

\n
\n

Note

\n

The dbm module has been renamed to dbm.ndbm in Python 3.0. The\n2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n

The dbm module provides an interface to the Unix “(n)dbm” library. Dbm\nobjects behave like mappings (dictionaries), except that keys and values are\nalways strings. Printing a dbm object doesn’t print the keys and values, and the\nitems() and values() methods are not supported.

\n

This module can be used with the “classic” ndbm interface, the BSD DB\ncompatibility interface, or the GNU GDBM compatibility interface. On Unix, the\nconfigure script will attempt to locate the appropriate header file\nto simplify building this module.

\n

The module defines the following:

\n
\n
\nexception dbm.error
\n
Raised on dbm-specific errors, such as I/O errors. KeyError is raised for\ngeneral mapping errors like specifying an incorrect key.
\n\n
\n
\ndbm.library
\n
Name of the ndbm implementation library used.
\n\n
\n
\ndbm.open(filename[, flag[, mode]])
\n

Open a dbm database and return a dbm object. The filename argument is the\nname of the database file (without the .dir or .pag extensions;\nnote that the BSD DB implementation of the interface will append the extension\n.db and only create one file).

\n

The optional flag argument must be one of these values:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueMeaning
'r'Open existing database for reading only\n(default)
'w'Open existing database for reading and\nwriting
'c'Open database for reading and writing,\ncreating it if it doesn’t exist
'n'Always create a new, empty database, open\nfor reading and writing
\n

The optional mode argument is the Unix mode of the file, used only when the\ndatabase has to be created. It defaults to octal 0666 (and will be\nmodified by the prevailing umask).

\n
\n\n
\n

See also

\n
\n
Module anydbm
\n
Generic interface to dbm-style databases.
\n
Module gdbm
\n
Similar interface to the GNU GDBM library.
\n
Module whichdb
\n
Utility module used to determine the type of an existing database.
\n
\n
\n
", "searchableItems": [ { "name": "dbm.open", "domId": "dbm_dbm.open" } ] }, { "url": "http://docs.python.org/library/gdbm.html", "title": "gdbm", "html": "
\n

11.9. gdbm — GNU’s reinterpretation of dbm

\n

Platforms: Unix

\n
\n

Note

\n

The gdbm module has been renamed to dbm.gnu in Python 3.0. The\n2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n

This module is quite similar to the dbm module, but uses gdbm instead\nto provide some additional functionality. Please note that the file formats\ncreated by gdbm and dbm are incompatible.

\n

The gdbm module provides an interface to the GNU DBM library. gdbm\nobjects behave like mappings (dictionaries), except that keys and values are\nalways strings. Printing a gdbm object doesn’t print the keys and values,\nand the items() and values() methods are not supported.

\n

The module defines the following constant and functions:

\n
\n
\nexception gdbm.error
\n
Raised on gdbm-specific errors, such as I/O errors. KeyError is\nraised for general mapping errors like specifying an incorrect key.
\n\n
\n
\ngdbm.open(filename[, flag[, mode]])
\n

Open a gdbm database and return a gdbm object. The filename argument\nis the name of the database file.

\n

The optional flag argument can be:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueMeaning
'r'Open existing database for reading only\n(default)
'w'Open existing database for reading and\nwriting
'c'Open database for reading and writing,\ncreating it if it doesn’t exist
'n'Always create a new, empty database, open\nfor reading and writing
\n

The following additional characters may be appended to the flag to control\nhow the database is opened:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueMeaning
'f'Open the database in fast mode. Writes\nto the database will not be synchronized.
's'Synchronized mode. This will cause changes\nto the database to be immediately written\nto the file.
'u'Do not lock database.
\n

Not all flags are valid for all versions of gdbm. The module constant\nopen_flags is a string of supported flag characters. The exception\nerror is raised if an invalid flag is specified.

\n

The optional mode argument is the Unix mode of the file, used only when the\ndatabase has to be created. It defaults to octal 0666.

\n
\n\n

In addition to the dictionary-like methods, gdbm objects have the following\nmethods:

\n
\n
\ngdbm.firstkey()
\n
It’s possible to loop over every key in the database using this method and the\nnextkey() method. The traversal is ordered by gdbm‘s internal hash\nvalues, and won’t be sorted by the key values. This method returns the starting\nkey.
\n\n
\n
\ngdbm.nextkey(key)
\n

Returns the key that follows key in the traversal. The following code prints\nevery key in the database db, without having to create a list in memory that\ncontains them all:

\n
k = db.firstkey()\nwhile k != None:\n    print k\n    k = db.nextkey(k)\n
\n
\n
\n\n
\n
\ngdbm.reorganize()
\n
If you have carried out a lot of deletions and would like to shrink the space\nused by the gdbm file, this routine will reorganize the database. gdbm\nwill not shorten the length of a database file except by using this\nreorganization; otherwise, deleted file space will be kept and reused as new\n(key, value) pairs are added.
\n\n
\n
\ngdbm.sync()
\n
When the database has been opened in fast mode, this method forces any\nunwritten data to be written to the disk.
\n\n
\n

See also

\n
\n
Module anydbm
\n
Generic interface to dbm-style databases.
\n
Module whichdb
\n
Utility module used to determine the type of an existing database.
\n
\n
\n
", "searchableItems": [ { "name": "gdbm.firstkey", "domId": "gdbm_gdbm.firstkey" }, { "name": "gdbm.nextkey", "domId": "gdbm_gdbm.nextkey" }, { "name": "gdbm.open", "domId": "gdbm_gdbm.open" }, { "name": "gdbm.reorganize", "domId": "gdbm_gdbm.reorganize" }, { "name": "gdbm.sync", "domId": "gdbm_gdbm.sync" } ] }, { "url": "http://docs.python.org/library/dumbdbm.html", "title": "dumbdbm", "html": "
\n

11.12. dumbdbm — Portable DBM implementation

\n
\n

Note

\n

The dumbdbm module has been renamed to dbm.dumb in Python 3.0.\nThe 2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n
\n

Note

\n

The dumbdbm module is intended as a last resort fallback for the\nanydbm module when no more robust module is available. The dumbdbm\nmodule is not written for speed and is not nearly as heavily used as the other\ndatabase modules.

\n
\n

The dumbdbm module provides a persistent dictionary-like interface which\nis written entirely in Python. Unlike other modules such as gdbm and\nbsddb, no external library is required. As with other persistent\nmappings, the keys and values must always be strings.

\n

The module defines the following:

\n
\n
\nexception dumbdbm.error
\n
Raised on dumbdbm-specific errors, such as I/O errors. KeyError is\nraised for general mapping errors like specifying an incorrect key.
\n\n
\n
\ndumbdbm.open(filename[, flag[, mode]])
\n

Open a dumbdbm database and return a dumbdbm object. The filename argument is\nthe basename of the database file (without any specific extensions). When a\ndumbdbm database is created, files with .dat and .dir extensions\nare created.

\n

The optional flag argument is currently ignored; the database is always opened\nfor update, and will be created if it does not exist.

\n

The optional mode argument is the Unix mode of the file, used only when the\ndatabase has to be created. It defaults to octal 0666 (and will be modified\nby the prevailing umask).

\n

\nChanged in version 2.2: The mode argument was ignored in earlier versions.

\n
\n\n
\n

See also

\n
\n
Module anydbm
\n
Generic interface to dbm-style databases.
\n
Module dbm
\n
Similar interface to the DBM/NDBM library.
\n
Module gdbm
\n
Similar interface to the GNU GDBM library.
\n
Module shelve
\n
Persistence module which stores non-string data.
\n
Module whichdb
\n
Utility module used to determine the type of an existing database.
\n
\n
\n
\n

11.12.1. Dumbdbm Objects

\n

In addition to the methods provided by the UserDict.DictMixin class,\ndumbdbm objects provide the following methods.

\n
\n
\ndumbdbm.sync()
\n
Synchronize the on-disk directory and data files. This method is called by the\nsync() method of Shelve objects.
\n\n
\n
", "searchableItems": [ { "name": "dumbdbm.dumbdbm.sync", "domId": "dumbdbm_dumbdbm.dumbdbm.sync" }, { "name": "dumbdbm.open", "domId": "dumbdbm_dumbdbm.open" } ] }, { "url": "http://docs.python.org/library/dbhash.html", "title": "dbhash", "html": "
\n

11.10. dbhash — DBM-style interface to the BSD database library

\n

\nDeprecated since version 2.6: The dbhash module has been deprecated for removal in Python 3.0.

\n

The dbhash module provides a function to open databases using the BSD\ndb library. This module mirrors the interface of the other Python database\nmodules that provide access to DBM-style databases. The bsddb module is\nrequired to use dbhash.

\n

This module provides an exception and a function:

\n
\n
\nexception dbhash.error
\n
Exception raised on database errors other than KeyError. It is a synonym\nfor bsddb.error.
\n\n
\n
\ndbhash.open(path[, flag[, mode]])
\n

Open a db database and return the database object. The path argument is\nthe name of the database file.

\n

The flag argument can be:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueMeaning
'r'Open existing database for reading only\n(default)
'w'Open existing database for reading and\nwriting
'c'Open database for reading and writing,\ncreating it if it doesn’t exist
'n'Always create a new, empty database, open\nfor reading and writing
\n

For platforms on which the BSD db library supports locking, an 'l'\ncan be appended to indicate that locking should be used.

\n

The optional mode parameter is used to indicate the Unix permission bits that\nshould be set if a new database must be created; this will be masked by the\ncurrent umask value for the process.

\n
\n\n
\n

See also

\n
\n
Module anydbm
\n
Generic interface to dbm-style databases.
\n
Module bsddb
\n
Lower-level interface to the BSD db library.
\n
Module whichdb
\n
Utility module used to determine the type of an existing database.
\n
\n
\n
\n

11.10.1. Database Objects

\n

The database objects returned by open() provide the methods common to all\nthe DBM-style databases and mapping objects. The following methods are\navailable in addition to the standard methods.

\n
\n
\ndbhash.first()
\n
It’s possible to loop over every key/value pair in the database using this\nmethod and the next() method. The traversal is ordered by the databases\ninternal hash values, and won’t be sorted by the key values. This method\nreturns the starting key.
\n\n
\n
\ndbhash.last()
\n
Return the last key/value pair in a database traversal. This may be used to\nbegin a reverse-order traversal; see previous().
\n\n
\n
\ndbhash.next()
\n

Returns the key next key/value pair in a database traversal. The following code\nprints every key in the database db, without having to create a list in\nmemory that contains them all:

\n
print db.first()\nfor i in xrange(1, len(db)):\n    print db.next()\n
\n
\n
\n\n
\n
\ndbhash.previous()
\n
Returns the previous key/value pair in a forward-traversal of the database. In\nconjunction with last(), this may be used to implement a reverse-order\ntraversal.
\n\n
\n
\ndbhash.sync()
\n
This method forces any unwritten data to be written to the disk.
\n\n
\n
", "searchableItems": [ { "name": "dbhash.dbhash.first", "domId": "dbhash_dbhash.dbhash.first" }, { "name": "dbhash.dbhash.last", "domId": "dbhash_dbhash.dbhash.last" }, { "name": "dbhash.dbhash.next", "domId": "dbhash_dbhash.dbhash.next" }, { "name": "dbhash.dbhash.previous", "domId": "dbhash_dbhash.dbhash.previous" }, { "name": "dbhash.dbhash.sync", "domId": "dbhash_dbhash.dbhash.sync" }, { "name": "dbhash.open", "domId": "dbhash_dbhash.open" } ] }, { "url": "http://docs.python.org/library/gzip.html", "title": "gzip", "html": "
\n

12.2. gzip — Support for gzip files

\n

Source code: Lib/gzip.py

\n
\n

This module provides a simple interface to compress and decompress files just\nlike the GNU programs gzip and gunzip would.

\n

The data compression is provided by the zlib module.

\n

The gzip module provides the GzipFile class which is modeled\nafter Python’s File Object. The GzipFile class reads and writes\ngzip-format files, automatically compressing or decompressing the\ndata so that it looks like an ordinary file object.

\n

Note that additional file formats which can be decompressed by the\ngzip and gunzip programs, such as those produced by\ncompress and pack, are not supported by this module.

\n

For other archive formats, see the bz2, zipfile, and\ntarfile modules.

\n

The module defines the following items:

\n
\n
\nclass gzip.GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]])
\n

Constructor for the GzipFile class, which simulates most of the methods\nof a file object, with the exception of the readinto() and\ntruncate() methods. At least one of fileobj and filename must be\ngiven a non-trivial value.

\n

The new class instance is based on fileobj, which can be a regular file, a\nStringIO object, or any other object which simulates a file. It\ndefaults to None, in which case filename is opened to provide a file\nobject.

\n

When fileobj is not None, the filename argument is only used to be\nincluded in the gzip file header, which may includes the original\nfilename of the uncompressed file. It defaults to the filename of fileobj, if\ndiscernible; otherwise, it defaults to the empty string, and in this case the\noriginal filename is not included in the header.

\n

The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w',\nor 'wb', depending on whether the file will be read or written. The default\nis the mode of fileobj if discernible; otherwise, the default is 'rb'. If\nnot given, the ‘b’ flag will be added to the mode to ensure the file is opened\nin binary mode for cross-platform portability.

\n

The compresslevel argument is an integer from 1 to 9 controlling the\nlevel of compression; 1 is fastest and produces the least compression, and\n9 is slowest and produces the most compression. The default is 9.

\n

The mtime argument is an optional numeric timestamp to be written to\nthe stream when compressing. All gzip compressed streams are\nrequired to contain a timestamp. If omitted or None, the current\ntime is used. This module ignores the timestamp when decompressing;\nhowever, some programs, such as gunzip, make use of it.\nThe format of the timestamp is the same as that of the return value of\ntime.time() and of the st_mtime attribute of the object returned\nby os.stat().

\n

Calling a GzipFile object’s close() method does not close\nfileobj, since you might wish to append more material after the compressed\ndata. This also allows you to pass a StringIO object opened for\nwriting as fileobj, and retrieve the resulting memory buffer using the\nStringIO object’s getvalue() method.

\n

GzipFile supports iteration and the with statement.

\n

\nChanged in version 2.7: Support for the with statement was added.

\n

\nChanged in version 2.7: Support for zero-padded files was added.

\n
\n\n
\n
\ngzip.open(filename[, mode[, compresslevel]])
\n
This is a shorthand for GzipFile(filename, mode, compresslevel).\nThe filename argument is required; mode defaults to 'rb' and\ncompresslevel defaults to 9.
\n\n
\n

12.2.1. Examples of usage

\n

Example of how to read a compressed file:

\n
import gzip\nf = gzip.open('/home/joe/file.txt.gz', 'rb')\nfile_content = f.read()\nf.close()\n
\n
\n

Example of how to create a compressed GZIP file:

\n
import gzip\ncontent = "Lots of content here"\nf = gzip.open('/home/joe/file.txt.gz', 'wb')\nf.write(content)\nf.close()\n
\n
\n

Example of how to GZIP compress an existing file:

\n
import gzip\nf_in = open('/home/joe/file.txt', 'rb')\nf_out = gzip.open('/home/joe/file.txt.gz', 'wb')\nf_out.writelines(f_in)\nf_out.close()\nf_in.close()\n
\n
\n
\n

See also

\n
\n
Module zlib
\n
The basic data compression module needed to support the gzip file\nformat.
\n
\n
\n
\n
", "searchableItems": [ { "name": "gzip.GzipFile", "domId": "gzip_gzip.GzipFile" }, { "name": "gzip.open", "domId": "gzip_gzip.open" } ] }, { "url": "http://docs.python.org/library/zlib.html", "title": "zlib", "html": "
\n

12.1. zlib — Compression compatible with gzip

\n

For applications that require data compression, the functions in this module\nallow compression and decompression, using the zlib library. The zlib library\nhas its own home page at http://www.zlib.net. There are known\nincompatibilities between the Python module and versions of the zlib library\nearlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using\n1.1.4 or later.

\n

zlib’s functions have many options and often need to be used in a particular\norder. This documentation doesn’t attempt to cover all of the permutations;\nconsult the zlib manual at http://www.zlib.net/manual.html for authoritative\ninformation.

\n

For reading and writing .gz files see the gzip module. For\nother archive formats, see the bz2, zipfile, and\ntarfile modules.

\n

The available exception and functions in this module are:

\n
\n
\nexception zlib.error
\n
Exception raised on compression and decompression errors.
\n\n
\n
\nzlib.adler32(data[, value])
\n

Computes a Adler-32 checksum of data. (An Adler-32 checksum is almost as\nreliable as a CRC32 but can be computed much more quickly.) If value is\npresent, it is used as the starting value of the checksum; otherwise, a fixed\ndefault value is used. This allows computing a running checksum over the\nconcatenation of several inputs. The algorithm is not cryptographically\nstrong, and should not be used for authentication or digital signatures. Since\nthe algorithm is designed for use as a checksum algorithm, it is not suitable\nfor use as a general hash algorithm.

\n

This function always returns an integer object.

\n
\n\n
\n

Note

\n

To generate the same numeric value across all Python versions and\nplatforms use adler32(data) & 0xffffffff. If you are only using\nthe checksum in packed binary format this is not necessary as the\nreturn value is the correct 32bit binary representation\nregardless of sign.

\n
\n

\nChanged in version 2.6: The return value is in the range [-2**31, 2**31-1]\nregardless of platform. In older versions the value is\nsigned on some platforms and unsigned on others.

\n

\nChanged in version 3.0: The return value is unsigned and in the range [0, 2**32-1]\nregardless of platform.

\n
\n
\nzlib.compress(string[, level])
\n
Compresses the data in string, returning a string contained compressed data.\nlevel is an integer from 1 to 9 controlling the level of compression;\n1 is fastest and produces the least compression, 9 is slowest and\nproduces the most. The default value is 6. Raises the error\nexception if any error occurs.
\n\n
\n
\nzlib.compressobj([level])
\n
Returns a compression object, to be used for compressing data streams that won’t\nfit into memory at once. level is an integer from 1 to 9 controlling\nthe level of compression; 1 is fastest and produces the least compression,\n9 is slowest and produces the most. The default value is 6.
\n\n
\n
\nzlib.crc32(data[, value])
\n

Computes a CRC (Cyclic Redundancy Check) checksum of data. If value is\npresent, it is used as the starting value of the checksum; otherwise, a fixed\ndefault value is used. This allows computing a running checksum over the\nconcatenation of several inputs. The algorithm is not cryptographically\nstrong, and should not be used for authentication or digital signatures. Since\nthe algorithm is designed for use as a checksum algorithm, it is not suitable\nfor use as a general hash algorithm.

\n

This function always returns an integer object.

\n
\n\n
\n

Note

\n

To generate the same numeric value across all Python versions and\nplatforms use crc32(data) & 0xffffffff. If you are only using\nthe checksum in packed binary format this is not necessary as the\nreturn value is the correct 32bit binary representation\nregardless of sign.

\n
\n

\nChanged in version 2.6: The return value is in the range [-2**31, 2**31-1]\nregardless of platform. In older versions the value would be\nsigned on some platforms and unsigned on others.

\n

\nChanged in version 3.0: The return value is unsigned and in the range [0, 2**32-1]\nregardless of platform.

\n
\n
\nzlib.decompress(string[, wbits[, bufsize]])
\n

Decompresses the data in string, returning a string containing the\nuncompressed data. The wbits parameter controls the size of the window\nbuffer, and is discussed further below.\nIf bufsize is given, it is used as the initial size of the output\nbuffer. Raises the error exception if any error occurs.

\n

The absolute value of wbits is the base two logarithm of the size of the\nhistory buffer (the “window size”) used when compressing data. Its absolute\nvalue should be between 8 and 15 for the most recent versions of the zlib\nlibrary, larger values resulting in better compression at the expense of greater\nmemory usage. When decompressing a stream, wbits must not be smaller\nthan the size originally used to compress the stream; using a too-small\nvalue will result in an exception. The default value is therefore the\nhighest value, 15. When wbits is negative, the standard\ngzip header is suppressed.

\n

bufsize is the initial size of the buffer used to hold decompressed data. If\nmore space is required, the buffer size will be increased as needed, so you\ndon’t have to get this value exactly right; tuning it will only save a few calls\nto malloc(). The default size is 16384.

\n
\n\n
\n
\nzlib.decompressobj([wbits])
\n
Returns a decompression object, to be used for decompressing data streams that\nwon’t fit into memory at once. The wbits parameter controls the size of the\nwindow buffer.
\n\n

Compression objects support the following methods:

\n
\n
\nCompress.compress(string)
\n
Compress string, returning a string containing compressed data for at least\npart of the data in string. This data should be concatenated to the output\nproduced by any preceding calls to the compress() method. Some input may\nbe kept in internal buffers for later processing.
\n\n
\n
\nCompress.flush([mode])
\n
All pending input is processed, and a string containing the remaining compressed\noutput is returned. mode can be selected from the constants\nZ_SYNC_FLUSH, Z_FULL_FLUSH, or Z_FINISH,\ndefaulting to Z_FINISH. Z_SYNC_FLUSH and\nZ_FULL_FLUSH allow compressing further strings of data, while\nZ_FINISH finishes the compressed stream and prevents compressing any\nmore data. After calling flush() with mode set to Z_FINISH,\nthe compress() method cannot be called again; the only realistic action is\nto delete the object.
\n\n
\n
\nCompress.copy()
\n

Returns a copy of the compression object. This can be used to efficiently\ncompress a set of data that share a common initial prefix.

\n

\nNew in version 2.5.

\n
\n\n

Decompression objects support the following methods, and two attributes:

\n
\n
\nDecompress.unused_data
\n

A string which contains any bytes past the end of the compressed data. That is,\nthis remains "" until the last byte that contains compression data is\navailable. If the whole string turned out to contain compressed data, this is\n"", the empty string.

\n

The only way to determine where a string of compressed data ends is by actually\ndecompressing it. This means that when compressed data is contained part of a\nlarger file, you can only find the end of it by reading data and feeding it\nfollowed by some non-empty string into a decompression object’s\ndecompress() method until the unused_data attribute is no longer\nthe empty string.

\n
\n\n
\n
\nDecompress.unconsumed_tail
\n
A string that contains any data that was not consumed by the last\ndecompress() call because it exceeded the limit for the uncompressed data\nbuffer. This data has not yet been seen by the zlib machinery, so you must feed\nit (possibly with further data concatenated to it) back to a subsequent\ndecompress() method call in order to get correct output.
\n\n
\n
\nDecompress.decompress(string[, max_length])
\n

Decompress string, returning a string containing the uncompressed data\ncorresponding to at least part of the data in string. This data should be\nconcatenated to the output produced by any preceding calls to the\ndecompress() method. Some of the input data may be preserved in internal\nbuffers for later processing.

\n

If the optional parameter max_length is supplied then the return value will be\nno longer than max_length. This may mean that not all of the compressed input\ncan be processed; and unconsumed data will be stored in the attribute\nunconsumed_tail. This string must be passed to a subsequent call to\ndecompress() if decompression is to continue. If max_length is not\nsupplied then the whole input is decompressed, and unconsumed_tail is an\nempty string.

\n
\n\n
\n
\nDecompress.flush([length])
\n

All pending input is processed, and a string containing the remaining\nuncompressed output is returned. After calling flush(), the\ndecompress() method cannot be called again; the only realistic action is\nto delete the object.

\n

The optional parameter length sets the initial size of the output buffer.

\n
\n\n
\n
\nDecompress.copy()
\n

Returns a copy of the decompression object. This can be used to save the state\nof the decompressor midway through the data stream in order to speed up random\nseeks into the stream at a future point.

\n

\nNew in version 2.5.

\n
\n\n
\n

See also

\n
\n
Module gzip
\n
Reading and writing gzip-format files.
\n
http://www.zlib.net
\n
The zlib library home page.
\n
http://www.zlib.net/manual.html
\n
The zlib manual explains the semantics and usage of the library’s many\nfunctions.
\n
\n
\n
", "searchableItems": [ { "name": "zlib.adler32", "domId": "zlib_zlib.adler32" }, { "name": "zlib.compress", "domId": "zlib_zlib.compress" }, { "name": "zlib.Compress.compress", "domId": "zlib_zlib.Compress.compress" }, { "name": "zlib.Compress.copy", "domId": "zlib_zlib.Compress.copy" }, { "name": "zlib.Compress.flush", "domId": "zlib_zlib.Compress.flush" }, { "name": "zlib.compressobj", "domId": "zlib_zlib.compressobj" }, { "name": "zlib.crc32", "domId": "zlib_zlib.crc32" }, { "name": "zlib.decompress", "domId": "zlib_zlib.decompress" }, { "name": "zlib.Decompress.copy", "domId": "zlib_zlib.Decompress.copy" }, { "name": "zlib.Decompress.decompress", "domId": "zlib_zlib.Decompress.decompress" }, { "name": "zlib.Decompress.flush", "domId": "zlib_zlib.Decompress.flush" }, { "name": "zlib.decompressobj", "domId": "zlib_zlib.decompressobj" } ] }, { "url": "http://docs.python.org/library/bsddb.html", "title": "bsddb", "html": "
\n

11.11. bsddb — Interface to Berkeley DB library

\n

\nDeprecated since version 2.6: The bsddb module has been deprecated for removal in Python 3.0.

\n

The bsddb module provides an interface to the Berkeley DB library. Users\ncan create hash, btree or record based library files using the appropriate open\ncall. Bsddb objects behave generally like dictionaries. Keys and values must be\nstrings, however, so to use other objects as keys or to store other kinds of\nobjects the user must serialize them somehow, typically using\nmarshal.dumps() or pickle.dumps().

\n

The bsddb module requires a Berkeley DB library version from 4.0 thru\n4.7.

\n
\n

See also

\n
\n
http://www.jcea.es/programacion/pybsddb.htm
\n
The website with documentation for the bsddb.db Python Berkeley DB\ninterface that closely mirrors the object oriented interface provided in\nBerkeley DB 4.x itself.
\n
http://www.oracle.com/database/berkeley-db/
\n
The Berkeley DB library.
\n
\n
\n

A more modern DB, DBEnv and DBSequence object interface is available in the\nbsddb.db module which closely matches the Berkeley DB C API documented at\nthe above URLs. Additional features provided by the bsddb.db API include\nfine tuning, transactions, logging, and multiprocess concurrent database access.

\n

The following is a description of the legacy bsddb interface compatible\nwith the old Python bsddb module. Starting in Python 2.5 this interface should\nbe safe for multithreaded access. The bsddb.db API is recommended for\nthreading users as it provides better control.

\n

The bsddb module defines the following functions that create objects that\naccess the appropriate type of Berkeley DB file. The first two arguments of\neach function are the same. For ease of portability, only the first two\narguments should be used in most instances.

\n
\n
\nbsddb.hashopen(filename[, flag[, mode[, pgsize[, ffactor[, nelem[, cachesize[, lorder[, hflags]]]]]]]])
\n
Open the hash format file named filename. Files never intended to be\npreserved on disk may be created by passing None as the filename. The\noptional flag identifies the mode used to open the file. It may be 'r'\n(read only), 'w' (read-write) , 'c' (read-write - create if necessary;\nthe default) or 'n' (read-write - truncate to zero length). The other\narguments are rarely used and are just passed to the low-level dbopen()\nfunction. Consult the Berkeley DB documentation for their use and\ninterpretation.
\n\n
\n
\nbsddb.btopen(filename[, flag[, mode[, btflags[, cachesize[, maxkeypage[, minkeypage[, pgsize[, lorder]]]]]]]])
\n
Open the btree format file named filename. Files never intended to be\npreserved on disk may be created by passing None as the filename. The\noptional flag identifies the mode used to open the file. It may be 'r'\n(read only), 'w' (read-write), 'c' (read-write - create if necessary;\nthe default) or 'n' (read-write - truncate to zero length). The other\narguments are rarely used and are just passed to the low-level dbopen function.\nConsult the Berkeley DB documentation for their use and interpretation.
\n\n
\n
\nbsddb.rnopen(filename[, flag[, mode[, rnflags[, cachesize[, pgsize[, lorder[, rlen[, delim[, source[, pad]]]]]]]]]])
\n
Open a DB record format file named filename. Files never intended to be\npreserved on disk may be created by passing None as the filename. The\noptional flag identifies the mode used to open the file. It may be 'r'\n(read only), 'w' (read-write), 'c' (read-write - create if necessary;\nthe default) or 'n' (read-write - truncate to zero length). The other\narguments are rarely used and are just passed to the low-level dbopen function.\nConsult the Berkeley DB documentation for their use and interpretation.
\n\n
\n

Note

\n

Beginning in 2.3 some Unix versions of Python may have a bsddb185 module.\nThis is present only to allow backwards compatibility with systems which ship\nwith the old Berkeley DB 1.85 database library. The bsddb185 module\nshould never be used directly in new code. The module has been removed in\nPython 3.0. If you find you still need it look in PyPI.

\n
\n
\n

See also

\n
\n
Module dbhash
\n
DBM-style interface to the bsddb
\n
\n
\n
\n

11.11.1. Hash, BTree and Record Objects

\n

Once instantiated, hash, btree and record objects support the same methods as\ndictionaries. In addition, they support the methods listed below.

\n

\nChanged in version 2.3.1: Added dictionary methods.

\n
\n
\nbsddbobject.close()
\n
Close the underlying file. The object can no longer be accessed. Since there\nis no open open() method for these objects, to open the file again a new\nbsddb module open function must be called.
\n\n
\n
\nbsddbobject.keys()
\n
Return the list of keys contained in the DB file. The order of the list is\nunspecified and should not be relied on. In particular, the order of the list\nreturned is different for different file formats.
\n\n
\n
\nbsddbobject.has_key(key)
\n
Return 1 if the DB file contains the argument as a key.
\n\n
\n
\nbsddbobject.set_location(key)
\n
Set the cursor to the item indicated by key and return a tuple containing the\nkey and its value. For binary tree databases (opened using btopen()), if\nkey does not actually exist in the database, the cursor will point to the next\nitem in sorted order and return that key and value. For other databases,\nKeyError will be raised if key is not found in the database.
\n\n
\n
\nbsddbobject.first()
\n
Set the cursor to the first item in the DB file and return it. The order of\nkeys in the file is unspecified, except in the case of B-Tree databases. This\nmethod raises bsddb.error if the database is empty.
\n\n
\n
\nbsddbobject.next()
\n
Set the cursor to the next item in the DB file and return it. The order of\nkeys in the file is unspecified, except in the case of B-Tree databases.
\n\n
\n
\nbsddbobject.previous()
\n
Set the cursor to the previous item in the DB file and return it. The order of\nkeys in the file is unspecified, except in the case of B-Tree databases. This\nis not supported on hashtable databases (those opened with hashopen()).
\n\n
\n
\nbsddbobject.last()
\n
Set the cursor to the last item in the DB file and return it. The order of keys\nin the file is unspecified. This is not supported on hashtable databases (those\nopened with hashopen()). This method raises bsddb.error if the\ndatabase is empty.
\n\n
\n
\nbsddbobject.sync()
\n
Synchronize the database on disk.
\n\n

Example:

\n
>>> import bsddb\n>>> db = bsddb.btopen('/tmp/spam.db', 'c')\n>>> for i in range(10): db['%d'i] = '%d' (i*i)\n...\n>>> db['3']\n'9'\n>>> db.keys()\n['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n>>> db.first()\n('0', '0')\n>>> db.next()\n('1', '1')\n>>> db.last()\n('9', '81')\n>>> db.set_location('2')\n('2', '4')\n>>> db.previous()\n('1', '1')\n>>> for k, v in db.iteritems():\n...     print k, v\n0 0\n1 1\n2 4\n3 9\n4 16\n5 25\n6 36\n7 49\n8 64\n9 81\n>>> '8' in db\nTrue\n>>> db.sync()\n0\n
\n
\n
\n
", "searchableItems": [ { "name": "bsddb.bsddbobject.close", "domId": "bsddb_bsddb.bsddbobject.close" }, { "name": "bsddb.bsddbobject.first", "domId": "bsddb_bsddb.bsddbobject.first" }, { "name": "bsddb.bsddbobject.has_key", "domId": "bsddb_bsddb.bsddbobject.has_key" }, { "name": "bsddb.bsddbobject.keys", "domId": "bsddb_bsddb.bsddbobject.keys" }, { "name": "bsddb.bsddbobject.last", "domId": "bsddb_bsddb.bsddbobject.last" }, { "name": "bsddb.bsddbobject.next", "domId": "bsddb_bsddb.bsddbobject.next" }, { "name": "bsddb.bsddbobject.previous", "domId": "bsddb_bsddb.bsddbobject.previous" }, { "name": "bsddb.bsddbobject.set_location", "domId": "bsddb_bsddb.bsddbobject.set_location" }, { "name": "bsddb.bsddbobject.sync", "domId": "bsddb_bsddb.bsddbobject.sync" }, { "name": "bsddb.btopen", "domId": "bsddb_bsddb.btopen" }, { "name": "bsddb.hashopen", "domId": "bsddb_bsddb.hashopen" }, { "name": "bsddb.rnopen", "domId": "bsddb_bsddb.rnopen" } ] }, { "url": "http://docs.python.org/library/bz2.html", "title": "bz2", "html": "
\n

12.3. bz2 — Compression compatible with bzip2

\n

\nNew in version 2.3.

\n

This module provides a comprehensive interface for the bz2 compression library.\nIt implements a complete file interface, one-shot (de)compression functions, and\ntypes for sequential (de)compression.

\n

For other archive formats, see the gzip, zipfile, and\ntarfile modules.

\n

Here is a summary of the features offered by the bz2 module:

\n\n
\n

12.3.1. (De)compression of files

\n

Handling of compressed files is offered by the BZ2File class.

\n
\n
\nclass bz2.BZ2File(filename[, mode[, buffering[, compresslevel]]])
\n

Open a bz2 file. Mode can be either 'r' or 'w', for reading (default)\nor writing. When opened for writing, the file will be created if it doesn’t\nexist, and truncated otherwise. If buffering is given, 0 means\nunbuffered, and larger numbers specify the buffer size; the default is\n0. If compresslevel is given, it must be a number between 1 and\n9; the default is 9. Add a 'U' to mode to open the file for input\nwith universal newline support. Any line ending in the input file will be\nseen as a '\\n' in Python. Also, a file so opened gains the attribute\nnewlines; the value for this attribute is one of None (no newline\nread yet), '\\r', '\\n', '\\r\\n' or a tuple containing all the\nnewline types seen. Universal newlines are available only when\nreading. Instances support iteration in the same way as normal file\ninstances.

\n

BZ2File supports the with statement.

\n

\nChanged in version 2.7: Support for the with statement was added.

\n
\n
\nclose()
\n
Close the file. Sets data attribute closed to true. A closed file\ncannot be used for further I/O operations. close() may be called\nmore than once without error.
\n\n
\n
\nread([size])
\n
Read at most size uncompressed bytes, returned as a string. If the\nsize argument is negative or omitted, read until EOF is reached.
\n\n
\n
\nreadline([size])
\n
Return the next line from the file, as a string, retaining newline. A\nnon-negative size argument limits the maximum number of bytes to return\n(an incomplete line may be returned then). Return an empty string at EOF.
\n\n
\n
\nreadlines([size])
\n
Return a list of lines read. The optional size argument, if given, is an\napproximate bound on the total number of bytes in the lines returned.
\n\n
\n
\nxreadlines()
\n

For backward compatibility. BZ2File objects now include the\nperformance optimizations previously implemented in the xreadlines\nmodule.

\n

\nDeprecated since version 2.3: This exists only for compatibility with the method by this name on\nfile objects, which is deprecated. Use for line in file\ninstead.

\n
\n\n
\n
\nseek(offset[, whence])
\n

Move to new file position. Argument offset is a byte count. Optional\nargument whence defaults to os.SEEK_SET or 0 (offset from start\nof file; offset should be >= 0); other values are os.SEEK_CUR or\n1 (move relative to current position; offset can be positive or\nnegative), and os.SEEK_END or 2 (move relative to end of file;\noffset is usually negative, although many platforms allow seeking beyond\nthe end of a file).

\n

Note that seeking of bz2 files is emulated, and depending on the\nparameters the operation may be extremely slow.

\n
\n\n
\n
\ntell()
\n
Return the current file position, an integer (may be a long integer).
\n\n
\n
\nwrite(data)
\n
Write string data to file. Note that due to buffering, close() may\nbe needed before the file on disk reflects the data written.
\n\n
\n
\nwritelines(sequence_of_strings)
\n
Write the sequence of strings to the file. Note that newlines are not\nadded. The sequence can be any iterable object producing strings. This is\nequivalent to calling write() for each string.
\n\n
\n\n
\n
\n

12.3.2. Sequential (de)compression

\n

Sequential compression and decompression is done using the classes\nBZ2Compressor and BZ2Decompressor.

\n
\n
\nclass bz2.BZ2Compressor([compresslevel])
\n

Create a new compressor object. This object may be used to compress data\nsequentially. If you want to compress data in one shot, use the\ncompress() function instead. The compresslevel parameter, if given,\nmust be a number between 1 and 9; the default is 9.

\n
\n
\ncompress(data)
\n
Provide more data to the compressor object. It will return chunks of\ncompressed data whenever possible. When you’ve finished providing data to\ncompress, call the flush() method to finish the compression process,\nand return what is left in internal buffers.
\n\n
\n
\nflush()
\n
Finish the compression process and return what is left in internal\nbuffers. You must not use the compressor object after calling this method.
\n\n
\n\n
\n
\nclass bz2.BZ2Decompressor
\n

Create a new decompressor object. This object may be used to decompress data\nsequentially. If you want to decompress data in one shot, use the\ndecompress() function instead.

\n
\n
\ndecompress(data)
\n
Provide more data to the decompressor object. It will return chunks of\ndecompressed data whenever possible. If you try to decompress data after\nthe end of stream is found, EOFError will be raised. If any data\nwas found after the end of stream, it’ll be ignored and saved in\nunused_data attribute.
\n\n
\n\n
\n
\n

12.3.3. One-shot (de)compression

\n

One-shot compression and decompression is provided through the compress()\nand decompress() functions.

\n
\n
\nbz2.compress(data[, compresslevel])
\n
Compress data in one shot. If you want to compress data sequentially, use\nan instance of BZ2Compressor instead. The compresslevel parameter,\nif given, must be a number between 1 and 9; the default is 9.
\n\n
\n
\nbz2.decompress(data)
\n
Decompress data in one shot. If you want to decompress data sequentially,\nuse an instance of BZ2Decompressor instead.
\n\n
\n
", "searchableItems": [ { "name": "bz2.BZ2Compressor", "domId": "bz2_bz2.BZ2Compressor" }, { "name": "bz2.BZ2Compressor.compress", "domId": "bz2_bz2.BZ2Compressor.compress" }, { "name": "bz2.BZ2Compressor.flush", "domId": "bz2_bz2.BZ2Compressor.flush" }, { "name": "bz2.BZ2Decompressor", "domId": "bz2_bz2.BZ2Decompressor" }, { "name": "bz2.BZ2Decompressor.decompress", "domId": "bz2_bz2.BZ2Decompressor.decompress" }, { "name": "bz2.BZ2File", "domId": "bz2_bz2.BZ2File" }, { "name": "bz2.BZ2File.close", "domId": "bz2_bz2.BZ2File.close" }, { "name": "bz2.BZ2File.read", "domId": "bz2_bz2.BZ2File.read" }, { "name": "bz2.BZ2File.readline", "domId": "bz2_bz2.BZ2File.readline" }, { "name": "bz2.BZ2File.readlines", "domId": "bz2_bz2.BZ2File.readlines" }, { "name": "bz2.BZ2File.seek", "domId": "bz2_bz2.BZ2File.seek" }, { "name": "bz2.BZ2File.tell", "domId": "bz2_bz2.BZ2File.tell" }, { "name": "bz2.BZ2File.write", "domId": "bz2_bz2.BZ2File.write" }, { "name": "bz2.BZ2File.writelines", "domId": "bz2_bz2.BZ2File.writelines" }, { "name": "bz2.BZ2File.xreadlines", "domId": "bz2_bz2.BZ2File.xreadlines" }, { "name": "bz2.compress", "domId": "bz2_bz2.compress" }, { "name": "bz2.decompress", "domId": "bz2_bz2.decompress" } ] }, { "url": "http://docs.python.org/library/zipfile.html", "title": "zipfile", "html": "
\n

12.4. zipfile — Work with ZIP archives

\n

\nNew in version 1.6.

\n

Source code: Lib/zipfile.py

\n
\n

The ZIP file format is a common archive and compression standard. This module\nprovides tools to create, read, write, append, and list a ZIP file. Any\nadvanced use of this module will require an understanding of the format, as\ndefined in PKZIP Application Note.

\n

This module does not currently handle multi-disk ZIP files.\nIt can handle ZIP files that use the ZIP64 extensions\n(that is ZIP files that are more than 4 GByte in size). It supports\ndecryption of encrypted files in ZIP archives, but it currently cannot\ncreate an encrypted file. Decryption is extremely slow as it is\nimplemented in native Python rather than C.

\n

For other archive formats, see the bz2, gzip, and\ntarfile modules.

\n

The module defines the following items:

\n
\n
\nexception zipfile.BadZipfile
\n
The error raised for bad ZIP files (old name: zipfile.error).
\n\n
\n
\nexception zipfile.LargeZipFile
\n
The error raised when a ZIP file would require ZIP64 functionality but that has\nnot been enabled.
\n\n
\n
\nclass zipfile.ZipFile
\n
The class for reading and writing ZIP files. See section\nZipFile Objects for constructor details.
\n\n
\n
\nclass zipfile.PyZipFile
\n
Class for creating ZIP archives containing Python libraries.
\n\n
\n
\nclass zipfile.ZipInfo([filename[, date_time]])
\n
Class used to represent information about a member of an archive. Instances\nof this class are returned by the getinfo() and infolist()\nmethods of ZipFile objects. Most users of the zipfile module\nwill not need to create these, but only use those created by this\nmodule. filename should be the full name of the archive member, and\ndate_time should be a tuple containing six fields which describe the time\nof the last modification to the file; the fields are described in section\nZipInfo Objects.
\n\n
\n
\nzipfile.is_zipfile(filename)
\n

Returns True if filename is a valid ZIP file based on its magic number,\notherwise returns False. filename may be a file or file-like object too.

\n

\nChanged in version 2.7: Support for file and file-like objects.

\n
\n\n
\n
\nzipfile.ZIP_STORED
\n
The numeric constant for an uncompressed archive member.
\n\n
\n
\nzipfile.ZIP_DEFLATED
\n
The numeric constant for the usual ZIP compression method. This requires the\nzlib module. No other compression methods are currently supported.
\n\n
\n

See also

\n
\n
PKZIP Application Note
\n
Documentation on the ZIP file format by Phil Katz, the creator of the format and\nalgorithms used.
\n
Info-ZIP Home Page
\n
Information about the Info-ZIP project’s ZIP archive programs and development\nlibraries.
\n
\n
\n
\n

12.4.1. ZipFile Objects

\n
\n
\nclass zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
\n

Open a ZIP file, where file can be either a path to a file (a string) or a\nfile-like object. The mode parameter should be 'r' to read an existing\nfile, 'w' to truncate and write a new file, or 'a' to append to an\nexisting file. If mode is 'a' and file refers to an existing ZIP\nfile, then additional files are added to it. If file does not refer to a\nZIP file, then a new ZIP archive is appended to the file. This is meant for\nadding a ZIP archive to another file (such as python.exe).

\n

\nChanged in version 2.6: If mode is a and the file does not exist at all, it is created.

\n

compression is the ZIP compression method to use when writing the archive,\nand should be ZIP_STORED or ZIP_DEFLATED; unrecognized\nvalues will cause RuntimeError to be raised. If ZIP_DEFLATED\nis specified but the zlib module is not available, RuntimeError\nis also raised. The default is ZIP_STORED. If allowZip64 is\nTrue zipfile will create ZIP files that use the ZIP64 extensions when\nthe zipfile is larger than 2 GB. If it is false (the default) zipfile\nwill raise an exception when the ZIP file would require ZIP64 extensions.\nZIP64 extensions are disabled by default because the default zip\nand unzip commands on Unix (the InfoZIP utilities) don’t support\nthese extensions.

\n

\nChanged in version 2.7.1: If the file is created with mode 'a' or 'w' and then\nclose()d without adding any files to the archive, the appropriate\nZIP structures for an empty archive will be written to the file.

\n

ZipFile is also a context manager and therefore supports the\nwith statement. In the example, myzip is closed after the\nwith statement’s suite is finished—even if an exception occurs:

\n
with ZipFile('spam.zip', 'w') as myzip:\n    myzip.write('eggs.txt')\n
\n
\n

\nNew in version 2.7: Added the ability to use ZipFile as a context manager.

\n
\n\n
\n
\nZipFile.close()
\n
Close the archive file. You must call close() before exiting your program\nor essential records will not be written.
\n\n
\n
\nZipFile.getinfo(name)
\n
Return a ZipInfo object with information about the archive member\nname. Calling getinfo() for a name not currently contained in the\narchive will raise a KeyError.
\n\n
\n
\nZipFile.infolist()
\n
Return a list containing a ZipInfo object for each member of the\narchive. The objects are in the same order as their entries in the actual ZIP\nfile on disk if an existing archive was opened.
\n\n
\n
\nZipFile.namelist()
\n
Return a list of archive members by name.
\n\n
\n
\nZipFile.open(name[, mode[, pwd]])
\n

Extract a member from the archive as a file-like object (ZipExtFile). name is\nthe name of the file in the archive, or a ZipInfo object. The mode\nparameter, if included, must be one of the following: 'r' (the default),\n'U', or 'rU'. Choosing 'U' or 'rU' will enable universal newline\nsupport in the read-only object. pwd is the password used for encrypted files.\nCalling open() on a closed ZipFile will raise a RuntimeError.

\n
\n

Note

\n

The file-like object is read-only and provides the following methods:\nread(), readline(), readlines(), __iter__(),\nnext().

\n
\n
\n

Note

\n

If the ZipFile was created by passing in a file-like object as the first\nargument to the constructor, then the object returned by open() shares the\nZipFile’s file pointer. Under these circumstances, the object returned by\nopen() should not be used after any additional operations are performed\non the ZipFile object. If the ZipFile was created by passing in a string (the\nfilename) as the first argument to the constructor, then open() will\ncreate a new file object that will be held by the ZipExtFile, allowing it to\noperate independently of the ZipFile.

\n
\n
\n

Note

\n

The open(), read() and extract() methods can take a filename\nor a ZipInfo object. You will appreciate this when trying to read a\nZIP file that contains members with duplicate names.

\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nZipFile.extract(member[, path[, pwd]])
\n

Extract a member from the archive to the current working directory; member\nmust be its full name or a ZipInfo object). Its file information is\nextracted as accurately as possible. path specifies a different directory\nto extract to. member can be a filename or a ZipInfo object.\npwd is the password used for encrypted files.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nZipFile.extractall([path[, members[, pwd]]])
\n

Extract all members from the archive to the current working directory. path\nspecifies a different directory to extract to. members is optional and must\nbe a subset of the list returned by namelist(). pwd is the password\nused for encrypted files.

\n
\n

Warning

\n

Never extract archives from untrusted sources without prior inspection.\nIt is possible that files are created outside of path, e.g. members\nthat have absolute filenames starting with "/" or filenames with two\ndots "..".

\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nZipFile.printdir()
\n
Print a table of contents for the archive to sys.stdout.
\n\n
\n
\nZipFile.setpassword(pwd)
\n

Set pwd as default password to extract encrypted files.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nZipFile.read(name[, pwd])
\n

Return the bytes of the file name in the archive. name is the name of the\nfile in the archive, or a ZipInfo object. The archive must be open for\nread or append. pwd is the password used for encrypted files and, if specified,\nit will override the default password set with setpassword(). Calling\nread() on a closed ZipFile will raise a RuntimeError.

\n

\nChanged in version 2.6: pwd was added, and name can now be a ZipInfo object.

\n
\n\n
\n
\nZipFile.testzip()
\n
Read all the files in the archive and check their CRC’s and file headers.\nReturn the name of the first bad file, or else return None. Calling\ntestzip() on a closed ZipFile will raise a RuntimeError.
\n\n
\n
\nZipFile.write(filename[, arcname[, compress_type]])
\n

Write the file named filename to the archive, giving it the archive name\narcname (by default, this will be the same as filename, but without a drive\nletter and with leading path separators removed). If given, compress_type\noverrides the value given for the compression parameter to the constructor for\nthe new entry. The archive must be open with mode 'w' or 'a' – calling\nwrite() on a ZipFile created with mode 'r' will raise a\nRuntimeError. Calling write() on a closed ZipFile will raise a\nRuntimeError.

\n
\n

Note

\n

There is no official file name encoding for ZIP files. If you have unicode file\nnames, you must convert them to byte strings in your desired encoding before\npassing them to write(). WinZip interprets all file names as encoded in\nCP437, also known as DOS Latin.

\n
\n
\n

Note

\n

Archive names should be relative to the archive root, that is, they should not\nstart with a path separator.

\n
\n
\n

Note

\n

If arcname (or filename, if arcname is not given) contains a null\nbyte, the name of the file in the archive will be truncated at the null byte.

\n
\n
\n\n
\n
\nZipFile.writestr(zinfo_or_arcname, bytes[, compress_type])
\n

Write the string bytes to the archive; zinfo_or_arcname is either the file\nname it will be given in the archive, or a ZipInfo instance. If it’s\nan instance, at least the filename, date, and time must be given. If it’s a\nname, the date and time is set to the current date and time. The archive must be\nopened with mode 'w' or 'a' – calling writestr() on a ZipFile\ncreated with mode 'r' will raise a RuntimeError. Calling\nwritestr() on a closed ZipFile will raise a RuntimeError.

\n

If given, compress_type overrides the value given for the compression\nparameter to the constructor for the new entry, or in the zinfo_or_arcname\n(if that is a ZipInfo instance).

\n
\n

Note

\n

When passing a ZipInfo instance as the zinfo_or_arcname parameter,\nthe compression method used will be that specified in the compress_type\nmember of the given ZipInfo instance. By default, the\nZipInfo constructor sets this member to ZIP_STORED.

\n
\n

\nChanged in version 2.7: The compression_type argument.

\n
\n\n

The following data attributes are also available:

\n
\n
\nZipFile.debug
\n
The level of debug output to use. This may be set from 0 (the default, no\noutput) to 3 (the most output). Debugging information is written to\nsys.stdout.
\n\n
\n
\nZipFile.comment
\n
The comment text associated with the ZIP file. If assigning a comment to a\nZipFile instance created with mode ‘a’ or ‘w’, this should be a\nstring no longer than 65535 bytes. Comments longer than this will be\ntruncated in the written archive when ZipFile.close() is called.
\n\n
\n
\n

12.4.2. PyZipFile Objects

\n

The PyZipFile constructor takes the same parameters as the\nZipFile constructor. Instances have one method in addition to those of\nZipFile objects.

\n
\n
\nPyZipFile.writepy(pathname[, basename])
\n

Search for files *.py and add the corresponding file to the archive.\nThe corresponding file is a *.pyo file if available, else a\n*.pyc file, compiling if necessary. If the pathname is a file, the\nfilename must end with .py, and just the (corresponding\n*.py[co]) file is added at the top level (no path information). If the\npathname is a file that does not end with .py, a RuntimeError\nwill be raised. If it is a directory, and the directory is not a package\ndirectory, then all the files *.py[co] are added at the top level. If\nthe directory is a package directory, then all *.py[co] are added under\nthe package name as a file path, and if any subdirectories are package\ndirectories, all of these are added recursively. basename is intended for\ninternal use only. The writepy() method makes archives with file names\nlike this:

\n
string.pyc                                # Top level name\ntest/__init__.pyc                         # Package directory\ntest/test_support.pyc                          # Module test.test_support\ntest/bogus/__init__.pyc                   # Subpackage directory\ntest/bogus/myfile.pyc                     # Submodule test.bogus.myfile\n
\n
\n
\n\n
\n
\n

12.4.3. ZipInfo Objects

\n

Instances of the ZipInfo class are returned by the getinfo() and\ninfolist() methods of ZipFile objects. Each object stores\ninformation about a single member of the ZIP archive.

\n

Instances have the following attributes:

\n
\n
\nZipInfo.filename
\n
Name of the file in the archive.
\n\n
\n
\nZipInfo.date_time
\n

The time and date of the last modification to the archive member. This is a\ntuple of six values:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
IndexValue
0Year (>= 1980)
1Month (one-based)
2Day of month (one-based)
3Hours (zero-based)
4Minutes (zero-based)
5Seconds (zero-based)
\n
\n

Note

\n

The ZIP file format does not support timestamps before 1980.

\n
\n
\n\n
\n
\nZipInfo.compress_type
\n
Type of compression for the archive member.
\n\n
\n
\nZipInfo.comment
\n
Comment for the individual archive member.
\n\n
\n
\nZipInfo.extra
\n
Expansion field data. The PKZIP Application Note contains\nsome comments on the internal structure of the data contained in this string.
\n\n
\n
\nZipInfo.create_system
\n
System which created ZIP archive.
\n\n
\n
\nZipInfo.create_version
\n
PKZIP version which created ZIP archive.
\n\n
\n
\nZipInfo.extract_version
\n
PKZIP version needed to extract archive.
\n\n
\n
\nZipInfo.reserved
\n
Must be zero.
\n\n
\n
\nZipInfo.flag_bits
\n
ZIP flag bits.
\n\n
\n
\nZipInfo.volume
\n
Volume number of file header.
\n\n
\n
\nZipInfo.internal_attr
\n
Internal attributes.
\n\n
\n
\nZipInfo.external_attr
\n
External file attributes.
\n\n
\n
\nZipInfo.header_offset
\n
Byte offset to the file header.
\n\n
\n
\nZipInfo.CRC
\n
CRC-32 of the uncompressed file.
\n\n
\n
\nZipInfo.compress_size
\n
Size of the compressed data.
\n\n
\n
\nZipInfo.file_size
\n
Size of the uncompressed file.
\n\n
\n
", "searchableItems": [ { "name": "zipfile.is_zipfile", "domId": "zipfile_zipfile.is_zipfile" }, { "name": "zipfile.PyZipFile", "domId": "zipfile_zipfile.PyZipFile" }, { "name": "zipfile.PyZipFile.writepy", "domId": "zipfile_zipfile.PyZipFile.writepy" }, { "name": "zipfile.ZipFile", "domId": "zipfile_zipfile.ZipFile" }, { "name": "zipfile.ZipFile.close", "domId": "zipfile_zipfile.ZipFile.close" }, { "name": "zipfile.ZipFile.extract", "domId": "zipfile_zipfile.ZipFile.extract" }, { "name": "zipfile.ZipFile.extractall", "domId": "zipfile_zipfile.ZipFile.extractall" }, { "name": "zipfile.ZipFile.getinfo", "domId": "zipfile_zipfile.ZipFile.getinfo" }, { "name": "zipfile.ZipFile.infolist", "domId": "zipfile_zipfile.ZipFile.infolist" }, { "name": "zipfile.ZipFile.namelist", "domId": "zipfile_zipfile.ZipFile.namelist" }, { "name": "zipfile.ZipFile.open", "domId": "zipfile_zipfile.ZipFile.open" }, { "name": "zipfile.ZipFile.printdir", "domId": "zipfile_zipfile.ZipFile.printdir" }, { "name": "zipfile.ZipFile.read", "domId": "zipfile_zipfile.ZipFile.read" }, { "name": "zipfile.ZipFile.setpassword", "domId": "zipfile_zipfile.ZipFile.setpassword" }, { "name": "zipfile.ZipFile.testzip", "domId": "zipfile_zipfile.ZipFile.testzip" }, { "name": "zipfile.ZipFile.write", "domId": "zipfile_zipfile.ZipFile.write" }, { "name": "zipfile.ZipFile.writestr", "domId": "zipfile_zipfile.ZipFile.writestr" }, { "name": "zipfile.ZipInfo", "domId": "zipfile_zipfile.ZipInfo" } ] }, { "url": "http://docs.python.org/library/robotparser.html", "title": "robotparser", "html": "
\n

13.3. robotparser — Parser for robots.txt

\n
\n

Note

\n

The robotparser module has been renamed urllib.robotparser in\nPython 3.0.\nThe 2to3 tool will automatically adapt imports when converting\nyour sources to 3.0.

\n
\n

This module provides a single class, RobotFileParser, which answers\nquestions about whether or not a particular user agent can fetch a URL on the\nWeb site that published the robots.txt file. For more details on the\nstructure of robots.txt files, see http://www.robotstxt.org/orig.html.

\n
\n
\nclass robotparser.RobotFileParser
\n

This class provides a set of methods to read, parse and answer questions\nabout a single robots.txt file.

\n
\n
\nset_url(url)
\n
Sets the URL referring to a robots.txt file.
\n\n
\n
\nread()
\n
Reads the robots.txt URL and feeds it to the parser.
\n\n
\n
\nparse(lines)
\n
Parses the lines argument.
\n\n
\n
\ncan_fetch(useragent, url)
\n
Returns True if the useragent is allowed to fetch the url\naccording to the rules contained in the parsed robots.txt\nfile.
\n\n
\n
\nmtime()
\n
Returns the time the robots.txt file was last fetched. This is\nuseful for long-running web spiders that need to check for new\nrobots.txt files periodically.
\n\n
\n
\nmodified()
\n
Sets the time the robots.txt file was last fetched to the current\ntime.
\n\n
\n\n

The following example demonstrates basic use of the RobotFileParser class.

\n
>>> import robotparser\n>>> rp = robotparser.RobotFileParser()\n>>> rp.set_url("http://www.musi-cal.com/robots.txt")\n>>> rp.read()\n>>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")\nFalse\n>>> rp.can_fetch("*", "http://www.musi-cal.com/")\nTrue\n
\n
\n
", "searchableItems": [ { "name": "robotparser.RobotFileParser", "domId": "robotparser_robotparser.RobotFileParser" }, { "name": "robotparser.RobotFileParser.can_fetch", "domId": "robotparser_robotparser.RobotFileParser.can_fetch" }, { "name": "robotparser.RobotFileParser.modified", "domId": "robotparser_robotparser.RobotFileParser.modified" }, { "name": "robotparser.RobotFileParser.mtime", "domId": "robotparser_robotparser.RobotFileParser.mtime" }, { "name": "robotparser.RobotFileParser.parse", "domId": "robotparser_robotparser.RobotFileParser.parse" }, { "name": "robotparser.RobotFileParser.read", "domId": "robotparser_robotparser.RobotFileParser.read" }, { "name": "robotparser.RobotFileParser.set_url", "domId": "robotparser_robotparser.RobotFileParser.set_url" } ] }, { "url": "http://docs.python.org/library/tarfile.html", "title": "tarfile", "html": "
\n

12.5. tarfile — Read and write tar archive files

\n

\nNew in version 2.3.

\n

Source code: Lib/tarfile.py

\n
\n

The tarfile module makes it possible to read and write tar\narchives, including those using gzip or bz2 compression.\n(.zip files can be read and written using the zipfile module.)

\n

Some facts and figures:

\n\n
\n
\ntarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)
\n

Return a TarFile object for the pathname name. For detailed\ninformation on TarFile objects and the keyword arguments that are\nallowed, see TarFile Objects.

\n

mode has to be a string of the form 'filemode[:compression]', it defaults\nto 'r'. Here is a full list of mode combinations:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
modeaction
'r' or 'r:*'Open for reading with transparent\ncompression (recommended).
'r:'Open for reading exclusively without\ncompression.
'r:gz'Open for reading with gzip compression.
'r:bz2'Open for reading with bzip2 compression.
'a' or 'a:'Open for appending with no compression. The\nfile is created if it does not exist.
'w' or 'w:'Open for uncompressed writing.
'w:gz'Open for gzip compressed writing.
'w:bz2'Open for bzip2 compressed writing.
\n

Note that 'a:gz' or 'a:bz2' is not possible. If mode is not suitable\nto open a certain (compressed) file for reading, ReadError is raised. Use\nmode 'r' to avoid this. If a compression method is not supported,\nCompressionError is raised.

\n

If fileobj is specified, it is used as an alternative to a file object opened\nfor name. It is supposed to be at position 0.

\n

For special purposes, there is a second format for mode:\n'filemode|[compression]'. tarfile.open() will return a TarFile\nobject that processes its data as a stream of blocks. No random seeking will\nbe done on the file. If given, fileobj may be any object that has a\nread() or write() method (depending on the mode). bufsize\nspecifies the blocksize and defaults to 20 * 512 bytes. Use this variant\nin combination with e.g. sys.stdin, a socket file object or a tape\ndevice. However, such a TarFile object is limited in that it does\nnot allow to be accessed randomly, see Examples. The currently\npossible modes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ModeAction
'r|*'Open a stream of tar blocks for reading\nwith transparent compression.
'r|'Open a stream of uncompressed tar blocks\nfor reading.
'r|gz'Open a gzip compressed stream for\nreading.
'r|bz2'Open a bzip2 compressed stream for\nreading.
'w|'Open an uncompressed stream for writing.
'w|gz'Open an gzip compressed stream for\nwriting.
'w|bz2'Open an bzip2 compressed stream for\nwriting.
\n
\n\n
\n
\nclass tarfile.TarFile
\n
Class for reading and writing tar archives. Do not use this class directly,\nbetter use tarfile.open() instead. See TarFile Objects.
\n\n
\n
\ntarfile.is_tarfile(name)
\n
Return True if name is a tar archive file, that the tarfile\nmodule can read.
\n\n
\n
\nclass tarfile.TarFileCompat(filename, mode='r', compression=TAR_PLAIN)
\n

Class for limited access to tar archives with a zipfile-like interface.\nPlease consult the documentation of the zipfile module for more details.\ncompression must be one of the following constants:

\n
\n
\nTAR_PLAIN
\n
Constant for an uncompressed tar archive.
\n\n
\n
\nTAR_GZIPPED
\n
Constant for a gzip compressed tar archive.
\n\n

\nDeprecated since version 2.6: The TarFileCompat class has been deprecated for removal in Python 3.0.

\n
\n\n
\n
\nexception tarfile.TarError
\n
Base class for all tarfile exceptions.
\n\n
\n
\nexception tarfile.ReadError
\n
Is raised when a tar archive is opened, that either cannot be handled by the\ntarfile module or is somehow invalid.
\n\n
\n
\nexception tarfile.CompressionError
\n
Is raised when a compression method is not supported or when the data cannot be\ndecoded properly.
\n\n
\n
\nexception tarfile.StreamError
\n
Is raised for the limitations that are typical for stream-like TarFile\nobjects.
\n\n
\n
\nexception tarfile.ExtractError
\n
Is raised for non-fatal errors when using TarFile.extract(), but only if\nTarFile.errorlevel== 2.
\n\n
\n
\nexception tarfile.HeaderError
\n

Is raised by TarInfo.frombuf() if the buffer it gets is invalid.

\n

\nNew in version 2.6.

\n
\n\n

Each of the following constants defines a tar archive format that the\ntarfile module is able to create. See section Supported tar formats for\ndetails.

\n
\n
\ntarfile.USTAR_FORMAT
\n
POSIX.1-1988 (ustar) format.
\n\n
\n
\ntarfile.GNU_FORMAT
\n
GNU tar format.
\n\n
\n
\ntarfile.PAX_FORMAT
\n
POSIX.1-2001 (pax) format.
\n\n
\n
\ntarfile.DEFAULT_FORMAT
\n
The default format for creating archives. This is currently GNU_FORMAT.
\n\n

The following variables are available on module level:

\n
\n
\ntarfile.ENCODING
\n
The default character encoding i.e. the value from either\nsys.getfilesystemencoding() or sys.getdefaultencoding().
\n\n
\n

See also

\n
\n
Module zipfile
\n
Documentation of the zipfile standard module.
\n
GNU tar manual, Basic Tar Format
\n
Documentation for tar archive files, including GNU tar extensions.
\n
\n
\n
\n

12.5.1. TarFile Objects

\n

The TarFile object provides an interface to a tar archive. A tar\narchive is a sequence of blocks. An archive member (a stored file) is made up of\na header block followed by data blocks. It is possible to store a file in a tar\narchive several times. Each archive member is represented by a TarInfo\nobject, see TarInfo Objects for details.

\n

A TarFile object can be used as a context manager in a with\nstatement. It will automatically be closed when the block is completed. Please\nnote that in the event of an exception an archive opened for writing will not\nbe finalized; only the internally used file object will be closed. See the\nExamples section for a use case.

\n

\nNew in version 2.7: Added support for the context manager protocol.

\n
\n
\nclass tarfile.TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0)
\n

All following arguments are optional and can be accessed as instance attributes\nas well.

\n

name is the pathname of the archive. It can be omitted if fileobj is given.\nIn this case, the file object’s name attribute is used if it exists.

\n

mode is either 'r' to read from an existing archive, 'a' to append\ndata to an existing file or 'w' to create a new file overwriting an existing\none.

\n

If fileobj is given, it is used for reading or writing data. If it can be\ndetermined, mode is overridden by fileobj‘s mode. fileobj will be used\nfrom position 0.

\n
\n

Note

\n

fileobj is not closed, when TarFile is closed.

\n
\n

format controls the archive format. It must be one of the constants\nUSTAR_FORMAT, GNU_FORMAT or PAX_FORMAT that are\ndefined at module level.

\n

\nNew in version 2.6.

\n

The tarinfo argument can be used to replace the default TarInfo class\nwith a different one.

\n

\nNew in version 2.6.

\n

If dereference is False, add symbolic and hard links to the archive. If it\nis True, add the content of the target files to the archive. This has no\neffect on systems that do not support symbolic links.

\n

If ignore_zeros is False, treat an empty block as the end of the archive.\nIf it is True, skip empty (and invalid) blocks and try to get as many members\nas possible. This is only useful for reading concatenated or damaged archives.

\n

debug can be set from 0 (no debug messages) up to 3 (all debug\nmessages). The messages are written to sys.stderr.

\n

If errorlevel is 0, all errors are ignored when using TarFile.extract().\nNevertheless, they appear as error messages in the debug output, when debugging\nis enabled. If 1, all fatal errors are raised as OSError or\nIOError exceptions. If 2, all non-fatal errors are raised as\nTarError exceptions as well.

\n

The encoding and errors arguments control the way strings are converted to\nunicode objects and vice versa. The default settings will work for most users.\nSee section Unicode issues for in-depth information.

\n

\nNew in version 2.6.

\n

The pax_headers argument is an optional dictionary of unicode strings which\nwill be added as a pax global header if format is PAX_FORMAT.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nTarFile.open(...)
\n
Alternative constructor. The tarfile.open() function is actually a\nshortcut to this classmethod.
\n\n
\n
\nTarFile.getmember(name)
\n

Return a TarInfo object for member name. If name can not be found\nin the archive, KeyError is raised.

\n
\n

Note

\n

If a member occurs more than once in the archive, its last occurrence is assumed\nto be the most up-to-date version.

\n
\n
\n\n
\n
\nTarFile.getmembers()
\n
Return the members of the archive as a list of TarInfo objects. The\nlist has the same order as the members in the archive.
\n\n
\n
\nTarFile.getnames()
\n
Return the members as a list of their names. It has the same order as the list\nreturned by getmembers().
\n\n
\n
\nTarFile.list(verbose=True)
\n
Print a table of contents to sys.stdout. If verbose is False,\nonly the names of the members are printed. If it is True, output\nsimilar to that of ls -l is produced.
\n\n
\n
\nTarFile.next()
\n
Return the next member of the archive as a TarInfo object, when\nTarFile is opened for reading. Return None if there is no more\navailable.
\n\n
\n
\nTarFile.extractall(path=".", members=None)
\n

Extract all members from the archive to the current working directory or\ndirectory path. If optional members is given, it must be a subset of the\nlist returned by getmembers(). Directory information like owner,\nmodification time and permissions are set after all members have been extracted.\nThis is done to work around two problems: A directory’s modification time is\nreset each time a file is created in it. And, if a directory’s permissions do\nnot allow writing, extracting files to it will fail.

\n
\n

Warning

\n

Never extract archives from untrusted sources without prior inspection.\nIt is possible that files are created outside of path, e.g. members\nthat have absolute filenames starting with "/" or filenames with two\ndots "..".

\n
\n

\nNew in version 2.5.

\n
\n\n
\n
\nTarFile.extract(member, path="")
\n

Extract a member from the archive to the current working directory, using its\nfull name. Its file information is extracted as accurately as possible. member\nmay be a filename or a TarInfo object. You can specify a different\ndirectory using path.

\n
\n

Note

\n

The extract() method does not take care of several extraction issues.\nIn most cases you should consider using the extractall() method.

\n
\n
\n

Warning

\n

See the warning for extractall().

\n
\n
\n\n
\n
\nTarFile.extractfile(member)
\n

Extract a member from the archive as a file object. member may be a filename\nor a TarInfo object. If member is a regular file, a file-like object\nis returned. If member is a link, a file-like object is constructed from the\nlink’s target. If member is none of the above, None is returned.

\n
\n

Note

\n

The file-like object is read-only. It provides the methods\nread(), readline(), readlines(), seek(), tell(),\nand close(), and also supports iteration over its lines.

\n
\n
\n\n
\n
\nTarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)
\n

Add the file name to the archive. name may be any type of file (directory,\nfifo, symbolic link, etc.). If given, arcname specifies an alternative name\nfor the file in the archive. Directories are added recursively by default. This\ncan be avoided by setting recursive to False. If exclude is given\nit must be a function that takes one filename argument and returns a boolean\nvalue. Depending on this value the respective file is either excluded\n(True) or added (False). If filter is specified it must\nbe a function that takes a TarInfo object argument and returns the\nchanged TarInfo object. If it instead returns None the TarInfo\nobject will be excluded from the archive. See Examples for an\nexample.

\n

\nChanged in version 2.6: Added the exclude parameter.

\n

\nChanged in version 2.7: Added the filter parameter.

\n

\nDeprecated since version 2.7: The exclude parameter is deprecated, please use the filter parameter\ninstead. For maximum portability, filter should be used as a keyword\nargument rather than as a positional argument so that code won’t be\naffected when exclude is ultimately removed.

\n
\n\n
\n
\nTarFile.addfile(tarinfo, fileobj=None)
\n

Add the TarInfo object tarinfo to the archive. If fileobj is given,\ntarinfo.size bytes are read from it and added to the archive. You can\ncreate TarInfo objects using gettarinfo().

\n
\n

Note

\n

On Windows platforms, fileobj should always be opened with mode 'rb' to\navoid irritation about the file size.

\n
\n
\n\n
\n
\nTarFile.gettarinfo(name=None, arcname=None, fileobj=None)
\n
Create a TarInfo object for either the file name or the file object\nfileobj (using os.fstat() on its file descriptor). You can modify some\nof the TarInfo‘s attributes before you add it using addfile().\nIf given, arcname specifies an alternative name for the file in the archive.
\n\n
\n
\nTarFile.close()
\n
Close the TarFile. In write mode, two finishing zero blocks are\nappended to the archive.
\n\n
\n
\nTarFile.posix
\n

Setting this to True is equivalent to setting the format\nattribute to USTAR_FORMAT, False is equivalent to\nGNU_FORMAT.

\n

\nChanged in version 2.4: posix defaults to False.

\n

\nDeprecated since version 2.6: Use the format attribute instead.

\n
\n\n
\n
\nTarFile.pax_headers
\n

A dictionary containing key-value pairs of pax global headers.

\n

\nNew in version 2.6.

\n
\n\n
\n
\n

12.5.2. TarInfo Objects

\n

A TarInfo object represents one member in a TarFile. Aside\nfrom storing all required attributes of a file (like file type, size, time,\npermissions, owner etc.), it provides some useful methods to determine its type.\nIt does not contain the file’s data itself.

\n

TarInfo objects are returned by TarFile‘s methods\ngetmember(), getmembers() and gettarinfo().

\n
\n
\nclass tarfile.TarInfo(name="")
\n
Create a TarInfo object.
\n\n
\n
\nTarInfo.frombuf(buf)
\n

Create and return a TarInfo object from string buffer buf.

\n

\nNew in version 2.6: Raises HeaderError if the buffer is invalid..

\n
\n\n
\n
\nTarInfo.fromtarfile(tarfile)
\n

Read the next member from the TarFile object tarfile and return it as\na TarInfo object.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nTarInfo.tobuf(format=DEFAULT_FORMAT, encoding=ENCODING, errors='strict')
\n

Create a string buffer from a TarInfo object. For information on the\narguments see the constructor of the TarFile class.

\n

\nChanged in version 2.6: The arguments were added.

\n
\n\n

A TarInfo object has the following public data attributes:

\n
\n
\nTarInfo.name
\n
Name of the archive member.
\n\n
\n
\nTarInfo.size
\n
Size in bytes.
\n\n
\n
\nTarInfo.mtime
\n
Time of last modification.
\n\n
\n
\nTarInfo.mode
\n
Permission bits.
\n\n
\n
\nTarInfo.type
\n
File type. type is usually one of these constants: REGTYPE,\nAREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE,\nFIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE,\nGNUTYPE_SPARSE. To determine the type of a TarInfo object\nmore conveniently, use the is_*() methods below.
\n\n
\n
\nTarInfo.linkname
\n
Name of the target file name, which is only present in TarInfo objects\nof type LNKTYPE and SYMTYPE.
\n\n
\n
\nTarInfo.uid
\n
User ID of the user who originally stored this member.
\n\n
\n
\nTarInfo.gid
\n
Group ID of the user who originally stored this member.
\n\n
\n
\nTarInfo.uname
\n
User name.
\n\n
\n
\nTarInfo.gname
\n
Group name.
\n\n
\n
\nTarInfo.pax_headers
\n

A dictionary containing key-value pairs of an associated pax extended header.

\n

\nNew in version 2.6.

\n
\n\n

A TarInfo object also provides some convenient query methods:

\n
\n
\nTarInfo.isfile()
\n
Return True if the Tarinfo object is a regular file.
\n\n
\n
\nTarInfo.isreg()
\n
Same as isfile().
\n\n
\n
\nTarInfo.isdir()
\n
Return True if it is a directory.
\n\n
\n
\nTarInfo.issym()
\n
Return True if it is a symbolic link.
\n\n
\n
\nTarInfo.islnk()
\n
Return True if it is a hard link.
\n\n
\n
\nTarInfo.ischr()
\n
Return True if it is a character device.
\n\n
\n
\nTarInfo.isblk()
\n
Return True if it is a block device.
\n\n
\n
\nTarInfo.isfifo()
\n
Return True if it is a FIFO.
\n\n
\n
\nTarInfo.isdev()
\n
Return True if it is one of character device, block device or FIFO.
\n\n
\n
\n

12.5.3. Examples

\n

How to extract an entire tar archive to the current working directory:

\n
import tarfile\ntar = tarfile.open("sample.tar.gz")\ntar.extractall()\ntar.close()\n
\n
\n

How to extract a subset of a tar archive with TarFile.extractall() using\na generator function instead of a list:

\n
import os\nimport tarfile\n\ndef py_files(members):\n    for tarinfo in members:\n        if os.path.splitext(tarinfo.name)[1] == ".py":\n            yield tarinfo\n\ntar = tarfile.open("sample.tar.gz")\ntar.extractall(members=py_files(tar))\ntar.close()\n
\n
\n

How to create an uncompressed tar archive from a list of filenames:

\n
import tarfile\ntar = tarfile.open("sample.tar", "w")\nfor name in ["foo", "bar", "quux"]:\n    tar.add(name)\ntar.close()\n
\n
\n

The same example using the with statement:

\n
import tarfile\nwith tarfile.open("sample.tar", "w") as tar:\n    for name in ["foo", "bar", "quux"]:\n        tar.add(name)\n
\n
\n

How to read a gzip compressed tar archive and display some member information:

\n
import tarfile\ntar = tarfile.open("sample.tar.gz", "r:gz")\nfor tarinfo in tar:\n    print tarinfo.name, "is", tarinfo.size, "bytes in size and is",\n    if tarinfo.isreg():\n        print "a regular file."\n    elif tarinfo.isdir():\n        print "a directory."\n    else:\n        print "something else."\ntar.close()\n
\n
\n

How to create an archive and reset the user information using the filter\nparameter in TarFile.add():

\n
import tarfile\ndef reset(tarinfo):\n    tarinfo.uid = tarinfo.gid = 0\n    tarinfo.uname = tarinfo.gname = "root"\n    return tarinfo\ntar = tarfile.open("sample.tar.gz", "w:gz")\ntar.add("foo", filter=reset)\ntar.close()\n
\n
\n
\n
\n

12.5.4. Supported tar formats

\n

There are three tar formats that can be created with the tarfile module:

\n\n

There are some more variants of the tar format which can be read, but not\ncreated:

\n\n
\n
\n

12.5.5. Unicode issues

\n

The tar format was originally conceived to make backups on tape drives with the\nmain focus on preserving file system information. Nowadays tar archives are\ncommonly used for file distribution and exchanging archives over networks. One\nproblem of the original format (that all other formats are merely variants of)\nis that there is no concept of supporting different character encodings. For\nexample, an ordinary tar archive created on a UTF-8 system cannot be read\ncorrectly on a Latin-1 system if it contains non-ASCII characters. Names (i.e.\nfilenames, linknames, user/group names) containing these characters will appear\ndamaged. Unfortunately, there is no way to autodetect the encoding of an\narchive.

\n

The pax format was designed to solve this problem. It stores non-ASCII names\nusing the universal character encoding UTF-8. When a pax archive is read,\nthese UTF-8 names are converted to the encoding of the local file system.

\n

The details of unicode conversion are controlled by the encoding and errors\nkeyword arguments of the TarFile class.

\n

The default value for encoding is the local character encoding. It is deduced\nfrom sys.getfilesystemencoding() and sys.getdefaultencoding(). In\nread mode, encoding is used exclusively to convert unicode names from a pax\narchive to strings in the local character encoding. In write mode, the use of\nencoding depends on the chosen archive format. In case of PAX_FORMAT,\ninput names that contain non-ASCII characters need to be decoded before being\nstored as UTF-8 strings. The other formats do not make use of encoding\nunless unicode objects are used as input names. These are converted to 8-bit\ncharacter strings before they are added to the archive.

\n

The errors argument defines how characters are treated that cannot be\nconverted to or from encoding. Possible values are listed in section\nCodec Base Classes. In read mode, there is an additional scheme\n'utf-8' which means that bad characters are replaced by their UTF-8\nrepresentation. This is the default scheme. In write mode the default value for\nerrors is 'strict' to ensure that name information is not altered\nunnoticed.

\n
\n
", "searchableItems": [ { "name": "tarfile.is_tarfile", "domId": "tarfile_tarfile.is_tarfile" }, { "name": "tarfile.open", "domId": "tarfile_tarfile.open" }, { "name": "tarfile.TarFile", "domId": "tarfile_tarfile.TarFile" }, { "name": "tarfile.TarFile.add", "domId": "tarfile_tarfile.TarFile.add" }, { "name": "tarfile.TarFile.addfile", "domId": "tarfile_tarfile.TarFile.addfile" }, { "name": "tarfile.TarFile.close", "domId": "tarfile_tarfile.TarFile.close" }, { "name": "tarfile.TarFile.extract", "domId": "tarfile_tarfile.TarFile.extract" }, { "name": "tarfile.TarFile.extractall", "domId": "tarfile_tarfile.TarFile.extractall" }, { "name": "tarfile.TarFile.extractfile", "domId": "tarfile_tarfile.TarFile.extractfile" }, { "name": "tarfile.TarFile.getmember", "domId": "tarfile_tarfile.TarFile.getmember" }, { "name": "tarfile.TarFile.getmembers", "domId": "tarfile_tarfile.TarFile.getmembers" }, { "name": "tarfile.TarFile.getnames", "domId": "tarfile_tarfile.TarFile.getnames" }, { "name": "tarfile.TarFile.gettarinfo", "domId": "tarfile_tarfile.TarFile.gettarinfo" }, { "name": "tarfile.TarFile.list", "domId": "tarfile_tarfile.TarFile.list" }, { "name": "tarfile.TarFile.next", "domId": "tarfile_tarfile.TarFile.next" }, { "name": "tarfile.TarFile.open", "domId": "tarfile_tarfile.TarFile.open" }, { "name": "tarfile.TarFileCompat", "domId": "tarfile_tarfile.TarFileCompat" }, { "name": "tarfile.TarInfo", "domId": "tarfile_tarfile.TarInfo" }, { "name": "tarfile.TarInfo.frombuf", "domId": "tarfile_tarfile.TarInfo.frombuf" }, { "name": "tarfile.TarInfo.fromtarfile", "domId": "tarfile_tarfile.TarInfo.fromtarfile" }, { "name": "tarfile.TarInfo.isblk", "domId": "tarfile_tarfile.TarInfo.isblk" }, { "name": "tarfile.TarInfo.ischr", "domId": "tarfile_tarfile.TarInfo.ischr" }, { "name": "tarfile.TarInfo.isdev", "domId": "tarfile_tarfile.TarInfo.isdev" }, { "name": "tarfile.TarInfo.isdir", "domId": "tarfile_tarfile.TarInfo.isdir" }, { "name": "tarfile.TarInfo.isfifo", "domId": "tarfile_tarfile.TarInfo.isfifo" }, { "name": "tarfile.TarInfo.isfile", "domId": "tarfile_tarfile.TarInfo.isfile" }, { "name": "tarfile.TarInfo.islnk", "domId": "tarfile_tarfile.TarInfo.islnk" }, { "name": "tarfile.TarInfo.isreg", "domId": "tarfile_tarfile.TarInfo.isreg" }, { "name": "tarfile.TarInfo.issym", "domId": "tarfile_tarfile.TarInfo.issym" }, { "name": "tarfile.TarInfo.tobuf", "domId": "tarfile_tarfile.TarInfo.tobuf" } ] }, { "url": "http://docs.python.org/library/configparser.html", "title": "ConfigParser", "html": "
\n

13.2. ConfigParser — Configuration file parser

\n
\n

Note

\n

The ConfigParser module has been renamed to configparser in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

This module defines the class ConfigParser. The ConfigParser\nclass implements a basic configuration file parser language which provides a\nstructure similar to what you would find on Microsoft Windows INI files. You\ncan use this to write Python programs which can be customized by end users\neasily.

\n
\n

Note

\n

This library does not interpret or write the value-type prefixes used in\nthe Windows Registry extended version of INI syntax.

\n
\n
\n

See also

\n
\n
Module shlex
\n
Support for a creating Unix shell-like mini-languages which can be used\nas an alternate format for application configuration files.
\n
Module json
\n
The json module implements a subset of JavaScript syntax which can also\nbe used for this purpose.
\n
\n
\n

The configuration file consists of sections, led by a [section] header and\nfollowed by name: value entries, with continuations in the style of\nRFC 822 (see section 3.1.1, “LONG HEADER FIELDS”); name=value is also\naccepted. Note that leading whitespace is removed from values. The optional\nvalues can contain format strings which refer to other values in the same\nsection, or values in a special DEFAULT section. Additional defaults can be\nprovided on initialization and retrieval. Lines beginning with '#' or\n';' are ignored and may be used to provide comments.

\n

Configuration files may include comments, prefixed by specific characters (#\nand ;). Comments may appear on their own in an otherwise empty line, or may\nbe entered in lines holding values or section names. In the latter case, they\nneed to be preceded by a whitespace character to be recognized as a comment.\n(For backwards compatibility, only ; starts an inline comment, while #\ndoes not.)

\n

On top of the core functionality, SafeConfigParser supports\ninterpolation. This means values can contain format strings which refer to\nother values in the same section, or values in a special DEFAULT section.\nAdditional defaults can be provided on initialization.

\n

For example:

\n
[My Section]\nfoodir: %(dir)s/whatever\ndir=frob\nlong: this value continues\n   in the next line
\n
\n

would resolve the %(dir)s to the value of dir (frob in this case).\nAll reference expansions are done on demand.

\n

Default values can be specified by passing them into the ConfigParser\nconstructor as a dictionary. Additional defaults may be passed into the\nget() method which will override all others.

\n

Sections are normally stored in a built-in dictionary. An alternative dictionary\ntype can be passed to the ConfigParser constructor. For example, if a\ndictionary type is passed that sorts its keys, the sections will be sorted on\nwrite-back, as will be the keys within each section.

\n
\n
\nclass ConfigParser.RawConfigParser([defaults[, dict_type[, allow_no_value]]])
\n

The basic configuration object. When defaults is given, it is initialized\ninto the dictionary of intrinsic defaults. When dict_type is given, it will\nbe used to create the dictionary objects for the list of sections, for the\noptions within a section, and for the default values. When allow_no_value\nis true (default: False), options without values are accepted; the value\npresented for these is None.

\n

This class does not\nsupport the magical interpolation behavior.

\n

All option names are passed through the optionxform() method. Its\ndefault implementation converts option names to lower case.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.6: dict_type was added.

\n

\nChanged in version 2.7: The default dict_type is collections.OrderedDict.\nallow_no_value was added.

\n
\n\n
\n
\nclass ConfigParser.ConfigParser([defaults[, dict_type[, allow_no_value]]])
\n

Derived class of RawConfigParser that implements the magical\ninterpolation feature and adds optional arguments to the get() and\nitems() methods. The values in defaults must be appropriate for the\n%()s string interpolation. Note that __name__ is an intrinsic default;\nits value is the section name, and will override any value provided in\ndefaults.

\n

All option names used in interpolation will be passed through the\noptionxform() method just like any other option name reference. Using\nthe default implementation of optionxform(), the values foo %(bar)s\nand foo %(BAR)s are equivalent.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.6: dict_type was added.

\n

\nChanged in version 2.7: The default dict_type is collections.OrderedDict.\nallow_no_value was added.

\n
\n\n
\n
\nclass ConfigParser.SafeConfigParser([defaults[, dict_type[, allow_no_value]]])
\n

Derived class of ConfigParser that implements a more-sane variant of\nthe magical interpolation feature. This implementation is more predictable as\nwell. New applications should prefer this version if they don’t need to be\ncompatible with older versions of Python.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.6: dict_type was added.

\n

\nChanged in version 2.7: The default dict_type is collections.OrderedDict.\nallow_no_value was added.

\n
\n\n
\n
\nexception ConfigParser.Error
\n
Base class for all other configparser exceptions.
\n\n
\n
\nexception ConfigParser.NoSectionError
\n
Exception raised when a specified section is not found.
\n\n
\n
\nexception ConfigParser.DuplicateSectionError
\n
Exception raised if add_section() is called with the name of a section\nthat is already present.
\n\n
\n
\nexception ConfigParser.NoOptionError
\n
Exception raised when a specified option is not found in the specified section.
\n\n
\n
\nexception ConfigParser.InterpolationError
\n
Base class for exceptions raised when problems occur performing string\ninterpolation.
\n\n
\n
\nexception ConfigParser.InterpolationDepthError
\n
Exception raised when string interpolation cannot be completed because the\nnumber of iterations exceeds MAX_INTERPOLATION_DEPTH. Subclass of\nInterpolationError.
\n\n
\n
\nexception ConfigParser.InterpolationMissingOptionError
\n

Exception raised when an option referenced from a value does not exist. Subclass\nof InterpolationError.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception ConfigParser.InterpolationSyntaxError
\n

Exception raised when the source text into which substitutions are made does not\nconform to the required syntax. Subclass of InterpolationError.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception ConfigParser.MissingSectionHeaderError
\n
Exception raised when attempting to parse a file which has no section headers.
\n\n
\n
\nexception ConfigParser.ParsingError
\n
Exception raised when errors occur attempting to parse a file.
\n\n
\n
\nConfigParser.MAX_INTERPOLATION_DEPTH
\n
The maximum depth for recursive interpolation for get() when the raw\nparameter is false. This is relevant only for the ConfigParser class.
\n\n
\n

See also

\n
\n
Module shlex
\n
Support for a creating Unix shell-like mini-languages which can be used as an\nalternate format for application configuration files.
\n
\n
\n
\n

13.2.1. RawConfigParser Objects

\n

RawConfigParser instances have the following methods:

\n
\n
\nRawConfigParser.defaults()
\n
Return a dictionary containing the instance-wide defaults.
\n\n
\n
\nRawConfigParser.sections()
\n
Return a list of the sections available; DEFAULT is not included in the\nlist.
\n\n
\n
\nRawConfigParser.add_section(section)
\n
Add a section named section to the instance. If a section by the given name\nalready exists, DuplicateSectionError is raised. If the name\nDEFAULT (or any of it’s case-insensitive variants) is passed,\nValueError is raised.
\n\n
\n
\nRawConfigParser.has_section(section)
\n
Indicates whether the named section is present in the configuration. The\nDEFAULT section is not acknowledged.
\n\n
\n
\nRawConfigParser.options(section)
\n
Returns a list of options available in the specified section.
\n\n
\n
\nRawConfigParser.has_option(section, option)
\n

If the given section exists, and contains the given option, return\nTrue; otherwise return False.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nRawConfigParser.read(filenames)
\n

Attempt to read and parse a list of filenames, returning a list of filenames\nwhich were successfully parsed. If filenames is a string or Unicode string,\nit is treated as a single filename. If a file named in filenames cannot be\nopened, that file will be ignored. This is designed so that you can specify a\nlist of potential configuration file locations (for example, the current\ndirectory, the user’s home directory, and some system-wide directory), and all\nexisting configuration files in the list will be read. If none of the named\nfiles exist, the ConfigParser instance will contain an empty dataset.\nAn application which requires initial values to be loaded from a file should\nload the required file or files using readfp() before calling read()\nfor any optional files:

\n
import ConfigParser, os\n\nconfig = ConfigParser.ConfigParser()\nconfig.readfp(open('defaults.cfg'))\nconfig.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])\n
\n
\n

\nChanged in version 2.4: Returns list of successfully parsed filenames.

\n
\n\n
\n
\nRawConfigParser.readfp(fp[, filename])
\n
Read and parse configuration data from the file or file-like object in fp\n(only the readline() method is used). If filename is omitted and fp\nhas a name attribute, that is used for filename; the default is\n<???>.
\n\n
\n
\nRawConfigParser.get(section, option)
\n
Get an option value for the named section.
\n\n
\n
\nRawConfigParser.getint(section, option)
\n
A convenience method which coerces the option in the specified section to an\ninteger.
\n\n
\n
\nRawConfigParser.getfloat(section, option)
\n
A convenience method which coerces the option in the specified section to a\nfloating point number.
\n\n
\n
\nRawConfigParser.getboolean(section, option)
\n
A convenience method which coerces the option in the specified section to a\nBoolean value. Note that the accepted values for the option are "1",\n"yes", "true", and "on", which cause this method to return True,\nand "0", "no", "false", and "off", which cause it to return\nFalse. These string values are checked in a case-insensitive manner. Any\nother value will cause it to raise ValueError.
\n\n
\n
\nRawConfigParser.items(section)
\n
Return a list of (name, value) pairs for each option in the given section.
\n\n
\n
\nRawConfigParser.set(section, option, value)
\n

If the given section exists, set the given option to the specified value;\notherwise raise NoSectionError. While it is possible to use\nRawConfigParser (or ConfigParser with raw parameters set to\ntrue) for internal storage of non-string values, full functionality (including\ninterpolation and output to files) can only be achieved using string values.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nRawConfigParser.write(fileobject)
\n

Write a representation of the configuration to the specified file object. This\nrepresentation can be parsed by a future read() call.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nRawConfigParser.remove_option(section, option)
\n

Remove the specified option from the specified section. If the section does\nnot exist, raise NoSectionError. If the option existed to be removed,\nreturn True; otherwise return False.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nRawConfigParser.remove_section(section)
\n
Remove the specified section from the configuration. If the section in fact\nexisted, return True. Otherwise return False.
\n\n
\n
\nRawConfigParser.optionxform(option)
\n

Transforms the option name option as found in an input file or as passed in\nby client code to the form that should be used in the internal structures.\nThe default implementation returns a lower-case version of option;\nsubclasses may override this or client code can set an attribute of this name\non instances to affect this behavior.

\n

You don’t necessarily need to subclass a ConfigParser to use this method, you\ncan also re-set it on an instance, to a function that takes a string\nargument. Setting it to str, for example, would make option names case\nsensitive:

\n
cfgparser = ConfigParser()\n...\ncfgparser.optionxform = str\n
\n
\n

Note that when reading configuration files, whitespace around the\noption names are stripped before optionxform() is called.

\n
\n\n
\n
\n

13.2.2. ConfigParser Objects

\n

The ConfigParser class extends some methods of the\nRawConfigParser interface, adding some optional arguments.

\n
\n
\nConfigParser.get(section, option[, raw[, vars]])
\n

Get an option value for the named section. If vars is provided, it\nmust be a dictionary. The option is looked up in vars (if provided),\nsection, and in defaults in that order.

\n

All the '%' interpolations are expanded in the return values, unless the\nraw argument is true. Values for interpolation keys are looked up in the\nsame manner as the option.

\n
\n\n
\n
\nConfigParser.items(section[, raw[, vars]])
\n

Return a list of (name, value) pairs for each option in the given section.\nOptional arguments have the same meaning as for the get() method.

\n

\nNew in version 2.3.

\n
\n\n
\n
\n

13.2.3. SafeConfigParser Objects

\n

The SafeConfigParser class implements the same extended interface as\nConfigParser, with the following addition:

\n
\n
\nSafeConfigParser.set(section, option, value)
\n

If the given section exists, set the given option to the specified value;\notherwise raise NoSectionError. value must be a string (str\nor unicode); if not, TypeError is raised.

\n

\nNew in version 2.4.

\n
\n\n
\n
\n

13.2.4. Examples

\n

An example of writing to a configuration file:

\n
import ConfigParser\n\nconfig = ConfigParser.RawConfigParser()\n\n# When adding sections or items, add them in the reverse order of\n# how you want them to be displayed in the actual file.\n# In addition, please note that using RawConfigParser's and the raw\n# mode of ConfigParser's respective set functions, you can assign\n# non-string values to keys internally, but will receive an error\n# when attempting to write to a file or when you get it in non-raw\n# mode. SafeConfigParser does not allow such assignments to take place.\nconfig.add_section('Section1')\nconfig.set('Section1', 'int', '15')\nconfig.set('Section1', 'bool', 'true')\nconfig.set('Section1', 'float', '3.1415')\nconfig.set('Section1', 'baz', 'fun')\nconfig.set('Section1', 'bar', 'Python')\nconfig.set('Section1', 'foo', '%(bar)s is %(baz)s!')\n\n# Writing our configuration file to 'example.cfg'\nwith open('example.cfg', 'wb') as configfile:\n    config.write(configfile)\n
\n
\n

An example of reading the configuration file again:

\n
import ConfigParser\n\nconfig = ConfigParser.RawConfigParser()\nconfig.read('example.cfg')\n\n# getfloat() raises an exception if the value is not a float\n# getint() and getboolean() also do this for their respective types\nfloat = config.getfloat('Section1', 'float')\nint = config.getint('Section1', 'int')\nprint float + int\n\n# Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.\n# This is because we are using a RawConfigParser().\nif config.getboolean('Section1', 'bool'):\n    print config.get('Section1', 'foo')\n
\n
\n

To get interpolation, you will need to use a ConfigParser or\nSafeConfigParser:

\n
import ConfigParser\n\nconfig = ConfigParser.ConfigParser()\nconfig.read('example.cfg')\n\n# Set the third, optional argument of get to 1 if you wish to use raw mode.\nprint config.get('Section1', 'foo', 0) # -> "Python is fun!"\nprint config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!"\n\n# The optional fourth argument is a dict with members that will take\n# precedence in interpolation.\nprint config.get('Section1', 'foo', 0, {'bar': 'Documentation',\n                                        'baz': 'evil'})\n
\n
\n

Defaults are available in all three types of ConfigParsers. They are used in\ninterpolation if an option used is not defined elsewhere.

\n
import ConfigParser\n\n# New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each\nconfig = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})\nconfig.read('example.cfg')\n\nprint config.get('Section1', 'foo') # -> "Python is fun!"\nconfig.remove_option('Section1', 'bar')\nconfig.remove_option('Section1', 'baz')\nprint config.get('Section1', 'foo') # -> "Life is hard!"\n
\n
\n

The function opt_move below can be used to move options between sections:

\n
def opt_move(config, section1, section2, option):\n    try:\n        config.set(section2, option, config.get(section1, option, 1))\n    except ConfigParser.NoSectionError:\n        # Create non-existent section\n        config.add_section(section2)\n        opt_move(config, section1, section2, option)\n    else:\n        config.remove_option(section1, option)\n
\n
\n

Some configuration files are known to include settings without values, but which\notherwise conform to the syntax supported by ConfigParser. The\nallow_no_value parameter to the constructor can be used to indicate that such\nvalues should be accepted:

\n
>>> import ConfigParser\n>>> import io\n\n>>> sample_config = """\n... [mysqld]\n... user = mysql\n... pid-file = /var/run/mysqld/mysqld.pid\n... skip-external-locking\n... old_passwords = 1\n... skip-bdb\n... skip-innodb\n... """\n>>> config = ConfigParser.RawConfigParser(allow_no_value=True)\n>>> config.readfp(io.BytesIO(sample_config))\n\n>>> # Settings with values are treated as before:\n>>> config.get("mysqld", "user")\n'mysql'\n\n>>> # Settings without values provide None:\n>>> config.get("mysqld", "skip-bdb")\n\n>>> # Settings which aren't specified still raise an error:\n>>> config.get("mysqld", "does-not-exist")\nTraceback (most recent call last):\n  ...\nConfigParser.NoOptionError: No option 'does-not-exist' in section: 'mysqld'\n
\n
\n
\n
", "searchableItems": [ { "name": "ConfigParser.ConfigParser", "domId": "ConfigParser_ConfigParser.ConfigParser" }, { "name": "ConfigParser.ConfigParser.get", "domId": "ConfigParser_ConfigParser.ConfigParser.get" }, { "name": "ConfigParser.ConfigParser.items", "domId": "ConfigParser_ConfigParser.ConfigParser.items" }, { "name": "ConfigParser.RawConfigParser", "domId": "ConfigParser_ConfigParser.RawConfigParser" }, { "name": "ConfigParser.RawConfigParser.add_section", "domId": "ConfigParser_ConfigParser.RawConfigParser.add_section" }, { "name": "ConfigParser.RawConfigParser.defaults", "domId": "ConfigParser_ConfigParser.RawConfigParser.defaults" }, { "name": "ConfigParser.RawConfigParser.get", "domId": "ConfigParser_ConfigParser.RawConfigParser.get" }, { "name": "ConfigParser.RawConfigParser.getboolean", "domId": "ConfigParser_ConfigParser.RawConfigParser.getboolean" }, { "name": "ConfigParser.RawConfigParser.getfloat", "domId": "ConfigParser_ConfigParser.RawConfigParser.getfloat" }, { "name": "ConfigParser.RawConfigParser.getint", "domId": "ConfigParser_ConfigParser.RawConfigParser.getint" }, { "name": "ConfigParser.RawConfigParser.has_option", "domId": "ConfigParser_ConfigParser.RawConfigParser.has_option" }, { "name": "ConfigParser.RawConfigParser.has_section", "domId": "ConfigParser_ConfigParser.RawConfigParser.has_section" }, { "name": "ConfigParser.RawConfigParser.items", "domId": "ConfigParser_ConfigParser.RawConfigParser.items" }, { "name": "ConfigParser.RawConfigParser.options", "domId": "ConfigParser_ConfigParser.RawConfigParser.options" }, { "name": "ConfigParser.RawConfigParser.optionxform", "domId": "ConfigParser_ConfigParser.RawConfigParser.optionxform" }, { "name": "ConfigParser.RawConfigParser.read", "domId": "ConfigParser_ConfigParser.RawConfigParser.read" }, { "name": "ConfigParser.RawConfigParser.readfp", "domId": "ConfigParser_ConfigParser.RawConfigParser.readfp" }, { "name": "ConfigParser.RawConfigParser.remove_option", "domId": "ConfigParser_ConfigParser.RawConfigParser.remove_option" }, { "name": "ConfigParser.RawConfigParser.remove_section", "domId": "ConfigParser_ConfigParser.RawConfigParser.remove_section" }, { "name": "ConfigParser.RawConfigParser.sections", "domId": "ConfigParser_ConfigParser.RawConfigParser.sections" }, { "name": "ConfigParser.RawConfigParser.set", "domId": "ConfigParser_ConfigParser.RawConfigParser.set" }, { "name": "ConfigParser.RawConfigParser.write", "domId": "ConfigParser_ConfigParser.RawConfigParser.write" }, { "name": "ConfigParser.SafeConfigParser", "domId": "ConfigParser_ConfigParser.SafeConfigParser" }, { "name": "ConfigParser.SafeConfigParser.set", "domId": "ConfigParser_ConfigParser.SafeConfigParser.set" } ] }, { "url": "http://docs.python.org/library/csv.html", "title": "csv", "html": "
\n

13.1. csv — CSV File Reading and Writing

\n

\nNew in version 2.3.

\n

The so-called CSV (Comma Separated Values) format is the most common import and\nexport format for spreadsheets and databases. There is no “CSV standard”, so\nthe format is operationally defined by the many applications which read and\nwrite it. The lack of a standard means that subtle differences often exist in\nthe data produced and consumed by different applications. These differences can\nmake it annoying to process CSV files from multiple sources. Still, while the\ndelimiters and quoting characters vary, the overall format is similar enough\nthat it is possible to write a single module which can efficiently manipulate\nsuch data, hiding the details of reading and writing the data from the\nprogrammer.

\n

The csv module implements classes to read and write tabular data in CSV\nformat. It allows programmers to say, “write this data in the format preferred\nby Excel,” or “read data from this file which was generated by Excel,” without\nknowing the precise details of the CSV format used by Excel. Programmers can\nalso describe the CSV formats understood by other applications or define their\nown special-purpose CSV formats.

\n

The csv module’s reader and writer objects read and\nwrite sequences. Programmers can also read and write data in dictionary form\nusing the DictReader and DictWriter classes.

\n
\n

Note

\n

This version of the csv module doesn’t support Unicode input. Also,\nthere are currently some issues regarding ASCII NUL characters. Accordingly,\nall input should be UTF-8 or printable ASCII to be safe; see the examples in\nsection Examples. These restrictions will be removed in the future.

\n
\n
\n

See also

\n
\n
PEP 305 - CSV File API
\n
The Python Enhancement Proposal which proposed this addition to Python.
\n
\n
\n
\n

13.1.1. Module Contents

\n

The csv module defines the following functions:

\n
\n
\ncsv.reader(csvfile[, dialect='excel'][, fmtparam])
\n

Return a reader object which will iterate over lines in the given csvfile.\ncsvfile can be any object which supports the iterator protocol and returns a\nstring each time its next() method is called — file objects and list\nobjects are both suitable. If csvfile is a file object, it must be opened\nwith the ‘b’ flag on platforms where that makes a difference. An optional\ndialect parameter can be given which is used to define a set of parameters\nspecific to a particular CSV dialect. It may be an instance of a subclass of\nthe Dialect class or one of the strings returned by the\nlist_dialects() function. The other optional fmtparam keyword arguments\ncan be given to override individual formatting parameters in the current\ndialect. For full details about the dialect and formatting parameters, see\nsection Dialects and Formatting Parameters.

\n

Each row read from the csv file is returned as a list of strings. No\nautomatic data type conversion is performed.

\n

A short usage example:

\n
>>> import csv\n>>> spamReader = csv.reader(open('eggs.csv', 'rb'), delimiter=' ', quotechar='|')\n>>> for row in spamReader:\n...     print ', '.join(row)\nSpam, Spam, Spam, Spam, Spam, Baked Beans\nSpam, Lovely Spam, Wonderful Spam\n
\n
\n

\nChanged in version 2.5: The parser is now stricter with respect to multi-line quoted fields. Previously,\nif a line ended within a quoted field without a terminating newline character, a\nnewline would be inserted into the returned field. This behavior caused problems\nwhen reading files which contained carriage return characters within fields.\nThe behavior was changed to return the field without inserting newlines. As a\nconsequence, if newlines embedded within fields are important, the input should\nbe split into lines in a manner which preserves the newline characters.

\n
\n\n
\n
\ncsv.writer(csvfile[, dialect='excel'][, fmtparam])
\n

Return a writer object responsible for converting the user’s data into delimited\nstrings on the given file-like object. csvfile can be any object with a\nwrite() method. If csvfile is a file object, it must be opened with the\n‘b’ flag on platforms where that makes a difference. An optional dialect\nparameter can be given which is used to define a set of parameters specific to a\nparticular CSV dialect. It may be an instance of a subclass of the\nDialect class or one of the strings returned by the\nlist_dialects() function. The other optional fmtparam keyword arguments\ncan be given to override individual formatting parameters in the current\ndialect. For full details about the dialect and formatting parameters, see\nsection Dialects and Formatting Parameters. To make it\nas easy as possible to interface with modules which implement the DB API, the\nvalue None is written as the empty string. While this isn’t a\nreversible transformation, it makes it easier to dump SQL NULL data values to\nCSV files without preprocessing the data returned from a cursor.fetch* call.\nAll other non-string data are stringified with str() before being written.

\n

A short usage example:

\n
>>> import csv\n>>> spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',\n...                         quotechar='|', quoting=csv.QUOTE_MINIMAL)\n>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])\n>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])\n
\n
\n
\n\n
\n
\ncsv.register_dialect(name[, dialect][, fmtparam])
\n
Associate dialect with name. name must be a string or Unicode object. The\ndialect can be specified either by passing a sub-class of Dialect, or\nby fmtparam keyword arguments, or both, with keyword arguments overriding\nparameters of the dialect. For full details about the dialect and formatting\nparameters, see section Dialects and Formatting Parameters.
\n\n
\n
\ncsv.unregister_dialect(name)
\n
Delete the dialect associated with name from the dialect registry. An\nError is raised if name is not a registered dialect name.
\n\n
\n
\ncsv.get_dialect(name)
\n

Return the dialect associated with name. An Error is raised if name\nis not a registered dialect name.

\n

\nChanged in version 2.5: This function now returns an immutable Dialect. Previously an\ninstance of the requested dialect was returned. Users could modify the\nunderlying class, changing the behavior of active readers and writers.

\n
\n\n
\n
\ncsv.list_dialects()
\n
Return the names of all registered dialects.
\n\n
\n
\ncsv.field_size_limit([new_limit])
\n

Returns the current maximum field size allowed by the parser. If new_limit is\ngiven, this becomes the new limit.

\n

\nNew in version 2.5.

\n
\n\n

The csv module defines the following classes:

\n
\n
\nclass csv.DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])
\n
Create an object which operates like a regular reader but maps the information\nread into a dict whose keys are given by the optional fieldnames parameter.\nIf the fieldnames parameter is omitted, the values in the first row of the\ncsvfile will be used as the fieldnames. If the row read has more fields\nthan the fieldnames sequence, the remaining data is added as a sequence\nkeyed by the value of restkey. If the row read has fewer fields than the\nfieldnames sequence, the remaining keys take the value of the optional\nrestval parameter. Any other optional or keyword arguments are passed to\nthe underlying reader instance.
\n\n
\n
\nclass csv.DictWriter(csvfile, fieldnames[, restval=''[, extrasaction='raise'[, dialect='excel'[, *args, **kwds]]]])
\n

Create an object which operates like a regular writer but maps dictionaries onto\noutput rows. The fieldnames parameter identifies the order in which values in\nthe dictionary passed to the writerow() method are written to the\ncsvfile. The optional restval parameter specifies the value to be written\nif the dictionary is missing a key in fieldnames. If the dictionary passed to\nthe writerow() method contains a key not found in fieldnames, the\noptional extrasaction parameter indicates what action to take. If it is set\nto 'raise' a ValueError is raised. If it is set to 'ignore',\nextra values in the dictionary are ignored. Any other optional or keyword\narguments are passed to the underlying writer instance.

\n

Note that unlike the DictReader class, the fieldnames parameter of\nthe DictWriter is not optional. Since Python’s dict objects\nare not ordered, there is not enough information available to deduce the order\nin which the row should be written to the csvfile.

\n
\n\n
\n
\nclass csv.Dialect
\n
The Dialect class is a container class relied on primarily for its\nattributes, which are used to define the parameters for a specific\nreader or writer instance.
\n\n
\n
\nclass csv.excel
\n
The excel class defines the usual properties of an Excel-generated CSV\nfile. It is registered with the dialect name 'excel'.
\n\n
\n
\nclass csv.excel_tab
\n
The excel_tab class defines the usual properties of an Excel-generated\nTAB-delimited file. It is registered with the dialect name 'excel-tab'.
\n\n
\n
\nclass csv.Sniffer
\n

The Sniffer class is used to deduce the format of a CSV file.

\n

The Sniffer class provides two methods:

\n
\n
\nsniff(sample[, delimiters=None])
\n
Analyze the given sample and return a Dialect subclass\nreflecting the parameters found. If the optional delimiters parameter\nis given, it is interpreted as a string containing possible valid\ndelimiter characters.
\n\n
\n
\nhas_header(sample)
\n
Analyze the sample text (presumed to be in CSV format) and return\nTrue if the first row appears to be a series of column headers.
\n\n
\n\n

An example for Sniffer use:

\n
csvfile = open("example.csv", "rb")\ndialect = csv.Sniffer().sniff(csvfile.read(1024))\ncsvfile.seek(0)\nreader = csv.reader(csvfile, dialect)\n# ... process CSV file contents here ...\n
\n
\n

The csv module defines the following constants:

\n
\n
\ncsv.QUOTE_ALL
\n
Instructs writer objects to quote all fields.
\n\n
\n
\ncsv.QUOTE_MINIMAL
\n
Instructs writer objects to only quote those fields which contain\nspecial characters such as delimiter, quotechar or any of the characters in\nlineterminator.
\n\n
\n
\ncsv.QUOTE_NONNUMERIC
\n

Instructs writer objects to quote all non-numeric fields.

\n

Instructs the reader to convert all non-quoted fields to type float.

\n
\n\n
\n
\ncsv.QUOTE_NONE
\n

Instructs writer objects to never quote fields. When the current\ndelimiter occurs in output data it is preceded by the current escapechar\ncharacter. If escapechar is not set, the writer will raise Error if\nany characters that require escaping are encountered.

\n

Instructs reader to perform no special processing of quote characters.

\n
\n\n

The csv module defines the following exception:

\n
\n
\nexception csv.Error
\n
Raised by any of the functions when an error is detected.
\n\n
\n
\n

13.1.2. Dialects and Formatting Parameters

\n

To make it easier to specify the format of input and output records, specific\nformatting parameters are grouped together into dialects. A dialect is a\nsubclass of the Dialect class having a set of specific methods and a\nsingle validate() method. When creating reader or\nwriter objects, the programmer can specify a string or a subclass of\nthe Dialect class as the dialect parameter. In addition to, or instead\nof, the dialect parameter, the programmer can also specify individual\nformatting parameters, which have the same names as the attributes defined below\nfor the Dialect class.

\n

Dialects support the following attributes:

\n
\n
\nDialect.delimiter
\n
A one-character string used to separate fields. It defaults to ','.
\n\n
\n
\nDialect.doublequote
\n

Controls how instances of quotechar appearing inside a field should be\nthemselves be quoted. When True, the character is doubled. When\nFalse, the escapechar is used as a prefix to the quotechar. It\ndefaults to True.

\n

On output, if doublequote is False and no escapechar is set,\nError is raised if a quotechar is found in a field.

\n
\n\n
\n
\nDialect.escapechar
\n
A one-character string used by the writer to escape the delimiter if quoting\nis set to QUOTE_NONE and the quotechar if doublequote is\nFalse. On reading, the escapechar removes any special meaning from\nthe following character. It defaults to None, which disables escaping.
\n\n
\n
\nDialect.lineterminator
\n

The string used to terminate lines produced by the writer. It defaults\nto '\\r\\n'.

\n
\n

Note

\n

The reader is hard-coded to recognise either '\\r' or '\\n' as\nend-of-line, and ignores lineterminator. This behavior may change in the\nfuture.

\n
\n
\n\n
\n
\nDialect.quotechar
\n
A one-character string used to quote fields containing special characters, such\nas the delimiter or quotechar, or which contain new-line characters. It\ndefaults to '"'.
\n\n
\n
\nDialect.quoting
\n
Controls when quotes should be generated by the writer and recognised by the\nreader. It can take on any of the QUOTE_* constants (see section\nModule Contents) and defaults to QUOTE_MINIMAL.
\n\n
\n
\nDialect.skipinitialspace
\n
When True, whitespace immediately following the delimiter is ignored.\nThe default is False.
\n\n
\n
\n

13.1.3. Reader Objects

\n

Reader objects (DictReader instances and objects returned by the\nreader() function) have the following public methods:

\n
\n
\ncsvreader.next()
\n
Return the next row of the reader’s iterable object as a list, parsed according\nto the current dialect.
\n\n

Reader objects have the following public attributes:

\n
\n
\ncsvreader.dialect
\n
A read-only description of the dialect in use by the parser.
\n\n
\n
\ncsvreader.line_num
\n

The number of lines read from the source iterator. This is not the same as the\nnumber of records returned, as records can span multiple lines.

\n

\nNew in version 2.5.

\n
\n\n

DictReader objects have the following public attribute:

\n
\n
\ncsvreader.fieldnames
\n

If not passed as a parameter when creating the object, this attribute is\ninitialized upon first access or when the first record is read from the\nfile.

\n

\nChanged in version 2.6.

\n
\n\n
\n
\n

13.1.4. Writer Objects

\n

Writer objects (DictWriter instances and objects returned by\nthe writer() function) have the following public methods. A row must be\na sequence of strings or numbers for Writer objects and a dictionary\nmapping fieldnames to strings or numbers (by passing them through str()\nfirst) for DictWriter objects. Note that complex numbers are written\nout surrounded by parens. This may cause some problems for other programs which\nread CSV files (assuming they support complex numbers at all).

\n
\n
\ncsvwriter.writerow(row)
\n
Write the row parameter to the writer’s file object, formatted according to\nthe current dialect.
\n\n
\n
\ncsvwriter.writerows(rows)
\n
Write all the rows parameters (a list of row objects as described above) to\nthe writer’s file object, formatted according to the current dialect.
\n\n

Writer objects have the following public attribute:

\n
\n
\ncsvwriter.dialect
\n
A read-only description of the dialect in use by the writer.
\n\n

DictWriter objects have the following public method:

\n
\n
\nDictWriter.writeheader()
\n

Write a row with the field names (as specified in the constructor).

\n

\nNew in version 2.7.

\n
\n\n
\n
\n

13.1.5. Examples

\n

The simplest example of reading a CSV file:

\n
import csv\nwith open('some.csv', 'rb') as f:\n    reader = csv.reader(f)\n    for row in reader:\n        print row\n
\n
\n

Reading a file with an alternate format:

\n
import csv\nwith open('passwd', 'rb') as f:\n    reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)\n    for row in reader:\n        print row\n
\n
\n

The corresponding simplest possible writing example is:

\n
import csv\nwith open('some.csv', 'wb') as f:\n    writer = csv.writer(f)\n    writer.writerows(someiterable)\n
\n
\n

Registering a new dialect:

\n
import csv\ncsv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)\nwith open('passwd', 'rb') as f:\n    reader = csv.reader(f, 'unixpwd')\n
\n
\n

A slightly more advanced use of the reader — catching and reporting errors:

\n
import csv, sys\nfilename = 'some.csv'\nwith open(filename, 'rb') as f:\n    reader = csv.reader(f)\n    try:\n        for row in reader:\n            print row\n    except csv.Error, e:\n        sys.exit('file %s, line %d: %s'  (filename, reader.line_num, e))\n
\n
\n

And while the module doesn’t directly support parsing strings, it can easily be\ndone:

\n
import csv\nfor row in csv.reader(['one,two,three']):\n    print row\n
\n
\n

The csv module doesn’t directly support reading and writing Unicode, but\nit is 8-bit-clean save for some problems with ASCII NUL characters. So you can\nwrite functions or classes that handle the encoding and decoding for you as long\nas you avoid encodings like UTF-16 that use NULs. UTF-8 is recommended.

\n

unicode_csv_reader() below is a generator that wraps csv.reader\nto handle Unicode CSV data (a list of Unicode strings). utf_8_encoder()\nis a generator that encodes the Unicode strings as UTF-8, one string (or row) at\na time. The encoded strings are parsed by the CSV reader, and\nunicode_csv_reader() decodes the UTF-8-encoded cells back into Unicode:

\n
import csv\n\ndef unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):\n    # csv.py doesn't do Unicode; encode temporarily as UTF-8:\n    csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),\n                            dialect=dialect, **kwargs)\n    for row in csv_reader:\n        # decode UTF-8 back to Unicode, cell by cell:\n        yield [unicode(cell, 'utf-8') for cell in row]\n\ndef utf_8_encoder(unicode_csv_data):\n    for line in unicode_csv_data:\n        yield line.encode('utf-8')\n
\n
\n

For all other encodings the following UnicodeReader and\nUnicodeWriter classes can be used. They take an additional encoding\nparameter in their constructor and make sure that the data passes the real\nreader or writer encoded as UTF-8:

\n
import csv, codecs, cStringIO\n\nclass UTF8Recoder:\n    """\n    Iterator that reads an encoded stream and reencodes the input to UTF-8\n    """\n    def __init__(self, f, encoding):\n        self.reader = codecs.getreader(encoding)(f)\n\n    def __iter__(self):\n        return self\n\n    def next(self):\n        return self.reader.next().encode("utf-8")\n\nclass UnicodeReader:\n    """\n    A CSV reader which will iterate over lines in the CSV file "f",\n    which is encoded in the given encoding.\n    """\n\n    def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):\n        f = UTF8Recoder(f, encoding)\n        self.reader = csv.reader(f, dialect=dialect, **kwds)\n\n    def next(self):\n        row = self.reader.next()\n        return [unicode(s, "utf-8") for s in row]\n\n    def __iter__(self):\n        return self\n\nclass UnicodeWriter:\n    """\n    A CSV writer which will write rows to CSV file "f",\n    which is encoded in the given encoding.\n    """\n\n    def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):\n        # Redirect output to a queue\n        self.queue = cStringIO.StringIO()\n        self.writer = csv.writer(self.queue, dialect=dialect, **kwds)\n        self.stream = f\n        self.encoder = codecs.getincrementalencoder(encoding)()\n\n    def writerow(self, row):\n        self.writer.writerow([s.encode("utf-8") for s in row])\n        # Fetch UTF-8 output from the queue ...\n        data = self.queue.getvalue()\n        data = data.decode("utf-8")\n        # ... and reencode it into the target encoding\n        data = self.encoder.encode(data)\n        # write to the target stream\n        self.stream.write(data)\n        # empty queue\n        self.queue.truncate(0)\n\n    def writerows(self, rows):\n        for row in rows:\n            self.writerow(row)\n
\n
\n
\n
", "searchableItems": [ { "name": "csv.csvreader.next", "domId": "csv_csv.csvreader.next" }, { "name": "csv.csvwriter.writerow", "domId": "csv_csv.csvwriter.writerow" }, { "name": "csv.csvwriter.writerows", "domId": "csv_csv.csvwriter.writerows" }, { "name": "csv.Dialect", "domId": "csv_csv.Dialect" }, { "name": "csv.DictReader", "domId": "csv_csv.DictReader" }, { "name": "csv.DictWriter", "domId": "csv_csv.DictWriter" }, { "name": "csv.DictWriter.writeheader", "domId": "csv_csv.DictWriter.writeheader" }, { "name": "csv.excel", "domId": "csv_csv.excel" }, { "name": "csv.excel_tab", "domId": "csv_csv.excel_tab" }, { "name": "csv.field_size_limit", "domId": "csv_csv.field_size_limit" }, { "name": "csv.get_dialect", "domId": "csv_csv.get_dialect" }, { "name": "csv.list_dialects", "domId": "csv_csv.list_dialects" }, { "name": "csv.reader", "domId": "csv_csv.reader" }, { "name": "csv.register_dialect", "domId": "csv_csv.register_dialect" }, { "name": "csv.Sniffer", "domId": "csv_csv.Sniffer" }, { "name": "csv.Sniffer.has_header", "domId": "csv_csv.Sniffer.has_header" }, { "name": "csv.Sniffer.sniff", "domId": "csv_csv.Sniffer.sniff" }, { "name": "csv.unregister_dialect", "domId": "csv_csv.unregister_dialect" }, { "name": "csv.writer", "domId": "csv_csv.writer" } ] }, { "url": "http://docs.python.org/library/sqlite3.html", "title": "sqlite3", "html": "
\n

11.13. sqlite3 — DB-API 2.0 interface for SQLite databases

\n

\nNew in version 2.5.

\n

SQLite is a C library that provides a lightweight disk-based database that\ndoesn’t require a separate server process and allows accessing the database\nusing a nonstandard variant of the SQL query language. Some applications can use\nSQLite for internal data storage. It’s also possible to prototype an\napplication using SQLite and then port the code to a larger database such as\nPostgreSQL or Oracle.

\n

sqlite3 was written by Gerhard Häring and provides a SQL interface compliant\nwith the DB-API 2.0 specification described by PEP 249.

\n

To use the module, you must first create a Connection object that\nrepresents the database. Here the data will be stored in the\n/tmp/example file:

\n
conn = sqlite3.connect('/tmp/example')\n
\n
\n

You can also supply the special name :memory: to create a database in RAM.

\n

Once you have a Connection, you can create a Cursor object\nand call its execute() method to perform SQL commands:

\n
c = conn.cursor()\n\n# Create table\nc.execute('''create table stocks\n(date text, trans text, symbol text,\n qty real, price real)''')\n\n# Insert a row of data\nc.execute("""insert into stocks\n          values ('2006-01-05','BUY','RHAT',100,35.14)""")\n\n# Save (commit) the changes\nconn.commit()\n\n# We can also close the cursor if we are done with it\nc.close()\n
\n
\n

Usually your SQL operations will need to use values from Python variables. You\nshouldn’t assemble your query using Python’s string operations because doing so\nis insecure; it makes your program vulnerable to an SQL injection attack.

\n

Instead, use the DB-API’s parameter substitution. Put ? as a placeholder\nwherever you want to use a value, and then provide a tuple of values as the\nsecond argument to the cursor’s execute() method. (Other database\nmodules may use a different placeholder, such as %s or :1.) For\nexample:

\n
# Never do this -- insecure!\nsymbol = 'IBM'\nc.execute("... where symbol = '%s'"  symbol)\n\n# Do this instead\nt = (symbol,)\nc.execute('select * from stocks where symbol=?', t)\n\n# Larger example\nfor t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),\n          ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),\n          ('2006-04-06', 'SELL', 'IBM', 500, 53.00),\n         ]:\n    c.execute('insert into stocks values (?,?,?,?,?)', t)\n
\n
\n

To retrieve data after executing a SELECT statement, you can either treat the\ncursor as an iterator, call the cursor’s fetchone() method to\nretrieve a single matching row, or call fetchall() to get a list of the\nmatching rows.

\n

This example uses the iterator form:

\n
>>> c = conn.cursor()\n>>> c.execute('select * from stocks order by price')\n>>> for row in c:\n...    print row\n...\n(u'2006-01-05', u'BUY', u'RHAT', 100, 35.14)\n(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)\n(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)\n(u'2006-04-05', u'BUY', u'MSFT', 1000, 72.0)\n>>>\n
\n
\n
\n

See also

\n
\n
http://code.google.com/p/pysqlite/
\n
The pysqlite web page – sqlite3 is developed externally under the name\n“pysqlite”.
\n
http://www.sqlite.org
\n
The SQLite web page; the documentation describes the syntax and the\navailable data types for the supported SQL dialect.
\n
PEP 249 - Database API Specification 2.0
\n
PEP written by Marc-André Lemburg.
\n
\n
\n
\n

11.13.1. Module functions and constants

\n
\n
\nsqlite3.PARSE_DECLTYPES
\n

This constant is meant to be used with the detect_types parameter of the\nconnect() function.

\n

Setting it makes the sqlite3 module parse the declared type for each\ncolumn it returns. It will parse out the first word of the declared type,\ni. e. for “integer primary key”, it will parse out “integer”, or for\n“number(10)” it will parse out “number”. Then for that column, it will look\ninto the converters dictionary and use the converter function registered for\nthat type there.

\n
\n\n
\n
\nsqlite3.PARSE_COLNAMES
\n

This constant is meant to be used with the detect_types parameter of the\nconnect() function.

\n

Setting this makes the SQLite interface parse the column name for each column it\nreturns. It will look for a string formed [mytype] in there, and then decide\nthat ‘mytype’ is the type of the column. It will try to find an entry of\n‘mytype’ in the converters dictionary and then use the converter function found\nthere to return the value. The column name found in Cursor.description\nis only the first word of the column name, i. e. if you use something like\n'as "x [datetime]"' in your SQL, then we will parse out everything until the\nfirst blank for the column name: the column name would simply be “x”.

\n
\n\n
\n
\nsqlite3.connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements])
\n

Opens a connection to the SQLite database file database. You can use\n":memory:" to open a database connection to a database that resides in RAM\ninstead of on disk.

\n

When a database is accessed by multiple connections, and one of the processes\nmodifies the database, the SQLite database is locked until that transaction is\ncommitted. The timeout parameter specifies how long the connection should wait\nfor the lock to go away until raising an exception. The default for the timeout\nparameter is 5.0 (five seconds).

\n

For the isolation_level parameter, please see the\nConnection.isolation_level property of Connection objects.

\n

SQLite natively supports only the types TEXT, INTEGER, FLOAT, BLOB and NULL. If\nyou want to use other types you must add support for them yourself. The\ndetect_types parameter and the using custom converters registered with the\nmodule-level register_converter() function allow you to easily do that.

\n

detect_types defaults to 0 (i. e. off, no type detection), you can set it to\nany combination of PARSE_DECLTYPES and PARSE_COLNAMES to turn\ntype detection on.

\n

By default, the sqlite3 module uses its Connection class for the\nconnect call. You can, however, subclass the Connection class and make\nconnect() use your class instead by providing your class for the factory\nparameter.

\n

Consult the section SQLite and Python types of this manual for details.

\n

The sqlite3 module internally uses a statement cache to avoid SQL parsing\noverhead. If you want to explicitly set the number of statements that are cached\nfor the connection, you can set the cached_statements parameter. The currently\nimplemented default is to cache 100 statements.

\n
\n\n
\n
\nsqlite3.register_converter(typename, callable)
\n
Registers a callable to convert a bytestring from the database into a custom\nPython type. The callable will be invoked for all database values that are of\nthe type typename. Confer the parameter detect_types of the connect()\nfunction for how the type detection works. Note that the case of typename and\nthe name of the type in your query must match!
\n\n
\n
\nsqlite3.register_adapter(type, callable)
\n
Registers a callable to convert the custom Python type type into one of\nSQLite’s supported types. The callable callable accepts as single parameter\nthe Python value, and must return a value of the following types: int, long,\nfloat, str (UTF-8 encoded), unicode or buffer.
\n\n
\n
\nsqlite3.complete_statement(sql)
\n

Returns True if the string sql contains one or more complete SQL\nstatements terminated by semicolons. It does not verify that the SQL is\nsyntactically correct, only that there are no unclosed string literals and the\nstatement is terminated by a semicolon.

\n

This can be used to build a shell for SQLite, as in the following example:

\n
# A minimal SQLite shell for experiments\n\nimport sqlite3\n\ncon = sqlite3.connect(":memory:")\ncon.isolation_level = None\ncur = con.cursor()\n\nbuffer = ""\n\nprint "Enter your SQL commands to execute in sqlite3."\nprint "Enter a blank line to exit."\n\nwhile True:\n    line = raw_input()\n    if line == "":\n        break\n    buffer += line\n    if sqlite3.complete_statement(buffer):\n        try:\n            buffer = buffer.strip()\n            cur.execute(buffer)\n\n            if buffer.lstrip().upper().startswith("SELECT"):\n                print cur.fetchall()\n        except sqlite3.Error, e:\n            print "An error occurred:", e.args[0]\n        buffer = ""\n\ncon.close()\n
\n
\n
\n\n
\n
\nsqlite3.enable_callback_tracebacks(flag)
\n
By default you will not get any tracebacks in user-defined functions,\naggregates, converters, authorizer callbacks etc. If you want to debug them, you\ncan call this function with flag as True. Afterwards, you will get tracebacks\nfrom callbacks on sys.stderr. Use False to disable the feature\nagain.
\n\n
\n
\n

11.13.2. Connection Objects

\n
\n
\nclass sqlite3.Connection
\n
A SQLite database connection has the following attributes and methods:
\n\n
\n
\nConnection.isolation_level
\n
Get or set the current isolation level. None for autocommit mode or\none of “DEFERRED”, “IMMEDIATE” or “EXCLUSIVE”. See section\nControlling Transactions for a more detailed explanation.
\n\n
\n
\nConnection.cursor([cursorClass])
\n
The cursor method accepts a single optional parameter cursorClass. If\nsupplied, this must be a custom cursor class that extends\nsqlite3.Cursor.
\n\n
\n
\nConnection.commit()
\n
This method commits the current transaction. If you don’t call this method,\nanything you did since the last call to commit() is not visible from\nother database connections. If you wonder why you don’t see the data you’ve\nwritten to the database, please check you didn’t forget to call this method.
\n\n
\n
\nConnection.rollback()
\n
This method rolls back any changes to the database since the last call to\ncommit().
\n\n
\n
\nConnection.close()
\n
This closes the database connection. Note that this does not automatically\ncall commit(). If you just close your database connection without\ncalling commit() first, your changes will be lost!
\n\n
\n
\nConnection.execute(sql[, parameters])
\n
This is a nonstandard shortcut that creates an intermediate cursor object by\ncalling the cursor method, then calls the cursor’s execute method with the parameters given.
\n\n
\n
\nConnection.executemany(sql[, parameters])
\n
This is a nonstandard shortcut that creates an intermediate cursor object by\ncalling the cursor method, then calls the cursor’s executemany method with the parameters given.
\n\n
\n
\nConnection.executescript(sql_script)
\n
This is a nonstandard shortcut that creates an intermediate cursor object by\ncalling the cursor method, then calls the cursor’s executescript method with the parameters given.
\n\n
\n
\nConnection.create_function(name, num_params, func)
\n

Creates a user-defined function that you can later use from within SQL\nstatements under the function name name. num_params is the number of\nparameters the function accepts, and func is a Python callable that is called\nas the SQL function.

\n

The function can return any of the types supported by SQLite: unicode, str, int,\nlong, float, buffer and None.

\n

Example:

\n
import sqlite3\nimport md5\n\ndef md5sum(t):\n    return md5.md5(t).hexdigest()\n\ncon = sqlite3.connect(":memory:")\ncon.create_function("md5", 1, md5sum)\ncur = con.cursor()\ncur.execute("select md5(?)", ("foo",))\nprint cur.fetchone()[0]\n
\n
\n
\n\n
\n
\nConnection.create_aggregate(name, num_params, aggregate_class)
\n

Creates a user-defined aggregate function.

\n

The aggregate class must implement a step method, which accepts the number\nof parameters num_params, and a finalize method which will return the\nfinal result of the aggregate.

\n

The finalize method can return any of the types supported by SQLite:\nunicode, str, int, long, float, buffer and None.

\n

Example:

\n
import sqlite3\n\nclass MySum:\n    def __init__(self):\n        self.count = 0\n\n    def step(self, value):\n        self.count += value\n\n    def finalize(self):\n        return self.count\n\ncon = sqlite3.connect(":memory:")\ncon.create_aggregate("mysum", 1, MySum)\ncur = con.cursor()\ncur.execute("create table test(i)")\ncur.execute("insert into test(i) values (1)")\ncur.execute("insert into test(i) values (2)")\ncur.execute("select mysum(i) from test")\nprint cur.fetchone()[0]\n
\n
\n
\n\n
\n
\nConnection.create_collation(name, callable)
\n

Creates a collation with the specified name and callable. The callable will\nbe passed two string arguments. It should return -1 if the first is ordered\nlower than the second, 0 if they are ordered equal and 1 if the first is ordered\nhigher than the second. Note that this controls sorting (ORDER BY in SQL) so\nyour comparisons don’t affect other SQL operations.

\n

Note that the callable will get its parameters as Python bytestrings, which will\nnormally be encoded in UTF-8.

\n

The following example shows a custom collation that sorts “the wrong way”:

\n
import sqlite3\n\ndef collate_reverse(string1, string2):\n    return -cmp(string1, string2)\n\ncon = sqlite3.connect(":memory:")\ncon.create_collation("reverse", collate_reverse)\n\ncur = con.cursor()\ncur.execute("create table test(x)")\ncur.executemany("insert into test(x) values (?)", [("a",), ("b",)])\ncur.execute("select x from test order by x collate reverse")\nfor row in cur:\n    print row\ncon.close()\n
\n
\n

To remove a collation, call create_collation with None as callable:

\n
con.create_collation("reverse", None)\n
\n
\n
\n\n
\n
\nConnection.interrupt()
\n
You can call this method from a different thread to abort any queries that might\nbe executing on the connection. The query will then abort and the caller will\nget an exception.
\n\n
\n
\nConnection.set_authorizer(authorizer_callback)
\n

This routine registers a callback. The callback is invoked for each attempt to\naccess a column of a table in the database. The callback should return\nSQLITE_OK if access is allowed, SQLITE_DENY if the entire SQL\nstatement should be aborted with an error and SQLITE_IGNORE if the\ncolumn should be treated as a NULL value. These constants are available in the\nsqlite3 module.

\n

The first argument to the callback signifies what kind of operation is to be\nauthorized. The second and third argument will be arguments or None\ndepending on the first argument. The 4th argument is the name of the database\n(“main”, “temp”, etc.) if applicable. The 5th argument is the name of the\ninner-most trigger or view that is responsible for the access attempt or\nNone if this access attempt is directly from input SQL code.

\n

Please consult the SQLite documentation about the possible values for the first\nargument and the meaning of the second and third argument depending on the first\none. All necessary constants are available in the sqlite3 module.

\n
\n\n
\n
\nConnection.set_progress_handler(handler, n)
\n

\nNew in version 2.6.

\n

This routine registers a callback. The callback is invoked for every n\ninstructions of the SQLite virtual machine. This is useful if you want to\nget called from SQLite during long-running operations, for example to update\na GUI.

\n

If you want to clear any previously installed progress handler, call the\nmethod with None for handler.

\n
\n\n
\n
\nConnection.enable_load_extension(enabled)
\n

\nNew in version 2.7.

\n

This routine allows/disallows the SQLite engine to load SQLite extensions\nfrom shared libraries. SQLite extensions can define new functions,\naggregates or whole new virtual table implementations. One well-known\nextension is the fulltext-search extension distributed with SQLite.

\n
import sqlite3\n\ncon = sqlite3.connect(":memory:")\n\n# enable extension loading\ncon.enable_load_extension(True)\n\n# Load the fulltext search extension\ncon.execute("select load_extension('./fts3.so')")\n\n# alternatively you can load the extension using an API call:\n# con.load_extension("./fts3.so")\n\n# disable extension laoding again\ncon.enable_load_extension(False)\n\n# example from SQLite wiki\ncon.execute("create virtual table recipe using fts3(name, ingredients)")\ncon.executescript("""\n    insert into recipe (name, ingredients) values ('broccoli stew', 'broccoli peppers cheese tomatoes');\n    insert into recipe (name, ingredients) values ('pumpkin stew', 'pumpkin onions garlic celery');\n    insert into recipe (name, ingredients) values ('broccoli pie', 'broccoli cheese onions flour');\n    insert into recipe (name, ingredients) values ('pumpkin pie', 'pumpkin sugar flour butter');\n    """)\nfor row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"):\n    print row\n
\n
\n

Loadable extensions are disabled by default. See [1]

\n
\n\n
\n
\nConnection.load_extension(path)
\n

\nNew in version 2.7.

\n

This routine loads a SQLite extension from a shared library. You have to\nenable extension loading with enable_load_extension() before you can\nuse this routine.

\n

Loadable extensions are disabled by default. See [1]

\n
\n\n
\n
\nConnection.row_factory
\n

You can change this attribute to a callable that accepts the cursor and the\noriginal row as a tuple and will return the real result row. This way, you can\nimplement more advanced ways of returning results, such as returning an object\nthat can also access columns by name.

\n

Example:

\n
import sqlite3\n\ndef dict_factory(cursor, row):\n    d = {}\n    for idx, col in enumerate(cursor.description):\n        d[col[0]] = row[idx]\n    return d\n\ncon = sqlite3.connect(":memory:")\ncon.row_factory = dict_factory\ncur = con.cursor()\ncur.execute("select 1 as a")\nprint cur.fetchone()["a"]\n
\n
\n

If returning a tuple doesn’t suffice and you want name-based access to\ncolumns, you should consider setting row_factory to the\nhighly-optimized sqlite3.Row type. Row provides both\nindex-based and case-insensitive name-based access to columns with almost no\nmemory overhead. It will probably be better than your own custom\ndictionary-based approach or even a db_row based solution.

\n
\n\n
\n
\nConnection.text_factory
\n

Using this attribute you can control what objects are returned for the TEXT\ndata type. By default, this attribute is set to unicode and the\nsqlite3 module will return Unicode objects for TEXT. If you want to\nreturn bytestrings instead, you can set it to str.

\n

For efficiency reasons, there’s also a way to return Unicode objects only for\nnon-ASCII data, and bytestrings otherwise. To activate it, set this attribute to\nsqlite3.OptimizedUnicode.

\n

You can also set it to any other callable that accepts a single bytestring\nparameter and returns the resulting object.

\n

See the following example code for illustration:

\n
import sqlite3\n\ncon = sqlite3.connect(":memory:")\ncur = con.cursor()\n\n# Create the table\ncon.execute("create table person(lastname, firstname)")\n\nAUSTRIA = u"\\xd6sterreich"\n\n# by default, rows are returned as Unicode\ncur.execute("select ?", (AUSTRIA,))\nrow = cur.fetchone()\nassert row[0] == AUSTRIA\n\n# but we can make sqlite3 always return bytestrings ...\ncon.text_factory = str\ncur.execute("select ?", (AUSTRIA,))\nrow = cur.fetchone()\nassert type(row[0]) == str\n# the bytestrings will be encoded in UTF-8, unless you stored garbage in the\n# database ...\nassert row[0] == AUSTRIA.encode("utf-8")\n\n# we can also implement a custom text_factory ...\n# here we implement one that will ignore Unicode characters that cannot be\n# decoded from UTF-8\ncon.text_factory = lambda x: unicode(x, "utf-8", "ignore")\ncur.execute("select ?", ("this is latin1 and would normally create errors" +\n                         u"\\xe4\\xf6\\xfc".encode("latin1"),))\nrow = cur.fetchone()\nassert type(row[0]) == unicode\n\n# sqlite3 offers a built-in optimized text_factory that will return bytestring\n# objects, if the data is in ASCII only, and otherwise return unicode objects\ncon.text_factory = sqlite3.OptimizedUnicode\ncur.execute("select ?", (AUSTRIA,))\nrow = cur.fetchone()\nassert type(row[0]) == unicode\n\ncur.execute("select ?", ("Germany",))\nrow = cur.fetchone()\nassert type(row[0]) == str\n
\n
\n
\n\n
\n
\nConnection.total_changes
\n
Returns the total number of database rows that have been modified, inserted, or\ndeleted since the database connection was opened.
\n\n
\n
\nConnection.iterdump
\n

Returns an iterator to dump the database in an SQL text format. Useful when\nsaving an in-memory database for later restoration. This function provides\nthe same capabilities as the .dump command in the sqlite3\nshell.

\n

\nNew in version 2.6.

\n

Example:

\n
# Convert file existing_db.db to SQL dump file dump.sql\nimport sqlite3, os\n\ncon = sqlite3.connect('existing_db.db')\nwith open('dump.sql', 'w') as f:\n    for line in con.iterdump():\n        f.write('%s\\n'  line)\n
\n
\n
\n\n
\n
\n

11.13.3. Cursor Objects

\n
\n
\nclass sqlite3.Cursor
\n
A Cursor instance has the following attributes and methods.
\n\n
\n
\nCursor.execute(sql[, parameters])
\n

Executes an SQL statement. The SQL statement may be parametrized (i. e.\nplaceholders instead of SQL literals). The sqlite3 module supports two\nkinds of placeholders: question marks (qmark style) and named placeholders\n(named style).

\n

This example shows how to use parameters with qmark style:

\n
import sqlite3\n\ncon = sqlite3.connect("mydb")\n\ncur = con.cursor()\n\nwho = "Yeltsin"\nage = 72\n\ncur.execute("select name_last, age from people where name_last=? and age=?", (who, age))\nprint cur.fetchone()\n
\n
\n

This example shows how to use the named style:

\n
import sqlite3\n\ncon = sqlite3.connect("mydb")\n\ncur = con.cursor()\n\nwho = "Yeltsin"\nage = 72\n\ncur.execute("select name_last, age from people where name_last=:who and age=:age",\n    {"who": who, "age": age})\nprint cur.fetchone()\n
\n
\n

execute() will only execute a single SQL statement. If you try to execute\nmore than one statement with it, it will raise a Warning. Use\nexecutescript() if you want to execute multiple SQL statements with one\ncall.

\n
\n\n
\n
\nCursor.executemany(sql, seq_of_parameters)
\n

Executes an SQL command against all parameter sequences or mappings found in\nthe sequence sql. The sqlite3 module also allows using an\niterator yielding parameters instead of a sequence.

\n
import sqlite3\n\nclass IterChars:\n    def __init__(self):\n        self.count = ord('a')\n\n    def __iter__(self):\n        return self\n\n    def next(self):\n        if self.count > ord('z'):\n            raise StopIteration\n        self.count += 1\n        return (chr(self.count - 1),) # this is a 1-tuple\n\ncon = sqlite3.connect(":memory:")\ncur = con.cursor()\ncur.execute("create table characters(c)")\n\ntheIter = IterChars()\ncur.executemany("insert into characters(c) values (?)", theIter)\n\ncur.execute("select c from characters")\nprint cur.fetchall()\n
\n
\n

Here’s a shorter example using a generator:

\n
import sqlite3\n\ndef char_generator():\n    import string\n    for c in string.letters[:26]:\n        yield (c,)\n\ncon = sqlite3.connect(":memory:")\ncur = con.cursor()\ncur.execute("create table characters(c)")\n\ncur.executemany("insert into characters(c) values (?)", char_generator())\n\ncur.execute("select c from characters")\nprint cur.fetchall()\n
\n
\n
\n\n
\n
\nCursor.executescript(sql_script)
\n

This is a nonstandard convenience method for executing multiple SQL statements\nat once. It issues a COMMIT statement first, then executes the SQL script it\ngets as a parameter.

\n

sql_script can be a bytestring or a Unicode string.

\n

Example:

\n
import sqlite3\n\ncon = sqlite3.connect(":memory:")\ncur = con.cursor()\ncur.executescript("""\n    create table person(\n        firstname,\n        lastname,\n        age\n    );\n\n    create table book(\n        title,\n        author,\n        published\n    );\n\n    insert into book(title, author, published)\n    values (\n        'Dirk Gently''s Holistic Detective Agency',\n        'Douglas Adams',\n        1987\n    );\n    """)\n
\n
\n
\n\n
\n
\nCursor.fetchone()
\n
Fetches the next row of a query result set, returning a single sequence,\nor None when no more data is available.
\n\n
\n
\nCursor.fetchmany([size=cursor.arraysize])
\n

Fetches the next set of rows of a query result, returning a list. An empty\nlist is returned when no more rows are available.

\n

The number of rows to fetch per call is specified by the size parameter.\nIf it is not given, the cursor’s arraysize determines the number of rows\nto be fetched. The method should try to fetch as many rows as indicated by\nthe size parameter. If this is not possible due to the specified number of\nrows not being available, fewer rows may be returned.

\n

Note there are performance considerations involved with the size parameter.\nFor optimal performance, it is usually best to use the arraysize attribute.\nIf the size parameter is used, then it is best for it to retain the same\nvalue from one fetchmany() call to the next.

\n
\n\n
\n
\nCursor.fetchall()
\n
Fetches all (remaining) rows of a query result, returning a list. Note that\nthe cursor’s arraysize attribute can affect the performance of this operation.\nAn empty list is returned when no rows are available.
\n\n
\n
\nCursor.rowcount
\n

Although the Cursor class of the sqlite3 module implements this\nattribute, the database engine’s own support for the determination of “rows\naffected”/”rows selected” is quirky.

\n

For DELETE statements, SQLite reports rowcount as 0 if you make a\nDELETE FROM table without any condition.

\n

For executemany() statements, the number of modifications are summed up\ninto rowcount.

\n

As required by the Python DB API Spec, the rowcount attribute “is -1 in\ncase no executeXX() has been performed on the cursor or the rowcount of the\nlast operation is not determinable by the interface”.

\n

This includes SELECT statements because we cannot determine the number of\nrows a query produced until all rows were fetched.

\n
\n\n
\n
\nCursor.lastrowid
\n
This read-only attribute provides the rowid of the last modified row. It is\nonly set if you issued a INSERT statement using the execute()\nmethod. For operations other than INSERT or when executemany() is\ncalled, lastrowid is set to None.
\n\n
\n
\nCursor.description
\n

This read-only attribute provides the column names of the last query. To\nremain compatible with the Python DB API, it returns a 7-tuple for each\ncolumn where the last six items of each tuple are None.

\n

It is set for SELECT statements without any matching rows as well.

\n
\n\n
\n
\n

11.13.4. Row Objects

\n
\n
\nclass sqlite3.Row
\n

A Row instance serves as a highly optimized\nrow_factory for Connection objects.\nIt tries to mimic a tuple in most of its features.

\n

It supports mapping access by column name and index, iteration,\nrepresentation, equality testing and len().

\n

If two Row objects have exactly the same columns and their\nmembers are equal, they compare equal.

\n

\nChanged in version 2.6: Added iteration and equality (hashability).

\n
\n
\nkeys()
\n

This method returns a tuple of column names. Immediately after a query,\nit is the first member of each tuple in Cursor.description.

\n

\nNew in version 2.6.

\n
\n\n
\n\n

Let’s assume we initialize a table as in the example given above:

\n
conn = sqlite3.connect(":memory:")\nc = conn.cursor()\nc.execute('''create table stocks\n(date text, trans text, symbol text,\n qty real, price real)''')\nc.execute("""insert into stocks\n          values ('2006-01-05','BUY','RHAT',100,35.14)""")\nconn.commit()\nc.close()\n
\n
\n

Now we plug Row in:

\n
>>> conn.row_factory = sqlite3.Row\n>>> c = conn.cursor()\n>>> c.execute('select * from stocks')\n<sqlite3.Cursor object at 0x7f4e7dd8fa80>\n>>> r = c.fetchone()\n>>> type(r)\n<type 'sqlite3.Row'>\n>>> r\n(u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.14)\n>>> len(r)\n5\n>>> r[2]\nu'RHAT'\n>>> r.keys()\n['date', 'trans', 'symbol', 'qty', 'price']\n>>> r['qty']\n100.0\n>>> for member in r: print member\n...\n2006-01-05\nBUY\nRHAT\n100.0\n35.14\n
\n
\n
\n
\n

11.13.5. SQLite and Python types

\n
\n

11.13.5.1. Introduction

\n

SQLite natively supports the following types: NULL, INTEGER,\nREAL, TEXT, BLOB.

\n

The following Python types can thus be sent to SQLite without any problem:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Python typeSQLite type
NoneNULL
intINTEGER
longINTEGER
floatREAL
str (UTF8-encoded)TEXT
unicodeTEXT
bufferBLOB
\n

This is how SQLite types are converted to Python types by default:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
SQLite typePython type
NULLNone
INTEGERint or long,\ndepending on size
REALfloat
TEXTdepends on text_factory,\nunicode by default
BLOBbuffer
\n

The type system of the sqlite3 module is extensible in two ways: you can\nstore additional Python types in a SQLite database via object adaptation, and\nyou can let the sqlite3 module convert SQLite types to different Python\ntypes via converters.

\n
\n
\n

11.13.5.2. Using adapters to store additional Python types in SQLite databases

\n

As described before, SQLite supports only a limited set of types natively. To\nuse other Python types with SQLite, you must adapt them to one of the\nsqlite3 module’s supported types for SQLite: one of NoneType, int, long, float,\nstr, unicode, buffer.

\n

The sqlite3 module uses Python object adaptation, as described in\nPEP 246 for this. The protocol to use is PrepareProtocol.

\n

There are two ways to enable the sqlite3 module to adapt a custom Python\ntype to one of the supported ones.

\n
\n

11.13.5.2.1. Letting your object adapt itself

\n

This is a good approach if you write the class yourself. Let’s suppose you have\na class like this:

\n
class Point(object):\n    def __init__(self, x, y):\n        self.x, self.y = x, y\n
\n
\n

Now you want to store the point in a single SQLite column. First you’ll have to\nchoose one of the supported types first to be used for representing the point.\nLet’s just use str and separate the coordinates using a semicolon. Then you need\nto give your class a method __conform__(self, protocol) which must return\nthe converted value. The parameter protocol will be PrepareProtocol.

\n
import sqlite3\n\nclass Point(object):\n    def __init__(self, x, y):\n        self.x, self.y = x, y\n\n    def __conform__(self, protocol):\n        if protocol is sqlite3.PrepareProtocol:\n            return "%f;%f"  (self.x, self.y)\n\ncon = sqlite3.connect(":memory:")\ncur = con.cursor()\n\np = Point(4.0, -3.2)\ncur.execute("select ?", (p,))\nprint cur.fetchone()[0]\n
\n
\n
\n
\n

11.13.5.2.2. Registering an adapter callable

\n

The other possibility is to create a function that converts the type to the\nstring representation and register the function with register_adapter().

\n
\n

Note

\n

The type/class to adapt must be a new-style class, i. e. it must have\nobject as one of its bases.

\n
\n
import sqlite3\n\nclass Point(object):\n    def __init__(self, x, y):\n        self.x, self.y = x, y\n\ndef adapt_point(point):\n    return "%f;%f"  (point.x, point.y)\n\nsqlite3.register_adapter(Point, adapt_point)\n\ncon = sqlite3.connect(":memory:")\ncur = con.cursor()\n\np = Point(4.0, -3.2)\ncur.execute("select ?", (p,))\nprint cur.fetchone()[0]\n
\n
\n

The sqlite3 module has two default adapters for Python’s built-in\ndatetime.date and datetime.datetime types. Now let’s suppose\nwe want to store datetime.datetime objects not in ISO representation,\nbut as a Unix timestamp.

\n
import sqlite3\nimport datetime, time\n\ndef adapt_datetime(ts):\n    return time.mktime(ts.timetuple())\n\nsqlite3.register_adapter(datetime.datetime, adapt_datetime)\n\ncon = sqlite3.connect(":memory:")\ncur = con.cursor()\n\nnow = datetime.datetime.now()\ncur.execute("select ?", (now,))\nprint cur.fetchone()[0]\n
\n
\n
\n
\n
\n

11.13.5.3. Converting SQLite values to custom Python types

\n

Writing an adapter lets you send custom Python types to SQLite. But to make it\nreally useful we need to make the Python to SQLite to Python roundtrip work.

\n

Enter converters.

\n

Let’s go back to the Point class. We stored the x and y coordinates\nseparated via semicolons as strings in SQLite.

\n

First, we’ll define a converter function that accepts the string as a parameter\nand constructs a Point object from it.

\n
\n

Note

\n

Converter functions always get called with a string, no matter under which\ndata type you sent the value to SQLite.

\n
\n
def convert_point(s):\n    x, y = map(float, s.split(";"))\n    return Point(x, y)\n
\n
\n

Now you need to make the sqlite3 module know that what you select from\nthe database is actually a point. There are two ways of doing this:

\n
    \n
  • Implicitly via the declared type
  • \n
  • Explicitly via the column name
  • \n
\n

Both ways are described in section Module functions and constants, in the entries\nfor the constants PARSE_DECLTYPES and PARSE_COLNAMES.

\n

The following example illustrates both approaches.

\n
import sqlite3\n\nclass Point(object):\n    def __init__(self, x, y):\n        self.x, self.y = x, y\n\n    def __repr__(self):\n        return "(%f;%f)"  (self.x, self.y)\n\ndef adapt_point(point):\n    return "%f;%f"  (point.x, point.y)\n\ndef convert_point(s):\n    x, y = map(float, s.split(";"))\n    return Point(x, y)\n\n# Register the adapter\nsqlite3.register_adapter(Point, adapt_point)\n\n# Register the converter\nsqlite3.register_converter("point", convert_point)\n\np = Point(4.0, -3.2)\n\n#########################\n# 1) Using declared types\ncon = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)\ncur = con.cursor()\ncur.execute("create table test(p point)")\n\ncur.execute("insert into test(p) values (?)", (p,))\ncur.execute("select p from test")\nprint "with declared types:", cur.fetchone()[0]\ncur.close()\ncon.close()\n\n#######################\n# 1) Using column names\ncon = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES)\ncur = con.cursor()\ncur.execute("create table test(p)")\n\ncur.execute("insert into test(p) values (?)", (p,))\ncur.execute('select p as "p [point]" from test')\nprint "with column names:", cur.fetchone()[0]\ncur.close()\ncon.close()\n
\n
\n
\n
\n

11.13.5.4. Default adapters and converters

\n

There are default adapters for the date and datetime types in the datetime\nmodule. They will be sent as ISO dates/ISO timestamps to SQLite.

\n

The default converters are registered under the name “date” for\ndatetime.date and under the name “timestamp” for\ndatetime.datetime.

\n

This way, you can use date/timestamps from Python without any additional\nfiddling in most cases. The format of the adapters is also compatible with the\nexperimental SQLite date/time functions.

\n

The following example demonstrates this.

\n
import sqlite3\nimport datetime\n\ncon = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)\ncur = con.cursor()\ncur.execute("create table test(d date, ts timestamp)")\n\ntoday = datetime.date.today()\nnow = datetime.datetime.now()\n\ncur.execute("insert into test(d, ts) values (?, ?)", (today, now))\ncur.execute("select d, ts from test")\nrow = cur.fetchone()\nprint today, "=>", row[0], type(row[0])\nprint now, "=>", row[1], type(row[1])\n\ncur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"')\nrow = cur.fetchone()\nprint "current_date", row[0], type(row[0])\nprint "current_timestamp", row[1], type(row[1])\n
\n
\n
\n
\n
\n

11.13.6. Controlling Transactions

\n

By default, the sqlite3 module opens transactions implicitly before a\nData Modification Language (DML) statement (i.e.\nINSERT/UPDATE/DELETE/REPLACE), and commits transactions\nimplicitly before a non-DML, non-query statement (i. e.\nanything other than SELECT or the aforementioned).

\n

So if you are within a transaction and issue a command like CREATE TABLE\n..., VACUUM, PRAGMA, the sqlite3 module will commit implicitly\nbefore executing that command. There are two reasons for doing that. The first\nis that some of these commands don’t work within transactions. The other reason\nis that sqlite3 needs to keep track of the transaction state (if a transaction\nis active or not).

\n

You can control which kind of BEGIN statements sqlite3 implicitly executes\n(or none at all) via the isolation_level parameter to the connect()\ncall, or via the isolation_level property of connections.

\n

If you want autocommit mode, then set isolation_level to None.

\n

Otherwise leave it at its default, which will result in a plain “BEGIN”\nstatement, or set it to one of SQLite’s supported isolation levels: “DEFERRED”,\n“IMMEDIATE” or “EXCLUSIVE”.

\n
\n
\n

11.13.7. Using sqlite3 efficiently

\n
\n

11.13.7.1. Using shortcut methods

\n

Using the nonstandard execute(), executemany() and\nexecutescript() methods of the Connection object, your code can\nbe written more concisely because you don’t have to create the (often\nsuperfluous) Cursor objects explicitly. Instead, the Cursor\nobjects are created implicitly and these shortcut methods return the cursor\nobjects. This way, you can execute a SELECT statement and iterate over it\ndirectly using only a single call on the Connection object.

\n
import sqlite3\n\npersons = [\n    ("Hugo", "Boss"),\n    ("Calvin", "Klein")\n    ]\n\ncon = sqlite3.connect(":memory:")\n\n# Create the table\ncon.execute("create table person(firstname, lastname)")\n\n# Fill the table\ncon.executemany("insert into person(firstname, lastname) values (?, ?)", persons)\n\n# Print the table contents\nfor row in con.execute("select firstname, lastname from person"):\n    print row\n\n# Using a dummy WHERE clause to not let SQLite take the shortcut table deletes.\nprint "I just deleted", con.execute("delete from person where 1=1").rowcount, "rows"\n
\n
\n
\n
\n

11.13.7.2. Accessing columns by name instead of by index

\n

One useful feature of the sqlite3 module is the built-in\nsqlite3.Row class designed to be used as a row factory.

\n

Rows wrapped with this class can be accessed both by index (like tuples) and\ncase-insensitively by name:

\n
import sqlite3\n\ncon = sqlite3.connect("mydb")\ncon.row_factory = sqlite3.Row\n\ncur = con.cursor()\ncur.execute("select name_last, age from people")\nfor row in cur:\n    assert row[0] == row["name_last"]\n    assert row["name_last"] == row["nAmE_lAsT"]\n    assert row[1] == row["age"]\n    assert row[1] == row["AgE"]\n
\n
\n
\n
\n

11.13.7.3. Using the connection as a context manager

\n

\nNew in version 2.6.

\n

Connection objects can be used as context managers\nthat automatically commit or rollback transactions. In the event of an\nexception, the transaction is rolled back; otherwise, the transaction is\ncommitted:

\n
import sqlite3\n\ncon = sqlite3.connect(":memory:")\ncon.execute("create table person (id integer primary key, firstname varchar unique)")\n\n# Successful, con.commit() is called automatically afterwards\nwith con:\n    con.execute("insert into person(firstname) values (?)", ("Joe",))\n\n# con.rollback() is called after the with block finishes with an exception, the\n# exception is still raised and must be caught\ntry:\n    with con:\n        con.execute("insert into person(firstname) values (?)", ("Joe",))\nexcept sqlite3.IntegrityError:\n    print "couldn't add Joe twice"\n
\n
\n
\n
\n
\n

11.13.8. Common issues

\n
\n

11.13.8.1. Multithreading

\n

Older SQLite versions had issues with sharing connections between threads.\nThat’s why the Python module disallows sharing connections and cursors between\nthreads. If you still try to do so, you will get an exception at runtime.

\n

The only exception is calling the interrupt() method, which\nonly makes sense to call from a different thread.

\n

Footnotes

\n\n\n\n\n\n
[1](1, 2) The sqlite3 module is not built with loadable extension support by\ndefault, because some platforms (notably Mac OS X) have SQLite libraries\nwhich are compiled without this feature. To get loadable extension support,\nyou must modify setup.py and remove the line that sets\nSQLITE_OMIT_LOAD_EXTENSION.
\n
\n
\n
", "searchableItems": [ { "name": "sqlite3.complete_statement", "domId": "sqlite3_sqlite3.complete_statement" }, { "name": "sqlite3.connect", "domId": "sqlite3_sqlite3.connect" }, { "name": "sqlite3.Connection", "domId": "sqlite3_sqlite3.Connection" }, { "name": "sqlite3.Connection.close", "domId": "sqlite3_sqlite3.Connection.close" }, { "name": "sqlite3.Connection.commit", "domId": "sqlite3_sqlite3.Connection.commit" }, { "name": "sqlite3.Connection.create_aggregate", "domId": "sqlite3_sqlite3.Connection.create_aggregate" }, { "name": "sqlite3.Connection.create_collation", "domId": "sqlite3_sqlite3.Connection.create_collation" }, { "name": "sqlite3.Connection.create_function", "domId": "sqlite3_sqlite3.Connection.create_function" }, { "name": "sqlite3.Connection.cursor", "domId": "sqlite3_sqlite3.Connection.cursor" }, { "name": "sqlite3.Connection.enable_load_extension", "domId": "sqlite3_sqlite3.Connection.enable_load_extension" }, { "name": "sqlite3.Connection.execute", "domId": "sqlite3_sqlite3.Connection.execute" }, { "name": "sqlite3.Connection.executemany", "domId": "sqlite3_sqlite3.Connection.executemany" }, { "name": "sqlite3.Connection.executescript", "domId": "sqlite3_sqlite3.Connection.executescript" }, { "name": "sqlite3.Connection.interrupt", "domId": "sqlite3_sqlite3.Connection.interrupt" }, { "name": "sqlite3.Connection.load_extension", "domId": "sqlite3_sqlite3.Connection.load_extension" }, { "name": "sqlite3.Connection.rollback", "domId": "sqlite3_sqlite3.Connection.rollback" }, { "name": "sqlite3.Connection.set_authorizer", "domId": "sqlite3_sqlite3.Connection.set_authorizer" }, { "name": "sqlite3.Connection.set_progress_handler", "domId": "sqlite3_sqlite3.Connection.set_progress_handler" }, { "name": "sqlite3.Cursor", "domId": "sqlite3_sqlite3.Cursor" }, { "name": "sqlite3.Cursor.execute", "domId": "sqlite3_sqlite3.Cursor.execute" }, { "name": "sqlite3.Cursor.executemany", "domId": "sqlite3_sqlite3.Cursor.executemany" }, { "name": "sqlite3.Cursor.executescript", "domId": "sqlite3_sqlite3.Cursor.executescript" }, { "name": "sqlite3.Cursor.fetchall", "domId": "sqlite3_sqlite3.Cursor.fetchall" }, { "name": "sqlite3.Cursor.fetchmany", "domId": "sqlite3_sqlite3.Cursor.fetchmany" }, { "name": "sqlite3.Cursor.fetchone", "domId": "sqlite3_sqlite3.Cursor.fetchone" }, { "name": "sqlite3.enable_callback_tracebacks", "domId": "sqlite3_sqlite3.enable_callback_tracebacks" }, { "name": "sqlite3.register_adapter", "domId": "sqlite3_sqlite3.register_adapter" }, { "name": "sqlite3.register_converter", "domId": "sqlite3_sqlite3.register_converter" }, { "name": "sqlite3.Row", "domId": "sqlite3_sqlite3.Row" }, { "name": "sqlite3.Row.keys", "domId": "sqlite3_sqlite3.Row.keys" } ] }, { "url": "http://docs.python.org/library/netrc.html", "title": "netrc", "html": "
\n

13.4. netrc — netrc file processing

\n

\nNew in version 1.5.2.

\n

Source code: Lib/netrc.py

\n
\n

The netrc class parses and encapsulates the netrc file format used by\nthe Unix ftp program and other FTP clients.

\n
\n
\nclass netrc.netrc([file])
\n
A netrc instance or subclass instance encapsulates data from a netrc\nfile. The initialization argument, if present, specifies the file to parse. If\nno argument is given, the file .netrc in the user’s home directory will\nbe read. Parse errors will raise NetrcParseError with diagnostic\ninformation including the file name, line number, and terminating token.
\n\n
\n
\nexception netrc.NetrcParseError
\n
Exception raised by the netrc class when syntactical errors are\nencountered in source text. Instances of this exception provide three\ninteresting attributes: msg is a textual explanation of the error,\nfilename is the name of the source file, and lineno gives the\nline number on which the error was found.
\n\n
\n

13.4.1. netrc Objects

\n

A netrc instance has the following methods:

\n
\n
\nnetrc.authenticators(host)
\n
Return a 3-tuple (login, account, password) of authenticators for host.\nIf the netrc file did not contain an entry for the given host, return the tuple\nassociated with the ‘default’ entry. If neither matching host nor default entry\nis available, return None.
\n\n
\n
\nnetrc.__repr__()
\n
Dump the class data as a string in the format of a netrc file. (This discards\ncomments and may reorder the entries.)
\n\n

Instances of netrc have public instance variables:

\n
\n
\nnetrc.hosts
\n
Dictionary mapping host names to (login, account, password) tuples. The\n‘default’ entry, if any, is represented as a pseudo-host by that name.
\n\n
\n
\nnetrc.macros
\n
Dictionary mapping macro names to string lists.
\n\n
\n

Note

\n

Passwords are limited to a subset of the ASCII character set. Versions of\nthis module prior to 2.3 were extremely limited. Starting with 2.3, all\nASCII punctuation is allowed in passwords. However, note that whitespace and\nnon-printable characters are not allowed in passwords. This is a limitation\nof the way the .netrc file is parsed and may be removed in the future.

\n
\n
\n
", "searchableItems": [ { "name": "netrc.netrc", "domId": "netrc_netrc.netrc" }, { "name": "netrc.netrc.__repr__", "domId": "netrc_netrc.netrc.__repr__" }, { "name": "netrc.netrc.authenticators", "domId": "netrc_netrc.netrc.authenticators" } ] }, { "url": "http://docs.python.org/library/hashlib.html", "title": "hashlib", "html": "
\n

14.1. hashlib — Secure hashes and message digests

\n

\nNew in version 2.5.

\n

Source code: Lib/hashlib.py

\n
\n

This module implements a common interface to many different secure hash and\nmessage digest algorithms. Included are the FIPS secure hash algorithms SHA1,\nSHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA’s MD5\nalgorithm (defined in Internet RFC 1321). The terms secure hash and message\ndigest are interchangeable. Older algorithms were called message digests. The\nmodern term is secure hash.

\n
\n

Note

\n

If you want the adler32 or crc32 hash functions they are available in\nthe zlib module.

\n
\n
\n

Warning

\n

Some algorithms have known hash collision weaknesses, see the FAQ at the end.

\n
\n

There is one constructor method named for each type of hash. All return\na hash object with the same simple interface. For example: use sha1() to\ncreate a SHA1 hash object. You can now feed this object with arbitrary strings\nusing the update() method. At any point you can ask it for the\ndigest of the concatenation of the strings fed to it so far using the\ndigest() or hexdigest() methods.

\n

Constructors for hash algorithms that are always present in this module are\nmd5(), sha1(), sha224(), sha256(), sha384(), and\nsha512(). Additional algorithms may also be available depending upon the\nOpenSSL library that Python uses on your platform.

\n

For example, to obtain the digest of the string 'Nobody inspects the spammish\nrepetition':

\n
>>> import hashlib\n>>> m = hashlib.md5()\n>>> m.update("Nobody inspects")\n>>> m.update(" the spammish repetition")\n>>> m.digest()\n'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'\n>>> m.digest_size\n16\n>>> m.block_size\n64\n
\n
\n

More condensed:

\n
>>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()\n'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'\n
\n
\n

A generic new() constructor that takes the string name of the desired\nalgorithm as its first parameter also exists to allow access to the above listed\nhashes as well as any other algorithms that your OpenSSL library may offer. The\nnamed constructors are much faster than new() and should be preferred.

\n

Using new() with an algorithm provided by OpenSSL:

\n
>>> h = hashlib.new('ripemd160')\n>>> h.update("Nobody inspects the spammish repetition")\n>>> h.hexdigest()\n'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'\n
\n
\n

This module provides the following constant attribute:

\n
\n
\nhashlib.algorithms
\n

A tuple providing the names of the hash algorithms guaranteed to be\nsupported by this module.

\n

\nNew in version 2.7.

\n
\n\n

The following values are provided as constant attributes of the hash objects\nreturned by the constructors:

\n
\n
\nhash.digest_size
\n
The size of the resulting hash in bytes.
\n\n
\n
\nhash.block_size
\n
The internal block size of the hash algorithm in bytes.
\n\n

A hash object has the following methods:

\n
\n
\nhash.update(arg)
\n

Update the hash object with the string arg. Repeated calls are equivalent to\na single call with the concatenation of all the arguments: m.update(a);\nm.update(b) is equivalent to m.update(a+b).

\n

\nChanged in version 2.7.

\n
\n\n
\n
\nhash.digest()
\n
Return the digest of the strings passed to the update() method so far.\nThis is a string of digest_size bytes which may contain non-ASCII\ncharacters, including null bytes.
\n\n
\n
\nhash.hexdigest()
\n
Like digest() except the digest is returned as a string of double length,\ncontaining only hexadecimal digits. This may be used to exchange the value\nsafely in email or other non-binary environments.
\n\n
\n
\nhash.copy()
\n
Return a copy (“clone”) of the hash object. This can be used to efficiently\ncompute the digests of strings that share a common initial substring.
\n\n
\n

See also

\n
\n
Module hmac
\n
A module to generate message authentication codes using hashes.
\n
Module base64
\n
Another way to encode binary hashes for non-binary environments.
\n
http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
\n
The FIPS 180-2 publication on Secure Hash Algorithms.
\n
http://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms
\n
Wikipedia article with information on which algorithms have known issues and\nwhat that means regarding their use.
\n
\n
\n
", "searchableItems": [ { "name": "hashlib.hash.copy", "domId": "hashlib_hashlib.hash.copy" }, { "name": "hashlib.hash.digest", "domId": "hashlib_hashlib.hash.digest" }, { "name": "hashlib.hash.hexdigest", "domId": "hashlib_hashlib.hash.hexdigest" }, { "name": "hashlib.hash.update", "domId": "hashlib_hashlib.hash.update" } ] }, { "url": "http://docs.python.org/library/hmac.html", "title": "hmac", "html": "
\n

14.2. hmac — Keyed-Hashing for Message Authentication

\n

\nNew in version 2.2.

\n

Source code: Lib/hmac.py

\n
\n

This module implements the HMAC algorithm as described by RFC 2104.

\n
\n
\nhmac.new(key[, msg[, digestmod]])
\n
Return a new hmac object. If msg is present, the method call update(msg)\nis made. digestmod is the digest constructor or module for the HMAC object to\nuse. It defaults to the hashlib.md5() constructor.
\n\n

An HMAC object has the following methods:

\n
\n
\nhmac.update(msg)
\n
Update the hmac object with the string msg. Repeated calls are equivalent to\na single call with the concatenation of all the arguments: m.update(a);\nm.update(b) is equivalent to m.update(a + b).
\n\n
\n
\nhmac.digest()
\n
Return the digest of the strings passed to the update() method so far.\nThis string will be the same length as the digest_size of the digest given to\nthe constructor. It may contain non-ASCII characters, including NUL bytes.
\n\n
\n
\nhmac.hexdigest()
\n
Like digest() except the digest is returned as a string twice the length\ncontaining only hexadecimal digits. This may be used to exchange the value\nsafely in email or other non-binary environments.
\n\n
\n
\nhmac.copy()
\n
Return a copy (“clone”) of the hmac object. This can be used to efficiently\ncompute the digests of strings that share a common initial substring.
\n\n
\n

See also

\n
\n
Module hashlib
\n
The Python module providing secure hash functions.
\n
\n
\n
", "searchableItems": [ { "name": "hmac.hmac.copy", "domId": "hmac_hmac.hmac.copy" }, { "name": "hmac.hmac.digest", "domId": "hmac_hmac.hmac.digest" }, { "name": "hmac.hmac.hexdigest", "domId": "hmac_hmac.hmac.hexdigest" }, { "name": "hmac.hmac.update", "domId": "hmac_hmac.hmac.update" }, { "name": "hmac.new", "domId": "hmac_hmac.new" } ] }, { "url": "http://docs.python.org/library/plistlib.html", "title": "plistlib", "html": "
\n

13.6. plistlib — Generate and parse Mac OS X .plist files

\n

\nChanged in version 2.6: This module was previously only available in the Mac-specific library, it is\nnow available for all platforms.

\n

Source code: Lib/plistlib.py

\n
\n

This module provides an interface for reading and writing the “property list”\nXML files used mainly by Mac OS X.

\n

The property list (.plist) file format is a simple XML pickle supporting\nbasic object types, like dictionaries, lists, numbers and strings. Usually the\ntop level object is a dictionary.

\n

Values can be strings, integers, floats, booleans, tuples, lists, dictionaries\n(but only with string keys), Data or datetime.datetime\nobjects. String values (including dictionary keys) may be unicode strings –\nthey will be written out as UTF-8.

\n

The <data> plist type is supported through the Data class. This is\na thin wrapper around a Python string. Use Data if your strings\ncontain control characters.

\n
\n

See also

\n
\n
PList manual page
\n
Apple’s documentation of the file format.
\n
\n
\n

This module defines the following functions:

\n
\n
\nplistlib.readPlist(pathOrFile)
\n

Read a plist file. pathOrFile may either be a file name or a (readable)\nfile object. Return the unpacked root object (which usually is a\ndictionary).

\n

The XML data is parsed using the Expat parser from xml.parsers.expat\n– see its documentation for possible exceptions on ill-formed XML.\nUnknown elements will simply be ignored by the plist parser.

\n
\n\n
\n
\nplistlib.writePlist(rootObject, pathOrFile)
\n

Write rootObject to a plist file. pathOrFile may either be a file name\nor a (writable) file object.

\n

A TypeError will be raised if the object is of an unsupported type or\na container that contains objects of unsupported types.

\n
\n\n
\n
\nplistlib.readPlistFromString(data)
\n
Read a plist from a string. Return the root object.
\n\n
\n
\nplistlib.writePlistToString(rootObject)
\n
Return rootObject as a plist-formatted string.
\n\n
\n
\nplistlib.readPlistFromResource(path[, restype='plst'[, resid=0]])
\n

Read a plist from the resource with type restype from the resource fork of\npath. Availability: Mac OS X.

\n
\n

Note

\n

In Python 3.x, this function has been removed.

\n
\n
\n\n
\n
\nplistlib.writePlistToResource(rootObject, path[, restype='plst'[, resid=0]])
\n

Write rootObject as a resource with type restype to the resource fork of\npath. Availability: Mac OS X.

\n
\n

Note

\n

In Python 3.x, this function has been removed.

\n
\n
\n\n

The following class is available:

\n
\n
\nclass plistlib.Data(data)
\n

Return a “data” wrapper object around the string data. This is used in\nfunctions converting from/to plists to represent the <data> type\navailable in plists.

\n

It has one attribute, data, that can be used to retrieve the Python\nstring stored in it.

\n
\n\n
\n

13.6.1. Examples

\n

Generating a plist:

\n
pl = dict(\n    aString="Doodah",\n    aList=["A", "B", 12, 32.1, [1, 2, 3]],\n    aFloat = 0.1,\n    anInt = 728,\n    aDict=dict(\n        anotherString="<hello & hi there!>",\n        aUnicodeValue=u'M\\xe4ssig, Ma\\xdf',\n        aTrueValue=True,\n        aFalseValue=False,\n    ),\n    someData = Data("<binary gunk>"),\n    someMoreData = Data("<lots of binary gunk>" * 10),\n    aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),\n)\n# unicode keys are possible, but a little awkward to use:\npl[u'\\xc5benraa'] = "That was a unicode key."\nwritePlist(pl, fileName)\n
\n
\n

Parsing a plist:

\n
pl = readPlist(pathOrFile)\nprint pl["aKey"]\n
\n
\n
\n
", "searchableItems": [ { "name": "plistlib.Data", "domId": "plistlib_plistlib.Data" }, { "name": "plistlib.readPlist", "domId": "plistlib_plistlib.readPlist" }, { "name": "plistlib.readPlistFromResource", "domId": "plistlib_plistlib.readPlistFromResource" }, { "name": "plistlib.readPlistFromString", "domId": "plistlib_plistlib.readPlistFromString" }, { "name": "plistlib.writePlist", "domId": "plistlib_plistlib.writePlist" }, { "name": "plistlib.writePlistToResource", "domId": "plistlib_plistlib.writePlistToResource" }, { "name": "plistlib.writePlistToString", "domId": "plistlib_plistlib.writePlistToString" } ] }, { "url": "http://docs.python.org/library/xdrlib.html", "title": "xdrlib", "html": "
\n

13.5. xdrlib — Encode and decode XDR data

\n

Source code: Lib/xdrlib.py

\n
\n

The xdrlib module supports the External Data Representation Standard as\ndescribed in RFC 1014, written by Sun Microsystems, Inc. June 1987. It\nsupports most of the data types described in the RFC.

\n

The xdrlib module defines two classes, one for packing variables into XDR\nrepresentation, and another for unpacking from XDR representation. There are\nalso two exception classes.

\n
\n
\nclass xdrlib.Packer
\n
Packer is the class for packing data into XDR representation. The\nPacker class is instantiated with no arguments.
\n\n
\n
\nclass xdrlib.Unpacker(data)
\n
Unpacker is the complementary class which unpacks XDR data values from a\nstring buffer. The input buffer is given as data.
\n\n
\n

See also

\n
\n
RFC 1014 - XDR: External Data Representation Standard
\n
This RFC defined the encoding of data which was XDR at the time this module was\noriginally written. It has apparently been obsoleted by RFC 1832.
\n
RFC 1832 - XDR: External Data Representation Standard
\n
Newer RFC that provides a revised definition of XDR.
\n
\n
\n
\n

13.5.1. Packer Objects

\n

Packer instances have the following methods:

\n
\n
\nPacker.get_buffer()
\n
Returns the current pack buffer as a string.
\n\n
\n
\nPacker.reset()
\n
Resets the pack buffer to the empty string.
\n\n

In general, you can pack any of the most common XDR data types by calling the\nappropriate pack_type() method. Each method takes a single argument, the\nvalue to pack. The following simple data type packing methods are supported:\npack_uint(), pack_int(), pack_enum(), pack_bool(),\npack_uhyper(), and pack_hyper().

\n
\n
\nPacker.pack_float(value)
\n
Packs the single-precision floating point number value.
\n\n
\n
\nPacker.pack_double(value)
\n
Packs the double-precision floating point number value.
\n\n

The following methods support packing strings, bytes, and opaque data:

\n
\n
\nPacker.pack_fstring(n, s)
\n
Packs a fixed length string, s. n is the length of the string but it is\nnot packed into the data buffer. The string is padded with null bytes if\nnecessary to guaranteed 4 byte alignment.
\n\n
\n
\nPacker.pack_fopaque(n, data)
\n
Packs a fixed length opaque data stream, similarly to pack_fstring().
\n\n
\n
\nPacker.pack_string(s)
\n
Packs a variable length string, s. The length of the string is first packed\nas an unsigned integer, then the string data is packed with\npack_fstring().
\n\n
\n
\nPacker.pack_opaque(data)
\n
Packs a variable length opaque data string, similarly to pack_string().
\n\n
\n
\nPacker.pack_bytes(bytes)
\n
Packs a variable length byte stream, similarly to pack_string().
\n\n

The following methods support packing arrays and lists:

\n
\n
\nPacker.pack_list(list, pack_item)
\n

Packs a list of homogeneous items. This method is useful for lists with an\nindeterminate size; i.e. the size is not available until the entire list has\nbeen walked. For each item in the list, an unsigned integer 1 is packed\nfirst, followed by the data value from the list. pack_item is the function\nthat is called to pack the individual item. At the end of the list, an unsigned\ninteger 0 is packed.

\n

For example, to pack a list of integers, the code might appear like this:

\n
import xdrlib\np = xdrlib.Packer()\np.pack_list([1, 2, 3], p.pack_int)\n
\n
\n
\n\n
\n
\nPacker.pack_farray(n, array, pack_item)
\n
Packs a fixed length list (array) of homogeneous items. n is the length of\nthe list; it is not packed into the buffer, but a ValueError exception\nis raised if len(array) is not equal to n. As above, pack_item is the\nfunction used to pack each element.
\n\n
\n
\nPacker.pack_array(list, pack_item)
\n
Packs a variable length list of homogeneous items. First, the length of the\nlist is packed as an unsigned integer, then each element is packed as in\npack_farray() above.
\n\n
\n
\n

13.5.2. Unpacker Objects

\n

The Unpacker class offers the following methods:

\n
\n
\nUnpacker.reset(data)
\n
Resets the string buffer with the given data.
\n\n
\n
\nUnpacker.get_position()
\n
Returns the current unpack position in the data buffer.
\n\n
\n
\nUnpacker.set_position(position)
\n
Sets the data buffer unpack position to position. You should be careful about\nusing get_position() and set_position().
\n\n
\n
\nUnpacker.get_buffer()
\n
Returns the current unpack data buffer as a string.
\n\n
\n
\nUnpacker.done()
\n
Indicates unpack completion. Raises an Error exception if all of the\ndata has not been unpacked.
\n\n

In addition, every data type that can be packed with a Packer, can be\nunpacked with an Unpacker. Unpacking methods are of the form\nunpack_type(), and take no arguments. They return the unpacked object.

\n
\n
\nUnpacker.unpack_float()
\n
Unpacks a single-precision floating point number.
\n\n
\n
\nUnpacker.unpack_double()
\n
Unpacks a double-precision floating point number, similarly to\nunpack_float().
\n\n

In addition, the following methods unpack strings, bytes, and opaque data:

\n
\n
\nUnpacker.unpack_fstring(n)
\n
Unpacks and returns a fixed length string. n is the number of characters\nexpected. Padding with null bytes to guaranteed 4 byte alignment is assumed.
\n\n
\n
\nUnpacker.unpack_fopaque(n)
\n
Unpacks and returns a fixed length opaque data stream, similarly to\nunpack_fstring().
\n\n
\n
\nUnpacker.unpack_string()
\n
Unpacks and returns a variable length string. The length of the string is first\nunpacked as an unsigned integer, then the string data is unpacked with\nunpack_fstring().
\n\n
\n
\nUnpacker.unpack_opaque()
\n
Unpacks and returns a variable length opaque data string, similarly to\nunpack_string().
\n\n
\n
\nUnpacker.unpack_bytes()
\n
Unpacks and returns a variable length byte stream, similarly to\nunpack_string().
\n\n

The following methods support unpacking arrays and lists:

\n
\n
\nUnpacker.unpack_list(unpack_item)
\n
Unpacks and returns a list of homogeneous items. The list is unpacked one\nelement at a time by first unpacking an unsigned integer flag. If the flag is\n1, then the item is unpacked and appended to the list. A flag of 0\nindicates the end of the list. unpack_item is the function that is called to\nunpack the items.
\n\n
\n
\nUnpacker.unpack_farray(n, unpack_item)
\n
Unpacks and returns (as a list) a fixed length array of homogeneous items. n\nis number of list elements to expect in the buffer. As above, unpack_item is\nthe function used to unpack each element.
\n\n
\n
\nUnpacker.unpack_array(unpack_item)
\n
Unpacks and returns a variable length list of homogeneous items. First, the\nlength of the list is unpacked as an unsigned integer, then each element is\nunpacked as in unpack_farray() above.
\n\n
\n
\n

13.5.3. Exceptions

\n

Exceptions in this module are coded as class instances:

\n
\n
\nexception xdrlib.Error
\n
The base exception class. Error has a single public attribute\nmsg containing the description of the error.
\n\n
\n
\nexception xdrlib.ConversionError
\n
Class derived from Error. Contains no additional instance variables.
\n\n

Here is an example of how you would catch one of these exceptions:

\n
import xdrlib\np = xdrlib.Packer()\ntry:\n    p.pack_double(8.01)\nexcept xdrlib.ConversionError, instance:\n    print 'packing the double failed:', instance.msg\n
\n
\n
\n
", "searchableItems": [ { "name": "xdrlib.Packer", "domId": "xdrlib_xdrlib.Packer" }, { "name": "xdrlib.Packer.get_buffer", "domId": "xdrlib_xdrlib.Packer.get_buffer" }, { "name": "xdrlib.Packer.pack_array", "domId": "xdrlib_xdrlib.Packer.pack_array" }, { "name": "xdrlib.Packer.pack_bytes", "domId": "xdrlib_xdrlib.Packer.pack_bytes" }, { "name": "xdrlib.Packer.pack_double", "domId": "xdrlib_xdrlib.Packer.pack_double" }, { "name": "xdrlib.Packer.pack_farray", "domId": "xdrlib_xdrlib.Packer.pack_farray" }, { "name": "xdrlib.Packer.pack_float", "domId": "xdrlib_xdrlib.Packer.pack_float" }, { "name": "xdrlib.Packer.pack_fopaque", "domId": "xdrlib_xdrlib.Packer.pack_fopaque" }, { "name": "xdrlib.Packer.pack_fstring", "domId": "xdrlib_xdrlib.Packer.pack_fstring" }, { "name": "xdrlib.Packer.pack_list", "domId": "xdrlib_xdrlib.Packer.pack_list" }, { "name": "xdrlib.Packer.pack_opaque", "domId": "xdrlib_xdrlib.Packer.pack_opaque" }, { "name": "xdrlib.Packer.pack_string", "domId": "xdrlib_xdrlib.Packer.pack_string" }, { "name": "xdrlib.Packer.reset", "domId": "xdrlib_xdrlib.Packer.reset" }, { "name": "xdrlib.Unpacker", "domId": "xdrlib_xdrlib.Unpacker" }, { "name": "xdrlib.Unpacker.done", "domId": "xdrlib_xdrlib.Unpacker.done" }, { "name": "xdrlib.Unpacker.get_buffer", "domId": "xdrlib_xdrlib.Unpacker.get_buffer" }, { "name": "xdrlib.Unpacker.get_position", "domId": "xdrlib_xdrlib.Unpacker.get_position" }, { "name": "xdrlib.Unpacker.reset", "domId": "xdrlib_xdrlib.Unpacker.reset" }, { "name": "xdrlib.Unpacker.set_position", "domId": "xdrlib_xdrlib.Unpacker.set_position" }, { "name": "xdrlib.Unpacker.unpack_array", "domId": "xdrlib_xdrlib.Unpacker.unpack_array" }, { "name": "xdrlib.Unpacker.unpack_bytes", "domId": "xdrlib_xdrlib.Unpacker.unpack_bytes" }, { "name": "xdrlib.Unpacker.unpack_double", "domId": "xdrlib_xdrlib.Unpacker.unpack_double" }, { "name": "xdrlib.Unpacker.unpack_farray", "domId": "xdrlib_xdrlib.Unpacker.unpack_farray" }, { "name": "xdrlib.Unpacker.unpack_float", "domId": "xdrlib_xdrlib.Unpacker.unpack_float" }, { "name": "xdrlib.Unpacker.unpack_fopaque", "domId": "xdrlib_xdrlib.Unpacker.unpack_fopaque" }, { "name": "xdrlib.Unpacker.unpack_fstring", "domId": "xdrlib_xdrlib.Unpacker.unpack_fstring" }, { "name": "xdrlib.Unpacker.unpack_list", "domId": "xdrlib_xdrlib.Unpacker.unpack_list" }, { "name": "xdrlib.Unpacker.unpack_opaque", "domId": "xdrlib_xdrlib.Unpacker.unpack_opaque" }, { "name": "xdrlib.Unpacker.unpack_string", "domId": "xdrlib_xdrlib.Unpacker.unpack_string" } ] }, { "url": "http://docs.python.org/library/md5.html", "title": "md5", "html": "
\n

14.3. md5 — MD5 message digest algorithm

\n

\nDeprecated since version 2.5: Use the hashlib module instead.

\n

This module implements the interface to RSA’s MD5 message digest algorithm (see\nalso Internet RFC 1321). Its use is quite straightforward: use new()\nto create an md5 object. You can now feed this object with arbitrary strings\nusing the update() method, and at any point you can ask it for the\ndigest (a strong kind of 128-bit checksum, a.k.a. “fingerprint”) of the\nconcatenation of the strings fed to it so far using the digest() method.

\n

For example, to obtain the digest of the string 'Nobody inspects the spammish\nrepetition':

\n
>>> import md5\n>>> m = md5.new()\n>>> m.update("Nobody inspects")\n>>> m.update(" the spammish repetition")\n>>> m.digest()\n'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'\n
\n
\n

More condensed:

\n
>>> md5.new("Nobody inspects the spammish repetition").digest()\n'\\xbbd\\x9c\\x83\\xdd\\x1e\\xa5\\xc9\\xd9\\xde\\xc9\\xa1\\x8d\\xf0\\xff\\xe9'\n
\n
\n

The following values are provided as constants in the module and as attributes\nof the md5 objects returned by new():

\n
\n
\nmd5.digest_size
\n
The size of the resulting digest in bytes. This is always 16.
\n\n

The md5 module provides the following functions:

\n
\n
\nmd5.new([arg])
\n
Return a new md5 object. If arg is present, the method call update(arg)\nis made.
\n\n
\n
\nmd5.md5([arg])
\n
For backward compatibility reasons, this is an alternative name for the\nnew() function.
\n\n

An md5 object has the following methods:

\n
\n
\nmd5.update(arg)
\n
Update the md5 object with the string arg. Repeated calls are equivalent to a\nsingle call with the concatenation of all the arguments: m.update(a);\nm.update(b) is equivalent to m.update(a+b).
\n\n
\n
\nmd5.digest()
\n
Return the digest of the strings passed to the update() method so far.\nThis is a 16-byte string which may contain non-ASCII characters, including null\nbytes.
\n\n
\n
\nmd5.hexdigest()
\n
Like digest() except the digest is returned as a string of length 32,\ncontaining only hexadecimal digits. This may be used to exchange the value\nsafely in email or other non-binary environments.
\n\n
\n
\nmd5.copy()
\n
Return a copy (“clone”) of the md5 object. This can be used to efficiently\ncompute the digests of strings that share a common initial substring.
\n\n
\n

See also

\n
\n
Module sha
\n
Similar module implementing the Secure Hash Algorithm (SHA). The SHA algorithm\nis considered a more secure hash.
\n
\n
\n
", "searchableItems": [ { "name": "md5.md5", "domId": "md5_md5.md5" }, { "name": "md5.md5.copy", "domId": "md5_md5.md5.copy" }, { "name": "md5.md5.digest", "domId": "md5_md5.md5.digest" }, { "name": "md5.md5.hexdigest", "domId": "md5_md5.md5.hexdigest" }, { "name": "md5.md5.update", "domId": "md5_md5.md5.update" }, { "name": "md5.new", "domId": "md5_md5.new" } ] }, { "url": "http://docs.python.org/library/sha.html", "title": "sha", "html": "
\n

14.4. sha — SHA-1 message digest algorithm

\n

\nDeprecated since version 2.5: Use the hashlib module instead.

\n

This module implements the interface to NIST’s secure hash algorithm, known as\nSHA-1. SHA-1 is an improved version of the original SHA hash algorithm. It is\nused in the same way as the md5 module: use new() to create an sha\nobject, then feed this object with arbitrary strings using the update()\nmethod, and at any point you can ask it for the digest of the\nconcatenation of the strings fed to it so far. SHA-1 digests are 160 bits\ninstead of MD5’s 128 bits.

\n
\n
\nsha.new([string])
\n
Return a new sha object. If string is present, the method call\nupdate(string) is made.
\n\n

The following values are provided as constants in the module and as attributes\nof the sha objects returned by new():

\n
\n
\nsha.blocksize
\n
Size of the blocks fed into the hash function; this is always 1. This size\nis used to allow an arbitrary string to be hashed.
\n\n
\n
\nsha.digest_size
\n
The size of the resulting digest in bytes. This is always 20.
\n\n

An sha object has the same methods as md5 objects:

\n
\n
\nsha.update(arg)
\n
Update the sha object with the string arg. Repeated calls are equivalent to a\nsingle call with the concatenation of all the arguments: m.update(a);\nm.update(b) is equivalent to m.update(a+b).
\n\n
\n
\nsha.digest()
\n
Return the digest of the strings passed to the update() method so far.\nThis is a 20-byte string which may contain non-ASCII characters, including null\nbytes.
\n\n
\n
\nsha.hexdigest()
\n
Like digest() except the digest is returned as a string of length 40,\ncontaining only hexadecimal digits. This may be used to exchange the value\nsafely in email or other non-binary environments.
\n\n
\n
\nsha.copy()
\n
Return a copy (“clone”) of the sha object. This can be used to efficiently\ncompute the digests of strings that share a common initial substring.
\n\n
\n

See also

\n
\n
Secure Hash Standard
\n
The Secure Hash Algorithm is defined by NIST document FIPS PUB 180-2: Secure\nHash Standard,\npublished in August 2002.
\n
Cryptographic Toolkit (Secure Hashing)
\n
Links from NIST to various information on secure hashing.
\n
\n
\n
", "searchableItems": [ { "name": "sha.new", "domId": "sha_sha.new" }, { "name": "sha.sha.copy", "domId": "sha_sha.sha.copy" }, { "name": "sha.sha.digest", "domId": "sha_sha.sha.digest" }, { "name": "sha.sha.hexdigest", "domId": "sha_sha.sha.hexdigest" }, { "name": "sha.sha.update", "domId": "sha_sha.sha.update" } ] }, { "url": "http://docs.python.org/library/time.html", "title": "time", "html": "
\n

15.3. time — Time access and conversions

\n

This module provides various time-related functions. For related\nfunctionality, see also the datetime and calendar modules.

\n

Although this module is always available,\nnot all functions are available on all platforms. Most of the functions\ndefined in this module call platform C library functions with the same name. It\nmay sometimes be helpful to consult the platform documentation, because the\nsemantics of these functions varies among platforms.

\n

An explanation of some terminology and conventions is in order.

\n\n\n\n\n\n

The module defines the following functions and data items:

\n
\n
\ntime.accept2dyear
\n
Boolean value indicating whether two-digit year values will be accepted. This\nis true by default, but will be set to false if the environment variable\nPYTHONY2K has been set to a non-empty string. It may also be modified\nat run time.
\n\n
\n
\ntime.altzone
\n
The offset of the local DST timezone, in seconds west of UTC, if one is defined.\nThis is negative if the local DST timezone is east of UTC (as in Western Europe,\nincluding the UK). Only use this if daylight is nonzero.
\n\n
\n
\ntime.asctime([t])
\n

Convert a tuple or struct_time representing a time as returned by\ngmtime() or localtime() to a 24-character string of the following\nform: 'Sun Jun 20 23:21:05 1993'. If t is not provided, the current time\nas returned by localtime() is used. Locale information is not used by\nasctime().

\n
\n

Note

\n

Unlike the C function of the same name, there is no trailing newline.

\n
\n

\nChanged in version 2.1: Allowed t to be omitted.

\n
\n\n
\n
\ntime.clock()
\n

On Unix, return the current processor time as a floating point number expressed\nin seconds. The precision, and in fact the very definition of the meaning of\n“processor time”, depends on that of the C function of the same name, but in any\ncase, this is the function to use for benchmarking Python or timing algorithms.

\n

On Windows, this function returns wall-clock seconds elapsed since the first\ncall to this function, as a floating point number, based on the Win32 function\nQueryPerformanceCounter(). The resolution is typically better than one\nmicrosecond.

\n
\n\n
\n
\ntime.ctime([secs])
\n

Convert a time expressed in seconds since the epoch to a string representing\nlocal time. If secs is not provided or None, the current time as\nreturned by time() is used. ctime(secs) is equivalent to\nasctime(localtime(secs)). Locale information is not used by ctime().

\n

\nChanged in version 2.1: Allowed secs to be omitted.

\n

\nChanged in version 2.4: If secs is None, the current time is used.

\n
\n\n
\n
\ntime.daylight
\n
Nonzero if a DST timezone is defined.
\n\n
\n
\ntime.gmtime([secs])
\n

Convert a time expressed in seconds since the epoch to a struct_time in\nUTC in which the dst flag is always zero. If secs is not provided or\nNone, the current time as returned by time() is used. Fractions\nof a second are ignored. See above for a description of the\nstruct_time object. See calendar.timegm() for the inverse of this\nfunction.

\n

\nChanged in version 2.1: Allowed secs to be omitted.

\n

\nChanged in version 2.4: If secs is None, the current time is used.

\n
\n\n
\n
\ntime.localtime([secs])
\n

Like gmtime() but converts to local time. If secs is not provided or\nNone, the current time as returned by time() is used. The dst\nflag is set to 1 when DST applies to the given time.

\n

\nChanged in version 2.1: Allowed secs to be omitted.

\n

\nChanged in version 2.4: If secs is None, the current time is used.

\n
\n\n
\n
\ntime.mktime(t)
\n
This is the inverse function of localtime(). Its argument is the\nstruct_time or full 9-tuple (since the dst flag is needed; use -1\nas the dst flag if it is unknown) which expresses the time in local time, not\nUTC. It returns a floating point number, for compatibility with time().\nIf the input value cannot be represented as a valid time, either\nOverflowError or ValueError will be raised (which depends on\nwhether the invalid value is caught by Python or the underlying C libraries).\nThe earliest date for which it can generate a time is platform-dependent.
\n\n
\n
\ntime.sleep(secs)
\n
Suspend execution for the given number of seconds. The argument may be a\nfloating point number to indicate a more precise sleep time. The actual\nsuspension time may be less than that requested because any caught signal will\nterminate the sleep() following execution of that signal’s catching\nroutine. Also, the suspension time may be longer than requested by an arbitrary\namount because of the scheduling of other activity in the system.
\n\n
\n
\ntime.strftime(format[, t])
\n

Convert a tuple or struct_time representing a time as returned by\ngmtime() or localtime() to a string as specified by the format\nargument. If t is not provided, the current time as returned by\nlocaltime() is used. format must be a string. ValueError is\nraised if any field in t is outside of the allowed range.

\n

\nChanged in version 2.1: Allowed t to be omitted.

\n

\nChanged in version 2.4: ValueError raised if a field in t is out of range.

\n

\nChanged in version 2.5: 0 is now a legal argument for any position in the time tuple; if it is normally\nillegal the value is forced to a correct one..

\n

The following directives can be embedded in the format string. They are shown\nwithout the optional field width and precision specification, and are replaced\nby the indicated characters in the strftime() result:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
DirectiveMeaningNotes
%aLocale’s abbreviated weekday\nname. 
%ALocale’s full weekday name. 
%bLocale’s abbreviated month\nname. 
%BLocale’s full month name. 
%cLocale’s appropriate date and\ntime representation. 
%dDay of the month as a decimal\nnumber [01,31]. 
%HHour (24-hour clock) as a\ndecimal number [00,23]. 
%IHour (12-hour clock) as a\ndecimal number [01,12]. 
%jDay of the year as a decimal\nnumber [001,366]. 
%mMonth as a decimal number\n[01,12]. 
%MMinute as a decimal number\n[00,59]. 
%pLocale’s equivalent of either\nAM or PM.(1)
%SSecond as a decimal number\n[00,61].(2)
%UWeek number of the year\n(Sunday as the first day of\nthe week) as a decimal number\n[00,53]. All days in a new\nyear preceding the first\nSunday are considered to be in\nweek 0.(3)
%wWeekday as a decimal number\n[0(Sunday),6]. 
%WWeek number of the year\n(Monday as the first day of\nthe week) as a decimal number\n[00,53]. All days in a new\nyear preceding the first\nMonday are considered to be in\nweek 0.(3)
%xLocale’s appropriate date\nrepresentation. 
%XLocale’s appropriate time\nrepresentation. 
%yYear without century as a\ndecimal number [00,99]. 
%YYear with century as a decimal\nnumber. 
%ZTime zone name (no characters\nif no time zone exists). 
A literal '%' character. 
\n

Notes:

\n
    \n
  1. When used with the strptime() function, the %p directive only affects\nthe output hour field if the %I directive is used to parse the hour.
  2. \n
  3. The range really is 0 to 61; this accounts for leap seconds and the\n(very rare) double leap seconds.
  4. \n
  5. When used with the strptime() function, %U and %W are only used in\ncalculations when the day of the week and the year are specified.
  6. \n
\n

Here is an example, a format for dates compatible with that specified in the\nRFC 2822 Internet email standard. [1]

\n
>>> from time import gmtime, strftime\n>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())\n'Thu, 28 Jun 2001 14:17:15 +0000'\n
\n
\n

Additional directives may be supported on certain platforms, but only the ones\nlisted here have a meaning standardized by ANSI C.

\n

On some platforms, an optional field width and precision specification can\nimmediately follow the initial '%' of a directive in the following order;\nthis is also not portable. The field width is normally 2 except for %j where\nit is 3.

\n
\n\n
\n
\ntime.strptime(string[, format])
\n

Parse a string representing a time according to a format. The return value is\na struct_time as returned by gmtime() or localtime().

\n

The format parameter uses the same directives as those used by\nstrftime(); it defaults to "%a %b %d %H:%M:%S %Y" which matches the\nformatting returned by ctime(). If string cannot be parsed according to\nformat, or if it has excess data after parsing, ValueError is raised.\nThe default values used to fill in any missing data when more accurate values\ncannot be inferred are (1900, 1, 1, 0, 0, 0, 0, 1, -1).

\n

For example:

\n
>>> import time\n>>> time.strptime("30 Nov 00", "%d %b %y")   # doctest: +NORMALIZE_WHITESPACE\ntime.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0,\n                 tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)\n
\n
\n

Support for the %Z directive is based on the values contained in tzname\nand whether daylight is true. Because of this, it is platform-specific\nexcept for recognizing UTC and GMT which are always known (and are considered to\nbe non-daylight savings timezones).

\n

Only the directives specified in the documentation are supported. Because\nstrftime() is implemented per platform it can sometimes offer more\ndirectives than those listed. But strptime() is independent of any platform\nand thus does not necessarily support all directives available that are not\ndocumented as supported.

\n
\n\n
\n
\nclass time.struct_time
\n

The type of the time value sequence returned by gmtime(),\nlocaltime(), and strptime(). It is an object with a named\ntuple interface: values can be accessed by index and by attribute name. The\nfollowing values are present:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
IndexAttributeValues
0tm_year(for example, 1993)
1tm_monrange [1, 12]
2tm_mdayrange [1, 31]
3tm_hourrange [0, 23]
4tm_minrange [0, 59]
5tm_secrange [0, 61]; see (1) in\nstrftime() description
6tm_wdayrange [0, 6], Monday is 0
7tm_ydayrange [1, 366]
8tm_isdst0, 1 or -1; see below
\n

\nNew in version 2.2.

\n

Note that unlike the C structure, the month value is a range of [1, 12], not\n[0, 11]. A year value will be handled as described under Year 2000\n(Y2K) issues above. A -1 argument as the daylight\nsavings flag, passed to mktime() will usually result in the correct\ndaylight savings state to be filled in.

\n

When a tuple with an incorrect length is passed to a function expecting a\nstruct_time, or having elements of the wrong type, a\nTypeError is raised.

\n
\n\n
\n
\ntime.time()
\n
Return the time as a floating point number expressed in seconds since the epoch,\nin UTC. Note that even though the time is always returned as a floating point\nnumber, not all systems provide time with a better precision than 1 second.\nWhile this function normally returns non-decreasing values, it can return a\nlower value than a previous call if the system clock has been set back between\nthe two calls.
\n\n
\n
\ntime.timezone
\n
The offset of the local (non-DST) timezone, in seconds west of UTC (negative in\nmost of Western Europe, positive in the US, zero in the UK).
\n\n
\n
\ntime.tzname
\n
A tuple of two strings: the first is the name of the local non-DST timezone, the\nsecond is the name of the local DST timezone. If no DST timezone is defined,\nthe second string should not be used.
\n\n
\n
\ntime.tzset()
\n

Resets the time conversion rules used by the library routines. The environment\nvariable TZ specifies how this is done.

\n

\nNew in version 2.3.

\n

Availability: Unix.

\n
\n

Note

\n

Although in many cases, changing the TZ environment variable may\naffect the output of functions like localtime() without calling\ntzset(), this behavior should not be relied on.

\n

The TZ environment variable should contain no whitespace.

\n
\n

The standard format of the TZ environment variable is (whitespace\nadded for clarity):

\n
std offset [dst [offset [,start[/time], end[/time]]]]
\n
\n

Where the components are:

\n
\n
std and dst
\n
Three or more alphanumerics giving the timezone abbreviations. These will be\npropagated into time.tzname
\n
offset
\n
The offset has the form: ± hh[:mm[:ss]]. This indicates the value\nadded the local time to arrive at UTC. If preceded by a ‘-‘, the timezone\nis east of the Prime Meridian; otherwise, it is west. If no offset follows\ndst, summer time is assumed to be one hour ahead of standard time.
\n
start[/time], end[/time]
\n

Indicates when to change to and back from DST. The format of the\nstart and end dates are one of the following:

\n
\n
Jn
\n
The Julian day n (1 <= n <= 365). Leap days are not counted, so in\nall years February 28 is day 59 and March 1 is day 60.
\n
n
\n
The zero-based Julian day (0 <= n <= 365). Leap days are counted, and\nit is possible to refer to February 29.
\n
Mm.n.d
\n
The d‘th day (0 <= d <= 6) or week n of month m of the year (1\n<= n <= 5, 1 <= m <= 12, where week 5 means “the last d day in\nmonth m” which may occur in either the fourth or the fifth\nweek). Week 1 is the first week in which the d‘th day occurs. Day\nzero is Sunday.
\n
\n

time has the same format as offset except that no leading sign\n(‘-‘ or ‘+’) is allowed. The default, if time is not given, is 02:00:00.

\n
\n
\n
>>> os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'\n>>> time.tzset()\n>>> time.strftime('%X %x %Z')\n'02:07:36 05/08/03 EDT'\n>>> os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'\n>>> time.tzset()\n>>> time.strftime('%X %x %Z')\n'16:08:12 05/08/03 AEST'\n
\n
\n

On many Unix systems (including *BSD, Linux, Solaris, and Darwin), it is more\nconvenient to use the system’s zoneinfo (tzfile(5)) database to\nspecify the timezone rules. To do this, set the TZ environment\nvariable to the path of the required timezone datafile, relative to the root of\nthe systems ‘zoneinfo’ timezone database, usually located at\n/usr/share/zoneinfo. For example, 'US/Eastern',\n'Australia/Melbourne', 'Egypt' or 'Europe/Amsterdam'.

\n
>>> os.environ['TZ'] = 'US/Eastern'\n>>> time.tzset()\n>>> time.tzname\n('EST', 'EDT')\n>>> os.environ['TZ'] = 'Egypt'\n>>> time.tzset()\n>>> time.tzname\n('EET', 'EEST')\n
\n
\n
\n\n
\n

See also

\n
\n
Module datetime
\n
More object-oriented interface to dates and times.
\n
Module locale
\n
Internationalization services. The locale settings can affect the return values\nfor some of the functions in the time module.
\n
Module calendar
\n
General calendar-related functions. timegm() is the inverse of\ngmtime() from this module.
\n
\n
\n

Footnotes

\n\n\n\n\n\n
[1]The use of %Z is now deprecated, but the %z escape that expands to the\npreferred hour/minute offset is not supported by all ANSI C libraries. Also, a\nstrict reading of the original 1982 RFC 822 standard calls for a two-digit\nyear (%y rather than %Y), but practice moved to 4-digit years long before the\nyear 2000. After that, RFC 822 became obsolete and the 4-digit year has\nbeen first recommended by RFC 1123 and then mandated by RFC 2822.
\n
", "searchableItems": [ { "name": "time.asctime", "domId": "time_time.asctime" }, { "name": "time.clock", "domId": "time_time.clock" }, { "name": "time.ctime", "domId": "time_time.ctime" }, { "name": "time.gmtime", "domId": "time_time.gmtime" }, { "name": "time.localtime", "domId": "time_time.localtime" }, { "name": "time.mktime", "domId": "time_time.mktime" }, { "name": "time.sleep", "domId": "time_time.sleep" }, { "name": "time.strftime", "domId": "time_time.strftime" }, { "name": "time.strptime", "domId": "time_time.strptime" }, { "name": "time.struct_time", "domId": "time_time.struct_time" }, { "name": "time.time", "domId": "time_time.time" }, { "name": "time.tzset", "domId": "time_time.tzset" } ] }, { "url": "http://docs.python.org/library/io.html", "title": "io", "html": "
\n

15.2. io — Core tools for working with streams

\n

\nNew in version 2.6.

\n

The io module provides the Python interfaces to stream handling.\nUnder Python 2.x, this is proposed as an alternative to the built-in\nfile object, but in Python 3.x it is the default interface to\naccess files and streams.

\n
\n

Note

\n

Since this module has been designed primarily for Python 3.x, you have to\nbe aware that all uses of “bytes” in this document refer to the\nstr type (of which bytes is an alias), and all uses\nof “text” refer to the unicode type. Furthermore, those two\ntypes are not interchangeable in the io APIs.

\n
\n

At the top of the I/O hierarchy is the abstract base class IOBase. It\ndefines the basic interface to a stream. Note, however, that there is no\nseparation between reading and writing to streams; implementations are allowed\nto raise an IOError if they do not support a given operation.

\n

Extending IOBase is RawIOBase which deals simply with the\nreading and writing of raw bytes to a stream. FileIO subclasses\nRawIOBase to provide an interface to files in the machine’s\nfile system.

\n

BufferedIOBase deals with buffering on a raw byte stream\n(RawIOBase). Its subclasses, BufferedWriter,\nBufferedReader, and BufferedRWPair buffer streams that are\nreadable, writable, and both readable and writable.\nBufferedRandom provides a buffered interface to random access\nstreams. BytesIO is a simple stream of in-memory bytes.

\n

Another IOBase subclass, TextIOBase, deals with\nstreams whose bytes represent text, and handles encoding and decoding\nfrom and to unicode strings. TextIOWrapper, which extends\nit, is a buffered text interface to a buffered raw stream\n(BufferedIOBase). Finally, StringIO is an in-memory\nstream for unicode text.

\n

Argument names are not part of the specification, and only the arguments of\nopen() are intended to be used as keyword arguments.

\n
\n

15.2.1. Module Interface

\n
\n
\nio.DEFAULT_BUFFER_SIZE
\n
An int containing the default buffer size used by the module’s buffered I/O\nclasses. open() uses the file’s blksize (as obtained by\nos.stat()) if possible.
\n\n
\n
\nio.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
\n

Open file and return a corresponding stream. If the file cannot be opened,\nan IOError is raised.

\n

file is either a string giving the pathname (absolute or\nrelative to the current working directory) of the file to be opened or\nan integer file descriptor of the file to be wrapped. (If a file descriptor\nis given, it is closed when the returned I/O object is closed, unless\nclosefd is set to False.)

\n

mode is an optional string that specifies the mode in which the file is\nopened. It defaults to 'r' which means open for reading in text mode.\nOther common values are 'w' for writing (truncating the file if it\nalready exists), and 'a' for appending (which on some Unix systems,\nmeans that all writes append to the end of the file regardless of the\ncurrent seek position). In text mode, if encoding is not specified the\nencoding used is platform dependent. (For reading and writing raw bytes use\nbinary mode and leave encoding unspecified.) The available modes are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CharacterMeaning
'r'open for reading (default)
'w'open for writing, truncating the file first
'a'open for writing, appending to the end of the file if it exists
'b'binary mode
't'text mode (default)
'+'open a disk file for updating (reading and writing)
'U'universal newline mode (for backwards compatibility; should\nnot be used in new code)
\n

The default mode is 'rt' (open for reading text). For binary random\naccess, the mode 'w+b' opens and truncates the file to 0 bytes, while\n'r+b' opens the file without truncation.

\n

Python distinguishes between files opened in binary and text modes, even when\nthe underlying operating system doesn’t. Files opened in binary mode\n(including 'b' in the mode argument) return contents as bytes\nobjects without any decoding. In text mode (the default, or when 't' is\nincluded in the mode argument), the contents of the file are returned as\nunicode strings, the bytes having been first decoded using a\nplatform-dependent encoding or using the specified encoding if given.

\n

buffering is an optional integer used to set the buffering policy.\nPass 0 to switch buffering off (only allowed in binary mode), 1 to select\nline buffering (only usable in text mode), and an integer > 1 to indicate\nthe size of a fixed-size chunk buffer. When no buffering argument is\ngiven, the default buffering policy works as follows:

\n
    \n
  • Binary files are buffered in fixed-size chunks; the size of the buffer\nis chosen using a heuristic trying to determine the underlying device’s\n“block size” and falling back on DEFAULT_BUFFER_SIZE.\nOn many systems, the buffer will typically be 4096 or 8192 bytes long.
  • \n
  • “Interactive” text files (files for which isatty() returns True)\nuse line buffering. Other text files use the policy described above\nfor binary files.
  • \n
\n

encoding is the name of the encoding used to decode or encode the file.\nThis should only be used in text mode. The default encoding is platform\ndependent (whatever locale.getpreferredencoding() returns), but any\nencoding supported by Python can be used. See the codecs module for\nthe list of supported encodings.

\n

errors is an optional string that specifies how encoding and decoding\nerrors are to be handled–this cannot be used in binary mode. Pass\n'strict' to raise a ValueError exception if there is an encoding\nerror (the default of None has the same effect), or pass 'ignore' to\nignore errors. (Note that ignoring encoding errors can lead to data loss.)\n'replace' causes a replacement marker (such as '?') to be inserted\nwhere there is malformed data. When writing, 'xmlcharrefreplace'\n(replace with the appropriate XML character reference) or\n'backslashreplace' (replace with backslashed escape sequences) can be\nused. Any other error handling name that has been registered with\ncodecs.register_error() is also valid.

\n

newline controls how universal newlines works (it only applies to text\nmode). It can be None, '', '\\n', '\\r', and '\\r\\n'. It\nworks as follows:

\n
    \n
  • On input, if newline is None, universal newlines mode is enabled.\nLines in the input can end in '\\n', '\\r', or '\\r\\n', and these\nare translated into '\\n' before being returned to the caller. If it is\n'', universal newline mode is enabled, but line endings are returned to\nthe caller untranslated. If it has any of the other legal values, input\nlines are only terminated by the given string, and the line ending is\nreturned to the caller untranslated.
  • \n
  • On output, if newline is None, any '\\n' characters written are\ntranslated to the system default line separator, os.linesep. If\nnewline is '', no translation takes place. If newline is any of\nthe other legal values, any '\\n' characters written are translated to\nthe given string.
  • \n
\n

If closefd is False and a file descriptor rather than a filename was\ngiven, the underlying file descriptor will be kept open when the file is\nclosed. If a filename is given closefd has no effect and must be True\n(the default).

\n

The type of file object returned by the open() function depends on the\nmode. When open() is used to open a file in a text mode ('w',\n'r', 'wt', 'rt', etc.), it returns a subclass of\nTextIOBase (specifically TextIOWrapper). When used to open\na file in a binary mode with buffering, the returned class is a subclass of\nBufferedIOBase. The exact class varies: in read binary mode, it\nreturns a BufferedReader; in write binary and append binary modes,\nit returns a BufferedWriter, and in read/write mode, it returns a\nBufferedRandom. When buffering is disabled, the raw stream, a\nsubclass of RawIOBase, FileIO, is returned.

\n

It is also possible to use an unicode or bytes string\nas a file for both reading and writing. For unicode strings\nStringIO can be used like a file opened in text mode,\nand for bytes a BytesIO can be used like a\nfile opened in a binary mode.

\n
\n\n
\n
\nexception io.BlockingIOError
\n

Error raised when blocking would occur on a non-blocking stream. It inherits\nIOError.

\n

In addition to those of IOError, BlockingIOError has one\nattribute:

\n
\n
\ncharacters_written
\n
An integer containing the number of characters written to the stream\nbefore it blocked.
\n\n
\n\n
\n
\nexception io.UnsupportedOperation
\n
An exception inheriting IOError and ValueError that is raised\nwhen an unsupported operation is called on a stream.
\n\n
\n
\n

15.2.2. I/O Base Classes

\n
\n
\nclass io.IOBase
\n

The abstract base class for all I/O classes, acting on streams of bytes.\nThere is no public constructor.

\n

This class provides empty abstract implementations for many methods\nthat derived classes can override selectively; the default\nimplementations represent a file that cannot be read, written or\nseeked.

\n

Even though IOBase does not declare read(), readinto(),\nor write() because their signatures will vary, implementations and\nclients should consider those methods part of the interface. Also,\nimplementations may raise a IOError when operations they do not\nsupport are called.

\n

The basic type used for binary data read from or written to a file is\nbytes (also known as str). bytearrays are\naccepted too, and in some cases (such as readinto) required.\nText I/O classes work with unicode data.

\n

Note that calling any method (even inquiries) on a closed stream is\nundefined. Implementations may raise IOError in this case.

\n

IOBase (and its subclasses) support the iterator protocol, meaning that an\nIOBase object can be iterated over yielding the lines in a stream.\nLines are defined slightly differently depending on whether the stream is\na binary stream (yielding bytes), or a text stream (yielding\nunicode strings). See readline() below.

\n

IOBase is also a context manager and therefore supports the\nwith statement. In this example, file is closed after the\nwith statement’s suite is finished—even if an exception occurs:

\n
with io.open('spam.txt', 'w') as file:\n    file.write(u'Spam and eggs!')\n
\n
\n

IOBase provides these data attributes and methods:

\n
\n
\nclose()
\n

Flush and close this stream. This method has no effect if the file is\nalready closed. Once the file is closed, any operation on the file\n(e.g. reading or writing) will raise a ValueError.

\n

As a convenience, it is allowed to call this method more than once;\nonly the first call, however, will have an effect.

\n
\n\n
\n
\nclosed
\n
True if the stream is closed.
\n\n
\n
\nfileno()
\n
Return the underlying file descriptor (an integer) of the stream if it\nexists. An IOError is raised if the IO object does not use a file\ndescriptor.
\n\n
\n
\nflush()
\n
Flush the write buffers of the stream if applicable. This does nothing\nfor read-only and non-blocking streams.
\n\n
\n
\nisatty()
\n
Return True if the stream is interactive (i.e., connected to\na terminal/tty device).
\n\n
\n
\nreadable()
\n
Return True if the stream can be read from. If False, read()\nwill raise IOError.
\n\n
\n
\nreadline(limit=-1)
\n

Read and return one line from the stream. If limit is specified, at\nmost limit bytes will be read.

\n

The line terminator is always b'\\n' for binary files; for text files,\nthe newlines argument to open() can be used to select the line\nterminator(s) recognized.

\n
\n\n
\n
\nreadlines(hint=-1)
\n
Read and return a list of lines from the stream. hint can be specified\nto control the number of lines read: no more lines will be read if the\ntotal size (in bytes/characters) of all lines so far exceeds hint.
\n\n
\n
\nseek(offset, whence=SEEK_SET)
\n

Change the stream position to the given byte offset. offset is\ninterpreted relative to the position indicated by whence. Values for\nwhence are:

\n
    \n
  • SEEK_SET or 0 – start of the stream (the default);\noffset should be zero or positive
  • \n
  • SEEK_CUR or 1 – current stream position; offset may\nbe negative
  • \n
  • SEEK_END or 2 – end of the stream; offset is usually\nnegative
  • \n
\n

Return the new absolute position.

\n

\nNew in version 2.7: The SEEK_* constants

\n
\n\n
\n
\nseekable()
\n
Return True if the stream supports random access. If False,\nseek(), tell() and truncate() will raise IOError.
\n\n
\n
\ntell()
\n
Return the current stream position.
\n\n
\n
\ntruncate(size=None)
\n
Resize the stream to the given size in bytes (or the current position\nif size is not specified). The current stream position isn’t changed.\nThis resizing can extend or reduce the current file size. In case of\nextension, the contents of the new file area depend on the platform\n(on most systems, additional bytes are zero-filled, on Windows they’re\nundetermined). The new file size is returned.
\n\n
\n
\nwritable()
\n
Return True if the stream supports writing. If False,\nwrite() and truncate() will raise IOError.
\n\n
\n
\nwritelines(lines)
\n
Write a list of lines to the stream. Line separators are not added, so it\nis usual for each of the lines provided to have a line separator at the\nend.
\n\n
\n\n
\n
\nclass io.RawIOBase
\n

Base class for raw binary I/O. It inherits IOBase. There is no\npublic constructor.

\n

Raw binary I/O typically provides low-level access to an underlying OS\ndevice or API, and does not try to encapsulate it in high-level primitives\n(this is left to Buffered I/O and Text I/O, described later in this page).

\n

In addition to the attributes and methods from IOBase,\nRawIOBase provides the following methods:

\n
\n
\nread(n=-1)
\n

Read up to n bytes from the object and return them. As a convenience,\nif n is unspecified or -1, readall() is called. Otherwise,\nonly one system call is ever made. Fewer than n bytes may be\nreturned if the operating system call returns fewer than n bytes.

\n

If 0 bytes are returned, and n was not 0, this indicates end of file.\nIf the object is in non-blocking mode and no bytes are available,\nNone is returned.

\n
\n\n
\n
\nreadall()
\n
Read and return all the bytes from the stream until EOF, using multiple\ncalls to the stream if necessary.
\n\n
\n
\nreadinto(b)
\n
Read up to len(b) bytes into bytearray b and return the number\nof bytes read. If the object is in non-blocking mode and no\nbytes are available, None is returned.
\n\n
\n
\nwrite(b)
\n
Write the given bytes or bytearray object, b, to the underlying raw\nstream and return the number of bytes written. This can be less than\nlen(b), depending on specifics of the underlying raw stream, and\nespecially if it is in non-blocking mode. None is returned if the\nraw stream is set not to block and no single byte could be readily\nwritten to it.
\n\n
\n\n
\n
\nclass io.BufferedIOBase
\n

Base class for binary streams that support some kind of buffering.\nIt inherits IOBase. There is no public constructor.

\n

The main difference with RawIOBase is that methods read(),\nreadinto() and write() will try (respectively) to read as much\ninput as requested or to consume all given output, at the expense of\nmaking perhaps more than one system call.

\n

In addition, those methods can raise BlockingIOError if the\nunderlying raw stream is in non-blocking mode and cannot take or give\nenough data; unlike their RawIOBase counterparts, they will\nnever return None.

\n

Besides, the read() method does not have a default\nimplementation that defers to readinto().

\n

A typical BufferedIOBase implementation should not inherit from a\nRawIOBase implementation, but wrap one, like\nBufferedWriter and BufferedReader do.

\n

BufferedIOBase provides or overrides these methods and attribute in\naddition to those from IOBase:

\n
\n
\nraw
\n
The underlying raw stream (a RawIOBase instance) that\nBufferedIOBase deals with. This is not part of the\nBufferedIOBase API and may not exist on some implementations.
\n\n
\n
\ndetach()
\n

Separate the underlying raw stream from the buffer and return it.

\n

After the raw stream has been detached, the buffer is in an unusable\nstate.

\n

Some buffers, like BytesIO, do not have the concept of a single\nraw stream to return from this method. They raise\nUnsupportedOperation.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nread(n=-1)
\n

Read and return up to n bytes. If the argument is omitted, None, or\nnegative, data is read and returned until EOF is reached. An empty bytes\nobject is returned if the stream is already at EOF.

\n

If the argument is positive, and the underlying raw stream is not\ninteractive, multiple raw reads may be issued to satisfy the byte count\n(unless EOF is reached first). But for interactive raw streams, at most\none raw read will be issued, and a short result does not imply that EOF is\nimminent.

\n

A BlockingIOError is raised if the underlying raw stream is in\nnon blocking-mode, and has no data available at the moment.

\n
\n\n
\n
\nread1(n=-1)
\n
Read and return up to n bytes, with at most one call to the underlying\nraw stream’s read() method. This can be useful if you\nare implementing your own buffering on top of a BufferedIOBase\nobject.
\n\n
\n
\nreadinto(b)
\n

Read up to len(b) bytes into bytearray b and return the number of bytes\nread.

\n

Like read(), multiple reads may be issued to the underlying raw\nstream, unless the latter is ‘interactive’.

\n

A BlockingIOError is raised if the underlying raw stream is in\nnon blocking-mode, and has no data available at the moment.

\n
\n\n
\n
\nwrite(b)
\n

Write the given bytes or bytearray object, b and return the number\nof bytes written (never less than len(b), since if the write fails\nan IOError will be raised). Depending on the actual\nimplementation, these bytes may be readily written to the underlying\nstream, or held in a buffer for performance and latency reasons.

\n

When in non-blocking mode, a BlockingIOError is raised if the\ndata needed to be written to the raw stream but it couldn’t accept\nall the data without blocking.

\n
\n\n
\n\n
\n
\n

15.2.3. Raw File I/O

\n
\n
\nclass io.FileIO(name, mode='r', closefd=True)
\n

FileIO represents an OS-level file containing bytes data.\nIt implements the RawIOBase interface (and therefore the\nIOBase interface, too).

\n

The name can be one of two things:

\n
    \n
  • a string representing the path to the file which will be opened;
  • \n
  • an integer representing the number of an existing OS-level file descriptor\nto which the resulting FileIO object will give access.
  • \n
\n

The mode can be 'r', 'w' or 'a' for reading (default), writing,\nor appending. The file will be created if it doesn’t exist when opened for\nwriting or appending; it will be truncated when opened for writing. Add a\n'+' to the mode to allow simultaneous reading and writing.

\n

The read() (when called with a positive argument), readinto()\nand write() methods on this class will only make one system call.

\n

In addition to the attributes and methods from IOBase and\nRawIOBase, FileIO provides the following data\nattributes and methods:

\n
\n
\nmode
\n
The mode as given in the constructor.
\n\n
\n
\nname
\n
The file name. This is the file descriptor of the file when no name is\ngiven in the constructor.
\n\n
\n\n
\n
\n

15.2.4. Buffered Streams

\n

Buffered I/O streams provide a higher-level interface to an I/O device\nthan raw I/O does.

\n
\n
\nclass io.BytesIO([initial_bytes])
\n

A stream implementation using an in-memory bytes buffer. It inherits\nBufferedIOBase.

\n

The argument initial_bytes is an optional initial bytes.

\n

BytesIO provides or overrides these methods in addition to those\nfrom BufferedIOBase and IOBase:

\n
\n
\ngetvalue()
\n
Return bytes containing the entire contents of the buffer.
\n\n
\n
\nread1()
\n
In BytesIO, this is the same as read().
\n\n
\n\n
\n
\nclass io.BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)
\n

A buffer providing higher-level access to a readable, sequential\nRawIOBase object. It inherits BufferedIOBase.\nWhen reading data from this object, a larger amount of data may be\nrequested from the underlying raw stream, and kept in an internal buffer.\nThe buffered data can then be returned directly on subsequent reads.

\n

The constructor creates a BufferedReader for the given readable\nraw stream and buffer_size. If buffer_size is omitted,\nDEFAULT_BUFFER_SIZE is used.

\n

BufferedReader provides or overrides these methods in addition to\nthose from BufferedIOBase and IOBase:

\n
\n
\npeek([n])
\n
Return bytes from the stream without advancing the position. At most one\nsingle read on the raw stream is done to satisfy the call. The number of\nbytes returned may be less or more than requested.
\n\n
\n
\nread([n])
\n
Read and return n bytes, or if n is not given or negative, until EOF\nor if the read call would block in non-blocking mode.
\n\n
\n
\nread1(n)
\n
Read and return up to n bytes with only one call on the raw stream. If\nat least one byte is buffered, only buffered bytes are returned.\nOtherwise, one raw stream read call is made.
\n\n
\n\n
\n
\nclass io.BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)
\n

A buffer providing higher-level access to a writeable, sequential\nRawIOBase object. It inherits BufferedIOBase.\nWhen writing to this object, data is normally held into an internal\nbuffer. The buffer will be written out to the underlying RawIOBase\nobject under various conditions, including:

\n
    \n
  • when the buffer gets too small for all pending data;
  • \n
  • when flush() is called;
  • \n
  • when a seek() is requested (for BufferedRandom objects);
  • \n
  • when the BufferedWriter object is closed or destroyed.
  • \n
\n

The constructor creates a BufferedWriter for the given writeable\nraw stream. If the buffer_size is not given, it defaults to\nDEFAULT_BUFFER_SIZE.

\n

A third argument, max_buffer_size, is supported, but unused and deprecated.

\n

BufferedWriter provides or overrides these methods in addition to\nthose from BufferedIOBase and IOBase:

\n
\n
\nflush()
\n
Force bytes held in the buffer into the raw stream. A\nBlockingIOError should be raised if the raw stream blocks.
\n\n
\n
\nwrite(b)
\n
Write the bytes or bytearray object, b and return the number of bytes\nwritten. When in non-blocking mode, a BlockingIOError is raised\nif the buffer needs to be written out but the raw stream blocks.
\n\n
\n\n
\n
\nclass io.BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)
\n

A buffered interface to random access streams. It inherits\nBufferedReader and BufferedWriter, and further supports\nseek() and tell() functionality.

\n

The constructor creates a reader and writer for a seekable raw stream, given\nin the first argument. If the buffer_size is omitted it defaults to\nDEFAULT_BUFFER_SIZE.

\n

A third argument, max_buffer_size, is supported, but unused and deprecated.

\n

BufferedRandom is capable of anything BufferedReader or\nBufferedWriter can do.

\n
\n\n
\n
\nclass io.BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)
\n

A buffered I/O object combining two unidirectional RawIOBase\nobjects – one readable, the other writeable – into a single bidirectional\nendpoint. It inherits BufferedIOBase.

\n

reader and writer are RawIOBase objects that are readable and\nwriteable respectively. If the buffer_size is omitted it defaults to\nDEFAULT_BUFFER_SIZE.

\n

A fourth argument, max_buffer_size, is supported, but unused and\ndeprecated.

\n

BufferedRWPair implements all of BufferedIOBase‘s methods\nexcept for detach(), which raises\nUnsupportedOperation.

\n
\n

Warning

\n

BufferedRWPair does not attempt to synchronize accesses to\nits underlying raw streams. You should not pass it the same object\nas reader and writer; use BufferedRandom instead.

\n
\n
\n\n
\n
\n

15.2.5. Text I/O

\n
\n
\nclass io.TextIOBase
\n

Base class for text streams. This class provides an unicode character\nand line based interface to stream I/O. There is no readinto()\nmethod because Python’s unicode strings are immutable.\nIt inherits IOBase. There is no public constructor.

\n

TextIOBase provides or overrides these data attributes and\nmethods in addition to those from IOBase:

\n
\n
\nencoding
\n
The name of the encoding used to decode the stream’s bytes into\nstrings, and to encode strings into bytes.
\n\n
\n
\nerrors
\n
The error setting of the decoder or encoder.
\n\n
\n
\nnewlines
\n
A string, a tuple of strings, or None, indicating the newlines\ntranslated so far. Depending on the implementation and the initial\nconstructor flags, this may not be available.
\n\n
\n
\nbuffer
\n
The underlying binary buffer (a BufferedIOBase instance) that\nTextIOBase deals with. This is not part of the\nTextIOBase API and may not exist on some implementations.
\n\n
\n
\ndetach()
\n

Separate the underlying binary buffer from the TextIOBase and\nreturn it.

\n

After the underlying buffer has been detached, the TextIOBase is\nin an unusable state.

\n

Some TextIOBase implementations, like StringIO, may not\nhave the concept of an underlying buffer and calling this method will\nraise UnsupportedOperation.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nread(n)
\n
Read and return at most n characters from the stream as a single\nunicode. If n is negative or None, reads until EOF.
\n\n
\n
\nreadline()
\n
Read until newline or EOF and return a single unicode. If the\nstream is already at EOF, an empty string is returned.
\n\n
\n
\nwrite(s)
\n
Write the unicode string s to the stream and return the\nnumber of characters written.
\n\n
\n\n
\n
\nclass io.TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False)
\n

A buffered text stream over a BufferedIOBase binary stream.\nIt inherits TextIOBase.

\n

encoding gives the name of the encoding that the stream will be decoded or\nencoded with. It defaults to locale.getpreferredencoding().

\n

errors is an optional string that specifies how encoding and decoding\nerrors are to be handled. Pass 'strict' to raise a ValueError\nexception if there is an encoding error (the default of None has the same\neffect), or pass 'ignore' to ignore errors. (Note that ignoring encoding\nerrors can lead to data loss.) 'replace' causes a replacement marker\n(such as '?') to be inserted where there is malformed data. When\nwriting, 'xmlcharrefreplace' (replace with the appropriate XML character\nreference) or 'backslashreplace' (replace with backslashed escape\nsequences) can be used. Any other error handling name that has been\nregistered with codecs.register_error() is also valid.

\n

newline can be None, '', '\\n', '\\r', or '\\r\\n'. It\ncontrols the handling of line endings. If it is None, universal newlines\nis enabled. With this enabled, on input, the lines endings '\\n',\n'\\r', or '\\r\\n' are translated to '\\n' before being returned to\nthe caller. Conversely, on output, '\\n' is translated to the system\ndefault line separator, os.linesep. If newline is any other of its\nlegal values, that newline becomes the newline when the file is read and it\nis returned untranslated. On output, '\\n' is converted to the newline.

\n

If line_buffering is True, flush() is implied when a call to\nwrite contains a newline character.

\n

TextIOWrapper provides one attribute in addition to those of\nTextIOBase and its parents:

\n
\n
\nline_buffering
\n
Whether line buffering is enabled.
\n\n
\n\n
\n
\nclass io.StringIO(initial_value=u'', newline=None)
\n

An in-memory stream for unicode text. It inherits TextIOWrapper.

\n

The initial value of the buffer (an empty unicode string by default) can\nbe set by providing initial_value. The newline argument works like\nthat of TextIOWrapper. The default is to do no newline\ntranslation.

\n

StringIO provides this method in addition to those from\nTextIOWrapper and its parents:

\n
\n
\ngetvalue()
\n
Return a unicode containing the entire contents of the buffer at any\ntime before the StringIO object’s close() method is\ncalled.
\n\n

Example usage:

\n
import io\n\noutput = io.StringIO()\noutput.write(u'First line.\\n')\noutput.write(u'Second line.\\n')\n\n# Retrieve file contents -- this will be\n# u'First line.\\nSecond line.\\n'\ncontents = output.getvalue()\n\n# Close object and discard memory buffer --\n# .getvalue() will now raise an exception.\noutput.close()\n
\n
\n
\n\n
\n
\nclass io.IncrementalNewlineDecoder
\n
A helper codec that decodes newlines for universal newlines mode. It\ninherits codecs.IncrementalDecoder.
\n\n
\n
\n

15.2.6. Advanced topics

\n

Here we will discuss several advanced topics pertaining to the concrete\nI/O implementations described above.

\n
\n

15.2.6.1. Performance

\n
\n

15.2.6.1.1. Binary I/O

\n

By reading and writing only large chunks of data even when the user asks\nfor a single byte, buffered I/O is designed to hide any inefficiency in\ncalling and executing the operating system’s unbuffered I/O routines. The\ngain will vary very much depending on the OS and the kind of I/O which is\nperformed (for example, on some contemporary OSes such as Linux, unbuffered\ndisk I/O can be as fast as buffered I/O). The bottom line, however, is\nthat buffered I/O will offer you predictable performance regardless of the\nplatform and the backing device. Therefore, it is most always preferable to\nuse buffered I/O rather than unbuffered I/O.

\n
\n
\n

15.2.6.1.2. Text I/O

\n

Text I/O over a binary storage (such as a file) is significantly slower than\nbinary I/O over the same storage, because it implies conversions from\nunicode to binary data using a character codec. This can become noticeable\nif you handle huge amounts of text data (for example very large log files).\nAlso, TextIOWrapper.tell() and TextIOWrapper.seek() are both\nquite slow due to the reconstruction algorithm used.

\n

StringIO, however, is a native in-memory unicode container and will\nexhibit similar speed to BytesIO.

\n
\n
\n
\n

15.2.6.2. Multi-threading

\n

FileIO objects are thread-safe to the extent that the operating\nsystem calls (such as read(2) under Unix) they are wrapping are thread-safe\ntoo.

\n

Binary buffered objects (instances of BufferedReader,\nBufferedWriter, BufferedRandom and BufferedRWPair)\nprotect their internal structures using a lock; it is therefore safe to call\nthem from multiple threads at once.

\n

TextIOWrapper objects are not thread-safe.

\n
\n
\n

15.2.6.3. Reentrancy

\n

Binary buffered objects (instances of BufferedReader,\nBufferedWriter, BufferedRandom and BufferedRWPair)\nare not reentrant. While reentrant calls will not happen in normal situations,\nthey can arise if you are doing I/O in a signal handler. If it is\nattempted to enter a buffered object again while already being accessed\nfrom the same thread, then a RuntimeError is raised.

\n

The above implicitly extends to text files, since the open()\nfunction will wrap a buffered object inside a TextIOWrapper. This\nincludes standard streams and therefore affects the built-in function\nprint() as well.

\n
\n
\n
", "searchableItems": [ { "name": "io.BufferedIOBase", "domId": "io_io.BufferedIOBase" }, { "name": "io.BufferedIOBase.detach", "domId": "io_io.BufferedIOBase.detach" }, { "name": "io.BufferedIOBase.read", "domId": "io_io.BufferedIOBase.read" }, { "name": "io.BufferedIOBase.read1", "domId": "io_io.BufferedIOBase.read1" }, { "name": "io.BufferedIOBase.readinto", "domId": "io_io.BufferedIOBase.readinto" }, { "name": "io.BufferedIOBase.write", "domId": "io_io.BufferedIOBase.write" }, { "name": "io.BufferedRandom", "domId": "io_io.BufferedRandom" }, { "name": "io.BufferedReader", "domId": "io_io.BufferedReader" }, { "name": "io.BufferedReader.peek", "domId": "io_io.BufferedReader.peek" }, { "name": "io.BufferedReader.read", "domId": "io_io.BufferedReader.read" }, { "name": "io.BufferedReader.read1", "domId": "io_io.BufferedReader.read1" }, { "name": "io.BufferedRWPair", "domId": "io_io.BufferedRWPair" }, { "name": "io.BufferedWriter", "domId": "io_io.BufferedWriter" }, { "name": "io.BufferedWriter.flush", "domId": "io_io.BufferedWriter.flush" }, { "name": "io.BufferedWriter.write", "domId": "io_io.BufferedWriter.write" }, { "name": "io.BytesIO", "domId": "io_io.BytesIO" }, { "name": "io.BytesIO.getvalue", "domId": "io_io.BytesIO.getvalue" }, { "name": "io.BytesIO.read1", "domId": "io_io.BytesIO.read1" }, { "name": "io.FileIO", "domId": "io_io.FileIO" }, { "name": "io.IncrementalNewlineDecoder", "domId": "io_io.IncrementalNewlineDecoder" }, { "name": "io.IOBase", "domId": "io_io.IOBase" }, { "name": "io.IOBase.close", "domId": "io_io.IOBase.close" }, { "name": "io.IOBase.fileno", "domId": "io_io.IOBase.fileno" }, { "name": "io.IOBase.flush", "domId": "io_io.IOBase.flush" }, { "name": "io.IOBase.isatty", "domId": "io_io.IOBase.isatty" }, { "name": "io.IOBase.readable", "domId": "io_io.IOBase.readable" }, { "name": "io.IOBase.readline", "domId": "io_io.IOBase.readline" }, { "name": "io.IOBase.readlines", "domId": "io_io.IOBase.readlines" }, { "name": "io.IOBase.seek", "domId": "io_io.IOBase.seek" }, { "name": "io.IOBase.seekable", "domId": "io_io.IOBase.seekable" }, { "name": "io.IOBase.tell", "domId": "io_io.IOBase.tell" }, { "name": "io.IOBase.truncate", "domId": "io_io.IOBase.truncate" }, { "name": "io.IOBase.writable", "domId": "io_io.IOBase.writable" }, { "name": "io.IOBase.writelines", "domId": "io_io.IOBase.writelines" }, { "name": "io.open", "domId": "io_io.open" }, { "name": "io.RawIOBase", "domId": "io_io.RawIOBase" }, { "name": "io.RawIOBase.read", "domId": "io_io.RawIOBase.read" }, { "name": "io.RawIOBase.readall", "domId": "io_io.RawIOBase.readall" }, { "name": "io.RawIOBase.readinto", "domId": "io_io.RawIOBase.readinto" }, { "name": "io.RawIOBase.write", "domId": "io_io.RawIOBase.write" }, { "name": "io.StringIO", "domId": "io_io.StringIO" }, { "name": "io.StringIO.getvalue", "domId": "io_io.StringIO.getvalue" }, { "name": "io.TextIOBase", "domId": "io_io.TextIOBase" }, { "name": "io.TextIOBase.detach", "domId": "io_io.TextIOBase.detach" }, { "name": "io.TextIOBase.read", "domId": "io_io.TextIOBase.read" }, { "name": "io.TextIOBase.readline", "domId": "io_io.TextIOBase.readline" }, { "name": "io.TextIOBase.write", "domId": "io_io.TextIOBase.write" }, { "name": "io.TextIOWrapper", "domId": "io_io.TextIOWrapper" } ] }, { "url": "http://docs.python.org/library/os.html", "title": "os", "html": "
\n

15.1. os — Miscellaneous operating system interfaces

\n

This module provides a portable way of using operating system dependent\nfunctionality. If you just want to read or write a file see open(), if\nyou want to manipulate paths, see the os.path module, and if you want to\nread all the lines in all the files on the command line see the fileinput\nmodule. For creating temporary files and directories see the tempfile\nmodule, and for high-level file and directory handling see the shutil\nmodule.

\n

Notes on the availability of these functions:

\n\n
\n

Note

\n

All functions in this module raise OSError in the case of invalid or\ninaccessible file names and paths, or other arguments that have the correct\ntype, but are not accepted by the operating system.

\n
\n
\n
\nexception os.error
\n
An alias for the built-in OSError exception.
\n\n
\n
\nos.name
\n

The name of the operating system dependent module imported. The following\nnames have currently been registered: 'posix', 'nt',\n'os2', 'ce', 'java', 'riscos'.

\n
\n

See also

\n

sys.platform has a finer granularity. os.uname() gives\nsystem-dependent version information.

\n

The platform module provides detailed checks for the\nsystem’s identity.

\n
\n
\n\n
\n

15.1.1. Process Parameters

\n

These functions and data items provide information and operate on the current\nprocess and user.

\n
\n
\nos.environ
\n

A mapping object representing the string environment. For example,\nenviron['HOME'] is the pathname of your home directory (on some platforms),\nand is equivalent to getenv("HOME") in C.

\n

This mapping is captured the first time the os module is imported,\ntypically during Python startup as part of processing site.py. Changes\nto the environment made after this time are not reflected in os.environ,\nexcept for changes made by modifying os.environ directly.

\n

If the platform supports the putenv() function, this mapping may be used\nto modify the environment as well as query the environment. putenv() will\nbe called automatically when the mapping is modified.

\n
\n

Note

\n

Calling putenv() directly does not change os.environ, so it’s better\nto modify os.environ.

\n
\n
\n

Note

\n

On some platforms, including FreeBSD and Mac OS X, setting environ may\ncause memory leaks. Refer to the system documentation for\nputenv().

\n
\n

If putenv() is not provided, a modified copy of this mapping may be\npassed to the appropriate process-creation functions to cause child processes\nto use a modified environment.

\n

If the platform supports the unsetenv() function, you can delete items in\nthis mapping to unset environment variables. unsetenv() will be called\nautomatically when an item is deleted from os.environ, and when\none of the pop() or clear() methods is called.

\n

\nChanged in version 2.6: Also unset environment variables when calling os.environ.clear()\nand os.environ.pop().

\n
\n\n
\n
\nos.chdir(path)
\n
\nos.fchdir(fd)
\n
\nos.getcwd()
\n
These functions are described in Files and Directories.
\n\n
\n
\nos.ctermid()
\n

Return the filename corresponding to the controlling terminal of the process.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getegid()
\n

Return the effective group id of the current process. This corresponds to the\n“set id” bit on the file being executed in the current process.

\n

Availability: Unix.

\n
\n\n
\n
\nos.geteuid()
\n

Return the current process’s effective user id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getgid()
\n

Return the real group id of the current process.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getgroups()
\n

Return list of supplemental group ids associated with the current process.

\n

Availability: Unix.

\n
\n\n
\n
\nos.initgroups(username, gid)
\n

Call the system initgroups() to initialize the group access list with all of\nthe groups of which the specified username is a member, plus the specified\ngroup id.

\n

Availability: Unix.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nos.getlogin()
\n

Return the name of the user logged in on the controlling terminal of the\nprocess. For most purposes, it is more useful to use the environment variable\nLOGNAME to find out who the user is, or\npwd.getpwuid(os.getuid())[0] to get the login name of the currently\neffective user id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getpgid(pid)
\n

Return the process group id of the process with process id pid. If pid is 0,\nthe process group id of the current process is returned.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.getpgrp()
\n

Return the id of the current process group.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getpid()
\n

Return the current process id.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.getppid()
\n

Return the parent’s process id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getresuid()
\n

Return a tuple (ruid, euid, suid) denoting the current process’s\nreal, effective, and saved user ids.

\n

Availability: Unix.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nos.getresgid()
\n

Return a tuple (rgid, egid, sgid) denoting the current process’s\nreal, effective, and saved group ids.

\n

Availability: Unix.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nos.getuid()
\n

Return the current process’s user id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getenv(varname[, value])
\n

Return the value of the environment variable varname if it exists, or value\nif it doesn’t. value defaults to None.

\n

Availability: most flavors of Unix, Windows.

\n
\n\n
\n
\nos.putenv(varname, value)
\n

Set the environment variable named varname to the string value. Such\nchanges to the environment affect subprocesses started with os.system(),\npopen() or fork() and execv().

\n

Availability: most flavors of Unix, Windows.

\n
\n

Note

\n

On some platforms, including FreeBSD and Mac OS X, setting environ may\ncause memory leaks. Refer to the system documentation for putenv.

\n
\n

When putenv() is supported, assignments to items in os.environ are\nautomatically translated into corresponding calls to putenv(); however,\ncalls to putenv() don’t update os.environ, so it is actually\npreferable to assign to items of os.environ.

\n
\n\n
\n
\nos.setegid(egid)
\n

Set the current process’s effective group id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.seteuid(euid)
\n

Set the current process’s effective user id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.setgid(gid)
\n

Set the current process’ group id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.setgroups(groups)
\n

Set the list of supplemental group ids associated with the current process to\ngroups. groups must be a sequence, and each element must be an integer\nidentifying a group. This operation is typically available only to the superuser.

\n

Availability: Unix.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nos.setpgrp()
\n

Call the system call setpgrp() or setpgrp(0, 0)() depending on\nwhich version is implemented (if any). See the Unix manual for the semantics.

\n

Availability: Unix.

\n
\n\n
\n
\nos.setpgid(pid, pgrp)
\n

Call the system call setpgid() to set the process group id of the\nprocess with id pid to the process group with id pgrp. See the Unix manual\nfor the semantics.

\n

Availability: Unix.

\n
\n\n
\n
\nos.setregid(rgid, egid)
\n

Set the current process’s real and effective group ids.

\n

Availability: Unix.

\n
\n\n
\n
\nos.setresgid(rgid, egid, sgid)
\n

Set the current process’s real, effective, and saved group ids.

\n

Availability: Unix.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nos.setresuid(ruid, euid, suid)
\n

Set the current process’s real, effective, and saved user ids.

\n

Availability: Unix.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nos.setreuid(ruid, euid)
\n

Set the current process’s real and effective user ids.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getsid(pid)
\n

Call the system call getsid(). See the Unix manual for the semantics.

\n

Availability: Unix.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nos.setsid()
\n

Call the system call setsid(). See the Unix manual for the semantics.

\n

Availability: Unix.

\n
\n\n
\n
\nos.setuid(uid)
\n

Set the current process’s user id.

\n

Availability: Unix.

\n
\n\n
\n
\nos.strerror(code)
\n

Return the error message corresponding to the error code in code.\nOn platforms where strerror() returns NULL when given an unknown\nerror number, ValueError is raised.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.umask(mask)
\n

Set the current numeric umask and return the previous umask.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.uname()
\n

Return a 5-tuple containing information identifying the current operating\nsystem. The tuple contains 5 strings: (sysname, nodename, release, version,\nmachine). Some systems truncate the nodename to 8 characters or to the\nleading component; a better way to get the hostname is\nsocket.gethostname() or even\nsocket.gethostbyaddr(socket.gethostname()).

\n

Availability: recent flavors of Unix.

\n
\n\n
\n
\nos.unsetenv(varname)
\n

Unset (delete) the environment variable named varname. Such changes to the\nenvironment affect subprocesses started with os.system(), popen() or\nfork() and execv().

\n

When unsetenv() is supported, deletion of items in os.environ is\nautomatically translated into a corresponding call to unsetenv(); however,\ncalls to unsetenv() don’t update os.environ, so it is actually\npreferable to delete items of os.environ.

\n

Availability: most flavors of Unix, Windows.

\n
\n\n
\n
\n

15.1.2. File Object Creation

\n

These functions create new file objects. (See also open().)

\n
\n
\nos.fdopen(fd[, mode[, bufsize]])
\n

Return an open file object connected to the file descriptor fd. The mode\nand bufsize arguments have the same meaning as the corresponding arguments to\nthe built-in open() function.

\n

Availability: Unix, Windows.

\n

\nChanged in version 2.3: When specified, the mode argument must now start with one of the letters\n'r', 'w', or 'a', otherwise a ValueError is raised.

\n

\nChanged in version 2.5: On Unix, when the mode argument starts with 'a', the O_APPEND flag is\nset on the file descriptor (which the fdopen() implementation already\ndoes on most platforms).

\n
\n\n
\n
\nos.popen(command[, mode[, bufsize]])
\n

Open a pipe to or from command. The return value is an open file object\nconnected to the pipe, which can be read or written depending on whether mode\nis 'r' (default) or 'w'. The bufsize argument has the same meaning as\nthe corresponding argument to the built-in open() function. The exit\nstatus of the command (encoded in the format specified for wait()) is\navailable as the return value of the close() method of the file object,\nexcept that when the exit status is zero (termination without errors), None\nis returned.

\n

Availability: Unix, Windows.

\n

\nDeprecated since version 2.6: This function is obsolete. Use the subprocess module. Check\nespecially the Replacing Older Functions with the subprocess Module section.

\n

\nChanged in version 2.0: This function worked unreliably under Windows in earlier versions of Python.\nThis was due to the use of the _popen() function from the libraries\nprovided with Windows. Newer versions of Python do not use the broken\nimplementation from the Windows libraries.

\n
\n\n
\n
\nos.tmpfile()
\n

Return a new file object opened in update mode (w+b). The file has no\ndirectory entries associated with it and will be automatically deleted once\nthere are no file descriptors for the file.

\n

Availability: Unix, Windows.

\n
\n\n

There are a number of different popen*() functions that provide slightly\ndifferent ways to create subprocesses.

\n

\nDeprecated since version 2.6: All of the popen*() functions are obsolete. Use the subprocess\nmodule.

\n

For each of the popen*() variants, if bufsize is specified, it\nspecifies the buffer size for the I/O pipes. mode, if provided, should be the\nstring 'b' or 't'; on Windows this is needed to determine whether the\nfile objects should be opened in binary or text mode. The default value for\nmode is 't'.

\n

Also, for each of these variants, on Unix, cmd may be a sequence, in which\ncase arguments will be passed directly to the program without shell intervention\n(as with os.spawnv()). If cmd is a string it will be passed to the shell\n(as with os.system()).

\n

These methods do not make it possible to retrieve the exit status from the child\nprocesses. The only way to control the input and output streams and also\nretrieve the return codes is to use the subprocess module; these are only\navailable on Unix.

\n

For a discussion of possible deadlock conditions related to the use of these\nfunctions, see Flow Control Issues.

\n
\n
\nos.popen2(cmd[, mode[, bufsize]])
\n

Execute cmd as a sub-process and return the file objects (child_stdin,\nchild_stdout).

\n

\nDeprecated since version 2.6: This function is obsolete. Use the subprocess module. Check\nespecially the Replacing Older Functions with the subprocess Module section.

\n

Availability: Unix, Windows.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nos.popen3(cmd[, mode[, bufsize]])
\n

Execute cmd as a sub-process and return the file objects (child_stdin,\nchild_stdout, child_stderr).

\n

\nDeprecated since version 2.6: This function is obsolete. Use the subprocess module. Check\nespecially the Replacing Older Functions with the subprocess Module section.

\n

Availability: Unix, Windows.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nos.popen4(cmd[, mode[, bufsize]])
\n

Execute cmd as a sub-process and return the file objects (child_stdin,\nchild_stdout_and_stderr).

\n

\nDeprecated since version 2.6: This function is obsolete. Use the subprocess module. Check\nespecially the Replacing Older Functions with the subprocess Module section.

\n

Availability: Unix, Windows.

\n

\nNew in version 2.0.

\n
\n\n

(Note that child_stdin, child_stdout, and child_stderr are named from the\npoint of view of the child process, so child_stdin is the child’s standard\ninput.)

\n

This functionality is also available in the popen2 module using functions\nof the same names, but the return values of those functions have a different\norder.

\n
\n
\n

15.1.3. File Descriptor Operations

\n

These functions operate on I/O streams referenced using file descriptors.

\n

File descriptors are small integers corresponding to a file that has been opened\nby the current process. For example, standard input is usually file descriptor\n0, standard output is 1, and standard error is 2. Further files opened by a\nprocess will then be assigned 3, 4, 5, and so forth. The name “file descriptor”\nis slightly deceptive; on Unix platforms, sockets and pipes are also referenced\nby file descriptors.

\n

The fileno() method can be used to obtain the file descriptor\nassociated with a file object when required. Note that using the file\ndescriptor directly will bypass the file object methods, ignoring aspects such\nas internal buffering of data.

\n
\n
\nos.close(fd)
\n

Close file descriptor fd.

\n

Availability: Unix, Windows.

\n
\n

Note

\n

This function is intended for low-level I/O and must be applied to a file\ndescriptor as returned by os.open() or pipe(). To close a “file\nobject” returned by the built-in function open() or by popen() or\nfdopen(), use its close() method.

\n
\n
\n\n
\n
\nos.closerange(fd_low, fd_high)
\n

Close all file descriptors from fd_low (inclusive) to fd_high (exclusive),\nignoring errors. Equivalent to:

\n
for fd in xrange(fd_low, fd_high):\n    try:\n        os.close(fd)\n    except OSError:\n        pass\n
\n
\n

Availability: Unix, Windows.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nos.dup(fd)
\n

Return a duplicate of file descriptor fd.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.dup2(fd, fd2)
\n

Duplicate file descriptor fd to fd2, closing the latter first if necessary.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.fchmod(fd, mode)
\n

Change the mode of the file given by fd to the numeric mode. See the docs\nfor chmod() for possible values of mode.

\n

Availability: Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nos.fchown(fd, uid, gid)
\n

Change the owner and group id of the file given by fd to the numeric uid\nand gid. To leave one of the ids unchanged, set it to -1.

\n

Availability: Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nos.fdatasync(fd)
\n

Force write of file with filedescriptor fd to disk. Does not force update of\nmetadata.

\n

Availability: Unix.

\n
\n

Note

\n

This function is not available on MacOS.

\n
\n
\n\n
\n
\nos.fpathconf(fd, name)
\n

Return system configuration information relevant to an open file. name\nspecifies the configuration value to retrieve; it may be a string which is the\nname of a defined system value; these names are specified in a number of\nstandards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define\nadditional names as well. The names known to the host operating system are\ngiven in the pathconf_names dictionary. For configuration variables not\nincluded in that mapping, passing an integer for name is also accepted.

\n

If name is a string and is not known, ValueError is raised. If a\nspecific value for name is not supported by the host system, even if it is\nincluded in pathconf_names, an OSError is raised with\nerrno.EINVAL for the error number.

\n

Availability: Unix.

\n
\n\n
\n
\nos.fstat(fd)
\n

Return status for file descriptor fd, like stat().

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.fstatvfs(fd)
\n

Return information about the filesystem containing the file associated with file\ndescriptor fd, like statvfs().

\n

Availability: Unix.

\n
\n\n
\n
\nos.fsync(fd)
\n

Force write of file with filedescriptor fd to disk. On Unix, this calls the\nnative fsync() function; on Windows, the MS _commit() function.

\n

If you’re starting with a Python file object f, first do f.flush(), and\nthen do os.fsync(f.fileno()), to ensure that all internal buffers associated\nwith f are written to disk.

\n

Availability: Unix, and Windows starting in 2.2.3.

\n
\n\n
\n
\nos.ftruncate(fd, length)
\n

Truncate the file corresponding to file descriptor fd, so that it is at most\nlength bytes in size.

\n

Availability: Unix.

\n
\n\n
\n
\nos.isatty(fd)
\n

Return True if the file descriptor fd is open and connected to a\ntty(-like) device, else False.

\n

Availability: Unix.

\n
\n\n
\n
\nos.lseek(fd, pos, how)
\n

Set the current position of file descriptor fd to position pos, modified\nby how: SEEK_SET or 0 to set the position relative to the\nbeginning of the file; SEEK_CUR or 1 to set it relative to the\ncurrent position; os.SEEK_END or 2 to set it relative to the end of\nthe file.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.SEEK_SET
\n
\nos.SEEK_CUR
\n
\nos.SEEK_END
\n

Parameters to the lseek() function. Their values are 0, 1, and 2,\nrespectively.

\n

Availability: Windows, Unix.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nos.open(file, flags[, mode])
\n

Open the file file and set various flags according to flags and possibly its\nmode according to mode. The default mode is 0777 (octal), and the\ncurrent umask value is first masked out. Return the file descriptor for the\nnewly opened file.

\n

For a description of the flag and mode values, see the C run-time documentation;\nflag constants (like O_RDONLY and O_WRONLY) are defined in\nthis module too (see open() flag constants). In particular, on Windows adding\nO_BINARY is needed to open files in binary mode.

\n

Availability: Unix, Windows.

\n
\n

Note

\n

This function is intended for low-level I/O. For normal usage, use the\nbuilt-in function open(), which returns a “file object” with\nread() and write() methods (and many more). To\nwrap a file descriptor in a “file object”, use fdopen().

\n
\n
\n\n
\n
\nos.openpty()
\n

Open a new pseudo-terminal pair. Return a pair of file descriptors (master,\nslave) for the pty and the tty, respectively. For a (slightly) more portable\napproach, use the pty module.

\n

Availability: some flavors of Unix.

\n
\n\n
\n
\nos.pipe()
\n

Create a pipe. Return a pair of file descriptors (r, w) usable for reading\nand writing, respectively.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.read(fd, n)
\n

Read at most n bytes from file descriptor fd. Return a string containing the\nbytes read. If the end of the file referred to by fd has been reached, an\nempty string is returned.

\n

Availability: Unix, Windows.

\n
\n

Note

\n

This function is intended for low-level I/O and must be applied to a file\ndescriptor as returned by os.open() or pipe(). To read a “file object”\nreturned by the built-in function open() or by popen() or\nfdopen(), or sys.stdin, use its read() or\nreadline() methods.

\n
\n
\n\n
\n
\nos.tcgetpgrp(fd)
\n

Return the process group associated with the terminal given by fd (an open\nfile descriptor as returned by os.open()).

\n

Availability: Unix.

\n
\n\n
\n
\nos.tcsetpgrp(fd, pg)
\n

Set the process group associated with the terminal given by fd (an open file\ndescriptor as returned by os.open()) to pg.

\n

Availability: Unix.

\n
\n\n
\n
\nos.ttyname(fd)
\n

Return a string which specifies the terminal device associated with\nfile descriptor fd. If fd is not associated with a terminal device, an\nexception is raised.

\n

Availability: Unix.

\n
\n\n
\n
\nos.write(fd, str)
\n

Write the string str to file descriptor fd. Return the number of bytes\nactually written.

\n

Availability: Unix, Windows.

\n
\n

Note

\n

This function is intended for low-level I/O and must be applied to a file\ndescriptor as returned by os.open() or pipe(). To write a “file\nobject” returned by the built-in function open() or by popen() or\nfdopen(), or sys.stdout or sys.stderr, use its\nwrite() method.

\n
\n
\n\n
\n

15.1.3.1. open() flag constants

\n

The following constants are options for the flags parameter to the\nopen() function. They can be combined using the bitwise OR operator\n|. Some of them are not available on all platforms. For descriptions of\ntheir availability and use, consult the open(2) manual page on Unix\nor the MSDN on Windows.

\n
\n
\nos.O_RDONLY
\n
\nos.O_WRONLY
\n
\nos.O_RDWR
\n
\nos.O_APPEND
\n
\nos.O_CREAT
\n
\nos.O_EXCL
\n
\nos.O_TRUNC
\n
These constants are available on Unix and Windows.
\n\n
\n
\nos.O_DSYNC
\n
\nos.O_RSYNC
\n
\nos.O_SYNC
\n
\nos.O_NDELAY
\n
\nos.O_NONBLOCK
\n
\nos.O_NOCTTY
\n
\nos.O_SHLOCK
\n
\nos.O_EXLOCK
\n
These constants are only available on Unix.
\n\n
\n
\nos.O_BINARY
\n
\nos.O_NOINHERIT
\n
\nos.O_SHORT_LIVED
\n
\nos.O_TEMPORARY
\n
\nos.O_RANDOM
\n
\nos.O_SEQUENTIAL
\n
\nos.O_TEXT
\n
These constants are only available on Windows.
\n\n
\n
\nos.O_ASYNC
\n
\nos.O_DIRECT
\n
\nos.O_DIRECTORY
\n
\nos.O_NOFOLLOW
\n
\nos.O_NOATIME
\n
These constants are GNU extensions and not present if they are not defined by\nthe C library.
\n\n
\n
\n
\n

15.1.4. Files and Directories

\n
\n
\nos.access(path, mode)
\n

Use the real uid/gid to test for access to path. Note that most operations\nwill use the effective uid/gid, therefore this routine can be used in a\nsuid/sgid environment to test if the invoking user has the specified access to\npath. mode should be F_OK to test the existence of path, or it\ncan be the inclusive OR of one or more of R_OK, W_OK, and\nX_OK to test permissions. Return True if access is allowed,\nFalse if not. See the Unix man page access(2) for more\ninformation.

\n

Availability: Unix, Windows.

\n
\n

Note

\n

Using access() to check if a user is authorized to e.g. open a file\nbefore actually doing so using open() creates a security hole,\nbecause the user might exploit the short time interval between checking\nand opening the file to manipulate it. It’s preferable to use EAFP\ntechniques. For example:

\n
if os.access("myfile", os.R_OK):\n    with open("myfile") as fp:\n        return fp.read()\nreturn "some default data"\n
\n
\n

is better written as:

\n
try:\n    fp = open("myfile")\nexcept IOError as e:\n    if e.errno == errno.EACCES:\n        return "some default data"\n    # Not a permission error.\n    raise\nelse:\n    with fp:\n        return fp.read()\n
\n
\n
\n
\n

Note

\n

I/O operations may fail even when access() indicates that they would\nsucceed, particularly for operations on network filesystems which may have\npermissions semantics beyond the usual POSIX permission-bit model.

\n
\n
\n\n
\n
\nos.F_OK
\n
Value to pass as the mode parameter of access() to test the existence of\npath.
\n\n
\n
\nos.R_OK
\n
Value to include in the mode parameter of access() to test the\nreadability of path.
\n\n
\n
\nos.W_OK
\n
Value to include in the mode parameter of access() to test the\nwritability of path.
\n\n
\n
\nos.X_OK
\n
Value to include in the mode parameter of access() to determine if\npath can be executed.
\n\n
\n
\nos.chdir(path)
\n

Change the current working directory to path.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.fchdir(fd)
\n

Change the current working directory to the directory represented by the file\ndescriptor fd. The descriptor must refer to an opened directory, not an open\nfile.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.getcwd()
\n

Return a string representing the current working directory.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.getcwdu()
\n

Return a Unicode object representing the current working directory.

\n

Availability: Unix, Windows.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.chflags(path, flags)
\n

Set the flags of path to the numeric flags. flags may take a combination\n(bitwise OR) of the following values (as defined in the stat module):

\n\n

Availability: Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nos.chroot(path)
\n

Change the root directory of the current process to path. Availability:\nUnix.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nos.chmod(path, mode)
\n

Change the mode of path to the numeric mode. mode may take one of the\nfollowing values (as defined in the stat module) or bitwise ORed\ncombinations of them:

\n\n

Availability: Unix, Windows.

\n
\n

Note

\n

Although Windows supports chmod(), you can only set the file’s read-only\nflag with it (via the stat.S_IWRITE and stat.S_IREAD\nconstants or a corresponding integer value). All other bits are\nignored.

\n
\n
\n\n
\n
\nos.chown(path, uid, gid)
\n

Change the owner and group id of path to the numeric uid and gid. To leave\none of the ids unchanged, set it to -1.

\n

Availability: Unix.

\n
\n\n
\n
\nos.lchflags(path, flags)
\n

Set the flags of path to the numeric flags, like chflags(), but do not\nfollow symbolic links.

\n

Availability: Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nos.lchmod(path, mode)
\n

Change the mode of path to the numeric mode. If path is a symlink, this\naffects the symlink rather than the target. See the docs for chmod()\nfor possible values of mode.

\n

Availability: Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nos.lchown(path, uid, gid)
\n

Change the owner and group id of path to the numeric uid and gid. This\nfunction will not follow symbolic links.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.link(source, link_name)
\n

Create a hard link pointing to source named link_name.

\n

Availability: Unix.

\n
\n\n
\n
\nos.listdir(path)
\n

Return a list containing the names of the entries in the directory given by\npath. The list is in arbitrary order. It does not include the special\nentries '.' and '..' even if they are present in the\ndirectory.

\n

Availability: Unix, Windows.

\n

\nChanged in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be\na list of Unicode objects. Undecodable filenames will still be returned as\nstring objects.

\n
\n\n
\n
\nos.lstat(path)
\n
Perform the equivalent of an lstat() system call on the given path.\nSimilar to stat(), but does not follow symbolic links. On\nplatforms that do not support symbolic links, this is an alias for\nstat().
\n\n
\n
\nos.mkfifo(path[, mode])
\n

Create a FIFO (a named pipe) named path with numeric mode mode. The default\nmode is 0666 (octal). The current umask value is first masked out from\nthe mode.

\n

Availability: Unix.

\n

FIFOs are pipes that can be accessed like regular files. FIFOs exist until they\nare deleted (for example with os.unlink()). Generally, FIFOs are used as\nrendezvous between “client” and “server” type processes: the server opens the\nFIFO for reading, and the client opens it for writing. Note that mkfifo()\ndoesn’t open the FIFO — it just creates the rendezvous point.

\n
\n\n
\n
\nos.mknod(filename[, mode=0600, device])
\n

Create a filesystem node (file, device special file or named pipe) named\nfilename. mode specifies both the permissions to use and the type of node to\nbe created, being combined (bitwise OR) with one of stat.S_IFREG,\nstat.S_IFCHR, stat.S_IFBLK,\nand stat.S_IFIFO (those constants are available in stat).\nFor stat.S_IFCHR and\nstat.S_IFBLK, device defines the newly created device special file (probably using\nos.makedev()), otherwise it is ignored.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.major(device)
\n

Extract the device major number from a raw device number (usually the\nst_dev or st_rdev field from stat).

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.minor(device)
\n

Extract the device minor number from a raw device number (usually the\nst_dev or st_rdev field from stat).

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.makedev(major, minor)
\n

Compose a raw device number from the major and minor device numbers.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.mkdir(path[, mode])
\n

Create a directory named path with numeric mode mode. The default mode is\n0777 (octal). On some systems, mode is ignored. Where it is used, the\ncurrent umask value is first masked out. If the directory already exists,\nOSError is raised.

\n

It is also possible to create temporary directories; see the\ntempfile module’s tempfile.mkdtemp() function.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.makedirs(path[, mode])
\n

Recursive directory creation function. Like mkdir(), but makes all\nintermediate-level directories needed to contain the leaf directory. Raises an\nerror exception if the leaf directory already exists or cannot be\ncreated. The default mode is 0777 (octal). On some systems, mode is\nignored. Where it is used, the current umask value is first masked out.

\n
\n

Note

\n

makedirs() will become confused if the path elements to create include\nos.pardir.

\n
\n

\nNew in version 1.5.2.

\n

\nChanged in version 2.3: This function now handles UNC paths correctly.

\n
\n\n
\n
\nos.pathconf(path, name)
\n

Return system configuration information relevant to a named file. name\nspecifies the configuration value to retrieve; it may be a string which is the\nname of a defined system value; these names are specified in a number of\nstandards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define\nadditional names as well. The names known to the host operating system are\ngiven in the pathconf_names dictionary. For configuration variables not\nincluded in that mapping, passing an integer for name is also accepted.

\n

If name is a string and is not known, ValueError is raised. If a\nspecific value for name is not supported by the host system, even if it is\nincluded in pathconf_names, an OSError is raised with\nerrno.EINVAL for the error number.

\n

Availability: Unix.

\n
\n\n
\n
\nos.pathconf_names
\n
Dictionary mapping names accepted by pathconf() and fpathconf() to\nthe integer values defined for those names by the host operating system. This\ncan be used to determine the set of names known to the system. Availability:\nUnix.
\n\n
\n
\nos.readlink(path)
\n

Return a string representing the path to which the symbolic link points. The\nresult may be either an absolute or relative pathname; if it is relative, it may\nbe converted to an absolute pathname using os.path.join(os.path.dirname(path),\nresult).

\n

\nChanged in version 2.6: If the path is a Unicode object the result will also be a Unicode object.

\n

Availability: Unix.

\n
\n\n
\n
\nos.remove(path)
\n

Remove (delete) the file path. If path is a directory, OSError is\nraised; see rmdir() below to remove a directory. This is identical to\nthe unlink() function documented below. On Windows, attempting to\nremove a file that is in use causes an exception to be raised; on Unix, the\ndirectory entry is removed but the storage allocated to the file is not made\navailable until the original file is no longer in use.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.removedirs(path)
\n

Remove directories recursively. Works like rmdir() except that, if the\nleaf directory is successfully removed, removedirs() tries to\nsuccessively remove every parent directory mentioned in path until an error\nis raised (which is ignored, because it generally means that a parent directory\nis not empty). For example, os.removedirs('foo/bar/baz') will first remove\nthe directory 'foo/bar/baz', and then remove 'foo/bar' and 'foo' if\nthey are empty. Raises OSError if the leaf directory could not be\nsuccessfully removed.

\n

\nNew in version 1.5.2.

\n
\n\n
\n
\nos.rename(src, dst)
\n

Rename the file or directory src to dst. If dst is a directory,\nOSError will be raised. On Unix, if dst exists and is a file, it will\nbe replaced silently if the user has permission. The operation may fail on some\nUnix flavors if src and dst are on different filesystems. If successful,\nthe renaming will be an atomic operation (this is a POSIX requirement). On\nWindows, if dst already exists, OSError will be raised even if it is a\nfile; there may be no way to implement an atomic rename when dst names an\nexisting file.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.renames(old, new)
\n

Recursive directory or file renaming function. Works like rename(), except\ncreation of any intermediate directories needed to make the new pathname good is\nattempted first. After the rename, directories corresponding to rightmost path\nsegments of the old name will be pruned away using removedirs().

\n

\nNew in version 1.5.2.

\n
\n

Note

\n

This function can fail with the new directory structure made if you lack\npermissions needed to remove the leaf directory or file.

\n
\n
\n\n
\n
\nos.rmdir(path)
\n

Remove (delete) the directory path. Only works when the directory is\nempty, otherwise, OSError is raised. In order to remove whole\ndirectory trees, shutil.rmtree() can be used.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.stat(path)
\n

Perform the equivalent of a stat() system call on the given path.\n(This function follows symlinks; to stat a symlink use lstat().)

\n

The return value is an object whose attributes correspond to the members\nof the stat structure, namely:

\n
    \n
  • st_mode - protection bits,
  • \n
  • st_ino - inode number,
  • \n
  • st_dev - device,
  • \n
  • st_nlink - number of hard links,
  • \n
  • st_uid - user id of owner,
  • \n
  • st_gid - group id of owner,
  • \n
  • st_size - size of file, in bytes,
  • \n
  • st_atime - time of most recent access,
  • \n
  • st_mtime - time of most recent content modification,
  • \n
  • st_ctime - platform dependent; time of most recent metadata change on\nUnix, or the time of creation on Windows)
  • \n
\n

\nChanged in version 2.3: If stat_float_times() returns True, the time values are floats, measuring\nseconds. Fractions of a second may be reported if the system supports that. On\nMac OS, the times are always floats. See stat_float_times() for further\ndiscussion.

\n

On some Unix systems (such as Linux), the following attributes may also be\navailable:

\n
    \n
  • st_blocks - number of blocks allocated for file
  • \n
  • st_blksize - filesystem blocksize
  • \n
  • st_rdev - type of device if an inode device
  • \n
  • st_flags - user defined flags for file
  • \n
\n

On other Unix systems (such as FreeBSD), the following attributes may be\navailable (but may be only filled out if root tries to use them):

\n
    \n
  • st_gen - file generation number
  • \n
  • st_birthtime - time of file creation
  • \n
\n

On Mac OS systems, the following attributes may also be available:

\n
    \n
  • st_rsize
  • \n
  • st_creator
  • \n
  • st_type
  • \n
\n

On RISCOS systems, the following attributes are also available:

\n
    \n
  • st_ftype (file type)
  • \n
  • st_attrs (attributes)
  • \n
  • st_obtype (object type).
  • \n
\n
\n

Note

\n

The exact meaning and resolution of the st_atime,\nst_mtime, and st_ctime attributes depend on the operating\nsystem and the file system. For example, on Windows systems using the FAT\nor FAT32 file systems, st_mtime has 2-second resolution, and\nst_atime has only 1-day resolution. See your operating system\ndocumentation for details.

\n
\n

For backward compatibility, the return value of stat() is also accessible\nas a tuple of at least 10 integers giving the most important (and portable)\nmembers of the stat structure, in the order st_mode,\nst_ino, st_dev, st_nlink, st_uid,\nst_gid, st_size, st_atime, st_mtime,\nst_ctime. More items may be added at the end by some implementations.

\n

The standard module stat defines functions and constants that are useful\nfor extracting information from a stat structure. (On Windows, some\nitems are filled with dummy values.)

\n

Example:

\n
>>> import os\n>>> statinfo = os.stat('somefile.txt')\n>>> statinfo\n(33188, 422511, 769, 1, 1032, 100, 926, 1105022698,1105022732, 1105022732)\n>>> statinfo.st_size\n926\n
\n
\n

Availability: Unix, Windows.

\n

\nChanged in version 2.2: Added access to values as attributes of the returned object.

\n

\nChanged in version 2.5: Added st_gen and st_birthtime.

\n
\n\n
\n
\nos.stat_float_times([newvalue])
\n

Determine whether stat_result represents time stamps as float objects.\nIf newvalue is True, future calls to stat() return floats, if it is\nFalse, future calls return ints. If newvalue is omitted, return the\ncurrent setting.

\n

For compatibility with older Python versions, accessing stat_result as\na tuple always returns integers.

\n

\nChanged in version 2.5: Python now returns float values by default. Applications which do not work\ncorrectly with floating point time stamps can use this function to restore the\nold behaviour.

\n

The resolution of the timestamps (that is the smallest possible fraction)\ndepends on the system. Some systems only support second resolution; on these\nsystems, the fraction will always be zero.

\n

It is recommended that this setting is only changed at program startup time in\nthe __main__ module; libraries should never change this setting. If an\napplication uses a library that works incorrectly if floating point time stamps\nare processed, this application should turn the feature off until the library\nhas been corrected.

\n
\n\n
\n
\nos.statvfs(path)
\n

Perform a statvfs() system call on the given path. The return value is\nan object whose attributes describe the filesystem on the given path, and\ncorrespond to the members of the statvfs structure, namely:\nf_bsize, f_frsize, f_blocks, f_bfree,\nf_bavail, f_files, f_ffree, f_favail,\nf_flag, f_namemax.

\n

For backward compatibility, the return value is also accessible as a tuple whose\nvalues correspond to the attributes, in the order given above. The standard\nmodule statvfs defines constants that are useful for extracting\ninformation from a statvfs structure when accessing it as a sequence;\nthis remains useful when writing code that needs to work with versions of Python\nthat don’t support accessing the fields as attributes.

\n

Availability: Unix.

\n

\nChanged in version 2.2: Added access to values as attributes of the returned object.

\n
\n\n
\n
\nos.symlink(source, link_name)
\n

Create a symbolic link pointing to source named link_name.

\n

Availability: Unix.

\n
\n\n
\n
\nos.tempnam([dir[, prefix]])
\n

Return a unique path name that is reasonable for creating a temporary file.\nThis will be an absolute path that names a potential directory entry in the\ndirectory dir or a common location for temporary files if dir is omitted or\nNone. If given and not None, prefix is used to provide a short prefix\nto the filename. Applications are responsible for properly creating and\nmanaging files created using paths returned by tempnam(); no automatic\ncleanup is provided. On Unix, the environment variable TMPDIR\noverrides dir, while on Windows TMP is used. The specific\nbehavior of this function depends on the C library implementation; some aspects\nare underspecified in system documentation.

\n
\n

Warning

\n

Use of tempnam() is vulnerable to symlink attacks; consider using\ntmpfile() (section File Object Creation) instead.

\n
\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.tmpnam()
\n

Return a unique path name that is reasonable for creating a temporary file.\nThis will be an absolute path that names a potential directory entry in a common\nlocation for temporary files. Applications are responsible for properly\ncreating and managing files created using paths returned by tmpnam(); no\nautomatic cleanup is provided.

\n
\n

Warning

\n

Use of tmpnam() is vulnerable to symlink attacks; consider using\ntmpfile() (section File Object Creation) instead.

\n
\n

Availability: Unix, Windows. This function probably shouldn’t be used on\nWindows, though: Microsoft’s implementation of tmpnam() always creates a\nname in the root directory of the current drive, and that’s generally a poor\nlocation for a temp file (depending on privileges, you may not even be able to\nopen a file using this name).

\n
\n\n
\n
\nos.TMP_MAX
\n
The maximum number of unique names that tmpnam() will generate before\nreusing names.
\n\n
\n
\nos.unlink(path)
\n

Remove (delete) the file path. This is the same function as\nremove(); the unlink() name is its traditional Unix\nname.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.utime(path, times)
\n

Set the access and modified times of the file specified by path. If times\nis None, then the file’s access and modified times are set to the current\ntime. (The effect is similar to running the Unix program touch on\nthe path.) Otherwise, times must be a 2-tuple of numbers, of the form\n(atime, mtime) which is used to set the access and modified times,\nrespectively. Whether a directory can be given for path depends on whether\nthe operating system implements directories as files (for example, Windows\ndoes not). Note that the exact times you set here may not be returned by a\nsubsequent stat() call, depending on the resolution with which your\noperating system records access and modification times; see stat().

\n

\nChanged in version 2.0: Added support for None for times.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
\n

Generate the file names in a directory tree by walking the tree\neither top-down or bottom-up. For each directory in the tree rooted at directory\ntop (including top itself), it yields a 3-tuple (dirpath, dirnames,\nfilenames).

\n

dirpath is a string, the path to the directory. dirnames is a list of the\nnames of the subdirectories in dirpath (excluding '.' and '..').\nfilenames is a list of the names of the non-directory files in dirpath.\nNote that the names in the lists contain no path components. To get a full path\n(which begins with top) to a file or directory in dirpath, do\nos.path.join(dirpath, name).

\n

If optional argument topdown is True or not specified, the triple for a\ndirectory is generated before the triples for any of its subdirectories\n(directories are generated top-down). If topdown is False, the triple for a\ndirectory is generated after the triples for all of its subdirectories\n(directories are generated bottom-up).

\n

When topdown is True, the caller can modify the dirnames list in-place\n(perhaps using del or slice assignment), and walk() will only\nrecurse into the subdirectories whose names remain in dirnames; this can be\nused to prune the search, impose a specific order of visiting, or even to inform\nwalk() about directories the caller creates or renames before it resumes\nwalk() again. Modifying dirnames when topdown is False is\nineffective, because in bottom-up mode the directories in dirnames are\ngenerated before dirpath itself is generated.

\n

By default, errors from the listdir() call are ignored. If optional\nargument onerror is specified, it should be a function; it will be called with\none argument, an OSError instance. It can report the error to continue\nwith the walk, or raise the exception to abort the walk. Note that the filename\nis available as the filename attribute of the exception object.

\n

By default, walk() will not walk down into symbolic links that resolve to\ndirectories. Set followlinks to True to visit directories pointed to by\nsymlinks, on systems that support them.

\n

\nNew in version 2.6: The followlinks parameter.

\n
\n

Note

\n

Be aware that setting followlinks to True can lead to infinite recursion if a\nlink points to a parent directory of itself. walk() does not keep track of\nthe directories it visited already.

\n
\n
\n

Note

\n

If you pass a relative pathname, don’t change the current working directory\nbetween resumptions of walk(). walk() never changes the current\ndirectory, and assumes that its caller doesn’t either.

\n
\n

This example displays the number of bytes taken by non-directory files in each\ndirectory under the starting directory, except that it doesn’t look under any\nCVS subdirectory:

\n
import os\nfrom os.path import join, getsize\nfor root, dirs, files in os.walk('python/Lib/email'):\n    print root, "consumes",\n    print sum(getsize(join(root, name)) for name in files),\n    print "bytes in", len(files), "non-directory files"\n    if 'CVS' in dirs:\n        dirs.remove('CVS')  # don't visit CVS directories\n
\n
\n

In the next example, walking the tree bottom-up is essential: rmdir()\ndoesn’t allow deleting a directory before the directory is empty:

\n
# Delete everything reachable from the directory named in "top",\n# assuming there are no symbolic links.\n# CAUTION:  This is dangerous!  For example, if top == '/', it\n# could delete all your disk files.\nimport os\nfor root, dirs, files in os.walk(top, topdown=False):\n    for name in files:\n        os.remove(os.path.join(root, name))\n    for name in dirs:\n        os.rmdir(os.path.join(root, name))\n
\n
\n

\nNew in version 2.3.

\n
\n\n
\n
\n

15.1.5. Process Management

\n

These functions may be used to create and manage processes.

\n

The various exec*() functions take a list of arguments for the new\nprogram loaded into the process. In each case, the first of these arguments is\npassed to the new program as its own name rather than as an argument a user may\nhave typed on a command line. For the C programmer, this is the argv[0]\npassed to a program’s main(). For example, os.execv('/bin/echo',\n['foo', 'bar']) will only print bar on standard output; foo will seem\nto be ignored.

\n
\n
\nos.abort()
\n

Generate a SIGABRT signal to the current process. On Unix, the default\nbehavior is to produce a core dump; on Windows, the process immediately returns\nan exit code of 3. Be aware that calling this function will not call the\nPython signal handler registered for SIGABRT with\nsignal.signal().

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.execl(path, arg0, arg1, ...)
\n
\nos.execle(path, arg0, arg1, ..., env)
\n
\nos.execlp(file, arg0, arg1, ...)
\n
\nos.execlpe(file, arg0, arg1, ..., env)
\n
\nos.execv(path, args)
\n
\nos.execve(path, args, env)
\n
\nos.execvp(file, args)
\n
\nos.execvpe(file, args, env)
\n

These functions all execute a new program, replacing the current process; they\ndo not return. On Unix, the new executable is loaded into the current process,\nand will have the same process id as the caller. Errors will be reported as\nOSError exceptions.

\n

The current process is replaced immediately. Open file objects and\ndescriptors are not flushed, so if there may be data buffered\non these open files, you should flush them using\nsys.stdout.flush() or os.fsync() before calling an\nexec*() function.

\n

The “l” and “v” variants of the exec*() functions differ in how\ncommand-line arguments are passed. The “l” variants are perhaps the easiest\nto work with if the number of parameters is fixed when the code is written; the\nindividual parameters simply become additional parameters to the execl*()\nfunctions. The “v” variants are good when the number of parameters is\nvariable, with the arguments being passed in a list or tuple as the args\nparameter. In either case, the arguments to the child process should start with\nthe name of the command being run, but this is not enforced.

\n

The variants which include a “p” near the end (execlp(),\nexeclpe(), execvp(), and execvpe()) will use the\nPATH environment variable to locate the program file. When the\nenvironment is being replaced (using one of the exec*e() variants,\ndiscussed in the next paragraph), the new environment is used as the source of\nthe PATH variable. The other variants, execl(), execle(),\nexecv(), and execve(), will not use the PATH variable to\nlocate the executable; path must contain an appropriate absolute or relative\npath.

\n

For execle(), execlpe(), execve(), and execvpe() (note\nthat these all end in “e”), the env parameter must be a mapping which is\nused to define the environment variables for the new process (these are used\ninstead of the current process’ environment); the functions execl(),\nexeclp(), execv(), and execvp() all cause the new process to\ninherit the environment of the current process.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos._exit(n)
\n

Exit the process with status n, without calling cleanup handlers, flushing\nstdio buffers, etc.

\n

Availability: Unix, Windows.

\n
\n

Note

\n

The standard way to exit is sys.exit(n). _exit() should\nnormally only be used in the child process after a fork().

\n
\n
\n\n

The following exit codes are defined and can be used with _exit(),\nalthough they are not required. These are typically used for system programs\nwritten in Python, such as a mail server’s external command delivery program.

\n
\n

Note

\n

Some of these may not be available on all Unix platforms, since there is some\nvariation. These constants are defined where they are defined by the underlying\nplatform.

\n
\n
\n
\nos.EX_OK
\n

Exit code that means no error occurred.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_USAGE
\n

Exit code that means the command was used incorrectly, such as when the wrong\nnumber of arguments are given.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_DATAERR
\n

Exit code that means the input data was incorrect.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_NOINPUT
\n

Exit code that means an input file did not exist or was not readable.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_NOUSER
\n

Exit code that means a specified user did not exist.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_NOHOST
\n

Exit code that means a specified host did not exist.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_UNAVAILABLE
\n

Exit code that means that a required service is unavailable.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_SOFTWARE
\n

Exit code that means an internal software error was detected.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_OSERR
\n

Exit code that means an operating system error was detected, such as the\ninability to fork or create a pipe.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_OSFILE
\n

Exit code that means some system file did not exist, could not be opened, or had\nsome other kind of error.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_CANTCREAT
\n

Exit code that means a user specified output file could not be created.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_IOERR
\n

Exit code that means that an error occurred while doing I/O on some file.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_TEMPFAIL
\n

Exit code that means a temporary failure occurred. This indicates something\nthat may not really be an error, such as a network connection that couldn’t be\nmade during a retryable operation.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_PROTOCOL
\n

Exit code that means that a protocol exchange was illegal, invalid, or not\nunderstood.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_NOPERM
\n

Exit code that means that there were insufficient permissions to perform the\noperation (but not intended for file system problems).

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_CONFIG
\n

Exit code that means that some kind of configuration error occurred.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.EX_NOTFOUND
\n

Exit code that means something like “an entry was not found”.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.fork()
\n

Fork a child process. Return 0 in the child and the child’s process id in the\nparent. If an error occurs OSError is raised.

\n

Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have\nknown issues when using fork() from a thread.

\n

Availability: Unix.

\n
\n\n
\n
\nos.forkpty()
\n

Fork a child process, using a new pseudo-terminal as the child’s controlling\nterminal. Return a pair of (pid, fd), where pid is 0 in the child, the\nnew child’s process id in the parent, and fd is the file descriptor of the\nmaster end of the pseudo-terminal. For a more portable approach, use the\npty module. If an error occurs OSError is raised.

\n

Availability: some flavors of Unix.

\n
\n\n
\n
\nos.kill(pid, sig)
\n

Send signal sig to the process pid. Constants for the specific signals\navailable on the host platform are defined in the signal module.

\n

Windows: The signal.CTRL_C_EVENT and\nsignal.CTRL_BREAK_EVENT signals are special signals which can\nonly be sent to console processes which share a common console window,\ne.g., some subprocesses. Any other value for sig will cause the process\nto be unconditionally killed by the TerminateProcess API, and the exit code\nwill be set to sig. The Windows version of kill() additionally takes\nprocess handles to be killed.

\n

\nNew in version 2.7: Windows support

\n
\n\n
\n
\nos.killpg(pgid, sig)
\n

Send the signal sig to the process group pgid.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.nice(increment)
\n

Add increment to the process’s “niceness”. Return the new niceness.

\n

Availability: Unix.

\n
\n\n
\n
\nos.plock(op)
\n

Lock program segments into memory. The value of op (defined in\n<sys/lock.h>) determines which segments are locked.

\n

Availability: Unix.

\n
\n\n
\n
\nos.popen(...)
\n
\nos.popen2(...)
\n
\nos.popen3(...)
\n
\nos.popen4(...)
\n
Run child processes, returning opened pipes for communications. These functions\nare described in section File Object Creation.
\n\n
\n
\nos.spawnl(mode, path, ...)
\n
\nos.spawnle(mode, path, ..., env)
\n
\nos.spawnlp(mode, file, ...)
\n
\nos.spawnlpe(mode, file, ..., env)
\n
\nos.spawnv(mode, path, args)
\n
\nos.spawnve(mode, path, args, env)
\n
\nos.spawnvp(mode, file, args)
\n
\nos.spawnvpe(mode, file, args, env)
\n

Execute the program path in a new process.

\n

(Note that the subprocess module provides more powerful facilities for\nspawning new processes and retrieving their results; using that module is\npreferable to using these functions. Check especially the\nReplacing Older Functions with the subprocess Module section.)

\n

If mode is P_NOWAIT, this function returns the process id of the new\nprocess; if mode is P_WAIT, returns the process’s exit code if it\nexits normally, or -signal, where signal is the signal that killed the\nprocess. On Windows, the process id will actually be the process handle, so can\nbe used with the waitpid() function.

\n

The “l” and “v” variants of the spawn*() functions differ in how\ncommand-line arguments are passed. The “l” variants are perhaps the easiest\nto work with if the number of parameters is fixed when the code is written; the\nindividual parameters simply become additional parameters to the\nspawnl*() functions. The “v” variants are good when the number of\nparameters is variable, with the arguments being passed in a list or tuple as\nthe args parameter. In either case, the arguments to the child process must\nstart with the name of the command being run.

\n

The variants which include a second “p” near the end (spawnlp(),\nspawnlpe(), spawnvp(), and spawnvpe()) will use the\nPATH environment variable to locate the program file. When the\nenvironment is being replaced (using one of the spawn*e() variants,\ndiscussed in the next paragraph), the new environment is used as the source of\nthe PATH variable. The other variants, spawnl(),\nspawnle(), spawnv(), and spawnve(), will not use the\nPATH variable to locate the executable; path must contain an\nappropriate absolute or relative path.

\n

For spawnle(), spawnlpe(), spawnve(), and spawnvpe()\n(note that these all end in “e”), the env parameter must be a mapping\nwhich is used to define the environment variables for the new process (they are\nused instead of the current process’ environment); the functions\nspawnl(), spawnlp(), spawnv(), and spawnvp() all cause\nthe new process to inherit the environment of the current process. Note that\nkeys and values in the env dictionary must be strings; invalid keys or\nvalues will cause the function to fail, with a return value of 127.

\n

As an example, the following calls to spawnlp() and spawnvpe() are\nequivalent:

\n
import os\nos.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')\n\nL = ['cp', 'index.html', '/dev/null']\nos.spawnvpe(os.P_WAIT, 'cp', L, os.environ)\n
\n
\n

Availability: Unix, Windows. spawnlp(), spawnlpe(), spawnvp()\nand spawnvpe() are not available on Windows. spawnle() and\nspawnve() are not thread-safe on Windows; we advise you to use the\nsubprocess module instead.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nos.P_NOWAIT
\n
\nos.P_NOWAITO
\n

Possible values for the mode parameter to the spawn*() family of\nfunctions. If either of these values is given, the spawn*() functions\nwill return as soon as the new process has been created, with the process id as\nthe return value.

\n

Availability: Unix, Windows.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nos.P_WAIT
\n

Possible value for the mode parameter to the spawn*() family of\nfunctions. If this is given as mode, the spawn*() functions will not\nreturn until the new process has run to completion and will return the exit code\nof the process the run is successful, or -signal if a signal kills the\nprocess.

\n

Availability: Unix, Windows.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nos.P_DETACH
\n
\nos.P_OVERLAY
\n

Possible values for the mode parameter to the spawn*() family of\nfunctions. These are less portable than those listed above. P_DETACH\nis similar to P_NOWAIT, but the new process is detached from the\nconsole of the calling process. If P_OVERLAY is used, the current\nprocess will be replaced; the spawn*() function will not return.

\n

Availability: Windows.

\n

\nNew in version 1.6.

\n
\n\n
\n
\nos.startfile(path[, operation])
\n

Start a file with its associated application.

\n

When operation is not specified or 'open', this acts like double-clicking\nthe file in Windows Explorer, or giving the file name as an argument to the\nstart command from the interactive command shell: the file is opened\nwith whatever application (if any) its extension is associated.

\n

When another operation is given, it must be a “command verb” that specifies\nwhat should be done with the file. Common verbs documented by Microsoft are\n'print' and 'edit' (to be used on files) as well as 'explore' and\n'find' (to be used on directories).

\n

startfile() returns as soon as the associated application is launched.\nThere is no option to wait for the application to close, and no way to retrieve\nthe application’s exit status. The path parameter is relative to the current\ndirectory. If you want to use an absolute path, make sure the first character\nis not a slash ('/'); the underlying Win32 ShellExecute() function\ndoesn’t work if it is. Use the os.path.normpath() function to ensure that\nthe path is properly encoded for Win32.

\n

Availability: Windows.

\n

\nNew in version 2.0.

\n

\nNew in version 2.5: The operation parameter.

\n
\n\n
\n
\nos.system(command)
\n

Execute the command (a string) in a subshell. This is implemented by calling\nthe Standard C function system(), and has the same limitations.\nChanges to sys.stdin, etc. are not reflected in the environment of the\nexecuted command.

\n

On Unix, the return value is the exit status of the process encoded in the\nformat specified for wait(). Note that POSIX does not specify the meaning\nof the return value of the C system() function, so the return value of\nthe Python function is system-dependent.

\n

On Windows, the return value is that returned by the system shell after running\ncommand, given by the Windows environment variable COMSPEC: on\ncommand.com systems (Windows 95, 98 and ME) this is always 0; on\ncmd.exe systems (Windows NT, 2000 and XP) this is the exit status of\nthe command run; on systems using a non-native shell, consult your shell\ndocumentation.

\n

The subprocess module provides more powerful facilities for spawning new\nprocesses and retrieving their results; using that module is preferable to using\nthis function. See the\nReplacing Older Functions with the subprocess Module section in the subprocess documentation\nfor some helpful recipes.

\n

Availability: Unix, Windows.

\n
\n\n
\n
\nos.times()
\n

Return a 5-tuple of floating point numbers indicating accumulated (processor\nor other) times, in seconds. The items are: user time, system time,\nchildren’s user time, children’s system time, and elapsed real time since a\nfixed point in the past, in that order. See the Unix manual page\ntimes(2) or the corresponding Windows Platform API documentation.\nOn Windows, only the first two items are filled, the others are zero.

\n

Availability: Unix, Windows

\n
\n\n
\n
\nos.wait()
\n

Wait for completion of a child process, and return a tuple containing its pid\nand exit status indication: a 16-bit number, whose low byte is the signal number\nthat killed the process, and whose high byte is the exit status (if the signal\nnumber is zero); the high bit of the low byte is set if a core file was\nproduced.

\n

Availability: Unix.

\n
\n\n
\n
\nos.waitpid(pid, options)
\n

The details of this function differ on Unix and Windows.

\n

On Unix: Wait for completion of a child process given by process id pid, and\nreturn a tuple containing its process id and exit status indication (encoded as\nfor wait()). The semantics of the call are affected by the value of the\ninteger options, which should be 0 for normal operation.

\n

If pid is greater than 0, waitpid() requests status information for\nthat specific process. If pid is 0, the request is for the status of any\nchild in the process group of the current process. If pid is -1, the\nrequest pertains to any child of the current process. If pid is less than\n-1, status is requested for any process in the process group -pid (the\nabsolute value of pid).

\n

An OSError is raised with the value of errno when the syscall\nreturns -1.

\n

On Windows: Wait for completion of a process given by process handle pid, and\nreturn a tuple containing pid, and its exit status shifted left by 8 bits\n(shifting makes cross-platform use of the function easier). A pid less than or\nequal to 0 has no special meaning on Windows, and raises an exception. The\nvalue of integer options has no effect. pid can refer to any process whose\nid is known, not necessarily a child process. The spawn() functions called\nwith P_NOWAIT return suitable process handles.

\n
\n\n
\n
\nos.wait3([options])
\n

Similar to waitpid(), except no process id argument is given and a\n3-element tuple containing the child’s process id, exit status indication, and\nresource usage information is returned. Refer to resource.getrusage() for details on resource usage information. The option\nargument is the same as that provided to waitpid() and wait4().

\n

Availability: Unix.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nos.wait4(pid, options)
\n

Similar to waitpid(), except a 3-element tuple, containing the child’s\nprocess id, exit status indication, and resource usage information is returned.\nRefer to resource.getrusage() for details on resource usage\ninformation. The arguments to wait4() are the same as those provided to\nwaitpid().

\n

Availability: Unix.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nos.WNOHANG
\n

The option for waitpid() to return immediately if no child process status\nis available immediately. The function returns (0, 0) in this case.

\n

Availability: Unix.

\n
\n\n
\n
\nos.WCONTINUED
\n

This option causes child processes to be reported if they have been continued\nfrom a job control stop since their status was last reported.

\n

Availability: Some Unix systems.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.WUNTRACED
\n

This option causes child processes to be reported if they have been stopped but\ntheir current state has not been reported since they were stopped.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n

The following functions take a process status code as returned by\nsystem(), wait(), or waitpid() as a parameter. They may be\nused to determine the disposition of a process.

\n
\n
\nos.WCOREDUMP(status)
\n

Return True if a core dump was generated for the process, otherwise\nreturn False.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.WIFCONTINUED(status)
\n

Return True if the process has been continued from a job control stop,\notherwise return False.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.WIFSTOPPED(status)
\n

Return True if the process has been stopped, otherwise return\nFalse.

\n

Availability: Unix.

\n
\n\n
\n
\nos.WIFSIGNALED(status)
\n

Return True if the process exited due to a signal, otherwise return\nFalse.

\n

Availability: Unix.

\n
\n\n
\n
\nos.WIFEXITED(status)
\n

Return True if the process exited using the exit(2) system call,\notherwise return False.

\n

Availability: Unix.

\n
\n\n
\n
\nos.WEXITSTATUS(status)
\n

If WIFEXITED(status) is true, return the integer parameter to the\nexit(2) system call. Otherwise, the return value is meaningless.

\n

Availability: Unix.

\n
\n\n
\n
\nos.WSTOPSIG(status)
\n

Return the signal which caused the process to stop.

\n

Availability: Unix.

\n
\n\n
\n
\nos.WTERMSIG(status)
\n

Return the signal which caused the process to exit.

\n

Availability: Unix.

\n
\n\n
\n
\n

15.1.6. Miscellaneous System Information

\n
\n
\nos.confstr(name)
\n

Return string-valued system configuration values. name specifies the\nconfiguration value to retrieve; it may be a string which is the name of a\ndefined system value; these names are specified in a number of standards (POSIX,\nUnix 95, Unix 98, and others). Some platforms define additional names as well.\nThe names known to the host operating system are given as the keys of the\nconfstr_names dictionary. For configuration variables not included in that\nmapping, passing an integer for name is also accepted.

\n

If the configuration value specified by name isn’t defined, None is\nreturned.

\n

If name is a string and is not known, ValueError is raised. If a\nspecific value for name is not supported by the host system, even if it is\nincluded in confstr_names, an OSError is raised with\nerrno.EINVAL for the error number.

\n

Availability: Unix

\n
\n\n
\n
\nos.confstr_names
\n

Dictionary mapping names accepted by confstr() to the integer values\ndefined for those names by the host operating system. This can be used to\ndetermine the set of names known to the system.

\n

Availability: Unix.

\n
\n\n
\n
\nos.getloadavg()
\n

Return the number of processes in the system run queue averaged over the last\n1, 5, and 15 minutes or raises OSError if the load average was\nunobtainable.

\n

Availability: Unix.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nos.sysconf(name)
\n

Return integer-valued system configuration values. If the configuration value\nspecified by name isn’t defined, -1 is returned. The comments regarding\nthe name parameter for confstr() apply here as well; the dictionary that\nprovides information on the known names is given by sysconf_names.

\n

Availability: Unix.

\n
\n\n
\n
\nos.sysconf_names
\n

Dictionary mapping names accepted by sysconf() to the integer values\ndefined for those names by the host operating system. This can be used to\ndetermine the set of names known to the system.

\n

Availability: Unix.

\n
\n\n

The following data values are used to support path manipulation operations. These\nare defined for all platforms.

\n

Higher-level operations on pathnames are defined in the os.path module.

\n
\n
\nos.curdir
\n
The constant string used by the operating system to refer to the current\ndirectory. This is '.' for Windows and POSIX. Also available via\nos.path.
\n\n
\n
\nos.pardir
\n
The constant string used by the operating system to refer to the parent\ndirectory. This is '..' for Windows and POSIX. Also available via\nos.path.
\n\n
\n
\nos.sep
\n
The character used by the operating system to separate pathname components.\nThis is '/' for POSIX and '\\\\' for Windows. Note that knowing this\nis not sufficient to be able to parse or concatenate pathnames — use\nos.path.split() and os.path.join() — but it is occasionally\nuseful. Also available via os.path.
\n\n
\n
\nos.altsep
\n
An alternative character used by the operating system to separate pathname\ncomponents, or None if only one separator character exists. This is set to\n'/' on Windows systems where sep is a backslash. Also available via\nos.path.
\n\n
\n
\nos.extsep
\n

The character which separates the base filename from the extension; for example,\nthe '.' in os.py. Also available via os.path.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nos.pathsep
\n
The character conventionally used by the operating system to separate search\npath components (as in PATH), such as ':' for POSIX or ';' for\nWindows. Also available via os.path.
\n\n
\n
\nos.defpath
\n
The default search path used by exec*p*() and spawn*p*() if the\nenvironment doesn’t have a 'PATH' key. Also available via os.path.
\n\n
\n
\nos.linesep
\n
The string used to separate (or, rather, terminate) lines on the current\nplatform. This may be a single character, such as '\\n' for POSIX, or\nmultiple characters, for example, '\\r\\n' for Windows. Do not use\nos.linesep as a line terminator when writing files opened in text mode (the\ndefault); use a single '\\n' instead, on all platforms.
\n\n
\n
\nos.devnull
\n

The file path of the null device. For example: '/dev/null' for\nPOSIX, 'nul' for Windows. Also available via os.path.

\n

\nNew in version 2.4.

\n
\n\n
\n
\n

15.1.7. Miscellaneous Functions

\n
\n
\nos.urandom(n)
\n

Return a string of n random bytes suitable for cryptographic use.

\n

This function returns random bytes from an OS-specific randomness source. The\nreturned data should be unpredictable enough for cryptographic applications,\nthough its exact quality depends on the OS implementation. On a UNIX-like\nsystem this will query /dev/urandom, and on Windows it will use CryptGenRandom.\nIf a randomness source is not found, NotImplementedError will be raised.

\n

\nNew in version 2.4.

\n
\n\n
\n
", "searchableItems": [ { "name": "os._exit", "domId": "os_os._exit" }, { "name": "os.abort", "domId": "os_os.abort" }, { "name": "os.access", "domId": "os_os.access" }, { "name": "os.chdir", "domId": "os_os.chdir" }, { "name": "os.chflags", "domId": "os_os.chflags" }, { "name": "os.chmod", "domId": "os_os.chmod" }, { "name": "os.chown", "domId": "os_os.chown" }, { "name": "os.chroot", "domId": "os_os.chroot" }, { "name": "os.close", "domId": "os_os.close" }, { "name": "os.closerange", "domId": "os_os.closerange" }, { "name": "os.confstr", "domId": "os_os.confstr" }, { "name": "os.ctermid", "domId": "os_os.ctermid" }, { "name": "os.dup", "domId": "os_os.dup" }, { "name": "os.dup2", "domId": "os_os.dup2" }, { "name": "os.execl", "domId": "os_os.execl" }, { "name": "os.fchdir", "domId": "os_os.fchdir" }, { "name": "os.fchmod", "domId": "os_os.fchmod" }, { "name": "os.fchown", "domId": "os_os.fchown" }, { "name": "os.fdatasync", "domId": "os_os.fdatasync" }, { "name": "os.fdopen", "domId": "os_os.fdopen" }, { "name": "os.fork", "domId": "os_os.fork" }, { "name": "os.forkpty", "domId": "os_os.forkpty" }, { "name": "os.fpathconf", "domId": "os_os.fpathconf" }, { "name": "os.fstat", "domId": "os_os.fstat" }, { "name": "os.fstatvfs", "domId": "os_os.fstatvfs" }, { "name": "os.fsync", "domId": "os_os.fsync" }, { "name": "os.ftruncate", "domId": "os_os.ftruncate" }, { "name": "os.getcwd", "domId": "os_os.getcwd" }, { "name": "os.getcwdu", "domId": "os_os.getcwdu" }, { "name": "os.getegid", "domId": "os_os.getegid" }, { "name": "os.getenv", "domId": "os_os.getenv" }, { "name": "os.geteuid", "domId": "os_os.geteuid" }, { "name": "os.getgid", "domId": "os_os.getgid" }, { "name": "os.getgroups", "domId": "os_os.getgroups" }, { "name": "os.getloadavg", "domId": "os_os.getloadavg" }, { "name": "os.getlogin", "domId": "os_os.getlogin" }, { "name": "os.getpgid", "domId": "os_os.getpgid" }, { "name": "os.getpgrp", "domId": "os_os.getpgrp" }, { "name": "os.getpid", "domId": "os_os.getpid" }, { "name": "os.getppid", "domId": "os_os.getppid" }, { "name": "os.getresgid", "domId": "os_os.getresgid" }, { "name": "os.getresuid", "domId": "os_os.getresuid" }, { "name": "os.getsid", "domId": "os_os.getsid" }, { "name": "os.getuid", "domId": "os_os.getuid" }, { "name": "os.initgroups", "domId": "os_os.initgroups" }, { "name": "os.isatty", "domId": "os_os.isatty" }, { "name": "os.kill", "domId": "os_os.kill" }, { "name": "os.killpg", "domId": "os_os.killpg" }, { "name": "os.lchflags", "domId": "os_os.lchflags" }, { "name": "os.lchmod", "domId": "os_os.lchmod" }, { "name": "os.lchown", "domId": "os_os.lchown" }, { "name": "os.link", "domId": "os_os.link" }, { "name": "os.listdir", "domId": "os_os.listdir" }, { "name": "os.lseek", "domId": "os_os.lseek" }, { "name": "os.lstat", "domId": "os_os.lstat" }, { "name": "os.major", "domId": "os_os.major" }, { "name": "os.makedev", "domId": "os_os.makedev" }, { "name": "os.makedirs", "domId": "os_os.makedirs" }, { "name": "os.minor", "domId": "os_os.minor" }, { "name": "os.mkdir", "domId": "os_os.mkdir" }, { "name": "os.mkfifo", "domId": "os_os.mkfifo" }, { "name": "os.mknod", "domId": "os_os.mknod" }, { "name": "os.nice", "domId": "os_os.nice" }, { "name": "os.open", "domId": "os_os.open" }, { "name": "os.openpty", "domId": "os_os.openpty" }, { "name": "os.pathconf", "domId": "os_os.pathconf" }, { "name": "os.pipe", "domId": "os_os.pipe" }, { "name": "os.plock", "domId": "os_os.plock" }, { "name": "os.popen", "domId": "os_os.popen" }, { "name": "os.popen2", "domId": "os_os.popen2" }, { "name": "os.popen3", "domId": "os_os.popen3" }, { "name": "os.popen4", "domId": "os_os.popen4" }, { "name": "os.putenv", "domId": "os_os.putenv" }, { "name": "os.read", "domId": "os_os.read" }, { "name": "os.readlink", "domId": "os_os.readlink" }, { "name": "os.remove", "domId": "os_os.remove" }, { "name": "os.removedirs", "domId": "os_os.removedirs" }, { "name": "os.rename", "domId": "os_os.rename" }, { "name": "os.renames", "domId": "os_os.renames" }, { "name": "os.rmdir", "domId": "os_os.rmdir" }, { "name": "os.setegid", "domId": "os_os.setegid" }, { "name": "os.seteuid", "domId": "os_os.seteuid" }, { "name": "os.setgid", "domId": "os_os.setgid" }, { "name": "os.setgroups", "domId": "os_os.setgroups" }, { "name": "os.setpgid", "domId": "os_os.setpgid" }, { "name": "os.setpgrp", "domId": "os_os.setpgrp" }, { "name": "os.setregid", "domId": "os_os.setregid" }, { "name": "os.setresgid", "domId": "os_os.setresgid" }, { "name": "os.setresuid", "domId": "os_os.setresuid" }, { "name": "os.setreuid", "domId": "os_os.setreuid" }, { "name": "os.setsid", "domId": "os_os.setsid" }, { "name": "os.setuid", "domId": "os_os.setuid" }, { "name": "os.spawnl", "domId": "os_os.spawnl" }, { "name": "os.startfile", "domId": "os_os.startfile" }, { "name": "os.stat", "domId": "os_os.stat" }, { "name": "os.stat_float_times", "domId": "os_os.stat_float_times" }, { "name": "os.statvfs", "domId": "os_os.statvfs" }, { "name": "os.strerror", "domId": "os_os.strerror" }, { "name": "os.symlink", "domId": "os_os.symlink" }, { "name": "os.sysconf", "domId": "os_os.sysconf" }, { "name": "os.system", "domId": "os_os.system" }, { "name": "os.tcgetpgrp", "domId": "os_os.tcgetpgrp" }, { "name": "os.tcsetpgrp", "domId": "os_os.tcsetpgrp" }, { "name": "os.tempnam", "domId": "os_os.tempnam" }, { "name": "os.times", "domId": "os_os.times" }, { "name": "os.tmpfile", "domId": "os_os.tmpfile" }, { "name": "os.tmpnam", "domId": "os_os.tmpnam" }, { "name": "os.ttyname", "domId": "os_os.ttyname" }, { "name": "os.umask", "domId": "os_os.umask" }, { "name": "os.uname", "domId": "os_os.uname" }, { "name": "os.unlink", "domId": "os_os.unlink" }, { "name": "os.unsetenv", "domId": "os_os.unsetenv" }, { "name": "os.urandom", "domId": "os_os.urandom" }, { "name": "os.utime", "domId": "os_os.utime" }, { "name": "os.wait", "domId": "os_os.wait" }, { "name": "os.wait3", "domId": "os_os.wait3" }, { "name": "os.wait4", "domId": "os_os.wait4" }, { "name": "os.waitpid", "domId": "os_os.waitpid" }, { "name": "os.walk", "domId": "os_os.walk" }, { "name": "os.WCOREDUMP", "domId": "os_os.WCOREDUMP" }, { "name": "os.WEXITSTATUS", "domId": "os_os.WEXITSTATUS" }, { "name": "os.WIFCONTINUED", "domId": "os_os.WIFCONTINUED" }, { "name": "os.WIFEXITED", "domId": "os_os.WIFEXITED" }, { "name": "os.WIFSIGNALED", "domId": "os_os.WIFSIGNALED" }, { "name": "os.WIFSTOPPED", "domId": "os_os.WIFSTOPPED" }, { "name": "os.write", "domId": "os_os.write" }, { "name": "os.WSTOPSIG", "domId": "os_os.WSTOPSIG" }, { "name": "os.WTERMSIG", "domId": "os_os.WTERMSIG" } ] }, { "url": "http://docs.python.org/library/getopt.html", "title": "getopt", "html": "
\n

15.6. getopt — C-style parser for command line options

\n

Source code: Lib/getopt.py

\n
\n
\n

Note

\n

The getopt module is a parser for command line options whose API is\ndesigned to be familiar to users of the C getopt() function. Users who\nare unfamiliar with the C getopt() function or who would like to write\nless code and get better help and error messages should consider using the\nargparse module instead.

\n
\n

This module helps scripts to parse the command line arguments in sys.argv.\nIt supports the same conventions as the Unix getopt() function (including\nthe special meanings of arguments of the form ‘-‘ and ‘--‘). Long\noptions similar to those supported by GNU software may be used as well via an\noptional third argument.

\n

This module provides two functions and an\nexception:

\n
\n
\ngetopt.getopt(args, options[, long_options])
\n

Parses command line options and parameter list. args is the argument list to\nbe parsed, without the leading reference to the running program. Typically, this\nmeans sys.argv[1:]. options is the string of option letters that the\nscript wants to recognize, with options that require an argument followed by a\ncolon (':'; i.e., the same format that Unix getopt() uses).

\n
\n

Note

\n

Unlike GNU getopt(), after a non-option argument, all further\narguments are considered also non-options. This is similar to the way\nnon-GNU Unix systems work.

\n
\n

long_options, if specified, must be a list of strings with the names of the\nlong options which should be supported. The leading '--'\ncharacters should not be included in the option name. Long options which\nrequire an argument should be followed by an equal sign ('='). Optional\narguments are not supported. To accept only long options, options should\nbe an empty string. Long options on the command line can be recognized so\nlong as they provide a prefix of the option name that matches exactly one of\nthe accepted options. For example, if long_options is ['foo', 'frob'],\nthe option --fo will match as --foo, but --f\nwill not match uniquely, so GetoptError will be raised.

\n

The return value consists of two elements: the first is a list of (option,\nvalue) pairs; the second is the list of program arguments left after the\noption list was stripped (this is a trailing slice of args). Each\noption-and-value pair returned has the option as its first element, prefixed\nwith a hyphen for short options (e.g., '-x') or two hyphens for long\noptions (e.g., '--long-option'), and the option argument as its\nsecond element, or an empty string if the option has no argument. The\noptions occur in the list in the same order in which they were found, thus\nallowing multiple occurrences. Long and short options may be mixed.

\n
\n\n
\n
\ngetopt.gnu_getopt(args, options[, long_options])
\n

This function works like getopt(), except that GNU style scanning mode is\nused by default. This means that option and non-option arguments may be\nintermixed. The getopt() function stops processing options as soon as a\nnon-option argument is encountered.

\n

If the first character of the option string is '+', or if the environment\nvariable POSIXLY_CORRECT is set, then option processing stops as\nsoon as a non-option argument is encountered.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception getopt.GetoptError
\n

This is raised when an unrecognized option is found in the argument list or when\nan option requiring an argument is given none. The argument to the exception is\na string indicating the cause of the error. For long options, an argument given\nto an option which does not require one will also cause this exception to be\nraised. The attributes msg and opt give the error message and\nrelated option; if there is no specific option to which the exception relates,\nopt is an empty string.

\n

\nChanged in version 1.6: Introduced GetoptError as a synonym for error.

\n
\n\n
\n
\nexception getopt.error
\n
Alias for GetoptError; for backward compatibility.
\n\n

An example using only Unix style options:

\n
>>> import getopt\n>>> args = '-a -b -cfoo -d bar a1 a2'.split()\n>>> args\n['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']\n>>> optlist, args = getopt.getopt(args, 'abc:d:')\n>>> optlist\n[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]\n>>> args\n['a1', 'a2']\n
\n
\n

Using long option names is equally easy:

\n
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'\n>>> args = s.split()\n>>> args\n['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']\n>>> optlist, args = getopt.getopt(args, 'x', [\n...     'condition=', 'output-file=', 'testing'])\n>>> optlist\n[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]\n>>> args\n['a1', 'a2']\n
\n
\n

In a script, typical usage is something like this:

\n
import getopt, sys\n\ndef main():\n    try:\n        opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])\n    except getopt.GetoptError, err:\n        # print help information and exit:\n        print str(err) # will print something like "option -a not recognized"\n        usage()\n        sys.exit(2)\n    output = None\n    verbose = False\n    for o, a in opts:\n        if o == "-v":\n            verbose = True\n        elif o in ("-h", "--help"):\n            usage()\n            sys.exit()\n        elif o in ("-o", "--output"):\n            output = a\n        else:\n            assert False, "unhandled option"\n    # ...\n\nif __name__ == "__main__":\n    main()\n
\n
\n

Note that an equivalent command line interface could be produced with less code\nand more informative help and error messages by using the argparse module:

\n
import argparse\n\nif __name__ == '__main__':\n    parser = argparse.ArgumentParser()\n    parser.add_argument('-o', '--output')\n    parser.add_argument('-v', dest='verbose', action='store_true')\n    args = parser.parse_args()\n    # ... do something with args.output ...\n    # ... do something with args.verbose ..\n
\n
\n
\n

See also

\n
\n
Module argparse
\n
Alternative command line option and argument parsing library.
\n
\n
\n
", "searchableItems": [ { "name": "getopt.getopt", "domId": "getopt_getopt.getopt" }, { "name": "getopt.gnu_getopt", "domId": "getopt_getopt.gnu_getopt" } ] }, { "url": "http://docs.python.org/library/optparse.html", "title": "optparse", "html": "
\n

15.5. optparse — Parser for command line options

\n

\nNew in version 2.3.

\n

\nDeprecated since version 2.7: The optparse module is deprecated and will not be developed further;\ndevelopment will continue with the argparse module.

\n

Source code: Lib/optparse.py

\n
\n

optparse is a more convenient, flexible, and powerful library for parsing\ncommand-line options than the old getopt module. optparse uses a\nmore declarative style of command-line parsing: you create an instance of\nOptionParser, populate it with options, and parse the command\nline. optparse allows users to specify options in the conventional\nGNU/POSIX syntax, and additionally generates usage and help messages for you.

\n

Here’s an example of using optparse in a simple script:

\n
from optparse import OptionParser\n[...]\nparser = OptionParser()\nparser.add_option("-f", "--file", dest="filename",\n                  help="write report to FILE", metavar="FILE")\nparser.add_option("-q", "--quiet",\n                  action="store_false", dest="verbose", default=True,\n                  help="don't print status messages to stdout")\n\n(options, args) = parser.parse_args()\n
\n
\n

With these few lines of code, users of your script can now do the “usual thing”\non the command-line, for example:

\n
<yourscript> --file=outfile -q
\n
\n

As it parses the command line, optparse sets attributes of the\noptions object returned by parse_args() based on user-supplied\ncommand-line values. When parse_args() returns from parsing this command\nline, options.filename will be "outfile" and options.verbose will be\nFalse. optparse supports both long and short options, allows short\noptions to be merged together, and allows options to be associated with their\narguments in a variety of ways. Thus, the following command lines are all\nequivalent to the above example:

\n
<yourscript> -f outfile --quiet\n<yourscript> --quiet --file outfile\n<yourscript> -q -foutfile\n<yourscript> -qfoutfile
\n
\n

Additionally, users can run one of

\n
<yourscript> -h\n<yourscript> --help
\n
\n

and optparse will print out a brief summary of your script’s options:

\n
Usage: <yourscript> [options]\n\nOptions:\n  -h, --help            show this help message and exit\n  -f FILE, --file=FILE  write report to FILE\n  -q, --quiet           don't print status messages to stdout\n
\n
\n

where the value of yourscript is determined at runtime (normally from\nsys.argv[0]).

\n
\n

15.5.1. Background

\n

optparse was explicitly designed to encourage the creation of programs\nwith straightforward, conventional command-line interfaces. To that end, it\nsupports only the most common command-line syntax and semantics conventionally\nused under Unix. If you are unfamiliar with these conventions, read this\nsection to acquaint yourself with them.

\n
\n

15.5.1.1. Terminology

\n
\n
argument
\n

a string entered on the command-line, and passed by the shell to execl()\nor execv(). In Python, arguments are elements of sys.argv[1:]\n(sys.argv[0] is the name of the program being executed). Unix shells\nalso use the term “word”.

\n

It is occasionally desirable to substitute an argument list other than\nsys.argv[1:], so you should read “argument” as “an element of\nsys.argv[1:], or of some other list provided as a substitute for\nsys.argv[1:]“.

\n
\n
option
\n

an argument used to supply extra information to guide or customize the\nexecution of a program. There are many different syntaxes for options; the\ntraditional Unix syntax is a hyphen (“-“) followed by a single letter,\ne.g. -x or -F. Also, traditional Unix syntax allows multiple\noptions to be merged into a single argument, e.g. -x -F is equivalent\nto -xF. The GNU project introduced -- followed by a series of\nhyphen-separated words, e.g. --file or --dry-run. These are the\nonly two option syntaxes provided by optparse.

\n

Some other option syntaxes that the world has seen include:

\n
    \n
  • a hyphen followed by a few letters, e.g. -pf (this is not the same\nas multiple options merged into a single argument)
  • \n
  • a hyphen followed by a whole word, e.g. -file (this is technically\nequivalent to the previous syntax, but they aren’t usually seen in the same\nprogram)
  • \n
  • a plus sign followed by a single letter, or a few letters, or a word, e.g.\n+f, +rgb
  • \n
  • a slash followed by a letter, or a few letters, or a word, e.g. /f,\n/file
  • \n
\n

These option syntaxes are not supported by optparse, and they never\nwill be. This is deliberate: the first three are non-standard on any\nenvironment, and the last only makes sense if you’re exclusively targeting\nVMS, MS-DOS, and/or Windows.

\n
\n
option argument
\n

an argument that follows an option, is closely associated with that option,\nand is consumed from the argument list when that option is. With\noptparse, option arguments may either be in a separate argument from\ntheir option:

\n
-f foo\n--file foo\n
\n
\n

or included in the same argument:

\n
-ffoo\n--file=foo\n
\n
\n

Typically, a given option either takes an argument or it doesn’t. Lots of\npeople want an “optional option arguments” feature, meaning that some options\nwill take an argument if they see it, and won’t if they don’t. This is\nsomewhat controversial, because it makes parsing ambiguous: if -a takes\nan optional argument and -b is another option entirely, how do we\ninterpret -ab? Because of this ambiguity, optparse does not\nsupport this feature.

\n
\n
positional argument
\n
something leftover in the argument list after options have been parsed, i.e.\nafter options and their arguments have been parsed and removed from the\nargument list.
\n
required option
\n
an option that must be supplied on the command-line; note that the phrase\n“required option” is self-contradictory in English. optparse doesn’t\nprevent you from implementing required options, but doesn’t give you much\nhelp at it either.
\n
\n

For example, consider this hypothetical command-line:

\n
prog -v --report /tmp/report.txt foo bar
\n
\n

-v and --report are both options. Assuming that --report\ntakes one argument, /tmp/report.txt is an option argument. foo and\nbar are positional arguments.

\n
\n
\n

15.5.1.2. What are options for?

\n

Options are used to provide extra information to tune or customize the execution\nof a program. In case it wasn’t clear, options are usually optional. A\nprogram should be able to run just fine with no options whatsoever. (Pick a\nrandom program from the Unix or GNU toolsets. Can it run without any options at\nall and still make sense? The main exceptions are find, tar, and\ndd—all of which are mutant oddballs that have been rightly criticized\nfor their non-standard syntax and confusing interfaces.)

\n

Lots of people want their programs to have “required options”. Think about it.\nIf it’s required, then it’s not optional! If there is a piece of information\nthat your program absolutely requires in order to run successfully, that’s what\npositional arguments are for.

\n

As an example of good command-line interface design, consider the humble cp\nutility, for copying files. It doesn’t make much sense to try to copy files\nwithout supplying a destination and at least one source. Hence, cp fails if\nyou run it with no arguments. However, it has a flexible, useful syntax that\ndoes not require any options at all:

\n
cp SOURCE DEST\ncp SOURCE ... DEST-DIR
\n
\n

You can get pretty far with just that. Most cp implementations provide a\nbunch of options to tweak exactly how the files are copied: you can preserve\nmode and modification time, avoid following symlinks, ask before clobbering\nexisting files, etc. But none of this distracts from the core mission of\ncp, which is to copy either one file to another, or several files to another\ndirectory.

\n
\n
\n

15.5.1.3. What are positional arguments for?

\n

Positional arguments are for those pieces of information that your program\nabsolutely, positively requires to run.

\n

A good user interface should have as few absolute requirements as possible. If\nyour program requires 17 distinct pieces of information in order to run\nsuccessfully, it doesn’t much matter how you get that information from the\nuser—most people will give up and walk away before they successfully run the\nprogram. This applies whether the user interface is a command-line, a\nconfiguration file, or a GUI: if you make that many demands on your users, most\nof them will simply give up.

\n

In short, try to minimize the amount of information that users are absolutely\nrequired to supply—use sensible defaults whenever possible. Of course, you\nalso want to make your programs reasonably flexible. That’s what options are\nfor. Again, it doesn’t matter if they are entries in a config file, widgets in\nthe “Preferences” dialog of a GUI, or command-line options—the more options\nyou implement, the more flexible your program is, and the more complicated its\nimplementation becomes. Too much flexibility has drawbacks as well, of course;\ntoo many options can overwhelm users and make your code much harder to maintain.

\n
\n
\n
\n

15.5.2. Tutorial

\n

While optparse is quite flexible and powerful, it’s also straightforward\nto use in most cases. This section covers the code patterns that are common to\nany optparse-based program.

\n

First, you need to import the OptionParser class; then, early in the main\nprogram, create an OptionParser instance:

\n
from optparse import OptionParser\n[...]\nparser = OptionParser()\n
\n
\n

Then you can start defining options. The basic syntax is:

\n
parser.add_option(opt_str, ...,\n                  attr=value, ...)\n
\n
\n

Each option has one or more option strings, such as -f or --file,\nand several option attributes that tell optparse what to expect and what\nto do when it encounters that option on the command line.

\n

Typically, each option will have one short option string and one long option\nstring, e.g.:

\n
parser.add_option("-f", "--file", ...)\n
\n
\n

You’re free to define as many short option strings and as many long option\nstrings as you like (including zero), as long as there is at least one option\nstring overall.

\n

The option strings passed to add_option() are effectively labels for the\noption defined by that call. For brevity, we will frequently refer to\nencountering an option on the command line; in reality, optparse\nencounters option strings and looks up options from them.

\n

Once all of your options are defined, instruct optparse to parse your\nprogram’s command line:

\n
(options, args) = parser.parse_args()\n
\n
\n

(If you like, you can pass a custom argument list to parse_args(), but\nthat’s rarely necessary: by default it uses sys.argv[1:].)

\n

parse_args() returns two values:

\n\n

This tutorial section only covers the four most important option attributes:\naction, type, dest\n(destination), and help. Of these, action is the\nmost fundamental.

\n
\n

15.5.2.1. Understanding option actions

\n

Actions tell optparse what to do when it encounters an option on the\ncommand line. There is a fixed set of actions hard-coded into optparse;\nadding new actions is an advanced topic covered in section\nExtending optparse. Most actions tell optparse to store\na value in some variable—for example, take a string from the command line and\nstore it in an attribute of options.

\n

If you don’t specify an option action, optparse defaults to store.

\n
\n
\n

15.5.2.2. The store action

\n

The most common option action is store, which tells optparse to take\nthe next argument (or the remainder of the current argument), ensure that it is\nof the correct type, and store it to your chosen destination.

\n

For example:

\n
parser.add_option("-f", "--file",\n                  action="store", type="string", dest="filename")\n
\n
\n

Now let’s make up a fake command line and ask optparse to parse it:

\n
args = ["-f", "foo.txt"]\n(options, args) = parser.parse_args(args)\n
\n
\n

When optparse sees the option string -f, it consumes the next\nargument, foo.txt, and stores it in options.filename. So, after this\ncall to parse_args(), options.filename is "foo.txt".

\n

Some other option types supported by optparse are int and float.\nHere’s an option that expects an integer argument:

\n
parser.add_option("-n", type="int", dest="num")\n
\n
\n

Note that this option has no long option string, which is perfectly acceptable.\nAlso, there’s no explicit action, since the default is store.

\n

Let’s parse another fake command-line. This time, we’ll jam the option argument\nright up against the option: since -n42 (one argument) is equivalent to\n-n 42 (two arguments), the code

\n
(options, args) = parser.parse_args(["-n42"])\nprint options.num\n
\n
\n

will print 42.

\n

If you don’t specify a type, optparse assumes string. Combined with\nthe fact that the default action is store, that means our first example can\nbe a lot shorter:

\n
parser.add_option("-f", "--file", dest="filename")\n
\n
\n

If you don’t supply a destination, optparse figures out a sensible\ndefault from the option strings: if the first long option string is\n--foo-bar, then the default destination is foo_bar. If there are no\nlong option strings, optparse looks at the first short option string: the\ndefault destination for -f is f.

\n

optparse also includes built-in long and complex types. Adding\ntypes is covered in section Extending optparse.

\n
\n
\n

15.5.2.3. Handling boolean (flag) options

\n

Flag options—set a variable to true or false when a particular option is seen\n—are quite common. optparse supports them with two separate actions,\nstore_true and store_false. For example, you might have a verbose\nflag that is turned on with -v and off with -q:

\n
parser.add_option("-v", action="store_true", dest="verbose")\nparser.add_option("-q", action="store_false", dest="verbose")\n
\n
\n

Here we have two different options with the same destination, which is perfectly\nOK. (It just means you have to be a bit careful when setting default values—\nsee below.)

\n

When optparse encounters -v on the command line, it sets\noptions.verbose to True; when it encounters -q,\noptions.verbose is set to False.

\n
\n
\n

15.5.2.4. Other actions

\n

Some other actions supported by optparse are:

\n
\n
"store_const"
\n
store a constant value
\n
"append"
\n
append this option’s argument to a list
\n
"count"
\n
increment a counter by one
\n
"callback"
\n
call a specified function
\n
\n

These are covered in section Reference Guide, Reference Guide\nand section Option Callbacks.

\n
\n
\n

15.5.2.5. Default values

\n

All of the above examples involve setting some variable (the “destination”) when\ncertain command-line options are seen. What happens if those options are never\nseen? Since we didn’t supply any defaults, they are all set to None. This\nis usually fine, but sometimes you want more control. optparse lets you\nsupply a default value for each destination, which is assigned before the\ncommand line is parsed.

\n

First, consider the verbose/quiet example. If we want optparse to set\nverbose to True unless -q is seen, then we can do this:

\n
parser.add_option("-v", action="store_true", dest="verbose", default=True)\nparser.add_option("-q", action="store_false", dest="verbose")\n
\n
\n

Since default values apply to the destination rather than to any particular\noption, and these two options happen to have the same destination, this is\nexactly equivalent:

\n
parser.add_option("-v", action="store_true", dest="verbose")\nparser.add_option("-q", action="store_false", dest="verbose", default=True)\n
\n
\n

Consider this:

\n
parser.add_option("-v", action="store_true", dest="verbose", default=False)\nparser.add_option("-q", action="store_false", dest="verbose", default=True)\n
\n
\n

Again, the default value for verbose will be True: the last default\nvalue supplied for any particular destination is the one that counts.

\n

A clearer way to specify default values is the set_defaults() method of\nOptionParser, which you can call at any time before calling parse_args():

\n
parser.set_defaults(verbose=True)\nparser.add_option(...)\n(options, args) = parser.parse_args()\n
\n
\n

As before, the last value specified for a given option destination is the one\nthat counts. For clarity, try to use one method or the other of setting default\nvalues, not both.

\n
\n
\n

15.5.2.6. Generating help

\n

optparse‘s ability to generate help and usage text automatically is\nuseful for creating user-friendly command-line interfaces. All you have to do\nis supply a help value for each option, and optionally a short\nusage message for your whole program. Here’s an OptionParser populated with\nuser-friendly (documented) options:

\n
usage = "usage: %prog [options] arg1 arg2"\nparser = OptionParser(usage=usage)\nparser.add_option("-v", "--verbose",\n                  action="store_true", dest="verbose", default=True,\n                  help="make lots of noise [default]")\nparser.add_option("-q", "--quiet",\n                  action="store_false", dest="verbose",\n                  help="be vewwy quiet (I'm hunting wabbits)")\nparser.add_option("-f", "--filename",\n                  metavar="FILE", help="write output to FILE")\nparser.add_option("-m", "--mode",\n                  default="intermediate",\n                  help="interaction mode: novice, intermediate, "\n                       "or expert [default: %default]")\n
\n
\n

If optparse encounters either -h or --help on the\ncommand-line, or if you just call parser.print_help(), it prints the\nfollowing to standard output:

\n
Usage: <yourscript> [options] arg1 arg2\n\nOptions:\n  -h, --help            show this help message and exit\n  -v, --verbose         make lots of noise [default]\n  -q, --quiet           be vewwy quiet (I'm hunting wabbits)\n  -f FILE, --filename=FILE\n                        write output to FILE\n  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or\n                        expert [default: intermediate]\n
\n
\n

(If the help output is triggered by a help option, optparse exits after\nprinting the help text.)

\n

There’s a lot going on here to help optparse generate the best possible\nhelp message:

\n
    \n
  • the script defines its own usage message:

    \n
    usage = "usage: %prog [options] arg1 arg2"\n
    \n
    \n

    optparse expands %prog in the usage string to the name of the\ncurrent program, i.e. os.path.basename(sys.argv[0]). The expanded string\nis then printed before the detailed option help.

    \n

    If you don’t supply a usage string, optparse uses a bland but sensible\ndefault: "Usage: %prog [options]", which is fine if your script doesn’t\ntake any positional arguments.

    \n
  • \n
  • every option defines a help string, and doesn’t worry about line-wrapping—\noptparse takes care of wrapping lines and making the help output look\ngood.

    \n
  • \n
  • options that take a value indicate this fact in their automatically-generated\nhelp message, e.g. for the “mode” option:

    \n
    -m MODE, --mode=MODE
    \n
    \n

    Here, “MODE” is called the meta-variable: it stands for the argument that the\nuser is expected to supply to -m/--mode. By default,\noptparse converts the destination variable name to uppercase and uses\nthat for the meta-variable. Sometimes, that’s not what you want—for\nexample, the --filename option explicitly sets metavar="FILE",\nresulting in this automatically-generated option description:

    \n
    -f FILE, --filename=FILE
    \n
    \n

    This is important for more than just saving space, though: the manually\nwritten help text uses the meta-variable FILE to clue the user in that\nthere’s a connection between the semi-formal syntax -f FILE and the informal\nsemantic description “write output to FILE”. This is a simple but effective\nway to make your help text a lot clearer and more useful for end users.

    \n
  • \n
\n

\nNew in version 2.4: Options that have a default value can include %default in the help\nstring—optparse will replace it with str() of the option’s\ndefault value. If an option has no default value (or the default value is\nNone), %default expands to none.

\n
\n

15.5.2.6.1. Grouping Options

\n

When dealing with many options, it is convenient to group these options for\nbetter help output. An OptionParser can contain several option groups,\neach of which can contain several options.

\n

An option group is obtained using the class OptionGroup:

\n
\n
\nclass optparse.OptionGroup(parser, title, description=None)
\n

where

\n
    \n
  • parser is the OptionParser instance the group will be insterted in\nto
  • \n
  • title is the group title
  • \n
  • description, optional, is a long description of the group
  • \n
\n
\n\n

OptionGroup inherits from OptionContainer (like\nOptionParser) and so the add_option() method can be used to add\nan option to the group.

\n

Once all the options are declared, using the OptionParser method\nadd_option_group() the group is added to the previously defined parser.

\n

Continuing with the parser defined in the previous section, adding an\nOptionGroup to a parser is easy:

\n
group = OptionGroup(parser, "Dangerous Options",\n                    "Caution: use these options at your own risk.  "\n                    "It is believed that some of them bite.")\ngroup.add_option("-g", action="store_true", help="Group option.")\nparser.add_option_group(group)\n
\n
\n

This would result in the following help output:

\n
Usage: <yourscript> [options] arg1 arg2\n\nOptions:\n  -h, --help            show this help message and exit\n  -v, --verbose         make lots of noise [default]\n  -q, --quiet           be vewwy quiet (I'm hunting wabbits)\n  -f FILE, --filename=FILE\n                        write output to FILE\n  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or\n                        expert [default: intermediate]\n\n  Dangerous Options:\n    Caution: use these options at your own risk.  It is believed that some\n    of them bite.\n\n    -g                  Group option.\n
\n
\n

A bit more complete example might involve using more than one group: still\nextending the previous example:

\n
group = OptionGroup(parser, "Dangerous Options",\n                    "Caution: use these options at your own risk.  "\n                    "It is believed that some of them bite.")\ngroup.add_option("-g", action="store_true", help="Group option.")\nparser.add_option_group(group)\n\ngroup = OptionGroup(parser, "Debug Options")\ngroup.add_option("-d", "--debug", action="store_true",\n                 help="Print debug information")\ngroup.add_option("-s", "--sql", action="store_true",\n                 help="Print all SQL statements executed")\ngroup.add_option("-e", action="store_true", help="Print every action done")\nparser.add_option_group(group)\n
\n
\n

that results in the following output:

\n
Usage: <yourscript> [options] arg1 arg2\n\nOptions:\n  -h, --help            show this help message and exit\n  -v, --verbose         make lots of noise [default]\n  -q, --quiet           be vewwy quiet (I'm hunting wabbits)\n  -f FILE, --filename=FILE\n                        write output to FILE\n  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or expert\n                        [default: intermediate]\n\n  Dangerous Options:\n    Caution: use these options at your own risk.  It is believed that some\n    of them bite.\n\n    -g                  Group option.\n\n  Debug Options:\n    -d, --debug         Print debug information\n    -s, --sql           Print all SQL statements executed\n    -e                  Print every action done\n
\n
\n

Another interesting method, in particular when working programmatically with\noption groups is:

\n
\n
\nOptionParser.get_option_group(opt_str)
\n
Return the OptionGroup to which the short or long option\nstring opt_str (e.g. '-o' or '--option') belongs. If\nthere’s no such OptionGroup, return None.
\n\n
\n
\n
\n

15.5.2.7. Printing a version string

\n

Similar to the brief usage string, optparse can also print a version\nstring for your program. You have to supply the string as the version\nargument to OptionParser:

\n
parser = OptionParser(usage="%prog [-f] [-q]", version="%prog 1.0")\n
\n
\n

%prog is expanded just like it is in usage. Apart from that,\nversion can contain anything you like. When you supply it, optparse\nautomatically adds a --version option to your parser. If it encounters\nthis option on the command line, it expands your version string (by\nreplacing %prog), prints it to stdout, and exits.

\n

For example, if your script is called /usr/bin/foo:

\n
$ /usr/bin/foo --version\nfoo 1.0
\n
\n

The following two methods can be used to print and get the version string:

\n
\n
\nOptionParser.print_version(file=None)
\n
Print the version message for the current program (self.version) to\nfile (default stdout). As with print_usage(), any occurrence\nof %prog in self.version is replaced with the name of the current\nprogram. Does nothing if self.version is empty or undefined.
\n\n
\n
\nOptionParser.get_version()
\n
Same as print_version() but returns the version string instead of\nprinting it.
\n\n
\n
\n

15.5.2.8. How optparse handles errors

\n

There are two broad classes of errors that optparse has to worry about:\nprogrammer errors and user errors. Programmer errors are usually erroneous\ncalls to OptionParser.add_option(), e.g. invalid option strings, unknown\noption attributes, missing option attributes, etc. These are dealt with in the\nusual way: raise an exception (either optparse.OptionError or\nTypeError) and let the program crash.

\n

Handling user errors is much more important, since they are guaranteed to happen\nno matter how stable your code is. optparse can automatically detect\nsome user errors, such as bad option arguments (passing -n 4x where\n-n takes an integer argument), missing arguments (-n at the end\nof the command line, where -n takes an argument of any type). Also,\nyou can call OptionParser.error() to signal an application-defined error\ncondition:

\n
(options, args) = parser.parse_args()\n[...]\nif options.a and options.b:\n    parser.error("options -a and -b are mutually exclusive")\n
\n
\n

In either case, optparse handles the error the same way: it prints the\nprogram’s usage message and an error message to standard error and exits with\nerror status 2.

\n

Consider the first example above, where the user passes 4x to an option\nthat takes an integer:

\n
$ /usr/bin/foo -n 4x\nUsage: foo [options]\n\nfoo: error: option -n: invalid integer value: '4x'
\n
\n

Or, where the user fails to pass a value at all:

\n
$ /usr/bin/foo -n\nUsage: foo [options]\n\nfoo: error: -n option requires an argument
\n
\n

optparse-generated error messages take care always to mention the\noption involved in the error; be sure to do the same when calling\nOptionParser.error() from your application code.

\n

If optparse‘s default error-handling behaviour does not suit your needs,\nyou’ll need to subclass OptionParser and override its exit()\nand/or error() methods.

\n
\n
\n

15.5.2.9. Putting it all together

\n

Here’s what optparse-based scripts usually look like:

\n
from optparse import OptionParser\n[...]\ndef main():\n    usage = "usage: %prog [options] arg"\n    parser = OptionParser(usage)\n    parser.add_option("-f", "--file", dest="filename",\n                      help="read data from FILENAME")\n    parser.add_option("-v", "--verbose",\n                      action="store_true", dest="verbose")\n    parser.add_option("-q", "--quiet",\n                      action="store_false", dest="verbose")\n    [...]\n    (options, args) = parser.parse_args()\n    if len(args) != 1:\n        parser.error("incorrect number of arguments")\n    if options.verbose:\n        print "reading %s..."  options.filename\n    [...]\n\nif __name__ == "__main__":\n    main()\n
\n
\n
\n
\n
\n

15.5.3. Reference Guide

\n
\n

15.5.3.1. Creating the parser

\n

The first step in using optparse is to create an OptionParser instance.

\n
\n
\nclass optparse.OptionParser(...)
\n

The OptionParser constructor has no required arguments, but a number of\noptional keyword arguments. You should always pass them as keyword\narguments, i.e. do not rely on the order in which the arguments are declared.

\n
\n
usage (default: "%prog [options]")
\n
The usage summary to print when your program is run incorrectly or with a\nhelp option. When optparse prints the usage string, it expands\n%prog to os.path.basename(sys.argv[0]) (or to prog if you\npassed that keyword argument). To suppress a usage message, pass the\nspecial value optparse.SUPPRESS_USAGE.
\n
option_list (default: [])
\n
A list of Option objects to populate the parser with. The options in\noption_list are added after any options in standard_option_list (a\nclass attribute that may be set by OptionParser subclasses), but before\nany version or help options. Deprecated; use add_option() after\ncreating the parser instead.
\n
option_class (default: optparse.Option)
\n
Class to use when adding options to the parser in add_option().
\n
version (default: None)
\n
A version string to print when the user supplies a version option. If you\nsupply a true value for version, optparse automatically adds a\nversion option with the single option string --version. The\nsubstring %prog is expanded the same as for usage.
\n
conflict_handler (default: "error")
\n
Specifies what to do when options with conflicting option strings are\nadded to the parser; see section\nConflicts between options.
\n
description (default: None)
\n
A paragraph of text giving a brief overview of your program.\noptparse reformats this paragraph to fit the current terminal width\nand prints it when the user requests help (after usage, but before the\nlist of options).
\n
formatter (default: a new IndentedHelpFormatter)
\n
An instance of optparse.HelpFormatter that will be used for printing help\ntext. optparse provides two concrete classes for this purpose:\nIndentedHelpFormatter and TitledHelpFormatter.
\n
add_help_option (default: True)
\n
If true, optparse will add a help option (with option strings -h\nand --help) to the parser.
\n
prog
\n
The string to use when expanding %prog in usage and version\ninstead of os.path.basename(sys.argv[0]).
\n
epilog (default: None)
\n
A paragraph of help text to print after the option help.
\n
\n
\n\n
\n
\n

15.5.3.2. Populating the parser

\n

There are several ways to populate the parser with options. The preferred way\nis by using OptionParser.add_option(), as shown in section\nTutorial. add_option() can be called in one of two ways:

\n
    \n
  • pass it an Option instance (as returned by make_option())
  • \n
  • pass it any combination of positional and keyword arguments that are\nacceptable to make_option() (i.e., to the Option constructor), and it\nwill create the Option instance for you
  • \n
\n

The other alternative is to pass a list of pre-constructed Option instances to\nthe OptionParser constructor, as in:

\n
option_list = [\n    make_option("-f", "--filename",\n                action="store", type="string", dest="filename"),\n    make_option("-q", "--quiet",\n                action="store_false", dest="verbose"),\n    ]\nparser = OptionParser(option_list=option_list)\n
\n
\n

(make_option() is a factory function for creating Option instances;\ncurrently it is an alias for the Option constructor. A future version of\noptparse may split Option into several classes, and make_option()\nwill pick the right class to instantiate. Do not instantiate Option directly.)

\n
\n
\n

15.5.3.3. Defining options

\n

Each Option instance represents a set of synonymous command-line option strings,\ne.g. -f and --file. You can specify any number of short or\nlong option strings, but you must specify at least one overall option string.

\n

The canonical way to create an Option instance is with the\nadd_option() method of OptionParser.

\n
\n
\nOptionParser.add_option(opt_str[, ...], attr=value, ...)
\n

To define an option with only a short option string:

\n
parser.add_option("-f", attr=value, ...)\n
\n
\n

And to define an option with only a long option string:

\n
parser.add_option("--foo", attr=value, ...)\n
\n
\n

The keyword arguments define attributes of the new Option object. The most\nimportant option attribute is action, and it largely\ndetermines which other attributes are relevant or required. If you pass\nirrelevant option attributes, or fail to pass required ones, optparse\nraises an OptionError exception explaining your mistake.

\n

An option’s action determines what optparse does when it encounters\nthis option on the command-line. The standard option actions hard-coded into\noptparse are:

\n
\n
"store"
\n
store this option’s argument (default)
\n
"store_const"
\n
store a constant value
\n
"store_true"
\n
store a true value
\n
"store_false"
\n
store a false value
\n
"append"
\n
append this option’s argument to a list
\n
"append_const"
\n
append a constant value to a list
\n
"count"
\n
increment a counter by one
\n
"callback"
\n
call a specified function
\n
"help"
\n
print a usage message including all options and the documentation for them
\n
\n

(If you don’t supply an action, the default is "store". For this action,\nyou may also supply type and dest option\nattributes; see Standard option actions.)

\n
\n\n

As you can see, most actions involve storing or updating a value somewhere.\noptparse always creates a special object for this, conventionally called\noptions (it happens to be an instance of optparse.Values). Option\narguments (and various other values) are stored as attributes of this object,\naccording to the dest (destination) option attribute.

\n

For example, when you call

\n
parser.parse_args()\n
\n
\n

one of the first things optparse does is create the options object:

\n
options = Values()\n
\n
\n

If one of the options in this parser is defined with

\n
parser.add_option("-f", "--file", action="store", type="string", dest="filename")\n
\n
\n

and the command-line being parsed includes any of the following:

\n
-ffoo\n-f foo\n--file=foo\n--file foo
\n
\n

then optparse, on seeing this option, will do the equivalent of

\n
options.filename = "foo"\n
\n
\n

The type and dest option attributes are almost\nas important as action, but action is the only\none that makes sense for all options.

\n
\n
\n

15.5.3.4. Option attributes

\n

The following option attributes may be passed as keyword arguments to\nOptionParser.add_option(). If you pass an option attribute that is not\nrelevant to a particular option, or fail to pass a required option attribute,\noptparse raises OptionError.

\n
\n
\nOption.action
\n

(default: "store")

\n

Determines optparse‘s behaviour when this option is seen on the\ncommand line; the available options are documented here.

\n
\n\n
\n
\nOption.type
\n

(default: "string")

\n

The argument type expected by this option (e.g., "string" or "int");\nthe available option types are documented here.

\n
\n\n
\n
\nOption.dest
\n

(default: derived from option strings)

\n

If the option’s action implies writing or modifying a value somewhere, this\ntells optparse where to write it: dest names an\nattribute of the options object that optparse builds as it parses\nthe command line.

\n
\n\n
\n
\nOption.default
\n
The value to use for this option’s destination if the option is not seen on\nthe command line. See also OptionParser.set_defaults().
\n\n
\n
\nOption.nargs
\n

(default: 1)

\n

How many arguments of type type should be consumed when this\noption is seen. If > 1, optparse will store a tuple of values to\ndest.

\n
\n\n
\n
\nOption.const
\n
For actions that store a constant value, the constant value to store.
\n\n
\n
\nOption.choices
\n
For options of type "choice", the list of strings the user may choose\nfrom.
\n\n
\n
\nOption.callback
\n
For options with action "callback", the callable to call when this option\nis seen. See section Option Callbacks for detail on the\narguments passed to the callable.
\n\n
\n
\nOption.callback_args
\n
\nOption.callback_kwargs
\n
Additional positional and keyword arguments to pass to callback after the\nfour standard callback arguments.
\n\n
\n
\nOption.help
\n
Help text to print for this option when listing all available options after\nthe user supplies a help option (such as --help). If\nno help text is supplied, the option will be listed without help text. To\nhide this option, use the special value optparse.SUPPRESS_HELP.
\n\n
\n
\nOption.metavar
\n

(default: derived from option strings)

\n

Stand-in for the option argument(s) to use when printing help text. See\nsection Tutorial for an example.

\n
\n\n
\n
\n

15.5.3.5. Standard option actions

\n

The various option actions all have slightly different requirements and effects.\nMost actions have several relevant option attributes which you may specify to\nguide optparse‘s behaviour; a few have required attributes, which you\nmust specify for any option using that action.

\n
    \n
  • "store" [relevant: type, dest,\nnargs, choices]

    \n

    The option must be followed by an argument, which is converted to a value\naccording to type and stored in dest. If\nnargs > 1, multiple arguments will be consumed from the\ncommand line; all will be converted according to type and\nstored to dest as a tuple. See the\nStandard option types section.

    \n

    If choices is supplied (a list or tuple of strings), the type\ndefaults to "choice".

    \n

    If type is not supplied, it defaults to "string".

    \n

    If dest is not supplied, optparse derives a destination\nfrom the first long option string (e.g., --foo-bar implies\nfoo_bar). If there are no long option strings, optparse derives a\ndestination from the first short option string (e.g., -f implies f).

    \n

    Example:

    \n
    parser.add_option("-f")\nparser.add_option("-p", type="float", nargs=3, dest="point")\n
    \n
    \n

    As it parses the command line

    \n
    -f foo.txt -p 1 -3.5 4 -fbar.txt
    \n
    \n

    optparse will set

    \n
    options.f = "foo.txt"\noptions.point = (1.0, -3.5, 4.0)\noptions.f = "bar.txt"\n
    \n
    \n
  • \n
  • "store_const" [required: const; relevant:\ndest]

    \n

    The value const is stored in dest.

    \n

    Example:

    \n
    parser.add_option("-q", "--quiet",\n                  action="store_const", const=0, dest="verbose")\nparser.add_option("-v", "--verbose",\n                  action="store_const", const=1, dest="verbose")\nparser.add_option("--noisy",\n                  action="store_const", const=2, dest="verbose")\n
    \n
    \n

    If --noisy is seen, optparse will set

    \n
    options.verbose = 2\n
    \n
    \n
  • \n
  • "store_true" [relevant: dest]

    \n

    A special case of "store_const" that stores a true value to\ndest.

    \n
  • \n
  • "store_false" [relevant: dest]

    \n

    Like "store_true", but stores a false value.

    \n

    Example:

    \n
    parser.add_option("--clobber", action="store_true", dest="clobber")\nparser.add_option("--no-clobber", action="store_false", dest="clobber")\n
    \n
    \n
  • \n
  • "append" [relevant: type, dest,\nnargs, choices]

    \n

    The option must be followed by an argument, which is appended to the list in\ndest. If no default value for dest is\nsupplied, an empty list is automatically created when optparse first\nencounters this option on the command-line. If nargs > 1,\nmultiple arguments are consumed, and a tuple of length nargs\nis appended to dest.

    \n

    The defaults for type and dest are the same as\nfor the "store" action.

    \n

    Example:

    \n
    parser.add_option("-t", "--tracks", action="append", type="int")\n
    \n
    \n

    If -t3 is seen on the command-line, optparse does the equivalent\nof:

    \n
    options.tracks = []\noptions.tracks.append(int("3"))\n
    \n
    \n

    If, a little later on, --tracks=4 is seen, it does:

    \n
    options.tracks.append(int("4"))\n
    \n
    \n
  • \n
  • "append_const" [required: const; relevant:\ndest]

    \n

    Like "store_const", but the value const is appended to\ndest; as with "append", dest defaults to\nNone, and an empty list is automatically created the first time the option\nis encountered.

    \n
  • \n
  • "count" [relevant: dest]

    \n

    Increment the integer stored at dest. If no default value is\nsupplied, dest is set to zero before being incremented the\nfirst time.

    \n

    Example:

    \n
    parser.add_option("-v", action="count", dest="verbosity")\n
    \n
    \n

    The first time -v is seen on the command line, optparse does the\nequivalent of:

    \n
    options.verbosity = 0\noptions.verbosity += 1\n
    \n
    \n

    Every subsequent occurrence of -v results in

    \n
    options.verbosity += 1\n
    \n
    \n
  • \n
  • "callback" [required: callback; relevant:\ntype, nargs, callback_args,\ncallback_kwargs]

    \n

    Call the function specified by callback, which is called as

    \n
    func(option, opt_str, value, parser, *args, **kwargs)\n
    \n
    \n

    See section Option Callbacks for more detail.

    \n
  • \n
  • "help"

    \n

    Prints a complete help message for all the options in the current option\nparser. The help message is constructed from the usage string passed to\nOptionParser’s constructor and the help string passed to every\noption.

    \n

    If no help string is supplied for an option, it will still be\nlisted in the help message. To omit an option entirely, use the special value\noptparse.SUPPRESS_HELP.

    \n

    optparse automatically adds a help option to all\nOptionParsers, so you do not normally need to create one.

    \n

    Example:

    \n
    from optparse import OptionParser, SUPPRESS_HELP\n\n# usually, a help option is added automatically, but that can\n# be suppressed using the add_help_option argument\nparser = OptionParser(add_help_option=False)\n\nparser.add_option("-h", "--help", action="help")\nparser.add_option("-v", action="store_true", dest="verbose",\n                  help="Be moderately verbose")\nparser.add_option("--file", dest="filename",\n                  help="Input file to read data from")\nparser.add_option("--secret", help=SUPPRESS_HELP)\n
    \n
    \n

    If optparse sees either -h or --help on the command line,\nit will print something like the following help message to stdout (assuming\nsys.argv[0] is "foo.py"):

    \n
    Usage: foo.py [options]\n\nOptions:\n  -h, --help        Show this help message and exit\n  -v                Be moderately verbose\n  --file=FILENAME   Input file to read data from\n
    \n
    \n

    After printing the help message, optparse terminates your process with\nsys.exit(0).

    \n
  • \n
  • "version"

    \n

    Prints the version number supplied to the OptionParser to stdout and exits.\nThe version number is actually formatted and printed by the\nprint_version() method of OptionParser. Generally only relevant if the\nversion argument is supplied to the OptionParser constructor. As with\nhelp options, you will rarely create version options,\nsince optparse automatically adds them when needed.

    \n
  • \n
\n
\n
\n

15.5.3.6. Standard option types

\n

optparse has six built-in option types: "string", "int",\n"long", "choice", "float" and "complex". If you need to add new\noption types, see section Extending optparse.

\n

Arguments to string options are not checked or converted in any way: the text on\nthe command line is stored in the destination (or passed to the callback) as-is.

\n

Integer arguments (type "int" or "long") are parsed as follows:

\n
    \n
  • if the number starts with 0x, it is parsed as a hexadecimal number
  • \n
  • if the number starts with 0, it is parsed as an octal number
  • \n
  • if the number starts with 0b, it is parsed as a binary number
  • \n
  • otherwise, the number is parsed as a decimal number
  • \n
\n

The conversion is done by calling either int() or long() with the\nappropriate base (2, 8, 10, or 16). If this fails, so will optparse,\nalthough with a more useful error message.

\n

"float" and "complex" option arguments are converted directly with\nfloat() and complex(), with similar error-handling.

\n

"choice" options are a subtype of "string" options. The\nchoices option attribute (a sequence of strings) defines the\nset of allowed option arguments. optparse.check_choice() compares\nuser-supplied option arguments against this master list and raises\nOptionValueError if an invalid string is given.

\n
\n
\n

15.5.3.7. Parsing arguments

\n

The whole point of creating and populating an OptionParser is to call its\nparse_args() method:

\n
(options, args) = parser.parse_args(args=None, values=None)\n
\n
\n

where the input parameters are

\n
\n
args
\n
the list of arguments to process (default: sys.argv[1:])
\n
values
\n
a optparse.Values object to store option arguments in (default: a\nnew instance of Values) – if you give an existing object, the\noption defaults will not be initialized on it
\n
\n

and the return values are

\n
\n
options
\n
the same object that was passed in as values, or the optparse.Values\ninstance created by optparse
\n
args
\n
the leftover positional arguments after all options have been processed
\n
\n

The most common usage is to supply neither keyword argument. If you supply\nvalues, it will be modified with repeated setattr() calls (roughly one\nfor every option argument stored to an option destination) and returned by\nparse_args().

\n

If parse_args() encounters any errors in the argument list, it calls the\nOptionParser’s error() method with an appropriate end-user error message.\nThis ultimately terminates your process with an exit status of 2 (the\ntraditional Unix exit status for command-line errors).

\n
\n
\n

15.5.3.8. Querying and manipulating your option parser

\n

The default behavior of the option parser can be customized slightly, and you\ncan also poke around your option parser and see what’s there. OptionParser\nprovides several methods to help you out:

\n
\n
\nOptionParser.disable_interspersed_args()
\n

Set parsing to stop on the first non-option. For example, if -a and\n-b are both simple options that take no arguments, optparse\nnormally accepts this syntax:

\n
prog -a arg1 -b arg2
\n
\n

and treats it as equivalent to

\n
prog -a -b arg1 arg2
\n
\n

To disable this feature, call disable_interspersed_args(). This\nrestores traditional Unix syntax, where option parsing stops with the first\nnon-option argument.

\n

Use this if you have a command processor which runs another command which has\noptions of its own and you want to make sure these options don’t get\nconfused. For example, each command might have a different set of options.

\n
\n\n
\n
\nOptionParser.enable_interspersed_args()
\n
Set parsing to not stop on the first non-option, allowing interspersing\nswitches with command arguments. This is the default behavior.
\n\n
\n
\nOptionParser.get_option(opt_str)
\n
Returns the Option instance with the option string opt_str, or None if\nno options have that option string.
\n\n
\n
\nOptionParser.has_option(opt_str)
\n
Return true if the OptionParser has an option with option string opt_str\n(e.g., -q or --verbose).
\n\n
\n
\nOptionParser.remove_option(opt_str)
\n
If the OptionParser has an option corresponding to opt_str, that\noption is removed. If that option provided any other option strings, all of\nthose option strings become invalid. If opt_str does not occur in any\noption belonging to this OptionParser, raises ValueError.
\n\n
\n
\n

15.5.3.9. Conflicts between options

\n

If you’re not careful, it’s easy to define options with conflicting option\nstrings:

\n
parser.add_option("-n", "--dry-run", ...)\n[...]\nparser.add_option("-n", "--noisy", ...)\n
\n
\n

(This is particularly true if you’ve defined your own OptionParser subclass with\nsome standard options.)

\n

Every time you add an option, optparse checks for conflicts with existing\noptions. If it finds any, it invokes the current conflict-handling mechanism.\nYou can set the conflict-handling mechanism either in the constructor:

\n
parser = OptionParser(..., conflict_handler=handler)\n
\n
\n

or with a separate call:

\n
parser.set_conflict_handler(handler)\n
\n
\n

The available conflict handlers are:

\n
\n
\n
"error" (default)
\n
assume option conflicts are a programming error and raise\nOptionConflictError
\n
"resolve"
\n
resolve option conflicts intelligently (see below)
\n
\n
\n

As an example, let’s define an OptionParser that resolves conflicts\nintelligently and add conflicting options to it:

\n
parser = OptionParser(conflict_handler="resolve")\nparser.add_option("-n", "--dry-run", ..., help="do no harm")\nparser.add_option("-n", "--noisy", ..., help="be noisy")\n
\n
\n

At this point, optparse detects that a previously-added option is already\nusing the -n option string. Since conflict_handler is "resolve",\nit resolves the situation by removing -n from the earlier option’s list of\noption strings. Now --dry-run is the only way for the user to activate\nthat option. If the user asks for help, the help message will reflect that:

\n
Options:\n  --dry-run     do no harm\n  [...]\n  -n, --noisy   be noisy
\n
\n

It’s possible to whittle away the option strings for a previously-added option\nuntil there are none left, and the user has no way of invoking that option from\nthe command-line. In that case, optparse removes that option completely,\nso it doesn’t show up in help text or anywhere else. Carrying on with our\nexisting OptionParser:

\n
parser.add_option("--dry-run", ..., help="new dry-run option")\n
\n
\n

At this point, the original -n/--dry-run option is no longer\naccessible, so optparse removes it, leaving this help text:

\n
Options:\n  [...]\n  -n, --noisy   be noisy\n  --dry-run     new dry-run option
\n
\n
\n
\n

15.5.3.10. Cleanup

\n

OptionParser instances have several cyclic references. This should not be a\nproblem for Python’s garbage collector, but you may wish to break the cyclic\nreferences explicitly by calling destroy() on your\nOptionParser once you are done with it. This is particularly useful in\nlong-running applications where large object graphs are reachable from your\nOptionParser.

\n
\n
\n

15.5.3.11. Other methods

\n

OptionParser supports several other public methods:

\n
\n
\nOptionParser.set_usage(usage)
\n
Set the usage string according to the rules described above for the usage\nconstructor keyword argument. Passing None sets the default usage\nstring; use optparse.SUPPRESS_USAGE to suppress a usage message.
\n\n
\n
\nOptionParser.print_usage(file=None)
\n
Print the usage message for the current program (self.usage) to file\n(default stdout). Any occurrence of the string %prog in self.usage\nis replaced with the name of the current program. Does nothing if\nself.usage is empty or not defined.
\n\n
\n
\nOptionParser.get_usage()
\n
Same as print_usage() but returns the usage string instead of\nprinting it.
\n\n
\n
\nOptionParser.set_defaults(dest=value, ...)
\n

Set default values for several option destinations at once. Using\nset_defaults() is the preferred way to set default values for options,\nsince multiple options can share the same destination. For example, if\nseveral “mode” options all set the same destination, any one of them can set\nthe default, and the last one wins:

\n
parser.add_option("--advanced", action="store_const",\n                  dest="mode", const="advanced",\n                  default="novice")    # overridden below\nparser.add_option("--novice", action="store_const",\n                  dest="mode", const="novice",\n                  default="advanced")  # overrides above setting\n
\n
\n

To avoid this confusion, use set_defaults():

\n
parser.set_defaults(mode="advanced")\nparser.add_option("--advanced", action="store_const",\n                  dest="mode", const="advanced")\nparser.add_option("--novice", action="store_const",\n                  dest="mode", const="novice")\n
\n
\n
\n\n
\n
\n
\n

15.5.4. Option Callbacks

\n

When optparse‘s built-in actions and types aren’t quite enough for your\nneeds, you have two choices: extend optparse or define a callback option.\nExtending optparse is more general, but overkill for a lot of simple\ncases. Quite often a simple callback is all you need.

\n

There are two steps to defining a callback option:

\n\n
\n

15.5.4.1. Defining a callback option

\n

As always, the easiest way to define a callback option is by using the\nOptionParser.add_option() method. Apart from action, the\nonly option attribute you must specify is callback, the function to call:

\n
parser.add_option("-c", action="callback", callback=my_callback)\n
\n
\n

callback is a function (or other callable object), so you must have already\ndefined my_callback() when you create this callback option. In this simple\ncase, optparse doesn’t even know if -c takes any arguments,\nwhich usually means that the option takes no arguments—the mere presence of\n-c on the command-line is all it needs to know. In some\ncircumstances, though, you might want your callback to consume an arbitrary\nnumber of command-line arguments. This is where writing callbacks gets tricky;\nit’s covered later in this section.

\n

optparse always passes four particular arguments to your callback, and it\nwill only pass additional arguments if you specify them via\ncallback_args and callback_kwargs. Thus, the\nminimal callback function signature is:

\n
def my_callback(option, opt, value, parser):
\n
\n

The four arguments to a callback are described below.

\n

There are several other option attributes that you can supply when you define a\ncallback option:

\n
\n
type
\n
has its usual meaning: as with the "store" or "append" actions, it\ninstructs optparse to consume one argument and convert it to\ntype. Rather than storing the converted value(s) anywhere,\nthough, optparse passes it to your callback function.
\n
nargs
\n
also has its usual meaning: if it is supplied and > 1, optparse will\nconsume nargs arguments, each of which must be convertible to\ntype. It then passes a tuple of converted values to your\ncallback.
\n
callback_args
\n
a tuple of extra positional arguments to pass to the callback
\n
callback_kwargs
\n
a dictionary of extra keyword arguments to pass to the callback
\n
\n
\n
\n

15.5.4.2. How callbacks are called

\n

All callbacks are called as follows:

\n
func(option, opt_str, value, parser, *args, **kwargs)\n
\n
\n

where

\n
\n
option
\n
is the Option instance that’s calling the callback
\n
opt_str
\n
is the option string seen on the command-line that’s triggering the callback.\n(If an abbreviated long option was used, opt_str will be the full,\ncanonical option string—e.g. if the user puts --foo on the\ncommand-line as an abbreviation for --foobar, then opt_str will be\n"--foobar".)
\n
value
\n
is the argument to this option seen on the command-line. optparse will\nonly expect an argument if type is set; the type of value will be\nthe type implied by the option’s type. If type for this option is\nNone (no argument expected), then value will be None. If nargs\n> 1, value will be a tuple of values of the appropriate type.
\n
parser
\n

is the OptionParser instance driving the whole thing, mainly useful because\nyou can access some other interesting data through its instance attributes:

\n
\n
parser.largs
\n
the current list of leftover arguments, ie. arguments that have been\nconsumed but are neither options nor option arguments. Feel free to modify\nparser.largs, e.g. by adding more arguments to it. (This list will\nbecome args, the second return value of parse_args().)
\n
parser.rargs
\n
the current list of remaining arguments, ie. with opt_str and\nvalue (if applicable) removed, and only the arguments following them\nstill there. Feel free to modify parser.rargs, e.g. by consuming more\narguments.
\n
parser.values
\n
the object where option values are by default stored (an instance of\noptparse.OptionValues). This lets callbacks use the same mechanism as the\nrest of optparse for storing option values; you don’t need to mess\naround with globals or closures. You can also access or modify the\nvalue(s) of any options already encountered on the command-line.
\n
\n
\n
args
\n
is a tuple of arbitrary positional arguments supplied via the\ncallback_args option attribute.
\n
kwargs
\n
is a dictionary of arbitrary keyword arguments supplied via\ncallback_kwargs.
\n
\n
\n
\n

15.5.4.3. Raising errors in a callback

\n

The callback function should raise OptionValueError if there are any\nproblems with the option or its argument(s). optparse catches this and\nterminates the program, printing the error message you supply to stderr. Your\nmessage should be clear, concise, accurate, and mention the option at fault.\nOtherwise, the user will have a hard time figuring out what he did wrong.

\n
\n
\n

15.5.4.4. Callback example 1: trivial callback

\n

Here’s an example of a callback option that takes no arguments, and simply\nrecords that the option was seen:

\n
def record_foo_seen(option, opt_str, value, parser):\n    parser.values.saw_foo = True\n\nparser.add_option("--foo", action="callback", callback=record_foo_seen)\n
\n
\n

Of course, you could do that with the "store_true" action.

\n
\n
\n

15.5.4.5. Callback example 2: check option order

\n

Here’s a slightly more interesting example: record the fact that -a is\nseen, but blow up if it comes after -b in the command-line.

\n
def check_order(option, opt_str, value, parser):\n    if parser.values.b:\n        raise OptionValueError("can't use -a after -b")\n    parser.values.a = 1\n[...]\nparser.add_option("-a", action="callback", callback=check_order)\nparser.add_option("-b", action="store_true", dest="b")\n
\n
\n
\n
\n

15.5.4.6. Callback example 3: check option order (generalized)

\n

If you want to re-use this callback for several similar options (set a flag, but\nblow up if -b has already been seen), it needs a bit of work: the error\nmessage and the flag that it sets must be generalized.

\n
def check_order(option, opt_str, value, parser):\n    if parser.values.b:\n        raise OptionValueError("can't use %s after -b"  opt_str)\n    setattr(parser.values, option.dest, 1)\n[...]\nparser.add_option("-a", action="callback", callback=check_order, dest='a')\nparser.add_option("-b", action="store_true", dest="b")\nparser.add_option("-c", action="callback", callback=check_order, dest='c')\n
\n
\n
\n
\n

15.5.4.7. Callback example 4: check arbitrary condition

\n

Of course, you could put any condition in there—you’re not limited to checking\nthe values of already-defined options. For example, if you have options that\nshould not be called when the moon is full, all you have to do is this:

\n
def check_moon(option, opt_str, value, parser):\n    if is_moon_full():\n        raise OptionValueError("%s option invalid when moon is full"\n                                opt_str)\n    setattr(parser.values, option.dest, 1)\n[...]\nparser.add_option("--foo",\n                  action="callback", callback=check_moon, dest="foo")\n
\n
\n

(The definition of is_moon_full() is left as an exercise for the reader.)

\n
\n
\n

15.5.4.8. Callback example 5: fixed arguments

\n

Things get slightly more interesting when you define callback options that take\na fixed number of arguments. Specifying that a callback option takes arguments\nis similar to defining a "store" or "append" option: if you define\ntype, then the option takes one argument that must be\nconvertible to that type; if you further define nargs, then the\noption takes nargs arguments.

\n

Here’s an example that just emulates the standard "store" action:

\n
def store_value(option, opt_str, value, parser):\n    setattr(parser.values, option.dest, value)\n[...]\nparser.add_option("--foo",\n                  action="callback", callback=store_value,\n                  type="int", nargs=3, dest="foo")\n
\n
\n

Note that optparse takes care of consuming 3 arguments and converting\nthem to integers for you; all you have to do is store them. (Or whatever;\nobviously you don’t need a callback for this example.)

\n
\n
\n

15.5.4.9. Callback example 6: variable arguments

\n

Things get hairy when you want an option to take a variable number of arguments.\nFor this case, you must write a callback, as optparse doesn’t provide any\nbuilt-in capabilities for it. And you have to deal with certain intricacies of\nconventional Unix command-line parsing that optparse normally handles for\nyou. In particular, callbacks should implement the conventional rules for bare\n-- and - arguments:

\n
    \n
  • either -- or - can be option arguments
  • \n
  • bare -- (if not the argument to some option): halt command-line\nprocessing and discard the --
  • \n
  • bare - (if not the argument to some option): halt command-line\nprocessing but keep the - (append it to parser.largs)
  • \n
\n

If you want an option that takes a variable number of arguments, there are\nseveral subtle, tricky issues to worry about. The exact implementation you\nchoose will be based on which trade-offs you’re willing to make for your\napplication (which is why optparse doesn’t support this sort of thing\ndirectly).

\n

Nevertheless, here’s a stab at a callback for an option with variable\narguments:

\n
 def vararg_callback(option, opt_str, value, parser):\n     assert value is None\n     value = []\n\n     def floatable(str):\n         try:\n             float(str)\n             return True\n         except ValueError:\n             return False\n\n     for arg in parser.rargs:\n         # stop on --foo like options\n         if arg[:2] == \"--\" and len(arg) > 2:\n             break\n         # stop on -a, but not on -3 or -3.0\n         if arg[:1] == \"-\" and len(arg) > 1 and not floatable(arg):\n             break\n         value.append(arg)\n\n     del parser.rargs[:len(value)]\n     setattr(parser.values, option.dest, value)\n\n[...]\nparser.add_option(\"-c\", \"--callback\", dest=\"vararg_attr\",\n                  action=\"callback\", callback=vararg_callback)
\n
\n
\n
\n
\n

15.5.5. Extending optparse

\n

Since the two major controlling factors in how optparse interprets\ncommand-line options are the action and type of each option, the most likely\ndirection of extension is to add new actions and new types.

\n
\n

15.5.5.1. Adding new types

\n

To add new types, you need to define your own subclass of optparse‘s\nOption class. This class has a couple of attributes that define\noptparse‘s types: TYPES and TYPE_CHECKER.

\n
\n
\nOption.TYPES
\n
A tuple of type names; in your subclass, simply define a new tuple\nTYPES that builds on the standard one.
\n\n
\n
\nOption.TYPE_CHECKER
\n

A dictionary mapping type names to type-checking functions. A type-checking\nfunction has the following signature:

\n
def check_mytype(option, opt, value)
\n
\n

where option is an Option instance, opt is an option string\n(e.g., -f), and value is the string from the command line that must\nbe checked and converted to your desired type. check_mytype() should\nreturn an object of the hypothetical type mytype. The value returned by\na type-checking function will wind up in the OptionValues instance returned\nby OptionParser.parse_args(), or be passed to a callback as the\nvalue parameter.

\n

Your type-checking function should raise OptionValueError if it\nencounters any problems. OptionValueError takes a single string\nargument, which is passed as-is to OptionParser‘s error()\nmethod, which in turn prepends the program name and the string "error:"\nand prints everything to stderr before terminating the process.

\n
\n\n

Here’s a silly example that demonstrates adding a "complex" option type to\nparse Python-style complex numbers on the command line. (This is even sillier\nthan it used to be, because optparse 1.3 added built-in support for\ncomplex numbers, but never mind.)

\n

First, the necessary imports:

\n
from copy import copy\nfrom optparse import Option, OptionValueError\n
\n
\n

You need to define your type-checker first, since it’s referred to later (in the\nTYPE_CHECKER class attribute of your Option subclass):

\n
def check_complex(option, opt, value):\n    try:\n        return complex(value)\n    except ValueError:\n        raise OptionValueError(\n            "option %s: invalid complex value: %r"  (opt, value))\n
\n
\n

Finally, the Option subclass:

\n
class MyOption (Option):\n    TYPES = Option.TYPES + ("complex",)\n    TYPE_CHECKER = copy(Option.TYPE_CHECKER)\n    TYPE_CHECKER["complex"] = check_complex\n
\n
\n

(If we didn’t make a copy() of Option.TYPE_CHECKER, we would end\nup modifying the TYPE_CHECKER attribute of optparse‘s\nOption class. This being Python, nothing stops you from doing that except good\nmanners and common sense.)

\n

That’s it! Now you can write a script that uses the new option type just like\nany other optparse-based script, except you have to instruct your\nOptionParser to use MyOption instead of Option:

\n
parser = OptionParser(option_class=MyOption)\nparser.add_option("-c", type="complex")\n
\n
\n

Alternately, you can build your own option list and pass it to OptionParser; if\nyou don’t use add_option() in the above way, you don’t need to tell\nOptionParser which option class to use:

\n
option_list = [MyOption("-c", action="store", type="complex", dest="c")]\nparser = OptionParser(option_list=option_list)\n
\n
\n
\n
\n

15.5.5.2. Adding new actions

\n

Adding new actions is a bit trickier, because you have to understand that\noptparse has a couple of classifications for actions:

\n
\n
“store” actions
\n
actions that result in optparse storing a value to an attribute of the\ncurrent OptionValues instance; these options require a dest\nattribute to be supplied to the Option constructor.
\n
“typed” actions
\n
actions that take a value from the command line and expect it to be of a\ncertain type; or rather, a string that can be converted to a certain type.\nThese options require a type attribute to the Option\nconstructor.
\n
\n

These are overlapping sets: some default “store” actions are "store",\n"store_const", "append", and "count", while the default “typed”\nactions are "store", "append", and "callback".

\n

When you add an action, you need to categorize it by listing it in at least one\nof the following class attributes of Option (all are lists of strings):

\n
\n
\nOption.ACTIONS
\n
All actions must be listed in ACTIONS.
\n\n
\n
\nOption.STORE_ACTIONS
\n
“store” actions are additionally listed here.
\n\n
\n
\nOption.TYPED_ACTIONS
\n
“typed” actions are additionally listed here.
\n\n
\n
\nOption.ALWAYS_TYPED_ACTIONS
\n
Actions that always take a type (i.e. whose options always take a value) are\nadditionally listed here. The only effect of this is that optparse\nassigns the default type, "string", to options with no explicit type\nwhose action is listed in ALWAYS_TYPED_ACTIONS.
\n\n

In order to actually implement your new action, you must override Option’s\ntake_action() method and add a case that recognizes your action.

\n

For example, let’s add an "extend" action. This is similar to the standard\n"append" action, but instead of taking a single value from the command-line\nand appending it to an existing list, "extend" will take multiple values in\na single comma-delimited string, and extend an existing list with them. That\nis, if --names is an "extend" option of type "string", the command\nline

\n
--names=foo,bar --names blah --names ding,dong
\n
\n

would result in a list

\n
["foo", "bar", "blah", "ding", "dong"]\n
\n
\n

Again we define a subclass of Option:

\n
class MyOption(Option):\n\n    ACTIONS = Option.ACTIONS + ("extend",)\n    STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)\n    TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)\n    ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)\n\n    def take_action(self, action, dest, opt, value, values, parser):\n        if action == "extend":\n            lvalue = value.split(",")\n            values.ensure_value(dest, []).extend(lvalue)\n        else:\n            Option.take_action(\n                self, action, dest, opt, value, values, parser)\n
\n
\n

Features of note:

\n
    \n
  • "extend" both expects a value on the command-line and stores that value\nsomewhere, so it goes in both STORE_ACTIONS and\nTYPED_ACTIONS.

    \n
  • \n
  • to ensure that optparse assigns the default type of "string" to\n"extend" actions, we put the "extend" action in\nALWAYS_TYPED_ACTIONS as well.

    \n
  • \n
  • MyOption.take_action() implements just this one new action, and passes\ncontrol back to Option.take_action() for the standard optparse\nactions.

    \n
  • \n
  • values is an instance of the optparse_parser.Values class, which provides\nthe very useful ensure_value() method. ensure_value() is\nessentially getattr() with a safety valve; it is called as

    \n
    values.ensure_value(attr, value)\n
    \n
    \n

    If the attr attribute of values doesn’t exist or is None, then\nensure_value() first sets it to value, and then returns ‘value. This is\nvery handy for actions like "extend", "append", and "count", all\nof which accumulate data in a variable and expect that variable to be of a\ncertain type (a list for the first two, an integer for the latter). Using\nensure_value() means that scripts using your action don’t have to worry\nabout setting a default value for the option destinations in question; they\ncan just leave the default as None and ensure_value() will take care of\ngetting it right when it’s needed.

    \n
  • \n
\n
\n
\n
", "searchableItems": [ { "name": "optparse.OptionGroup", "domId": "optparse_optparse.OptionGroup" }, { "name": "optparse.OptionParser", "domId": "optparse_optparse.OptionParser" }, { "name": "optparse.OptionParser.add_option", "domId": "optparse_optparse.OptionParser.add_option" }, { "name": "optparse.OptionParser.disable_interspersed_args", "domId": "optparse_optparse.OptionParser.disable_interspersed_args" }, { "name": "optparse.OptionParser.enable_interspersed_args", "domId": "optparse_optparse.OptionParser.enable_interspersed_args" }, { "name": "optparse.OptionParser.get_option", "domId": "optparse_optparse.OptionParser.get_option" }, { "name": "optparse.OptionParser.get_option_group", "domId": "optparse_optparse.OptionParser.get_option_group" }, { "name": "optparse.OptionParser.get_usage", "domId": "optparse_optparse.OptionParser.get_usage" }, { "name": "optparse.OptionParser.get_version", "domId": "optparse_optparse.OptionParser.get_version" }, { "name": "optparse.OptionParser.has_option", "domId": "optparse_optparse.OptionParser.has_option" }, { "name": "optparse.OptionParser.print_usage", "domId": "optparse_optparse.OptionParser.print_usage" }, { "name": "optparse.OptionParser.print_version", "domId": "optparse_optparse.OptionParser.print_version" }, { "name": "optparse.OptionParser.remove_option", "domId": "optparse_optparse.OptionParser.remove_option" }, { "name": "optparse.OptionParser.set_defaults", "domId": "optparse_optparse.OptionParser.set_defaults" }, { "name": "optparse.OptionParser.set_usage", "domId": "optparse_optparse.OptionParser.set_usage" } ] }, { "url": "http://docs.python.org/library/getpass.html", "title": "getpass", "html": "
\n

15.10. getpass — Portable password input

\n

The getpass module provides two functions:

\n
\n
\ngetpass.getpass([prompt[, stream]])
\n

Prompt the user for a password without echoing. The user is prompted using the\nstring prompt, which defaults to 'Password: '. On Unix, the prompt is\nwritten to the file-like object stream. stream defaults to the\ncontrolling terminal (/dev/tty) or if that is unavailable to sys.stderr\n(this argument is ignored on Windows).

\n

If echo free input is unavailable getpass() falls back to printing\na warning message to stream and reading from sys.stdin and\nissuing a GetPassWarning.

\n

Availability: Macintosh, Unix, Windows.

\n

\nChanged in version 2.5: The stream parameter was added.

\n

\nChanged in version 2.6: On Unix it defaults to using /dev/tty before falling back\nto sys.stdin and sys.stderr.

\n
\n

Note

\n

If you call getpass from within IDLE, the input may be done in the\nterminal you launched IDLE from rather than the idle window itself.

\n
\n
\n\n
\n
\nexception getpass.GetPassWarning
\n
A UserWarning subclass issued when password input may be echoed.
\n\n
\n
\ngetpass.getuser()
\n

Return the “login name” of the user. Availability: Unix, Windows.

\n

This function checks the environment variables LOGNAME,\nUSER, LNAME and USERNAME, in order, and returns\nthe value of the first one which is set to a non-empty string. If none are set,\nthe login name from the password database is returned on systems which support\nthe pwd module, otherwise, an exception is raised.

\n
\n\n
", "searchableItems": [ { "name": "getpass.getpass", "domId": "getpass_getpass.getpass" }, { "name": "getpass.getuser", "domId": "getpass_getpass.getuser" } ] }, { "url": "http://docs.python.org/library/logging.handlers.html", "title": "logging.handlers", "html": "
\n

15.9. logging.handlers — Logging handlers

\n
\n

Important

\n

This page contains only reference information. For tutorials,\nplease see

\n\n
\n

The following useful handlers are provided in the package. Note that three of\nthe handlers (StreamHandler, FileHandler and\nNullHandler) are actually defined in the logging module itself,\nbut have been documented here along with the other handlers.

\n
\n

15.9.1. StreamHandler

\n

The StreamHandler class, located in the core logging package,\nsends logging output to streams such as sys.stdout, sys.stderr or any\nfile-like object (or, more precisely, any object which supports write()\nand flush() methods).

\n
\n
\nclass logging.StreamHandler(stream=None)
\n

Returns a new instance of the StreamHandler class. If stream is\nspecified, the instance will use it for logging output; otherwise, sys.stderr\nwill be used.

\n
\n
\nemit(record)
\n
If a formatter is specified, it is used to format the record. The record\nis then written to the stream with a newline terminator. If exception\ninformation is present, it is formatted using\ntraceback.print_exception() and appended to the stream.
\n\n
\n
\nflush()
\n
Flushes the stream by calling its flush() method. Note that the\nclose() method is inherited from Handler and so does\nno output, so an explicit flush() call may be needed at times.
\n\n
\n\n
\n
\n

15.9.2. FileHandler

\n

The FileHandler class, located in the core logging package,\nsends logging output to a disk file. It inherits the output functionality from\nStreamHandler.

\n
\n
\nclass logging.FileHandler(filename, mode='a', encoding=None, delay=False)
\n

Returns a new instance of the FileHandler class. The specified file is\nopened and used as the stream for logging. If mode is not specified,\n'a' is used. If encoding is not None, it is used to open the file\nwith that encoding. If delay is true, then file opening is deferred until the\nfirst call to emit(). By default, the file grows indefinitely.

\n

\nChanged in version 2.6: delay was added.

\n
\n
\nclose()
\n
Closes the file.
\n\n
\n
\nemit(record)
\n
Outputs the record to the file.
\n\n
\n\n
\n
\n

15.9.3. NullHandler

\n

\nNew in version 2.7.

\n

The NullHandler class, located in the core logging package,\ndoes not do any formatting or output. It is essentially a ‘no-op’ handler\nfor use by library developers.

\n
\n
\nclass logging.NullHandler
\n

Returns a new instance of the NullHandler class.

\n
\n
\nemit(record)
\n
This method does nothing.
\n\n
\n
\nhandle(record)
\n
This method does nothing.
\n\n
\n
\ncreateLock()
\n
This method returns None for the lock, since there is no\nunderlying I/O to which access needs to be serialized.
\n\n
\n\n

See Configuring Logging for a Library for more information on how to use\nNullHandler.

\n
\n
\n

15.9.4. WatchedFileHandler

\n

\nNew in version 2.6.

\n

The WatchedFileHandler class, located in the logging.handlers\nmodule, is a FileHandler which watches the file it is logging to. If\nthe file changes, it is closed and reopened using the file name.

\n

A file change can happen because of usage of programs such as newsyslog and\nlogrotate which perform log file rotation. This handler, intended for use\nunder Unix/Linux, watches the file to see if it has changed since the last emit.\n(A file is deemed to have changed if its device or inode have changed.) If the\nfile has changed, the old file stream is closed, and the file opened to get a\nnew stream.

\n

This handler is not appropriate for use under Windows, because under Windows\nopen log files cannot be moved or renamed - logging opens the files with\nexclusive locks - and so there is no need for such a handler. Furthermore,\nST_INO is not supported under Windows; stat() always returns zero for\nthis value.

\n
\n
\nclass logging.handlers.WatchedFileHandler(filename[, mode[, encoding[, delay]]])
\n

Returns a new instance of the WatchedFileHandler class. The specified\nfile is opened and used as the stream for logging. If mode is not specified,\n'a' is used. If encoding is not None, it is used to open the file\nwith that encoding. If delay is true, then file opening is deferred until the\nfirst call to emit(). By default, the file grows indefinitely.

\n
\n
\nemit(record)
\n
Outputs the record to the file, but first checks to see if the file has\nchanged. If it has, the existing stream is flushed and closed and the\nfile opened again, before outputting the record to the file.
\n\n
\n\n
\n
\n

15.9.5. RotatingFileHandler

\n

The RotatingFileHandler class, located in the logging.handlers\nmodule, supports rotation of disk log files.

\n
\n
\nclass logging.handlers.RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)
\n

Returns a new instance of the RotatingFileHandler class. The specified\nfile is opened and used as the stream for logging. If mode is not specified,\n'a' is used. If encoding is not None, it is used to open the file\nwith that encoding. If delay is true, then file opening is deferred until the\nfirst call to emit(). By default, the file grows indefinitely.

\n

You can use the maxBytes and backupCount values to allow the file to\nrollover at a predetermined size. When the size is about to be exceeded,\nthe file is closed and a new file is silently opened for output. Rollover occurs\nwhenever the current log file is nearly maxBytes in length; if maxBytes is\nzero, rollover never occurs. If backupCount is non-zero, the system will save\nold log files by appending the extensions ‘.1’, ‘.2’ etc., to the filename. For\nexample, with a backupCount of 5 and a base file name of app.log, you\nwould get app.log, app.log.1, app.log.2, up to\napp.log.5. The file being written to is always app.log. When\nthis file is filled, it is closed and renamed to app.log.1, and if files\napp.log.1, app.log.2, etc. exist, then they are renamed to\napp.log.2, app.log.3 etc. respectively.

\n

\nChanged in version 2.6: delay was added.

\n
\n
\ndoRollover()
\n
Does a rollover, as described above.
\n\n
\n
\nemit(record)
\n
Outputs the record to the file, catering for rollover as described\npreviously.
\n\n
\n\n
\n
\n

15.9.6. TimedRotatingFileHandler

\n

The TimedRotatingFileHandler class, located in the\nlogging.handlers module, supports rotation of disk log files at certain\ntimed intervals.

\n
\n
\nclass logging.handlers.TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
\n

Returns a new instance of the TimedRotatingFileHandler class. The\nspecified file is opened and used as the stream for logging. On rotating it also\nsets the filename suffix. Rotating happens based on the product of when and\ninterval.

\n

You can use the when to specify the type of interval. The list of possible\nvalues is below. Note that they are not case sensitive.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueType of interval
'S'Seconds
'M'Minutes
'H'Hours
'D'Days
'W'Week day (0=Monday)
'midnight'Roll over at midnight
\n

The system will save old log files by appending extensions to the filename.\nThe extensions are date-and-time based, using the strftime format\n%Y-%m-%d_%H-%M-%S or a leading portion thereof, depending on the\nrollover interval.

\n

When computing the next rollover time for the first time (when the handler\nis created), the last modification time of an existing log file, or else\nthe current time, is used to compute when the next rotation will occur.

\n

If the utc argument is true, times in UTC will be used; otherwise\nlocal time is used.

\n

If backupCount is nonzero, at most backupCount files\nwill be kept, and if more would be created when rollover occurs, the oldest\none is deleted. The deletion logic uses the interval to determine which\nfiles to delete, so changing the interval may leave old files lying around.

\n

If delay is true, then file opening is deferred until the first call to\nemit().

\n

\nChanged in version 2.6: delay and utc were added.

\n
\n
\ndoRollover()
\n
Does a rollover, as described above.
\n\n
\n
\nemit(record)
\n
Outputs the record to the file, catering for rollover as described above.
\n\n
\n\n
\n
\n

15.9.7. SocketHandler

\n

The SocketHandler class, located in the logging.handlers module,\nsends logging output to a network socket. The base class uses a TCP socket.

\n
\n
\nclass logging.handlers.SocketHandler(host, port)
\n

Returns a new instance of the SocketHandler class intended to\ncommunicate with a remote machine whose address is given by host and port.

\n
\n
\nclose()
\n
Closes the socket.
\n\n
\n
\nemit()
\n
Pickles the record’s attribute dictionary and writes it to the socket in\nbinary format. If there is an error with the socket, silently drops the\npacket. If the connection was previously lost, re-establishes the\nconnection. To unpickle the record at the receiving end into a\nLogRecord, use the makeLogRecord() function.
\n\n
\n
\nhandleError()
\n
Handles an error which has occurred during emit(). The most likely\ncause is a lost connection. Closes the socket so that we can retry on the\nnext event.
\n\n
\n
\nmakeSocket()
\n
This is a factory method which allows subclasses to define the precise\ntype of socket they want. The default implementation creates a TCP socket\n(socket.SOCK_STREAM).
\n\n
\n
\nmakePickle(record)
\n

Pickles the record’s attribute dictionary in binary format with a length\nprefix, and returns it ready for transmission across the socket.

\n

Note that pickles aren’t completely secure. If you are concerned about\nsecurity, you may want to override this method to implement a more secure\nmechanism. For example, you can sign pickles using HMAC and then verify\nthem on the receiving end, or alternatively you can disable unpickling of\nglobal objects on the receiving end.

\n
\n\n
\n
\nsend(packet)
\n
Send a pickled string packet to the socket. This function allows for\npartial sends which can happen when the network is busy.
\n\n
\n
\ncreateSocket()
\n

Tries to create a socket; on failure, uses an exponential back-off\nalgorithm. On intial failure, the handler will drop the message it was\ntrying to send. When subsequent messages are handled by the same\ninstance, it will not try connecting until some time has passed. The\ndefault parameters are such that the initial delay is one second, and if\nafter that delay the connection still can’t be made, the handler will\ndouble the delay each time up to a maximum of 30 seconds.

\n

This behaviour is controlled by the following handler attributes:

\n
    \n
  • retryStart (initial delay, defaulting to 1.0 seconds).
  • \n
  • retryFactor (multiplier, defaulting to 2.0).
  • \n
  • retryMax (maximum delay, defaulting to 30.0 seconds).
  • \n
\n

This means that if the remote listener starts up after the handler has\nbeen used, you could lose messages (since the handler won’t even attempt\na connection until the delay has elapsed, but just silently drop messages\nduring the delay period).

\n
\n\n
\n\n
\n
\n

15.9.8. DatagramHandler

\n

The DatagramHandler class, located in the logging.handlers\nmodule, inherits from SocketHandler to support sending logging messages\nover UDP sockets.

\n
\n
\nclass logging.handlers.DatagramHandler(host, port)
\n

Returns a new instance of the DatagramHandler class intended to\ncommunicate with a remote machine whose address is given by host and port.

\n
\n
\nemit()
\n
Pickles the record’s attribute dictionary and writes it to the socket in\nbinary format. If there is an error with the socket, silently drops the\npacket. To unpickle the record at the receiving end into a\nLogRecord, use the makeLogRecord() function.
\n\n
\n
\nmakeSocket()
\n
The factory method of SocketHandler is here overridden to create\na UDP socket (socket.SOCK_DGRAM).
\n\n
\n
\nsend(s)
\n
Send a pickled string to a socket.
\n\n
\n\n
\n
\n

15.9.9. SysLogHandler

\n

The SysLogHandler class, located in the logging.handlers module,\nsupports sending logging messages to a remote or local Unix syslog.

\n
\n
\nclass logging.handlers.SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
\n

Returns a new instance of the SysLogHandler class intended to\ncommunicate with a remote Unix machine whose address is given by address in\nthe form of a (host, port) tuple. If address is not specified,\n('localhost', 514) is used. The address is used to open a socket. An\nalternative to providing a (host, port) tuple is providing an address as a\nstring, for example ‘/dev/log’. In this case, a Unix domain socket is used to\nsend the message to the syslog. If facility is not specified,\nLOG_USER is used. The type of socket opened depends on the\nsocktype argument, which defaults to socket.SOCK_DGRAM and thus\nopens a UDP socket. To open a TCP socket (for use with the newer syslog\ndaemons such as rsyslog), specify a value of socket.SOCK_STREAM.

\n

Note that if your server is not listening on UDP port 514,\nSysLogHandler may appear not to work. In that case, check what\naddress you should be using for a domain socket - it’s system dependent.\nFor example, on Linux it’s usually ‘/dev/log’ but on OS/X it’s\n‘/var/run/syslog’. You’ll need to check your platform and use the\nappropriate address (you may need to do this check at runtime if your\napplication needs to run on several platforms). On Windows, you pretty\nmuch have to use the UDP option.

\n

\nChanged in version 2.7: socktype was added.

\n
\n
\nclose()
\n
Closes the socket to the remote host.
\n\n
\n
\nemit(record)
\n
The record is formatted, and then sent to the syslog server. If exception\ninformation is present, it is not sent to the server.
\n\n
\n
\nencodePriority(facility, priority)
\n

Encodes the facility and priority into an integer. You can pass in strings\nor integers - if strings are passed, internal mapping dictionaries are\nused to convert them to integers.

\n

The symbolic LOG_ values are defined in SysLogHandler and\nmirror the values defined in the sys/syslog.h header file.

\n

Priorities

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Name (string)Symbolic value
alertLOG_ALERT
crit or criticalLOG_CRIT
debugLOG_DEBUG
emerg or panicLOG_EMERG
err or errorLOG_ERR
infoLOG_INFO
noticeLOG_NOTICE
warn or warningLOG_WARNING
\n

Facilities

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Name (string)Symbolic value
authLOG_AUTH
authprivLOG_AUTHPRIV
cronLOG_CRON
daemonLOG_DAEMON
ftpLOG_FTP
kernLOG_KERN
lprLOG_LPR
mailLOG_MAIL
newsLOG_NEWS
syslogLOG_SYSLOG
userLOG_USER
uucpLOG_UUCP
local0LOG_LOCAL0
local1LOG_LOCAL1
local2LOG_LOCAL2
local3LOG_LOCAL3
local4LOG_LOCAL4
local5LOG_LOCAL5
local6LOG_LOCAL6
local7LOG_LOCAL7
\n
\n\n
\n
\nmapPriority(levelname)
\n
Maps a logging level name to a syslog priority name.\nYou may need to override this if you are using custom levels, or\nif the default algorithm is not suitable for your needs. The\ndefault algorithm maps DEBUG, INFO, WARNING, ERROR and\nCRITICAL to the equivalent syslog names, and all other level\nnames to ‘warning’.
\n\n
\n\n
\n
\n

15.9.10. NTEventLogHandler

\n

The NTEventLogHandler class, located in the logging.handlers\nmodule, supports sending logging messages to a local Windows NT, Windows 2000 or\nWindows XP event log. Before you can use it, you need Mark Hammond’s Win32\nextensions for Python installed.

\n
\n
\nclass logging.handlers.NTEventLogHandler(appname, dllname=None, logtype='Application')
\n

Returns a new instance of the NTEventLogHandler class. The appname is\nused to define the application name as it appears in the event log. An\nappropriate registry entry is created using this name. The dllname should give\nthe fully qualified pathname of a .dll or .exe which contains message\ndefinitions to hold in the log (if not specified, 'win32service.pyd' is used\n- this is installed with the Win32 extensions and contains some basic\nplaceholder message definitions. Note that use of these placeholders will make\nyour event logs big, as the entire message source is held in the log. If you\nwant slimmer logs, you have to pass in the name of your own .dll or .exe which\ncontains the message definitions you want to use in the event log). The\nlogtype is one of 'Application', 'System' or 'Security', and\ndefaults to 'Application'.

\n
\n
\nclose()
\n
At this point, you can remove the application name from the registry as a\nsource of event log entries. However, if you do this, you will not be able\nto see the events as you intended in the Event Log Viewer - it needs to be\nable to access the registry to get the .dll name. The current version does\nnot do this.
\n\n
\n
\nemit(record)
\n
Determines the message ID, event category and event type, and then logs\nthe message in the NT event log.
\n\n
\n
\ngetEventCategory(record)
\n
Returns the event category for the record. Override this if you want to\nspecify your own categories. This version returns 0.
\n\n
\n
\ngetEventType(record)
\n
Returns the event type for the record. Override this if you want to\nspecify your own types. This version does a mapping using the handler’s\ntypemap attribute, which is set up in __init__() to a dictionary\nwhich contains mappings for DEBUG, INFO,\nWARNING, ERROR and CRITICAL. If you are using\nyour own levels, you will either need to override this method or place a\nsuitable dictionary in the handler’s typemap attribute.
\n\n
\n
\ngetMessageID(record)
\n
Returns the message ID for the record. If you are using your own messages,\nyou could do this by having the msg passed to the logger being an ID\nrather than a format string. Then, in here, you could use a dictionary\nlookup to get the message ID. This version returns 1, which is the base\nmessage ID in win32service.pyd.
\n\n
\n\n
\n
\n

15.9.11. SMTPHandler

\n

The SMTPHandler class, located in the logging.handlers module,\nsupports sending logging messages to an email address via SMTP.

\n
\n
\nclass logging.handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None)
\n

Returns a new instance of the SMTPHandler class. The instance is\ninitialized with the from and to addresses and subject line of the email.\nThe toaddrs should be a list of strings. To specify a non-standard SMTP\nport, use the (host, port) tuple format for the mailhost argument. If you\nuse a string, the standard SMTP port is used. If your SMTP server requires\nauthentication, you can specify a (username, password) tuple for the\ncredentials argument.

\n

To specify the use of a secure protocol (TLS), pass in a tuple to the\nsecure argument. This will only be used when authentication credentials are\nsupplied. The tuple should be either an empty tuple, or a single-value tuple\nwith the name of a keyfile, or a 2-value tuple with the names of the keyfile\nand certificate file. (This tuple is passed to the\nsmtplib.SMTP.starttls() method.)

\n

\nChanged in version 2.6: credentials was added.

\n

\nChanged in version 2.7: secure was added.

\n
\n
\nemit(record)
\n
Formats the record and sends it to the specified addressees.
\n\n
\n
\ngetSubject(record)
\n
If you want to specify a subject line which is record-dependent, override\nthis method.
\n\n
\n\n
\n
\n

15.9.12. MemoryHandler

\n

The MemoryHandler class, located in the logging.handlers module,\nsupports buffering of logging records in memory, periodically flushing them to a\ntarget handler. Flushing occurs whenever the buffer is full, or when an\nevent of a certain severity or greater is seen.

\n

MemoryHandler is a subclass of the more general\nBufferingHandler, which is an abstract class. This buffers logging\nrecords in memory. Whenever each record is added to the buffer, a check is made\nby calling shouldFlush() to see if the buffer should be flushed. If it\nshould, then flush() is expected to do the needful.

\n
\n
\nclass logging.handlers.BufferingHandler(capacity)
\n

Initializes the handler with a buffer of the specified capacity.

\n
\n
\nemit(record)
\n
Appends the record to the buffer. If shouldFlush() returns true,\ncalls flush() to process the buffer.
\n\n
\n
\nflush()
\n
You can override this to implement custom flushing behavior. This version\njust zaps the buffer to empty.
\n\n
\n
\nshouldFlush(record)
\n
Returns true if the buffer is up to capacity. This method can be\noverridden to implement custom flushing strategies.
\n\n
\n\n
\n
\nclass logging.handlers.MemoryHandler(capacity, flushLevel=ERROR, target=None)
\n

Returns a new instance of the MemoryHandler class. The instance is\ninitialized with a buffer size of capacity. If flushLevel is not specified,\nERROR is used. If no target is specified, the target will need to be\nset using setTarget() before this handler does anything useful.

\n
\n
\nclose()
\n
Calls flush(), sets the target to None and clears the\nbuffer.
\n\n
\n
\nflush()
\n
For a MemoryHandler, flushing means just sending the buffered\nrecords to the target, if there is one. The buffer is also cleared when\nthis happens. Override if you want different behavior.
\n\n
\n
\nsetTarget(target)
\n
\n\n

\nChanged in version 2.6: credentials was added.

Sets the target handler for this handler.

\n

\n
\n
\nshouldFlush(record)
\n
Checks for buffer full or a record at the flushLevel or higher.
\n\n
\n\n
\n
\n

15.9.13. HTTPHandler

\n

The HTTPHandler class, located in the logging.handlers module,\nsupports sending logging messages to a Web server, using either GET or\nPOST semantics.

\n
\n
\nclass logging.handlers.HTTPHandler(host, url, method='GET')
\n

Returns a new instance of the HTTPHandler class. The host can be\nof the form host:port, should you need to use a specific port number.\nIf no method is specified, GET is used.

\n
\n
\nemit(record)
\n
Sends the record to the Web server as a percent-encoded dictionary.
\n\n
\n\n
\n

See also

\n
\n
Module logging
\n
API reference for the logging module.
\n
Module logging.config
\n
Configuration API for the logging module.
\n
\n
\n
\n
", "searchableItems": [ { "name": "logging.FileHandler", "domId": "logging.handlers_logging.FileHandler" }, { "name": "logging.FileHandler.close", "domId": "logging.handlers_logging.FileHandler.close" }, { "name": "logging.FileHandler.emit", "domId": "logging.handlers_logging.FileHandler.emit" }, { "name": "logging.handlers.BufferingHandler", "domId": "logging.handlers_logging.handlers.BufferingHandler" }, { "name": "logging.handlers.BufferingHandler.emit", "domId": "logging.handlers_logging.handlers.BufferingHandler.emit" }, { "name": "logging.handlers.BufferingHandler.flush", "domId": "logging.handlers_logging.handlers.BufferingHandler.flush" }, { "name": "logging.handlers.BufferingHandler.shouldFlush", "domId": "logging.handlers_logging.handlers.BufferingHandler.shouldFlush" }, { "name": "logging.handlers.DatagramHandler", "domId": "logging.handlers_logging.handlers.DatagramHandler" }, { "name": "logging.handlers.DatagramHandler.emit", "domId": "logging.handlers_logging.handlers.DatagramHandler.emit" }, { "name": "logging.handlers.DatagramHandler.makeSocket", "domId": "logging.handlers_logging.handlers.DatagramHandler.makeSocket" }, { "name": "logging.handlers.DatagramHandler.send", "domId": "logging.handlers_logging.handlers.DatagramHandler.send" }, { "name": "logging.handlers.HTTPHandler", "domId": "logging.handlers_logging.handlers.HTTPHandler" }, { "name": "logging.handlers.HTTPHandler.emit", "domId": "logging.handlers_logging.handlers.HTTPHandler.emit" }, { "name": "logging.handlers.MemoryHandler", "domId": "logging.handlers_logging.handlers.MemoryHandler" }, { "name": "logging.handlers.MemoryHandler.close", "domId": "logging.handlers_logging.handlers.MemoryHandler.close" }, { "name": "logging.handlers.MemoryHandler.flush", "domId": "logging.handlers_logging.handlers.MemoryHandler.flush" }, { "name": "logging.handlers.MemoryHandler.setTarget", "domId": "logging.handlers_logging.handlers.MemoryHandler.setTarget" }, { "name": "logging.handlers.MemoryHandler.shouldFlush", "domId": "logging.handlers_logging.handlers.MemoryHandler.shouldFlush" }, { "name": "logging.handlers.NTEventLogHandler", "domId": "logging.handlers_logging.handlers.NTEventLogHandler" }, { "name": "logging.handlers.NTEventLogHandler.close", "domId": "logging.handlers_logging.handlers.NTEventLogHandler.close" }, { "name": "logging.handlers.NTEventLogHandler.emit", "domId": "logging.handlers_logging.handlers.NTEventLogHandler.emit" }, { "name": "logging.handlers.NTEventLogHandler.getEventCategory", "domId": "logging.handlers_logging.handlers.NTEventLogHandler.getEventCategory" }, { "name": "logging.handlers.NTEventLogHandler.getEventType", "domId": "logging.handlers_logging.handlers.NTEventLogHandler.getEventType" }, { "name": "logging.handlers.NTEventLogHandler.getMessageID", "domId": "logging.handlers_logging.handlers.NTEventLogHandler.getMessageID" }, { "name": "logging.handlers.RotatingFileHandler", "domId": "logging.handlers_logging.handlers.RotatingFileHandler" }, { "name": "logging.handlers.RotatingFileHandler.doRollover", "domId": "logging.handlers_logging.handlers.RotatingFileHandler.doRollover" }, { "name": "logging.handlers.RotatingFileHandler.emit", "domId": "logging.handlers_logging.handlers.RotatingFileHandler.emit" }, { "name": "logging.handlers.SMTPHandler", "domId": "logging.handlers_logging.handlers.SMTPHandler" }, { "name": "logging.handlers.SMTPHandler.emit", "domId": "logging.handlers_logging.handlers.SMTPHandler.emit" }, { "name": "logging.handlers.SMTPHandler.getSubject", "domId": "logging.handlers_logging.handlers.SMTPHandler.getSubject" }, { "name": "logging.handlers.SocketHandler", "domId": "logging.handlers_logging.handlers.SocketHandler" }, { "name": "logging.handlers.SocketHandler.close", "domId": "logging.handlers_logging.handlers.SocketHandler.close" }, { "name": "logging.handlers.SocketHandler.createSocket", "domId": "logging.handlers_logging.handlers.SocketHandler.createSocket" }, { "name": "logging.handlers.SocketHandler.emit", "domId": "logging.handlers_logging.handlers.SocketHandler.emit" }, { "name": "logging.handlers.SocketHandler.handleError", "domId": "logging.handlers_logging.handlers.SocketHandler.handleError" }, { "name": "logging.handlers.SocketHandler.makePickle", "domId": "logging.handlers_logging.handlers.SocketHandler.makePickle" }, { "name": "logging.handlers.SocketHandler.makeSocket", "domId": "logging.handlers_logging.handlers.SocketHandler.makeSocket" }, { "name": "logging.handlers.SocketHandler.send", "domId": "logging.handlers_logging.handlers.SocketHandler.send" }, { "name": "logging.handlers.SysLogHandler", "domId": "logging.handlers_logging.handlers.SysLogHandler" }, { "name": "logging.handlers.SysLogHandler.close", "domId": "logging.handlers_logging.handlers.SysLogHandler.close" }, { "name": "logging.handlers.SysLogHandler.emit", "domId": "logging.handlers_logging.handlers.SysLogHandler.emit" }, { "name": "logging.handlers.SysLogHandler.encodePriority", "domId": "logging.handlers_logging.handlers.SysLogHandler.encodePriority" }, { "name": "logging.handlers.SysLogHandler.mapPriority", "domId": "logging.handlers_logging.handlers.SysLogHandler.mapPriority" }, { "name": "logging.handlers.TimedRotatingFileHandler", "domId": "logging.handlers_logging.handlers.TimedRotatingFileHandler" }, { "name": "logging.handlers.TimedRotatingFileHandler.doRollover", "domId": "logging.handlers_logging.handlers.TimedRotatingFileHandler.doRollover" }, { "name": "logging.handlers.TimedRotatingFileHandler.emit", "domId": "logging.handlers_logging.handlers.TimedRotatingFileHandler.emit" }, { "name": "logging.handlers.WatchedFileHandler", "domId": "logging.handlers_logging.handlers.WatchedFileHandler" }, { "name": "logging.handlers.WatchedFileHandler.emit", "domId": "logging.handlers_logging.handlers.WatchedFileHandler.emit" }, { "name": "logging.NullHandler", "domId": "logging.handlers_logging.NullHandler" }, { "name": "logging.NullHandler.createLock", "domId": "logging.handlers_logging.NullHandler.createLock" }, { "name": "logging.NullHandler.emit", "domId": "logging.handlers_logging.NullHandler.emit" }, { "name": "logging.NullHandler.handle", "domId": "logging.handlers_logging.NullHandler.handle" }, { "name": "logging.StreamHandler", "domId": "logging.handlers_logging.StreamHandler" }, { "name": "logging.StreamHandler.emit", "domId": "logging.handlers_logging.StreamHandler.emit" }, { "name": "logging.StreamHandler.flush", "domId": "logging.handlers_logging.StreamHandler.flush" } ] }, { "url": "http://docs.python.org/library/argparse.html", "title": "argparse", "html": "
\n

15.4. argparse — Parser for command-line options, arguments and sub-commands

\n

\nNew in version 2.7.

\n

Source code: Lib/argparse.py

\n
\n

The argparse module makes it easy to write user-friendly command-line\ninterfaces. The program defines what arguments it requires, and argparse\nwill figure out how to parse those out of sys.argv. The argparse\nmodule also automatically generates help and usage messages and issues errors\nwhen users give the program invalid arguments.

\n
\n

15.4.1. Example

\n

The following code is a Python program that takes a list of integers and\nproduces either the sum or the max:

\n
import argparse\n\nparser = argparse.ArgumentParser(description='Process some integers.')\nparser.add_argument('integers', metavar='N', type=int, nargs='+',\n                   help='an integer for the accumulator')\nparser.add_argument('--sum', dest='accumulate', action='store_const',\n                   const=sum, default=max,\n                   help='sum the integers (default: find the max)')\n\nargs = parser.parse_args()\nprint args.accumulate(args.integers)\n
\n
\n

Assuming the Python code above is saved into a file called prog.py, it can\nbe run at the command line and provides useful help messages:

\n
$ prog.py -h\nusage: prog.py [-h] [--sum] N [N ...]\n\nProcess some integers.\n\npositional arguments:\n N           an integer for the accumulator\n\noptional arguments:\n -h, --help  show this help message and exit\n --sum       sum the integers (default: find the max)
\n
\n

When run with the appropriate arguments, it prints either the sum or the max of\nthe command-line integers:

\n
$ prog.py 1 2 3 4\n4\n\n$ prog.py 1 2 3 4 --sum\n10
\n
\n

If invalid arguments are passed in, it will issue an error:

\n
$ prog.py a b c\nusage: prog.py [-h] [--sum] N [N ...]\nprog.py: error: argument N: invalid int value: 'a'
\n
\n

The following sections walk you through this example.

\n
\n

15.4.1.1. Creating a parser

\n

The first step in using the argparse is creating an\nArgumentParser object:

\n
>>> parser = argparse.ArgumentParser(description='Process some integers.')\n
\n
\n

The ArgumentParser object will hold all the information necessary to\nparse the command line into Python data types.

\n
\n
\n

15.4.1.2. Adding arguments

\n

Filling an ArgumentParser with information about program arguments is\ndone by making calls to the add_argument() method.\nGenerally, these calls tell the ArgumentParser how to take the strings\non the command line and turn them into objects. This information is stored and\nused when parse_args() is called. For example:

\n
>>> parser.add_argument('integers', metavar='N', type=int, nargs='+',\n...                     help='an integer for the accumulator')\n>>> parser.add_argument('--sum', dest='accumulate', action='store_const',\n...                     const=sum, default=max,\n...                     help='sum the integers (default: find the max)')\n
\n
\n

Later, calling parse_args() will return an object with\ntwo attributes, integers and accumulate. The integers attribute\nwill be a list of one or more ints, and the accumulate attribute will be\neither the sum() function, if --sum was specified at the command line,\nor the max() function if it was not.

\n
\n
\n

15.4.1.3. Parsing arguments

\n

ArgumentParser parses arguments through the\nparse_args() method. This will inspect the command line,\nconvert each argument to the appropriate type and then invoke the appropriate action.\nIn most cases, this means a simple Namespace object will be built up from\nattributes parsed out of the command line:

\n
>>> parser.parse_args(['--sum', '7', '-1', '42'])\nNamespace(accumulate=<built-in function sum>, integers=[7, -1, 42])\n
\n
\n

In a script, parse_args() will typically be called with no\narguments, and the ArgumentParser will automatically determine the\ncommand-line arguments from sys.argv.

\n
\n
\n
\n

15.4.2. ArgumentParser objects

\n
\n
\nclass argparse.ArgumentParser([description][, epilog][, prog][, usage][, add_help][, argument_default][, parents][, prefix_chars][, conflict_handler][, formatter_class])
\n

Create a new ArgumentParser object. Each parameter has its own more\ndetailed description below, but in short they are:

\n
    \n
  • description - Text to display before the argument help.
  • \n
  • epilog - Text to display after the argument help.
  • \n
  • add_help - Add a -h/–help option to the parser. (default: True)
  • \n
  • argument_default - Set the global default value for arguments.\n(default: None)
  • \n
  • parents - A list of ArgumentParser objects whose arguments should\nalso be included.
  • \n
  • prefix_chars - The set of characters that prefix optional arguments.\n(default: ‘-‘)
  • \n
  • fromfile_prefix_chars - The set of characters that prefix files from\nwhich additional arguments should be read. (default: None)
  • \n
  • formatter_class - A class for customizing the help output.
  • \n
  • conflict_handler - Usually unnecessary, defines strategy for resolving\nconflicting optionals.
  • \n
  • prog - The name of the program (default:\nsys.argv[0])
  • \n
  • usage - The string describing the program usage (default: generated)
  • \n
\n
\n\n

The following sections describe how each of these are used.

\n
\n

15.4.2.1. description

\n

Most calls to the ArgumentParser constructor will use the\ndescription= keyword argument. This argument gives a brief description of\nwhat the program does and how it works. In help messages, the description is\ndisplayed between the command-line usage string and the help messages for the\nvarious arguments:

\n
>>> parser = argparse.ArgumentParser(description='A foo that bars')\n>>> parser.print_help()\nusage: argparse.py [-h]\n\nA foo that bars\n\noptional arguments:\n -h, --help  show this help message and exit\n
\n
\n

By default, the description will be line-wrapped so that it fits within the\ngiven space. To change this behavior, see the formatter_class argument.

\n
\n
\n

15.4.2.2. epilog

\n

Some programs like to display additional description of the program after the\ndescription of the arguments. Such text can be specified using the epilog=\nargument to ArgumentParser:

\n
>>> parser = argparse.ArgumentParser(\n...     description='A foo that bars',\n...     epilog="And that's how you'd foo a bar")\n>>> parser.print_help()\nusage: argparse.py [-h]\n\nA foo that bars\n\noptional arguments:\n -h, --help  show this help message and exit\n\nAnd that's how you'd foo a bar\n
\n
\n

As with the description argument, the epilog= text is by default\nline-wrapped, but this behavior can be adjusted with the formatter_class\nargument to ArgumentParser.

\n
\n
\n

15.4.2.3. add_help

\n

By default, ArgumentParser objects add an option which simply displays\nthe parser’s help message. For example, consider a file named\nmyprogram.py containing the following code:

\n
import argparse\nparser = argparse.ArgumentParser()\nparser.add_argument('--foo', help='foo help')\nargs = parser.parse_args()\n
\n
\n

If -h or --help is supplied at the command line, the ArgumentParser\nhelp will be printed:

\n
$ python myprogram.py --help\nusage: myprogram.py [-h] [--foo FOO]\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo FOO   foo help
\n
\n

Occasionally, it may be useful to disable the addition of this help option.\nThis can be achieved by passing False as the add_help= argument to\nArgumentParser:

\n
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)\n>>> parser.add_argument('--foo', help='foo help')\n>>> parser.print_help()\nusage: PROG [--foo FOO]\n\noptional arguments:\n --foo FOO  foo help\n
\n
\n

The help option is typically -h/--help. The exception to this is\nif the prefix_chars= is specified and does not include -, in\nwhich case -h and --help are not valid options. In\nthis case, the first character in prefix_chars is used to prefix\nthe help options:

\n
>>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='+/')\n>>> parser.print_help()\nusage: PROG [+h]\n\noptional arguments:\n  +h, ++help  show this help message and exit\n
\n
\n
\n
\n

15.4.2.4. prefix_chars

\n

Most command-line options will use - as the prefix, e.g. -f/--foo.\nParsers that need to support different or additional prefix\ncharacters, e.g. for options\nlike +f or /foo, may specify them using the prefix_chars= argument\nto the ArgumentParser constructor:

\n
>>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+')\n>>> parser.add_argument('+f')\n>>> parser.add_argument('++bar')\n>>> parser.parse_args('+f X ++bar Y'.split())\nNamespace(bar='Y', f='X')\n
\n
\n

The prefix_chars= argument defaults to '-'. Supplying a set of\ncharacters that does not include - will cause -f/--foo options to be\ndisallowed.

\n
\n
\n

15.4.2.5. fromfile_prefix_chars

\n

Sometimes, for example when dealing with a particularly long argument lists, it\nmay make sense to keep the list of arguments in a file rather than typing it out\nat the command line. If the fromfile_prefix_chars= argument is given to the\nArgumentParser constructor, then arguments that start with any of the\nspecified characters will be treated as files, and will be replaced by the\narguments they contain. For example:

\n
>>> with open('args.txt', 'w') as fp:\n...    fp.write('-f\\nbar')\n>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')\n>>> parser.add_argument('-f')\n>>> parser.parse_args(['-f', 'foo', '@args.txt'])\nNamespace(f='bar')\n
\n
\n

Arguments read from a file must by default be one per line (but see also\nconvert_arg_line_to_args()) and are treated as if they\nwere in the same place as the original file referencing argument on the command\nline. So in the example above, the expression ['-f', 'foo', '@args.txt']\nis considered equivalent to the expression ['-f', 'foo', '-f', 'bar'].

\n

The fromfile_prefix_chars= argument defaults to None, meaning that\narguments will never be treated as file references.

\n
\n
\n

15.4.2.6. argument_default

\n

Generally, argument defaults are specified either by passing a default to\nadd_argument() or by calling the\nset_defaults() methods with a specific set of name-value\npairs. Sometimes however, it may be useful to specify a single parser-wide\ndefault for arguments. This can be accomplished by passing the\nargument_default= keyword argument to ArgumentParser. For example,\nto globally suppress attribute creation on parse_args()\ncalls, we supply argument_default=SUPPRESS:

\n
>>> parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)\n>>> parser.add_argument('--foo')\n>>> parser.add_argument('bar', nargs='?')\n>>> parser.parse_args(['--foo', '1', 'BAR'])\nNamespace(bar='BAR', foo='1')\n>>> parser.parse_args([])\nNamespace()\n
\n
\n
\n
\n

15.4.2.7. parents

\n

Sometimes, several parsers share a common set of arguments. Rather than\nrepeating the definitions of these arguments, a single parser with all the\nshared arguments and passed to parents= argument to ArgumentParser\ncan be used. The parents= argument takes a list of ArgumentParser\nobjects, collects all the positional and optional actions from them, and adds\nthese actions to the ArgumentParser object being constructed:

\n
>>> parent_parser = argparse.ArgumentParser(add_help=False)\n>>> parent_parser.add_argument('--parent', type=int)\n\n>>> foo_parser = argparse.ArgumentParser(parents=[parent_parser])\n>>> foo_parser.add_argument('foo')\n>>> foo_parser.parse_args(['--parent', '2', 'XXX'])\nNamespace(foo='XXX', parent=2)\n\n>>> bar_parser = argparse.ArgumentParser(parents=[parent_parser])\n>>> bar_parser.add_argument('--bar')\n>>> bar_parser.parse_args(['--bar', 'YYY'])\nNamespace(bar='YYY', parent=None)\n
\n
\n

Note that most parent parsers will specify add_help=False. Otherwise, the\nArgumentParser will see two -h/--help options (one in the parent\nand one in the child) and raise an error.

\n
\n

Note

\n

You must fully initialize the parsers before passing them via parents=.\nIf you change the parent parsers after the child parser, those changes will\nnot be reflected in the child.

\n
\n
\n
\n

15.4.2.8. formatter_class

\n

ArgumentParser objects allow the help formatting to be customized by\nspecifying an alternate formatting class. Currently, there are three such\nclasses:

\n
\n
\nclass argparse.RawDescriptionHelpFormatter
\n
\nclass argparse.RawTextHelpFormatter
\n
\nclass argparse.ArgumentDefaultsHelpFormatter
\n
\n\n

The first two allow more control over how textual descriptions are displayed,\nwhile the last automatically adds information about argument default values.

\n

By default, ArgumentParser objects line-wrap the description and\nepilog texts in command-line help messages:

\n
>>> parser = argparse.ArgumentParser(\n...     prog='PROG',\n...     description='''this description\n...         was indented weird\n...             but that is okay''',\n...     epilog='''\n...             likewise for this epilog whose whitespace will\n...         be cleaned up and whose words will be wrapped\n...         across a couple lines''')\n>>> parser.print_help()\nusage: PROG [-h]\n\nthis description was indented weird but that is okay\n\noptional arguments:\n -h, --help  show this help message and exit\n\nlikewise for this epilog whose whitespace will be cleaned up and whose words\nwill be wrapped across a couple lines\n
\n
\n

Passing RawDescriptionHelpFormatter as formatter_class=\nindicates that description and epilog are already correctly formatted and\nshould not be line-wrapped:

\n
>>> parser = argparse.ArgumentParser(\n...     prog='PROG',\n...     formatter_class=argparse.RawDescriptionHelpFormatter,\n...     description=textwrap.dedent('''\\\n...         Please do not mess up this text!\n...         --------------------------------\n...             I have indented it\n...             exactly the way\n...             I want it\n...         '''))\n>>> parser.print_help()\nusage: PROG [-h]\n\nPlease do not mess up this text!\n--------------------------------\n   I have indented it\n   exactly the way\n   I want it\n\noptional arguments:\n -h, --help  show this help message and exit\n
\n
\n

RawTextHelpFormatter maintains whitespace for all sorts of help text,\nincluding argument descriptions.

\n

The other formatter class available, ArgumentDefaultsHelpFormatter,\nwill add information about the default value of each of the arguments:

\n
>>> parser = argparse.ArgumentParser(\n...     prog='PROG',\n...     formatter_class=argparse.ArgumentDefaultsHelpFormatter)\n>>> parser.add_argument('--foo', type=int, default=42, help='FOO!')\n>>> parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!')\n>>> parser.print_help()\nusage: PROG [-h] [--foo FOO] [bar [bar ...]]\n\npositional arguments:\n bar         BAR! (default: [1, 2, 3])\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo FOO   FOO! (default: 42)\n
\n
\n
\n
\n

15.4.2.9. conflict_handler

\n

ArgumentParser objects do not allow two actions with the same option\nstring. By default, ArgumentParser objects raises an exception if an\nattempt is made to create an argument with an option string that is already in\nuse:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-f', '--foo', help='old foo help')\n>>> parser.add_argument('--foo', help='new foo help')\nTraceback (most recent call last):\n ..\nArgumentError: argument --foo: conflicting option string(s): --foo\n
\n
\n

Sometimes (e.g. when using parents) it may be useful to simply override any\nolder arguments with the same option string. To get this behavior, the value\n'resolve' can be supplied to the conflict_handler= argument of\nArgumentParser:

\n
>>> parser = argparse.ArgumentParser(prog='PROG', conflict_handler='resolve')\n>>> parser.add_argument('-f', '--foo', help='old foo help')\n>>> parser.add_argument('--foo', help='new foo help')\n>>> parser.print_help()\nusage: PROG [-h] [-f FOO] [--foo FOO]\n\noptional arguments:\n -h, --help  show this help message and exit\n -f FOO      old foo help\n --foo FOO   new foo help\n
\n
\n

Note that ArgumentParser objects only remove an action if all of its\noption strings are overridden. So, in the example above, the old -f/--foo\naction is retained as the -f action, because only the --foo option\nstring was overridden.

\n
\n
\n

15.4.2.10. prog

\n

By default, ArgumentParser objects uses sys.argv[0] to determine\nhow to display the name of the program in help messages. This default is almost\nalways desirable because it will make the help messages match how the program was\ninvoked on the command line. For example, consider a file named\nmyprogram.py with the following code:

\n
import argparse\nparser = argparse.ArgumentParser()\nparser.add_argument('--foo', help='foo help')\nargs = parser.parse_args()\n
\n
\n

The help for this program will display myprogram.py as the program name\n(regardless of where the program was invoked from):

\n
$ python myprogram.py --help\nusage: myprogram.py [-h] [--foo FOO]\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo FOO   foo help\n$ cd ..\n$ python subdir\\myprogram.py --help\nusage: myprogram.py [-h] [--foo FOO]\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo FOO   foo help
\n
\n

To change this default behavior, another value can be supplied using the\nprog= argument to ArgumentParser:

\n
>>> parser = argparse.ArgumentParser(prog='myprogram')\n>>> parser.print_help()\nusage: myprogram [-h]\n\noptional arguments:\n -h, --help  show this help message and exit\n
\n
\n

Note that the program name, whether determined from sys.argv[0] or from the\nprog= argument, is available to help messages using the %(prog)s format\nspecifier.

\n
>>> parser = argparse.ArgumentParser(prog='myprogram')\n>>> parser.add_argument('--foo', help='foo of the %(prog)s program')\n>>> parser.print_help()\nusage: myprogram [-h] [--foo FOO]\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo FOO   foo of the myprogram program\n
\n
\n
\n
\n

15.4.2.11. usage

\n

By default, ArgumentParser calculates the usage message from the\narguments it contains:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('--foo', nargs='?', help='foo help')\n>>> parser.add_argument('bar', nargs='+', help='bar help')\n>>> parser.print_help()\nusage: PROG [-h] [--foo [FOO]] bar [bar ...]\n\npositional arguments:\n bar          bar help\n\noptional arguments:\n -h, --help   show this help message and exit\n --foo [FOO]  foo help\n
\n
\n

The default message can be overridden with the usage= keyword argument:

\n
>>> parser = argparse.ArgumentParser(prog='PROG', usage='%(prog)s [options]')\n>>> parser.add_argument('--foo', nargs='?', help='foo help')\n>>> parser.add_argument('bar', nargs='+', help='bar help')\n>>> parser.print_help()\nusage: PROG [options]\n\npositional arguments:\n bar          bar help\n\noptional arguments:\n -h, --help   show this help message and exit\n --foo [FOO]  foo help\n
\n
\n

The %(prog)s format specifier is available to fill in the program name in\nyour usage messages.

\n
\n
\n
\n

15.4.3. The add_argument() method

\n
\n
\nArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
\n

Define how a single command-line argument should be parsed. Each parameter\nhas its own more detailed description below, but in short they are:

\n
    \n
  • name or flags - Either a name or a list of option strings, e.g. foo\nor -f, --foo.
  • \n
  • action - The basic type of action to be taken when this argument is\nencountered at the command line.
  • \n
  • nargs - The number of command-line arguments that should be consumed.
  • \n
  • const - A constant value required by some action and nargs selections.
  • \n
  • default - The value produced if the argument is absent from the\ncommand line.
  • \n
  • type - The type to which the command-line argument should be converted.
  • \n
  • choices - A container of the allowable values for the argument.
  • \n
  • required - Whether or not the command-line option may be omitted\n(optionals only).
  • \n
  • help - A brief description of what the argument does.
  • \n
  • metavar - A name for the argument in usage messages.
  • \n
  • dest - The name of the attribute to be added to the object returned by\nparse_args().
  • \n
\n
\n\n

The following sections describe how each of these are used.

\n
\n

15.4.3.1. name or flags

\n

The add_argument() method must know whether an optional\nargument, like -f or --foo, or a positional argument, like a list of\nfilenames, is expected. The first arguments passed to\nadd_argument() must therefore be either a series of\nflags, or a simple argument name. For example, an optional argument could\nbe created like:

\n
>>> parser.add_argument('-f', '--foo')\n
\n
\n

while a positional argument could be created like:

\n
>>> parser.add_argument('bar')\n
\n
\n

When parse_args() is called, optional arguments will be\nidentified by the - prefix, and the remaining arguments will be assumed to\nbe positional:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-f', '--foo')\n>>> parser.add_argument('bar')\n>>> parser.parse_args(['BAR'])\nNamespace(bar='BAR', foo=None)\n>>> parser.parse_args(['BAR', '--foo', 'FOO'])\nNamespace(bar='BAR', foo='FOO')\n>>> parser.parse_args(['--foo', 'FOO'])\nusage: PROG [-h] [-f FOO] bar\nPROG: error: too few arguments\n
\n
\n
\n
\n

15.4.3.2. action

\n

ArgumentParser objects associate command-line arguments with actions. These\nactions can do just about anything with the command-line arguments associated with\nthem, though most actions simply add an attribute to the object returned by\nparse_args(). The action keyword argument specifies\nhow the command-line arguments should be handled. The supported actions are:

\n
    \n
  • 'store' - This just stores the argument’s value. This is the default\naction. For example:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo')\n>>> parser.parse_args('--foo 1'.split())\nNamespace(foo='1')\n
    \n
    \n
  • \n
  • 'store_const' - This stores the value specified by the const keyword\nargument. (Note that the const keyword argument defaults to the rather\nunhelpful None.) The 'store_const' action is most commonly used with\noptional arguments that specify some sort of flag. For example:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', action='store_const', const=42)\n>>> parser.parse_args('--foo'.split())\nNamespace(foo=42)\n
    \n
    \n
  • \n
  • 'store_true' and 'store_false' - These are special cases of\n'store_const' using for storing the values True and False\nrespectively. In addition, they create default values of False and True\nrespectively. For example:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', action='store_true')\n>>> parser.add_argument('--bar', action='store_false')\n>>> parser.add_argument('--baz', action='store_false')\n>>> parser.parse_args('--foo --bar'.split())\nNamespace(bar=False, baz=True, foo=True)\n
    \n
    \n
  • \n
  • 'append' - This stores a list, and appends each argument value to the\nlist. This is useful to allow an option to be specified multiple times.\nExample usage:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', action='append')\n>>> parser.parse_args('--foo 1 --foo 2'.split())\nNamespace(foo=['1', '2'])\n
    \n
    \n
  • \n
  • 'append_const' - This stores a list, and appends the value specified by\nthe const keyword argument to the list. (Note that the const keyword\nargument defaults to None.) The 'append_const' action is typically\nuseful when multiple arguments need to store constants to the same list. For\nexample:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--str', dest='types', action='append_const', const=str)\n>>> parser.add_argument('--int', dest='types', action='append_const', const=int)\n>>> parser.parse_args('--str --int'.split())\nNamespace(types=[<type 'str'>, <type 'int'>])\n
    \n
    \n
  • \n
  • 'count' - This counts the number of times a keyword argument occurs. For\nexample, this is useful for increasing verbosity levels:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--verbose', '-v', action='count')\n>>> parser.parse_args('-vvv'.split())\nNamespace(verbose=3)\n
    \n
    \n
  • \n
  • 'help' - This prints a complete help message for all the options in the\ncurrent parser and then exits. By default a help action is automatically\nadded to the parser. See ArgumentParser for details of how the\noutput is created.

    \n
  • \n
  • 'version' - This expects a version= keyword argument in the\nadd_argument() call, and prints version information\nand exits when invoked.

    \n
    >>> import argparse\n>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')\n>>> parser.parse_args(['--version'])\nPROG 2.0\n
    \n
    \n
  • \n
\n

You can also specify an arbitrary action by passing an object that implements\nthe Action API. The easiest way to do this is to extend\nargparse.Action, supplying an appropriate __call__ method. The\n__call__ method should accept four parameters:

\n
    \n
  • parser - The ArgumentParser object which contains this action.
  • \n
  • namespace - The Namespace object that will be returned by\nparse_args(). Most actions add an attribute to this\nobject.
  • \n
  • values - The associated command-line arguments, with any type conversions\napplied. (Type conversions are specified with the type keyword argument to\nadd_argument().
  • \n
  • option_string - The option string that was used to invoke this action.\nThe option_string argument is optional, and will be absent if the action\nis associated with a positional argument.
  • \n
\n

An example of a custom action:

\n
>>> class FooAction(argparse.Action):\n...     def __call__(self, parser, namespace, values, option_string=None):\n...         print '%r %r %r'  (namespace, values, option_string)\n...         setattr(namespace, self.dest, values)\n...\n>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', action=FooAction)\n>>> parser.add_argument('bar', action=FooAction)\n>>> args = parser.parse_args('1 --foo 2'.split())\nNamespace(bar=None, foo=None) '1' None\nNamespace(bar='1', foo=None) '2' '--foo'\n>>> args\nNamespace(bar='1', foo='2')\n
\n
\n
\n
\n

15.4.3.3. nargs

\n

ArgumentParser objects usually associate a single command-line argument with a\nsingle action to be taken. The nargs keyword argument associates a\ndifferent number of command-line arguments with a single action. The supported\nvalues are:

\n
    \n
  • N (an integer). N arguments from the command line will be gathered together into a\nlist. For example:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', nargs=2)\n>>> parser.add_argument('bar', nargs=1)\n>>> parser.parse_args('c --foo a b'.split())\nNamespace(bar=['c'], foo=['a', 'b'])\n
    \n
    \n

    Note that nargs=1 produces a list of one item. This is different from\nthe default, in which the item is produced by itself.

    \n
  • \n
  • '?'. One argument will be consumed from the command line if possible, and\nproduced as a single item. If no command-line argument is present, the value from\ndefault will be produced. Note that for optional arguments, there is an\nadditional case - the option string is present but not followed by a\ncommand-line argument. In this case the value from const will be produced. Some\nexamples to illustrate this:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', nargs='?', const='c', default='d')\n>>> parser.add_argument('bar', nargs='?', default='d')\n>>> parser.parse_args('XX --foo YY'.split())\nNamespace(bar='XX', foo='YY')\n>>> parser.parse_args('XX --foo'.split())\nNamespace(bar='XX', foo='c')\n>>> parser.parse_args(''.split())\nNamespace(bar='d', foo='d')\n
    \n
    \n

    One of the more common uses of nargs='?' is to allow optional input and\noutput files:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),\n...                     default=sys.stdin)\n>>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),\n...                     default=sys.stdout)\n>>> parser.parse_args(['input.txt', 'output.txt'])\nNamespace(infile=<open file 'input.txt', mode 'r' at 0x...>,\n          outfile=<open file 'output.txt', mode 'w' at 0x...>)\n>>> parser.parse_args([])\nNamespace(infile=<open file '<stdin>', mode 'r' at 0x...>,\n          outfile=<open file '<stdout>', mode 'w' at 0x...>)\n
    \n
    \n
  • \n
  • '*'. All command-line arguments present are gathered into a list. Note that\nit generally doesn’t make much sense to have more than one positional argument\nwith nargs='*', but multiple optional arguments with nargs='*' is\npossible. For example:

    \n
    >>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', nargs='*')\n>>> parser.add_argument('--bar', nargs='*')\n>>> parser.add_argument('baz', nargs='*')\n>>> parser.parse_args('a b --foo x y --bar 1 2'.split())\nNamespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y'])\n
    \n
    \n
  • \n
  • '+'. Just like '*', all command-line args present are gathered into a\nlist. Additionally, an error message will be generated if there wasn’t at\nleast one command-line argument present. For example:

    \n
    >>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('foo', nargs='+')\n>>> parser.parse_args('a b'.split())\nNamespace(foo=['a', 'b'])\n>>> parser.parse_args(''.split())\nusage: PROG [-h] foo [foo ...]\nPROG: error: too few arguments\n
    \n
    \n
  • \n
\n

If the nargs keyword argument is not provided, the number of arguments consumed\nis determined by the action. Generally this means a single command-line argument\nwill be consumed and a single item (not a list) will be produced.

\n
\n
\n

15.4.3.4. const

\n

The const argument of add_argument() is used to hold\nconstant values that are not read from the command line but are required for\nthe various ArgumentParser actions. The two most common uses of it are:

\n
    \n
  • When add_argument() is called with\naction='store_const' or action='append_const'. These actions add the\nconst value to one of the attributes of the object returned by parse_args(). See the action description for examples.
  • \n
  • When add_argument() is called with option strings\n(like -f or --foo) and nargs='?'. This creates an optional\nargument that can be followed by zero or one command-line arguments.\nWhen parsing the command line, if the option string is encountered with no\ncommand-line argument following it, the value of const will be assumed instead.\nSee the nargs description for examples.
  • \n
\n

The const keyword argument defaults to None.

\n
\n
\n

15.4.3.5. default

\n

All optional arguments and some positional arguments may be omitted at the\ncommand line. The default keyword argument of\nadd_argument(), whose value defaults to None,\nspecifies what value should be used if the command-line argument is not present.\nFor optional arguments, the default value is used when the option string\nwas not present at the command line:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', default=42)\n>>> parser.parse_args('--foo 2'.split())\nNamespace(foo='2')\n>>> parser.parse_args(''.split())\nNamespace(foo=42)\n
\n
\n

For positional arguments with nargs equal to ? or *, the default value\nis used when no command-line argument was present:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('foo', nargs='?', default=42)\n>>> parser.parse_args('a'.split())\nNamespace(foo='a')\n>>> parser.parse_args(''.split())\nNamespace(foo=42)\n
\n
\n

Providing default=argparse.SUPPRESS causes no attribute to be added if the\ncommand-line argument was not present.:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', default=argparse.SUPPRESS)\n>>> parser.parse_args([])\nNamespace()\n>>> parser.parse_args(['--foo', '1'])\nNamespace(foo='1')\n
\n
\n
\n
\n

15.4.3.6. type

\n

By default, ArgumentParser objects read command-line arguments in as simple\nstrings. However, quite often the command-line string should instead be\ninterpreted as another type, like a float or int. The\ntype keyword argument of add_argument() allows any\nnecessary type-checking and type conversions to be performed. Common built-in\ntypes and functions can be used directly as the value of the type argument:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('foo', type=int)\n>>> parser.add_argument('bar', type=file)\n>>> parser.parse_args('2 temp.txt'.split())\nNamespace(bar=<open file 'temp.txt', mode 'r' at 0x...>, foo=2)\n
\n
\n

To ease the use of various types of files, the argparse module provides the\nfactory FileType which takes the mode= and bufsize= arguments of the\nfile object. For example, FileType('w') can be used to create a\nwritable file:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('bar', type=argparse.FileType('w'))\n>>> parser.parse_args(['out.txt'])\nNamespace(bar=<open file 'out.txt', mode 'w' at 0x...>)\n
\n
\n

type= can take any callable that takes a single string argument and returns\nthe converted value:

\n
>>> def perfect_square(string):\n...     value = int(string)\n...     sqrt = math.sqrt(value)\n...     if sqrt != int(sqrt):\n...         msg = "%r is not a perfect square"  string\n...         raise argparse.ArgumentTypeError(msg)\n...     return value\n...\n>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('foo', type=perfect_square)\n>>> parser.parse_args('9'.split())\nNamespace(foo=9)\n>>> parser.parse_args('7'.split())\nusage: PROG [-h] foo\nPROG: error: argument foo: '7' is not a perfect square\n
\n
\n

The choices keyword argument may be more convenient for type checkers that\nsimply check against a range of values:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('foo', type=int, choices=xrange(5, 10))\n>>> parser.parse_args('7'.split())\nNamespace(foo=7)\n>>> parser.parse_args('11'.split())\nusage: PROG [-h] {5,6,7,8,9}\nPROG: error: argument foo: invalid choice: 11 (choose from 5, 6, 7, 8, 9)\n
\n
\n

See the choices section for more details.

\n
\n
\n

15.4.3.7. choices

\n

Some command-line arguments should be selected from a restricted set of values.\nThese can be handled by passing a container object as the choices keyword\nargument to add_argument(). When the command line is\nparsed, argument values will be checked, and an error message will be displayed if\nthe argument was not one of the acceptable values:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('foo', choices='abc')\n>>> parser.parse_args('c'.split())\nNamespace(foo='c')\n>>> parser.parse_args('X'.split())\nusage: PROG [-h] {a,b,c}\nPROG: error: argument foo: invalid choice: 'X' (choose from 'a', 'b', 'c')\n
\n
\n

Note that inclusion in the choices container is checked after any type\nconversions have been performed, so the type of the objects in the choices\ncontainer should match the type specified:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('foo', type=complex, choices=[1, 1j])\n>>> parser.parse_args('1j'.split())\nNamespace(foo=1j)\n>>> parser.parse_args('-- -4'.split())\nusage: PROG [-h] {1,1j}\nPROG: error: argument foo: invalid choice: (-4+0j) (choose from 1, 1j)\n
\n
\n

Any object that supports the in operator can be passed as the choices\nvalue, so dict objects, set objects, custom containers,\netc. are all supported.

\n
\n
\n

15.4.3.8. required

\n

In general, the argparse module assumes that flags like -f and --bar\nindicate optional arguments, which can always be omitted at the command line.\nTo make an option required, True can be specified for the required=\nkeyword argument to add_argument():

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', required=True)\n>>> parser.parse_args(['--foo', 'BAR'])\nNamespace(foo='BAR')\n>>> parser.parse_args([])\nusage: argparse.py [-h] [--foo FOO]\nargparse.py: error: option --foo is required\n
\n
\n

As the example shows, if an option is marked as required,\nparse_args() will report an error if that option is not\npresent at the command line.

\n
\n

Note

\n

Required options are generally considered bad form because users expect\noptions to be optional, and thus they should be avoided when possible.

\n
\n
\n
\n

15.4.3.9. help

\n

The help value is a string containing a brief description of the argument.\nWhen a user requests help (usually by using -h or --help at the\ncommand line), these help descriptions will be displayed with each\nargument:

\n
>>> parser = argparse.ArgumentParser(prog='frobble')\n>>> parser.add_argument('--foo', action='store_true',\n...         help='foo the bars before frobbling')\n>>> parser.add_argument('bar', nargs='+',\n...         help='one of the bars to be frobbled')\n>>> parser.parse_args('-h'.split())\nusage: frobble [-h] [--foo] bar [bar ...]\n\npositional arguments:\n bar     one of the bars to be frobbled\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo   foo the bars before frobbling\n
\n
\n

The help strings can include various format specifiers to avoid repetition\nof things like the program name or the argument default. The available\nspecifiers include the program name, %(prog)s and most keyword arguments to\nadd_argument(), e.g. %(default)s, %(type)s, etc.:

\n
>>> parser = argparse.ArgumentParser(prog='frobble')\n>>> parser.add_argument('bar', nargs='?', type=int, default=42,\n...         help='the bar to %(prog)s (default: %(default)s)')\n>>> parser.print_help()\nusage: frobble [-h] [bar]\n\npositional arguments:\n bar     the bar to frobble (default: 42)\n\noptional arguments:\n -h, --help  show this help message and exit\n
\n
\n

argparse supports silencing the help entry for certain options, by\nsetting the help value to argparse.SUPPRESS:

\n
>>> parser = argparse.ArgumentParser(prog='frobble')\n>>> parser.add_argument('--foo', help=argparse.SUPPRESS)\n>>> parser.print_help()\nusage: frobble [-h]\n\noptional arguments:\n  -h, --help  show this help message and exit\n
\n
\n
\n
\n

15.4.3.10. metavar

\n

When ArgumentParser generates help messages, it need some way to refer\nto each expected argument. By default, ArgumentParser objects use the dest\nvalue as the “name” of each object. By default, for positional argument\nactions, the dest value is used directly, and for optional argument actions,\nthe dest value is uppercased. So, a single positional argument with\ndest='bar' will be referred to as bar. A single\noptional argument --foo that should be followed by a single command-line argument\nwill be referred to as FOO. An example:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo')\n>>> parser.add_argument('bar')\n>>> parser.parse_args('X --foo Y'.split())\nNamespace(bar='X', foo='Y')\n>>> parser.print_help()\nusage:  [-h] [--foo FOO] bar\n\npositional arguments:\n bar\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo FOO\n
\n
\n

An alternative name can be specified with metavar:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', metavar='YYY')\n>>> parser.add_argument('bar', metavar='XXX')\n>>> parser.parse_args('X --foo Y'.split())\nNamespace(bar='X', foo='Y')\n>>> parser.print_help()\nusage:  [-h] [--foo YYY] XXX\n\npositional arguments:\n XXX\n\noptional arguments:\n -h, --help  show this help message and exit\n --foo YYY\n
\n
\n

Note that metavar only changes the displayed name - the name of the\nattribute on the parse_args() object is still determined\nby the dest value.

\n

Different values of nargs may cause the metavar to be used multiple times.\nProviding a tuple to metavar specifies a different display for each of the\narguments:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-x', nargs=2)\n>>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz'))\n>>> parser.print_help()\nusage: PROG [-h] [-x X X] [--foo bar baz]\n\noptional arguments:\n -h, --help     show this help message and exit\n -x X X\n --foo bar baz\n
\n
\n
\n
\n

15.4.3.11. dest

\n

Most ArgumentParser actions add some value as an attribute of the\nobject returned by parse_args(). The name of this\nattribute is determined by the dest keyword argument of\nadd_argument(). For positional argument actions,\ndest is normally supplied as the first argument to\nadd_argument():

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('bar')\n>>> parser.parse_args('XXX'.split())\nNamespace(bar='XXX')\n
\n
\n

For optional argument actions, the value of dest is normally inferred from\nthe option strings. ArgumentParser generates the value of dest by\ntaking the first long option string and stripping away the initial --\nstring. If no long option strings were supplied, dest will be derived from\nthe first short option string by stripping the initial - character. Any\ninternal - characters will be converted to _ characters to make sure\nthe string is a valid attribute name. The examples below illustrate this\nbehavior:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('-f', '--foo-bar', '--foo')\n>>> parser.add_argument('-x', '-y')\n>>> parser.parse_args('-f 1 -x 2'.split())\nNamespace(foo_bar='1', x='2')\n>>> parser.parse_args('--foo 1 -y 2'.split())\nNamespace(foo_bar='1', x='2')\n
\n
\n

dest allows a custom attribute name to be provided:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', dest='bar')\n>>> parser.parse_args('--foo XXX'.split())\nNamespace(bar='XXX')\n
\n
\n
\n
\n
\n

15.4.4. The parse_args() method

\n
\n
\nArgumentParser.parse_args(args=None, namespace=None)
\n

Convert argument strings to objects and assign them as attributes of the\nnamespace. Return the populated namespace.

\n

Previous calls to add_argument() determine exactly what objects are\ncreated and how they are assigned. See the documentation for\nadd_argument() for details.

\n

By default, the argument strings are taken from sys.argv, and a new empty\nNamespace object is created for the attributes.

\n
\n\n
\n

15.4.4.1. Option value syntax

\n

The parse_args() method supports several ways of\nspecifying the value of an option (if it takes one). In the simplest case, the\noption and its value are passed as two separate arguments:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-x')\n>>> parser.add_argument('--foo')\n>>> parser.parse_args('-x X'.split())\nNamespace(foo=None, x='X')\n>>> parser.parse_args('--foo FOO'.split())\nNamespace(foo='FOO', x=None)\n
\n
\n

For long options (options with names longer than a single character), the option\nand value can also be passed as a single command-line argument, using = to\nseparate them:

\n
>>> parser.parse_args('--foo=FOO'.split())\nNamespace(foo='FOO', x=None)\n
\n
\n

For short options (options only one character long), the option and its value\ncan be concatenated:

\n
>>> parser.parse_args('-xX'.split())\nNamespace(foo=None, x='X')\n
\n
\n

Several short options can be joined together, using only a single - prefix,\nas long as only the last option (or none of them) requires a value:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-x', action='store_true')\n>>> parser.add_argument('-y', action='store_true')\n>>> parser.add_argument('-z')\n>>> parser.parse_args('-xyzZ'.split())\nNamespace(x=True, y=True, z='Z')\n
\n
\n
\n
\n

15.4.4.2. Invalid arguments

\n

While parsing the command line, parse_args() checks for a\nvariety of errors, including ambiguous options, invalid types, invalid options,\nwrong number of positional arguments, etc. When it encounters such an error,\nit exits and prints the error along with a usage message:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('--foo', type=int)\n>>> parser.add_argument('bar', nargs='?')\n\n>>> # invalid type\n>>> parser.parse_args(['--foo', 'spam'])\nusage: PROG [-h] [--foo FOO] [bar]\nPROG: error: argument --foo: invalid int value: 'spam'\n\n>>> # invalid option\n>>> parser.parse_args(['--bar'])\nusage: PROG [-h] [--foo FOO] [bar]\nPROG: error: no such option: --bar\n\n>>> # wrong number of arguments\n>>> parser.parse_args(['spam', 'badger'])\nusage: PROG [-h] [--foo FOO] [bar]\nPROG: error: extra arguments found: badger\n
\n
\n
\n
\n

15.4.4.3. Arguments containing -

\n

The parse_args() method attempts to give errors whenever\nthe user has clearly made a mistake, but some situations are inherently\nambiguous. For example, the command-line argument -1 could either be an\nattempt to specify an option or an attempt to provide a positional argument.\nThe parse_args() method is cautious here: positional\narguments may only begin with - if they look like negative numbers and\nthere are no options in the parser that look like negative numbers:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-x')\n>>> parser.add_argument('foo', nargs='?')\n\n>>> # no negative number options, so -1 is a positional argument\n>>> parser.parse_args(['-x', '-1'])\nNamespace(foo=None, x='-1')\n\n>>> # no negative number options, so -1 and -5 are positional arguments\n>>> parser.parse_args(['-x', '-1', '-5'])\nNamespace(foo='-5', x='-1')\n\n>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-1', dest='one')\n>>> parser.add_argument('foo', nargs='?')\n\n>>> # negative number options present, so -1 is an option\n>>> parser.parse_args(['-1', 'X'])\nNamespace(foo=None, one='X')\n\n>>> # negative number options present, so -2 is an option\n>>> parser.parse_args(['-2'])\nusage: PROG [-h] [-1 ONE] [foo]\nPROG: error: no such option: -2\n\n>>> # negative number options present, so both -1s are options\n>>> parser.parse_args(['-1', '-1'])\nusage: PROG [-h] [-1 ONE] [foo]\nPROG: error: argument -1: expected one argument\n
\n
\n

If you have positional arguments that must begin with - and don’t look\nlike negative numbers, you can insert the pseudo-argument '--' which tells\nparse_args() that everything after that is a positional\nargument:

\n
>>> parser.parse_args(['--', '-f'])\nNamespace(foo='-f', one=None)\n
\n
\n
\n
\n

15.4.4.4. Argument abbreviations

\n

The parse_args() method allows long options to be\nabbreviated if the abbreviation is unambiguous:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('-bacon')\n>>> parser.add_argument('-badger')\n>>> parser.parse_args('-bac MMM'.split())\nNamespace(bacon='MMM', badger=None)\n>>> parser.parse_args('-bad WOOD'.split())\nNamespace(bacon=None, badger='WOOD')\n>>> parser.parse_args('-ba BA'.split())\nusage: PROG [-h] [-bacon BACON] [-badger BADGER]\nPROG: error: ambiguous option: -ba could match -badger, -bacon\n
\n
\n

An error is produced for arguments that could produce more than one options.

\n
\n
\n

15.4.4.5. Beyond sys.argv

\n

Sometimes it may be useful to have an ArgumentParser parse arguments other than those\nof sys.argv. This can be accomplished by passing a list of strings to\nparse_args(). This is useful for testing at the\ninteractive prompt:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument(\n...     'integers', metavar='int', type=int, choices=xrange(10),\n...  nargs='+', help='an integer in the range 0..9')\n>>> parser.add_argument(\n...     '--sum', dest='accumulate', action='store_const', const=sum,\n...   default=max, help='sum the integers (default: find the max)')\n>>> parser.parse_args(['1', '2', '3', '4'])\nNamespace(accumulate=<built-in function max>, integers=[1, 2, 3, 4])\n>>> parser.parse_args('1 2 3 4 --sum'.split())\nNamespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])\n
\n
\n
\n
\n

15.4.4.6. The Namespace object

\n
\n
\nclass argparse.Namespace
\n
Simple class used by default by parse_args() to create\nan object holding attributes and return it.
\n\n

This class is deliberately simple, just an object subclass with a\nreadable string representation. If you prefer to have dict-like view of the\nattributes, you can use the standard Python idiom, vars():

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo')\n>>> args = parser.parse_args(['--foo', 'BAR'])\n>>> vars(args)\n{'foo': 'BAR'}\n
\n
\n

It may also be useful to have an ArgumentParser assign attributes to an\nalready existing object, rather than a new Namespace object. This can\nbe achieved by specifying the namespace= keyword argument:

\n
>>> class C(object):\n...     pass\n...\n>>> c = C()\n>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo')\n>>> parser.parse_args(args=['--foo', 'BAR'], namespace=c)\n>>> c.foo\n'BAR'\n
\n
\n
\n
\n
\n

15.4.5. Other utilities

\n
\n

15.4.5.1. Sub-commands

\n
\n
\nArgumentParser.add_subparsers()
\n

Many programs split up their functionality into a number of sub-commands,\nfor example, the svn program can invoke sub-commands like svn\ncheckout, svn update, and svn commit. Splitting up functionality\nthis way can be a particularly good idea when a program performs several\ndifferent functions which require different kinds of command-line arguments.\nArgumentParser supports the creation of such sub-commands with the\nadd_subparsers() method. The add_subparsers() method is normally\ncalled with no arguments and returns an special action object. This object\nhas a single method, add_parser(), which takes a\ncommand name and any ArgumentParser constructor arguments, and\nreturns an ArgumentParser object that can be modified as usual.

\n

Some example usage:

\n
>>> # create the top-level parser\n>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> parser.add_argument('--foo', action='store_true', help='foo help')\n>>> subparsers = parser.add_subparsers(help='sub-command help')\n>>>\n>>> # create the parser for the "a" command\n>>> parser_a = subparsers.add_parser('a', help='a help')\n>>> parser_a.add_argument('bar', type=int, help='bar help')\n>>>\n>>> # create the parser for the "b" command\n>>> parser_b = subparsers.add_parser('b', help='b help')\n>>> parser_b.add_argument('--baz', choices='XYZ', help='baz help')\n>>>\n>>> # parse some argument lists\n>>> parser.parse_args(['a', '12'])\nNamespace(bar=12, foo=False)\n>>> parser.parse_args(['--foo', 'b', '--baz', 'Z'])\nNamespace(baz='Z', foo=True)\n
\n
\n

Note that the object returned by parse_args() will only contain\nattributes for the main parser and the subparser that was selected by the\ncommand line (and not any other subparsers). So in the example above, when\nthe a command is specified, only the foo and bar attributes are\npresent, and when the b command is specified, only the foo and\nbaz attributes are present.

\n

Similarly, when a help message is requested from a subparser, only the help\nfor that particular parser will be printed. The help message will not\ninclude parent parser or sibling parser messages. (A help message for each\nsubparser command, however, can be given by supplying the help= argument\nto add_parser() as above.)

\n
>>> parser.parse_args(['--help'])\nusage: PROG [-h] [--foo] {a,b} ...\n\npositional arguments:\n  {a,b}   sub-command help\na     a help\nb     b help\n\noptional arguments:\n  -h, --help  show this help message and exit\n  --foo   foo help\n\n>>> parser.parse_args(['a', '--help'])\nusage: PROG a [-h] bar\n\npositional arguments:\n  bar     bar help\n\noptional arguments:\n  -h, --help  show this help message and exit\n\n>>> parser.parse_args(['b', '--help'])\nusage: PROG b [-h] [--baz {X,Y,Z}]\n\noptional arguments:\n  -h, --help     show this help message and exit\n  --baz {X,Y,Z}  baz help\n
\n
\n

The add_subparsers() method also supports title and description\nkeyword arguments. When either is present, the subparser’s commands will\nappear in their own group in the help output. For example:

\n
>>> parser = argparse.ArgumentParser()\n>>> subparsers = parser.add_subparsers(title='subcommands',\n...                                    description='valid subcommands',\n...                                    help='additional help')\n>>> subparsers.add_parser('foo')\n>>> subparsers.add_parser('bar')\n>>> parser.parse_args(['-h'])\nusage:  [-h] {foo,bar} ...\n\noptional arguments:\n  -h, --help  show this help message and exit\n\nsubcommands:\n  valid subcommands\n\n  {foo,bar}   additional help\n
\n
\n

One particularly effective way of handling sub-commands is to combine the use\nof the add_subparsers() method with calls to set_defaults() so\nthat each subparser knows which Python function it should execute. For\nexample:

\n
>>> # sub-command functions\n>>> def foo(args):\n...     print args.x * args.y\n...\n>>> def bar(args):\n...     print '((%s))'  args.z\n...\n>>> # create the top-level parser\n>>> parser = argparse.ArgumentParser()\n>>> subparsers = parser.add_subparsers()\n>>>\n>>> # create the parser for the "foo" command\n>>> parser_foo = subparsers.add_parser('foo')\n>>> parser_foo.add_argument('-x', type=int, default=1)\n>>> parser_foo.add_argument('y', type=float)\n>>> parser_foo.set_defaults(func=foo)\n>>>\n>>> # create the parser for the "bar" command\n>>> parser_bar = subparsers.add_parser('bar')\n>>> parser_bar.add_argument('z')\n>>> parser_bar.set_defaults(func=bar)\n>>>\n>>> # parse the args and call whatever function was selected\n>>> args = parser.parse_args('foo 1 -x 2'.split())\n>>> args.func(args)\n2.0\n>>>\n>>> # parse the args and call whatever function was selected\n>>> args = parser.parse_args('bar XYZYX'.split())\n>>> args.func(args)\n((XYZYX))\n
\n
\n

This way, you can let parse_args() does the job of calling the\nappropriate function after argument parsing is complete. Associating\nfunctions with actions like this is typically the easiest way to handle the\ndifferent actions for each of your subparsers. However, if it is necessary\nto check the name of the subparser that was invoked, the dest keyword\nargument to the add_subparsers() call will work:

\n
>>> parser = argparse.ArgumentParser()\n>>> subparsers = parser.add_subparsers(dest='subparser_name')\n>>> subparser1 = subparsers.add_parser('1')\n>>> subparser1.add_argument('-x')\n>>> subparser2 = subparsers.add_parser('2')\n>>> subparser2.add_argument('y')\n>>> parser.parse_args(['2', 'frobble'])\nNamespace(subparser_name='2', y='frobble')\n
\n
\n
\n\n
\n
\n

15.4.5.2. FileType objects

\n
\n
\nclass argparse.FileType(mode='r', bufsize=None)
\n

The FileType factory creates objects that can be passed to the type\nargument of ArgumentParser.add_argument(). Arguments that have\nFileType objects as their type will open command-line arguments as files\nwith the requested modes and buffer sizes:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--output', type=argparse.FileType('wb', 0))\n>>> parser.parse_args(['--output', 'out'])\nNamespace(output=<open file 'out', mode 'wb' at 0x...>)\n
\n
\n

FileType objects understand the pseudo-argument '-' and automatically\nconvert this into sys.stdin for readable FileType objects and\nsys.stdout for writable FileType objects:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('infile', type=argparse.FileType('r'))\n>>> parser.parse_args(['-'])\nNamespace(infile=<open file '<stdin>', mode 'r' at 0x...>)\n
\n
\n
\n\n
\n
\n

15.4.5.3. Argument groups

\n
\n
\nArgumentParser.add_argument_group(title=None, description=None)
\n

By default, ArgumentParser groups command-line arguments into\n“positional arguments” and “optional arguments” when displaying help\nmessages. When there is a better conceptual grouping of arguments than this\ndefault one, appropriate groups can be created using the\nadd_argument_group() method:

\n
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)\n>>> group = parser.add_argument_group('group')\n>>> group.add_argument('--foo', help='foo help')\n>>> group.add_argument('bar', help='bar help')\n>>> parser.print_help()\nusage: PROG [--foo FOO] bar\n\ngroup:\n  bar    bar help\n  --foo FOO  foo help\n
\n
\n

The add_argument_group() method returns an argument group object which\nhas an add_argument() method just like a regular\nArgumentParser. When an argument is added to the group, the parser\ntreats it just like a normal argument, but displays the argument in a\nseparate group for help messages. The add_argument_group() method\naccepts title and description arguments which can be used to\ncustomize this display:

\n
>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)\n>>> group1 = parser.add_argument_group('group1', 'group1 description')\n>>> group1.add_argument('foo', help='foo help')\n>>> group2 = parser.add_argument_group('group2', 'group2 description')\n>>> group2.add_argument('--bar', help='bar help')\n>>> parser.print_help()\nusage: PROG [--bar BAR] foo\n\ngroup1:\n  group1 description\n\n  foo    foo help\n\ngroup2:\n  group2 description\n\n  --bar BAR  bar help\n
\n
\n

Note that any arguments not your user defined groups will end up back in the\nusual “positional arguments” and “optional arguments” sections.

\n
\n\n
\n
\n

15.4.5.4. Mutual exclusion

\n
\n
\nargparse.add_mutually_exclusive_group(required=False)
\n

Create a mutually exclusive group. argparse will make sure that only\none of the arguments in the mutually exclusive group was present on the\ncommand line:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> group = parser.add_mutually_exclusive_group()\n>>> group.add_argument('--foo', action='store_true')\n>>> group.add_argument('--bar', action='store_false')\n>>> parser.parse_args(['--foo'])\nNamespace(bar=True, foo=True)\n>>> parser.parse_args(['--bar'])\nNamespace(bar=False, foo=False)\n>>> parser.parse_args(['--foo', '--bar'])\nusage: PROG [-h] [--foo | --bar]\nPROG: error: argument --bar: not allowed with argument --foo\n
\n
\n

The add_mutually_exclusive_group() method also accepts a required\nargument, to indicate that at least one of the mutually exclusive arguments\nis required:

\n
>>> parser = argparse.ArgumentParser(prog='PROG')\n>>> group = parser.add_mutually_exclusive_group(required=True)\n>>> group.add_argument('--foo', action='store_true')\n>>> group.add_argument('--bar', action='store_false')\n>>> parser.parse_args([])\nusage: PROG [-h] (--foo | --bar)\nPROG: error: one of the arguments --foo --bar is required\n
\n
\n

Note that currently mutually exclusive argument groups do not support the\ntitle and description arguments of\nadd_argument_group().

\n
\n\n
\n
\n

15.4.5.5. Parser defaults

\n
\n
\nArgumentParser.set_defaults(**kwargs)
\n

Most of the time, the attributes of the object returned by parse_args()\nwill be fully determined by inspecting the command-line arguments and the argument\nactions. set_defaults() allows some additional\nattributes that are determined without any inspection of the command line to\nbe added:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('foo', type=int)\n>>> parser.set_defaults(bar=42, baz='badger')\n>>> parser.parse_args(['736'])\nNamespace(bar=42, baz='badger', foo=736)\n
\n
\n

Note that parser-level defaults always override argument-level defaults:

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', default='bar')\n>>> parser.set_defaults(foo='spam')\n>>> parser.parse_args([])\nNamespace(foo='spam')\n
\n
\n

Parser-level defaults can be particularly useful when working with multiple\nparsers. See the add_subparsers() method for an\nexample of this type.

\n
\n\n
\n
\nArgumentParser.get_default(dest)
\n

Get the default value for a namespace attribute, as set by either\nadd_argument() or by\nset_defaults():

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', default='badger')\n>>> parser.get_default('foo')\n'badger'\n
\n
\n
\n\n
\n
\n

15.4.5.6. Printing help

\n

In most typical applications, parse_args() will take\ncare of formatting and printing any usage or error messages. However, several\nformatting methods are available:

\n
\n
\nArgumentParser.print_usage(file=None)
\n
Print a brief description of how the ArgumentParser should be\ninvoked on the command line. If file is None, sys.stdout is\nassumed.
\n\n
\n
\nArgumentParser.print_help(file=None)
\n
Print a help message, including the program usage and information about the\narguments registered with the ArgumentParser. If file is\nNone, sys.stdout is assumed.
\n\n

There are also variants of these methods that simply return a string instead of\nprinting it:

\n
\n
\nArgumentParser.format_usage()
\n
Return a string containing a brief description of how the\nArgumentParser should be invoked on the command line.
\n\n
\n
\nArgumentParser.format_help()
\n
Return a string containing a help message, including the program usage and\ninformation about the arguments registered with the ArgumentParser.
\n\n
\n
\n

15.4.5.7. Partial parsing

\n
\n
\nArgumentParser.parse_known_args(args=None, namespace=None)
\n
\n\n

Sometimes a script may only parse a few of the command-line arguments, passing\nthe remaining arguments on to another script or program. In these cases, the\nparse_known_args() method can be useful. It works much like\nparse_args() except that it does not produce an error when\nextra arguments are present. Instead, it returns a two item tuple containing\nthe populated namespace and the list of remaining argument strings.

\n
>>> parser = argparse.ArgumentParser()\n>>> parser.add_argument('--foo', action='store_true')\n>>> parser.add_argument('bar')\n>>> parser.parse_known_args(['--foo', '--badger', 'BAR', 'spam'])\n(Namespace(bar='BAR', foo=True), ['--badger', 'spam'])\n
\n
\n
\n
\n

15.4.5.8. Customizing file parsing

\n
\n
\nArgumentParser.convert_arg_line_to_args(arg_line)
\n

Arguments that are read from a file (see the fromfile_prefix_chars\nkeyword argument to the ArgumentParser constructor) are read one\nargument per line. convert_arg_line_to_args() can be overriden for\nfancier reading.

\n

This method takes a single argument arg_line which is a string read from\nthe argument file. It returns a list of arguments parsed from this string.\nThe method is called once per line read from the argument file, in order.

\n

A useful override of this method is one that treats each space-separated word\nas an argument:

\n
def convert_arg_line_to_args(self, arg_line):\n    for arg in arg_line.split():\n        if not arg.strip():\n            continue\n        yield arg\n
\n
\n
\n\n
\n
\n

15.4.5.9. Exiting methods

\n
\n
\nArgumentParser.exit(status=0, message=None)
\n
This method terminates the program, exiting with the specified status\nand, if given, it prints a message before that.
\n\n
\n
\nArgumentParser.error(message)
\n
This method prints a usage message including the message to the\nstandard error and terminates the program with a status code of 2.
\n\n
\n
\n
\n

15.4.6. Upgrading optparse code

\n

Originally, the argparse module had attempted to maintain compatibility\nwith optparse. However, optparse was difficult to extend\ntransparently, particularly with the changes required to support the new\nnargs= specifiers and better usage messages. When most everything in\noptparse had either been copy-pasted over or monkey-patched, it no\nlonger seemed practical to try to maintain the backwards compatibility.

\n

A partial upgrade path from optparse to argparse:

\n\n
\n
", "searchableItems": [ { "name": "argparse.add_mutually_exclusive_group", "domId": "argparse_argparse.add_mutually_exclusive_group" }, { "name": "argparse.ArgumentParser", "domId": "argparse_argparse.ArgumentParser" }, { "name": "argparse.ArgumentParser.add_argument", "domId": "argparse_argparse.ArgumentParser.add_argument" }, { "name": "argparse.ArgumentParser.add_argument_group", "domId": "argparse_argparse.ArgumentParser.add_argument_group" }, { "name": "argparse.ArgumentParser.add_subparsers", "domId": "argparse_argparse.ArgumentParser.add_subparsers" }, { "name": "argparse.ArgumentParser.convert_arg_line_to_args", "domId": "argparse_argparse.ArgumentParser.convert_arg_line_to_args" }, { "name": "argparse.ArgumentParser.error", "domId": "argparse_argparse.ArgumentParser.error" }, { "name": "argparse.ArgumentParser.exit", "domId": "argparse_argparse.ArgumentParser.exit" }, { "name": "argparse.ArgumentParser.format_help", "domId": "argparse_argparse.ArgumentParser.format_help" }, { "name": "argparse.ArgumentParser.format_usage", "domId": "argparse_argparse.ArgumentParser.format_usage" }, { "name": "argparse.ArgumentParser.get_default", "domId": "argparse_argparse.ArgumentParser.get_default" }, { "name": "argparse.ArgumentParser.parse_args", "domId": "argparse_argparse.ArgumentParser.parse_args" }, { "name": "argparse.ArgumentParser.parse_known_args", "domId": "argparse_argparse.ArgumentParser.parse_known_args" }, { "name": "argparse.ArgumentParser.print_help", "domId": "argparse_argparse.ArgumentParser.print_help" }, { "name": "argparse.ArgumentParser.print_usage", "domId": "argparse_argparse.ArgumentParser.print_usage" }, { "name": "argparse.ArgumentParser.set_defaults", "domId": "argparse_argparse.ArgumentParser.set_defaults" }, { "name": "argparse.FileType", "domId": "argparse_argparse.FileType" }, { "name": "argparse.Namespace", "domId": "argparse_argparse.Namespace" }, { "name": "argparse.RawDescriptionHelpFormatter", "domId": "argparse_argparse.RawDescriptionHelpFormatter" } ] }, { "url": "http://docs.python.org/library/curses.ascii.html", "title": "curses.ascii", "html": "
\n

15.13. curses.ascii — Utilities for ASCII characters

\n

\nNew in version 1.6.

\n

The curses.ascii module supplies name constants for ASCII characters and\nfunctions to test membership in various ASCII character classes. The constants\nsupplied are names for control characters as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameMeaning
NUL 
SOHStart of heading, console interrupt
STXStart of text
ETXEnd of text
EOTEnd of transmission
ENQEnquiry, goes with ACK flow control
ACKAcknowledgement
BELBell
BSBackspace
TABTab
HTAlias for TAB: “Horizontal tab”
LFLine feed
NLAlias for LF: “New line”
VTVertical tab
FFForm feed
CRCarriage return
SOShift-out, begin alternate character set
SIShift-in, resume default character set
DLEData-link escape
DC1XON, for flow control
DC2Device control 2, block-mode flow control
DC3XOFF, for flow control
DC4Device control 4
NAKNegative acknowledgement
SYNSynchronous idle
ETBEnd transmission block
CANCancel
EMEnd of medium
SUBSubstitute
ESCEscape
FSFile separator
GSGroup separator
RSRecord separator, block-mode terminator
USUnit separator
SPSpace
DELDelete
\n

Note that many of these have little practical significance in modern usage. The\nmnemonics derive from teleprinter conventions that predate digital computers.

\n

The module supplies the following functions, patterned on those in the standard\nC library:

\n
\n
\ncurses.ascii.isalnum(c)
\n
Checks for an ASCII alphanumeric character; it is equivalent to isalpha(c) or\nisdigit(c).
\n\n
\n
\ncurses.ascii.isalpha(c)
\n
Checks for an ASCII alphabetic character; it is equivalent to isupper(c) or\nislower(c).
\n\n
\n
\ncurses.ascii.isascii(c)
\n
Checks for a character value that fits in the 7-bit ASCII set.
\n\n
\n
\ncurses.ascii.isblank(c)
\n
Checks for an ASCII whitespace character.
\n\n
\n
\ncurses.ascii.iscntrl(c)
\n
Checks for an ASCII control character (in the range 0x00 to 0x1f).
\n\n
\n
\ncurses.ascii.isdigit(c)
\n
Checks for an ASCII decimal digit, '0' through '9'. This is equivalent\nto c in string.digits.
\n\n
\n
\ncurses.ascii.isgraph(c)
\n
Checks for ASCII any printable character except space.
\n\n
\n
\ncurses.ascii.islower(c)
\n
Checks for an ASCII lower-case character.
\n\n
\n
\ncurses.ascii.isprint(c)
\n
Checks for any ASCII printable character including space.
\n\n
\n
\ncurses.ascii.ispunct(c)
\n
Checks for any printable ASCII character which is not a space or an alphanumeric\ncharacter.
\n\n
\n
\ncurses.ascii.isspace(c)
\n
Checks for ASCII white-space characters; space, line feed, carriage return, form\nfeed, horizontal tab, vertical tab.
\n\n
\n
\ncurses.ascii.isupper(c)
\n
Checks for an ASCII uppercase letter.
\n\n
\n
\ncurses.ascii.isxdigit(c)
\n
Checks for an ASCII hexadecimal digit. This is equivalent to c in\nstring.hexdigits.
\n\n
\n
\ncurses.ascii.isctrl(c)
\n
Checks for an ASCII control character (ordinal values 0 to 31).
\n\n
\n
\ncurses.ascii.ismeta(c)
\n
Checks for a non-ASCII character (ordinal values 0x80 and above).
\n\n

These functions accept either integers or strings; when the argument is a\nstring, it is first converted using the built-in function ord().

\n

Note that all these functions check ordinal bit values derived from the first\ncharacter of the string you pass in; they do not actually know anything about\nthe host machine’s character encoding. For functions that know about the\ncharacter encoding (and handle internationalization properly) see the\nstring module.

\n

The following two functions take either a single-character string or integer\nbyte value; they return a value of the same type.

\n
\n
\ncurses.ascii.ascii(c)
\n
Return the ASCII value corresponding to the low 7 bits of c.
\n\n
\n
\ncurses.ascii.ctrl(c)
\n
Return the control character corresponding to the given character (the character\nbit value is bitwise-anded with 0x1f).
\n\n
\n
\ncurses.ascii.alt(c)
\n
Return the 8-bit character corresponding to the given ASCII character (the\ncharacter bit value is bitwise-ored with 0x80).
\n\n

The following function takes either a single-character string or integer value;\nit returns a string.

\n
\n
\ncurses.ascii.unctrl(c)
\n
Return a string representation of the ASCII character c. If c is printable,\nthis string is the character itself. If the character is a control character\n(0x00-0x1f) the string consists of a caret ('^') followed by the\ncorresponding uppercase letter. If the character is an ASCII delete (0x7f) the\nstring is '^?'. If the character has its meta bit (0x80) set, the meta bit\nis stripped, the preceding rules applied, and '!' prepended to the result.
\n\n
\n
\ncurses.ascii.controlnames
\n
A 33-element string array that contains the ASCII mnemonics for the thirty-two\nASCII control characters from 0 (NUL) to 0x1f (US), in order, plus the mnemonic\nSP for the space character.
\n\n
", "searchableItems": [ { "name": "curses.ascii.alt", "domId": "curses.ascii_curses.ascii.alt" }, { "name": "curses.ascii.ascii", "domId": "curses.ascii_curses.ascii.ascii" }, { "name": "curses.ascii.ctrl", "domId": "curses.ascii_curses.ascii.ctrl" }, { "name": "curses.ascii.isalnum", "domId": "curses.ascii_curses.ascii.isalnum" }, { "name": "curses.ascii.isalpha", "domId": "curses.ascii_curses.ascii.isalpha" }, { "name": "curses.ascii.isascii", "domId": "curses.ascii_curses.ascii.isascii" }, { "name": "curses.ascii.isblank", "domId": "curses.ascii_curses.ascii.isblank" }, { "name": "curses.ascii.iscntrl", "domId": "curses.ascii_curses.ascii.iscntrl" }, { "name": "curses.ascii.isctrl", "domId": "curses.ascii_curses.ascii.isctrl" }, { "name": "curses.ascii.isdigit", "domId": "curses.ascii_curses.ascii.isdigit" }, { "name": "curses.ascii.isgraph", "domId": "curses.ascii_curses.ascii.isgraph" }, { "name": "curses.ascii.islower", "domId": "curses.ascii_curses.ascii.islower" }, { "name": "curses.ascii.ismeta", "domId": "curses.ascii_curses.ascii.ismeta" }, { "name": "curses.ascii.isprint", "domId": "curses.ascii_curses.ascii.isprint" }, { "name": "curses.ascii.ispunct", "domId": "curses.ascii_curses.ascii.ispunct" }, { "name": "curses.ascii.isspace", "domId": "curses.ascii_curses.ascii.isspace" }, { "name": "curses.ascii.isupper", "domId": "curses.ascii_curses.ascii.isupper" }, { "name": "curses.ascii.isxdigit", "domId": "curses.ascii_curses.ascii.isxdigit" }, { "name": "curses.ascii.unctrl", "domId": "curses.ascii_curses.ascii.unctrl" } ] }, { "url": "http://docs.python.org/library/logging.config.html", "title": "logging.config", "html": "
\n

15.8. logging.config — Logging configuration

\n
\n

Important

\n

This page contains only reference information. For tutorials,\nplease see

\n\n
\n

This section describes the API for configuring the logging module.

\n
\n

15.8.1. Configuration functions

\n

The following functions configure the logging module. They are located in the\nlogging.config module. Their use is optional — you can configure the\nlogging module using these functions or by making calls to the main API (defined\nin logging itself) and defining handlers which are declared either in\nlogging or logging.handlers.

\n
\n
\nlogging.config.dictConfig(config)
\n
\n

Takes the logging configuration from a dictionary. The contents of\nthis dictionary are described in Configuration dictionary schema\nbelow.

\n

If an error is encountered during configuration, this function will\nraise a ValueError, TypeError, AttributeError\nor ImportError with a suitably descriptive message. The\nfollowing is a (possibly incomplete) list of conditions which will\nraise an error:

\n
    \n
  • A level which is not a string or which is a string not\ncorresponding to an actual logging level.
  • \n
  • A propagate value which is not a boolean.
  • \n
  • An id which does not have a corresponding destination.
  • \n
  • A non-existent handler id found during an incremental call.
  • \n
  • An invalid logger name.
  • \n
  • Inability to resolve to an internal or external object.
  • \n
\n

Parsing is performed by the DictConfigurator class, whose\nconstructor is passed the dictionary used for configuration, and\nhas a configure() method. The logging.config module\nhas a callable attribute dictConfigClass\nwhich is initially set to DictConfigurator.\nYou can replace the value of dictConfigClass with a\nsuitable implementation of your own.

\n

dictConfig() calls dictConfigClass passing\nthe specified dictionary, and then calls the configure() method on\nthe returned object to put the configuration into effect:

\n
def dictConfig(config):\n    dictConfigClass(config).configure()\n
\n
\n

For example, a subclass of DictConfigurator could call\nDictConfigurator.__init__() in its own __init__(), then\nset up custom prefixes which would be usable in the subsequent\nconfigure() call. dictConfigClass would be bound to\nthis new subclass, and then dictConfig() could be called exactly as\nin the default, uncustomized state.

\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nlogging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True)
\n

Reads the logging configuration from a configparser-format file\nnamed fname. This function can be called several times from an\napplication, allowing an end user to select from various pre-canned\nconfigurations (if the developer provides a mechanism to present the choices\nand load the chosen configuration).

\n\n\n\n\n\n\n\n
Parameters:
    \n
  • defaults – Defaults to be passed to the ConfigParser can be specified\nin this argument.
  • \n
  • disable_existing_loggers – If specified as False, loggers which\nexist when this call is made are left\nalone. The default is True because this\nenables old behaviour in a backward-\ncompatible way. This behaviour is to\ndisable any existing loggers unless they or\ntheir ancestors are explicitly named in the\nlogging configuration.
  • \n
\n
\n

\nChanged in version 2.6: The disable_existing_loggers keyword argument was added. Previously,\nexisting loggers were always disabled.

\n
\n\n
\n
\nlogging.config.listen(port=DEFAULT_LOGGING_CONFIG_PORT)
\n

Starts up a socket server on the specified port, and listens for new\nconfigurations. If no port is specified, the module’s default\nDEFAULT_LOGGING_CONFIG_PORT is used. Logging configurations will be\nsent as a file suitable for processing by fileConfig(). Returns a\nThread instance on which you can call start() to start the\nserver, and which you can join() when appropriate. To stop the server,\ncall stopListening().

\n

To send a configuration to the socket, read in the configuration file and\nsend it to the socket as a string of bytes preceded by a four-byte length\nstring packed in binary using struct.pack('>L', n).

\n
\n\n
\n
\nlogging.config.stopListening()
\n
Stops the listening server which was created with a call to listen().\nThis is typically called before calling join() on the return value from\nlisten().
\n\n
\n
\n

15.8.2. Configuration dictionary schema

\n

Describing a logging configuration requires listing the various\nobjects to create and the connections between them; for example, you\nmay create a handler named ‘console’ and then say that the logger\nnamed ‘startup’ will send its messages to the ‘console’ handler.\nThese objects aren’t limited to those provided by the logging\nmodule because you might write your own formatter or handler class.\nThe parameters to these classes may also need to include external\nobjects such as sys.stderr. The syntax for describing these\nobjects and connections is defined in Object connections\nbelow.

\n
\n

15.8.2.1. Dictionary Schema Details

\n

The dictionary passed to dictConfig() must contain the following\nkeys:

\n
    \n
  • version - to be set to an integer value representing the schema\nversion. The only valid value at present is 1, but having this key\nallows the schema to evolve while still preserving backwards\ncompatibility.
  • \n
\n

All other keys are optional, but if present they will be interpreted\nas described below. In all cases below where a ‘configuring dict’ is\nmentioned, it will be checked for the special '()' key to see if a\ncustom instantiation is required. If so, the mechanism described in\nUser-defined objects below is used to create an instance;\notherwise, the context is used to determine what to instantiate.

\n
    \n
  • formatters - the corresponding value will be a dict in which each\nkey is a formatter id and each value is a dict describing how to\nconfigure the corresponding Formatter instance.

    \n

    The configuring dict is searched for keys format and datefmt\n(with defaults of None) and these are used to construct a\nlogging.Formatter instance.

    \n
  • \n
  • filters - the corresponding value will be a dict in which each key\nis a filter id and each value is a dict describing how to configure\nthe corresponding Filter instance.

    \n

    The configuring dict is searched for the key name (defaulting to the\nempty string) and this is used to construct a logging.Filter\ninstance.

    \n
  • \n
  • handlers - the corresponding value will be a dict in which each\nkey is a handler id and each value is a dict describing how to\nconfigure the corresponding Handler instance.

    \n

    The configuring dict is searched for the following keys:

    \n
      \n
    • class (mandatory). This is the fully qualified name of the\nhandler class.
    • \n
    • level (optional). The level of the handler.
    • \n
    • formatter (optional). The id of the formatter for this\nhandler.
    • \n
    • filters (optional). A list of ids of the filters for this\nhandler.
    • \n
    \n

    All other keys are passed through as keyword arguments to the\nhandler’s constructor. For example, given the snippet:

    \n
    handlers:\n  console:\n    class : logging.StreamHandler\n    formatter: brief\n    level   : INFO\n    filters: [allow_foo]\n    stream  : ext://sys.stdout\n  file:\n    class : logging.handlers.RotatingFileHandler\n    formatter: precise\n    filename: logconfig.log\n    maxBytes: 1024\n    backupCount: 3
    \n
    \n

    the handler with id console is instantiated as a\nlogging.StreamHandler, using sys.stdout as the underlying\nstream. The handler with id file is instantiated as a\nlogging.handlers.RotatingFileHandler with the keyword arguments\nfilename='logconfig.log', maxBytes=1024, backupCount=3.

    \n
  • \n
  • loggers - the corresponding value will be a dict in which each key\nis a logger name and each value is a dict describing how to\nconfigure the corresponding Logger instance.

    \n

    The configuring dict is searched for the following keys:

    \n
      \n
    • level (optional). The level of the logger.
    • \n
    • propagate (optional). The propagation setting of the logger.
    • \n
    • filters (optional). A list of ids of the filters for this\nlogger.
    • \n
    • handlers (optional). A list of ids of the handlers for this\nlogger.
    • \n
    \n

    The specified loggers will be configured according to the level,\npropagation, filters and handlers specified.

    \n
  • \n
  • root - this will be the configuration for the root logger.\nProcessing of the configuration will be as for any logger, except\nthat the propagate setting will not be applicable.

    \n
  • \n
  • incremental - whether the configuration is to be interpreted as\nincremental to the existing configuration. This value defaults to\nFalse, which means that the specified configuration replaces the\nexisting configuration with the same semantics as used by the\nexisting fileConfig() API.

    \n

    If the specified value is True, the configuration is processed\nas described in the section on Incremental Configuration.

    \n
  • \n
  • disable_existing_loggers - whether any existing loggers are to be\ndisabled. This setting mirrors the parameter of the same name in\nfileConfig(). If absent, this parameter defaults to True.\nThis value is ignored if incremental is True.

    \n
  • \n
\n
\n
\n

15.8.2.2. Incremental Configuration

\n

It is difficult to provide complete flexibility for incremental\nconfiguration. For example, because objects such as filters\nand formatters are anonymous, once a configuration is set up, it is\nnot possible to refer to such anonymous objects when augmenting a\nconfiguration.

\n

Furthermore, there is not a compelling case for arbitrarily altering\nthe object graph of loggers, handlers, filters, formatters at\nrun-time, once a configuration is set up; the verbosity of loggers and\nhandlers can be controlled just by setting levels (and, in the case of\nloggers, propagation flags). Changing the object graph arbitrarily in\na safe way is problematic in a multi-threaded environment; while not\nimpossible, the benefits are not worth the complexity it adds to the\nimplementation.

\n

Thus, when the incremental key of a configuration dict is present\nand is True, the system will completely ignore any formatters and\nfilters entries, and process only the level\nsettings in the handlers entries, and the level and\npropagate settings in the loggers and root entries.

\n

Using a value in the configuration dict lets configurations to be sent\nover the wire as pickled dicts to a socket listener. Thus, the logging\nverbosity of a long-running application can be altered over time with\nno need to stop and restart the application.

\n
\n
\n

15.8.2.3. Object connections

\n

The schema describes a set of logging objects - loggers,\nhandlers, formatters, filters - which are connected to each other in\nan object graph. Thus, the schema needs to represent connections\nbetween the objects. For example, say that, once configured, a\nparticular logger has attached to it a particular handler. For the\npurposes of this discussion, we can say that the logger represents the\nsource, and the handler the destination, of a connection between the\ntwo. Of course in the configured objects this is represented by the\nlogger holding a reference to the handler. In the configuration dict,\nthis is done by giving each destination object an id which identifies\nit unambiguously, and then using the id in the source object’s\nconfiguration to indicate that a connection exists between the source\nand the destination object with that id.

\n

So, for example, consider the following YAML snippet:

\n
formatters:\n  brief:\n    # configuration for formatter with id 'brief' goes here\n  precise:\n    # configuration for formatter with id 'precise' goes here\nhandlers:\n  h1: #This is an id\n   # configuration of handler with id 'h1' goes here\n   formatter: brief\n  h2: #This is another id\n   # configuration of handler with id 'h2' goes here\n   formatter: precise\nloggers:\n  foo.bar.baz:\n    # other configuration for logger 'foo.bar.baz'\n    handlers: [h1, h2]
\n
\n

(Note: YAML used here because it’s a little more readable than the\nequivalent Python source form for the dictionary.)

\n

The ids for loggers are the logger names which would be used\nprogrammatically to obtain a reference to those loggers, e.g.\nfoo.bar.baz. The ids for Formatters and Filters can be any string\nvalue (such as brief, precise above) and they are transient,\nin that they are only meaningful for processing the configuration\ndictionary and used to determine connections between objects, and are\nnot persisted anywhere when the configuration call is complete.

\n

The above snippet indicates that logger named foo.bar.baz should\nhave two handlers attached to it, which are described by the handler\nids h1 and h2. The formatter for h1 is that described by id\nbrief, and the formatter for h2 is that described by id\nprecise.

\n
\n
\n

15.8.2.4. User-defined objects

\n

The schema supports user-defined objects for handlers, filters and\nformatters. (Loggers do not need to have different types for\ndifferent instances, so there is no support in this configuration\nschema for user-defined logger classes.)

\n

Objects to be configured are described by dictionaries\nwhich detail their configuration. In some places, the logging system\nwill be able to infer from the context how an object is to be\ninstantiated, but when a user-defined object is to be instantiated,\nthe system will not know how to do this. In order to provide complete\nflexibility for user-defined object instantiation, the user needs\nto provide a ‘factory’ - a callable which is called with a\nconfiguration dictionary and which returns the instantiated object.\nThis is signalled by an absolute import path to the factory being\nmade available under the special key '()'. Here’s a concrete\nexample:

\n
formatters:\n  brief:\n    format: '%(message)s'\n  default:\n    format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'\n    datefmt: '%Y-%m-%d %H:%M:%S'\n  custom:\n      (): my.package.customFormatterFactory\n      bar: baz\n      spam: 99.9\n      answer: 42
\n
\n

The above YAML snippet defines three formatters. The first, with id\nbrief, is a standard logging.Formatter instance with the\nspecified format string. The second, with id default, has a\nlonger format and also defines the time format explicitly, and will\nresult in a logging.Formatter initialized with those two format\nstrings. Shown in Python source form, the brief and default\nformatters have configuration sub-dictionaries:

\n
{\n  'format' : '%(message)s'\n}\n
\n
\n

and:

\n
{\n  'format' : '%(asctime)s %(levelname)-8s %(name)-15s %(message)s',\n  'datefmt' : '%Y-%m-%d %H:%M:%S'\n}\n
\n
\n

respectively, and as these dictionaries do not contain the special key\n'()', the instantiation is inferred from the context: as a result,\nstandard logging.Formatter instances are created. The\nconfiguration sub-dictionary for the third formatter, with id\ncustom, is:

\n
{\n  '()' : 'my.package.customFormatterFactory',\n  'bar' : 'baz',\n  'spam' : 99.9,\n  'answer' : 42\n}\n
\n
\n

and this contains the special key '()', which means that\nuser-defined instantiation is wanted. In this case, the specified\nfactory callable will be used. If it is an actual callable it will be\nused directly - otherwise, if you specify a string (as in the example)\nthe actual callable will be located using normal import mechanisms.\nThe callable will be called with the remaining items in the\nconfiguration sub-dictionary as keyword arguments. In the above\nexample, the formatter with id custom will be assumed to be\nreturned by the call:

\n
my.package.customFormatterFactory(bar='baz', spam=99.9, answer=42)\n
\n
\n

The key '()' has been used as the special key because it is not a\nvalid keyword parameter name, and so will not clash with the names of\nthe keyword arguments used in the call. The '()' also serves as a\nmnemonic that the corresponding value is a callable.

\n
\n
\n

15.8.2.5. Access to external objects

\n

There are times where a configuration needs to refer to objects\nexternal to the configuration, for example sys.stderr. If the\nconfiguration dict is constructed using Python code, this is\nstraightforward, but a problem arises when the configuration is\nprovided via a text file (e.g. JSON, YAML). In a text file, there is\nno standard way to distinguish sys.stderr from the literal string\n'sys.stderr'. To facilitate this distinction, the configuration\nsystem looks for certain special prefixes in string values and\ntreat them specially. For example, if the literal string\n'ext://sys.stderr' is provided as a value in the configuration,\nthen the ext:// will be stripped off and the remainder of the\nvalue processed using normal import mechanisms.

\n

The handling of such prefixes is done in a way analogous to protocol\nhandling: there is a generic mechanism to look for prefixes which\nmatch the regular expression ^(?P<prefix>[a-z]+)://(?P<suffix>.*)$\nwhereby, if the prefix is recognised, the suffix is processed\nin a prefix-dependent manner and the result of the processing replaces\nthe string value. If the prefix is not recognised, then the string\nvalue will be left as-is.

\n
\n
\n

15.8.2.6. Access to internal objects

\n

As well as external objects, there is sometimes also a need to refer\nto objects in the configuration. This will be done implicitly by the\nconfiguration system for things that it knows about. For example, the\nstring value 'DEBUG' for a level in a logger or handler will\nautomatically be converted to the value logging.DEBUG, and the\nhandlers, filters and formatter entries will take an\nobject id and resolve to the appropriate destination object.

\n

However, a more generic mechanism is needed for user-defined\nobjects which are not known to the logging module. For\nexample, consider logging.handlers.MemoryHandler, which takes\na target argument which is another handler to delegate to. Since\nthe system already knows about this class, then in the configuration,\nthe given target just needs to be the object id of the relevant\ntarget handler, and the system will resolve to the handler from the\nid. If, however, a user defines a my.package.MyHandler which has\nan alternate handler, the configuration system would not know that\nthe alternate referred to a handler. To cater for this, a generic\nresolution system allows the user to specify:

\n
handlers:\n  file:\n    # configuration of file handler goes here\n\n  custom:\n    (): my.package.MyHandler\n    alternate: cfg://handlers.file
\n
\n

The literal string 'cfg://handlers.file' will be resolved in an\nanalogous way to strings with the ext:// prefix, but looking\nin the configuration itself rather than the import namespace. The\nmechanism allows access by dot or by index, in a similar way to\nthat provided by str.format. Thus, given the following snippet:

\n
handlers:\n  email:\n    class: logging.handlers.SMTPHandler\n    mailhost: localhost\n    fromaddr: my_app@domain.tld\n    toaddrs:\n      - support_team@domain.tld\n      - dev_team@domain.tld\n    subject: Houston, we have a problem.
\n
\n

in the configuration, the string 'cfg://handlers' would resolve to\nthe dict with key handlers, the string 'cfg://handlers.email\nwould resolve to the dict with key email in the handlers dict,\nand so on. The string 'cfg://handlers.email.toaddrs[1] would\nresolve to 'dev_team.domain.tld' and the string\n'cfg://handlers.email.toaddrs[0]' would resolve to the value\n'support_team@domain.tld'. The subject value could be accessed\nusing either 'cfg://handlers.email.subject' or, equivalently,\n'cfg://handlers.email[subject]'. The latter form only needs to be\nused if the key contains spaces or non-alphanumeric characters. If an\nindex value consists only of decimal digits, access will be attempted\nusing the corresponding integer value, falling back to the string\nvalue if needed.

\n

Given a string cfg://handlers.myhandler.mykey.123, this will\nresolve to config_dict['handlers']['myhandler']['mykey']['123'].\nIf the string is specified as cfg://handlers.myhandler.mykey[123],\nthe system will attempt to retrieve the value from\nconfig_dict['handlers']['myhandler']['mykey'][123], and fall back\nto config_dict['handlers']['myhandler']['mykey']['123'] if that\nfails.

\n
\n
\n

15.8.2.7. Import resolution and custom importers

\n

Import resolution, by default, uses the builtin __import__() function\nto do its importing. You may want to replace this with your own importing\nmechanism: if so, you can replace the importer attribute of the\nDictConfigurator or its superclass, the\nBaseConfigurator class. However, you need to be\ncareful because of the way functions are accessed from classes via\ndescriptors. If you are using a Python callable to do your imports, and you\nwant to define it at class level rather than instance level, you need to wrap\nit with staticmethod(). For example:

\n
from importlib import import_module\nfrom logging.config import BaseConfigurator\n\nBaseConfigurator.importer = staticmethod(import_module)\n
\n
\n

You don’t need to wrap with staticmethod() if you’re setting the import\ncallable on a configurator instance.

\n
\n
\n
\n

15.8.3. Configuration file format

\n

The configuration file format understood by fileConfig() is based on\nconfigparser functionality. The file must contain sections called\n[loggers], [handlers] and [formatters] which identify by name the\nentities of each type which are defined in the file. For each such entity, there\nis a separate section which identifies how that entity is configured. Thus, for\na logger named log01 in the [loggers] section, the relevant\nconfiguration details are held in a section [logger_log01]. Similarly, a\nhandler called hand01 in the [handlers] section will have its\nconfiguration held in a section called [handler_hand01], while a formatter\ncalled form01 in the [formatters] section will have its configuration\nspecified in a section called [formatter_form01]. The root logger\nconfiguration must be specified in a section called [logger_root].

\n

Examples of these sections in the file are given below.

\n
[loggers]\nkeys=root,log02,log03,log04,log05,log06,log07\n\n[handlers]\nkeys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09\n\n[formatters]\nkeys=form01,form02,form03,form04,form05,form06,form07,form08,form09\n
\n
\n

The root logger must specify a level and a list of handlers. An example of a\nroot logger section is given below.

\n
[logger_root]\nlevel=NOTSET\nhandlers=hand01\n
\n
\n

The level entry can be one of DEBUG, INFO, WARNING, ERROR, CRITICAL or\nNOTSET. For the root logger only, NOTSET means that all messages will be\nlogged. Level values are eval()uated in the context of the logging\npackage’s namespace.

\n

The handlers entry is a comma-separated list of handler names, which must\nappear in the [handlers] section. These names must appear in the\n[handlers] section and have corresponding sections in the configuration\nfile.

\n

For loggers other than the root logger, some additional information is required.\nThis is illustrated by the following example.

\n
[logger_parser]\nlevel=DEBUG\nhandlers=hand01\npropagate=1\nqualname=compiler.parser\n
\n
\n

The level and handlers entries are interpreted as for the root logger,\nexcept that if a non-root logger’s level is specified as NOTSET, the system\nconsults loggers higher up the hierarchy to determine the effective level of the\nlogger. The propagate entry is set to 1 to indicate that messages must\npropagate to handlers higher up the logger hierarchy from this logger, or 0 to\nindicate that messages are not propagated to handlers up the hierarchy. The\nqualname entry is the hierarchical channel name of the logger, that is to\nsay the name used by the application to get the logger.

\n

Sections which specify handler configuration are exemplified by the following.

\n
[handler_hand01]\nclass=StreamHandler\nlevel=NOTSET\nformatter=form01\nargs=(sys.stdout,)
\n
\n

The class entry indicates the handler’s class (as determined by eval()\nin the logging package’s namespace). The level is interpreted as for\nloggers, and NOTSET is taken to mean ‘log everything’.

\n

\nChanged in version 2.6: Added support for resolving the handler’s class as a dotted module and\nclass name.

\n

The formatter entry indicates the key name of the formatter for this\nhandler. If blank, a default formatter (logging._defaultFormatter) is used.\nIf a name is specified, it must appear in the [formatters] section and have\na corresponding section in the configuration file.

\n

The args entry, when eval()uated in the context of the logging\npackage’s namespace, is the list of arguments to the constructor for the handler\nclass. Refer to the constructors for the relevant handlers, or to the examples\nbelow, to see how typical entries are constructed.

\n
[handler_hand02]\nclass=FileHandler\nlevel=DEBUG\nformatter=form02\nargs=('python.log', 'w')\n\n[handler_hand03]\nclass=handlers.SocketHandler\nlevel=INFO\nformatter=form03\nargs=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)\n\n[handler_hand04]\nclass=handlers.DatagramHandler\nlevel=WARN\nformatter=form04\nargs=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)\n\n[handler_hand05]\nclass=handlers.SysLogHandler\nlevel=ERROR\nformatter=form05\nargs=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)\n\n[handler_hand06]\nclass=handlers.NTEventLogHandler\nlevel=CRITICAL\nformatter=form06\nargs=('Python Application', '', 'Application')\n\n[handler_hand07]\nclass=handlers.SMTPHandler\nlevel=WARN\nformatter=form07\nargs=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')\n\n[handler_hand08]\nclass=handlers.MemoryHandler\nlevel=NOTSET\nformatter=form08\ntarget=\nargs=(10, ERROR)\n\n[handler_hand09]\nclass=handlers.HTTPHandler\nlevel=NOTSET\nformatter=form09\nargs=('localhost:9022', '/log', 'GET')
\n
\n

Sections which specify formatter configuration are typified by the following.

\n
[formatter_form01]\nformat=F1 %(asctime)s %(levelname)s %(message)s\ndatefmt=\nclass=logging.Formatter
\n
\n

The format entry is the overall format string, and the datefmt entry is\nthe strftime()-compatible date/time format string. If empty, the\npackage substitutes ISO8601 format date/times, which is almost equivalent to\nspecifying the date format string '%Y-%m-%d %H:%M:%S'. The ISO8601 format\nalso specifies milliseconds, which are appended to the result of using the above\nformat string, with a comma separator. An example time in ISO8601 format is\n2003-01-23 00:29:50,411.

\n

The class entry is optional. It indicates the name of the formatter’s class\n(as a dotted module and class name.) This option is useful for instantiating a\nFormatter subclass. Subclasses of Formatter can present\nexception tracebacks in an expanded or condensed format.

\n
\n

See also

\n
\n
Module logging
\n
API reference for the logging module.
\n
Module logging.handlers
\n
Useful handlers included with the logging module.
\n
\n
\n
\n
", "searchableItems": [ { "name": "logging.config.dictConfig", "domId": "logging.config_logging.config.dictConfig" }, { "name": "logging.config.fileConfig", "domId": "logging.config_logging.config.fileConfig" }, { "name": "logging.config.listen", "domId": "logging.config_logging.config.listen" }, { "name": "logging.config.stopListening", "domId": "logging.config_logging.config.stopListening" } ] }, { "url": "http://docs.python.org/library/curses.panel.html", "title": "curses.panel", "html": "
\n

15.14. curses.panel — A panel stack extension for curses

\n

Panels are windows with the added feature of depth, so they can be stacked on\ntop of each other, and only the visible portions of each window will be\ndisplayed. Panels can be added, moved up or down in the stack, and removed.

\n
\n

15.14.1. Functions

\n

The module curses.panel defines the following functions:

\n
\n
\ncurses.panel.bottom_panel()
\n
Returns the bottom panel in the panel stack.
\n\n
\n
\ncurses.panel.new_panel(win)
\n
Returns a panel object, associating it with the given window win. Be aware\nthat you need to keep the returned panel object referenced explicitly. If you\ndon’t, the panel object is garbage collected and removed from the panel stack.
\n\n
\n
\ncurses.panel.top_panel()
\n
Returns the top panel in the panel stack.
\n\n
\n
\ncurses.panel.update_panels()
\n
Updates the virtual screen after changes in the panel stack. This does not call\ncurses.doupdate(), so you’ll have to do this yourself.
\n\n
\n
\n

15.14.2. Panel Objects

\n

Panel objects, as returned by new_panel() above, are windows with a\nstacking order. There’s always a window associated with a panel which determines\nthe content, while the panel methods are responsible for the window’s depth in\nthe panel stack.

\n

Panel objects have the following methods:

\n
\n
\nPanel.above()
\n
Returns the panel above the current panel.
\n\n
\n
\nPanel.below()
\n
Returns the panel below the current panel.
\n\n
\n
\nPanel.bottom()
\n
Push the panel to the bottom of the stack.
\n\n
\n
\nPanel.hidden()
\n
Returns true if the panel is hidden (not visible), false otherwise.
\n\n
\n
\nPanel.hide()
\n
Hide the panel. This does not delete the object, it just makes the window on\nscreen invisible.
\n\n
\n
\nPanel.move(y, x)
\n
Move the panel to the screen coordinates (y, x).
\n\n
\n
\nPanel.replace(win)
\n
Change the window associated with the panel to the window win.
\n\n
\n
\nPanel.set_userptr(obj)
\n
Set the panel’s user pointer to obj. This is used to associate an arbitrary\npiece of data with the panel, and can be any Python object.
\n\n
\n
\nPanel.show()
\n
Display the panel (which might have been hidden).
\n\n
\n
\nPanel.top()
\n
Push panel to the top of the stack.
\n\n
\n
\nPanel.userptr()
\n
Returns the user pointer for the panel. This might be any Python object.
\n\n
\n
\nPanel.window()
\n
Returns the window object associated with the panel.
\n\n
\n
", "searchableItems": [ { "name": "curses.panel.bottom_panel", "domId": "curses.panel_curses.panel.bottom_panel" }, { "name": "curses.panel.new_panel", "domId": "curses.panel_curses.panel.new_panel" }, { "name": "curses.panel.Panel.above", "domId": "curses.panel_curses.panel.Panel.above" }, { "name": "curses.panel.Panel.below", "domId": "curses.panel_curses.panel.Panel.below" }, { "name": "curses.panel.Panel.bottom", "domId": "curses.panel_curses.panel.Panel.bottom" }, { "name": "curses.panel.Panel.hidden", "domId": "curses.panel_curses.panel.Panel.hidden" }, { "name": "curses.panel.Panel.hide", "domId": "curses.panel_curses.panel.Panel.hide" }, { "name": "curses.panel.Panel.move", "domId": "curses.panel_curses.panel.Panel.move" }, { "name": "curses.panel.Panel.replace", "domId": "curses.panel_curses.panel.Panel.replace" }, { "name": "curses.panel.Panel.set_userptr", "domId": "curses.panel_curses.panel.Panel.set_userptr" }, { "name": "curses.panel.Panel.show", "domId": "curses.panel_curses.panel.Panel.show" }, { "name": "curses.panel.Panel.top", "domId": "curses.panel_curses.panel.Panel.top" }, { "name": "curses.panel.Panel.userptr", "domId": "curses.panel_curses.panel.Panel.userptr" }, { "name": "curses.panel.Panel.window", "domId": "curses.panel_curses.panel.Panel.window" }, { "name": "curses.panel.top_panel", "domId": "curses.panel_curses.panel.top_panel" }, { "name": "curses.panel.update_panels", "domId": "curses.panel_curses.panel.update_panels" } ] }, { "url": "http://docs.python.org/library/platform.html", "title": "platform", "html": "
\n

15.15. platform — Access to underlying platform’s identifying data

\n

\nNew in version 2.3.

\n

Source code: Lib/platform.py

\n
\n
\n

Note

\n

Specific platforms listed alphabetically, with Linux included in the Unix\nsection.

\n
\n
\n

15.15.1. Cross Platform

\n
\n
\nplatform.architecture(executable=sys.executable, bits='', linkage='')
\n

Queries the given executable (defaults to the Python interpreter binary) for\nvarious architecture information.

\n

Returns a tuple (bits, linkage) which contain information about the bit\narchitecture and the linkage format used for the executable. Both values are\nreturned as strings.

\n

Values that cannot be determined are returned as given by the parameter presets.\nIf bits is given as '', the sizeof(pointer)() (or\nsizeof(long)() on Python version < 1.5.2) is used as indicator for the\nsupported pointer size.

\n

The function relies on the system’s file command to do the actual work.\nThis is available on most if not all Unix platforms and some non-Unix platforms\nand then only if the executable points to the Python interpreter. Reasonable\ndefaults are used when the above needs are not met.

\n
\n

Note

\n

On Mac OS X (and perhaps other platforms), executable files may be\nuniversal files containing multiple architectures.

\n

To get at the “64-bitness” of the current interpreter, it is more\nreliable to query the sys.maxsize attribute:

\n
is_64bits = sys.maxsize > 2**32\n
\n
\n
\n
\n\n
\n
\nplatform.machine()
\n
Returns the machine type, e.g. 'i386'. An empty string is returned if the\nvalue cannot be determined.
\n\n
\n
\nplatform.node()
\n
Returns the computer’s network name (may not be fully qualified!). An empty\nstring is returned if the value cannot be determined.
\n\n
\n
\nplatform.platform(aliased=0, terse=0)
\n

Returns a single string identifying the underlying platform with as much useful\ninformation as possible.

\n

The output is intended to be human readable rather than machine parseable. It\nmay look different on different platforms and this is intended.

\n

If aliased is true, the function will use aliases for various platforms that\nreport system names which differ from their common names, for example SunOS will\nbe reported as Solaris. The system_alias() function is used to implement\nthis.

\n

Setting terse to true causes the function to return only the absolute minimum\ninformation needed to identify the platform.

\n
\n\n
\n
\nplatform.processor()
\n

Returns the (real) processor name, e.g. 'amdk6'.

\n

An empty string is returned if the value cannot be determined. Note that many\nplatforms do not provide this information or simply return the same value as for\nmachine(). NetBSD does this.

\n
\n\n
\n
\nplatform.python_build()
\n
Returns a tuple (buildno, builddate) stating the Python build number and\ndate as strings.
\n\n
\n
\nplatform.python_compiler()
\n
Returns a string identifying the compiler used for compiling Python.
\n\n
\n
\nplatform.python_branch()
\n

Returns a string identifying the Python implementation SCM branch.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nplatform.python_implementation()
\n

Returns a string identifying the Python implementation. Possible return values\nare: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nplatform.python_revision()
\n

Returns a string identifying the Python implementation SCM revision.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nplatform.python_version()
\n

Returns the Python version as string 'major.minor.patchlevel'

\n

Note that unlike the Python sys.version, the returned value will always\ninclude the patchlevel (it defaults to 0).

\n
\n\n
\n
\nplatform.python_version_tuple()
\n

Returns the Python version as tuple (major, minor, patchlevel) of strings.

\n

Note that unlike the Python sys.version, the returned value will always\ninclude the patchlevel (it defaults to '0').

\n
\n\n
\n
\nplatform.release()
\n
Returns the system’s release, e.g. '2.2.0' or 'NT' An empty string is\nreturned if the value cannot be determined.
\n\n
\n
\nplatform.system()
\n
Returns the system/OS name, e.g. 'Linux', 'Windows', or 'Java'. An\nempty string is returned if the value cannot be determined.
\n\n
\n
\nplatform.system_alias(system, release, version)
\n
Returns (system, release, version) aliased to common marketing names used\nfor some systems. It also does some reordering of the information in some cases\nwhere it would otherwise cause confusion.
\n\n
\n
\nplatform.version()
\n
Returns the system’s release version, e.g. '#3 on degas'. An empty string is\nreturned if the value cannot be determined.
\n\n
\n
\nplatform.uname()
\n

Fairly portable uname interface. Returns a tuple of strings (system, node,\nrelease, version, machine, processor) identifying the underlying platform.

\n

Note that unlike the os.uname() function this also returns possible\nprocessor information as additional tuple entry.

\n

Entries which cannot be determined are set to ''.

\n
\n\n
\n
\n

15.15.2. Java Platform

\n
\n
\nplatform.java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', ''))
\n

Version interface for Jython.

\n

Returns a tuple (release, vendor, vminfo, osinfo) with vminfo being a\ntuple (vm_name, vm_release, vm_vendor) and osinfo being a tuple\n(os_name, os_version, os_arch). Values which cannot be determined are set to\nthe defaults given as parameters (which all default to '').

\n
\n\n
\n
\n

15.15.3. Windows Platform

\n
\n
\nplatform.win32_ver(release='', version='', csd='', ptype='')
\n

Get additional version information from the Windows Registry and return a tuple\n(version, csd, ptype) referring to version number, CSD level and OS type\n(multi/single processor).

\n

As a hint: ptype is 'Uniprocessor Free' on single processor NT machines\nand 'Multiprocessor Free' on multi processor machines. The ‘Free’ refers\nto the OS version being free of debugging code. It could also state ‘Checked’\nwhich means the OS version uses debugging code, i.e. code that checks arguments,\nranges, etc.

\n
\n

Note

\n

This function works best with Mark Hammond’s\nwin32all package installed, but also on Python 2.3 and\nlater (support for this was added in Python 2.6). It obviously\nonly runs on Win32 compatible platforms.

\n
\n
\n\n
\n

15.15.3.1. Win95/98 specific

\n
\n
\nplatform.popen(cmd, mode='r', bufsize=None)
\n
Portable popen() interface. Find a working popen implementation\npreferring win32pipe.popen(). On Windows NT, win32pipe.popen()\nshould work; on Windows 9x it hangs due to bugs in the MS C library.
\n\n
\n
\n
\n

15.15.4. Mac OS Platform

\n
\n
\nplatform.mac_ver(release='', versioninfo=('', '', ''), machine='')
\n

Get Mac OS version information and return it as tuple (release, versioninfo,\nmachine) with versioninfo being a tuple (version, dev_stage,\nnon_release_version).

\n

Entries which cannot be determined are set to ''. All tuple entries are\nstrings.

\n
\n\n
\n
\n

15.15.5. Unix Platforms

\n
\n
\nplatform.dist(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat', 'mandrake', ...))
\n

This is an old version of the functionality now provided by\nlinux_distribution(). For new code, please use the\nlinux_distribution().

\n

The only difference between the two is that dist() always\nreturns the short name of the distribution taken from the\nsupported_dists parameter.

\n

\nDeprecated since version 2.6.

\n
\n\n
\n
\nplatform.linux_distribution(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat', 'mandrake', ...), full_distribution_name=1)
\n

Tries to determine the name of the Linux OS distribution name.

\n

supported_dists may be given to define the set of Linux distributions to\nlook for. It defaults to a list of currently supported Linux distributions\nidentified by their release file name.

\n

If full_distribution_name is true (default), the full distribution read\nfrom the OS is returned. Otherwise the short name taken from\nsupported_dists is used.

\n

Returns a tuple (distname,version,id) which defaults to the args given as\nparameters. id is the item in parentheses after the version number. It\nis usually the version codename.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nplatform.libc_ver(executable=sys.executable, lib='', version='', chunksize=2048)
\n

Tries to determine the libc version against which the file executable (defaults\nto the Python interpreter) is linked. Returns a tuple of strings (lib,\nversion) which default to the given parameters in case the lookup fails.

\n

Note that this function has intimate knowledge of how different libc versions\nadd symbols to the executable is probably only usable for executables compiled\nusing gcc.

\n

The file is read and scanned in chunks of chunksize bytes.

\n
\n\n
\n
", "searchableItems": [ { "name": "platform.architecture", "domId": "platform_platform.architecture" }, { "name": "platform.dist", "domId": "platform_platform.dist" }, { "name": "platform.java_ver", "domId": "platform_platform.java_ver" }, { "name": "platform.libc_ver", "domId": "platform_platform.libc_ver" }, { "name": "platform.linux_distribution", "domId": "platform_platform.linux_distribution" }, { "name": "platform.mac_ver", "domId": "platform_platform.mac_ver" }, { "name": "platform.machine", "domId": "platform_platform.machine" }, { "name": "platform.node", "domId": "platform_platform.node" }, { "name": "platform.platform", "domId": "platform_platform.platform" }, { "name": "platform.popen", "domId": "platform_platform.popen" }, { "name": "platform.processor", "domId": "platform_platform.processor" }, { "name": "platform.python_branch", "domId": "platform_platform.python_branch" }, { "name": "platform.python_build", "domId": "platform_platform.python_build" }, { "name": "platform.python_compiler", "domId": "platform_platform.python_compiler" }, { "name": "platform.python_implementation", "domId": "platform_platform.python_implementation" }, { "name": "platform.python_revision", "domId": "platform_platform.python_revision" }, { "name": "platform.python_version", "domId": "platform_platform.python_version" }, { "name": "platform.python_version_tuple", "domId": "platform_platform.python_version_tuple" }, { "name": "platform.release", "domId": "platform_platform.release" }, { "name": "platform.system", "domId": "platform_platform.system" }, { "name": "platform.system_alias", "domId": "platform_platform.system_alias" }, { "name": "platform.uname", "domId": "platform_platform.uname" }, { "name": "platform.version", "domId": "platform_platform.version" }, { "name": "platform.win32_ver", "domId": "platform_platform.win32_ver" } ] }, { "url": "http://docs.python.org/library/curses.html", "title": "curses", "html": "
\n

15.11. curses — Terminal handling for character-cell displays

\n

Platforms: Unix

\n

\nChanged in version 1.6: Added support for the ncurses library and converted to a package.

\n

The curses module provides an interface to the curses library, the\nde-facto standard for portable advanced terminal handling.

\n

While curses is most widely used in the Unix environment, versions are available\nfor DOS, OS/2, and possibly other systems as well. This extension module is\ndesigned to match the API of ncurses, an open-source curses library hosted on\nLinux and the BSD variants of Unix.

\n
\n

Note

\n

Since version 5.4, the ncurses library decides how to interpret non-ASCII data\nusing the nl_langinfo function. That means that you have to call\nlocale.setlocale() in the application and encode Unicode strings\nusing one of the system’s available encodings. This example uses the\nsystem’s default encoding:

\n
import locale\nlocale.setlocale(locale.LC_ALL, '')\ncode = locale.getpreferredencoding()\n
\n
\n

Then use code as the encoding for str.encode() calls.

\n
\n
\n

See also

\n
\n
Module curses.ascii
\n
Utilities for working with ASCII characters, regardless of your locale settings.
\n
Module curses.panel
\n
A panel stack extension that adds depth to curses windows.
\n
Module curses.textpad
\n
Editable text widget for curses supporting Emacs-like bindings.
\n
Curses Programming with Python
\n
Tutorial material on using curses with Python, by Andrew Kuchling and Eric\nRaymond.
\n
\n

The Demo/curses/ directory in the Python source distribution contains\nsome example programs using the curses bindings provided by this module.

\n
\n
\n

15.11.1. Functions

\n

The module curses defines the following exception:

\n
\n
\nexception curses.error
\n
Exception raised when a curses library function returns an error.
\n\n
\n

Note

\n

Whenever x or y arguments to a function or a method are optional, they\ndefault to the current cursor location. Whenever attr is optional, it defaults\nto A_NORMAL.

\n
\n

The module curses defines the following functions:

\n
\n
\ncurses.baudrate()
\n
Return the output speed of the terminal in bits per second. On software\nterminal emulators it will have a fixed high value. Included for historical\nreasons; in former times, it was used to write output loops for time delays and\noccasionally to change interfaces depending on the line speed.
\n\n
\n
\ncurses.beep()
\n
Emit a short attention sound.
\n\n
\n
\ncurses.can_change_color()
\n
Return True or False, depending on whether the programmer can change the colors\ndisplayed by the terminal.
\n\n
\n
\ncurses.cbreak()
\n
Enter cbreak mode. In cbreak mode (sometimes called “rare” mode) normal tty\nline buffering is turned off and characters are available to be read one by one.\nHowever, unlike raw mode, special characters (interrupt, quit, suspend, and flow\ncontrol) retain their effects on the tty driver and calling program. Calling\nfirst raw() then cbreak() leaves the terminal in cbreak mode.
\n\n
\n
\ncurses.color_content(color_number)
\n
Return the intensity of the red, green, and blue (RGB) components in the color\ncolor_number, which must be between 0 and COLORS. A 3-tuple is\nreturned, containing the R,G,B values for the given color, which will be between\n0 (no component) and 1000 (maximum amount of component).
\n\n
\n
\ncurses.color_pair(color_number)
\n
Return the attribute value for displaying text in the specified color. This\nattribute value can be combined with A_STANDOUT, A_REVERSE,\nand the other A_* attributes. pair_number() is the counterpart\nto this function.
\n\n
\n
\ncurses.curs_set(visibility)
\n
Set the cursor state. visibility can be set to 0, 1, or 2, for invisible,\nnormal, or very visible. If the terminal supports the visibility requested, the\nprevious cursor state is returned; otherwise, an exception is raised. On many\nterminals, the “visible” mode is an underline cursor and the “very visible” mode\nis a block cursor.
\n\n
\n
\ncurses.def_prog_mode()
\n
Save the current terminal mode as the “program” mode, the mode when the running\nprogram is using curses. (Its counterpart is the “shell” mode, for when the\nprogram is not in curses.) Subsequent calls to reset_prog_mode() will\nrestore this mode.
\n\n
\n
\ncurses.def_shell_mode()
\n
Save the current terminal mode as the “shell” mode, the mode when the running\nprogram is not using curses. (Its counterpart is the “program” mode, when the\nprogram is using curses capabilities.) Subsequent calls to\nreset_shell_mode() will restore this mode.
\n\n
\n
\ncurses.delay_output(ms)
\n
Insert an ms millisecond pause in output.
\n\n
\n
\ncurses.doupdate()
\n

Update the physical screen. The curses library keeps two data structures, one\nrepresenting the current physical screen contents and a virtual screen\nrepresenting the desired next state. The doupdate() ground updates the\nphysical screen to match the virtual screen.

\n

The virtual screen may be updated by a noutrefresh() call after write\noperations such as addstr() have been performed on a window. The normal\nrefresh() call is simply noutrefresh() followed by doupdate();\nif you have to update multiple windows, you can speed performance and perhaps\nreduce screen flicker by issuing noutrefresh() calls on all windows,\nfollowed by a single doupdate().

\n
\n\n
\n
\ncurses.echo()
\n
Enter echo mode. In echo mode, each character input is echoed to the screen as\nit is entered.
\n\n
\n
\ncurses.endwin()
\n
De-initialize the library, and return terminal to normal status.
\n\n
\n
\ncurses.erasechar()
\n
Return the user’s current erase character. Under Unix operating systems this\nis a property of the controlling tty of the curses program, and is not set by\nthe curses library itself.
\n\n
\n
\ncurses.filter()
\n
The filter() routine, if used, must be called before initscr() is\ncalled. The effect is that, during those calls, LINES is set to 1; the\ncapabilities clear, cup, cud, cud1, cuu1, cuu, vpa are disabled; and the home\nstring is set to the value of cr. The effect is that the cursor is confined to\nthe current line, and so are screen updates. This may be used for enabling\ncharacter-at-a-time line editing without touching the rest of the screen.
\n\n
\n
\ncurses.flash()
\n
Flash the screen. That is, change it to reverse-video and then change it back\nin a short interval. Some people prefer such as ‘visible bell’ to the audible\nattention signal produced by beep().
\n\n
\n
\ncurses.flushinp()
\n
Flush all input buffers. This throws away any typeahead that has been typed\nby the user and has not yet been processed by the program.
\n\n
\n
\ncurses.getmouse()
\n
After getch() returns KEY_MOUSE to signal a mouse event, this\nmethod should be call to retrieve the queued mouse event, represented as a\n5-tuple (id, x, y, z, bstate). id is an ID value used to distinguish\nmultiple devices, and x, y, z are the event’s coordinates. (z is\ncurrently unused.) bstate is an integer value whose bits will be set to\nindicate the type of event, and will be the bitwise OR of one or more of the\nfollowing constants, where n is the button number from 1 to 4:\nBUTTONn_PRESSED, BUTTONn_RELEASED, BUTTONn_CLICKED,\nBUTTONn_DOUBLE_CLICKED, BUTTONn_TRIPLE_CLICKED,\nBUTTON_SHIFT, BUTTON_CTRL, BUTTON_ALT.
\n\n
\n
\ncurses.getsyx()
\n
Return the current coordinates of the virtual screen cursor in y and x. If\nleaveok is currently true, then -1,-1 is returned.
\n\n
\n
\ncurses.getwin(file)
\n
Read window related data stored in the file by an earlier putwin() call.\nThe routine then creates and initializes a new window using that data, returning\nthe new window object.
\n\n
\n
\ncurses.has_colors()
\n
Return True if the terminal can display colors; otherwise, return False.
\n\n
\n
\ncurses.has_ic()
\n
Return True if the terminal has insert- and delete-character capabilities.\nThis function is included for historical reasons only, as all modern software\nterminal emulators have such capabilities.
\n\n
\n
\ncurses.has_il()
\n
Return True if the terminal has insert- and delete-line capabilities, or can\nsimulate them using scrolling regions. This function is included for\nhistorical reasons only, as all modern software terminal emulators have such\ncapabilities.
\n\n
\n
\ncurses.has_key(ch)
\n
Take a key value ch, and return True if the current terminal type recognizes\na key with that value.
\n\n
\n
\ncurses.halfdelay(tenths)
\n
Used for half-delay mode, which is similar to cbreak mode in that characters\ntyped by the user are immediately available to the program. However, after\nblocking for tenths tenths of seconds, an exception is raised if nothing has\nbeen typed. The value of tenths must be a number between 1 and 255. Use\nnocbreak() to leave half-delay mode.
\n\n
\n
\ncurses.init_color(color_number, r, g, b)
\n
Change the definition of a color, taking the number of the color to be changed\nfollowed by three RGB values (for the amounts of red, green, and blue\ncomponents). The value of color_number must be between 0 and\nCOLORS. Each of r, g, b, must be a value between 0 and\n1000. When init_color() is used, all occurrences of that color on the\nscreen immediately change to the new definition. This function is a no-op on\nmost terminals; it is active only if can_change_color() returns 1.
\n\n
\n
\ncurses.init_pair(pair_number, fg, bg)
\n
Change the definition of a color-pair. It takes three arguments: the number of\nthe color-pair to be changed, the foreground color number, and the background\ncolor number. The value of pair_number must be between 1 and\nCOLOR_PAIRS - 1 (the 0 color pair is wired to white on black and cannot\nbe changed). The value of fg and bg arguments must be between 0 and\nCOLORS. If the color-pair was previously initialized, the screen is\nrefreshed and all occurrences of that color-pair are changed to the new\ndefinition.
\n\n
\n
\ncurses.initscr()
\n

Initialize the library. Return a WindowObject which represents the\nwhole screen.

\n
\n

Note

\n

If there is an error opening the terminal, the underlying curses library may\ncause the interpreter to exit.

\n
\n
\n\n
\n
\ncurses.is_term_resized(nlines, ncols)
\n
Return True if resize_term() would modify the window structure,\nFalse otherwise.
\n\n
\n
\ncurses.isendwin()
\n
Return True if endwin() has been called (that is, the curses library has\nbeen deinitialized).
\n\n
\n
\ncurses.keyname(k)
\n
Return the name of the key numbered k. The name of a key generating printable\nASCII character is the key’s character. The name of a control-key combination\nis a two-character string consisting of a caret followed by the corresponding\nprintable ASCII character. The name of an alt-key combination (128-255) is a\nstring consisting of the prefix ‘M-‘ followed by the name of the corresponding\nASCII character.
\n\n
\n
\ncurses.killchar()
\n
Return the user’s current line kill character. Under Unix operating systems\nthis is a property of the controlling tty of the curses program, and is not set\nby the curses library itself.
\n\n
\n
\ncurses.longname()
\n
Return a string containing the terminfo long name field describing the current\nterminal. The maximum length of a verbose description is 128 characters. It is\ndefined only after the call to initscr().
\n\n
\n
\ncurses.meta(yes)
\n
If yes is 1, allow 8-bit characters to be input. If yes is 0, allow only\n7-bit chars.
\n\n
\n
\ncurses.mouseinterval(interval)
\n
Set the maximum time in milliseconds that can elapse between press and release\nevents in order for them to be recognized as a click, and return the previous\ninterval value. The default value is 200 msec, or one fifth of a second.
\n\n
\n
\ncurses.mousemask(mousemask)
\n
Set the mouse events to be reported, and return a tuple (availmask,\noldmask). availmask indicates which of the specified mouse events can be\nreported; on complete failure it returns 0. oldmask is the previous value of\nthe given window’s mouse event mask. If this function is never called, no mouse\nevents are ever reported.
\n\n
\n
\ncurses.napms(ms)
\n
Sleep for ms milliseconds.
\n\n
\n
\ncurses.newpad(nlines, ncols)
\n

Create and return a pointer to a new pad data structure with the given number\nof lines and columns. A pad is returned as a window object.

\n

A pad is like a window, except that it is not restricted by the screen size, and\nis not necessarily associated with a particular part of the screen. Pads can be\nused when a large window is needed, and only a part of the window will be on the\nscreen at one time. Automatic refreshes of pads (such as from scrolling or\nechoing of input) do not occur. The refresh() and noutrefresh()\nmethods of a pad require 6 arguments to specify the part of the pad to be\ndisplayed and the location on the screen to be used for the display. The\narguments are pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol; the p\narguments refer to the upper left corner of the pad region to be displayed and\nthe s arguments define a clipping box on the screen within which the pad region\nis to be displayed.

\n
\n\n
\n
\ncurses.newwin([nlines, ncols], begin_y, begin_x)
\n

Return a new window, whose left-upper corner is at (begin_y, begin_x), and\nwhose height/width is nlines/ncols.

\n

By default, the window will extend from the specified position to the lower\nright corner of the screen.

\n
\n\n
\n
\ncurses.nl()
\n
Enter newline mode. This mode translates the return key into newline on input,\nand translates newline into return and line-feed on output. Newline mode is\ninitially on.
\n\n
\n
\ncurses.nocbreak()
\n
Leave cbreak mode. Return to normal “cooked” mode with line buffering.
\n\n
\n
\ncurses.noecho()
\n
Leave echo mode. Echoing of input characters is turned off.
\n\n
\n
\ncurses.nonl()
\n
Leave newline mode. Disable translation of return into newline on input, and\ndisable low-level translation of newline into newline/return on output (but this\ndoes not change the behavior of addch('\\n'), which always does the\nequivalent of return and line feed on the virtual screen). With translation\noff, curses can sometimes speed up vertical motion a little; also, it will be\nable to detect the return key on input.
\n\n
\n
\ncurses.noqiflush()
\n
When the noqiflush() routine is used, normal flush of input and output queues\nassociated with the INTR, QUIT and SUSP characters will not be done. You may\nwant to call noqiflush() in a signal handler if you want output to\ncontinue as though the interrupt had not occurred, after the handler exits.
\n\n
\n
\ncurses.noraw()
\n
Leave raw mode. Return to normal “cooked” mode with line buffering.
\n\n
\n
\ncurses.pair_content(pair_number)
\n
Return a tuple (fg, bg) containing the colors for the requested color pair.\nThe value of pair_number must be between 1 and COLOR_PAIRS - 1.
\n\n
\n
\ncurses.pair_number(attr)
\n
Return the number of the color-pair set by the attribute value attr.\ncolor_pair() is the counterpart to this function.
\n\n
\n
\ncurses.putp(string)
\n
Equivalent to tputs(str, 1, putchar); emit the value of a specified\nterminfo capability for the current terminal. Note that the output of putp()\nalways goes to standard output.
\n\n
\n
\ncurses.qiflush([flag])
\n
If flag is False, the effect is the same as calling noqiflush(). If\nflag is True, or no argument is provided, the queues will be flushed when\nthese control characters are read.
\n\n
\n
\ncurses.raw()
\n
Enter raw mode. In raw mode, normal line buffering and processing of\ninterrupt, quit, suspend, and flow control keys are turned off; characters are\npresented to curses input functions one by one.
\n\n
\n
\ncurses.reset_prog_mode()
\n
Restore the terminal to “program” mode, as previously saved by\ndef_prog_mode().
\n\n
\n
\ncurses.reset_shell_mode()
\n
Restore the terminal to “shell” mode, as previously saved by\ndef_shell_mode().
\n\n
\n
\ncurses.resetty()
\n
Restore the state of the terminal modes to what it was at the last call to\nsavetty().
\n\n
\n
\ncurses.resize_term(nlines, ncols)
\n
Backend function used by resizeterm(), performing most of the work;\nwhen resizing the windows, resize_term() blank-fills the areas that are\nextended. The calling application should fill in these areas with\nappropriate data. The resize_term() function attempts to resize all\nwindows. However, due to the calling convention of pads, it is not possible\nto resize these without additional interaction with the application.
\n\n
\n
\ncurses.resizeterm(nlines, ncols)
\n
Resize the standard and current windows to the specified dimensions, and\nadjusts other bookkeeping data used by the curses library that record the\nwindow dimensions (in particular the SIGWINCH handler).
\n\n
\n
\ncurses.savetty()
\n
Save the current state of the terminal modes in a buffer, usable by\nresetty().
\n\n
\n
\ncurses.setsyx(y, x)
\n
Set the virtual screen cursor to y, x. If y and x are both -1, then\nleaveok is set.
\n\n
\n
\ncurses.setupterm([termstr, fd])
\n
Initialize the terminal. termstr is a string giving the terminal name; if\nomitted, the value of the TERM environment variable will be used. fd is the\nfile descriptor to which any initialization sequences will be sent; if not\nsupplied, the file descriptor for sys.stdout will be used.
\n\n
\n
\ncurses.start_color()
\n

Must be called if the programmer wants to use colors, and before any other color\nmanipulation routine is called. It is good practice to call this routine right\nafter initscr().

\n

start_color() initializes eight basic colors (black, red, green, yellow,\nblue, magenta, cyan, and white), and two global variables in the curses\nmodule, COLORS and COLOR_PAIRS, containing the maximum number\nof colors and color-pairs the terminal can support. It also restores the colors\non the terminal to the values they had when the terminal was just turned on.

\n
\n\n
\n
\ncurses.termattrs()
\n
Return a logical OR of all video attributes supported by the terminal. This\ninformation is useful when a curses program needs complete control over the\nappearance of the screen.
\n\n
\n
\ncurses.termname()
\n
Return the value of the environment variable TERM, truncated to 14 characters.
\n\n
\n
\ncurses.tigetflag(capname)
\n
Return the value of the Boolean capability corresponding to the terminfo\ncapability name capname. The value -1 is returned if capname is not a\nBoolean capability, or 0 if it is canceled or absent from the terminal\ndescription.
\n\n
\n
\ncurses.tigetnum(capname)
\n
Return the value of the numeric capability corresponding to the terminfo\ncapability name capname. The value -2 is returned if capname is not a\nnumeric capability, or -1 if it is canceled or absent from the terminal\ndescription.
\n\n
\n
\ncurses.tigetstr(capname)
\n
Return the value of the string capability corresponding to the terminfo\ncapability name capname. None is returned if capname is not a string\ncapability, or is canceled or absent from the terminal description.
\n\n
\n
\ncurses.tparm(str[, ...])
\n
Instantiate the string str with the supplied parameters, where str should\nbe a parameterized string obtained from the terminfo database. E.g.\ntparm(tigetstr("cup"), 5, 3) could result in '\\033[6;4H', the exact\nresult depending on terminal type.
\n\n
\n
\ncurses.typeahead(fd)
\n

Specify that the file descriptor fd be used for typeahead checking. If fd\nis -1, then no typeahead checking is done.

\n

The curses library does “line-breakout optimization” by looking for typeahead\nperiodically while updating the screen. If input is found, and it is coming\nfrom a tty, the current update is postponed until refresh or doupdate is called\nagain, allowing faster response to commands typed in advance. This function\nallows specifying a different file descriptor for typeahead checking.

\n
\n\n
\n
\ncurses.unctrl(ch)
\n
Return a string which is a printable representation of the character ch.\nControl characters are displayed as a caret followed by the character, for\nexample as ^C. Printing characters are left as they are.
\n\n
\n
\ncurses.ungetch(ch)
\n

Push ch so the next getch() will return it.

\n
\n

Note

\n

Only one ch can be pushed before getch() is called.

\n
\n
\n\n
\n
\ncurses.ungetmouse(id, x, y, z, bstate)
\n
Push a KEY_MOUSE event onto the input queue, associating the given\nstate data with it.
\n\n
\n
\ncurses.use_env(flag)
\n
If used, this function should be called before initscr() or newterm are\ncalled. When flag is False, the values of lines and columns specified in the\nterminfo database will be used, even if environment variables LINES\nand COLUMNS (used by default) are set, or if curses is running in a\nwindow (in which case default behavior would be to use the window size if\nLINES and COLUMNS are not set).
\n\n
\n
\ncurses.use_default_colors()
\n
Allow use of default values for colors on terminals supporting this feature. Use\nthis to support transparency in your application. The default color is assigned\nto the color number -1. After calling this function, init_pair(x,\ncurses.COLOR_RED, -1) initializes, for instance, color pair x to a red\nforeground color on the default background.
\n\n
\n
\ncurses.wrapper(func, ...)
\n
Initialize curses and call another callable object, func, which should be the\nrest of your curses-using application. If the application raises an exception,\nthis function will restore the terminal to a sane state before re-raising the\nexception and generating a traceback. The callable object func is then passed\nthe main window ‘stdscr’ as its first argument, followed by any other arguments\npassed to wrapper(). Before calling func, wrapper() turns on\ncbreak mode, turns off echo, enables the terminal keypad, and initializes colors\nif the terminal has color support. On exit (whether normally or by exception)\nit restores cooked mode, turns on echo, and disables the terminal keypad.
\n\n
\n
\n

15.11.2. Window Objects

\n

Window objects, as returned by initscr() and newwin() above, have\nthe following methods:

\n
\n
\nwindow.addch([y, x], ch[, attr])
\n
\n

Note

\n

A character means a C character (an ASCII code), rather than a Python\ncharacter (a string of length 1). (This note is true whenever the\ndocumentation mentions a character.) The built-in ord() is handy for\nconveying strings to codes.

\n
\n

Paint character ch at (y, x) with attributes attr, overwriting any\ncharacter previously painter at that location. By default, the character\nposition and attributes are the current settings for the window object.

\n
\n\n
\n
\nwindow.addnstr([y, x], str, n[, attr])
\n
Paint at most n characters of the string str at (y, x) with attributes\nattr, overwriting anything previously on the display.
\n\n
\n
\nwindow.addstr([y, x], str[, attr])
\n
Paint the string str at (y, x) with attributes attr, overwriting\nanything previously on the display.
\n\n
\n
\nwindow.attroff(attr)
\n
Remove attribute attr from the “background” set applied to all writes to the\ncurrent window.
\n\n
\n
\nwindow.attron(attr)
\n
Add attribute attr from the “background” set applied to all writes to the\ncurrent window.
\n\n
\n
\nwindow.attrset(attr)
\n
Set the “background” set of attributes to attr. This set is initially 0 (no\nattributes).
\n\n
\n
\nwindow.bkgd(ch[, attr])
\n

Set the background property of the window to the character ch, with\nattributes attr. The change is then applied to every character position in\nthat window:

\n
    \n
  • The attribute of every character in the window is changed to the new\nbackground attribute.
  • \n
  • Wherever the former background character appears, it is changed to the new\nbackground character.
  • \n
\n
\n\n
\n
\nwindow.bkgdset(ch[, attr])
\n
Set the window’s background. A window’s background consists of a character and\nany combination of attributes. The attribute part of the background is combined\n(OR’ed) with all non-blank characters that are written into the window. Both\nthe character and attribute parts of the background are combined with the blank\ncharacters. The background becomes a property of the character and moves with\nthe character through any scrolling and insert/delete line/character operations.
\n\n
\n
\nwindow.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])
\n

Draw a border around the edges of the window. Each parameter specifies the\ncharacter to use for a specific part of the border; see the table below for more\ndetails. The characters can be specified as integers or as one-character\nstrings.

\n
\n

Note

\n

A 0 value for any parameter will cause the default character to be used for\nthat parameter. Keyword parameters can not be used. The defaults are listed\nin this table:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ParameterDescriptionDefault value
lsLeft sideACS_VLINE
rsRight sideACS_VLINE
tsTopACS_HLINE
bsBottomACS_HLINE
tlUpper-left cornerACS_ULCORNER
trUpper-right cornerACS_URCORNER
blBottom-left cornerACS_LLCORNER
brBottom-right cornerACS_LRCORNER
\n
\n\n
\n
\nwindow.box([vertch, horch])
\n
Similar to border(), but both ls and rs are vertch and both ts and\nbs are horch. The default corner characters are always used by this function.
\n\n
\n
\nwindow.chgat([y, x][, num], attr)
\n
Set the attributes of num characters at the current cursor position, or at\nposition (y, x) if supplied. If no value of num is given or num = -1,\nthe attribute will be set on all the characters to the end of the line. This\nfunction does not move the cursor. The changed line will be touched using the\ntouchline() method so that the contents will be redisplayed by the next\nwindow refresh.
\n\n
\n
\nwindow.clear()
\n
Like erase(), but also cause the whole window to be repainted upon next\ncall to refresh().
\n\n
\n
\nwindow.clearok(yes)
\n
If yes is 1, the next call to refresh() will clear the window\ncompletely.
\n\n
\n
\nwindow.clrtobot()
\n
Erase from cursor to the end of the window: all lines below the cursor are\ndeleted, and then the equivalent of clrtoeol() is performed.
\n\n
\n
\nwindow.clrtoeol()
\n
Erase from cursor to the end of the line.
\n\n
\n
\nwindow.cursyncup()
\n
Update the current cursor position of all the ancestors of the window to\nreflect the current cursor position of the window.
\n\n
\n
\nwindow.delch([y, x])
\n
Delete any character at (y, x).
\n\n
\n
\nwindow.deleteln()
\n
Delete the line under the cursor. All following lines are moved up by one line.
\n\n
\n
\nwindow.derwin([nlines, ncols], begin_y, begin_x)
\n
An abbreviation for “derive window”, derwin() is the same as calling\nsubwin(), except that begin_y and begin_x are relative to the origin\nof the window, rather than relative to the entire screen. Return a window\nobject for the derived window.
\n\n
\n
\nwindow.echochar(ch[, attr])
\n
Add character ch with attribute attr, and immediately call refresh()\non the window.
\n\n
\n
\nwindow.enclose(y, x)
\n
Test whether the given pair of screen-relative character-cell coordinates are\nenclosed by the given window, returning True or False. It is useful for\ndetermining what subset of the screen windows enclose the location of a mouse\nevent.
\n\n
\n
\nwindow.erase()
\n
Clear the window.
\n\n
\n
\nwindow.getbegyx()
\n
Return a tuple (y, x) of co-ordinates of upper-left corner.
\n\n
\n
\nwindow.getbkgd()
\n
Return the given window’s current background character/attribute pair.
\n\n
\n
\nwindow.getch([y, x])
\n
Get a character. Note that the integer returned does not have to be in ASCII\nrange: function keys, keypad keys and so on return numbers higher than 256. In\nno-delay mode, -1 is returned if there is no input, else getch() waits\nuntil a key is pressed.
\n\n
\n
\nwindow.getkey([y, x])
\n
Get a character, returning a string instead of an integer, as getch()\ndoes. Function keys, keypad keys and so on return a multibyte string containing\nthe key name. In no-delay mode, an exception is raised if there is no input.
\n\n
\n
\nwindow.getmaxyx()
\n
Return a tuple (y, x) of the height and width of the window.
\n\n
\n
\nwindow.getparyx()
\n
Return the beginning coordinates of this window relative to its parent window\ninto two integer variables y and x. Return -1, -1 if this window has no\nparent.
\n\n
\n
\nwindow.getstr([y, x])
\n
Read a string from the user, with primitive line editing capacity.
\n\n
\n
\nwindow.getyx()
\n
Return a tuple (y, x) of current cursor position relative to the window’s\nupper-left corner.
\n\n
\n
\nwindow.hline([y, x], ch, n)
\n
Display a horizontal line starting at (y, x) with length n consisting of\nthe character ch.
\n\n
\n
\nwindow.idcok(flag)
\n
If flag is False, curses no longer considers using the hardware insert/delete\ncharacter feature of the terminal; if flag is True, use of character insertion\nand deletion is enabled. When curses is first initialized, use of character\ninsert/delete is enabled by default.
\n\n
\n
\nwindow.idlok(yes)
\n
If called with yes equal to 1, curses will try and use hardware line\nediting facilities. Otherwise, line insertion/deletion are disabled.
\n\n
\n
\nwindow.immedok(flag)
\n
If flag is True, any change in the window image automatically causes the\nwindow to be refreshed; you no longer have to call refresh() yourself.\nHowever, it may degrade performance considerably, due to repeated calls to\nwrefresh. This option is disabled by default.
\n\n
\n
\nwindow.inch([y, x])
\n
Return the character at the given position in the window. The bottom 8 bits are\nthe character proper, and upper bits are the attributes.
\n\n
\n
\nwindow.insch([y, x], ch[, attr])
\n
Paint character ch at (y, x) with attributes attr, moving the line from\nposition x right by one character.
\n\n
\n
\nwindow.insdelln(nlines)
\n
Insert nlines lines into the specified window above the current line. The\nnlines bottom lines are lost. For negative nlines, delete nlines lines\nstarting with the one under the cursor, and move the remaining lines up. The\nbottom nlines lines are cleared. The current cursor position remains the\nsame.
\n\n
\n
\nwindow.insertln()
\n
Insert a blank line under the cursor. All following lines are moved down by one\nline.
\n\n
\n
\nwindow.insnstr([y, x], str, n[, attr])
\n
Insert a character string (as many characters as will fit on the line) before\nthe character under the cursor, up to n characters. If n is zero or\nnegative, the entire string is inserted. All characters to the right of the\ncursor are shifted right, with the rightmost characters on the line being lost.\nThe cursor position does not change (after moving to y, x, if specified).
\n\n
\n
\nwindow.insstr([y, x], str[, attr])
\n
Insert a character string (as many characters as will fit on the line) before\nthe character under the cursor. All characters to the right of the cursor are\nshifted right, with the rightmost characters on the line being lost. The cursor\nposition does not change (after moving to y, x, if specified).
\n\n
\n
\nwindow.instr([y, x][, n])
\n
Return a string of characters, extracted from the window starting at the\ncurrent cursor position, or at y, x if specified. Attributes are stripped\nfrom the characters. If n is specified, instr() returns a string\nat most n characters long (exclusive of the trailing NUL).
\n\n
\n
\nwindow.is_linetouched(line)
\n
Return True if the specified line was modified since the last call to\nrefresh(); otherwise return False. Raise a curses.error\nexception if line is not valid for the given window.
\n\n
\n
\nwindow.is_wintouched()
\n
Return True if the specified window was modified since the last call to\nrefresh(); otherwise return False.
\n\n
\n
\nwindow.keypad(yes)
\n
If yes is 1, escape sequences generated by some keys (keypad, function keys)\nwill be interpreted by curses. If yes is 0, escape sequences will be\nleft as is in the input stream.
\n\n
\n
\nwindow.leaveok(yes)
\n

If yes is 1, cursor is left where it is on update, instead of being at “cursor\nposition.” This reduces cursor movement where possible. If possible the cursor\nwill be made invisible.

\n

If yes is 0, cursor will always be at “cursor position” after an update.

\n
\n\n
\n
\nwindow.move(new_y, new_x)
\n
Move cursor to (new_y, new_x).
\n\n
\n
\nwindow.mvderwin(y, x)
\n
Move the window inside its parent window. The screen-relative parameters of\nthe window are not changed. This routine is used to display different parts of\nthe parent window at the same physical position on the screen.
\n\n
\n
\nwindow.mvwin(new_y, new_x)
\n
Move the window so its upper-left corner is at (new_y, new_x).
\n\n
\n
\nwindow.nodelay(yes)
\n
If yes is 1, getch() will be non-blocking.
\n\n
\n
\nwindow.notimeout(yes)
\n

If yes is 1, escape sequences will not be timed out.

\n

If yes is 0, after a few milliseconds, an escape sequence will not be\ninterpreted, and will be left in the input stream as is.

\n
\n\n
\n
\nwindow.noutrefresh()
\n
Mark for refresh but wait. This function updates the data structure\nrepresenting the desired state of the window, but does not force an update of\nthe physical screen. To accomplish that, call doupdate().
\n\n
\n
\nwindow.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
\n

Overlay the window on top of destwin. The windows need not be the same size,\nonly the overlapping region is copied. This copy is non-destructive, which means\nthat the current background character does not overwrite the old contents of\ndestwin.

\n

To get fine-grained control over the copied region, the second form of\noverlay() can be used. sminrow and smincol are the upper-left\ncoordinates of the source window, and the other variables mark a rectangle in\nthe destination window.

\n
\n\n
\n
\nwindow.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
\n

Overwrite the window on top of destwin. The windows need not be the same size,\nin which case only the overlapping region is copied. This copy is destructive,\nwhich means that the current background character overwrites the old contents of\ndestwin.

\n

To get fine-grained control over the copied region, the second form of\noverwrite() can be used. sminrow and smincol are the upper-left\ncoordinates of the source window, the other variables mark a rectangle in the\ndestination window.

\n
\n\n
\n
\nwindow.putwin(file)
\n
Write all data associated with the window into the provided file object. This\ninformation can be later retrieved using the getwin() function.
\n\n
\n
\nwindow.redrawln(beg, num)
\n
Indicate that the num screen lines, starting at line beg, are corrupted and\nshould be completely redrawn on the next refresh() call.
\n\n
\n
\nwindow.redrawwin()
\n
Touch the entire window, causing it to be completely redrawn on the next\nrefresh() call.
\n\n
\n
\nwindow.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
\n

Update the display immediately (sync actual screen with previous\ndrawing/deleting methods).

\n

The 6 optional arguments can only be specified when the window is a pad created\nwith newpad(). The additional parameters are needed to indicate what part\nof the pad and screen are involved. pminrow and pmincol specify the upper\nleft-hand corner of the rectangle to be displayed in the pad. sminrow,\nsmincol, smaxrow, and smaxcol specify the edges of the rectangle to be\ndisplayed on the screen. The lower right-hand corner of the rectangle to be\ndisplayed in the pad is calculated from the screen coordinates, since the\nrectangles must be the same size. Both rectangles must be entirely contained\nwithin their respective structures. Negative values of pminrow, pmincol,\nsminrow, or smincol are treated as if they were zero.

\n
\n\n
\n
\nwindow.resize(nlines, ncols)
\n
Reallocate storage for a curses window to adjust its dimensions to the\nspecified values. If either dimension is larger than the current values, the\nwindow’s data is filled with blanks that have the current background\nrendition (as set by bkgdset()) merged into them.
\n\n
\n
\nwindow.scroll([lines=1])
\n
Scroll the screen or scrolling region upward by lines lines.
\n\n
\n
\nwindow.scrollok(flag)
\n
Control what happens when the cursor of a window is moved off the edge of the\nwindow or scrolling region, either as a result of a newline action on the bottom\nline, or typing the last character of the last line. If flag is false, the\ncursor is left on the bottom line. If flag is true, the window is scrolled up\none line. Note that in order to get the physical scrolling effect on the\nterminal, it is also necessary to call idlok().
\n\n
\n
\nwindow.setscrreg(top, bottom)
\n
Set the scrolling region from line top to line bottom. All scrolling actions\nwill take place in this region.
\n\n
\n
\nwindow.standend()
\n
Turn off the standout attribute. On some terminals this has the side effect of\nturning off all attributes.
\n\n
\n
\nwindow.standout()
\n
Turn on attribute A_STANDOUT.
\n\n
\n
\nwindow.subpad([nlines, ncols], begin_y, begin_x)
\n
Return a sub-window, whose upper-left corner is at (begin_y, begin_x), and\nwhose width/height is ncols/nlines.
\n\n
\n
\nwindow.subwin([nlines, ncols], begin_y, begin_x)
\n

Return a sub-window, whose upper-left corner is at (begin_y, begin_x), and\nwhose width/height is ncols/nlines.

\n

By default, the sub-window will extend from the specified position to the lower\nright corner of the window.

\n
\n\n
\n
\nwindow.syncdown()
\n
Touch each location in the window that has been touched in any of its ancestor\nwindows. This routine is called by refresh(), so it should almost never\nbe necessary to call it manually.
\n\n
\n
\nwindow.syncok(flag)
\n
If called with flag set to True, then syncup() is called automatically\nwhenever there is a change in the window.
\n\n
\n
\nwindow.syncup()
\n
Touch all locations in ancestors of the window that have been changed in the\nwindow.
\n\n
\n
\nwindow.timeout(delay)
\n
Set blocking or non-blocking read behavior for the window. If delay is\nnegative, blocking read is used (which will wait indefinitely for input). If\ndelay is zero, then non-blocking read is used, and -1 will be returned by\ngetch() if no input is waiting. If delay is positive, then\ngetch() will block for delay milliseconds, and return -1 if there is\nstill no input at the end of that time.
\n\n
\n
\nwindow.touchline(start, count[, changed])
\n
Pretend count lines have been changed, starting with line start. If\nchanged is supplied, it specifies whether the affected lines are marked as\nhaving been changed (changed=1) or unchanged (changed=0).
\n\n
\n
\nwindow.touchwin()
\n
Pretend the whole window has been changed, for purposes of drawing\noptimizations.
\n\n
\n
\nwindow.untouchwin()
\n
Mark all lines in the window as unchanged since the last call to\nrefresh().
\n\n
\n
\nwindow.vline([y, x], ch, n)
\n
Display a vertical line starting at (y, x) with length n consisting of the\ncharacter ch.
\n\n
\n
\n

15.11.3. Constants

\n

The curses module defines the following data members:

\n
\n
\ncurses.ERR
\n
Some curses routines that return an integer, such as getch(), return\nERR upon failure.
\n\n
\n
\ncurses.OK
\n
Some curses routines that return an integer, such as napms(), return\nOK upon success.
\n\n
\n
\ncurses.version
\n
A string representing the current version of the module. Also available as\n__version__.
\n\n

Several constants are available to specify character cell attributes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeMeaning
A_ALTCHARSETAlternate character set mode.
A_BLINKBlink mode.
A_BOLDBold mode.
A_DIMDim mode.
A_NORMALNormal attribute.
A_REVERSEReverse background and\nforeground colors.
A_STANDOUTStandout mode.
A_UNDERLINEUnderline mode.
\n

Keys are referred to by integer constants with names starting with KEY_.\nThe exact keycaps available are system dependent.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Key constantKey
KEY_MINMinimum key value
KEY_BREAKBreak key (unreliable)
KEY_DOWNDown-arrow
KEY_UPUp-arrow
KEY_LEFTLeft-arrow
KEY_RIGHTRight-arrow
KEY_HOMEHome key (upward+left arrow)
KEY_BACKSPACEBackspace (unreliable)
KEY_F0Function keys. Up to 64 function keys are\nsupported.
KEY_FnValue of function key n
KEY_DLDelete line
KEY_ILInsert line
KEY_DCDelete character
KEY_ICInsert char or enter insert mode
KEY_EICExit insert char mode
KEY_CLEARClear screen
KEY_EOSClear to end of screen
KEY_EOLClear to end of line
KEY_SFScroll 1 line forward
KEY_SRScroll 1 line backward (reverse)
KEY_NPAGENext page
KEY_PPAGEPrevious page
KEY_STABSet tab
KEY_CTABClear tab
KEY_CATABClear all tabs
KEY_ENTEREnter or send (unreliable)
KEY_SRESETSoft (partial) reset (unreliable)
KEY_RESETReset or hard reset (unreliable)
KEY_PRINTPrint
KEY_LLHome down or bottom (lower left)
KEY_A1Upper left of keypad
KEY_A3Upper right of keypad
KEY_B2Center of keypad
KEY_C1Lower left of keypad
KEY_C3Lower right of keypad
KEY_BTABBack tab
KEY_BEGBeg (beginning)
KEY_CANCELCancel
KEY_CLOSEClose
KEY_COMMANDCmd (command)
KEY_COPYCopy
KEY_CREATECreate
KEY_ENDEnd
KEY_EXITExit
KEY_FINDFind
KEY_HELPHelp
KEY_MARKMark
KEY_MESSAGEMessage
KEY_MOVEMove
KEY_NEXTNext
KEY_OPENOpen
KEY_OPTIONSOptions
KEY_PREVIOUSPrev (previous)
KEY_REDORedo
KEY_REFERENCERef (reference)
KEY_REFRESHRefresh
KEY_REPLACEReplace
KEY_RESTARTRestart
KEY_RESUMEResume
KEY_SAVESave
KEY_SBEGShifted Beg (beginning)
KEY_SCANCELShifted Cancel
KEY_SCOMMANDShifted Command
KEY_SCOPYShifted Copy
KEY_SCREATEShifted Create
KEY_SDCShifted Delete char
KEY_SDLShifted Delete line
KEY_SELECTSelect
KEY_SENDShifted End
KEY_SEOLShifted Clear line
KEY_SEXITShifted Dxit
KEY_SFINDShifted Find
KEY_SHELPShifted Help
KEY_SHOMEShifted Home
KEY_SICShifted Input
KEY_SLEFTShifted Left arrow
KEY_SMESSAGEShifted Message
KEY_SMOVEShifted Move
KEY_SNEXTShifted Next
KEY_SOPTIONSShifted Options
KEY_SPREVIOUSShifted Prev
KEY_SPRINTShifted Print
KEY_SREDOShifted Redo
KEY_SREPLACEShifted Replace
KEY_SRIGHTShifted Right arrow
KEY_SRSUMEShifted Resume
KEY_SSAVEShifted Save
KEY_SSUSPENDShifted Suspend
KEY_SUNDOShifted Undo
KEY_SUSPENDSuspend
KEY_UNDOUndo
KEY_MOUSEMouse event has occurred
KEY_RESIZETerminal resize event
KEY_MAXMaximum key value
\n

On VT100s and their software emulations, such as X terminal emulators, there are\nnormally at least four function keys (KEY_F1, KEY_F2,\nKEY_F3, KEY_F4) available, and the arrow keys mapped to\nKEY_UP, KEY_DOWN, KEY_LEFT and KEY_RIGHT in\nthe obvious way. If your machine has a PC keyboard, it is safe to expect arrow\nkeys and twelve function keys (older PC keyboards may have only ten function\nkeys); also, the following keypad mappings are standard:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
KeycapConstant
InsertKEY_IC
DeleteKEY_DC
HomeKEY_HOME
EndKEY_END
Page UpKEY_NPAGE
Page DownKEY_PPAGE
\n

The following table lists characters from the alternate character set. These are\ninherited from the VT100 terminal, and will generally be available on software\nemulations such as X terminals. When there is no graphic available, curses\nfalls back on a crude printable ASCII approximation.

\n
\n

Note

\n

These are available only after initscr() has been called.

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ACS codeMeaning
ACS_BBSSalternate name for upper right corner
ACS_BLOCKsolid square block
ACS_BOARDboard of squares
ACS_BSBSalternate name for horizontal line
ACS_BSSBalternate name for upper left corner
ACS_BSSSalternate name for top tee
ACS_BTEEbottom tee
ACS_BULLETbullet
ACS_CKBOARDchecker board (stipple)
ACS_DARROWarrow pointing down
ACS_DEGREEdegree symbol
ACS_DIAMONDdiamond
ACS_GEQUALgreater-than-or-equal-to
ACS_HLINEhorizontal line
ACS_LANTERNlantern symbol
ACS_LARROWleft arrow
ACS_LEQUALless-than-or-equal-to
ACS_LLCORNERlower left-hand corner
ACS_LRCORNERlower right-hand corner
ACS_LTEEleft tee
ACS_NEQUALnot-equal sign
ACS_PIletter pi
ACS_PLMINUSplus-or-minus sign
ACS_PLUSbig plus sign
ACS_RARROWright arrow
ACS_RTEEright tee
ACS_S1scan line 1
ACS_S3scan line 3
ACS_S7scan line 7
ACS_S9scan line 9
ACS_SBBSalternate name for lower right corner
ACS_SBSBalternate name for vertical line
ACS_SBSSalternate name for right tee
ACS_SSBBalternate name for lower left corner
ACS_SSBSalternate name for bottom tee
ACS_SSSBalternate name for left tee
ACS_SSSSalternate name for crossover or big plus
ACS_STERLINGpound sterling
ACS_TTEEtop tee
ACS_UARROWup arrow
ACS_ULCORNERupper left corner
ACS_URCORNERupper right corner
ACS_VLINEvertical line
\n

The following table lists the predefined colors:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantColor
COLOR_BLACKBlack
COLOR_BLUEBlue
COLOR_CYANCyan (light greenish blue)
COLOR_GREENGreen
COLOR_MAGENTAMagenta (purplish red)
COLOR_REDRed
COLOR_WHITEWhite
COLOR_YELLOWYellow
\n
\n
\n

15.12. curses.textpad — Text input widget for curses programs

\n

\nNew in version 1.6.

\n

The curses.textpad module provides a Textbox class that handles\nelementary text editing in a curses window, supporting a set of keybindings\nresembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,\nFrameMaker, and many other programs). The module also provides a\nrectangle-drawing function useful for framing text boxes or for other purposes.

\n

The module curses.textpad defines the following function:

\n
\n
\ncurses.textpad.rectangle(win, uly, ulx, lry, lrx)
\n
Draw a rectangle. The first argument must be a window object; the remaining\narguments are coordinates relative to that window. The second and third\narguments are the y and x coordinates of the upper left hand corner of the\nrectangle to be drawn; the fourth and fifth arguments are the y and x\ncoordinates of the lower right hand corner. The rectangle will be drawn using\nVT100/IBM PC forms characters on terminals that make this possible (including\nxterm and most other software terminal emulators). Otherwise it will be drawn\nwith ASCII dashes, vertical bars, and plus signs.
\n\n
\n

15.12.1. Textbox objects

\n

You can instantiate a Textbox object as follows:

\n
\n
\nclass curses.textpad.Textbox(win)
\n

Return a textbox widget object. The win argument should be a curses\nWindowObject in which the textbox is to be contained. The edit cursor\nof the textbox is initially located at the upper left hand corner of the\ncontaining window, with coordinates (0, 0). The instance’s\nstripspaces flag is initially on.

\n

Textbox objects have the following methods:

\n
\n
\nedit([validator])
\n
This is the entry point you will normally use. It accepts editing\nkeystrokes until one of the termination keystrokes is entered. If\nvalidator is supplied, it must be a function. It will be called for\neach keystroke entered with the keystroke as a parameter; command dispatch\nis done on the result. This method returns the window contents as a\nstring; whether blanks in the window are included is affected by the\nstripspaces attribute.
\n\n
\n
\ndo_command(ch)
\n

Process a single command keystroke. Here are the supported special\nkeystrokes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
KeystrokeAction
Control-AGo to left edge of window.
Control-BCursor left, wrapping to previous line if\nappropriate.
Control-DDelete character under cursor.
Control-EGo to right edge (stripspaces off) or end\nof line (stripspaces on).
Control-FCursor right, wrapping to next line when\nappropriate.
Control-GTerminate, returning the window contents.
Control-HDelete character backward.
Control-JTerminate if the window is 1 line,\notherwise insert newline.
Control-KIf line is blank, delete it, otherwise\nclear to end of line.
Control-LRefresh screen.
Control-NCursor down; move down one line.
Control-OInsert a blank line at cursor location.
Control-PCursor up; move up one line.
\n

Move operations do nothing if the cursor is at an edge where the movement\nis not possible. The following synonyms are supported where possible:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantKeystroke
KEY_LEFTControl-B
KEY_RIGHTControl-F
KEY_UPControl-P
KEY_DOWNControl-N
KEY_BACKSPACEControl-h
\n

All other keystrokes are treated as a command to insert the given\ncharacter and move right (with line wrapping).

\n
\n\n
\n
\ngather()
\n
Return the window contents as a string; whether blanks in the\nwindow are included is affected by the stripspaces member.
\n\n
\n
\nstripspaces
\n
This attribute is a flag which controls the interpretation of blanks in\nthe window. When it is on, trailing blanks on each line are ignored; any\ncursor motion that would land the cursor on a trailing blank goes to the\nend of that line instead, and trailing blanks are stripped when the window\ncontents are gathered.
\n\n
\n\n
\n
", "searchableItems": [ { "name": "curses.baudrate", "domId": "curses_curses.baudrate" }, { "name": "curses.beep", "domId": "curses_curses.beep" }, { "name": "curses.can_change_color", "domId": "curses_curses.can_change_color" }, { "name": "curses.cbreak", "domId": "curses_curses.cbreak" }, { "name": "curses.color_content", "domId": "curses_curses.color_content" }, { "name": "curses.color_pair", "domId": "curses_curses.color_pair" }, { "name": "curses.curs_set", "domId": "curses_curses.curs_set" }, { "name": "curses.def_prog_mode", "domId": "curses_curses.def_prog_mode" }, { "name": "curses.def_shell_mode", "domId": "curses_curses.def_shell_mode" }, { "name": "curses.delay_output", "domId": "curses_curses.delay_output" }, { "name": "curses.doupdate", "domId": "curses_curses.doupdate" }, { "name": "curses.echo", "domId": "curses_curses.echo" }, { "name": "curses.endwin", "domId": "curses_curses.endwin" }, { "name": "curses.erasechar", "domId": "curses_curses.erasechar" }, { "name": "curses.filter", "domId": "curses_curses.filter" }, { "name": "curses.flash", "domId": "curses_curses.flash" }, { "name": "curses.flushinp", "domId": "curses_curses.flushinp" }, { "name": "curses.getmouse", "domId": "curses_curses.getmouse" }, { "name": "curses.getsyx", "domId": "curses_curses.getsyx" }, { "name": "curses.getwin", "domId": "curses_curses.getwin" }, { "name": "curses.halfdelay", "domId": "curses_curses.halfdelay" }, { "name": "curses.has_colors", "domId": "curses_curses.has_colors" }, { "name": "curses.has_ic", "domId": "curses_curses.has_ic" }, { "name": "curses.has_il", "domId": "curses_curses.has_il" }, { "name": "curses.has_key", "domId": "curses_curses.has_key" }, { "name": "curses.init_color", "domId": "curses_curses.init_color" }, { "name": "curses.init_pair", "domId": "curses_curses.init_pair" }, { "name": "curses.initscr", "domId": "curses_curses.initscr" }, { "name": "curses.is_term_resized", "domId": "curses_curses.is_term_resized" }, { "name": "curses.isendwin", "domId": "curses_curses.isendwin" }, { "name": "curses.keyname", "domId": "curses_curses.keyname" }, { "name": "curses.killchar", "domId": "curses_curses.killchar" }, { "name": "curses.longname", "domId": "curses_curses.longname" }, { "name": "curses.meta", "domId": "curses_curses.meta" }, { "name": "curses.mouseinterval", "domId": "curses_curses.mouseinterval" }, { "name": "curses.mousemask", "domId": "curses_curses.mousemask" }, { "name": "curses.napms", "domId": "curses_curses.napms" }, { "name": "curses.newpad", "domId": "curses_curses.newpad" }, { "name": "curses.newwin", "domId": "curses_curses.newwin" }, { "name": "curses.nl", "domId": "curses_curses.nl" }, { "name": "curses.nocbreak", "domId": "curses_curses.nocbreak" }, { "name": "curses.noecho", "domId": "curses_curses.noecho" }, { "name": "curses.nonl", "domId": "curses_curses.nonl" }, { "name": "curses.noqiflush", "domId": "curses_curses.noqiflush" }, { "name": "curses.noraw", "domId": "curses_curses.noraw" }, { "name": "curses.pair_content", "domId": "curses_curses.pair_content" }, { "name": "curses.pair_number", "domId": "curses_curses.pair_number" }, { "name": "curses.putp", "domId": "curses_curses.putp" }, { "name": "curses.qiflush", "domId": "curses_curses.qiflush" }, { "name": "curses.raw", "domId": "curses_curses.raw" }, { "name": "curses.reset_prog_mode", "domId": "curses_curses.reset_prog_mode" }, { "name": "curses.reset_shell_mode", "domId": "curses_curses.reset_shell_mode" }, { "name": "curses.resetty", "domId": "curses_curses.resetty" }, { "name": "curses.resize_term", "domId": "curses_curses.resize_term" }, { "name": "curses.resizeterm", "domId": "curses_curses.resizeterm" }, { "name": "curses.savetty", "domId": "curses_curses.savetty" }, { "name": "curses.setsyx", "domId": "curses_curses.setsyx" }, { "name": "curses.setupterm", "domId": "curses_curses.setupterm" }, { "name": "curses.start_color", "domId": "curses_curses.start_color" }, { "name": "curses.termattrs", "domId": "curses_curses.termattrs" }, { "name": "curses.termname", "domId": "curses_curses.termname" }, { "name": "curses.textpad.rectangle", "domId": "curses_curses.textpad.rectangle" }, { "name": "curses.textpad.Textbox", "domId": "curses_curses.textpad.Textbox" }, { "name": "curses.textpad.Textbox.do_command", "domId": "curses_curses.textpad.Textbox.do_command" }, { "name": "curses.textpad.Textbox.edit", "domId": "curses_curses.textpad.Textbox.edit" }, { "name": "curses.textpad.Textbox.gather", "domId": "curses_curses.textpad.Textbox.gather" }, { "name": "curses.tigetflag", "domId": "curses_curses.tigetflag" }, { "name": "curses.tigetnum", "domId": "curses_curses.tigetnum" }, { "name": "curses.tigetstr", "domId": "curses_curses.tigetstr" }, { "name": "curses.tparm", "domId": "curses_curses.tparm" }, { "name": "curses.typeahead", "domId": "curses_curses.typeahead" }, { "name": "curses.unctrl", "domId": "curses_curses.unctrl" }, { "name": "curses.ungetch", "domId": "curses_curses.ungetch" }, { "name": "curses.ungetmouse", "domId": "curses_curses.ungetmouse" }, { "name": "curses.use_default_colors", "domId": "curses_curses.use_default_colors" }, { "name": "curses.use_env", "domId": "curses_curses.use_env" }, { "name": "curses.window.addch", "domId": "curses_curses.window.addch" }, { "name": "curses.window.addnstr", "domId": "curses_curses.window.addnstr" }, { "name": "curses.window.addstr", "domId": "curses_curses.window.addstr" }, { "name": "curses.window.attroff", "domId": "curses_curses.window.attroff" }, { "name": "curses.window.attron", "domId": "curses_curses.window.attron" }, { "name": "curses.window.attrset", "domId": "curses_curses.window.attrset" }, { "name": "curses.window.bkgd", "domId": "curses_curses.window.bkgd" }, { "name": "curses.window.bkgdset", "domId": "curses_curses.window.bkgdset" }, { "name": "curses.window.border", "domId": "curses_curses.window.border" }, { "name": "curses.window.box", "domId": "curses_curses.window.box" }, { "name": "curses.window.chgat", "domId": "curses_curses.window.chgat" }, { "name": "curses.window.clear", "domId": "curses_curses.window.clear" }, { "name": "curses.window.clearok", "domId": "curses_curses.window.clearok" }, { "name": "curses.window.clrtobot", "domId": "curses_curses.window.clrtobot" }, { "name": "curses.window.clrtoeol", "domId": "curses_curses.window.clrtoeol" }, { "name": "curses.window.cursyncup", "domId": "curses_curses.window.cursyncup" }, { "name": "curses.window.delch", "domId": "curses_curses.window.delch" }, { "name": "curses.window.deleteln", "domId": "curses_curses.window.deleteln" }, { "name": "curses.window.derwin", "domId": "curses_curses.window.derwin" }, { "name": "curses.window.echochar", "domId": "curses_curses.window.echochar" }, { "name": "curses.window.enclose", "domId": "curses_curses.window.enclose" }, { "name": "curses.window.erase", "domId": "curses_curses.window.erase" }, { "name": "curses.window.getbegyx", "domId": "curses_curses.window.getbegyx" }, { "name": "curses.window.getbkgd", "domId": "curses_curses.window.getbkgd" }, { "name": "curses.window.getch", "domId": "curses_curses.window.getch" }, { "name": "curses.window.getkey", "domId": "curses_curses.window.getkey" }, { "name": "curses.window.getmaxyx", "domId": "curses_curses.window.getmaxyx" }, { "name": "curses.window.getparyx", "domId": "curses_curses.window.getparyx" }, { "name": "curses.window.getstr", "domId": "curses_curses.window.getstr" }, { "name": "curses.window.getyx", "domId": "curses_curses.window.getyx" }, { "name": "curses.window.hline", "domId": "curses_curses.window.hline" }, { "name": "curses.window.idcok", "domId": "curses_curses.window.idcok" }, { "name": "curses.window.idlok", "domId": "curses_curses.window.idlok" }, { "name": "curses.window.immedok", "domId": "curses_curses.window.immedok" }, { "name": "curses.window.inch", "domId": "curses_curses.window.inch" }, { "name": "curses.window.insch", "domId": "curses_curses.window.insch" }, { "name": "curses.window.insdelln", "domId": "curses_curses.window.insdelln" }, { "name": "curses.window.insertln", "domId": "curses_curses.window.insertln" }, { "name": "curses.window.insnstr", "domId": "curses_curses.window.insnstr" }, { "name": "curses.window.insstr", "domId": "curses_curses.window.insstr" }, { "name": "curses.window.instr", "domId": "curses_curses.window.instr" }, { "name": "curses.window.is_linetouched", "domId": "curses_curses.window.is_linetouched" }, { "name": "curses.window.is_wintouched", "domId": "curses_curses.window.is_wintouched" }, { "name": "curses.window.keypad", "domId": "curses_curses.window.keypad" }, { "name": "curses.window.leaveok", "domId": "curses_curses.window.leaveok" }, { "name": "curses.window.move", "domId": "curses_curses.window.move" }, { "name": "curses.window.mvderwin", "domId": "curses_curses.window.mvderwin" }, { "name": "curses.window.mvwin", "domId": "curses_curses.window.mvwin" }, { "name": "curses.window.nodelay", "domId": "curses_curses.window.nodelay" }, { "name": "curses.window.notimeout", "domId": "curses_curses.window.notimeout" }, { "name": "curses.window.noutrefresh", "domId": "curses_curses.window.noutrefresh" }, { "name": "curses.window.overlay", "domId": "curses_curses.window.overlay" }, { "name": "curses.window.overwrite", "domId": "curses_curses.window.overwrite" }, { "name": "curses.window.putwin", "domId": "curses_curses.window.putwin" }, { "name": "curses.window.redrawln", "domId": "curses_curses.window.redrawln" }, { "name": "curses.window.redrawwin", "domId": "curses_curses.window.redrawwin" }, { "name": "curses.window.refresh", "domId": "curses_curses.window.refresh" }, { "name": "curses.window.resize", "domId": "curses_curses.window.resize" }, { "name": "curses.window.scroll", "domId": "curses_curses.window.scroll" }, { "name": "curses.window.scrollok", "domId": "curses_curses.window.scrollok" }, { "name": "curses.window.setscrreg", "domId": "curses_curses.window.setscrreg" }, { "name": "curses.window.standend", "domId": "curses_curses.window.standend" }, { "name": "curses.window.standout", "domId": "curses_curses.window.standout" }, { "name": "curses.window.subpad", "domId": "curses_curses.window.subpad" }, { "name": "curses.window.subwin", "domId": "curses_curses.window.subwin" }, { "name": "curses.window.syncdown", "domId": "curses_curses.window.syncdown" }, { "name": "curses.window.syncok", "domId": "curses_curses.window.syncok" }, { "name": "curses.window.syncup", "domId": "curses_curses.window.syncup" }, { "name": "curses.window.timeout", "domId": "curses_curses.window.timeout" }, { "name": "curses.window.touchline", "domId": "curses_curses.window.touchline" }, { "name": "curses.window.touchwin", "domId": "curses_curses.window.touchwin" }, { "name": "curses.window.untouchwin", "domId": "curses_curses.window.untouchwin" }, { "name": "curses.window.vline", "domId": "curses_curses.window.vline" }, { "name": "curses.wrapper", "domId": "curses_curses.wrapper" } ] }, { "url": "http://docs.python.org/library/errno.html", "title": "errno", "html": "
\n

15.16. errno — Standard errno system symbols

\n

This module makes available standard errno system symbols. The value of each\nsymbol is the corresponding integer value. The names and descriptions are\nborrowed from linux/include/errno.h, which should be pretty\nall-inclusive.

\n
\n
\nerrno.errorcode
\n
Dictionary providing a mapping from the errno value to the string name in the\nunderlying system. For instance, errno.errorcode[errno.EPERM] maps to\n'EPERM'.
\n\n

To translate a numeric error code to an error message, use os.strerror().

\n

Of the following list, symbols that are not used on the current platform are not\ndefined by the module. The specific list of defined symbols is available as\nerrno.errorcode.keys(). Symbols available can include:

\n
\n
\nerrno.EPERM
\n
Operation not permitted
\n\n
\n
\nerrno.ENOENT
\n
No such file or directory
\n\n
\n
\nerrno.ESRCH
\n
No such process
\n\n
\n
\nerrno.EINTR
\n
Interrupted system call
\n\n
\n
\nerrno.EIO
\n
I/O error
\n\n
\n
\nerrno.ENXIO
\n
No such device or address
\n\n
\n
\nerrno.E2BIG
\n
Arg list too long
\n\n
\n
\nerrno.ENOEXEC
\n
Exec format error
\n\n
\n
\nerrno.EBADF
\n
Bad file number
\n\n
\n
\nerrno.ECHILD
\n
No child processes
\n\n
\n
\nerrno.EAGAIN
\n
Try again
\n\n
\n
\nerrno.ENOMEM
\n
Out of memory
\n\n
\n
\nerrno.EACCES
\n
Permission denied
\n\n
\n
\nerrno.EFAULT
\n
Bad address
\n\n
\n
\nerrno.ENOTBLK
\n
Block device required
\n\n
\n
\nerrno.EBUSY
\n
Device or resource busy
\n\n
\n
\nerrno.EEXIST
\n
File exists
\n\n
\n
\nerrno.EXDEV
\n
Cross-device link
\n\n
\n
\nerrno.ENODEV
\n
No such device
\n\n
\n
\nerrno.ENOTDIR
\n
Not a directory
\n\n
\n
\nerrno.EISDIR
\n
Is a directory
\n\n
\n
\nerrno.EINVAL
\n
Invalid argument
\n\n
\n
\nerrno.ENFILE
\n
File table overflow
\n\n
\n
\nerrno.EMFILE
\n
Too many open files
\n\n
\n
\nerrno.ENOTTY
\n
Not a typewriter
\n\n
\n
\nerrno.ETXTBSY
\n
Text file busy
\n\n
\n
\nerrno.EFBIG
\n
File too large
\n\n
\n
\nerrno.ENOSPC
\n
No space left on device
\n\n
\n
\nerrno.ESPIPE
\n
Illegal seek
\n\n
\n
\nerrno.EROFS
\n
Read-only file system
\n\n
\n
\nerrno.EMLINK
\n
Too many links
\n\n
\n
\nerrno.EPIPE
\n
Broken pipe
\n\n
\n
\nerrno.EDOM
\n
Math argument out of domain of func
\n\n
\n
\nerrno.ERANGE
\n
Math result not representable
\n\n
\n
\nerrno.EDEADLK
\n
Resource deadlock would occur
\n\n
\n
\nerrno.ENAMETOOLONG
\n
File name too long
\n\n
\n
\nerrno.ENOLCK
\n
No record locks available
\n\n
\n
\nerrno.ENOSYS
\n
Function not implemented
\n\n
\n
\nerrno.ENOTEMPTY
\n
Directory not empty
\n\n
\n
\nerrno.ELOOP
\n
Too many symbolic links encountered
\n\n
\n
\nerrno.EWOULDBLOCK
\n
Operation would block
\n\n
\n
\nerrno.ENOMSG
\n
No message of desired type
\n\n
\n
\nerrno.EIDRM
\n
Identifier removed
\n\n
\n
\nerrno.ECHRNG
\n
Channel number out of range
\n\n
\n
\nerrno.EL2NSYNC
\n
Level 2 not synchronized
\n\n
\n
\nerrno.EL3HLT
\n
Level 3 halted
\n\n
\n
\nerrno.EL3RST
\n
Level 3 reset
\n\n
\n
\nerrno.ELNRNG
\n
Link number out of range
\n\n
\n
\nerrno.EUNATCH
\n
Protocol driver not attached
\n\n
\n
\nerrno.ENOCSI
\n
No CSI structure available
\n\n
\n
\nerrno.EL2HLT
\n
Level 2 halted
\n\n
\n
\nerrno.EBADE
\n
Invalid exchange
\n\n
\n
\nerrno.EBADR
\n
Invalid request descriptor
\n\n
\n
\nerrno.EXFULL
\n
Exchange full
\n\n
\n
\nerrno.ENOANO
\n
No anode
\n\n
\n
\nerrno.EBADRQC
\n
Invalid request code
\n\n
\n
\nerrno.EBADSLT
\n
Invalid slot
\n\n
\n
\nerrno.EDEADLOCK
\n
File locking deadlock error
\n\n
\n
\nerrno.EBFONT
\n
Bad font file format
\n\n
\n
\nerrno.ENOSTR
\n
Device not a stream
\n\n
\n
\nerrno.ENODATA
\n
No data available
\n\n
\n
\nerrno.ETIME
\n
Timer expired
\n\n
\n
\nerrno.ENOSR
\n
Out of streams resources
\n\n
\n
\nerrno.ENONET
\n
Machine is not on the network
\n\n
\n
\nerrno.ENOPKG
\n
Package not installed
\n\n
\n
\nerrno.EREMOTE
\n
Object is remote
\n\n
\n
\nerrno.ENOLINK
\n
Link has been severed
\n\n
\n
\nerrno.EADV
\n
Advertise error
\n\n
\n
\nerrno.ESRMNT
\n
Srmount error
\n\n
\n
\nerrno.ECOMM
\n
Communication error on send
\n\n
\n
\nerrno.EPROTO
\n
Protocol error
\n\n
\n
\nerrno.EMULTIHOP
\n
Multihop attempted
\n\n
\n
\nerrno.EDOTDOT
\n
RFS specific error
\n\n
\n
\nerrno.EBADMSG
\n
Not a data message
\n\n
\n
\nerrno.EOVERFLOW
\n
Value too large for defined data type
\n\n
\n
\nerrno.ENOTUNIQ
\n
Name not unique on network
\n\n
\n
\nerrno.EBADFD
\n
File descriptor in bad state
\n\n
\n
\nerrno.EREMCHG
\n
Remote address changed
\n\n
\n
\nerrno.ELIBACC
\n
Can not access a needed shared library
\n\n
\n
\nerrno.ELIBBAD
\n
Accessing a corrupted shared library
\n\n
\n
\nerrno.ELIBSCN
\n
.lib section in a.out corrupted
\n\n
\n
\nerrno.ELIBMAX
\n
Attempting to link in too many shared libraries
\n\n
\n
\nerrno.ELIBEXEC
\n
Cannot exec a shared library directly
\n\n
\n
\nerrno.EILSEQ
\n
Illegal byte sequence
\n\n
\n
\nerrno.ERESTART
\n
Interrupted system call should be restarted
\n\n
\n
\nerrno.ESTRPIPE
\n
Streams pipe error
\n\n
\n
\nerrno.EUSERS
\n
Too many users
\n\n
\n
\nerrno.ENOTSOCK
\n
Socket operation on non-socket
\n\n
\n
\nerrno.EDESTADDRREQ
\n
Destination address required
\n\n
\n
\nerrno.EMSGSIZE
\n
Message too long
\n\n
\n
\nerrno.EPROTOTYPE
\n
Protocol wrong type for socket
\n\n
\n
\nerrno.ENOPROTOOPT
\n
Protocol not available
\n\n
\n
\nerrno.EPROTONOSUPPORT
\n
Protocol not supported
\n\n
\n
\nerrno.ESOCKTNOSUPPORT
\n
Socket type not supported
\n\n
\n
\nerrno.EOPNOTSUPP
\n
Operation not supported on transport endpoint
\n\n
\n
\nerrno.EPFNOSUPPORT
\n
Protocol family not supported
\n\n
\n
\nerrno.EAFNOSUPPORT
\n
Address family not supported by protocol
\n\n
\n
\nerrno.EADDRINUSE
\n
Address already in use
\n\n
\n
\nerrno.EADDRNOTAVAIL
\n
Cannot assign requested address
\n\n
\n
\nerrno.ENETDOWN
\n
Network is down
\n\n
\n
\nerrno.ENETUNREACH
\n
Network is unreachable
\n\n
\n
\nerrno.ENETRESET
\n
Network dropped connection because of reset
\n\n
\n
\nerrno.ECONNABORTED
\n
Software caused connection abort
\n\n
\n
\nerrno.ECONNRESET
\n
Connection reset by peer
\n\n
\n
\nerrno.ENOBUFS
\n
No buffer space available
\n\n
\n
\nerrno.EISCONN
\n
Transport endpoint is already connected
\n\n
\n
\nerrno.ENOTCONN
\n
Transport endpoint is not connected
\n\n
\n
\nerrno.ESHUTDOWN
\n
Cannot send after transport endpoint shutdown
\n\n
\n
\nerrno.ETOOMANYREFS
\n
Too many references: cannot splice
\n\n
\n
\nerrno.ETIMEDOUT
\n
Connection timed out
\n\n
\n
\nerrno.ECONNREFUSED
\n
Connection refused
\n\n
\n
\nerrno.EHOSTDOWN
\n
Host is down
\n\n
\n
\nerrno.EHOSTUNREACH
\n
No route to host
\n\n
\n
\nerrno.EALREADY
\n
Operation already in progress
\n\n
\n
\nerrno.EINPROGRESS
\n
Operation now in progress
\n\n
\n
\nerrno.ESTALE
\n
Stale NFS file handle
\n\n
\n
\nerrno.EUCLEAN
\n
Structure needs cleaning
\n\n
\n
\nerrno.ENOTNAM
\n
Not a XENIX named type file
\n\n
\n
\nerrno.ENAVAIL
\n
No XENIX semaphores available
\n\n
\n
\nerrno.EISNAM
\n
Is a named type file
\n\n
\n
\nerrno.EREMOTEIO
\n
Remote I/O error
\n\n
\n
\nerrno.EDQUOT
\n
Quota exceeded
\n\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/select.html", "title": "select", "html": "
\n

16.1. select — Waiting for I/O completion

\n

This module provides access to the select() and poll() functions\navailable in most operating systems, epoll() available on Linux 2.5+ and\nkqueue() available on most BSD.\nNote that on Windows, it only works for sockets; on other operating systems,\nit also works for other file types (in particular, on Unix, it works on pipes).\nIt cannot be used on regular files to determine whether a file has grown since\nit was last read.

\n

The module defines the following:

\n
\n
\nexception select.error
\n
The exception raised when an error occurs. The accompanying value is a pair\ncontaining the numeric error code from errno and the corresponding\nstring, as would be printed by the C function perror().
\n\n
\n
\nselect.epoll([sizehint=-1])
\n

(Only supported on Linux 2.5.44 and newer.) Returns an edge polling object,\nwhich can be used as Edge or Level Triggered interface for I/O events; see\nsection Edge and Level Trigger Polling (epoll) Objects below for the methods supported by epolling\nobjects.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nselect.poll()
\n
(Not supported by all operating systems.) Returns a polling object, which\nsupports registering and unregistering file descriptors, and then polling them\nfor I/O events; see section Polling Objects below for the methods supported\nby polling objects.
\n\n
\n
\nselect.kqueue()
\n

(Only supported on BSD.) Returns a kernel queue object; see section\nKqueue Objects below for the methods supported by kqueue objects.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nselect.kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)
\n

(Only supported on BSD.) Returns a kernel event object; see section\nKevent Objects below for the methods supported by kevent objects.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nselect.select(rlist, wlist, xlist[, timeout])
\n

This is a straightforward interface to the Unix select() system call.\nThe first three arguments are sequences of ‘waitable objects’: either\nintegers representing file descriptors or objects with a parameterless method\nnamed fileno() returning such an integer:

\n
    \n
  • rlist: wait until ready for reading
  • \n
  • wlist: wait until ready for writing
  • \n
  • xlist: wait for an “exceptional condition” (see the manual page for what\nyour system considers such a condition)
  • \n
\n

Empty sequences are allowed, but acceptance of three empty sequences is\nplatform-dependent. (It is known to work on Unix but not on Windows.) The\noptional timeout argument specifies a time-out as a floating point number\nin seconds. When the timeout argument is omitted the function blocks until\nat least one file descriptor is ready. A time-out value of zero specifies a\npoll and never blocks.

\n

The return value is a triple of lists of objects that are ready: subsets of the\nfirst three arguments. When the time-out is reached without a file descriptor\nbecoming ready, three empty lists are returned.

\n

Among the acceptable object types in the sequences are Python file objects (e.g.\nsys.stdin, or objects returned by open() or os.popen()), socket\nobjects returned by socket.socket(). You may also define a wrapper\nclass yourself, as long as it has an appropriate fileno() method (that\nreally returns a file descriptor, not just a random integer).

\n
\n

Note

\n

File objects on Windows are not acceptable, but sockets are. On Windows,\nthe underlying select() function is provided by the WinSock\nlibrary, and does not handle file descriptors that don’t originate from\nWinSock.

\n
\n
\n\n
\n
\nselect.PIPE_BUF
\n

Files reported as ready for writing by select(), poll() or\nsimilar interfaces in this module are guaranteed to not block on a write\nof up to PIPE_BUF bytes.\nThis value is guaranteed by POSIX to be at least 512. Availability: Unix.

\n

\nNew in version 2.7.

\n
\n\n
\n

16.1.1. Edge and Level Trigger Polling (epoll) Objects

\n
\n

http://linux.die.net/man/4/epoll

\n

eventmask

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
EPOLLINAvailable for read
EPOLLOUTAvailable for write
EPOLLPRIUrgent data for read
EPOLLERRError condition happened on the assoc. fd
EPOLLHUPHang up happened on the assoc. fd
EPOLLETSet Edge Trigger behavior, the default is\nLevel Trigger behavior
EPOLLONESHOTSet one-shot behavior. After one event is\npulled out, the fd is internally disabled
EPOLLRDNORMEquivalent to EPOLLIN
EPOLLRDBANDPriority data band can be read.
EPOLLWRNORMEquivalent to EPOLLOUT
EPOLLWRBANDPriority data may be written.
EPOLLMSGIgnored.
\n
\n
\n
\nepoll.close()
\n
Close the control file descriptor of the epoll object.
\n\n
\n
\nepoll.fileno()
\n
Return the file descriptor number of the control fd.
\n\n
\n
\nepoll.fromfd(fd)
\n
Create an epoll object from a given file descriptor.
\n\n
\n
\nepoll.register(fd[, eventmask])
\n

Register a fd descriptor with the epoll object.

\n
\n

Note

\n

Registering a file descriptor that’s already registered raises an\nIOError – contrary to Polling Objects‘s register.

\n
\n
\n\n
\n
\nepoll.modify(fd, eventmask)
\n
Modify a register file descriptor.
\n\n
\n
\nepoll.unregister(fd)
\n
Remove a registered file descriptor from the epoll object.
\n\n
\n
\nepoll.poll([timeout=-1[, maxevents=-1]])
\n
Wait for events. timeout in seconds (float)
\n\n
\n
\n

16.1.2. Polling Objects

\n

The poll() system call, supported on most Unix systems, provides better\nscalability for network servers that service many, many clients at the same\ntime. poll() scales better because the system call only requires listing\nthe file descriptors of interest, while select() builds a bitmap, turns\non bits for the fds of interest, and then afterward the whole bitmap has to be\nlinearly scanned again. select() is O(highest file descriptor), while\npoll() is O(number of file descriptors).

\n
\n
\npoll.register(fd[, eventmask])
\n

Register a file descriptor with the polling object. Future calls to the\npoll() method will then check whether the file descriptor has any pending\nI/O events. fd can be either an integer, or an object with a fileno()\nmethod that returns an integer. File objects implement fileno(), so they\ncan also be used as the argument.

\n

eventmask is an optional bitmask describing the type of events you want to\ncheck for, and can be a combination of the constants POLLIN,\nPOLLPRI, and POLLOUT, described in the table below. If not\nspecified, the default value used will check for all 3 types of events.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
POLLINThere is data to read
POLLPRIThere is urgent data to read
POLLOUTReady for output: writing will not block
POLLERRError condition of some sort
POLLHUPHung up
POLLNVALInvalid request: descriptor not open
\n

Registering a file descriptor that’s already registered is not an error, and has\nthe same effect as registering the descriptor exactly once.

\n
\n\n
\n
\npoll.modify(fd, eventmask)
\n

Modifies an already registered fd. This has the same effect as\nregister(fd, eventmask). Attempting to modify a file descriptor\nthat was never registered causes an IOError exception with errno\nENOENT to be raised.

\n

\nNew in version 2.6.

\n
\n\n
\n
\npoll.unregister(fd)
\n

Remove a file descriptor being tracked by a polling object. Just like the\nregister() method, fd can be an integer or an object with a\nfileno() method that returns an integer.

\n

Attempting to remove a file descriptor that was never registered causes a\nKeyError exception to be raised.

\n
\n\n
\n
\npoll.poll([timeout])
\n
Polls the set of registered file descriptors, and returns a possibly-empty list\ncontaining (fd, event) 2-tuples for the descriptors that have events or\nerrors to report. fd is the file descriptor, and event is a bitmask with\nbits set for the reported events for that descriptor — POLLIN for\nwaiting input, POLLOUT to indicate that the descriptor can be written\nto, and so forth. An empty list indicates that the call timed out and no file\ndescriptors had any events to report. If timeout is given, it specifies the\nlength of time in milliseconds which the system will wait for events before\nreturning. If timeout is omitted, negative, or None, the call will\nblock until there is an event for this poll object.
\n\n
\n
\n

16.1.3. Kqueue Objects

\n
\n
\nkqueue.close()
\n
Close the control file descriptor of the kqueue object.
\n\n
\n
\nkqueue.fileno()
\n
Return the file descriptor number of the control fd.
\n\n
\n
\nkqueue.fromfd(fd)
\n
Create a kqueue object from a given file descriptor.
\n\n
\n
\nkqueue.control(changelist, max_events[, timeout=None]) → eventlist
\n

Low level interface to kevent

\n
    \n
  • changelist must be an iterable of kevent object or None
  • \n
  • max_events must be 0 or a positive integer
  • \n
  • timeout in seconds (floats possible)
  • \n
\n
\n\n
\n
\n

16.1.4. Kevent Objects

\n

http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2

\n
\n
\nkevent.ident
\n
Value used to identify the event. The interpretation depends on the filter\nbut it’s usually the file descriptor. In the constructor ident can either\nbe an int or an object with a fileno() function. kevent stores the integer\ninternally.
\n\n
\n
\nkevent.filter
\n

Name of the kernel filter.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
KQ_FILTER_READTakes a descriptor and returns whenever\nthere is data available to read
KQ_FILTER_WRITETakes a descriptor and returns whenever\nthere is data available to write
KQ_FILTER_AIOAIO requests
KQ_FILTER_VNODEReturns when one or more of the requested\nevents watched in fflag occurs
KQ_FILTER_PROCWatch for events on a process id
KQ_FILTER_NETDEVWatch for events on a network device\n[not available on Mac OS X]
KQ_FILTER_SIGNALReturns whenever the watched signal is\ndelivered to the process
KQ_FILTER_TIMEREstablishes an arbitrary timer
\n
\n\n
\n
\nkevent.flags
\n

Filter action.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
KQ_EV_ADDAdds or modifies an event
KQ_EV_DELETERemoves an event from the queue
KQ_EV_ENABLEPermitscontrol() to returns the event
KQ_EV_DISABLEDisablesevent
KQ_EV_ONESHOTRemoves event after first occurrence
KQ_EV_CLEARReset the state after an event is retrieved
KQ_EV_SYSFLAGSinternal event
KQ_EV_FLAG1internal event
KQ_EV_EOFFilter specific EOF condition
KQ_EV_ERRORSee return values
\n
\n\n
\n
\nkevent.fflags
\n

Filter specific flags.

\n

KQ_FILTER_READ and KQ_FILTER_WRITE filter flags:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
KQ_NOTE_LOWATlow water mark of a socket buffer
\n

KQ_FILTER_VNODE filter flags:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
KQ_NOTE_DELETEunlink() was called
KQ_NOTE_WRITEa write occurred
KQ_NOTE_EXTENDthe file was extended
KQ_NOTE_ATTRIBan attribute was changed
KQ_NOTE_LINKthe link count has changed
KQ_NOTE_RENAMEthe file was renamed
KQ_NOTE_REVOKEaccess to the file was revoked
\n

KQ_FILTER_PROC filter flags:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
KQ_NOTE_EXITthe process has exited
KQ_NOTE_FORKthe process has called fork()
KQ_NOTE_EXECthe process has executed a new process
KQ_NOTE_PCTRLMASKinternal filter flag
KQ_NOTE_PDATAMASKinternal filter flag
KQ_NOTE_TRACKfollow a process across fork()
KQ_NOTE_CHILDreturned on the child process for\nNOTE_TRACK
KQ_NOTE_TRACKERRunable to attach to a child
\n

KQ_FILTER_NETDEV filter flags (not available on Mac OS X):

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
KQ_NOTE_LINKUPlink is up
KQ_NOTE_LINKDOWNlink is down
KQ_NOTE_LINKINVlink state is invalid
\n
\n\n
\n
\nkevent.data
\n
Filter specific data.
\n\n
\n
\nkevent.udata
\n
User defined value.
\n\n
\n
", "searchableItems": [ { "name": "select.epoll", "domId": "select_select.epoll" }, { "name": "select.epoll.close", "domId": "select_select.epoll.close" }, { "name": "select.epoll.fileno", "domId": "select_select.epoll.fileno" }, { "name": "select.epoll.fromfd", "domId": "select_select.epoll.fromfd" }, { "name": "select.epoll.modify", "domId": "select_select.epoll.modify" }, { "name": "select.epoll.poll", "domId": "select_select.epoll.poll" }, { "name": "select.epoll.register", "domId": "select_select.epoll.register" }, { "name": "select.epoll.unregister", "domId": "select_select.epoll.unregister" }, { "name": "select.kevent", "domId": "select_select.kevent" }, { "name": "select.kqueue", "domId": "select_select.kqueue" }, { "name": "select.kqueue.close", "domId": "select_select.kqueue.close" }, { "name": "select.kqueue.control", "domId": "select_select.kqueue.control" }, { "name": "select.kqueue.fileno", "domId": "select_select.kqueue.fileno" }, { "name": "select.kqueue.fromfd", "domId": "select_select.kqueue.fromfd" }, { "name": "select.poll", "domId": "select_select.poll" }, { "name": "select.poll.modify", "domId": "select_select.poll.modify" }, { "name": "select.poll.poll", "domId": "select_select.poll.poll" }, { "name": "select.poll.register", "domId": "select_select.poll.register" }, { "name": "select.poll.unregister", "domId": "select_select.poll.unregister" }, { "name": "select.select", "domId": "select_select.select" } ] }, { "url": "http://docs.python.org/library/dummy_threading.html", "title": "dummy_threading", "html": "
\n

16.4. dummy_threading — Drop-in replacement for the threading module

\n

Source code: Lib/dummy_threading.py

\n
\n

This module provides a duplicate interface to the threading module. It\nis meant to be imported when the thread module is not provided on a\nplatform.

\n

Suggested usage is:

\n
try:\n    import threading as _threading\nexcept ImportError:\n    import dummy_threading as _threading\n
\n
\n

Be careful to not use this module where deadlock might occur from a thread\nbeing created that blocks waiting for another thread to be created. This often\noccurs with blocking I/O.

\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/thread.html", "title": "thread", "html": "
\n

16.3. thread — Multiple threads of control

\n
\n

Note

\n

The thread module has been renamed to _thread in Python 3.0.\nThe 2to3 tool will automatically adapt imports when converting your\nsources to 3.0; however, you should consider using the high-level\nthreading module instead.

\n
\n

This module provides low-level primitives for working with multiple threads\n(also called light-weight processes or tasks) — multiple threads of\ncontrol sharing their global data space. For synchronization, simple locks\n(also called mutexes or binary semaphores) are provided.\nThe threading module provides an easier to use and higher-level\nthreading API built on top of this module.

\n

The module is optional. It is supported on Windows, Linux, SGI IRIX, Solaris\n2.x, as well as on systems that have a POSIX thread (a.k.a. “pthread”)\nimplementation. For systems lacking the thread module, the\ndummy_thread module is available. It duplicates this module’s interface\nand can be used as a drop-in replacement.

\n

It defines the following constant and functions:

\n
\n
\nexception thread.error
\n
Raised on thread-specific errors.
\n\n
\n
\nthread.LockType
\n
This is the type of lock objects.
\n\n
\n
\nthread.start_new_thread(function, args[, kwargs])
\n
Start a new thread and return its identifier. The thread executes the function\nfunction with the argument list args (which must be a tuple). The optional\nkwargs argument specifies a dictionary of keyword arguments. When the function\nreturns, the thread silently exits. When the function terminates with an\nunhandled exception, a stack trace is printed and then the thread exits (but\nother threads continue to run).
\n\n
\n
\nthread.interrupt_main()
\n

Raise a KeyboardInterrupt exception in the main thread. A subthread can\nuse this function to interrupt the main thread.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nthread.exit()
\n
Raise the SystemExit exception. When not caught, this will cause the\nthread to exit silently.
\n\n
\n
\nthread.allocate_lock()
\n
Return a new lock object. Methods of locks are described below. The lock is\ninitially unlocked.
\n\n
\n
\nthread.get_ident()
\n
Return the ‘thread identifier’ of the current thread. This is a nonzero\ninteger. Its value has no direct meaning; it is intended as a magic cookie to\nbe used e.g. to index a dictionary of thread-specific data. Thread identifiers\nmay be recycled when a thread exits and another thread is created.
\n\n
\n
\nthread.stack_size([size])
\n

Return the thread stack size used when creating new threads. The optional\nsize argument specifies the stack size to be used for subsequently created\nthreads, and must be 0 (use platform or configured default) or a positive\ninteger value of at least 32,768 (32kB). If changing the thread stack size is\nunsupported, the error exception is raised. If the specified stack size is\ninvalid, a ValueError is raised and the stack size is unmodified. 32kB\nis currently the minimum supported stack size value to guarantee sufficient\nstack space for the interpreter itself. Note that some platforms may have\nparticular restrictions on values for the stack size, such as requiring a\nminimum stack size > 32kB or requiring allocation in multiples of the system\nmemory page size - platform documentation should be referred to for more\ninformation (4kB pages are common; using multiples of 4096 for the stack size is\nthe suggested approach in the absence of more specific information).\nAvailability: Windows, systems with POSIX threads.

\n

\nNew in version 2.5.

\n
\n\n

Lock objects have the following methods:

\n
\n
\nlock.acquire([waitflag])
\n
Without the optional argument, this method acquires the lock unconditionally, if\nnecessary waiting until it is released by another thread (only one thread at a\ntime can acquire a lock — that’s their reason for existence). If the integer\nwaitflag argument is present, the action depends on its value: if it is zero,\nthe lock is only acquired if it can be acquired immediately without waiting,\nwhile if it is nonzero, the lock is acquired unconditionally as before. The\nreturn value is True if the lock is acquired successfully, False if not.
\n\n
\n
\nlock.release()
\n
Releases the lock. The lock must have been acquired earlier, but not\nnecessarily by the same thread.
\n\n
\n
\nlock.locked()
\n
Return the status of the lock: True if it has been acquired by some thread,\nFalse if not.
\n\n

In addition to these methods, lock objects can also be used via the\nwith statement, e.g.:

\n
import thread\n\na_lock = thread.allocate_lock()\n\nwith a_lock:\n    print "a_lock is locked while this executes"\n
\n
\n

Caveats:

\n
\n
\n\n
", "searchableItems": [ { "name": "thread.allocate_lock", "domId": "thread_thread.allocate_lock" }, { "name": "thread.exit", "domId": "thread_thread.exit" }, { "name": "thread.get_ident", "domId": "thread_thread.get_ident" }, { "name": "thread.interrupt_main", "domId": "thread_thread.interrupt_main" }, { "name": "thread.lock.acquire", "domId": "thread_thread.lock.acquire" }, { "name": "thread.lock.locked", "domId": "thread_thread.lock.locked" }, { "name": "thread.lock.release", "domId": "thread_thread.lock.release" }, { "name": "thread.stack_size", "domId": "thread_thread.stack_size" }, { "name": "thread.start_new_thread", "domId": "thread_thread.start_new_thread" } ] }, { "url": "http://docs.python.org/library/logging.html", "title": "logging", "html": "
\n

15.7. logging — Logging facility for Python

\n
\n

Important

\n

This page contains the API reference information. For tutorial\ninformation and discussion of more advanced topics, see

\n\n
\n

\nNew in version 2.3.

\n

This module defines functions and classes which implement a flexible event\nlogging system for applications and libraries.

\n

The key benefit of having the logging API provided by a standard library module\nis that all Python modules can participate in logging, so your application log\ncan include your own messages integrated with messages from third-party\nmodules.

\n

The module provides a lot of functionality and flexibility. If you are\nunfamiliar with logging, the best way to get to grips with it is to see the\ntutorials (see the links on the right).

\n

The basic classes defined by the module, together with their functions, are\nlisted below.

\n\n
\n

15.7.1. Logger Objects

\n

Loggers have the following attributes and methods. Note that Loggers are never\ninstantiated directly, but always through the module-level function\nlogging.getLogger(name).

\n
\n
\nclass logging.Logger
\n
\n\n
\n
\nLogger.propagate
\n

If this evaluates to true, logging messages are passed by this logger and by\nits child loggers to the handlers of higher level (ancestor) loggers.\nMessages are passed directly to the ancestor loggers’ handlers - neither the\nlevel nor filters of the ancestor loggers in question are considered.

\n

If this evaluates to false, logging messages are not passed to the handlers\nof ancestor loggers.

\n

The constructor sets this attribute to True.

\n
\n\n
\n
\nLogger.setLevel(lvl)
\n

Sets the threshold for this logger to lvl. Logging messages which are less\nsevere than lvl will be ignored. When a logger is created, the level is set to\nNOTSET (which causes all messages to be processed when the logger is\nthe root logger, or delegation to the parent when the logger is a non-root\nlogger). Note that the root logger is created with level WARNING.

\n

The term ‘delegation to the parent’ means that if a logger has a level of\nNOTSET, its chain of ancestor loggers is traversed until either an ancestor with\na level other than NOTSET is found, or the root is reached.

\n

If an ancestor is found with a level other than NOTSET, then that ancestor’s\nlevel is treated as the effective level of the logger where the ancestor search\nbegan, and is used to determine how a logging event is handled.

\n

If the root is reached, and it has a level of NOTSET, then all messages will be\nprocessed. Otherwise, the root’s level will be used as the effective level.

\n
\n\n
\n
\nLogger.isEnabledFor(lvl)
\n
Indicates if a message of severity lvl would be processed by this logger.\nThis method checks first the module-level level set by\nlogging.disable(lvl) and then the logger’s effective level as determined\nby getEffectiveLevel().
\n\n
\n
\nLogger.getEffectiveLevel()
\n
Indicates the effective level for this logger. If a value other than\nNOTSET has been set using setLevel(), it is returned. Otherwise,\nthe hierarchy is traversed towards the root until a value other than\nNOTSET is found, and that value is returned.
\n\n
\n
\nLogger.getChild(suffix)
\n

Returns a logger which is a descendant to this logger, as determined by the suffix.\nThus, logging.getLogger('abc').getChild('def.ghi') would return the same\nlogger as would be returned by logging.getLogger('abc.def.ghi'). This is a\nconvenience method, useful when the parent logger is named using e.g. __name__\nrather than a literal string.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nLogger.debug(msg, *args, **kwargs)
\n

Logs a message with level DEBUG on this logger. The msg is the\nmessage format string, and the args are the arguments which are merged into\nmsg using the string formatting operator. (Note that this means that you can\nuse keywords in the format string, together with a single dictionary argument.)

\n

There are two keyword arguments in kwargs which are inspected: exc_info\nwhich, if it does not evaluate as false, causes exception information to be\nadded to the logging message. If an exception tuple (in the format returned by\nsys.exc_info()) is provided, it is used; otherwise, sys.exc_info()\nis called to get the exception information.

\n

The second keyword argument is extra which can be used to pass a\ndictionary which is used to populate the __dict__ of the LogRecord created for\nthe logging event with user-defined attributes. These custom attributes can then\nbe used as you like. For example, they could be incorporated into logged\nmessages. For example:

\n
FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'\nlogging.basicConfig(format=FORMAT)\nd = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }\nlogger = logging.getLogger('tcpserver')\nlogger.warning('Protocol problem: %s', 'connection reset', extra=d)\n
\n
\n

would print something like

\n
2006-02-08 22:20:02,165 192.168.0.1 fbloggs  Protocol problem: connection reset
\n
\n

The keys in the dictionary passed in extra should not clash with the keys used\nby the logging system. (See the Formatter documentation for more\ninformation on which keys are used by the logging system.)

\n

If you choose to use these attributes in logged messages, you need to exercise\nsome care. In the above example, for instance, the Formatter has been\nset up with a format string which expects ‘clientip’ and ‘user’ in the attribute\ndictionary of the LogRecord. If these are missing, the message will not be\nlogged because a string formatting exception will occur. So in this case, you\nalways need to pass the extra dictionary with these keys.

\n

While this might be annoying, this feature is intended for use in specialized\ncircumstances, such as multi-threaded servers where the same code executes in\nmany contexts, and interesting conditions which arise are dependent on this\ncontext (such as remote client IP address and authenticated user name, in the\nabove example). In such circumstances, it is likely that specialized\nFormatters would be used with particular Handlers.

\n
\n\n
\n
\nLogger.info(msg, *args, **kwargs)
\n
Logs a message with level INFO on this logger. The arguments are\ninterpreted as for debug().
\n\n
\n
\nLogger.warning(msg, *args, **kwargs)
\n
Logs a message with level WARNING on this logger. The arguments are\ninterpreted as for debug().
\n\n
\n
\nLogger.error(msg, *args, **kwargs)
\n
Logs a message with level ERROR on this logger. The arguments are\ninterpreted as for debug().
\n\n
\n
\nLogger.critical(msg, *args, **kwargs)
\n
Logs a message with level CRITICAL on this logger. The arguments are\ninterpreted as for debug().
\n\n
\n
\nLogger.log(lvl, msg, *args, **kwargs)
\n
Logs a message with integer level lvl on this logger. The other arguments are\ninterpreted as for debug().
\n\n
\n
\nLogger.exception(msg, *args)
\n
Logs a message with level ERROR on this logger. The arguments are\ninterpreted as for debug(). Exception info is added to the logging\nmessage. This method should only be called from an exception handler.
\n\n
\n
\nLogger.addFilter(filt)
\n
Adds the specified filter filt to this logger.
\n\n
\n
\nLogger.removeFilter(filt)
\n
Removes the specified filter filt from this logger.
\n\n
\n
\nLogger.filter(record)
\n
Applies this logger’s filters to the record and returns a true value if the\nrecord is to be processed.
\n\n
\n
\nLogger.addHandler(hdlr)
\n
Adds the specified handler hdlr to this logger.
\n\n
\n
\nLogger.removeHandler(hdlr)
\n
Removes the specified handler hdlr from this logger.
\n\n
\n
\nLogger.findCaller()
\n

Finds the caller’s source filename and line number. Returns the filename, line\nnumber and function name as a 3-element tuple.

\n

\nChanged in version 2.4: The function name was added. In earlier versions, the filename and line\nnumber were returned as a 2-element tuple.

\n
\n\n
\n
\nLogger.handle(record)
\n
Handles a record by passing it to all handlers associated with this logger and\nits ancestors (until a false value of propagate is found). This method is used\nfor unpickled records received from a socket, as well as those created locally.\nLogger-level filtering is applied using filter().
\n\n
\n
\nLogger.makeRecord(name, lvl, fn, lno, msg, args, exc_info, func=None, extra=None)
\n

This is a factory method which can be overridden in subclasses to create\nspecialized LogRecord instances.

\n

\nChanged in version 2.5: func and extra were added.

\n
\n\n
\n
\n

15.7.2. Handler Objects

\n

Handlers have the following attributes and methods. Note that Handler\nis never instantiated directly; this class acts as a base for more useful\nsubclasses. However, the __init__() method in subclasses needs to call\nHandler.__init__().

\n
\n
\nHandler.__init__(level=NOTSET)
\n
Initializes the Handler instance by setting its level, setting the list\nof filters to the empty list and creating a lock (using createLock()) for\nserializing access to an I/O mechanism.
\n\n
\n
\nHandler.createLock()
\n
Initializes a thread lock which can be used to serialize access to underlying\nI/O functionality which may not be threadsafe.
\n\n
\n
\nHandler.acquire()
\n
Acquires the thread lock created with createLock().
\n\n
\n
\nHandler.release()
\n
Releases the thread lock acquired with acquire().
\n\n
\n
\nHandler.setLevel(lvl)
\n
Sets the threshold for this handler to lvl. Logging messages which are less\nsevere than lvl will be ignored. When a handler is created, the level is set\nto NOTSET (which causes all messages to be processed).
\n\n
\n
\nHandler.setFormatter(form)
\n
Sets the Formatter for this handler to form.
\n\n
\n
\nHandler.addFilter(filt)
\n
Adds the specified filter filt to this handler.
\n\n
\n
\nHandler.removeFilter(filt)
\n
Removes the specified filter filt from this handler.
\n\n
\n
\nHandler.filter(record)
\n
Applies this handler’s filters to the record and returns a true value if the\nrecord is to be processed.
\n\n
\n
\nHandler.flush()
\n
Ensure all logging output has been flushed. This version does nothing and is\nintended to be implemented by subclasses.
\n\n
\n
\nHandler.close()
\n
Tidy up any resources used by the handler. This version does no output but\nremoves the handler from an internal list of handlers which is closed when\nshutdown() is called. Subclasses should ensure that this gets called\nfrom overridden close() methods.
\n\n
\n
\nHandler.handle(record)
\n
Conditionally emits the specified logging record, depending on filters which may\nhave been added to the handler. Wraps the actual emission of the record with\nacquisition/release of the I/O thread lock.
\n\n
\n
\nHandler.handleError(record)
\n
This method should be called from handlers when an exception is encountered\nduring an emit() call. By default it does nothing, which means that\nexceptions get silently ignored. This is what is mostly wanted for a logging\nsystem - most users will not care about errors in the logging system, they are\nmore interested in application errors. You could, however, replace this with a\ncustom handler if you wish. The specified record is the one which was being\nprocessed when the exception occurred.
\n\n
\n
\nHandler.format(record)
\n
Do formatting for a record - if a formatter is set, use it. Otherwise, use the\ndefault formatter for the module.
\n\n
\n
\nHandler.emit(record)
\n
Do whatever it takes to actually log the specified logging record. This version\nis intended to be implemented by subclasses and so raises a\nNotImplementedError.
\n\n

For a list of handlers included as standard, see logging.handlers.

\n
\n
\n

15.7.3. Formatter Objects

\n

Formatter objects have the following attributes and methods. They are\nresponsible for converting a LogRecord to (usually) a string which can\nbe interpreted by either a human or an external system. The base\nFormatter allows a formatting string to be specified. If none is\nsupplied, the default value of '%(message)s' is used.

\n

A Formatter can be initialized with a format string which makes use of knowledge\nof the LogRecord attributes - such as the default value mentioned above\nmaking use of the fact that the user’s message and arguments are pre-formatted\ninto a LogRecord‘s message attribute. This format string contains\nstandard Python %-style mapping keys. See section String Formatting Operations\nfor more information on string formatting.

\n

The useful mapping keys in a LogRecord are given in the section on\nLogRecord attributes.

\n
\n
\nclass logging.Formatter(fmt=None, datefmt=None)
\n

Returns a new instance of the Formatter class. The instance is\ninitialized with a format string for the message as a whole, as well as a\nformat string for the date/time portion of a message. If no fmt is\nspecified, '%(message)s' is used. If no datefmt is specified, the\nISO8601 date format is used.

\n
\n
\nformat(record)
\n
The record’s attribute dictionary is used as the operand to a string\nformatting operation. Returns the resulting string. Before formatting the\ndictionary, a couple of preparatory steps are carried out. The message\nattribute of the record is computed using msg % args. If the\nformatting string contains '(asctime)', formatTime() is called\nto format the event time. If there is exception information, it is\nformatted using formatException() and appended to the message. Note\nthat the formatted exception information is cached in attribute\nexc_text. This is useful because the exception information can be\npickled and sent across the wire, but you should be careful if you have\nmore than one Formatter subclass which customizes the formatting\nof exception information. In this case, you will have to clear the cached\nvalue after a formatter has done its formatting, so that the next\nformatter to handle the event doesn’t use the cached value but\nrecalculates it afresh.
\n\n
\n
\nformatTime(record, datefmt=None)
\n

This method should be called from format() by a formatter which\nwants to make use of a formatted time. This method can be overridden in\nformatters to provide for any specific requirement, but the basic behavior\nis as follows: if datefmt (a string) is specified, it is used with\ntime.strftime() to format the creation time of the\nrecord. Otherwise, the ISO8601 format is used. The resulting string is\nreturned.

\n

This function uses a user-configurable function to convert the creation\ntime to a tuple. By default, time.localtime() is used; to change\nthis for a particular formatter instance, set the converter attribute\nto a function with the same signature as time.localtime() or\ntime.gmtime(). To change it for all formatters, for example if you\nwant all logging times to be shown in GMT, set the converter\nattribute in the Formatter class.

\n
\n\n
\n
\nformatException(exc_info)
\n
Formats the specified exception information (a standard exception tuple as\nreturned by sys.exc_info()) as a string. This default implementation\njust uses traceback.print_exception(). The resulting string is\nreturned.
\n\n
\n\n
\n
\n

15.7.4. Filter Objects

\n

Filters can be used by Handlers and Loggers for more sophisticated\nfiltering than is provided by levels. The base filter class only allows events\nwhich are below a certain point in the logger hierarchy. For example, a filter\ninitialized with ‘A.B’ will allow events logged by loggers ‘A.B’, ‘A.B.C’,\n‘A.B.C.D’, ‘A.B.D’ etc. but not ‘A.BB’, ‘B.A.B’ etc. If initialized with the\nempty string, all events are passed.

\n
\n
\nclass logging.Filter(name='')
\n

Returns an instance of the Filter class. If name is specified, it\nnames a logger which, together with its children, will have its events allowed\nthrough the filter. If name is the empty string, allows every event.

\n
\n
\nfilter(record)
\n
Is the specified record to be logged? Returns zero for no, nonzero for\nyes. If deemed appropriate, the record may be modified in-place by this\nmethod.
\n\n
\n\n

Note that filters attached to handlers are consulted whenever an event is\nemitted by the handler, whereas filters attached to loggers are consulted\nwhenever an event is logged to the handler (using debug(), info(),\netc.) This means that events which have been generated by descendant loggers\nwill not be filtered by a logger’s filter setting, unless the filter has also\nbeen applied to those descendant loggers.

\n

You don’t actually need to subclass Filter: you can pass any instance\nwhich has a filter method with the same semantics.

\n

Although filters are used primarily to filter records based on more\nsophisticated criteria than levels, they get to see every record which is\nprocessed by the handler or logger they’re attached to: this can be useful if\nyou want to do things like counting how many records were processed by a\nparticular logger or handler, or adding, changing or removing attributes in\nthe LogRecord being processed. Obviously changing the LogRecord needs to be\ndone with some care, but it does allow the injection of contextual information\ninto logs (see Using Filters to impart contextual information).

\n
\n
\n

15.7.5. LogRecord Objects

\n

LogRecord instances are created automatically by the Logger\nevery time something is logged, and can be created manually via\nmakeLogRecord() (for example, from a pickled event received over the\nwire).

\n
\n
\nclass logging.LogRecord(name, level, pathname, lineno, msg, args, exc_info, func=None)
\n

Contains all the information pertinent to the event being logged.

\n

The primary information is passed in msg and args, which\nare combined using msg args to create the message field of the\nrecord.

\n\n\n\n\n\n\n\n
Parameters:
    \n
  • name – The name of the logger used to log the event represented by\nthis LogRecord.
  • \n
  • level – The numeric level of the logging event (one of DEBUG, INFO etc.)\nNote that this is converted to two attributes of the LogRecord:\nlevelno for the numeric value and levelname for the\ncorresponding level name.
  • \n
  • pathname – The full pathname of the source file where the logging call\nwas made.
  • \n
  • lineno – The line number in the source file where the logging call was\nmade.
  • \n
  • msg – The event description message, possibly a format string with\nplaceholders for variable data.
  • \n
  • args – Variable data to merge into the msg argument to obtain the\nevent description.
  • \n
  • exc_info – An exception tuple with the current exception information,\nor None if no exception information is available.
  • \n
  • func – The name of the function or method from which the logging call\nwas invoked.
  • \n
\n
\n

\nChanged in version 2.5: func was added.

\n
\n
\ngetMessage()
\n
Returns the message for this LogRecord instance after merging any\nuser-supplied arguments with the message. If the user-supplied message\nargument to the logging call is not a string, str() is called on it to\nconvert it to a string. This allows use of user-defined classes as\nmessages, whose __str__ method can return the actual format string to\nbe used.
\n\n
\n\n
\n
\n

15.7.6. LogRecord attributes

\n

The LogRecord has a number of attributes, most of which are derived from the\nparameters to the constructor. (Note that the names do not always correspond\nexactly between the LogRecord constructor parameters and the LogRecord\nattributes.) These attributes can be used to merge data from the record into\nthe format string. The following table lists (in alphabetical order) the\nattribute names, their meanings and the corresponding placeholder in a %-style\nformat string.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Attribute nameFormatDescription
argsYou shouldn’t need to\nformat this yourself.The tuple of arguments merged into msg to\nproduce message.
asctime%(asctime)sHuman-readable time when the\nLogRecord was created. By default\nthis is of the form ‘2003-07-08 16:49:45,896’\n(the numbers after the comma are millisecond\nportion of the time).
created%(created)fTime when the LogRecord was created\n(as returned by time.time()).
exc_infoYou shouldn’t need to\nformat this yourself.Exception tuple (à la sys.exc_info) or,\nif no exception has occurred, None.
filename%(filename)sFilename portion of pathname.
funcName%(funcName)sName of function containing the logging call.
levelname%(levelname)sText logging level for the message\n('DEBUG', 'INFO', 'WARNING',\n'ERROR', 'CRITICAL').
levelno%(levelno)sNumeric logging level for the message\n(DEBUG, INFO,\nWARNING, ERROR,\nCRITICAL).
lineno%(lineno)dSource line number where the logging call was\nissued (if available).
module%(module)sModule (name portion of filename).
msecs%(msecs)dMillisecond portion of the time when the\nLogRecord was created.
message%(message)sThe logged message, computed as msg \nargs. This is set when\nFormatter.format() is invoked.
msgYou shouldn’t need to\nformat this yourself.The format string passed in the original\nlogging call. Merged with args to\nproduce message, or an arbitrary object\n(see Using arbitrary objects as messages).
name%(name)sName of the logger used to log the call.
pathname%(pathname)sFull pathname of the source file where the\nlogging call was issued (if available).
process%(process)dProcess ID (if available).
processName%(processName)sProcess name (if available).
relativeCreated%(relativeCreated)dTime in milliseconds when the LogRecord was\ncreated, relative to the time the logging\nmodule was loaded.
thread%(thread)dThread ID (if available).
threadName%(threadName)sThread name (if available).
\n

\nChanged in version 2.5: funcName was added.

\n
\n
\n

15.7.7. LoggerAdapter Objects

\n

LoggerAdapter instances are used to conveniently pass contextual\ninformation into logging calls. For a usage example , see the section on\nadding contextual information to your logging output.

\n

\nNew in version 2.6.

\n
\n
\nclass logging.LoggerAdapter(logger, extra)
\n

Returns an instance of LoggerAdapter initialized with an\nunderlying Logger instance and a dict-like object.

\n
\n
\nprocess(msg, kwargs)
\n
Modifies the message and/or keyword arguments passed to a logging call in\norder to insert contextual information. This implementation takes the object\npassed as extra to the constructor and adds it to kwargs using key\n‘extra’. The return value is a (msg, kwargs) tuple which has the\n(possibly modified) versions of the arguments passed in.
\n\n
\n\n

In addition to the above, LoggerAdapter supports the following\nmethods of Logger, i.e. debug(), info(), warning(),\nerror(), exception(), critical(), log(),\nisEnabledFor(), getEffectiveLevel(), setLevel(),\nhasHandlers(). These methods have the same signatures as their\ncounterparts in Logger, so you can use the two types of instances\ninterchangeably.

\n

\nChanged in version 2.7: The isEnabledFor() method was added to LoggerAdapter. This\nmethod delegates to the underlying logger.

\n
\n
\n

15.7.8. Thread Safety

\n

The logging module is intended to be thread-safe without any special work\nneeding to be done by its clients. It achieves this though using threading\nlocks; there is one lock to serialize access to the module’s shared data, and\neach handler also creates a lock to serialize access to its underlying I/O.

\n

If you are implementing asynchronous signal handlers using the signal\nmodule, you may not be able to use logging from within such handlers. This is\nbecause lock implementations in the threading module are not always\nre-entrant, and so cannot be invoked from such signal handlers.

\n
\n
\n

15.7.9. Module-Level Functions

\n

In addition to the classes described above, there are a number of module- level\nfunctions.

\n
\n
\nlogging.getLogger([name])
\n

Return a logger with the specified name or, if no name is specified, return a\nlogger which is the root logger of the hierarchy. If specified, the name is\ntypically a dot-separated hierarchical name like “a”, “a.b” or “a.b.c.d”.\nChoice of these names is entirely up to the developer who is using logging.

\n

All calls to this function with a given name return the same logger instance.\nThis means that logger instances never need to be passed between different parts\nof an application.

\n
\n\n
\n
\nlogging.getLoggerClass()
\n

Return either the standard Logger class, or the last class passed to\nsetLoggerClass(). This function may be called from within a new class\ndefinition, to ensure that installing a customised Logger class will\nnot undo customisations already applied by other code. For example:

\n
class MyLogger(logging.getLoggerClass()):\n    # ... override behaviour here
\n
\n
\n\n
\n
\nlogging.debug(msg[, *args[, **kwargs]])
\n

Logs a message with level DEBUG on the root logger. The msg is the\nmessage format string, and the args are the arguments which are merged into\nmsg using the string formatting operator. (Note that this means that you can\nuse keywords in the format string, together with a single dictionary argument.)

\n

There are two keyword arguments in kwargs which are inspected: exc_info\nwhich, if it does not evaluate as false, causes exception information to be\nadded to the logging message. If an exception tuple (in the format returned by\nsys.exc_info()) is provided, it is used; otherwise, sys.exc_info()\nis called to get the exception information.

\n

The other optional keyword argument is extra which can be used to pass a\ndictionary which is used to populate the __dict__ of the LogRecord created for\nthe logging event with user-defined attributes. These custom attributes can then\nbe used as you like. For example, they could be incorporated into logged\nmessages. For example:

\n
FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s"\nlogging.basicConfig(format=FORMAT)\nd = {'clientip': '192.168.0.1', 'user': 'fbloggs'}\nlogging.warning("Protocol problem: %s", "connection reset", extra=d)\n
\n
\n

would print something like:

\n
2006-02-08 22:20:02,165 192.168.0.1 fbloggs  Protocol problem: connection reset
\n
\n

The keys in the dictionary passed in extra should not clash with the keys used\nby the logging system. (See the Formatter documentation for more\ninformation on which keys are used by the logging system.)

\n

If you choose to use these attributes in logged messages, you need to exercise\nsome care. In the above example, for instance, the Formatter has been\nset up with a format string which expects ‘clientip’ and ‘user’ in the attribute\ndictionary of the LogRecord. If these are missing, the message will not be\nlogged because a string formatting exception will occur. So in this case, you\nalways need to pass the extra dictionary with these keys.

\n

While this might be annoying, this feature is intended for use in specialized\ncircumstances, such as multi-threaded servers where the same code executes in\nmany contexts, and interesting conditions which arise are dependent on this\ncontext (such as remote client IP address and authenticated user name, in the\nabove example). In such circumstances, it is likely that specialized\nFormatters would be used with particular Handlers.

\n

\nChanged in version 2.5: extra was added.

\n
\n\n
\n
\nlogging.info(msg[, *args[, **kwargs]])
\n
Logs a message with level INFO on the root logger. The arguments are\ninterpreted as for debug().
\n\n
\n
\nlogging.warning(msg[, *args[, **kwargs]])
\n
Logs a message with level WARNING on the root logger. The arguments are\ninterpreted as for debug().
\n\n
\n
\nlogging.error(msg[, *args[, **kwargs]])
\n
Logs a message with level ERROR on the root logger. The arguments are\ninterpreted as for debug().
\n\n
\n
\nlogging.critical(msg[, *args[, **kwargs]])
\n
Logs a message with level CRITICAL on the root logger. The arguments\nare interpreted as for debug().
\n\n
\n
\nlogging.exception(msg[, *args])
\n
Logs a message with level ERROR on the root logger. The arguments are\ninterpreted as for debug(). Exception info is added to the logging\nmessage. This function should only be called from an exception handler.
\n\n
\n
\nlogging.log(level, msg[, *args[, **kwargs]])
\n

Logs a message with level level on the root logger. The other arguments are\ninterpreted as for debug().

\n

PLEASE NOTE: The above module-level functions which delegate to the root\nlogger should not be used in threads, in versions of Python earlier than\n2.7.1 and 3.2, unless at least one handler has been added to the root\nlogger before the threads are started. These convenience functions call\nbasicConfig() to ensure that at least one handler is available; in\nearlier versions of Python, this can (under rare circumstances) lead to\nhandlers being added multiple times to the root logger, which can in turn\nlead to multiple messages for the same event.

\n
\n\n
\n
\nlogging.disable(lvl)
\n
Provides an overriding level lvl for all loggers which takes precedence over\nthe logger’s own level. When the need arises to temporarily throttle logging\noutput down across the whole application, this function can be useful. Its\neffect is to disable all logging calls of severity lvl and below, so that\nif you call it with a value of INFO, then all INFO and DEBUG events would be\ndiscarded, whereas those of severity WARNING and above would be processed\naccording to the logger’s effective level.
\n\n
\n
\nlogging.addLevelName(lvl, levelName)
\n

Associates level lvl with text levelName in an internal dictionary, which is\nused to map numeric levels to a textual representation, for example when a\nFormatter formats a message. This function can also be used to define\nyour own levels. The only constraints are that all levels used must be\nregistered using this function, levels should be positive integers and they\nshould increase in increasing order of severity.

\n

NOTE: If you are thinking of defining your own levels, please see the section\non Custom Levels.

\n
\n\n
\n
\nlogging.getLevelName(lvl)
\n
Returns the textual representation of logging level lvl. If the level is one\nof the predefined levels CRITICAL, ERROR, WARNING,\nINFO or DEBUG then you get the corresponding string. If you\nhave associated levels with names using addLevelName() then the name you\nhave associated with lvl is returned. If a numeric value corresponding to one\nof the defined levels is passed in, the corresponding string representation is\nreturned. Otherwise, the string “Level %s” % lvl is returned.
\n\n
\n
\nlogging.makeLogRecord(attrdict)
\n
Creates and returns a new LogRecord instance whose attributes are\ndefined by attrdict. This function is useful for taking a pickled\nLogRecord attribute dictionary, sent over a socket, and reconstituting\nit as a LogRecord instance at the receiving end.
\n\n
\n
\nlogging.basicConfig([**kwargs])
\n

Does basic configuration for the logging system by creating a\nStreamHandler with a default Formatter and adding it to the\nroot logger. The functions debug(), info(), warning(),\nerror() and critical() will call basicConfig() automatically\nif no handlers are defined for the root logger.

\n

This function does nothing if the root logger already has handlers\nconfigured for it.

\n

\nChanged in version 2.4: Formerly, basicConfig() did not take any keyword arguments.

\n

PLEASE NOTE: This function should be called from the main thread\nbefore other threads are started. In versions of Python prior to\n2.7.1 and 3.2, if this function is called from multiple threads,\nit is possible (in rare circumstances) that a handler will be added\nto the root logger more than once, leading to unexpected results\nsuch as messages being duplicated in the log.

\n

The following keyword arguments are supported.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FormatDescription
filenameSpecifies that a FileHandler be created,\nusing the specified filename, rather than a\nStreamHandler.
filemodeSpecifies the mode to open the file, if\nfilename is specified (if filemode is\nunspecified, it defaults to ‘a’).
formatUse the specified format string for the\nhandler.
datefmtUse the specified date/time format.
levelSet the root logger level to the specified\nlevel.
streamUse the specified stream to initialize the\nStreamHandler. Note that this argument is\nincompatible with ‘filename’ - if both are\npresent, ‘stream’ is ignored.
\n
\n\n
\n
\nlogging.shutdown()
\n
Informs the logging system to perform an orderly shutdown by flushing and\nclosing all handlers. This should be called at application exit and no\nfurther use of the logging system should be made after this call.
\n\n
\n
\nlogging.setLoggerClass(klass)
\n
Tells the logging system to use the class klass when instantiating a logger.\nThe class should define __init__() such that only a name argument is\nrequired, and the __init__() should call Logger.__init__(). This\nfunction is typically called before any loggers are instantiated by applications\nwhich need to use custom logger behavior.
\n\n
\n
\n

15.7.10. Integration with the warnings module

\n

The captureWarnings() function can be used to integrate logging\nwith the warnings module.

\n
\n
\nlogging.captureWarnings(capture)
\n

This function is used to turn the capture of warnings by logging on and\noff.

\n

If capture is True, warnings issued by the warnings module will\nbe redirected to the logging system. Specifically, a warning will be\nformatted using warnings.formatwarning() and the resulting string\nlogged to a logger named ‘py.warnings’ with a severity of WARNING.

\n

If capture is False, the redirection of warnings to the logging system\nwill stop, and warnings will be redirected to their original destinations\n(i.e. those in effect before captureWarnings(True) was called).

\n
\n\n
\n

See also

\n
\n
Module logging.config
\n
Configuration API for the logging module.
\n
Module logging.handlers
\n
Useful handlers included with the logging module.
\n
PEP 282 - A Logging System
\n
The proposal which described this feature for inclusion in the Python standard\nlibrary.
\n
Original Python logging package
\n
This is the original source for the logging package. The version of the\npackage available from this site is suitable for use with Python 1.5.2, 2.1.x\nand 2.2.x, which do not include the logging package in the standard\nlibrary.
\n
\n
\n
\n
", "searchableItems": [ { "name": "logging.addLevelName", "domId": "logging_logging.addLevelName" }, { "name": "logging.basicConfig", "domId": "logging_logging.basicConfig" }, { "name": "logging.captureWarnings", "domId": "logging_logging.captureWarnings" }, { "name": "logging.critical", "domId": "logging_logging.critical" }, { "name": "logging.debug", "domId": "logging_logging.debug" }, { "name": "logging.disable", "domId": "logging_logging.disable" }, { "name": "logging.error", "domId": "logging_logging.error" }, { "name": "logging.exception", "domId": "logging_logging.exception" }, { "name": "logging.Filter", "domId": "logging_logging.Filter" }, { "name": "logging.Filter.filter", "domId": "logging_logging.Filter.filter" }, { "name": "logging.Formatter", "domId": "logging_logging.Formatter" }, { "name": "logging.Formatter.format", "domId": "logging_logging.Formatter.format" }, { "name": "logging.Formatter.formatException", "domId": "logging_logging.Formatter.formatException" }, { "name": "logging.Formatter.formatTime", "domId": "logging_logging.Formatter.formatTime" }, { "name": "logging.getLevelName", "domId": "logging_logging.getLevelName" }, { "name": "logging.getLogger", "domId": "logging_logging.getLogger" }, { "name": "logging.getLoggerClass", "domId": "logging_logging.getLoggerClass" }, { "name": "logging.Handler.__init__", "domId": "logging_logging.Handler.__init__" }, { "name": "logging.Handler.acquire", "domId": "logging_logging.Handler.acquire" }, { "name": "logging.Handler.addFilter", "domId": "logging_logging.Handler.addFilter" }, { "name": "logging.Handler.close", "domId": "logging_logging.Handler.close" }, { "name": "logging.Handler.createLock", "domId": "logging_logging.Handler.createLock" }, { "name": "logging.Handler.emit", "domId": "logging_logging.Handler.emit" }, { "name": "logging.Handler.filter", "domId": "logging_logging.Handler.filter" }, { "name": "logging.Handler.flush", "domId": "logging_logging.Handler.flush" }, { "name": "logging.Handler.format", "domId": "logging_logging.Handler.format" }, { "name": "logging.Handler.handle", "domId": "logging_logging.Handler.handle" }, { "name": "logging.Handler.handleError", "domId": "logging_logging.Handler.handleError" }, { "name": "logging.Handler.release", "domId": "logging_logging.Handler.release" }, { "name": "logging.Handler.removeFilter", "domId": "logging_logging.Handler.removeFilter" }, { "name": "logging.Handler.setFormatter", "domId": "logging_logging.Handler.setFormatter" }, { "name": "logging.Handler.setLevel", "domId": "logging_logging.Handler.setLevel" }, { "name": "logging.info", "domId": "logging_logging.info" }, { "name": "logging.log", "domId": "logging_logging.log" }, { "name": "logging.Logger", "domId": "logging_logging.Logger" }, { "name": "logging.Logger.addFilter", "domId": "logging_logging.Logger.addFilter" }, { "name": "logging.Logger.addHandler", "domId": "logging_logging.Logger.addHandler" }, { "name": "logging.Logger.critical", "domId": "logging_logging.Logger.critical" }, { "name": "logging.Logger.debug", "domId": "logging_logging.Logger.debug" }, { "name": "logging.Logger.error", "domId": "logging_logging.Logger.error" }, { "name": "logging.Logger.exception", "domId": "logging_logging.Logger.exception" }, { "name": "logging.Logger.filter", "domId": "logging_logging.Logger.filter" }, { "name": "logging.Logger.findCaller", "domId": "logging_logging.Logger.findCaller" }, { "name": "logging.Logger.getChild", "domId": "logging_logging.Logger.getChild" }, { "name": "logging.Logger.getEffectiveLevel", "domId": "logging_logging.Logger.getEffectiveLevel" }, { "name": "logging.Logger.handle", "domId": "logging_logging.Logger.handle" }, { "name": "logging.Logger.info", "domId": "logging_logging.Logger.info" }, { "name": "logging.Logger.isEnabledFor", "domId": "logging_logging.Logger.isEnabledFor" }, { "name": "logging.Logger.log", "domId": "logging_logging.Logger.log" }, { "name": "logging.Logger.makeRecord", "domId": "logging_logging.Logger.makeRecord" }, { "name": "logging.Logger.removeFilter", "domId": "logging_logging.Logger.removeFilter" }, { "name": "logging.Logger.removeHandler", "domId": "logging_logging.Logger.removeHandler" }, { "name": "logging.Logger.setLevel", "domId": "logging_logging.Logger.setLevel" }, { "name": "logging.Logger.warning", "domId": "logging_logging.Logger.warning" }, { "name": "logging.LoggerAdapter", "domId": "logging_logging.LoggerAdapter" }, { "name": "logging.LoggerAdapter.process", "domId": "logging_logging.LoggerAdapter.process" }, { "name": "logging.LogRecord", "domId": "logging_logging.LogRecord" }, { "name": "logging.LogRecord.getMessage", "domId": "logging_logging.LogRecord.getMessage" }, { "name": "logging.makeLogRecord", "domId": "logging_logging.makeLogRecord" }, { "name": "logging.setLoggerClass", "domId": "logging_logging.setLoggerClass" }, { "name": "logging.shutdown", "domId": "logging_logging.shutdown" }, { "name": "logging.warning", "domId": "logging_logging.warning" } ] }, { "url": "http://docs.python.org/library/dummy_thread.html", "title": "dummy_thread", "html": "
\n

16.5. dummy_thread — Drop-in replacement for the thread module

\n
\n

Note

\n

The dummy_thread module has been renamed to _dummy_thread in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0; however, you should consider using the\nhigh-lever dummy_threading module instead.

\n
\n

Source code: Lib/dummy_thread.py

\n
\n

This module provides a duplicate interface to the thread module. It is\nmeant to be imported when the thread module is not provided on a\nplatform.

\n

Suggested usage is:

\n
try:\n    import thread as _thread\nexcept ImportError:\n    import dummy_thread as _thread\n
\n
\n

Be careful to not use this module where deadlock might occur from a thread\nbeing created that blocks waiting for another thread to be created. This often\noccurs with blocking I/O.

\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/ctypes.html", "title": "ctypes", "html": "
\n

15.17. ctypes — A foreign function library for Python

\n

\nNew in version 2.5.

\n

ctypes is a foreign function library for Python. It provides C compatible\ndata types, and allows calling functions in DLLs or shared libraries. It can be\nused to wrap these libraries in pure Python.

\n
\n

15.17.1. ctypes tutorial

\n

Note: The code samples in this tutorial use doctest to make sure that\nthey actually work. Since some code samples behave differently under Linux,\nWindows, or Mac OS X, they contain doctest directives in comments.

\n

Note: Some code samples reference the ctypes c_int type. This type is\nan alias for the c_long type on 32-bit systems. So, you should not be\nconfused if c_long is printed if you would expect c_int —\nthey are actually the same type.

\n\n
\n

15.17.1.2. Accessing functions from loaded dlls

\n

Functions are accessed as attributes of dll objects:

\n
>>> from ctypes import *\n>>> libc.printf\n<_FuncPtr object at 0x...>\n>>> print windll.kernel32.GetModuleHandleA # doctest: +WINDOWS\n<_FuncPtr object at 0x...>\n>>> print windll.kernel32.MyOwnFunction # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\n  File "ctypes.py", line 239, in __getattr__\n    func = _StdcallFuncPtr(name, self)\nAttributeError: function 'MyOwnFunction' not found\n>>>\n
\n
\n

Note that win32 system dlls like kernel32 and user32 often export ANSI\nas well as UNICODE versions of a function. The UNICODE version is exported with\nan W appended to the name, while the ANSI version is exported with an A\nappended to the name. The win32 GetModuleHandle function, which returns a\nmodule handle for a given module name, has the following C prototype, and a\nmacro is used to expose one of them as GetModuleHandle depending on whether\nUNICODE is defined or not:

\n
/* ANSI version */\nHMODULE GetModuleHandleA(LPCSTR lpModuleName);\n/* UNICODE version */\nHMODULE GetModuleHandleW(LPCWSTR lpModuleName);
\n
\n

windll does not try to select one of them by magic, you must access the\nversion you need by specifying GetModuleHandleA or GetModuleHandleW\nexplicitly, and then call it with strings or unicode strings\nrespectively.

\n

Sometimes, dlls export functions with names which aren’t valid Python\nidentifiers, like "??2@YAPAXI@Z". In this case you have to use\ngetattr() to retrieve the function:

\n
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z") # doctest: +WINDOWS\n<_FuncPtr object at 0x...>\n>>>\n
\n
\n

On Windows, some dlls export functions not by name but by ordinal. These\nfunctions can be accessed by indexing the dll object with the ordinal number:

\n
>>> cdll.kernel32[1] # doctest: +WINDOWS\n<_FuncPtr object at 0x...>\n>>> cdll.kernel32[0] # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\n  File "ctypes.py", line 310, in __getitem__\n    func = _StdcallFuncPtr(name, self)\nAttributeError: function ordinal 0 not found\n>>>\n
\n
\n
\n
\n

15.17.1.3. Calling functions

\n

You can call these functions like any other Python callable. This example uses\nthe time() function, which returns system time in seconds since the Unix\nepoch, and the GetModuleHandleA() function, which returns a win32 module\nhandle.

\n

This example calls both functions with a NULL pointer (None should be used\nas the NULL pointer):

\n
>>> print libc.time(None) # doctest: +SKIP\n1150640792\n>>> print hex(windll.kernel32.GetModuleHandleA(None)) # doctest: +WINDOWS\n0x1d000000\n>>>\n
\n
\n

ctypes tries to protect you from calling functions with the wrong number\nof arguments or the wrong calling convention. Unfortunately this only works on\nWindows. It does this by examining the stack after the function returns, so\nalthough an error is raised the function has been called:

\n
>>> windll.kernel32.GetModuleHandleA() # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: Procedure probably called with not enough arguments (4 bytes missing)\n>>> windll.kernel32.GetModuleHandleA(0, 0) # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: Procedure probably called with too many arguments (4 bytes in excess)\n>>>\n
\n
\n

The same exception is raised when you call an stdcall function with the\ncdecl calling convention, or vice versa:

\n
>>> cdll.kernel32.GetModuleHandleA(None) # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: Procedure probably called with not enough arguments (4 bytes missing)\n>>>\n\n>>> windll.msvcrt.printf("spam") # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: Procedure probably called with too many arguments (4 bytes in excess)\n>>>\n
\n
\n

To find out the correct calling convention you have to look into the C header\nfile or the documentation for the function you want to call.

\n

On Windows, ctypes uses win32 structured exception handling to prevent\ncrashes from general protection faults when functions are called with invalid\nargument values:

\n
>>> windll.kernel32.GetModuleHandleA(32) # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nWindowsError: exception: access violation reading 0x00000020\n>>>\n
\n
\n

There are, however, enough ways to crash Python with ctypes, so you\nshould be careful anyway.

\n

None, integers, longs, byte strings and unicode strings are the only native\nPython objects that can directly be used as parameters in these function calls.\nNone is passed as a C NULL pointer, byte strings and unicode strings are\npassed as pointer to the memory block that contains their data (char *\nor wchar_t *). Python integers and Python longs are passed as the\nplatforms default C int type, their value is masked to fit into the C\ntype.

\n

Before we move on calling functions with other parameter types, we have to learn\nmore about ctypes data types.

\n
\n
\n

15.17.1.4. Fundamental data types

\n

ctypes defines a number of primitive C compatible data types :

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ctypes typeC typePython type
c_bool_Boolbool (1)
c_charchar1-character string
c_wcharwchar_t1-character unicode string
c_bytecharint/long
c_ubyteunsigned charint/long
c_shortshortint/long
c_ushortunsigned shortint/long
c_intintint/long
c_uintunsigned intint/long
c_longlongint/long
c_ulongunsigned longint/long
c_longlong__int64 or long longint/long
c_ulonglongunsigned __int64 or\nunsigned long longint/long
c_floatfloatfloat
c_doubledoublefloat
c_longdoublelong doublefloat
c_char_pchar * (NUL terminated)string or None
c_wchar_pwchar_t * (NUL terminated)unicode or None
c_void_pvoid *int/long or None
\n
    \n
  1. The constructor accepts any object with a truth value.
  2. \n
\n

All these types can be created by calling them with an optional initializer of\nthe correct type and value:

\n
>>> c_int()\nc_long(0)\n>>> c_char_p("Hello, World")\nc_char_p('Hello, World')\n>>> c_ushort(-3)\nc_ushort(65533)\n>>>\n
\n
\n

Since these types are mutable, their value can also be changed afterwards:

\n
>>> i = c_int(42)\n>>> print i\nc_long(42)\n>>> print i.value\n42\n>>> i.value = -99\n>>> print i.value\n-99\n>>>\n
\n
\n

Assigning a new value to instances of the pointer types c_char_p,\nc_wchar_p, and c_void_p changes the memory location they\npoint to, not the contents of the memory block (of course not, because Python\nstrings are immutable):

\n
>>> s = "Hello, World"\n>>> c_s = c_char_p(s)\n>>> print c_s\nc_char_p('Hello, World')\n>>> c_s.value = "Hi, there"\n>>> print c_s\nc_char_p('Hi, there')\n>>> print s                 # first string is unchanged\nHello, World\n>>>\n
\n
\n

You should be careful, however, not to pass them to functions expecting pointers\nto mutable memory. If you need mutable memory blocks, ctypes has a\ncreate_string_buffer() function which creates these in various ways. The\ncurrent memory block contents can be accessed (or changed) with the raw\nproperty; if you want to access it as NUL terminated string, use the value\nproperty:

\n
>>> from ctypes import *\n>>> p = create_string_buffer(3)      # create a 3 byte buffer, initialized to NUL bytes\n>>> print sizeof(p), repr(p.raw)\n3 '\\x00\\x00\\x00'\n>>> p = create_string_buffer("Hello")      # create a buffer containing a NUL terminated string\n>>> print sizeof(p), repr(p.raw)\n6 'Hello\\x00'\n>>> print repr(p.value)\n'Hello'\n>>> p = create_string_buffer("Hello", 10)  # create a 10 byte buffer\n>>> print sizeof(p), repr(p.raw)\n10 'Hello\\x00\\x00\\x00\\x00\\x00'\n>>> p.value = "Hi"\n>>> print sizeof(p), repr(p.raw)\n10 'Hi\\x00lo\\x00\\x00\\x00\\x00\\x00'\n>>>\n
\n
\n

The create_string_buffer() function replaces the c_buffer() function\n(which is still available as an alias), as well as the c_string() function\nfrom earlier ctypes releases. To create a mutable memory block containing\nunicode characters of the C type wchar_t use the\ncreate_unicode_buffer() function.

\n
\n
\n

15.17.1.5. Calling functions, continued

\n

Note that printf prints to the real standard output channel, not to\nsys.stdout, so these examples will only work at the console prompt, not\nfrom within IDLE or PythonWin:

\n
>>> printf = libc.printf\n>>> printf("Hello, %s\\n", "World!")\nHello, World!\n14\n>>> printf("Hello, %S\\n", u"World!")\nHello, World!\n14\n>>> printf("%d bottles of beer\\n", 42)\n42 bottles of beer\n19\n>>> printf("%f bottles of beer\\n", 42.5)\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nArgumentError: argument 2: exceptions.TypeError: Don't know how to convert parameter 2\n>>>\n
\n
\n

As has been mentioned before, all Python types except integers, strings, and\nunicode strings have to be wrapped in their corresponding ctypes type, so\nthat they can be converted to the required C data type:

\n
>>> printf("An int %d, a double %f\\n", 1234, c_double(3.14))\nAn int 1234, a double 3.140000\n31\n>>>\n
\n
\n
\n
\n

15.17.1.6. Calling functions with your own custom data types

\n

You can also customize ctypes argument conversion to allow instances of\nyour own classes be used as function arguments. ctypes looks for an\n_as_parameter_ attribute and uses this as the function argument. Of\ncourse, it must be one of integer, string, or unicode:

\n
>>> class Bottles(object):\n...     def __init__(self, number):\n...         self._as_parameter_ = number\n...\n>>> bottles = Bottles(42)\n>>> printf("%d bottles of beer\\n", bottles)\n42 bottles of beer\n19\n>>>\n
\n
\n

If you don’t want to store the instance’s data in the _as_parameter_\ninstance variable, you could define a property() which makes the data\navailable.

\n
\n
\n

15.17.1.7. Specifying the required argument types (function prototypes)

\n

It is possible to specify the required argument types of functions exported from\nDLLs by setting the argtypes attribute.

\n

argtypes must be a sequence of C data types (the printf function is\nprobably not a good example here, because it takes a variable number and\ndifferent types of parameters depending on the format string, on the other hand\nthis is quite handy to experiment with this feature):

\n
>>> printf.argtypes = [c_char_p, c_char_p, c_int, c_double]\n>>> printf("String '%s', Int %d, Double %f\\n", "Hi", 10, 2.2)\nString 'Hi', Int 10, Double 2.200000\n37\n>>>\n
\n
\n

Specifying a format protects against incompatible argument types (just as a\nprototype for a C function), and tries to convert the arguments to valid types:

\n
>>> printf("%d %d %d", 1, 2, 3)\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nArgumentError: argument 2: exceptions.TypeError: wrong type\n>>> printf("%s %d %f\\n", "X", 2, 3)\nX 2 3.000000\n13\n>>>\n
\n
\n

If you have defined your own classes which you pass to function calls, you have\nto implement a from_param() class method for them to be able to use them\nin the argtypes sequence. The from_param() class method receives\nthe Python object passed to the function call, it should do a typecheck or\nwhatever is needed to make sure this object is acceptable, and then return the\nobject itself, its _as_parameter_ attribute, or whatever you want to\npass as the C function argument in this case. Again, the result should be an\ninteger, string, unicode, a ctypes instance, or an object with an\n_as_parameter_ attribute.

\n
\n
\n

15.17.1.8. Return types

\n

By default functions are assumed to return the C int type. Other\nreturn types can be specified by setting the restype attribute of the\nfunction object.

\n

Here is a more advanced example, it uses the strchr function, which expects\na string pointer and a char, and returns a pointer to a string:

\n
>>> strchr = libc.strchr\n>>> strchr("abcdef", ord("d")) # doctest: +SKIP\n8059983\n>>> strchr.restype = c_char_p # c_char_p is a pointer to a string\n>>> strchr("abcdef", ord("d"))\n'def'\n>>> print strchr("abcdef", ord("x"))\nNone\n>>>\n
\n
\n

If you want to avoid the ord("x") calls above, you can set the\nargtypes attribute, and the second argument will be converted from a\nsingle character Python string into a C char:

\n
>>> strchr.restype = c_char_p\n>>> strchr.argtypes = [c_char_p, c_char]\n>>> strchr("abcdef", "d")\n'def'\n>>> strchr("abcdef", "def")\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nArgumentError: argument 2: exceptions.TypeError: one character string expected\n>>> print strchr("abcdef", "x")\nNone\n>>> strchr("abcdef", "d")\n'def'\n>>>\n
\n
\n

You can also use a callable Python object (a function or a class for example) as\nthe restype attribute, if the foreign function returns an integer. The\ncallable will be called with the integer the C function returns, and the\nresult of this call will be used as the result of your function call. This is\nuseful to check for error return values and automatically raise an exception:

\n
>>> GetModuleHandle = windll.kernel32.GetModuleHandleA # doctest: +WINDOWS\n>>> def ValidHandle(value):\n...     if value == 0:\n...         raise WinError()\n...     return value\n...\n>>>\n>>> GetModuleHandle.restype = ValidHandle # doctest: +WINDOWS\n>>> GetModuleHandle(None) # doctest: +WINDOWS\n486539264\n>>> GetModuleHandle("something silly") # doctest: +WINDOWS\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\n  File "<stdin>", line 3, in ValidHandle\nWindowsError: [Errno 126] The specified module could not be found.\n>>>\n
\n
\n

WinError is a function which will call Windows FormatMessage() api to\nget the string representation of an error code, and returns an exception.\nWinError takes an optional error code parameter, if no one is used, it calls\nGetLastError() to retrieve it.

\n

Please note that a much more powerful error checking mechanism is available\nthrough the errcheck attribute; see the reference manual for details.

\n
\n
\n

15.17.1.9. Passing pointers (or: passing parameters by reference)

\n

Sometimes a C api function expects a pointer to a data type as parameter,\nprobably to write into the corresponding location, or if the data is too large\nto be passed by value. This is also known as passing parameters by reference.

\n

ctypes exports the byref() function which is used to pass\nparameters by reference. The same effect can be achieved with the\npointer() function, although pointer() does a lot more work since it\nconstructs a real pointer object, so it is faster to use byref() if you\ndon’t need the pointer object in Python itself:

\n
>>> i = c_int()\n>>> f = c_float()\n>>> s = create_string_buffer('\\000' * 32)\n>>> print i.value, f.value, repr(s.value)\n0 0.0 ''\n>>> libc.sscanf("1 3.14 Hello", "%d %f %s",\n...             byref(i), byref(f), s)\n3\n>>> print i.value, f.value, repr(s.value)\n1 3.1400001049 'Hello'\n>>>\n
\n
\n
\n
\n

15.17.1.10. Structures and unions

\n

Structures and unions must derive from the Structure and Union\nbase classes which are defined in the ctypes module. Each subclass must\ndefine a _fields_ attribute. _fields_ must be a list of\n2-tuples, containing a field name and a field type.

\n

The field type must be a ctypes type like c_int, or any other\nderived ctypes type: structure, union, array, pointer.

\n

Here is a simple example of a POINT structure, which contains two integers named\nx and y, and also shows how to initialize a structure in the constructor:

\n
>>> from ctypes import *\n>>> class POINT(Structure):\n...     _fields_ = [("x", c_int),\n...                 ("y", c_int)]\n...\n>>> point = POINT(10, 20)\n>>> print point.x, point.y\n10 20\n>>> point = POINT(y=5)\n>>> print point.x, point.y\n0 5\n>>> POINT(1, 2, 3)\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: too many initializers\n>>>\n
\n
\n

You can, however, build much more complicated structures. Structures can itself\ncontain other structures by using a structure as a field type.

\n

Here is a RECT structure which contains two POINTs named upperleft and\nlowerright:

\n
>>> class RECT(Structure):\n...     _fields_ = [("upperleft", POINT),\n...                 ("lowerright", POINT)]\n...\n>>> rc = RECT(point)\n>>> print rc.upperleft.x, rc.upperleft.y\n0 5\n>>> print rc.lowerright.x, rc.lowerright.y\n0 0\n>>>\n
\n
\n

Nested structures can also be initialized in the constructor in several ways:

\n
>>> r = RECT(POINT(1, 2), POINT(3, 4))\n>>> r = RECT((1, 2), (3, 4))\n
\n
\n

Field descriptors can be retrieved from the class, they are useful\nfor debugging because they can provide useful information:

\n
>>> print POINT.x\n<Field type=c_long, ofs=0, size=4>\n>>> print POINT.y\n<Field type=c_long, ofs=4, size=4>\n>>>\n
\n
\n
\n
\n

15.17.1.11. Structure/union alignment and byte order

\n

By default, Structure and Union fields are aligned in the same way the C\ncompiler does it. It is possible to override this behavior be specifying a\n_pack_ class attribute in the subclass definition. This must be set to a\npositive integer and specifies the maximum alignment for the fields. This is\nwhat #pragma pack(n) also does in MSVC.

\n

ctypes uses the native byte order for Structures and Unions. To build\nstructures with non-native byte order, you can use one of the\nBigEndianStructure, LittleEndianStructure,\nBigEndianUnion, and LittleEndianUnion base classes. These\nclasses cannot contain pointer fields.

\n
\n
\n

15.17.1.12. Bit fields in structures and unions

\n

It is possible to create structures and unions containing bit fields. Bit fields\nare only possible for integer fields, the bit width is specified as the third\nitem in the _fields_ tuples:

\n
>>> class Int(Structure):\n...     _fields_ = [("first_16", c_int, 16),\n...                 ("second_16", c_int, 16)]\n...\n>>> print Int.first_16\n<Field type=c_long, ofs=0:0, bits=16>\n>>> print Int.second_16\n<Field type=c_long, ofs=0:16, bits=16>\n>>>\n
\n
\n
\n
\n

15.17.1.13. Arrays

\n

Arrays are sequences, containing a fixed number of instances of the same type.

\n

The recommended way to create array types is by multiplying a data type with a\npositive integer:

\n
TenPointsArrayType = POINT * 10\n
\n
\n

Here is an example of an somewhat artificial data type, a structure containing 4\nPOINTs among other stuff:

\n
>>> from ctypes import *\n>>> class POINT(Structure):\n...    _fields_ = ("x", c_int), ("y", c_int)\n...\n>>> class MyStruct(Structure):\n...    _fields_ = [("a", c_int),\n...                ("b", c_float),\n...                ("point_array", POINT * 4)]\n>>>\n>>> print len(MyStruct().point_array)\n4\n>>>\n
\n
\n

Instances are created in the usual way, by calling the class:

\n
arr = TenPointsArrayType()\nfor pt in arr:\n    print pt.x, pt.y\n
\n
\n

The above code print a series of 0 0 lines, because the array contents is\ninitialized to zeros.

\n

Initializers of the correct type can also be specified:

\n
>>> from ctypes import *\n>>> TenIntegers = c_int * 10\n>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n>>> print ii\n<c_long_Array_10 object at 0x...>\n>>> for i in ii: print i,\n...\n1 2 3 4 5 6 7 8 9 10\n>>>\n
\n
\n
\n
\n

15.17.1.14. Pointers

\n

Pointer instances are created by calling the pointer() function on a\nctypes type:

\n
>>> from ctypes import *\n>>> i = c_int(42)\n>>> pi = pointer(i)\n>>>\n
\n
\n

Pointer instances have a contents attribute which returns the object to\nwhich the pointer points, the i object above:

\n
>>> pi.contents\nc_long(42)\n>>>\n
\n
\n

Note that ctypes does not have OOR (original object return), it constructs a\nnew, equivalent object each time you retrieve an attribute:

\n
>>> pi.contents is i\nFalse\n>>> pi.contents is pi.contents\nFalse\n>>>\n
\n
\n

Assigning another c_int instance to the pointer’s contents attribute\nwould cause the pointer to point to the memory location where this is stored:

\n
>>> i = c_int(99)\n>>> pi.contents = i\n>>> pi.contents\nc_long(99)\n>>>\n
\n
\n

Pointer instances can also be indexed with integers:

\n
>>> pi[0]\n99\n>>>\n
\n
\n

Assigning to an integer index changes the pointed to value:

\n
>>> print i\nc_long(99)\n>>> pi[0] = 22\n>>> print i\nc_long(22)\n>>>\n
\n
\n

It is also possible to use indexes different from 0, but you must know what\nyou’re doing, just as in C: You can access or change arbitrary memory locations.\nGenerally you only use this feature if you receive a pointer from a C function,\nand you know that the pointer actually points to an array instead of a single\nitem.

\n

Behind the scenes, the pointer() function does more than simply create\npointer instances, it has to create pointer types first. This is done with\nthe POINTER() function, which accepts any ctypes type, and returns\na new type:

\n
>>> PI = POINTER(c_int)\n>>> PI\n<class 'ctypes.LP_c_long'>\n>>> PI(42)\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nTypeError: expected c_long instead of int\n>>> PI(c_int(42))\n<ctypes.LP_c_long object at 0x...>\n>>>\n
\n
\n

Calling the pointer type without an argument creates a NULL pointer.\nNULL pointers have a False boolean value:

\n
>>> null_ptr = POINTER(c_int)()\n>>> print bool(null_ptr)\nFalse\n>>>\n
\n
\n

ctypes checks for NULL when dereferencing pointers (but dereferencing\ninvalid non-NULL pointers would crash Python):

\n
>>> null_ptr[0]\nTraceback (most recent call last):\n    ....\nValueError: NULL pointer access\n>>>\n\n>>> null_ptr[0] = 1234\nTraceback (most recent call last):\n    ....\nValueError: NULL pointer access\n>>>\n
\n
\n
\n
\n

15.17.1.15. Type conversions

\n

Usually, ctypes does strict type checking. This means, if you have\nPOINTER(c_int) in the argtypes list of a function or as the type of\na member field in a structure definition, only instances of exactly the same\ntype are accepted. There are some exceptions to this rule, where ctypes accepts\nother objects. For example, you can pass compatible array instances instead of\npointer types. So, for POINTER(c_int), ctypes accepts an array of c_int:

\n
>>> class Bar(Structure):\n...     _fields_ = [("count", c_int), ("values", POINTER(c_int))]\n...\n>>> bar = Bar()\n>>> bar.values = (c_int * 3)(1, 2, 3)\n>>> bar.count = 3\n>>> for i in range(bar.count):\n...     print bar.values[i]\n...\n1\n2\n3\n>>>\n
\n
\n

To set a POINTER type field to NULL, you can assign None:

\n
>>> bar.values = None\n>>>\n
\n
\n

Sometimes you have instances of incompatible types. In C, you can cast one type\ninto another type. ctypes provides a cast() function which can be\nused in the same way. The Bar structure defined above accepts\nPOINTER(c_int) pointers or c_int arrays for its values field,\nbut not instances of other types:

\n
>>> bar.values = (c_byte * 4)()\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nTypeError: incompatible types, c_byte_Array_4 instance instead of LP_c_long instance\n>>>\n
\n
\n

For these cases, the cast() function is handy.

\n

The cast() function can be used to cast a ctypes instance into a pointer\nto a different ctypes data type. cast() takes two parameters, a ctypes\nobject that is or can be converted to a pointer of some kind, and a ctypes\npointer type. It returns an instance of the second argument, which references\nthe same memory block as the first argument:

\n
>>> a = (c_byte * 4)()\n>>> cast(a, POINTER(c_int))\n<ctypes.LP_c_long object at ...>\n>>>\n
\n
\n

So, cast() can be used to assign to the values field of Bar the\nstructure:

\n
>>> bar = Bar()\n>>> bar.values = cast((c_byte * 4)(), POINTER(c_int))\n>>> print bar.values[0]\n0\n>>>\n
\n
\n
\n
\n

15.17.1.16. Incomplete Types

\n

Incomplete Types are structures, unions or arrays whose members are not yet\nspecified. In C, they are specified by forward declarations, which are defined\nlater:

\n
struct cell; /* forward declaration */\n\nstruct cell {\n    char *name;\n    struct cell *next;\n};
\n
\n

The straightforward translation into ctypes code would be this, but it does not\nwork:

\n
>>> class cell(Structure):\n...     _fields_ = [("name", c_char_p),\n...                 ("next", POINTER(cell))]\n...\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\n  File "<stdin>", line 2, in cell\nNameError: name 'cell' is not defined\n>>>\n
\n
\n

because the new class cell is not available in the class statement itself.\nIn ctypes, we can define the cell class and set the _fields_\nattribute later, after the class statement:

\n
>>> from ctypes import *\n>>> class cell(Structure):\n...     pass\n...\n>>> cell._fields_ = [("name", c_char_p),\n...                  ("next", POINTER(cell))]\n>>>\n
\n
\n

Lets try it. We create two instances of cell, and let them point to each\nother, and finally follow the pointer chain a few times:

\n
>>> c1 = cell()\n>>> c1.name = "foo"\n>>> c2 = cell()\n>>> c2.name = "bar"\n>>> c1.next = pointer(c2)\n>>> c2.next = pointer(c1)\n>>> p = c1\n>>> for i in range(8):\n...     print p.name,\n...     p = p.next[0]\n...\nfoo bar foo bar foo bar foo bar\n>>>\n
\n
\n
\n
\n

15.17.1.17. Callback functions

\n

ctypes allows to create C callable function pointers from Python callables.\nThese are sometimes called callback functions.

\n

First, you must create a class for the callback function, the class knows the\ncalling convention, the return type, and the number and types of arguments this\nfunction will receive.

\n

The CFUNCTYPE factory function creates types for callback functions using the\nnormal cdecl calling convention, and, on Windows, the WINFUNCTYPE factory\nfunction creates types for callback functions using the stdcall calling\nconvention.

\n

Both of these factory functions are called with the result type as first\nargument, and the callback functions expected argument types as the remaining\narguments.

\n

I will present an example here which uses the standard C library’s qsort()\nfunction, this is used to sort items with the help of a callback function.\nqsort() will be used to sort an array of integers:

\n
>>> IntArray5 = c_int * 5\n>>> ia = IntArray5(5, 1, 7, 33, 99)\n>>> qsort = libc.qsort\n>>> qsort.restype = None\n>>>\n
\n
\n

qsort() must be called with a pointer to the data to sort, the number of\nitems in the data array, the size of one item, and a pointer to the comparison\nfunction, the callback. The callback will then be called with two pointers to\nitems, and it must return a negative integer if the first item is smaller than\nthe second, a zero if they are equal, and a positive integer else.

\n

So our callback function receives pointers to integers, and must return an\ninteger. First we create the type for the callback function:

\n
>>> CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))\n>>>\n
\n
\n

For the first implementation of the callback function, we simply print the\narguments we get, and return 0 (incremental development ;-):

\n
>>> def py_cmp_func(a, b):\n...     print "py_cmp_func", a, b\n...     return 0\n...\n>>>\n
\n
\n

Create the C callable callback:

\n
>>> cmp_func = CMPFUNC(py_cmp_func)\n>>>\n
\n
\n

And we’re ready to go:

\n
>>> qsort(ia, len(ia), sizeof(c_int), cmp_func) # doctest: +WINDOWS\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\npy_cmp_func <ctypes.LP_c_long object at 0x00...> <ctypes.LP_c_long object at 0x00...>\n>>>\n
\n
\n

We know how to access the contents of a pointer, so lets redefine our callback:

\n
>>> def py_cmp_func(a, b):\n...     print "py_cmp_func", a[0], b[0]\n...     return 0\n...\n>>> cmp_func = CMPFUNC(py_cmp_func)\n>>>\n
\n
\n

Here is what we get on Windows:

\n
>>> qsort(ia, len(ia), sizeof(c_int), cmp_func) # doctest: +WINDOWS\npy_cmp_func 7 1\npy_cmp_func 33 1\npy_cmp_func 99 1\npy_cmp_func 5 1\npy_cmp_func 7 5\npy_cmp_func 33 5\npy_cmp_func 99 5\npy_cmp_func 7 99\npy_cmp_func 33 99\npy_cmp_func 7 33\n>>>\n
\n
\n

It is funny to see that on linux the sort function seems to work much more\nefficiently, it is doing less comparisons:

\n
>>> qsort(ia, len(ia), sizeof(c_int), cmp_func) # doctest: +LINUX\npy_cmp_func 5 1\npy_cmp_func 33 99\npy_cmp_func 7 33\npy_cmp_func 5 7\npy_cmp_func 1 7\n>>>\n
\n
\n

Ah, we’re nearly done! The last step is to actually compare the two items and\nreturn a useful result:

\n
>>> def py_cmp_func(a, b):\n...     print "py_cmp_func", a[0], b[0]\n...     return a[0] - b[0]\n...\n>>>\n
\n
\n

Final run on Windows:

\n
>>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func)) # doctest: +WINDOWS\npy_cmp_func 33 7\npy_cmp_func 99 33\npy_cmp_func 5 99\npy_cmp_func 1 99\npy_cmp_func 33 7\npy_cmp_func 1 33\npy_cmp_func 5 33\npy_cmp_func 5 7\npy_cmp_func 1 7\npy_cmp_func 5 1\n>>>\n
\n
\n

and on Linux:

\n
>>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func)) # doctest: +LINUX\npy_cmp_func 5 1\npy_cmp_func 33 99\npy_cmp_func 7 33\npy_cmp_func 1 7\npy_cmp_func 5 7\n>>>\n
\n
\n

It is quite interesting to see that the Windows qsort() function needs\nmore comparisons than the linux version!

\n

As we can easily check, our array is sorted now:

\n
>>> for i in ia: print i,\n...\n1 5 7 33 99\n>>>\n
\n
\n

Important note for callback functions:

\n

Make sure you keep references to CFUNCTYPE objects as long as they are used from\nC code. ctypes doesn’t, and if you don’t, they may be garbage collected,\ncrashing your program when a callback is made.

\n
\n
\n

15.17.1.18. Accessing values exported from dlls

\n

Some shared libraries not only export functions, they also export variables. An\nexample in the Python library itself is the Py_OptimizeFlag, an integer set\nto 0, 1, or 2, depending on the -O or -OO flag given on\nstartup.

\n

ctypes can access values like this with the in_dll() class methods of\nthe type. pythonapi is a predefined symbol giving access to the Python C\napi:

\n
>>> opt_flag = c_int.in_dll(pythonapi, "Py_OptimizeFlag")\n>>> print opt_flag\nc_long(0)\n>>>\n
\n
\n

If the interpreter would have been started with -O, the sample would\nhave printed c_long(1), or c_long(2) if -OO would have been\nspecified.

\n

An extended example which also demonstrates the use of pointers accesses the\nPyImport_FrozenModules pointer exported by Python.

\n

Quoting the Python docs: This pointer is initialized to point to an array of\n“struct _frozen” records, terminated by one whose members are all NULL or zero.\nWhen a frozen module is imported, it is searched in this table. Third-party code\ncould play tricks with this to provide a dynamically created collection of\nfrozen modules.

\n

So manipulating this pointer could even prove useful. To restrict the example\nsize, we show only how this table can be read with ctypes:

\n
>>> from ctypes import *\n>>>\n>>> class struct_frozen(Structure):\n...     _fields_ = [("name", c_char_p),\n...                 ("code", POINTER(c_ubyte)),\n...                 ("size", c_int)]\n...\n>>>\n
\n
\n

We have defined the struct _frozen data type, so we can get the pointer to\nthe table:

\n
>>> FrozenTable = POINTER(struct_frozen)\n>>> table = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules")\n>>>\n
\n
\n

Since table is a pointer to the array of struct_frozen records, we\ncan iterate over it, but we just have to make sure that our loop terminates,\nbecause pointers have no size. Sooner or later it would probably crash with an\naccess violation or whatever, so it’s better to break out of the loop when we\nhit the NULL entry:

\n
>>> for item in table:\n...    print item.name, item.size\n...    if item.name is None:\n...        break\n...\n__hello__ 104\n__phello__ -104\n__phello__.spam 104\nNone 0\n>>>\n
\n
\n

The fact that standard Python has a frozen module and a frozen package\n(indicated by the negative size member) is not well known, it is only used for\ntesting. Try it out with import __hello__ for example.

\n
\n
\n

15.17.1.19. Surprises

\n

There are some edges in ctypes where you may be expect something else than\nwhat actually happens.

\n

Consider the following example:

\n
>>> from ctypes import *\n>>> class POINT(Structure):\n...     _fields_ = ("x", c_int), ("y", c_int)\n...\n>>> class RECT(Structure):\n...     _fields_ = ("a", POINT), ("b", POINT)\n...\n>>> p1 = POINT(1, 2)\n>>> p2 = POINT(3, 4)\n>>> rc = RECT(p1, p2)\n>>> print rc.a.x, rc.a.y, rc.b.x, rc.b.y\n1 2 3 4\n>>> # now swap the two points\n>>> rc.a, rc.b = rc.b, rc.a\n>>> print rc.a.x, rc.a.y, rc.b.x, rc.b.y\n3 4 3 4\n>>>\n
\n
\n

Hm. We certainly expected the last statement to print 3 4 1 2. What\nhappened? Here are the steps of the rc.a, rc.b = rc.b, rc.a line above:

\n
>>> temp0, temp1 = rc.b, rc.a\n>>> rc.a = temp0\n>>> rc.b = temp1\n>>>\n
\n
\n

Note that temp0 and temp1 are objects still using the internal buffer of\nthe rc object above. So executing rc.a = temp0 copies the buffer\ncontents of temp0 into rc ‘s buffer. This, in turn, changes the\ncontents of temp1. So, the last assignment rc.b = temp1, doesn’t have\nthe expected effect.

\n

Keep in mind that retrieving sub-objects from Structure, Unions, and Arrays\ndoesn’t copy the sub-object, instead it retrieves a wrapper object accessing\nthe root-object’s underlying buffer.

\n

Another example that may behave different from what one would expect is this:

\n
>>> s = c_char_p()\n>>> s.value = "abc def ghi"\n>>> s.value\n'abc def ghi'\n>>> s.value is s.value\nFalse\n>>>\n
\n
\n

Why is it printing False? ctypes instances are objects containing a memory\nblock plus some descriptors accessing the contents of the memory.\nStoring a Python object in the memory block does not store the object itself,\ninstead the contents of the object is stored. Accessing the contents again\nconstructs a new Python object each time!

\n
\n
\n

15.17.1.20. Variable-sized data types

\n

ctypes provides some support for variable-sized arrays and structures.

\n

The resize() function can be used to resize the memory buffer of an\nexisting ctypes object. The function takes the object as first argument, and\nthe requested size in bytes as the second argument. The memory block cannot be\nmade smaller than the natural memory block specified by the objects type, a\nValueError is raised if this is tried:

\n
>>> short_array = (c_short * 4)()\n>>> print sizeof(short_array)\n8\n>>> resize(short_array, 4)\nTraceback (most recent call last):\n    ...\nValueError: minimum size is 8\n>>> resize(short_array, 32)\n>>> sizeof(short_array)\n32\n>>> sizeof(type(short_array))\n8\n>>>\n
\n
\n

This is nice and fine, but how would one access the additional elements\ncontained in this array? Since the type still only knows about 4 elements, we\nget errors accessing other elements:

\n
>>> short_array[:]\n[0, 0, 0, 0]\n>>> short_array[7]\nTraceback (most recent call last):\n    ...\nIndexError: invalid index\n>>>\n
\n
\n

Another way to use variable-sized data types with ctypes is to use the\ndynamic nature of Python, and (re-)define the data type after the required size\nis already known, on a case by case basis.

\n
\n
\n
\n

15.17.2. ctypes reference

\n
\n

15.17.2.1. Finding shared libraries

\n

When programming in a compiled language, shared libraries are accessed when\ncompiling/linking a program, and when the program is run.

\n

The purpose of the find_library() function is to locate a library in a way\nsimilar to what the compiler does (on platforms with several versions of a\nshared library the most recent should be loaded), while the ctypes library\nloaders act like when a program is run, and call the runtime loader directly.

\n

The ctypes.util module provides a function which can help to determine the\nlibrary to load.

\n
\n
\nctypes.util.find_library(name)
\n
Try to find a library and return a pathname. name is the library name without\nany prefix like lib, suffix like .so, .dylib or version number (this\nis the form used for the posix linker option -l). If no library can\nbe found, returns None.
\n\n

The exact functionality is system dependent.

\n

On Linux, find_library() tries to run external programs\n(/sbin/ldconfig, gcc, and objdump) to find the library file. It\nreturns the filename of the library file. Here are some examples:

\n
>>> from ctypes.util import find_library\n>>> find_library("m")\n'libm.so.6'\n>>> find_library("c")\n'libc.so.6'\n>>> find_library("bz2")\n'libbz2.so.1.0'\n>>>\n
\n
\n

On OS X, find_library() tries several predefined naming schemes and paths\nto locate the library, and returns a full pathname if successful:

\n
>>> from ctypes.util import find_library\n>>> find_library("c")\n'/usr/lib/libc.dylib'\n>>> find_library("m")\n'/usr/lib/libm.dylib'\n>>> find_library("bz2")\n'/usr/lib/libbz2.dylib'\n>>> find_library("AGL")\n'/System/Library/Frameworks/AGL.framework/AGL'\n>>>\n
\n
\n

On Windows, find_library() searches along the system search path, and\nreturns the full pathname, but since there is no predefined naming scheme a call\nlike find_library("c") will fail and return None.

\n

If wrapping a shared library with ctypes, it may be better to determine\nthe shared library name at development type, and hardcode that into the wrapper\nmodule instead of using find_library() to locate the library at runtime.

\n
\n
\n

15.17.2.2. Loading shared libraries

\n

There are several ways to loaded shared libraries into the Python process. One\nway is to instantiate one of the following classes:

\n
\n
\nclass ctypes.CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)
\n
Instances of this class represent loaded shared libraries. Functions in these\nlibraries use the standard C calling convention, and are assumed to return\nint.
\n\n
\n
\nclass ctypes.OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)
\n
Windows only: Instances of this class represent loaded shared libraries,\nfunctions in these libraries use the stdcall calling convention, and are\nassumed to return the windows specific HRESULT code. HRESULT\nvalues contain information specifying whether the function call failed or\nsucceeded, together with additional error code. If the return value signals a\nfailure, an WindowsError is automatically raised.
\n\n
\n
\nclass ctypes.WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)
\n

Windows only: Instances of this class represent loaded shared libraries,\nfunctions in these libraries use the stdcall calling convention, and are\nassumed to return int by default.

\n

On Windows CE only the standard calling convention is used, for convenience the\nWinDLL and OleDLL use the standard calling convention on this\nplatform.

\n
\n\n

The Python global interpreter lock is released before calling any\nfunction exported by these libraries, and reacquired afterwards.

\n
\n
\nclass ctypes.PyDLL(name, mode=DEFAULT_MODE, handle=None)
\n

Instances of this class behave like CDLL instances, except that the\nPython GIL is not released during the function call, and after the function\nexecution the Python error flag is checked. If the error flag is set, a Python\nexception is raised.

\n

Thus, this is only useful to call Python C api functions directly.

\n
\n\n

All these classes can be instantiated by calling them with at least one\nargument, the pathname of the shared library. If you have an existing handle to\nan already loaded shared library, it can be passed as the handle named\nparameter, otherwise the underlying platforms dlopen or LoadLibrary\nfunction is used to load the library into the process, and to get a handle to\nit.

\n

The mode parameter can be used to specify how the library is loaded. For\ndetails, consult the dlopen(3) manpage, on Windows, mode is\nignored.

\n

The use_errno parameter, when set to True, enables a ctypes mechanism that\nallows to access the system errno error number in a safe way.\nctypes maintains a thread-local copy of the systems errno\nvariable; if you call foreign functions created with use_errno=True then the\nerrno value before the function call is swapped with the ctypes private\ncopy, the same happens immediately after the function call.

\n

The function ctypes.get_errno() returns the value of the ctypes private\ncopy, and the function ctypes.set_errno() changes the ctypes private copy\nto a new value and returns the former value.

\n

The use_last_error parameter, when set to True, enables the same mechanism for\nthe Windows error code which is managed by the GetLastError() and\nSetLastError() Windows API functions; ctypes.get_last_error() and\nctypes.set_last_error() are used to request and change the ctypes private\ncopy of the windows error code.

\n

\nNew in version 2.6: The use_last_error and use_errno optional parameters were added.

\n
\n
\nctypes.RTLD_GLOBAL
\n
Flag to use as mode parameter. On platforms where this flag is not available,\nit is defined as the integer zero.
\n\n
\n
\nctypes.RTLD_LOCAL
\n
Flag to use as mode parameter. On platforms where this is not available, it\nis the same as RTLD_GLOBAL.
\n\n
\n
\nctypes.DEFAULT_MODE
\n
The default mode which is used to load shared libraries. On OSX 10.3, this is\nRTLD_GLOBAL, otherwise it is the same as RTLD_LOCAL.
\n\n

Instances of these classes have no public methods, however __getattr__()\nand __getitem__() have special behavior: functions exported by the shared\nlibrary can be accessed as attributes of by index. Please note that both\n__getattr__() and __getitem__() cache their result, so calling them\nrepeatedly returns the same object each time.

\n

The following public attributes are available, their name starts with an\nunderscore to not clash with exported function names:

\n
\n
\nPyDLL._handle
\n
The system handle used to access the library.
\n\n
\n
\nPyDLL._name
\n
The name of the library passed in the constructor.
\n\n

Shared libraries can also be loaded by using one of the prefabricated objects,\nwhich are instances of the LibraryLoader class, either by calling the\nLoadLibrary() method, or by retrieving the library as attribute of the\nloader instance.

\n
\n
\nclass ctypes.LibraryLoader(dlltype)
\n

Class which loads shared libraries. dlltype should be one of the\nCDLL, PyDLL, WinDLL, or OleDLL types.

\n

__getattr__() has special behavior: It allows to load a shared library by\naccessing it as attribute of a library loader instance. The result is cached,\nso repeated attribute accesses return the same library each time.

\n
\n
\nLoadLibrary(name)
\n
Load a shared library into the process and return it. This method always\nreturns a new instance of the library.
\n\n
\n\n

These prefabricated library loaders are available:

\n
\n
\nctypes.cdll
\n
Creates CDLL instances.
\n\n
\n
\nctypes.windll
\n
Windows only: Creates WinDLL instances.
\n\n
\n
\nctypes.oledll
\n
Windows only: Creates OleDLL instances.
\n\n
\n
\nctypes.pydll
\n
Creates PyDLL instances.
\n\n

For accessing the C Python api directly, a ready-to-use Python shared library\nobject is available:

\n
\n
\nctypes.pythonapi
\n
An instance of PyDLL that exposes Python C API functions as\nattributes. Note that all these functions are assumed to return C\nint, which is of course not always the truth, so you have to assign\nthe correct restype attribute to use these functions.
\n\n
\n
\n

15.17.2.3. Foreign functions

\n

As explained in the previous section, foreign functions can be accessed as\nattributes of loaded shared libraries. The function objects created in this way\nby default accept any number of arguments, accept any ctypes data instances as\narguments, and return the default result type specified by the library loader.\nThey are instances of a private class:

\n
\n
\nclass ctypes._FuncPtr
\n

Base class for C callable foreign functions.

\n

Instances of foreign functions are also C compatible data types; they\nrepresent C function pointers.

\n

This behavior can be customized by assigning to special attributes of the\nforeign function object.

\n
\n
\nrestype
\n

Assign a ctypes type to specify the result type of the foreign function.\nUse None for void, a function not returning anything.

\n

It is possible to assign a callable Python object that is not a ctypes\ntype, in this case the function is assumed to return a C int, and\nthe callable will be called with this integer, allowing to do further\nprocessing or error checking. Using this is deprecated, for more flexible\npost processing or error checking use a ctypes data type as\nrestype and assign a callable to the errcheck attribute.

\n
\n\n
\n
\nargtypes
\n

Assign a tuple of ctypes types to specify the argument types that the\nfunction accepts. Functions using the stdcall calling convention can\nonly be called with the same number of arguments as the length of this\ntuple; functions using the C calling convention accept additional,\nunspecified arguments as well.

\n

When a foreign function is called, each actual argument is passed to the\nfrom_param() class method of the items in the argtypes\ntuple, this method allows to adapt the actual argument to an object that\nthe foreign function accepts. For example, a c_char_p item in\nthe argtypes tuple will convert a unicode string passed as\nargument into an byte string using ctypes conversion rules.

\n

New: It is now possible to put items in argtypes which are not ctypes\ntypes, but each item must have a from_param() method which returns a\nvalue usable as argument (integer, string, ctypes instance). This allows\nto define adapters that can adapt custom objects as function parameters.

\n
\n\n
\n
\nerrcheck
\n

Assign a Python function or another callable to this attribute. The\ncallable will be called with three or more arguments:

\n
\n
\ncallable(result, func, arguments)
\n

result is what the foreign function returns, as specified by the\nrestype attribute.

\n

func is the foreign function object itself, this allows to reuse the\nsame callable object to check or post process the results of several\nfunctions.

\n

arguments is a tuple containing the parameters originally passed to\nthe function call, this allows to specialize the behavior on the\narguments used.

\n
\n\n

The object that this function returns will be returned from the\nforeign function call, but it can also check the result value\nand raise an exception if the foreign function call failed.

\n
\n\n
\n\n
\n
\nexception ctypes.ArgumentError
\n
This exception is raised when a foreign function call cannot convert one of the\npassed arguments.
\n\n
\n
\n

15.17.2.4. Function prototypes

\n

Foreign functions can also be created by instantiating function prototypes.\nFunction prototypes are similar to function prototypes in C; they describe a\nfunction (return type, argument types, calling convention) without defining an\nimplementation. The factory functions must be called with the desired result\ntype and the argument types of the function.

\n
\n
\nctypes.CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)
\n

The returned function prototype creates functions that use the standard C\ncalling convention. The function will release the GIL during the call. If\nuse_errno is set to True, the ctypes private copy of the system\nerrno variable is exchanged with the real errno value before\nand after the call; use_last_error does the same for the Windows error\ncode.

\n

\nChanged in version 2.6: The optional use_errno and use_last_error parameters were added.

\n
\n\n
\n
\nctypes.WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)
\n
Windows only: The returned function prototype creates functions that use the\nstdcall calling convention, except on Windows CE where\nWINFUNCTYPE() is the same as CFUNCTYPE(). The function will\nrelease the GIL during the call. use_errno and use_last_error have the\nsame meaning as above.
\n\n
\n
\nctypes.PYFUNCTYPE(restype, *argtypes)
\n
The returned function prototype creates functions that use the Python calling\nconvention. The function will not release the GIL during the call.
\n\n

Function prototypes created by these factory functions can be instantiated in\ndifferent ways, depending on the type and number of the parameters in the call:

\n
\n
\n
\nprototype(address)
\n
Returns a foreign function at the specified address which must be an integer.
\n\n
\n
\nprototype(callable)
\n
Create a C callable function (a callback function) from a Python callable.
\n\n
\n
\nprototype(func_spec[, paramflags])
\n
Returns a foreign function exported by a shared library. func_spec must be a\n2-tuple (name_or_ordinal, library). The first item is the name of the\nexported function as string, or the ordinal of the exported function as small\ninteger. The second item is the shared library instance.
\n\n
\n
\nprototype(vtbl_index, name[, paramflags[, iid]])
\n

Returns a foreign function that will call a COM method. vtbl_index is the\nindex into the virtual function table, a small non-negative integer. name is\nname of the COM method. iid is an optional pointer to the interface identifier\nwhich is used in extended error reporting.

\n

COM methods use a special calling convention: They require a pointer to the COM\ninterface as first argument, in addition to those parameters that are specified\nin the argtypes tuple.

\n
\n\n

The optional paramflags parameter creates foreign function wrappers with much\nmore functionality than the features described above.

\n

paramflags must be a tuple of the same length as argtypes.

\n

Each item in this tuple contains further information about a parameter, it must\nbe a tuple containing one, two, or three items.

\n

The first item is an integer containing a combination of direction\nflags for the parameter:

\n
\n
\n
1
\n
Specifies an input parameter to the function.
\n
2
\n
Output parameter. The foreign function fills in a value.
\n
4
\n
Input parameter which defaults to the integer zero.
\n
\n
\n

The optional second item is the parameter name as string. If this is specified,\nthe foreign function can be called with named parameters.

\n

The optional third item is the default value for this parameter.

\n
\n

This example demonstrates how to wrap the Windows MessageBoxA function so\nthat it supports default parameters and named arguments. The C declaration from\nthe windows header file is this:

\n
WINUSERAPI int WINAPI\nMessageBoxA(\n    HWND hWnd ,\n    LPCSTR lpText,\n    LPCSTR lpCaption,\n    UINT uType);
\n
\n

Here is the wrapping with ctypes:

\n
>>> from ctypes import c_int, WINFUNCTYPE, windll\n>>> from ctypes.wintypes import HWND, LPCSTR, UINT\n>>> prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)\n>>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)\n>>> MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)\n>>>\n
\n
\n

The MessageBox foreign function can now be called in these ways:

\n
>>> MessageBox()\n>>> MessageBox(text="Spam, spam, spam")\n>>> MessageBox(flags=2, text="foo bar")\n>>>\n
\n
\n

A second example demonstrates output parameters. The win32 GetWindowRect\nfunction retrieves the dimensions of a specified window by copying them into\nRECT structure that the caller has to supply. Here is the C declaration:

\n
WINUSERAPI BOOL WINAPI\nGetWindowRect(\n     HWND hWnd,\n     LPRECT lpRect);
\n
\n

Here is the wrapping with ctypes:

\n
>>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError\n>>> from ctypes.wintypes import BOOL, HWND, RECT\n>>> prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))\n>>> paramflags = (1, "hwnd"), (2, "lprect")\n>>> GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)\n>>>\n
\n
\n

Functions with output parameters will automatically return the output parameter\nvalue if there is a single one, or a tuple containing the output parameter\nvalues when there are more than one, so the GetWindowRect function now returns a\nRECT instance, when called.

\n

Output parameters can be combined with the errcheck protocol to do\nfurther output processing and error checking. The win32 GetWindowRect api\nfunction returns a BOOL to signal success or failure, so this function could\ndo the error checking, and raises an exception when the api call failed:

\n
>>> def errcheck(result, func, args):\n...     if not result:\n...         raise WinError()\n...     return args\n...\n>>> GetWindowRect.errcheck = errcheck\n>>>\n
\n
\n

If the errcheck function returns the argument tuple it receives\nunchanged, ctypes continues the normal processing it does on the output\nparameters. If you want to return a tuple of window coordinates instead of a\nRECT instance, you can retrieve the fields in the function and return them\ninstead, the normal processing will no longer take place:

\n
>>> def errcheck(result, func, args):\n...     if not result:\n...         raise WinError()\n...     rc = args[1]\n...     return rc.left, rc.top, rc.bottom, rc.right\n...\n>>> GetWindowRect.errcheck = errcheck\n>>>\n
\n
\n
\n
\n

15.17.2.5. Utility functions

\n
\n
\nctypes.addressof(obj)
\n
Returns the address of the memory buffer as integer. obj must be an\ninstance of a ctypes type.
\n\n
\n
\nctypes.alignment(obj_or_type)
\n
Returns the alignment requirements of a ctypes type. obj_or_type must be a\nctypes type or instance.
\n\n
\n
\nctypes.byref(obj[, offset])
\n

Returns a light-weight pointer to obj, which must be an instance of a\nctypes type. offset defaults to zero, and must be an integer that will be\nadded to the internal pointer value.

\n

byref(obj, offset) corresponds to this C code:

\n
(((char *)&obj) + offset)
\n
\n

The returned object can only be used as a foreign function call\nparameter. It behaves similar to pointer(obj), but the\nconstruction is a lot faster.

\n

\nNew in version 2.6: The offset optional argument was added.

\n
\n\n
\n
\nctypes.cast(obj, type)
\n
This function is similar to the cast operator in C. It returns a new\ninstance of type which points to the same memory block as obj. type\nmust be a pointer type, and obj must be an object that can be interpreted\nas a pointer.
\n\n
\n
\nctypes.create_string_buffer(init_or_size[, size])
\n

This function creates a mutable character buffer. The returned object is a\nctypes array of c_char.

\n

init_or_size must be an integer which specifies the size of the array, or a\nstring which will be used to initialize the array items.

\n

If a string is specified as first argument, the buffer is made one item larger\nthan the length of the string so that the last element in the array is a NUL\ntermination character. An integer can be passed as second argument which allows\nto specify the size of the array if the length of the string should not be used.

\n

If the first parameter is a unicode string, it is converted into an 8-bit string\naccording to ctypes conversion rules.

\n
\n\n
\n
\nctypes.create_unicode_buffer(init_or_size[, size])
\n

This function creates a mutable unicode character buffer. The returned object is\na ctypes array of c_wchar.

\n

init_or_size must be an integer which specifies the size of the array, or a\nunicode string which will be used to initialize the array items.

\n

If a unicode string is specified as first argument, the buffer is made one item\nlarger than the length of the string so that the last element in the array is a\nNUL termination character. An integer can be passed as second argument which\nallows to specify the size of the array if the length of the string should not\nbe used.

\n

If the first parameter is a 8-bit string, it is converted into an unicode string\naccording to ctypes conversion rules.

\n
\n\n
\n
\nctypes.DllCanUnloadNow()
\n
Windows only: This function is a hook which allows to implement in-process\nCOM servers with ctypes. It is called from the DllCanUnloadNow function that\nthe _ctypes extension dll exports.
\n\n
\n
\nctypes.DllGetClassObject()
\n
Windows only: This function is a hook which allows to implement in-process\nCOM servers with ctypes. It is called from the DllGetClassObject function\nthat the _ctypes extension dll exports.
\n\n
\n
\nctypes.util.find_library(name)
\n

Try to find a library and return a pathname. name is the library name\nwithout any prefix like lib, suffix like .so, .dylib or version\nnumber (this is the form used for the posix linker option -l). If\nno library can be found, returns None.

\n

The exact functionality is system dependent.

\n

\nChanged in version 2.6: Windows only: find_library("m") or find_library("c") return the\nresult of a call to find_msvcrt().

\n
\n\n
\n
\nctypes.util.find_msvcrt()
\n

Windows only: return the filename of the VC runtype library used by Python,\nand by the extension modules. If the name of the library cannot be\ndetermined, None is returned.

\n

If you need to free memory, for example, allocated by an extension module\nwith a call to the free(void *), it is important that you use the\nfunction in the same library that allocated the memory.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nctypes.FormatError([code])
\n
Windows only: Returns a textual description of the error code code. If no\nerror code is specified, the last error code is used by calling the Windows\napi function GetLastError.
\n\n
\n
\nctypes.GetLastError()
\n
Windows only: Returns the last error code set by Windows in the calling thread.\nThis function calls the Windows GetLastError() function directly,\nit does not return the ctypes-private copy of the error code.
\n\n
\n
\nctypes.get_errno()
\n

Returns the current value of the ctypes-private copy of the system\nerrno variable in the calling thread.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nctypes.get_last_error()
\n

Windows only: returns the current value of the ctypes-private copy of the system\nLastError variable in the calling thread.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nctypes.memmove(dst, src, count)
\n
Same as the standard C memmove library function: copies count bytes from\nsrc to dst. dst and src must be integers or ctypes instances that can\nbe converted to pointers.
\n\n
\n
\nctypes.memset(dst, c, count)
\n
Same as the standard C memset library function: fills the memory block at\naddress dst with count bytes of value c. dst must be an integer\nspecifying an address, or a ctypes instance.
\n\n
\n
\nctypes.POINTER(type)
\n
This factory function creates and returns a new ctypes pointer type. Pointer\ntypes are cached an reused internally, so calling this function repeatedly is\ncheap. type must be a ctypes type.
\n\n
\n
\nctypes.pointer(obj)
\n

This function creates a new pointer instance, pointing to obj. The returned\nobject is of the type POINTER(type(obj)).

\n

Note: If you just want to pass a pointer to an object to a foreign function\ncall, you should use byref(obj) which is much faster.

\n
\n\n
\n
\nctypes.resize(obj, size)
\n
This function resizes the internal memory buffer of obj, which must be an\ninstance of a ctypes type. It is not possible to make the buffer smaller\nthan the native size of the objects type, as given by sizeof(type(obj)),\nbut it is possible to enlarge the buffer.
\n\n
\n
\nctypes.set_conversion_mode(encoding, errors)
\n

This function sets the rules that ctypes objects use when converting between\n8-bit strings and unicode strings. encoding must be a string specifying an\nencoding, like 'utf-8' or 'mbcs', errors must be a string\nspecifying the error handling on encoding/decoding errors. Examples of\npossible values are "strict", "replace", or "ignore".

\n

set_conversion_mode() returns a 2-tuple containing the previous\nconversion rules. On windows, the initial conversion rules are ('mbcs',\n'ignore'), on other systems ('ascii', 'strict').

\n
\n\n
\n
\nctypes.set_errno(value)
\n

Set the current value of the ctypes-private copy of the system errno\nvariable in the calling thread to value and return the previous value.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nctypes.set_last_error(value)
\n

Windows only: set the current value of the ctypes-private copy of the system\nLastError variable in the calling thread to value and return the\nprevious value.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nctypes.sizeof(obj_or_type)
\n
Returns the size in bytes of a ctypes type or instance memory buffer. Does the\nsame as the C sizeof() function.
\n\n
\n
\nctypes.string_at(address[, size])
\n
This function returns the string starting at memory address address. If size\nis specified, it is used as size, otherwise the string is assumed to be\nzero-terminated.
\n\n
\n
\nctypes.WinError(code=None, descr=None)
\n
Windows only: this function is probably the worst-named thing in ctypes. It\ncreates an instance of WindowsError. If code is not specified,\nGetLastError is called to determine the error code. If descr is not\nspecified, FormatError() is called to get a textual description of the\nerror.
\n\n
\n
\nctypes.wstring_at(address[, size])
\n
This function returns the wide character string starting at memory address\naddress as unicode string. If size is specified, it is used as the\nnumber of characters of the string, otherwise the string is assumed to be\nzero-terminated.
\n\n
\n
\n

15.17.2.6. Data types

\n
\n
\nclass ctypes._CData
\n

This non-public class is the common base class of all ctypes data types.\nAmong other things, all ctypes type instances contain a memory block that\nhold C compatible data; the address of the memory block is returned by the\naddressof() helper function. Another instance variable is exposed as\n_objects; this contains other Python objects that need to be kept\nalive in case the memory block contains pointers.

\n

Common methods of ctypes data types, these are all class methods (to be\nexact, they are methods of the metaclass):

\n
\n
\nfrom_buffer(source[, offset])
\n

This method returns a ctypes instance that shares the buffer of the\nsource object. The source object must support the writeable buffer\ninterface. The optional offset parameter specifies an offset into the\nsource buffer in bytes; the default is zero. If the source buffer is not\nlarge enough a ValueError is raised.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nfrom_buffer_copy(source[, offset])
\n

This method creates a ctypes instance, copying the buffer from the\nsource object buffer which must be readable. The optional offset\nparameter specifies an offset into the source buffer in bytes; the default\nis zero. If the source buffer is not large enough a ValueError is\nraised.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nfrom_address(address)
\n
This method returns a ctypes type instance using the memory specified by\naddress which must be an integer.
\n\n
\n
\nfrom_param(obj)
\n

This method adapts obj to a ctypes type. It is called with the actual\nobject used in a foreign function call when the type is present in the\nforeign function’s argtypes tuple; it must return an object that\ncan be used as a function call parameter.

\n

All ctypes data types have a default implementation of this classmethod\nthat normally returns obj if that is an instance of the type. Some\ntypes accept other objects as well.

\n
\n\n
\n
\nin_dll(library, name)
\n
This method returns a ctypes type instance exported by a shared\nlibrary. name is the name of the symbol that exports the data, library\nis the loaded shared library.
\n\n

Common instance variables of ctypes data types:

\n
\n
\n_b_base_
\n
Sometimes ctypes data instances do not own the memory block they contain,\ninstead they share part of the memory block of a base object. The\n_b_base_ read-only member is the root ctypes object that owns the\nmemory block.
\n\n
\n
\n_b_needsfree_
\n
This read-only variable is true when the ctypes data instance has\nallocated the memory block itself, false otherwise.
\n\n
\n
\n_objects
\n
This member is either None or a dictionary containing Python objects\nthat need to be kept alive so that the memory block contents is kept\nvalid. This object is only exposed for debugging; never modify the\ncontents of this dictionary.
\n\n
\n\n
\n
\n

15.17.2.7. Fundamental data types

\n
\n
\nclass ctypes._SimpleCData
\n

This non-public class is the base class of all fundamental ctypes data\ntypes. It is mentioned here because it contains the common attributes of the\nfundamental ctypes data types. _SimpleCData is a subclass of\n_CData, so it inherits their methods and attributes.

\n

\nChanged in version 2.6: ctypes data types that are not and do not contain pointers can now be\npickled.

\n

Instances have a single attribute:

\n
\n
\nvalue
\n

This attribute contains the actual value of the instance. For integer and\npointer types, it is an integer, for character types, it is a single\ncharacter string, for character pointer types it is a Python string or\nunicode string.

\n

When the value attribute is retrieved from a ctypes instance, usually\na new object is returned each time. ctypes does not implement\noriginal object return, always a new object is constructed. The same is\ntrue for all other ctypes object instances.

\n
\n\n
\n\n

Fundamental data types, when returned as foreign function call results, or, for\nexample, by retrieving structure field members or array items, are transparently\nconverted to native Python types. In other words, if a foreign function has a\nrestype of c_char_p, you will always receive a Python string,\nnot a c_char_p instance.

\n

Subclasses of fundamental data types do not inherit this behavior. So, if a\nforeign functions restype is a subclass of c_void_p, you will\nreceive an instance of this subclass from the function call. Of course, you can\nget the value of the pointer by accessing the value attribute.

\n

These are the fundamental ctypes data types:

\n
\n
\nclass ctypes.c_byte
\n
Represents the C signed char datatype, and interprets the value as\nsmall integer. The constructor accepts an optional integer initializer; no\noverflow checking is done.
\n\n
\n
\nclass ctypes.c_char
\n
Represents the C char datatype, and interprets the value as a single\ncharacter. The constructor accepts an optional string initializer, the\nlength of the string must be exactly one character.
\n\n
\n
\nclass ctypes.c_char_p
\n
Represents the C char * datatype when it points to a zero-terminated\nstring. For a general character pointer that may also point to binary data,\nPOINTER(c_char) must be used. The constructor accepts an integer\naddress, or a string.
\n\n
\n
\nclass ctypes.c_double
\n
Represents the C double datatype. The constructor accepts an\noptional float initializer.
\n\n
\n
\nclass ctypes.c_longdouble
\n

Represents the C long double datatype. The constructor accepts an\noptional float initializer. On platforms where sizeof(long double) ==\nsizeof(double) it is an alias to c_double.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nclass ctypes.c_float
\n
Represents the C float datatype. The constructor accepts an\noptional float initializer.
\n\n
\n
\nclass ctypes.c_int
\n
Represents the C signed int datatype. The constructor accepts an\noptional integer initializer; no overflow checking is done. On platforms\nwhere sizeof(int) == sizeof(long) it is an alias to c_long.
\n\n
\n
\nclass ctypes.c_int8
\n
Represents the C 8-bit signed int datatype. Usually an alias for\nc_byte.
\n\n
\n
\nclass ctypes.c_int16
\n
Represents the C 16-bit signed int datatype. Usually an alias for\nc_short.
\n\n
\n
\nclass ctypes.c_int32
\n
Represents the C 32-bit signed int datatype. Usually an alias for\nc_int.
\n\n
\n
\nclass ctypes.c_int64
\n
Represents the C 64-bit signed int datatype. Usually an alias for\nc_longlong.
\n\n
\n
\nclass ctypes.c_long
\n
Represents the C signed long datatype. The constructor accepts an\noptional integer initializer; no overflow checking is done.
\n\n
\n
\nclass ctypes.c_longlong
\n
Represents the C signed long long datatype. The constructor accepts\nan optional integer initializer; no overflow checking is done.
\n\n
\n
\nclass ctypes.c_short
\n
Represents the C signed short datatype. The constructor accepts an\noptional integer initializer; no overflow checking is done.
\n\n
\n
\nclass ctypes.c_size_t
\n
Represents the C size_t datatype.
\n\n
\n
\nclass ctypes.c_ssize_t
\n

Represents the C ssize_t datatype.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nclass ctypes.c_ubyte
\n
Represents the C unsigned char datatype, it interprets the value as\nsmall integer. The constructor accepts an optional integer initializer; no\noverflow checking is done.
\n\n
\n
\nclass ctypes.c_uint
\n
Represents the C unsigned int datatype. The constructor accepts an\noptional integer initializer; no overflow checking is done. On platforms\nwhere sizeof(int) == sizeof(long) it is an alias for c_ulong.
\n\n
\n
\nclass ctypes.c_uint8
\n
Represents the C 8-bit unsigned int datatype. Usually an alias for\nc_ubyte.
\n\n
\n
\nclass ctypes.c_uint16
\n
Represents the C 16-bit unsigned int datatype. Usually an alias for\nc_ushort.
\n\n
\n
\nclass ctypes.c_uint32
\n
Represents the C 32-bit unsigned int datatype. Usually an alias for\nc_uint.
\n\n
\n
\nclass ctypes.c_uint64
\n
Represents the C 64-bit unsigned int datatype. Usually an alias for\nc_ulonglong.
\n\n
\n
\nclass ctypes.c_ulong
\n
Represents the C unsigned long datatype. The constructor accepts an\noptional integer initializer; no overflow checking is done.
\n\n
\n
\nclass ctypes.c_ulonglong
\n
Represents the C unsigned long long datatype. The constructor\naccepts an optional integer initializer; no overflow checking is done.
\n\n
\n
\nclass ctypes.c_ushort
\n
Represents the C unsigned short datatype. The constructor accepts\nan optional integer initializer; no overflow checking is done.
\n\n
\n
\nclass ctypes.c_void_p
\n
Represents the C void * type. The value is represented as integer.\nThe constructor accepts an optional integer initializer.
\n\n
\n
\nclass ctypes.c_wchar
\n
Represents the C wchar_t datatype, and interprets the value as a\nsingle character unicode string. The constructor accepts an optional string\ninitializer, the length of the string must be exactly one character.
\n\n
\n
\nclass ctypes.c_wchar_p
\n
Represents the C wchar_t * datatype, which must be a pointer to a\nzero-terminated wide character string. The constructor accepts an integer\naddress, or a string.
\n\n
\n
\nclass ctypes.c_bool
\n

Represent the C bool datatype (more accurately, _Bool from\nC99). Its value can be True or False, and the constructor accepts any object\nthat has a truth value.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nclass ctypes.HRESULT
\n
Windows only: Represents a HRESULT value, which contains success or\nerror information for a function or method call.
\n\n
\n
\nclass ctypes.py_object
\n
Represents the C PyObject * datatype. Calling this without an\nargument creates a NULL PyObject * pointer.
\n\n

The ctypes.wintypes module provides quite some other Windows specific\ndata types, for example HWND, WPARAM, or DWORD. Some\nuseful structures like MSG or RECT are also defined.

\n
\n
\n

15.17.2.8. Structured data types

\n
\n
\nclass ctypes.Union(*args, **kw)
\n
Abstract base class for unions in native byte order.
\n\n
\n
\nclass ctypes.BigEndianStructure(*args, **kw)
\n
Abstract base class for structures in big endian byte order.
\n\n
\n
\nclass ctypes.LittleEndianStructure(*args, **kw)
\n
Abstract base class for structures in little endian byte order.
\n\n

Structures with non-native byte order cannot contain pointer type fields, or any\nother data types containing pointer type fields.

\n
\n
\nclass ctypes.Structure(*args, **kw)
\n

Abstract base class for structures in native byte order.

\n

Concrete structure and union types must be created by subclassing one of these\ntypes, and at least define a _fields_ class variable. ctypes will\ncreate descriptors which allow reading and writing the fields by direct\nattribute accesses. These are the

\n
\n
\n_fields_
\n

A sequence defining the structure fields. The items must be 2-tuples or\n3-tuples. The first item is the name of the field, the second item\nspecifies the type of the field; it can be any ctypes data type.

\n

For integer type fields like c_int, a third optional item can be\ngiven. It must be a small positive integer defining the bit width of the\nfield.

\n

Field names must be unique within one structure or union. This is not\nchecked, only one field can be accessed when names are repeated.

\n

It is possible to define the _fields_ class variable after the\nclass statement that defines the Structure subclass, this allows to create\ndata types that directly or indirectly reference themselves:

\n
class List(Structure):\n    pass\nList._fields_ = [("pnext", POINTER(List)),\n                 ...\n                ]\n
\n
\n

The _fields_ class variable must, however, be defined before the\ntype is first used (an instance is created, sizeof() is called on it,\nand so on). Later assignments to the _fields_ class variable will\nraise an AttributeError.

\n

Structure and union subclass constructors accept both positional and named\narguments. Positional arguments are used to initialize the fields in the\nsame order as they appear in the _fields_ definition, named\narguments are used to initialize the fields with the corresponding name.

\n

It is possible to defined sub-subclasses of structure types, they inherit\nthe fields of the base class plus the _fields_ defined in the\nsub-subclass, if any.

\n
\n\n
\n
\n_pack_
\n
An optional small integer that allows to override the alignment of\nstructure fields in the instance. _pack_ must already be defined\nwhen _fields_ is assigned, otherwise it will have no effect.
\n\n
\n
\n_anonymous_
\n

An optional sequence that lists the names of unnamed (anonymous) fields.\n_anonymous_ must be already defined when _fields_ is\nassigned, otherwise it will have no effect.

\n

The fields listed in this variable must be structure or union type fields.\nctypes will create descriptors in the structure type that allows to\naccess the nested fields directly, without the need to create the\nstructure or union field.

\n

Here is an example type (Windows):

\n
class _U(Union):\n    _fields_ = [("lptdesc", POINTER(TYPEDESC)),\n                ("lpadesc", POINTER(ARRAYDESC)),\n                ("hreftype", HREFTYPE)]\n\nclass TYPEDESC(Structure):\n    _anonymous_ = ("u",)\n    _fields_ = [("u", _U),\n                ("vt", VARTYPE)]\n
\n
\n

The TYPEDESC structure describes a COM data type, the vt field\nspecifies which one of the union fields is valid. Since the u field\nis defined as anonymous field, it is now possible to access the members\ndirectly off the TYPEDESC instance. td.lptdesc and td.u.lptdesc\nare equivalent, but the former is faster since it does not need to create\na temporary union instance:

\n
td = TYPEDESC()\ntd.vt = VT_PTR\ntd.lptdesc = POINTER(some_type)\ntd.u.lptdesc = POINTER(some_type)\n
\n
\n
\n\n

It is possible to defined sub-subclasses of structures, they inherit the\nfields of the base class. If the subclass definition has a separate\n_fields_ variable, the fields specified in this are appended to the\nfields of the base class.

\n

Structure and union constructors accept both positional and keyword\narguments. Positional arguments are used to initialize member fields in the\nsame order as they are appear in _fields_. Keyword arguments in the\nconstructor are interpreted as attribute assignments, so they will initialize\n_fields_ with the same name, or create new attributes for names not\npresent in _fields_.

\n
\n\n
\n
\n

15.17.2.9. Arrays and pointers

\n

Not yet written - please see the sections Pointers and section\nArrays in the tutorial.

\n
\n
\n
", "searchableItems": [ { "name": "ctypes._CData", "domId": "ctypes_ctypes._CData" }, { "name": "ctypes._CData.from_address", "domId": "ctypes_ctypes._CData.from_address" }, { "name": "ctypes._CData.from_buffer", "domId": "ctypes_ctypes._CData.from_buffer" }, { "name": "ctypes._CData.from_buffer_copy", "domId": "ctypes_ctypes._CData.from_buffer_copy" }, { "name": "ctypes._CData.from_param", "domId": "ctypes_ctypes._CData.from_param" }, { "name": "ctypes._CData.in_dll", "domId": "ctypes_ctypes._CData.in_dll" }, { "name": "ctypes._FuncPtr", "domId": "ctypes_ctypes._FuncPtr" }, { "name": "ctypes._SimpleCData", "domId": "ctypes_ctypes._SimpleCData" }, { "name": "ctypes.addressof", "domId": "ctypes_ctypes.addressof" }, { "name": "ctypes.alignment", "domId": "ctypes_ctypes.alignment" }, { "name": "ctypes.BigEndianStructure", "domId": "ctypes_ctypes.BigEndianStructure" }, { "name": "ctypes.byref", "domId": "ctypes_ctypes.byref" }, { "name": "ctypes.c_bool", "domId": "ctypes_ctypes.c_bool" }, { "name": "ctypes.c_byte", "domId": "ctypes_ctypes.c_byte" }, { "name": "ctypes.c_char", "domId": "ctypes_ctypes.c_char" }, { "name": "ctypes.c_char_p", "domId": "ctypes_ctypes.c_char_p" }, { "name": "ctypes.c_double", "domId": "ctypes_ctypes.c_double" }, { "name": "ctypes.c_float", "domId": "ctypes_ctypes.c_float" }, { "name": "ctypes.c_int", "domId": "ctypes_ctypes.c_int" }, { "name": "ctypes.c_int16", "domId": "ctypes_ctypes.c_int16" }, { "name": "ctypes.c_int32", "domId": "ctypes_ctypes.c_int32" }, { "name": "ctypes.c_int64", "domId": "ctypes_ctypes.c_int64" }, { "name": "ctypes.c_int8", "domId": "ctypes_ctypes.c_int8" }, { "name": "ctypes.c_long", "domId": "ctypes_ctypes.c_long" }, { "name": "ctypes.c_longdouble", "domId": "ctypes_ctypes.c_longdouble" }, { "name": "ctypes.c_longlong", "domId": "ctypes_ctypes.c_longlong" }, { "name": "ctypes.c_short", "domId": "ctypes_ctypes.c_short" }, { "name": "ctypes.c_size_t", "domId": "ctypes_ctypes.c_size_t" }, { "name": "ctypes.c_ssize_t", "domId": "ctypes_ctypes.c_ssize_t" }, { "name": "ctypes.c_ubyte", "domId": "ctypes_ctypes.c_ubyte" }, { "name": "ctypes.c_uint", "domId": "ctypes_ctypes.c_uint" }, { "name": "ctypes.c_uint16", "domId": "ctypes_ctypes.c_uint16" }, { "name": "ctypes.c_uint32", "domId": "ctypes_ctypes.c_uint32" }, { "name": "ctypes.c_uint64", "domId": "ctypes_ctypes.c_uint64" }, { "name": "ctypes.c_uint8", "domId": "ctypes_ctypes.c_uint8" }, { "name": "ctypes.c_ulong", "domId": "ctypes_ctypes.c_ulong" }, { "name": "ctypes.c_ulonglong", "domId": "ctypes_ctypes.c_ulonglong" }, { "name": "ctypes.c_ushort", "domId": "ctypes_ctypes.c_ushort" }, { "name": "ctypes.c_void_p", "domId": "ctypes_ctypes.c_void_p" }, { "name": "ctypes.c_wchar", "domId": "ctypes_ctypes.c_wchar" }, { "name": "ctypes.c_wchar_p", "domId": "ctypes_ctypes.c_wchar_p" }, { "name": "ctypes.cast", "domId": "ctypes_ctypes.cast" }, { "name": "ctypes.CDLL", "domId": "ctypes_ctypes.CDLL" }, { "name": "ctypes.CFUNCTYPE", "domId": "ctypes_ctypes.CFUNCTYPE" }, { "name": "ctypes.create_string_buffer", "domId": "ctypes_ctypes.create_string_buffer" }, { "name": "ctypes.create_unicode_buffer", "domId": "ctypes_ctypes.create_unicode_buffer" }, { "name": "ctypes.DllCanUnloadNow", "domId": "ctypes_ctypes.DllCanUnloadNow" }, { "name": "ctypes.DllGetClassObject", "domId": "ctypes_ctypes.DllGetClassObject" }, { "name": "ctypes.FormatError", "domId": "ctypes_ctypes.FormatError" }, { "name": "ctypes.get_errno", "domId": "ctypes_ctypes.get_errno" }, { "name": "ctypes.get_last_error", "domId": "ctypes_ctypes.get_last_error" }, { "name": "ctypes.GetLastError", "domId": "ctypes_ctypes.GetLastError" }, { "name": "ctypes.HRESULT", "domId": "ctypes_ctypes.HRESULT" }, { "name": "ctypes.LibraryLoader", "domId": "ctypes_ctypes.LibraryLoader" }, { "name": "ctypes.LibraryLoader.LoadLibrary", "domId": "ctypes_ctypes.LibraryLoader.LoadLibrary" }, { "name": "ctypes.LittleEndianStructure", "domId": "ctypes_ctypes.LittleEndianStructure" }, { "name": "ctypes.memmove", "domId": "ctypes_ctypes.memmove" }, { "name": "ctypes.memset", "domId": "ctypes_ctypes.memset" }, { "name": "ctypes.OleDLL", "domId": "ctypes_ctypes.OleDLL" }, { "name": "ctypes.POINTER", "domId": "ctypes_ctypes_ctypes.POINTER" }, { "name": "ctypes.pointer", "domId": "ctypes_ctypes.pointer" }, { "name": "ctypes.POINTER", "domId": "ctypes_ctypes_ctypes.POINTER" }, { "name": "ctypes.py_object", "domId": "ctypes_ctypes.py_object" }, { "name": "ctypes.PyDLL", "domId": "ctypes_ctypes.PyDLL" }, { "name": "ctypes.PYFUNCTYPE", "domId": "ctypes_ctypes.PYFUNCTYPE" }, { "name": "ctypes.resize", "domId": "ctypes_ctypes.resize" }, { "name": "ctypes.set_conversion_mode", "domId": "ctypes_ctypes.set_conversion_mode" }, { "name": "ctypes.set_errno", "domId": "ctypes_ctypes.set_errno" }, { "name": "ctypes.set_last_error", "domId": "ctypes_ctypes.set_last_error" }, { "name": "ctypes.sizeof", "domId": "ctypes_ctypes.sizeof" }, { "name": "ctypes.string_at", "domId": "ctypes_ctypes.string_at" }, { "name": "ctypes.Structure", "domId": "ctypes_ctypes.Structure" }, { "name": "ctypes.Union", "domId": "ctypes_ctypes.Union" }, { "name": "ctypes.util.find_library", "domId": "ctypes_ctypes.util.find_library" }, { "name": "ctypes.util.find_msvcrt", "domId": "ctypes_ctypes.util.find_msvcrt" }, { "name": "ctypes.WinDLL", "domId": "ctypes_ctypes.WinDLL" }, { "name": "ctypes.WinError", "domId": "ctypes_ctypes.WinError" }, { "name": "ctypes.WINFUNCTYPE", "domId": "ctypes_ctypes.WINFUNCTYPE" }, { "name": "ctypes.wstring_at", "domId": "ctypes_ctypes.wstring_at" } ] }, { "url": "http://docs.python.org/library/readline.html", "title": "readline", "html": "
\n

16.8. readline — GNU readline interface

\n

Platforms: Unix

\n

The readline module defines a number of functions to facilitate\ncompletion and reading/writing of history files from the Python interpreter.\nThis module can be used directly or via the rlcompleter module. Settings\nmade using this module affect the behaviour of both the interpreter’s\ninteractive prompt and the prompts offered by the raw_input() and\ninput() built-in functions.

\n
\n

Note

\n

On MacOS X the readline module can be implemented using\nthe libedit library instead of GNU readline.

\n

The configuration file for libedit is different from that\nof GNU readline. If you programmatically load configuration strings\nyou can check for the text “libedit” in readline.__doc__\nto differentiate between GNU readline and libedit.

\n
\n

The readline module defines the following functions:

\n
\n
\nreadline.parse_and_bind(string)
\n
Parse and execute single line of a readline init file.
\n\n
\n
\nreadline.get_line_buffer()
\n
Return the current contents of the line buffer.
\n\n
\n
\nreadline.insert_text(string)
\n
Insert text into the command line.
\n\n
\n
\nreadline.read_init_file([filename])
\n
Parse a readline initialization file. The default filename is the last filename\nused.
\n\n
\n
\nreadline.read_history_file([filename])
\n
Load a readline history file. The default filename is ~/.history.
\n\n
\n
\nreadline.write_history_file([filename])
\n
Save a readline history file. The default filename is ~/.history.
\n\n
\n
\nreadline.clear_history()
\n

Clear the current history. (Note: this function is not available if the\ninstalled version of GNU readline doesn’t support it.)

\n

\nNew in version 2.4.

\n
\n\n
\n
\nreadline.get_history_length()
\n
Return the desired length of the history file. Negative values imply unlimited\nhistory file size.
\n\n
\n
\nreadline.set_history_length(length)
\n
Set the number of lines to save in the history file. write_history_file()\nuses this value to truncate the history file when saving. Negative values imply\nunlimited history file size.
\n\n
\n
\nreadline.get_current_history_length()
\n

Return the number of lines currently in the history. (This is different from\nget_history_length(), which returns the maximum number of lines that will\nbe written to a history file.)

\n

\nNew in version 2.3.

\n
\n\n
\n
\nreadline.get_history_item(index)
\n

Return the current contents of history item at index.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nreadline.remove_history_item(pos)
\n

Remove history item specified by its position from the history.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nreadline.replace_history_item(pos, line)
\n

Replace history item specified by its position with the given line.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nreadline.redisplay()
\n

Change what’s displayed on the screen to reflect the current contents of the\nline buffer.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nreadline.set_startup_hook([function])
\n
Set or remove the startup_hook function. If function is specified, it will be\nused as the new startup_hook function; if omitted or None, any hook function\nalready installed is removed. The startup_hook function is called with no\narguments just before readline prints the first prompt.
\n\n
\n
\nreadline.set_pre_input_hook([function])
\n
Set or remove the pre_input_hook function. If function is specified, it will\nbe used as the new pre_input_hook function; if omitted or None, any hook\nfunction already installed is removed. The pre_input_hook function is called\nwith no arguments after the first prompt has been printed and just before\nreadline starts reading input characters.
\n\n
\n
\nreadline.set_completer([function])
\n
Set or remove the completer function. If function is specified, it will be\nused as the new completer function; if omitted or None, any completer\nfunction already installed is removed. The completer function is called as\nfunction(text, state), for state in 0, 1, 2, ..., until it\nreturns a non-string value. It should return the next possible completion\nstarting with text.
\n\n
\n
\nreadline.get_completer()
\n

Get the completer function, or None if no completer function has been set.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nreadline.get_completion_type()
\n

Get the type of completion being attempted.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nreadline.get_begidx()
\n
Get the beginning index of the readline tab-completion scope.
\n\n
\n
\nreadline.get_endidx()
\n
Get the ending index of the readline tab-completion scope.
\n\n
\n
\nreadline.set_completer_delims(string)
\n
Set the readline word delimiters for tab-completion.
\n\n
\n
\nreadline.get_completer_delims()
\n
Get the readline word delimiters for tab-completion.
\n\n
\n
\nreadline.set_completion_display_matches_hook([function])
\n

Set or remove the completion display function. If function is\nspecified, it will be used as the new completion display function;\nif omitted or None, any completion display function already\ninstalled is removed. The completion display function is called as\nfunction(substitution, [matches], longest_match_length) once\neach time matches need to be displayed.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nreadline.add_history(line)
\n
Append a line to the history buffer, as if it was the last line typed.
\n\n
\n

See also

\n
\n
Module rlcompleter
\n
Completion of Python identifiers at the interactive prompt.
\n
\n
\n
\n

16.8.1. Example

\n

The following example demonstrates how to use the readline module’s\nhistory reading and writing functions to automatically load and save a history\nfile named .pyhist from the user’s home directory. The code below would\nnormally be executed automatically during interactive sessions from the user’s\nPYTHONSTARTUP file.

\n
import os\nimport readline\nhistfile = os.path.join(os.path.expanduser("~"), ".pyhist")\ntry:\n    readline.read_history_file(histfile)\nexcept IOError:\n    pass\nimport atexit\natexit.register(readline.write_history_file, histfile)\ndel os, histfile\n
\n
\n

The following example extends the code.InteractiveConsole class to\nsupport history save/restore.

\n
import code\nimport readline\nimport atexit\nimport os\n\nclass HistoryConsole(code.InteractiveConsole):\n    def __init__(self, locals=None, filename="<console>",\n                 histfile=os.path.expanduser("~/.console-history")):\n        code.InteractiveConsole.__init__(self, locals, filename)\n        self.init_history(histfile)\n\n    def init_history(self, histfile):\n        readline.parse_and_bind("tab: complete")\n        if hasattr(readline, "read_history_file"):\n            try:\n                readline.read_history_file(histfile)\n            except IOError:\n                pass\n            atexit.register(self.save_history, histfile)\n\n    def save_history(self, histfile):\n        readline.write_history_file(histfile)\n
\n
\n
\n
", "searchableItems": [ { "name": "readline.add_history", "domId": "readline_readline.add_history" }, { "name": "readline.clear_history", "domId": "readline_readline.clear_history" }, { "name": "readline.get_begidx", "domId": "readline_readline.get_begidx" }, { "name": "readline.get_completer", "domId": "readline_readline.get_completer" }, { "name": "readline.get_completer_delims", "domId": "readline_readline.get_completer_delims" }, { "name": "readline.get_completion_type", "domId": "readline_readline.get_completion_type" }, { "name": "readline.get_current_history_length", "domId": "readline_readline.get_current_history_length" }, { "name": "readline.get_endidx", "domId": "readline_readline.get_endidx" }, { "name": "readline.get_history_item", "domId": "readline_readline.get_history_item" }, { "name": "readline.get_history_length", "domId": "readline_readline.get_history_length" }, { "name": "readline.get_line_buffer", "domId": "readline_readline.get_line_buffer" }, { "name": "readline.insert_text", "domId": "readline_readline.insert_text" }, { "name": "readline.parse_and_bind", "domId": "readline_readline.parse_and_bind" }, { "name": "readline.read_history_file", "domId": "readline_readline.read_history_file" }, { "name": "readline.read_init_file", "domId": "readline_readline.read_init_file" }, { "name": "readline.redisplay", "domId": "readline_readline.redisplay" }, { "name": "readline.remove_history_item", "domId": "readline_readline.remove_history_item" }, { "name": "readline.replace_history_item", "domId": "readline_readline.replace_history_item" }, { "name": "readline.set_completer", "domId": "readline_readline.set_completer" }, { "name": "readline.set_completer_delims", "domId": "readline_readline.set_completer_delims" }, { "name": "readline.set_completion_display_matches_hook", "domId": "readline_readline.set_completion_display_matches_hook" }, { "name": "readline.set_history_length", "domId": "readline_readline.set_history_length" }, { "name": "readline.set_pre_input_hook", "domId": "readline_readline.set_pre_input_hook" }, { "name": "readline.set_startup_hook", "domId": "readline_readline.set_startup_hook" }, { "name": "readline.write_history_file", "domId": "readline_readline.write_history_file" } ] }, { "url": "http://docs.python.org/library/mmap.html", "title": "mmap", "html": "
\n

16.7. mmap — Memory-mapped file support

\n

Memory-mapped file objects behave like both strings and like file objects.\nUnlike normal string objects, however, these are mutable. You can use mmap\nobjects in most places where strings are expected; for example, you can use\nthe re module to search through a memory-mapped file. Since they’re\nmutable, you can change a single character by doing obj[index] = 'a', or\nchange a substring by assigning to a slice: obj[i1:i2] = '...'. You can\nalso read and write data starting at the current file position, and\nseek() through the file to different positions.

\n

A memory-mapped file is created by the mmap constructor, which is\ndifferent on Unix and on Windows. In either case you must provide a file\ndescriptor for a file opened for update. If you wish to map an existing Python\nfile object, use its fileno() method to obtain the correct value for the\nfileno parameter. Otherwise, you can open the file using the\nos.open() function, which returns a file descriptor directly (the file\nstill needs to be closed when done).

\n
\n

Note

\n

If you want to create a memory-mapping for a writable, buffered file, you\nshould flush() the file first. This is necessary to ensure\nthat local modifications to the buffers are actually available to the\nmapping.

\n
\n

For both the Unix and Windows versions of the constructor, access may be\nspecified as an optional keyword parameter. access accepts one of three\nvalues: ACCESS_READ, ACCESS_WRITE, or ACCESS_COPY\nto specify read-only, write-through or copy-on-write memory respectively.\naccess can be used on both Unix and Windows. If access is not specified,\nWindows mmap returns a write-through mapping. The initial memory values for\nall three access types are taken from the specified file. Assignment to an\nACCESS_READ memory map raises a TypeError exception.\nAssignment to an ACCESS_WRITE memory map affects both memory and the\nunderlying file. Assignment to an ACCESS_COPY memory map affects\nmemory but does not update the underlying file.

\n

\nChanged in version 2.5: To map anonymous memory, -1 should be passed as the fileno along with the\nlength.

\n

\nChanged in version 2.6: mmap.mmap has formerly been a factory function creating mmap objects. Now\nmmap.mmap is the class itself.

\n
\n
\nclass mmap.mmap(fileno, length[, tagname[, access[, offset]]])
\n

(Windows version) Maps length bytes from the file specified by the\nfile handle fileno, and creates a mmap object. If length is larger\nthan the current size of the file, the file is extended to contain length\nbytes. If length is 0, the maximum length of the map is the current\nsize of the file, except that if the file is empty Windows raises an\nexception (you cannot create an empty mapping on Windows).

\n

tagname, if specified and not None, is a string giving a tag name for\nthe mapping. Windows allows you to have many different mappings against\nthe same file. If you specify the name of an existing tag, that tag is\nopened, otherwise a new tag of this name is created. If this parameter is\nomitted or None, the mapping is created without a name. Avoiding the\nuse of the tag parameter will assist in keeping your code portable between\nUnix and Windows.

\n

offset may be specified as a non-negative integer offset. mmap references\nwill be relative to the offset from the beginning of the file. offset\ndefaults to 0. offset must be a multiple of the ALLOCATIONGRANULARITY.

\n
\n\n
\n
\nclass mmap.mmap(fileno, length[, flags[, prot[, access[, offset]]]])
\n

(Unix version) Maps length bytes from the file specified by the file\ndescriptor fileno, and returns a mmap object. If length is 0, the\nmaximum length of the map will be the current size of the file when\nmmap is called.

\n

flags specifies the nature of the mapping. MAP_PRIVATE creates a\nprivate copy-on-write mapping, so changes to the contents of the mmap\nobject will be private to this process, and MAP_SHARED creates a\nmapping that’s shared with all other processes mapping the same areas of\nthe file. The default value is MAP_SHARED.

\n

prot, if specified, gives the desired memory protection; the two most\nuseful values are PROT_READ and PROT_WRITE, to specify\nthat the pages may be read or written. prot defaults to\nPROT_READ | PROT_WRITE.

\n

access may be specified in lieu of flags and prot as an optional\nkeyword parameter. It is an error to specify both flags, prot and\naccess. See the description of access above for information on how to\nuse this parameter.

\n

offset may be specified as a non-negative integer offset. mmap references\nwill be relative to the offset from the beginning of the file. offset\ndefaults to 0. offset must be a multiple of the PAGESIZE or\nALLOCATIONGRANULARITY.

\n

To ensure validity of the created memory mapping the file specified\nby the descriptor fileno is internally automatically synchronized\nwith physical backing store on Mac OS X and OpenVMS.

\n

This example shows a simple way of using mmap:

\n
import mmap\n\n# write a simple example file\nwith open("hello.txt", "wb") as f:\n    f.write("Hello Python!\\n")\n\nwith open("hello.txt", "r+b") as f:\n    # memory-map the file, size 0 means whole file\n    map = mmap.mmap(f.fileno(), 0)\n    # read content via standard file methods\n    print map.readline()  # prints "Hello Python!"\n    # read content via slice notation\n    print map[:5]  # prints "Hello"\n    # update content using slice notation;\n    # note that new content must have same size\n    map[6:] = " world!\\n"\n    # ... and read again using standard file methods\n    map.seek(0)\n    print map.readline()  # prints "Hello  world!"\n    # close the map\n    map.close()\n
\n
\n

The next example demonstrates how to create an anonymous map and exchange\ndata between the parent and child processes:

\n
import mmap\nimport os\n\nmap = mmap.mmap(-1, 13)\nmap.write("Hello world!")\n\npid = os.fork()\n\nif pid == 0: # In a child process\n    map.seek(0)\n    print map.readline()\n\n    map.close()\n
\n
\n

Memory-mapped file objects support the following methods:

\n
\n
\nmmap.close()
\n
Close the file. Subsequent calls to other methods of the object will\nresult in an exception being raised.
\n\n
\n
\nmmap.find(string[, start[, end]])
\n
Returns the lowest index in the object where the substring string is\nfound, such that string is contained in the range [start, end].\nOptional arguments start and end are interpreted as in slice notation.\nReturns -1 on failure.
\n\n
\n
\nmmap.flush([offset, size])
\n

Flushes changes made to the in-memory copy of a file back to disk. Without\nuse of this call there is no guarantee that changes are written back before\nthe object is destroyed. If offset and size are specified, only\nchanges to the given range of bytes will be flushed to disk; otherwise, the\nwhole extent of the mapping is flushed.

\n

(Windows version) A nonzero value returned indicates success; zero\nindicates failure.

\n

(Unix version) A zero value is returned to indicate success. An\nexception is raised when the call failed.

\n
\n\n
\n
\nmmap.move(dest, src, count)
\n
Copy the count bytes starting at offset src to the destination index\ndest. If the mmap was created with ACCESS_READ, then calls to\nmove will raise a TypeError exception.
\n\n
\n
\nmmap.read(num)
\n
Return a string containing up to num bytes starting from the current\nfile position; the file position is updated to point after the bytes that\nwere returned.
\n\n
\n
\nmmap.read_byte()
\n
Returns a string of length 1 containing the character at the current file\nposition, and advances the file position by 1.
\n\n
\n
\nmmap.readline()
\n
Returns a single line, starting at the current file position and up to the\nnext newline.
\n\n
\n
\nmmap.resize(newsize)
\n
Resizes the map and the underlying file, if any. If the mmap was created\nwith ACCESS_READ or ACCESS_COPY, resizing the map will\nraise a TypeError exception.
\n\n
\n
\nmmap.rfind(string[, start[, end]])
\n
Returns the highest index in the object where the substring string is\nfound, such that string is contained in the range [start, end].\nOptional arguments start and end are interpreted as in slice notation.\nReturns -1 on failure.
\n\n
\n
\nmmap.seek(pos[, whence])
\n
Set the file’s current position. whence argument is optional and\ndefaults to os.SEEK_SET or 0 (absolute file positioning); other\nvalues are os.SEEK_CUR or 1 (seek relative to the current\nposition) and os.SEEK_END or 2 (seek relative to the file’s end).
\n\n
\n
\nmmap.size()
\n
Return the length of the file, which can be larger than the size of the\nmemory-mapped area.
\n\n
\n
\nmmap.tell()
\n
Returns the current position of the file pointer.
\n\n
\n
\nmmap.write(string)
\n
Write the bytes in string into memory at the current position of the\nfile pointer; the file position is updated to point after the bytes that\nwere written. If the mmap was created with ACCESS_READ, then\nwriting to it will raise a TypeError exception.
\n\n
\n
\nmmap.write_byte(byte)
\n
Write the single-character string byte into memory at the current\nposition of the file pointer; the file position is advanced by 1. If\nthe mmap was created with ACCESS_READ, then writing to it will\nraise a TypeError exception.
\n\n
\n\n
", "searchableItems": [ { "name": "mmap.close", "domId": "mmap_mmap.close" }, { "name": "mmap.find", "domId": "mmap_mmap.find" }, { "name": "mmap.flush", "domId": "mmap_mmap.flush" }, { "name": "mmap.mmap", "domId": "mmap_mmap.mmap" }, { "name": "mmap.move", "domId": "mmap_mmap.move" }, { "name": "mmap.read", "domId": "mmap_mmap.read" }, { "name": "mmap.read_byte", "domId": "mmap_mmap.read_byte" }, { "name": "mmap.readline", "domId": "mmap_mmap.readline" }, { "name": "mmap.resize", "domId": "mmap_mmap.resize" }, { "name": "mmap.rfind", "domId": "mmap_mmap.rfind" }, { "name": "mmap.seek", "domId": "mmap_mmap.seek" }, { "name": "mmap.size", "domId": "mmap_mmap.size" }, { "name": "mmap.tell", "domId": "mmap_mmap.tell" }, { "name": "mmap.write", "domId": "mmap_mmap.write" }, { "name": "mmap.write_byte", "domId": "mmap_mmap.write_byte" } ] }, { "url": "http://docs.python.org/library/threading.html", "title": "threading", "html": "
\n

16.2. threading — Higher-level threading interface

\n

Source code: Lib/threading.py

\n
\n

This module constructs higher-level threading interfaces on top of the lower\nlevel thread module.\nSee also the mutex and Queue modules.

\n

The dummy_threading module is provided for situations where\nthreading cannot be used because thread is missing.

\n
\n

Note

\n

Starting with Python 2.6, this module provides PEP 8 compliant aliases and\nproperties to replace the camelCase names that were inspired by Java’s\nthreading API. This updated API is compatible with that of the\nmultiprocessing module. However, no schedule has been set for the\ndeprecation of the camelCase names and they remain fully supported in\nboth Python 2.x and 3.x.

\n
\n
\n

Note

\n

Starting with Python 2.5, several Thread methods raise RuntimeError\ninstead of AssertionError if called erroneously.

\n
\n
\n

CPython implementation detail: Due to the Global Interpreter Lock, in CPython only one thread\ncan execute Python code at once (even though certain performance-oriented\nlibraries might overcome this limitation).\nIf you want your application to make better of use of the computational\nresources of multi-core machines, you are advised to use\nmultiprocessing. However, threading is still an appropriate model\nif you want to run multiple I/O-bound tasks simultaneously.

\n
\n

This module defines the following functions and objects:

\n
\n
\nthreading.active_count()
\n
\nthreading.activeCount()
\n
Return the number of Thread objects currently alive. The returned\ncount is equal to the length of the list returned by enumerate().
\n\n
\n
\nthreading.Condition()
\n

A factory function that returns a new condition variable object. A condition\nvariable allows one or more threads to wait until they are notified by another\nthread.

\n

See Condition Objects.

\n
\n\n
\n
\nthreading.current_thread()
\n
\nthreading.currentThread()
\n
Return the current Thread object, corresponding to the caller’s thread\nof control. If the caller’s thread of control was not created through the\nthreading module, a dummy thread object with limited functionality is\nreturned.
\n\n
\n
\nthreading.enumerate()
\n
Return a list of all Thread objects currently alive. The list\nincludes daemonic threads, dummy thread objects created by\ncurrent_thread(), and the main thread. It excludes terminated threads\nand threads that have not yet been started.
\n\n
\n
\nthreading.Event()
\n

A factory function that returns a new event object. An event manages a flag\nthat can be set to true with the set() method and reset to false\nwith the clear() method. The wait() method blocks until the flag\nis true.

\n

See Event Objects.

\n
\n\n
\n
\nclass threading.local
\n

A class that represents thread-local data. Thread-local data are data whose\nvalues are thread specific. To manage thread-local data, just create an\ninstance of local (or a subclass) and store attributes on it:

\n
mydata = threading.local()\nmydata.x = 1\n
\n
\n

The instance’s values will be different for separate threads.

\n

For more details and extensive examples, see the documentation string of the\n_threading_local module.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nthreading.Lock()
\n

A factory function that returns a new primitive lock object. Once a thread has\nacquired it, subsequent attempts to acquire it block, until it is released; any\nthread may release it.

\n

See Lock Objects.

\n
\n\n
\n
\nthreading.RLock()
\n

A factory function that returns a new reentrant lock object. A reentrant lock\nmust be released by the thread that acquired it. Once a thread has acquired a\nreentrant lock, the same thread may acquire it again without blocking; the\nthread must release it once for each time it has acquired it.

\n

See RLock Objects.

\n
\n\n
\n
\nthreading.Semaphore([value])
\n

A factory function that returns a new semaphore object. A semaphore manages a\ncounter representing the number of release() calls minus the number of\nacquire() calls, plus an initial value. The acquire() method blocks\nif necessary until it can return without making the counter negative. If not\ngiven, value defaults to 1.

\n

See Semaphore Objects.

\n
\n\n
\n
\nthreading.BoundedSemaphore([value])
\n
A factory function that returns a new bounded semaphore object. A bounded\nsemaphore checks to make sure its current value doesn’t exceed its initial\nvalue. If it does, ValueError is raised. In most situations semaphores\nare used to guard resources with limited capacity. If the semaphore is released\ntoo many times it’s a sign of a bug. If not given, value defaults to 1.
\n\n
\n
\nclass threading.Thread
\n

A class that represents a thread of control. This class can be safely\nsubclassed in a limited fashion.

\n

See Thread Objects.

\n
\n\n
\n
\nclass threading.Timer
\n

A thread that executes a function after a specified interval has passed.

\n

See Timer Objects.

\n
\n\n
\n
\nthreading.settrace(func)
\n

Set a trace function for all threads started from the threading module.\nThe func will be passed to sys.settrace() for each thread, before its\nrun() method is called.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nthreading.setprofile(func)
\n

Set a profile function for all threads started from the threading module.\nThe func will be passed to sys.setprofile() for each thread, before its\nrun() method is called.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nthreading.stack_size([size])
\n

Return the thread stack size used when creating new threads. The optional\nsize argument specifies the stack size to be used for subsequently created\nthreads, and must be 0 (use platform or configured default) or a positive\ninteger value of at least 32,768 (32kB). If changing the thread stack size is\nunsupported, a ThreadError is raised. If the specified stack size is\ninvalid, a ValueError is raised and the stack size is unmodified. 32kB\nis currently the minimum supported stack size value to guarantee sufficient\nstack space for the interpreter itself. Note that some platforms may have\nparticular restrictions on values for the stack size, such as requiring a\nminimum stack size > 32kB or requiring allocation in multiples of the system\nmemory page size - platform documentation should be referred to for more\ninformation (4kB pages are common; using multiples of 4096 for the stack size is\nthe suggested approach in the absence of more specific information).\nAvailability: Windows, systems with POSIX threads.

\n

\nNew in version 2.5.

\n
\n\n

Detailed interfaces for the objects are documented below.

\n

The design of this module is loosely based on Java’s threading model. However,\nwhere Java makes locks and condition variables basic behavior of every object,\nthey are separate objects in Python. Python’s Thread class supports a\nsubset of the behavior of Java’s Thread class; currently, there are no\npriorities, no thread groups, and threads cannot be destroyed, stopped,\nsuspended, resumed, or interrupted. The static methods of Java’s Thread class,\nwhen implemented, are mapped to module-level functions.

\n

All of the methods described below are executed atomically.

\n
\n

16.2.1. Thread Objects

\n

This class represents an activity that is run in a separate thread of control.\nThere are two ways to specify the activity: by passing a callable object to the\nconstructor, or by overriding the run() method in a subclass. No other\nmethods (except for the constructor) should be overridden in a subclass. In\nother words, only override the __init__() and run() methods of\nthis class.

\n

Once a thread object is created, its activity must be started by calling the\nthread’s start() method. This invokes the run() method in a\nseparate thread of control.

\n

Once the thread’s activity is started, the thread is considered ‘alive’. It\nstops being alive when its run() method terminates – either normally, or\nby raising an unhandled exception. The is_alive() method tests whether the\nthread is alive.

\n

Other threads can call a thread’s join() method. This blocks the calling\nthread until the thread whose join() method is called is terminated.

\n

A thread has a name. The name can be passed to the constructor, and read or\nchanged through the name attribute.

\n

A thread can be flagged as a “daemon thread”. The significance of this flag is\nthat the entire Python program exits when only daemon threads are left. The\ninitial value is inherited from the creating thread. The flag can be set\nthrough the daemon property.

\n

There is a “main thread” object; this corresponds to the initial thread of\ncontrol in the Python program. It is not a daemon thread.

\n

There is the possibility that “dummy thread objects” are created. These are\nthread objects corresponding to “alien threads”, which are threads of control\nstarted outside the threading module, such as directly from C code. Dummy\nthread objects have limited functionality; they are always considered alive and\ndaemonic, and cannot be join()ed. They are never deleted, since it is\nimpossible to detect the termination of alien threads.

\n
\n
\nclass threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
\n

This constructor should always be called with keyword arguments. Arguments\nare:

\n

group should be None; reserved for future extension when a\nThreadGroup class is implemented.

\n

target is the callable object to be invoked by the run() method.\nDefaults to None, meaning nothing is called.

\n

name is the thread name. By default, a unique name is constructed of the\nform “Thread-N” where N is a small decimal number.

\n

args is the argument tuple for the target invocation. Defaults to ().

\n

kwargs is a dictionary of keyword arguments for the target invocation.\nDefaults to {}.

\n

If the subclass overrides the constructor, it must make sure to invoke the\nbase class constructor (Thread.__init__()) before doing anything else to\nthe thread.

\n
\n
\nstart()
\n

Start the thread’s activity.

\n

It must be called at most once per thread object. It arranges for the\nobject’s run() method to be invoked in a separate thread of control.

\n

This method will raise a RuntimeError if called more than once\non the same thread object.

\n
\n\n
\n
\nrun()
\n

Method representing the thread’s activity.

\n

You may override this method in a subclass. The standard run()\nmethod invokes the callable object passed to the object’s constructor as\nthe target argument, if any, with sequential and keyword arguments taken\nfrom the args and kwargs arguments, respectively.

\n
\n\n
\n
\njoin([timeout])
\n

Wait until the thread terminates. This blocks the calling thread until the\nthread whose join() method is called terminates – either normally\nor through an unhandled exception – or until the optional timeout occurs.

\n

When the timeout argument is present and not None, it should be a\nfloating point number specifying a timeout for the operation in seconds\n(or fractions thereof). As join() always returns None, you must\ncall isAlive() after join() to decide whether a timeout\nhappened – if the thread is still alive, the join() call timed out.

\n

When the timeout argument is not present or None, the operation will\nblock until the thread terminates.

\n

A thread can be join()ed many times.

\n

join() raises a RuntimeError if an attempt is made to join\nthe current thread as that would cause a deadlock. It is also an error to\njoin() a thread before it has been started and attempts to do so\nraises the same exception.

\n
\n\n
\n
\ngetName()
\n
\nsetName()
\n
Old API for name.
\n\n
\n
\nname
\n
A string used for identification purposes only. It has no semantics.\nMultiple threads may be given the same name. The initial name is set by\nthe constructor.
\n\n
\n
\nident
\n

The ‘thread identifier’ of this thread or None if the thread has not\nbeen started. This is a nonzero integer. See the\nthread.get_ident() function. Thread identifiers may be recycled\nwhen a thread exits and another thread is created. The identifier is\navailable even after the thread has exited.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nis_alive()
\n
\nisAlive()
\n

Return whether the thread is alive.

\n

This method returns True just before the run() method starts\nuntil just after the run() method terminates. The module function\nenumerate() returns a list of all alive threads.

\n
\n\n
\n
\nisDaemon()
\n
\nsetDaemon()
\n
Old API for daemon.
\n\n
\n
\ndaemon
\n

A boolean value indicating whether this thread is a daemon thread (True)\nor not (False). This must be set before start() is called,\notherwise RuntimeError is raised. Its initial value is inherited\nfrom the creating thread; the main thread is not a daemon thread and\ntherefore all threads created in the main thread default to daemon\n= False.

\n

The entire Python program exits when no alive non-daemon threads are left.

\n
\n\n
\n\n
\n
\n

16.2.2. Lock Objects

\n

A primitive lock is a synchronization primitive that is not owned by a\nparticular thread when locked. In Python, it is currently the lowest level\nsynchronization primitive available, implemented directly by the thread\nextension module.

\n

A primitive lock is in one of two states, “locked” or “unlocked”. It is created\nin the unlocked state. It has two basic methods, acquire() and\nrelease(). When the state is unlocked, acquire() changes the state\nto locked and returns immediately. When the state is locked, acquire()\nblocks until a call to release() in another thread changes it to unlocked,\nthen the acquire() call resets it to locked and returns. The\nrelease() method should only be called in the locked state; it changes the\nstate to unlocked and returns immediately. If an attempt is made to release an\nunlocked lock, a RuntimeError will be raised.

\n

When more than one thread is blocked in acquire() waiting for the state to\nturn to unlocked, only one thread proceeds when a release() call resets\nthe state to unlocked; which one of the waiting threads proceeds is not defined,\nand may vary across implementations.

\n

All methods are executed atomically.

\n
\n
\nLock.acquire([blocking])
\n

Acquire a lock, blocking or non-blocking.

\n

When invoked without arguments, block until the lock is unlocked, then set it to\nlocked, and return true.

\n

When invoked with the blocking argument set to true, do the same thing as when\ncalled without arguments, and return true.

\n

When invoked with the blocking argument set to false, do not block. If a call\nwithout an argument would block, return false immediately; otherwise, do the\nsame thing as when called without arguments, and return true.

\n
\n\n
\n
\nLock.release()
\n

Release a lock.

\n

When the lock is locked, reset it to unlocked, and return. If any other threads\nare blocked waiting for the lock to become unlocked, allow exactly one of them\nto proceed.

\n

Do not call this method when the lock is unlocked.

\n

There is no return value.

\n
\n\n
\n
\n

16.2.3. RLock Objects

\n

A reentrant lock is a synchronization primitive that may be acquired multiple\ntimes by the same thread. Internally, it uses the concepts of “owning thread”\nand “recursion level” in addition to the locked/unlocked state used by primitive\nlocks. In the locked state, some thread owns the lock; in the unlocked state,\nno thread owns it.

\n

To lock the lock, a thread calls its acquire() method; this returns once\nthe thread owns the lock. To unlock the lock, a thread calls its\nrelease() method. acquire()/release() call pairs may be\nnested; only the final release() (the release() of the outermost\npair) resets the lock to unlocked and allows another thread blocked in\nacquire() to proceed.

\n
\n
\nRLock.acquire([blocking=1])
\n

Acquire a lock, blocking or non-blocking.

\n

When invoked without arguments: if this thread already owns the lock, increment\nthe recursion level by one, and return immediately. Otherwise, if another\nthread owns the lock, block until the lock is unlocked. Once the lock is\nunlocked (not owned by any thread), then grab ownership, set the recursion level\nto one, and return. If more than one thread is blocked waiting until the lock\nis unlocked, only one at a time will be able to grab ownership of the lock.\nThere is no return value in this case.

\n

When invoked with the blocking argument set to true, do the same thing as when\ncalled without arguments, and return true.

\n

When invoked with the blocking argument set to false, do not block. If a call\nwithout an argument would block, return false immediately; otherwise, do the\nsame thing as when called without arguments, and return true.

\n
\n\n
\n
\nRLock.release()
\n

Release a lock, decrementing the recursion level. If after the decrement it is\nzero, reset the lock to unlocked (not owned by any thread), and if any other\nthreads are blocked waiting for the lock to become unlocked, allow exactly one\nof them to proceed. If after the decrement the recursion level is still\nnonzero, the lock remains locked and owned by the calling thread.

\n

Only call this method when the calling thread owns the lock. A\nRuntimeError is raised if this method is called when the lock is\nunlocked.

\n

There is no return value.

\n
\n\n
\n
\n

16.2.4. Condition Objects

\n

A condition variable is always associated with some kind of lock; this can be\npassed in or one will be created by default. (Passing one in is useful when\nseveral condition variables must share the same lock.)

\n

A condition variable has acquire() and release() methods that call\nthe corresponding methods of the associated lock. It also has a wait()\nmethod, and notify() and notifyAll() methods. These three must only\nbe called when the calling thread has acquired the lock, otherwise a\nRuntimeError is raised.

\n

The wait() method releases the lock, and then blocks until it is awakened\nby a notify() or notifyAll() call for the same condition variable in\nanother thread. Once awakened, it re-acquires the lock and returns. It is also\npossible to specify a timeout.

\n

The notify() method wakes up one of the threads waiting for the condition\nvariable, if any are waiting. The notifyAll() method wakes up all threads\nwaiting for the condition variable.

\n

Note: the notify() and notifyAll() methods don’t release the lock;\nthis means that the thread or threads awakened will not return from their\nwait() call immediately, but only when the thread that called\nnotify() or notifyAll() finally relinquishes ownership of the lock.

\n

Tip: the typical programming style using condition variables uses the lock to\nsynchronize access to some shared state; threads that are interested in a\nparticular change of state call wait() repeatedly until they see the\ndesired state, while threads that modify the state call notify() or\nnotifyAll() when they change the state in such a way that it could\npossibly be a desired state for one of the waiters. For example, the following\ncode is a generic producer-consumer situation with unlimited buffer capacity:

\n
# Consume one item\ncv.acquire()\nwhile not an_item_is_available():\n    cv.wait()\nget_an_available_item()\ncv.release()\n\n# Produce one item\ncv.acquire()\nmake_an_item_available()\ncv.notify()\ncv.release()\n
\n
\n

To choose between notify() and notifyAll(), consider whether one\nstate change can be interesting for only one or several waiting threads. E.g.\nin a typical producer-consumer situation, adding one item to the buffer only\nneeds to wake up one consumer thread.

\n
\n
\nclass threading.Condition([lock])
\n

If the lock argument is given and not None, it must be a Lock\nor RLock object, and it is used as the underlying lock. Otherwise,\na new RLock object is created and used as the underlying lock.

\n
\n
\nacquire(*args)
\n
Acquire the underlying lock. This method calls the corresponding method on\nthe underlying lock; the return value is whatever that method returns.
\n\n
\n
\nrelease()
\n
Release the underlying lock. This method calls the corresponding method on\nthe underlying lock; there is no return value.
\n\n
\n
\nwait([timeout])
\n

Wait until notified or until a timeout occurs. If the calling thread has not\nacquired the lock when this method is called, a RuntimeError is raised.

\n

This method releases the underlying lock, and then blocks until it is\nawakened by a notify() or notifyAll() call for the same\ncondition variable in another thread, or until the optional timeout\noccurs. Once awakened or timed out, it re-acquires the lock and returns.

\n

When the timeout argument is present and not None, it should be a\nfloating point number specifying a timeout for the operation in seconds\n(or fractions thereof).

\n

When the underlying lock is an RLock, it is not released using\nits release() method, since this may not actually unlock the lock\nwhen it was acquired multiple times recursively. Instead, an internal\ninterface of the RLock class is used, which really unlocks it\neven when it has been recursively acquired several times. Another internal\ninterface is then used to restore the recursion level when the lock is\nreacquired.

\n
\n\n
\n
\nnotify(n=1)
\n

By default, wake up one thread waiting on this condition, if any. If the\ncalling thread has not acquired the lock when this method is called, a\nRuntimeError is raised.

\n

This method wakes up at most n of the threads waiting for the condition\nvariable; it is a no-op if no threads are waiting.

\n

The current implementation wakes up exactly n threads, if at least n\nthreads are waiting. However, it’s not safe to rely on this behavior.\nA future, optimized implementation may occasionally wake up more than\nn threads.

\n

Note: an awakened thread does not actually return from its wait()\ncall until it can reacquire the lock. Since notify() does not\nrelease the lock, its caller should.

\n
\n\n
\n
\nnotify_all()
\n
\nnotifyAll()
\n
Wake up all threads waiting on this condition. This method acts like\nnotify(), but wakes up all waiting threads instead of one. If the\ncalling thread has not acquired the lock when this method is called, a\nRuntimeError is raised.
\n\n
\n\n
\n
\n

16.2.5. Semaphore Objects

\n

This is one of the oldest synchronization primitives in the history of computer\nscience, invented by the early Dutch computer scientist Edsger W. Dijkstra (he\nused P() and V() instead of acquire() and release()).

\n

A semaphore manages an internal counter which is decremented by each\nacquire() call and incremented by each release() call. The counter\ncan never go below zero; when acquire() finds that it is zero, it blocks,\nwaiting until some other thread calls release().

\n
\n
\nclass threading.Semaphore([value])
\n

The optional argument gives the initial value for the internal counter; it\ndefaults to 1. If the value given is less than 0, ValueError is\nraised.

\n
\n
\nacquire([blocking])
\n

Acquire a semaphore.

\n

When invoked without arguments: if the internal counter is larger than\nzero on entry, decrement it by one and return immediately. If it is zero\non entry, block, waiting until some other thread has called\nrelease() to make it larger than zero. This is done with proper\ninterlocking so that if multiple acquire() calls are blocked,\nrelease() will wake exactly one of them up. The implementation may\npick one at random, so the order in which blocked threads are awakened\nshould not be relied on. There is no return value in this case.

\n

When invoked with blocking set to true, do the same thing as when called\nwithout arguments, and return true.

\n

When invoked with blocking set to false, do not block. If a call\nwithout an argument would block, return false immediately; otherwise, do\nthe same thing as when called without arguments, and return true.

\n
\n\n
\n
\nrelease()
\n
Release a semaphore, incrementing the internal counter by one. When it\nwas zero on entry and another thread is waiting for it to become larger\nthan zero again, wake up that thread.
\n\n
\n\n
\n

16.2.5.1. Semaphore Example

\n

Semaphores are often used to guard resources with limited capacity, for example,\na database server. In any situation where the size of the resource is fixed,\nyou should use a bounded semaphore. Before spawning any worker threads, your\nmain thread would initialize the semaphore:

\n
maxconnections = 5\n...\npool_sema = BoundedSemaphore(value=maxconnections)\n
\n
\n

Once spawned, worker threads call the semaphore’s acquire and release methods\nwhen they need to connect to the server:

\n
pool_sema.acquire()\nconn = connectdb()\n... use connection ...\nconn.close()\npool_sema.release()\n
\n
\n

The use of a bounded semaphore reduces the chance that a programming error which\ncauses the semaphore to be released more than it’s acquired will go undetected.

\n
\n
\n
\n

16.2.6. Event Objects

\n

This is one of the simplest mechanisms for communication between threads: one\nthread signals an event and other threads wait for it.

\n

An event object manages an internal flag that can be set to true with the\nset() method and reset to false with the clear() method. The\nwait() method blocks until the flag is true.

\n
\n
\nclass threading.Event
\n

The internal flag is initially false.

\n
\n
\nis_set()
\n
\nisSet()
\n

Return true if and only if the internal flag is true.

\n

\nChanged in version 2.6: The is_set() syntax is new.

\n
\n\n
\n
\nset()
\n
Set the internal flag to true. All threads waiting for it to become true\nare awakened. Threads that call wait() once the flag is true will\nnot block at all.
\n\n
\n
\nclear()
\n
Reset the internal flag to false. Subsequently, threads calling\nwait() will block until set() is called to set the internal\nflag to true again.
\n\n
\n
\nwait([timeout])
\n

Block until the internal flag is true. If the internal flag is true on\nentry, return immediately. Otherwise, block until another thread calls\nset() to set the flag to true, or until the optional timeout\noccurs.

\n

When the timeout argument is present and not None, it should be a\nfloating point number specifying a timeout for the operation in seconds\n(or fractions thereof).

\n

This method returns the internal flag on exit, so it will always return\nTrue except if a timeout is given and the operation times out.

\n

\nChanged in version 2.7: Previously, the method always returned None.

\n
\n\n
\n\n
\n
\n

16.2.7. Timer Objects

\n

This class represents an action that should be run only after a certain amount\nof time has passed — a timer. Timer is a subclass of Thread\nand as such also functions as an example of creating custom threads.

\n

Timers are started, as with threads, by calling their start() method. The\ntimer can be stopped (before its action has begun) by calling the cancel()\nmethod. The interval the timer will wait before executing its action may not be\nexactly the same as the interval specified by the user.

\n

For example:

\n
def hello():\n    print "hello, world"\n\nt = Timer(30.0, hello)\nt.start() # after 30 seconds, "hello, world" will be printed\n
\n
\n
\n
\nclass threading.Timer(interval, function, args=[], kwargs={})
\n

Create a timer that will run function with arguments args and keyword\narguments kwargs, after interval seconds have passed.

\n
\n
\ncancel()
\n
Stop the timer, and cancel the execution of the timer’s action. This will\nonly work if the timer is still in its waiting stage.
\n\n
\n\n
\n
\n

16.2.8. Using locks, conditions, and semaphores in the with statement

\n

All of the objects provided by this module that have acquire() and\nrelease() methods can be used as context managers for a with\nstatement. The acquire() method will be called when the block is entered,\nand release() will be called when the block is exited.

\n

Currently, Lock, RLock, Condition,\nSemaphore, and BoundedSemaphore objects may be used as\nwith statement context managers. For example:

\n
import threading\n\nsome_rlock = threading.RLock()\n\nwith some_rlock:\n    print "some_rlock is locked while this executes"\n
\n
\n
\n
\n

16.2.9. Importing in threaded code

\n

While the import machinery is thread-safe, there are two key restrictions on\nthreaded imports due to inherent limitations in the way that thread-safety is\nprovided:

\n\n
\n
", "searchableItems": [ { "name": "threading.active_count", "domId": "threading_threading.active_count" }, { "name": "threading.BoundedSemaphore", "domId": "threading_threading.BoundedSemaphore" }, { "name": "threading.Condition", "domId": "threading_threading.Condition" }, { "name": "threading.Condition.acquire", "domId": "threading_threading.Condition.acquire" }, { "name": "threading.Condition.notify", "domId": "threading_threading.Condition.notify" }, { "name": "threading.Condition.notify_all", "domId": "threading_threading.Condition.notify_all" }, { "name": "threading.Condition.release", "domId": "threading_threading.Condition.release" }, { "name": "threading.Condition.wait", "domId": "threading_threading.Condition.wait" }, { "name": "threading.current_thread", "domId": "threading_threading.current_thread" }, { "name": "threading.enumerate", "domId": "threading_threading.enumerate" }, { "name": "threading.Event", "domId": "threading_threading.Event" }, { "name": "threading.Event.clear", "domId": "threading_threading.Event.clear" }, { "name": "threading.Event.is_set", "domId": "threading_threading.Event.is_set" }, { "name": "threading.Event.set", "domId": "threading_threading.Event.set" }, { "name": "threading.Event.wait", "domId": "threading_threading.Event.wait" }, { "name": "threading.local", "domId": "threading_threading.local" }, { "name": "threading.Lock", "domId": "threading_threading.Lock" }, { "name": "threading.Lock.acquire", "domId": "threading_threading.Lock.acquire" }, { "name": "threading.Lock.release", "domId": "threading_threading.Lock.release" }, { "name": "threading.RLock", "domId": "threading_threading.RLock" }, { "name": "threading.RLock.acquire", "domId": "threading_threading.RLock.acquire" }, { "name": "threading.RLock.release", "domId": "threading_threading.RLock.release" }, { "name": "threading.Semaphore", "domId": "threading_threading.Semaphore" }, { "name": "threading.Semaphore.acquire", "domId": "threading_threading.Semaphore.acquire" }, { "name": "threading.Semaphore.release", "domId": "threading_threading.Semaphore.release" }, { "name": "threading.setprofile", "domId": "threading_threading.setprofile" }, { "name": "threading.settrace", "domId": "threading_threading.settrace" }, { "name": "threading.stack_size", "domId": "threading_threading.stack_size" }, { "name": "threading.Thread", "domId": "threading_threading.Thread" }, { "name": "threading.Thread.getName", "domId": "threading_threading.Thread.getName" }, { "name": "threading.Thread.is_alive", "domId": "threading_threading.Thread.is_alive" }, { "name": "threading.Thread.isDaemon", "domId": "threading_threading.Thread.isDaemon" }, { "name": "threading.Thread.join", "domId": "threading_threading.Thread.join" }, { "name": "threading.Thread.run", "domId": "threading_threading.Thread.run" }, { "name": "threading.Thread.start", "domId": "threading_threading.Thread.start" }, { "name": "threading.Timer", "domId": "threading_threading.Timer" }, { "name": "threading.Timer.cancel", "domId": "threading_threading.Timer.cancel" } ] }, { "url": "http://docs.python.org/library/rlcompleter.html", "title": "rlcompleter", "html": "
\n

16.9. rlcompleter — Completion function for GNU readline

\n

Source code: Lib/rlcompleter.py

\n
\n

The rlcompleter module defines a completion function suitable for the\nreadline module by completing valid Python identifiers and keywords.

\n

When this module is imported on a Unix platform with the readline module\navailable, an instance of the Completer class is automatically created\nand its complete() method is set as the readline completer.

\n

Example:

\n
>>> import rlcompleter\n>>> import readline\n>>> readline.parse_and_bind("tab: complete")\n>>> readline. <TAB PRESSED>\nreadline.__doc__          readline.get_line_buffer(  readline.read_init_file(\nreadline.__file__         readline.insert_text(      readline.set_completer(\nreadline.__name__         readline.parse_and_bind(\n>>> readline.\n
\n
\n

The rlcompleter module is designed for use with Python’s interactive\nmode. A user can add the following lines to his or her initialization file\n(identified by the PYTHONSTARTUP environment variable) to get\nautomatic Tab completion:

\n
try:\n    import readline\nexcept ImportError:\n    print "Module readline not available."\nelse:\n    import rlcompleter\n    readline.parse_and_bind("tab: complete")\n
\n
\n

On platforms without readline, the Completer class defined by\nthis module can still be used for custom purposes.

\n
\n

16.9.1. Completer Objects

\n

Completer objects have the following method:

\n
\n
\nCompleter.complete(text, state)
\n

Return the stateth completion for text.

\n

If called for text that doesn’t include a period character ('.'), it will\ncomplete from names currently defined in __main__, __builtin__ and\nkeywords (as defined by the keyword module).

\n

If called for a dotted name, it will try to evaluate anything without obvious\nside-effects (functions will not be evaluated, but it can generate calls to\n__getattr__()) up to the last part, and find matches for the rest via the\ndir() function. Any exception raised during the evaluation of the\nexpression is caught, silenced and None is returned.

\n
\n\n
\n
", "searchableItems": [ { "name": "rlcompleter.Completer.complete", "domId": "rlcompleter_rlcompleter.Completer.complete" } ] }, { "url": "http://docs.python.org/library/signal.html", "title": "signal", "html": "
\n

17.4. signal — Set handlers for asynchronous events

\n

This module provides mechanisms to use signal handlers in Python. Some general\nrules for working with signals and their handlers:

\n\n

The variables defined in the signal module are:

\n
\n
\nsignal.SIG_DFL
\n
This is one of two standard signal handling options; it will simply perform\nthe default function for the signal. For example, on most systems the\ndefault action for SIGQUIT is to dump core and exit, while the\ndefault action for SIGCHLD is to simply ignore it.
\n\n
\n
\nsignal.SIG_IGN
\n
This is another standard signal handler, which will simply ignore the given\nsignal.
\n\n
\n
\nSIG*
\n
All the signal numbers are defined symbolically. For example, the hangup signal\nis defined as signal.SIGHUP; the variable names are identical to the\nnames used in C programs, as found in <signal.h>. The Unix man page for\n‘signal()‘ lists the existing signals (on some systems this is\nsignal(2), on others the list is in signal(7)). Note that\nnot all systems define the same set of signal names; only those names defined by\nthe system are defined by this module.
\n\n
\n
\nsignal.CTRL_C_EVENT
\n

The signal corresponding to the CTRL+C keystroke event. This signal can\nonly be used with os.kill().

\n

Availability: Windows.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nsignal.CTRL_BREAK_EVENT
\n

The signal corresponding to the CTRL+BREAK keystroke event. This signal can\nonly be used with os.kill().

\n

Availability: Windows.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nsignal.NSIG
\n
One more than the number of the highest signal number.
\n\n
\n
\nsignal.ITIMER_REAL
\n
Decrements interval timer in real time, and delivers SIGALRM upon expiration.
\n\n
\n
\nsignal.ITIMER_VIRTUAL
\n
Decrements interval timer only when the process is executing, and delivers\nSIGVTALRM upon expiration.
\n\n
\n
\nsignal.ITIMER_PROF
\n
Decrements interval timer both when the process executes and when the\nsystem is executing on behalf of the process. Coupled with ITIMER_VIRTUAL,\nthis timer is usually used to profile the time spent by the application\nin user and kernel space. SIGPROF is delivered upon expiration.
\n\n

The signal module defines one exception:

\n
\n
\nexception signal.ItimerError
\n
Raised to signal an error from the underlying setitimer() or\ngetitimer() implementation. Expect this error if an invalid\ninterval timer or a negative time is passed to setitimer().\nThis error is a subtype of IOError.
\n\n

The signal module defines the following functions:

\n
\n
\nsignal.alarm(time)
\n
If time is non-zero, this function requests that a SIGALRM signal be\nsent to the process in time seconds. Any previously scheduled alarm is\ncanceled (only one alarm can be scheduled at any time). The returned value is\nthen the number of seconds before any previously set alarm was to have been\ndelivered. If time is zero, no alarm is scheduled, and any scheduled alarm is\ncanceled. If the return value is zero, no alarm is currently scheduled. (See\nthe Unix man page alarm(2).) Availability: Unix.
\n\n
\n
\nsignal.getsignal(signalnum)
\n
Return the current signal handler for the signal signalnum. The returned value\nmay be a callable Python object, or one of the special values\nsignal.SIG_IGN, signal.SIG_DFL or None. Here,\nsignal.SIG_IGN means that the signal was previously ignored,\nsignal.SIG_DFL means that the default way of handling the signal was\npreviously in use, and None means that the previous signal handler was not\ninstalled from Python.
\n\n
\n
\nsignal.pause()
\n
Cause the process to sleep until a signal is received; the appropriate handler\nwill then be called. Returns nothing. Not on Windows. (See the Unix man page\nsignal(2).)
\n\n
\n
\nsignal.setitimer(which, seconds[, interval])
\n

Sets given interval timer (one of signal.ITIMER_REAL,\nsignal.ITIMER_VIRTUAL or signal.ITIMER_PROF) specified\nby which to fire after seconds (float is accepted, different from\nalarm()) and after that every interval seconds. The interval\ntimer specified by which can be cleared by setting seconds to zero.

\n

When an interval timer fires, a signal is sent to the process.\nThe signal sent is dependent on the timer being used;\nsignal.ITIMER_REAL will deliver SIGALRM,\nsignal.ITIMER_VIRTUAL sends SIGVTALRM,\nand signal.ITIMER_PROF will deliver SIGPROF.

\n

The old values are returned as a tuple: (delay, interval).

\n

Attempting to pass an invalid interval timer will cause an\nItimerError. Availability: Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsignal.getitimer(which)
\n

Returns current value of a given interval timer specified by which.\nAvailability: Unix.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsignal.set_wakeup_fd(fd)
\n

Set the wakeup fd to fd. When a signal is received, a '\\0' byte is\nwritten to the fd. This can be used by a library to wakeup a poll or select\ncall, allowing the signal to be fully processed.

\n

The old wakeup fd is returned. fd must be non-blocking. It is up to the\nlibrary to remove any bytes before calling poll or select again.

\n

When threads are enabled, this function can only be called from the main thread;\nattempting to call it from other threads will cause a ValueError\nexception to be raised.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsignal.siginterrupt(signalnum, flag)
\n

Change system call restart behaviour: if flag is False, system\ncalls will be restarted when interrupted by signal signalnum, otherwise\nsystem calls will be interrupted. Returns nothing. Availability: Unix (see\nthe man page siginterrupt(3) for further information).

\n

Note that installing a signal handler with signal() will reset the\nrestart behaviour to interruptible by implicitly calling\nsiginterrupt() with a true flag value for the given signal.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsignal.signal(signalnum, handler)
\n

Set the handler for signal signalnum to the function handler. handler can\nbe a callable Python object taking two arguments (see below), or one of the\nspecial values signal.SIG_IGN or signal.SIG_DFL. The previous\nsignal handler will be returned (see the description of getsignal()\nabove). (See the Unix man page signal(2).)

\n

When threads are enabled, this function can only be called from the main thread;\nattempting to call it from other threads will cause a ValueError\nexception to be raised.

\n

The handler is called with two arguments: the signal number and the current\nstack frame (None or a frame object; for a description of frame objects,\nsee the description in the type hierarchy or see the\nattribute descriptions in the inspect module).

\n

On Windows, signal() can only be called with SIGABRT,\nSIGFPE, SIGILL, SIGINT, SIGSEGV, or\nSIGTERM. A ValueError will be raised in any other case.

\n
\n\n
\n

17.4.1. Example

\n

Here is a minimal example program. It uses the alarm() function to limit\nthe time spent waiting to open a file; this is useful if the file is for a\nserial device that may not be turned on, which would normally cause the\nos.open() to hang indefinitely. The solution is to set a 5-second alarm\nbefore opening the file; if the operation takes too long, the alarm signal will\nbe sent, and the handler raises an exception.

\n
import signal, os\n\ndef handler(signum, frame):\n    print 'Signal handler called with signal', signum\n    raise IOError("Couldn't open device!")\n\n# Set the signal handler and a 5-second alarm\nsignal.signal(signal.SIGALRM, handler)\nsignal.alarm(5)\n\n# This open() may hang indefinitely\nfd = os.open('/dev/ttyS0', os.O_RDWR)\n\nsignal.alarm(0)          # Disable the alarm\n
\n
\n
\n
", "searchableItems": [ { "name": "signal.alarm", "domId": "signal_signal.alarm" }, { "name": "signal.getitimer", "domId": "signal_signal.getitimer" }, { "name": "signal.getsignal", "domId": "signal_signal.getsignal" }, { "name": "signal.pause", "domId": "signal_signal.pause" }, { "name": "signal.set_wakeup_fd", "domId": "signal_signal.set_wakeup_fd" }, { "name": "signal.setitimer", "domId": "signal_signal.setitimer" }, { "name": "signal.siginterrupt", "domId": "signal_signal.siginterrupt" }, { "name": "signal.signal", "domId": "signal_signal.signal" } ] }, { "url": "http://docs.python.org/library/popen2.html", "title": "popen2", "html": "
\n

17.5. popen2 — Subprocesses with accessible I/O streams

\n

\nDeprecated since version 2.6: This module is obsolete. Use the subprocess module. Check\nespecially the Replacing Older Functions with the subprocess Module section.

\n

This module allows you to spawn processes and connect to their\ninput/output/error pipes and obtain their return codes under Unix and Windows.

\n

The subprocess module provides more powerful facilities for spawning new\nprocesses and retrieving their results. Using the subprocess module is\npreferable to using the popen2 module.

\n

The primary interface offered by this module is a trio of factory functions.\nFor each of these, if bufsize is specified, it specifies the buffer size for\nthe I/O pipes. mode, if provided, should be the string 'b' or 't'; on\nWindows this is needed to determine whether the file objects should be opened in\nbinary or text mode. The default value for mode is 't'.

\n

On Unix, cmd may be a sequence, in which case arguments will be passed\ndirectly to the program without shell intervention (as with os.spawnv()).\nIf cmd is a string it will be passed to the shell (as with os.system()).

\n

The only way to retrieve the return codes for the child processes is by using\nthe poll() or wait() methods on the Popen3 and\nPopen4 classes; these are only available on Unix. This information is\nnot available when using the popen2(), popen3(), and popen4()\nfunctions, or the equivalent functions in the os module. (Note that the\ntuples returned by the os module’s functions are in a different order\nfrom the ones returned by the popen2 module.)

\n
\n
\npopen2.popen2(cmd[, bufsize[, mode]])
\n
Executes cmd as a sub-process. Returns the file objects (child_stdout,\nchild_stdin).
\n\n
\n
\npopen2.popen3(cmd[, bufsize[, mode]])
\n
Executes cmd as a sub-process. Returns the file objects (child_stdout,\nchild_stdin, child_stderr).
\n\n
\n
\npopen2.popen4(cmd[, bufsize[, mode]])
\n

Executes cmd as a sub-process. Returns the file objects\n(child_stdout_and_stderr, child_stdin).

\n

\nNew in version 2.0.

\n
\n\n

On Unix, a class defining the objects returned by the factory functions is also\navailable. These are not used for the Windows implementation, and are not\navailable on that platform.

\n
\n
\nclass popen2.Popen3(cmd[, capturestderr[, bufsize]])
\n

This class represents a child process. Normally, Popen3 instances are\ncreated using the popen2() and popen3() factory functions described\nabove.

\n

If not using one of the helper functions to create Popen3 objects, the\nparameter cmd is the shell command to execute in a sub-process. The\ncapturestderr flag, if true, specifies that the object should capture standard\nerror output of the child process. The default is false. If the bufsize\nparameter is specified, it specifies the size of the I/O buffers to/from the\nchild process.

\n
\n\n
\n
\nclass popen2.Popen4(cmd[, bufsize])
\n

Similar to Popen3, but always captures standard error into the same\nfile object as standard output. These are typically created using\npopen4().

\n

\nNew in version 2.0.

\n
\n\n
\n

17.5.1. Popen3 and Popen4 Objects

\n

Instances of the Popen3 and Popen4 classes have the following\nmethods:

\n
\n
\nPopen3.poll()
\n
Returns -1 if child process hasn’t completed yet, or its status code\n(see wait()) otherwise.
\n\n
\n
\nPopen3.wait()
\n
Waits for and returns the status code of the child process. The status code\nencodes both the return code of the process and information about whether it\nexited using the exit() system call or died due to a signal. Functions\nto help interpret the status code are defined in the os module; see\nsection Process Management for the W*() family of functions.
\n\n

The following attributes are also available:

\n
\n
\nPopen3.fromchild
\n
A file object that provides output from the child process. For Popen4\ninstances, this will provide both the standard output and standard error\nstreams.
\n\n
\n
\nPopen3.tochild
\n
A file object that provides input to the child process.
\n\n
\n
\nPopen3.childerr
\n
A file object that provides error output from the child process, if\ncapturestderr was true for the constructor, otherwise None. This will\nalways be None for Popen4 instances.
\n\n
\n
\nPopen3.pid
\n
The process ID of the child process.
\n\n
\n
\n

17.5.2. Flow Control Issues

\n

Any time you are working with any form of inter-process communication, control\nflow needs to be carefully thought out. This remains the case with the file\nobjects provided by this module (or the os module equivalents).

\n

When reading output from a child process that writes a lot of data to standard\nerror while the parent is reading from the child’s standard output, a deadlock\ncan occur. A similar situation can occur with other combinations of reads and\nwrites. The essential factors are that more than _PC_PIPE_BUF bytes\nare being written by one process in a blocking fashion, while the other process\nis reading from the first process, also in a blocking fashion.

\n

There are several ways to deal with this situation.

\n

The simplest application change, in many cases, will be to follow this model in\nthe parent process:

\n
import popen2\n\nr, w, e = popen2.popen3('python slave.py')\ne.readlines()\nr.readlines()\nr.close()\ne.close()\nw.close()\n
\n
\n

with code like this in the child:

\n
import os\nimport sys\n\n# note that each of these print statements\n# writes a single long string\n\nprint >>sys.stderr, 400 * 'this is a test\\n'\nos.close(sys.stderr.fileno())\nprint >>sys.stdout, 400 * 'this is another test\\n'\n
\n
\n

In particular, note that sys.stderr must be closed after writing all data,\nor readlines() won’t return. Also note that os.close() must be\nused, as sys.stderr.close() won’t close stderr (otherwise assigning to\nsys.stderr will silently close it, so no further errors can be printed).

\n

Applications which need to support a more general approach should integrate I/O\nover pipes with their select() loops, or use separate threads to read each\nof the individual files provided by whichever popen*() function or\nPopen* class was used.

\n
\n

See also

\n
\n
Module subprocess
\n
Module for spawning and managing subprocesses.
\n
\n
\n
\n
", "searchableItems": [ { "name": "popen2.popen2", "domId": "popen2_popen2.popen2" }, { "name": "popen2.Popen3", "domId": "popen2_popen2_popen2.Popen3" }, { "name": "popen2.popen3", "domId": "popen2_popen2.popen3" }, { "name": "popen2.Popen3", "domId": "popen2_popen2_popen2.Popen3" }, { "name": "popen2.Popen3.poll", "domId": "popen2_popen2.Popen3.poll" }, { "name": "popen2.Popen3.wait", "domId": "popen2_popen2.Popen3.wait" }, { "name": "popen2.Popen4", "domId": "popen2_popen2_popen2.Popen4" }, { "name": "popen2.popen4", "domId": "popen2_popen2.popen4" }, { "name": "popen2.Popen4", "domId": "popen2_popen2_popen2.Popen4" } ] }, { "url": "http://docs.python.org/library/subprocess.html", "title": "subprocess", "html": "
\n

17.1. subprocess — Subprocess management

\n

\nNew in version 2.4.

\n

The subprocess module allows you to spawn new processes, connect to their\ninput/output/error pipes, and obtain their return codes. This module intends to\nreplace several other, older modules and functions, such as:

\n
os.system\nos.spawn*\nos.popen*\npopen2.*\ncommands.*
\n
\n

Information about how the subprocess module can be used to replace these\nmodules and functions can be found in the following sections.

\n
\n

See also

\n

PEP 324 – PEP proposing the subprocess module

\n
\n
\n

17.1.1. Using the subprocess Module

\n

The recommended approach to invoking subprocesses is to use the following\nconvenience functions for all use cases they can handle. For more advanced\nuse cases, the underlying Popen interface can be used directly.

\n
\n
\nsubprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
\n

Run the command described by args. Wait for command to complete, then\nreturn the returncode attribute.

\n

The arguments shown above are merely the most common ones, described below\nin Frequently Used Arguments (hence the slightly odd notation in\nthe abbreviated signature). The full function signature is the same as\nthat of the Popen constructor - this functions passes all\nsupplied arguments directly through to that interface.

\n

Examples:

\n
>>> subprocess.call(["ls", "-l"])\n0\n\n>>> subprocess.call("exit 1", shell=True)\n1\n
\n
\n
\n

Warning

\n

Invoking the system shell with shell=True can be a security hazard\nif combined with untrusted input. See the warning under\nFrequently Used Arguments for details.

\n
\n
\n

Note

\n

Do not use stdout=PIPE or stderr=PIPE with this function. As\nthe pipes are not being read in the current process, the child\nprocess may block if it generates enough output to a pipe to fill up\nthe OS pipe buffer.

\n
\n
\n\n
\n
\nsubprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
\n

Run command with arguments. Wait for command to complete. If the return\ncode was zero then return, otherwise raise CalledProcessError. The\nCalledProcessError object will have the return code in the\nreturncode attribute.

\n

The arguments shown above are merely the most common ones, described below\nin Frequently Used Arguments (hence the slightly odd notation in\nthe abbreviated signature). The full function signature is the same as\nthat of the Popen constructor - this functions passes all\nsupplied arguments directly through to that interface.

\n

Examples:

\n
>>> subprocess.check_call(["ls", "-l"])\n0\n\n>>> subprocess.check_call("exit 1", shell=True)\nTraceback (most recent call last):\n   ...\nsubprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1\n
\n
\n

\nNew in version 2.5.

\n
\n

Warning

\n

Invoking the system shell with shell=True can be a security hazard\nif combined with untrusted input. See the warning under\nFrequently Used Arguments for details.

\n
\n
\n

Note

\n

Do not use stdout=PIPE or stderr=PIPE with this function. As\nthe pipes are not being read in the current process, the child\nprocess may block if it generates enough output to a pipe to fill up\nthe OS pipe buffer.

\n
\n
\n\n
\n
\nsubprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
\n

Run command with arguments and return its output as a byte string.

\n

If the return code was non-zero it raises a CalledProcessError. The\nCalledProcessError object will have the return code in the\nreturncode attribute and any output in the output\nattribute.

\n

The arguments shown above are merely the most common ones, described below\nin Frequently Used Arguments (hence the slightly odd notation in\nthe abbreviated signature). The full function signature is largely the\nsame as that of the Popen constructor, except that stdout is\nnot permitted as it is used internally. All other supplied arguments are\npassed directly through to the Popen constructor.

\n

Examples:

\n
>>> subprocess.check_output(["echo", "Hello World!"])\n'Hello World!\\n'\n\n>>> subprocess.check_output("exit 1", shell=True)\nTraceback (most recent call last):\n   ...\nsubprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1\n
\n
\n

To also capture standard error in the result, use\nstderr=subprocess.STDOUT:

\n
>>> subprocess.check_output(\n...     "ls non_existent_file; exit 0",\n...     stderr=subprocess.STDOUT,\n...     shell=True)\n'ls: non_existent_file: No such file or directory\\n'\n
\n
\n

\nNew in version 2.7.

\n
\n

Warning

\n

Invoking the system shell with shell=True can be a security hazard\nif combined with untrusted input. See the warning under\nFrequently Used Arguments for details.

\n
\n
\n

Note

\n

Do not use stderr=PIPE with this function. As the pipe is not being\nread in the current process, the child process may block if it\ngenerates enough output to the pipe to fill up the OS pipe buffer.

\n
\n
\n\n
\n
\nsubprocess.PIPE
\n
Special value that can be used as the stdin, stdout or stderr argument\nto Popen and indicates that a pipe to the standard stream should be\nopened.
\n\n
\n
\nsubprocess.STDOUT
\n
Special value that can be used as the stderr argument to Popen and\nindicates that standard error should go into the same handle as standard\noutput.
\n\n
\n

17.1.1.1. Frequently Used Arguments

\n

To support a wide variety of use cases, the Popen constructor (and\nthe convenience functions) accept a large number of optional arguments. For\nmost typical use cases, many of these arguments can be safely left at their\ndefault values. The arguments that are most commonly needed are:

\n
\n

args is required for all calls and should be a string, or a sequence of\nprogram arguments. Providing a sequence of arguments is generally\npreferred, as it allows the module to take care of any required escaping\nand quoting of arguments (e.g. to permit spaces in file names). If passing\na single string, either shell must be True (see below) or else\nthe string must simply name the program to be executed without specifying\nany arguments.

\n

stdin, stdout and stderr specify the executed program’s standard input,\nstandard output and standard error file handles, respectively. Valid values\nare PIPE, an existing file descriptor (a positive integer), an\nexisting file object, and None. PIPE indicates that a new pipe\nto the child should be created. With the default settings of None, no\nredirection will occur; the child’s file handles will be inherited from the\nparent. Additionally, stderr can be STDOUT, which indicates that\nthe stderr data from the child process should be captured into the same file\nhandle as for stdout.

\n

When stdout or stderr are pipes and universal_newlines is\nTrue then all line endings will be converted to '\\n' as\ndescribed for the universal newlines ‘U’` mode argument to open().

\n

If shell is True, the specified command will be executed through\nthe shell. This can be useful if you are using Python primarily for the\nenhanced control flow it offers over most system shells and still want\naccess to other shell features such as filename wildcards, shell pipes and\nenvironment variable expansion.

\n
\n

Warning

\n

Executing shell commands that incorporate unsanitized input from an\nuntrusted source makes a program vulnerable to shell injection,\na serious security flaw which can result in arbitrary command execution.\nFor this reason, the use of shell=True is strongly discouraged in cases\nwhere the command string is constructed from external input:

\n
>>> from subprocess import call\n>>> filename = input("What file would you like to display?\\n")\nWhat file would you like to display?\nnon_existent; rm -rf / #\n>>> call("cat " + filename, shell=True) # Uh-oh. This will end badly...\n
\n
\n

shell=False disables all shell based features, but does not suffer\nfrom this vulnerability; see the Note in the Popen constructor\ndocumentation for helpful hints in getting shell=False to work.

\n
\n
\n

These options, along with all of the other options, are described in more\ndetail in the Popen constructor documentation.

\n
\n
\n

17.1.1.2. Popen Constructor

\n

The underlying process creation and management in this module is handled by\nthe Popen class. It offers a lot of flexibility so that developers\nare able to handle the less common cases not covered by the convenience\nfunctions.

\n
\n
\nclass subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
\n

Arguments are:

\n

args should be a string, or a sequence of program arguments. The program\nto execute is normally the first item in the args sequence or the string if\na string is given, but can be explicitly set by using the executable\nargument. When executable is given, the first item in the args sequence\nis still treated by most programs as the command name, which can then be\ndifferent from the actual executable name. On Unix, it becomes the display\nname for the executing program in utilities such as ps.

\n

On Unix, with shell=False (default): In this case, the Popen class uses\nos.execvp() to execute the child program. args should normally be a\nsequence. If a string is specified for args, it will be used as the name\nor path of the program to execute; this will only work if the program is\nbeing given no arguments.

\n
\n

Note

\n

shlex.split() can be useful when determining the correct\ntokenization for args, especially in complex cases:

\n
>>> import shlex, subprocess\n>>> command_line = raw_input()\n/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"\n>>> args = shlex.split(command_line)\n>>> print args\n['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]\n>>> p = subprocess.Popen(args) # Success!\n
\n
\n

Note in particular that options (such as -input) and arguments (such\nas eggs.txt) that are separated by whitespace in the shell go in separate\nlist elements, while arguments that need quoting or backslash escaping when\nused in the shell (such as filenames containing spaces or the echo command\nshown above) are single list elements.

\n
\n

On Unix, with shell=True: If args is a string, it specifies the command\nstring to execute through the shell. This means that the string must be\nformatted exactly as it would be when typed at the shell prompt. This\nincludes, for example, quoting or backslash escaping filenames with spaces in\nthem. If args is a sequence, the first item specifies the command string, and\nany additional items will be treated as additional arguments to the shell\nitself. That is to say, Popen does the equivalent of:

\n
Popen(['/bin/sh', '-c', args[0], args[1], ...])\n
\n
\n

On Windows: the Popen class uses CreateProcess() to execute the child\nchild program, which operates on strings. If args is a sequence, it will\nbe converted to a string in a manner described in\nConverting an argument sequence to a string on Windows.

\n

bufsize, if given, has the same meaning as the corresponding argument to the\nbuilt-in open() function: 0 means unbuffered, 1 means line\nbuffered, any other positive value means use a buffer of (approximately) that\nsize. A negative bufsize means to use the system default, which usually means\nfully buffered. The default value for bufsize is 0 (unbuffered).

\n
\n

Note

\n

If you experience performance issues, it is recommended that you try to\nenable buffering by setting bufsize to either -1 or a large enough\npositive value (such as 4096).

\n
\n

The executable argument specifies the program to execute. It is very seldom\nneeded: Usually, the program to execute is defined by the args argument. If\nshell=True, the executable argument specifies which shell to use. On Unix,\nthe default shell is /bin/sh. On Windows, the default shell is\nspecified by the COMSPEC environment variable. The only reason you\nwould need to specify shell=True on Windows is where the command you\nwish to execute is actually built in to the shell, eg dir, copy.\nYou don’t need shell=True to run a batch file, nor to run a console-based\nexecutable.

\n

stdin, stdout and stderr specify the executed program’s standard input,\nstandard output and standard error file handles, respectively. Valid values\nare PIPE, an existing file descriptor (a positive integer), an\nexisting file object, and None. PIPE indicates that a new pipe\nto the child should be created. With the default settings of None, no\nredirection will occur; the child’s file handles will be inherited from the\nparent. Additionally, stderr can be STDOUT, which indicates that\nthe stderr data from the child process should be captured into the same file\nhandle as for stdout.

\n

If preexec_fn is set to a callable object, this object will be called in the\nchild process just before the child is executed. (Unix only)

\n

If close_fds is true, all file descriptors except 0, 1 and\n2 will be closed before the child process is executed. (Unix only).\nOr, on Windows, if close_fds is true then no handles will be inherited by the\nchild process. Note that on Windows, you cannot set close_fds to true and\nalso redirect the standard handles by setting stdin, stdout or stderr.

\n

If shell is True, the specified command will be executed through the\nshell.

\n
\n

Warning

\n

Enabling this option can be a security hazard if combined with untrusted\ninput. See the warning under Frequently Used Arguments\nfor details.

\n
\n

If cwd is not None, the child’s current directory will be changed to cwd\nbefore it is executed. Note that this directory is not considered when\nsearching the executable, so you can’t specify the program’s path relative to\ncwd.

\n

If env is not None, it must be a mapping that defines the environment\nvariables for the new process; these are used instead of inheriting the current\nprocess’ environment, which is the default behavior.

\n
\n

Note

\n

If specified, env must provide any variables required\nfor the program to execute. On Windows, in order to run a\nside-by-side assembly the specified env must include a valid\nSystemRoot.

\n
\n

If universal_newlines is True, the file objects stdout and stderr are\nopened as text files, but lines may be terminated by any of '\\n', the Unix\nend-of-line convention, '\\r', the old Macintosh convention or '\\r\\n', the\nWindows convention. All of these external representations are seen as '\\n'\nby the Python program.

\n
\n

Note

\n

This feature is only available if Python is built with universal newline\nsupport (the default). Also, the newlines attribute of the file objects\nstdout, stdin and stderr are not updated by the\ncommunicate() method.

\n
\n

If given, startupinfo will be a STARTUPINFO object, which is\npassed to the underlying CreateProcess function.\ncreationflags, if given, can be CREATE_NEW_CONSOLE or\nCREATE_NEW_PROCESS_GROUP. (Windows only)

\n
\n\n
\n
\n

17.1.1.3. Exceptions

\n

Exceptions raised in the child process, before the new program has started to\nexecute, will be re-raised in the parent. Additionally, the exception object\nwill have one extra attribute called child_traceback, which is a string\ncontaining traceback information from the child’s point of view.

\n

The most common exception raised is OSError. This occurs, for example,\nwhen trying to execute a non-existent file. Applications should prepare for\nOSError exceptions.

\n

A ValueError will be raised if Popen is called with invalid\narguments.

\n

check_call() and check_output() will raise\nCalledProcessError if the called process returns a non-zero return\ncode.

\n
\n
\n

17.1.1.4. Security

\n

Unlike some other popen functions, this implementation will never call a\nsystem shell implicitly. This means that all characters, including shell\nmetacharacters, can safely be passed to child processes. Obviously, if the\nshell is invoked explicitly, then it is the application’s responsibility to\nensure that all whitespace and metacharacters are quoted appropriately.

\n
\n
\n
\n

17.1.2. Popen Objects

\n

Instances of the Popen class have the following methods:

\n
\n
\nPopen.poll()
\n
Check if child process has terminated. Set and return returncode\nattribute.
\n\n
\n
\nPopen.wait()
\n

Wait for child process to terminate. Set and return returncode\nattribute.

\n
\n

Warning

\n

This will deadlock when using stdout=PIPE and/or\nstderr=PIPE and the child process generates enough output to\na pipe such that it blocks waiting for the OS pipe buffer to\naccept more data. Use communicate() to avoid that.

\n
\n
\n\n
\n
\nPopen.communicate(input=None)
\n

Interact with process: Send data to stdin. Read data from stdout and stderr,\nuntil end-of-file is reached. Wait for process to terminate. The optional\ninput argument should be a string to be sent to the child process, or\nNone, if no data should be sent to the child.

\n

communicate() returns a tuple (stdoutdata, stderrdata).

\n

Note that if you want to send data to the process’s stdin, you need to create\nthe Popen object with stdin=PIPE. Similarly, to get anything other than\nNone in the result tuple, you need to give stdout=PIPE and/or\nstderr=PIPE too.

\n
\n

Note

\n

The data read is buffered in memory, so do not use this method if the data\nsize is large or unlimited.

\n
\n
\n\n
\n
\nPopen.send_signal(signal)
\n

Sends the signal signal to the child.

\n
\n

Note

\n

On Windows, SIGTERM is an alias for terminate(). CTRL_C_EVENT and\nCTRL_BREAK_EVENT can be sent to processes started with a creationflags\nparameter which includes CREATE_NEW_PROCESS_GROUP.

\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nPopen.terminate()
\n

Stop the child. On Posix OSs the method sends SIGTERM to the\nchild. On Windows the Win32 API function TerminateProcess() is called\nto stop the child.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nPopen.kill()
\n

Kills the child. On Posix OSs the function sends SIGKILL to the child.\nOn Windows kill() is an alias for terminate().

\n

\nNew in version 2.6.

\n
\n\n

The following attributes are also available:

\n
\n

Warning

\n

Use communicate() rather than .stdin.write,\n.stdout.read or .stderr.read to avoid\ndeadlocks due to any of the other OS pipe buffers filling up and blocking the\nchild process.

\n
\n
\n
\nPopen.stdin
\n
If the stdin argument was PIPE, this attribute is a file object\nthat provides input to the child process. Otherwise, it is None.
\n\n
\n
\nPopen.stdout
\n
If the stdout argument was PIPE, this attribute is a file object\nthat provides output from the child process. Otherwise, it is None.
\n\n
\n
\nPopen.stderr
\n
If the stderr argument was PIPE, this attribute is a file object\nthat provides error output from the child process. Otherwise, it is\nNone.
\n\n
\n
\nPopen.pid
\n

The process ID of the child process.

\n

Note that if you set the shell argument to True, this is the process ID\nof the spawned shell.

\n
\n\n
\n
\nPopen.returncode
\n

The child return code, set by poll() and wait() (and indirectly\nby communicate()). A None value indicates that the process\nhasn’t terminated yet.

\n

A negative value -N indicates that the child was terminated by signal\nN (Unix only).

\n
\n\n
\n
\n

17.1.3. Windows Popen Helpers

\n

The STARTUPINFO class and following constants are only available\non Windows.

\n
\n
\nclass subprocess.STARTUPINFO
\n

Partial support of the Windows\nSTARTUPINFO\nstructure is used for Popen creation.

\n
\n
\ndwFlags
\n

A bit field that determines whether certain STARTUPINFO\nattributes are used when the process creates a window.

\n
si = subprocess.STARTUPINFO()\nsi.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW\n
\n
\n
\n\n
\n
\nhStdInput
\n
If dwFlags specifies STARTF_USESTDHANDLES, this attribute\nis the standard input handle for the process. If\nSTARTF_USESTDHANDLES is not specified, the default for standard\ninput is the keyboard buffer.
\n\n
\n
\nhStdOutput
\n
If dwFlags specifies STARTF_USESTDHANDLES, this attribute\nis the standard output handle for the process. Otherwise, this attribute\nis ignored and the default for standard output is the console window’s\nbuffer.
\n\n
\n
\nhStdError
\n
If dwFlags specifies STARTF_USESTDHANDLES, this attribute\nis the standard error handle for the process. Otherwise, this attribute is\nignored and the default for standard error is the console window’s buffer.
\n\n
\n
\nwShowWindow
\n

If dwFlags specifies STARTF_USESHOWWINDOW, this attribute\ncan be any of the values that can be specified in the nCmdShow\nparameter for the\nShowWindow\nfunction, except for SW_SHOWDEFAULT. Otherwise, this attribute is\nignored.

\n

SW_HIDE is provided for this attribute. It is used when\nPopen is called with shell=True.

\n
\n\n
\n\n
\n

17.1.3.1. Constants

\n

The subprocess module exposes the following constants.

\n
\n
\nsubprocess.STD_INPUT_HANDLE
\n
The standard input device. Initially, this is the console input buffer,\nCONIN$.
\n\n
\n
\nsubprocess.STD_OUTPUT_HANDLE
\n
The standard output device. Initially, this is the active console screen\nbuffer, CONOUT$.
\n\n
\n
\nsubprocess.STD_ERROR_HANDLE
\n
The standard error device. Initially, this is the active console screen\nbuffer, CONOUT$.
\n\n
\n
\nsubprocess.SW_HIDE
\n
Hides the window. Another window will be activated.
\n\n
\n
\nsubprocess.STARTF_USESTDHANDLES
\n
Specifies that the STARTUPINFO.hStdInput,\nSTARTUPINFO.hStdOutput, and STARTUPINFO.hStdError attributes\ncontain additional information.
\n\n
\n
\nsubprocess.STARTF_USESHOWWINDOW
\n
Specifies that the STARTUPINFO.wShowWindow attribute contains\nadditional information.
\n\n
\n
\nsubprocess.CREATE_NEW_CONSOLE
\n

The new process has a new console, instead of inheriting its parent’s\nconsole (the default).

\n

This flag is always set when Popen is created with shell=True.

\n
\n\n
\n
\nsubprocess.CREATE_NEW_PROCESS_GROUP
\n

A Popen creationflags parameter to specify that a new process\ngroup will be created. This flag is necessary for using os.kill()\non the subprocess.

\n

This flag is ignored if CREATE_NEW_CONSOLE is specified.

\n
\n\n
\n
\n
\n

17.1.4. Replacing Older Functions with the subprocess Module

\n

In this section, “a becomes b” means that b can be used as a replacement for a.

\n
\n

Note

\n

All “a” functions in this section fail (more or less) silently if the\nexecuted program cannot be found; the “b” replacements raise OSError\ninstead.

\n

In addition, the replacements using check_output() will fail with a\nCalledProcessError if the requested operation produces a non-zero\nreturn code. The output is still available as the output attribute of\nthe raised exception.

\n
\n

In the following examples, we assume that the relevant functions have already\nbeen imported from the subprocess module.

\n
\n

17.1.4.1. Replacing /bin/sh shell backquote

\n
output=`mycmd myarg`\n# becomes\noutput = check_output([\"mycmd\", \"myarg\"])
\n
\n
\n
\n

17.1.4.2. Replacing shell pipeline

\n
output=`dmesg | grep hda`\n# becomes\np1 = Popen([\"dmesg\"], stdout=PIPE)\np2 = Popen([\"grep\", \"hda\"], stdin=p1.stdout, stdout=PIPE)\np1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.\noutput = p2.communicate()[0]
\n
\n

The p1.stdout.close() call after starting the p2 is important in order for p1\nto receive a SIGPIPE if p2 exits before p1.

\n

Alternatively, for trusted input, the shell’s own pipeline support may still\nbe used directly:

\n
\noutput=`dmesg | grep hda`\n# becomes\noutput=check_output(“dmesg | grep hda”, shell=True)
\n
\n
\n

17.1.4.3. Replacing os.system()

\n
sts = os.system("mycmd" + " myarg")\n# becomes\nsts = call("mycmd" + " myarg", shell=True)\n
\n
\n

Notes:

\n
    \n
  • Calling the program through the shell is usually not required.
  • \n
\n

A more realistic example would look like this:

\n
try:\n    retcode = call("mycmd" + " myarg", shell=True)\n    if retcode < 0:\n        print >>sys.stderr, "Child was terminated by signal", -retcode\n    else:\n        print >>sys.stderr, "Child returned", retcode\nexcept OSError, e:\n    print >>sys.stderr, "Execution failed:", e\n
\n
\n
\n
\n

17.1.4.4. Replacing the os.spawn family

\n

P_NOWAIT example:

\n
pid = os.spawnlp(os.P_NOWAIT, \"/bin/mycmd\", \"mycmd\", \"myarg\")\n==>\npid = Popen([\"/bin/mycmd\", \"myarg\"]).pid
\n
\n

P_WAIT example:

\n
retcode = os.spawnlp(os.P_WAIT, \"/bin/mycmd\", \"mycmd\", \"myarg\")\n==>\nretcode = call([\"/bin/mycmd\", \"myarg\"])
\n
\n

Vector example:

\n
os.spawnvp(os.P_NOWAIT, path, args)\n==>\nPopen([path] + args[1:])
\n
\n

Environment example:

\n
os.spawnlpe(os.P_NOWAIT, \"/bin/mycmd\", \"mycmd\", \"myarg\", env)\n==>\nPopen([\"/bin/mycmd\", \"myarg\"], env={\"PATH\": \"/usr/bin\"})
\n
\n
\n
\n

17.1.4.5. Replacing os.popen(), os.popen2(), os.popen3()

\n
pipe = os.popen(\"cmd\", 'r', bufsize)\n==>\npipe = Popen(\"cmd\", shell=True, bufsize=bufsize, stdout=PIPE).stdout
\n
\n
pipe = os.popen(\"cmd\", 'w', bufsize)\n==>\npipe = Popen(\"cmd\", shell=True, bufsize=bufsize, stdin=PIPE).stdin
\n
\n
(child_stdin, child_stdout) = os.popen2(\"cmd\", mode, bufsize)\n==>\np = Popen(\"cmd\", shell=True, bufsize=bufsize,\n          stdin=PIPE, stdout=PIPE, close_fds=True)\n(child_stdin, child_stdout) = (p.stdin, p.stdout)
\n
\n
(child_stdin,\n child_stdout,\n child_stderr) = os.popen3(\"cmd\", mode, bufsize)\n==>\np = Popen(\"cmd\", shell=True, bufsize=bufsize,\n          stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)\n(child_stdin,\n child_stdout,\n child_stderr) = (p.stdin, p.stdout, p.stderr)
\n
\n
(child_stdin, child_stdout_and_stderr) = os.popen4(\"cmd\", mode,\n                                                   bufsize)\n==>\np = Popen(\"cmd\", shell=True, bufsize=bufsize,\n          stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)\n(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)
\n
\n

On Unix, os.popen2, os.popen3 and os.popen4 also accept a sequence as\nthe command to execute, in which case arguments will be passed\ndirectly to the program without shell intervention. This usage can be\nreplaced as follows:

\n
(child_stdin, child_stdout) = os.popen2([\"/bin/ls\", \"-l\"], mode,\n                                        bufsize)\n==>\np = Popen([\"/bin/ls\", \"-l\"], bufsize=bufsize, stdin=PIPE, stdout=PIPE)\n(child_stdin, child_stdout) = (p.stdin, p.stdout)
\n
\n

Return code handling translates as follows:

\n
pipe = os.popen(\"cmd\", 'w')\n...\nrc = pipe.close()\nif rc is not None and rc >> 8:\n    print \"There were some errors\"\n==>\nprocess = Popen(\"cmd\", 'w', shell=True, stdin=PIPE)\n...\nprocess.stdin.close()\nif process.wait() != 0:\n    print \"There were some errors\"
\n
\n
\n
\n

17.1.4.6. Replacing functions from the popen2 module

\n
(child_stdout, child_stdin) = popen2.popen2(\"somestring\", bufsize, mode)\n==>\np = Popen([\"somestring\"], shell=True, bufsize=bufsize,\n          stdin=PIPE, stdout=PIPE, close_fds=True)\n(child_stdout, child_stdin) = (p.stdout, p.stdin)
\n
\n

On Unix, popen2 also accepts a sequence as the command to execute, in\nwhich case arguments will be passed directly to the program without\nshell intervention. This usage can be replaced as follows:

\n
(child_stdout, child_stdin) = popen2.popen2([\"mycmd\", \"myarg\"], bufsize,\n                                            mode)\n==>\np = Popen([\"mycmd\", \"myarg\"], bufsize=bufsize,\n          stdin=PIPE, stdout=PIPE, close_fds=True)\n(child_stdout, child_stdin) = (p.stdout, p.stdin)
\n
\n

popen2.Popen3 and popen2.Popen4 basically work as\nsubprocess.Popen, except that:

\n
    \n
  • Popen raises an exception if the execution fails.
  • \n
  • the capturestderr argument is replaced with the stderr argument.
  • \n
  • stdin=PIPE and stdout=PIPE must be specified.
  • \n
  • popen2 closes all file descriptors by default, but you have to specify\nclose_fds=True with Popen.
  • \n
\n
\n
\n
\n

17.1.5. Notes

\n
\n

17.1.5.1. Converting an argument sequence to a string on Windows

\n

On Windows, an args sequence is converted to a string that can be parsed\nusing the following rules (which correspond to the rules used by the MS C\nruntime):

\n
    \n
  1. Arguments are delimited by white space, which is either a\nspace or a tab.
  2. \n
  3. A string surrounded by double quotation marks is\ninterpreted as a single argument, regardless of white space\ncontained within. A quoted string can be embedded in an\nargument.
  4. \n
  5. A double quotation mark preceded by a backslash is\ninterpreted as a literal double quotation mark.
  6. \n
  7. Backslashes are interpreted literally, unless they\nimmediately precede a double quotation mark.
  8. \n
  9. If backslashes immediately precede a double quotation mark,\nevery pair of backslashes is interpreted as a literal\nbackslash. If the number of backslashes is odd, the last\nbackslash escapes the next double quotation mark as\ndescribed in rule 3.
  10. \n
\n
\n
\n
", "searchableItems": [ { "name": "subprocess.call", "domId": "subprocess_subprocess.call" }, { "name": "subprocess.check_call", "domId": "subprocess_subprocess.check_call" }, { "name": "subprocess.check_output", "domId": "subprocess_subprocess.check_output" }, { "name": "subprocess.Popen", "domId": "subprocess_subprocess.Popen" }, { "name": "subprocess.Popen.communicate", "domId": "subprocess_subprocess.Popen.communicate" }, { "name": "subprocess.Popen.kill", "domId": "subprocess_subprocess.Popen.kill" }, { "name": "subprocess.Popen.poll", "domId": "subprocess_subprocess.Popen.poll" }, { "name": "subprocess.Popen.send_signal", "domId": "subprocess_subprocess.Popen.send_signal" }, { "name": "subprocess.Popen.terminate", "domId": "subprocess_subprocess.Popen.terminate" }, { "name": "subprocess.Popen.wait", "domId": "subprocess_subprocess.Popen.wait" }, { "name": "subprocess.STARTUPINFO", "domId": "subprocess_subprocess.STARTUPINFO" } ] }, { "url": "http://docs.python.org/library/multiprocessing.html", "title": "multiprocessing", "html": "
\n

16.6. multiprocessing — Process-based “threading” interface

\n

\nNew in version 2.6.

\n
\n

16.6.1. Introduction

\n

multiprocessing is a package that supports spawning processes using an\nAPI similar to the threading module. The multiprocessing package\noffers both local and remote concurrency, effectively side-stepping the\nGlobal Interpreter Lock by using subprocesses instead of threads. Due\nto this, the multiprocessing module allows the programmer to fully\nleverage multiple processors on a given machine. It runs on both Unix and\nWindows.

\n
\n

Warning

\n

Some of this package’s functionality requires a functioning shared semaphore\nimplementation on the host operating system. Without one, the\nmultiprocessing.synchronize module will be disabled, and attempts to\nimport it will result in an ImportError. See\nissue 3770 for additional information.

\n
\n
\n

Note

\n

Functionality within this package requires that the __main__ module be\nimportable by the children. This is covered in Programming guidelines\nhowever it is worth pointing out here. This means that some examples, such\nas the multiprocessing.Pool examples will not work in the\ninteractive interpreter. For example:

\n
>>> from multiprocessing import Pool\n>>> p = Pool(5)\n>>> def f(x):\n...     return x*x\n...\n>>> p.map(f, [1,2,3])\nProcess PoolWorker-1:\nProcess PoolWorker-2:\nProcess PoolWorker-3:\nTraceback (most recent call last):\nAttributeError: 'module' object has no attribute 'f'\nAttributeError: 'module' object has no attribute 'f'\nAttributeError: 'module' object has no attribute 'f'\n
\n
\n

(If you try this it will actually output three full tracebacks\ninterleaved in a semi-random fashion, and then you may have to\nstop the master process somehow.)

\n
\n
\n

16.6.1.1. The Process class

\n

In multiprocessing, processes are spawned by creating a Process\nobject and then calling its start() method. Process\nfollows the API of threading.Thread. A trivial example of a\nmultiprocess program is

\n
from multiprocessing import Process\n\ndef f(name):\n    print 'hello', name\n\nif __name__ == '__main__':\n    p = Process(target=f, args=('bob',))\n    p.start()\n    p.join()\n
\n
\n

To show the individual process IDs involved, here is an expanded example:

\n
from multiprocessing import Process\nimport os\n\ndef info(title):\n    print title\n    print 'module name:', __name__\n    print 'parent process:', os.getppid()\n    print 'process id:', os.getpid()\n\ndef f(name):\n    info('function f')\n    print 'hello', name\n\nif __name__ == '__main__':\n    info('main line')\n    p = Process(target=f, args=('bob',))\n    p.start()\n    p.join()\n
\n
\n

For an explanation of why (on Windows) the if __name__ == '__main__' part is\nnecessary, see Programming guidelines.

\n
\n
\n

16.6.1.2. Exchanging objects between processes

\n

multiprocessing supports two types of communication channel between\nprocesses:

\n

Queues

\n
\n

The Queue class is a near clone of Queue.Queue. For\nexample:

\n
from multiprocessing import Process, Queue\n\ndef f(q):\n    q.put([42, None, 'hello'])\n\nif __name__ == '__main__':\n    q = Queue()\n    p = Process(target=f, args=(q,))\n    p.start()\n    print q.get()    # prints "[42, None, 'hello']"\n    p.join()\n
\n
\n

Queues are thread and process safe.

\n
\n

Pipes

\n
\n

The Pipe() function returns a pair of connection objects connected by a\npipe which by default is duplex (two-way). For example:

\n
from multiprocessing import Process, Pipe\n\ndef f(conn):\n    conn.send([42, None, 'hello'])\n    conn.close()\n\nif __name__ == '__main__':\n    parent_conn, child_conn = Pipe()\n    p = Process(target=f, args=(child_conn,))\n    p.start()\n    print parent_conn.recv()   # prints "[42, None, 'hello']"\n    p.join()\n
\n
\n

The two connection objects returned by Pipe() represent the two ends of\nthe pipe. Each connection object has send() and\nrecv() methods (among others). Note that data in a pipe\nmay become corrupted if two processes (or threads) try to read from or write\nto the same end of the pipe at the same time. Of course there is no risk\nof corruption from processes using different ends of the pipe at the same\ntime.

\n
\n
\n
\n

16.6.1.3. Synchronization between processes

\n

multiprocessing contains equivalents of all the synchronization\nprimitives from threading. For instance one can use a lock to ensure\nthat only one process prints to standard output at a time:

\n
from multiprocessing import Process, Lock\n\ndef f(l, i):\n    l.acquire()\n    print 'hello world', i\n    l.release()\n\nif __name__ == '__main__':\n    lock = Lock()\n\n    for num in range(10):\n        Process(target=f, args=(lock, num)).start()\n
\n
\n

Without using the lock output from the different processes is liable to get all\nmixed up.

\n
\n
\n

16.6.1.4. Sharing state between processes

\n

As mentioned above, when doing concurrent programming it is usually best to\navoid using shared state as far as possible. This is particularly true when\nusing multiple processes.

\n

However, if you really do need to use some shared data then\nmultiprocessing provides a couple of ways of doing so.

\n

Shared memory

\n
\n

Data can be stored in a shared memory map using Value or\nArray. For example, the following code

\n
from multiprocessing import Process, Value, Array\n\ndef f(n, a):\n    n.value = 3.1415927\n    for i in range(len(a)):\n        a[i] = -a[i]\n\nif __name__ == '__main__':\n    num = Value('d', 0.0)\n    arr = Array('i', range(10))\n\n    p = Process(target=f, args=(num, arr))\n    p.start()\n    p.join()\n\n    print num.value\n    print arr[:]\n
\n
\n

will print

\n
3.1415927\n[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n
\n
\n

The 'd' and 'i' arguments used when creating num and arr are\ntypecodes of the kind used by the array module: 'd' indicates a\ndouble precision float and 'i' indicates a signed integer. These shared\nobjects will be process and thread-safe.

\n

For more flexibility in using shared memory one can use the\nmultiprocessing.sharedctypes module which supports the creation of\narbitrary ctypes objects allocated from shared memory.

\n
\n

Server process

\n
\n

A manager object returned by Manager() controls a server process which\nholds Python objects and allows other processes to manipulate them using\nproxies.

\n

A manager returned by Manager() will support types list,\ndict, Namespace, Lock, RLock,\nSemaphore, BoundedSemaphore, Condition,\nEvent, Queue, Value and Array. For\nexample,

\n
from multiprocessing import Process, Manager\n\ndef f(d, l):\n    d[1] = '1'\n    d['2'] = 2\n    d[0.25] = None\n    l.reverse()\n\nif __name__ == '__main__':\n    manager = Manager()\n\n    d = manager.dict()\n    l = manager.list(range(10))\n\n    p = Process(target=f, args=(d, l))\n    p.start()\n    p.join()\n\n    print d\n    print l\n
\n
\n

will print

\n
{0.25: None, 1: '1', '2': 2}\n[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n
\n
\n

Server process managers are more flexible than using shared memory objects\nbecause they can be made to support arbitrary object types. Also, a single\nmanager can be shared by processes on different computers over a network.\nThey are, however, slower than using shared memory.

\n
\n
\n
\n

16.6.1.5. Using a pool of workers

\n

The Pool class represents a pool of worker\nprocesses. It has methods which allows tasks to be offloaded to the worker\nprocesses in a few different ways.

\n

For example:

\n
from multiprocessing import Pool\n\ndef f(x):\n    return x*x\n\nif __name__ == '__main__':\n    pool = Pool(processes=4)              # start 4 worker processes\n    result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously\n    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow\n    print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"\n
\n
\n
\n
\n
\n

16.6.2. Reference

\n

The multiprocessing package mostly replicates the API of the\nthreading module.

\n
\n

16.6.2.1. Process and exceptions

\n
\n
\nclass multiprocessing.Process([group[, target[, name[, args[, kwargs]]]]])
\n

Process objects represent activity that is run in a separate process. The\nProcess class has equivalents of all the methods of\nthreading.Thread.

\n

The constructor should always be called with keyword arguments. group\nshould always be None; it exists solely for compatibility with\nthreading.Thread. target is the callable object to be invoked by\nthe run() method. It defaults to None, meaning nothing is\ncalled. name is the process name. By default, a unique name is constructed\nof the form ‘Process-N1:N2:...:Nk‘ where N1,N2,...,Nk is a sequence of integers whose length\nis determined by the generation of the process. args is the argument\ntuple for the target invocation. kwargs is a dictionary of keyword\narguments for the target invocation. By default, no arguments are passed to\ntarget.

\n

If a subclass overrides the constructor, it must make sure it invokes the\nbase class constructor (Process.__init__()) before doing anything else\nto the process.

\n
\n
\nrun()
\n

Method representing the process’s activity.

\n

You may override this method in a subclass. The standard run()\nmethod invokes the callable object passed to the object’s constructor as\nthe target argument, if any, with sequential and keyword arguments taken\nfrom the args and kwargs arguments, respectively.

\n
\n\n
\n
\nstart()
\n

Start the process’s activity.

\n

This must be called at most once per process object. It arranges for the\nobject’s run() method to be invoked in a separate process.

\n
\n\n
\n
\njoin([timeout])
\n

Block the calling thread until the process whose join() method is\ncalled terminates or until the optional timeout occurs.

\n

If timeout is None then there is no timeout.

\n

A process can be joined many times.

\n

A process cannot join itself because this would cause a deadlock. It is\nan error to attempt to join a process before it has been started.

\n
\n\n
\n
\nname
\n

The process’s name.

\n

The name is a string used for identification purposes only. It has no\nsemantics. Multiple processes may be given the same name. The initial\nname is set by the constructor.

\n
\n\n
\n
\nis_alive()
\n

Return whether the process is alive.

\n

Roughly, a process object is alive from the moment the start()\nmethod returns until the child process terminates.

\n
\n\n
\n
\ndaemon
\n

The process’s daemon flag, a Boolean value. This must be set before\nstart() is called.

\n

The initial value is inherited from the creating process.

\n

When a process exits, it attempts to terminate all of its daemonic child\nprocesses.

\n

Note that a daemonic process is not allowed to create child processes.\nOtherwise a daemonic process would leave its children orphaned if it gets\nterminated when its parent process exits. Additionally, these are not\nUnix daemons or services, they are normal processes that will be\nterminated (and not joined) if non-daemonic processes have exited.

\n
\n\n

In addition to the Threading.Thread API, Process objects\nalso support the following attributes and methods:

\n
\n
\npid
\n
Return the process ID. Before the process is spawned, this will be\nNone.
\n\n
\n
\nexitcode
\n
The child’s exit code. This will be None if the process has not yet\nterminated. A negative value -N indicates that the child was terminated\nby signal N.
\n\n
\n
\nauthkey
\n

The process’s authentication key (a byte string).

\n

When multiprocessing is initialized the main process is assigned a\nrandom string using os.random().

\n

When a Process object is created, it will inherit the\nauthentication key of its parent process, although this may be changed by\nsetting authkey to another byte string.

\n

See Authentication keys.

\n
\n\n
\n
\nterminate()
\n

Terminate the process. On Unix this is done using the SIGTERM signal;\non Windows TerminateProcess() is used. Note that exit handlers and\nfinally clauses, etc., will not be executed.

\n

Note that descendant processes of the process will not be terminated –\nthey will simply become orphaned.

\n
\n

Warning

\n

If this method is used when the associated process is using a pipe or\nqueue then the pipe or queue is liable to become corrupted and may\nbecome unusable by other process. Similarly, if the process has\nacquired a lock or semaphore etc. then terminating it is liable to\ncause other processes to deadlock.

\n
\n
\n\n

Note that the start(), join(), is_alive() and\nexit_code methods should only be called by the process that created\nthe process object.

\n

Example usage of some of the methods of Process:

\n
>>> import multiprocessing, time, signal\n>>> p = multiprocessing.Process(target=time.sleep, args=(1000,))\n>>> print p, p.is_alive()\n<Process(Process-1, initial)> False\n>>> p.start()\n>>> print p, p.is_alive()\n<Process(Process-1, started)> True\n>>> p.terminate()\n>>> time.sleep(0.1)\n>>> print p, p.is_alive()\n<Process(Process-1, stopped[SIGTERM])> False\n>>> p.exitcode == -signal.SIGTERM\nTrue\n
\n
\n
\n\n
\n
\nexception multiprocessing.BufferTooShort
\n

Exception raised by Connection.recv_bytes_into() when the supplied\nbuffer object is too small for the message read.

\n

If e is an instance of BufferTooShort then e.args[0] will give\nthe message as a byte string.

\n
\n\n
\n
\n

16.6.2.2. Pipes and Queues

\n

When using multiple processes, one generally uses message passing for\ncommunication between processes and avoids having to use any synchronization\nprimitives like locks.

\n

For passing messages one can use Pipe() (for a connection between two\nprocesses) or a queue (which allows multiple producers and consumers).

\n

The Queue and JoinableQueue types are multi-producer,\nmulti-consumer FIFO queues modelled on the Queue.Queue class in the\nstandard library. They differ in that Queue lacks the\ntask_done() and join() methods introduced\ninto Python 2.5’s Queue.Queue class.

\n

If you use JoinableQueue then you must call\nJoinableQueue.task_done() for each task removed from the queue or else the\nsemaphore used to count the number of unfinished tasks may eventually overflow,\nraising an exception.

\n

Note that one can also create a shared queue by using a manager object – see\nManagers.

\n
\n

Note

\n

multiprocessing uses the usual Queue.Empty and\nQueue.Full exceptions to signal a timeout. They are not available in\nthe multiprocessing namespace so you need to import them from\nQueue.

\n
\n
\n

Warning

\n

If a process is killed using Process.terminate() or os.kill()\nwhile it is trying to use a Queue, then the data in the queue is\nlikely to become corrupted. This may cause any other process to get an\nexception when it tries to use the queue later on.

\n
\n
\n

Warning

\n

As mentioned above, if a child process has put items on a queue (and it has\nnot used JoinableQueue.cancel_join_thread()), then that process will\nnot terminate until all buffered items have been flushed to the pipe.

\n

This means that if you try joining that process you may get a deadlock unless\nyou are sure that all items which have been put on the queue have been\nconsumed. Similarly, if the child process is non-daemonic then the parent\nprocess may hang on exit when it tries to join all its non-daemonic children.

\n

Note that a queue created using a manager does not have this issue. See\nProgramming guidelines.

\n
\n

For an example of the usage of queues for interprocess communication see\nExamples.

\n
\n
\nmultiprocessing.Pipe([duplex])
\n

Returns a pair (conn1, conn2) of Connection objects representing\nthe ends of a pipe.

\n

If duplex is True (the default) then the pipe is bidirectional. If\nduplex is False then the pipe is unidirectional: conn1 can only be\nused for receiving messages and conn2 can only be used for sending\nmessages.

\n
\n\n
\n
\nclass multiprocessing.Queue([maxsize])
\n

Returns a process shared queue implemented using a pipe and a few\nlocks/semaphores. When a process first puts an item on the queue a feeder\nthread is started which transfers objects from a buffer into the pipe.

\n

The usual Queue.Empty and Queue.Full exceptions from the\nstandard library’s Queue module are raised to signal timeouts.

\n

Queue implements all the methods of Queue.Queue except for\ntask_done() and join().

\n
\n
\nqsize()
\n

Return the approximate size of the queue. Because of\nmultithreading/multiprocessing semantics, this number is not reliable.

\n

Note that this may raise NotImplementedError on Unix platforms like\nMac OS X where sem_getvalue() is not implemented.

\n
\n\n
\n
\nempty()
\n
Return True if the queue is empty, False otherwise. Because of\nmultithreading/multiprocessing semantics, this is not reliable.
\n\n
\n
\nfull()
\n
Return True if the queue is full, False otherwise. Because of\nmultithreading/multiprocessing semantics, this is not reliable.
\n\n
\n
\nput(obj[, block[, timeout]])
\n
Put obj into the queue. If the optional argument block is True\n(the default) and timeout is None (the default), block if necessary until\na free slot is available. If timeout is a positive number, it blocks at\nmost timeout seconds and raises the Queue.Full exception if no\nfree slot was available within that time. Otherwise (block is\nFalse), put an item on the queue if a free slot is immediately\navailable, else raise the Queue.Full exception (timeout is\nignored in that case).
\n\n
\n
\nput_nowait(obj)
\n
Equivalent to put(obj, False).
\n\n
\n
\nget([block[, timeout]])
\n
Remove and return an item from the queue. If optional args block is\nTrue (the default) and timeout is None (the default), block if\nnecessary until an item is available. If timeout is a positive number,\nit blocks at most timeout seconds and raises the Queue.Empty\nexception if no item was available within that time. Otherwise (block is\nFalse), return an item if one is immediately available, else raise the\nQueue.Empty exception (timeout is ignored in that case).
\n\n
\n
\nget_nowait()
\n
\nget_no_wait()
\n
Equivalent to get(False).
\n\n

multiprocessing.Queue has a few additional methods not found in\nQueue.Queue. These methods are usually unnecessary for most\ncode:

\n
\n
\nclose()
\n
Indicate that no more data will be put on this queue by the current\nprocess. The background thread will quit once it has flushed all buffered\ndata to the pipe. This is called automatically when the queue is garbage\ncollected.
\n\n
\n
\njoin_thread()
\n

Join the background thread. This can only be used after close() has\nbeen called. It blocks until the background thread exits, ensuring that\nall data in the buffer has been flushed to the pipe.

\n

By default if a process is not the creator of the queue then on exit it\nwill attempt to join the queue’s background thread. The process can call\ncancel_join_thread() to make join_thread() do nothing.

\n
\n\n
\n
\ncancel_join_thread()
\n
Prevent join_thread() from blocking. In particular, this prevents\nthe background thread from being joined automatically when the process\nexits – see join_thread().
\n\n
\n\n
\n
\nclass multiprocessing.JoinableQueue([maxsize])
\n

JoinableQueue, a Queue subclass, is a queue which\nadditionally has task_done() and join() methods.

\n
\n
\ntask_done()
\n

Indicate that a formerly enqueued task is complete. Used by queue consumer\nthreads. For each get() used to fetch a task, a subsequent\ncall to task_done() tells the queue that the processing on the task\nis complete.

\n

If a join() is currently blocking, it will resume when all\nitems have been processed (meaning that a task_done() call was\nreceived for every item that had been put() into the queue).

\n

Raises a ValueError if called more times than there were items\nplaced in the queue.

\n
\n\n
\n
\njoin()
\n

Block until all items in the queue have been gotten and processed.

\n

The count of unfinished tasks goes up whenever an item is added to the\nqueue. The count goes down whenever a consumer thread calls\ntask_done() to indicate that the item was retrieved and all work on\nit is complete. When the count of unfinished tasks drops to zero,\njoin() unblocks.

\n
\n\n
\n\n
\n
\n

16.6.2.3. Miscellaneous

\n
\n
\nmultiprocessing.active_children()
\n

Return list of all live children of the current process.

\n

Calling this has the side affect of “joining” any processes which have\nalready finished.

\n
\n\n
\n
\nmultiprocessing.cpu_count()
\n
Return the number of CPUs in the system. May raise\nNotImplementedError.
\n\n
\n
\nmultiprocessing.current_process()
\n

Return the Process object corresponding to the current process.

\n

An analogue of threading.current_thread().

\n
\n\n
\n
\nmultiprocessing.freeze_support()
\n

Add support for when a program which uses multiprocessing has been\nfrozen to produce a Windows executable. (Has been tested with py2exe,\nPyInstaller and cx_Freeze.)

\n

One needs to call this function straight after the if __name__ ==\n'__main__' line of the main module. For example:

\n
from multiprocessing import Process, freeze_support\n\ndef f():\n    print 'hello world!'\n\nif __name__ == '__main__':\n    freeze_support()\n    Process(target=f).start()\n
\n
\n

If the freeze_support() line is omitted then trying to run the frozen\nexecutable will raise RuntimeError.

\n

If the module is being run normally by the Python interpreter then\nfreeze_support() has no effect.

\n
\n\n
\n
\nmultiprocessing.set_executable()
\n

Sets the path of the Python interpreter to use when starting a child process.\n(By default sys.executable is used). Embedders will probably need to\ndo some thing like

\n
set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))\n
\n
\n

before they can create child processes. (Windows only)

\n
\n\n\n
\n
\n

16.6.2.4. Connection Objects

\n

Connection objects allow the sending and receiving of picklable objects or\nstrings. They can be thought of as message oriented connected sockets.

\n

Connection objects are usually created using Pipe() – see also\nListeners and Clients.

\n
\n
\nclass multiprocessing.Connection
\n
\n
\nsend(obj)
\n

Send an object to the other end of the connection which should be read\nusing recv().

\n

The object must be picklable. Very large pickles (approximately 32 MB+,\nthough it depends on the OS) may raise a ValueError exception.

\n
\n\n
\n
\nrecv()
\n
Return an object sent from the other end of the connection using\nsend(). Blocks until there its something to receive. Raises\nEOFError if there is nothing left to receive\nand the other end was closed.
\n\n
\n
\nfileno()
\n
Return the file descriptor or handle used by the connection.
\n\n
\n
\nclose()
\n

Close the connection.

\n

This is called automatically when the connection is garbage collected.

\n
\n\n
\n
\npoll([timeout])
\n

Return whether there is any data available to be read.

\n

If timeout is not specified then it will return immediately. If\ntimeout is a number then this specifies the maximum time in seconds to\nblock. If timeout is None then an infinite timeout is used.

\n
\n\n
\n
\nsend_bytes(buffer[, offset[, size]])
\n

Send byte data from an object supporting the buffer interface as a\ncomplete message.

\n

If offset is given then data is read from that position in buffer. If\nsize is given then that many bytes will be read from buffer. Very large\nbuffers (approximately 32 MB+, though it depends on the OS) may raise a\nValueError exception

\n
\n\n
\n
\nrecv_bytes([maxlength])
\n

Return a complete message of byte data sent from the other end of the\nconnection as a string. Blocks until there is something to receive.\nRaises EOFError if there is nothing left\nto receive and the other end has closed.

\n

If maxlength is specified and the message is longer than maxlength\nthen IOError is raised and the connection will no longer be\nreadable.

\n
\n\n
\n
\nrecv_bytes_into(buffer[, offset])
\n

Read into buffer a complete message of byte data sent from the other end\nof the connection and return the number of bytes in the message. Blocks\nuntil there is something to receive. Raises\nEOFError if there is nothing left to receive and the other end was\nclosed.

\n

buffer must be an object satisfying the writable buffer interface. If\noffset is given then the message will be written into the buffer from\nthat position. Offset must be a non-negative integer less than the\nlength of buffer (in bytes).

\n

If the buffer is too short then a BufferTooShort exception is\nraised and the complete message is available as e.args[0] where e\nis the exception instance.

\n
\n\n
\n\n

For example:

\n
>>> from multiprocessing import Pipe\n>>> a, b = Pipe()\n>>> a.send([1, 'hello', None])\n>>> b.recv()\n[1, 'hello', None]\n>>> b.send_bytes('thank you')\n>>> a.recv_bytes()\n'thank you'\n>>> import array\n>>> arr1 = array.array('i', range(5))\n>>> arr2 = array.array('i', [0] * 10)\n>>> a.send_bytes(arr1)\n>>> count = b.recv_bytes_into(arr2)\n>>> assert count == len(arr1) * arr1.itemsize\n>>> arr2\narray('i', [0, 1, 2, 3, 4, 0, 0, 0, 0, 0])\n
\n
\n
\n

Warning

\n

The Connection.recv() method automatically unpickles the data it\nreceives, which can be a security risk unless you can trust the process\nwhich sent the message.

\n

Therefore, unless the connection object was produced using Pipe() you\nshould only use the recv() and send()\nmethods after performing some sort of authentication. See\nAuthentication keys.

\n
\n
\n

Warning

\n

If a process is killed while it is trying to read or write to a pipe then\nthe data in the pipe is likely to become corrupted, because it may become\nimpossible to be sure where the message boundaries lie.

\n
\n
\n
\n

16.6.2.5. Synchronization primitives

\n

Generally synchronization primitives are not as necessary in a multiprocess\nprogram as they are in a multithreaded program. See the documentation for\nthreading module.

\n

Note that one can also create synchronization primitives by using a manager\nobject – see Managers.

\n
\n
\nclass multiprocessing.BoundedSemaphore([value])
\n

A bounded semaphore object: a clone of threading.BoundedSemaphore.

\n

(On Mac OS X, this is indistinguishable from Semaphore because\nsem_getvalue() is not implemented on that platform).

\n
\n\n
\n
\nclass multiprocessing.Condition([lock])
\n

A condition variable: a clone of threading.Condition.

\n

If lock is specified then it should be a Lock or RLock\nobject from multiprocessing.

\n
\n\n
\n
\nclass multiprocessing.Event
\n

A clone of threading.Event.\nThis method returns the state of the internal semaphore on exit, so it\nwill always return True except if a timeout is given and the operation\ntimes out.

\n

\nChanged in version 2.7: Previously, the method always returned None.

\n
\n\n
\n
\nclass multiprocessing.Lock
\n
A non-recursive lock object: a clone of threading.Lock.
\n\n
\n
\nclass multiprocessing.RLock
\n
A recursive lock object: a clone of threading.RLock.
\n\n
\n
\nclass multiprocessing.Semaphore([value])
\n
A semaphore object: a clone of threading.Semaphore.
\n\n
\n

Note

\n

The acquire() method of BoundedSemaphore, Lock,\nRLock and Semaphore has a timeout parameter not supported\nby the equivalents in threading. The signature is\nacquire(block=True, timeout=None) with keyword parameters being\nacceptable. If block is True and timeout is not None then it\nspecifies a timeout in seconds. If block is False then timeout is\nignored.

\n

On Mac OS X, sem_timedwait is unsupported, so calling acquire() with\na timeout will emulate that function’s behavior using a sleeping loop.

\n
\n
\n

Note

\n

If the SIGINT signal generated by Ctrl-C arrives while the main thread is\nblocked by a call to BoundedSemaphore.acquire(), Lock.acquire(),\nRLock.acquire(), Semaphore.acquire(), Condition.acquire()\nor Condition.wait() then the call will be immediately interrupted and\nKeyboardInterrupt will be raised.

\n

This differs from the behaviour of threading where SIGINT will be\nignored while the equivalent blocking calls are in progress.

\n
\n
\n
\n

16.6.2.6. Shared ctypes Objects

\n

It is possible to create shared objects using shared memory which can be\ninherited by child processes.

\n
\n
\nmultiprocessing.Value(typecode_or_type, *args[, lock])
\n

Return a ctypes object allocated from shared memory. By default the\nreturn value is actually a synchronized wrapper for the object.

\n

typecode_or_type determines the type of the returned object: it is either a\nctypes type or a one character typecode of the kind used by the array\nmodule. *args is passed on to the constructor for the type.

\n

If lock is True (the default) then a new lock object is created to\nsynchronize access to the value. If lock is a Lock or\nRLock object then that will be used to synchronize access to the\nvalue. If lock is False then access to the returned object will not be\nautomatically protected by a lock, so it will not necessarily be\n“process-safe”.

\n

Note that lock is a keyword-only argument.

\n
\n\n
\n
\nmultiprocessing.Array(typecode_or_type, size_or_initializer, *, lock=True)
\n

Return a ctypes array allocated from shared memory. By default the return\nvalue is actually a synchronized wrapper for the array.

\n

typecode_or_type determines the type of the elements of the returned array:\nit is either a ctypes type or a one character typecode of the kind used by\nthe array module. If size_or_initializer is an integer, then it\ndetermines the length of the array, and the array will be initially zeroed.\nOtherwise, size_or_initializer is a sequence which is used to initialize\nthe array and whose length determines the length of the array.

\n

If lock is True (the default) then a new lock object is created to\nsynchronize access to the value. If lock is a Lock or\nRLock object then that will be used to synchronize access to the\nvalue. If lock is False then access to the returned object will not be\nautomatically protected by a lock, so it will not necessarily be\n“process-safe”.

\n

Note that lock is a keyword only argument.

\n

Note that an array of ctypes.c_char has value and raw\nattributes which allow one to use it to store and retrieve strings.

\n
\n\n
\n

16.6.2.6.1. The multiprocessing.sharedctypes module

\n

The multiprocessing.sharedctypes module provides functions for allocating\nctypes objects from shared memory which can be inherited by child\nprocesses.

\n
\n

Note

\n

Although it is possible to store a pointer in shared memory remember that\nthis will refer to a location in the address space of a specific process.\nHowever, the pointer is quite likely to be invalid in the context of a second\nprocess and trying to dereference the pointer from the second process may\ncause a crash.

\n
\n
\n
\nmultiprocessing.sharedctypes.RawArray(typecode_or_type, size_or_initializer)
\n

Return a ctypes array allocated from shared memory.

\n

typecode_or_type determines the type of the elements of the returned array:\nit is either a ctypes type or a one character typecode of the kind used by\nthe array module. If size_or_initializer is an integer then it\ndetermines the length of the array, and the array will be initially zeroed.\nOtherwise size_or_initializer is a sequence which is used to initialize the\narray and whose length determines the length of the array.

\n

Note that setting and getting an element is potentially non-atomic – use\nArray() instead to make sure that access is automatically synchronized\nusing a lock.

\n
\n\n
\n
\nmultiprocessing.sharedctypes.RawValue(typecode_or_type, *args)
\n

Return a ctypes object allocated from shared memory.

\n

typecode_or_type determines the type of the returned object: it is either a\nctypes type or a one character typecode of the kind used by the array\nmodule. *args is passed on to the constructor for the type.

\n

Note that setting and getting the value is potentially non-atomic – use\nValue() instead to make sure that access is automatically synchronized\nusing a lock.

\n

Note that an array of ctypes.c_char has value and raw\nattributes which allow one to use it to store and retrieve strings – see\ndocumentation for ctypes.

\n
\n\n
\n
\nmultiprocessing.sharedctypes.Array(typecode_or_type, size_or_initializer, *args[, lock])
\n

The same as RawArray() except that depending on the value of lock a\nprocess-safe synchronization wrapper may be returned instead of a raw ctypes\narray.

\n

If lock is True (the default) then a new lock object is created to\nsynchronize access to the value. If lock is a Lock or\nRLock object then that will be used to synchronize access to the\nvalue. If lock is False then access to the returned object will not be\nautomatically protected by a lock, so it will not necessarily be\n“process-safe”.

\n

Note that lock is a keyword-only argument.

\n
\n\n
\n
\nmultiprocessing.sharedctypes.Value(typecode_or_type, *args[, lock])
\n

The same as RawValue() except that depending on the value of lock a\nprocess-safe synchronization wrapper may be returned instead of a raw ctypes\nobject.

\n

If lock is True (the default) then a new lock object is created to\nsynchronize access to the value. If lock is a Lock or\nRLock object then that will be used to synchronize access to the\nvalue. If lock is False then access to the returned object will not be\nautomatically protected by a lock, so it will not necessarily be\n“process-safe”.

\n

Note that lock is a keyword-only argument.

\n
\n\n
\n
\nmultiprocessing.sharedctypes.copy(obj)
\n
Return a ctypes object allocated from shared memory which is a copy of the\nctypes object obj.
\n\n
\n
\nmultiprocessing.sharedctypes.synchronized(obj[, lock])
\n

Return a process-safe wrapper object for a ctypes object which uses lock to\nsynchronize access. If lock is None (the default) then a\nmultiprocessing.RLock object is created automatically.

\n

A synchronized wrapper will have two methods in addition to those of the\nobject it wraps: get_obj() returns the wrapped object and\nget_lock() returns the lock object used for synchronization.

\n

Note that accessing the ctypes object through the wrapper can be a lot slower\nthan accessing the raw ctypes object.

\n
\n\n

The table below compares the syntax for creating shared ctypes objects from\nshared memory with the normal ctypes syntax. (In the table MyStruct is some\nsubclass of ctypes.Structure.)

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ctypessharedctypes using typesharedctypes using typecode
c_double(2.4)RawValue(c_double, 2.4)RawValue(‘d’, 2.4)
MyStruct(4, 6)RawValue(MyStruct, 4, 6) 
(c_short * 7)()RawArray(c_short, 7)RawArray(‘h’, 7)
(c_int * 3)(9, 2, 8)RawArray(c_int, (9, 2, 8))RawArray(‘i’, (9, 2, 8))
\n

Below is an example where a number of ctypes objects are modified by a child\nprocess:

\n
from multiprocessing import Process, Lock\nfrom multiprocessing.sharedctypes import Value, Array\nfrom ctypes import Structure, c_double\n\nclass Point(Structure):\n    _fields_ = [('x', c_double), ('y', c_double)]\n\ndef modify(n, x, s, A):\n    n.value **= 2\n    x.value **= 2\n    s.value = s.value.upper()\n    for a in A:\n        a.x **= 2\n        a.y **= 2\n\nif __name__ == '__main__':\n    lock = Lock()\n\n    n = Value('i', 7)\n    x = Value(c_double, 1.0/3.0, lock=False)\n    s = Array('c', 'hello world', lock=lock)\n    A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)\n\n    p = Process(target=modify, args=(n, x, s, A))\n    p.start()\n    p.join()\n\n    print n.value\n    print x.value\n    print s.value\n    print [(a.x, a.y) for a in A]\n
\n
\n

The results printed are

\n
49\n0.1111111111111111\nHELLO WORLD\n[(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)]\n
\n
\n
\n
\n
\n

16.6.2.7. Managers

\n

Managers provide a way to create data which can be shared between different\nprocesses. A manager object controls a server process which manages shared\nobjects. Other processes can access the shared objects by using proxies.

\n
\n
\nmultiprocessing.Manager()
\n
Returns a started SyncManager object which\ncan be used for sharing objects between processes. The returned manager\nobject corresponds to a spawned child process and has methods which will\ncreate shared objects and return corresponding proxies.
\n\n

Manager processes will be shutdown as soon as they are garbage collected or\ntheir parent process exits. The manager classes are defined in the\nmultiprocessing.managers module:

\n
\n
\nclass multiprocessing.managers.BaseManager([address[, authkey]])
\n

Create a BaseManager object.

\n

Once created one should call start() or get_server().serve_forever() to ensure\nthat the manager object refers to a started manager process.

\n

address is the address on which the manager process listens for new\nconnections. If address is None then an arbitrary one is chosen.

\n

authkey is the authentication key which will be used to check the validity\nof incoming connections to the server process. If authkey is None then\ncurrent_process().authkey. Otherwise authkey is used and it\nmust be a string.

\n
\n
\nstart([initializer[, initargs]])
\n
Start a subprocess to start the manager. If initializer is not None\nthen the subprocess will call initializer(*initargs) when it starts.
\n\n
\n
\nget_server()
\n

Returns a Server object which represents the actual server under\nthe control of the Manager. The Server object supports the\nserve_forever() method:

\n
>>> from multiprocessing.managers import BaseManager\n>>> manager = BaseManager(address=('', 50000), authkey='abc')\n>>> server = manager.get_server()\n>>> server.serve_forever()\n
\n
\n

Server additionally has an address attribute.

\n
\n\n
\n
\nconnect()
\n

Connect a local manager object to a remote manager process:

\n
>>> from multiprocessing.managers import BaseManager\n>>> m = BaseManager(address=('127.0.0.1', 5000), authkey='abc')\n>>> m.connect()\n
\n
\n
\n\n
\n
\nshutdown()
\n

Stop the process used by the manager. This is only available if\nstart() has been used to start the server process.

\n

This can be called multiple times.

\n
\n\n
\n
\nregister(typeid[, callable[, proxytype[, exposed[, method_to_typeid[, create_method]]]]])
\n

A classmethod which can be used for registering a type or callable with\nthe manager class.

\n

typeid is a “type identifier” which is used to identify a particular\ntype of shared object. This must be a string.

\n

callable is a callable used for creating objects for this type\nidentifier. If a manager instance will be created using the\nfrom_address() classmethod or if the create_method argument is\nFalse then this can be left as None.

\n

proxytype is a subclass of BaseProxy which is used to create\nproxies for shared objects with this typeid. If None then a proxy\nclass is created automatically.

\n

exposed is used to specify a sequence of method names which proxies for\nthis typeid should be allowed to access using\nBaseProxy._callMethod(). (If exposed is None then\nproxytype._exposed_ is used instead if it exists.) In the case\nwhere no exposed list is specified, all “public methods” of the shared\nobject will be accessible. (Here a “public method” means any attribute\nwhich has a __call__() method and whose name does not begin with\n'_'.)

\n

method_to_typeid is a mapping used to specify the return type of those\nexposed methods which should return a proxy. It maps method names to\ntypeid strings. (If method_to_typeid is None then\nproxytype._method_to_typeid_ is used instead if it exists.) If a\nmethod’s name is not a key of this mapping or if the mapping is None\nthen the object returned by the method will be copied by value.

\n

create_method determines whether a method should be created with name\ntypeid which can be used to tell the server process to create a new\nshared object and return a proxy for it. By default it is True.

\n
\n\n

BaseManager instances also have one read-only property:

\n
\n
\naddress
\n
The address used by the manager.
\n\n
\n\n
\n
\nclass multiprocessing.managers.SyncManager
\n

A subclass of BaseManager which can be used for the synchronization\nof processes. Objects of this type are returned by\nmultiprocessing.Manager().

\n

It also supports creation of shared lists and dictionaries.

\n
\n
\nBoundedSemaphore([value])
\n
Create a shared threading.BoundedSemaphore object and return a\nproxy for it.
\n\n
\n
\nCondition([lock])
\n

Create a shared threading.Condition object and return a proxy for\nit.

\n

If lock is supplied then it should be a proxy for a\nthreading.Lock or threading.RLock object.

\n
\n\n
\n
\nEvent()
\n
Create a shared threading.Event object and return a proxy for it.
\n\n
\n
\nLock()
\n
Create a shared threading.Lock object and return a proxy for it.
\n\n
\n
\nNamespace()
\n
Create a shared Namespace object and return a proxy for it.
\n\n
\n
\nQueue([maxsize])
\n
Create a shared Queue.Queue object and return a proxy for it.
\n\n
\n
\nRLock()
\n
Create a shared threading.RLock object and return a proxy for it.
\n\n
\n
\nSemaphore([value])
\n
Create a shared threading.Semaphore object and return a proxy for\nit.
\n\n
\n
\nArray(typecode, sequence)
\n
Create an array and return a proxy for it.
\n\n
\n
\nValue(typecode, value)
\n
Create an object with a writable value attribute and return a proxy\nfor it.
\n\n
\n
\ndict()
\n
\ndict(mapping)
\n
\ndict(sequence)
\n
Create a shared dict object and return a proxy for it.
\n\n
\n
\nlist()
\n
\nlist(sequence)
\n
Create a shared list object and return a proxy for it.
\n\n
\n

Note

\n

Modifications to mutable values or items in dict and list proxies will not\nbe propagated through the manager, because the proxy has no way of knowing\nwhen its values or items are modified. To modify such an item, you can\nre-assign the modified object to the container proxy:

\n
# create a list proxy and append a mutable object (a dictionary)\nlproxy = manager.list()\nlproxy.append({})\n# now mutate the dictionary\nd = lproxy[0]\nd['a'] = 1\nd['b'] = 2\n# at this point, the changes to d are not yet synced, but by\n# reassigning the dictionary, the proxy is notified of the change\nlproxy[0] = d\n
\n
\n
\n
\n\n
\n

16.6.2.7.1. Namespace objects

\n

A namespace object has no public methods, but does have writable attributes.\nIts representation shows the values of its attributes.

\n

However, when using a proxy for a namespace object, an attribute beginning with\n'_' will be an attribute of the proxy and not an attribute of the referent:

\n
>>> manager = multiprocessing.Manager()\n>>> Global = manager.Namespace()\n>>> Global.x = 10\n>>> Global.y = 'hello'\n>>> Global._z = 12.3    # this is an attribute of the proxy\n>>> print Global\nNamespace(x=10, y='hello')\n
\n
\n
\n
\n

16.6.2.7.2. Customized managers

\n

To create one’s own manager, one creates a subclass of BaseManager and\nuses the register() classmethod to register new types or\ncallables with the manager class. For example:

\n
from multiprocessing.managers import BaseManager\n\nclass MathsClass(object):\n    def add(self, x, y):\n        return x + y\n    def mul(self, x, y):\n        return x * y\n\nclass MyManager(BaseManager):\n    pass\n\nMyManager.register('Maths', MathsClass)\n\nif __name__ == '__main__':\n    manager = MyManager()\n    manager.start()\n    maths = manager.Maths()\n    print maths.add(4, 3)         # prints 7\n    print maths.mul(7, 8)         # prints 56\n
\n
\n
\n
\n

16.6.2.7.3. Using a remote manager

\n

It is possible to run a manager server on one machine and have clients use it\nfrom other machines (assuming that the firewalls involved allow it).

\n

Running the following commands creates a server for a single shared queue which\nremote clients can access:

\n
>>> from multiprocessing.managers import BaseManager\n>>> import Queue\n>>> queue = Queue.Queue()\n>>> class QueueManager(BaseManager): pass\n>>> QueueManager.register('get_queue', callable=lambda:queue)\n>>> m = QueueManager(address=('', 50000), authkey='abracadabra')\n>>> s = m.get_server()\n>>> s.serve_forever()\n
\n
\n

One client can access the server as follows:

\n
>>> from multiprocessing.managers import BaseManager\n>>> class QueueManager(BaseManager): pass\n>>> QueueManager.register('get_queue')\n>>> m = QueueManager(address=('foo.bar.org', 50000), authkey='abracadabra')\n>>> m.connect()\n>>> queue = m.get_queue()\n>>> queue.put('hello')\n
\n
\n

Another client can also use it:

\n
>>> from multiprocessing.managers import BaseManager\n>>> class QueueManager(BaseManager): pass\n>>> QueueManager.register('get_queue')\n>>> m = QueueManager(address=('foo.bar.org', 50000), authkey='abracadabra')\n>>> m.connect()\n>>> queue = m.get_queue()\n>>> queue.get()\n'hello'\n
\n
\n

Local processes can also access that queue, using the code from above on the\nclient to access it remotely:

\n
>>> from multiprocessing import Process, Queue\n>>> from multiprocessing.managers import BaseManager\n>>> class Worker(Process):\n...     def __init__(self, q):\n...         self.q = q\n...         super(Worker, self).__init__()\n...     def run(self):\n...         self.q.put('local hello')\n...\n>>> queue = Queue()\n>>> w = Worker(queue)\n>>> w.start()\n>>> class QueueManager(BaseManager): pass\n...\n>>> QueueManager.register('get_queue', callable=lambda: queue)\n>>> m = QueueManager(address=('', 50000), authkey='abracadabra')\n>>> s = m.get_server()\n>>> s.serve_forever()\n
\n
\n
\n
\n
\n

16.6.2.8. Proxy Objects

\n

A proxy is an object which refers to a shared object which lives (presumably)\nin a different process. The shared object is said to be the referent of the\nproxy. Multiple proxy objects may have the same referent.

\n

A proxy object has methods which invoke corresponding methods of its referent\n(although not every method of the referent will necessarily be available through\nthe proxy). A proxy can usually be used in most of the same ways that its\nreferent can:

\n
>>> from multiprocessing import Manager\n>>> manager = Manager()\n>>> l = manager.list([i*i for i in range(10)])\n>>> print l\n[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n>>> print repr(l)\n<ListProxy object, typeid 'list' at 0x...>\n>>> l[4]\n16\n>>> l[2:5]\n[4, 9, 16]\n
\n
\n

Notice that applying str() to a proxy will return the representation of\nthe referent, whereas applying repr() will return the representation of\nthe proxy.

\n

An important feature of proxy objects is that they are picklable so they can be\npassed between processes. Note, however, that if a proxy is sent to the\ncorresponding manager’s process then unpickling it will produce the referent\nitself. This means, for example, that one shared object can contain a second:

\n
>>> a = manager.list()\n>>> b = manager.list()\n>>> a.append(b)         # referent of a now contains referent of b\n>>> print a, b\n[[]] []\n>>> b.append('hello')\n>>> print a, b\n[['hello']] ['hello']\n
\n
\n
\n

Note

\n

The proxy types in multiprocessing do nothing to support comparisons\nby value. So, for instance, we have:

\n
>>> manager.list([1,2,3]) == [1,2,3]\nFalse\n
\n
\n

One should just use a copy of the referent instead when making comparisons.

\n
\n
\n
\nclass multiprocessing.managers.BaseProxy
\n

Proxy objects are instances of subclasses of BaseProxy.

\n
\n
\n_callmethod(methodname[, args[, kwds]])
\n

Call and return the result of a method of the proxy’s referent.

\n

If proxy is a proxy whose referent is obj then the expression

\n
proxy._callmethod(methodname, args, kwds)\n
\n
\n

will evaluate the expression

\n
getattr(obj, methodname)(*args, **kwds)\n
\n
\n

in the manager’s process.

\n

The returned value will be a copy of the result of the call or a proxy to\na new shared object – see documentation for the method_to_typeid\nargument of BaseManager.register().

\n

If an exception is raised by the call, then is re-raised by\n_callmethod(). If some other exception is raised in the manager’s\nprocess then this is converted into a RemoteError exception and is\nraised by _callmethod().

\n

Note in particular that an exception will be raised if methodname has\nnot been exposed

\n

An example of the usage of _callmethod():

\n
>>> l = manager.list(range(10))\n>>> l._callmethod('__len__')\n10\n>>> l._callmethod('__getslice__', (2, 7))   # equiv to `l[2:7]`\n[2, 3, 4, 5, 6]\n>>> l._callmethod('__getitem__', (20,))     # equiv to `l[20]`\nTraceback (most recent call last):\n...\nIndexError: list index out of range\n
\n
\n
\n\n
\n
\n_getvalue()
\n

Return a copy of the referent.

\n

If the referent is unpicklable then this will raise an exception.

\n
\n\n
\n
\n__repr__()
\n
Return a representation of the proxy object.
\n\n
\n
\n__str__()
\n
Return the representation of the referent.
\n\n
\n\n
\n

16.6.2.8.1. Cleanup

\n

A proxy object uses a weakref callback so that when it gets garbage collected it\nderegisters itself from the manager which owns its referent.

\n

A shared object gets deleted from the manager process when there are no longer\nany proxies referring to it.

\n
\n
\n
\n

16.6.2.9. Process Pools

\n

One can create a pool of processes which will carry out tasks submitted to it\nwith the Pool class.

\n
\n
\nclass multiprocessing.Pool([processes[, initializer[, initargs[, maxtasksperchild]]]])
\n

A process pool object which controls a pool of worker processes to which jobs\ncan be submitted. It supports asynchronous results with timeouts and\ncallbacks and has a parallel map implementation.

\n

processes is the number of worker processes to use. If processes is\nNone then the number returned by cpu_count() is used. If\ninitializer is not None then each worker process will call\ninitializer(*initargs) when it starts.

\n

\nNew in version 2.7: maxtasksperchild is the number of tasks a worker process can complete\nbefore it will exit and be replaced with a fresh worker process, to enable\nunused resources to be freed. The default maxtasksperchild is None, which\nmeans worker processes will live as long as the pool.

\n
\n

Note

\n

Worker processes within a Pool typically live for the complete\nduration of the Pool’s work queue. A frequent pattern found in other\nsystems (such as Apache, mod_wsgi, etc) to free resources held by\nworkers is to allow a worker within a pool to complete only a set\namount of work before being exiting, being cleaned up and a new\nprocess spawned to replace the old one. The maxtasksperchild\nargument to the Pool exposes this ability to the end user.

\n
\n
\n
\napply(func[, args[, kwds]])
\n
Equivalent of the apply() built-in function. It blocks until the\nresult is ready, so apply_async() is better suited for performing\nwork in parallel. Additionally, func is only executed in one of the\nworkers of the pool.
\n\n
\n
\napply_async(func[, args[, kwds[, callback]]])
\n

A variant of the apply() method which returns a result object.

\n

If callback is specified then it should be a callable which accepts a\nsingle argument. When the result becomes ready callback is applied to\nit (unless the call failed). callback should complete immediately since\notherwise the thread which handles the results will get blocked.

\n
\n\n
\n
\nmap(func, iterable[, chunksize])
\n

A parallel equivalent of the map() built-in function (it supports only\none iterable argument though). It blocks until the result is ready.

\n

This method chops the iterable into a number of chunks which it submits to\nthe process pool as separate tasks. The (approximate) size of these\nchunks can be specified by setting chunksize to a positive integer.

\n
\n\n
\n
\nmap_async(func, iterable[, chunksize[, callback]])
\n

A variant of the map() method which returns a result object.

\n

If callback is specified then it should be a callable which accepts a\nsingle argument. When the result becomes ready callback is applied to\nit (unless the call failed). callback should complete immediately since\notherwise the thread which handles the results will get blocked.

\n
\n\n
\n
\nimap(func, iterable[, chunksize])
\n

An equivalent of itertools.imap().

\n

The chunksize argument is the same as the one used by the map()\nmethod. For very long iterables using a large value for chunksize can\nmake the job complete much faster than using the default value of\n1.

\n

Also if chunksize is 1 then the next() method of the iterator\nreturned by the imap() method has an optional timeout parameter:\nnext(timeout) will raise multiprocessing.TimeoutError if the\nresult cannot be returned within timeout seconds.

\n
\n\n
\n
\nimap_unordered(func, iterable[, chunksize])
\n
The same as imap() except that the ordering of the results from the\nreturned iterator should be considered arbitrary. (Only when there is\nonly one worker process is the order guaranteed to be “correct”.)
\n\n
\n
\nclose()
\n
Prevents any more tasks from being submitted to the pool. Once all the\ntasks have been completed the worker processes will exit.
\n\n
\n
\nterminate()
\n
Stops the worker processes immediately without completing outstanding\nwork. When the pool object is garbage collected terminate() will be\ncalled immediately.
\n\n
\n
\njoin()
\n
Wait for the worker processes to exit. One must call close() or\nterminate() before using join().
\n\n
\n\n
\n
\nclass multiprocessing.pool.AsyncResult
\n

The class of the result returned by Pool.apply_async() and\nPool.map_async().

\n
\n
\nget([timeout])
\n
Return the result when it arrives. If timeout is not None and the\nresult does not arrive within timeout seconds then\nmultiprocessing.TimeoutError is raised. If the remote call raised\nan exception then that exception will be reraised by get().
\n\n
\n
\nwait([timeout])
\n
Wait until the result is available or until timeout seconds pass.
\n\n
\n
\nready()
\n
Return whether the call has completed.
\n\n
\n
\nsuccessful()
\n
Return whether the call completed without raising an exception. Will\nraise AssertionError if the result is not ready.
\n\n
\n\n

The following example demonstrates the use of a pool:

\n
from multiprocessing import Pool\n\ndef f(x):\n    return x*x\n\nif __name__ == '__main__':\n    pool = Pool(processes=4)              # start 4 worker processes\n\n    result = pool.apply_async(f, (10,))    # evaluate "f(10)" asynchronously\n    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow\n\n    print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"\n\n    it = pool.imap(f, range(10))\n    print it.next()                       # prints "0"\n    print it.next()                       # prints "1"\n    print it.next(timeout=1)              # prints "4" unless your computer is *very* slow\n\n    import time\n    result = pool.apply_async(time.sleep, (10,))\n    print result.get(timeout=1)           # raises TimeoutError\n
\n
\n
\n
\n

16.6.2.10. Listeners and Clients

\n

Usually message passing between processes is done using queues or by using\nConnection objects returned by Pipe().

\n

However, the multiprocessing.connection module allows some extra\nflexibility. It basically gives a high level message oriented API for dealing\nwith sockets or Windows named pipes, and also has support for digest\nauthentication using the hmac module.

\n
\n
\nmultiprocessing.connection.deliver_challenge(connection, authkey)
\n

Send a randomly generated message to the other end of the connection and wait\nfor a reply.

\n

If the reply matches the digest of the message using authkey as the key\nthen a welcome message is sent to the other end of the connection. Otherwise\nAuthenticationError is raised.

\n
\n\n
\n
\nmultiprocessing.connection.answerChallenge(connection, authkey)
\n

Receive a message, calculate the digest of the message using authkey as the\nkey, and then send the digest back.

\n

If a welcome message is not received, then AuthenticationError is\nraised.

\n
\n\n
\n
\nmultiprocessing.connection.Client(address[, family[, authenticate[, authkey]]])
\n

Attempt to set up a connection to the listener which is using address\naddress, returning a Connection.

\n

The type of the connection is determined by family argument, but this can\ngenerally be omitted since it can usually be inferred from the format of\naddress. (See Address Formats)

\n

If authenticate is True or authkey is a string then digest\nauthentication is used. The key used for authentication will be either\nauthkey or current_process().authkey) if authkey is None.\nIf authentication fails then AuthenticationError is raised. See\nAuthentication keys.

\n
\n\n
\n
\nclass multiprocessing.connection.Listener([address[, family[, backlog[, authenticate[, authkey]]]]])
\n

A wrapper for a bound socket or Windows named pipe which is ‘listening’ for\nconnections.

\n

address is the address to be used by the bound socket or named pipe of the\nlistener object.

\n
\n

Note

\n

If an address of ‘0.0.0.0’ is used, the address will not be a connectable\nend point on Windows. If you require a connectable end-point,\nyou should use ‘127.0.0.1’.

\n
\n

family is the type of socket (or named pipe) to use. This can be one of\nthe strings 'AF_INET' (for a TCP socket), 'AF_UNIX' (for a Unix\ndomain socket) or 'AF_PIPE' (for a Windows named pipe). Of these only\nthe first is guaranteed to be available. If family is None then the\nfamily is inferred from the format of address. If address is also\nNone then a default is chosen. This default is the family which is\nassumed to be the fastest available. See\nAddress Formats. Note that if family is\n'AF_UNIX' and address is None then the socket will be created in a\nprivate temporary directory created using tempfile.mkstemp().

\n

If the listener object uses a socket then backlog (1 by default) is passed\nto the listen() method of the socket once it has been bound.

\n

If authenticate is True (False by default) or authkey is not\nNone then digest authentication is used.

\n

If authkey is a string then it will be used as the authentication key;\notherwise it must be None.

\n

If authkey is None and authenticate is True then\ncurrent_process().authkey is used as the authentication key. If\nauthkey is None and authenticate is False then no\nauthentication is done. If authentication fails then\nAuthenticationError is raised. See Authentication keys.

\n
\n
\naccept()
\n
Accept a connection on the bound socket or named pipe of the listener\nobject and return a Connection object. If authentication is\nattempted and fails, then AuthenticationError is raised.
\n\n
\n
\nclose()
\n
Close the bound socket or named pipe of the listener object. This is\ncalled automatically when the listener is garbage collected. However it\nis advisable to call it explicitly.
\n\n

Listener objects have the following read-only properties:

\n
\n
\naddress
\n
The address which is being used by the Listener object.
\n\n
\n
\nlast_accepted
\n
The address from which the last accepted connection came. If this is\nunavailable then it is None.
\n\n
\n\n

The module defines two exceptions:

\n
\n
\nexception multiprocessing.connection.AuthenticationError
\n
Exception raised when there is an authentication error.
\n\n

Examples

\n

The following server code creates a listener which uses 'secret password' as\nan authentication key. It then waits for a connection and sends some data to\nthe client:

\n
from multiprocessing.connection import Listener\nfrom array import array\n\naddress = ('localhost', 6000)     # family is deduced to be 'AF_INET'\nlistener = Listener(address, authkey='secret password')\n\nconn = listener.accept()\nprint 'connection accepted from', listener.last_accepted\n\nconn.send([2.25, None, 'junk', float])\n\nconn.send_bytes('hello')\n\nconn.send_bytes(array('i', [42, 1729]))\n\nconn.close()\nlistener.close()\n
\n
\n

The following code connects to the server and receives some data from the\nserver:

\n
from multiprocessing.connection import Client\nfrom array import array\n\naddress = ('localhost', 6000)\nconn = Client(address, authkey='secret password')\n\nprint conn.recv()                 # => [2.25, None, 'junk', float]\n\nprint conn.recv_bytes()            # => 'hello'\n\narr = array('i', [0, 0, 0, 0, 0])\nprint conn.recv_bytes_into(arr)     # => 8\nprint arr                         # => array('i', [42, 1729, 0, 0, 0])\n\nconn.close()\n
\n
\n
\n

16.6.2.10.1. Address Formats

\n
    \n
  • An 'AF_INET' address is a tuple of the form (hostname, port) where\nhostname is a string and port is an integer.

    \n
  • \n
  • An 'AF_UNIX' address is a string representing a filename on the\nfilesystem.

    \n
  • \n
  • \n
    An 'AF_PIPE' address is a string of the form
    \n

    r'\\\\.\\pipe\\PipeName'. To use Client() to connect to a named\npipe on a remote computer called ServerName one should use an address of the\nform r'\\\\ServerName\\pipe\\PipeName' instead.

    \n
    \n
    \n
  • \n
\n

Note that any string beginning with two backslashes is assumed by default to be\nan 'AF_PIPE' address rather than an 'AF_UNIX' address.

\n
\n
\n
\n

16.6.2.11. Authentication keys

\n

When one uses Connection.recv(), the data received is automatically\nunpickled. Unfortunately unpickling data from an untrusted source is a security\nrisk. Therefore Listener and Client() use the hmac module\nto provide digest authentication.

\n

An authentication key is a string which can be thought of as a password: once a\nconnection is established both ends will demand proof that the other knows the\nauthentication key. (Demonstrating that both ends are using the same key does\nnot involve sending the key over the connection.)

\n

If authentication is requested but do authentication key is specified then the\nreturn value of current_process().authkey is used (see\nProcess). This value will automatically inherited by\nany Process object that the current process creates.\nThis means that (by default) all processes of a multi-process program will share\na single authentication key which can be used when setting up connections\nbetween themselves.

\n

Suitable authentication keys can also be generated by using os.urandom().

\n
\n
\n

16.6.2.12. Logging

\n

Some support for logging is available. Note, however, that the logging\npackage does not use process shared locks so it is possible (depending on the\nhandler type) for messages from different processes to get mixed up.

\n
\n
\nmultiprocessing.get_logger()
\n

Returns the logger used by multiprocessing. If necessary, a new one\nwill be created.

\n

When first created the logger has level logging.NOTSET and no\ndefault handler. Messages sent to this logger will not by default propagate\nto the root logger.

\n

Note that on Windows child processes will only inherit the level of the\nparent process’s logger – any other customization of the logger will not be\ninherited.

\n
\n\n
\n
\nmultiprocessing.log_to_stderr()
\n
This function performs a call to get_logger() but in addition to\nreturning the logger created by get_logger, it adds a handler which sends\noutput to sys.stderr using format\n'[%(levelname)s/%(processName)s] %(message)s'.
\n\n

Below is an example session with logging turned on:

\n
>>> import multiprocessing, logging\n>>> logger = multiprocessing.log_to_stderr()\n>>> logger.setLevel(logging.INFO)\n>>> logger.warning('doomed')\n[WARNING/MainProcess] doomed\n>>> m = multiprocessing.Manager()\n[INFO/SyncManager-...] child process calling self.run()\n[INFO/SyncManager-...] created temp directory /.../pymp-...\n[INFO/SyncManager-...] manager serving at '/.../listener-...'\n>>> del m\n[INFO/MainProcess] sending shutdown message to manager\n[INFO/SyncManager-...] manager exiting with exitcode 0\n
\n
\n

In addition to having these two logging functions, the multiprocessing also\nexposes two additional logging level attributes. These are SUBWARNING\nand SUBDEBUG. The table below illustrates where theses fit in the\nnormal level hierarchy.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
LevelNumeric value
SUBWARNING25
SUBDEBUG5
\n

For a full table of logging levels, see the logging module.

\n

These additional logging levels are used primarily for certain debug messages\nwithin the multiprocessing module. Below is the same example as above, except\nwith SUBDEBUG enabled:

\n
>>> import multiprocessing, logging\n>>> logger = multiprocessing.log_to_stderr()\n>>> logger.setLevel(multiprocessing.SUBDEBUG)\n>>> logger.warning('doomed')\n[WARNING/MainProcess] doomed\n>>> m = multiprocessing.Manager()\n[INFO/SyncManager-...] child process calling self.run()\n[INFO/SyncManager-...] created temp directory /.../pymp-...\n[INFO/SyncManager-...] manager serving at '/.../pymp-djGBXN/listener-...'\n>>> del m\n[SUBDEBUG/MainProcess] finalizer calling ...\n[INFO/MainProcess] sending shutdown message to manager\n[DEBUG/SyncManager-...] manager received shutdown message\n[SUBDEBUG/SyncManager-...] calling <Finalize object, callback=unlink, ...\n[SUBDEBUG/SyncManager-...] finalizer calling <built-in function unlink> ...\n[SUBDEBUG/SyncManager-...] calling <Finalize object, dead>\n[SUBDEBUG/SyncManager-...] finalizer calling <function rmtree at 0x5aa730> ...\n[INFO/SyncManager-...] manager exiting with exitcode 0\n
\n
\n
\n
\n

16.6.2.13. The multiprocessing.dummy module

\n

multiprocessing.dummy replicates the API of multiprocessing but is\nno more than a wrapper around the threading module.

\n
\n
\n
\n

16.6.3. Programming guidelines

\n

There are certain guidelines and idioms which should be adhered to when using\nmultiprocessing.

\n
\n

16.6.3.1. All platforms

\n

Avoid shared state

\n
\n

As far as possible one should try to avoid shifting large amounts of data\nbetween processes.

\n

It is probably best to stick to using queues or pipes for communication\nbetween processes rather than using the lower level synchronization\nprimitives from the threading module.

\n
\n

Picklability

\n
\nEnsure that the arguments to the methods of proxies are picklable.
\n

Thread safety of proxies

\n
\n

Do not use a proxy object from more than one thread unless you protect it\nwith a lock.

\n

(There is never a problem with different processes using the same proxy.)

\n
\n

Joining zombie processes

\n
\nOn Unix when a process finishes but has not been joined it becomes a zombie.\nThere should never be very many because each time a new process starts (or\nactive_children() is called) all completed processes which have not\nyet been joined will be joined. Also calling a finished process’s\nProcess.is_alive() will join the process. Even so it is probably good\npractice to explicitly join all the processes that you start.
\n

Better to inherit than pickle/unpickle

\n
\nOn Windows many types from multiprocessing need to be picklable so\nthat child processes can use them. However, one should generally avoid\nsending shared objects to other processes using pipes or queues. Instead\nyou should arrange the program so that a process which needs access to a\nshared resource created elsewhere can inherit it from an ancestor process.
\n

Avoid terminating processes

\n
\n

Using the Process.terminate() method to stop a process is liable to\ncause any shared resources (such as locks, semaphores, pipes and queues)\ncurrently being used by the process to become broken or unavailable to other\nprocesses.

\n

Therefore it is probably best to only consider using\nProcess.terminate() on processes which never use any shared resources.

\n
\n

Joining processes that use queues

\n
\n

Bear in mind that a process that has put items in a queue will wait before\nterminating until all the buffered items are fed by the “feeder” thread to\nthe underlying pipe. (The child process can call the\nQueue.cancel_join_thread() method of the queue to avoid this behaviour.)

\n

This means that whenever you use a queue you need to make sure that all\nitems which have been put on the queue will eventually be removed before the\nprocess is joined. Otherwise you cannot be sure that processes which have\nput items on the queue will terminate. Remember also that non-daemonic\nprocesses will be automatically be joined.

\n

An example which will deadlock is the following:

\n
from multiprocessing import Process, Queue\n\ndef f(q):\n    q.put('X' * 1000000)\n\nif __name__ == '__main__':\n    queue = Queue()\n    p = Process(target=f, args=(queue,))\n    p.start()\n    p.join()                    # this deadlocks\n    obj = queue.get()\n
\n
\n

A fix here would be to swap the last two lines round (or simply remove the\np.join() line).

\n
\n

Explicitly pass resources to child processes

\n
\n

On Unix a child process can make use of a shared resource created in a\nparent process using a global resource. However, it is better to pass the\nobject as an argument to the constructor for the child process.

\n

Apart from making the code (potentially) compatible with Windows this also\nensures that as long as the child process is still alive the object will not\nbe garbage collected in the parent process. This might be important if some\nresource is freed when the object is garbage collected in the parent\nprocess.

\n

So for instance

\n
from multiprocessing import Process, Lock\n\ndef f():\n    ... do something using "lock" ...\n\nif __name__ == '__main__':\n   lock = Lock()\n   for i in range(10):\n        Process(target=f).start()\n
\n
\n

should be rewritten as

\n
from multiprocessing import Process, Lock\n\ndef f(l):\n    ... do something using "l" ...\n\nif __name__ == '__main__':\n   lock = Lock()\n   for i in range(10):\n        Process(target=f, args=(lock,)).start()\n
\n
\n
\n

Beware of replacing sys.stdin with a “file like object”

\n
\n

multiprocessing originally unconditionally called:

\n
os.close(sys.stdin.fileno())\n
\n
\n

in the multiprocessing.Process._bootstrap() method — this resulted\nin issues with processes-in-processes. This has been changed to:

\n
sys.stdin.close()\nsys.stdin = open(os.devnull)\n
\n
\n

Which solves the fundamental issue of processes colliding with each other\nresulting in a bad file descriptor error, but introduces a potential danger\nto applications which replace sys.stdin() with a “file-like object”\nwith output buffering. This danger is that if multiple processes call\nclose() on this file-like object, it could result in the same\ndata being flushed to the object multiple times, resulting in corruption.

\n

If you write a file-like object and implement your own caching, you can\nmake it fork-safe by storing the pid whenever you append to the cache,\nand discarding the cache when the pid changes. For example:

\n
@property\ndef cache(self):\n    pid = os.getpid()\n    if pid != self._pid:\n        self._pid = pid\n        self._cache = []\n    return self._cache\n
\n
\n

For more information, see issue 5155, issue 5313 and issue 5331

\n
\n
\n
\n

16.6.3.2. Windows

\n

Since Windows lacks os.fork() it has a few extra restrictions:

\n

More picklability

\n
\n

Ensure that all arguments to Process.__init__() are picklable. This\nmeans, in particular, that bound or unbound methods cannot be used directly\nas the target argument on Windows — just define a function and use\nthat instead.

\n

Also, if you subclass Process then make sure that instances will be\npicklable when the Process.start() method is called.

\n
\n

Global variables

\n
\n

Bear in mind that if code run in a child process tries to access a global\nvariable, then the value it sees (if any) may not be the same as the value\nin the parent process at the time that Process.start() was called.

\n

However, global variables which are just module level constants cause no\nproblems.

\n
\n

Safe importing of main module

\n
\n

Make sure that the main module can be safely imported by a new Python\ninterpreter without causing unintended side effects (such a starting a new\nprocess).

\n

For example, under Windows running the following module would fail with a\nRuntimeError:

\n
from multiprocessing import Process\n\ndef foo():\n    print 'hello'\n\np = Process(target=foo)\np.start()\n
\n
\n

Instead one should protect the “entry point” of the program by using if\n__name__ == '__main__': as follows:

\n
from multiprocessing import Process, freeze_support\n\ndef foo():\n    print 'hello'\n\nif __name__ == '__main__':\n    freeze_support()\n    p = Process(target=foo)\n    p.start()\n
\n
\n

(The freeze_support() line can be omitted if the program will be run\nnormally instead of frozen.)

\n

This allows the newly spawned Python interpreter to safely import the module\nand then run the module’s foo() function.

\n

Similar restrictions apply if a pool or manager is created in the main\nmodule.

\n
\n
\n
\n
\n

16.6.4. Examples

\n

Demonstration of how to create and use customized managers and proxies:

\n
#\n# This module shows how to use arbitrary callables with a subclass of\n# `BaseManager`.\n#\n# Copyright (c) 2006-2008, R Oudkerk\n# All rights reserved.\n#\n\nfrom multiprocessing import freeze_support\nfrom multiprocessing.managers import BaseManager, BaseProxy\nimport operator\n\n##\n\nclass Foo(object):\n    def f(self):\n        print 'you called Foo.f()'\n    def g(self):\n        print 'you called Foo.g()'\n    def _h(self):\n        print 'you called Foo._h()'\n\n# A simple generator function\ndef baz():\n    for i in xrange(10):\n        yield i*i\n\n# Proxy type for generator objects\nclass GeneratorProxy(BaseProxy):\n    _exposed_ = ('next', '__next__')\n    def __iter__(self):\n        return self\n    def next(self):\n        return self._callmethod('next')\n    def __next__(self):\n        return self._callmethod('__next__')\n\n# Function to return the operator module\ndef get_operator_module():\n    return operator\n\n##\n\nclass MyManager(BaseManager):\n    pass\n\n# register the Foo class; make `f()` and `g()` accessible via proxy\nMyManager.register('Foo1', Foo)\n\n# register the Foo class; make `g()` and `_h()` accessible via proxy\nMyManager.register('Foo2', Foo, exposed=('g', '_h'))\n\n# register the generator function baz; use `GeneratorProxy` to make proxies\nMyManager.register('baz', baz, proxytype=GeneratorProxy)\n\n# register get_operator_module(); make public functions accessible via proxy\nMyManager.register('operator', get_operator_module)\n\n##\n\ndef test():\n    manager = MyManager()\n    manager.start()\n\n    print '-' * 20\n\n    f1 = manager.Foo1()\n    f1.f()\n    f1.g()\n    assert not hasattr(f1, '_h')\n    assert sorted(f1._exposed_) == sorted(['f', 'g'])\n\n    print '-' * 20\n\n    f2 = manager.Foo2()\n    f2.g()\n    f2._h()\n    assert not hasattr(f2, 'f')\n    assert sorted(f2._exposed_) == sorted(['g', '_h'])\n\n    print '-' * 20\n\n    it = manager.baz()\n    for i in it:\n        print '<%d>'  i,\n    print\n\n    print '-' * 20\n\n    op = manager.operator()\n    print 'op.add(23, 45) =', op.add(23, 45)\n    print 'op.pow(2, 94) =', op.pow(2, 94)\n    print 'op.getslice(range(10), 2, 6) =', op.getslice(range(10), 2, 6)\n    print 'op.repeat(range(5), 3) =', op.repeat(range(5), 3)\n    print 'op._exposed_ =', op._exposed_\n\n##\n\nif __name__ == '__main__':\n    freeze_support()\n    test()\n
\n
\n

Using Pool:

\n
#\n# A test of `multiprocessing.Pool` class\n#\n# Copyright (c) 2006-2008, R Oudkerk\n# All rights reserved.\n#\n\nimport multiprocessing\nimport time\nimport random\nimport sys\n\n#\n# Functions used by test code\n#\n\ndef calculate(func, args):\n    result = func(*args)\n    return '%s says that %s%s = %s'  (\n        multiprocessing.current_process().name,\n        func.__name__, args, result\n        )\n\ndef calculatestar(args):\n    return calculate(*args)\n\ndef mul(a, b):\n    time.sleep(0.5*random.random())\n    return a * b\n\ndef plus(a, b):\n    time.sleep(0.5*random.random())\n    return a + b\n\ndef f(x):\n    return 1.0 / (x-5.0)\n\ndef pow3(x):\n    return x**3\n\ndef noop(x):\n    pass\n\n#\n# Test code\n#\n\ndef test():\n    print 'cpu_count() = %d\\n'  multiprocessing.cpu_count()\n\n    #\n    # Create pool\n    #\n\n    PROCESSES = 4\n    print 'Creating pool with %d processes\\n'  PROCESSES\n    pool = multiprocessing.Pool(PROCESSES)\n    print 'pool = %s'  pool\n    print\n\n    #\n    # Tests\n    #\n\n    TASKS = [(mul, (i, 7)) for i in range(10)] + \\\n            [(plus, (i, 8)) for i in range(10)]\n\n    results = [pool.apply_async(calculate, t) for t in TASKS]\n    imap_it = pool.imap(calculatestar, TASKS)\n    imap_unordered_it = pool.imap_unordered(calculatestar, TASKS)\n\n    print 'Ordered results using pool.apply_async():'\n    for r in results:\n        print '\\t', r.get()\n    print\n\n    print 'Ordered results using pool.imap():'\n    for x in imap_it:\n        print '\\t', x\n    print\n\n    print 'Unordered results using pool.imap_unordered():'\n    for x in imap_unordered_it:\n        print '\\t', x\n    print\n\n    print 'Ordered results using pool.map() --- will block till complete:'\n    for x in pool.map(calculatestar, TASKS):\n        print '\\t', x\n    print\n\n    #\n    # Simple benchmarks\n    #\n\n    N = 100000\n    print 'def pow3(x): return x**3'\n\n    t = time.time()\n    A = map(pow3, xrange(N))\n    print '\\tmap(pow3, xrange(%d)):\\n\\t\\t%s seconds'  \\\n          (N, time.time() - t)\n\n    t = time.time()\n    B = pool.map(pow3, xrange(N))\n    print '\\tpool.map(pow3, xrange(%d)):\\n\\t\\t%s seconds'  \\\n          (N, time.time() - t)\n\n    t = time.time()\n    C = list(pool.imap(pow3, xrange(N), chunksize=N//8))\n    print '\\tlist(pool.imap(pow3, xrange(%d), chunksize=%d)):\\n\\t\\t%s' \\\n          ' seconds'  (N, N//8, time.time() - t)\n\n    assert A == B == C, (len(A), len(B), len(C))\n    print\n\n    L = [None] * 1000000\n    print 'def noop(x): pass'\n    print 'L = [None] * 1000000'\n\n    t = time.time()\n    A = map(noop, L)\n    print '\\tmap(noop, L):\\n\\t\\t%s seconds'  \\\n          (time.time() - t)\n\n    t = time.time()\n    B = pool.map(noop, L)\n    print '\\tpool.map(noop, L):\\n\\t\\t%s seconds'  \\\n          (time.time() - t)\n\n    t = time.time()\n    C = list(pool.imap(noop, L, chunksize=len(L)//8))\n    print '\\tlist(pool.imap(noop, L, chunksize=%d)):\\n\\t\\t%s seconds'  \\\n          (len(L)//8, time.time() - t)\n\n    assert A == B == C, (len(A), len(B), len(C))\n    print\n\n    del A, B, C, L\n\n    #\n    # Test error handling\n    #\n\n    print 'Testing error handling:'\n\n    try:\n        print pool.apply(f, (5,))\n    except ZeroDivisionError:\n        print '\\tGot ZeroDivisionError as expected from pool.apply()'\n    else:\n        raise AssertionError('expected ZeroDivisionError')\n\n    try:\n        print pool.map(f, range(10))\n    except ZeroDivisionError:\n        print '\\tGot ZeroDivisionError as expected from pool.map()'\n    else:\n        raise AssertionError('expected ZeroDivisionError')\n\n    try:\n        print list(pool.imap(f, range(10)))\n    except ZeroDivisionError:\n        print '\\tGot ZeroDivisionError as expected from list(pool.imap())'\n    else:\n        raise AssertionError('expected ZeroDivisionError')\n\n    it = pool.imap(f, range(10))\n    for i in range(10):\n        try:\n            x = it.next()\n        except ZeroDivisionError:\n            if i == 5:\n                pass\n        except StopIteration:\n            break\n        else:\n            if i == 5:\n                raise AssertionError('expected ZeroDivisionError')\n\n    assert i == 9\n    print '\\tGot ZeroDivisionError as expected from IMapIterator.next()'\n    print\n\n    #\n    # Testing timeouts\n    #\n\n    print 'Testing ApplyResult.get() with timeout:',\n    res = pool.apply_async(calculate, TASKS[0])\n    while 1:\n        sys.stdout.flush()\n        try:\n            sys.stdout.write('\\n\\t%s'  res.get(0.02))\n            break\n        except multiprocessing.TimeoutError:\n            sys.stdout.write('.')\n    print\n    print\n\n    print 'Testing IMapIterator.next() with timeout:',\n    it = pool.imap(calculatestar, TASKS)\n    while 1:\n        sys.stdout.flush()\n        try:\n            sys.stdout.write('\\n\\t%s'  it.next(0.02))\n        except StopIteration:\n            break\n        except multiprocessing.TimeoutError:\n            sys.stdout.write('.')\n    print\n    print\n\n    #\n    # Testing callback\n    #\n\n    print 'Testing callback:'\n\n    A = []\n    B = [56, 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]\n\n    r = pool.apply_async(mul, (7, 8), callback=A.append)\n    r.wait()\n\n    r = pool.map_async(pow3, range(10), callback=A.extend)\n    r.wait()\n\n    if A == B:\n        print '\\tcallbacks succeeded\\n'\n    else:\n        print '\\t*** callbacks failed\\n\\t\\t%s != %s\\n'  (A, B)\n\n    #\n    # Check there are no outstanding tasks\n    #\n\n    assert not pool._cache, 'cache = %r'  pool._cache\n\n    #\n    # Check close() methods\n    #\n\n    print 'Testing close():'\n\n    for worker in pool._pool:\n        assert worker.is_alive()\n\n    result = pool.apply_async(time.sleep, [0.5])\n    pool.close()\n    pool.join()\n\n    assert result.get() is None\n\n    for worker in pool._pool:\n        assert not worker.is_alive()\n\n    print '\\tclose() succeeded\\n'\n\n    #\n    # Check terminate() method\n    #\n\n    print 'Testing terminate():'\n\n    pool = multiprocessing.Pool(2)\n    DELTA = 0.1\n    ignore = pool.apply(pow3, [2])\n    results = [pool.apply_async(time.sleep, [DELTA]) for i in range(100)]\n    pool.terminate()\n    pool.join()\n\n    for worker in pool._pool:\n        assert not worker.is_alive()\n\n    print '\\tterminate() succeeded\\n'\n\n    #\n    # Check garbage collection\n    #\n\n    print 'Testing garbage collection:'\n\n    pool = multiprocessing.Pool(2)\n    DELTA = 0.1\n    processes = pool._pool\n    ignore = pool.apply(pow3, [2])\n    results = [pool.apply_async(time.sleep, [DELTA]) for i in range(100)]\n\n    results = pool = None\n\n    time.sleep(DELTA * 2)\n\n    for worker in processes:\n        assert not worker.is_alive()\n\n    print '\\tgarbage collection succeeded\\n'\n\n\nif __name__ == '__main__':\n    multiprocessing.freeze_support()\n\n    assert len(sys.argv) in (1, 2)\n\n    if len(sys.argv) == 1 or sys.argv[1] == 'processes':\n        print ' Using processes '.center(79, '-')\n    elif sys.argv[1] == 'threads':\n        print ' Using threads '.center(79, '-')\n        import multiprocessing.dummy as multiprocessing\n    else:\n        print 'Usage:\\n\\t%s [processes | threads]'  sys.argv[0]\n        raise SystemExit(2)\n\n    test()\n
\n
\n

Synchronization types like locks, conditions and queues:

\n
#\n# A test file for the `multiprocessing` package\n#\n# Copyright (c) 2006-2008, R Oudkerk\n# All rights reserved.\n#\n\nimport time, sys, random\nfrom Queue import Empty\n\nimport multiprocessing               # may get overwritten\n\n\n#### TEST_VALUE\n\ndef value_func(running, mutex):\n    random.seed()\n    time.sleep(random.random()*4)\n\n    mutex.acquire()\n    print '\\n\\t\\t\\t' + str(multiprocessing.current_process()) + ' has finished'\n    running.value -= 1\n    mutex.release()\n\ndef test_value():\n    TASKS = 10\n    running = multiprocessing.Value('i', TASKS)\n    mutex = multiprocessing.Lock()\n\n    for i in range(TASKS):\n        p = multiprocessing.Process(target=value_func, args=(running, mutex))\n        p.start()\n\n    while running.value > 0:\n        time.sleep(0.08)\n        mutex.acquire()\n        print running.value,\n        sys.stdout.flush()\n        mutex.release()\n\n    print\n    print 'No more running processes'\n\n\n#### TEST_QUEUE\n\ndef queue_func(queue):\n    for i in range(30):\n        time.sleep(0.5 * random.random())\n        queue.put(i*i)\n    queue.put('STOP')\n\ndef test_queue():\n    q = multiprocessing.Queue()\n\n    p = multiprocessing.Process(target=queue_func, args=(q,))\n    p.start()\n\n    o = None\n    while o != 'STOP':\n        try:\n            o = q.get(timeout=0.3)\n            print o,\n            sys.stdout.flush()\n        except Empty:\n            print 'TIMEOUT'\n\n    print\n\n\n#### TEST_CONDITION\n\ndef condition_func(cond):\n    cond.acquire()\n    print '\\t' + str(cond)\n    time.sleep(2)\n    print '\\tchild is notifying'\n    print '\\t' + str(cond)\n    cond.notify()\n    cond.release()\n\ndef test_condition():\n    cond = multiprocessing.Condition()\n\n    p = multiprocessing.Process(target=condition_func, args=(cond,))\n    print cond\n\n    cond.acquire()\n    print cond\n    cond.acquire()\n    print cond\n\n    p.start()\n\n    print 'main is waiting'\n    cond.wait()\n    print 'main has woken up'\n\n    print cond\n    cond.release()\n    print cond\n    cond.release()\n\n    p.join()\n    print cond\n\n\n#### TEST_SEMAPHORE\n\ndef semaphore_func(sema, mutex, running):\n    sema.acquire()\n\n    mutex.acquire()\n    running.value += 1\n    print running.value, 'tasks are running'\n    mutex.release()\n\n    random.seed()\n    time.sleep(random.random()*2)\n\n    mutex.acquire()\n    running.value -= 1\n    print '%s has finished'  multiprocessing.current_process()\n    mutex.release()\n\n    sema.release()\n\ndef test_semaphore():\n    sema = multiprocessing.Semaphore(3)\n    mutex = multiprocessing.RLock()\n    running = multiprocessing.Value('i', 0)\n\n    processes = [\n        multiprocessing.Process(target=semaphore_func,\n                                args=(sema, mutex, running))\n        for i in range(10)\n        ]\n\n    for p in processes:\n        p.start()\n\n    for p in processes:\n        p.join()\n\n\n#### TEST_JOIN_TIMEOUT\n\ndef join_timeout_func():\n    print '\\tchild sleeping'\n    time.sleep(5.5)\n    print '\\n\\tchild terminating'\n\ndef test_join_timeout():\n    p = multiprocessing.Process(target=join_timeout_func)\n    p.start()\n\n    print 'waiting for process to finish'\n\n    while 1:\n        p.join(timeout=1)\n        if not p.is_alive():\n            break\n        print '.',\n        sys.stdout.flush()\n\n\n#### TEST_EVENT\n\ndef event_func(event):\n    print '\\t%r is waiting'  multiprocessing.current_process()\n    event.wait()\n    print '\\t%r has woken up'  multiprocessing.current_process()\n\ndef test_event():\n    event = multiprocessing.Event()\n\n    processes = [multiprocessing.Process(target=event_func, args=(event,))\n                 for i in range(5)]\n\n    for p in processes:\n        p.start()\n\n    print 'main is sleeping'\n    time.sleep(2)\n\n    print 'main is setting event'\n    event.set()\n\n    for p in processes:\n        p.join()\n\n\n#### TEST_SHAREDVALUES\n\ndef sharedvalues_func(values, arrays, shared_values, shared_arrays):\n    for i in range(len(values)):\n        v = values[i][1]\n        sv = shared_values[i].value\n        assert v == sv\n\n    for i in range(len(values)):\n        a = arrays[i][1]\n        sa = list(shared_arrays[i][:])\n        assert a == sa\n\n    print 'Tests passed'\n\ndef test_sharedvalues():\n    values = [\n        ('i', 10),\n        ('h', -2),\n        ('d', 1.25)\n        ]\n    arrays = [\n        ('i', range(100)),\n        ('d', [0.25 * i for i in range(100)]),\n        ('H', range(1000))\n        ]\n\n    shared_values = [multiprocessing.Value(id, v) for id, v in values]\n    shared_arrays = [multiprocessing.Array(id, a) for id, a in arrays]\n\n    p = multiprocessing.Process(\n        target=sharedvalues_func,\n        args=(values, arrays, shared_values, shared_arrays)\n        )\n    p.start()\n    p.join()\n\n    assert p.exitcode == 0\n\n\n####\n\ndef test(namespace=multiprocessing):\n    global multiprocessing\n\n    multiprocessing = namespace\n\n    for func in [ test_value, test_queue, test_condition,\n                  test_semaphore, test_join_timeout, test_event,\n                  test_sharedvalues ]:\n\n        print '\\n\\t######## %s\\n'  func.__name__\n        func()\n\n    ignore = multiprocessing.active_children()      # cleanup any old processes\n    if hasattr(multiprocessing, '_debug_info'):\n        info = multiprocessing._debug_info()\n        if info:\n            print info\n            raise ValueError('there should be no positive refcounts left')\n\n\nif __name__ == '__main__':\n    multiprocessing.freeze_support()\n\n    assert len(sys.argv) in (1, 2)\n\n    if len(sys.argv) == 1 or sys.argv[1] == 'processes':\n        print ' Using processes '.center(79, '-')\n        namespace = multiprocessing\n    elif sys.argv[1] == 'manager':\n        print ' Using processes and a manager '.center(79, '-')\n        namespace = multiprocessing.Manager()\n        namespace.Process = multiprocessing.Process\n        namespace.current_process = multiprocessing.current_process\n        namespace.active_children = multiprocessing.active_children\n    elif sys.argv[1] == 'threads':\n        print ' Using threads '.center(79, '-')\n        import multiprocessing.dummy as namespace\n    else:\n        print 'Usage:\\n\\t%s [processes | manager | threads]'  sys.argv[0]\n        raise SystemExit(2)\n\n    test(namespace)\n
\n
\n

An example showing how to use queues to feed tasks to a collection of worker\nprocesses and collect the results:

\n
#\n# Simple example which uses a pool of workers to carry out some tasks.\n#\n# Notice that the results will probably not come out of the output\n# queue in the same in the same order as the corresponding tasks were\n# put on the input queue.  If it is important to get the results back\n# in the original order then consider using `Pool.map()` or\n# `Pool.imap()` (which will save on the amount of code needed anyway).\n#\n# Copyright (c) 2006-2008, R Oudkerk\n# All rights reserved.\n#\n\nimport time\nimport random\n\nfrom multiprocessing import Process, Queue, current_process, freeze_support\n\n#\n# Function run by worker processes\n#\n\ndef worker(input, output):\n    for func, args in iter(input.get, 'STOP'):\n        result = calculate(func, args)\n        output.put(result)\n\n#\n# Function used to calculate result\n#\n\ndef calculate(func, args):\n    result = func(*args)\n    return '%s says that %s%s = %s'  \\\n        (current_process().name, func.__name__, args, result)\n\n#\n# Functions referenced by tasks\n#\n\ndef mul(a, b):\n    time.sleep(0.5*random.random())\n    return a * b\n\ndef plus(a, b):\n    time.sleep(0.5*random.random())\n    return a + b\n\n#\n#\n#\n\ndef test():\n    NUMBER_OF_PROCESSES = 4\n    TASKS1 = [(mul, (i, 7)) for i in range(20)]\n    TASKS2 = [(plus, (i, 8)) for i in range(10)]\n\n    # Create queues\n    task_queue = Queue()\n    done_queue = Queue()\n\n    # Submit tasks\n    for task in TASKS1:\n        task_queue.put(task)\n\n    # Start worker processes\n    for i in range(NUMBER_OF_PROCESSES):\n        Process(target=worker, args=(task_queue, done_queue)).start()\n\n    # Get and print results\n    print 'Unordered results:'\n    for i in range(len(TASKS1)):\n        print '\\t', done_queue.get()\n\n    # Add more tasks using `put()`\n    for task in TASKS2:\n        task_queue.put(task)\n\n    # Get and print some more results\n    for i in range(len(TASKS2)):\n        print '\\t', done_queue.get()\n\n    # Tell child processes to stop\n    for i in range(NUMBER_OF_PROCESSES):\n        task_queue.put('STOP')\n\n\nif __name__ == '__main__':\n    freeze_support()\n    test()\n
\n
\n

An example of how a pool of worker processes can each run a\nSimpleHTTPServer.HttpServer instance while sharing a single listening\nsocket.

\n
#\n# Example where a pool of http servers share a single listening socket\n#\n# On Windows this module depends on the ability to pickle a socket\n# object so that the worker processes can inherit a copy of the server\n# object.  (We import `multiprocessing.reduction` to enable this pickling.)\n#\n# Not sure if we should synchronize access to `socket.accept()` method by\n# using a process-shared lock -- does not seem to be necessary.\n#\n# Copyright (c) 2006-2008, R Oudkerk\n# All rights reserved.\n#\n\nimport os\nimport sys\n\nfrom multiprocessing import Process, current_process, freeze_support\nfrom BaseHTTPServer import HTTPServer\nfrom SimpleHTTPServer import SimpleHTTPRequestHandler\n\nif sys.platform == 'win32':\n    import multiprocessing.reduction    # make sockets pickable/inheritable\n\n\ndef note(format, *args):\n    sys.stderr.write('[%s]\\t%s\\n'  (current_process().name, formatargs))\n\n\nclass RequestHandler(SimpleHTTPRequestHandler):\n    # we override log_message() to show which process is handling the request\n    def log_message(self, format, *args):\n        note(format, *args)\n\ndef serve_forever(server):\n    note('starting server')\n    try:\n        server.serve_forever()\n    except KeyboardInterrupt:\n        pass\n\n\ndef runpool(address, number_of_processes):\n    # create a single server object -- children will each inherit a copy\n    server = HTTPServer(address, RequestHandler)\n\n    # create child processes to act as workers\n    for i in range(number_of_processes-1):\n        Process(target=serve_forever, args=(server,)).start()\n\n    # main process also acts as a worker\n    serve_forever(server)\n\n\ndef test():\n    DIR = os.path.join(os.path.dirname(__file__), '..')\n    ADDRESS = ('localhost', 8000)\n    NUMBER_OF_PROCESSES = 4\n\n    print 'Serving at http://%s:%d using %d worker processes'  \\\n          (ADDRESS[0], ADDRESS[1], NUMBER_OF_PROCESSES)\n    print 'To exit press Ctrl-' + ['C', 'Break'][sys.platform=='win32']\n\n    os.chdir(DIR)\n    runpool(ADDRESS, NUMBER_OF_PROCESSES)\n\n\nif __name__ == '__main__':\n    freeze_support()\n    test()\n
\n
\n

Some simple benchmarks comparing multiprocessing with threading:

\n
#\n# Simple benchmarks for the multiprocessing package\n#\n# Copyright (c) 2006-2008, R Oudkerk\n# All rights reserved.\n#\n\nimport time, sys, multiprocessing, threading, Queue, gc\n\nif sys.platform == 'win32':\n    _timer = time.clock\nelse:\n    _timer = time.time\n\ndelta = 1\n\n\n#### TEST_QUEUESPEED\n\ndef queuespeed_func(q, c, iterations):\n    a = '0' * 256\n    c.acquire()\n    c.notify()\n    c.release()\n\n    for i in xrange(iterations):\n        q.put(a)\n\n    q.put('STOP')\n\ndef test_queuespeed(Process, q, c):\n    elapsed = 0\n    iterations = 1\n\n    while elapsed < delta:\n        iterations *= 2\n\n        p = Process(target=queuespeed_func, args=(q, c, iterations))\n        c.acquire()\n        p.start()\n        c.wait()\n        c.release()\n\n        result = None\n        t = _timer()\n\n        while result != 'STOP':\n            result = q.get()\n\n        elapsed = _timer() - t\n\n        p.join()\n\n    print iterations, 'objects passed through the queue in', elapsed, 'seconds'\n    print 'average number/sec:', iterations/elapsed\n\n\n#### TEST_PIPESPEED\n\ndef pipe_func(c, cond, iterations):\n    a = '0' * 256\n    cond.acquire()\n    cond.notify()\n    cond.release()\n\n    for i in xrange(iterations):\n        c.send(a)\n\n    c.send('STOP')\n\ndef test_pipespeed():\n    c, d = multiprocessing.Pipe()\n    cond = multiprocessing.Condition()\n    elapsed = 0\n    iterations = 1\n\n    while elapsed < delta:\n        iterations *= 2\n\n        p = multiprocessing.Process(target=pipe_func,\n                                    args=(d, cond, iterations))\n        cond.acquire()\n        p.start()\n        cond.wait()\n        cond.release()\n\n        result = None\n        t = _timer()\n\n        while result != 'STOP':\n            result = c.recv()\n\n        elapsed = _timer() - t\n        p.join()\n\n    print iterations, 'objects passed through connection in',elapsed,'seconds'\n    print 'average number/sec:', iterations/elapsed\n\n\n#### TEST_SEQSPEED\n\ndef test_seqspeed(seq):\n    elapsed = 0\n    iterations = 1\n\n    while elapsed < delta:\n        iterations *= 2\n\n        t = _timer()\n\n        for i in xrange(iterations):\n            a = seq[5]\n\n        elapsed = _timer()-t\n\n    print iterations, 'iterations in', elapsed, 'seconds'\n    print 'average number/sec:', iterations/elapsed\n\n\n#### TEST_LOCK\n\ndef test_lockspeed(l):\n    elapsed = 0\n    iterations = 1\n\n    while elapsed < delta:\n        iterations *= 2\n\n        t = _timer()\n\n        for i in xrange(iterations):\n            l.acquire()\n            l.release()\n\n        elapsed = _timer()-t\n\n    print iterations, 'iterations in', elapsed, 'seconds'\n    print 'average number/sec:', iterations/elapsed\n\n\n#### TEST_CONDITION\n\ndef conditionspeed_func(c, N):\n    c.acquire()\n    c.notify()\n\n    for i in xrange(N):\n        c.wait()\n        c.notify()\n\n    c.release()\n\ndef test_conditionspeed(Process, c):\n    elapsed = 0\n    iterations = 1\n\n    while elapsed < delta:\n        iterations *= 2\n\n        c.acquire()\n        p = Process(target=conditionspeed_func, args=(c, iterations))\n        p.start()\n\n        c.wait()\n\n        t = _timer()\n\n        for i in xrange(iterations):\n            c.notify()\n            c.wait()\n\n        elapsed = _timer()-t\n\n        c.release()\n        p.join()\n\n    print iterations * 2, 'waits in', elapsed, 'seconds'\n    print 'average number/sec:', iterations * 2 / elapsed\n\n####\n\ndef test():\n    manager = multiprocessing.Manager()\n\n    gc.disable()\n\n    print '\\n\\t######## testing Queue.Queue\\n'\n    test_queuespeed(threading.Thread, Queue.Queue(),\n                    threading.Condition())\n    print '\\n\\t######## testing multiprocessing.Queue\\n'\n    test_queuespeed(multiprocessing.Process, multiprocessing.Queue(),\n                    multiprocessing.Condition())\n    print '\\n\\t######## testing Queue managed by server process\\n'\n    test_queuespeed(multiprocessing.Process, manager.Queue(),\n                    manager.Condition())\n    print '\\n\\t######## testing multiprocessing.Pipe\\n'\n    test_pipespeed()\n\n    print\n\n    print '\\n\\t######## testing list\\n'\n    test_seqspeed(range(10))\n    print '\\n\\t######## testing list managed by server process\\n'\n    test_seqspeed(manager.list(range(10)))\n    print '\\n\\t######## testing Array("i", ..., lock=False)\\n'\n    test_seqspeed(multiprocessing.Array('i', range(10), lock=False))\n    print '\\n\\t######## testing Array("i", ..., lock=True)\\n'\n    test_seqspeed(multiprocessing.Array('i', range(10), lock=True))\n\n    print\n\n    print '\\n\\t######## testing threading.Lock\\n'\n    test_lockspeed(threading.Lock())\n    print '\\n\\t######## testing threading.RLock\\n'\n    test_lockspeed(threading.RLock())\n    print '\\n\\t######## testing multiprocessing.Lock\\n'\n    test_lockspeed(multiprocessing.Lock())\n    print '\\n\\t######## testing multiprocessing.RLock\\n'\n    test_lockspeed(multiprocessing.RLock())\n    print '\\n\\t######## testing lock managed by server process\\n'\n    test_lockspeed(manager.Lock())\n    print '\\n\\t######## testing rlock managed by server process\\n'\n    test_lockspeed(manager.RLock())\n\n    print\n\n    print '\\n\\t######## testing threading.Condition\\n'\n    test_conditionspeed(threading.Thread, threading.Condition())\n    print '\\n\\t######## testing multiprocessing.Condition\\n'\n    test_conditionspeed(multiprocessing.Process, multiprocessing.Condition())\n    print '\\n\\t######## testing condition managed by a server process\\n'\n    test_conditionspeed(multiprocessing.Process, manager.Condition())\n\n    gc.enable()\n\nif __name__ == '__main__':\n    multiprocessing.freeze_support()\n    test()\n
\n
\n
\n
", "searchableItems": [ { "name": "multiprocessing.active_children", "domId": "multiprocessing_multiprocessing.active_children" }, { "name": "multiprocessing.Array", "domId": "multiprocessing_multiprocessing.Array" }, { "name": "multiprocessing.BoundedSemaphore", "domId": "multiprocessing_multiprocessing.BoundedSemaphore" }, { "name": "multiprocessing.Condition", "domId": "multiprocessing_multiprocessing.Condition" }, { "name": "multiprocessing.Connection", "domId": "multiprocessing_multiprocessing.Connection" }, { "name": "multiprocessing.connection.answerChallenge", "domId": "multiprocessing_multiprocessing.connection.answerChallenge" }, { "name": "multiprocessing.connection.Client", "domId": "multiprocessing_multiprocessing.connection.Client" }, { "name": "multiprocessing.Connection.close", "domId": "multiprocessing_multiprocessing.Connection.close" }, { "name": "multiprocessing.connection.deliver_challenge", "domId": "multiprocessing_multiprocessing.connection.deliver_challenge" }, { "name": "multiprocessing.Connection.fileno", "domId": "multiprocessing_multiprocessing.Connection.fileno" }, { "name": "multiprocessing.connection.Listener", "domId": "multiprocessing_multiprocessing.connection.Listener" }, { "name": "multiprocessing.connection.Listener.accept", "domId": "multiprocessing_multiprocessing.connection.Listener.accept" }, { "name": "multiprocessing.connection.Listener.close", "domId": "multiprocessing_multiprocessing.connection.Listener.close" }, { "name": "multiprocessing.Connection.poll", "domId": "multiprocessing_multiprocessing.Connection.poll" }, { "name": "multiprocessing.Connection.recv", "domId": "multiprocessing_multiprocessing.Connection.recv" }, { "name": "multiprocessing.Connection.recv_bytes", "domId": "multiprocessing_multiprocessing.Connection.recv_bytes" }, { "name": "multiprocessing.Connection.recv_bytes_into", "domId": "multiprocessing_multiprocessing.Connection.recv_bytes_into" }, { "name": "multiprocessing.Connection.send", "domId": "multiprocessing_multiprocessing.Connection.send" }, { "name": "multiprocessing.Connection.send_bytes", "domId": "multiprocessing_multiprocessing.Connection.send_bytes" }, { "name": "multiprocessing.cpu_count", "domId": "multiprocessing_multiprocessing.cpu_count" }, { "name": "multiprocessing.current_process", "domId": "multiprocessing_multiprocessing.current_process" }, { "name": "multiprocessing.Event", "domId": "multiprocessing_multiprocessing.Event" }, { "name": "multiprocessing.freeze_support", "domId": "multiprocessing_multiprocessing.freeze_support" }, { "name": "multiprocessing.get_logger", "domId": "multiprocessing_multiprocessing.get_logger" }, { "name": "multiprocessing.JoinableQueue", "domId": "multiprocessing_multiprocessing.JoinableQueue" }, { "name": "multiprocessing.JoinableQueue.join", "domId": "multiprocessing_multiprocessing.JoinableQueue.join" }, { "name": "multiprocessing.JoinableQueue.task_done", "domId": "multiprocessing_multiprocessing.JoinableQueue.task_done" }, { "name": "multiprocessing.Lock", "domId": "multiprocessing_multiprocessing.Lock" }, { "name": "multiprocessing.log_to_stderr", "domId": "multiprocessing_multiprocessing.log_to_stderr" }, { "name": "multiprocessing.managers.BaseManager", "domId": "multiprocessing_multiprocessing.managers.BaseManager" }, { "name": "multiprocessing.managers.BaseManager.connect", "domId": "multiprocessing_multiprocessing.managers.BaseManager.connect" }, { "name": "multiprocessing.managers.BaseManager.get_server", "domId": "multiprocessing_multiprocessing.managers.BaseManager.get_server" }, { "name": "multiprocessing.managers.BaseManager.register", "domId": "multiprocessing_multiprocessing.managers.BaseManager.register" }, { "name": "multiprocessing.managers.BaseManager.shutdown", "domId": "multiprocessing_multiprocessing.managers.BaseManager.shutdown" }, { "name": "multiprocessing.managers.BaseManager.start", "domId": "multiprocessing_multiprocessing.managers.BaseManager.start" }, { "name": "multiprocessing.managers.BaseProxy", "domId": "multiprocessing_multiprocessing.managers.BaseProxy" }, { "name": "multiprocessing.managers.BaseProxy.__repr__", "domId": "multiprocessing_multiprocessing.managers.BaseProxy.__repr__" }, { "name": "multiprocessing.managers.BaseProxy.__str__", "domId": "multiprocessing_multiprocessing.managers.BaseProxy.__str__" }, { "name": "multiprocessing.managers.BaseProxy._callmethod", "domId": "multiprocessing_multiprocessing.managers.BaseProxy._callmethod" }, { "name": "multiprocessing.managers.BaseProxy._getvalue", "domId": "multiprocessing_multiprocessing.managers.BaseProxy._getvalue" }, { "name": "multiprocessing.managers.SyncManager", "domId": "multiprocessing_multiprocessing.managers.SyncManager" }, { "name": "multiprocessing.managers.SyncManager.Array", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Array" }, { "name": "multiprocessing.managers.SyncManager.BoundedSemaphore", "domId": "multiprocessing_multiprocessing.managers.SyncManager.BoundedSemaphore" }, { "name": "multiprocessing.managers.SyncManager.Condition", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Condition" }, { "name": "multiprocessing.managers.SyncManager.dict", "domId": "multiprocessing_multiprocessing.managers.SyncManager.dict" }, { "name": "multiprocessing.managers.SyncManager.Event", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Event" }, { "name": "multiprocessing.managers.SyncManager.list", "domId": "multiprocessing_multiprocessing.managers.SyncManager.list" }, { "name": "multiprocessing.managers.SyncManager.Lock", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Lock" }, { "name": "multiprocessing.managers.SyncManager.Namespace", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Namespace" }, { "name": "multiprocessing.managers.SyncManager.Queue", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Queue" }, { "name": "multiprocessing.managers.SyncManager.RLock", "domId": "multiprocessing_multiprocessing.managers.SyncManager.RLock" }, { "name": "multiprocessing.managers.SyncManager.Semaphore", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Semaphore" }, { "name": "multiprocessing.managers.SyncManager.Value", "domId": "multiprocessing_multiprocessing.managers.SyncManager.Value" }, { "name": "multiprocessing.Pipe", "domId": "multiprocessing_multiprocessing.Pipe" }, { "name": "multiprocessing.pool.AsyncResult", "domId": "multiprocessing_multiprocessing.pool.AsyncResult" }, { "name": "multiprocessing.pool.AsyncResult.get", "domId": "multiprocessing_multiprocessing.pool.AsyncResult.get" }, { "name": "multiprocessing.pool.AsyncResult.ready", "domId": "multiprocessing_multiprocessing.pool.AsyncResult.ready" }, { "name": "multiprocessing.pool.AsyncResult.successful", "domId": "multiprocessing_multiprocessing.pool.AsyncResult.successful" }, { "name": "multiprocessing.pool.AsyncResult.wait", "domId": "multiprocessing_multiprocessing.pool.AsyncResult.wait" }, { "name": "multiprocessing.pool.multiprocessing.Pool", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool" }, { "name": "multiprocessing.pool.multiprocessing.Pool.apply", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.apply" }, { "name": "multiprocessing.pool.multiprocessing.Pool.apply_async", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.apply_async" }, { "name": "multiprocessing.pool.multiprocessing.Pool.close", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.close" }, { "name": "multiprocessing.pool.multiprocessing.Pool.imap", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.imap" }, { "name": "multiprocessing.pool.multiprocessing.Pool.imap_unordered", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.imap_unordered" }, { "name": "multiprocessing.pool.multiprocessing.Pool.join", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.join" }, { "name": "multiprocessing.pool.multiprocessing.Pool.map", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.map" }, { "name": "multiprocessing.pool.multiprocessing.Pool.map_async", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.map_async" }, { "name": "multiprocessing.pool.multiprocessing.Pool.terminate", "domId": "multiprocessing_multiprocessing.pool.multiprocessing.Pool.terminate" }, { "name": "multiprocessing.Process", "domId": "multiprocessing_multiprocessing.Process" }, { "name": "multiprocessing.Process.is_alive", "domId": "multiprocessing_multiprocessing.Process.is_alive" }, { "name": "multiprocessing.Process.join", "domId": "multiprocessing_multiprocessing.Process.join" }, { "name": "multiprocessing.Process.run", "domId": "multiprocessing_multiprocessing.Process.run" }, { "name": "multiprocessing.Process.start", "domId": "multiprocessing_multiprocessing.Process.start" }, { "name": "multiprocessing.Process.terminate", "domId": "multiprocessing_multiprocessing.Process.terminate" }, { "name": "multiprocessing.Queue", "domId": "multiprocessing_multiprocessing.Queue" }, { "name": "multiprocessing.Queue.cancel_join_thread", "domId": "multiprocessing_multiprocessing.Queue.cancel_join_thread" }, { "name": "multiprocessing.Queue.close", "domId": "multiprocessing_multiprocessing.Queue.close" }, { "name": "multiprocessing.Queue.empty", "domId": "multiprocessing_multiprocessing.Queue.empty" }, { "name": "multiprocessing.Queue.full", "domId": "multiprocessing_multiprocessing.Queue.full" }, { "name": "multiprocessing.Queue.get", "domId": "multiprocessing_multiprocessing.Queue.get" }, { "name": "multiprocessing.Queue.get_nowait", "domId": "multiprocessing_multiprocessing.Queue.get_nowait" }, { "name": "multiprocessing.Queue.join_thread", "domId": "multiprocessing_multiprocessing.Queue.join_thread" }, { "name": "multiprocessing.Queue.put", "domId": "multiprocessing_multiprocessing.Queue.put" }, { "name": "multiprocessing.Queue.put_nowait", "domId": "multiprocessing_multiprocessing.Queue.put_nowait" }, { "name": "multiprocessing.Queue.qsize", "domId": "multiprocessing_multiprocessing.Queue.qsize" }, { "name": "multiprocessing.RLock", "domId": "multiprocessing_multiprocessing.RLock" }, { "name": "multiprocessing.Semaphore", "domId": "multiprocessing_multiprocessing.Semaphore" }, { "name": "multiprocessing.set_executable", "domId": "multiprocessing_multiprocessing.set_executable" }, { "name": "multiprocessing.sharedctypes.Array", "domId": "multiprocessing_multiprocessing.sharedctypes.Array" }, { "name": "multiprocessing.sharedctypes.copy", "domId": "multiprocessing_multiprocessing.sharedctypes.copy" }, { "name": "multiprocessing.sharedctypes.multiprocessing.Manager", "domId": "multiprocessing_multiprocessing.sharedctypes.multiprocessing.Manager" }, { "name": "multiprocessing.sharedctypes.RawArray", "domId": "multiprocessing_multiprocessing.sharedctypes.RawArray" }, { "name": "multiprocessing.sharedctypes.RawValue", "domId": "multiprocessing_multiprocessing.sharedctypes.RawValue" }, { "name": "multiprocessing.sharedctypes.synchronized", "domId": "multiprocessing_multiprocessing.sharedctypes.synchronized" }, { "name": "multiprocessing.sharedctypes.Value", "domId": "multiprocessing_multiprocessing.sharedctypes.Value" }, { "name": "multiprocessing.Value", "domId": "multiprocessing_multiprocessing.Value" } ] }, { "url": "http://docs.python.org/library/ssl.html", "title": "ssl", "html": "
\n

17.3. ssl — TLS/SSL wrapper for socket objects

\n

\nNew in version 2.6.

\n

Source code: Lib/ssl.py

\n
\n

This module provides access to Transport Layer Security (often known as “Secure\nSockets Layer”) encryption and peer authentication facilities for network\nsockets, both client-side and server-side. This module uses the OpenSSL\nlibrary. It is available on all modern Unix systems, Windows, Mac OS X, and\nprobably additional platforms, as long as OpenSSL is installed on that platform.

\n
\n

Note

\n

Some behavior may be platform dependent, since calls are made to the\noperating system socket APIs. The installed version of OpenSSL may also\ncause variations in behavior.

\n
\n

This section documents the objects and functions in the ssl module; for more\ngeneral information about TLS, SSL, and certificates, the reader is referred to\nthe documents in the “See Also” section at the bottom.

\n

This module provides a class, ssl.SSLSocket, which is derived from the\nsocket.socket type, and provides a socket-like wrapper that also\nencrypts and decrypts the data going over the socket with SSL. It supports\nadditional read() and write() methods, along with a method,\ngetpeercert(), to retrieve the certificate of the other side of the\nconnection, and a method, cipher(), to retrieve the cipher being used for\nthe secure connection.

\n
\n

17.3.1. Functions, Constants, and Exceptions

\n
\n
\nexception ssl.SSLError
\n
Raised to signal an error from the underlying SSL implementation. This\nsignifies some problem in the higher-level encryption and authentication\nlayer that’s superimposed on the underlying network connection. This error\nis a subtype of socket.error, which in turn is a subtype of\nIOError.
\n\n
\n
\nssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version={see docs}, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True, ciphers=None)
\n

Takes an instance sock of socket.socket, and returns an instance\nof ssl.SSLSocket, a subtype of socket.socket, which wraps\nthe underlying socket in an SSL context. For client-side sockets, the\ncontext construction is lazy; if the underlying socket isn’t connected yet,\nthe context construction will be performed after connect() is called on\nthe socket. For server-side sockets, if the socket has no remote peer, it is\nassumed to be a listening socket, and the server-side SSL wrapping is\nautomatically performed on client connections accepted via the accept()\nmethod. wrap_socket() may raise SSLError.

\n

The keyfile and certfile parameters specify optional files which\ncontain a certificate to be used to identify the local side of the\nconnection. See the discussion of Certificates for more\ninformation on how the certificate is stored in the certfile.

\n

Often the private key is stored in the same file as the certificate; in this\ncase, only the certfile parameter need be passed. If the private key is\nstored in a separate file, both parameters must be used. If the private key\nis stored in the certfile, it should come before the first certificate in\nthe certificate chain:

\n
-----BEGIN RSA PRIVATE KEY-----\n... (private key in base64 encoding) ...\n-----END RSA PRIVATE KEY-----\n-----BEGIN CERTIFICATE-----\n... (certificate in base64 PEM encoding) ...\n-----END CERTIFICATE-----
\n
\n

The parameter server_side is a boolean which identifies whether\nserver-side or client-side behavior is desired from this socket.

\n

The parameter cert_reqs specifies whether a certificate is required from\nthe other side of the connection, and whether it will be validated if\nprovided. It must be one of the three values CERT_NONE\n(certificates ignored), CERT_OPTIONAL (not required, but validated\nif provided), or CERT_REQUIRED (required and validated). If the\nvalue of this parameter is not CERT_NONE, then the ca_certs\nparameter must point to a file of CA certificates.

\n

The ca_certs file contains a set of concatenated “certification\nauthority” certificates, which are used to validate certificates passed from\nthe other end of the connection. See the discussion of\nCertificates for more information about how to arrange the\ncertificates in this file.

\n

The parameter ssl_version specifies which version of the SSL protocol to\nuse. Typically, the server chooses a particular protocol version, and the\nclient must adapt to the server’s choice. Most of the versions are not\ninteroperable with the other versions. If not specified, the default is\nPROTOCOL_SSLv23; it provides the most compatibility with other\nversions.

\n

Here’s a table showing which versions in a client (down the side) can connect\nto which versions in a server (along the top):

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
client / serverSSLv2SSLv3SSLv23TLSv1
SSLv2yesnoyesno
SSLv3noyesyesno
SSLv23yesnoyesno
TLSv1nonoyesyes
\n
\n
\n

Note

\n

Which connections succeed will vary depending on the version of\nOpenSSL. For instance, in some older versions of OpenSSL (such\nas 0.9.7l on OS X 10.4), an SSLv2 client could not connect to an\nSSLv23 server. Another example: beginning with OpenSSL 1.0.0,\nan SSLv23 client will not actually attempt SSLv2 connections\nunless you explicitly enable SSLv2 ciphers; for example, you\nmight specify "ALL" or "SSLv2" as the ciphers parameter\nto enable them.

\n
\n

The ciphers parameter sets the available ciphers for this SSL object.\nIt should be a string in the OpenSSL cipher list format.

\n

The parameter do_handshake_on_connect specifies whether to do the SSL\nhandshake automatically after doing a socket.connect(), or whether the\napplication program will call it explicitly, by invoking the\nSSLSocket.do_handshake() method. Calling\nSSLSocket.do_handshake() explicitly gives the program control over the\nblocking behavior of the socket I/O involved in the handshake.

\n

The parameter suppress_ragged_eofs specifies how the\nSSLSocket.read() method should signal unexpected EOF from the other end\nof the connection. If specified as True (the default), it returns a\nnormal EOF in response to unexpected EOF errors raised from the underlying\nsocket; if False, it will raise the exceptions back to the caller.

\n

\nChanged in version 2.7: New optional argument ciphers.

\n
\n\n
\n
\nssl.RAND_status()
\n
Returns True if the SSL pseudo-random number generator has been seeded with\n‘enough’ randomness, and False otherwise. You can use ssl.RAND_egd()\nand ssl.RAND_add() to increase the randomness of the pseudo-random\nnumber generator.
\n\n
\n
\nssl.RAND_egd(path)
\n

If you are running an entropy-gathering daemon (EGD) somewhere, and path\nis the pathname of a socket connection open to it, this will read 256 bytes\nof randomness from the socket, and add it to the SSL pseudo-random number\ngenerator to increase the security of generated secret keys. This is\ntypically only necessary on systems without better sources of randomness.

\n

See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources\nof entropy-gathering daemons.

\n
\n\n
\n
\nssl.RAND_add(bytes, entropy)
\n
Mixes the given bytes into the SSL pseudo-random number generator. The\nparameter entropy (a float) is a lower bound on the entropy contained in\nstring (so you can always use 0.0). See RFC 1750 for more\ninformation on sources of entropy.
\n\n
\n
\nssl.cert_time_to_seconds(timestring)
\n

Returns a floating-point value containing a normal seconds-after-the-epoch\ntime value, given the time-string representing the “notBefore” or “notAfter”\ndate from a certificate.

\n

Here’s an example:

\n
>>> import ssl\n>>> ssl.cert_time_to_seconds("May  9 00:00:00 2007 GMT")\n1178694000.0\n>>> import time\n>>> time.ctime(ssl.cert_time_to_seconds("May  9 00:00:00 2007 GMT"))\n'Wed May  9 00:00:00 2007'\n>>>\n
\n
\n
\n\n
\n
\nssl.get_server_certificate(addr, ssl_version=PROTOCOL_SSLv3, ca_certs=None)
\n
Given the address addr of an SSL-protected server, as a (hostname,\nport-number) pair, fetches the server’s certificate, and returns it as a\nPEM-encoded string. If ssl_version is specified, uses that version of\nthe SSL protocol to attempt to connect to the server. If ca_certs is\nspecified, it should be a file containing a list of root certificates, the\nsame format as used for the same parameter in wrap_socket(). The call\nwill attempt to validate the server certificate against that set of root\ncertificates, and will fail if the validation attempt fails.
\n\n
\n
\nssl.DER_cert_to_PEM_cert(DER_cert_bytes)
\n
Given a certificate as a DER-encoded blob of bytes, returns a PEM-encoded\nstring version of the same certificate.
\n\n
\n
\nssl.PEM_cert_to_DER_cert(PEM_cert_string)
\n
Given a certificate as an ASCII PEM string, returns a DER-encoded sequence of\nbytes for that same certificate.
\n\n
\n
\nssl.CERT_NONE
\n
Value to pass to the cert_reqs parameter to sslobject() when no\ncertificates will be required or validated from the other side of the socket\nconnection.
\n\n
\n
\nssl.CERT_OPTIONAL
\n
Value to pass to the cert_reqs parameter to sslobject() when no\ncertificates will be required from the other side of the socket connection,\nbut if they are provided, will be validated. Note that use of this setting\nrequires a valid certificate validation file also be passed as a value of the\nca_certs parameter.
\n\n
\n
\nssl.CERT_REQUIRED
\n
Value to pass to the cert_reqs parameter to sslobject() when\ncertificates will be required from the other side of the socket connection.\nNote that use of this setting requires a valid certificate validation file\nalso be passed as a value of the ca_certs parameter.
\n\n
\n
\nssl.PROTOCOL_SSLv2
\n

Selects SSL version 2 as the channel encryption protocol.

\n

This protocol is not available if OpenSSL is compiled with OPENSSL_NO_SSL2\nflag.

\n
\n

Warning

\n

SSL version 2 is insecure. Its use is highly discouraged.

\n
\n
\n\n
\n
\nssl.PROTOCOL_SSLv23
\n
Selects SSL version 2 or 3 as the channel encryption protocol. This is a\nsetting to use with servers for maximum compatibility with the other end of\nan SSL connection, but it may cause the specific ciphers chosen for the\nencryption to be of fairly low quality.
\n\n
\n
\nssl.PROTOCOL_SSLv3
\n
Selects SSL version 3 as the channel encryption protocol. For clients, this\nis the maximally compatible SSL variant.
\n\n
\n
\nssl.PROTOCOL_TLSv1
\n
Selects TLS version 1 as the channel encryption protocol. This is the most\nmodern version, and probably the best choice for maximum protection, if both\nsides can speak it.
\n\n
\n
\nssl.OPENSSL_VERSION
\n

The version string of the OpenSSL library loaded by the interpreter:

\n
>>> ssl.OPENSSL_VERSION\n'OpenSSL 0.9.8k 25 Mar 2009'\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nssl.OPENSSL_VERSION_INFO
\n

A tuple of five integers representing version information about the\nOpenSSL library:

\n
>>> ssl.OPENSSL_VERSION_INFO\n(0, 9, 8, 11, 15)\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nssl.OPENSSL_VERSION_NUMBER
\n

The raw version number of the OpenSSL library, as a single integer:

\n
>>> ssl.OPENSSL_VERSION_NUMBER\n9470143L\n>>> hex(ssl.OPENSSL_VERSION_NUMBER)\n'0x9080bfL'\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\n

17.3.2. SSLSocket Objects

\n
\n
\nSSLSocket.read([nbytes=1024])
\n
Reads up to nbytes bytes from the SSL-encrypted channel and returns them.
\n\n
\n
\nSSLSocket.write(data)
\n
Writes the data to the other side of the connection, using the SSL\nchannel to encrypt. Returns the number of bytes written.
\n\n
\n
\nSSLSocket.getpeercert(binary_form=False)
\n

If there is no certificate for the peer on the other end of the connection,\nreturns None.

\n

If the parameter binary_form is False, and a certificate was\nreceived from the peer, this method returns a dict instance. If the\ncertificate was not validated, the dict is empty. If the certificate was\nvalidated, it returns a dict with the keys subject (the principal for\nwhich the certificate was issued), and notAfter (the time after which the\ncertificate should not be trusted). The certificate was already validated,\nso the notBefore and issuer fields are not returned. If a\ncertificate contains an instance of the Subject Alternative Name extension\n(see RFC 3280), there will also be a subjectAltName key in the\ndictionary.

\n

The “subject” field is a tuple containing the sequence of relative\ndistinguished names (RDNs) given in the certificate’s data structure for the\nprincipal, and each RDN is a sequence of name-value pairs:

\n
{'notAfter': 'Feb 16 16:54:50 2013 GMT',\n 'subject': ((('countryName', u'US'),),\n             (('stateOrProvinceName', u'Delaware'),),\n             (('localityName', u'Wilmington'),),\n             (('organizationName', u'Python Software Foundation'),),\n             (('organizationalUnitName', u'SSL'),),\n             (('commonName', u'somemachine.python.org'),))}\n
\n
\n

If the binary_form parameter is True, and a certificate was\nprovided, this method returns the DER-encoded form of the entire certificate\nas a sequence of bytes, or None if the peer did not provide a\ncertificate. This return value is independent of validation; if validation\nwas required (CERT_OPTIONAL or CERT_REQUIRED), it will have\nbeen validated, but if CERT_NONE was used to establish the\nconnection, the certificate, if present, will not have been validated.

\n
\n\n
\n
\nSSLSocket.cipher()
\n
Returns a three-value tuple containing the name of the cipher being used, the\nversion of the SSL protocol that defines its use, and the number of secret\nbits being used. If no connection has been established, returns None.
\n\n
\n
\nSSLSocket.do_handshake()
\n

Perform a TLS/SSL handshake. If this is used with a non-blocking socket, it\nmay raise SSLError with an arg[0] of SSL_ERROR_WANT_READ\nor SSL_ERROR_WANT_WRITE, in which case it must be called again until\nit completes successfully. For example, to simulate the behavior of a\nblocking socket, one might write:

\n
while True:\n    try:\n        s.do_handshake()\n        break\n    except ssl.SSLError, err:\n        if err.args[0] == ssl.SSL_ERROR_WANT_READ:\n            select.select([s], [], [])\n        elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:\n            select.select([], [s], [])\n        else:\n            raise\n
\n
\n
\n\n
\n
\nSSLSocket.unwrap()
\n
Performs the SSL shutdown handshake, which removes the TLS layer from the\nunderlying socket, and returns the underlying socket object. This can be\nused to go from encrypted operation over a connection to unencrypted. The\nsocket instance returned should always be used for further communication with\nthe other side of the connection, rather than the original socket instance\n(which may not function properly after the unwrap).
\n\n
\n
\n

17.3.3. Certificates

\n

Certificates in general are part of a public-key / private-key system. In this\nsystem, each principal, (which may be a machine, or a person, or an\norganization) is assigned a unique two-part encryption key. One part of the key\nis public, and is called the public key; the other part is kept secret, and is\ncalled the private key. The two parts are related, in that if you encrypt a\nmessage with one of the parts, you can decrypt it with the other part, and\nonly with the other part.

\n

A certificate contains information about two principals. It contains the name\nof a subject, and the subject’s public key. It also contains a statement by a\nsecond principal, the issuer, that the subject is who he claims to be, and\nthat this is indeed the subject’s public key. The issuer’s statement is signed\nwith the issuer’s private key, which only the issuer knows. However, anyone can\nverify the issuer’s statement by finding the issuer’s public key, decrypting the\nstatement with it, and comparing it to the other information in the certificate.\nThe certificate also contains information about the time period over which it is\nvalid. This is expressed as two fields, called “notBefore” and “notAfter”.

\n

In the Python use of certificates, a client or server can use a certificate to\nprove who they are. The other side of a network connection can also be required\nto produce a certificate, and that certificate can be validated to the\nsatisfaction of the client or server that requires such validation. The\nconnection attempt can be set to raise an exception if the validation fails.\nValidation is done automatically, by the underlying OpenSSL framework; the\napplication need not concern itself with its mechanics. But the application\ndoes usually need to provide sets of certificates to allow this process to take\nplace.

\n

Python uses files to contain certificates. They should be formatted as “PEM”\n(see RFC 1422), which is a base-64 encoded form wrapped with a header line\nand a footer line:

\n
-----BEGIN CERTIFICATE-----\n... (certificate in base64 PEM encoding) ...\n-----END CERTIFICATE-----
\n
\n

The Python files which contain certificates can contain a sequence of\ncertificates, sometimes called a certificate chain. This chain should start\nwith the specific certificate for the principal who “is” the client or server,\nand then the certificate for the issuer of that certificate, and then the\ncertificate for the issuer of that certificate, and so on up the chain till\nyou get to a certificate which is self-signed, that is, a certificate which\nhas the same subject and issuer, sometimes called a root certificate. The\ncertificates should just be concatenated together in the certificate file. For\nexample, suppose we had a three certificate chain, from our server certificate\nto the certificate of the certification authority that signed our server\ncertificate, to the root certificate of the agency which issued the\ncertification authority’s certificate:

\n
-----BEGIN CERTIFICATE-----\n... (certificate for your server)...\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\n... (the certificate for the CA)...\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\n... (the root certificate for the CA's issuer)...\n-----END CERTIFICATE-----
\n
\n

If you are going to require validation of the other side of the connection’s\ncertificate, you need to provide a “CA certs” file, filled with the certificate\nchains for each issuer you are willing to trust. Again, this file just contains\nthese chains concatenated together. For validation, Python will use the first\nchain it finds in the file which matches.

\n

Some “standard” root certificates are available from various certification\nauthorities: CACert.org, Thawte, Verisign, Positive SSL\n(used by python.org), Equifax and GeoTrust.

\n

In general, if you are using SSL3 or TLS1, you don’t need to put the full chain\nin your “CA certs” file; you only need the root certificates, and the remote\npeer is supposed to furnish the other certificates necessary to chain from its\ncertificate to a root certificate. See RFC 4158 for more discussion of the\nway in which certification chains can be built.

\n

If you are going to create a server that provides SSL-encrypted connection\nservices, you will need to acquire a certificate for that service. There are\nmany ways of acquiring appropriate certificates, such as buying one from a\ncertification authority. Another common practice is to generate a self-signed\ncertificate. The simplest way to do this is with the OpenSSL package, using\nsomething like the following:

\n
\n
\n

The disadvantage of a self-signed certificate is that it is its own root\ncertificate, and no one else will have it in their cache of known (and trusted)\nroot certificates.

\n
\n
\n

17.3.4. Examples

\n
\n

17.3.4.1. Testing for SSL support

\n

To test for the presence of SSL support in a Python installation, user code\nshould use the following idiom:

\n
try:\n    import ssl\nexcept ImportError:\n    pass\nelse:\n    ... # do something that requires SSL support\n
\n
\n
\n
\n

17.3.4.2. Client-side operation

\n

This example connects to an SSL server, prints the server’s address and\ncertificate, sends some bytes, and reads part of the response:

\n
import socket, ssl, pprint\n\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\n# require a certificate from the server\nssl_sock = ssl.wrap_socket(s,\n                           ca_certs="/etc/ca_certs_file",\n                           cert_reqs=ssl.CERT_REQUIRED)\n\nssl_sock.connect(('www.verisign.com', 443))\n\nprint repr(ssl_sock.getpeername())\nprint ssl_sock.cipher()\nprint pprint.pformat(ssl_sock.getpeercert())\n\n# Set a simple HTTP request -- use httplib in actual code.\nssl_sock.write("""GET / HTTP/1.0\\r\nHost: www.verisign.com\\r\\n\\r\\n""")\n\n# Read a chunk of data.  Will not necessarily\n# read all the data returned by the server.\ndata = ssl_sock.read()\n\n# note that closing the SSLSocket will also close the underlying socket\nssl_sock.close()\n
\n
\n

As of September 6, 2007, the certificate printed by this program looked like\nthis:

\n
{'notAfter': 'May  8 23:59:59 2009 GMT',\n 'subject': ((('serialNumber', u'2497886'),),\n             (('1.3.6.1.4.1.311.60.2.1.3', u'US'),),\n             (('1.3.6.1.4.1.311.60.2.1.2', u'Delaware'),),\n             (('countryName', u'US'),),\n             (('postalCode', u'94043'),),\n             (('stateOrProvinceName', u'California'),),\n             (('localityName', u'Mountain View'),),\n             (('streetAddress', u'487 East Middlefield Road'),),\n             (('organizationName', u'VeriSign, Inc.'),),\n             (('organizationalUnitName',\n               u'Production Security Services'),),\n             (('organizationalUnitName',\n               u'Terms of use at www.verisign.com/rpa (c)06'),),\n             (('commonName', u'www.verisign.com'),))}\n
\n
\n

which is a fairly poorly-formed subject field.

\n
\n
\n

17.3.4.3. Server-side operation

\n

For server operation, typically you’d need to have a server certificate, and\nprivate key, each in a file. You’d open a socket, bind it to a port, call\nlisten() on it, then start waiting for clients to connect:

\n
import socket, ssl\n\nbindsocket = socket.socket()\nbindsocket.bind(('myaddr.mydomain.com', 10023))\nbindsocket.listen(5)\n
\n
\n

When one did, you’d call accept() on the socket to get the new socket from\nthe other end, and use wrap_socket() to create a server-side SSL context\nfor it:

\n
while True:\n    newsocket, fromaddr = bindsocket.accept()\n    connstream = ssl.wrap_socket(newsocket,\n                                 server_side=True,\n                                 certfile="mycertfile",\n                                 keyfile="mykeyfile",\n                                 ssl_version=ssl.PROTOCOL_TLSv1)\n    try:\n        deal_with_client(connstream)\n    finally:\n        connstream.shutdown(socket.SHUT_RDWR)\n        connstream.close()\n
\n
\n

Then you’d read data from the connstream and do something with it till you\nare finished with the client (or the client is finished with you):

\n
def deal_with_client(connstream):\n    data = connstream.read()\n    # null data means the client is finished with us\n    while data:\n        if not do_something(connstream, data):\n            # we'll assume do_something returns False\n            # when we're finished with client\n            break\n        data = connstream.read()\n    # finished with client\n
\n
\n

And go back to listening for new client connections.

\n\n
\n
\n
", "searchableItems": [ { "name": "ssl.cert_time_to_seconds", "domId": "ssl_ssl.cert_time_to_seconds" }, { "name": "ssl.DER_cert_to_PEM_cert", "domId": "ssl_ssl.DER_cert_to_PEM_cert" }, { "name": "ssl.get_server_certificate", "domId": "ssl_ssl.get_server_certificate" }, { "name": "ssl.PEM_cert_to_DER_cert", "domId": "ssl_ssl.PEM_cert_to_DER_cert" }, { "name": "ssl.RAND_add", "domId": "ssl_ssl.RAND_add" }, { "name": "ssl.RAND_egd", "domId": "ssl_ssl.RAND_egd" }, { "name": "ssl.RAND_status", "domId": "ssl_ssl.RAND_status" }, { "name": "ssl.SSLSocket.cipher", "domId": "ssl_ssl.SSLSocket.cipher" }, { "name": "ssl.SSLSocket.do_handshake", "domId": "ssl_ssl.SSLSocket.do_handshake" }, { "name": "ssl.SSLSocket.getpeercert", "domId": "ssl_ssl.SSLSocket.getpeercert" }, { "name": "ssl.SSLSocket.read", "domId": "ssl_ssl.SSLSocket.read" }, { "name": "ssl.SSLSocket.unwrap", "domId": "ssl_ssl.SSLSocket.unwrap" }, { "name": "ssl.SSLSocket.write", "domId": "ssl_ssl.SSLSocket.write" }, { "name": "ssl.wrap_socket", "domId": "ssl_ssl.wrap_socket" } ] }, { "url": "http://docs.python.org/library/asyncore.html", "title": "asyncore", "html": "
\n

17.6. asyncore — Asynchronous socket handler

\n

Source code: Lib/asyncore.py

\n
\n

This module provides the basic infrastructure for writing asynchronous socket\nservice clients and servers.

\n

There are only two ways to have a program on a single processor do “more than\none thing at a time.” Multi-threaded programming is the simplest and most\npopular way to do it, but there is another very different technique, that lets\nyou have nearly all the advantages of multi-threading, without actually using\nmultiple threads. It’s really only practical if your program is largely I/O\nbound. If your program is processor bound, then pre-emptive scheduled threads\nare probably what you really need. Network servers are rarely processor\nbound, however.

\n

If your operating system supports the select() system call in its I/O\nlibrary (and nearly all do), then you can use it to juggle multiple\ncommunication channels at once; doing other work while your I/O is taking\nplace in the “background.” Although this strategy can seem strange and\ncomplex, especially at first, it is in many ways easier to understand and\ncontrol than multi-threaded programming. The asyncore module solves\nmany of the difficult problems for you, making the task of building\nsophisticated high-performance network servers and clients a snap. For\n“conversational” applications and protocols the companion asynchat\nmodule is invaluable.

\n

The basic idea behind both modules is to create one or more network\nchannels, instances of class asyncore.dispatcher and\nasynchat.async_chat. Creating the channels adds them to a global\nmap, used by the loop() function if you do not provide it with your own\nmap.

\n

Once the initial channel(s) is(are) created, calling the loop() function\nactivates channel service, which continues until the last channel (including\nany that have been added to the map during asynchronous service) is closed.

\n
\n
\nasyncore.loop([timeout[, use_poll[, map[, count]]]])
\n

Enter a polling loop that terminates after count passes or all open\nchannels have been closed. All arguments are optional. The count\nparameter defaults to None, resulting in the loop terminating only when all\nchannels have been closed. The timeout argument sets the timeout\nparameter for the appropriate select() or poll() call, measured\nin seconds; the default is 30 seconds. The use_poll parameter, if true,\nindicates that poll() should be used in preference to select()\n(the default is False).

\n

The map parameter is a dictionary whose items are the channels to watch.\nAs channels are closed they are deleted from their map. If map is\nomitted, a global map is used. Channels (instances of\nasyncore.dispatcher, asynchat.async_chat and subclasses\nthereof) can freely be mixed in the map.

\n
\n\n
\n
\nclass asyncore.dispatcher
\n

The dispatcher class is a thin wrapper around a low-level socket\nobject. To make it more useful, it has a few methods for event-handling\nwhich are called from the asynchronous loop. Otherwise, it can be treated\nas a normal non-blocking socket object.

\n

The firing of low-level events at certain times or in certain connection\nstates tells the asynchronous loop that certain higher-level events have\ntaken place. For example, if we have asked for a socket to connect to\nanother host, we know that the connection has been made when the socket\nbecomes writable for the first time (at this point you know that you may\nwrite to it with the expectation of success). The implied higher-level\nevents are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
EventDescription
handle_connect()Implied by the first read or write\nevent
handle_close()Implied by a read event with no data\navailable
handle_accept()Implied by a read event on a listening\nsocket
\n

During asynchronous processing, each mapped channel’s readable() and\nwritable() methods are used to determine whether the channel’s socket\nshould be added to the list of channels select()ed or\npoll()ed for read and write events.

\n

Thus, the set of channel events is larger than the basic socket events. The\nfull set of methods that can be overridden in your subclass follows:

\n
\n
\nhandle_read()
\n
Called when the asynchronous loop detects that a read() call on the\nchannel’s socket will succeed.
\n\n
\n
\nhandle_write()
\n

Called when the asynchronous loop detects that a writable socket can be\nwritten. Often this method will implement the necessary buffering for\nperformance. For example:

\n
def handle_write(self):\n    sent = self.send(self.buffer)\n    self.buffer = self.buffer[sent:]\n
\n
\n
\n\n
\n
\nhandle_expt()
\n
Called when there is out of band (OOB) data for a socket connection. This\nwill almost never happen, as OOB is tenuously supported and rarely used.
\n\n
\n
\nhandle_connect()
\n
Called when the active opener’s socket actually makes a connection. Might\nsend a “welcome” banner, or initiate a protocol negotiation with the\nremote endpoint, for example.
\n\n
\n
\nhandle_close()
\n
Called when the socket is closed.
\n\n
\n
\nhandle_error()
\n
Called when an exception is raised and not otherwise handled. The default\nversion prints a condensed traceback.
\n\n
\n
\nhandle_accept()
\n
Called on listening channels (passive openers) when a connection can be\nestablished with a new remote endpoint that has issued a connect()\ncall for the local endpoint.
\n\n
\n
\nreadable()
\n
Called each time around the asynchronous loop to determine whether a\nchannel’s socket should be added to the list on which read events can\noccur. The default method simply returns True, indicating that by\ndefault, all channels will be interested in read events.
\n\n
\n
\nwritable()
\n
Called each time around the asynchronous loop to determine whether a\nchannel’s socket should be added to the list on which write events can\noccur. The default method simply returns True, indicating that by\ndefault, all channels will be interested in write events.
\n\n

In addition, each channel delegates or extends many of the socket methods.\nMost of these are nearly identical to their socket partners.

\n
\n
\ncreate_socket(family, type)
\n
This is identical to the creation of a normal socket, and will use the\nsame options for creation. Refer to the socket documentation for\ninformation on creating sockets.
\n\n
\n
\nconnect(address)
\n
As with the normal socket object, address is a tuple with the first\nelement the host to connect to, and the second the port number.
\n\n
\n
\nsend(data)
\n
Send data to the remote end-point of the socket.
\n\n
\n
\nrecv(buffer_size)
\n
Read at most buffer_size bytes from the socket’s remote end-point. An\nempty string implies that the channel has been closed from the other end.
\n\n
\n
\nlisten(backlog)
\n
Listen for connections made to the socket. The backlog argument\nspecifies the maximum number of queued connections and should be at least\n1; the maximum value is system-dependent (usually 5).
\n\n
\n
\nbind(address)
\n
Bind the socket to address. The socket must not already be bound. (The\nformat of address depends on the address family — refer to the\nsocket documentation for more information.) To mark\nthe socket as re-usable (setting the SO_REUSEADDR option), call\nthe dispatcher object’s set_reuse_addr() method.
\n\n
\n
\naccept()
\n
Accept a connection. The socket must be bound to an address and listening\nfor connections. The return value can be either None or a pair\n(conn, address) where conn is a new socket object usable to send\nand receive data on the connection, and address is the address bound to\nthe socket on the other end of the connection.\nWhen None is returned it means the connection didn’t take place, in\nwhich case the server should just ignore this event and keep listening\nfor further incoming connections.
\n\n
\n
\nclose()
\n
Close the socket. All future operations on the socket object will fail.\nThe remote end-point will receive no more data (after queued data is\nflushed). Sockets are automatically closed when they are\ngarbage-collected.
\n\n
\n\n
\n
\nclass asyncore.dispatcher_with_send
\n
A dispatcher subclass which adds simple buffered output capability,\nuseful for simple clients. For more sophisticated usage use\nasynchat.async_chat.
\n\n
\n
\nclass asyncore.file_dispatcher
\n
A file_dispatcher takes a file descriptor or file object along with an\noptional map argument and wraps it for use with the poll() or\nloop() functions. If provided a file object or anything with a\nfileno() method, that method will be called and passed to the\nfile_wrapper constructor. Availability: UNIX.
\n\n
\n
\nclass asyncore.file_wrapper
\n
A file_wrapper takes an integer file descriptor and calls os.dup() to\nduplicate the handle so that the original handle may be closed independently\nof the file_wrapper. This class implements sufficient methods to emulate a\nsocket for use by the file_dispatcher class. Availability: UNIX.
\n\n
\n

17.6.1. asyncore Example basic HTTP client

\n

Here is a very basic HTTP client that uses the dispatcher class to\nimplement its socket handling:

\n
import asyncore, socket\n\nclass HTTPClient(asyncore.dispatcher):\n\n    def __init__(self, host, path):\n        asyncore.dispatcher.__init__(self)\n        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)\n        self.connect( (host, 80) )\n        self.buffer = 'GET %s HTTP/1.0\\r\\n\\r\\n'  path\n\n    def handle_connect(self):\n        pass\n\n    def handle_close(self):\n        self.close()\n\n    def handle_read(self):\n        print self.recv(8192)\n\n    def writable(self):\n        return (len(self.buffer) > 0)\n\n    def handle_write(self):\n        sent = self.send(self.buffer)\n        self.buffer = self.buffer[sent:]\n\n\nclient = HTTPClient('www.python.org', '/')\nasyncore.loop()\n
\n
\n
\n
\n

17.6.2. asyncore Example basic echo server

\n

Here is a basic echo server that uses the dispatcher class to accept\nconnections and dispatches the incoming connections to a handler:

\n
import asyncore\nimport socket\n\nclass EchoHandler(asyncore.dispatcher_with_send):\n\n    def handle_read(self):\n        data = self.recv(8192)\n        if data:\n            self.send(data)\n\nclass EchoServer(asyncore.dispatcher):\n\n    def __init__(self, host, port):\n        asyncore.dispatcher.__init__(self)\n        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)\n        self.set_reuse_addr()\n        self.bind((host, port))\n        self.listen(5)\n\n    def handle_accept(self):\n        pair = self.accept()\n        if pair is None:\n            pass\n        else:\n            sock, addr = pair\n            print 'Incoming connection from %s'  repr(addr)\n            handler = EchoHandler(sock)\n\nserver = EchoServer('localhost', 8080)\nasyncore.loop()\n
\n
\n
\n
", "searchableItems": [ { "name": "asyncore.dispatcher", "domId": "asyncore_asyncore.dispatcher" }, { "name": "asyncore.dispatcher.accept", "domId": "asyncore_asyncore.dispatcher.accept" }, { "name": "asyncore.dispatcher.bind", "domId": "asyncore_asyncore.dispatcher.bind" }, { "name": "asyncore.dispatcher.close", "domId": "asyncore_asyncore.dispatcher.close" }, { "name": "asyncore.dispatcher.connect", "domId": "asyncore_asyncore.dispatcher.connect" }, { "name": "asyncore.dispatcher.create_socket", "domId": "asyncore_asyncore.dispatcher.create_socket" }, { "name": "asyncore.dispatcher.handle_accept", "domId": "asyncore_asyncore.dispatcher.handle_accept" }, { "name": "asyncore.dispatcher.handle_close", "domId": "asyncore_asyncore.dispatcher.handle_close" }, { "name": "asyncore.dispatcher.handle_connect", "domId": "asyncore_asyncore.dispatcher.handle_connect" }, { "name": "asyncore.dispatcher.handle_error", "domId": "asyncore_asyncore.dispatcher.handle_error" }, { "name": "asyncore.dispatcher.handle_expt", "domId": "asyncore_asyncore.dispatcher.handle_expt" }, { "name": "asyncore.dispatcher.handle_read", "domId": "asyncore_asyncore.dispatcher.handle_read" }, { "name": "asyncore.dispatcher.handle_write", "domId": "asyncore_asyncore.dispatcher.handle_write" }, { "name": "asyncore.dispatcher.listen", "domId": "asyncore_asyncore.dispatcher.listen" }, { "name": "asyncore.dispatcher.readable", "domId": "asyncore_asyncore.dispatcher.readable" }, { "name": "asyncore.dispatcher.recv", "domId": "asyncore_asyncore.dispatcher.recv" }, { "name": "asyncore.dispatcher.send", "domId": "asyncore_asyncore.dispatcher.send" }, { "name": "asyncore.dispatcher.writable", "domId": "asyncore_asyncore.dispatcher.writable" }, { "name": "asyncore.dispatcher_with_send", "domId": "asyncore_asyncore.dispatcher_with_send" }, { "name": "asyncore.file_dispatcher", "domId": "asyncore_asyncore.file_dispatcher" }, { "name": "asyncore.file_wrapper", "domId": "asyncore_asyncore.file_wrapper" }, { "name": "asyncore.loop", "domId": "asyncore_asyncore.loop" } ] }, { "url": "http://docs.python.org/library/socket.html", "title": "socket", "html": "
\n

17.2. socket — Low-level networking interface

\n

This module provides access to the BSD socket interface. It is available on\nall modern Unix systems, Windows, Mac OS X, BeOS, OS/2, and probably additional\nplatforms.

\n
\n

Note

\n

Some behavior may be platform dependent, since calls are made to the operating\nsystem socket APIs.

\n
\n

For an introduction to socket programming (in C), see the following papers: An\nIntroductory 4.3BSD Interprocess Communication Tutorial, by Stuart Sechrest and\nAn Advanced 4.3BSD Interprocess Communication Tutorial, by Samuel J. Leffler et\nal, both in the UNIX Programmer’s Manual, Supplementary Documents 1 (sections\nPS1:7 and PS1:8). The platform-specific reference material for the various\nsocket-related system calls are also a valuable source of information on the\ndetails of socket semantics. For Unix, refer to the manual pages; for Windows,\nsee the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may\nwant to refer to RFC 3493 titled Basic Socket Interface Extensions for IPv6.

\n

The Python interface is a straightforward transliteration of the Unix system\ncall and library interface for sockets to Python’s object-oriented style: the\nsocket() function returns a socket object whose methods implement\nthe various socket system calls. Parameter types are somewhat higher-level than\nin the C interface: as with read() and write() operations on Python\nfiles, buffer allocation on receive operations is automatic, and buffer length\nis implicit on send operations.

\n

Socket addresses are represented as follows: A single string is used for the\nAF_UNIX address family. A pair (host, port) is used for the\nAF_INET address family, where host is a string representing either a\nhostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address\nlike '100.50.200.5', and port is an integral port number. For\nAF_INET6 address family, a four-tuple (host, port, flowinfo,\nscopeid) is used, where flowinfo and scopeid represents sin6_flowinfo\nand sin6_scope_id member in struct sockaddr_in6 in C. For\nsocket module methods, flowinfo and scopeid can be omitted just for\nbackward compatibility. Note, however, omission of scopeid can cause problems\nin manipulating scoped IPv6 addresses. Other address families are currently not\nsupported. The address format required by a particular socket object is\nautomatically selected based on the address family specified when the socket\nobject was created.

\n

For IPv4 addresses, two special forms are accepted instead of a host address:\nthe empty string represents INADDR_ANY, and the string\n'<broadcast>' represents INADDR_BROADCAST. The behavior is not\navailable for IPv6 for backward compatibility, therefore, you may want to avoid\nthese if you intend to support IPv6 with your Python programs.

\n

If you use a hostname in the host portion of IPv4/v6 socket address, the\nprogram may show a nondeterministic behavior, as Python uses the first address\nreturned from the DNS resolution. The socket address will be resolved\ndifferently into an actual IPv4/v6 address, depending on the results from DNS\nresolution and/or the host configuration. For deterministic behavior use a\nnumeric address in host portion.

\n

\nNew in version 2.5: AF_NETLINK sockets are represented as pairs pid, groups.

\n

\nNew in version 2.6: Linux-only support for TIPC is also available using the AF_TIPC\naddress family. TIPC is an open, non-IP based networked protocol designed\nfor use in clustered computer environments. Addresses are represented by a\ntuple, and the fields depend on the address type. The general tuple form is\n(addr_type, v1, v2, v3 [, scope]), where:

\n

\n

All errors raise exceptions. The normal exceptions for invalid argument types\nand out-of-memory conditions can be raised; errors related to socket or address\nsemantics raise the error socket.error.

\n

Non-blocking mode is supported through setblocking(). A\ngeneralization of this based on timeouts is supported through\nsettimeout().

\n

The module socket exports the following constants and functions:

\n
\n
\nexception socket.error
\n

This exception is raised for socket-related errors. The accompanying value is\neither a string telling what went wrong or a pair (errno, string)\nrepresenting an error returned by a system call, similar to the value\naccompanying os.error. See the module errno, which contains names\nfor the error codes defined by the underlying operating system.

\n

\nChanged in version 2.6: socket.error is now a child class of IOError.

\n
\n\n
\n
\nexception socket.herror
\n

This exception is raised for address-related errors, i.e. for functions that use\nh_errno in the C API, including gethostbyname_ex() and\ngethostbyaddr().

\n

The accompanying value is a pair (h_errno, string) representing an error\nreturned by a library call. string represents the description of h_errno, as\nreturned by the hstrerror() C function.

\n
\n\n
\n
\nexception socket.gaierror
\n
This exception is raised for address-related errors, for getaddrinfo() and\ngetnameinfo(). The accompanying value is a pair (error, string)\nrepresenting an error returned by a library call. string represents the\ndescription of error, as returned by the gai_strerror() C function. The\nerror value will match one of the EAI_* constants defined in this\nmodule.
\n\n
\n
\nexception socket.timeout
\n

This exception is raised when a timeout occurs on a socket which has had\ntimeouts enabled via a prior call to settimeout(). The accompanying value\nis a string whose value is currently always “timed out”.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsocket.AF_UNIX
\n
\nsocket.AF_INET
\n
\nsocket.AF_INET6
\n
These constants represent the address (and protocol) families, used for the\nfirst argument to socket(). If the AF_UNIX constant is not\ndefined then this protocol is unsupported.
\n\n
\n
\nsocket.SOCK_STREAM
\n
\nsocket.SOCK_DGRAM
\n
\nsocket.SOCK_RAW
\n
\nsocket.SOCK_RDM
\n
\nsocket.SOCK_SEQPACKET
\n
These constants represent the socket types, used for the second argument to\nsocket(). (Only SOCK_STREAM and SOCK_DGRAM appear to be\ngenerally useful.)
\n\n
\n
\nSO_*
\n
\nsocket.SOMAXCONN
\n
\nMSG_*
\n
\nSOL_*
\n
\nIPPROTO_*
\n
\nIPPORT_*
\n
\nINADDR_*
\n
\nIP_*
\n
\nIPV6_*
\n
\nEAI_*
\n
\nAI_*
\n
\nNI_*
\n
\nTCP_*
\n
Many constants of these forms, documented in the Unix documentation on sockets\nand/or the IP protocol, are also defined in the socket module. They are\ngenerally used in arguments to the setsockopt() and getsockopt()\nmethods of socket objects. In most cases, only those symbols that are defined\nin the Unix header files are defined; for a few symbols, default values are\nprovided.
\n\n
\n
\nSIO_*
\n
\nRCVALL_*
\n

Constants for Windows’ WSAIoctl(). The constants are used as arguments to the\nioctl() method of socket objects.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nTIPC_*
\n

TIPC related constants, matching the ones exported by the C socket API. See\nthe TIPC documentation for more information.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsocket.has_ipv6
\n

This constant contains a boolean value which indicates if IPv6 is supported on\nthis platform.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsocket.create_connection(address[, timeout[, source_address]])
\n

Connect to a TCP service listening on the Internet address (a 2-tuple\n(host, port)), and return the socket object. This is a higher-level\nfunction than socket.connect(): if host is a non-numeric hostname,\nit will try to resolve it for both AF_INET and AF_INET6,\nand then try to connect to all possible addresses in turn until a\nconnection succeeds. This makes it easy to write clients that are\ncompatible to both IPv4 and IPv6.

\n

Passing the optional timeout parameter will set the timeout on the\nsocket instance before attempting to connect. If no timeout is\nsupplied, the global default timeout setting returned by\ngetdefaulttimeout() is used.

\n

If supplied, source_address must be a 2-tuple (host, port) for the\nsocket to bind to as its source address before connecting. If host or port\nare ‘’ or 0 respectively the OS default behavior will be used.

\n

\nNew in version 2.6.

\n

\nChanged in version 2.7: source_address was added.

\n
\n\n
\n
\nsocket.getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0)
\n

Translate the host/port argument into a sequence of 5-tuples that contain\nall the necessary arguments for creating a socket connected to that service.\nhost is a domain name, a string representation of an IPv4/v6 address\nor None. port is a string service name such as 'http', a numeric\nport number or None. By passing None as the value of host\nand port, you can pass NULL to the underlying C API.

\n

The family, socktype and proto arguments can be optionally specified\nin order to narrow the list of addresses returned. Passing zero as a\nvalue for each of these arguments selects the full range of results.\nThe flags argument can be one or several of the AI_* constants,\nand will influence how results are computed and returned.\nFor example, AI_NUMERICHOST will disable domain name resolution\nand will raise an error if host is a domain name.

\n

The function returns a list of 5-tuples with the following structure:

\n

(family, socktype, proto, canonname, sockaddr)

\n

In these tuples, family, socktype, proto are all integers and are\nmeant to be passed to the socket() function. canonname will be\na string representing the canonical name of the host if\nAI_CANONNAME is part of the flags argument; else canonname\nwill be empty. sockaddr is a tuple describing a socket address, whose\nformat depends on the returned family (a (address, port) 2-tuple for\nAF_INET, a (address, port, flow info, scope id) 4-tuple for\nAF_INET6), and is meant to be passed to the socket.connect()\nmethod.

\n

The following example fetches address information for a hypothetical TCP\nconnection to www.python.org on port 80 (results may differ on your\nsystem if IPv6 isn’t enabled):

\n
>>> socket.getaddrinfo("www.python.org", 80, 0, 0, socket.SOL_TCP)\n[(2, 1, 6, '', ('82.94.164.162', 80)),\n (10, 1, 6, '', ('2001:888:2000:d::a2', 80, 0, 0))]\n
\n
\n

\nNew in version 2.2.

\n
\n\n
\n
\nsocket.getfqdn([name])
\n

Return a fully qualified domain name for name. If name is omitted or empty,\nit is interpreted as the local host. To find the fully qualified name, the\nhostname returned by gethostbyaddr() is checked, followed by aliases for the\nhost, if available. The first name which includes a period is selected. In\ncase no fully qualified domain name is available, the hostname as returned by\ngethostname() is returned.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nsocket.gethostbyname(hostname)
\n
Translate a host name to IPv4 address format. The IPv4 address is returned as a\nstring, such as '100.50.200.5'. If the host name is an IPv4 address itself\nit is returned unchanged. See gethostbyname_ex() for a more complete\ninterface. gethostbyname() does not support IPv6 name resolution, and\ngetaddrinfo() should be used instead for IPv4/v6 dual stack support.
\n\n
\n
\nsocket.gethostbyname_ex(hostname)
\n
Translate a host name to IPv4 address format, extended interface. Return a\ntriple (hostname, aliaslist, ipaddrlist) where hostname is the primary\nhost name responding to the given ip_address, aliaslist is a (possibly\nempty) list of alternative host names for the same address, and ipaddrlist is\na list of IPv4 addresses for the same interface on the same host (often but not\nalways a single address). gethostbyname_ex() does not support IPv6 name\nresolution, and getaddrinfo() should be used instead for IPv4/v6 dual\nstack support.
\n\n
\n
\nsocket.gethostname()
\n

Return a string containing the hostname of the machine where the Python\ninterpreter is currently executing.

\n

If you want to know the current machine’s IP address, you may want to use\ngethostbyname(gethostname()). This operation assumes that there is a\nvalid address-to-host mapping for the host, and the assumption does not\nalways hold.

\n

Note: gethostname() doesn’t always return the fully qualified domain\nname; use getfqdn() (see above).

\n
\n\n
\n
\nsocket.gethostbyaddr(ip_address)
\n
Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the\nprimary host name responding to the given ip_address, aliaslist is a\n(possibly empty) list of alternative host names for the same address, and\nipaddrlist is a list of IPv4/v6 addresses for the same interface on the same\nhost (most likely containing only a single address). To find the fully qualified\ndomain name, use the function getfqdn(). gethostbyaddr() supports\nboth IPv4 and IPv6.
\n\n
\n
\nsocket.getnameinfo(sockaddr, flags)
\n

Translate a socket address sockaddr into a 2-tuple (host, port). Depending\non the settings of flags, the result can contain a fully-qualified domain name\nor numeric address representation in host. Similarly, port can contain a\nstring port name or a numeric port number.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nsocket.getprotobyname(protocolname)
\n
Translate an Internet protocol name (for example, 'icmp') to a constant\nsuitable for passing as the (optional) third argument to the socket()\nfunction. This is usually only needed for sockets opened in “raw” mode\n(SOCK_RAW); for the normal socket modes, the correct protocol is chosen\nautomatically if the protocol is omitted or zero.
\n\n
\n
\nsocket.getservbyname(servicename[, protocolname])
\n
Translate an Internet service name and protocol name to a port number for that\nservice. The optional protocol name, if given, should be 'tcp' or\n'udp', otherwise any protocol will match.
\n\n
\n
\nsocket.getservbyport(port[, protocolname])
\n
Translate an Internet port number and protocol name to a service name for that\nservice. The optional protocol name, if given, should be 'tcp' or\n'udp', otherwise any protocol will match.
\n\n
\n
\nsocket.socket([family[, type[, proto]]])
\n
Create a new socket using the given address family, socket type and protocol\nnumber. The address family should be AF_INET (the default),\nAF_INET6 or AF_UNIX. The socket type should be\nSOCK_STREAM (the default), SOCK_DGRAM or perhaps one of the\nother SOCK_ constants. The protocol number is usually zero and may be\nomitted in that case.
\n\n
\n
\nsocket.socketpair([family[, type[, proto]]])
\n

Build a pair of connected socket objects using the given address family, socket\ntype, and protocol number. Address family, socket type, and protocol number are\nas for the socket() function above. The default family is AF_UNIX\nif defined on the platform; otherwise, the default is AF_INET.\nAvailability: Unix.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nsocket.fromfd(fd, family, type[, proto])
\n
Duplicate the file descriptor fd (an integer as returned by a file object’s\nfileno() method) and build a socket object from the result. Address\nfamily, socket type and protocol number are as for the socket() function\nabove. The file descriptor should refer to a socket, but this is not checked —\nsubsequent operations on the object may fail if the file descriptor is invalid.\nThis function is rarely needed, but can be used to get or set socket options on\na socket passed to a program as standard input or output (such as a server\nstarted by the Unix inet daemon). The socket is assumed to be in blocking mode.\nAvailability: Unix.
\n\n
\n
\nsocket.ntohl(x)
\n
Convert 32-bit positive integers from network to host byte order. On machines\nwhere the host byte order is the same as network byte order, this is a no-op;\notherwise, it performs a 4-byte swap operation.
\n\n
\n
\nsocket.ntohs(x)
\n
Convert 16-bit positive integers from network to host byte order. On machines\nwhere the host byte order is the same as network byte order, this is a no-op;\notherwise, it performs a 2-byte swap operation.
\n\n
\n
\nsocket.htonl(x)
\n
Convert 32-bit positive integers from host to network byte order. On machines\nwhere the host byte order is the same as network byte order, this is a no-op;\notherwise, it performs a 4-byte swap operation.
\n\n
\n
\nsocket.htons(x)
\n
Convert 16-bit positive integers from host to network byte order. On machines\nwhere the host byte order is the same as network byte order, this is a no-op;\notherwise, it performs a 2-byte swap operation.
\n\n
\n
\nsocket.inet_aton(ip_string)
\n

Convert an IPv4 address from dotted-quad string format (for example,\n‘123.45.67.89’) to 32-bit packed binary format, as a string four characters in\nlength. This is useful when conversing with a program that uses the standard C\nlibrary and needs objects of type struct in_addr, which is the C type\nfor the 32-bit packed binary this function returns.

\n

inet_aton() also accepts strings with less than three dots; see the\nUnix manual page inet(3) for details.

\n

If the IPv4 address string passed to this function is invalid,\nsocket.error will be raised. Note that exactly what is valid depends on\nthe underlying C implementation of inet_aton().

\n

inet_aton() does not support IPv6, and inet_pton() should be used\ninstead for IPv4/v6 dual stack support.

\n
\n\n
\n
\nsocket.inet_ntoa(packed_ip)
\n

Convert a 32-bit packed IPv4 address (a string four characters in length) to its\nstandard dotted-quad string representation (for example, ‘123.45.67.89’). This\nis useful when conversing with a program that uses the standard C library and\nneeds objects of type struct in_addr, which is the C type for the\n32-bit packed binary data this function takes as an argument.

\n

If the string passed to this function is not exactly 4 bytes in length,\nsocket.error will be raised. inet_ntoa() does not support IPv6, and\ninet_ntop() should be used instead for IPv4/v6 dual stack support.

\n
\n\n
\n
\nsocket.inet_pton(address_family, ip_string)
\n

Convert an IP address from its family-specific string format to a packed, binary\nformat. inet_pton() is useful when a library or network protocol calls for\nan object of type struct in_addr (similar to inet_aton()) or\nstruct in6_addr.

\n

Supported values for address_family are currently AF_INET and\nAF_INET6. If the IP address string ip_string is invalid,\nsocket.error will be raised. Note that exactly what is valid depends on\nboth the value of address_family and the underlying implementation of\ninet_pton().

\n

Availability: Unix (maybe not all platforms).

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsocket.inet_ntop(address_family, packed_ip)
\n

Convert a packed IP address (a string of some number of characters) to its\nstandard, family-specific string representation (for example, '7.10.0.5' or\n'5aef:2b::8') inet_ntop() is useful when a library or network protocol\nreturns an object of type struct in_addr (similar to inet_ntoa())\nor struct in6_addr.

\n

Supported values for address_family are currently AF_INET and\nAF_INET6. If the string packed_ip is not the correct length for the\nspecified address family, ValueError will be raised. A\nsocket.error is raised for errors from the call to inet_ntop().

\n

Availability: Unix (maybe not all platforms).

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsocket.getdefaulttimeout()
\n

Return the default timeout in seconds (float) for new socket objects. A value\nof None indicates that new socket objects have no timeout. When the socket\nmodule is first imported, the default is None.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsocket.setdefaulttimeout(timeout)
\n

Set the default timeout in seconds (float) for new socket objects. A value of\nNone indicates that new socket objects have no timeout. When the socket\nmodule is first imported, the default is None.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsocket.SocketType
\n
This is a Python type object that represents the socket object type. It is the\nsame as type(socket(...)).
\n\n
\n

See also

\n
\n
Module SocketServer
\n
Classes that simplify writing network servers.
\n
Module ssl
\n
A TLS/SSL wrapper for socket objects.
\n
\n
\n
\n

17.2.1. Socket Objects

\n

Socket objects have the following methods. Except for makefile() these\ncorrespond to Unix system calls applicable to sockets.

\n
\n
\nsocket.accept()
\n
Accept a connection. The socket must be bound to an address and listening for\nconnections. The return value is a pair (conn, address) where conn is a\nnew socket object usable to send and receive data on the connection, and\naddress is the address bound to the socket on the other end of the connection.
\n\n
\n
\nsocket.bind(address)
\n

Bind the socket to address. The socket must not already be bound. (The format\nof address depends on the address family — see above.)

\n
\n

Note

\n

This method has historically accepted a pair of parameters for AF_INET\naddresses instead of only a tuple. This was never intentional and is no longer\navailable in Python 2.0 and later.

\n
\n
\n\n
\n
\nsocket.close()
\n

Close the socket. All future operations on the socket object will fail. The\nremote end will receive no more data (after queued data is flushed). Sockets are\nautomatically closed when they are garbage-collected.

\n
\n

Note

\n

close() releases the resource associated with a connection but\ndoes not necessarily close the connection immediately. If you want\nto close the connection in a timely fashion, call shutdown()\nbefore close().

\n
\n
\n\n
\n
\nsocket.connect(address)
\n

Connect to a remote socket at address. (The format of address depends on the\naddress family — see above.)

\n
\n

Note

\n

This method has historically accepted a pair of parameters for AF_INET\naddresses instead of only a tuple. This was never intentional and is no longer\navailable in Python 2.0 and later.

\n
\n
\n\n
\n
\nsocket.connect_ex(address)
\n

Like connect(address), but return an error indicator instead of raising an\nexception for errors returned by the C-level connect() call (other\nproblems, such as “host not found,” can still raise exceptions). The error\nindicator is 0 if the operation succeeded, otherwise the value of the\nerrno variable. This is useful to support, for example, asynchronous\nconnects.

\n
\n

Note

\n

This method has historically accepted a pair of parameters for AF_INET\naddresses instead of only a tuple. This was never intentional and is no longer\navailable in Python 2.0 and later.

\n
\n
\n\n
\n
\nsocket.fileno()
\n

Return the socket’s file descriptor (a small integer). This is useful with\nselect.select().

\n

Under Windows the small integer returned by this method cannot be used where a\nfile descriptor can be used (such as os.fdopen()). Unix does not have\nthis limitation.

\n
\n\n
\n
\nsocket.getpeername()
\n
Return the remote address to which the socket is connected. This is useful to\nfind out the port number of a remote IPv4/v6 socket, for instance. (The format\nof the address returned depends on the address family — see above.) On some\nsystems this function is not supported.
\n\n
\n
\nsocket.getsockname()
\n
Return the socket’s own address. This is useful to find out the port number of\nan IPv4/v6 socket, for instance. (The format of the address returned depends on\nthe address family — see above.)
\n\n
\n
\nsocket.getsockopt(level, optname[, buflen])
\n
Return the value of the given socket option (see the Unix man page\ngetsockopt(2)). The needed symbolic constants (SO_* etc.)\nare defined in this module. If buflen is absent, an integer option is assumed\nand its integer value is returned by the function. If buflen is present, it\nspecifies the maximum length of the buffer used to receive the option in, and\nthis buffer is returned as a string. It is up to the caller to decode the\ncontents of the buffer (see the optional built-in module struct for a way\nto decode C structures encoded as strings).
\n\n
\n
\nsocket.ioctl(control, option)
\n
\n\n\n\n\n\n\n
Platform:Windows
\n

The ioctl() method is a limited interface to the WSAIoctl system\ninterface. Please refer to the Win32 documentation for more\ninformation.

\n

On other platforms, the generic fcntl.fcntl() and fcntl.ioctl()\nfunctions may be used; they accept a socket object as their first argument.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsocket.listen(backlog)
\n
Listen for connections made to the socket. The backlog argument specifies the\nmaximum number of queued connections and should be at least 0; the maximum value\nis system-dependent (usually 5), the minimum value is forced to 0.
\n\n
\n
\nsocket.makefile([mode[, bufsize]])
\n

Return a file object associated with the socket. (File objects are\ndescribed in File Objects.) The file object\nreferences a dup()ped version of the socket file descriptor, so the\nfile object and socket object may be closed or garbage-collected independently.\nThe socket must be in blocking mode (it can not have a timeout). The optional\nmode and bufsize arguments are interpreted the same way as by the built-in\nfile() function.

\n
\n

Note

\n

On Windows, the file-like object created by makefile() cannot be\nused where a file object with a file descriptor is expected, such as the\nstream arguments of subprocess.Popen().

\n
\n
\n\n
\n
\nsocket.recv(bufsize[, flags])
\n

Receive data from the socket. The return value is a string representing the\ndata received. The maximum amount of data to be received at once is specified\nby bufsize. See the Unix manual page recv(2) for the meaning of\nthe optional argument flags; it defaults to zero.

\n
\n

Note

\n

For best match with hardware and network realities, the value of bufsize\nshould be a relatively small power of 2, for example, 4096.

\n
\n
\n\n
\n
\nsocket.recvfrom(bufsize[, flags])
\n
Receive data from the socket. The return value is a pair (string, address)\nwhere string is a string representing the data received and address is the\naddress of the socket sending the data. See the Unix manual page\nrecv(2) for the meaning of the optional argument flags; it defaults\nto zero. (The format of address depends on the address family — see above.)
\n\n
\n
\nsocket.recvfrom_into(buffer[, nbytes[, flags]])
\n

Receive data from the socket, writing it into buffer instead of creating a\nnew string. The return value is a pair (nbytes, address) where nbytes is\nthe number of bytes received and address is the address of the socket sending\nthe data. See the Unix manual page recv(2) for the meaning of the\noptional argument flags; it defaults to zero. (The format of address\ndepends on the address family — see above.)

\n

\nNew in version 2.5.

\n
\n\n
\n
\nsocket.recv_into(buffer[, nbytes[, flags]])
\n

Receive up to nbytes bytes from the socket, storing the data into a buffer\nrather than creating a new string. If nbytes is not specified (or 0),\nreceive up to the size available in the given buffer. Returns the number of\nbytes received. See the Unix manual page recv(2) for the meaning\nof the optional argument flags; it defaults to zero.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nsocket.send(string[, flags])
\n
Send data to the socket. The socket must be connected to a remote socket. The\noptional flags argument has the same meaning as for recv() above.\nReturns the number of bytes sent. Applications are responsible for checking that\nall data has been sent; if only some of the data was transmitted, the\napplication needs to attempt delivery of the remaining data.
\n\n
\n
\nsocket.sendall(string[, flags])
\n
Send data to the socket. The socket must be connected to a remote socket. The\noptional flags argument has the same meaning as for recv() above.\nUnlike send(), this method continues to send data from string until\neither all data has been sent or an error occurs. None is returned on\nsuccess. On error, an exception is raised, and there is no way to determine how\nmuch data, if any, was successfully sent.
\n\n
\n
\nsocket.sendto(string[, flags], address)
\n
Send data to the socket. The socket should not be connected to a remote socket,\nsince the destination socket is specified by address. The optional flags\nargument has the same meaning as for recv() above. Return the number of\nbytes sent. (The format of address depends on the address family — see\nabove.)
\n\n
\n
\nsocket.setblocking(flag)
\n
Set blocking or non-blocking mode of the socket: if flag is 0, the socket is\nset to non-blocking, else to blocking mode. Initially all sockets are in\nblocking mode. In non-blocking mode, if a recv() call doesn’t find any\ndata, or if a send() call can’t immediately dispose of the data, a\nerror exception is raised; in blocking mode, the calls block until they\ncan proceed. s.setblocking(0) is equivalent to s.settimeout(0.0);\ns.setblocking(1) is equivalent to s.settimeout(None).
\n\n
\n
\nsocket.settimeout(value)
\n

Set a timeout on blocking socket operations. The value argument can be a\nnonnegative float expressing seconds, or None. If a float is given,\nsubsequent socket operations will raise a timeout exception if the\ntimeout period value has elapsed before the operation has completed. Setting\na timeout of None disables timeouts on socket operations.\ns.settimeout(0.0) is equivalent to s.setblocking(0);\ns.settimeout(None) is equivalent to s.setblocking(1).

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsocket.gettimeout()
\n

Return the timeout in seconds (float) associated with socket operations, or\nNone if no timeout is set. This reflects the last call to\nsetblocking() or settimeout().

\n

\nNew in version 2.3.

\n
\n\n

Some notes on socket blocking and timeouts: A socket object can be in one of\nthree modes: blocking, non-blocking, or timeout. Sockets are always created in\nblocking mode. In blocking mode, operations block until complete or\nthe system returns an error (such as connection timed out). In\nnon-blocking mode, operations fail (with an error that is unfortunately\nsystem-dependent) if they cannot be completed immediately. In timeout mode,\noperations fail if they cannot be completed within the timeout specified for the\nsocket or if the system returns an error. The setblocking()\nmethod is simply a shorthand for certain settimeout() calls.

\n

Timeout mode internally sets the socket in non-blocking mode. The blocking and\ntimeout modes are shared between file descriptors and socket objects that refer\nto the same network endpoint. A consequence of this is that file objects\nreturned by the makefile() method must only be used when the\nsocket is in blocking mode; in timeout or non-blocking mode file operations\nthat cannot be completed immediately will fail.

\n

Note that the connect() operation is subject to the timeout\nsetting, and in general it is recommended to call settimeout()\nbefore calling connect() or pass a timeout parameter to\ncreate_connection(). The system network stack may return a connection\ntimeout error of its own regardless of any Python socket timeout setting.

\n
\n
\nsocket.setsockopt(level, optname, value)
\n

Set the value of the given socket option (see the Unix manual page\nsetsockopt(2)). The needed symbolic constants are defined in the\nsocket module (SO_* etc.). The value can be an integer or a\nstring representing a buffer. In the latter case it is up to the caller to\nensure that the string contains the proper bits (see the optional built-in\nmodule struct for a way to encode C structures as strings).

\n
\n\n
\n
\nsocket.shutdown(how)
\n
Shut down one or both halves of the connection. If how is SHUT_RD,\nfurther receives are disallowed. If how is SHUT_WR, further sends\nare disallowed. If how is SHUT_RDWR, further sends and receives are\ndisallowed. Depending on the platform, shutting down one half of the connection\ncan also close the opposite half (e.g. on Mac OS X, shutdown(SHUT_WR) does\nnot allow further reads on the other end of the connection).
\n\n

Note that there are no methods read() or write(); use\nrecv() and send() without flags argument instead.

\n

Socket objects also have these (read-only) attributes that correspond to the\nvalues given to the socket constructor.

\n
\n
\nsocket.family
\n

The socket family.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nsocket.type
\n

The socket type.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nsocket.proto
\n

The socket protocol.

\n

\nNew in version 2.5.

\n
\n\n
\n
\n

17.2.2. Example

\n

Here are four minimal example programs using the TCP/IP protocol: a server that\nechoes all data that it receives back (servicing only one client), and a client\nusing it. Note that a server must perform the sequence socket(),\nbind(), listen(), accept() (possibly\nrepeating the accept() to service more than one client), while a\nclient only needs the sequence socket(), connect(). Also\nnote that the server does not send()/recv() on the\nsocket it is listening on but on the new socket returned by\naccept().

\n

The first two examples support IPv4 only.

\n
# Echo server program\nimport socket\n\nHOST = ''                 # Symbolic name meaning all available interfaces\nPORT = 50007              # Arbitrary non-privileged port\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.bind((HOST, PORT))\ns.listen(1)\nconn, addr = s.accept()\nprint 'Connected by', addr\nwhile 1:\n    data = conn.recv(1024)\n    if not data: break\n    conn.send(data)\nconn.close()\n
\n
\n
# Echo client program\nimport socket\n\nHOST = 'daring.cwi.nl'    # The remote host\nPORT = 50007              # The same port as used by the server\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.connect((HOST, PORT))\ns.send('Hello, world')\ndata = s.recv(1024)\ns.close()\nprint 'Received', repr(data)\n
\n
\n

The next two examples are identical to the above two, but support both IPv4 and\nIPv6. The server side will listen to the first address family available (it\nshould listen to both instead). On most of IPv6-ready systems, IPv6 will take\nprecedence and the server may not accept IPv4 traffic. The client side will try\nto connect to the all addresses returned as a result of the name resolution, and\nsends traffic to the first one connected successfully.

\n
# Echo server program\nimport socket\nimport sys\n\nHOST = None               # Symbolic name meaning all available interfaces\nPORT = 50007              # Arbitrary non-privileged port\ns = None\nfor res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,\n                              socket.SOCK_STREAM, 0, socket.AI_PASSIVE):\n    af, socktype, proto, canonname, sa = res\n    try:\n        s = socket.socket(af, socktype, proto)\n    except socket.error, msg:\n        s = None\n        continue\n    try:\n        s.bind(sa)\n        s.listen(1)\n    except socket.error, msg:\n        s.close()\n        s = None\n        continue\n    break\nif s is None:\n    print 'could not open socket'\n    sys.exit(1)\nconn, addr = s.accept()\nprint 'Connected by', addr\nwhile 1:\n    data = conn.recv(1024)\n    if not data: break\n    conn.send(data)\nconn.close()\n
\n
\n
# Echo client program\nimport socket\nimport sys\n\nHOST = 'daring.cwi.nl'    # The remote host\nPORT = 50007              # The same port as used by the server\ns = None\nfor res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):\n    af, socktype, proto, canonname, sa = res\n    try:\n        s = socket.socket(af, socktype, proto)\n    except socket.error, msg:\n        s = None\n        continue\n    try:\n        s.connect(sa)\n    except socket.error, msg:\n        s.close()\n        s = None\n        continue\n    break\nif s is None:\n    print 'could not open socket'\n    sys.exit(1)\ns.send('Hello, world')\ndata = s.recv(1024)\ns.close()\nprint 'Received', repr(data)\n
\n
\n

The last example shows how to write a very simple network sniffer with raw\nsockets on Windows. The example requires administrator privileges to modify\nthe interface:

\n
import socket\n\n# the public network interface\nHOST = socket.gethostbyname(socket.gethostname())\n\n# create a raw socket and bind it to the public interface\ns = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)\ns.bind((HOST, 0))\n\n# Include IP headers\ns.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)\n\n# receive all packages\ns.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)\n\n# receive a package\nprint s.recvfrom(65565)\n\n# disabled promiscuous mode\ns.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)\n
\n
\n

Running an example several times with too small delay between executions, could\nlead to this error:

\n
socket.error: [Errno 98] Address already in use
\n
\n

This is because the previous execution has left the socket in a TIME_WAIT\nstate, and can’t be immediately reused.

\n

There is a socket flag to set, in order to prevent this,\nsocket.SO_REUSEADDR:

\n
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\ns.bind((HOST, PORT))\n
\n
\n

the SO_REUSEADDR flag tells the kernel to reuse a local socket in\nTIME_WAIT state, without waiting for its natural timeout to expire.

\n
\n
", "searchableItems": [ { "name": "socket.create_connection", "domId": "socket_socket.create_connection" }, { "name": "socket.fromfd", "domId": "socket_socket.fromfd" }, { "name": "socket.getaddrinfo", "domId": "socket_socket.getaddrinfo" }, { "name": "socket.getdefaulttimeout", "domId": "socket_socket.getdefaulttimeout" }, { "name": "socket.getfqdn", "domId": "socket_socket.getfqdn" }, { "name": "socket.gethostbyaddr", "domId": "socket_socket.gethostbyaddr" }, { "name": "socket.gethostbyname", "domId": "socket_socket.gethostbyname" }, { "name": "socket.gethostbyname_ex", "domId": "socket_socket.gethostbyname_ex" }, { "name": "socket.gethostname", "domId": "socket_socket.gethostname" }, { "name": "socket.getnameinfo", "domId": "socket_socket.getnameinfo" }, { "name": "socket.getprotobyname", "domId": "socket_socket.getprotobyname" }, { "name": "socket.getservbyname", "domId": "socket_socket.getservbyname" }, { "name": "socket.getservbyport", "domId": "socket_socket.getservbyport" }, { "name": "socket.htonl", "domId": "socket_socket.htonl" }, { "name": "socket.htons", "domId": "socket_socket.htons" }, { "name": "socket.inet_aton", "domId": "socket_socket.inet_aton" }, { "name": "socket.inet_ntoa", "domId": "socket_socket.inet_ntoa" }, { "name": "socket.inet_ntop", "domId": "socket_socket.inet_ntop" }, { "name": "socket.inet_pton", "domId": "socket_socket.inet_pton" }, { "name": "socket.ntohl", "domId": "socket_socket.ntohl" }, { "name": "socket.ntohs", "domId": "socket_socket.ntohs" }, { "name": "socket.setdefaulttimeout", "domId": "socket_socket.setdefaulttimeout" }, { "name": "socket.socket", "domId": "socket_socket.socket" }, { "name": "socket.socket.accept", "domId": "socket_socket.socket.accept" }, { "name": "socket.socket.bind", "domId": "socket_socket.socket.bind" }, { "name": "socket.socket.close", "domId": "socket_socket.socket.close" }, { "name": "socket.socket.connect", "domId": "socket_socket.socket.connect" }, { "name": "socket.socket.connect_ex", "domId": "socket_socket.socket.connect_ex" }, { "name": "socket.socket.fileno", "domId": "socket_socket.socket.fileno" }, { "name": "socket.socket.getpeername", "domId": "socket_socket.socket.getpeername" }, { "name": "socket.socket.getsockname", "domId": "socket_socket.socket.getsockname" }, { "name": "socket.socket.getsockopt", "domId": "socket_socket.socket.getsockopt" }, { "name": "socket.socket.gettimeout", "domId": "socket_socket.socket.gettimeout" }, { "name": "socket.socket.ioctl", "domId": "socket_socket.socket.ioctl" }, { "name": "socket.socket.listen", "domId": "socket_socket.socket.listen" }, { "name": "socket.socket.makefile", "domId": "socket_socket.socket.makefile" }, { "name": "socket.socket.recv", "domId": "socket_socket.socket.recv" }, { "name": "socket.socket.recv_into", "domId": "socket_socket.socket.recv_into" }, { "name": "socket.socket.recvfrom", "domId": "socket_socket.socket.recvfrom" }, { "name": "socket.socket.recvfrom_into", "domId": "socket_socket.socket.recvfrom_into" }, { "name": "socket.socket.send", "domId": "socket_socket.socket.send" }, { "name": "socket.socket.sendall", "domId": "socket_socket.socket.sendall" }, { "name": "socket.socket.sendto", "domId": "socket_socket.socket.sendto" }, { "name": "socket.socket.setblocking", "domId": "socket_socket.socket.setblocking" }, { "name": "socket.socket.setsockopt", "domId": "socket_socket.socket.setsockopt" }, { "name": "socket.socket.settimeout", "domId": "socket_socket.socket.settimeout" }, { "name": "socket.socket.shutdown", "domId": "socket_socket.socket.shutdown" }, { "name": "socket.socketpair", "domId": "socket_socket.socketpair" } ] }, { "url": "http://docs.python.org/library/asynchat.html", "title": "asynchat", "html": "
\n

17.7. asynchat — Asynchronous socket command/response handler

\n

Source code: Lib/asynchat.py

\n
\n

This module builds on the asyncore infrastructure, simplifying\nasynchronous clients and servers and making it easier to handle protocols\nwhose elements are terminated by arbitrary strings, or are of variable length.\nasynchat defines the abstract class async_chat that you\nsubclass, providing implementations of the collect_incoming_data() and\nfound_terminator() methods. It uses the same asynchronous loop as\nasyncore, and the two types of channel, asyncore.dispatcher\nand asynchat.async_chat, can freely be mixed in the channel map.\nTypically an asyncore.dispatcher server channel generates new\nasynchat.async_chat channel objects as it receives incoming\nconnection requests.

\n
\n
\nclass asynchat.async_chat
\n

This class is an abstract subclass of asyncore.dispatcher. To make\npractical use of the code you must subclass async_chat, providing\nmeaningful collect_incoming_data() and found_terminator()\nmethods.\nThe asyncore.dispatcher methods can be used, although not all make\nsense in a message/response context.

\n

Like asyncore.dispatcher, async_chat defines a set of\nevents that are generated by an analysis of socket conditions after a\nselect() call. Once the polling loop has been started the\nasync_chat object’s methods are called by the event-processing\nframework with no action on the part of the programmer.

\n

Two class attributes can be modified, to improve performance, or possibly\neven to conserve memory.

\n
\n
\nac_in_buffer_size
\n
The asynchronous input buffer size (default 4096).
\n\n
\n
\nac_out_buffer_size
\n
The asynchronous output buffer size (default 4096).
\n\n

Unlike asyncore.dispatcher, async_chat allows you to\ndefine a first-in-first-out queue (fifo) of producers. A producer need\nhave only one method, more(), which should return data to be\ntransmitted on the channel.\nThe producer indicates exhaustion (i.e. that it contains no more data) by\nhaving its more() method return the empty string. At this point the\nasync_chat object removes the producer from the fifo and starts\nusing the next producer, if any. When the producer fifo is empty the\nhandle_write() method does nothing. You use the channel object’s\nset_terminator() method to describe how to recognize the end of, or\nan important breakpoint in, an incoming transmission from the remote\nendpoint.

\n

To build a functioning async_chat subclass your input methods\ncollect_incoming_data() and found_terminator() must handle the\ndata that the channel receives asynchronously. The methods are described\nbelow.

\n
\n\n
\n
\nasync_chat.close_when_done()
\n
Pushes a None on to the producer fifo. When this producer is popped off\nthe fifo it causes the channel to be closed.
\n\n
\n
\nasync_chat.collect_incoming_data(data)
\n
Called with data holding an arbitrary amount of received data. The\ndefault method, which must be overridden, raises a\nNotImplementedError exception.
\n\n
\n
\nasync_chat.discard_buffers()
\n
In emergencies this method will discard any data held in the input and/or\noutput buffers and the producer fifo.
\n\n
\n
\nasync_chat.found_terminator()
\n
Called when the incoming data stream matches the termination condition set\nby set_terminator(). The default method, which must be overridden,\nraises a NotImplementedError exception. The buffered input data\nshould be available via an instance attribute.
\n\n
\n
\nasync_chat.get_terminator()
\n
Returns the current terminator for the channel.
\n\n
\n
\nasync_chat.push(data)
\n
Pushes data on to the channel’s fifo to ensure its transmission.\nThis is all you need to do to have the channel write the data out to the\nnetwork, although it is possible to use your own producers in more complex\nschemes to implement encryption and chunking, for example.
\n\n
\n
\nasync_chat.push_with_producer(producer)
\n
Takes a producer object and adds it to the producer fifo associated with\nthe channel. When all currently-pushed producers have been exhausted the\nchannel will consume this producer’s data by calling its more()\nmethod and send the data to the remote endpoint.
\n\n
\n
\nasync_chat.set_terminator(term)
\n

Sets the terminating condition to be recognized on the channel. term\nmay be any of three types of value, corresponding to three different ways\nto handle incoming protocol data.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
termDescription
stringWill call found_terminator() when the\nstring is found in the input stream
integerWill call found_terminator() when the\nindicated number of characters have been\nreceived
NoneThe channel continues to collect data\nforever
\n

Note that any data following the terminator will be available for reading\nby the channel after found_terminator() is called.

\n
\n\n
\n

17.7.1. asynchat - Auxiliary Classes

\n
\n
\nclass asynchat.fifo([list=None])
\n

A fifo holding data which has been pushed by the application but\nnot yet popped for writing to the channel. A fifo is a list used\nto hold data and/or producers until they are required. If the list\nargument is provided then it should contain producers or data items to be\nwritten to the channel.

\n
\n
\nis_empty()
\n
Returns True if and only if the fifo is empty.
\n\n
\n
\nfirst()
\n
Returns the least-recently push()ed item from the fifo.
\n\n
\n
\npush(data)
\n
Adds the given data (which may be a string or a producer object) to the\nproducer fifo.
\n\n
\n
\npop()
\n
If the fifo is not empty, returns True, first(), deleting the popped\nitem. Returns False, None for an empty fifo.
\n\n
\n\n
\n
\n

17.7.2. asynchat Example

\n

The following partial example shows how HTTP requests can be read with\nasync_chat. A web server might create an\nhttp_request_handler object for each incoming client connection.\nNotice that initially the channel terminator is set to match the blank line at\nthe end of the HTTP headers, and a flag indicates that the headers are being\nread.

\n

Once the headers have been read, if the request is of type POST (indicating\nthat further data are present in the input stream) then the\nContent-Length: header is used to set a numeric terminator to read the\nright amount of data from the channel.

\n

The handle_request() method is called once all relevant input has been\nmarshalled, after setting the channel terminator to None to ensure that\nany extraneous data sent by the web client are ignored.

\n
class http_request_handler(asynchat.async_chat):\n\n    def __init__(self, sock, addr, sessions, log):\n        asynchat.async_chat.__init__(self, sock=sock)\n        self.addr = addr\n        self.sessions = sessions\n        self.ibuffer = []\n        self.obuffer = ""\n        self.set_terminator("\\r\\n\\r\\n")\n        self.reading_headers = True\n        self.handling = False\n        self.cgi_data = None\n        self.log = log\n\n    def collect_incoming_data(self, data):\n        """Buffer the data"""\n        self.ibuffer.append(data)\n\n    def found_terminator(self):\n        if self.reading_headers:\n            self.reading_headers = False\n            self.parse_headers("".join(self.ibuffer))\n            self.ibuffer = []\n            if self.op.upper() == "POST":\n                clen = self.headers.getheader("content-length")\n                self.set_terminator(int(clen))\n            else:\n                self.handling = True\n                self.set_terminator(None)\n                self.handle_request()\n        elif not self.handling:\n            self.set_terminator(None) # browsers sometimes over-send\n            self.cgi_data = parse(self.headers, "".join(self.ibuffer))\n            self.handling = True\n            self.ibuffer = []\n            self.handle_request()\n
\n
\n
\n
", "searchableItems": [ { "name": "asynchat.async_chat", "domId": "asynchat_asynchat.async_chat" }, { "name": "asynchat.async_chat.close_when_done", "domId": "asynchat_asynchat.async_chat.close_when_done" }, { "name": "asynchat.async_chat.collect_incoming_data", "domId": "asynchat_asynchat.async_chat.collect_incoming_data" }, { "name": "asynchat.async_chat.discard_buffers", "domId": "asynchat_asynchat.async_chat.discard_buffers" }, { "name": "asynchat.async_chat.found_terminator", "domId": "asynchat_asynchat.async_chat.found_terminator" }, { "name": "asynchat.async_chat.get_terminator", "domId": "asynchat_asynchat.async_chat.get_terminator" }, { "name": "asynchat.async_chat.push", "domId": "asynchat_asynchat.async_chat.push" }, { "name": "asynchat.async_chat.push_with_producer", "domId": "asynchat_asynchat.async_chat.push_with_producer" }, { "name": "asynchat.async_chat.set_terminator", "domId": "asynchat_asynchat.async_chat.set_terminator" }, { "name": "asynchat.fifo", "domId": "asynchat_asynchat.fifo" }, { "name": "asynchat.fifo.first", "domId": "asynchat_asynchat.fifo.first" }, { "name": "asynchat.fifo.is_empty", "domId": "asynchat_asynchat.fifo.is_empty" }, { "name": "asynchat.fifo.pop", "domId": "asynchat_asynchat.fifo.pop" }, { "name": "asynchat.fifo.push", "domId": "asynchat_asynchat.fifo.push" } ] }, { "url": "http://docs.python.org/library/mailcap.html", "title": "mailcap", "html": "
\n

18.3. mailcap — Mailcap file handling

\n

Source code: Lib/mailcap.py

\n
\n

Mailcap files are used to configure how MIME-aware applications such as mail\nreaders and Web browsers react to files with different MIME types. (The name\n“mailcap” is derived from the phrase “mail capability”.) For example, a mailcap\nfile might contain a line like video/mpeg; xmpeg %s. Then, if the user\nencounters an email message or Web document with the MIME type\nvideo/mpeg, %s will be replaced by a filename (usually one\nbelonging to a temporary file) and the xmpeg program can be\nautomatically started to view the file.

\n

The mailcap format is documented in RFC 1524, “A User Agent Configuration\nMechanism For Multimedia Mail Format Information,” but is not an Internet\nstandard. However, mailcap files are supported on most Unix systems.

\n
\n
\nmailcap.findmatch(caps, MIMEtype[, key[, filename[, plist]]])
\n

Return a 2-tuple; the first element is a string containing the command line to\nbe executed (which can be passed to os.system()), and the second element\nis the mailcap entry for a given MIME type. If no matching MIME type can be\nfound, (None, None) is returned.

\n

key is the name of the field desired, which represents the type of activity to\nbe performed; the default value is ‘view’, since in the most common case you\nsimply want to view the body of the MIME-typed data. Other possible values\nmight be ‘compose’ and ‘edit’, if you wanted to create a new body of the given\nMIME type or alter the existing body data. See RFC 1524 for a complete list\nof these fields.

\n

filename is the filename to be substituted for %s in the command line; the\ndefault value is '/dev/null' which is almost certainly not what you want, so\nusually you’ll override it by specifying a filename.

\n

plist can be a list containing named parameters; the default value is simply\nan empty list. Each entry in the list must be a string containing the parameter\nname, an equals sign ('='), and the parameter’s value. Mailcap entries can\ncontain named parameters like %{foo}, which will be replaced by the value\nof the parameter named ‘foo’. For example, if the command line showpartial\n%{id} %{number} %{total} was in a mailcap file, and plist was set to\n['id=1', 'number=2', 'total=3'], the resulting command line would be\n'showpartial 1 2 3'.

\n

In a mailcap file, the “test” field can optionally be specified to test some\nexternal condition (such as the machine architecture, or the window system in\nuse) to determine whether or not the mailcap line applies. findmatch()\nwill automatically check such conditions and skip the entry if the check fails.

\n
\n\n
\n
\nmailcap.getcaps()
\n

Returns a dictionary mapping MIME types to a list of mailcap file entries. This\ndictionary must be passed to the findmatch() function. An entry is stored\nas a list of dictionaries, but it shouldn’t be necessary to know the details of\nthis representation.

\n

The information is derived from all of the mailcap files found on the system.\nSettings in the user’s mailcap file $HOME/.mailcap will override\nsettings in the system mailcap files /etc/mailcap,\n/usr/etc/mailcap, and /usr/local/etc/mailcap.

\n
\n\n

An example usage:

\n
>>> import mailcap\n>>> d=mailcap.getcaps()\n>>> mailcap.findmatch(d, 'video/mpeg', filename='/tmp/tmp1223')\n('xmpeg /tmp/tmp1223', {'view': 'xmpeg %s'})\n
\n
\n
", "searchableItems": [ { "name": "mailcap.findmatch", "domId": "mailcap_mailcap.findmatch" }, { "name": "mailcap.getcaps", "domId": "mailcap_mailcap.getcaps" } ] }, { "url": "http://docs.python.org/library/email.html", "title": "email", "html": "
\n

18.1. email — An email and MIME handling package

\n

\nNew in version 2.2.

\n

The email package is a library for managing email messages, including\nMIME and other RFC 2822-based message documents. It subsumes most of the\nfunctionality in several older standard modules such as rfc822,\nmimetools, multifile, and other non-standard packages such as\nmimecntl. It is specifically not designed to do any sending of email\nmessages to SMTP (RFC 2821), NNTP, or other servers; those are functions of\nmodules such as smtplib and nntplib. The email package\nattempts to be as RFC-compliant as possible, supporting in addition to\nRFC 2822, such MIME-related RFCs as RFC 2045, RFC 2046, RFC 2047,\nand RFC 2231.

\n

The primary distinguishing feature of the email package is that it splits\nthe parsing and generating of email messages from the internal object model\nrepresentation of email. Applications using the email package deal\nprimarily with objects; you can add sub-objects to messages, remove sub-objects\nfrom messages, completely re-arrange the contents, etc. There is a separate\nparser and a separate generator which handles the transformation from flat text\nto the object model, and then back to flat text again. There are also handy\nsubclasses for some common MIME object types, and a few miscellaneous utilities\nthat help with such common tasks as extracting and parsing message field values,\ncreating RFC-compliant dates, etc.

\n

The following sections describe the functionality of the email package.\nThe ordering follows a progression that should be common in applications: an\nemail message is read as flat text from a file or other source, the text is\nparsed to produce the object structure of the email message, this structure is\nmanipulated, and finally, the object tree is rendered back into flat text.

\n

It is perfectly feasible to create the object structure out of whole cloth —\ni.e. completely from scratch. From there, a similar progression can be taken as\nabove.

\n

Also included are detailed specifications of all the classes and modules that\nthe email package provides, the exception classes you might encounter\nwhile using the email package, some auxiliary utilities, and a few\nexamples. For users of the older mimelib package, or previous versions\nof the email package, a section on differences and porting is provided.

\n

Contents of the email package documentation:

\n\n
\n

See also

\n
\n
Module smtplib
\n
SMTP protocol client
\n
Module nntplib
\n
NNTP protocol client
\n
\n
\n
\n

18.1.12. Package History

\n

This table describes the release history of the email package, corresponding to\nthe version of Python that the package was released with. For purposes of this\ndocument, when you see a note about change or added versions, these refer to the\nPython version the change was made in, not the email package version. This\ntable also describes the Python compatibility of each version of the package.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
email versiondistributed withcompatible with
1.xPython 2.2.0 to Python 2.2.1no longer supported
2.5Python 2.2.2+ and Python 2.3Python 2.1 to 2.5
3.0Python 2.4Python 2.3 to 2.5
4.0Python 2.5Python 2.3 to 2.5
\n

Here are the major differences between email version 4 and version 3:

\n\n

Here are the major differences between email version 3 and version 2:

\n\n

Here are the differences between email version 2 and version 1:

\n\n
\n
\n

18.1.13. Differences from mimelib

\n

The email package was originally prototyped as a separate library called\nmimelib. Changes have been made so that method names\nare more consistent, and some methods or modules have either been added or\nremoved. The semantics of some of the methods have also changed. For the most\npart, any functionality available in mimelib is still available in the\nemail package, albeit often in a different way. Backward compatibility\nbetween the mimelib package and the email package was not a\npriority.

\n

Here is a brief description of the differences between the mimelib and\nthe email packages, along with hints on how to port your applications.

\n

Of course, the most visible difference between the two packages is that the\npackage name has been changed to email. In addition, the top-level\npackage has the following differences:

\n\n

The Message class has the following differences:

\n\n

The Parser class has no differences in its public interface. It does\nhave some additional smarts to recognize message/delivery-status\ntype messages, which it represents as a Message instance containing\nseparate Message subparts for each header block in the delivery status\nnotification [1].

\n

The Generator class has no differences in its public interface. There\nis a new class in the email.generator module though, called\nDecodedGenerator which provides most of the functionality previously\navailable in the Message.getpayloadastext() method.

\n

The following modules and classes have been changed:

\n\n

mimelib provided some utility functions in its address and\ndate modules. All of these functions have been moved to the\nemail.utils module.

\n

The MsgReader class/module has been removed. Its functionality is most\nclosely supported in the body_line_iterator() function in the\nemail.iterators module.

\n

Footnotes

\n\n\n\n\n\n
[1]Delivery Status Notifications (DSN) are defined in RFC 1894.
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/json.html", "title": "json", "html": "
\n

18.2. json — JSON encoder and decoder

\n

\nNew in version 2.6.

\n

JSON (JavaScript Object Notation) is a subset of JavaScript\nsyntax (ECMA-262 3rd edition) used as a lightweight data interchange format.

\n

json exposes an API familiar to users of the standard library\nmarshal and pickle modules.

\n

Encoding basic Python object hierarchies:

\n
>>> import json\n>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])\n'["foo", {"bar": ["baz", null, 1.0, 2]}]'\n>>> print json.dumps("\\"foo\\bar")\n"\\"foo\\bar"\n>>> print json.dumps(u'\\u1234')\n"\\u1234"\n>>> print json.dumps('\\\\')\n"\\\\"\n>>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)\n{"a": 0, "b": 0, "c": 0}\n>>> from StringIO import StringIO\n>>> io = StringIO()\n>>> json.dump(['streaming API'], io)\n>>> io.getvalue()\n'["streaming API"]'\n
\n
\n

Compact encoding:

\n
>>> import json\n>>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))\n'[1,2,3,{"4":5,"6":7}]'\n
\n
\n

Pretty printing:

\n
>>> import json\n>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)\n{\n    "4": 5,\n    "6": 7\n}\n
\n
\n

Decoding JSON:

\n
>>> import json\n>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')\n[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]\n>>> json.loads('"\\\\"foo\\\\bar"')\nu'"foo\\x08ar'\n>>> from StringIO import StringIO\n>>> io = StringIO('["streaming API"]')\n>>> json.load(io)\n[u'streaming API']\n
\n
\n

Specializing JSON object decoding:

\n
>>> import json\n>>> def as_complex(dct):\n...     if '__complex__' in dct:\n...         return complex(dct['real'], dct['imag'])\n...     return dct\n...\n>>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',\n...     object_hook=as_complex)\n(1+2j)\n>>> import decimal\n>>> json.loads('1.1', parse_float=decimal.Decimal)\nDecimal('1.1')\n
\n
\n

Extending JSONEncoder:

\n
>>> import json\n>>> class ComplexEncoder(json.JSONEncoder):\n...     def default(self, obj):\n...         if isinstance(obj, complex):\n...             return [obj.real, obj.imag]\n...         return json.JSONEncoder.default(self, obj)\n...\n>>> dumps(2 + 1j, cls=ComplexEncoder)\n'[2.0, 1.0]'\n>>> ComplexEncoder().encode(2 + 1j)\n'[2.0, 1.0]'\n>>> list(ComplexEncoder().iterencode(2 + 1j))\n['[', '2.0', ', ', '1.0', ']']\n
\n
\n

Using json.tool from the shell to validate and pretty-print:

\n
$ echo '{"json":"obj"}' | python -mjson.tool\n{\n    "json": "obj"\n}\n$ echo '{ 1.2:3.4}' | python -mjson.tool\nExpecting property name: line 1 column 2 (char 2)\n
\n
\n
\n

Note

\n

The JSON produced by this module’s default settings is a subset of\nYAML, so it may be used as a serializer for that as well.

\n
\n
\n

18.2.1. Basic Usage

\n
\n
\njson.dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
\n

Serialize obj as a JSON formatted stream to fp (a .write()-supporting\nfile-like object).

\n

If skipkeys is True (default: False), then dict keys that are not\nof a basic type (str, unicode, int, long,\nfloat, bool, None) will be skipped instead of raising a\nTypeError.

\n

If ensure_ascii is False (default: True), then some chunks written\nto fp may be unicode instances, subject to normal Python\nstr to unicode coercion rules. Unless fp.write()\nexplicitly understands unicode (as in codecs.getwriter()) this\nis likely to cause an error.

\n

If check_circular is False (default: True), then the circular\nreference check for container types will be skipped and a circular reference\nwill result in an OverflowError (or worse).

\n

If allow_nan is False (default: True), then it will be a\nValueError to serialize out of range float values (nan,\ninf, -inf) in strict compliance of the JSON specification, instead of\nusing the JavaScript equivalents (NaN, Infinity, -Infinity).

\n

If indent is a non-negative integer, then JSON array elements and object\nmembers will be pretty-printed with that indent level. An indent level of 0,\nor negative, will only insert newlines. None (the default) selects the\nmost compact representation.

\n

If separators is an (item_separator, dict_separator) tuple, then it\nwill be used instead of the default (', ', ': ') separators. (',',\n':') is the most compact JSON representation.

\n

encoding is the character encoding for str instances, default is UTF-8.

\n

default(obj) is a function that should return a serializable version of\nobj or raise TypeError. The default simply raises TypeError.

\n

To use a custom JSONEncoder subclass (e.g. one that overrides the\ndefault() method to serialize additional types), specify it with the\ncls kwarg; otherwise JSONEncoder is used.

\n
\n

Note

\n

Unlike pickle and marshal, JSON is not a framed protocol so\ntrying to serialize more objects with repeated calls to dump() and\nthe same fp will result in an invalid JSON file.

\n
\n
\n\n
\n
\njson.dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
\n

Serialize obj to a JSON formatted str.

\n

If ensure_ascii is False, then the return value will be a\nunicode instance. The other arguments have the same meaning as in\ndump().

\n
\n\n
\n
\njson.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
\n

Deserialize fp (a .read()-supporting file-like object containing a JSON\ndocument) to a Python object.

\n

If the contents of fp are encoded with an ASCII based encoding other than\nUTF-8 (e.g. latin-1), then an appropriate encoding name must be specified.\nEncodings that are not ASCII based (such as UCS-2) are not allowed, and\nshould be wrapped with codecs.getreader(encoding)(fp), or simply decoded\nto a unicode object and passed to loads().

\n

object_hook is an optional function that will be called with the result of\nany object literal decoded (a dict). The return value of\nobject_hook will be used instead of the dict. This feature can be used\nto implement custom decoders (e.g. JSON-RPC class hinting).

\n

object_pairs_hook is an optional function that will be called with the\nresult of any object literal decoded with an ordered list of pairs. The\nreturn value of object_pairs_hook will be used instead of the\ndict. This feature can be used to implement custom decoders that\nrely on the order that the key and value pairs are decoded (for example,\ncollections.OrderedDict() will remember the order of insertion). If\nobject_hook is also defined, the object_pairs_hook takes priority.

\n

\nChanged in version 2.7: Added support for object_pairs_hook.

\n

parse_float, if specified, will be called with the string of every JSON\nfloat to be decoded. By default, this is equivalent to float(num_str).\nThis can be used to use another datatype or parser for JSON floats\n(e.g. decimal.Decimal).

\n

parse_int, if specified, will be called with the string of every JSON int\nto be decoded. By default, this is equivalent to int(num_str). This can\nbe used to use another datatype or parser for JSON integers\n(e.g. float).

\n

parse_constant, if specified, will be called with one of the following\nstrings: '-Infinity', 'Infinity', 'NaN', 'null', 'true',\n'false'. This can be used to raise an exception if invalid JSON numbers\nare encountered.

\n

To use a custom JSONDecoder subclass, specify it with the cls\nkwarg; otherwise JSONDecoder is used. Additional keyword arguments\nwill be passed to the constructor of the class.

\n
\n\n
\n
\njson.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
\n

Deserialize s (a str or unicode instance containing a JSON\ndocument) to a Python object.

\n

If s is a str instance and is encoded with an ASCII based encoding\nother than UTF-8 (e.g. latin-1), then an appropriate encoding name must be\nspecified. Encodings that are not ASCII based (such as UCS-2) are not\nallowed and should be decoded to unicode first.

\n

The other arguments have the same meaning as in load().

\n
\n\n
\n
\n

18.2.2. Encoders and decoders

\n
\n
\nclass json.JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, strict[, object_pairs_hook]]]]]]])
\n

Simple JSON decoder.

\n

Performs the following translations in decoding by default:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
JSONPython
objectdict
arraylist
stringunicode
number (int)int, long
number (real)float
trueTrue
falseFalse
nullNone
\n

It also understands NaN, Infinity, and -Infinity as their\ncorresponding float values, which is outside the JSON spec.

\n

encoding determines the encoding used to interpret any str objects\ndecoded by this instance (UTF-8 by default). It has no effect when decoding\nunicode objects.

\n

Note that currently only encodings that are a superset of ASCII work, strings\nof other encodings should be passed in as unicode.

\n

object_hook, if specified, will be called with the result of every JSON\nobject decoded and its return value will be used in place of the given\ndict. This can be used to provide custom deserializations (e.g. to\nsupport JSON-RPC class hinting).

\n

object_pairs_hook, if specified will be called with the result of every\nJSON object decoded with an ordered list of pairs. The return value of\nobject_pairs_hook will be used instead of the dict. This\nfeature can be used to implement custom decoders that rely on the order\nthat the key and value pairs are decoded (for example,\ncollections.OrderedDict() will remember the order of insertion). If\nobject_hook is also defined, the object_pairs_hook takes priority.

\n

\nChanged in version 2.7: Added support for object_pairs_hook.

\n

parse_float, if specified, will be called with the string of every JSON\nfloat to be decoded. By default, this is equivalent to float(num_str).\nThis can be used to use another datatype or parser for JSON floats\n(e.g. decimal.Decimal).

\n

parse_int, if specified, will be called with the string of every JSON int\nto be decoded. By default, this is equivalent to int(num_str). This can\nbe used to use another datatype or parser for JSON integers\n(e.g. float).

\n

parse_constant, if specified, will be called with one of the following\nstrings: '-Infinity', 'Infinity', 'NaN', 'null', 'true',\n'false'. This can be used to raise an exception if invalid JSON numbers\nare encountered.

\n

If strict is False (True is the default), then control characters\nwill be allowed inside strings. Control characters in this context are\nthose with character codes in the 0-31 range, including '\\t' (tab),\n'\\n', '\\r' and '\\0'.

\n
\n
\ndecode(s)
\n
Return the Python representation of s (a str or\nunicode instance containing a JSON document)
\n\n
\n
\nraw_decode(s)
\n

Decode a JSON document from s (a str or unicode\nbeginning with a JSON document) and return a 2-tuple of the Python\nrepresentation and the index in s where the document ended.

\n

This can be used to decode a JSON document from a string that may have\nextraneous data at the end.

\n
\n\n
\n\n
\n
\nclass json.JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
\n

Extensible JSON encoder for Python data structures.

\n

Supports the following objects and types by default:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
PythonJSON
dictobject
list, tuplearray
str, unicodestring
int, long, floatnumber
Truetrue
Falsefalse
Nonenull
\n

To extend this to recognize other objects, subclass and implement a\ndefault() method with another method that returns a serializable object\nfor o if possible, otherwise it should call the superclass implementation\n(to raise TypeError).

\n

If skipkeys is False (the default), then it is a TypeError to\nattempt encoding of keys that are not str, int, long, float or None. If\nskipkeys is True, such items are simply skipped.

\n

If ensure_ascii is True (the default), the output is guaranteed to be\nstr objects with all incoming unicode characters escaped. If\nensure_ascii is False, the output will be a unicode object.

\n

If check_circular is True (the default), then lists, dicts, and custom\nencoded objects will be checked for circular references during encoding to\nprevent an infinite recursion (which would cause an OverflowError).\nOtherwise, no such check takes place.

\n

If allow_nan is True (the default), then NaN, Infinity, and\n-Infinity will be encoded as such. This behavior is not JSON\nspecification compliant, but is consistent with most JavaScript based\nencoders and decoders. Otherwise, it will be a ValueError to encode\nsuch floats.

\n

If sort_keys is True (default False), then the output of dictionaries\nwill be sorted by key; this is useful for regression tests to ensure that\nJSON serializations can be compared on a day-to-day basis.

\n

If indent is a non-negative integer (it is None by default), then JSON\narray elements and object members will be pretty-printed with that indent\nlevel. An indent level of 0 will only insert newlines. None is the most\ncompact representation.

\n

If specified, separators should be an (item_separator, key_separator)\ntuple. The default is (', ', ': '). To get the most compact JSON\nrepresentation, you should specify (',', ':') to eliminate whitespace.

\n

If specified, default is a function that gets called for objects that can’t\notherwise be serialized. It should return a JSON encodable version of the\nobject or raise a TypeError.

\n

If encoding is not None, then all input strings will be transformed\ninto unicode using that encoding prior to JSON-encoding. The default is\nUTF-8.

\n
\n
\ndefault(o)
\n

Implement this method in a subclass such that it returns a serializable\nobject for o, or calls the base implementation (to raise a\nTypeError).

\n

For example, to support arbitrary iterators, you could implement default\nlike this:

\n
def default(self, o):\n   try:\n       iterable = iter(o)\n   except TypeError:\n       pass\n   else:\n       return list(iterable)\n   return JSONEncoder.default(self, o)\n
\n
\n
\n\n
\n
\nencode(o)
\n

Return a JSON string representation of a Python data structure, o. For\nexample:

\n
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})\n'{"foo": ["bar", "baz"]}'\n
\n
\n
\n\n
\n
\niterencode(o)
\n

Encode the given object, o, and yield each string representation as\navailable. For example:

\n
for chunk in JSONEncoder().iterencode(bigobject):\n    mysocket.write(chunk)\n
\n
\n
\n\n
\n\n
\n
", "searchableItems": [ { "name": "json.dump", "domId": "json_json.dump" }, { "name": "json.dumps", "domId": "json_json.dumps" }, { "name": "json.JSONDecoder", "domId": "json_json.JSONDecoder" }, { "name": "json.JSONDecoder.decode", "domId": "json_json.JSONDecoder.decode" }, { "name": "json.JSONDecoder.raw_decode", "domId": "json_json.JSONDecoder.raw_decode" }, { "name": "json.JSONEncoder", "domId": "json_json.JSONEncoder" }, { "name": "json.JSONEncoder.default", "domId": "json_json.JSONEncoder.default" }, { "name": "json.JSONEncoder.encode", "domId": "json_json.JSONEncoder.encode" }, { "name": "json.JSONEncoder.iterencode", "domId": "json_json.JSONEncoder.iterencode" }, { "name": "json.load", "domId": "json_json.load" }, { "name": "json.loads", "domId": "json_json.loads" } ] }, { "url": "http://docs.python.org/library/mimetools.html", "title": "mimetools", "html": "
\n

18.6. mimetools — Tools for parsing MIME messages

\n

\nDeprecated since version 2.3: The email package should be used in preference to the mimetools\nmodule. This module is present only to maintain backward compatibility, and\nit has been removed in 3.x.

\n

This module defines a subclass of the rfc822 module’s Message\nclass and a number of utility functions that are useful for the manipulation for\nMIME multipart or encoded message.

\n

It defines the following items:

\n
\n
\nclass mimetools.Message(fp[, seekable])
\n
Return a new instance of the Message class. This is a subclass of the\nrfc822.Message class, with some additional methods (see below). The\nseekable argument has the same meaning as for rfc822.Message.
\n\n
\n
\nmimetools.choose_boundary()
\n
Return a unique string that has a high likelihood of being usable as a part\nboundary. The string has the form 'hostipaddr.uid.pid.timestamp.random'.
\n\n
\n
\nmimetools.decode(input, output, encoding)
\n
Read data encoded using the allowed MIME encoding from open file object\ninput and write the decoded data to open file object output. Valid values\nfor encoding include 'base64', 'quoted-printable', 'uuencode',\n'x-uuencode', 'uue', 'x-uue', '7bit', and '8bit'. Decoding\nmessages encoded in '7bit' or '8bit' has no effect. The input is simply\ncopied to the output.
\n\n
\n
\nmimetools.encode(input, output, encoding)
\n
Read data from open file object input and write it encoded using the allowed\nMIME encoding to open file object output. Valid values for encoding are\nthe same as for decode().
\n\n
\n
\nmimetools.copyliteral(input, output)
\n
Read lines from open file input until EOF and write them to open file\noutput.
\n\n
\n
\nmimetools.copybinary(input, output)
\n
Read blocks until EOF from open file input and write them to open file\noutput. The block size is currently fixed at 8192.
\n\n
\n

See also

\n
\n
Module email
\n
Comprehensive email handling package; supersedes the mimetools module.
\n
Module rfc822
\n
Provides the base class for mimetools.Message.
\n
Module multifile
\n
Support for reading files which contain distinct parts, such as MIME data.
\n
http://faqs.cs.uu.nl/na-dir/mail/mime-faq/.html
\n
The MIME Frequently Asked Questions document. For an overview of MIME, see the\nanswer to question 1.1 in Part 1 of this document.
\n
\n
\n
\n

18.6.1. Additional Methods of Message Objects

\n

The Message class defines the following methods in addition to the\nrfc822.Message methods:

\n
\n
\nMessage.getplist()
\n
Return the parameter list of the Content-Type header. This is a\nlist of strings. For parameters of the form key=value, key is converted\nto lower case but value is not. For example, if the message contains the\nheader Content-type: text/html; spam=1; Spam=2; Spam then getplist()\nwill return the Python list ['spam=1', 'spam=2', 'Spam'].
\n\n
\n
\nMessage.getparam(name)
\n
Return the value of the first parameter (as returned by getplist()) of\nthe form name=value for the given name. If value is surrounded by\nquotes of the form ‘<...>‘ or ‘"..."‘, these are removed.
\n\n
\n
\nMessage.getencoding()
\n
Return the encoding specified in the Content-Transfer-Encoding\nmessage header. If no such header exists, return '7bit'. The encoding is\nconverted to lower case.
\n\n
\n
\nMessage.gettype()
\n
Return the message type (of the form type/subtype) as specified in the\nContent-Type header. If no such header exists, return\n'text/plain'. The type is converted to lower case.
\n\n
\n
\nMessage.getmaintype()
\n
Return the main type as specified in the Content-Type header. If\nno such header exists, return 'text'. The main type is converted to lower\ncase.
\n\n
\n
\nMessage.getsubtype()
\n
Return the subtype as specified in the Content-Type header. If no\nsuch header exists, return 'plain'. The subtype is converted to lower case.
\n\n
\n
", "searchableItems": [ { "name": "mimetools.choose_boundary", "domId": "mimetools_mimetools.choose_boundary" }, { "name": "mimetools.copybinary", "domId": "mimetools_mimetools.copybinary" }, { "name": "mimetools.copyliteral", "domId": "mimetools_mimetools.copyliteral" }, { "name": "mimetools.decode", "domId": "mimetools_mimetools.decode" }, { "name": "mimetools.encode", "domId": "mimetools_mimetools.encode" }, { "name": "mimetools.Message", "domId": "mimetools_mimetools.Message" }, { "name": "mimetools.Message.getencoding", "domId": "mimetools_mimetools.Message.getencoding" }, { "name": "mimetools.Message.getmaintype", "domId": "mimetools_mimetools.Message.getmaintype" }, { "name": "mimetools.Message.getparam", "domId": "mimetools_mimetools.Message.getparam" }, { "name": "mimetools.Message.getplist", "domId": "mimetools_mimetools.Message.getplist" }, { "name": "mimetools.Message.getsubtype", "domId": "mimetools_mimetools.Message.getsubtype" }, { "name": "mimetools.Message.gettype", "domId": "mimetools_mimetools.Message.gettype" } ] }, { "url": "http://docs.python.org/library/mhlib.html", "title": "mhlib", "html": "
\n

18.5. mhlib — Access to MH mailboxes

\n

\nDeprecated since version 2.6: The mhlib module has been removed in Python 3.0. Use the\nmailbox instead.

\n

The mhlib module provides a Python interface to MH folders and their\ncontents.

\n

The module contains three basic classes, MH, which represents a\nparticular collection of folders, Folder, which represents a single\nfolder, and Message, which represents a single message.

\n
\n
\nclass mhlib.MH([path[, profile]])
\n
MH represents a collection of MH folders.
\n\n
\n
\nclass mhlib.Folder(mh, name)
\n
The Folder class represents a single folder and its messages.
\n\n
\n
\nclass mhlib.Message(folder, number[, name])
\n
Message objects represent individual messages in a folder. The Message\nclass is derived from mimetools.Message.
\n\n
\n

18.5.1. MH Objects

\n

MH instances have the following methods:

\n
\n
\nMH.error(format[, ...])
\n
Print an error message – can be overridden.
\n\n
\n
\nMH.getprofile(key)
\n
Return a profile entry (None if not set).
\n\n
\n
\nMH.getpath()
\n
Return the mailbox pathname.
\n\n
\n
\nMH.getcontext()
\n
Return the current folder name.
\n\n
\n
\nMH.setcontext(name)
\n
Set the current folder name.
\n\n
\n
\nMH.listfolders()
\n
Return a list of top-level folders.
\n\n
\n
\nMH.listallfolders()
\n
Return a list of all folders.
\n\n
\n
\nMH.listsubfolders(name)
\n
Return a list of direct subfolders of the given folder.
\n\n
\n
\nMH.listallsubfolders(name)
\n
Return a list of all subfolders of the given folder.
\n\n
\n
\nMH.makefolder(name)
\n
Create a new folder.
\n\n
\n
\nMH.deletefolder(name)
\n
Delete a folder – must have no subfolders.
\n\n
\n
\nMH.openfolder(name)
\n
Return a new open folder object.
\n\n
\n
\n

18.5.2. Folder Objects

\n

Folder instances represent open folders and have the following methods:

\n
\n
\nFolder.error(format[, ...])
\n
Print an error message – can be overridden.
\n\n
\n
\nFolder.getfullname()
\n
Return the folder’s full pathname.
\n\n
\n
\nFolder.getsequencesfilename()
\n
Return the full pathname of the folder’s sequences file.
\n\n
\n
\nFolder.getmessagefilename(n)
\n
Return the full pathname of message n of the folder.
\n\n
\n
\nFolder.listmessages()
\n
Return a list of messages in the folder (as numbers).
\n\n
\n
\nFolder.getcurrent()
\n
Return the current message number.
\n\n
\n
\nFolder.setcurrent(n)
\n
Set the current message number to n.
\n\n
\n
\nFolder.parsesequence(seq)
\n
Parse msgs syntax into list of messages.
\n\n
\n
\nFolder.getlast()
\n
Get last message, or 0 if no messages are in the folder.
\n\n
\n
\nFolder.setlast(n)
\n
Set last message (internal use only).
\n\n
\n
\nFolder.getsequences()
\n
Return dictionary of sequences in folder. The sequence names are used as keys,\nand the values are the lists of message numbers in the sequences.
\n\n
\n
\nFolder.putsequences(dict)
\n
Return dictionary of sequences in folder name: list.
\n\n
\n
\nFolder.removemessages(list)
\n
Remove messages in list from folder.
\n\n
\n
\nFolder.refilemessages(list, tofolder)
\n
Move messages in list to other folder.
\n\n
\n
\nFolder.movemessage(n, tofolder, ton)
\n
Move one message to a given destination in another folder.
\n\n
\n
\nFolder.copymessage(n, tofolder, ton)
\n
Copy one message to a given destination in another folder.
\n\n
\n
\n

18.5.3. Message Objects

\n

The Message class adds one method to those of\nmimetools.Message:

\n
\n
\nMessage.openmessage(n)
\n
Return a new open message object (costs a file descriptor).
\n\n
\n
", "searchableItems": [ { "name": "mhlib.Folder", "domId": "mhlib_mhlib.Folder" }, { "name": "mhlib.Folder.copymessage", "domId": "mhlib_mhlib.Folder.copymessage" }, { "name": "mhlib.Folder.error", "domId": "mhlib_mhlib.Folder.error" }, { "name": "mhlib.Folder.getcurrent", "domId": "mhlib_mhlib.Folder.getcurrent" }, { "name": "mhlib.Folder.getfullname", "domId": "mhlib_mhlib.Folder.getfullname" }, { "name": "mhlib.Folder.getlast", "domId": "mhlib_mhlib.Folder.getlast" }, { "name": "mhlib.Folder.getmessagefilename", "domId": "mhlib_mhlib.Folder.getmessagefilename" }, { "name": "mhlib.Folder.getsequences", "domId": "mhlib_mhlib.Folder.getsequences" }, { "name": "mhlib.Folder.getsequencesfilename", "domId": "mhlib_mhlib.Folder.getsequencesfilename" }, { "name": "mhlib.Folder.listmessages", "domId": "mhlib_mhlib.Folder.listmessages" }, { "name": "mhlib.Folder.movemessage", "domId": "mhlib_mhlib.Folder.movemessage" }, { "name": "mhlib.Folder.parsesequence", "domId": "mhlib_mhlib.Folder.parsesequence" }, { "name": "mhlib.Folder.putsequences", "domId": "mhlib_mhlib.Folder.putsequences" }, { "name": "mhlib.Folder.refilemessages", "domId": "mhlib_mhlib.Folder.refilemessages" }, { "name": "mhlib.Folder.removemessages", "domId": "mhlib_mhlib.Folder.removemessages" }, { "name": "mhlib.Folder.setcurrent", "domId": "mhlib_mhlib.Folder.setcurrent" }, { "name": "mhlib.Folder.setlast", "domId": "mhlib_mhlib.Folder.setlast" }, { "name": "mhlib.Message", "domId": "mhlib_mhlib.Message" }, { "name": "mhlib.Message.openmessage", "domId": "mhlib_mhlib.Message.openmessage" }, { "name": "mhlib.MH", "domId": "mhlib_mhlib.MH" }, { "name": "mhlib.MH.deletefolder", "domId": "mhlib_mhlib.MH.deletefolder" }, { "name": "mhlib.MH.error", "domId": "mhlib_mhlib.MH.error" }, { "name": "mhlib.MH.getcontext", "domId": "mhlib_mhlib.MH.getcontext" }, { "name": "mhlib.MH.getpath", "domId": "mhlib_mhlib.MH.getpath" }, { "name": "mhlib.MH.getprofile", "domId": "mhlib_mhlib.MH.getprofile" }, { "name": "mhlib.MH.listallfolders", "domId": "mhlib_mhlib.MH.listallfolders" }, { "name": "mhlib.MH.listallsubfolders", "domId": "mhlib_mhlib.MH.listallsubfolders" }, { "name": "mhlib.MH.listfolders", "domId": "mhlib_mhlib.MH.listfolders" }, { "name": "mhlib.MH.listsubfolders", "domId": "mhlib_mhlib.MH.listsubfolders" }, { "name": "mhlib.MH.makefolder", "domId": "mhlib_mhlib.MH.makefolder" }, { "name": "mhlib.MH.openfolder", "domId": "mhlib_mhlib.MH.openfolder" }, { "name": "mhlib.MH.setcontext", "domId": "mhlib_mhlib.MH.setcontext" } ] }, { "url": "http://docs.python.org/library/mimetypes.html", "title": "mimetypes", "html": "
\n

18.7. mimetypes — Map filenames to MIME types

\n

Source code: Lib/mimetypes.py

\n
\n

The mimetypes module converts between a filename or URL and the MIME type\nassociated with the filename extension. Conversions are provided from filename\nto MIME type and from MIME type to filename extension; encodings are not\nsupported for the latter conversion.

\n

The module provides one class and a number of convenience functions. The\nfunctions are the normal interface to this module, but some applications may be\ninterested in the class as well.

\n

The functions described below provide the primary interface for this module. If\nthe module has not been initialized, they will call init() if they rely on\nthe information init() sets up.

\n
\n
\nmimetypes.guess_type(url, strict=True)
\n

Guess the type of a file based on its filename or URL, given by url. The\nreturn value is a tuple (type, encoding) where type is None if the\ntype can’t be guessed (missing or unknown suffix) or a string of the form\n'type/subtype', usable for a MIME content-type header.

\n

encoding is None for no encoding or the name of the program used to encode\n(e.g. compress or gzip). The encoding is suitable for use\nas a Content-Encoding header, not as a\nContent-Transfer-Encoding header. The mappings are table driven.\nEncoding suffixes are case sensitive; type suffixes are first tried case\nsensitively, then case insensitively.

\n

The optional strict argument is a flag specifying whether the list of known MIME types\nis limited to only the official types registered with IANA.\nWhen strict is True (the default), only the IANA types are supported; when\nstrict is False, some additional non-standard but commonly used MIME types\nare also recognized.

\n
\n\n
\n
\nmimetypes.guess_all_extensions(type, strict=True)
\n

Guess the extensions for a file based on its MIME type, given by type. The\nreturn value is a list of strings giving all possible filename extensions,\nincluding the leading dot ('.'). The extensions are not guaranteed to have\nbeen associated with any particular data stream, but would be mapped to the MIME\ntype type by guess_type().

\n

The optional strict argument has the same meaning as with the guess_type() function.

\n
\n\n
\n
\nmimetypes.guess_extension(type, strict=True)
\n

Guess the extension for a file based on its MIME type, given by type. The\nreturn value is a string giving a filename extension, including the leading dot\n('.'). The extension is not guaranteed to have been associated with any\nparticular data stream, but would be mapped to the MIME type type by\nguess_type(). If no extension can be guessed for type, None is\nreturned.

\n

The optional strict argument has the same meaning as with the guess_type() function.

\n
\n\n

Some additional functions and data items are available for controlling the\nbehavior of the module.

\n
\n
\nmimetypes.init(files=None)
\n

Initialize the internal data structures. If given, files must be a sequence\nof file names which should be used to augment the default type map. If omitted,\nthe file names to use are taken from knownfiles; on Windows, the\ncurrent registry settings are loaded. Each file named in files or\nknownfiles takes precedence over those named before it. Calling\ninit() repeatedly is allowed.

\n

\nChanged in version 2.7: Previously, Windows registry settings were ignored.

\n
\n\n
\n
\nmimetypes.read_mime_types(filename)
\n
Load the type map given in the file filename, if it exists. The type map is\nreturned as a dictionary mapping filename extensions, including the leading dot\n('.'), to strings of the form 'type/subtype'. If the file filename\ndoes not exist or cannot be read, None is returned.
\n\n
\n
\nmimetypes.add_type(type, ext, strict=True)
\n

Add a mapping from the MIME type type to the extension ext. When the\nextension is already known, the new type will replace the old one. When the type\nis already known the extension will be added to the list of known extensions.

\n

When strict is True (the default), the mapping will added to the official MIME\ntypes, otherwise to the non-standard ones.

\n
\n\n
\n
\nmimetypes.inited
\n
Flag indicating whether or not the global data structures have been initialized.\nThis is set to True by init().
\n\n
\n
\nmimetypes.knownfiles
\n

List of type map file names commonly installed. These files are typically named\nmime.types and are installed in different locations by different\npackages.

\n
\n\n
\n
\nmimetypes.suffix_map
\n
Dictionary mapping suffixes to suffixes. This is used to allow recognition of\nencoded files for which the encoding and the type are indicated by the same\nextension. For example, the .tgz extension is mapped to .tar.gz\nto allow the encoding and type to be recognized separately.
\n\n
\n
\nmimetypes.encodings_map
\n
Dictionary mapping filename extensions to encoding types.
\n\n
\n
\nmimetypes.types_map
\n
Dictionary mapping filename extensions to MIME types.
\n\n
\n
\nmimetypes.common_types
\n
Dictionary mapping filename extensions to non-standard, but commonly found MIME\ntypes.
\n\n

An example usage of the module:

\n
>>> import mimetypes\n>>> mimetypes.init()\n>>> mimetypes.knownfiles\n['/etc/mime.types', '/etc/httpd/mime.types', ... ]\n>>> mimetypes.suffix_map['.tgz']\n'.tar.gz'\n>>> mimetypes.encodings_map['.gz']\n'gzip'\n>>> mimetypes.types_map['.tgz']\n'application/x-tar-gz'\n
\n
\n
\n

18.7.1. MimeTypes Objects

\n

The MimeTypes class may be useful for applications which may want more\nthan one MIME-type database; it provides an interface similar to the one of the\nmimetypes module.

\n
\n
\nclass mimetypes.MimeTypes(filenames=(), strict=True)
\n

This class represents a MIME-types database. By default, it provides access to\nthe same database as the rest of this module. The initial database is a copy of\nthat provided by the module, and may be extended by loading additional\nmime.types-style files into the database using the read() or\nreadfp() methods. The mapping dictionaries may also be cleared before\nloading additional data if the default data is not desired.

\n

The optional filenames parameter can be used to cause additional files to be\nloaded “on top” of the default database.

\n
\n\n
\n
\nMimeTypes.suffix_map
\n
Dictionary mapping suffixes to suffixes. This is used to allow recognition of\nencoded files for which the encoding and the type are indicated by the same\nextension. For example, the .tgz extension is mapped to .tar.gz\nto allow the encoding and type to be recognized separately. This is initially a\ncopy of the global suffix_map defined in the module.
\n\n
\n
\nMimeTypes.encodings_map
\n
Dictionary mapping filename extensions to encoding types. This is initially a\ncopy of the global encodings_map defined in the module.
\n\n
\n
\nMimeTypes.types_map
\n
Tuple containing two dictionaries, mapping filename extensions to MIME types:\nthe first dictionary is for the non-standards types and the second one is for\nthe standard types. They are initialized by common_types and\ntypes_map.
\n\n
\n
\nMimeTypes.types_map_inv
\n
Tuple containing two dictionaries, mapping MIME types to a list of filename\nextensions: the first dictionary is for the non-standards types and the\nsecond one is for the standard types. They are initialized by\ncommon_types and types_map.
\n\n
\n
\nMimeTypes.guess_extension(type, strict=True)
\n
Similar to the guess_extension() function, using the tables stored as part\nof the object.
\n\n
\n
\nMimeTypes.guess_type(url, strict=True)
\n
Similar to the guess_type() function, using the tables stored as part of\nthe object.
\n\n
\n
\nMimeTypes.guess_all_extensions(type, strict=True)
\n
Similar to the guess_all_extensions() function, using the tables stored\nas part of the object.
\n\n
\n
\nMimeTypes.read(filename, strict=True)
\n

Load MIME information from a file named filename. This uses readfp() to\nparse the file.

\n

If strict is True, information will be added to list of standard types,\nelse to the list of non-standard types.

\n
\n\n
\n
\nMimeTypes.readfp(fp, strict=True)
\n

Load MIME type information from an open file fp. The file must have the format of\nthe standard mime.types files.

\n

If strict is True, information will be added to the list of standard\ntypes, else to the list of non-standard types.

\n
\n\n
\n
\nMimeTypes.read_windows_registry(strict=True)
\n

Load MIME type information from the Windows registry. Availability: Windows.

\n

If strict is True, information will be added to the list of standard\ntypes, else to the list of non-standard types.

\n

\nNew in version 2.7.

\n
\n\n
\n
", "searchableItems": [ { "name": "mimetypes.add_type", "domId": "mimetypes_mimetypes.add_type" }, { "name": "mimetypes.guess_all_extensions", "domId": "mimetypes_mimetypes.guess_all_extensions" }, { "name": "mimetypes.guess_extension", "domId": "mimetypes_mimetypes.guess_extension" }, { "name": "mimetypes.guess_type", "domId": "mimetypes_mimetypes.guess_type" }, { "name": "mimetypes.init", "domId": "mimetypes_mimetypes.init" }, { "name": "mimetypes.MimeTypes", "domId": "mimetypes_mimetypes.MimeTypes" }, { "name": "mimetypes.MimeTypes.guess_all_extensions", "domId": "mimetypes_mimetypes.MimeTypes.guess_all_extensions" }, { "name": "mimetypes.MimeTypes.guess_extension", "domId": "mimetypes_mimetypes.MimeTypes.guess_extension" }, { "name": "mimetypes.MimeTypes.guess_type", "domId": "mimetypes_mimetypes.MimeTypes.guess_type" }, { "name": "mimetypes.MimeTypes.read", "domId": "mimetypes_mimetypes.MimeTypes.read" }, { "name": "mimetypes.MimeTypes.read_windows_registry", "domId": "mimetypes_mimetypes.MimeTypes.read_windows_registry" }, { "name": "mimetypes.MimeTypes.readfp", "domId": "mimetypes_mimetypes.MimeTypes.readfp" }, { "name": "mimetypes.read_mime_types", "domId": "mimetypes_mimetypes.read_mime_types" } ] }, { "url": "http://docs.python.org/library/mimewriter.html", "title": "MimeWriter", "html": "
\n

18.8. MimeWriter — Generic MIME file writer

\n

\nDeprecated since version 2.3: The email package should be used in preference to the MimeWriter\nmodule. This module is present only to maintain backward compatibility.

\n

This module defines the class MimeWriter. The MimeWriter\nclass implements a basic formatter for creating MIME multi-part files. It\ndoesn’t seek around the output file nor does it use large amounts of buffer\nspace. You must write the parts out in the order that they should occur in the\nfinal file. MimeWriter does buffer the headers you add, allowing you\nto rearrange their order.

\n
\n
\nclass MimeWriter.MimeWriter(fp)
\n
Return a new instance of the MimeWriter class. The only argument\npassed, fp, is a file object to be used for writing. Note that a\nStringIO object could also be used.
\n\n
\n

18.8.1. MimeWriter Objects

\n

MimeWriter instances have the following methods:

\n
\n
\nMimeWriter.addheader(key, value[, prefix])
\n
Add a header line to the MIME message. The key is the name of the header,\nwhere the value obviously provides the value of the header. The optional\nargument prefix determines where the header is inserted; 0 means append\nat the end, 1 is insert at the start. The default is to append.
\n\n
\n
\nMimeWriter.flushheaders()
\n
Causes all headers accumulated so far to be written out (and forgotten). This is\nuseful if you don’t need a body part at all, e.g. for a subpart of type\nmessage/rfc822 that’s (mis)used to store some header-like\ninformation.
\n\n
\n
\nMimeWriter.startbody(ctype[, plist[, prefix]])
\n
Returns a file-like object which can be used to write to the body of the\nmessage. The content-type is set to the provided ctype, and the optional\nparameter plist provides additional parameters for the content-type\ndeclaration. prefix functions as in addheader() except that the default\nis to insert at the start.
\n\n
\n
\nMimeWriter.startmultipartbody(subtype[, boundary[, plist[, prefix]]])
\n
Returns a file-like object which can be used to write to the body of the\nmessage. Additionally, this method initializes the multi-part code, where\nsubtype provides the multipart subtype, boundary may provide a user-defined\nboundary specification, and plist provides optional parameters for the\nsubtype. prefix functions as in startbody(). Subparts should be created\nusing nextpart().
\n\n
\n
\nMimeWriter.nextpart()
\n
Returns a new instance of MimeWriter which represents an individual\npart in a multipart message. This may be used to write the part as well as\nused for creating recursively complex multipart messages. The message must first\nbe initialized with startmultipartbody() before using nextpart().
\n\n
\n
\nMimeWriter.lastpart()
\n
This is used to designate the last part of a multipart message, and should\nalways be used when writing multipart messages.
\n\n
\n
", "searchableItems": [ { "name": "MimeWriter.MimeWriter", "domId": "MimeWriter_MimeWriter.MimeWriter" }, { "name": "MimeWriter.MimeWriter.addheader", "domId": "MimeWriter_MimeWriter.MimeWriter.addheader" }, { "name": "MimeWriter.MimeWriter.flushheaders", "domId": "MimeWriter_MimeWriter.MimeWriter.flushheaders" }, { "name": "MimeWriter.MimeWriter.lastpart", "domId": "MimeWriter_MimeWriter.MimeWriter.lastpart" }, { "name": "MimeWriter.MimeWriter.nextpart", "domId": "MimeWriter_MimeWriter.MimeWriter.nextpart" }, { "name": "MimeWriter.MimeWriter.startbody", "domId": "MimeWriter_MimeWriter.MimeWriter.startbody" }, { "name": "MimeWriter.MimeWriter.startmultipartbody", "domId": "MimeWriter_MimeWriter.MimeWriter.startmultipartbody" } ] }, { "url": "http://docs.python.org/library/mimify.html", "title": "mimify", "html": "
\n

18.9. mimify — MIME processing of mail messages

\n

\nDeprecated since version 2.3: The email package should be used in preference to the mimify\nmodule. This module is present only to maintain backward compatibility.

\n

The mimify module defines two functions to convert mail messages to and\nfrom MIME format. The mail message can be either a simple message or a\nso-called multipart message. Each part is treated separately. Mimifying (a part\nof) a message entails encoding the message as quoted-printable if it contains\nany characters that cannot be represented using 7-bit ASCII. Unmimifying (a\npart of) a message entails undoing the quoted-printable encoding. Mimify and\nunmimify are especially useful when a message has to be edited before being\nsent. Typical use would be:

\n
unmimify message\nedit message\nmimify message\nsend message
\n
\n

The modules defines the following user-callable functions and user-settable\nvariables:

\n
\n
\nmimify.mimify(infile, outfile)
\n
Copy the message in infile to outfile, converting parts to quoted-printable\nand adding MIME mail headers when necessary. infile and outfile can be file\nobjects (actually, any object that has a readline() method (for infile)\nor a write() method (for outfile)) or strings naming the files. If\ninfile and outfile are both strings, they may have the same value.
\n\n
\n
\nmimify.unmimify(infile, outfile[, decode_base64])
\n
Copy the message in infile to outfile, decoding all quoted-printable parts.\ninfile and outfile can be file objects (actually, any object that has a\nreadline() method (for infile) or a write() method (for\noutfile)) or strings naming the files. If infile and outfile are both\nstrings, they may have the same value. If the decode_base64 argument is\nprovided and tests true, any parts that are coded in the base64 encoding are\ndecoded as well.
\n\n
\n
\nmimify.mime_decode_header(line)
\n
Return a decoded version of the encoded header line in line. This only\nsupports the ISO 8859-1 charset (Latin-1).
\n\n
\n
\nmimify.mime_encode_header(line)
\n
Return a MIME-encoded version of the header line in line.
\n\n
\n
\nmimify.MAXLEN
\n
By default, a part will be encoded as quoted-printable when it contains any\nnon-ASCII characters (characters with the 8th bit set), or if there are any\nlines longer than MAXLEN characters (default value 200).
\n\n
\n
\nmimify.CHARSET
\n
When not specified in the mail headers, a character set must be filled in. The\nstring used is stored in CHARSET, and the default value is ISO-8859-1\n(also known as Latin1 (latin-one)).
\n\n

This module can also be used from the command line. Usage is as follows:

\n
mimify.py -e [-l length] [infile [outfile]]\nmimify.py -d [-b] [infile [outfile]]
\n
\n

to encode (mimify) and decode (unmimify) respectively. infile defaults to\nstandard input, outfile defaults to standard output. The same file can be\nspecified for input and output.

\n

If the -l option is given when encoding, if there are any lines longer than\nthe specified length, the containing part will be encoded.

\n

If the -b option is given when decoding, any base64 parts will be decoded as\nwell.

\n
\n

See also

\n
\n
Module quopri
\n
Encode and decode MIME quoted-printable files.
\n
\n
\n
", "searchableItems": [ { "name": "mimify.mime_decode_header", "domId": "mimify_mimify.mime_decode_header" }, { "name": "mimify.mime_encode_header", "domId": "mimify_mimify.mime_encode_header" }, { "name": "mimify.mimify", "domId": "mimify_mimify.mimify" }, { "name": "mimify.unmimify", "domId": "mimify_mimify.unmimify" } ] }, { "url": "http://docs.python.org/library/mailbox.html", "title": "mailbox", "html": "
\n

18.4. mailbox — Manipulate mailboxes in various formats

\n

This module defines two classes, Mailbox and Message, for\naccessing and manipulating on-disk mailboxes and the messages they contain.\nMailbox offers a dictionary-like mapping from keys to messages.\nMessage extends the email.Message module’s Message\nclass with format-specific state and behavior. Supported mailbox formats are\nMaildir, mbox, MH, Babyl, and MMDF.

\n
\n

See also

\n
\n
Module email
\n
Represent and manipulate messages.
\n
\n
\n
\n

18.4.1. Mailbox objects

\n
\n
\nclass mailbox.Mailbox
\n

A mailbox, which may be inspected and modified.

\n

The Mailbox class defines an interface and is not intended to be\ninstantiated. Instead, format-specific subclasses should inherit from\nMailbox and your code should instantiate a particular subclass.

\n

The Mailbox interface is dictionary-like, with small keys\ncorresponding to messages. Keys are issued by the Mailbox instance\nwith which they will be used and are only meaningful to that Mailbox\ninstance. A key continues to identify a message even if the corresponding\nmessage is modified, such as by replacing it with another message.

\n

Messages may be added to a Mailbox instance using the set-like\nmethod add() and removed using a del statement or the set-like\nmethods remove() and discard().

\n

Mailbox interface semantics differ from dictionary semantics in some\nnoteworthy ways. Each time a message is requested, a new representation\n(typically a Message instance) is generated based upon the current\nstate of the mailbox. Similarly, when a message is added to a\nMailbox instance, the provided message representation’s contents are\ncopied. In neither case is a reference to the message representation kept by\nthe Mailbox instance.

\n

The default Mailbox iterator iterates over message representations,\nnot keys as the default dictionary iterator does. Moreover, modification of a\nmailbox during iteration is safe and well-defined. Messages added to the\nmailbox after an iterator is created will not be seen by the\niterator. Messages removed from the mailbox before the iterator yields them\nwill be silently skipped, though using a key from an iterator may result in a\nKeyError exception if the corresponding message is subsequently\nremoved.

\n
\n

Warning

\n

Be very cautious when modifying mailboxes that might be simultaneously\nchanged by some other process. The safest mailbox format to use for such\ntasks is Maildir; try to avoid using single-file formats such as mbox for\nconcurrent writing. If you’re modifying a mailbox, you must lock it by\ncalling the lock() and unlock() methods before reading any\nmessages in the file or making any changes by adding or deleting a\nmessage. Failing to lock the mailbox runs the risk of losing messages or\ncorrupting the entire mailbox.

\n
\n

Mailbox instances have the following methods:

\n
\n
\nadd(message)
\n

Add message to the mailbox and return the key that has been assigned to\nit.

\n

Parameter message may be a Message instance, an\nemail.Message.Message instance, a string, or a file-like object\n(which should be open in text mode). If message is an instance of the\nappropriate format-specific Message subclass (e.g., if it’s an\nmboxMessage instance and this is an mbox instance), its\nformat-specific information is used. Otherwise, reasonable defaults for\nformat-specific information are used.

\n
\n\n
\n
\nremove(key)
\n
\n__delitem__(key)
\n
\ndiscard(key)
\n

Delete the message corresponding to key from the mailbox.

\n

If no such message exists, a KeyError exception is raised if the\nmethod was called as remove() or __delitem__() but no\nexception is raised if the method was called as discard(). The\nbehavior of discard() may be preferred if the underlying mailbox\nformat supports concurrent modification by other processes.

\n
\n\n
\n
\n__setitem__(key, message)
\n

Replace the message corresponding to key with message. Raise a\nKeyError exception if no message already corresponds to key.

\n

As with add(), parameter message may be a Message\ninstance, an email.Message.Message instance, a string, or a\nfile-like object (which should be open in text mode). If message is an\ninstance of the appropriate format-specific Message subclass\n(e.g., if it’s an mboxMessage instance and this is an\nmbox instance), its format-specific information is\nused. Otherwise, the format-specific information of the message that\ncurrently corresponds to key is left unchanged.

\n
\n\n
\n
\niterkeys()
\n
\nkeys()
\n
Return an iterator over all keys if called as iterkeys() or return a\nlist of keys if called as keys().
\n\n
\n
\nitervalues()
\n
\n__iter__()
\n
\nvalues()
\n

Return an iterator over representations of all messages if called as\nitervalues() or __iter__() or return a list of such\nrepresentations if called as values(). The messages are represented\nas instances of the appropriate format-specific Message subclass\nunless a custom message factory was specified when the Mailbox\ninstance was initialized.

\n
\n

Note

\n

The behavior of __iter__() is unlike that of dictionaries, which\niterate over keys.

\n
\n
\n\n
\n
\niteritems()
\n
\nitems()
\n
Return an iterator over (key, message) pairs, where key is a key and\nmessage is a message representation, if called as iteritems() or\nreturn a list of such pairs if called as items(). The messages are\nrepresented as instances of the appropriate format-specific\nMessage subclass unless a custom message factory was specified\nwhen the Mailbox instance was initialized.
\n\n
\n
\nget(key[, default=None])
\n
\n__getitem__(key)
\n
Return a representation of the message corresponding to key. If no such\nmessage exists, default is returned if the method was called as\nget() and a KeyError exception is raised if the method was\ncalled as __getitem__(). The message is represented as an instance\nof the appropriate format-specific Message subclass unless a\ncustom message factory was specified when the Mailbox instance\nwas initialized.
\n\n
\n
\nget_message(key)
\n
Return a representation of the message corresponding to key as an\ninstance of the appropriate format-specific Message subclass, or\nraise a KeyError exception if no such message exists.
\n\n
\n
\nget_string(key)
\n
Return a string representation of the message corresponding to key, or\nraise a KeyError exception if no such message exists.
\n\n
\n
\nget_file(key)
\n

Return a file-like representation of the message corresponding to key,\nor raise a KeyError exception if no such message exists. The\nfile-like object behaves as if open in binary mode. This file should be\nclosed once it is no longer needed.

\n
\n

Note

\n

Unlike other representations of messages, file-like representations are\nnot necessarily independent of the Mailbox instance that\ncreated them or of the underlying mailbox. More specific documentation\nis provided by each subclass.

\n
\n
\n\n
\n
\nhas_key(key)
\n
\n__contains__(key)
\n
Return True if key corresponds to a message, False otherwise.
\n\n
\n
\n__len__()
\n
Return a count of messages in the mailbox.
\n\n
\n
\nclear()
\n
Delete all messages from the mailbox.
\n\n
\n
\npop(key[, default])
\n
Return a representation of the message corresponding to key and delete\nthe message. If no such message exists, return default if it was\nsupplied or else raise a KeyError exception. The message is\nrepresented as an instance of the appropriate format-specific\nMessage subclass unless a custom message factory was specified\nwhen the Mailbox instance was initialized.
\n\n
\n
\npopitem()
\n
Return an arbitrary (key, message) pair, where key is a key and\nmessage is a message representation, and delete the corresponding\nmessage. If the mailbox is empty, raise a KeyError exception. The\nmessage is represented as an instance of the appropriate format-specific\nMessage subclass unless a custom message factory was specified\nwhen the Mailbox instance was initialized.
\n\n
\n
\nupdate(arg)
\n

Parameter arg should be a key-to-message mapping or an iterable of\n(key, message) pairs. Updates the mailbox so that, for each given\nkey and message, the message corresponding to key is set to\nmessage as if by using __setitem__(). As with __setitem__(),\neach key must already correspond to a message in the mailbox or else a\nKeyError exception will be raised, so in general it is incorrect\nfor arg to be a Mailbox instance.

\n
\n

Note

\n

Unlike with dictionaries, keyword arguments are not supported.

\n
\n
\n\n
\n
\nflush()
\n
Write any pending changes to the filesystem. For some Mailbox\nsubclasses, changes are always written immediately and flush() does\nnothing, but you should still make a habit of calling this method.
\n\n
\n
\nlock()
\n
Acquire an exclusive advisory lock on the mailbox so that other processes\nknow not to modify it. An ExternalClashError is raised if the lock\nis not available. The particular locking mechanisms used depend upon the\nmailbox format. You should always lock the mailbox before making any\nmodifications to its contents.
\n\n
\n
\nunlock()
\n
Release the lock on the mailbox, if any.
\n\n
\n
\nclose()
\n
Flush the mailbox, unlock it if necessary, and close any open files. For\nsome Mailbox subclasses, this method does nothing.
\n\n
\n\n
\n

18.4.1.1. Maildir

\n
\n
\nclass mailbox.Maildir(dirname[, factory=rfc822.Message[, create=True]])
\n

A subclass of Mailbox for mailboxes in Maildir format. Parameter\nfactory is a callable object that accepts a file-like message representation\n(which behaves as if opened in binary mode) and returns a custom representation.\nIf factory is None, MaildirMessage is used as the default message\nrepresentation. If create is True, the mailbox is created if it does not\nexist.

\n

It is for historical reasons that factory defaults to rfc822.Message\nand that dirname is named as such rather than path. For a Maildir\ninstance that behaves like instances of other Mailbox subclasses, set\nfactory to None.

\n

Maildir is a directory-based mailbox format invented for the qmail mail\ntransfer agent and now widely supported by other programs. Messages in a\nMaildir mailbox are stored in separate files within a common directory\nstructure. This design allows Maildir mailboxes to be accessed and modified\nby multiple unrelated programs without data corruption, so file locking is\nunnecessary.

\n

Maildir mailboxes contain three subdirectories, namely: tmp,\nnew, and cur. Messages are created momentarily in the\ntmp subdirectory and then moved to the new subdirectory to\nfinalize delivery. A mail user agent may subsequently move the message to the\ncur subdirectory and store information about the state of the message\nin a special “info” section appended to its file name.

\n

Folders of the style introduced by the Courier mail transfer agent are also\nsupported. Any subdirectory of the main mailbox is considered a folder if\n'.' is the first character in its name. Folder names are represented by\nMaildir without the leading '.'. Each folder is itself a Maildir\nmailbox but should not contain other folders. Instead, a logical nesting is\nindicated using '.' to delimit levels, e.g., “Archived.2005.07”.

\n
\n

Note

\n

The Maildir specification requires the use of a colon (':') in certain\nmessage file names. However, some operating systems do not permit this\ncharacter in file names, If you wish to use a Maildir-like format on such\nan operating system, you should specify another character to use\ninstead. The exclamation point ('!') is a popular choice. For\nexample:

\n
import mailbox\nmailbox.Maildir.colon = '!'\n
\n
\n

The colon attribute may also be set on a per-instance basis.

\n
\n

Maildir instances have all of the methods of Mailbox in\naddition to the following:

\n
\n
\nlist_folders()
\n
Return a list of the names of all folders.
\n\n
\n
\nget_folder(folder)
\n
Return a Maildir instance representing the folder whose name is\nfolder. A NoSuchMailboxError exception is raised if the folder\ndoes not exist.
\n\n
\n
\nadd_folder(folder)
\n
Create a folder whose name is folder and return a Maildir\ninstance representing it.
\n\n
\n
\nremove_folder(folder)
\n
Delete the folder whose name is folder. If the folder contains any\nmessages, a NotEmptyError exception will be raised and the folder\nwill not be deleted.
\n\n
\n
\nclean()
\n
Delete temporary files from the mailbox that have not been accessed in the\nlast 36 hours. The Maildir specification says that mail-reading programs\nshould do this occasionally.
\n\n

Some Mailbox methods implemented by Maildir deserve special\nremarks:

\n
\n
\nadd(message)
\n
\n__setitem__(key, message)
\n
\nupdate(arg)
\n
\n

Warning

\n

These methods generate unique file names based upon the current process\nID. When using multiple threads, undetected name clashes may occur and\ncause corruption of the mailbox unless threads are coordinated to avoid\nusing these methods to manipulate the same mailbox simultaneously.

\n
\n
\n\n
\n
\nflush()
\n
All changes to Maildir mailboxes are immediately applied, so this method\ndoes nothing.
\n\n
\n
\nlock()
\n
\nunlock()
\n
Maildir mailboxes do not support (or require) locking, so these methods do\nnothing.
\n\n
\n
\nclose()
\n
Maildir instances do not keep any open files and the underlying\nmailboxes do not support locking, so this method does nothing.
\n\n
\n
\nget_file(key)
\n
Depending upon the host platform, it may not be possible to modify or\nremove the underlying message while the returned file remains open.
\n\n
\n\n
\n

See also

\n
\n
maildir man page from qmail
\n
The original specification of the format.
\n
Using maildir format
\n
Notes on Maildir by its inventor. Includes an updated name-creation scheme and\ndetails on “info” semantics.
\n
maildir man page from Courier
\n
Another specification of the format. Describes a common extension for supporting\nfolders.
\n
\n
\n
\n
\n

18.4.1.2. mbox

\n
\n
\nclass mailbox.mbox(path[, factory=None[, create=True]])
\n

A subclass of Mailbox for mailboxes in mbox format. Parameter factory\nis a callable object that accepts a file-like message representation (which\nbehaves as if opened in binary mode) and returns a custom representation. If\nfactory is None, mboxMessage is used as the default message\nrepresentation. If create is True, the mailbox is created if it does not\nexist.

\n

The mbox format is the classic format for storing mail on Unix systems. All\nmessages in an mbox mailbox are stored in a single file with the beginning of\neach message indicated by a line whose first five characters are “From “.

\n

Several variations of the mbox format exist to address perceived shortcomings in\nthe original. In the interest of compatibility, mbox implements the\noriginal format, which is sometimes referred to as mboxo. This means that\nthe Content-Length header, if present, is ignored and that any\noccurrences of “From ” at the beginning of a line in a message body are\ntransformed to “>From ” when storing the message, although occurrences of “>From\n” are not transformed to “From ” when reading the message.

\n

Some Mailbox methods implemented by mbox deserve special\nremarks:

\n
\n
\nget_file(key)
\n
Using the file after calling flush() or close() on the\nmbox instance may yield unpredictable results or raise an\nexception.
\n\n
\n
\nlock()
\n
\nunlock()
\n
Three locking mechanisms are used—dot locking and, if available, the\nflock() and lockf() system calls.
\n\n
\n\n
\n

See also

\n
\n
mbox man page from qmail
\n
A specification of the format and its variations.
\n
mbox man page from tin
\n
Another specification of the format, with details on locking.
\n
Configuring Netscape Mail on Unix: Why The Content-Length Format is Bad
\n
An argument for using the original mbox format rather than a variation.
\n
“mbox” is a family of several mutually incompatible mailbox formats
\n
A history of mbox variations.
\n
\n
\n
\n
\n

18.4.1.3. MH

\n
\n
\nclass mailbox.MH(path[, factory=None[, create=True]])
\n

A subclass of Mailbox for mailboxes in MH format. Parameter factory\nis a callable object that accepts a file-like message representation (which\nbehaves as if opened in binary mode) and returns a custom representation. If\nfactory is None, MHMessage is used as the default message\nrepresentation. If create is True, the mailbox is created if it does not\nexist.

\n

MH is a directory-based mailbox format invented for the MH Message Handling\nSystem, a mail user agent. Each message in an MH mailbox resides in its own\nfile. An MH mailbox may contain other MH mailboxes (called folders) in\naddition to messages. Folders may be nested indefinitely. MH mailboxes also\nsupport sequences, which are named lists used to logically group\nmessages without moving them to sub-folders. Sequences are defined in a file\ncalled .mh_sequences in each folder.

\n

The MH class manipulates MH mailboxes, but it does not attempt to\nemulate all of mh‘s behaviors. In particular, it does not modify\nand is not affected by the context or .mh_profile files that\nare used by mh to store its state and configuration.

\n

MH instances have all of the methods of Mailbox in addition\nto the following:

\n
\n
\nlist_folders()
\n
Return a list of the names of all folders.
\n\n
\n
\nget_folder(folder)
\n
Return an MH instance representing the folder whose name is\nfolder. A NoSuchMailboxError exception is raised if the folder\ndoes not exist.
\n\n
\n
\nadd_folder(folder)
\n
Create a folder whose name is folder and return an MH instance\nrepresenting it.
\n\n
\n
\nremove_folder(folder)
\n
Delete the folder whose name is folder. If the folder contains any\nmessages, a NotEmptyError exception will be raised and the folder\nwill not be deleted.
\n\n
\n
\nget_sequences()
\n
Return a dictionary of sequence names mapped to key lists. If there are no\nsequences, the empty dictionary is returned.
\n\n
\n
\nset_sequences(sequences)
\n
Re-define the sequences that exist in the mailbox based upon sequences,\na dictionary of names mapped to key lists, like returned by\nget_sequences().
\n\n
\n
\npack()
\n

Rename messages in the mailbox as necessary to eliminate gaps in\nnumbering. Entries in the sequences list are updated correspondingly.

\n
\n

Note

\n

Already-issued keys are invalidated by this operation and should not be\nsubsequently used.

\n
\n
\n\n

Some Mailbox methods implemented by MH deserve special\nremarks:

\n
\n
\nremove(key)
\n
\n__delitem__(key)
\n
\ndiscard(key)
\n
These methods immediately delete the message. The MH convention of marking\na message for deletion by prepending a comma to its name is not used.
\n\n
\n
\nlock()
\n
\nunlock()
\n
Three locking mechanisms are used—dot locking and, if available, the\nflock() and lockf() system calls. For MH mailboxes, locking\nthe mailbox means locking the .mh_sequences file and, only for the\nduration of any operations that affect them, locking individual message\nfiles.
\n\n
\n
\nget_file(key)
\n
Depending upon the host platform, it may not be possible to remove the\nunderlying message while the returned file remains open.
\n\n
\n
\nflush()
\n
All changes to MH mailboxes are immediately applied, so this method does\nnothing.
\n\n
\n
\nclose()
\n
MH instances do not keep any open files, so this method is\nequivalent to unlock().
\n\n
\n\n
\n

See also

\n
\n
nmh - Message Handling System
\n
Home page of nmh, an updated version of the original mh.
\n
MH & nmh: Email for Users & Programmers
\n
A GPL-licensed book on mh and nmh, with some information\non the mailbox format.
\n
\n
\n
\n
\n

18.4.1.4. Babyl

\n
\n
\nclass mailbox.Babyl(path[, factory=None[, create=True]])
\n

A subclass of Mailbox for mailboxes in Babyl format. Parameter\nfactory is a callable object that accepts a file-like message representation\n(which behaves as if opened in binary mode) and returns a custom representation.\nIf factory is None, BabylMessage is used as the default message\nrepresentation. If create is True, the mailbox is created if it does not\nexist.

\n

Babyl is a single-file mailbox format used by the Rmail mail user agent\nincluded with Emacs. The beginning of a message is indicated by a line\ncontaining the two characters Control-Underscore ('\\037') and Control-L\n('\\014'). The end of a message is indicated by the start of the next\nmessage or, in the case of the last message, a line containing a\nControl-Underscore ('\\037') character.

\n

Messages in a Babyl mailbox have two sets of headers, original headers and\nso-called visible headers. Visible headers are typically a subset of the\noriginal headers that have been reformatted or abridged to be more\nattractive. Each message in a Babyl mailbox also has an accompanying list of\nlabels, or short strings that record extra information about the\nmessage, and a list of all user-defined labels found in the mailbox is kept\nin the Babyl options section.

\n

Babyl instances have all of the methods of Mailbox in\naddition to the following:

\n
\n
\nget_labels()
\n

Return a list of the names of all user-defined labels used in the mailbox.

\n
\n

Note

\n

The actual messages are inspected to determine which labels exist in\nthe mailbox rather than consulting the list of labels in the Babyl\noptions section, but the Babyl section is updated whenever the mailbox\nis modified.

\n
\n
\n\n

Some Mailbox methods implemented by Babyl deserve special\nremarks:

\n
\n
\nget_file(key)
\n
In Babyl mailboxes, the headers of a message are not stored contiguously\nwith the body of the message. To generate a file-like representation, the\nheaders and body are copied together into a StringIO instance\n(from the StringIO module), which has an API identical to that of a\nfile. As a result, the file-like object is truly independent of the\nunderlying mailbox but does not save memory compared to a string\nrepresentation.
\n\n
\n
\nlock()
\n
\nunlock()
\n
Three locking mechanisms are used—dot locking and, if available, the\nflock() and lockf() system calls.
\n\n
\n\n
\n

See also

\n
\n
Format of Version 5 Babyl Files
\n
A specification of the Babyl format.
\n
Reading Mail with Rmail
\n
The Rmail manual, with some information on Babyl semantics.
\n
\n
\n
\n
\n

18.4.1.5. MMDF

\n
\n
\nclass mailbox.MMDF(path[, factory=None[, create=True]])
\n

A subclass of Mailbox for mailboxes in MMDF format. Parameter factory\nis a callable object that accepts a file-like message representation (which\nbehaves as if opened in binary mode) and returns a custom representation. If\nfactory is None, MMDFMessage is used as the default message\nrepresentation. If create is True, the mailbox is created if it does not\nexist.

\n

MMDF is a single-file mailbox format invented for the Multichannel Memorandum\nDistribution Facility, a mail transfer agent. Each message is in the same\nform as an mbox message but is bracketed before and after by lines containing\nfour Control-A ('\\001') characters. As with the mbox format, the\nbeginning of each message is indicated by a line whose first five characters\nare “From “, but additional occurrences of “From ” are not transformed to\n“>From ” when storing messages because the extra message separator lines\nprevent mistaking such occurrences for the starts of subsequent messages.

\n

Some Mailbox methods implemented by MMDF deserve special\nremarks:

\n
\n
\nget_file(key)
\n
Using the file after calling flush() or close() on the\nMMDF instance may yield unpredictable results or raise an\nexception.
\n\n
\n
\nlock()
\n
\nunlock()
\n
Three locking mechanisms are used—dot locking and, if available, the\nflock() and lockf() system calls.
\n\n
\n\n
\n

See also

\n
\n
mmdf man page from tin
\n
A specification of MMDF format from the documentation of tin, a newsreader.
\n
MMDF
\n
A Wikipedia article describing the Multichannel Memorandum Distribution\nFacility.
\n
\n
\n
\n
\n
\n

18.4.2. Message objects

\n
\n
\nclass mailbox.Message([message])
\n

A subclass of the email.Message module’s Message. Subclasses of\nmailbox.Message add mailbox-format-specific state and behavior.

\n

If message is omitted, the new instance is created in a default, empty state.\nIf message is an email.Message.Message instance, its contents are\ncopied; furthermore, any format-specific information is converted insofar as\npossible if message is a Message instance. If message is a string\nor a file, it should contain an RFC 2822-compliant message, which is read\nand parsed.

\n

The format-specific state and behaviors offered by subclasses vary, but in\ngeneral it is only the properties that are not specific to a particular\nmailbox that are supported (although presumably the properties are specific\nto a particular mailbox format). For example, file offsets for single-file\nmailbox formats and file names for directory-based mailbox formats are not\nretained, because they are only applicable to the original mailbox. But state\nsuch as whether a message has been read by the user or marked as important is\nretained, because it applies to the message itself.

\n

There is no requirement that Message instances be used to represent\nmessages retrieved using Mailbox instances. In some situations, the\ntime and memory required to generate Message representations might\nnot be acceptable. For such situations, Mailbox instances also\noffer string and file-like representations, and a custom message factory may\nbe specified when a Mailbox instance is initialized.

\n
\n\n
\n

18.4.2.1. MaildirMessage

\n
\n
\nclass mailbox.MaildirMessage([message])
\n

A message with Maildir-specific behaviors. Parameter message has the same\nmeaning as with the Message constructor.

\n

Typically, a mail user agent application moves all of the messages in the\nnew subdirectory to the cur subdirectory after the first time\nthe user opens and closes the mailbox, recording that the messages are old\nwhether or not they’ve actually been read. Each message in cur has an\n“info” section added to its file name to store information about its state.\n(Some mail readers may also add an “info” section to messages in\nnew.) The “info” section may take one of two forms: it may contain\n“2,” followed by a list of standardized flags (e.g., “2,FR”) or it may\ncontain “1,” followed by so-called experimental information. Standard flags\nfor Maildir messages are as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FlagMeaningExplanation
DDraftUnder composition
FFlaggedMarked as important
PPassedForwarded, resent, or bounced
RRepliedReplied to
SSeenRead
TTrashedMarked for subsequent deletion
\n

MaildirMessage instances offer the following methods:

\n
\n
\nget_subdir()
\n

Return either “new” (if the message should be stored in the new\nsubdirectory) or “cur” (if the message should be stored in the cur\nsubdirectory).

\n
\n

Note

\n

A message is typically moved from new to cur after its\nmailbox has been accessed, whether or not the message is has been\nread. A message msg has been read if "S" in msg.get_flags() is\nTrue.

\n
\n
\n\n
\n
\nset_subdir(subdir)
\n
Set the subdirectory the message should be stored in. Parameter subdir\nmust be either “new” or “cur”.
\n\n
\n
\nget_flags()
\n
Return a string specifying the flags that are currently set. If the\nmessage complies with the standard Maildir format, the result is the\nconcatenation in alphabetical order of zero or one occurrence of each of\n'D', 'F', 'P', 'R', 'S', and 'T'. The empty string\nis returned if no flags are set or if “info” contains experimental\nsemantics.
\n\n
\n
\nset_flags(flags)
\n
Set the flags specified by flags and unset all others.
\n\n
\n
\nadd_flag(flag)
\n
Set the flag(s) specified by flag without changing other flags. To add\nmore than one flag at a time, flag may be a string of more than one\ncharacter. The current “info” is overwritten whether or not it contains\nexperimental information rather than flags.
\n\n
\n
\nremove_flag(flag)
\n
Unset the flag(s) specified by flag without changing other flags. To\nremove more than one flag at a time, flag maybe a string of more than\none character. If “info” contains experimental information rather than\nflags, the current “info” is not modified.
\n\n
\n
\nget_date()
\n
Return the delivery date of the message as a floating-point number\nrepresenting seconds since the epoch.
\n\n
\n
\nset_date(date)
\n
Set the delivery date of the message to date, a floating-point number\nrepresenting seconds since the epoch.
\n\n
\n
\nget_info()
\n
Return a string containing the “info” for a message. This is useful for\naccessing and modifying “info” that is experimental (i.e., not a list of\nflags).
\n\n
\n
\nset_info(info)
\n
Set “info” to info, which should be a string.
\n\n
\n\n

When a MaildirMessage instance is created based upon an\nmboxMessage or MMDFMessage instance, the Status\nand X-Status headers are omitted and the following conversions\ntake place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting statemboxMessage or MMDFMessage\nstate
“cur” subdirectoryO flag
F flagF flag
R flagA flag
S flagR flag
T flagD flag
\n

When a MaildirMessage instance is created based upon an\nMHMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMHMessage state
“cur” subdirectory“unseen” sequence
“cur” subdirectory and S flagno “unseen” sequence
F flag“flagged” sequence
R flag“replied” sequence
\n

When a MaildirMessage instance is created based upon a\nBabylMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateBabylMessage state
“cur” subdirectory“unseen” label
“cur” subdirectory and S flagno “unseen” label
P flag“forwarded” or “resent” label
R flag“answered” label
T flag“deleted” label
\n
\n
\n

18.4.2.2. mboxMessage

\n
\n
\nclass mailbox.mboxMessage([message])
\n

A message with mbox-specific behaviors. Parameter message has the same meaning\nas with the Message constructor.

\n

Messages in an mbox mailbox are stored together in a single file. The\nsender’s envelope address and the time of delivery are typically stored in a\nline beginning with “From ” that is used to indicate the start of a message,\nthough there is considerable variation in the exact format of this data among\nmbox implementations. Flags that indicate the state of the message, such as\nwhether it has been read or marked as important, are typically stored in\nStatus and X-Status headers.

\n

Conventional flags for mbox messages are as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FlagMeaningExplanation
RReadRead
OOldPreviously detected by MUA
DDeletedMarked for subsequent deletion
FFlaggedMarked as important
AAnsweredReplied to
\n

The “R” and “O” flags are stored in the Status header, and the\n“D”, “F”, and “A” flags are stored in the X-Status header. The\nflags and headers typically appear in the order mentioned.

\n

mboxMessage instances offer the following methods:

\n
\n
\nget_from()
\n
Return a string representing the “From ” line that marks the start of the\nmessage in an mbox mailbox. The leading “From ” and the trailing newline\nare excluded.
\n\n
\n
\nset_from(from_[, time_=None])
\n
Set the “From ” line to from_, which should be specified without a\nleading “From ” or trailing newline. For convenience, time_ may be\nspecified and will be formatted appropriately and appended to from_. If\ntime_ is specified, it should be a struct_time instance, a\ntuple suitable for passing to time.strftime(), or True (to use\ntime.gmtime()).
\n\n
\n
\nget_flags()
\n
Return a string specifying the flags that are currently set. If the\nmessage complies with the conventional format, the result is the\nconcatenation in the following order of zero or one occurrence of each of\n'R', 'O', 'D', 'F', and 'A'.
\n\n
\n
\nset_flags(flags)
\n
Set the flags specified by flags and unset all others. Parameter flags\nshould be the concatenation in any order of zero or more occurrences of\neach of 'R', 'O', 'D', 'F', and 'A'.
\n\n
\n
\nadd_flag(flag)
\n
Set the flag(s) specified by flag without changing other flags. To add\nmore than one flag at a time, flag may be a string of more than one\ncharacter.
\n\n
\n
\nremove_flag(flag)
\n
Unset the flag(s) specified by flag without changing other flags. To\nremove more than one flag at a time, flag maybe a string of more than\none character.
\n\n
\n\n

When an mboxMessage instance is created based upon a\nMaildirMessage instance, a “From ” line is generated based upon the\nMaildirMessage instance’s delivery date, and the following conversions\ntake place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMaildirMessage state
R flagS flag
O flag“cur” subdirectory
D flagT flag
F flagF flag
A flagR flag
\n

When an mboxMessage instance is created based upon an\nMHMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMHMessage state
R flag and O flagno “unseen” sequence
O flag“unseen” sequence
F flag“flagged” sequence
A flag“replied” sequence
\n

When an mboxMessage instance is created based upon a\nBabylMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateBabylMessage state
R flag and O flagno “unseen” label
O flag“unseen” label
D flag“deleted” label
A flag“answered” label
\n

When a Message instance is created based upon an MMDFMessage\ninstance, the “From ” line is copied and all flags directly correspond:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMMDFMessage state
R flagR flag
O flagO flag
D flagD flag
F flagF flag
A flagA flag
\n
\n
\n

18.4.2.3. MHMessage

\n
\n
\nclass mailbox.MHMessage([message])
\n

A message with MH-specific behaviors. Parameter message has the same meaning\nas with the Message constructor.

\n

MH messages do not support marks or flags in the traditional sense, but they\ndo support sequences, which are logical groupings of arbitrary messages. Some\nmail reading programs (although not the standard mh and\nnmh) use sequences in much the same way flags are used with other\nformats, as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
SequenceExplanation
unseenNot read, but previously detected by MUA
repliedReplied to
flaggedMarked as important
\n

MHMessage instances offer the following methods:

\n
\n
\nget_sequences()
\n
Return a list of the names of sequences that include this message.
\n\n
\n
\nset_sequences(sequences)
\n
Set the list of sequences that include this message.
\n\n
\n
\nadd_sequence(sequence)
\n
Add sequence to the list of sequences that include this message.
\n\n
\n
\nremove_sequence(sequence)
\n
Remove sequence from the list of sequences that include this message.
\n\n
\n\n

When an MHMessage instance is created based upon a\nMaildirMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMaildirMessage state
“unseen” sequenceno S flag
“replied” sequenceR flag
“flagged” sequenceF flag
\n

When an MHMessage instance is created based upon an\nmboxMessage or MMDFMessage instance, the Status\nand X-Status headers are omitted and the following conversions\ntake place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting statemboxMessage or MMDFMessage\nstate
“unseen” sequenceno R flag
“replied” sequenceA flag
“flagged” sequenceF flag
\n

When an MHMessage instance is created based upon a\nBabylMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateBabylMessage state
“unseen” sequence“unseen” label
“replied” sequence“answered” label
\n
\n
\n

18.4.2.4. BabylMessage

\n
\n
\nclass mailbox.BabylMessage([message])
\n

A message with Babyl-specific behaviors. Parameter message has the same\nmeaning as with the Message constructor.

\n

Certain message labels, called attributes, are defined by convention\nto have special meanings. The attributes are as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
LabelExplanation
unseenNot read, but previously detected by MUA
deletedMarked for subsequent deletion
filedCopied to another file or mailbox
answeredReplied to
forwardedForwarded
editedModified by the user
resentResent
\n

By default, Rmail displays only visible headers. The BabylMessage\nclass, though, uses the original headers because they are more\ncomplete. Visible headers may be accessed explicitly if desired.

\n

BabylMessage instances offer the following methods:

\n
\n
\nget_labels()
\n
Return a list of labels on the message.
\n\n
\n
\nset_labels(labels)
\n
Set the list of labels on the message to labels.
\n\n
\n
\nadd_label(label)
\n
Add label to the list of labels on the message.
\n\n
\n
\nremove_label(label)
\n
Remove label from the list of labels on the message.
\n\n
\n
\nget_visible()
\n
Return an Message instance whose headers are the message’s\nvisible headers and whose body is empty.
\n\n
\n
\nset_visible(visible)
\n
Set the message’s visible headers to be the same as the headers in\nmessage. Parameter visible should be a Message instance, an\nemail.Message.Message instance, a string, or a file-like object\n(which should be open in text mode).
\n\n
\n
\nupdate_visible()
\n
When a BabylMessage instance’s original headers are modified, the\nvisible headers are not automatically modified to correspond. This method\nupdates the visible headers as follows: each visible header with a\ncorresponding original header is set to the value of the original header,\neach visible header without a corresponding original header is removed,\nand any of Date, From, Reply-To,\nTo, CC, and Subject that are\npresent in the original headers but not the visible headers are added to\nthe visible headers.
\n\n
\n\n

When a BabylMessage instance is created based upon a\nMaildirMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMaildirMessage state
“unseen” labelno S flag
“deleted” labelT flag
“answered” labelR flag
“forwarded” labelP flag
\n

When a BabylMessage instance is created based upon an\nmboxMessage or MMDFMessage instance, the Status\nand X-Status headers are omitted and the following conversions\ntake place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting statemboxMessage or MMDFMessage\nstate
“unseen” labelno R flag
“deleted” labelD flag
“answered” labelA flag
\n

When a BabylMessage instance is created based upon an\nMHMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMHMessage state
“unseen” label“unseen” sequence
“answered” label“replied” sequence
\n
\n
\n

18.4.2.5. MMDFMessage

\n
\n
\nclass mailbox.MMDFMessage([message])
\n

A message with MMDF-specific behaviors. Parameter message has the same meaning\nas with the Message constructor.

\n

As with message in an mbox mailbox, MMDF messages are stored with the\nsender’s address and the delivery date in an initial line beginning with\n“From “. Likewise, flags that indicate the state of the message are\ntypically stored in Status and X-Status headers.

\n

Conventional flags for MMDF messages are identical to those of mbox message\nand are as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FlagMeaningExplanation
RReadRead
OOldPreviously detected by MUA
DDeletedMarked for subsequent deletion
FFlaggedMarked as important
AAnsweredReplied to
\n

The “R” and “O” flags are stored in the Status header, and the\n“D”, “F”, and “A” flags are stored in the X-Status header. The\nflags and headers typically appear in the order mentioned.

\n

MMDFMessage instances offer the following methods, which are\nidentical to those offered by mboxMessage:

\n
\n
\nget_from()
\n
Return a string representing the “From ” line that marks the start of the\nmessage in an mbox mailbox. The leading “From ” and the trailing newline\nare excluded.
\n\n
\n
\nset_from(from_[, time_=None])
\n
Set the “From ” line to from_, which should be specified without a\nleading “From ” or trailing newline. For convenience, time_ may be\nspecified and will be formatted appropriately and appended to from_. If\ntime_ is specified, it should be a struct_time instance, a\ntuple suitable for passing to time.strftime(), or True (to use\ntime.gmtime()).
\n\n
\n
\nget_flags()
\n
Return a string specifying the flags that are currently set. If the\nmessage complies with the conventional format, the result is the\nconcatenation in the following order of zero or one occurrence of each of\n'R', 'O', 'D', 'F', and 'A'.
\n\n
\n
\nset_flags(flags)
\n
Set the flags specified by flags and unset all others. Parameter flags\nshould be the concatenation in any order of zero or more occurrences of\neach of 'R', 'O', 'D', 'F', and 'A'.
\n\n
\n
\nadd_flag(flag)
\n
Set the flag(s) specified by flag without changing other flags. To add\nmore than one flag at a time, flag may be a string of more than one\ncharacter.
\n\n
\n
\nremove_flag(flag)
\n
Unset the flag(s) specified by flag without changing other flags. To\nremove more than one flag at a time, flag maybe a string of more than\none character.
\n\n
\n\n

When an MMDFMessage instance is created based upon a\nMaildirMessage instance, a “From ” line is generated based upon the\nMaildirMessage instance’s delivery date, and the following conversions\ntake place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMaildirMessage state
R flagS flag
O flag“cur” subdirectory
D flagT flag
F flagF flag
A flagR flag
\n

When an MMDFMessage instance is created based upon an\nMHMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateMHMessage state
R flag and O flagno “unseen” sequence
O flag“unseen” sequence
F flag“flagged” sequence
A flag“replied” sequence
\n

When an MMDFMessage instance is created based upon a\nBabylMessage instance, the following conversions take place:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting stateBabylMessage state
R flag and O flagno “unseen” label
O flag“unseen” label
D flag“deleted” label
A flag“answered” label
\n

When an MMDFMessage instance is created based upon an\nmboxMessage instance, the “From ” line is copied and all flags directly\ncorrespond:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Resulting statemboxMessage state
R flagR flag
O flagO flag
D flagD flag
F flagF flag
A flagA flag
\n
\n
\n
\n

18.4.3. Exceptions

\n

The following exception classes are defined in the mailbox module:

\n
\n
\nexception mailbox.Error
\n
The based class for all other module-specific exceptions.
\n\n
\n
\nexception mailbox.NoSuchMailboxError
\n
Raised when a mailbox is expected but is not found, such as when instantiating a\nMailbox subclass with a path that does not exist (and with the create\nparameter set to False), or when opening a folder that does not exist.
\n\n
\n
\nexception mailbox.NotEmptyError
\n
Raised when a mailbox is not empty but is expected to be, such as when deleting\na folder that contains messages.
\n\n
\n
\nexception mailbox.ExternalClashError
\n
Raised when some mailbox-related condition beyond the control of the program\ncauses it to be unable to proceed, such as when failing to acquire a lock that\nanother program already holds a lock, or when a uniquely-generated file name\nalready exists.
\n\n
\n
\nexception mailbox.FormatError
\n
Raised when the data in a file cannot be parsed, such as when an MH\ninstance attempts to read a corrupted .mh_sequences file.
\n\n
\n
\n

18.4.4. Deprecated classes and methods

\n

\nDeprecated since version 2.6.

\n

Older versions of the mailbox module do not support modification of\nmailboxes, such as adding or removing message, and do not provide classes to\nrepresent format-specific message properties. For backward compatibility, the\nolder mailbox classes are still available, but the newer classes should be used\nin preference to them. The old classes will be removed in Python 3.0.

\n

Older mailbox objects support only iteration and provide a single public method:

\n
\n
\noldmailbox.next()
\n
Return the next message in the mailbox, created with the optional factory\nargument passed into the mailbox object’s constructor. By default this is an\nrfc822.Message object (see the rfc822 module). Depending on the\nmailbox implementation the fp attribute of this object may be a true file\nobject or a class instance simulating a file object, taking care of things like\nmessage boundaries if multiple mail messages are contained in a single file,\netc. If no more messages are available, this method returns None.
\n\n

Most of the older mailbox classes have names that differ from the current\nmailbox class names, except for Maildir. For this reason, the new\nMaildir class defines a next() method and its constructor differs\nslightly from those of the other new mailbox classes.

\n

The older mailbox classes whose names are not the same as their newer\ncounterparts are as follows:

\n
\n
\nclass mailbox.UnixMailbox(fp[, factory])
\n

Access to a classic Unix-style mailbox, where all messages are contained in a\nsingle file and separated by From (a.k.a. From_) lines. The file object\nfp points to the mailbox file. The optional factory parameter is a callable\nthat should create new message objects. factory is called with one argument,\nfp by the next() method of the mailbox object. The default is the\nrfc822.Message class (see the rfc822 module – and the note\nbelow).

\n
\n

Note

\n

For reasons of this module’s internal implementation, you will probably want to\nopen the fp object in binary mode. This is especially important on Windows.

\n
\n

For maximum portability, messages in a Unix-style mailbox are separated by any\nline that begins exactly with the string 'From ' (note the trailing space)\nif preceded by exactly two newlines. Because of the wide-range of variations in\npractice, nothing else on the From_ line should be considered. However, the\ncurrent implementation doesn’t check for the leading two newlines. This is\nusually fine for most applications.

\n

The UnixMailbox class implements a more strict version of From_\nline checking, using a regular expression that usually correctly matched\nFrom_ delimiters. It considers delimiter line to be separated by From\nname time lines. For maximum portability, use the\nPortableUnixMailbox class instead. This class is identical to\nUnixMailbox except that individual messages are separated by only\nFrom lines.

\n
\n\n
\n
\nclass mailbox.PortableUnixMailbox(fp[, factory])
\n
A less-strict version of UnixMailbox, which considers only the From\nat the beginning of the line separating messages. The “name time” portion\nof the From line is ignored, to protect against some variations that are\nobserved in practice. This works since lines in the message which begin with\n'From ' are quoted by mail handling software at delivery-time.
\n\n
\n
\nclass mailbox.MmdfMailbox(fp[, factory])
\n
Access an MMDF-style mailbox, where all messages are contained in a single file\nand separated by lines consisting of 4 control-A characters. The file object\nfp points to the mailbox file. Optional factory is as with the\nUnixMailbox class.
\n\n
\n
\nclass mailbox.MHMailbox(dirname[, factory])
\n
Access an MH mailbox, a directory with each message in a separate file with a\nnumeric name. The name of the mailbox directory is passed in dirname.\nfactory is as with the UnixMailbox class.
\n\n
\n
\nclass mailbox.BabylMailbox(fp[, factory])
\n
Access a Babyl mailbox, which is similar to an MMDF mailbox. In Babyl format,\neach message has two sets of headers, the original headers and the visible\nheaders. The original headers appear before a line containing only '*** EOOH\n***' (End-Of-Original-Headers) and the visible headers appear after the\nEOOH line. Babyl-compliant mail readers will show you only the visible\nheaders, and BabylMailbox objects will return messages containing only\nthe visible headers. You’ll have to do your own parsing of the mailbox file to\nget at the original headers. Mail messages start with the EOOH line and end\nwith a line containing only '\\037\\014'. factory is as with the\nUnixMailbox class.
\n\n

If you wish to use the older mailbox classes with the email module rather\nthan the deprecated rfc822 module, you can do so as follows:

\n
import email\nimport email.Errors\nimport mailbox\n\ndef msgfactory(fp):\n    try:\n        return email.message_from_file(fp)\n    except email.Errors.MessageParseError:\n        # Don't return None since that will\n        # stop the mailbox iterator\n        return ''\n\nmbox = mailbox.UnixMailbox(fp, msgfactory)\n
\n
\n

Alternatively, if you know your mailbox contains only well-formed MIME messages,\nyou can simplify this to:

\n
import email\nimport mailbox\n\nmbox = mailbox.UnixMailbox(fp, email.message_from_file)\n
\n
\n
\n
\n

18.4.5. Examples

\n

A simple example of printing the subjects of all messages in a mailbox that seem\ninteresting:

\n
import mailbox\nfor message in mailbox.mbox('~/mbox'):\n    subject = message['subject']       # Could possibly be None.\n    if subject and 'python' in subject.lower():\n        print subject\n
\n
\n

To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the\nformat-specific information that can be converted:

\n
import mailbox\ndestination = mailbox.MH('~/Mail')\ndestination.lock()\nfor message in mailbox.Babyl('~/RMAIL'):\n    destination.add(mailbox.MHMessage(message))\ndestination.flush()\ndestination.unlock()\n
\n
\n

This example sorts mail from several mailing lists into different mailboxes,\nbeing careful to avoid mail corruption due to concurrent modification by other\nprograms, mail loss due to interruption of the program, or premature termination\ndue to malformed messages in the mailbox:

\n
import mailbox\nimport email.Errors\n\nlist_names = ('python-list', 'python-dev', 'python-bugs')\n\nboxes = dict((name, mailbox.mbox('~/email/%s'  name)) for name in list_names)\ninbox = mailbox.Maildir('~/Maildir', factory=None)\n\nfor key in inbox.iterkeys():\n    try:\n        message = inbox[key]\n    except email.Errors.MessageParseError:\n        continue                # The message is malformed. Just leave it.\n\n    for name in list_names:\n        list_id = message['list-id']\n        if list_id and name in list_id:\n            # Get mailbox to use\n            box = boxes[name]\n\n            # Write copy to disk before removing original.\n            # If there's a crash, you might duplicate a message, but\n            # that's better than losing a message completely.\n            box.lock()\n            box.add(message)\n            box.flush()\n            box.unlock()\n\n            # Remove original message\n            inbox.lock()\n            inbox.discard(key)\n            inbox.flush()\n            inbox.unlock()\n            break               # Found destination, so stop looking.\n\nfor box in boxes.itervalues():\n    box.close()\n
\n
\n
\n
", "searchableItems": [ { "name": "mailbox.Babyl", "domId": "mailbox_mailbox.Babyl" }, { "name": "mailbox.Babyl.get_file", "domId": "mailbox_mailbox.Babyl.get_file" }, { "name": "mailbox.Babyl.get_labels", "domId": "mailbox_mailbox.Babyl.get_labels" }, { "name": "mailbox.Babyl.lock", "domId": "mailbox_mailbox.Babyl.lock" }, { "name": "mailbox.BabylMailbox", "domId": "mailbox_mailbox.BabylMailbox" }, { "name": "mailbox.BabylMessage", "domId": "mailbox_mailbox.BabylMessage" }, { "name": "mailbox.BabylMessage.add_label", "domId": "mailbox_mailbox.BabylMessage.add_label" }, { "name": "mailbox.BabylMessage.get_labels", "domId": "mailbox_mailbox.BabylMessage.get_labels" }, { "name": "mailbox.BabylMessage.get_visible", "domId": "mailbox_mailbox.BabylMessage.get_visible" }, { "name": "mailbox.BabylMessage.remove_label", "domId": "mailbox_mailbox.BabylMessage.remove_label" }, { "name": "mailbox.BabylMessage.set_labels", "domId": "mailbox_mailbox.BabylMessage.set_labels" }, { "name": "mailbox.BabylMessage.set_visible", "domId": "mailbox_mailbox.BabylMessage.set_visible" }, { "name": "mailbox.BabylMessage.update_visible", "domId": "mailbox_mailbox.BabylMessage.update_visible" }, { "name": "mailbox.Mailbox", "domId": "mailbox_mailbox.Mailbox" }, { "name": "mailbox.Mailbox.__len__", "domId": "mailbox_mailbox.Mailbox.__len__" }, { "name": "mailbox.Mailbox.__setitem__", "domId": "mailbox_mailbox.Mailbox.__setitem__" }, { "name": "mailbox.Mailbox.add", "domId": "mailbox_mailbox.Mailbox.add" }, { "name": "mailbox.Mailbox.clear", "domId": "mailbox_mailbox.Mailbox.clear" }, { "name": "mailbox.Mailbox.close", "domId": "mailbox_mailbox.Mailbox.close" }, { "name": "mailbox.Mailbox.flush", "domId": "mailbox_mailbox.Mailbox.flush" }, { "name": "mailbox.Mailbox.get", "domId": "mailbox_mailbox.Mailbox.get" }, { "name": "mailbox.Mailbox.get_file", "domId": "mailbox_mailbox.Mailbox.get_file" }, { "name": "mailbox.Mailbox.get_message", "domId": "mailbox_mailbox.Mailbox.get_message" }, { "name": "mailbox.Mailbox.get_string", "domId": "mailbox_mailbox.Mailbox.get_string" }, { "name": "mailbox.Mailbox.has_key", "domId": "mailbox_mailbox.Mailbox.has_key" }, { "name": "mailbox.Mailbox.iteritems", "domId": "mailbox_mailbox.Mailbox.iteritems" }, { "name": "mailbox.Mailbox.iterkeys", "domId": "mailbox_mailbox.Mailbox.iterkeys" }, { "name": "mailbox.Mailbox.itervalues", "domId": "mailbox_mailbox.Mailbox.itervalues" }, { "name": "mailbox.Mailbox.lock", "domId": "mailbox_mailbox.Mailbox.lock" }, { "name": "mailbox.Mailbox.pop", "domId": "mailbox_mailbox.Mailbox.pop" }, { "name": "mailbox.Mailbox.popitem", "domId": "mailbox_mailbox.Mailbox.popitem" }, { "name": "mailbox.Mailbox.remove", "domId": "mailbox_mailbox.Mailbox.remove" }, { "name": "mailbox.Mailbox.unlock", "domId": "mailbox_mailbox.Mailbox.unlock" }, { "name": "mailbox.Mailbox.update", "domId": "mailbox_mailbox.Mailbox.update" }, { "name": "mailbox.Maildir", "domId": "mailbox_mailbox.Maildir" }, { "name": "mailbox.Maildir.add", "domId": "mailbox_mailbox.Maildir.add" }, { "name": "mailbox.Maildir.add_folder", "domId": "mailbox_mailbox.Maildir.add_folder" }, { "name": "mailbox.Maildir.clean", "domId": "mailbox_mailbox.Maildir.clean" }, { "name": "mailbox.Maildir.close", "domId": "mailbox_mailbox.Maildir.close" }, { "name": "mailbox.Maildir.flush", "domId": "mailbox_mailbox.Maildir.flush" }, { "name": "mailbox.Maildir.get_file", "domId": "mailbox_mailbox.Maildir.get_file" }, { "name": "mailbox.Maildir.get_folder", "domId": "mailbox_mailbox.Maildir.get_folder" }, { "name": "mailbox.Maildir.list_folders", "domId": "mailbox_mailbox.Maildir.list_folders" }, { "name": "mailbox.Maildir.lock", "domId": "mailbox_mailbox.Maildir.lock" }, { "name": "mailbox.Maildir.remove_folder", "domId": "mailbox_mailbox.Maildir.remove_folder" }, { "name": "mailbox.MaildirMessage", "domId": "mailbox_mailbox.MaildirMessage" }, { "name": "mailbox.MaildirMessage.add_flag", "domId": "mailbox_mailbox.MaildirMessage.add_flag" }, { "name": "mailbox.MaildirMessage.get_date", "domId": "mailbox_mailbox.MaildirMessage.get_date" }, { "name": "mailbox.MaildirMessage.get_flags", "domId": "mailbox_mailbox.MaildirMessage.get_flags" }, { "name": "mailbox.MaildirMessage.get_info", "domId": "mailbox_mailbox.MaildirMessage.get_info" }, { "name": "mailbox.MaildirMessage.get_subdir", "domId": "mailbox_mailbox.MaildirMessage.get_subdir" }, { "name": "mailbox.MaildirMessage.remove_flag", "domId": "mailbox_mailbox.MaildirMessage.remove_flag" }, { "name": "mailbox.MaildirMessage.set_date", "domId": "mailbox_mailbox.MaildirMessage.set_date" }, { "name": "mailbox.MaildirMessage.set_flags", "domId": "mailbox_mailbox.MaildirMessage.set_flags" }, { "name": "mailbox.MaildirMessage.set_info", "domId": "mailbox_mailbox.MaildirMessage.set_info" }, { "name": "mailbox.MaildirMessage.set_subdir", "domId": "mailbox_mailbox.MaildirMessage.set_subdir" }, { "name": "mailbox.mbox", "domId": "mailbox_mailbox.mbox" }, { "name": "mailbox.mbox.get_file", "domId": "mailbox_mailbox.mbox.get_file" }, { "name": "mailbox.mbox.lock", "domId": "mailbox_mailbox.mbox.lock" }, { "name": "mailbox.mboxMessage", "domId": "mailbox_mailbox.mboxMessage" }, { "name": "mailbox.mboxMessage.add_flag", "domId": "mailbox_mailbox.mboxMessage.add_flag" }, { "name": "mailbox.mboxMessage.get_flags", "domId": "mailbox_mailbox.mboxMessage.get_flags" }, { "name": "mailbox.mboxMessage.get_from", "domId": "mailbox_mailbox.mboxMessage.get_from" }, { "name": "mailbox.mboxMessage.remove_flag", "domId": "mailbox_mailbox.mboxMessage.remove_flag" }, { "name": "mailbox.mboxMessage.set_flags", "domId": "mailbox_mailbox.mboxMessage.set_flags" }, { "name": "mailbox.mboxMessage.set_from", "domId": "mailbox_mailbox.mboxMessage.set_from" }, { "name": "mailbox.Message", "domId": "mailbox_mailbox.Message" }, { "name": "mailbox.MH", "domId": "mailbox_mailbox.MH" }, { "name": "mailbox.MH.add_folder", "domId": "mailbox_mailbox.MH.add_folder" }, { "name": "mailbox.MH.close", "domId": "mailbox_mailbox.MH.close" }, { "name": "mailbox.MH.flush", "domId": "mailbox_mailbox.MH.flush" }, { "name": "mailbox.MH.get_file", "domId": "mailbox_mailbox.MH.get_file" }, { "name": "mailbox.MH.get_folder", "domId": "mailbox_mailbox.MH.get_folder" }, { "name": "mailbox.MH.get_sequences", "domId": "mailbox_mailbox.MH.get_sequences" }, { "name": "mailbox.MH.list_folders", "domId": "mailbox_mailbox.MH.list_folders" }, { "name": "mailbox.MH.lock", "domId": "mailbox_mailbox.MH.lock" }, { "name": "mailbox.MH.pack", "domId": "mailbox_mailbox.MH.pack" }, { "name": "mailbox.MH.remove", "domId": "mailbox_mailbox.MH.remove" }, { "name": "mailbox.MH.remove_folder", "domId": "mailbox_mailbox.MH.remove_folder" }, { "name": "mailbox.MH.set_sequences", "domId": "mailbox_mailbox.MH.set_sequences" }, { "name": "mailbox.MHMailbox", "domId": "mailbox_mailbox.MHMailbox" }, { "name": "mailbox.MHMessage", "domId": "mailbox_mailbox.MHMessage" }, { "name": "mailbox.MHMessage.add_sequence", "domId": "mailbox_mailbox.MHMessage.add_sequence" }, { "name": "mailbox.MHMessage.get_sequences", "domId": "mailbox_mailbox.MHMessage.get_sequences" }, { "name": "mailbox.MHMessage.remove_sequence", "domId": "mailbox_mailbox.MHMessage.remove_sequence" }, { "name": "mailbox.MHMessage.set_sequences", "domId": "mailbox_mailbox.MHMessage.set_sequences" }, { "name": "mailbox.MMDF", "domId": "mailbox_mailbox.MMDF" }, { "name": "mailbox.MMDF.get_file", "domId": "mailbox_mailbox.MMDF.get_file" }, { "name": "mailbox.MMDF.lock", "domId": "mailbox_mailbox.MMDF.lock" }, { "name": "mailbox.MmdfMailbox", "domId": "mailbox_mailbox.MmdfMailbox" }, { "name": "mailbox.MMDFMessage", "domId": "mailbox_mailbox.MMDFMessage" }, { "name": "mailbox.MMDFMessage.add_flag", "domId": "mailbox_mailbox.MMDFMessage.add_flag" }, { "name": "mailbox.MMDFMessage.get_flags", "domId": "mailbox_mailbox.MMDFMessage.get_flags" }, { "name": "mailbox.MMDFMessage.get_from", "domId": "mailbox_mailbox.MMDFMessage.get_from" }, { "name": "mailbox.MMDFMessage.remove_flag", "domId": "mailbox_mailbox.MMDFMessage.remove_flag" }, { "name": "mailbox.MMDFMessage.set_flags", "domId": "mailbox_mailbox.MMDFMessage.set_flags" }, { "name": "mailbox.MMDFMessage.set_from", "domId": "mailbox_mailbox.MMDFMessage.set_from" }, { "name": "mailbox.oldmailbox.next", "domId": "mailbox_mailbox.oldmailbox.next" }, { "name": "mailbox.PortableUnixMailbox", "domId": "mailbox_mailbox.PortableUnixMailbox" }, { "name": "mailbox.UnixMailbox", "domId": "mailbox_mailbox.UnixMailbox" } ] }, { "url": "http://docs.python.org/library/base64.html", "title": "base64", "html": "
\n

18.12. base64 — RFC 3548: Base16, Base32, Base64 Data Encodings

\n

This module provides data encoding and decoding as specified in RFC 3548.\nThis standard defines the Base16, Base32, and Base64 algorithms for encoding and\ndecoding arbitrary binary strings into text strings that can be safely sent by\nemail, used as parts of URLs, or included as part of an HTTP POST request. The\nencoding algorithm is not the same as the uuencode program.

\n

There are two interfaces provided by this module. The modern interface supports\nencoding and decoding string objects using all three alphabets. The legacy\ninterface provides for encoding and decoding to and from file-like objects as\nwell as strings, but only using the Base64 standard alphabet.

\n

The modern interface, which was introduced in Python 2.4, provides:

\n
\n
\nbase64.b64encode(s[, altchars])
\n

Encode a string use Base64.

\n

s is the string to encode. Optional altchars must be a string of at least\nlength 2 (additional characters are ignored) which specifies an alternative\nalphabet for the + and / characters. This allows an application to e.g.\ngenerate URL or filesystem safe Base64 strings. The default is None, for\nwhich the standard Base64 alphabet is used.

\n

The encoded string is returned.

\n
\n\n
\n
\nbase64.b64decode(s[, altchars])
\n

Decode a Base64 encoded string.

\n

s is the string to decode. Optional altchars must be a string of at least\nlength 2 (additional characters are ignored) which specifies the alternative\nalphabet used instead of the + and / characters.

\n

The decoded string is returned. A TypeError is raised if s were\nincorrectly padded or if there are non-alphabet characters present in the\nstring.

\n
\n\n
\n
\nbase64.standard_b64encode(s)
\n
Encode string s using the standard Base64 alphabet.
\n\n
\n
\nbase64.standard_b64decode(s)
\n
Decode string s using the standard Base64 alphabet.
\n\n
\n
\nbase64.urlsafe_b64encode(s)
\n
Encode string s using a URL-safe alphabet, which substitutes - instead of\n+ and _ instead of / in the standard Base64 alphabet. The result\ncan still contain =.
\n\n
\n
\nbase64.urlsafe_b64decode(s)
\n
Decode string s using a URL-safe alphabet, which substitutes - instead of\n+ and _ instead of / in the standard Base64 alphabet.
\n\n
\n
\nbase64.b32encode(s)
\n
Encode a string using Base32. s is the string to encode. The encoded string\nis returned.
\n\n
\n
\nbase64.b32decode(s[, casefold[, map01]])
\n

Decode a Base32 encoded string.

\n

s is the string to decode. Optional casefold is a flag specifying whether a\nlowercase alphabet is acceptable as input. For security purposes, the default\nis False.

\n

RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O\n(oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)\nor letter L (el). The optional argument map01 when not None, specifies\nwhich letter the digit 1 should be mapped to (when map01 is not None, the\ndigit 0 is always mapped to the letter O). For security purposes the default is\nNone, so that 0 and 1 are not allowed in the input.

\n

The decoded string is returned. A TypeError is raised if s were\nincorrectly padded or if there are non-alphabet characters present in the\nstring.

\n
\n\n
\n
\nbase64.b16encode(s)
\n

Encode a string using Base16.

\n

s is the string to encode. The encoded string is returned.

\n
\n\n
\n
\nbase64.b16decode(s[, casefold])
\n

Decode a Base16 encoded string.

\n

s is the string to decode. Optional casefold is a flag specifying whether a\nlowercase alphabet is acceptable as input. For security purposes, the default\nis False.

\n

The decoded string is returned. A TypeError is raised if s were\nincorrectly padded or if there are non-alphabet characters present in the\nstring.

\n
\n\n

The legacy interface:

\n
\n
\nbase64.decode(input, output)
\n
Decode the contents of the input file and write the resulting binary data to\nthe output file. input and output must either be file objects or objects\nthat mimic the file object interface. input will be read until\ninput.read() returns an empty string.
\n\n
\n
\nbase64.decodestring(s)
\n
Decode the string s, which must contain one or more lines of base64 encoded\ndata, and return a string containing the resulting binary data.
\n\n
\n
\nbase64.encode(input, output)
\n
Encode the contents of the input file and write the resulting base64 encoded\ndata to the output file. input and output must either be file objects or\nobjects that mimic the file object interface. input will be read until\ninput.read() returns an empty string. encode() returns the encoded\ndata plus a trailing newline character ('\\n').
\n\n
\n
\nbase64.encodestring(s)
\n
Encode the string s, which can contain arbitrary binary data, and return a\nstring containing one or more lines of base64-encoded data.\nencodestring() returns a string containing one or more lines of\nbase64-encoded data always including an extra trailing newline ('\\n').
\n\n

An example usage of the module:

\n
>>> import base64\n>>> encoded = base64.b64encode('data to be encoded')\n>>> encoded\n'ZGF0YSB0byBiZSBlbmNvZGVk'\n>>> data = base64.b64decode(encoded)\n>>> data\n'data to be encoded'\n
\n
\n
\n

See also

\n
\n
Module binascii
\n
Support module containing ASCII-to-binary and binary-to-ASCII conversions.
\n
RFC 1521 - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
\n
Section 5.2, “Base64 Content-Transfer-Encoding,” provides the definition of the\nbase64 encoding.
\n
\n
\n
", "searchableItems": [ { "name": "base64.b16decode", "domId": "base64_base64.b16decode" }, { "name": "base64.b16encode", "domId": "base64_base64.b16encode" }, { "name": "base64.b32decode", "domId": "base64_base64.b32decode" }, { "name": "base64.b32encode", "domId": "base64_base64.b32encode" }, { "name": "base64.b64decode", "domId": "base64_base64.b64decode" }, { "name": "base64.b64encode", "domId": "base64_base64.b64encode" }, { "name": "base64.decode", "domId": "base64_base64.decode" }, { "name": "base64.decodestring", "domId": "base64_base64.decodestring" }, { "name": "base64.encode", "domId": "base64_base64.encode" }, { "name": "base64.encodestring", "domId": "base64_base64.encodestring" }, { "name": "base64.standard_b64decode", "domId": "base64_base64.standard_b64decode" }, { "name": "base64.standard_b64encode", "domId": "base64_base64.standard_b64encode" }, { "name": "base64.urlsafe_b64decode", "domId": "base64_base64.urlsafe_b64decode" }, { "name": "base64.urlsafe_b64encode", "domId": "base64_base64.urlsafe_b64encode" } ] }, { "url": "http://docs.python.org/library/rfc822.html", "title": "rfc822", "html": "
\n

18.11. rfc822 — Parse RFC 2822 mail headers

\n

\nDeprecated since version 2.3: The email package should be used in preference to the rfc822\nmodule. This module is present only to maintain backward compatibility, and\nhas been removed in 3.0.

\n

This module defines a class, Message, which represents an “email\nmessage” as defined by the Internet standard RFC 2822. [1] Such messages\nconsist of a collection of message headers, and a message body. This module\nalso defines a helper class AddressList for parsing RFC 2822\naddresses. Please refer to the RFC for information on the specific syntax of\nRFC 2822 messages.

\n

The mailbox module provides classes to read mailboxes produced by\nvarious end-user mail programs.

\n
\n
\nclass rfc822.Message(file[, seekable])
\n

A Message instance is instantiated with an input object as parameter.\nMessage relies only on the input object having a readline() method; in\nparticular, ordinary file objects qualify. Instantiation reads headers from the\ninput object up to a delimiter line (normally a blank line) and stores them in\nthe instance. The message body, following the headers, is not consumed.

\n

This class can work with any input object that supports a readline()\nmethod. If the input object has seek and tell capability, the\nrewindbody() method will work; also, illegal lines will be pushed back\nonto the input stream. If the input object lacks seek but has an unread()\nmethod that can push back a line of input, Message will use that to\npush back illegal lines. Thus this class can be used to parse messages coming\nfrom a buffered stream.

\n

The optional seekable argument is provided as a workaround for certain stdio\nlibraries in which tell() discards buffered data before discovering that\nthe lseek() system call doesn’t work. For maximum portability, you\nshould set the seekable argument to zero to prevent that initial tell()\nwhen passing in an unseekable object such as a file object created from a socket\nobject.

\n

Input lines as read from the file may either be terminated by CR-LF or by a\nsingle linefeed; a terminating CR-LF is replaced by a single linefeed before the\nline is stored.

\n

All header matching is done independent of upper or lower case; e.g.\nm['From'], m['from'] and m['FROM'] all yield the same result.

\n
\n\n
\n
\nclass rfc822.AddressList(field)
\n
You may instantiate the AddressList helper class using a single string\nparameter, a comma-separated list of RFC 2822 addresses to be parsed. (The\nparameter None yields an empty list.)
\n\n
\n
\nrfc822.quote(str)
\n
Return a new string with backslashes in str replaced by two backslashes and\ndouble quotes replaced by backslash-double quote.
\n\n
\n
\nrfc822.unquote(str)
\n
Return a new string which is an unquoted version of str. If str ends and\nbegins with double quotes, they are stripped off. Likewise if str ends and\nbegins with angle brackets, they are stripped off.
\n\n
\n
\nrfc822.parseaddr(address)
\n
Parse address, which should be the value of some address-containing field such\nas To or Cc, into its constituent “realname” and\n“email address” parts. Returns a tuple of that information, unless the parse\nfails, in which case a 2-tuple (None, None) is returned.
\n\n
\n
\nrfc822.dump_address_pair(pair)
\n
The inverse of parseaddr(), this takes a 2-tuple of the form (realname,\nemail_address) and returns the string value suitable for a To or\nCc header. If the first element of pair is false, then the\nsecond element is returned unmodified.
\n\n
\n
\nrfc822.parsedate(date)
\n
Attempts to parse a date according to the rules in RFC 2822. however, some\nmailers don’t follow that format as specified, so parsedate() tries to\nguess correctly in such cases. date is a string containing an RFC 2822\ndate, such as 'Mon, 20 Nov 1995 19:12:08 -0500'. If it succeeds in parsing\nthe date, parsedate() returns a 9-tuple that can be passed directly to\ntime.mktime(); otherwise None will be returned. Note that indexes 6,\n7, and 8 of the result tuple are not usable.
\n\n
\n
\nrfc822.parsedate_tz(date)
\n
Performs the same function as parsedate(), but returns either None or\na 10-tuple; the first 9 elements make up a tuple that can be passed directly to\ntime.mktime(), and the tenth is the offset of the date’s timezone from UTC\n(which is the official term for Greenwich Mean Time). (Note that the sign of\nthe timezone offset is the opposite of the sign of the time.timezone\nvariable for the same timezone; the latter variable follows the POSIX standard\nwhile this module follows RFC 2822.) If the input string has no timezone,\nthe last element of the tuple returned is None. Note that indexes 6, 7, and\n8 of the result tuple are not usable.
\n\n
\n
\nrfc822.mktime_tz(tuple)
\n
Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. If\nthe timezone item in the tuple is None, assume local time. Minor\ndeficiency: this first interprets the first 8 elements as a local time and then\ncompensates for the timezone difference; this may yield a slight error around\ndaylight savings time switch dates. Not enough to worry about for common use.
\n\n
\n

See also

\n
\n
Module email
\n
Comprehensive email handling package; supersedes the rfc822 module.
\n
Module mailbox
\n
Classes to read various mailbox formats produced by end-user mail programs.
\n
Module mimetools
\n
Subclass of rfc822.Message that handles MIME encoded messages.
\n
\n
\n
\n

18.11.1. Message Objects

\n

A Message instance has the following methods:

\n
\n
\nMessage.rewindbody()
\n
Seek to the start of the message body. This only works if the file object is\nseekable.
\n\n
\n
\nMessage.isheader(line)
\n
Returns a line’s canonicalized fieldname (the dictionary key that will be used\nto index it) if the line is a legal RFC 2822 header; otherwise returns\nNone (implying that parsing should stop here and the line be pushed back on\nthe input stream). It is sometimes useful to override this method in a\nsubclass.
\n\n
\n
\nMessage.islast(line)
\n
Return true if the given line is a delimiter on which Message should stop. The\ndelimiter line is consumed, and the file object’s read location positioned\nimmediately after it. By default this method just checks that the line is\nblank, but you can override it in a subclass.
\n\n
\n
\nMessage.iscomment(line)
\n
Return True if the given line should be ignored entirely, just skipped. By\ndefault this is a stub that always returns False, but you can override it in\na subclass.
\n\n
\n
\nMessage.getallmatchingheaders(name)
\n
Return a list of lines consisting of all headers matching name, if any. Each\nphysical line, whether it is a continuation line or not, is a separate list\nitem. Return the empty list if no header matches name.
\n\n
\n
\nMessage.getfirstmatchingheader(name)
\n
Return a list of lines comprising the first header matching name, and its\ncontinuation line(s), if any. Return None if there is no header matching\nname.
\n\n
\n
\nMessage.getrawheader(name)
\n
Return a single string consisting of the text after the colon in the first\nheader matching name. This includes leading whitespace, the trailing\nlinefeed, and internal linefeeds and whitespace if there any continuation\nline(s) were present. Return None if there is no header matching name.
\n\n
\n
\nMessage.getheader(name[, default])
\n
Return a single string consisting of the last header matching name,\nbut strip leading and trailing whitespace.\nInternal whitespace is not stripped. The optional default argument can be\nused to specify a different default to be returned when there is no header\nmatching name; it defaults to None.\nThis is the preferred way to get parsed headers.
\n\n
\n
\nMessage.get(name[, default])
\n
An alias for getheader(), to make the interface more compatible with\nregular dictionaries.
\n\n
\n
\nMessage.getaddr(name)
\n

Return a pair (full name, email address) parsed from the string returned by\ngetheader(name). If no header matching name exists, return (None,\nNone); otherwise both the full name and the address are (possibly empty)\nstrings.

\n

Example: If m‘s first From header contains the string\n'jack@cwi.nl (Jack Jansen)', then m.getaddr('From') will yield the pair\n('Jack Jansen', 'jack@cwi.nl'). If the header contained 'Jack Jansen\n<jack@cwi.nl>' instead, it would yield the exact same result.

\n
\n\n
\n
\nMessage.getaddrlist(name)
\n

This is similar to getaddr(list), but parses a header containing a list of\nemail addresses (e.g. a To header) and returns a list of (full\nname, email address) pairs (even if there was only one address in the header).\nIf there is no header matching name, return an empty list.

\n

If multiple headers exist that match the named header (e.g. if there are several\nCc headers), all are parsed for addresses. Any continuation lines\nthe named headers contain are also parsed.

\n
\n\n
\n
\nMessage.getdate(name)
\n

Retrieve a header using getheader() and parse it into a 9-tuple compatible\nwith time.mktime(); note that fields 6, 7, and 8 are not usable. If\nthere is no header matching name, or it is unparsable, return None.

\n

Date parsing appears to be a black art, and not all mailers adhere to the\nstandard. While it has been tested and found correct on a large collection of\nemail from many sources, it is still possible that this function may\noccasionally yield an incorrect result.

\n
\n\n
\n
\nMessage.getdate_tz(name)
\n
Retrieve a header using getheader() and parse it into a 10-tuple; the\nfirst 9 elements will make a tuple compatible with time.mktime(), and the\n10th is a number giving the offset of the date’s timezone from UTC. Note that\nfields 6, 7, and 8 are not usable. Similarly to getdate(), if there is\nno header matching name, or it is unparsable, return None.
\n\n

Message instances also support a limited mapping interface. In\nparticular: m[name] is like m.getheader(name) but raises KeyError\nif there is no matching header; and len(m), m.get(name[, default]),\nname in m, m.keys(), m.values() m.items(), and\nm.setdefault(name[, default]) act as expected, with the one difference\nthat setdefault() uses an empty string as the default value.\nMessage instances also support the mapping writable interface m[name]\n= value and del m[name]. Message objects do not support the\nclear(), copy(), popitem(), or update() methods of the\nmapping interface. (Support for get() and setdefault() was only\nadded in Python 2.2.)

\n

Finally, Message instances have some public instance variables:

\n
\n
\nMessage.headers
\n
A list containing the entire set of header lines, in the order in which they\nwere read (except that setitem calls may disturb this order). Each line contains\na trailing newline. The blank line terminating the headers is not contained in\nthe list.
\n\n
\n
\nMessage.fp
\n
The file or file-like object passed at instantiation time. This can be used to\nread the message content.
\n\n
\n
\nMessage.unixfrom
\n
The Unix From line, if the message had one, or an empty string. This is\nneeded to regenerate the message in some contexts, such as an mbox-style\nmailbox file.
\n\n
\n
\n

18.11.2. AddressList Objects

\n

An AddressList instance has the following methods:

\n
\n
\nAddressList.__len__()
\n
Return the number of addresses in the address list.
\n\n
\n
\nAddressList.__str__()
\n
Return a canonicalized string representation of the address list. Addresses are\nrendered in “name” <host@domain> form, comma-separated.
\n\n
\n
\nAddressList.__add__(alist)
\n
Return a new AddressList instance that contains all addresses in both\nAddressList operands, with duplicates removed (set union).
\n\n
\n
\nAddressList.__iadd__(alist)
\n
In-place version of __add__(); turns this AddressList instance\ninto the union of itself and the right-hand instance, alist.
\n\n
\n
\nAddressList.__sub__(alist)
\n
Return a new AddressList instance that contains every address in the\nleft-hand AddressList operand that is not present in the right-hand\naddress operand (set difference).
\n\n
\n
\nAddressList.__isub__(alist)
\n
In-place version of __sub__(), removing addresses in this list which are\nalso in alist.
\n\n

Finally, AddressList instances have one public instance variable:

\n
\n
\nAddressList.addresslist
\n
A list of tuple string pairs, one per address. In each member, the first is the\ncanonicalized name part, the second is the actual route-address ('@'-separated username-host.domain pair).
\n\n

Footnotes

\n\n\n\n\n\n
[1]This module originally conformed to RFC 822, hence the name. Since then,\nRFC 2822 has been released as an update to RFC 822. This module should be\nconsidered RFC 2822-conformant, especially in cases where the syntax or\nsemantics have changed since RFC 822.
\n
\n
", "searchableItems": [ { "name": "rfc822.AddressList", "domId": "rfc822_rfc822.AddressList" }, { "name": "rfc822.AddressList.__add__", "domId": "rfc822_rfc822.AddressList.__add__" }, { "name": "rfc822.AddressList.__iadd__", "domId": "rfc822_rfc822.AddressList.__iadd__" }, { "name": "rfc822.AddressList.__isub__", "domId": "rfc822_rfc822.AddressList.__isub__" }, { "name": "rfc822.AddressList.__len__", "domId": "rfc822_rfc822.AddressList.__len__" }, { "name": "rfc822.AddressList.__str__", "domId": "rfc822_rfc822.AddressList.__str__" }, { "name": "rfc822.AddressList.__sub__", "domId": "rfc822_rfc822.AddressList.__sub__" }, { "name": "rfc822.dump_address_pair", "domId": "rfc822_rfc822.dump_address_pair" }, { "name": "rfc822.Message", "domId": "rfc822_rfc822.Message" }, { "name": "rfc822.Message.get", "domId": "rfc822_rfc822.Message.get" }, { "name": "rfc822.Message.getaddr", "domId": "rfc822_rfc822.Message.getaddr" }, { "name": "rfc822.Message.getaddrlist", "domId": "rfc822_rfc822.Message.getaddrlist" }, { "name": "rfc822.Message.getallmatchingheaders", "domId": "rfc822_rfc822.Message.getallmatchingheaders" }, { "name": "rfc822.Message.getdate", "domId": "rfc822_rfc822.Message.getdate" }, { "name": "rfc822.Message.getdate_tz", "domId": "rfc822_rfc822.Message.getdate_tz" }, { "name": "rfc822.Message.getfirstmatchingheader", "domId": "rfc822_rfc822.Message.getfirstmatchingheader" }, { "name": "rfc822.Message.getheader", "domId": "rfc822_rfc822.Message.getheader" }, { "name": "rfc822.Message.getrawheader", "domId": "rfc822_rfc822.Message.getrawheader" }, { "name": "rfc822.Message.iscomment", "domId": "rfc822_rfc822.Message.iscomment" }, { "name": "rfc822.Message.isheader", "domId": "rfc822_rfc822.Message.isheader" }, { "name": "rfc822.Message.islast", "domId": "rfc822_rfc822.Message.islast" }, { "name": "rfc822.Message.rewindbody", "domId": "rfc822_rfc822.Message.rewindbody" }, { "name": "rfc822.mktime_tz", "domId": "rfc822_rfc822.mktime_tz" }, { "name": "rfc822.parseaddr", "domId": "rfc822_rfc822.parseaddr" }, { "name": "rfc822.parsedate", "domId": "rfc822_rfc822.parsedate" }, { "name": "rfc822.parsedate_tz", "domId": "rfc822_rfc822.parsedate_tz" }, { "name": "rfc822.quote", "domId": "rfc822_rfc822.quote" }, { "name": "rfc822.unquote", "domId": "rfc822_rfc822.unquote" } ] }, { "url": "http://docs.python.org/library/multifile.html", "title": "multifile", "html": "
\n

18.10. multifile — Support for files containing distinct parts

\n

\nDeprecated since version 2.5: The email package should be used in preference to the multifile\nmodule. This module is present only to maintain backward compatibility.

\n

The MultiFile object enables you to treat sections of a text file as\nfile-like input objects, with '' being returned by readline() when a\ngiven delimiter pattern is encountered. The defaults of this class are designed\nto make it useful for parsing MIME multipart messages, but by subclassing it and\noverriding methods it can be easily adapted for more general use.

\n
\n
\nclass multifile.MultiFile(fp[, seekable])
\n

Create a multi-file. You must instantiate this class with an input object\nargument for the MultiFile instance to get lines from, such as a file\nobject returned by open().

\n

MultiFile only ever looks at the input object’s readline(),\nseek() and tell() methods, and the latter two are only needed if you\nwant random access to the individual MIME parts. To use MultiFile on a\nnon-seekable stream object, set the optional seekable argument to false; this\nwill prevent using the input object’s seek() and tell() methods.

\n
\n\n

It will be useful to know that in MultiFile‘s view of the world, text\nis composed of three kinds of lines: data, section-dividers, and end-markers.\nMultiFile is designed to support parsing of messages that may have multiple\nnested message parts, each with its own pattern for section-divider and\nend-marker lines.

\n
\n

See also

\n
\n
Module email
\n
Comprehensive email handling package; supersedes the multifile module.
\n
\n
\n
\n

18.10.1. MultiFile Objects

\n

A MultiFile instance has the following methods:

\n
\n
\nMultiFile.readline(str)
\n
Read a line. If the line is data (not a section-divider or end-marker or real\nEOF) return it. If the line matches the most-recently-stacked boundary, return\n'' and set self.last to 1 or 0 according as the match is or is not an\nend-marker. If the line matches any other stacked boundary, raise an error. On\nencountering end-of-file on the underlying stream object, the method raises\nError unless all boundaries have been popped.
\n\n
\n
\nMultiFile.readlines(str)
\n
Return all lines remaining in this part as a list of strings.
\n\n
\n
\nMultiFile.read()
\n
Read all lines, up to the next section. Return them as a single (multiline)\nstring. Note that this doesn’t take a size argument!
\n\n
\n
\nMultiFile.seek(pos[, whence])
\n
Seek. Seek indices are relative to the start of the current section. The pos\nand whence arguments are interpreted as for a file seek.
\n\n
\n
\nMultiFile.tell()
\n
Return the file position relative to the start of the current section.
\n\n
\n
\nMultiFile.next()
\n
Skip lines to the next section (that is, read lines until a section-divider or\nend-marker has been consumed). Return true if there is such a section, false if\nan end-marker is seen. Re-enable the most-recently-pushed boundary.
\n\n
\n
\nMultiFile.is_data(str)
\n

Return true if str is data and false if it might be a section boundary. As\nwritten, it tests for a prefix other than '--' at start of line (which\nall MIME boundaries have) but it is declared so it can be overridden in derived\nclasses.

\n

Note that this test is used intended as a fast guard for the real boundary\ntests; if it always returns false it will merely slow processing, not cause it\nto fail.

\n
\n\n
\n
\nMultiFile.push(str)
\n

Push a boundary string. When a decorated version of this boundary is found as\nan input line, it will be interpreted as a section-divider or end-marker\n(depending on the decoration, see RFC 2045). All subsequent reads will\nreturn the empty string to indicate end-of-file, until a call to pop()\nremoves the boundary a or next() call reenables it.

\n

It is possible to push more than one boundary. Encountering the\nmost-recently-pushed boundary will return EOF; encountering any other\nboundary will raise an error.

\n
\n\n
\n
\nMultiFile.pop()
\n
Pop a section boundary. This boundary will no longer be interpreted as EOF.
\n\n
\n
\nMultiFile.section_divider(str)
\n
Turn a boundary into a section-divider line. By default, this method\nprepends '--' (which MIME section boundaries have) but it is declared so\nit can be overridden in derived classes. This method need not append LF or\nCR-LF, as comparison with the result ignores trailing whitespace.
\n\n
\n
\nMultiFile.end_marker(str)
\n
Turn a boundary string into an end-marker line. By default, this method\nprepends '--' and appends '--' (like a MIME-multipart end-of-message\nmarker) but it is declared so it can be overridden in derived classes. This\nmethod need not append LF or CR-LF, as comparison with the result ignores\ntrailing whitespace.
\n\n

Finally, MultiFile instances have two public instance variables:

\n
\n
\nMultiFile.level
\n
Nesting depth of the current part.
\n\n
\n
\nMultiFile.last
\n
True if the last end-of-file was for an end-of-message marker.
\n\n
\n
\n

18.10.2. MultiFile Example

\n
import mimetools\nimport multifile\nimport StringIO\n\ndef extract_mime_part_matching(stream, mimetype):\n    """Return the first element in a multipart MIME message on stream\n    matching mimetype."""\n\n    msg = mimetools.Message(stream)\n    msgtype = msg.gettype()\n    params = msg.getplist()\n\n    data = StringIO.StringIO()\n    if msgtype[:10] == "multipart/":\n\n        file = multifile.MultiFile(stream)\n        file.push(msg.getparam("boundary"))\n        while file.next():\n            submsg = mimetools.Message(file)\n            try:\n                data = StringIO.StringIO()\n                mimetools.decode(file, data, submsg.getencoding())\n            except ValueError:\n                continue\n            if submsg.gettype() == mimetype:\n                break\n        file.pop()\n    return data.getvalue()\n
\n
\n
\n
", "searchableItems": [ { "name": "multifile.MultiFile", "domId": "multifile_multifile.MultiFile" }, { "name": "multifile.MultiFile.end_marker", "domId": "multifile_multifile.MultiFile.end_marker" }, { "name": "multifile.MultiFile.is_data", "domId": "multifile_multifile.MultiFile.is_data" }, { "name": "multifile.MultiFile.next", "domId": "multifile_multifile.MultiFile.next" }, { "name": "multifile.MultiFile.pop", "domId": "multifile_multifile.MultiFile.pop" }, { "name": "multifile.MultiFile.push", "domId": "multifile_multifile.MultiFile.push" }, { "name": "multifile.MultiFile.read", "domId": "multifile_multifile.MultiFile.read" }, { "name": "multifile.MultiFile.readline", "domId": "multifile_multifile.MultiFile.readline" }, { "name": "multifile.MultiFile.readlines", "domId": "multifile_multifile.MultiFile.readlines" }, { "name": "multifile.MultiFile.section_divider", "domId": "multifile_multifile.MultiFile.section_divider" }, { "name": "multifile.MultiFile.seek", "domId": "multifile_multifile.MultiFile.seek" }, { "name": "multifile.MultiFile.tell", "domId": "multifile_multifile.MultiFile.tell" } ] }, { "url": "http://docs.python.org/library/uu.html", "title": "uu", "html": "
\n

18.16. uu — Encode and decode uuencode files

\n

Source code: Lib/uu.py

\n
\n

This module encodes and decodes files in uuencode format, allowing arbitrary\nbinary data to be transferred over ASCII-only connections. Wherever a file\nargument is expected, the methods accept a file-like object. For backwards\ncompatibility, a string containing a pathname is also accepted, and the\ncorresponding file will be opened for reading and writing; the pathname '-'\nis understood to mean the standard input or output. However, this interface is\ndeprecated; it’s better for the caller to open the file itself, and be sure\nthat, when required, the mode is 'rb' or 'wb' on Windows.

\n

This code was contributed by Lance Ellinghouse, and modified by Jack Jansen.

\n

The uu module defines the following functions:

\n
\n
\nuu.encode(in_file, out_file[, name[, mode]])
\n
Uuencode file in_file into file out_file. The uuencoded file will have the\nheader specifying name and mode as the defaults for the results of decoding\nthe file. The default defaults are taken from in_file, or '-' and 0666\nrespectively.
\n\n
\n
\nuu.decode(in_file[, out_file[, mode[, quiet]]])
\n

This call decodes uuencoded file in_file placing the result on file\nout_file. If out_file is a pathname, mode is used to set the permission\nbits if the file must be created. Defaults for out_file and mode are taken\nfrom the uuencode header. However, if the file specified in the header already\nexists, a uu.Error is raised.

\n

decode() may print a warning to standard error if the input was produced\nby an incorrect uuencoder and Python could recover from that error. Setting\nquiet to a true value silences this warning.

\n
\n\n
\n
\nexception uu.Error
\n
Subclass of Exception, this can be raised by uu.decode() under\nvarious situations, such as described above, but also including a badly\nformatted header, or truncated input file.
\n\n
\n

See also

\n
\n
Module binascii
\n
Support module containing ASCII-to-binary and binary-to-ASCII conversions.
\n
\n
\n
", "searchableItems": [ { "name": "uu.decode", "domId": "uu_uu.decode" }, { "name": "uu.encode", "domId": "uu_uu.encode" } ] }, { "url": "http://docs.python.org/library/binascii.html", "title": "binascii", "html": "
\n

18.14. binascii — Convert between binary and ASCII

\n

The binascii module contains a number of methods to convert between\nbinary and various ASCII-encoded binary representations. Normally, you will not\nuse these functions directly but use wrapper modules like uu,\nbase64, or binhex instead. The binascii module contains\nlow-level functions written in C for greater speed that are used by the\nhigher-level modules.

\n

The binascii module defines the following functions:

\n
\n
\nbinascii.a2b_uu(string)
\n
Convert a single line of uuencoded data back to binary and return the binary\ndata. Lines normally contain 45 (binary) bytes, except for the last line. Line\ndata may be followed by whitespace.
\n\n
\n
\nbinascii.b2a_uu(data)
\n
Convert binary data to a line of ASCII characters, the return value is the\nconverted line, including a newline char. The length of data should be at most\n45.
\n\n
\n
\nbinascii.a2b_base64(string)
\n
Convert a block of base64 data back to binary and return the binary data. More\nthan one line may be passed at a time.
\n\n
\n
\nbinascii.b2a_base64(data)
\n
Convert binary data to a line of ASCII characters in base64 coding. The return\nvalue is the converted line, including a newline char. The length of data\nshould be at most 57 to adhere to the base64 standard.
\n\n
\n
\nbinascii.a2b_qp(string[, header])
\n
Convert a block of quoted-printable data back to binary and return the binary\ndata. More than one line may be passed at a time. If the optional argument\nheader is present and true, underscores will be decoded as spaces.
\n\n
\n
\nbinascii.b2a_qp(data[, quotetabs, istext, header])
\n
Convert binary data to a line(s) of ASCII characters in quoted-printable\nencoding. The return value is the converted line(s). If the optional argument\nquotetabs is present and true, all tabs and spaces will be encoded. If the\noptional argument istext is present and true, newlines are not encoded but\ntrailing whitespace will be encoded. If the optional argument header is\npresent and true, spaces will be encoded as underscores per RFC1522. If the\noptional argument header is present and false, newline characters will be\nencoded as well; otherwise linefeed conversion might corrupt the binary data\nstream.
\n\n
\n
\nbinascii.a2b_hqx(string)
\n
Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.\nThe string should contain a complete number of binary bytes, or (in case of the\nlast portion of the binhex4 data) have the remaining bits zero.
\n\n
\n
\nbinascii.rledecode_hqx(data)
\n
Perform RLE-decompression on the data, as per the binhex4 standard. The\nalgorithm uses 0x90 after a byte as a repeat indicator, followed by a count.\nA count of 0 specifies a byte value of 0x90. The routine returns the\ndecompressed data, unless data input data ends in an orphaned repeat indicator,\nin which case the Incomplete exception is raised.
\n\n
\n
\nbinascii.rlecode_hqx(data)
\n
Perform binhex4 style RLE-compression on data and return the result.
\n\n
\n
\nbinascii.b2a_hqx(data)
\n
Perform hexbin4 binary-to-ASCII translation and return the resulting string. The\nargument should already be RLE-coded, and have a length divisible by 3 (except\npossibly the last fragment).
\n\n
\n
\nbinascii.crc_hqx(data, crc)
\n
Compute the binhex4 crc value of data, starting with an initial crc and\nreturning the result.
\n\n
\n
\nbinascii.crc32(data[, crc])
\n

Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This\nis consistent with the ZIP file checksum. Since the algorithm is designed for\nuse as a checksum algorithm, it is not suitable for use as a general hash\nalgorithm. Use as follows:

\n
print binascii.crc32("hello world")\n# Or, in two pieces:\ncrc = binascii.crc32("hello")\ncrc = binascii.crc32(" world", crc) & 0xffffffff\nprint 'crc32 = 0x%08x'  crc\n
\n
\n
\n\n
\n

Note

\n

To generate the same numeric value across all Python versions and\nplatforms use crc32(data) & 0xffffffff. If you are only using\nthe checksum in packed binary format this is not necessary as the\nreturn value is the correct 32bit binary representation\nregardless of sign.

\n
\n

\nChanged in version 2.6: The return value is in the range [-2**31, 2**31-1]\nregardless of platform. In the past the value would be signed on\nsome platforms and unsigned on others. Use & 0xffffffff on the\nvalue if you want it to match 3.0 behavior.

\n

\nChanged in version 3.0: The return value is unsigned and in the range [0, 2**32-1]\nregardless of platform.

\n
\n
\nbinascii.b2a_hex(data)
\n
\nbinascii.hexlify(data)
\n
Return the hexadecimal representation of the binary data. Every byte of\ndata is converted into the corresponding 2-digit hex representation. The\nresulting string is therefore twice as long as the length of data.
\n\n
\n
\nbinascii.a2b_hex(hexstr)
\n
\nbinascii.unhexlify(hexstr)
\n
Return the binary data represented by the hexadecimal string hexstr. This\nfunction is the inverse of b2a_hex(). hexstr must contain an even number\nof hexadecimal digits (which can be upper or lower case), otherwise a\nTypeError is raised.
\n\n
\n
\nexception binascii.Error
\n
Exception raised on errors. These are usually programming errors.
\n\n
\n
\nexception binascii.Incomplete
\n
Exception raised on incomplete data. These are usually not programming errors,\nbut may be handled by reading a little more data and trying again.
\n\n
\n

See also

\n
\n
Module base64
\n
Support for base64 encoding used in MIME email messages.
\n
Module binhex
\n
Support for the binhex format used on the Macintosh.
\n
Module uu
\n
Support for UU encoding used on Unix.
\n
Module quopri
\n
Support for quoted-printable encoding used in MIME email messages.
\n
\n
\n
", "searchableItems": [ { "name": "binascii.a2b_base64", "domId": "binascii_binascii.a2b_base64" }, { "name": "binascii.a2b_hex", "domId": "binascii_binascii.a2b_hex" }, { "name": "binascii.a2b_hqx", "domId": "binascii_binascii.a2b_hqx" }, { "name": "binascii.a2b_qp", "domId": "binascii_binascii.a2b_qp" }, { "name": "binascii.a2b_uu", "domId": "binascii_binascii.a2b_uu" }, { "name": "binascii.b2a_base64", "domId": "binascii_binascii.b2a_base64" }, { "name": "binascii.b2a_hex", "domId": "binascii_binascii.b2a_hex" }, { "name": "binascii.b2a_hqx", "domId": "binascii_binascii.b2a_hqx" }, { "name": "binascii.b2a_qp", "domId": "binascii_binascii.b2a_qp" }, { "name": "binascii.b2a_uu", "domId": "binascii_binascii.b2a_uu" }, { "name": "binascii.crc32", "domId": "binascii_binascii.crc32" }, { "name": "binascii.crc_hqx", "domId": "binascii_binascii.crc_hqx" }, { "name": "binascii.rlecode_hqx", "domId": "binascii_binascii.rlecode_hqx" }, { "name": "binascii.rledecode_hqx", "domId": "binascii_binascii.rledecode_hqx" } ] }, { "url": "http://docs.python.org/library/htmlparser.html", "title": "HTMLParser", "html": "
\n

19.1. HTMLParser — Simple HTML and XHTML parser

\n
\n

Note

\n

The HTMLParser module has been renamed to html.parser in Python\n3. The 2to3 tool will automatically adapt imports when converting\nyour sources to Python 3.

\n
\n

\nNew in version 2.2.

\n

Source code: Lib/HTMLParser.py

\n
\n

This module defines a class HTMLParser which serves as the basis for\nparsing text files formatted in HTML (HyperText Mark-up Language) and XHTML.\nUnlike the parser in htmllib, this parser is not based on the SGML parser\nin sgmllib.

\n
\n
\nclass HTMLParser.HTMLParser
\n

The HTMLParser class is instantiated without arguments.

\n

An HTMLParser instance is fed HTML data and calls handler functions when tags\nbegin and end. The HTMLParser class is meant to be overridden by the\nuser to provide a desired behavior.

\n

Unlike the parser in htmllib, this parser does not check that end tags\nmatch start tags or call the end-tag handler for elements which are closed\nimplicitly by closing an outer element.

\n
\n\n

An exception is defined as well:

\n
\n
\nexception HTMLParser.HTMLParseError
\n
Exception raised by the HTMLParser class when it encounters an error\nwhile parsing. This exception provides three attributes: msg is a brief\nmessage explaining the error, lineno is the number of the line on which\nthe broken construct was detected, and offset is the number of\ncharacters into the line at which the construct starts.
\n\n

HTMLParser instances have the following methods:

\n
\n
\nHTMLParser.reset()
\n
Reset the instance. Loses all unprocessed data. This is called implicitly at\ninstantiation time.
\n\n
\n
\nHTMLParser.feed(data)
\n
Feed some text to the parser. It is processed insofar as it consists of\ncomplete elements; incomplete data is buffered until more data is fed or\nclose() is called. data can be either unicode or\nstr, but passing unicode is advised.
\n\n
\n
\nHTMLParser.close()
\n
Force processing of all buffered data as if it were followed by an end-of-file\nmark. This method may be redefined by a derived class to define additional\nprocessing at the end of the input, but the redefined version should always call\nthe HTMLParser base class method close().
\n\n
\n
\nHTMLParser.getpos()
\n
Return current line number and offset.
\n\n
\n
\nHTMLParser.get_starttag_text()
\n
Return the text of the most recently opened start tag. This should not normally\nbe needed for structured processing, but may be useful in dealing with HTML “as\ndeployed” or for re-generating input with minimal changes (whitespace between\nattributes can be preserved, etc.).
\n\n
\n
\nHTMLParser.handle_starttag(tag, attrs)
\n

This method is called to handle the start of a tag. It is intended to be\noverridden by a derived class; the base class implementation does nothing.

\n

The tag argument is the name of the tag converted to lower case. The attrs\nargument is a list of (name, value) pairs containing the attributes found\ninside the tag’s <> brackets. The name will be translated to lower case,\nand quotes in the value have been removed, and character and entity references\nhave been replaced. For instance, for the tag <A\nHREF="http://www.cwi.nl/">, this method would be called as\nhandle_starttag('a', [('href', 'http://www.cwi.nl/')]).

\n

\nChanged in version 2.6: All entity references from htmlentitydefs are now replaced in the attribute\nvalues.

\n
\n\n
\n
\nHTMLParser.handle_startendtag(tag, attrs)
\n
Similar to handle_starttag(), but called when the parser encounters an\nXHTML-style empty tag (<img ... />). This method may be overridden by\nsubclasses which require this particular lexical information; the default\nimplementation simply calls handle_starttag() and handle_endtag().
\n\n
\n
\nHTMLParser.handle_endtag(tag)
\n
This method is called to handle the end tag of an element. It is intended to be\noverridden by a derived class; the base class implementation does nothing. The\ntag argument is the name of the tag converted to lower case.
\n\n
\n
\nHTMLParser.handle_data(data)
\n
This method is called to process arbitrary data (e.g. the content of\n<script>...</script> and <style>...</style>). It is intended to be\noverridden by a derived class; the base class implementation does nothing.
\n\n
\n
\nHTMLParser.handle_charref(name)
\n
This method is called to process a character reference of the form &#ref;.\nIt is intended to be overridden by a derived class; the base class\nimplementation does nothing.
\n\n
\n
\nHTMLParser.handle_entityref(name)
\n
This method is called to process a general entity reference of the form\n&name; where name is an general entity reference. It is intended to be\noverridden by a derived class; the base class implementation does nothing.
\n\n
\n
\nHTMLParser.handle_comment(data)
\n
This method is called when a comment is encountered. The comment argument is\na string containing the text between the -- and -- delimiters, but not\nthe delimiters themselves. For example, the comment <!--text--> will cause\nthis method to be called with the argument 'text'. It is intended to be\noverridden by a derived class; the base class implementation does nothing.
\n\n
\n
\nHTMLParser.handle_decl(decl)
\n
Method called when an SGML doctype declaration is read by the parser.\nThe decl parameter will be the entire contents of the declaration inside\nthe <!...> markup. It is intended to be overridden by a derived class;\nthe base class implementation does nothing.
\n\n
\n
\nHTMLParser.unknown_decl(data)
\n
Method called when an unrecognized SGML declaration is read by the parser.\nThe data parameter will be the entire contents of the declaration inside\nthe <!...> markup. It is sometimes useful to be overridden by a\nderived class; the base class implementation throws an HTMLParseError.
\n\n
\n
\nHTMLParser.handle_pi(data)
\n

Method called when a processing instruction is encountered. The data\nparameter will contain the entire processing instruction. For example, for the\nprocessing instruction <?proc color='red'>, this method would be called as\nhandle_pi("proc color='red'"). It is intended to be overridden by a derived\nclass; the base class implementation does nothing.

\n
\n

Note

\n

The HTMLParser class uses the SGML syntactic rules for processing\ninstructions. An XHTML processing instruction using the trailing '?' will\ncause the '?' to be included in data.

\n
\n
\n\n
\n

19.1.1. Example HTML Parser Application

\n

As a basic example, below is a simple HTML parser that uses the\nHTMLParser class to print out start tags, end tags and data\nas they are encountered:

\n
from HTMLParser import HTMLParser\n\nclass MyHTMLParser(HTMLParser):\n    def handle_starttag(self, tag, attrs):\n        print "Encountered a start tag:", tag\n    def handle_endtag(self, tag):\n        print "Encountered  an end tag:", tag\n    def handle_data(self, data):\n        print "Encountered   some data:", data\n\n\nparser = MyHTMLParser()\nparser.feed('<html><head><title>Test</title></head>'\n            '<body><h1>Parse me!</h1></body></html>')\n
\n
\n
\n
", "searchableItems": [ { "name": "HTMLParser.HTMLParser", "domId": "HTMLParser_HTMLParser.HTMLParser" }, { "name": "HTMLParser.HTMLParser.close", "domId": "HTMLParser_HTMLParser.HTMLParser.close" }, { "name": "HTMLParser.HTMLParser.feed", "domId": "HTMLParser_HTMLParser.HTMLParser.feed" }, { "name": "HTMLParser.HTMLParser.get_starttag_text", "domId": "HTMLParser_HTMLParser.HTMLParser.get_starttag_text" }, { "name": "HTMLParser.HTMLParser.getpos", "domId": "HTMLParser_HTMLParser.HTMLParser.getpos" }, { "name": "HTMLParser.HTMLParser.handle_charref", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_charref" }, { "name": "HTMLParser.HTMLParser.handle_comment", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_comment" }, { "name": "HTMLParser.HTMLParser.handle_data", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_data" }, { "name": "HTMLParser.HTMLParser.handle_decl", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_decl" }, { "name": "HTMLParser.HTMLParser.handle_endtag", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_endtag" }, { "name": "HTMLParser.HTMLParser.handle_entityref", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_entityref" }, { "name": "HTMLParser.HTMLParser.handle_pi", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_pi" }, { "name": "HTMLParser.HTMLParser.handle_startendtag", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_startendtag" }, { "name": "HTMLParser.HTMLParser.handle_starttag", "domId": "HTMLParser_HTMLParser.HTMLParser.handle_starttag" }, { "name": "HTMLParser.HTMLParser.reset", "domId": "HTMLParser_HTMLParser.HTMLParser.reset" }, { "name": "HTMLParser.HTMLParser.unknown_decl", "domId": "HTMLParser_HTMLParser.HTMLParser.unknown_decl" } ] }, { "url": "http://docs.python.org/library/binhex.html", "title": "binhex", "html": "
\n

18.13. binhex — Encode and decode binhex4 files

\n

This module encodes and decodes files in binhex4 format, a format allowing\nrepresentation of Macintosh files in ASCII. On the Macintosh, both forks of a\nfile and the finder information are encoded (or decoded), on other platforms\nonly the data fork is handled.

\n
\n

Note

\n

In Python 3.x, special Macintosh support has been removed.

\n
\n

The binhex module defines the following functions:

\n
\n
\nbinhex.binhex(input, output)
\n
Convert a binary file with filename input to binhex file output. The\noutput parameter can either be a filename or a file-like object (any object\nsupporting a write() and close() method).
\n\n
\n
\nbinhex.hexbin(input[, output])
\n
Decode a binhex file input. input may be a filename or a file-like object\nsupporting read() and close() methods. The resulting file is written\nto a file named output, unless the argument is omitted in which case the\noutput filename is read from the binhex file.
\n\n

The following exception is also defined:

\n
\n
\nexception binhex.Error
\n
Exception raised when something can’t be encoded using the binhex format (for\nexample, a filename is too long to fit in the filename field), or when input is\nnot properly encoded binhex data.
\n\n
\n

See also

\n
\n
Module binascii
\n
Support module containing ASCII-to-binary and binary-to-ASCII conversions.
\n
\n
\n
\n

18.13.1. Notes

\n

There is an alternative, more powerful interface to the coder and decoder, see\nthe source for details.

\n

If you code or decode textfiles on non-Macintosh platforms they will still use\nthe old Macintosh newline convention (carriage-return as end of line).

\n

As of this writing, hexbin() appears to not work in all cases.

\n
\n
", "searchableItems": [ { "name": "binhex.binhex", "domId": "binhex_binhex.binhex" }, { "name": "binhex.hexbin", "domId": "binhex_binhex.hexbin" } ] }, { "url": "http://docs.python.org/library/quopri.html", "title": "quopri", "html": "
\n

18.15. quopri — Encode and decode MIME quoted-printable data

\n

Source code: Lib/quopri.py

\n
\n

This module performs quoted-printable transport encoding and decoding, as\ndefined in RFC 1521: “MIME (Multipurpose Internet Mail Extensions) Part One:\nMechanisms for Specifying and Describing the Format of Internet Message Bodies”.\nThe quoted-printable encoding is designed for data where there are relatively\nfew nonprintable characters; the base64 encoding scheme available via the\nbase64 module is more compact if there are many such characters, as when\nsending a graphics file.

\n
\n
\nquopri.decode(input, output[, header])
\n
Decode the contents of the input file and write the resulting decoded binary\ndata to the output file. input and output must either be file objects or\nobjects that mimic the file object interface. input will be read until\ninput.readline() returns an empty string. If the optional argument header\nis present and true, underscore will be decoded as space. This is used to decode\n“Q”-encoded headers as described in RFC 1522: “MIME (Multipurpose Internet\nMail Extensions) Part Two: Message Header Extensions for Non-ASCII Text”.
\n\n
\n
\nquopri.encode(input, output, quotetabs)
\n
Encode the contents of the input file and write the resulting quoted-printable\ndata to the output file. input and output must either be file objects or\nobjects that mimic the file object interface. input will be read until\ninput.readline() returns an empty string. quotetabs is a flag which\ncontrols whether to encode embedded spaces and tabs; when true it encodes such\nembedded whitespace, and when false it leaves them unencoded. Note that spaces\nand tabs appearing at the end of lines are always encoded, as per RFC 1521.
\n\n
\n
\nquopri.decodestring(s[, header])
\n
Like decode(), except that it accepts a source string and returns the\ncorresponding decoded string.
\n\n
\n
\nquopri.encodestring(s[, quotetabs])
\n
Like encode(), except that it accepts a source string and returns the\ncorresponding encoded string. quotetabs is optional (defaulting to 0), and is\npassed straight through to encode().
\n\n
\n

See also

\n
\n
Module mimify
\n
General utilities for processing of MIME messages.
\n
Module base64
\n
Encode and decode MIME base64 data
\n
\n
\n
", "searchableItems": [ { "name": "quopri.decode", "domId": "quopri_quopri.decode" }, { "name": "quopri.decodestring", "domId": "quopri_quopri.decodestring" }, { "name": "quopri.encode", "domId": "quopri_quopri.encode" }, { "name": "quopri.encodestring", "domId": "quopri_quopri.encodestring" } ] }, { "url": "http://docs.python.org/library/sgmllib.html", "title": "sgmllib", "html": "
\n

19.2. sgmllib — Simple SGML parser

\n

\nDeprecated since version 2.6: The sgmllib module has been removed in Python 3.0.

\n

This module defines a class SGMLParser which serves as the basis for\nparsing text files formatted in SGML (Standard Generalized Mark-up Language).\nIn fact, it does not provide a full SGML parser — it only parses SGML insofar\nas it is used by HTML, and the module only exists as a base for the\nhtmllib module. Another HTML parser which supports XHTML and offers a\nsomewhat different interface is available in the HTMLParser module.

\n
\n
\nclass sgmllib.SGMLParser
\n

The SGMLParser class is instantiated without arguments. The parser is\nhardcoded to recognize the following constructs:

\n
    \n
  • Opening and closing tags of the form <tag attr="value" ...> and\n</tag>, respectively.
  • \n
  • Numeric character references of the form &#name;.
  • \n
  • Entity references of the form &name;.
  • \n
  • SGML comments of the form <!--text-->. Note that spaces, tabs, and\nnewlines are allowed between the trailing > and the immediately preceding\n--.
  • \n
\n
\n\n

A single exception is defined as well:

\n
\n
\nexception sgmllib.SGMLParseError
\n

Exception raised by the SGMLParser class when it encounters an error\nwhile parsing.

\n

\nNew in version 2.1.

\n
\n\n

SGMLParser instances have the following methods:

\n
\n
\nSGMLParser.reset()
\n
Reset the instance. Loses all unprocessed data. This is called implicitly at\ninstantiation time.
\n\n
\n
\nSGMLParser.setnomoretags()
\n
Stop processing tags. Treat all following input as literal input (CDATA).\n(This is only provided so the HTML tag <PLAINTEXT> can be implemented.)
\n\n
\n
\nSGMLParser.setliteral()
\n
Enter literal mode (CDATA mode).
\n\n
\n
\nSGMLParser.feed(data)
\n
Feed some text to the parser. It is processed insofar as it consists of\ncomplete elements; incomplete data is buffered until more data is fed or\nclose() is called.
\n\n
\n
\nSGMLParser.close()
\n
Force processing of all buffered data as if it were followed by an end-of-file\nmark. This method may be redefined by a derived class to define additional\nprocessing at the end of the input, but the redefined version should always call\nclose().
\n\n
\n
\nSGMLParser.get_starttag_text()
\n
Return the text of the most recently opened start tag. This should not normally\nbe needed for structured processing, but may be useful in dealing with HTML “as\ndeployed” or for re-generating input with minimal changes (whitespace between\nattributes can be preserved, etc.).
\n\n
\n
\nSGMLParser.handle_starttag(tag, method, attributes)
\n

This method is called to handle start tags for which either a start_tag()\nor do_tag() method has been defined. The tag argument is the name of\nthe tag converted to lower case, and the method argument is the bound method\nwhich should be used to support semantic interpretation of the start tag. The\nattributes argument is a list of (name, value) pairs containing the\nattributes found inside the tag’s <> brackets.

\n

The name has been translated to lower case. Double quotes and backslashes in\nthe value have been interpreted, as well as known character references and\nknown entity references terminated by a semicolon (normally, entity references\ncan be terminated by any non-alphanumerical character, but this would break the\nvery common case of <A HREF="url?spam=1&eggs=2"> when eggs is a valid\nentity name).

\n

For instance, for the tag <A HREF="http://www.cwi.nl/">, this method would\nbe called as unknown_starttag('a', [('href', 'http://www.cwi.nl/')]). The\nbase implementation simply calls method with attributes as the only\nargument.

\n

\nNew in version 2.5: Handling of entity and character references within attribute values.

\n
\n\n
\n
\nSGMLParser.handle_endtag(tag, method)
\n
This method is called to handle endtags for which an end_tag() method has\nbeen defined. The tag argument is the name of the tag converted to lower\ncase, and the method argument is the bound method which should be used to\nsupport semantic interpretation of the end tag. If no end_tag() method is\ndefined for the closing element, this handler is not called. The base\nimplementation simply calls method.
\n\n
\n
\nSGMLParser.handle_data(data)
\n
This method is called to process arbitrary data. It is intended to be\noverridden by a derived class; the base class implementation does nothing.
\n\n
\n
\nSGMLParser.handle_charref(ref)
\n

This method is called to process a character reference of the form &#ref;.\nThe base implementation uses convert_charref() to convert the reference to\na string. If that method returns a string, it is passed to handle_data(),\notherwise unknown_charref(ref) is called to handle the error.

\n

\nChanged in version 2.5: Use convert_charref() instead of hard-coding the conversion.

\n
\n\n
\n
\nSGMLParser.convert_charref(ref)
\n

Convert a character reference to a string, or None. ref is the reference\npassed in as a string. In the base implementation, ref must be a decimal\nnumber in the range 0-255. It converts the code point found using the\nconvert_codepoint() method. If ref is invalid or out of range, this\nmethod returns None. This method is called by the default\nhandle_charref() implementation and by the attribute value parser.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nSGMLParser.convert_codepoint(codepoint)
\n

Convert a codepoint to a str value. Encodings can be handled here if\nappropriate, though the rest of sgmllib is oblivious on this matter.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nSGMLParser.handle_entityref(ref)
\n

This method is called to process a general entity reference of the form\n&ref; where ref is an general entity reference. It converts ref by\npassing it to convert_entityref(). If a translation is returned, it calls\nthe method handle_data() with the translation; otherwise, it calls the\nmethod unknown_entityref(ref). The default entitydefs defines\ntranslations for &amp;, &apos, &gt;, &lt;, and &quot;.

\n

\nChanged in version 2.5: Use convert_entityref() instead of hard-coding the conversion.

\n
\n\n
\n
\nSGMLParser.convert_entityref(ref)
\n

Convert a named entity reference to a str value, or None. The\nresulting value will not be parsed. ref will be only the name of the entity.\nThe default implementation looks for ref in the instance (or class) variable\nentitydefs which should be a mapping from entity names to corresponding\ntranslations. If no translation is available for ref, this method returns\nNone. This method is called by the default handle_entityref()\nimplementation and by the attribute value parser.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nSGMLParser.handle_comment(comment)
\n
This method is called when a comment is encountered. The comment argument is\na string containing the text between the <!-- and --> delimiters, but\nnot the delimiters themselves. For example, the comment <!--text--> will\ncause this method to be called with the argument 'text'. The default method\ndoes nothing.
\n\n
\n
\nSGMLParser.handle_decl(data)
\n
Method called when an SGML declaration is read by the parser. In practice, the\nDOCTYPE declaration is the only thing observed in HTML, but the parser does\nnot discriminate among different (or broken) declarations. Internal subsets in\na DOCTYPE declaration are not supported. The data parameter will be the\nentire contents of the declaration inside the <!...> markup. The\ndefault implementation does nothing.
\n\n
\n
\nSGMLParser.report_unbalanced(tag)
\n
This method is called when an end tag is found which does not correspond to any\nopen element.
\n\n
\n
\nSGMLParser.unknown_starttag(tag, attributes)
\n
This method is called to process an unknown start tag. It is intended to be\noverridden by a derived class; the base class implementation does nothing.
\n\n
\n
\nSGMLParser.unknown_endtag(tag)
\n
This method is called to process an unknown end tag. It is intended to be\noverridden by a derived class; the base class implementation does nothing.
\n\n
\n
\nSGMLParser.unknown_charref(ref)
\n
This method is called to process unresolvable numeric character references.\nRefer to handle_charref() to determine what is handled by default. It is\nintended to be overridden by a derived class; the base class implementation does\nnothing.
\n\n
\n
\nSGMLParser.unknown_entityref(ref)
\n
This method is called to process an unknown entity reference. It is intended to\nbe overridden by a derived class; the base class implementation does nothing.
\n\n

Apart from overriding or extending the methods listed above, derived classes may\nalso define methods of the following form to define processing of specific tags.\nTag names in the input stream are case independent; the tag occurring in\nmethod names must be in lower case:

\n
\n
\nSGMLParser.start_tag(attributes)
\n
This method is called to process an opening tag tag. It has preference over\ndo_tag(). The attributes argument has the same meaning as described for\nhandle_starttag() above.
\n\n
\n
\nSGMLParser.do_tag(attributes)
\n
This method is called to process an opening tag tag for which no\nstart_tag() method is defined. The attributes argument has the same\nmeaning as described for handle_starttag() above.
\n\n
\n
\nSGMLParser.end_tag()
\n
This method is called to process a closing tag tag.
\n\n

Note that the parser maintains a stack of open elements for which no end tag has\nbeen found yet. Only tags processed by start_tag() are pushed on this\nstack. Definition of an end_tag() method is optional for these tags. For\ntags processed by do_tag() or by unknown_tag(), no end_tag()\nmethod must be defined; if defined, it will not be used. If both\nstart_tag() and do_tag() methods exist for a tag, the\nstart_tag() method takes precedence.

\n
", "searchableItems": [ { "name": "sgmllib.SGMLParser", "domId": "sgmllib_sgmllib.SGMLParser" }, { "name": "sgmllib.SGMLParser.close", "domId": "sgmllib_sgmllib.SGMLParser.close" }, { "name": "sgmllib.SGMLParser.convert_charref", "domId": "sgmllib_sgmllib.SGMLParser.convert_charref" }, { "name": "sgmllib.SGMLParser.convert_codepoint", "domId": "sgmllib_sgmllib.SGMLParser.convert_codepoint" }, { "name": "sgmllib.SGMLParser.convert_entityref", "domId": "sgmllib_sgmllib.SGMLParser.convert_entityref" }, { "name": "sgmllib.SGMLParser.feed", "domId": "sgmllib_sgmllib.SGMLParser.feed" }, { "name": "sgmllib.SGMLParser.get_starttag_text", "domId": "sgmllib_sgmllib.SGMLParser.get_starttag_text" }, { "name": "sgmllib.SGMLParser.handle_charref", "domId": "sgmllib_sgmllib.SGMLParser.handle_charref" }, { "name": "sgmllib.SGMLParser.handle_comment", "domId": "sgmllib_sgmllib.SGMLParser.handle_comment" }, { "name": "sgmllib.SGMLParser.handle_data", "domId": "sgmllib_sgmllib.SGMLParser.handle_data" }, { "name": "sgmllib.SGMLParser.handle_decl", "domId": "sgmllib_sgmllib.SGMLParser.handle_decl" }, { "name": "sgmllib.SGMLParser.handle_endtag", "domId": "sgmllib_sgmllib.SGMLParser.handle_endtag" }, { "name": "sgmllib.SGMLParser.handle_entityref", "domId": "sgmllib_sgmllib.SGMLParser.handle_entityref" }, { "name": "sgmllib.SGMLParser.handle_starttag", "domId": "sgmllib_sgmllib.SGMLParser.handle_starttag" }, { "name": "sgmllib.SGMLParser.report_unbalanced", "domId": "sgmllib_sgmllib.SGMLParser.report_unbalanced" }, { "name": "sgmllib.SGMLParser.reset", "domId": "sgmllib_sgmllib.SGMLParser.reset" }, { "name": "sgmllib.SGMLParser.setliteral", "domId": "sgmllib_sgmllib.SGMLParser.setliteral" }, { "name": "sgmllib.SGMLParser.setnomoretags", "domId": "sgmllib_sgmllib.SGMLParser.setnomoretags" }, { "name": "sgmllib.SGMLParser.unknown_charref", "domId": "sgmllib_sgmllib.SGMLParser.unknown_charref" }, { "name": "sgmllib.SGMLParser.unknown_endtag", "domId": "sgmllib_sgmllib.SGMLParser.unknown_endtag" }, { "name": "sgmllib.SGMLParser.unknown_entityref", "domId": "sgmllib_sgmllib.SGMLParser.unknown_entityref" }, { "name": "sgmllib.SGMLParser.unknown_starttag", "domId": "sgmllib_sgmllib.SGMLParser.unknown_starttag" } ] }, { "url": "http://docs.python.org/library/htmllib.html", "title": "htmllib", "html": "
\n

19.3. htmllib — A parser for HTML documents

\n

\nDeprecated since version 2.6: The htmllib module has been removed in Python 3.0.

\n

This module defines a class which can serve as a base for parsing text files\nformatted in the HyperText Mark-up Language (HTML). The class is not directly\nconcerned with I/O — it must be provided with input in string form via a\nmethod, and makes calls to methods of a “formatter” object in order to produce\noutput. The HTMLParser class is designed to be used as a base class\nfor other classes in order to add functionality, and allows most of its methods\nto be extended or overridden. In turn, this class is derived from and extends\nthe SGMLParser class defined in module sgmllib. The\nHTMLParser implementation supports the HTML 2.0 language as described\nin RFC 1866. Two implementations of formatter objects are provided in the\nformatter module; refer to the documentation for that module for\ninformation on the formatter interface.

\n

The following is a summary of the interface defined by\nsgmllib.SGMLParser:

\n\n

The module defines a parser class and an exception:

\n
\n
\nclass htmllib.HTMLParser(formatter)
\n
This is the basic HTML parser class. It supports all entity names required by\nthe XHTML 1.0 Recommendation (http://www.w3.org/TR/xhtml1). It also defines\nhandlers for all HTML 2.0 and many HTML 3.0 and 3.2 elements.
\n\n
\n
\nexception htmllib.HTMLParseError
\n

Exception raised by the HTMLParser class when it encounters an error\nwhile parsing.

\n

\nNew in version 2.4.

\n
\n\n
\n

See also

\n
\n
Module formatter
\n
Interface definition for transforming an abstract flow of formatting events into\nspecific output events on writer objects.
\n
Module HTMLParser
\n
Alternate HTML parser that offers a slightly lower-level view of the input, but\nis designed to work with XHTML, and does not implement some of the SGML syntax\nnot used in “HTML as deployed” and which isn’t legal for XHTML.
\n
Module htmlentitydefs
\n
Definition of replacement text for XHTML 1.0 entities.
\n
Module sgmllib
\n
Base class for HTMLParser.
\n
\n
\n
\n

19.3.1. HTMLParser Objects

\n

In addition to tag methods, the HTMLParser class provides some\nadditional methods and instance variables for use within tag methods.

\n
\n
\nHTMLParser.formatter
\n
This is the formatter instance associated with the parser.
\n\n
\n
\nHTMLParser.nofill
\n
Boolean flag which should be true when whitespace should not be collapsed, or\nfalse when it should be. In general, this should only be true when character\ndata is to be treated as “preformatted” text, as within a <PRE> element.\nThe default value is false. This affects the operation of handle_data()\nand save_end().
\n\n
\n
\nHTMLParser.anchor_bgn(href, name, type)
\n
This method is called at the start of an anchor region. The arguments\ncorrespond to the attributes of the <A> tag with the same names. The\ndefault implementation maintains a list of hyperlinks (defined by the HREF\nattribute for <A> tags) within the document. The list of hyperlinks is\navailable as the data attribute anchorlist.
\n\n
\n
\nHTMLParser.anchor_end()
\n
This method is called at the end of an anchor region. The default\nimplementation adds a textual footnote marker using an index into the list of\nhyperlinks created by anchor_bgn().
\n\n
\n
\nHTMLParser.handle_image(source, alt[, ismap[, align[, width[, height]]]])
\n
This method is called to handle images. The default implementation simply\npasses the alt value to the handle_data() method.
\n\n
\n
\nHTMLParser.save_bgn()
\n
Begins saving character data in a buffer instead of sending it to the formatter\nobject. Retrieve the stored data via save_end(). Use of the\nsave_bgn() / save_end() pair may not be nested.
\n\n
\n
\nHTMLParser.save_end()
\n
Ends buffering character data and returns all data saved since the preceding\ncall to save_bgn(). If the nofill flag is false, whitespace is\ncollapsed to single spaces. A call to this method without a preceding call to\nsave_bgn() will raise a TypeError exception.
\n\n
\n
\n

19.4. htmlentitydefs — Definitions of HTML general entities

\n
\n

Note

\n

The htmlentitydefs module has been renamed to html.entities in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

Source code: Lib/htmlentitydefs.py

\n
\n

This module defines three dictionaries, name2codepoint, codepoint2name,\nand entitydefs. entitydefs is used by the htmllib module to\nprovide the entitydefs attribute of the HTMLParser class. The\ndefinition provided here contains all the entities defined by XHTML 1.0 that\ncan be handled using simple textual substitution in the Latin-1 character set\n(ISO-8859-1).

\n
\n
\nhtmlentitydefs.entitydefs
\n
A dictionary mapping XHTML 1.0 entity definitions to their replacement text in\nISO Latin-1.
\n\n
\n
\nhtmlentitydefs.name2codepoint
\n

A dictionary that maps HTML entity names to the Unicode codepoints.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nhtmlentitydefs.codepoint2name
\n

A dictionary that maps Unicode codepoints to HTML entity names.

\n

\nNew in version 2.3.

\n
\n\n
", "searchableItems": [ { "name": "htmllib.HTMLParser", "domId": "htmllib_htmllib.HTMLParser" }, { "name": "htmllib.HTMLParser.anchor_bgn", "domId": "htmllib_htmllib.HTMLParser.anchor_bgn" }, { "name": "htmllib.HTMLParser.anchor_end", "domId": "htmllib_htmllib.HTMLParser.anchor_end" }, { "name": "htmllib.HTMLParser.handle_image", "domId": "htmllib_htmllib.HTMLParser.handle_image" }, { "name": "htmllib.HTMLParser.save_bgn", "domId": "htmllib_htmllib.HTMLParser.save_bgn" }, { "name": "htmllib.HTMLParser.save_end", "domId": "htmllib_htmllib.HTMLParser.save_end" } ] }, { "url": "http://docs.python.org/library/xml.dom.pulldom.html", "title": "xml.dom.pulldom", "html": "
\n

19.8. xml.dom.pulldom — Support for building partial DOM trees

\n

\nNew in version 2.0.

\n

Source code: Lib/xml/dom/pulldom.py

\n
\n

xml.dom.pulldom allows building only selected portions of a Document\nObject Model representation of a document from SAX events.

\n
\n
\nclass xml.dom.pulldom.PullDOM([documentFactory])
\n
xml.sax.handler.ContentHandler implementation that ...
\n\n
\n
\nclass xml.dom.pulldom.DOMEventStream(stream, parser, bufsize)
\n
...
\n\n
\n
\nclass xml.dom.pulldom.SAX2DOM([documentFactory])
\n
xml.sax.handler.ContentHandler implementation that ...
\n\n
\n
\nxml.dom.pulldom.parse(stream_or_string[, parser[, bufsize]])
\n
...
\n\n
\n
\nxml.dom.pulldom.parseString(string[, parser])
\n
...
\n\n
\n
\nxml.dom.pulldom.default_bufsize
\n

Default value for the bufsize parameter to parse().

\n

\nChanged in version 2.1: The value of this variable can be changed before calling parse() and the\nnew value will take effect.

\n
\n\n
\n

19.8.1. DOMEventStream Objects

\n
\n
\nDOMEventStream.getEvent()
\n
...
\n\n
\n
\nDOMEventStream.expandNode(node)
\n
...
\n\n
\n
\nDOMEventStream.reset()
\n
...
\n\n
\n
", "searchableItems": [ { "name": "xml.dom.pulldom.DOMEventStream", "domId": "xml.dom.pulldom_xml.dom.pulldom.DOMEventStream" }, { "name": "xml.dom.pulldom.DOMEventStream.expandNode", "domId": "xml.dom.pulldom_xml.dom.pulldom.DOMEventStream.expandNode" }, { "name": "xml.dom.pulldom.DOMEventStream.getEvent", "domId": "xml.dom.pulldom_xml.dom.pulldom.DOMEventStream.getEvent" }, { "name": "xml.dom.pulldom.DOMEventStream.reset", "domId": "xml.dom.pulldom_xml.dom.pulldom.DOMEventStream.reset" }, { "name": "xml.dom.pulldom.parse", "domId": "xml.dom.pulldom_xml.dom.pulldom.parse" }, { "name": "xml.dom.pulldom.parseString", "domId": "xml.dom.pulldom_xml.dom.pulldom.parseString" }, { "name": "xml.dom.pulldom.PullDOM", "domId": "xml.dom.pulldom_xml.dom.pulldom.PullDOM" }, { "name": "xml.dom.pulldom.SAX2DOM", "domId": "xml.dom.pulldom_xml.dom.pulldom.SAX2DOM" } ] }, { "url": "http://docs.python.org/library/xml.dom.html", "title": "xml.dom", "html": "
\n

19.6. xml.dom — The Document Object Model API

\n

\nNew in version 2.0.

\n

The Document Object Model, or “DOM,” is a cross-language API from the World Wide\nWeb Consortium (W3C) for accessing and modifying XML documents. A DOM\nimplementation presents an XML document as a tree structure, or allows client\ncode to build such a structure from scratch. It then gives access to the\nstructure through a set of objects which provided well-known interfaces.

\n

The DOM is extremely useful for random-access applications. SAX only allows you\na view of one bit of the document at a time. If you are looking at one SAX\nelement, you have no access to another. If you are looking at a text node, you\nhave no access to a containing element. When you write a SAX application, you\nneed to keep track of your program’s position in the document somewhere in your\nown code. SAX does not do it for you. Also, if you need to look ahead in the\nXML document, you are just out of luck.

\n

Some applications are simply impossible in an event driven model with no access\nto a tree. Of course you could build some sort of tree yourself in SAX events,\nbut the DOM allows you to avoid writing that code. The DOM is a standard tree\nrepresentation for XML data.

\n

The Document Object Model is being defined by the W3C in stages, or “levels” in\ntheir terminology. The Python mapping of the API is substantially based on the\nDOM Level 2 recommendation.

\n

DOM applications typically start by parsing some XML into a DOM. How this is\naccomplished is not covered at all by DOM Level 1, and Level 2 provides only\nlimited improvements: There is a DOMImplementation object class which\nprovides access to Document creation methods, but no way to access an\nXML reader/parser/Document builder in an implementation-independent way. There\nis also no well-defined way to access these methods without an existing\nDocument object. In Python, each DOM implementation will provide a\nfunction getDOMImplementation(). DOM Level 3 adds a Load/Store\nspecification, which defines an interface to the reader, but this is not yet\navailable in the Python standard library.

\n

Once you have a DOM document object, you can access the parts of your XML\ndocument through its properties and methods. These properties are defined in\nthe DOM specification; this portion of the reference manual describes the\ninterpretation of the specification in Python.

\n

The specification provided by the W3C defines the DOM API for Java, ECMAScript,\nand OMG IDL. The Python mapping defined here is based in large part on the IDL\nversion of the specification, but strict compliance is not required (though\nimplementations are free to support the strict mapping from IDL). See section\nConformance for a detailed discussion of mapping requirements.

\n
\n

See also

\n
\n
Document Object Model (DOM) Level 2 Specification
\n
The W3C recommendation upon which the Python DOM API is based.
\n
Document Object Model (DOM) Level 1 Specification
\n
The W3C recommendation for the DOM supported by xml.dom.minidom.
\n
Python Language Mapping Specification
\n
This specifies the mapping from OMG IDL to Python.
\n
\n
\n
\n

19.6.1. Module Contents

\n

The xml.dom contains the following functions:

\n
\n
\nxml.dom.registerDOMImplementation(name, factory)
\n
Register the factory function with the name name. The factory function\nshould return an object which implements the DOMImplementation\ninterface. The factory function can return the same object every time, or a new\none for each call, as appropriate for the specific implementation (e.g. if that\nimplementation supports some customization).
\n\n
\n
\nxml.dom.getDOMImplementation([name[, features]])
\n

Return a suitable DOM implementation. The name is either well-known, the\nmodule name of a DOM implementation, or None. If it is not None, imports\nthe corresponding module and returns a DOMImplementation object if the\nimport succeeds. If no name is given, and if the environment variable\nPYTHON_DOM is set, this variable is used to find the implementation.

\n

If name is not given, this examines the available implementations to find one\nwith the required feature set. If no implementation can be found, raise an\nImportError. The features list must be a sequence of (feature,\nversion) pairs which are passed to the hasFeature() method on available\nDOMImplementation objects.

\n
\n\n

Some convenience constants are also provided:

\n
\n
\nxml.dom.EMPTY_NAMESPACE
\n

The value used to indicate that no namespace is associated with a node in the\nDOM. This is typically found as the namespaceURI of a node, or used as\nthe namespaceURI parameter to a namespaces-specific method.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nxml.dom.XML_NAMESPACE
\n

The namespace URI associated with the reserved prefix xml, as defined by\nNamespaces in XML (section 4).

\n

\nNew in version 2.2.

\n
\n\n
\n
\nxml.dom.XMLNS_NAMESPACE
\n

The namespace URI for namespace declarations, as defined by Document Object\nModel (DOM) Level 2 Core Specification (section 1.1.8).

\n

\nNew in version 2.2.

\n
\n\n
\n
\nxml.dom.XHTML_NAMESPACE
\n

The URI of the XHTML namespace as defined by XHTML 1.0: The Extensible\nHyperText Markup Language (section 3.1.1).

\n

\nNew in version 2.2.

\n
\n\n

In addition, xml.dom contains a base Node class and the DOM\nexception classes. The Node class provided by this module does not\nimplement any of the methods or attributes defined by the DOM specification;\nconcrete DOM implementations must provide those. The Node class\nprovided as part of this module does provide the constants used for the\nnodeType attribute on concrete Node objects; they are located\nwithin the class rather than at the module level to conform with the DOM\nspecifications.

\n
\n
\n

19.6.2. Objects in the DOM

\n

The definitive documentation for the DOM is the DOM specification from the W3C.

\n

Note that DOM attributes may also be manipulated as nodes instead of as simple\nstrings. It is fairly rare that you must do this, however, so this usage is not\nyet documented.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
InterfaceSectionPurpose
DOMImplementationDOMImplementation ObjectsInterface to the underlying\nimplementation.
NodeNode ObjectsBase interface for most objects\nin a document.
NodeListNodeList ObjectsInterface for a sequence of\nnodes.
DocumentTypeDocumentType ObjectsInformation about the\ndeclarations needed to process\na document.
DocumentDocument ObjectsObject which represents an\nentire document.
ElementElement ObjectsElement nodes in the document\nhierarchy.
AttrAttr ObjectsAttribute value nodes on\nelement nodes.
CommentComment ObjectsRepresentation of comments in\nthe source document.
TextText and CDATASection ObjectsNodes containing textual\ncontent from the document.
ProcessingInstructionProcessingInstruction ObjectsProcessing instruction\nrepresentation.
\n

An additional section describes the exceptions defined for working with the DOM\nin Python.

\n
\n

19.6.2.1. DOMImplementation Objects

\n

The DOMImplementation interface provides a way for applications to\ndetermine the availability of particular features in the DOM they are using.\nDOM Level 2 added the ability to create new Document and\nDocumentType objects using the DOMImplementation as well.

\n
\n
\nDOMImplementation.hasFeature(feature, version)
\n
Return true if the feature identified by the pair of strings feature and\nversion is implemented.
\n\n
\n
\nDOMImplementation.createDocument(namespaceUri, qualifiedName, doctype)
\n
Return a new Document object (the root of the DOM), with a child\nElement object having the given namespaceUri and qualifiedName. The\ndoctype must be a DocumentType object created by\ncreateDocumentType(), or None. In the Python DOM API, the first two\narguments can also be None in order to indicate that no Element\nchild is to be created.
\n\n
\n
\nDOMImplementation.createDocumentType(qualifiedName, publicId, systemId)
\n
Return a new DocumentType object that encapsulates the given\nqualifiedName, publicId, and systemId strings, representing the\ninformation contained in an XML document type declaration.
\n\n
\n
\n

19.6.2.2. Node Objects

\n

All of the components of an XML document are subclasses of Node.

\n
\n
\nNode.nodeType
\n
An integer representing the node type. Symbolic constants for the types are on\nthe Node object: ELEMENT_NODE, ATTRIBUTE_NODE,\nTEXT_NODE, CDATA_SECTION_NODE, ENTITY_NODE,\nPROCESSING_INSTRUCTION_NODE, COMMENT_NODE,\nDOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE.\nThis is a read-only attribute.
\n\n
\n
\nNode.parentNode
\n
The parent of the current node, or None for the document node. The value is\nalways a Node object or None. For Element nodes, this\nwill be the parent element, except for the root element, in which case it will\nbe the Document object. For Attr nodes, this is always\nNone. This is a read-only attribute.
\n\n
\n
\nNode.attributes
\n
A NamedNodeMap of attribute objects. Only elements have actual values\nfor this; others provide None for this attribute. This is a read-only\nattribute.
\n\n
\n
\nNode.previousSibling
\n
The node that immediately precedes this one with the same parent. For\ninstance the element with an end-tag that comes just before the self\nelement’s start-tag. Of course, XML documents are made up of more than just\nelements so the previous sibling could be text, a comment, or something else.\nIf this node is the first child of the parent, this attribute will be\nNone. This is a read-only attribute.
\n\n
\n
\nNode.nextSibling
\n
The node that immediately follows this one with the same parent. See also\npreviousSibling. If this is the last child of the parent, this\nattribute will be None. This is a read-only attribute.
\n\n
\n
\nNode.childNodes
\n
A list of nodes contained within this node. This is a read-only attribute.
\n\n
\n
\nNode.firstChild
\n
The first child of the node, if there are any, or None. This is a read-only\nattribute.
\n\n
\n
\nNode.lastChild
\n
The last child of the node, if there are any, or None. This is a read-only\nattribute.
\n\n
\n
\nNode.localName
\n
The part of the tagName following the colon if there is one, else the\nentire tagName. The value is a string.
\n\n
\n
\nNode.prefix
\n
The part of the tagName preceding the colon if there is one, else the\nempty string. The value is a string, or None
\n\n
\n
\nNode.namespaceURI
\n
The namespace associated with the element name. This will be a string or\nNone. This is a read-only attribute.
\n\n
\n
\nNode.nodeName
\n
This has a different meaning for each node type; see the DOM specification for\ndetails. You can always get the information you would get here from another\nproperty such as the tagName property for elements or the name\nproperty for attributes. For all node types, the value of this attribute will be\neither a string or None. This is a read-only attribute.
\n\n
\n
\nNode.nodeValue
\n
This has a different meaning for each node type; see the DOM specification for\ndetails. The situation is similar to that with nodeName. The value is\na string or None.
\n\n
\n
\nNode.hasAttributes()
\n
Returns true if the node has any attributes.
\n\n
\n
\nNode.hasChildNodes()
\n
Returns true if the node has any child nodes.
\n\n
\n
\nNode.isSameNode(other)
\n

Returns true if other refers to the same node as this node. This is especially\nuseful for DOM implementations which use any sort of proxy architecture (because\nmore than one object can refer to the same node).

\n
\n

Note

\n

This is based on a proposed DOM Level 3 API which is still in the “working\ndraft” stage, but this particular interface appears uncontroversial. Changes\nfrom the W3C will not necessarily affect this method in the Python DOM interface\n(though any new W3C API for this would also be supported).

\n
\n
\n\n
\n
\nNode.appendChild(newChild)
\n
Add a new child node to this node at the end of the list of\nchildren, returning newChild. If the node was already in\nin the tree, it is removed first.
\n\n
\n
\nNode.insertBefore(newChild, refChild)
\n
Insert a new child node before an existing child. It must be the case that\nrefChild is a child of this node; if not, ValueError is raised.\nnewChild is returned. If refChild is None, it inserts newChild at the\nend of the children’s list.
\n\n
\n
\nNode.removeChild(oldChild)
\n
Remove a child node. oldChild must be a child of this node; if not,\nValueError is raised. oldChild is returned on success. If oldChild\nwill not be used further, its unlink() method should be called.
\n\n
\n
\nNode.replaceChild(newChild, oldChild)
\n
Replace an existing node with a new node. It must be the case that oldChild\nis a child of this node; if not, ValueError is raised.
\n\n
\n
\nNode.normalize()
\n

Join adjacent text nodes so that all stretches of text are stored as single\nText instances. This simplifies processing text from a DOM tree for\nmany applications.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nNode.cloneNode(deep)
\n
Clone this node. Setting deep means to clone all child nodes as well. This\nreturns the clone.
\n\n
\n
\n

19.6.2.3. NodeList Objects

\n

A NodeList represents a sequence of nodes. These objects are used in\ntwo ways in the DOM Core recommendation: the Element objects provides\none as its list of child nodes, and the getElementsByTagName() and\ngetElementsByTagNameNS() methods of Node return objects with this\ninterface to represent query results.

\n

The DOM Level 2 recommendation defines one method and one attribute for these\nobjects:

\n
\n
\nNodeList.item(i)
\n
Return the i‘th item from the sequence, if there is one, or None. The\nindex i is not allowed to be less then zero or greater than or equal to the\nlength of the sequence.
\n\n
\n
\nNodeList.length
\n
The number of nodes in the sequence.
\n\n

In addition, the Python DOM interface requires that some additional support is\nprovided to allow NodeList objects to be used as Python sequences. All\nNodeList implementations must include support for __len__() and\n__getitem__(); this allows iteration over the NodeList in\nfor statements and proper support for the len() built-in\nfunction.

\n

If a DOM implementation supports modification of the document, the\nNodeList implementation must also support the __setitem__() and\n__delitem__() methods.

\n
\n
\n

19.6.2.4. DocumentType Objects

\n

Information about the notations and entities declared by a document (including\nthe external subset if the parser uses it and can provide the information) is\navailable from a DocumentType object. The DocumentType for a\ndocument is available from the Document object’s doctype\nattribute; if there is no DOCTYPE declaration for the document, the\ndocument’s doctype attribute will be set to None instead of an\ninstance of this interface.

\n

DocumentType is a specialization of Node, and adds the\nfollowing attributes:

\n
\n
\nDocumentType.publicId
\n
The public identifier for the external subset of the document type definition.\nThis will be a string or None.
\n\n
\n
\nDocumentType.systemId
\n
The system identifier for the external subset of the document type definition.\nThis will be a URI as a string, or None.
\n\n
\n
\nDocumentType.internalSubset
\n
A string giving the complete internal subset from the document. This does not\ninclude the brackets which enclose the subset. If the document has no internal\nsubset, this should be None.
\n\n
\n
\nDocumentType.name
\n
The name of the root element as given in the DOCTYPE declaration, if\npresent.
\n\n
\n
\nDocumentType.entities
\n
This is a NamedNodeMap giving the definitions of external entities.\nFor entity names defined more than once, only the first definition is provided\n(others are ignored as required by the XML recommendation). This may be\nNone if the information is not provided by the parser, or if no entities are\ndefined.
\n\n
\n
\nDocumentType.notations
\n
This is a NamedNodeMap giving the definitions of notations. For\nnotation names defined more than once, only the first definition is provided\n(others are ignored as required by the XML recommendation). This may be\nNone if the information is not provided by the parser, or if no notations\nare defined.
\n\n
\n
\n

19.6.2.5. Document Objects

\n

A Document represents an entire XML document, including its constituent\nelements, attributes, processing instructions, comments etc. Remember that it\ninherits properties from Node.

\n
\n
\nDocument.documentElement
\n
The one and only root element of the document.
\n\n
\n
\nDocument.createElement(tagName)
\n
Create and return a new element node. The element is not inserted into the\ndocument when it is created. You need to explicitly insert it with one of the\nother methods such as insertBefore() or appendChild().
\n\n
\n
\nDocument.createElementNS(namespaceURI, tagName)
\n
Create and return a new element with a namespace. The tagName may have a\nprefix. The element is not inserted into the document when it is created. You\nneed to explicitly insert it with one of the other methods such as\ninsertBefore() or appendChild().
\n\n
\n
\nDocument.createTextNode(data)
\n
Create and return a text node containing the data passed as a parameter. As\nwith the other creation methods, this one does not insert the node into the\ntree.
\n\n
\n
\nDocument.createComment(data)
\n
Create and return a comment node containing the data passed as a parameter. As\nwith the other creation methods, this one does not insert the node into the\ntree.
\n\n
\n
\nDocument.createProcessingInstruction(target, data)
\n
Create and return a processing instruction node containing the target and\ndata passed as parameters. As with the other creation methods, this one does\nnot insert the node into the tree.
\n\n
\n
\nDocument.createAttribute(name)
\n
Create and return an attribute node. This method does not associate the\nattribute node with any particular element. You must use\nsetAttributeNode() on the appropriate Element object to use the\nnewly created attribute instance.
\n\n
\n
\nDocument.createAttributeNS(namespaceURI, qualifiedName)
\n
Create and return an attribute node with a namespace. The tagName may have a\nprefix. This method does not associate the attribute node with any particular\nelement. You must use setAttributeNode() on the appropriate\nElement object to use the newly created attribute instance.
\n\n
\n
\nDocument.getElementsByTagName(tagName)
\n
Search for all descendants (direct children, children’s children, etc.) with a\nparticular element type name.
\n\n
\n
\nDocument.getElementsByTagNameNS(namespaceURI, localName)
\n
Search for all descendants (direct children, children’s children, etc.) with a\nparticular namespace URI and localname. The localname is the part of the\nnamespace after the prefix.
\n\n
\n
\n

19.6.2.6. Element Objects

\n

Element is a subclass of Node, so inherits all the attributes\nof that class.

\n
\n
\nElement.tagName
\n
The element type name. In a namespace-using document it may have colons in it.\nThe value is a string.
\n\n
\n
\nElement.getElementsByTagName(tagName)
\n
Same as equivalent method in the Document class.
\n\n
\n
\nElement.getElementsByTagNameNS(namespaceURI, localName)
\n
Same as equivalent method in the Document class.
\n\n
\n
\nElement.hasAttribute(name)
\n
Returns true if the element has an attribute named by name.
\n\n
\n
\nElement.hasAttributeNS(namespaceURI, localName)
\n
Returns true if the element has an attribute named by namespaceURI and\nlocalName.
\n\n
\n
\nElement.getAttribute(name)
\n
Return the value of the attribute named by name as a string. If no such\nattribute exists, an empty string is returned, as if the attribute had no value.
\n\n
\n
\nElement.getAttributeNode(attrname)
\n
Return the Attr node for the attribute named by attrname.
\n\n
\n
\nElement.getAttributeNS(namespaceURI, localName)
\n
Return the value of the attribute named by namespaceURI and localName as a\nstring. If no such attribute exists, an empty string is returned, as if the\nattribute had no value.
\n\n
\n
\nElement.getAttributeNodeNS(namespaceURI, localName)
\n
Return an attribute value as a node, given a namespaceURI and localName.
\n\n
\n
\nElement.removeAttribute(name)
\n
Remove an attribute by name. If there is no matching attribute, a\nNotFoundErr is raised.
\n\n
\n
\nElement.removeAttributeNode(oldAttr)
\n
Remove and return oldAttr from the attribute list, if present. If oldAttr is\nnot present, NotFoundErr is raised.
\n\n
\n
\nElement.removeAttributeNS(namespaceURI, localName)
\n
Remove an attribute by name. Note that it uses a localName, not a qname. No\nexception is raised if there is no matching attribute.
\n\n
\n
\nElement.setAttribute(name, value)
\n
Set an attribute value from a string.
\n\n
\n
\nElement.setAttributeNode(newAttr)
\n
Add a new attribute node to the element, replacing an existing attribute if\nnecessary if the name attribute matches. If a replacement occurs, the\nold attribute node will be returned. If newAttr is already in use,\nInuseAttributeErr will be raised.
\n\n
\n
\nElement.setAttributeNodeNS(newAttr)
\n
Add a new attribute node to the element, replacing an existing attribute if\nnecessary if the namespaceURI and localName attributes match.\nIf a replacement occurs, the old attribute node will be returned. If newAttr\nis already in use, InuseAttributeErr will be raised.
\n\n
\n
\nElement.setAttributeNS(namespaceURI, qname, value)
\n
Set an attribute value from a string, given a namespaceURI and a qname.\nNote that a qname is the whole attribute name. This is different than above.
\n\n
\n
\n

19.6.2.7. Attr Objects

\n

Attr inherits from Node, so inherits all its attributes.

\n
\n
\nAttr.name
\n
The attribute name.\nIn a namespace-using document it may include a colon.
\n\n
\n
\nAttr.localName
\n
The part of the name following the colon if there is one, else the\nentire name.\nThis is a read-only attribute.
\n\n
\n
\nAttr.prefix
\n
The part of the name preceding the colon if there is one, else the\nempty string.
\n\n
\n
\nAttr.value
\n
The text value of the attribute. This is a synonym for the\nnodeValue attribute.
\n\n
\n
\n

19.6.2.8. NamedNodeMap Objects

\n

NamedNodeMap does not inherit from Node.

\n
\n
\nNamedNodeMap.length
\n
The length of the attribute list.
\n\n
\n
\nNamedNodeMap.item(index)
\n
Return an attribute with a particular index. The order you get the attributes\nin is arbitrary but will be consistent for the life of a DOM. Each item is an\nattribute node. Get its value with the value attribute.
\n\n

There are also experimental methods that give this class more mapping behavior.\nYou can use them or you can use the standardized getAttribute*() family\nof methods on the Element objects.

\n
\n
\n

19.6.2.9. Comment Objects

\n

Comment represents a comment in the XML document. It is a subclass of\nNode, but cannot have child nodes.

\n
\n
\nComment.data
\n
The content of the comment as a string. The attribute contains all characters\nbetween the leading <!-- and trailing -->, but does not\ninclude them.
\n\n
\n
\n

19.6.2.10. Text and CDATASection Objects

\n

The Text interface represents text in the XML document. If the parser\nand DOM implementation support the DOM’s XML extension, portions of the text\nenclosed in CDATA marked sections are stored in CDATASection objects.\nThese two interfaces are identical, but provide different values for the\nnodeType attribute.

\n

These interfaces extend the Node interface. They cannot have child\nnodes.

\n
\n
\nText.data
\n
The content of the text node as a string.
\n\n
\n

Note

\n

The use of a CDATASection node does not indicate that the node\nrepresents a complete CDATA marked section, only that the content of the node\nwas part of a CDATA section. A single CDATA section may be represented by more\nthan one node in the document tree. There is no way to determine whether two\nadjacent CDATASection nodes represent different CDATA marked sections.

\n
\n
\n
\n

19.6.2.11. ProcessingInstruction Objects

\n

Represents a processing instruction in the XML document; this inherits from the\nNode interface and cannot have child nodes.

\n
\n
\nProcessingInstruction.target
\n
The content of the processing instruction up to the first whitespace character.\nThis is a read-only attribute.
\n\n
\n
\nProcessingInstruction.data
\n
The content of the processing instruction following the first whitespace\ncharacter.
\n\n
\n
\n

19.6.2.12. Exceptions

\n

\nNew in version 2.1.

\n

The DOM Level 2 recommendation defines a single exception, DOMException,\nand a number of constants that allow applications to determine what sort of\nerror occurred. DOMException instances carry a code attribute\nthat provides the appropriate value for the specific exception.

\n

The Python DOM interface provides the constants, but also expands the set of\nexceptions so that a specific exception exists for each of the exception codes\ndefined by the DOM. The implementations must raise the appropriate specific\nexception, each of which carries the appropriate value for the code\nattribute.

\n
\n
\nexception xml.dom.DOMException
\n
Base exception class used for all specific DOM exceptions. This exception class\ncannot be directly instantiated.
\n\n
\n
\nexception xml.dom.DomstringSizeErr
\n
Raised when a specified range of text does not fit into a string. This is not\nknown to be used in the Python DOM implementations, but may be received from DOM\nimplementations not written in Python.
\n\n
\n
\nexception xml.dom.HierarchyRequestErr
\n
Raised when an attempt is made to insert a node where the node type is not\nallowed.
\n\n
\n
\nexception xml.dom.IndexSizeErr
\n
Raised when an index or size parameter to a method is negative or exceeds the\nallowed values.
\n\n
\n
\nexception xml.dom.InuseAttributeErr
\n
Raised when an attempt is made to insert an Attr node that is already\npresent elsewhere in the document.
\n\n
\n
\nexception xml.dom.InvalidAccessErr
\n
Raised if a parameter or an operation is not supported on the underlying object.
\n\n
\n
\nexception xml.dom.InvalidCharacterErr
\n
This exception is raised when a string parameter contains a character that is\nnot permitted in the context it’s being used in by the XML 1.0 recommendation.\nFor example, attempting to create an Element node with a space in the\nelement type name will cause this error to be raised.
\n\n
\n
\nexception xml.dom.InvalidModificationErr
\n
Raised when an attempt is made to modify the type of a node.
\n\n
\n
\nexception xml.dom.InvalidStateErr
\n
Raised when an attempt is made to use an object that is not defined or is no\nlonger usable.
\n\n
\n
\nexception xml.dom.NamespaceErr
\n
If an attempt is made to change any object in a way that is not permitted with\nregard to the Namespaces in XML\nrecommendation, this exception is raised.
\n\n
\n
\nexception xml.dom.NotFoundErr
\n
Exception when a node does not exist in the referenced context. For example,\nNamedNodeMap.removeNamedItem() will raise this if the node passed in does\nnot exist in the map.
\n\n
\n
\nexception xml.dom.NotSupportedErr
\n
Raised when the implementation does not support the requested type of object or\noperation.
\n\n
\n
\nexception xml.dom.NoDataAllowedErr
\n
This is raised if data is specified for a node which does not support data.
\n\n
\n
\nexception xml.dom.NoModificationAllowedErr
\n
Raised on attempts to modify an object where modifications are not allowed (such\nas for read-only nodes).
\n\n
\n
\nexception xml.dom.SyntaxErr
\n
Raised when an invalid or illegal string is specified.
\n\n
\n
\nexception xml.dom.WrongDocumentErr
\n
Raised when a node is inserted in a different document than it currently belongs\nto, and the implementation does not support migrating the node from one document\nto the other.
\n\n

The exception codes defined in the DOM recommendation map to the exceptions\ndescribed above according to this table:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantException
DOMSTRING_SIZE_ERRDomstringSizeErr
HIERARCHY_REQUEST_ERRHierarchyRequestErr
INDEX_SIZE_ERRIndexSizeErr
INUSE_ATTRIBUTE_ERRInuseAttributeErr
INVALID_ACCESS_ERRInvalidAccessErr
INVALID_CHARACTER_ERRInvalidCharacterErr
INVALID_MODIFICATION_ERRInvalidModificationErr
INVALID_STATE_ERRInvalidStateErr
NAMESPACE_ERRNamespaceErr
NOT_FOUND_ERRNotFoundErr
NOT_SUPPORTED_ERRNotSupportedErr
NO_DATA_ALLOWED_ERRNoDataAllowedErr
NO_MODIFICATION_ALLOWED_ERRNoModificationAllowedErr
SYNTAX_ERRSyntaxErr
WRONG_DOCUMENT_ERRWrongDocumentErr
\n
\n
\n
\n

19.6.3. Conformance

\n

This section describes the conformance requirements and relationships between\nthe Python DOM API, the W3C DOM recommendations, and the OMG IDL mapping for\nPython.

\n
\n

19.6.3.1. Type Mapping

\n

The primitive IDL types used in the DOM specification are mapped to Python types\naccording to the following table.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
IDL TypePython Type
booleanIntegerType (with a value of 0 or\n1)
intIntegerType
long intIntegerType
unsigned intIntegerType
\n

Additionally, the DOMString defined in the recommendation is mapped to\na Python string or Unicode string. Applications should be able to handle\nUnicode whenever a string is returned from the DOM.

\n

The IDL null value is mapped to None, which may be accepted or\nprovided by the implementation whenever null is allowed by the API.

\n
\n
\n

19.6.3.2. Accessor Methods

\n

The mapping from OMG IDL to Python defines accessor functions for IDL\nattribute declarations in much the way the Java mapping does.\nMapping the IDL declarations

\n
readonly attribute string someValue;\n         attribute string anotherValue;
\n
\n

yields three accessor functions: a “get” method for someValue\n(_get_someValue()), and “get” and “set” methods for anotherValue\n(_get_anotherValue() and _set_anotherValue()). The mapping, in\nparticular, does not require that the IDL attributes are accessible as normal\nPython attributes: object.someValue is not required to work, and may\nraise an AttributeError.

\n

The Python DOM API, however, does require that normal attribute access work.\nThis means that the typical surrogates generated by Python IDL compilers are not\nlikely to work, and wrapper objects may be needed on the client if the DOM\nobjects are accessed via CORBA. While this does require some additional\nconsideration for CORBA DOM clients, the implementers with experience using DOM\nover CORBA from Python do not consider this a problem. Attributes that are\ndeclared readonly may not restrict write access in all DOM\nimplementations.

\n

In the Python DOM API, accessor functions are not required. If provided, they\nshould take the form defined by the Python IDL mapping, but these methods are\nconsidered unnecessary since the attributes are accessible directly from Python.\n“Set” accessors should never be provided for readonly attributes.

\n

The IDL definitions do not fully embody the requirements of the W3C DOM API,\nsuch as the notion of certain objects, such as the return value of\ngetElementsByTagName(), being “live”. The Python DOM API does not require\nimplementations to enforce such requirements.

\n
\n
\n
", "searchableItems": [ { "name": "xml.dom.Document.createAttribute", "domId": "xml.dom_xml.dom.Document.createAttribute" }, { "name": "xml.dom.Document.createAttributeNS", "domId": "xml.dom_xml.dom.Document.createAttributeNS" }, { "name": "xml.dom.Document.createComment", "domId": "xml.dom_xml.dom.Document.createComment" }, { "name": "xml.dom.Document.createElement", "domId": "xml.dom_xml.dom.Document.createElement" }, { "name": "xml.dom.Document.createElementNS", "domId": "xml.dom_xml.dom.Document.createElementNS" }, { "name": "xml.dom.Document.createProcessingInstruction", "domId": "xml.dom_xml.dom.Document.createProcessingInstruction" }, { "name": "xml.dom.Document.createTextNode", "domId": "xml.dom_xml.dom.Document.createTextNode" }, { "name": "xml.dom.Document.getElementsByTagName", "domId": "xml.dom_xml.dom.Document.getElementsByTagName" }, { "name": "xml.dom.Document.getElementsByTagNameNS", "domId": "xml.dom_xml.dom.Document.getElementsByTagNameNS" }, { "name": "xml.dom.DOMImplementation.createDocument", "domId": "xml.dom_xml.dom.DOMImplementation.createDocument" }, { "name": "xml.dom.DOMImplementation.createDocumentType", "domId": "xml.dom_xml.dom.DOMImplementation.createDocumentType" }, { "name": "xml.dom.DOMImplementation.hasFeature", "domId": "xml.dom_xml.dom.DOMImplementation.hasFeature" }, { "name": "xml.dom.Element.getAttribute", "domId": "xml.dom_xml.dom.Element.getAttribute" }, { "name": "xml.dom.Element.getAttributeNode", "domId": "xml.dom_xml.dom.Element.getAttributeNode" }, { "name": "xml.dom.Element.getAttributeNodeNS", "domId": "xml.dom_xml.dom.Element.getAttributeNodeNS" }, { "name": "xml.dom.Element.getAttributeNS", "domId": "xml.dom_xml.dom.Element.getAttributeNS" }, { "name": "xml.dom.Element.getElementsByTagName", "domId": "xml.dom_xml.dom.Element.getElementsByTagName" }, { "name": "xml.dom.Element.getElementsByTagNameNS", "domId": "xml.dom_xml.dom.Element.getElementsByTagNameNS" }, { "name": "xml.dom.Element.hasAttribute", "domId": "xml.dom_xml.dom.Element.hasAttribute" }, { "name": "xml.dom.Element.hasAttributeNS", "domId": "xml.dom_xml.dom.Element.hasAttributeNS" }, { "name": "xml.dom.Element.removeAttribute", "domId": "xml.dom_xml.dom.Element.removeAttribute" }, { "name": "xml.dom.Element.removeAttributeNode", "domId": "xml.dom_xml.dom.Element.removeAttributeNode" }, { "name": "xml.dom.Element.removeAttributeNS", "domId": "xml.dom_xml.dom.Element.removeAttributeNS" }, { "name": "xml.dom.Element.setAttribute", "domId": "xml.dom_xml.dom.Element.setAttribute" }, { "name": "xml.dom.Element.setAttributeNode", "domId": "xml.dom_xml.dom.Element.setAttributeNode" }, { "name": "xml.dom.Element.setAttributeNodeNS", "domId": "xml.dom_xml.dom.Element.setAttributeNodeNS" }, { "name": "xml.dom.Element.setAttributeNS", "domId": "xml.dom_xml.dom.Element.setAttributeNS" }, { "name": "xml.dom.getDOMImplementation", "domId": "xml.dom_xml.dom.getDOMImplementation" }, { "name": "xml.dom.NamedNodeMap.item", "domId": "xml.dom_xml.dom.NamedNodeMap.item" }, { "name": "xml.dom.Node.appendChild", "domId": "xml.dom_xml.dom.Node.appendChild" }, { "name": "xml.dom.Node.cloneNode", "domId": "xml.dom_xml.dom.Node.cloneNode" }, { "name": "xml.dom.Node.hasAttributes", "domId": "xml.dom_xml.dom.Node.hasAttributes" }, { "name": "xml.dom.Node.hasChildNodes", "domId": "xml.dom_xml.dom.Node.hasChildNodes" }, { "name": "xml.dom.Node.insertBefore", "domId": "xml.dom_xml.dom.Node.insertBefore" }, { "name": "xml.dom.Node.isSameNode", "domId": "xml.dom_xml.dom.Node.isSameNode" }, { "name": "xml.dom.Node.normalize", "domId": "xml.dom_xml.dom.Node.normalize" }, { "name": "xml.dom.Node.removeChild", "domId": "xml.dom_xml.dom.Node.removeChild" }, { "name": "xml.dom.Node.replaceChild", "domId": "xml.dom_xml.dom.Node.replaceChild" }, { "name": "xml.dom.NodeList.item", "domId": "xml.dom_xml.dom.NodeList.item" }, { "name": "xml.dom.registerDOMImplementation", "domId": "xml.dom_xml.dom.registerDOMImplementation" } ] }, { "url": "http://docs.python.org/library/xml.sax.html", "title": "xml.sax", "html": "
\n

19.9. xml.sax — Support for SAX2 parsers

\n

\nNew in version 2.0.

\n

The xml.sax package provides a number of modules which implement the\nSimple API for XML (SAX) interface for Python. The package itself provides the\nSAX exceptions and the convenience functions which will be most used by users of\nthe SAX API.

\n

The convenience functions are:

\n
\n
\nxml.sax.make_parser([parser_list])
\n
Create and return a SAX XMLReader object. The first parser found will\nbe used. If parser_list is provided, it must be a sequence of strings which\nname modules that have a function named create_parser(). Modules listed\nin parser_list will be used before modules in the default list of parsers.
\n\n
\n
\nxml.sax.parse(filename_or_stream, handler[, error_handler])
\n
Create a SAX parser and use it to parse a document. The document, passed in as\nfilename_or_stream, can be a filename or a file object. The handler\nparameter needs to be a SAX ContentHandler instance. If\nerror_handler is given, it must be a SAX ErrorHandler instance; if\nomitted, SAXParseException will be raised on all errors. There is no\nreturn value; all work must be done by the handler passed in.
\n\n
\n
\nxml.sax.parseString(string, handler[, error_handler])
\n
Similar to parse(), but parses from a buffer string received as a\nparameter.
\n\n

A typical SAX application uses three kinds of objects: readers, handlers and\ninput sources. “Reader” in this context is another term for parser, i.e. some\npiece of code that reads the bytes or characters from the input source, and\nproduces a sequence of events. The events then get distributed to the handler\nobjects, i.e. the reader invokes a method on the handler. A SAX application\nmust therefore obtain a reader object, create or open the input sources, create\nthe handlers, and connect these objects all together. As the final step of\npreparation, the reader is called to parse the input. During parsing, methods on\nthe handler objects are called based on structural and syntactic events from the\ninput data.

\n

For these objects, only the interfaces are relevant; they are normally not\ninstantiated by the application itself. Since Python does not have an explicit\nnotion of interface, they are formally introduced as classes, but applications\nmay use implementations which do not inherit from the provided classes. The\nInputSource, Locator, Attributes,\nAttributesNS, and XMLReader interfaces are defined in the\nmodule xml.sax.xmlreader. The handler interfaces are defined in\nxml.sax.handler. For convenience, InputSource (which is often\ninstantiated directly) and the handler classes are also available from\nxml.sax. These interfaces are described below.

\n

In addition to these classes, xml.sax provides the following exception\nclasses.

\n
\n
\nexception xml.sax.SAXException(msg[, exception])
\n

Encapsulate an XML error or warning. This class can contain basic error or\nwarning information from either the XML parser or the application: it can be\nsubclassed to provide additional functionality or to add localization. Note\nthat although the handlers defined in the ErrorHandler interface\nreceive instances of this exception, it is not required to actually raise the\nexception — it is also useful as a container for information.

\n

When instantiated, msg should be a human-readable description of the error.\nThe optional exception parameter, if given, should be None or an exception\nthat was caught by the parsing code and is being passed along as information.

\n

This is the base class for the other SAX exception classes.

\n
\n\n
\n
\nexception xml.sax.SAXParseException(msg, exception, locator)
\n
Subclass of SAXException raised on parse errors. Instances of this class\nare passed to the methods of the SAX ErrorHandler interface to provide\ninformation about the parse error. This class supports the SAX Locator\ninterface as well as the SAXException interface.
\n\n
\n
\nexception xml.sax.SAXNotRecognizedException(msg[, exception])
\n
Subclass of SAXException raised when a SAX XMLReader is\nconfronted with an unrecognized feature or property. SAX applications and\nextensions may use this class for similar purposes.
\n\n
\n
\nexception xml.sax.SAXNotSupportedException(msg[, exception])
\n
Subclass of SAXException raised when a SAX XMLReader is asked to\nenable a feature that is not supported, or to set a property to a value that the\nimplementation does not support. SAX applications and extensions may use this\nclass for similar purposes.
\n\n
\n

See also

\n
\n
SAX: The Simple API for XML
\n
This site is the focal point for the definition of the SAX API. It provides a\nJava implementation and online documentation. Links to implementations and\nhistorical information are also available.
\n
Module xml.sax.handler
\n
Definitions of the interfaces for application-provided objects.
\n
Module xml.sax.saxutils
\n
Convenience functions for use in SAX applications.
\n
Module xml.sax.xmlreader
\n
Definitions of the interfaces for parser-provided objects.
\n
\n
\n
\n

19.9.1. SAXException Objects

\n

The SAXException exception class supports the following methods:

\n
\n
\nSAXException.getMessage()
\n
Return a human-readable message describing the error condition.
\n\n
\n
\nSAXException.getException()
\n
Return an encapsulated exception object, or None.
\n\n
\n
", "searchableItems": [ { "name": "xml.sax.make_parser", "domId": "xml.sax_xml.sax.make_parser" }, { "name": "xml.sax.parse", "domId": "xml.sax_xml.sax.parse" }, { "name": "xml.sax.parseString", "domId": "xml.sax_xml.sax.parseString" }, { "name": "xml.sax.SAXException.getException", "domId": "xml.sax_xml.sax.SAXException.getException" }, { "name": "xml.sax.SAXException.getMessage", "domId": "xml.sax_xml.sax.SAXException.getMessage" } ] }, { "url": "http://docs.python.org/library/xml.dom.minidom.html", "title": "xml.dom.minidom", "html": "
\n

19.7. xml.dom.minidom — Lightweight DOM implementation

\n

\nNew in version 2.0.

\n

Source code: Lib/xml/dom/minidom.py

\n
\n

xml.dom.minidom is a light-weight implementation of the Document Object\nModel interface. It is intended to be simpler than the full DOM and also\nsignificantly smaller.

\n

DOM applications typically start by parsing some XML into a DOM. With\nxml.dom.minidom, this is done through the parse functions:

\n
from xml.dom.minidom import parse, parseString\n\ndom1 = parse('c:\\\\temp\\\\mydata.xml') # parse an XML file by name\n\ndatasource = open('c:\\\\temp\\\\mydata.xml')\ndom2 = parse(datasource)   # parse an open file\n\ndom3 = parseString('<myxml>Some data<empty/> some more data</myxml>')\n
\n
\n

The parse() function can take either a filename or an open file object.

\n
\n
\nxml.dom.minidom.parse(filename_or_file[, parser[, bufsize]])
\n
Return a Document from the given input. filename_or_file may be\neither a file name, or a file-like object. parser, if given, must be a SAX2\nparser object. This function will change the document handler of the parser and\nactivate namespace support; other parser configuration (like setting an entity\nresolver) must have been done in advance.
\n\n

If you have XML in a string, you can use the parseString() function\ninstead:

\n
\n
\nxml.dom.minidom.parseString(string[, parser])
\n
Return a Document that represents the string. This method creates a\nStringIO object for the string and passes that on to parse().
\n\n

Both functions return a Document object representing the content of the\ndocument.

\n

What the parse() and parseString() functions do is connect an XML\nparser with a “DOM builder” that can accept parse events from any SAX parser and\nconvert them into a DOM tree. The name of the functions are perhaps misleading,\nbut are easy to grasp when learning the interfaces. The parsing of the document\nwill be completed before these functions return; it’s simply that these\nfunctions do not provide a parser implementation themselves.

\n

You can also create a Document by calling a method on a “DOM\nImplementation” object. You can get this object either by calling the\ngetDOMImplementation() function in the xml.dom package or the\nxml.dom.minidom module. Using the implementation from the\nxml.dom.minidom module will always return a Document instance\nfrom the minidom implementation, while the version from xml.dom may\nprovide an alternate implementation (this is likely if you have the PyXML\npackage installed). Once you have a\nDocument, you can add child nodes to it to populate the DOM:

\n
from xml.dom.minidom import getDOMImplementation\n\nimpl = getDOMImplementation()\n\nnewdoc = impl.createDocument(None, "some_tag", None)\ntop_element = newdoc.documentElement\ntext = newdoc.createTextNode('Some textual content.')\ntop_element.appendChild(text)\n
\n
\n

Once you have a DOM document object, you can access the parts of your XML\ndocument through its properties and methods. These properties are defined in\nthe DOM specification. The main property of the document object is the\ndocumentElement property. It gives you the main element in the XML\ndocument: the one that holds all others. Here is an example program:

\n
dom3 = parseString("<myxml>Some data</myxml>")\nassert dom3.documentElement.tagName == "myxml"\n
\n
\n

When you are finished with a DOM tree, you may optionally call the\nunlink() method to encourage early cleanup of the now-unneeded\nobjects. unlink() is a xml.dom.minidom-specific\nextension to the DOM API that renders the node and its descendants are\nessentially useless. Otherwise, Python’s garbage collector will\neventually take care of the objects in the tree.

\n
\n

See also

\n
\n
Document Object Model (DOM) Level 1 Specification
\n
The W3C recommendation for the DOM supported by xml.dom.minidom.
\n
\n
\n
\n

19.7.1. DOM Objects

\n

The definition of the DOM API for Python is given as part of the xml.dom\nmodule documentation. This section lists the differences between the API and\nxml.dom.minidom.

\n
\n
\nNode.unlink()
\n
Break internal references within the DOM so that it will be garbage collected on\nversions of Python without cyclic GC. Even when cyclic GC is available, using\nthis can make large amounts of memory available sooner, so calling this on DOM\nobjects as soon as they are no longer needed is good practice. This only needs\nto be called on the Document object, but may be called on child nodes\nto discard children of that node.
\n\n
\n
\nNode.writexml(writer[, indent=""[, addindent=""[, newl=""]]])
\n

Write XML to the writer object. The writer should have a write() method\nwhich matches that of the file object interface. The indent parameter is the\nindentation of the current node. The addindent parameter is the incremental\nindentation to use for subnodes of the current one. The newl parameter\nspecifies the string to use to terminate newlines.

\n

For the Document node, an additional keyword argument encoding can\nbe used to specify the encoding field of the XML header.

\n

\nChanged in version 2.1: The optional keyword parameters indent, addindent, and newl were added to\nsupport pretty output.

\n

\nChanged in version 2.3: For the Document node, an additional keyword argument\nencoding can be used to specify the encoding field of the XML header.

\n
\n\n
\n
\nNode.toxml([encoding])
\n

Return the XML that the DOM represents as a string.

\n

With no argument, the XML header does not specify an encoding, and the result is\nUnicode string if the default encoding cannot represent all characters in the\ndocument. Encoding this string in an encoding other than UTF-8 is likely\nincorrect, since UTF-8 is the default encoding of XML.

\n

With an explicit encoding [1] argument, the result is a byte string in the\nspecified encoding. It is recommended that this argument is always specified. To\navoid UnicodeError exceptions in case of unrepresentable text data, the\nencoding argument should be specified as “utf-8”.

\n

\nChanged in version 2.3: the encoding argument was introduced; see writexml().

\n
\n\n
\n
\nNode.toprettyxml([indent=""[, newl=""[, encoding=""]]])
\n

Return a pretty-printed version of the document. indent specifies the\nindentation string and defaults to a tabulator; newl specifies the string\nemitted at the end of each line and defaults to \\n.

\n

\nNew in version 2.1.

\n

\nChanged in version 2.3: the encoding argument was introduced; see writexml().

\n
\n\n

The following standard DOM methods have special considerations with\nxml.dom.minidom:

\n
\n
\nNode.cloneNode(deep)
\n
Although this method was present in the version of xml.dom.minidom\npackaged with Python 2.0, it was seriously broken. This has been corrected for\nsubsequent releases.
\n\n
\n
\n

19.7.2. DOM Example

\n

This example program is a fairly realistic example of a simple program. In this\nparticular case, we do not take much advantage of the flexibility of the DOM.

\n
import xml.dom.minidom\n\ndocument = """\\\n<slideshow>\n<title>Demo slideshow</title>\n<slide><title>Slide title</title>\n<point>This is a demo</point>\n<point>Of a program for processing slides</point>\n</slide>\n\n<slide><title>Another demo slide</title>\n<point>It is important</point>\n<point>To have more than</point>\n<point>one slide</point>\n</slide>\n</slideshow>\n"""\n\ndom = xml.dom.minidom.parseString(document)\n\ndef getText(nodelist):\n    rc = []\n    for node in nodelist:\n        if node.nodeType == node.TEXT_NODE:\n            rc.append(node.data)\n    return ''.join(rc)\n\ndef handleSlideshow(slideshow):\n    print "<html>"\n    handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])\n    slides = slideshow.getElementsByTagName("slide")\n    handleToc(slides)\n    handleSlides(slides)\n    print "</html>"\n\ndef handleSlides(slides):\n    for slide in slides:\n        handleSlide(slide)\n\ndef handleSlide(slide):\n    handleSlideTitle(slide.getElementsByTagName("title")[0])\n    handlePoints(slide.getElementsByTagName("point"))\n\ndef handleSlideshowTitle(title):\n    print "<title>%s</title>"  getText(title.childNodes)\n\ndef handleSlideTitle(title):\n    print "<h2>%s</h2>"  getText(title.childNodes)\n\ndef handlePoints(points):\n    print "<ul>"\n    for point in points:\n        handlePoint(point)\n    print "</ul>"\n\ndef handlePoint(point):\n    print "<li>%s</li>"  getText(point.childNodes)\n\ndef handleToc(slides):\n    for slide in slides:\n        title = slide.getElementsByTagName("title")[0]\n        print "<p>%s</p>"  getText(title.childNodes)\n\nhandleSlideshow(dom)\n
\n
\n
\n
\n

19.7.3. minidom and the DOM standard

\n

The xml.dom.minidom module is essentially a DOM 1.0-compatible DOM with\nsome DOM 2 features (primarily namespace features).

\n

Usage of the DOM interface in Python is straight-forward. The following mapping\nrules apply:

\n\n

The following interfaces have no implementation in xml.dom.minidom:

\n\n

Most of these reflect information in the XML document that is not of general\nutility to most DOM users.

\n

Footnotes

\n\n\n\n\n\n
[1]The encoding string included in XML output should conform to the\nappropriate standards. For example, “UTF-8” is valid, but “UTF8” is\nnot. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl\nand http://www.iana.org/assignments/character-sets .
\n
\n
", "searchableItems": [ { "name": "xml.dom.minidom.Node.cloneNode", "domId": "xml.dom.minidom_xml.dom.minidom.Node.cloneNode" }, { "name": "xml.dom.minidom.Node.toprettyxml", "domId": "xml.dom.minidom_xml.dom.minidom.Node.toprettyxml" }, { "name": "xml.dom.minidom.Node.toxml", "domId": "xml.dom.minidom_xml.dom.minidom.Node.toxml" }, { "name": "xml.dom.minidom.Node.unlink", "domId": "xml.dom.minidom_xml.dom.minidom.Node.unlink" }, { "name": "xml.dom.minidom.Node.writexml", "domId": "xml.dom.minidom_xml.dom.minidom.Node.writexml" }, { "name": "xml.dom.minidom.parse", "domId": "xml.dom.minidom_xml.dom.minidom.parse" }, { "name": "xml.dom.minidom.parseString", "domId": "xml.dom.minidom_xml.dom.minidom.parseString" } ] }, { "url": "http://docs.python.org/library/pyexpat.html", "title": "xml.parsers.expat", "html": "
\n

19.5. xml.parsers.expat — Fast XML parsing using Expat

\n

\nNew in version 2.0.

\n

The xml.parsers.expat module is a Python interface to the Expat\nnon-validating XML parser. The module provides a single extension type,\nxmlparser, that represents the current state of an XML parser. After\nan xmlparser object has been created, various attributes of the object\ncan be set to handler functions. When an XML document is then fed to the\nparser, the handler functions are called for the character data and markup in\nthe XML document.

\n

This module uses the pyexpat module to provide access to the Expat\nparser. Direct use of the pyexpat module is deprecated.

\n

This module provides one exception and one type object:

\n
\n
\nexception xml.parsers.expat.ExpatError
\n
The exception raised when Expat reports an error. See section\nExpatError Exceptions for more information on interpreting Expat errors.
\n\n
\n
\nexception xml.parsers.expat.error
\n
Alias for ExpatError.
\n\n
\n
\nxml.parsers.expat.XMLParserType
\n
The type of the return values from the ParserCreate() function.
\n\n

The xml.parsers.expat module contains two functions:

\n
\n
\nxml.parsers.expat.ErrorString(errno)
\n
Returns an explanatory string for a given error number errno.
\n\n
\n
\nxml.parsers.expat.ParserCreate([encoding[, namespace_separator]])
\n

Creates and returns a new xmlparser object. encoding, if specified,\nmust be a string naming the encoding used by the XML data. Expat doesn’t\nsupport as many encodings as Python does, and its repertoire of encodings can’t\nbe extended; it supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If\nencoding [1] is given it will override the implicit or explicit encoding of the\ndocument.

\n

Expat can optionally do XML namespace processing for you, enabled by providing a\nvalue for namespace_separator. The value must be a one-character string; a\nValueError will be raised if the string has an illegal length (None\nis considered the same as omission). When namespace processing is enabled,\nelement type names and attribute names that belong to a namespace will be\nexpanded. The element name passed to the element handlers\nStartElementHandler and EndElementHandler will be the\nconcatenation of the namespace URI, the namespace separator character, and the\nlocal part of the name. If the namespace separator is a zero byte (chr(0))\nthen the namespace URI and the local part will be concatenated without any\nseparator.

\n

For example, if namespace_separator is set to a space character (' ') and\nthe following document is parsed:

\n
<?xml version=\"1.0\"?>\n<root xmlns    = \"http://default-namespace.org/\"\n      xmlns:py = \"http://www.python.org/ns/\">\n  <py:elem1 />\n  <elem2 xmlns=\"\" />\n</root>
\n
\n

StartElementHandler will receive the following strings for each\nelement:

\n
http://default-namespace.org/ root\nhttp://www.python.org/ns/ elem1\nelem2
\n
\n
\n\n
\n

See also

\n
\n
The Expat XML Parser
\n
Home page of the Expat project.
\n
\n
\n
\n

19.5.1. XMLParser Objects

\n

xmlparser objects have the following methods:

\n
\n
\nxmlparser.Parse(data[, isfinal])
\n
Parses the contents of the string data, calling the appropriate handler\nfunctions to process the parsed data. isfinal must be true on the final call\nto this method. data can be the empty string at any time.
\n\n
\n
\nxmlparser.ParseFile(file)
\n
Parse XML data reading from the object file. file only needs to provide\nthe read(nbytes) method, returning the empty string when there’s no more\ndata.
\n\n
\n
\nxmlparser.SetBase(base)
\n
Sets the base to be used for resolving relative URIs in system identifiers in\ndeclarations. Resolving relative identifiers is left to the application: this\nvalue will be passed through as the base argument to the\nExternalEntityRefHandler(), NotationDeclHandler(), and\nUnparsedEntityDeclHandler() functions.
\n\n
\n
\nxmlparser.GetBase()
\n
Returns a string containing the base set by a previous call to SetBase(),\nor None if SetBase() hasn’t been called.
\n\n
\n
\nxmlparser.GetInputContext()
\n

Returns the input data that generated the current event as a string. The data is\nin the encoding of the entity which contains the text. When called while an\nevent handler is not active, the return value is None.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nxmlparser.ExternalEntityParserCreate(context[, encoding])
\n
Create a “child” parser which can be used to parse an external parsed entity\nreferred to by content parsed by the parent parser. The context parameter\nshould be the string passed to the ExternalEntityRefHandler() handler\nfunction, described below. The child parser is created with the\nordered_attributes, returns_unicode and\nspecified_attributes set to the values of this parser.
\n\n
\n
\nxmlparser.SetParamEntityParsing(flag)
\n
Control parsing of parameter entities (including the external DTD subset).\nPossible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\nXML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\nXML_PARAM_ENTITY_PARSING_ALWAYS. Return true if setting the flag\nwas successful.
\n\n
\n
\nxmlparser.UseForeignDTD([flag])
\n

Calling this with a true value for flag (the default) will cause Expat to call\nthe ExternalEntityRefHandler with None for all arguments to\nallow an alternate DTD to be loaded. If the document does not contain a\ndocument type declaration, the ExternalEntityRefHandler will still be\ncalled, but the StartDoctypeDeclHandler and\nEndDoctypeDeclHandler will not be called.

\n

Passing a false value for flag will cancel a previous call that passed a true\nvalue, but otherwise has no effect.

\n

This method can only be called before the Parse() or ParseFile()\nmethods are called; calling it after either of those have been called causes\nExpatError to be raised with the code attribute set to\nerrors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING.

\n

\nNew in version 2.3.

\n
\n\n

xmlparser objects have the following attributes:

\n
\n
\nxmlparser.buffer_size
\n

The size of the buffer used when buffer_text is true.\nA new buffer size can be set by assigning a new integer value\nto this attribute.\nWhen the size is changed, the buffer will be flushed.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.6: The buffer size can now be changed.

\n
\n\n
\n
\nxmlparser.buffer_text
\n

Setting this to true causes the xmlparser object to buffer textual\ncontent returned by Expat to avoid multiple calls to the\nCharacterDataHandler() callback whenever possible. This can improve\nperformance substantially since Expat normally breaks character data into chunks\nat every line ending. This attribute is false by default, and may be changed at\nany time.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nxmlparser.buffer_used
\n

If buffer_text is enabled, the number of bytes stored in the buffer.\nThese bytes represent UTF-8 encoded text. This attribute has no meaningful\ninterpretation when buffer_text is false.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nxmlparser.ordered_attributes
\n

Setting this attribute to a non-zero integer causes the attributes to be\nreported as a list rather than a dictionary. The attributes are presented in\nthe order found in the document text. For each attribute, two list entries are\npresented: the attribute name and the attribute value. (Older versions of this\nmodule also used this format.) By default, this attribute is false; it may be\nchanged at any time.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nxmlparser.returns_unicode
\n

If this attribute is set to a non-zero integer, the handler functions will be\npassed Unicode strings. If returns_unicode is False, 8-bit\nstrings containing UTF-8 encoded data will be passed to the handlers. This is\nTrue by default when Python is built with Unicode support.

\n

\nChanged in version 1.6: Can be changed at any time to affect the result type.

\n
\n\n
\n
\nxmlparser.specified_attributes
\n

If set to a non-zero integer, the parser will report only those attributes which\nwere specified in the document instance and not those which were derived from\nattribute declarations. Applications which set this need to be especially\ncareful to use what additional information is available from the declarations as\nneeded to comply with the standards for the behavior of XML processors. By\ndefault, this attribute is false; it may be changed at any time.

\n

\nNew in version 2.1.

\n
\n\n

The following attributes contain values relating to the most recent error\nencountered by an xmlparser object, and will only have correct values\nonce a call to Parse() or ParseFile() has raised a\nxml.parsers.expat.ExpatError exception.

\n
\n
\nxmlparser.ErrorByteIndex
\n
Byte index at which an error occurred.
\n\n
\n
\nxmlparser.ErrorCode
\n
Numeric code specifying the problem. This value can be passed to the\nErrorString() function, or compared to one of the constants defined in the\nerrors object.
\n\n
\n
\nxmlparser.ErrorColumnNumber
\n
Column number at which an error occurred.
\n\n
\n
\nxmlparser.ErrorLineNumber
\n
Line number at which an error occurred.
\n\n

The following attributes contain values relating to the current parse location\nin an xmlparser object. During a callback reporting a parse event they\nindicate the location of the first of the sequence of characters that generated\nthe event. When called outside of a callback, the position indicated will be\njust past the last parse event (regardless of whether there was an associated\ncallback).

\n

\nNew in version 2.4.

\n
\n
\nxmlparser.CurrentByteIndex
\n
Current byte index in the parser input.
\n\n
\n
\nxmlparser.CurrentColumnNumber
\n
Current column number in the parser input.
\n\n
\n
\nxmlparser.CurrentLineNumber
\n
Current line number in the parser input.
\n\n

Here is the list of handlers that can be set. To set a handler on an\nxmlparser object o, use o.handlername = func. handlername must\nbe taken from the following list, and func must be a callable object accepting\nthe correct number of arguments. The arguments are all strings, unless\notherwise stated.

\n
\n
\nxmlparser.XmlDeclHandler(version, encoding, standalone)
\n

Called when the XML declaration is parsed. The XML declaration is the\n(optional) declaration of the applicable version of the XML recommendation, the\nencoding of the document text, and an optional “standalone” declaration.\nversion and encoding will be strings of the type dictated by the\nreturns_unicode attribute, and standalone will be 1 if the\ndocument is declared standalone, 0 if it is declared not to be standalone,\nor -1 if the standalone clause was omitted. This is only available with\nExpat version 1.95.0 or newer.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nxmlparser.StartDoctypeDeclHandler(doctypeName, systemId, publicId, has_internal_subset)
\n
Called when Expat begins parsing the document type declaration (<!DOCTYPE\n...). The doctypeName is provided exactly as presented. The systemId and\npublicId parameters give the system and public identifiers if specified, or\nNone if omitted. has_internal_subset will be true if the document\ncontains and internal document declaration subset. This requires Expat version\n1.2 or newer.
\n\n
\n
\nxmlparser.EndDoctypeDeclHandler()
\n
Called when Expat is done parsing the document type declaration. This requires\nExpat version 1.2 or newer.
\n\n
\n
\nxmlparser.ElementDeclHandler(name, model)
\n
Called once for each element type declaration. name is the name of the\nelement type, and model is a representation of the content model.
\n\n
\n
\nxmlparser.AttlistDeclHandler(elname, attname, type, default, required)
\n
Called for each declared attribute for an element type. If an attribute list\ndeclaration declares three attributes, this handler is called three times, once\nfor each attribute. elname is the name of the element to which the\ndeclaration applies and attname is the name of the attribute declared. The\nattribute type is a string passed as type; the possible values are\n'CDATA', 'ID', 'IDREF', ... default gives the default value for\nthe attribute used when the attribute is not specified by the document instance,\nor None if there is no default value (#IMPLIED values). If the\nattribute is required to be given in the document instance, required will be\ntrue. This requires Expat version 1.95.0 or newer.
\n\n
\n
\nxmlparser.StartElementHandler(name, attributes)
\n
Called for the start of every element. name is a string containing the\nelement name, and attributes is a dictionary mapping attribute names to their\nvalues.
\n\n
\n
\nxmlparser.EndElementHandler(name)
\n
Called for the end of every element.
\n\n
\n
\nxmlparser.ProcessingInstructionHandler(target, data)
\n
Called for every processing instruction.
\n\n
\n
\nxmlparser.CharacterDataHandler(data)
\n
Called for character data. This will be called for normal character data, CDATA\nmarked content, and ignorable whitespace. Applications which must distinguish\nthese cases can use the StartCdataSectionHandler,\nEndCdataSectionHandler, and ElementDeclHandler callbacks to\ncollect the required information.
\n\n
\n
\nxmlparser.UnparsedEntityDeclHandler(entityName, base, systemId, publicId, notationName)
\n
Called for unparsed (NDATA) entity declarations. This is only present for\nversion 1.2 of the Expat library; for more recent versions, use\nEntityDeclHandler instead. (The underlying function in the Expat\nlibrary has been declared obsolete.)
\n\n
\n
\nxmlparser.EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName)
\n

Called for all entity declarations. For parameter and internal entities,\nvalue will be a string giving the declared contents of the entity; this will\nbe None for external entities. The notationName parameter will be\nNone for parsed entities, and the name of the notation for unparsed\nentities. is_parameter_entity will be true if the entity is a parameter entity\nor false for general entities (most applications only need to be concerned with\ngeneral entities). This is only available starting with version 1.95.0 of the\nExpat library.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nxmlparser.NotationDeclHandler(notationName, base, systemId, publicId)
\n
Called for notation declarations. notationName, base, and systemId, and\npublicId are strings if given. If the public identifier is omitted,\npublicId will be None.
\n\n
\n
\nxmlparser.StartNamespaceDeclHandler(prefix, uri)
\n
Called when an element contains a namespace declaration. Namespace declarations\nare processed before the StartElementHandler is called for the element\non which declarations are placed.
\n\n
\n
\nxmlparser.EndNamespaceDeclHandler(prefix)
\n
Called when the closing tag is reached for an element that contained a\nnamespace declaration. This is called once for each namespace declaration on\nthe element in the reverse of the order for which the\nStartNamespaceDeclHandler was called to indicate the start of each\nnamespace declaration’s scope. Calls to this handler are made after the\ncorresponding EndElementHandler for the end of the element.
\n\n
\n
\nxmlparser.CommentHandler(data)
\n
Called for comments. data is the text of the comment, excluding the leading\n‘<!--‘ and trailing ‘-->‘.
\n\n
\n
\nxmlparser.StartCdataSectionHandler()
\n
Called at the start of a CDATA section. This and EndCdataSectionHandler\nare needed to be able to identify the syntactical start and end for CDATA\nsections.
\n\n
\n
\nxmlparser.EndCdataSectionHandler()
\n
Called at the end of a CDATA section.
\n\n
\n
\nxmlparser.DefaultHandler(data)
\n
Called for any characters in the XML document for which no applicable handler\nhas been specified. This means characters that are part of a construct which\ncould be reported, but for which no handler has been supplied.
\n\n
\n
\nxmlparser.DefaultHandlerExpand(data)
\n
This is the same as the DefaultHandler(), but doesn’t inhibit expansion\nof internal entities. The entity reference will not be passed to the default\nhandler.
\n\n
\n
\nxmlparser.NotStandaloneHandler()
\n
Called if the XML document hasn’t been declared as being a standalone document.\nThis happens when there is an external subset or a reference to a parameter\nentity, but the XML declaration does not set standalone to yes in an XML\ndeclaration. If this handler returns 0, then the parser will raise an\nXML_ERROR_NOT_STANDALONE error. If this handler is not set, no\nexception is raised by the parser for this condition.
\n\n
\n
\nxmlparser.ExternalEntityRefHandler(context, base, systemId, publicId)
\n

Called for references to external entities. base is the current base, as set\nby a previous call to SetBase(). The public and system identifiers,\nsystemId and publicId, are strings if given; if the public identifier is not\ngiven, publicId will be None. The context value is opaque and should\nonly be used as described below.

\n

For external entities to be parsed, this handler must be implemented. It is\nresponsible for creating the sub-parser using\nExternalEntityParserCreate(context), initializing it with the appropriate\ncallbacks, and parsing the entity. This handler should return an integer; if it\nreturns 0, the parser will raise an\nXML_ERROR_EXTERNAL_ENTITY_HANDLING error, otherwise parsing will\ncontinue.

\n

If this handler is not provided, external entities are reported by the\nDefaultHandler callback, if provided.

\n
\n\n
\n
\n

19.5.2. ExpatError Exceptions

\n

ExpatError exceptions have a number of interesting attributes:

\n
\n
\nExpatError.code
\n

Expat’s internal error number for the specific error. This will match one of\nthe constants defined in the errors object from this module.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nExpatError.lineno
\n

Line number on which the error was detected. The first line is numbered 1.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nExpatError.offset
\n

Character offset into the line where the error occurred. The first column is\nnumbered 0.

\n

\nNew in version 2.1.

\n
\n\n
\n
\n

19.5.3. Example

\n

The following program defines three handlers that just print out their\narguments.

\n
import xml.parsers.expat\n\n# 3 handler functions\ndef start_element(name, attrs):\n    print 'Start element:', name, attrs\ndef end_element(name):\n    print 'End element:', name\ndef char_data(data):\n    print 'Character data:', repr(data)\n\np = xml.parsers.expat.ParserCreate()\n\np.StartElementHandler = start_element\np.EndElementHandler = end_element\np.CharacterDataHandler = char_data\n\np.Parse("""<?xml version="1.0"?>\n<parent id="top"><child1 name="paul">Text goes here</child1>\n<child2 name="fred">More text</child2>\n</parent>""", 1)\n
\n
\n

The output from this program is:

\n
Start element: parent {'id': 'top'}\nStart element: child1 {'name': 'paul'}\nCharacter data: 'Text goes here'\nEnd element: child1\nCharacter data: '\\n'\nStart element: child2 {'name': 'fred'}\nCharacter data: 'More text'\nEnd element: child2\nCharacter data: '\\n'\nEnd element: parent
\n
\n
\n
\n

19.5.4. Content Model Descriptions

\n

Content modules are described using nested tuples. Each tuple contains four\nvalues: the type, the quantifier, the name, and a tuple of children. Children\nare simply additional content module descriptions.

\n

The values of the first two fields are constants defined in the model object\nof the xml.parsers.expat module. These constants can be collected in two\ngroups: the model type group and the quantifier group.

\n

The constants in the model type group are:

\n
\n
\nxml.parsers.expat.XML_CTYPE_ANY
\n
The element named by the model name was declared to have a content model of\nANY.
\n\n
\n
\nxml.parsers.expat.XML_CTYPE_CHOICE
\n
The named element allows a choice from a number of options; this is used for\ncontent models such as (A | B | C).
\n\n
\n
\nxml.parsers.expat.XML_CTYPE_EMPTY
\n
Elements which are declared to be EMPTY have this model type.
\n\n
\n
\nxml.parsers.expat.XML_CTYPE_MIXED
\n
\n\n
\n
\nxml.parsers.expat.XML_CTYPE_NAME
\n
\n\n
\n
\nxml.parsers.expat.XML_CTYPE_SEQ
\n
Models which represent a series of models which follow one after the other are\nindicated with this model type. This is used for models such as (A, B, C).
\n\n

The constants in the quantifier group are:

\n
\n
\nxml.parsers.expat.XML_CQUANT_NONE
\n
No modifier is given, so it can appear exactly once, as for A.
\n\n
\n
\nxml.parsers.expat.XML_CQUANT_OPT
\n
The model is optional: it can appear once or not at all, as for A?.
\n\n
\n
\nxml.parsers.expat.XML_CQUANT_PLUS
\n
The model must occur one or more times (like A+).
\n\n
\n
\nxml.parsers.expat.XML_CQUANT_REP
\n
The model must occur zero or more times, as for A*.
\n\n
\n
\n

19.5.5. Expat error constants

\n

The following constants are provided in the errors object of the\nxml.parsers.expat module. These constants are useful in interpreting\nsome of the attributes of the ExpatError exception objects raised when an\nerror has occurred.

\n

The errors object has the following attributes:

\n
\n
\nxml.parsers.expat.XML_ERROR_ASYNC_ENTITY
\n
\n\n
\n
\nxml.parsers.expat.XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF
\n
An entity reference in an attribute value referred to an external entity instead\nof an internal entity.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_BAD_CHAR_REF
\n
A character reference referred to a character which is illegal in XML (for\nexample, character 0, or ‘&#0;‘).
\n\n
\n
\nxml.parsers.expat.XML_ERROR_BINARY_ENTITY_REF
\n
An entity reference referred to an entity which was declared with a notation, so\ncannot be parsed.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_DUPLICATE_ATTRIBUTE
\n
An attribute was used more than once in a start tag.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_INCORRECT_ENCODING
\n
\n\n
\n
\nxml.parsers.expat.XML_ERROR_INVALID_TOKEN
\n
Raised when an input byte could not properly be assigned to a character; for\nexample, a NUL byte (value 0) in a UTF-8 input stream.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_JUNK_AFTER_DOC_ELEMENT
\n
Something other than whitespace occurred after the document element.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_MISPLACED_XML_PI
\n
An XML declaration was found somewhere other than the start of the input data.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_NO_ELEMENTS
\n
The document contains no elements (XML requires all documents to contain exactly\none top-level element)..
\n\n
\n
\nxml.parsers.expat.XML_ERROR_NO_MEMORY
\n
Expat was not able to allocate memory internally.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_PARAM_ENTITY_REF
\n
A parameter entity reference was found where it was not allowed.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_PARTIAL_CHAR
\n
An incomplete character was found in the input.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_RECURSIVE_ENTITY_REF
\n
An entity reference contained another reference to the same entity; possibly via\na different name, and possibly indirectly.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_SYNTAX
\n
Some unspecified syntax error was encountered.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_TAG_MISMATCH
\n
An end tag did not match the innermost open start tag.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_UNCLOSED_TOKEN
\n
Some token (such as a start tag) was not closed before the end of the stream or\nthe next token was encountered.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_UNDEFINED_ENTITY
\n
A reference was made to a entity which was not defined.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_UNKNOWN_ENCODING
\n
The document encoding is not supported by Expat.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_UNCLOSED_CDATA_SECTION
\n
A CDATA marked section was not closed.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_EXTERNAL_ENTITY_HANDLING
\n
\n\n
\n
\nxml.parsers.expat.XML_ERROR_NOT_STANDALONE
\n
The parser determined that the document was not “standalone” though it declared\nitself to be in the XML declaration, and the NotStandaloneHandler was\nset and returned 0.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_UNEXPECTED_STATE
\n
\n\n
\n
\nxml.parsers.expat.XML_ERROR_ENTITY_DECLARED_IN_PE
\n
\n\n
\n
\nxml.parsers.expat.XML_ERROR_FEATURE_REQUIRES_XML_DTD
\n
An operation was requested that requires DTD support to be compiled in, but\nExpat was configured without DTD support. This should never be reported by a\nstandard build of the xml.parsers.expat module.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING
\n
A behavioral change was requested after parsing started that can only be changed\nbefore parsing has started. This is (currently) only raised by\nUseForeignDTD().
\n\n
\n
\nxml.parsers.expat.XML_ERROR_UNBOUND_PREFIX
\n
An undeclared prefix was found when namespace processing was enabled.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_UNDECLARING_PREFIX
\n
The document attempted to remove the namespace declaration associated with a\nprefix.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_INCOMPLETE_PE
\n
A parameter entity contained incomplete markup.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_XML_DECL
\n
The document contained no document element at all.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_TEXT_DECL
\n
There was an error parsing a text declaration in an external entity.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_PUBLICID
\n
Characters were found in the public id that are not allowed.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_SUSPENDED
\n
The requested operation was made on a suspended parser, but isn’t allowed. This\nincludes attempts to provide additional input or to stop the parser.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_NOT_SUSPENDED
\n
An attempt to resume the parser was made when the parser had not been suspended.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_ABORTED
\n
This should not be reported to Python applications.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_FINISHED
\n
The requested operation was made on a parser which was finished parsing input,\nbut isn’t allowed. This includes attempts to provide additional input or to\nstop the parser.
\n\n
\n
\nxml.parsers.expat.XML_ERROR_SUSPEND_PE
\n
\n\n

Footnotes

\n\n\n\n\n\n
[1]The encoding string included in XML output should conform to the\nappropriate standards. For example, “UTF-8” is valid, but “UTF8” is\nnot. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl\nand http://www.iana.org/assignments/character-sets .
\n
\n
", "searchableItems": [ { "name": "xml.parsers.expat.ErrorString", "domId": "xml.parsers.expat_xml.parsers.expat.ErrorString" }, { "name": "xml.parsers.expat.ParserCreate", "domId": "xml.parsers.expat_xml.parsers.expat.ParserCreate" }, { "name": "xml.parsers.expat.xmlparser.AttlistDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.AttlistDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.CharacterDataHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.CharacterDataHandler" }, { "name": "xml.parsers.expat.xmlparser.CommentHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.CommentHandler" }, { "name": "xml.parsers.expat.xmlparser.DefaultHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.DefaultHandler" }, { "name": "xml.parsers.expat.xmlparser.DefaultHandlerExpand", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.DefaultHandlerExpand" }, { "name": "xml.parsers.expat.xmlparser.ElementDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.ElementDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.EndCdataSectionHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.EndCdataSectionHandler" }, { "name": "xml.parsers.expat.xmlparser.EndDoctypeDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.EndDoctypeDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.EndElementHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.EndElementHandler" }, { "name": "xml.parsers.expat.xmlparser.EndNamespaceDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.EndNamespaceDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.EntityDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.EntityDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.ExternalEntityParserCreate", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.ExternalEntityParserCreate" }, { "name": "xml.parsers.expat.xmlparser.ExternalEntityRefHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.ExternalEntityRefHandler" }, { "name": "xml.parsers.expat.xmlparser.GetBase", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.GetBase" }, { "name": "xml.parsers.expat.xmlparser.GetInputContext", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.GetInputContext" }, { "name": "xml.parsers.expat.xmlparser.NotationDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.NotationDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.NotStandaloneHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.NotStandaloneHandler" }, { "name": "xml.parsers.expat.xmlparser.Parse", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.Parse" }, { "name": "xml.parsers.expat.xmlparser.ParseFile", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.ParseFile" }, { "name": "xml.parsers.expat.xmlparser.ProcessingInstructionHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.ProcessingInstructionHandler" }, { "name": "xml.parsers.expat.xmlparser.SetBase", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.SetBase" }, { "name": "xml.parsers.expat.xmlparser.SetParamEntityParsing", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.SetParamEntityParsing" }, { "name": "xml.parsers.expat.xmlparser.StartCdataSectionHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.StartCdataSectionHandler" }, { "name": "xml.parsers.expat.xmlparser.StartDoctypeDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.StartDoctypeDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.StartElementHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.StartElementHandler" }, { "name": "xml.parsers.expat.xmlparser.StartNamespaceDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.StartNamespaceDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.UnparsedEntityDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.UnparsedEntityDeclHandler" }, { "name": "xml.parsers.expat.xmlparser.UseForeignDTD", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.UseForeignDTD" }, { "name": "xml.parsers.expat.xmlparser.XmlDeclHandler", "domId": "xml.parsers.expat_xml.parsers.expat.xmlparser.XmlDeclHandler" } ] }, { "url": "http://docs.python.org/library/xml.sax.utils.html", "title": "xml.sax.saxutils", "html": "
\n

19.11. xml.sax.saxutils — SAX Utilities

\n

\nNew in version 2.0.

\n

The module xml.sax.saxutils contains a number of classes and functions\nthat are commonly useful when creating SAX applications, either in direct use,\nor as base classes.

\n
\n
\nxml.sax.saxutils.escape(data[, entities])
\n

Escape '&', '<', and '>' in a string of data.

\n

You can escape other strings of data by passing a dictionary as the optional\nentities parameter. The keys and values must all be strings; each key will be\nreplaced with its corresponding value. The characters '&', '<' and\n'>' are always escaped, even if entities is provided.

\n
\n\n
\n
\nxml.sax.saxutils.unescape(data[, entities])
\n

Unescape '&amp;', '&lt;', and '&gt;' in a string of data.

\n

You can unescape other strings of data by passing a dictionary as the optional\nentities parameter. The keys and values must all be strings; each key will be\nreplaced with its corresponding value. '&amp', '&lt;', and '&gt;'\nare always unescaped, even if entities is provided.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nxml.sax.saxutils.quoteattr(data[, entities])
\n

Similar to escape(), but also prepares data to be used as an\nattribute value. The return value is a quoted version of data with any\nadditional required replacements. quoteattr() will select a quote\ncharacter based on the content of data, attempting to avoid encoding any\nquote characters in the string. If both single- and double-quote characters\nare already in data, the double-quote characters will be encoded and data\nwill be wrapped in double-quotes. The resulting string can be used directly\nas an attribute value:

\n
>>> print "<element attr=%s>"  quoteattr("ab ' cd \\" ef")\n<element attr="ab ' cd &quot; ef">\n
\n
\n

This function is useful when generating attribute values for HTML or any SGML\nusing the reference concrete syntax.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nclass xml.sax.saxutils.XMLGenerator([out[, encoding]])
\n
This class implements the ContentHandler interface by writing SAX\nevents back into an XML document. In other words, using an XMLGenerator\nas the content handler will reproduce the original document being parsed. out\nshould be a file-like object which will default to sys.stdout. encoding is\nthe encoding of the output stream which defaults to 'iso-8859-1'.
\n\n
\n
\nclass xml.sax.saxutils.XMLFilterBase(base)
\n
This class is designed to sit between an XMLReader and the client\napplication’s event handlers. By default, it does nothing but pass requests up\nto the reader and events on to the handlers unmodified, but subclasses can\noverride specific methods to modify the event stream or the configuration\nrequests as they pass through.
\n\n
\n
\nxml.sax.saxutils.prepare_input_source(source[, base])
\n
This function takes an input source and an optional base URL and returns a fully\nresolved InputSource object ready for reading. The input source can be\ngiven as a string, a file-like object, or an InputSource object;\nparsers will use this function to implement the polymorphic source argument to\ntheir parse() method.
\n\n
", "searchableItems": [ { "name": "xml.sax.saxutils.escape", "domId": "xml.sax.saxutils_xml.sax.saxutils.escape" }, { "name": "xml.sax.saxutils.prepare_input_source", "domId": "xml.sax.saxutils_xml.sax.saxutils.prepare_input_source" }, { "name": "xml.sax.saxutils.quoteattr", "domId": "xml.sax.saxutils_xml.sax.saxutils.quoteattr" }, { "name": "xml.sax.saxutils.unescape", "domId": "xml.sax.saxutils_xml.sax.saxutils.unescape" }, { "name": "xml.sax.saxutils.XMLFilterBase", "domId": "xml.sax.saxutils_xml.sax.saxutils.XMLFilterBase" }, { "name": "xml.sax.saxutils.XMLGenerator", "domId": "xml.sax.saxutils_xml.sax.saxutils.XMLGenerator" } ] }, { "url": "http://docs.python.org/library/xml.sax.handler.html", "title": "xml.sax.handler", "html": "
\n

19.10. xml.sax.handler — Base classes for SAX handlers

\n

\nNew in version 2.0.

\n

The SAX API defines four kinds of handlers: content handlers, DTD handlers,\nerror handlers, and entity resolvers. Applications normally only need to\nimplement those interfaces whose events they are interested in; they can\nimplement the interfaces in a single object or in multiple objects. Handler\nimplementations should inherit from the base classes provided in the module\nxml.sax.handler, so that all methods get default implementations.

\n
\n
\nclass xml.sax.handler.ContentHandler
\n
This is the main callback interface in SAX, and the one most important to\napplications. The order of events in this interface mirrors the order of the\ninformation in the document.
\n\n
\n
\nclass xml.sax.handler.DTDHandler
\n

Handle DTD events.

\n

This interface specifies only those DTD events required for basic parsing\n(unparsed entities and attributes).

\n
\n\n
\n
\nclass xml.sax.handler.EntityResolver
\n
Basic interface for resolving entities. If you create an object implementing\nthis interface, then register the object with your Parser, the parser will call\nthe method in your object to resolve all external entities.
\n\n
\n
\nclass xml.sax.handler.ErrorHandler
\n
Interface used by the parser to present error and warning messages to the\napplication. The methods of this object control whether errors are immediately\nconverted to exceptions or are handled in some other way.
\n\n

In addition to these classes, xml.sax.handler provides symbolic constants\nfor the feature and property names.

\n
\n
\nxml.sax.handler.feature_namespaces
\n
\n
value: "http://xml.org/sax/features/namespaces"
\n
true: Perform Namespace processing.
\n
false: Optionally do not perform Namespace processing (implies\nnamespace-prefixes; default).
\n
access: (parsing) read-only; (not parsing) read/write
\n
\n
\n\n
\n
\nxml.sax.handler.feature_namespace_prefixes
\n
\n
value: "http://xml.org/sax/features/namespace-prefixes"
\n
true: Report the original prefixed names and attributes used for Namespace\ndeclarations.
\n
false: Do not report attributes used for Namespace declarations, and\noptionally do not report original prefixed names (default).
\n
access: (parsing) read-only; (not parsing) read/write
\n
\n
\n\n
\n
\nxml.sax.handler.feature_string_interning
\n
\n
value: "http://xml.org/sax/features/string-interning"
\n
true: All element names, prefixes, attribute names, Namespace URIs, and\nlocal names are interned using the built-in intern function.
\n
false: Names are not necessarily interned, although they may be (default).
\n
access: (parsing) read-only; (not parsing) read/write
\n
\n
\n\n
\n
\nxml.sax.handler.feature_validation
\n
\n
value: "http://xml.org/sax/features/validation"
\n
true: Report all validation errors (implies external-general-entities and\nexternal-parameter-entities).
\n
false: Do not report validation errors.
\n
access: (parsing) read-only; (not parsing) read/write
\n
\n
\n\n
\n
\nxml.sax.handler.feature_external_ges
\n
\n
value: "http://xml.org/sax/features/external-general-entities"
\n
true: Include all external general (text) entities.
\n
false: Do not include external general entities.
\n
access: (parsing) read-only; (not parsing) read/write
\n
\n
\n\n
\n
\nxml.sax.handler.feature_external_pes
\n
\n
value: "http://xml.org/sax/features/external-parameter-entities"
\n
true: Include all external parameter entities, including the external DTD\nsubset.
\n
false: Do not include any external parameter entities, even the external\nDTD subset.
\n
access: (parsing) read-only; (not parsing) read/write
\n
\n
\n\n
\n
\nxml.sax.handler.all_features
\n
List of all features.
\n\n
\n
\nxml.sax.handler.property_lexical_handler
\n
\n
value: "http://xml.org/sax/properties/lexical-handler"
\n
data type: xml.sax.sax2lib.LexicalHandler (not supported in Python 2)
\n
description: An optional extension handler for lexical events like\ncomments.
\n
access: read/write
\n
\n
\n\n
\n
\nxml.sax.handler.property_declaration_handler
\n
\n
value: "http://xml.org/sax/properties/declaration-handler"
\n
data type: xml.sax.sax2lib.DeclHandler (not supported in Python 2)
\n
description: An optional extension handler for DTD-related events other\nthan notations and unparsed entities.
\n
access: read/write
\n
\n
\n\n
\n
\nxml.sax.handler.property_dom_node
\n
\n
value: "http://xml.org/sax/properties/dom-node"
\n
data type: org.w3c.dom.Node (not supported in Python 2)
\n
description: When parsing, the current DOM node being visited if this is\na DOM iterator; when not parsing, the root DOM node for iteration.
\n
access: (parsing) read-only; (not parsing) read/write
\n
\n
\n\n
\n
\nxml.sax.handler.property_xml_string
\n
\n
value: "http://xml.org/sax/properties/xml-string"
\n
data type: String
\n
description: The literal string of characters that was the source for the\ncurrent event.
\n
access: read-only
\n
\n
\n\n
\n
\nxml.sax.handler.all_properties
\n
List of all known property names.
\n\n
\n

19.10.1. ContentHandler Objects

\n

Users are expected to subclass ContentHandler to support their\napplication. The following methods are called by the parser on the appropriate\nevents in the input document:

\n
\n
\nContentHandler.setDocumentLocator(locator)
\n

Called by the parser to give the application a locator for locating the origin\nof document events.

\n

SAX parsers are strongly encouraged (though not absolutely required) to supply a\nlocator: if it does so, it must supply the locator to the application by\ninvoking this method before invoking any of the other methods in the\nDocumentHandler interface.

\n

The locator allows the application to determine the end position of any\ndocument-related event, even if the parser is not reporting an error. Typically,\nthe application will use this information for reporting its own errors (such as\ncharacter content that does not match an application’s business rules). The\ninformation returned by the locator is probably not sufficient for use with a\nsearch engine.

\n

Note that the locator will return correct information only during the invocation\nof the events in this interface. The application should not attempt to use it at\nany other time.

\n
\n\n
\n
\nContentHandler.startDocument()
\n

Receive notification of the beginning of a document.

\n

The SAX parser will invoke this method only once, before any other methods in\nthis interface or in DTDHandler (except for setDocumentLocator()).

\n
\n\n
\n
\nContentHandler.endDocument()
\n

Receive notification of the end of a document.

\n

The SAX parser will invoke this method only once, and it will be the last method\ninvoked during the parse. The parser shall not invoke this method until it has\neither abandoned parsing (because of an unrecoverable error) or reached the end\nof input.

\n
\n\n
\n
\nContentHandler.startPrefixMapping(prefix, uri)
\n

Begin the scope of a prefix-URI Namespace mapping.

\n

The information from this event is not necessary for normal Namespace\nprocessing: the SAX XML reader will automatically replace prefixes for element\nand attribute names when the feature_namespaces feature is enabled (the\ndefault).

\n

There are cases, however, when applications need to use prefixes in character\ndata or in attribute values, where they cannot safely be expanded automatically;\nthe startPrefixMapping() and endPrefixMapping() events supply the\ninformation to the application to expand prefixes in those contexts itself, if\nnecessary.

\n

Note that startPrefixMapping() and endPrefixMapping() events are not\nguaranteed to be properly nested relative to each-other: all\nstartPrefixMapping() events will occur before the corresponding\nstartElement() event, and all endPrefixMapping() events will occur\nafter the corresponding endElement() event, but their order is not\nguaranteed.

\n
\n\n
\n
\nContentHandler.endPrefixMapping(prefix)
\n

End the scope of a prefix-URI mapping.

\n

See startPrefixMapping() for details. This event will always occur after\nthe corresponding endElement() event, but the order of\nendPrefixMapping() events is not otherwise guaranteed.

\n
\n\n
\n
\nContentHandler.startElement(name, attrs)
\n

Signals the start of an element in non-namespace mode.

\n

The name parameter contains the raw XML 1.0 name of the element type as a\nstring and the attrs parameter holds an object of the Attributes\ninterface (see The Attributes Interface) containing the attributes of\nthe element. The object passed as attrs may be re-used by the parser; holding\non to a reference to it is not a reliable way to keep a copy of the attributes.\nTo keep a copy of the attributes, use the copy() method of the attrs\nobject.

\n
\n\n
\n
\nContentHandler.endElement(name)
\n

Signals the end of an element in non-namespace mode.

\n

The name parameter contains the name of the element type, just as with the\nstartElement() event.

\n
\n\n
\n
\nContentHandler.startElementNS(name, qname, attrs)
\n

Signals the start of an element in namespace mode.

\n

The name parameter contains the name of the element type as a (uri,\nlocalname) tuple, the qname parameter contains the raw XML 1.0 name used in\nthe source document, and the attrs parameter holds an instance of the\nAttributesNS interface (see The AttributesNS Interface)\ncontaining the attributes of the element. If no namespace is associated with\nthe element, the uri component of name will be None. The object passed\nas attrs may be re-used by the parser; holding on to a reference to it is not\na reliable way to keep a copy of the attributes. To keep a copy of the\nattributes, use the copy() method of the attrs object.

\n

Parsers may set the qname parameter to None, unless the\nfeature_namespace_prefixes feature is activated.

\n
\n\n
\n
\nContentHandler.endElementNS(name, qname)
\n

Signals the end of an element in namespace mode.

\n

The name parameter contains the name of the element type, just as with the\nstartElementNS() method, likewise the qname parameter.

\n
\n\n
\n
\nContentHandler.characters(content)
\n

Receive notification of character data.

\n

The Parser will call this method to report each chunk of character data. SAX\nparsers may return all contiguous character data in a single chunk, or they may\nsplit it into several chunks; however, all of the characters in any single event\nmust come from the same external entity so that the Locator provides useful\ninformation.

\n

content may be a Unicode string or a byte string; the expat reader module\nproduces always Unicode strings.

\n
\n

Note

\n

The earlier SAX 1 interface provided by the Python XML Special Interest Group\nused a more Java-like interface for this method. Since most parsers used from\nPython did not take advantage of the older interface, the simpler signature was\nchosen to replace it. To convert old code to the new interface, use content\ninstead of slicing content with the old offset and length parameters.

\n
\n
\n\n
\n
\nContentHandler.ignorableWhitespace(whitespace)
\n

Receive notification of ignorable whitespace in element content.

\n

Validating Parsers must use this method to report each chunk of ignorable\nwhitespace (see the W3C XML 1.0 recommendation, section 2.10): non-validating\nparsers may also use this method if they are capable of parsing and using\ncontent models.

\n

SAX parsers may return all contiguous whitespace in a single chunk, or they may\nsplit it into several chunks; however, all of the characters in any single event\nmust come from the same external entity, so that the Locator provides useful\ninformation.

\n
\n\n
\n
\nContentHandler.processingInstruction(target, data)
\n

Receive notification of a processing instruction.

\n

The Parser will invoke this method once for each processing instruction found:\nnote that processing instructions may occur before or after the main document\nelement.

\n

A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a\ntext declaration (XML 1.0, section 4.3.1) using this method.

\n
\n\n
\n
\nContentHandler.skippedEntity(name)
\n

Receive notification of a skipped entity.

\n

The Parser will invoke this method once for each entity skipped. Non-validating\nprocessors may skip entities if they have not seen the declarations (because,\nfor example, the entity was declared in an external DTD subset). All processors\nmay skip external entities, depending on the values of the\nfeature_external_ges and the feature_external_pes properties.

\n
\n\n
\n
\n

19.10.2. DTDHandler Objects

\n

DTDHandler instances provide the following methods:

\n
\n
\nDTDHandler.notationDecl(name, publicId, systemId)
\n
Handle a notation declaration event.
\n\n
\n
\nDTDHandler.unparsedEntityDecl(name, publicId, systemId, ndata)
\n
Handle an unparsed entity declaration event.
\n\n
\n
\n

19.10.3. EntityResolver Objects

\n
\n
\nEntityResolver.resolveEntity(publicId, systemId)
\n
Resolve the system identifier of an entity and return either the system\nidentifier to read from as a string, or an InputSource to read from. The default\nimplementation returns systemId.
\n\n
\n
\n

19.10.4. ErrorHandler Objects

\n

Objects with this interface are used to receive error and warning information\nfrom the XMLReader. If you create an object that implements this\ninterface, then register the object with your XMLReader, the parser\nwill call the methods in your object to report all warnings and errors. There\nare three levels of errors available: warnings, (possibly) recoverable errors,\nand unrecoverable errors. All methods take a SAXParseException as the\nonly parameter. Errors and warnings may be converted to an exception by raising\nthe passed-in exception object.

\n
\n
\nErrorHandler.error(exception)
\n
Called when the parser encounters a recoverable error. If this method does not\nraise an exception, parsing may continue, but further document information\nshould not be expected by the application. Allowing the parser to continue may\nallow additional errors to be discovered in the input document.
\n\n
\n
\nErrorHandler.fatalError(exception)
\n
Called when the parser encounters an error it cannot recover from; parsing is\nexpected to terminate when this method returns.
\n\n
\n
\nErrorHandler.warning(exception)
\n
Called when the parser presents minor warning information to the application.\nParsing is expected to continue when this method returns, and document\ninformation will continue to be passed to the application. Raising an exception\nin this method will cause parsing to end.
\n\n
\n
", "searchableItems": [ { "name": "xml.sax.handler.ContentHandler", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler" }, { "name": "xml.sax.handler.ContentHandler.characters", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.characters" }, { "name": "xml.sax.handler.ContentHandler.endDocument", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.endDocument" }, { "name": "xml.sax.handler.ContentHandler.endElement", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.endElement" }, { "name": "xml.sax.handler.ContentHandler.endElementNS", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.endElementNS" }, { "name": "xml.sax.handler.ContentHandler.endPrefixMapping", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.endPrefixMapping" }, { "name": "xml.sax.handler.ContentHandler.ignorableWhitespace", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.ignorableWhitespace" }, { "name": "xml.sax.handler.ContentHandler.processingInstruction", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.processingInstruction" }, { "name": "xml.sax.handler.ContentHandler.setDocumentLocator", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.setDocumentLocator" }, { "name": "xml.sax.handler.ContentHandler.skippedEntity", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.skippedEntity" }, { "name": "xml.sax.handler.ContentHandler.startDocument", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.startDocument" }, { "name": "xml.sax.handler.ContentHandler.startElement", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.startElement" }, { "name": "xml.sax.handler.ContentHandler.startElementNS", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.startElementNS" }, { "name": "xml.sax.handler.ContentHandler.startPrefixMapping", "domId": "xml.sax.handler_xml.sax.handler.ContentHandler.startPrefixMapping" }, { "name": "xml.sax.handler.DTDHandler", "domId": "xml.sax.handler_xml.sax.handler.DTDHandler" }, { "name": "xml.sax.handler.DTDHandler.notationDecl", "domId": "xml.sax.handler_xml.sax.handler.DTDHandler.notationDecl" }, { "name": "xml.sax.handler.DTDHandler.unparsedEntityDecl", "domId": "xml.sax.handler_xml.sax.handler.DTDHandler.unparsedEntityDecl" }, { "name": "xml.sax.handler.EntityResolver", "domId": "xml.sax.handler_xml.sax.handler.EntityResolver" }, { "name": "xml.sax.handler.EntityResolver.resolveEntity", "domId": "xml.sax.handler_xml.sax.handler.EntityResolver.resolveEntity" }, { "name": "xml.sax.handler.ErrorHandler", "domId": "xml.sax.handler_xml.sax.handler.ErrorHandler" }, { "name": "xml.sax.handler.ErrorHandler.error", "domId": "xml.sax.handler_xml.sax.handler.ErrorHandler.error" }, { "name": "xml.sax.handler.ErrorHandler.fatalError", "domId": "xml.sax.handler_xml.sax.handler.ErrorHandler.fatalError" }, { "name": "xml.sax.handler.ErrorHandler.warning", "domId": "xml.sax.handler_xml.sax.handler.ErrorHandler.warning" } ] }, { "url": "http://docs.python.org/library/webbrowser.html", "title": "webbrowser", "html": "
\n

20.1. webbrowser — Convenient Web-browser controller

\n

Source code: Lib/webbrowser.py

\n
\n

The webbrowser module provides a high-level interface to allow displaying\nWeb-based documents to users. Under most circumstances, simply calling the\nopen() function from this module will do the right thing.

\n

Under Unix, graphical browsers are preferred under X11, but text-mode browsers\nwill be used if graphical browsers are not available or an X11 display isn’t\navailable. If text-mode browsers are used, the calling process will block until\nthe user exits the browser.

\n

If the environment variable BROWSER exists, it is interpreted to\noverride the platform default list of browsers, as a os.pathsep-separated\nlist of browsers to try in order. When the value of a list part contains the\nstring %s, then it is interpreted as a literal browser command line to be\nused with the argument URL substituted for %s; if the part does not contain\n%s, it is simply interpreted as the name of the browser to launch. [1]

\n

For non-Unix platforms, or when a remote browser is available on Unix, the\ncontrolling process will not wait for the user to finish with the browser, but\nallow the remote browser to maintain its own windows on the display. If remote\nbrowsers are not available on Unix, the controlling process will launch a new\nbrowser and wait.

\n

The script webbrowser can be used as a command-line interface for the\nmodule. It accepts an URL as the argument. It accepts the following optional\nparameters: -n opens the URL in a new browser window, if possible;\n-t opens the URL in a new browser page (“tab”). The options are,\nnaturally, mutually exclusive.

\n

The following exception is defined:

\n
\n
\nexception webbrowser.Error
\n
Exception raised when a browser control error occurs.
\n\n

The following functions are defined:

\n
\n
\nwebbrowser.open(url[, new=0[, autoraise=True]])
\n

Display url using the default browser. If new is 0, the url is opened\nin the same browser window if possible. If new is 1, a new browser window\nis opened if possible. If new is 2, a new browser page (“tab”) is opened\nif possible. If autoraise is True, the window is raised if possible\n(note that under many window managers this will occur regardless of the\nsetting of this variable).

\n

Note that on some platforms, trying to open a filename using this function,\nmay work and start the operating system’s associated program. However, this\nis neither supported nor portable.

\n

\nChanged in version 2.5: new can now be 2.

\n
\n\n
\n
\nwebbrowser.open_new(url)
\n
Open url in a new window of the default browser, if possible, otherwise, open\nurl in the only browser window.
\n\n
\n
\nwebbrowser.open_new_tab(url)
\n

Open url in a new page (“tab”) of the default browser, if possible, otherwise\nequivalent to open_new().

\n

\nNew in version 2.5.

\n
\n\n
\n
\nwebbrowser.get([name])
\n
Return a controller object for the browser type name. If name is empty,\nreturn a controller for a default browser appropriate to the caller’s\nenvironment.
\n\n
\n
\nwebbrowser.register(name, constructor[, instance])
\n

Register the browser type name. Once a browser type is registered, the\nget() function can return a controller for that browser type. If\ninstance is not provided, or is None, constructor will be called without\nparameters to create an instance when needed. If instance is provided,\nconstructor will never be called, and may be None.

\n

This entry point is only useful if you plan to either set the BROWSER\nvariable or call get() with a nonempty argument matching the name of a\nhandler you declare.

\n
\n\n

A number of browser types are predefined. This table gives the type names that\nmay be passed to the get() function and the corresponding instantiations\nfor the controller classes, all defined in this module.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Type NameClass NameNotes
'mozilla'Mozilla('mozilla') 
'firefox'Mozilla('mozilla') 
'netscape'Mozilla('netscape') 
'galeon'Galeon('galeon') 
'epiphany'Galeon('epiphany') 
'skipstone'BackgroundBrowser('skipstone') 
'kfmclient'Konqueror()(1)
'konqueror'Konqueror()(1)
'kfm'Konqueror()(1)
'mosaic'BackgroundBrowser('mosaic') 
'opera'Opera() 
'grail'Grail() 
'links'GenericBrowser('links') 
'elinks'Elinks('elinks') 
'lynx'GenericBrowser('lynx') 
'w3m'GenericBrowser('w3m') 
'windows-default'WindowsDefault(2)
'internet-config'InternetConfig(3)
'macosx'MacOSX('default')(4)
\n

Notes:

\n
    \n
  1. “Konqueror” is the file manager for the KDE desktop environment for Unix, and\nonly makes sense to use if KDE is running. Some way of reliably detecting KDE\nwould be nice; the KDEDIR variable is not sufficient. Note also that\nthe name “kfm” is used even when using the konqueror command with KDE\n2 — the implementation selects the best strategy for running Konqueror.
  2. \n
  3. Only on Windows platforms.
  4. \n
  5. Only on Mac OS platforms; requires the standard MacPython ic module.
  6. \n
  7. Only on Mac OS X platform.
  8. \n
\n

Here are some simple examples:

\n
url = 'http://www.python.org/'\n\n# Open URL in a new tab, if a browser window is already open.\nwebbrowser.open_new_tab(url + 'doc/')\n\n# Open URL in new window, raising the window if possible.\nwebbrowser.open_new(url)\n
\n
\n
\n

20.1.1. Browser Controller Objects

\n

Browser controllers provide these methods which parallel three of the\nmodule-level convenience functions:

\n
\n
\ncontroller.open(url[, new=0[, autoraise=True]])
\n
Display url using the browser handled by this controller. If new is 1, a new\nbrowser window is opened if possible. If new is 2, a new browser page (“tab”)\nis opened if possible.
\n\n
\n
\ncontroller.open_new(url)
\n
Open url in a new window of the browser handled by this controller, if\npossible, otherwise, open url in the only browser window. Alias\nopen_new().
\n\n
\n
\ncontroller.open_new_tab(url)
\n

Open url in a new page (“tab”) of the browser handled by this controller, if\npossible, otherwise equivalent to open_new().

\n

\nNew in version 2.5.

\n
\n\n

Footnotes

\n\n\n\n\n\n
[1]Executables named here without a full path will be searched in the\ndirectories given in the PATH environment variable.
\n
\n
", "searchableItems": [ { "name": "webbrowser.controller.open", "domId": "webbrowser_webbrowser.controller.open" }, { "name": "webbrowser.controller.open_new", "domId": "webbrowser_webbrowser.controller.open_new" }, { "name": "webbrowser.controller.open_new_tab", "domId": "webbrowser_webbrowser.controller.open_new_tab" }, { "name": "webbrowser.get", "domId": "webbrowser_webbrowser.get" }, { "name": "webbrowser.open", "domId": "webbrowser_webbrowser.open" }, { "name": "webbrowser.open_new", "domId": "webbrowser_webbrowser.open_new" }, { "name": "webbrowser.open_new_tab", "domId": "webbrowser_webbrowser.open_new_tab" }, { "name": "webbrowser.register", "domId": "webbrowser_webbrowser.register" } ] }, { "url": "http://docs.python.org/library/cgitb.html", "title": "cgitb", "html": "
\n

20.3. cgitb — Traceback manager for CGI scripts

\n

\nNew in version 2.2.

\n

The cgitb module provides a special exception handler for Python scripts.\n(Its name is a bit misleading. It was originally designed to display extensive\ntraceback information in HTML for CGI scripts. It was later generalized to also\ndisplay this information in plain text.) After this module is activated, if an\nuncaught exception occurs, a detailed, formatted report will be displayed. The\nreport includes a traceback showing excerpts of the source code for each level,\nas well as the values of the arguments and local variables to currently running\nfunctions, to help you debug the problem. Optionally, you can save this\ninformation to a file instead of sending it to the browser.

\n

To enable this feature, simply add this to the top of your CGI script:

\n
import cgitb\ncgitb.enable()\n
\n
\n

The options to the enable() function control whether the report is\ndisplayed in the browser and whether the report is logged to a file for later\nanalysis.

\n
\n
\ncgitb.enable([display[, logdir[, context[, format]]]])
\n

This function causes the cgitb module to take over the interpreter’s\ndefault handling for exceptions by setting the value of sys.excepthook.

\n

The optional argument display defaults to 1 and can be set to 0 to\nsuppress sending the traceback to the browser. If the argument logdir is\npresent, the traceback reports are written to files. The value of logdir\nshould be a directory where these files will be placed. The optional argument\ncontext is the number of lines of context to display around the current line\nof source code in the traceback; this defaults to 5. If the optional\nargument format is "html", the output is formatted as HTML. Any other\nvalue forces plain text output. The default value is "html".

\n
\n\n
\n
\ncgitb.handler([info])
\n
This function handles an exception using the default settings (that is, show a\nreport in the browser, but don’t log to a file). This can be used when you’ve\ncaught an exception and want to report it using cgitb. The optional\ninfo argument should be a 3-tuple containing an exception type, exception\nvalue, and traceback object, exactly like the tuple returned by\nsys.exc_info(). If the info argument is not supplied, the current\nexception is obtained from sys.exc_info().
\n\n
", "searchableItems": [ { "name": "cgitb.enable", "domId": "cgitb_cgitb.enable" }, { "name": "cgitb.handler", "domId": "cgitb_cgitb.handler" } ] }, { "url": "http://docs.python.org/library/xml.sax.reader.html", "title": "xml.sax.xmlreader", "html": "
\n

19.12. xml.sax.xmlreader — Interface for XML parsers

\n

\nNew in version 2.0.

\n

SAX parsers implement the XMLReader interface. They are implemented in\na Python module, which must provide a function create_parser(). This\nfunction is invoked by xml.sax.make_parser() with no arguments to create\na new parser object.

\n
\n
\nclass xml.sax.xmlreader.XMLReader
\n
Base class which can be inherited by SAX parsers.
\n\n
\n
\nclass xml.sax.xmlreader.IncrementalParser
\n

In some cases, it is desirable not to parse an input source at once, but to feed\nchunks of the document as they get available. Note that the reader will normally\nnot read the entire file, but read it in chunks as well; still parse()\nwon’t return until the entire document is processed. So these interfaces should\nbe used if the blocking behaviour of parse() is not desirable.

\n

When the parser is instantiated it is ready to begin accepting data from the\nfeed method immediately. After parsing has been finished with a call to close\nthe reset method must be called to make the parser ready to accept new data,\neither from feed or using the parse method.

\n

Note that these methods must not be called during parsing, that is, after\nparse has been called and before it returns.

\n

By default, the class also implements the parse method of the XMLReader\ninterface using the feed, close and reset methods of the IncrementalParser\ninterface as a convenience to SAX 2.0 driver writers.

\n
\n\n
\n
\nclass xml.sax.xmlreader.Locator
\n
Interface for associating a SAX event with a document location. A locator object\nwill return valid results only during calls to DocumentHandler methods; at any\nother time, the results are unpredictable. If information is not available,\nmethods may return None.
\n\n
\n
\nclass xml.sax.xmlreader.InputSource([systemId])
\n

Encapsulation of the information needed by the XMLReader to read\nentities.

\n

This class may include information about the public identifier, system\nidentifier, byte stream (possibly with character encoding information) and/or\nthe character stream of an entity.

\n

Applications will create objects of this class for use in the\nXMLReader.parse() method and for returning from\nEntityResolver.resolveEntity.

\n

An InputSource belongs to the application, the XMLReader is\nnot allowed to modify InputSource objects passed to it from the\napplication, although it may make copies and modify those.

\n
\n\n
\n
\nclass xml.sax.xmlreader.AttributesImpl(attrs)
\n
This is an implementation of the Attributes interface (see section\nThe Attributes Interface). This is a dictionary-like object which\nrepresents the element attributes in a startElement() call. In addition\nto the most useful dictionary operations, it supports a number of other\nmethods as described by the interface. Objects of this class should be\ninstantiated by readers; attrs must be a dictionary-like object containing\na mapping from attribute names to attribute values.
\n\n
\n
\nclass xml.sax.xmlreader.AttributesNSImpl(attrs, qnames)
\n
Namespace-aware variant of AttributesImpl, which will be passed to\nstartElementNS(). It is derived from AttributesImpl, but\nunderstands attribute names as two-tuples of namespaceURI and\nlocalname. In addition, it provides a number of methods expecting qualified\nnames as they appear in the original document. This class implements the\nAttributesNS interface (see section The AttributesNS Interface).
\n\n
\n

19.12.1. XMLReader Objects

\n

The XMLReader interface supports the following methods:

\n
\n
\nXMLReader.parse(source)
\n
Process an input source, producing SAX events. The source object can be a\nsystem identifier (a string identifying the input source – typically a file\nname or an URL), a file-like object, or an InputSource object. When\nparse() returns, the input is completely processed, and the parser object\ncan be discarded or reset. As a limitation, the current implementation only\naccepts byte streams; processing of character streams is for further study.
\n\n
\n
\nXMLReader.getContentHandler()
\n
Return the current ContentHandler.
\n\n
\n
\nXMLReader.setContentHandler(handler)
\n
Set the current ContentHandler. If no ContentHandler is set,\ncontent events will be discarded.
\n\n
\n
\nXMLReader.getDTDHandler()
\n
Return the current DTDHandler.
\n\n
\n
\nXMLReader.setDTDHandler(handler)
\n
Set the current DTDHandler. If no DTDHandler is set, DTD\nevents will be discarded.
\n\n
\n
\nXMLReader.getEntityResolver()
\n
Return the current EntityResolver.
\n\n
\n
\nXMLReader.setEntityResolver(handler)
\n
Set the current EntityResolver. If no EntityResolver is set,\nattempts to resolve an external entity will result in opening the system\nidentifier for the entity, and fail if it is not available.
\n\n
\n
\nXMLReader.getErrorHandler()
\n
Return the current ErrorHandler.
\n\n
\n
\nXMLReader.setErrorHandler(handler)
\n
Set the current error handler. If no ErrorHandler is set, errors will\nbe raised as exceptions, and warnings will be printed.
\n\n
\n
\nXMLReader.setLocale(locale)
\n

Allow an application to set the locale for errors and warnings.

\n

SAX parsers are not required to provide localization for errors and warnings; if\nthey cannot support the requested locale, however, they must raise a SAX\nexception. Applications may request a locale change in the middle of a parse.

\n
\n\n
\n
\nXMLReader.getFeature(featurename)
\n
Return the current setting for feature featurename. If the feature is not\nrecognized, SAXNotRecognizedException is raised. The well-known\nfeaturenames are listed in the module xml.sax.handler.
\n\n
\n
\nXMLReader.setFeature(featurename, value)
\n
Set the featurename to value. If the feature is not recognized,\nSAXNotRecognizedException is raised. If the feature or its setting is not\nsupported by the parser, SAXNotSupportedException is raised.
\n\n
\n
\nXMLReader.getProperty(propertyname)
\n
Return the current setting for property propertyname. If the property is not\nrecognized, a SAXNotRecognizedException is raised. The well-known\npropertynames are listed in the module xml.sax.handler.
\n\n
\n
\nXMLReader.setProperty(propertyname, value)
\n
Set the propertyname to value. If the property is not recognized,\nSAXNotRecognizedException is raised. If the property or its setting is\nnot supported by the parser, SAXNotSupportedException is raised.
\n\n
\n
\n

19.12.2. IncrementalParser Objects

\n

Instances of IncrementalParser offer the following additional methods:

\n
\n
\nIncrementalParser.feed(data)
\n
Process a chunk of data.
\n\n
\n
\nIncrementalParser.close()
\n
Assume the end of the document. That will check well-formedness conditions that\ncan be checked only at the end, invoke handlers, and may clean up resources\nallocated during parsing.
\n\n
\n
\nIncrementalParser.reset()
\n
This method is called after close has been called to reset the parser so that it\nis ready to parse new documents. The results of calling parse or feed after\nclose without calling reset are undefined.
\n\n
\n
\n

19.12.3. Locator Objects

\n

Instances of Locator provide these methods:

\n
\n
\nLocator.getColumnNumber()
\n
Return the column number where the current event ends.
\n\n
\n
\nLocator.getLineNumber()
\n
Return the line number where the current event ends.
\n\n
\n
\nLocator.getPublicId()
\n
Return the public identifier for the current event.
\n\n
\n
\nLocator.getSystemId()
\n
Return the system identifier for the current event.
\n\n
\n
\n

19.12.4. InputSource Objects

\n
\n
\nInputSource.setPublicId(id)
\n
Sets the public identifier of this InputSource.
\n\n
\n
\nInputSource.getPublicId()
\n
Returns the public identifier of this InputSource.
\n\n
\n
\nInputSource.setSystemId(id)
\n
Sets the system identifier of this InputSource.
\n\n
\n
\nInputSource.getSystemId()
\n
Returns the system identifier of this InputSource.
\n\n
\n
\nInputSource.setEncoding(encoding)
\n

Sets the character encoding of this InputSource.

\n

The encoding must be a string acceptable for an XML encoding declaration (see\nsection 4.3.3 of the XML recommendation).

\n

The encoding attribute of the InputSource is ignored if the\nInputSource also contains a character stream.

\n
\n\n
\n
\nInputSource.getEncoding()
\n
Get the character encoding of this InputSource.
\n\n
\n
\nInputSource.setByteStream(bytefile)
\n

Set the byte stream (a Python file-like object which does not perform\nbyte-to-character conversion) for this input source.

\n

The SAX parser will ignore this if there is also a character stream specified,\nbut it will use a byte stream in preference to opening a URI connection itself.

\n

If the application knows the character encoding of the byte stream, it should\nset it with the setEncoding method.

\n
\n\n
\n
\nInputSource.getByteStream()
\n

Get the byte stream for this input source.

\n

The getEncoding method will return the character encoding for this byte stream,\nor None if unknown.

\n
\n\n
\n
\nInputSource.setCharacterStream(charfile)
\n

Set the character stream for this input source. (The stream must be a Python 1.6\nUnicode-wrapped file-like that performs conversion to Unicode strings.)

\n

If there is a character stream specified, the SAX parser will ignore any byte\nstream and will not attempt to open a URI connection to the system identifier.

\n
\n\n
\n
\nInputSource.getCharacterStream()
\n
Get the character stream for this input source.
\n\n
\n
\n

19.12.5. The Attributes Interface

\n

Attributes objects implement a portion of the mapping protocol,\nincluding the methods copy(), get(), has_key(), items(),\nkeys(), and values(). The following methods are also provided:

\n
\n
\nAttributes.getLength()
\n
Return the number of attributes.
\n\n
\n
\nAttributes.getNames()
\n
Return the names of the attributes.
\n\n
\n
\nAttributes.getType(name)
\n
Returns the type of the attribute name, which is normally 'CDATA'.
\n\n
\n
\nAttributes.getValue(name)
\n
Return the value of attribute name.
\n\n
\n
\n

19.12.6. The AttributesNS Interface

\n

This interface is a subtype of the Attributes interface (see section\nThe Attributes Interface). All methods supported by that interface are also\navailable on AttributesNS objects.

\n

The following methods are also available:

\n
\n
\nAttributesNS.getValueByQName(name)
\n
Return the value for a qualified name.
\n\n
\n
\nAttributesNS.getNameByQName(name)
\n
Return the (namespace, localname) pair for a qualified name.
\n\n
\n
\nAttributesNS.getQNameByName(name)
\n
Return the qualified name for a (namespace, localname) pair.
\n\n
\n
\nAttributesNS.getQNames()
\n
Return the qualified names of all attributes.
\n\n
\n
", "searchableItems": [ { "name": "xml.sax.xmlreader.Attributes.getLength", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Attributes.getLength" }, { "name": "xml.sax.xmlreader.Attributes.getNames", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Attributes.getNames" }, { "name": "xml.sax.xmlreader.Attributes.getType", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Attributes.getType" }, { "name": "xml.sax.xmlreader.Attributes.getValue", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Attributes.getValue" }, { "name": "xml.sax.xmlreader.AttributesImpl", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.AttributesImpl" }, { "name": "xml.sax.xmlreader.AttributesNS.getNameByQName", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.AttributesNS.getNameByQName" }, { "name": "xml.sax.xmlreader.AttributesNS.getQNameByName", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.AttributesNS.getQNameByName" }, { "name": "xml.sax.xmlreader.AttributesNS.getQNames", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.AttributesNS.getQNames" }, { "name": "xml.sax.xmlreader.AttributesNS.getValueByQName", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.AttributesNS.getValueByQName" }, { "name": "xml.sax.xmlreader.AttributesNSImpl", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.AttributesNSImpl" }, { "name": "xml.sax.xmlreader.IncrementalParser", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.IncrementalParser" }, { "name": "xml.sax.xmlreader.IncrementalParser.close", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.IncrementalParser.close" }, { "name": "xml.sax.xmlreader.IncrementalParser.feed", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.IncrementalParser.feed" }, { "name": "xml.sax.xmlreader.IncrementalParser.reset", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.IncrementalParser.reset" }, { "name": "xml.sax.xmlreader.InputSource", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource" }, { "name": "xml.sax.xmlreader.InputSource.getByteStream", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.getByteStream" }, { "name": "xml.sax.xmlreader.InputSource.getCharacterStream", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.getCharacterStream" }, { "name": "xml.sax.xmlreader.InputSource.getEncoding", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.getEncoding" }, { "name": "xml.sax.xmlreader.InputSource.getPublicId", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.getPublicId" }, { "name": "xml.sax.xmlreader.InputSource.getSystemId", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.getSystemId" }, { "name": "xml.sax.xmlreader.InputSource.setByteStream", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.setByteStream" }, { "name": "xml.sax.xmlreader.InputSource.setCharacterStream", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.setCharacterStream" }, { "name": "xml.sax.xmlreader.InputSource.setEncoding", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.setEncoding" }, { "name": "xml.sax.xmlreader.InputSource.setPublicId", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.setPublicId" }, { "name": "xml.sax.xmlreader.InputSource.setSystemId", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.InputSource.setSystemId" }, { "name": "xml.sax.xmlreader.Locator", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Locator" }, { "name": "xml.sax.xmlreader.Locator.getColumnNumber", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Locator.getColumnNumber" }, { "name": "xml.sax.xmlreader.Locator.getLineNumber", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Locator.getLineNumber" }, { "name": "xml.sax.xmlreader.Locator.getPublicId", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Locator.getPublicId" }, { "name": "xml.sax.xmlreader.Locator.getSystemId", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.Locator.getSystemId" }, { "name": "xml.sax.xmlreader.XMLReader", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader" }, { "name": "xml.sax.xmlreader.XMLReader.getContentHandler", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.getContentHandler" }, { "name": "xml.sax.xmlreader.XMLReader.getDTDHandler", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.getDTDHandler" }, { "name": "xml.sax.xmlreader.XMLReader.getEntityResolver", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.getEntityResolver" }, { "name": "xml.sax.xmlreader.XMLReader.getErrorHandler", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.getErrorHandler" }, { "name": "xml.sax.xmlreader.XMLReader.getFeature", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.getFeature" }, { "name": "xml.sax.xmlreader.XMLReader.getProperty", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.getProperty" }, { "name": "xml.sax.xmlreader.XMLReader.parse", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.parse" }, { "name": "xml.sax.xmlreader.XMLReader.setContentHandler", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.setContentHandler" }, { "name": "xml.sax.xmlreader.XMLReader.setDTDHandler", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.setDTDHandler" }, { "name": "xml.sax.xmlreader.XMLReader.setEntityResolver", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.setEntityResolver" }, { "name": "xml.sax.xmlreader.XMLReader.setErrorHandler", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.setErrorHandler" }, { "name": "xml.sax.xmlreader.XMLReader.setFeature", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.setFeature" }, { "name": "xml.sax.xmlreader.XMLReader.setLocale", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.setLocale" }, { "name": "xml.sax.xmlreader.XMLReader.setProperty", "domId": "xml.sax.xmlreader_xml.sax.xmlreader.XMLReader.setProperty" } ] }, { "url": "http://docs.python.org/library/xml.etree.elementtree.html", "title": "xml.etree.ElementTree", "html": "
\n

19.13. xml.etree.ElementTree — The ElementTree XML API

\n

\nNew in version 2.5.

\n

Source code: Lib/xml/etree/ElementTree.py

\n
\n

The Element type is a flexible container object, designed to store\nhierarchical data structures in memory. The type can be described as a cross\nbetween a list and a dictionary.

\n

Each element has a number of properties associated with it:

\n\n

To create an element instance, use the Element constructor or the\nSubElement() factory function.

\n

The ElementTree class can be used to wrap an element structure, and\nconvert it from and to XML.

\n

A C implementation of this API is available as xml.etree.cElementTree.

\n

See http://effbot.org/zone/element-index.htm for tutorials and links to other\ndocs. Fredrik Lundh’s page is also the location of the development version of\nthe xml.etree.ElementTree.

\n

\nChanged in version 2.7: The ElementTree API is updated to 1.3. For more information, see\nIntroducing ElementTree 1.3.

\n
\n

19.13.1. Functions

\n
\n
\nxml.etree.ElementTree.Comment(text=None)
\n
Comment element factory. This factory function creates a special element\nthat will be serialized as an XML comment by the standard serializer. The\ncomment string can be either a bytestring or a Unicode string. text is a\nstring containing the comment string. Returns an element instance\nrepresenting a comment.
\n\n
\n
\nxml.etree.ElementTree.dump(elem)
\n

Writes an element tree or element structure to sys.stdout. This function\nshould be used for debugging only.

\n

The exact output format is implementation dependent. In this version, it’s\nwritten as an ordinary XML file.

\n

elem is an element tree or an individual element.

\n
\n\n
\n
\nxml.etree.ElementTree.fromstring(text)
\n
Parses an XML section from a string constant. Same as XML(). text\nis a string containing XML data. Returns an Element instance.
\n\n
\n
\nxml.etree.ElementTree.fromstringlist(sequence, parser=None)
\n

Parses an XML document from a sequence of string fragments. sequence is a\nlist or other sequence containing XML data fragments. parser is an\noptional parser instance. If not given, the standard XMLParser\nparser is used. Returns an Element instance.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nxml.etree.ElementTree.iselement(element)
\n
Checks if an object appears to be a valid element object. element is an\nelement instance. Returns a true value if this is an element object.
\n\n
\n
\nxml.etree.ElementTree.iterparse(source, events=None, parser=None)
\n

Parses an XML section into an element tree incrementally, and reports what’s\ngoing on to the user. source is a filename or file object containing XML\ndata. events is a list of events to report back. If omitted, only “end”\nevents are reported. parser is an optional parser instance. If not\ngiven, the standard XMLParser parser is used. Returns an\niterator providing (event, elem) pairs.

\n
\n

Note

\n

iterparse() only guarantees that it has seen the “>”\ncharacter of a starting tag when it emits a “start” event, so the\nattributes are defined, but the contents of the text and tail attributes\nare undefined at that point. The same applies to the element children;\nthey may or may not be present.

\n

If you need a fully populated element, look for “end” events instead.

\n
\n
\n\n
\n
\nxml.etree.ElementTree.parse(source, parser=None)
\n
Parses an XML section into an element tree. source is a filename or file\nobject containing XML data. parser is an optional parser instance. If\nnot given, the standard XMLParser parser is used. Returns an\nElementTree instance.
\n\n
\n
\nxml.etree.ElementTree.ProcessingInstruction(target, text=None)
\n
PI element factory. This factory function creates a special element that\nwill be serialized as an XML processing instruction. target is a string\ncontaining the PI target. text is a string containing the PI contents, if\ngiven. Returns an element instance, representing a processing instruction.
\n\n
\n
\nxml.etree.ElementTree.register_namespace(prefix, uri)
\n

Registers a namespace prefix. The registry is global, and any existing\nmapping for either the given prefix or the namespace URI will be removed.\nprefix is a namespace prefix. uri is a namespace uri. Tags and\nattributes in this namespace will be serialized with the given prefix, if at\nall possible.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nxml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra)
\n

Subelement factory. This function creates an element instance, and appends\nit to an existing element.

\n

The element name, attribute names, and attribute values can be either\nbytestrings or Unicode strings. parent is the parent element. tag is\nthe subelement name. attrib is an optional dictionary, containing element\nattributes. extra contains additional attributes, given as keyword\narguments. Returns an element instance.

\n
\n\n
\n
\nxml.etree.ElementTree.tostring(element, encoding="us-ascii", method="xml")
\n
Generates a string representation of an XML element, including all\nsubelements. element is an Element instance. encoding [1] is\nthe output encoding (default is US-ASCII). method is either "xml",\n"html" or "text" (default is "xml"). Returns an encoded string\ncontaining the XML data.
\n\n
\n
\nxml.etree.ElementTree.tostringlist(element, encoding="us-ascii", method="xml")
\n

Generates a string representation of an XML element, including all\nsubelements. element is an Element instance. encoding [1] is\nthe output encoding (default is US-ASCII). method is either "xml",\n"html" or "text" (default is "xml"). Returns a list of encoded\nstrings containing the XML data. It does not guarantee any specific\nsequence, except that "".join(tostringlist(element)) ==\ntostring(element).

\n

\nNew in version 2.7.

\n
\n\n
\n
\nxml.etree.ElementTree.XML(text, parser=None)
\n
Parses an XML section from a string constant. This function can be used to\nembed “XML literals” in Python code. text is a string containing XML\ndata. parser is an optional parser instance. If not given, the standard\nXMLParser parser is used. Returns an Element instance.
\n\n
\n
\nxml.etree.ElementTree.XMLID(text, parser=None)
\n
Parses an XML section from a string constant, and also returns a dictionary\nwhich maps from element id:s to elements. text is a string containing XML\ndata. parser is an optional parser instance. If not given, the standard\nXMLParser parser is used. Returns a tuple containing an\nElement instance and a dictionary.
\n\n
\n
\n

19.13.2. Element Objects

\n
\n
\nclass xml.etree.ElementTree.Element(tag, attrib={}, **extra)
\n

Element class. This class defines the Element interface, and provides a\nreference implementation of this interface.

\n

The element name, attribute names, and attribute values can be either\nbytestrings or Unicode strings. tag is the element name. attrib is\nan optional dictionary, containing element attributes. extra contains\nadditional attributes, given as keyword arguments.

\n
\n
\ntag
\n
A string identifying what kind of data this element represents (the\nelement type, in other words).
\n\n
\n
\ntext
\n
The text attribute can be used to hold additional data associated with\nthe element. As the name implies this attribute is usually a string but\nmay be any application-specific object. If the element is created from\nan XML file the attribute will contain any text found between the element\ntags.
\n\n
\n
\ntail
\n
The tail attribute can be used to hold additional data associated with\nthe element. This attribute is usually a string but may be any\napplication-specific object. If the element is created from an XML file\nthe attribute will contain any text found after the element’s end tag and\nbefore the next tag.
\n\n
\n
\nattrib
\n
A dictionary containing the element’s attributes. Note that while the\nattrib value is always a real mutable Python dictionary, an ElementTree\nimplementation may choose to use another internal representation, and\ncreate the dictionary only if someone asks for it. To take advantage of\nsuch implementations, use the dictionary methods below whenever possible.
\n\n

The following dictionary-like methods work on the element attributes.

\n
\n
\nclear()
\n
Resets an element. This function removes all subelements, clears all\nattributes, and sets the text and tail attributes to None.
\n\n
\n
\nget(key, default=None)
\n

Gets the element attribute named key.

\n

Returns the attribute value, or default if the attribute was not found.

\n
\n\n
\n
\nitems()
\n
Returns the element attributes as a sequence of (name, value) pairs. The\nattributes are returned in an arbitrary order.
\n\n
\n
\nkeys()
\n
Returns the elements attribute names as a list. The names are returned\nin an arbitrary order.
\n\n
\n
\nset(key, value)
\n
Set the attribute key on the element to value.
\n\n

The following methods work on the element’s children (subelements).

\n
\n
\nappend(subelement)
\n
Adds the element subelement to the end of this elements internal list\nof subelements.
\n\n
\n
\nextend(subelements)
\n

Appends subelements from a sequence object with zero or more elements.\nRaises AssertionError if a subelement is not a valid object.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nfind(match)
\n
Finds the first subelement matching match. match may be a tag name\nor path. Returns an element instance or None.
\n\n
\n
\nfindall(match)
\n
Finds all matching subelements, by tag name or path. Returns a list\ncontaining all matching elements in document order.
\n\n
\n
\nfindtext(match, default=None)
\n
Finds text for the first subelement matching match. match may be\na tag name or path. Returns the text content of the first matching\nelement, or default if no element was found. Note that if the matching\nelement has no text content an empty string is returned.
\n\n
\n
\ngetchildren()
\n

\nDeprecated since version 2.7: Use list(elem) or iteration.

\n
\n\n
\n
\ngetiterator(tag=None)
\n

\nDeprecated since version 2.7: Use method Element.iter() instead.

\n
\n\n
\n
\ninsert(index, element)
\n
Inserts a subelement at the given position in this element.
\n\n
\n
\niter(tag=None)
\n

Creates a tree iterator with the current element as the root.\nThe iterator iterates over this element and all elements below it, in\ndocument (depth first) order. If tag is not None or '*', only\nelements whose tag equals tag are returned from the iterator. If the\ntree structure is modified during iteration, the result is undefined.

\n

\nNew in version 2.7.

\n
\n\n
\n
\niterfind(match)
\n

Finds all matching subelements, by tag name or path. Returns an iterable\nyielding all matching elements in document order.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nitertext()
\n

Creates a text iterator. The iterator loops over this element and all\nsubelements, in document order, and returns all inner text.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nmakeelement(tag, attrib)
\n
Creates a new element object of the same type as this element. Do not\ncall this method, use the SubElement() factory function instead.
\n\n
\n
\nremove(subelement)
\n
Removes subelement from the element. Unlike the find* methods this\nmethod compares elements based on the instance identity, not on tag value\nor contents.
\n\n

Element objects also support the following sequence type methods\nfor working with subelements: __delitem__(), __getitem__(),\n__setitem__(), __len__().

\n

Caution: Elements with no subelements will test as False. This behavior\nwill change in future versions. Use specific len(elem) or elem is\nNone test instead.

\n
element = root.find('foo')\n\nif not element:  # careful!\n    print "element not found, or element has no subelements"\n\nif element is None:\n    print "element not found"\n
\n
\n
\n\n
\n
\n

19.13.3. ElementTree Objects

\n
\n
\nclass xml.etree.ElementTree.ElementTree(element=None, file=None)
\n

ElementTree wrapper class. This class represents an entire element\nhierarchy, and adds some extra support for serialization to and from\nstandard XML.

\n

element is the root element. The tree is initialized with the contents\nof the XML file if given.

\n
\n
\n_setroot(element)
\n
Replaces the root element for this tree. This discards the current\ncontents of the tree, and replaces it with the given element. Use with\ncare. element is an element instance.
\n\n
\n
\nfind(match)
\n
Finds the first toplevel element matching match. match may be a tag\nname or path. Same as getroot().find(match). Returns the first matching\nelement, or None if no element was found.
\n\n
\n
\nfindall(match)
\n
Finds all matching subelements, by tag name or path. Same as\ngetroot().findall(match). match may be a tag name or path. Returns a\nlist containing all matching elements, in document order.
\n\n
\n
\nfindtext(match, default=None)
\n
Finds the element text for the first toplevel element with given tag.\nSame as getroot().findtext(match). match may be a tag name or path.\ndefault is the value to return if the element was not found. Returns\nthe text content of the first matching element, or the default value no\nelement was found. Note that if the element is found, but has no text\ncontent, this method returns an empty string.
\n\n
\n
\ngetiterator(tag=None)
\n

\nDeprecated since version 2.7: Use method ElementTree.iter() instead.

\n
\n\n
\n
\ngetroot()
\n
Returns the root element for this tree.
\n\n
\n
\niter(tag=None)
\n
Creates and returns a tree iterator for the root element. The iterator\nloops over all elements in this tree, in section order. tag is the tag\nto look for (default is to return all elements)
\n\n
\n
\niterfind(match)
\n

Finds all matching subelements, by tag name or path. Same as\ngetroot().iterfind(match). Returns an iterable yielding all matching\nelements in document order.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nparse(source, parser=None)
\n
Loads an external XML section into this element tree. source is a file\nname or file object. parser is an optional parser instance. If not\ngiven, the standard XMLParser parser is used. Returns the section\nroot element.
\n\n
\n
\nwrite(file, encoding="us-ascii", xml_declaration=None, method="xml")
\n
Writes the element tree to a file, as XML. file is a file name, or a\nfile object opened for writing. encoding [1] is the output encoding\n(default is US-ASCII). xml_declaration controls if an XML declaration\nshould be added to the file. Use False for never, True for always, None\nfor only if not US-ASCII or UTF-8 (default is None). method is either\n"xml", "html" or "text" (default is "xml"). Returns an\nencoded string.
\n\n
\n\n

This is the XML file that is going to be manipulated:

\n
<html>\n    <head>\n        <title>Example page</title>\n    </head>\n    <body>\n        <p>Moved to <a href=\"http://example.org/\">example.org</a>\n        or <a href=\"http://example.com/\">example.com</a>.</p>\n    </body>\n</html>
\n
\n

Example of changing the attribute “target” of every link in first paragraph:

\n
>>> from xml.etree.ElementTree import ElementTree\n>>> tree = ElementTree()\n>>> tree.parse("index.xhtml")\n<Element 'html' at 0xb77e6fac>\n>>> p = tree.find("body/p")     # Finds first occurrence of tag p in body\n>>> p\n<Element 'p' at 0xb77ec26c>\n>>> links = list(p.iter("a"))   # Returns list of all links\n>>> links\n[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]\n>>> for i in links:             # Iterates through all found links\n...     i.attrib["target"] = "blank"\n>>> tree.write("output.xhtml")\n
\n
\n
\n
\n

19.13.4. QName Objects

\n
\n
\nclass xml.etree.ElementTree.QName(text_or_uri, tag=None)
\n
QName wrapper. This can be used to wrap a QName attribute value, in order\nto get proper namespace handling on output. text_or_uri is a string\ncontaining the QName value, in the form {uri}local, or, if the tag argument\nis given, the URI part of a QName. If tag is given, the first argument is\ninterpreted as an URI, and this argument is interpreted as a local name.\nQName instances are opaque.
\n\n
\n
\n

19.13.5. TreeBuilder Objects

\n
\n
\nclass xml.etree.ElementTree.TreeBuilder(element_factory=None)
\n

Generic element structure builder. This builder converts a sequence of\nstart, data, and end method calls to a well-formed element structure. You\ncan use this class to build an element structure using a custom XML parser,\nor a parser for some other XML-like format. The element_factory is called\nto create new Element instances when given.

\n
\n
\nclose()
\n
Flushes the builder buffers, and returns the toplevel document\nelement. Returns an Element instance.
\n\n
\n
\ndata(data)
\n
Adds text to the current element. data is a string. This should be\neither a bytestring, or a Unicode string.
\n\n
\n
\nend(tag)
\n
Closes the current element. tag is the element name. Returns the\nclosed element.
\n\n
\n
\nstart(tag, attrs)
\n
Opens a new element. tag is the element name. attrs is a dictionary\ncontaining element attributes. Returns the opened element.
\n\n

In addition, a custom TreeBuilder object can provide the\nfollowing method:

\n
\n
\ndoctype(name, pubid, system)
\n

Handles a doctype declaration. name is the doctype name. pubid is\nthe public identifier. system is the system identifier. This method\ndoes not exist on the default TreeBuilder class.

\n

\nNew in version 2.7.

\n
\n\n
\n\n
\n
\n

19.13.6. XMLParser Objects

\n
\n
\nclass xml.etree.ElementTree.XMLParser(html=0, target=None, encoding=None)
\n

Element structure builder for XML source data, based on the expat\nparser. html are predefined HTML entities. This flag is not supported by\nthe current implementation. target is the target object. If omitted, the\nbuilder uses an instance of the standard TreeBuilder class. encoding [1]\nis optional. If given, the value overrides the encoding specified in the\nXML file.

\n
\n
\nclose()
\n
Finishes feeding data to the parser. Returns an element structure.
\n\n
\n
\ndoctype(name, pubid, system)
\n

\nDeprecated since version 2.7: Define the TreeBuilder.doctype() method on a custom TreeBuilder\ntarget.

\n
\n\n
\n
\nfeed(data)
\n
Feeds data to the parser. data is encoded data.
\n\n
\n\n

XMLParser.feed() calls target‘s start() method\nfor each opening tag, its end() method for each closing tag,\nand data is processed by method data(). XMLParser.close()\ncalls target‘s method close().\nXMLParser can be used not only for building a tree structure.\nThis is an example of counting the maximum depth of an XML file:

\n
>>> from xml.etree.ElementTree import XMLParser\n>>> class MaxDepth:                     # The target object of the parser\n...     maxDepth = 0\n...     depth = 0\n...     def start(self, tag, attrib):   # Called for each opening tag.\n...         self.depth += 1\n...         if self.depth > self.maxDepth:\n...             self.maxDepth = self.depth\n...     def end(self, tag):             # Called for each closing tag.\n...         self.depth -= 1\n...     def data(self, data):\n...         pass            # We do not need to do anything with data.\n...     def close(self):    # Called when all data has been parsed.\n...         return self.maxDepth\n...\n>>> target = MaxDepth()\n>>> parser = XMLParser(target=target)\n>>> exampleXml = """\n... <a>\n...   <b>\n...   </b>\n...   <b>\n...     <c>\n...       <d>\n...       </d>\n...     </c>\n...   </b>\n... </a>"""\n>>> parser.feed(exampleXml)\n>>> parser.close()\n4\n
\n
\n

Footnotes

\n\n\n\n\n\n
[1]The encoding string included in XML output should conform to the\nappropriate standards. For example, “UTF-8” is valid, but “UTF8” is\nnot. See http://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl\nand http://www.iana.org/assignments/character-sets.
\n
\n
", "searchableItems": [ { "name": "xml.etree.ElementTree.Comment", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Comment" }, { "name": "xml.etree.ElementTree.dump", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.dump" }, { "name": "xml.etree.ElementTree.Element", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element" }, { "name": "xml.etree.ElementTree.Element.append", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.append" }, { "name": "xml.etree.ElementTree.Element.clear", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.clear" }, { "name": "xml.etree.ElementTree.Element.extend", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.extend" }, { "name": "xml.etree.ElementTree.Element.find", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.find" }, { "name": "xml.etree.ElementTree.Element.findall", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.findall" }, { "name": "xml.etree.ElementTree.Element.findtext", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.findtext" }, { "name": "xml.etree.ElementTree.Element.get", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.get" }, { "name": "xml.etree.ElementTree.Element.getchildren", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.getchildren" }, { "name": "xml.etree.ElementTree.Element.getiterator", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.getiterator" }, { "name": "xml.etree.ElementTree.Element.insert", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.insert" }, { "name": "xml.etree.ElementTree.Element.items", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.items" }, { "name": "xml.etree.ElementTree.Element.iter", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.iter" }, { "name": "xml.etree.ElementTree.Element.iterfind", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.iterfind" }, { "name": "xml.etree.ElementTree.Element.itertext", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.itertext" }, { "name": "xml.etree.ElementTree.Element.keys", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.keys" }, { "name": "xml.etree.ElementTree.Element.makeelement", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.makeelement" }, { "name": "xml.etree.ElementTree.Element.remove", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.remove" }, { "name": "xml.etree.ElementTree.Element.set", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.Element.set" }, { "name": "xml.etree.ElementTree.ElementTree", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree" }, { "name": "xml.etree.ElementTree.ElementTree._setroot", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree._setroot" }, { "name": "xml.etree.ElementTree.ElementTree.find", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.find" }, { "name": "xml.etree.ElementTree.ElementTree.findall", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.findall" }, { "name": "xml.etree.ElementTree.ElementTree.findtext", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.findtext" }, { "name": "xml.etree.ElementTree.ElementTree.getiterator", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.getiterator" }, { "name": "xml.etree.ElementTree.ElementTree.getroot", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.getroot" }, { "name": "xml.etree.ElementTree.ElementTree.iter", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.iter" }, { "name": "xml.etree.ElementTree.ElementTree.iterfind", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.iterfind" }, { "name": "xml.etree.ElementTree.ElementTree.parse", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.parse" }, { "name": "xml.etree.ElementTree.ElementTree.write", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ElementTree.write" }, { "name": "xml.etree.ElementTree.fromstring", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.fromstring" }, { "name": "xml.etree.ElementTree.fromstringlist", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.fromstringlist" }, { "name": "xml.etree.ElementTree.iselement", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.iselement" }, { "name": "xml.etree.ElementTree.iterparse", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.iterparse" }, { "name": "xml.etree.ElementTree.parse", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.parse" }, { "name": "xml.etree.ElementTree.ProcessingInstruction", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.ProcessingInstruction" }, { "name": "xml.etree.ElementTree.QName", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.QName" }, { "name": "xml.etree.ElementTree.register_namespace", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.register_namespace" }, { "name": "xml.etree.ElementTree.SubElement", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.SubElement" }, { "name": "xml.etree.ElementTree.tostring", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.tostring" }, { "name": "xml.etree.ElementTree.tostringlist", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.tostringlist" }, { "name": "xml.etree.ElementTree.TreeBuilder", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.TreeBuilder" }, { "name": "xml.etree.ElementTree.TreeBuilder.close", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.TreeBuilder.close" }, { "name": "xml.etree.ElementTree.TreeBuilder.data", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.TreeBuilder.data" }, { "name": "xml.etree.ElementTree.TreeBuilder.doctype", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.TreeBuilder.doctype" }, { "name": "xml.etree.ElementTree.TreeBuilder.end", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.TreeBuilder.end" }, { "name": "xml.etree.ElementTree.TreeBuilder.start", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.TreeBuilder.start" }, { "name": "xml.etree.ElementTree.XML", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.XML" }, { "name": "xml.etree.ElementTree.XMLID", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.XMLID" }, { "name": "xml.etree.ElementTree.XMLParser", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.XMLParser" }, { "name": "xml.etree.ElementTree.XMLParser.close", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.XMLParser.close" }, { "name": "xml.etree.ElementTree.XMLParser.doctype", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.XMLParser.doctype" }, { "name": "xml.etree.ElementTree.XMLParser.feed", "domId": "xml.etree.ElementTree_xml.etree.ElementTree.XMLParser.feed" } ] }, { "url": "http://docs.python.org/library/cgi.html", "title": "cgi", "html": "
\n

20.2. cgi — Common Gateway Interface support

\n

Source code: Lib/cgi.py

\n
\n

Support module for Common Gateway Interface (CGI) scripts.

\n

This module defines a number of utilities for use by CGI scripts written in\nPython.

\n
\n

20.2.1. Introduction

\n

A CGI script is invoked by an HTTP server, usually to process user input\nsubmitted through an HTML <FORM> or <ISINDEX> element.

\n

Most often, CGI scripts live in the server’s special cgi-bin directory.\nThe HTTP server places all sorts of information about the request (such as the\nclient’s hostname, the requested URL, the query string, and lots of other\ngoodies) in the script’s shell environment, executes the script, and sends the\nscript’s output back to the client.

\n

The script’s input is connected to the client too, and sometimes the form data\nis read this way; at other times the form data is passed via the “query string”\npart of the URL. This module is intended to take care of the different cases\nand provide a simpler interface to the Python script. It also provides a number\nof utilities that help in debugging scripts, and the latest addition is support\nfor file uploads from a form (if your browser supports it).

\n

The output of a CGI script should consist of two sections, separated by a blank\nline. The first section contains a number of headers, telling the client what\nkind of data is following. Python code to generate a minimal header section\nlooks like this:

\n
print "Content-Type: text/html"     # HTML is following\nprint                               # blank line, end of headers\n
\n
\n

The second section is usually HTML, which allows the client software to display\nnicely formatted text with header, in-line images, etc. Here’s Python code that\nprints a simple piece of HTML:

\n
print "<TITLE>CGI script output</TITLE>"\nprint "<H1>This is my first CGI script</H1>"\nprint "Hello, world!"\n
\n
\n
\n
\n

20.2.2. Using the cgi module

\n

Begin by writing import cgi. Do not use from cgi import * — the\nmodule defines all sorts of names for its own use or for backward compatibility\nthat you don’t want in your namespace.

\n

When you write a new script, consider adding these lines:

\n
import cgitb\ncgitb.enable()\n
\n
\n

This activates a special exception handler that will display detailed reports in\nthe Web browser if any errors occur. If you’d rather not show the guts of your\nprogram to users of your script, you can have the reports saved to files\ninstead, with code like this:

\n
import cgitb\ncgitb.enable(display=0, logdir="/tmp")\n
\n
\n

It’s very helpful to use this feature during script development. The reports\nproduced by cgitb provide information that can save you a lot of time in\ntracking down bugs. You can always remove the cgitb line later when you\nhave tested your script and are confident that it works correctly.

\n

To get at submitted form data, it’s best to use the FieldStorage class.\nThe other classes defined in this module are provided mostly for backward\ncompatibility. Instantiate it exactly once, without arguments. This reads the\nform contents from standard input or the environment (depending on the value of\nvarious environment variables set according to the CGI standard). Since it may\nconsume standard input, it should be instantiated only once.

\n

The FieldStorage instance can be indexed like a Python dictionary.\nIt allows membership testing with the in operator, and also supports\nthe standard dictionary method keys() and the built-in function\nlen(). Form fields containing empty strings are ignored and do not appear\nin the dictionary; to keep such values, provide a true value for the optional\nkeep_blank_values keyword parameter when creating the FieldStorage\ninstance.

\n

For instance, the following code (which assumes that the\nContent-Type header and blank line have already been printed)\nchecks that the fields name and addr are both set to a non-empty\nstring:

\n
form = cgi.FieldStorage()\nif "name" not in form or "addr" not in form:\n    print "<H1>Error</H1>"\n    print "Please fill in the name and addr fields."\n    return\nprint "<p>name:", form["name"].value\nprint "<p>addr:", form["addr"].value\n...further form processing here...\n
\n
\n

Here the fields, accessed through form[key], are themselves instances of\nFieldStorage (or MiniFieldStorage, depending on the form\nencoding). The value attribute of the instance yields the string value\nof the field. The getvalue() method returns this string value directly;\nit also accepts an optional second argument as a default to return if the\nrequested key is not present.

\n

If the submitted form data contains more than one field with the same name, the\nobject retrieved by form[key] is not a FieldStorage or\nMiniFieldStorage instance but a list of such instances. Similarly, in\nthis situation, form.getvalue(key) would return a list of strings. If you\nexpect this possibility (when your HTML form contains multiple fields with the\nsame name), use the getlist() function, which always returns a list of\nvalues (so that you do not need to special-case the single item case). For\nexample, this code concatenates any number of username fields, separated by\ncommas:

\n
value = form.getlist("username")\nusernames = ",".join(value)\n
\n
\n

If a field represents an uploaded file, accessing the value via the\nvalue attribute or the getvalue() method reads the entire file in\nmemory as a string. This may not be what you want. You can test for an uploaded\nfile by testing either the filename attribute or the file\nattribute. You can then read the data at leisure from the file\nattribute:

\n
fileitem = form["userfile"]\nif fileitem.file:\n    # It's an uploaded file; count lines\n    linecount = 0\n    while 1:\n        line = fileitem.file.readline()\n        if not line: break\n        linecount = linecount + 1\n
\n
\n

If an error is encountered when obtaining the contents of an uploaded file\n(for example, when the user interrupts the form submission by clicking on\na Back or Cancel button) the done attribute of the object for the\nfield will be set to the value -1.

\n

The file upload draft standard entertains the possibility of uploading multiple\nfiles from one field (using a recursive multipart/* encoding).\nWhen this occurs, the item will be a dictionary-like FieldStorage item.\nThis can be determined by testing its type attribute, which should be\nmultipart/form-data (or perhaps another MIME type matching\nmultipart/*). In this case, it can be iterated over recursively\njust like the top-level form object.

\n

When a form is submitted in the “old” format (as the query string or as a single\ndata part of type application/x-www-form-urlencoded), the items will\nactually be instances of the class MiniFieldStorage. In this case, the\nlist, file, and filename attributes are always None.

\n

A form submitted via POST that also has a query string will contain both\nFieldStorage and MiniFieldStorage items.

\n
\n
\n

20.2.3. Higher Level Interface

\n

\nNew in version 2.2.

\n

The previous section explains how to read CGI form data using the\nFieldStorage class. This section describes a higher level interface\nwhich was added to this class to allow one to do it in a more readable and\nintuitive way. The interface doesn’t make the techniques described in previous\nsections obsolete — they are still useful to process file uploads efficiently,\nfor example.

\n

The interface consists of two simple methods. Using the methods you can process\nform data in a generic way, without the need to worry whether only one or more\nvalues were posted under one name.

\n

In the previous section, you learned to write following code anytime you\nexpected a user to post more than one value under one name:

\n
item = form.getvalue(\"item\")\nif isinstance(item, list):\n    # The user is requesting more than one item.\nelse:\n    # The user is requesting only one item.
\n
\n

This situation is common for example when a form contains a group of multiple\ncheckboxes with the same name:

\n
<input type=\"checkbox\" name=\"item\" value=\"1\" />\n<input type=\"checkbox\" name=\"item\" value=\"2\" />
\n
\n

In most situations, however, there’s only one form control with a particular\nname in a form and then you expect and need only one value associated with this\nname. So you write a script containing for example this code:

\n
user = form.getvalue("user").upper()\n
\n
\n

The problem with the code is that you should never expect that a client will\nprovide valid input to your scripts. For example, if a curious user appends\nanother user=foo pair to the query string, then the script would crash,\nbecause in this situation the getvalue("user") method call returns a list\ninstead of a string. Calling the upper() method on a list is not valid\n(since lists do not have a method of this name) and results in an\nAttributeError exception.

\n

Therefore, the appropriate way to read form data values was to always use the\ncode which checks whether the obtained value is a single value or a list of\nvalues. That’s annoying and leads to less readable scripts.

\n

A more convenient approach is to use the methods getfirst() and\ngetlist() provided by this higher level interface.

\n
\n
\nFieldStorage.getfirst(name[, default])
\n
This method always returns only one value associated with form field name.\nThe method returns only the first value in case that more values were posted\nunder such name. Please note that the order in which the values are received\nmay vary from browser to browser and should not be counted on. [1] If no such\nform field or value exists then the method returns the value specified by the\noptional parameter default. This parameter defaults to None if not\nspecified.
\n\n
\n
\nFieldStorage.getlist(name)
\n
This method always returns a list of values associated with form field name.\nThe method returns an empty list if no such form field or value exists for\nname. It returns a list consisting of one item if only one such value exists.
\n\n

Using these methods you can write nice compact code:

\n
import cgi\nform = cgi.FieldStorage()\nuser = form.getfirst("user", "").upper()    # This way it's safe.\nfor item in form.getlist("item"):\n    do_something(item)\n
\n
\n
\n
\n

20.2.4. Old classes

\n

\nDeprecated since version 2.6.

\n

SvFormContentDict stores single value form content as dictionary; it\nassumes each field name occurs in the form only once.

\n

FormContentDict stores multiple value form content as a dictionary (the\nform items are lists of values). Useful if your form contains multiple fields\nwith the same name.

\n

Other classes (FormContent, InterpFormContentDict) are present\nfor backwards compatibility with really old applications only.

\n
\n
\n

20.2.5. Functions

\n

These are useful if you want more control, or if you want to employ some of the\nalgorithms implemented in this module in other circumstances.

\n
\n
\ncgi.parse(fp[, keep_blank_values[, strict_parsing]])
\n
Parse a query in the environment or from a file (the file defaults to\nsys.stdin). The keep_blank_values and strict_parsing parameters are\npassed to urlparse.parse_qs() unchanged.
\n\n
\n
\ncgi.parse_qs(qs[, keep_blank_values[, strict_parsing]])
\n
This function is deprecated in this module. Use urlparse.parse_qs()\ninstead. It is maintained here only for backward compatiblity.
\n\n
\n
\ncgi.parse_qsl(qs[, keep_blank_values[, strict_parsing]])
\n
This function is deprecated in this module. Use urlparse.parse_qsl()\ninstead. It is maintained here only for backward compatiblity.
\n\n
\n
\ncgi.parse_multipart(fp, pdict)
\n

Parse input of type multipart/form-data (for file uploads).\nArguments are fp for the input file and pdict for a dictionary containing\nother parameters in the Content-Type header.

\n

Returns a dictionary just like urlparse.parse_qs() keys are the field names, each\nvalue is a list of values for that field. This is easy to use but not much good\nif you are expecting megabytes to be uploaded — in that case, use the\nFieldStorage class instead which is much more flexible.

\n

Note that this does not parse nested multipart parts — use\nFieldStorage for that.

\n
\n\n
\n
\ncgi.parse_header(string)
\n
Parse a MIME header (such as Content-Type) into a main value and a\ndictionary of parameters.
\n\n
\n
\ncgi.test()
\n
Robust test CGI script, usable as main program. Writes minimal HTTP headers and\nformats all information provided to the script in HTML form.
\n\n
\n
\ncgi.print_environ()
\n
Format the shell environment in HTML.
\n\n
\n
\ncgi.print_form(form)
\n
Format a form in HTML.
\n\n
\n
\ncgi.print_directory()
\n
Format the current directory in HTML.
\n\n
\n
\ncgi.print_environ_usage()
\n
Print a list of useful (used by CGI) environment variables in HTML.
\n\n
\n
\ncgi.escape(s[, quote])
\n

Convert the characters '&', '<' and '>' in string s to HTML-safe\nsequences. Use this if you need to display text that might contain such\ncharacters in HTML. If the optional flag quote is true, the quotation mark\ncharacter (") is also translated; this helps for inclusion in an HTML\nattribute value delimited by double quotes, as in <a href="...">. Note\nthat single quotes are never translated.

\n

If the value to be quoted might include single- or double-quote characters,\nor both, consider using the quoteattr() function in the\nxml.sax.saxutils module instead.

\n
\n\n
\n
\n

20.2.6. Caring about security

\n

There’s one important rule: if you invoke an external program (via the\nos.system() or os.popen() functions. or others with similar\nfunctionality), make very sure you don’t pass arbitrary strings received from\nthe client to the shell. This is a well-known security hole whereby clever\nhackers anywhere on the Web can exploit a gullible CGI script to invoke\narbitrary shell commands. Even parts of the URL or field names cannot be\ntrusted, since the request doesn’t have to come from your form!

\n

To be on the safe side, if you must pass a string gotten from a form to a shell\ncommand, you should make sure the string contains only alphanumeric characters,\ndashes, underscores, and periods.

\n
\n
\n

20.2.7. Installing your CGI script on a Unix system

\n

Read the documentation for your HTTP server and check with your local system\nadministrator to find the directory where CGI scripts should be installed;\nusually this is in a directory cgi-bin in the server tree.

\n

Make sure that your script is readable and executable by “others”; the Unix file\nmode should be 0755 octal (use chmod 0755 filename). Make sure that the\nfirst line of the script contains #! starting in column 1 followed by the\npathname of the Python interpreter, for instance:

\n
#!/usr/local/bin/python\n
\n
\n

Make sure the Python interpreter exists and is executable by “others”.

\n

Make sure that any files your script needs to read or write are readable or\nwritable, respectively, by “others” — their mode should be 0644 for\nreadable and 0666 for writable. This is because, for security reasons, the\nHTTP server executes your script as user “nobody”, without any special\nprivileges. It can only read (write, execute) files that everybody can read\n(write, execute). The current directory at execution time is also different (it\nis usually the server’s cgi-bin directory) and the set of environment variables\nis also different from what you get when you log in. In particular, don’t count\non the shell’s search path for executables (PATH) or the Python module\nsearch path (PYTHONPATH) to be set to anything interesting.

\n

If you need to load modules from a directory which is not on Python’s default\nmodule search path, you can change the path in your script, before importing\nother modules. For example:

\n
import sys\nsys.path.insert(0, "/usr/home/joe/lib/python")\nsys.path.insert(0, "/usr/local/lib/python")\n
\n
\n

(This way, the directory inserted last will be searched first!)

\n

Instructions for non-Unix systems will vary; check your HTTP server’s\ndocumentation (it will usually have a section on CGI scripts).

\n
\n
\n

20.2.8. Testing your CGI script

\n

Unfortunately, a CGI script will generally not run when you try it from the\ncommand line, and a script that works perfectly from the command line may fail\nmysteriously when run from the server. There’s one reason why you should still\ntest your script from the command line: if it contains a syntax error, the\nPython interpreter won’t execute it at all, and the HTTP server will most likely\nsend a cryptic error to the client.

\n

Assuming your script has no syntax errors, yet it does not work, you have no\nchoice but to read the next section.

\n
\n
\n

20.2.9. Debugging CGI scripts

\n

First of all, check for trivial installation errors — reading the section\nabove on installing your CGI script carefully can save you a lot of time. If\nyou wonder whether you have understood the installation procedure correctly, try\ninstalling a copy of this module file (cgi.py) as a CGI script. When\ninvoked as a script, the file will dump its environment and the contents of the\nform in HTML form. Give it the right mode etc, and send it a request. If it’s\ninstalled in the standard cgi-bin directory, it should be possible to\nsend it a request by entering a URL into your browser of the form:

\n
http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
\n
\n

If this gives an error of type 404, the server cannot find the script – perhaps\nyou need to install it in a different directory. If it gives another error,\nthere’s an installation problem that you should fix before trying to go any\nfurther. If you get a nicely formatted listing of the environment and form\ncontent (in this example, the fields should be listed as “addr” with value “At\nHome” and “name” with value “Joe Blow”), the cgi.py script has been\ninstalled correctly. If you follow the same procedure for your own script, you\nshould now be able to debug it.

\n

The next step could be to call the cgi module’s test() function\nfrom your script: replace its main code with the single statement

\n
cgi.test()\n
\n
\n

This should produce the same results as those gotten from installing the\ncgi.py file itself.

\n

When an ordinary Python script raises an unhandled exception (for whatever\nreason: of a typo in a module name, a file that can’t be opened, etc.), the\nPython interpreter prints a nice traceback and exits. While the Python\ninterpreter will still do this when your CGI script raises an exception, most\nlikely the traceback will end up in one of the HTTP server’s log files, or be\ndiscarded altogether.

\n

Fortunately, once you have managed to get your script to execute some code,\nyou can easily send tracebacks to the Web browser using the cgitb module.\nIf you haven’t done so already, just add the lines:

\n
import cgitb\ncgitb.enable()\n
\n
\n

to the top of your script. Then try running it again; when a problem occurs,\nyou should see a detailed report that will likely make apparent the cause of the\ncrash.

\n

If you suspect that there may be a problem in importing the cgitb module,\nyou can use an even more robust approach (which only uses built-in modules):

\n
import sys\nsys.stderr = sys.stdout\nprint "Content-Type: text/plain"\nprint\n...your code here...\n
\n
\n

This relies on the Python interpreter to print the traceback. The content type\nof the output is set to plain text, which disables all HTML processing. If your\nscript works, the raw HTML will be displayed by your client. If it raises an\nexception, most likely after the first two lines have been printed, a traceback\nwill be displayed. Because no HTML interpretation is going on, the traceback\nwill be readable.

\n
\n
\n

20.2.10. Common problems and solutions

\n\n

Footnotes

\n\n\n\n\n\n
[1]Note that some recent versions of the HTML specification do state what order the\nfield values should be supplied in, but knowing whether a request was\nreceived from a conforming browser, or even from a browser at all, is tedious\nand error-prone.
\n
\n
", "searchableItems": [ { "name": "cgi.escape", "domId": "cgi_cgi.escape" }, { "name": "cgi.FieldStorage.getfirst", "domId": "cgi_cgi.FieldStorage.getfirst" }, { "name": "cgi.FieldStorage.getlist", "domId": "cgi_cgi.FieldStorage.getlist" }, { "name": "cgi.parse", "domId": "cgi_cgi.parse" }, { "name": "cgi.parse_header", "domId": "cgi_cgi.parse_header" }, { "name": "cgi.parse_multipart", "domId": "cgi_cgi.parse_multipart" }, { "name": "cgi.parse_qs", "domId": "cgi_cgi.parse_qs" }, { "name": "cgi.parse_qsl", "domId": "cgi_cgi.parse_qsl" }, { "name": "cgi.print_directory", "domId": "cgi_cgi.print_directory" }, { "name": "cgi.print_environ", "domId": "cgi_cgi.print_environ" }, { "name": "cgi.print_environ_usage", "domId": "cgi_cgi.print_environ_usage" }, { "name": "cgi.print_form", "domId": "cgi_cgi.print_form" }, { "name": "cgi.test", "domId": "cgi_cgi.test" } ] }, { "url": "http://docs.python.org/library/urllib.html", "title": "urllib", "html": "
\n

20.5. urllib — Open arbitrary resources by URL

\n
\n

Note

\n

The urllib module has been split into parts and renamed in\nPython 3.0 to urllib.request, urllib.parse,\nand urllib.error. The 2to3 tool will automatically adapt\nimports when converting your sources to 3.0.\nAlso note that the urllib.urlopen() function has been removed in\nPython 3.0 in favor of urllib2.urlopen().

\n
\n

This module provides a high-level interface for fetching data across the World\nWide Web. In particular, the urlopen() function is similar to the\nbuilt-in function open(), but accepts Universal Resource Locators (URLs)\ninstead of filenames. Some restrictions apply — it can only open URLs for\nreading, and no seek operations are available.

\n
\n

Warning

\n

When opening HTTPS URLs, it does not attempt to validate the\nserver certificate. Use at your own risk!

\n
\n
\n

20.5.1. High-level interface

\n
\n
\nurllib.urlopen(url[, data[, proxies]])
\n

Open a network object denoted by a URL for reading. If the URL does not have a\nscheme identifier, or if it has file: as its scheme identifier, this\nopens a local file (without universal newlines); otherwise it opens a socket to\na server somewhere on the network. If the connection cannot be made the\nIOError exception is raised. If all went well, a file-like object is\nreturned. This supports the following methods: read(), readline(),\nreadlines(), fileno(), close(), info(), getcode() and\ngeturl(). It also has proper support for the iterator protocol. One\ncaveat: the read() method, if the size argument is omitted or negative,\nmay not read until the end of the data stream; there is no good way to determine\nthat the entire stream from a socket has been read in the general case.

\n

Except for the info(), getcode() and geturl() methods,\nthese methods have the same interface as for file objects — see section\nFile Objects in this manual. (It is not a built-in file object,\nhowever, so it can’t be used at those few places where a true built-in file\nobject is required.)

\n

The info() method returns an instance of the class\nmimetools.Message containing meta-information associated with the\nURL. When the method is HTTP, these headers are those returned by the server\nat the head of the retrieved HTML page (including Content-Length and\nContent-Type). When the method is FTP, a Content-Length header will be\npresent if (as is now usual) the server passed back a file length in response\nto the FTP retrieval request. A Content-Type header will be present if the\nMIME type can be guessed. When the method is local-file, returned headers\nwill include a Date representing the file’s last-modified time, a\nContent-Length giving file size, and a Content-Type containing a guess at the\nfile’s type. See also the description of the mimetools module.

\n

The geturl() method returns the real URL of the page. In some cases, the\nHTTP server redirects a client to another URL. The urlopen() function\nhandles this transparently, but in some cases the caller needs to know which URL\nthe client was redirected to. The geturl() method can be used to get at\nthis redirected URL.

\n

The getcode() method returns the HTTP status code that was sent with the\nresponse, or None if the URL is no HTTP URL.

\n

If the url uses the http: scheme identifier, the optional data\nargument may be given to specify a POST request (normally the request type\nis GET). The data argument must be in standard\napplication/x-www-form-urlencoded format; see the urlencode()\nfunction below.

\n

The urlopen() function works transparently with proxies which do not\nrequire authentication. In a Unix or Windows environment, set the\nhttp_proxy, or ftp_proxy environment variables to a URL that\nidentifies the proxy server before starting the Python interpreter. For example\n(the '%' is the command prompt):

\n
% http_proxy=\"http://www.someproxy.com:3128\"\n% export http_proxy\n% python\n...
\n
\n

The no_proxy environment variable can be used to specify hosts which\nshouldn’t be reached via proxy; if set, it should be a comma-separated list\nof hostname suffixes, optionally with :port appended, for example\ncern.ch,ncsa.uiuc.edu,some.host:8080.

\n

In a Windows environment, if no proxy environment variables are set, proxy\nsettings are obtained from the registry’s Internet Settings section.

\n

In a Mac OS X environment, urlopen() will retrieve proxy information\nfrom the OS X System Configuration Framework, which can be managed with\nNetwork System Preferences panel.

\n

Alternatively, the optional proxies argument may be used to explicitly specify\nproxies. It must be a dictionary mapping scheme names to proxy URLs, where an\nempty dictionary causes no proxies to be used, and None (the default value)\ncauses environmental proxy settings to be used as discussed above. For\nexample:

\n
# Use http://www.someproxy.com:3128 for http proxying\nproxies = {'http': 'http://www.someproxy.com:3128'}\nfilehandle = urllib.urlopen(some_url, proxies=proxies)\n# Don't use any proxies\nfilehandle = urllib.urlopen(some_url, proxies={})\n# Use proxies from environment - both versions are equivalent\nfilehandle = urllib.urlopen(some_url, proxies=None)\nfilehandle = urllib.urlopen(some_url)\n
\n
\n

Proxies which require authentication for use are not currently supported; this\nis considered an implementation limitation.

\n

\nChanged in version 2.3: Added the proxies support.

\n

\nChanged in version 2.6: Added getcode() to returned object and support for the\nno_proxy environment variable.

\n

\nDeprecated since version 2.6: The urlopen() function has been removed in Python 3.0 in favor\nof urllib2.urlopen().

\n
\n\n
\n
\nurllib.urlretrieve(url[, filename[, reporthook[, data]]])
\n

Copy a network object denoted by a URL to a local file, if necessary. If the URL\npoints to a local file, or a valid cached copy of the object exists, the object\nis not copied. Return a tuple (filename, headers) where filename is the\nlocal file name under which the object can be found, and headers is whatever\nthe info() method of the object returned by urlopen() returned (for\na remote object, possibly cached). Exceptions are the same as for\nurlopen().

\n

The second argument, if present, specifies the file location to copy to (if\nabsent, the location will be a tempfile with a generated name). The third\nargument, if present, is a hook function that will be called once on\nestablishment of the network connection and once after each block read\nthereafter. The hook will be passed three arguments; a count of blocks\ntransferred so far, a block size in bytes, and the total size of the file. The\nthird argument may be -1 on older FTP servers which do not return a file\nsize in response to a retrieval request.

\n

If the url uses the http: scheme identifier, the optional data\nargument may be given to specify a POST request (normally the request type\nis GET). The data argument must in standard\napplication/x-www-form-urlencoded format; see the urlencode()\nfunction below.

\n

\nChanged in version 2.5: urlretrieve() will raise ContentTooShortError when it detects that\nthe amount of data available was less than the expected amount (which is the\nsize reported by a Content-Length header). This can occur, for example, when\nthe download is interrupted.

The Content-Length is treated as a lower bound: if there’s more data to read,\nurlretrieve() reads more data, but if less data is available, it raises\nthe exception.

\n

You can still retrieve the downloaded data in this case, it is stored in the\ncontent attribute of the exception instance.

\n

If no Content-Length header was supplied, urlretrieve() can not check\nthe size of the data it has downloaded, and just returns it. In this case you\njust have to assume that the download was successful.

\n

\n
\n\n
\n
\nurllib._urlopener
\n

The public functions urlopen() and urlretrieve() create an instance\nof the FancyURLopener class and use it to perform their requested\nactions. To override this functionality, programmers can create a subclass of\nURLopener or FancyURLopener, then assign an instance of that\nclass to the urllib._urlopener variable before calling the desired function.\nFor example, applications may want to specify a different\nUser-Agent header than URLopener defines. This can be\naccomplished with the following code:

\n
import urllib\n\nclass AppURLopener(urllib.FancyURLopener):\n    version = "App/1.7"\n\nurllib._urlopener = AppURLopener()\n
\n
\n
\n\n
\n
\nurllib.urlcleanup()
\n
Clear the cache that may have been built up by previous calls to\nurlretrieve().
\n\n
\n
\n

20.5.2. Utility functions

\n
\n
\nurllib.quote(string[, safe])
\n

Replace special characters in string using the %xx escape. Letters,\ndigits, and the characters '_.-' are never quoted. By default, this\nfunction is intended for quoting the path section of the URL. The optional\nsafe parameter specifies additional characters that should not be quoted\n— its default value is '/'.

\n

Example: quote('/~connolly/') yields '/%7econnolly/'.

\n
\n\n
\n
\nurllib.quote_plus(string[, safe])
\n
Like quote(), but also replaces spaces by plus signs, as required for\nquoting HTML form values when building up a query string to go into a URL.\nPlus signs in the original string are escaped unless they are included in\nsafe. It also does not have safe default to '/'.
\n\n
\n
\nurllib.unquote(string)
\n

Replace %xx escapes by their single-character equivalent.

\n

Example: unquote('/%7Econnolly/') yields '/~connolly/'.

\n
\n\n
\n
\nurllib.unquote_plus(string)
\n
Like unquote(), but also replaces plus signs by spaces, as required for\nunquoting HTML form values.
\n\n
\n
\nurllib.urlencode(query[, doseq])
\n
Convert a mapping object or a sequence of two-element tuples to a\n“percent-encoded” string, suitable to pass to urlopen() above as the\noptional data argument. This is useful to pass a dictionary of form\nfields to a POST request. The resulting string is a series of\nkey=value pairs separated by '&' characters, where both key and\nvalue are quoted using quote_plus() above. When a sequence of\ntwo-element tuples is used as the query argument, the first element of\neach tuple is a key and the second is a value. The value element in itself\ncan be a sequence and in that case, if the optional parameter doseq is\nevaluates to True, individual key=value pairs separated by '&' are\ngenerated for each element of the value sequence for the key. The order of\nparameters in the encoded string will match the order of parameter tuples in\nthe sequence. The urlparse module provides the functions\nparse_qs() and parse_qsl() which are used to parse query strings\ninto Python data structures.
\n\n
\n
\nurllib.pathname2url(path)
\n
Convert the pathname path from the local syntax for a path to the form used in\nthe path component of a URL. This does not produce a complete URL. The return\nvalue will already be quoted using the quote() function.
\n\n
\n
\nurllib.url2pathname(path)
\n
Convert the path component path from an percent-encoded URL to the local syntax for a\npath. This does not accept a complete URL. This function uses unquote()\nto decode path.
\n\n
\n
\nurllib.getproxies()
\n
This helper function returns a dictionary of scheme to proxy server URL\nmappings. It scans the environment for variables named <scheme>_proxy,\nin case insensitive way, for all operating systems first, and when it cannot\nfind it, looks for proxy information from Mac OSX System Configuration for\nMac OS X and Windows Systems Registry for Windows.
\n\n
\n
\n

20.5.3. URL Opener objects

\n
\n
\nclass urllib.URLopener([proxies[, **x509]])
\n

Base class for opening and reading URLs. Unless you need to support opening\nobjects using schemes other than http:, ftp:, or file:,\nyou probably want to use FancyURLopener.

\n

By default, the URLopener class sends a User-Agent header\nof urllib/VVV, where VVV is the urllib version number.\nApplications can define their own User-Agent header by subclassing\nURLopener or FancyURLopener and setting the class attribute\nversion to an appropriate string value in the subclass definition.

\n

The optional proxies parameter should be a dictionary mapping scheme names to\nproxy URLs, where an empty dictionary turns proxies off completely. Its default\nvalue is None, in which case environmental proxy settings will be used if\npresent, as discussed in the definition of urlopen(), above.

\n

Additional keyword parameters, collected in x509, may be used for\nauthentication of the client when using the https: scheme. The keywords\nkey_file and cert_file are supported to provide an SSL key and certificate;\nboth are needed to support client authentication.

\n

URLopener objects will raise an IOError exception if the server\nreturns an error code.

\n
\n
\n
\nopen(fullurl[, data])
\n
Open fullurl using the appropriate protocol. This method sets up cache and\nproxy information, then calls the appropriate open method with its input\narguments. If the scheme is not recognized, open_unknown() is called.\nThe data argument has the same meaning as the data argument of\nurlopen().
\n\n
\n
\nopen_unknown(fullurl[, data])
\n
Overridable interface to open unknown URL types.
\n\n
\n
\nretrieve(url[, filename[, reporthook[, data]]])
\n

Retrieves the contents of url and places it in filename. The return value\nis a tuple consisting of a local filename and either a\nmimetools.Message object containing the response headers (for remote\nURLs) or None (for local URLs). The caller must then open and read the\ncontents of filename. If filename is not given and the URL refers to a\nlocal file, the input filename is returned. If the URL is non-local and\nfilename is not given, the filename is the output of tempfile.mktemp()\nwith a suffix that matches the suffix of the last path component of the input\nURL. If reporthook is given, it must be a function accepting three numeric\nparameters. It will be called after each chunk of data is read from the\nnetwork. reporthook is ignored for local URLs.

\n

If the url uses the http: scheme identifier, the optional data\nargument may be given to specify a POST request (normally the request type\nis GET). The data argument must in standard\napplication/x-www-form-urlencoded format; see the urlencode()\nfunction below.

\n
\n\n
\n
\nversion
\n
Variable that specifies the user agent of the opener object. To get\nurllib to tell servers that it is a particular user agent, set this in a\nsubclass as a class variable or in the constructor before calling the base\nconstructor.
\n\n
\n
\n\n
\n
\nclass urllib.FancyURLopener(...)
\n

FancyURLopener subclasses URLopener providing default handling\nfor the following HTTP response codes: 301, 302, 303, 307 and 401. For the 30x\nresponse codes listed above, the Location header is used to fetch\nthe actual URL. For 401 response codes (authentication required), basic HTTP\nauthentication is performed. For the 30x response codes, recursion is bounded\nby the value of the maxtries attribute, which defaults to 10.

\n

For all other response codes, the method http_error_default() is called\nwhich you can override in subclasses to handle the error appropriately.

\n
\n

Note

\n

According to the letter of RFC 2616, 301 and 302 responses to POST requests\nmust not be automatically redirected without confirmation by the user. In\nreality, browsers do allow automatic redirection of these responses, changing\nthe POST to a GET, and urllib reproduces this behaviour.

\n
\n

The parameters to the constructor are the same as those for URLopener.

\n
\n

Note

\n
\nWhen performing basic authentication, a FancyURLopener instance calls\nits prompt_user_passwd() method. The default implementation asks the\nusers for the required information on the controlling terminal. A subclass may\noverride this method to support more appropriate behavior if needed.
\n

The FancyURLopener class offers one additional method that should be\noverloaded to provide the appropriate behavior:

\n
\n
\nprompt_user_passwd(host, realm)
\n

Return information needed to authenticate the user at the given host in the\nspecified security realm. The return value should be a tuple, (user,\npassword), which can be used for basic authentication.

\n

The implementation prompts for this information on the terminal; an application\nshould override this method to use an appropriate interaction model in the local\nenvironment.

\n
\n\n
\n
\n\n
\n
\nexception urllib.ContentTooShortError(msg[, content])
\n

This exception is raised when the urlretrieve() function detects that the\namount of the downloaded data is less than the expected amount (given by the\nContent-Length header). The content attribute stores the downloaded\n(and supposedly truncated) data.

\n

\nNew in version 2.5.

\n
\n\n
\n
\n

20.5.4. urllib Restrictions

\n
\n
\n\n
\n
\n

20.5.5. Examples

\n

Here is an example session that uses the GET method to retrieve a URL\ncontaining parameters:

\n
>>> import urllib\n>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})\n>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s"  params)\n>>> print f.read()\n
\n
\n

The following example uses the POST method instead:

\n
>>> import urllib\n>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})\n>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)\n>>> print f.read()\n
\n
\n

The following example uses an explicitly specified HTTP proxy, overriding\nenvironment settings:

\n
>>> import urllib\n>>> proxies = {'http': 'http://proxy.example.com:8080/'}\n>>> opener = urllib.FancyURLopener(proxies)\n>>> f = opener.open("http://www.python.org")\n>>> f.read()\n
\n
\n

The following example uses no proxies at all, overriding environment settings:

\n
>>> import urllib\n>>> opener = urllib.FancyURLopener({})\n>>> f = opener.open("http://www.python.org/")\n>>> f.read()\n
\n
\n
\n
", "searchableItems": [ { "name": "urllib.FancyURLopener", "domId": "urllib_urllib.FancyURLopener" }, { "name": "urllib.FancyURLopener.prompt_user_passwd", "domId": "urllib_urllib.FancyURLopener.prompt_user_passwd" }, { "name": "urllib.getproxies", "domId": "urllib_urllib.getproxies" }, { "name": "urllib.pathname2url", "domId": "urllib_urllib.pathname2url" }, { "name": "urllib.quote", "domId": "urllib_urllib.quote" }, { "name": "urllib.quote_plus", "domId": "urllib_urllib.quote_plus" }, { "name": "urllib.unquote", "domId": "urllib_urllib.unquote" }, { "name": "urllib.unquote_plus", "domId": "urllib_urllib.unquote_plus" }, { "name": "urllib.url2pathname", "domId": "urllib_urllib.url2pathname" }, { "name": "urllib.urlcleanup", "domId": "urllib_urllib.urlcleanup" }, { "name": "urllib.urlencode", "domId": "urllib_urllib.urlencode" }, { "name": "urllib.urlopen", "domId": "urllib_urllib.urlopen" }, { "name": "urllib.URLopener", "domId": "urllib_urllib.URLopener" }, { "name": "urllib.URLopener.open", "domId": "urllib_urllib.URLopener.open" }, { "name": "urllib.URLopener.open_unknown", "domId": "urllib_urllib.URLopener.open_unknown" }, { "name": "urllib.URLopener.retrieve", "domId": "urllib_urllib.URLopener.retrieve" }, { "name": "urllib.urlretrieve", "domId": "urllib_urllib.urlretrieve" } ] }, { "url": "http://docs.python.org/library/wsgiref.html", "title": "wsgiref", "html": "
\n

20.4. wsgiref — WSGI Utilities and Reference Implementation

\n

\nNew in version 2.5.

\n

The Web Server Gateway Interface (WSGI) is a standard interface between web\nserver software and web applications written in Python. Having a standard\ninterface makes it easy to use an application that supports WSGI with a number\nof different web servers.

\n

Only authors of web servers and programming frameworks need to know every detail\nand corner case of the WSGI design. You don’t need to understand every detail\nof WSGI just to install a WSGI application or to write a web application using\nan existing framework.

\n

wsgiref is a reference implementation of the WSGI specification that can\nbe used to add WSGI support to a web server or framework. It provides utilities\nfor manipulating WSGI environment variables and response headers, base classes\nfor implementing WSGI servers, a demo HTTP server that serves WSGI applications,\nand a validation tool that checks WSGI servers and applications for conformance\nto the WSGI specification (PEP 333).

\n

See http://www.wsgi.org for more information about WSGI, and links to tutorials\nand other resources.

\n
\n

20.4.1. wsgiref.util – WSGI environment utilities

\n

This module provides a variety of utility functions for working with WSGI\nenvironments. A WSGI environment is a dictionary containing HTTP request\nvariables as described in PEP 333. All of the functions taking an environ\nparameter expect a WSGI-compliant dictionary to be supplied; please see\nPEP 333 for a detailed specification.

\n
\n
\nwsgiref.util.guess_scheme(environ)
\n

Return a guess for whether wsgi.url_scheme should be “http” or “https”, by\nchecking for a HTTPS environment variable in the environ dictionary. The\nreturn value is a string.

\n

This function is useful when creating a gateway that wraps CGI or a CGI-like\nprotocol such as FastCGI. Typically, servers providing such protocols will\ninclude a HTTPS variable with a value of “1” “yes”, or “on” when a request\nis received via SSL. So, this function returns “https” if such a value is\nfound, and “http” otherwise.

\n
\n\n
\n
\nwsgiref.util.request_uri(environ[, include_query=1])
\n
Return the full request URI, optionally including the query string, using the\nalgorithm found in the “URL Reconstruction” section of PEP 333. If\ninclude_query is false, the query string is not included in the resulting URI.
\n\n
\n
\nwsgiref.util.application_uri(environ)
\n
Similar to request_uri(), except that the PATH_INFO and\nQUERY_STRING variables are ignored. The result is the base URI of the\napplication object addressed by the request.
\n\n
\n
\nwsgiref.util.shift_path_info(environ)
\n

Shift a single name from PATH_INFO to SCRIPT_NAME and return the name.\nThe environ dictionary is modified in-place; use a copy if you need to keep\nthe original PATH_INFO or SCRIPT_NAME intact.

\n

If there are no remaining path segments in PATH_INFO, None is returned.

\n

Typically, this routine is used to process each portion of a request URI path,\nfor example to treat the path as a series of dictionary keys. This routine\nmodifies the passed-in environment to make it suitable for invoking another WSGI\napplication that is located at the target URI. For example, if there is a WSGI\napplication at /foo, and the request URI path is /foo/bar/baz, and the\nWSGI application at /foo calls shift_path_info(), it will receive the\nstring “bar”, and the environment will be updated to be suitable for passing to\na WSGI application at /foo/bar. That is, SCRIPT_NAME will change from\n/foo to /foo/bar, and PATH_INFO will change from /bar/baz to\n/baz.

\n

When PATH_INFO is just a “/”, this routine returns an empty string and\nappends a trailing slash to SCRIPT_NAME, even though empty path segments are\nnormally ignored, and SCRIPT_NAME doesn’t normally end in a slash. This is\nintentional behavior, to ensure that an application can tell the difference\nbetween URIs ending in /x from ones ending in /x/ when using this\nroutine to do object traversal.

\n
\n\n
\n
\nwsgiref.util.setup_testing_defaults(environ)
\n

Update environ with trivial defaults for testing purposes.

\n

This routine adds various parameters required for WSGI, including HTTP_HOST,\nSERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME,\nPATH_INFO, and all of the PEP 333-defined wsgi.* variables. It\nonly supplies default values, and does not replace any existing settings for\nthese variables.

\n

This routine is intended to make it easier for unit tests of WSGI servers and\napplications to set up dummy environments. It should NOT be used by actual WSGI\nservers or applications, since the data is fake!

\n

Example usage:

\n
from wsgiref.util import setup_testing_defaults\nfrom wsgiref.simple_server import make_server\n\n# A relatively simple WSGI application. It's going to print out the\n# environment dictionary after being updated by setup_testing_defaults\ndef simple_app(environ, start_response):\n    setup_testing_defaults(environ)\n\n    status = '200 OK'\n    headers = [('Content-type', 'text/plain')]\n\n    start_response(status, headers)\n\n    ret = ["%s: %s\\n"  (key, value)\n           for key, value in environ.iteritems()]\n    return ret\n\nhttpd = make_server('', 8000, simple_app)\nprint "Serving on port 8000..."\nhttpd.serve_forever()\n
\n
\n
\n\n

In addition to the environment functions above, the wsgiref.util module\nalso provides these miscellaneous utilities:

\n
\n
\nwsgiref.util.is_hop_by_hop(header_name)
\n
Return true if ‘header_name’ is an HTTP/1.1 “Hop-by-Hop” header, as defined by\nRFC 2616.
\n\n
\n
\nclass wsgiref.util.FileWrapper(filelike[, blksize=8192])
\n

A wrapper to convert a file-like object to an iterator. The resulting objects\nsupport both __getitem__() and __iter__() iteration styles, for\ncompatibility with Python 2.1 and Jython. As the object is iterated over, the\noptional blksize parameter will be repeatedly passed to the filelike\nobject’s read() method to obtain strings to yield. When read()\nreturns an empty string, iteration is ended and is not resumable.

\n

If filelike has a close() method, the returned object will also have a\nclose() method, and it will invoke the filelike object’s close()\nmethod when called.

\n

Example usage:

\n
from StringIO import StringIO\nfrom wsgiref.util import FileWrapper\n\n# We're using a StringIO-buffer for as the file-like object\nfilelike = StringIO("This is an example file-like object"*10)\nwrapper = FileWrapper(filelike, blksize=5)\n\nfor chunk in wrapper:\n    print chunk\n
\n
\n
\n\n
\n
\n

20.4.2. wsgiref.headers – WSGI response header tools

\n

This module provides a single class, Headers, for convenient\nmanipulation of WSGI response headers using a mapping-like interface.

\n
\n
\nclass wsgiref.headers.Headers(headers)
\n

Create a mapping-like object wrapping headers, which must be a list of header\nname/value tuples as described in PEP 333. Any changes made to the new\nHeaders object will directly update the headers list it was created\nwith.

\n

Headers objects support typical mapping operations including\n__getitem__(), get(), __setitem__(), setdefault(),\n__delitem__(), __contains__() and has_key(). For each of\nthese methods, the key is the header name (treated case-insensitively), and the\nvalue is the first value associated with that header name. Setting a header\ndeletes any existing values for that header, then adds a new value at the end of\nthe wrapped header list. Headers’ existing order is generally maintained, with\nnew headers added to the end of the wrapped list.

\n

Unlike a dictionary, Headers objects do not raise an error when you try\nto get or delete a key that isn’t in the wrapped header list. Getting a\nnonexistent header just returns None, and deleting a nonexistent header does\nnothing.

\n

Headers objects also support keys(), values(), and\nitems() methods. The lists returned by keys() and items() can\ninclude the same key more than once if there is a multi-valued header. The\nlen() of a Headers object is the same as the length of its\nitems(), which is the same as the length of the wrapped header list. In\nfact, the items() method just returns a copy of the wrapped header list.

\n

Calling str() on a Headers object returns a formatted string\nsuitable for transmission as HTTP response headers. Each header is placed on a\nline with its value, separated by a colon and a space. Each line is terminated\nby a carriage return and line feed, and the string is terminated with a blank\nline.

\n

In addition to their mapping interface and formatting features, Headers\nobjects also have the following methods for querying and adding multi-valued\nheaders, and for adding headers with MIME parameters:

\n
\n
\nget_all(name)
\n

Return a list of all the values for the named header.

\n

The returned list will be sorted in the order they appeared in the original\nheader list or were added to this instance, and may contain duplicates. Any\nfields deleted and re-inserted are always appended to the header list. If no\nfields exist with the given name, returns an empty list.

\n
\n\n
\n
\nadd_header(name, value, **_params)
\n

Add a (possibly multi-valued) header, with optional MIME parameters specified\nvia keyword arguments.

\n

name is the header field to add. Keyword arguments can be used to set MIME\nparameters for the header field. Each parameter must be a string or None.\nUnderscores in parameter names are converted to dashes, since dashes are illegal\nin Python identifiers, but many MIME parameter names include dashes. If the\nparameter value is a string, it is added to the header value parameters in the\nform name="value". If it is None, only the parameter name is added.\n(This is used for MIME parameters without a value.) Example usage:

\n
h.add_header('content-disposition', 'attachment', filename='bud.gif')\n
\n
\n

The above will add a header that looks like this:

\n
Content-Disposition: attachment; filename=\"bud.gif\"
\n
\n
\n\n
\n\n
\n
\n

20.4.3. wsgiref.simple_server – a simple WSGI HTTP server

\n

This module implements a simple HTTP server (based on BaseHTTPServer)\nthat serves WSGI applications. Each server instance serves a single WSGI\napplication on a given host and port. If you want to serve multiple\napplications on a single host and port, you should create a WSGI application\nthat parses PATH_INFO to select which application to invoke for each\nrequest. (E.g., using the shift_path_info() function from\nwsgiref.util.)

\n
\n
\nwsgiref.simple_server.make_server(host, port, app[, server_class=WSGIServer[, handler_class=WSGIRequestHandler]])
\n

Create a new WSGI server listening on host and port, accepting connections\nfor app. The return value is an instance of the supplied server_class, and\nwill process requests using the specified handler_class. app must be a WSGI\napplication object, as defined by PEP 333.

\n

Example usage:

\n
from wsgiref.simple_server import make_server, demo_app\n\nhttpd = make_server('', 8000, demo_app)\nprint "Serving HTTP on port 8000..."\n\n# Respond to requests until process is killed\nhttpd.serve_forever()\n\n# Alternative: serve one request, then exit\nhttpd.handle_request()\n
\n
\n
\n\n
\n
\nwsgiref.simple_server.demo_app(environ, start_response)
\n
This function is a small but complete WSGI application that returns a text page\ncontaining the message “Hello world!” and a list of the key/value pairs provided\nin the environ parameter. It’s useful for verifying that a WSGI server (such\nas wsgiref.simple_server) is able to run a simple WSGI application\ncorrectly.
\n\n
\n
\nclass wsgiref.simple_server.WSGIServer(server_address, RequestHandlerClass)
\n

Create a WSGIServer instance. server_address should be a\n(host,port) tuple, and RequestHandlerClass should be the subclass of\nBaseHTTPServer.BaseHTTPRequestHandler that will be used to process\nrequests.

\n

You do not normally need to call this constructor, as the make_server()\nfunction can handle all the details for you.

\n

WSGIServer is a subclass of BaseHTTPServer.HTTPServer, so all\nof its methods (such as serve_forever() and handle_request()) are\navailable. WSGIServer also provides these WSGI-specific methods:

\n
\n
\nset_app(application)
\n
Sets the callable application as the WSGI application that will receive\nrequests.
\n\n
\n
\nget_app()
\n
Returns the currently-set application callable.
\n\n

Normally, however, you do not need to use these additional methods, as\nset_app() is normally called by make_server(), and the\nget_app() exists mainly for the benefit of request handler instances.

\n
\n\n
\n
\nclass wsgiref.simple_server.WSGIRequestHandler(request, client_address, server)
\n

Create an HTTP handler for the given request (i.e. a socket), client_address\n(a (host,port) tuple), and server (WSGIServer instance).

\n

You do not need to create instances of this class directly; they are\nautomatically created as needed by WSGIServer objects. You can,\nhowever, subclass this class and supply it as a handler_class to the\nmake_server() function. Some possibly relevant methods for overriding in\nsubclasses:

\n
\n
\nget_environ()
\n
Returns a dictionary containing the WSGI environment for a request. The default\nimplementation copies the contents of the WSGIServer object’s\nbase_environ dictionary attribute and then adds various headers derived\nfrom the HTTP request. Each call to this method should return a new dictionary\ncontaining all of the relevant CGI environment variables as specified in\nPEP 333.
\n\n
\n
\nget_stderr()
\n
Return the object that should be used as the wsgi.errors stream. The default\nimplementation just returns sys.stderr.
\n\n
\n
\nhandle()
\n
Process the HTTP request. The default implementation creates a handler instance\nusing a wsgiref.handlers class to implement the actual WSGI application\ninterface.
\n\n
\n\n
\n
\n

20.4.4. wsgiref.validate — WSGI conformance checker

\n

When creating new WSGI application objects, frameworks, servers, or middleware,\nit can be useful to validate the new code’s conformance using\nwsgiref.validate. This module provides a function that creates WSGI\napplication objects that validate communications between a WSGI server or\ngateway and a WSGI application object, to check both sides for protocol\nconformance.

\n

Note that this utility does not guarantee complete PEP 333 compliance; an\nabsence of errors from this module does not necessarily mean that errors do not\nexist. However, if this module does produce an error, then it is virtually\ncertain that either the server or application is not 100% compliant.

\n

This module is based on the paste.lint module from Ian Bicking’s “Python\nPaste” library.

\n
\n
\nwsgiref.validate.validator(application)
\n

Wrap application and return a new WSGI application object. The returned\napplication will forward all requests to the original application, and will\ncheck that both the application and the server invoking it are conforming to\nthe WSGI specification and to RFC 2616.

\n

Any detected nonconformance results in an AssertionError being raised;\nnote, however, that how these errors are handled is server-dependent. For\nexample, wsgiref.simple_server and other servers based on\nwsgiref.handlers (that don’t override the error handling methods to do\nsomething else) will simply output a message that an error has occurred, and\ndump the traceback to sys.stderr or some other error stream.

\n

This wrapper may also generate output using the warnings module to\nindicate behaviors that are questionable but which may not actually be\nprohibited by PEP 333. Unless they are suppressed using Python command-line\noptions or the warnings API, any such warnings will be written to\nsys.stderr (not wsgi.errors, unless they happen to be the same\nobject).

\n

Example usage:

\n
from wsgiref.validate import validator\nfrom wsgiref.simple_server import make_server\n\n# Our callable object which is intentionally not compliant to the\n# standard, so the validator is going to break\ndef simple_app(environ, start_response):\n    status = '200 OK' # HTTP Status\n    headers = [('Content-type', 'text/plain')] # HTTP Headers\n    start_response(status, headers)\n\n    # This is going to break because we need to return a list, and\n    # the validator is going to inform us\n    return "Hello World"\n\n# This is the application wrapped in a validator\nvalidator_app = validator(simple_app)\n\nhttpd = make_server('', 8000, validator_app)\nprint "Listening on port 8000...."\nhttpd.serve_forever()\n
\n
\n
\n\n
\n
\n

20.4.5. wsgiref.handlers – server/gateway base classes

\n

This module provides base handler classes for implementing WSGI servers and\ngateways. These base classes handle most of the work of communicating with a\nWSGI application, as long as they are given a CGI-like environment, along with\ninput, output, and error streams.

\n
\n
\nclass wsgiref.handlers.CGIHandler
\n

CGI-based invocation via sys.stdin, sys.stdout, sys.stderr and\nos.environ. This is useful when you have a WSGI application and want to run\nit as a CGI script. Simply invoke CGIHandler().run(app), where app is\nthe WSGI application object you wish to invoke.

\n

This class is a subclass of BaseCGIHandler that sets wsgi.run_once\nto true, wsgi.multithread to false, and wsgi.multiprocess to true, and\nalways uses sys and os to obtain the necessary CGI streams and\nenvironment.

\n
\n\n
\n
\nclass wsgiref.handlers.BaseCGIHandler(stdin, stdout, stderr, environ[, multithread=True[, multiprocess=False]])
\n

Similar to CGIHandler, but instead of using the sys and\nos modules, the CGI environment and I/O streams are specified explicitly.\nThe multithread and multiprocess values are used to set the\nwsgi.multithread and wsgi.multiprocess flags for any applications run by\nthe handler instance.

\n

This class is a subclass of SimpleHandler intended for use with\nsoftware other than HTTP “origin servers”. If you are writing a gateway\nprotocol implementation (such as CGI, FastCGI, SCGI, etc.) that uses a\nStatus: header to send an HTTP status, you probably want to subclass this\ninstead of SimpleHandler.

\n
\n\n
\n
\nclass wsgiref.handlers.SimpleHandler(stdin, stdout, stderr, environ[, multithread=True[, multiprocess=False]])
\n

Similar to BaseCGIHandler, but designed for use with HTTP origin\nservers. If you are writing an HTTP server implementation, you will probably\nwant to subclass this instead of BaseCGIHandler

\n

This class is a subclass of BaseHandler. It overrides the\n__init__(), get_stdin(), get_stderr(), add_cgi_vars(),\n_write(), and _flush() methods to support explicitly setting the\nenvironment and streams via the constructor. The supplied environment and\nstreams are stored in the stdin, stdout, stderr, and\nenviron attributes.

\n
\n\n
\n
\nclass wsgiref.handlers.BaseHandler
\n

This is an abstract base class for running WSGI applications. Each instance\nwill handle a single HTTP request, although in principle you could create a\nsubclass that was reusable for multiple requests.

\n

BaseHandler instances have only one method intended for external use:

\n
\n
\nrun(app)
\n
Run the specified WSGI application, app.
\n\n

All of the other BaseHandler methods are invoked by this method in the\nprocess of running the application, and thus exist primarily to allow\ncustomizing the process.

\n

The following methods MUST be overridden in a subclass:

\n
\n
\n_write(data)
\n
Buffer the string data for transmission to the client. It’s okay if this\nmethod actually transmits the data; BaseHandler just separates write\nand flush operations for greater efficiency when the underlying system actually\nhas such a distinction.
\n\n
\n
\n_flush()
\n
Force buffered data to be transmitted to the client. It’s okay if this method\nis a no-op (i.e., if _write() actually sends the data).
\n\n
\n
\nget_stdin()
\n
Return an input stream object suitable for use as the wsgi.input of the\nrequest currently being processed.
\n\n
\n
\nget_stderr()
\n
Return an output stream object suitable for use as the wsgi.errors of the\nrequest currently being processed.
\n\n
\n
\nadd_cgi_vars()
\n
Insert CGI variables for the current request into the environ attribute.
\n\n

Here are some other methods and attributes you may wish to override. This list\nis only a summary, however, and does not include every method that can be\noverridden. You should consult the docstrings and source code for additional\ninformation before attempting to create a customized BaseHandler\nsubclass.

\n

Attributes and methods for customizing the WSGI environment:

\n
\n
\nwsgi_multithread
\n
The value to be used for the wsgi.multithread environment variable. It\ndefaults to true in BaseHandler, but may have a different default (or\nbe set by the constructor) in the other subclasses.
\n\n
\n
\nwsgi_multiprocess
\n
The value to be used for the wsgi.multiprocess environment variable. It\ndefaults to true in BaseHandler, but may have a different default (or\nbe set by the constructor) in the other subclasses.
\n\n
\n
\nwsgi_run_once
\n
The value to be used for the wsgi.run_once environment variable. It\ndefaults to false in BaseHandler, but CGIHandler sets it to\ntrue by default.
\n\n
\n
\nos_environ
\n
The default environment variables to be included in every request’s WSGI\nenvironment. By default, this is a copy of os.environ at the time that\nwsgiref.handlers was imported, but subclasses can either create their own\nat the class or instance level. Note that the dictionary should be considered\nread-only, since the default value is shared between multiple classes and\ninstances.
\n\n
\n
\nserver_software
\n
If the origin_server attribute is set, this attribute’s value is used to\nset the default SERVER_SOFTWARE WSGI environment variable, and also to set a\ndefault Server: header in HTTP responses. It is ignored for handlers (such\nas BaseCGIHandler and CGIHandler) that are not HTTP origin\nservers.
\n\n
\n
\nget_scheme()
\n
Return the URL scheme being used for the current request. The default\nimplementation uses the guess_scheme() function from wsgiref.util\nto guess whether the scheme should be “http” or “https”, based on the current\nrequest’s environ variables.
\n\n
\n
\nsetup_environ()
\n
Set the environ attribute to a fully-populated WSGI environment. The\ndefault implementation uses all of the above methods and attributes, plus the\nget_stdin(), get_stderr(), and add_cgi_vars() methods and the\nwsgi_file_wrapper attribute. It also inserts a SERVER_SOFTWARE key\nif not present, as long as the origin_server attribute is a true value\nand the server_software attribute is set.
\n\n

Methods and attributes for customizing exception handling:

\n
\n
\nlog_exception(exc_info)
\n
Log the exc_info tuple in the server log. exc_info is a (type, value,\ntraceback) tuple. The default implementation simply writes the traceback to\nthe request’s wsgi.errors stream and flushes it. Subclasses can override\nthis method to change the format or retarget the output, mail the traceback to\nan administrator, or whatever other action may be deemed suitable.
\n\n
\n
\ntraceback_limit
\n
The maximum number of frames to include in tracebacks output by the default\nlog_exception() method. If None, all frames are included.
\n\n
\n
\nerror_output(environ, start_response)
\n

This method is a WSGI application to generate an error page for the user. It is\nonly invoked if an error occurs before headers are sent to the client.

\n

This method can access the current error information using sys.exc_info(),\nand should pass that information to start_response when calling it (as\ndescribed in the “Error Handling” section of PEP 333).

\n

The default implementation just uses the error_status,\nerror_headers, and error_body attributes to generate an output\npage. Subclasses can override this to produce more dynamic error output.

\n

Note, however, that it’s not recommended from a security perspective to spit out\ndiagnostics to any old user; ideally, you should have to do something special to\nenable diagnostic output, which is why the default implementation doesn’t\ninclude any.

\n
\n\n
\n
\nerror_status
\n
The HTTP status used for error responses. This should be a status string as\ndefined in PEP 333; it defaults to a 500 code and message.
\n\n
\n
\nerror_headers
\n
The HTTP headers used for error responses. This should be a list of WSGI\nresponse headers ((name, value) tuples), as described in PEP 333. The\ndefault list just sets the content type to text/plain.
\n\n
\n
\nerror_body
\n
The error response body. This should be an HTTP response body string. It\ndefaults to the plain text, “A server error occurred. Please contact the\nadministrator.”
\n\n

Methods and attributes for PEP 333‘s “Optional Platform-Specific File\nHandling” feature:

\n
\n
\nwsgi_file_wrapper
\n
A wsgi.file_wrapper factory, or None. The default value of this\nattribute is the FileWrapper class from wsgiref.util.
\n\n
\n
\nsendfile()
\n
Override to implement platform-specific file transmission. This method is\ncalled only if the application’s return value is an instance of the class\nspecified by the wsgi_file_wrapper attribute. It should return a true\nvalue if it was able to successfully transmit the file, so that the default\ntransmission code will not be executed. The default implementation of this\nmethod just returns a false value.
\n\n

Miscellaneous methods and attributes:

\n
\n
\norigin_server
\n

This attribute should be set to a true value if the handler’s _write() and\n_flush() are being used to communicate directly to the client, rather than\nvia a CGI-like gateway protocol that wants the HTTP status in a special\nStatus: header.

\n

This attribute’s default value is true in BaseHandler, but false in\nBaseCGIHandler and CGIHandler.

\n
\n\n
\n
\nhttp_version
\n
If origin_server is true, this string attribute is used to set the HTTP\nversion of the response set to the client. It defaults to "1.0".
\n\n
\n\n
\n
\n

20.4.6. Examples

\n

This is a working “Hello World” WSGI application:

\n
from wsgiref.simple_server import make_server\n\n# Every WSGI application must have an application object - a callable\n# object that accepts two arguments. For that purpose, we're going to\n# use a function (note that you're not limited to a function, you can\n# use a class for example). The first argument passed to the function\n# is a dictionary containing CGI-style envrironment variables and the\n# second variable is the callable object (see PEP 333).\ndef hello_world_app(environ, start_response):\n    status = '200 OK' # HTTP Status\n    headers = [('Content-type', 'text/plain')] # HTTP Headers\n    start_response(status, headers)\n\n    # The returned object is going to be printed\n    return ["Hello World"]\n\nhttpd = make_server('', 8000, hello_world_app)\nprint "Serving on port 8000..."\n\n# Serve until process is killed\nhttpd.serve_forever()\n
\n
\n
\n
", "searchableItems": [ { "name": "wsgiref.handlers.BaseCGIHandler", "domId": "wsgiref_wsgiref.handlers.BaseCGIHandler" }, { "name": "wsgiref.handlers.BaseHandler", "domId": "wsgiref_wsgiref.handlers.BaseHandler" }, { "name": "wsgiref.handlers.BaseHandler._flush", "domId": "wsgiref_wsgiref.handlers.BaseHandler._flush" }, { "name": "wsgiref.handlers.BaseHandler._write", "domId": "wsgiref_wsgiref.handlers.BaseHandler._write" }, { "name": "wsgiref.handlers.BaseHandler.add_cgi_vars", "domId": "wsgiref_wsgiref.handlers.BaseHandler.add_cgi_vars" }, { "name": "wsgiref.handlers.BaseHandler.error_output", "domId": "wsgiref_wsgiref.handlers.BaseHandler.error_output" }, { "name": "wsgiref.handlers.BaseHandler.get_scheme", "domId": "wsgiref_wsgiref.handlers.BaseHandler.get_scheme" }, { "name": "wsgiref.handlers.BaseHandler.get_stderr", "domId": "wsgiref_wsgiref.handlers.BaseHandler.get_stderr" }, { "name": "wsgiref.handlers.BaseHandler.get_stdin", "domId": "wsgiref_wsgiref.handlers.BaseHandler.get_stdin" }, { "name": "wsgiref.handlers.BaseHandler.log_exception", "domId": "wsgiref_wsgiref.handlers.BaseHandler.log_exception" }, { "name": "wsgiref.handlers.BaseHandler.run", "domId": "wsgiref_wsgiref.handlers.BaseHandler.run" }, { "name": "wsgiref.handlers.BaseHandler.sendfile", "domId": "wsgiref_wsgiref.handlers.BaseHandler.sendfile" }, { "name": "wsgiref.handlers.BaseHandler.setup_environ", "domId": "wsgiref_wsgiref.handlers.BaseHandler.setup_environ" }, { "name": "wsgiref.handlers.CGIHandler", "domId": "wsgiref_wsgiref.handlers.CGIHandler" }, { "name": "wsgiref.handlers.SimpleHandler", "domId": "wsgiref_wsgiref.handlers.SimpleHandler" }, { "name": "wsgiref.headers.Headers", "domId": "wsgiref_wsgiref.headers.Headers" }, { "name": "wsgiref.headers.Headers.add_header", "domId": "wsgiref_wsgiref.headers.Headers.add_header" }, { "name": "wsgiref.headers.Headers.get_all", "domId": "wsgiref_wsgiref.headers.Headers.get_all" }, { "name": "wsgiref.simple_server.demo_app", "domId": "wsgiref_wsgiref.simple_server.demo_app" }, { "name": "wsgiref.simple_server.make_server", "domId": "wsgiref_wsgiref.simple_server.make_server" }, { "name": "wsgiref.simple_server.WSGIRequestHandler", "domId": "wsgiref_wsgiref.simple_server.WSGIRequestHandler" }, { "name": "wsgiref.simple_server.WSGIRequestHandler.get_environ", "domId": "wsgiref_wsgiref.simple_server.WSGIRequestHandler.get_environ" }, { "name": "wsgiref.simple_server.WSGIRequestHandler.get_stderr", "domId": "wsgiref_wsgiref.simple_server.WSGIRequestHandler.get_stderr" }, { "name": "wsgiref.simple_server.WSGIRequestHandler.handle", "domId": "wsgiref_wsgiref.simple_server.WSGIRequestHandler.handle" }, { "name": "wsgiref.simple_server.WSGIServer", "domId": "wsgiref_wsgiref.simple_server.WSGIServer" }, { "name": "wsgiref.simple_server.WSGIServer.get_app", "domId": "wsgiref_wsgiref.simple_server.WSGIServer.get_app" }, { "name": "wsgiref.simple_server.WSGIServer.set_app", "domId": "wsgiref_wsgiref.simple_server.WSGIServer.set_app" }, { "name": "wsgiref.util.application_uri", "domId": "wsgiref_wsgiref.util.application_uri" }, { "name": "wsgiref.util.FileWrapper", "domId": "wsgiref_wsgiref.util.FileWrapper" }, { "name": "wsgiref.util.guess_scheme", "domId": "wsgiref_wsgiref.util.guess_scheme" }, { "name": "wsgiref.util.is_hop_by_hop", "domId": "wsgiref_wsgiref.util.is_hop_by_hop" }, { "name": "wsgiref.util.request_uri", "domId": "wsgiref_wsgiref.util.request_uri" }, { "name": "wsgiref.util.setup_testing_defaults", "domId": "wsgiref_wsgiref.util.setup_testing_defaults" }, { "name": "wsgiref.util.shift_path_info", "domId": "wsgiref_wsgiref.util.shift_path_info" }, { "name": "wsgiref.validate.validator", "domId": "wsgiref_wsgiref.validate.validator" } ] }, { "url": "http://docs.python.org/library/ftplib.html", "title": "ftplib", "html": "
\n

20.8. ftplib — FTP protocol client

\n

Source code: Lib/ftplib.py

\n
\n

This module defines the class FTP and a few related items. The\nFTP class implements the client side of the FTP protocol. You can use\nthis to write Python programs that perform a variety of automated FTP jobs, such\nas mirroring other ftp servers. It is also used by the module urllib to\nhandle URLs that use FTP. For more information on FTP (File Transfer Protocol),\nsee Internet RFC 959.

\n

Here’s a sample session using the ftplib module:

\n
>>> from ftplib import FTP\n>>> ftp = FTP('ftp.cwi.nl')   # connect to host, default port\n>>> ftp.login()               # user anonymous, passwd anonymous@\n>>> ftp.retrlines('LIST')     # list directory contents\ntotal 24418\ndrwxrwsr-x   5 ftp-usr  pdmaint     1536 Mar 20 09:48 .\ndr-xr-srwt 105 ftp-usr  pdmaint     1536 Mar 21 14:32 ..\n-rw-r--r--   1 ftp-usr  pdmaint     5305 Mar 20 09:48 INDEX\n .\n .\n .\n>>> ftp.retrbinary('RETR README', open('README', 'wb').write)\n'226 Transfer complete.'\n>>> ftp.quit()\n
\n
\n

The module defines the following items:

\n
\n
\nclass ftplib.FTP([host[, user[, passwd[, acct[, timeout]]]]])
\n

Return a new instance of the FTP class. When host is given, the\nmethod call connect(host) is made. When user is given, additionally\nthe method call login(user, passwd, acct) is made (where passwd and\nacct default to the empty string when not given). The optional timeout\nparameter specifies a timeout in seconds for blocking operations like the\nconnection attempt (if is not specified, the global default timeout setting\nwill be used).

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n
\nclass ftplib.FTP_TLS([host[, user[, passwd[, acct[, keyfile[, certfile[, timeout]]]]]]])
\n

A FTP subclass which adds TLS support to FTP as described in\nRFC 4217.\nConnect as usual to port 21 implicitly securing the FTP control connection\nbefore authenticating. Securing the data connection requires the user to\nexplicitly ask for it by calling the prot_p() method.\nkeyfile and certfile are optional – they can contain a PEM formatted\nprivate key and certificate chain file name for the SSL connection.

\n

\nNew in version 2.7.

\n

Here’s a sample session using the FTP_TLS class:

\n
>>> from ftplib import FTP_TLS\n>>> ftps = FTP_TLS('ftp.python.org')\n>>> ftps.login()           # login anonymously before securing control channel\n>>> ftps.prot_p()          # switch to secure data connection\n>>> ftps.retrlines('LIST') # list directory content securely\ntotal 9\ndrwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .\ndrwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..\ndrwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin\ndrwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc\nd-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming\ndrwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib\ndrwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub\ndrwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr\n-rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg\n'226 Transfer complete.'\n>>> ftps.quit()\n>>>\n
\n
\n
\n\n
\n
\nexception ftplib.error_reply
\n
Exception raised when an unexpected reply is received from the server.
\n\n
\n
\nexception ftplib.error_temp
\n
Exception raised when an error code signifying a temporary error (response\ncodes in the range 400–499) is received.
\n\n
\n
\nexception ftplib.error_perm
\n
Exception raised when an error code signifying a permanent error (response\ncodes in the range 500–599) is received.
\n\n
\n
\nexception ftplib.error_proto
\n
Exception raised when a reply is received from the server that does not fit\nthe response specifications of the File Transfer Protocol, i.e. begin with a\ndigit in the range 1–5.
\n\n
\n
\nftplib.all_errors
\n
The set of all exceptions (as a tuple) that methods of FTP\ninstances may raise as a result of problems with the FTP connection (as\nopposed to programming errors made by the caller). This set includes the\nfour exceptions listed above as well as socket.error and\nIOError.
\n\n
\n

See also

\n
\n
Module netrc
\n
Parser for the .netrc file format. The file .netrc is\ntypically used by FTP clients to load user authentication information\nbefore prompting the user.
\n
\n

The file Tools/scripts/ftpmirror.py in the Python source distribution is\na script that can mirror FTP sites, or portions thereof, using the ftplib\nmodule. It can be used as an extended example that applies this module.

\n
\n
\n

20.8.1. FTP Objects

\n

Several methods are available in two flavors: one for handling text files and\nanother for binary files. These are named for the command which is used\nfollowed by lines for the text version or binary for the binary version.

\n

FTP instances have the following methods:

\n
\n
\nFTP.set_debuglevel(level)
\n
Set the instance’s debugging level. This controls the amount of debugging\noutput printed. The default, 0, produces no debugging output. A value of\n1 produces a moderate amount of debugging output, generally a single line\nper request. A value of 2 or higher produces the maximum amount of\ndebugging output, logging each line sent and received on the control connection.
\n\n
\n
\nFTP.connect(host[, port[, timeout]])
\n

Connect to the given host and port. The default port number is 21, as\nspecified by the FTP protocol specification. It is rarely needed to specify a\ndifferent port number. This function should be called only once for each\ninstance; it should not be called at all if a host was given when the instance\nwas created. All other methods can only be used after a connection has been\nmade.

\n

The optional timeout parameter specifies a timeout in seconds for the\nconnection attempt. If no timeout is passed, the global default timeout\nsetting will be used.

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n
\nFTP.getwelcome()
\n
Return the welcome message sent by the server in reply to the initial\nconnection. (This message sometimes contains disclaimers or help information\nthat may be relevant to the user.)
\n\n
\n
\nFTP.login([user[, passwd[, acct]]])
\n
Log in as the given user. The passwd and acct parameters are optional and\ndefault to the empty string. If no user is specified, it defaults to\n'anonymous'. If user is 'anonymous', the default passwd is\n'anonymous@'. This function should be called only once for each instance,\nafter a connection has been established; it should not be called at all if a\nhost and user were given when the instance was created. Most FTP commands are\nonly allowed after the client has logged in. The acct parameter supplies\n“accounting information”; few systems implement this.
\n\n
\n
\nFTP.abort()
\n
Abort a file transfer that is in progress. Using this does not always work, but\nit’s worth a try.
\n\n
\n
\nFTP.sendcmd(command)
\n
Send a simple command string to the server and return the response string.
\n\n
\n
\nFTP.voidcmd(command)
\n
Send a simple command string to the server and handle the response. Return\nnothing if a response code corresponding to success (codes in the range\n200–299) is received. Raise error_reply otherwise.
\n\n
\n
\nFTP.retrbinary(command, callback[, maxblocksize[, rest]])
\n
Retrieve a file in binary transfer mode. command should be an appropriate\nRETR command: 'RETR filename'. The callback function is called for\neach block of data received, with a single string argument giving the data\nblock. The optional maxblocksize argument specifies the maximum chunk size to\nread on the low-level socket object created to do the actual transfer (which\nwill also be the largest size of the data blocks passed to callback). A\nreasonable default is chosen. rest means the same thing as in the\ntransfercmd() method.
\n\n
\n
\nFTP.retrlines(command[, callback])
\n
Retrieve a file or directory listing in ASCII transfer mode. command\nshould be an appropriate RETR command (see retrbinary()) or a\ncommand such as LIST, NLST or MLSD (usually just the string\n'LIST'). LIST retrieves a list of files and information about those files.\nNLST retrieves a list of file names. On some servers, MLSD retrieves\na machine readable list of files and information about those files. The callback\nfunction is called for each line with a string argument containing the line with\nthe trailing CRLF stripped. The default callback prints the line to sys.stdout.
\n\n
\n
\nFTP.set_pasv(boolean)
\n
Enable “passive” mode if boolean is true, other disable passive mode. (In\nPython 2.0 and before, passive mode was off by default; in Python 2.1 and later,\nit is on by default.)
\n\n
\n
\nFTP.storbinary(command, file[, blocksize, callback, rest])
\n

Store a file in binary transfer mode. command should be an appropriate\nSTOR command: "STOR filename". file is an open file object which is\nread until EOF using its read() method in blocks of size blocksize to\nprovide the data to be stored. The blocksize argument defaults to 8192.\ncallback is an optional single parameter callable that is called\non each block of data after it is sent. rest means the same thing as in\nthe transfercmd() method.

\n

\nChanged in version 2.1: default for blocksize added.

\n

\nChanged in version 2.6: callback parameter added.

\n

\nChanged in version 2.7: rest parameter added.

\n
\n\n
\n
\nFTP.storlines(command, file[, callback])
\n

Store a file in ASCII transfer mode. command should be an appropriate\nSTOR command (see storbinary()). Lines are read until EOF from the\nopen file object file using its readline() method to provide the data to\nbe stored. callback is an optional single parameter callable\nthat is called on each line after it is sent.

\n

\nChanged in version 2.6: callback parameter added.

\n
\n\n
\n
\nFTP.transfercmd(cmd[, rest])
\n

Initiate a transfer over the data connection. If the transfer is active, send a\nEPRT or PORT command and the transfer command specified by cmd, and\naccept the connection. If the server is passive, send a EPSV or PASV\ncommand, connect to it, and start the transfer command. Either way, return the\nsocket for the connection.

\n

If optional rest is given, a REST command is sent to the server, passing\nrest as an argument. rest is usually a byte offset into the requested file,\ntelling the server to restart sending the file’s bytes at the requested offset,\nskipping over the initial bytes. Note however that RFC 959 requires only that\nrest be a string containing characters in the printable range from ASCII code\n33 to ASCII code 126. The transfercmd() method, therefore, converts\nrest to a string, but no check is performed on the string’s contents. If the\nserver does not recognize the REST command, an error_reply exception\nwill be raised. If this happens, simply call transfercmd() without a\nrest argument.

\n
\n\n
\n
\nFTP.ntransfercmd(cmd[, rest])
\n
Like transfercmd(), but returns a tuple of the data connection and the\nexpected size of the data. If the expected size could not be computed, None\nwill be returned as the expected size. cmd and rest means the same thing as\nin transfercmd().
\n\n
\n
\nFTP.nlst(argument[, ...])
\n
Return a list of file names as returned by the NLST command. The\noptional argument is a directory to list (default is the current server\ndirectory). Multiple arguments can be used to pass non-standard options to\nthe NLST command.
\n\n
\n
\nFTP.dir(argument[, ...])
\n
Produce a directory listing as returned by the LIST command, printing it to\nstandard output. The optional argument is a directory to list (default is the\ncurrent server directory). Multiple arguments can be used to pass non-standard\noptions to the LIST command. If the last argument is a function, it is used\nas a callback function as for retrlines(); the default prints to\nsys.stdout. This method returns None.
\n\n
\n
\nFTP.rename(fromname, toname)
\n
Rename file fromname on the server to toname.
\n\n
\n
\nFTP.delete(filename)
\n
Remove the file named filename from the server. If successful, returns the\ntext of the response, otherwise raises error_perm on permission errors or\nerror_reply on other errors.
\n\n
\n
\nFTP.cwd(pathname)
\n
Set the current directory on the server.
\n\n
\n
\nFTP.mkd(pathname)
\n
Create a new directory on the server.
\n\n
\n
\nFTP.pwd()
\n
Return the pathname of the current directory on the server.
\n\n
\n
\nFTP.rmd(dirname)
\n
Remove the directory named dirname on the server.
\n\n
\n
\nFTP.size(filename)
\n
Request the size of the file named filename on the server. On success, the\nsize of the file is returned as an integer, otherwise None is returned.\nNote that the SIZE command is not standardized, but is supported by many\ncommon server implementations.
\n\n
\n
\nFTP.quit()
\n
Send a QUIT command to the server and close the connection. This is the\n“polite” way to close a connection, but it may raise an exception if the server\nresponds with an error to the QUIT command. This implies a call to the\nclose() method which renders the FTP instance useless for\nsubsequent calls (see below).
\n\n
\n
\nFTP.close()
\n
Close the connection unilaterally. This should not be applied to an already\nclosed connection such as after a successful call to quit(). After this\ncall the FTP instance should not be used any more (after a call to\nclose() or quit() you cannot reopen the connection by issuing\nanother login() method).
\n\n
\n
\n

20.8.2. FTP_TLS Objects

\n

FTP_TLS class inherits from FTP, defining these additional objects:

\n
\n
\nFTP_TLS.ssl_version
\n
The SSL version to use (defaults to TLSv1).
\n\n
\n
\nFTP_TLS.auth()
\n
Set up secure control connection by using TLS or SSL, depending on what specified in ssl_version() attribute.
\n\n
\n
\nFTP_TLS.prot_p()
\n
Set up secure data connection.
\n\n
\n
\nFTP_TLS.prot_c()
\n
Set up clear text data connection.
\n\n
\n
", "searchableItems": [ { "name": "ftplib.FTP", "domId": "ftplib_ftplib.FTP" }, { "name": "ftplib.FTP.abort", "domId": "ftplib_ftplib.FTP.abort" }, { "name": "ftplib.FTP.close", "domId": "ftplib_ftplib.FTP.close" }, { "name": "ftplib.FTP.connect", "domId": "ftplib_ftplib.FTP.connect" }, { "name": "ftplib.FTP.cwd", "domId": "ftplib_ftplib.FTP.cwd" }, { "name": "ftplib.FTP.delete", "domId": "ftplib_ftplib.FTP.delete" }, { "name": "ftplib.FTP.dir", "domId": "ftplib_ftplib.FTP.dir" }, { "name": "ftplib.FTP.getwelcome", "domId": "ftplib_ftplib.FTP.getwelcome" }, { "name": "ftplib.FTP.login", "domId": "ftplib_ftplib.FTP.login" }, { "name": "ftplib.FTP.mkd", "domId": "ftplib_ftplib.FTP.mkd" }, { "name": "ftplib.FTP.nlst", "domId": "ftplib_ftplib.FTP.nlst" }, { "name": "ftplib.FTP.ntransfercmd", "domId": "ftplib_ftplib.FTP.ntransfercmd" }, { "name": "ftplib.FTP.pwd", "domId": "ftplib_ftplib.FTP.pwd" }, { "name": "ftplib.FTP.quit", "domId": "ftplib_ftplib.FTP.quit" }, { "name": "ftplib.FTP.rename", "domId": "ftplib_ftplib.FTP.rename" }, { "name": "ftplib.FTP.retrbinary", "domId": "ftplib_ftplib.FTP.retrbinary" }, { "name": "ftplib.FTP.retrlines", "domId": "ftplib_ftplib.FTP.retrlines" }, { "name": "ftplib.FTP.rmd", "domId": "ftplib_ftplib.FTP.rmd" }, { "name": "ftplib.FTP.sendcmd", "domId": "ftplib_ftplib.FTP.sendcmd" }, { "name": "ftplib.FTP.set_debuglevel", "domId": "ftplib_ftplib.FTP.set_debuglevel" }, { "name": "ftplib.FTP.set_pasv", "domId": "ftplib_ftplib.FTP.set_pasv" }, { "name": "ftplib.FTP.size", "domId": "ftplib_ftplib.FTP.size" }, { "name": "ftplib.FTP.storbinary", "domId": "ftplib_ftplib.FTP.storbinary" }, { "name": "ftplib.FTP.storlines", "domId": "ftplib_ftplib.FTP.storlines" }, { "name": "ftplib.FTP.transfercmd", "domId": "ftplib_ftplib.FTP.transfercmd" }, { "name": "ftplib.FTP.voidcmd", "domId": "ftplib_ftplib.FTP.voidcmd" }, { "name": "ftplib.FTP_TLS", "domId": "ftplib_ftplib.FTP_TLS" }, { "name": "ftplib.FTP_TLS.auth", "domId": "ftplib_ftplib.FTP_TLS.auth" }, { "name": "ftplib.FTP_TLS.prot_c", "domId": "ftplib_ftplib.FTP_TLS.prot_c" }, { "name": "ftplib.FTP_TLS.prot_p", "domId": "ftplib_ftplib.FTP_TLS.prot_p" } ] }, { "url": "http://docs.python.org/library/httplib.html", "title": "httplib", "html": "
\n

20.7. httplib — HTTP protocol client

\n
\n

Note

\n

The httplib module has been renamed to http.client in Python\n3.0. The 2to3 tool will automatically adapt imports when converting\nyour sources to 3.0.

\n
\n

Source code: Lib/httplib.py

\n
\n

This module defines classes which implement the client side of the HTTP and\nHTTPS protocols. It is normally not used directly — the module urllib\nuses it to handle URLs that use HTTP and HTTPS.

\n
\n

Note

\n

HTTPS support is only available if the socket module was compiled with\nSSL support.

\n
\n
\n

Note

\n

The public interface for this module changed substantially in Python 2.0. The\nHTTP class is retained only for backward compatibility with 1.5.2. It\nshould not be used in new code. Refer to the online docstrings for usage.

\n
\n

The module provides the following classes:

\n
\n
\nclass httplib.HTTPConnection(host[, port[, strict[, timeout[, source_address]]]])
\n

An HTTPConnection instance represents one transaction with an HTTP\nserver. It should be instantiated passing it a host and optional port\nnumber. If no port number is passed, the port is extracted from the host\nstring if it has the form host:port, else the default HTTP port (80) is\nused. When True, the optional parameter strict (which defaults to a false\nvalue) causes BadStatusLine to\nbe raised if the status line can’t be parsed as a valid HTTP/1.0 or 1.1\nstatus line. If the optional timeout parameter is given, blocking\noperations (like connection attempts) will timeout after that many seconds\n(if it is not given, the global default timeout setting is used).\nThe optional source_address parameter may be a tuple of a (host, port)\nto use as the source address the HTTP connection is made from.

\n

For example, the following calls all create instances that connect to the server\nat the same host and port:

\n
>>> h1 = httplib.HTTPConnection('www.cwi.nl')\n>>> h2 = httplib.HTTPConnection('www.cwi.nl:80')\n>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)\n>>> h3 = httplib.HTTPConnection('www.cwi.nl', 80, timeout=10)\n
\n
\n

\nNew in version 2.0.

\n

\nChanged in version 2.6: timeout was added.

\n

\nChanged in version 2.7: source_address was added.

\n
\n\n
\n
\nclass httplib.HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout[, source_address]]]]]])
\n

A subclass of HTTPConnection that uses SSL for communication with\nsecure servers. Default port is 443. key_file is the name of a PEM\nformatted file that contains your private key. cert_file is a PEM formatted\ncertificate chain file.

\n
\n

Warning

\n

This does not do any verification of the server’s certificate.

\n
\n

\nNew in version 2.0.

\n

\nChanged in version 2.6: timeout was added.

\n

\nChanged in version 2.7: source_address was added.

\n
\n\n
\n
\nclass httplib.HTTPResponse(sock[, debuglevel=0][, strict=0])
\n

Class whose instances are returned upon successful connection. Not instantiated\ndirectly by user.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nclass httplib.HTTPMessage
\n
An HTTPMessage instance is used to hold the headers from an HTTP\nresponse. It is implemented using the mimetools.Message class and\nprovides utility functions to deal with HTTP Headers. It is not directly\ninstantiated by the users.
\n\n

The following exceptions are raised as appropriate:

\n
\n
\nexception httplib.HTTPException
\n

The base class of the other exceptions in this module. It is a subclass of\nException.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.NotConnected
\n

A subclass of HTTPException.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.InvalidURL
\n

A subclass of HTTPException, raised if a port is given and is either\nnon-numeric or empty.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nexception httplib.UnknownProtocol
\n

A subclass of HTTPException.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.UnknownTransferEncoding
\n

A subclass of HTTPException.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.UnimplementedFileMode
\n

A subclass of HTTPException.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.IncompleteRead
\n

A subclass of HTTPException.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.ImproperConnectionState
\n

A subclass of HTTPException.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.CannotSendRequest
\n

A subclass of ImproperConnectionState.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.CannotSendHeader
\n

A subclass of ImproperConnectionState.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.ResponseNotReady
\n

A subclass of ImproperConnectionState.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nexception httplib.BadStatusLine
\n

A subclass of HTTPException. Raised if a server responds with a HTTP\nstatus code that we don’t understand.

\n

\nNew in version 2.0.

\n
\n\n

The constants defined in this module are:

\n
\n
\nhttplib.HTTP_PORT
\n
The default port for the HTTP protocol (always 80).
\n\n
\n
\nhttplib.HTTPS_PORT
\n
The default port for the HTTPS protocol (always 443).
\n\n

and also the following constants for integer status codes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantValueDefinition
CONTINUE100HTTP/1.1, RFC 2616, Section\n10.1.1
SWITCHING_PROTOCOLS101HTTP/1.1, RFC 2616, Section\n10.1.2
PROCESSING102WEBDAV, RFC 2518, Section 10.1
OK200HTTP/1.1, RFC 2616, Section\n10.2.1
CREATED201HTTP/1.1, RFC 2616, Section\n10.2.2
ACCEPTED202HTTP/1.1, RFC 2616, Section\n10.2.3
NON_AUTHORITATIVE_INFORMATION203HTTP/1.1, RFC 2616, Section\n10.2.4
NO_CONTENT204HTTP/1.1, RFC 2616, Section\n10.2.5
RESET_CONTENT205HTTP/1.1, RFC 2616, Section\n10.2.6
PARTIAL_CONTENT206HTTP/1.1, RFC 2616, Section\n10.2.7
MULTI_STATUS207WEBDAV RFC 2518, Section 10.2
IM_USED226Delta encoding in HTTP,\nRFC 3229, Section 10.4.1
MULTIPLE_CHOICES300HTTP/1.1, RFC 2616, Section\n10.3.1
MOVED_PERMANENTLY301HTTP/1.1, RFC 2616, Section\n10.3.2
FOUND302HTTP/1.1, RFC 2616, Section\n10.3.3
SEE_OTHER303HTTP/1.1, RFC 2616, Section\n10.3.4
NOT_MODIFIED304HTTP/1.1, RFC 2616, Section\n10.3.5
USE_PROXY305HTTP/1.1, RFC 2616, Section\n10.3.6
TEMPORARY_REDIRECT307HTTP/1.1, RFC 2616, Section\n10.3.8
BAD_REQUEST400HTTP/1.1, RFC 2616, Section\n10.4.1
UNAUTHORIZED401HTTP/1.1, RFC 2616, Section\n10.4.2
PAYMENT_REQUIRED402HTTP/1.1, RFC 2616, Section\n10.4.3
FORBIDDEN403HTTP/1.1, RFC 2616, Section\n10.4.4
NOT_FOUND404HTTP/1.1, RFC 2616, Section\n10.4.5
METHOD_NOT_ALLOWED405HTTP/1.1, RFC 2616, Section\n10.4.6
NOT_ACCEPTABLE406HTTP/1.1, RFC 2616, Section\n10.4.7
PROXY_AUTHENTICATION_REQUIRED407HTTP/1.1, RFC 2616, Section\n10.4.8
REQUEST_TIMEOUT408HTTP/1.1, RFC 2616, Section\n10.4.9
CONFLICT409HTTP/1.1, RFC 2616, Section\n10.4.10
GONE410HTTP/1.1, RFC 2616, Section\n10.4.11
LENGTH_REQUIRED411HTTP/1.1, RFC 2616, Section\n10.4.12
PRECONDITION_FAILED412HTTP/1.1, RFC 2616, Section\n10.4.13
REQUEST_ENTITY_TOO_LARGE413HTTP/1.1, RFC 2616, Section\n10.4.14
REQUEST_URI_TOO_LONG414HTTP/1.1, RFC 2616, Section\n10.4.15
UNSUPPORTED_MEDIA_TYPE415HTTP/1.1, RFC 2616, Section\n10.4.16
REQUESTED_RANGE_NOT_SATISFIABLE416HTTP/1.1, RFC 2616, Section\n10.4.17
EXPECTATION_FAILED417HTTP/1.1, RFC 2616, Section\n10.4.18
UNPROCESSABLE_ENTITY422WEBDAV, RFC 2518, Section 10.3
LOCKED423WEBDAV RFC 2518, Section 10.4
FAILED_DEPENDENCY424WEBDAV, RFC 2518, Section 10.5
UPGRADE_REQUIRED426HTTP Upgrade to TLS,\nRFC 2817, Section 6
INTERNAL_SERVER_ERROR500HTTP/1.1, RFC 2616, Section\n10.5.1
NOT_IMPLEMENTED501HTTP/1.1, RFC 2616, Section\n10.5.2
BAD_GATEWAY502HTTP/1.1 RFC 2616, Section\n10.5.3
SERVICE_UNAVAILABLE503HTTP/1.1, RFC 2616, Section\n10.5.4
GATEWAY_TIMEOUT504HTTP/1.1 RFC 2616, Section\n10.5.5
HTTP_VERSION_NOT_SUPPORTED505HTTP/1.1, RFC 2616, Section\n10.5.6
INSUFFICIENT_STORAGE507WEBDAV, RFC 2518, Section 10.6
NOT_EXTENDED510An HTTP Extension Framework,\nRFC 2774, Section 7
\n
\n
\nhttplib.responses
\n

This dictionary maps the HTTP 1.1 status codes to the W3C names.

\n

Example: httplib.responses[httplib.NOT_FOUND] is 'Not Found'.

\n

\nNew in version 2.5.

\n
\n\n
\n

20.7.1. HTTPConnection Objects

\n

HTTPConnection instances have the following methods:

\n
\n
\nHTTPConnection.request(method, url[, body[, headers]])
\n

This will send a request to the server using the HTTP request method method\nand the selector url. If the body argument is present, it should be a\nstring of data to send after the headers are finished. Alternatively, it may\nbe an open file object, in which case the contents of the file is sent; this\nfile object should support fileno() and read() methods. The header\nContent-Length is automatically set to the correct value. The headers\nargument should be a mapping of extra HTTP headers to send with the request.

\n

\nChanged in version 2.6: body can be a file object.

\n
\n\n
\n
\nHTTPConnection.getresponse()
\n

Should be called after a request is sent to get the response from the server.\nReturns an HTTPResponse instance.

\n
\n

Note

\n

Note that you must have read the whole response before you can send a new\nrequest to the server.

\n
\n
\n\n
\n
\nHTTPConnection.set_debuglevel(level)
\n
Set the debugging level (the amount of debugging output printed). The default\ndebug level is 0, meaning no debugging output is printed.
\n\n
\n
\nHTTPConnection.set_tunnel(host, port=None, headers=None)
\n

Set the host and the port for HTTP Connect Tunnelling. Normally used when\nit is required to do HTTPS Conection through a proxy server.

\n

The headers argument should be a mapping of extra HTTP headers to send\nwith the CONNECT request.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nHTTPConnection.connect()
\n
Connect to the server specified when the object was created.
\n\n
\n
\nHTTPConnection.close()
\n
Close the connection to the server.
\n\n

As an alternative to using the request() method described above, you can\nalso send your request step by step, by using the four functions below.

\n
\n
\nHTTPConnection.putrequest(request, selector[, skip_host[, skip_accept_encoding]])
\n

This should be the first call after the connection to the server has been made.\nIt sends a line to the server consisting of the request string, the selector\nstring, and the HTTP version (HTTP/1.1). To disable automatic sending of\nHost: or Accept-Encoding: headers (for example to accept additional\ncontent encodings), specify skip_host or skip_accept_encoding with non-False\nvalues.

\n

\nChanged in version 2.4: skip_accept_encoding argument added.

\n
\n\n
\n
\nHTTPConnection.putheader(header, argument[, ...])
\n
Send an RFC 822-style header to the server. It sends a line to the server\nconsisting of the header, a colon and a space, and the first argument. If more\narguments are given, continuation lines are sent, each consisting of a tab and\nan argument.
\n\n
\n
\nHTTPConnection.endheaders(message_body=None)
\n

Send a blank line to the server, signalling the end of the headers. The\noptional message_body argument can be used to pass a message body\nassociated with the request. The message body will be sent in the same\npacket as the message headers if it is string, otherwise it is sent in a\nseparate packet.

\n

\nChanged in version 2.7: message_body was added.

\n
\n\n
\n
\nHTTPConnection.send(data)
\n
Send data to the server. This should be used directly only after the\nendheaders() method has been called and before getresponse() is\ncalled.
\n\n
\n
\n

20.7.2. HTTPResponse Objects

\n

HTTPResponse instances have the following methods and attributes:

\n
\n
\nHTTPResponse.read([amt])
\n
Reads and returns the response body, or up to the next amt bytes.
\n\n
\n
\nHTTPResponse.getheader(name[, default])
\n
Get the contents of the header name, or default if there is no matching\nheader.
\n\n
\n
\nHTTPResponse.getheaders()
\n

Return a list of (header, value) tuples.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nHTTPResponse.fileno()
\n
Returns the fileno of the underlying socket.
\n\n
\n
\nHTTPResponse.msg
\n
A mimetools.Message instance containing the response headers.
\n\n
\n
\nHTTPResponse.version
\n
HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
\n\n
\n
\nHTTPResponse.status
\n
Status code returned by server.
\n\n
\n
\nHTTPResponse.reason
\n
Reason phrase returned by server.
\n\n
\n
\n

20.7.3. Examples

\n

Here is an example session that uses the GET method:

\n
>>> import httplib\n>>> conn = httplib.HTTPConnection("www.python.org")\n>>> conn.request("GET", "/index.html")\n>>> r1 = conn.getresponse()\n>>> print r1.status, r1.reason\n200 OK\n>>> data1 = r1.read()\n>>> conn.request("GET", "/parrot.spam")\n>>> r2 = conn.getresponse()\n>>> print r2.status, r2.reason\n404 Not Found\n>>> data2 = r2.read()\n>>> conn.close()\n
\n
\n

Here is an example session that uses the HEAD method. Note that the\nHEAD method never returns any data.

\n
>>> import httplib\n>>> conn = httplib.HTTPConnection("www.python.org")\n>>> conn.request("HEAD","/index.html")\n>>> res = conn.getresponse()\n>>> print res.status, res.reason\n200 OK\n>>> data = res.read()\n>>> print len(data)\n0\n>>> data == ''\nTrue\n
\n
\n

Here is an example session that shows how to POST requests:

\n
>>> import httplib, urllib\n>>> params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})\n>>> headers = {"Content-type": "application/x-www-form-urlencoded",\n...            "Accept": "text/plain"}\n>>> conn = httplib.HTTPConnection("bugs.python.org")\n>>> conn.request("POST", "", params, headers)\n>>> response = conn.getresponse()\n>>> print response.status, response.reason\n302 Found\n>>> data = response.read()\n>>> data\n'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'\n>>> conn.close()\n
\n
\n
\n
", "searchableItems": [ { "name": "httplib.HTTPConnection", "domId": "httplib_httplib.HTTPConnection" }, { "name": "httplib.HTTPConnection.close", "domId": "httplib_httplib.HTTPConnection.close" }, { "name": "httplib.HTTPConnection.connect", "domId": "httplib_httplib.HTTPConnection.connect" }, { "name": "httplib.HTTPConnection.endheaders", "domId": "httplib_httplib.HTTPConnection.endheaders" }, { "name": "httplib.HTTPConnection.getresponse", "domId": "httplib_httplib.HTTPConnection.getresponse" }, { "name": "httplib.HTTPConnection.putheader", "domId": "httplib_httplib.HTTPConnection.putheader" }, { "name": "httplib.HTTPConnection.putrequest", "domId": "httplib_httplib.HTTPConnection.putrequest" }, { "name": "httplib.HTTPConnection.request", "domId": "httplib_httplib.HTTPConnection.request" }, { "name": "httplib.HTTPConnection.send", "domId": "httplib_httplib.HTTPConnection.send" }, { "name": "httplib.HTTPConnection.set_debuglevel", "domId": "httplib_httplib.HTTPConnection.set_debuglevel" }, { "name": "httplib.HTTPConnection.set_tunnel", "domId": "httplib_httplib.HTTPConnection.set_tunnel" }, { "name": "httplib.HTTPMessage", "domId": "httplib_httplib.HTTPMessage" }, { "name": "httplib.HTTPResponse", "domId": "httplib_httplib.HTTPResponse" }, { "name": "httplib.HTTPResponse.fileno", "domId": "httplib_httplib.HTTPResponse.fileno" }, { "name": "httplib.HTTPResponse.getheader", "domId": "httplib_httplib.HTTPResponse.getheader" }, { "name": "httplib.HTTPResponse.getheaders", "domId": "httplib_httplib.HTTPResponse.getheaders" }, { "name": "httplib.HTTPResponse.read", "domId": "httplib_httplib.HTTPResponse.read" }, { "name": "httplib.HTTPSConnection", "domId": "httplib_httplib.HTTPSConnection" } ] }, { "url": "http://docs.python.org/library/urllib2.html", "title": "urllib2", "html": "
\n

20.6. urllib2 — extensible library for opening URLs

\n
\n

Note

\n

The urllib2 module has been split across several modules in\nPython 3.0 named urllib.request and urllib.error.\nThe 2to3 tool will automatically adapt imports when converting\nyour sources to 3.0.

\n
\n

The urllib2 module defines functions and classes which help in opening\nURLs (mostly HTTP) in a complex world — basic and digest authentication,\nredirections, cookies and more.

\n

The urllib2 module defines the following functions:

\n
\n
\nurllib2.urlopen(url[, data][, timeout])
\n

Open the URL url, which can be either a string or a Request object.

\n
\n

Warning

\n

HTTPS requests do not do any verification of the server’s certificate.

\n
\n

data may be a string specifying additional data to send to the server, or\nNone if no such data is needed. Currently HTTP requests are the only ones\nthat use data; the HTTP request will be a POST instead of a GET when the\ndata parameter is provided. data should be a buffer in the standard\napplication/x-www-form-urlencoded format. The\nurllib.urlencode() function takes a mapping or sequence of 2-tuples and\nreturns a string in this format. urllib2 module sends HTTP/1.1 requests with\nConnection:close header included.

\n

The optional timeout parameter specifies a timeout in seconds for blocking\noperations like the connection attempt (if not specified, the global default\ntimeout setting will be used). This actually only works for HTTP, HTTPS and\nFTP connections.

\n

This function returns a file-like object with two additional methods:

\n
    \n
  • geturl() — return the URL of the resource retrieved, commonly used to\ndetermine if a redirect was followed
  • \n
  • info() — return the meta-information of the page, such as headers,\nin the form of an mimetools.Message instance\n(see Quick Reference to HTTP Headers)
  • \n
\n

Raises URLError on errors.

\n

Note that None may be returned if no handler handles the request (though the\ndefault installed global OpenerDirector uses UnknownHandler to\nensure this never happens).

\n

In addition, default installed ProxyHandler makes sure the requests\nare handled through the proxy when they are set.

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n
\nurllib2.install_opener(opener)
\n
Install an OpenerDirector instance as the default global opener.\nInstalling an opener is only necessary if you want urlopen to use that opener;\notherwise, simply call OpenerDirector.open() instead of urlopen().\nThe code does not check for a real OpenerDirector, and any class with\nthe appropriate interface will work.
\n\n
\n
\nurllib2.build_opener([handler, ...])
\n

Return an OpenerDirector instance, which chains the handlers in the\norder given. handlers can be either instances of BaseHandler, or\nsubclasses of BaseHandler (in which case it must be possible to call\nthe constructor without any parameters). Instances of the following classes\nwill be in front of the handlers, unless the handlers contain them,\ninstances of them or subclasses of them: ProxyHandler,\nUnknownHandler, HTTPHandler, HTTPDefaultErrorHandler,\nHTTPRedirectHandler, FTPHandler, FileHandler,\nHTTPErrorProcessor.

\n

If the Python installation has SSL support (i.e., if the ssl module can be imported),\nHTTPSHandler will also be added.

\n

Beginning in Python 2.3, a BaseHandler subclass may also change its\nhandler_order attribute to modify its position in the handlers\nlist.

\n
\n\n

The following exceptions are raised as appropriate:

\n
\n
\nexception urllib2.URLError
\n

The handlers raise this exception (or derived exceptions) when they run into a\nproblem. It is a subclass of IOError.

\n
\n
\nreason
\n
The reason for this error. It can be a message string or another exception\ninstance (socket.error for remote URLs, OSError for local\nURLs).
\n\n
\n\n
\n
\nexception urllib2.HTTPError
\n

Though being an exception (a subclass of URLError), an HTTPError\ncan also function as a non-exceptional file-like return value (the same thing\nthat urlopen() returns). This is useful when handling exotic HTTP\nerrors, such as requests for authentication.

\n
\n
\ncode
\n
An HTTP status code as defined in RFC 2616.\nThis numeric value corresponds to a value found in the dictionary of\ncodes as found in BaseHTTPServer.BaseHTTPRequestHandler.responses.
\n\n
\n\n

The following classes are provided:

\n
\n
\nclass urllib2.Request(url[, data][, headers][, origin_req_host][, unverifiable])
\n

This class is an abstraction of a URL request.

\n

url should be a string containing a valid URL.

\n

data may be a string specifying additional data to send to the server, or\nNone if no such data is needed. Currently HTTP requests are the only ones\nthat use data; the HTTP request will be a POST instead of a GET when the\ndata parameter is provided. data should be a buffer in the standard\napplication/x-www-form-urlencoded format. The\nurllib.urlencode() function takes a mapping or sequence of 2-tuples and\nreturns a string in this format.

\n

headers should be a dictionary, and will be treated as if add_header()\nwas called with each key and value as arguments. This is often used to “spoof”\nthe User-Agent header, which is used by a browser to identify itself –\nsome HTTP servers only allow requests coming from common browsers as opposed\nto scripts. For example, Mozilla Firefox may identify itself as "Mozilla/5.0\n(X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11", while urllib2‘s\ndefault user agent string is "Python-urllib/2.6" (on Python 2.6).

\n

The final two arguments are only of interest for correct handling of third-party\nHTTP cookies:

\n

origin_req_host should be the request-host of the origin transaction, as\ndefined by RFC 2965. It defaults to cookielib.request_host(self). This\nis the host name or IP address of the original request that was initiated by the\nuser. For example, if the request is for an image in an HTML document, this\nshould be the request-host of the request for the page containing the image.

\n

unverifiable should indicate whether the request is unverifiable, as defined\nby RFC 2965. It defaults to False. An unverifiable request is one whose URL\nthe user did not have the option to approve. For example, if the request is for\nan image in an HTML document, and the user had no option to approve the\nautomatic fetching of the image, this should be true.

\n
\n\n
\n
\nclass urllib2.OpenerDirector
\n
The OpenerDirector class opens URLs via BaseHandlers chained\ntogether. It manages the chaining of handlers, and recovery from errors.
\n\n
\n
\nclass urllib2.BaseHandler
\n
This is the base class for all registered handlers — and handles only the\nsimple mechanics of registration.
\n\n
\n
\nclass urllib2.HTTPDefaultErrorHandler
\n
A class which defines a default handler for HTTP error responses; all responses\nare turned into HTTPError exceptions.
\n\n
\n
\nclass urllib2.HTTPRedirectHandler
\n
A class to handle redirections.
\n\n
\n
\nclass urllib2.HTTPCookieProcessor([cookiejar])
\n
A class to handle HTTP Cookies.
\n\n
\n
\nclass urllib2.ProxyHandler([proxies])
\n

Cause requests to go through a proxy. If proxies is given, it must be a\ndictionary mapping protocol names to URLs of proxies. The default is to read\nthe list of proxies from the environment variables\n. If no proxy environment variables are set, in a\nWindows environment, proxy settings are obtained from the registry’s\nInternet Settings section and in a Mac OS X environment, proxy information\nis retrieved from the OS X System Configuration Framework.

\n

To disable autodetected proxy pass an empty dictionary.

\n
\n\n
\n
\nclass urllib2.HTTPPasswordMgr
\n
Keep a database of (realm, uri) -> (user, password) mappings.
\n\n
\n
\nclass urllib2.HTTPPasswordMgrWithDefaultRealm
\n
Keep a database of (realm, uri) -> (user, password) mappings. A realm of\nNone is considered a catch-all realm, which is searched if no other realm\nfits.
\n\n
\n
\nclass urllib2.AbstractBasicAuthHandler([password_mgr])
\n
This is a mixin class that helps with HTTP authentication, both to the remote\nhost and to a proxy. password_mgr, if given, should be something that is\ncompatible with HTTPPasswordMgr; refer to section\nHTTPPasswordMgr Objects for information on the interface that must be\nsupported.
\n\n
\n
\nclass urllib2.HTTPBasicAuthHandler([password_mgr])
\n
Handle authentication with the remote host. password_mgr, if given, should be\nsomething that is compatible with HTTPPasswordMgr; refer to section\nHTTPPasswordMgr Objects for information on the interface that must be\nsupported.
\n\n
\n
\nclass urllib2.ProxyBasicAuthHandler([password_mgr])
\n
Handle authentication with the proxy. password_mgr, if given, should be\nsomething that is compatible with HTTPPasswordMgr; refer to section\nHTTPPasswordMgr Objects for information on the interface that must be\nsupported.
\n\n
\n
\nclass urllib2.AbstractDigestAuthHandler([password_mgr])
\n
This is a mixin class that helps with HTTP authentication, both to the remote\nhost and to a proxy. password_mgr, if given, should be something that is\ncompatible with HTTPPasswordMgr; refer to section\nHTTPPasswordMgr Objects for information on the interface that must be\nsupported.
\n\n
\n
\nclass urllib2.HTTPDigestAuthHandler([password_mgr])
\n
Handle authentication with the remote host. password_mgr, if given, should be\nsomething that is compatible with HTTPPasswordMgr; refer to section\nHTTPPasswordMgr Objects for information on the interface that must be\nsupported.
\n\n
\n
\nclass urllib2.ProxyDigestAuthHandler([password_mgr])
\n
Handle authentication with the proxy. password_mgr, if given, should be\nsomething that is compatible with HTTPPasswordMgr; refer to section\nHTTPPasswordMgr Objects for information on the interface that must be\nsupported.
\n\n
\n
\nclass urllib2.HTTPHandler
\n
A class to handle opening of HTTP URLs.
\n\n
\n
\nclass urllib2.HTTPSHandler
\n
A class to handle opening of HTTPS URLs.
\n\n
\n
\nclass urllib2.FileHandler
\n
Open local files.
\n\n
\n
\nclass urllib2.FTPHandler
\n
Open FTP URLs.
\n\n
\n
\nclass urllib2.CacheFTPHandler
\n
Open FTP URLs, keeping a cache of open FTP connections to minimize delays.
\n\n
\n
\nclass urllib2.UnknownHandler
\n
A catch-all class to handle unknown URLs.
\n\n
\n
\nclass urllib2.HTTPErrorProcessor
\n
Process HTTP error responses.
\n\n
\n

20.6.1. Request Objects

\n

The following methods describe all of Request‘s public interface, and\nso all must be overridden in subclasses.

\n
\n
\nRequest.add_data(data)
\n
Set the Request data to data. This is ignored by all handlers except\nHTTP handlers — and there it should be a byte string, and will change the\nrequest to be POST rather than GET.
\n\n
\n
\nRequest.get_method()
\n
Return a string indicating the HTTP request method. This is only meaningful for\nHTTP requests, and currently always returns 'GET' or 'POST'.
\n\n
\n
\nRequest.has_data()
\n
Return whether the instance has a non-None data.
\n\n
\n
\nRequest.get_data()
\n
Return the instance’s data.
\n\n
\n
\nRequest.add_header(key, val)
\n
Add another header to the request. Headers are currently ignored by all\nhandlers except HTTP handlers, where they are added to the list of headers sent\nto the server. Note that there cannot be more than one header with the same\nname, and later calls will overwrite previous calls in case the key collides.\nCurrently, this is no loss of HTTP functionality, since all headers which have\nmeaning when used more than once have a (header-specific) way of gaining the\nsame functionality using only one header.
\n\n
\n
\nRequest.add_unredirected_header(key, header)
\n

Add a header that will not be added to a redirected request.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nRequest.has_header(header)
\n

Return whether the instance has the named header (checks both regular and\nunredirected).

\n

\nNew in version 2.4.

\n
\n\n
\n
\nRequest.get_full_url()
\n
Return the URL given in the constructor.
\n\n
\n
\nRequest.get_type()
\n
Return the type of the URL — also known as the scheme.
\n\n
\n
\nRequest.get_host()
\n
Return the host to which a connection will be made.
\n\n
\n
\nRequest.get_selector()
\n
Return the selector — the part of the URL that is sent to the server.
\n\n
\n
\nRequest.set_proxy(host, type)
\n
Prepare the request by connecting to a proxy server. The host and type will\nreplace those of the instance, and the instance’s selector will be the original\nURL given in the constructor.
\n\n
\n
\nRequest.get_origin_req_host()
\n
Return the request-host of the origin transaction, as defined by RFC 2965.\nSee the documentation for the Request constructor.
\n\n
\n
\nRequest.is_unverifiable()
\n
Return whether the request is unverifiable, as defined by RFC 2965. See the\ndocumentation for the Request constructor.
\n\n
\n
\n

20.6.2. OpenerDirector Objects

\n

OpenerDirector instances have the following methods:

\n
\n
\nOpenerDirector.add_handler(handler)
\n

handler should be an instance of BaseHandler. The following\nmethods are searched, and added to the possible chains (note that HTTP errors\nare a special case).

\n
    \n
  • protocol_open — signal that the handler knows how to open\nprotocol URLs.
  • \n
  • http_error_type — signal that the handler knows how to handle\nHTTP errors with HTTP error code type.
  • \n
  • protocol_error — signal that the handler knows how to handle\nerrors from (non-http) protocol.
  • \n
  • protocol_request — signal that the handler knows how to\npre-process protocol requests.
  • \n
  • protocol_response — signal that the handler knows how to\npost-process protocol responses.
  • \n
\n
\n\n
\n
\nOpenerDirector.open(url[, data][, timeout])
\n

Open the given url (which can be a request object or a string), optionally\npassing the given data. Arguments, return values and exceptions raised are\nthe same as those of urlopen() (which simply calls the open()\nmethod on the currently installed global OpenerDirector). The\noptional timeout parameter specifies a timeout in seconds for blocking\noperations like the connection attempt (if not specified, the global default\ntimeout setting will be used). The timeout feature actually works only for\nHTTP, HTTPS and FTP connections).

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n
\nOpenerDirector.error(proto[, arg[, ...]])
\n

Handle an error of the given protocol. This will call the registered error\nhandlers for the given protocol with the given arguments (which are protocol\nspecific). The HTTP protocol is a special case which uses the HTTP response\ncode to determine the specific error handler; refer to the http_error_*()\nmethods of the handler classes.

\n

Return values and exceptions raised are the same as those of urlopen().

\n
\n\n

OpenerDirector objects open URLs in three stages:

\n

The order in which these methods are called within each stage is determined by\nsorting the handler instances.

\n
    \n
  1. Every handler with a method named like protocol_request has that\nmethod called to pre-process the request.

    \n
  2. \n
  3. Handlers with a method named like protocol_open are called to handle\nthe request. This stage ends when a handler either returns a non-None\nvalue (ie. a response), or raises an exception (usually URLError).\nExceptions are allowed to propagate.

    \n

    In fact, the above algorithm is first tried for methods named\ndefault_open(). If all such methods return None, the\nalgorithm is repeated for methods named like protocol_open. If all\nsuch methods return None, the algorithm is repeated for methods\nnamed unknown_open().

    \n

    Note that the implementation of these methods may involve calls of the parent\nOpenerDirector instance’s open() and\nerror() methods.

    \n
  4. \n
  5. Every handler with a method named like protocol_response has that\nmethod called to post-process the response.

    \n
  6. \n
\n
\n
\n

20.6.3. BaseHandler Objects

\n

BaseHandler objects provide a couple of methods that are directly\nuseful, and others that are meant to be used by derived classes. These are\nintended for direct use:

\n
\n
\nBaseHandler.add_parent(director)
\n
Add a director as parent.
\n\n
\n
\nBaseHandler.close()
\n
Remove any parents.
\n\n

The following attributes and methods should only be used by classes derived from\nBaseHandler.

\n
\n

Note

\n

The convention has been adopted that subclasses defining\nprotocol_request() or protocol_response() methods are named\n*Processor; all others are named *Handler.

\n
\n
\n
\nBaseHandler.parent
\n
A valid OpenerDirector, which can be used to open using a different\nprotocol, or handle errors.
\n\n
\n
\nBaseHandler.default_open(req)
\n

This method is not defined in BaseHandler, but subclasses should\ndefine it if they want to catch all URLs.

\n

This method, if implemented, will be called by the parent\nOpenerDirector. It should return a file-like object as described in\nthe return value of the open() of OpenerDirector, or None.\nIt should raise URLError, unless a truly exceptional thing happens (for\nexample, MemoryError should not be mapped to URLError).

\n

This method will be called before any protocol-specific open method.

\n
\n\n
\n
\nBaseHandler.protocol_open(req)
\n

(“protocol” is to be replaced by the protocol name.)

\n

This method is not defined in BaseHandler, but subclasses should\ndefine it if they want to handle URLs with the given protocol.

\n

This method, if defined, will be called by the parent OpenerDirector.\nReturn values should be the same as for default_open().

\n
\n\n
\n
\nBaseHandler.unknown_open(req)
\n

This method is not defined in BaseHandler, but subclasses should\ndefine it if they want to catch all URLs with no specific registered handler to\nopen it.

\n

This method, if implemented, will be called by the parent\nOpenerDirector. Return values should be the same as for\ndefault_open().

\n
\n\n
\n
\nBaseHandler.http_error_default(req, fp, code, msg, hdrs)
\n

This method is not defined in BaseHandler, but subclasses should\noverride it if they intend to provide a catch-all for otherwise unhandled HTTP\nerrors. It will be called automatically by the OpenerDirector getting\nthe error, and should not normally be called in other circumstances.

\n

req will be a Request object, fp will be a file-like object with\nthe HTTP error body, code will be the three-digit code of the error, msg\nwill be the user-visible explanation of the code and hdrs will be a mapping\nobject with the headers of the error.

\n

Return values and exceptions raised should be the same as those of\nurlopen().

\n
\n\n
\n
\nBaseHandler.http_error_nnn(req, fp, code, msg, hdrs)
\n

nnn should be a three-digit HTTP error code. This method is also not defined\nin BaseHandler, but will be called, if it exists, on an instance of a\nsubclass, when an HTTP error with code nnn occurs.

\n

Subclasses should override this method to handle specific HTTP errors.

\n

Arguments, return values and exceptions raised should be the same as for\nhttp_error_default().

\n
\n\n
\n
\nBaseHandler.protocol_request(req)
\n

(“protocol” is to be replaced by the protocol name.)

\n

This method is not defined in BaseHandler, but subclasses should\ndefine it if they want to pre-process requests of the given protocol.

\n

This method, if defined, will be called by the parent OpenerDirector.\nreq will be a Request object. The return value should be a\nRequest object.

\n
\n\n
\n
\nBaseHandler.protocol_response(req, response)
\n

(“protocol” is to be replaced by the protocol name.)

\n

This method is not defined in BaseHandler, but subclasses should\ndefine it if they want to post-process responses of the given protocol.

\n

This method, if defined, will be called by the parent OpenerDirector.\nreq will be a Request object. response will be an object\nimplementing the same interface as the return value of urlopen(). The\nreturn value should implement the same interface as the return value of\nurlopen().

\n
\n\n
\n
\n

20.6.4. HTTPRedirectHandler Objects

\n
\n

Note

\n

Some HTTP redirections require action from this module’s client code. If this\nis the case, HTTPError is raised. See RFC 2616 for details of the\nprecise meanings of the various redirection codes.

\n
\n
\n
\nHTTPRedirectHandler.redirect_request(req, fp, code, msg, hdrs, newurl)
\n

Return a Request or None in response to a redirect. This is called\nby the default implementations of the http_error_30*() methods when a\nredirection is received from the server. If a redirection should take place,\nreturn a new Request to allow http_error_30*() to perform the\nredirect to newurl. Otherwise, raise HTTPError if no other handler\nshould try to handle this URL, or return None if you can’t but another\nhandler might.

\n
\n

Note

\n

The default implementation of this method does not strictly follow RFC 2616,\nwhich says that 301 and 302 responses to POST requests must not be\nautomatically redirected without confirmation by the user. In reality, browsers\ndo allow automatic redirection of these responses, changing the POST to a\nGET, and the default implementation reproduces this behavior.

\n
\n
\n\n
\n
\nHTTPRedirectHandler.http_error_301(req, fp, code, msg, hdrs)
\n
Redirect to the Location: or URI: URL. This method is called by the\nparent OpenerDirector when getting an HTTP ‘moved permanently’ response.
\n\n
\n
\nHTTPRedirectHandler.http_error_302(req, fp, code, msg, hdrs)
\n
The same as http_error_301(), but called for the ‘found’ response.
\n\n
\n
\nHTTPRedirectHandler.http_error_303(req, fp, code, msg, hdrs)
\n
The same as http_error_301(), but called for the ‘see other’ response.
\n\n
\n
\nHTTPRedirectHandler.http_error_307(req, fp, code, msg, hdrs)
\n
The same as http_error_301(), but called for the ‘temporary redirect’\nresponse.
\n\n
\n
\n

20.6.5. HTTPCookieProcessor Objects

\n

\nNew in version 2.4.

\n

HTTPCookieProcessor instances have one attribute:

\n
\n
\nHTTPCookieProcessor.cookiejar
\n
The cookielib.CookieJar in which cookies are stored.
\n\n
\n
\n

20.6.6. ProxyHandler Objects

\n
\n
\nProxyHandler.protocol_open(request)
\n

(“protocol” is to be replaced by the protocol name.)

\n

The ProxyHandler will have a method protocol_open for every\nprotocol which has a proxy in the proxies dictionary given in the\nconstructor. The method will modify requests to go through the proxy, by\ncalling request.set_proxy(), and call the next handler in the chain to\nactually execute the protocol.

\n
\n\n
\n
\n

20.6.7. HTTPPasswordMgr Objects

\n

These methods are available on HTTPPasswordMgr and\nHTTPPasswordMgrWithDefaultRealm objects.

\n
\n
\nHTTPPasswordMgr.add_password(realm, uri, user, passwd)
\n
uri can be either a single URI, or a sequence of URIs. realm, user and\npasswd must be strings. This causes (user, passwd) to be used as\nauthentication tokens when authentication for realm and a super-URI of any of\nthe given URIs is given.
\n\n
\n
\nHTTPPasswordMgr.find_user_password(realm, authuri)
\n

Get user/password for given realm and URI, if any. This method will return\n(None, None) if there is no matching user/password.

\n

For HTTPPasswordMgrWithDefaultRealm objects, the realm None will be\nsearched if the given realm has no matching user/password.

\n
\n\n
\n
\n

20.6.8. AbstractBasicAuthHandler Objects

\n
\n
\nAbstractBasicAuthHandler.http_error_auth_reqed(authreq, host, req, headers)
\n

Handle an authentication request by getting a user/password pair, and re-trying\nthe request. authreq should be the name of the header where the information\nabout the realm is included in the request, host specifies the URL and path to\nauthenticate for, req should be the (failed) Request object, and\nheaders should be the error headers.

\n

host is either an authority (e.g. "python.org") or a URL containing an\nauthority component (e.g. "http://python.org/"). In either case, the\nauthority must not contain a userinfo component (so, "python.org" and\n"python.org:80" are fine, "joe:password@python.org" is not).

\n
\n\n
\n
\n

20.6.9. HTTPBasicAuthHandler Objects

\n
\n
\nHTTPBasicAuthHandler.http_error_401(req, fp, code, msg, hdrs)
\n
Retry the request with authentication information, if available.
\n\n
\n
\n

20.6.10. ProxyBasicAuthHandler Objects

\n
\n
\nProxyBasicAuthHandler.http_error_407(req, fp, code, msg, hdrs)
\n
Retry the request with authentication information, if available.
\n\n
\n
\n

20.6.11. AbstractDigestAuthHandler Objects

\n
\n
\nAbstractDigestAuthHandler.http_error_auth_reqed(authreq, host, req, headers)
\n
authreq should be the name of the header where the information about the realm\nis included in the request, host should be the host to authenticate to, req\nshould be the (failed) Request object, and headers should be the\nerror headers.
\n\n
\n
\n

20.6.12. HTTPDigestAuthHandler Objects

\n
\n
\nHTTPDigestAuthHandler.http_error_401(req, fp, code, msg, hdrs)
\n
Retry the request with authentication information, if available.
\n\n
\n
\n

20.6.13. ProxyDigestAuthHandler Objects

\n
\n
\nProxyDigestAuthHandler.http_error_407(req, fp, code, msg, hdrs)
\n
Retry the request with authentication information, if available.
\n\n
\n
\n

20.6.14. HTTPHandler Objects

\n
\n
\nHTTPHandler.http_open(req)
\n
Send an HTTP request, which can be either GET or POST, depending on\nreq.has_data().
\n\n
\n
\n

20.6.15. HTTPSHandler Objects

\n
\n
\nHTTPSHandler.https_open(req)
\n
Send an HTTPS request, which can be either GET or POST, depending on\nreq.has_data().
\n\n
\n
\n

20.6.16. FileHandler Objects

\n
\n
\nFileHandler.file_open(req)
\n
Open the file locally, if there is no host name, or the host name is\n'localhost'. Change the protocol to ftp otherwise, and retry opening it\nusing parent.
\n\n
\n
\n

20.6.17. FTPHandler Objects

\n
\n
\nFTPHandler.ftp_open(req)
\n
Open the FTP file indicated by req. The login is always done with empty\nusername and password.
\n\n
\n
\n

20.6.18. CacheFTPHandler Objects

\n

CacheFTPHandler objects are FTPHandler objects with the\nfollowing additional methods:

\n
\n
\nCacheFTPHandler.setTimeout(t)
\n
Set timeout of connections to t seconds.
\n\n
\n
\nCacheFTPHandler.setMaxConns(m)
\n
Set maximum number of cached connections to m.
\n\n
\n
\n

20.6.19. UnknownHandler Objects

\n
\n
\nUnknownHandler.unknown_open()
\n
Raise a URLError exception.
\n\n
\n
\n

20.6.20. HTTPErrorProcessor Objects

\n

\nNew in version 2.4.

\n
\n
\nHTTPErrorProcessor.http_response()
\n

Process HTTP error responses.

\n

For 200 error codes, the response object is returned immediately.

\n

For non-200 error codes, this simply passes the job on to the\nprotocol_error_code handler methods, via\nOpenerDirector.error(). Eventually,\nurllib2.HTTPDefaultErrorHandler will raise an HTTPError if no\nother handler handles the error.

\n
\n\n
\n
\nHTTPErrorProcessor.https_response()
\n

Process HTTPS error responses.

\n

The behavior is same as http_response().

\n
\n\n
\n
\n

20.6.21. Examples

\n

This example gets the python.org main page and displays the first 100 bytes of\nit:

\n
>>> import urllib2\n>>> f = urllib2.urlopen('http://www.python.org/')\n>>> print f.read(100)\n<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n<?xml-stylesheet href="./css/ht2html\n
\n
\n

Here we are sending a data-stream to the stdin of a CGI and reading the data it\nreturns to us. Note that this example will only work when the Python\ninstallation supports SSL.

\n
>>> import urllib2\n>>> req = urllib2.Request(url='https://localhost/cgi-bin/test.cgi',\n...                       data='This data is passed to stdin of the CGI')\n>>> f = urllib2.urlopen(req)\n>>> print f.read()\nGot Data: "This data is passed to stdin of the CGI"\n
\n
\n

The code for the sample CGI used in the above example is:

\n
#!/usr/bin/env python\nimport sys\ndata = sys.stdin.read()\nprint 'Content-type: text-plain\\n\\nGot Data: "%s"'  data\n
\n
\n

Use of Basic HTTP Authentication:

\n
import urllib2\n# Create an OpenerDirector with support for Basic HTTP Authentication...\nauth_handler = urllib2.HTTPBasicAuthHandler()\nauth_handler.add_password(realm='PDQ Application',\n                          uri='https://mahler:8092/site-updates.py',\n                          user='klem',\n                          passwd='kadidd!ehopper')\nopener = urllib2.build_opener(auth_handler)\n# ...and install it globally so it can be used with urlopen.\nurllib2.install_opener(opener)\nurllib2.urlopen('http://www.example.com/login.html')\n
\n
\n

build_opener() provides many handlers by default, including a\nProxyHandler. By default, ProxyHandler uses the environment\nvariables named <scheme>_proxy, where <scheme> is the URL scheme\ninvolved. For example, the http_proxy environment variable is read to\nobtain the HTTP proxy’s URL.

\n

This example replaces the default ProxyHandler with one that uses\nprogrammatically-supplied proxy URLs, and adds proxy authorization support with\nProxyBasicAuthHandler.

\n
proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})\nproxy_auth_handler = urllib2.ProxyBasicAuthHandler()\nproxy_auth_handler.add_password('realm', 'host', 'username', 'password')\n\nopener = urllib2.build_opener(proxy_handler, proxy_auth_handler)\n# This time, rather than install the OpenerDirector, we use it directly:\nopener.open('http://www.example.com/login.html')\n
\n
\n

Adding HTTP headers:

\n

Use the headers argument to the Request constructor, or:

\n
import urllib2\nreq = urllib2.Request('http://www.example.com/')\nreq.add_header('Referer', 'http://www.python.org/')\nr = urllib2.urlopen(req)\n
\n
\n

OpenerDirector automatically adds a User-Agent header to\nevery Request. To change this:

\n
import urllib2\nopener = urllib2.build_opener()\nopener.addheaders = [('User-agent', 'Mozilla/5.0')]\nopener.open('http://www.example.com/')\n
\n
\n

Also, remember that a few standard headers (Content-Length,\nContent-Type and Host) are added when the\nRequest is passed to urlopen() (or OpenerDirector.open()).

\n
\n
", "searchableItems": [ { "name": "urllib2.AbstractBasicAuthHandler", "domId": "urllib2_urllib2.AbstractBasicAuthHandler" }, { "name": "urllib2.AbstractBasicAuthHandler.http_error_auth_reqed", "domId": "urllib2_urllib2.AbstractBasicAuthHandler.http_error_auth_reqed" }, { "name": "urllib2.AbstractDigestAuthHandler", "domId": "urllib2_urllib2.AbstractDigestAuthHandler" }, { "name": "urllib2.AbstractDigestAuthHandler.http_error_auth_reqed", "domId": "urllib2_urllib2.AbstractDigestAuthHandler.http_error_auth_reqed" }, { "name": "urllib2.BaseHandler", "domId": "urllib2_urllib2.BaseHandler" }, { "name": "urllib2.BaseHandler.add_parent", "domId": "urllib2_urllib2.BaseHandler.add_parent" }, { "name": "urllib2.BaseHandler.close", "domId": "urllib2_urllib2.BaseHandler.close" }, { "name": "urllib2.BaseHandler.default_open", "domId": "urllib2_urllib2.BaseHandler.default_open" }, { "name": "urllib2.BaseHandler.http_error_default", "domId": "urllib2_urllib2.BaseHandler.http_error_default" }, { "name": "urllib2.BaseHandler.http_error_nnn", "domId": "urllib2_urllib2.BaseHandler.http_error_nnn" }, { "name": "urllib2.BaseHandler.unknown_open", "domId": "urllib2_urllib2.BaseHandler.unknown_open" }, { "name": "urllib2.build_opener", "domId": "urllib2_urllib2.build_opener" }, { "name": "urllib2.CacheFTPHandler", "domId": "urllib2_urllib2.CacheFTPHandler" }, { "name": "urllib2.CacheFTPHandler.setMaxConns", "domId": "urllib2_urllib2.CacheFTPHandler.setMaxConns" }, { "name": "urllib2.CacheFTPHandler.setTimeout", "domId": "urllib2_urllib2.CacheFTPHandler.setTimeout" }, { "name": "urllib2.FileHandler", "domId": "urllib2_urllib2.FileHandler" }, { "name": "urllib2.FileHandler.file_open", "domId": "urllib2_urllib2.FileHandler.file_open" }, { "name": "urllib2.FTPHandler", "domId": "urllib2_urllib2.FTPHandler" }, { "name": "urllib2.FTPHandler.ftp_open", "domId": "urllib2_urllib2.FTPHandler.ftp_open" }, { "name": "urllib2.HTTPBasicAuthHandler", "domId": "urllib2_urllib2.HTTPBasicAuthHandler" }, { "name": "urllib2.HTTPBasicAuthHandler.http_error_401", "domId": "urllib2_urllib2.HTTPBasicAuthHandler.http_error_401" }, { "name": "urllib2.HTTPCookieProcessor", "domId": "urllib2_urllib2.HTTPCookieProcessor" }, { "name": "urllib2.HTTPDefaultErrorHandler", "domId": "urllib2_urllib2.HTTPDefaultErrorHandler" }, { "name": "urllib2.HTTPDigestAuthHandler", "domId": "urllib2_urllib2.HTTPDigestAuthHandler" }, { "name": "urllib2.HTTPDigestAuthHandler.http_error_401", "domId": "urllib2_urllib2.HTTPDigestAuthHandler.http_error_401" }, { "name": "urllib2.HTTPErrorProcessor", "domId": "urllib2_urllib2.HTTPErrorProcessor" }, { "name": "urllib2.HTTPErrorProcessor.http_response", "domId": "urllib2_urllib2.HTTPErrorProcessor.http_response" }, { "name": "urllib2.HTTPErrorProcessor.https_response", "domId": "urllib2_urllib2.HTTPErrorProcessor.https_response" }, { "name": "urllib2.HTTPHandler", "domId": "urllib2_urllib2.HTTPHandler" }, { "name": "urllib2.HTTPHandler.http_open", "domId": "urllib2_urllib2.HTTPHandler.http_open" }, { "name": "urllib2.HTTPPasswordMgr", "domId": "urllib2_urllib2.HTTPPasswordMgr" }, { "name": "urllib2.HTTPPasswordMgr.add_password", "domId": "urllib2_urllib2.HTTPPasswordMgr.add_password" }, { "name": "urllib2.HTTPPasswordMgr.find_user_password", "domId": "urllib2_urllib2.HTTPPasswordMgr.find_user_password" }, { "name": "urllib2.HTTPPasswordMgrWithDefaultRealm", "domId": "urllib2_urllib2.HTTPPasswordMgrWithDefaultRealm" }, { "name": "urllib2.HTTPRedirectHandler", "domId": "urllib2_urllib2.HTTPRedirectHandler" }, { "name": "urllib2.HTTPRedirectHandler.http_error_301", "domId": "urllib2_urllib2.HTTPRedirectHandler.http_error_301" }, { "name": "urllib2.HTTPRedirectHandler.http_error_302", "domId": "urllib2_urllib2.HTTPRedirectHandler.http_error_302" }, { "name": "urllib2.HTTPRedirectHandler.http_error_303", "domId": "urllib2_urllib2.HTTPRedirectHandler.http_error_303" }, { "name": "urllib2.HTTPRedirectHandler.http_error_307", "domId": "urllib2_urllib2.HTTPRedirectHandler.http_error_307" }, { "name": "urllib2.HTTPRedirectHandler.redirect_request", "domId": "urllib2_urllib2.HTTPRedirectHandler.redirect_request" }, { "name": "urllib2.HTTPSHandler", "domId": "urllib2_urllib2.HTTPSHandler" }, { "name": "urllib2.HTTPSHandler.https_open", "domId": "urllib2_urllib2.HTTPSHandler.https_open" }, { "name": "urllib2.install_opener", "domId": "urllib2_urllib2.install_opener" }, { "name": "urllib2.OpenerDirector", "domId": "urllib2_urllib2.OpenerDirector" }, { "name": "urllib2.OpenerDirector.add_handler", "domId": "urllib2_urllib2.OpenerDirector.add_handler" }, { "name": "urllib2.OpenerDirector.error", "domId": "urllib2_urllib2.OpenerDirector.error" }, { "name": "urllib2.OpenerDirector.open", "domId": "urllib2_urllib2.OpenerDirector.open" }, { "name": "urllib2.ProxyBasicAuthHandler", "domId": "urllib2_urllib2.ProxyBasicAuthHandler" }, { "name": "urllib2.ProxyBasicAuthHandler.http_error_407", "domId": "urllib2_urllib2.ProxyBasicAuthHandler.http_error_407" }, { "name": "urllib2.ProxyDigestAuthHandler", "domId": "urllib2_urllib2.ProxyDigestAuthHandler" }, { "name": "urllib2.ProxyDigestAuthHandler.http_error_407", "domId": "urllib2_urllib2.ProxyDigestAuthHandler.http_error_407" }, { "name": "urllib2.ProxyHandler", "domId": "urllib2_urllib2.ProxyHandler" }, { "name": "urllib2.Request", "domId": "urllib2_urllib2.Request" }, { "name": "urllib2.Request.add_data", "domId": "urllib2_urllib2.Request.add_data" }, { "name": "urllib2.Request.add_header", "domId": "urllib2_urllib2.Request.add_header" }, { "name": "urllib2.Request.add_unredirected_header", "domId": "urllib2_urllib2.Request.add_unredirected_header" }, { "name": "urllib2.Request.get_data", "domId": "urllib2_urllib2.Request.get_data" }, { "name": "urllib2.Request.get_full_url", "domId": "urllib2_urllib2.Request.get_full_url" }, { "name": "urllib2.Request.get_host", "domId": "urllib2_urllib2.Request.get_host" }, { "name": "urllib2.Request.get_method", "domId": "urllib2_urllib2.Request.get_method" }, { "name": "urllib2.Request.get_origin_req_host", "domId": "urllib2_urllib2.Request.get_origin_req_host" }, { "name": "urllib2.Request.get_selector", "domId": "urllib2_urllib2.Request.get_selector" }, { "name": "urllib2.Request.get_type", "domId": "urllib2_urllib2.Request.get_type" }, { "name": "urllib2.Request.has_data", "domId": "urllib2_urllib2.Request.has_data" }, { "name": "urllib2.Request.has_header", "domId": "urllib2_urllib2.Request.has_header" }, { "name": "urllib2.Request.is_unverifiable", "domId": "urllib2_urllib2.Request.is_unverifiable" }, { "name": "urllib2.Request.set_proxy", "domId": "urllib2_urllib2.Request.set_proxy" }, { "name": "urllib2.UnknownHandler", "domId": "urllib2_urllib2.UnknownHandler" }, { "name": "urllib2.UnknownHandler.unknown_open", "domId": "urllib2_urllib2.UnknownHandler.unknown_open" }, { "name": "urllib2.urlopen", "domId": "urllib2_urllib2.urlopen" } ] }, { "url": "http://docs.python.org/library/poplib.html", "title": "poplib", "html": "
\n

20.9. poplib — POP3 protocol client

\n

Source code: Lib/poplib.py

\n
\n

This module defines a class, POP3, which encapsulates a connection to a\nPOP3 server and implements the protocol as defined in RFC 1725. The\nPOP3 class supports both the minimal and optional command sets.\nAdditionally, this module provides a class POP3_SSL, which provides\nsupport for connecting to POP3 servers that use SSL as an underlying protocol\nlayer.

\n

Note that POP3, though widely supported, is obsolescent. The implementation\nquality of POP3 servers varies widely, and too many are quite poor. If your\nmailserver supports IMAP, you would be better off using the\nimaplib.IMAP4 class, as IMAP servers tend to be better implemented.

\n

A single class is provided by the poplib module:

\n
\n
\nclass poplib.POP3(host[, port[, timeout]])
\n

This class implements the actual POP3 protocol. The connection is created when\nthe instance is initialized. If port is omitted, the standard POP3 port (110)\nis used. The optional timeout parameter specifies a timeout in seconds for the\nconnection attempt (if not specified, the global default timeout setting will\nbe used).

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n
\nclass poplib.POP3_SSL(host[, port[, keyfile[, certfile]]])
\n

This is a subclass of POP3 that connects to the server over an SSL\nencrypted socket. If port is not specified, 995, the standard POP3-over-SSL\nport is used. keyfile and certfile are also optional - they can contain a\nPEM formatted private key and certificate chain file for the SSL connection.

\n

\nNew in version 2.4.

\n
\n\n

One exception is defined as an attribute of the poplib module:

\n
\n
\nexception poplib.error_proto
\n
Exception raised on any errors from this module (errors from socket\nmodule are not caught). The reason for the exception is passed to the\nconstructor as a string.
\n\n
\n

See also

\n
\n
Module imaplib
\n
The standard Python IMAP module.
\n
Frequently Asked Questions About Fetchmail
\n
The FAQ for the fetchmail POP/IMAP client collects information on\nPOP3 server variations and RFC noncompliance that may be useful if you need to\nwrite an application based on the POP protocol.
\n
\n
\n
\n

20.9.1. POP3 Objects

\n

All POP3 commands are represented by methods of the same name, in lower-case;\nmost return the response text sent by the server.

\n

An POP3 instance has the following methods:

\n
\n
\nPOP3.set_debuglevel(level)
\n
Set the instance’s debugging level. This controls the amount of debugging\noutput printed. The default, 0, produces no debugging output. A value of\n1 produces a moderate amount of debugging output, generally a single line\nper request. A value of 2 or higher produces the maximum amount of\ndebugging output, logging each line sent and received on the control connection.
\n\n
\n
\nPOP3.getwelcome()
\n
Returns the greeting string sent by the POP3 server.
\n\n
\n
\nPOP3.user(username)
\n
Send user command, response should indicate that a password is required.
\n\n
\n
\nPOP3.pass_(password)
\n
Send password, response includes message count and mailbox size. Note: the\nmailbox on the server is locked until quit() is called.
\n\n
\n
\nPOP3.apop(user, secret)
\n
Use the more secure APOP authentication to log into the POP3 server.
\n\n
\n
\nPOP3.rpop(user)
\n
Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
\n\n
\n
\nPOP3.stat()
\n
Get mailbox status. The result is a tuple of 2 integers: (message count,\nmailbox size).
\n\n
\n
\nPOP3.list([which])
\n
Request message list, result is in the form (response, ['mesg_num octets',\n...], octets). If which is set, it is the message to list.
\n\n
\n
\nPOP3.retr(which)
\n
Retrieve whole message number which, and set its seen flag. Result is in form\n(response, ['line', ...], octets).
\n\n
\n
\nPOP3.dele(which)
\n
Flag message number which for deletion. On most servers deletions are not\nactually performed until QUIT (the major exception is Eudora QPOP, which\ndeliberately violates the RFCs by doing pending deletes on any disconnect).
\n\n
\n
\nPOP3.rset()
\n
Remove any deletion marks for the mailbox.
\n\n
\n
\nPOP3.noop()
\n
Do nothing. Might be used as a keep-alive.
\n\n
\n
\nPOP3.quit()
\n
Signoff: commit changes, unlock mailbox, drop connection.
\n\n
\n
\nPOP3.top(which, howmuch)
\n

Retrieves the message header plus howmuch lines of the message after the\nheader of message number which. Result is in form (response, ['line', ...],\noctets).

\n

The POP3 TOP command this method uses, unlike the RETR command, doesn’t set the\nmessage’s seen flag; unfortunately, TOP is poorly specified in the RFCs and is\nfrequently broken in off-brand servers. Test this method by hand against the\nPOP3 servers you will use before trusting it.

\n
\n\n
\n
\nPOP3.uidl([which])
\n
Return message digest (unique id) list. If which is specified, result contains\nthe unique id for that message in the form 'response mesgnum uid, otherwise\nresult is list (response, ['mesgnum uid', ...], octets).
\n\n

Instances of POP3_SSL have no additional methods. The interface of this\nsubclass is identical to its parent.

\n
\n
\n

20.9.2. POP3 Example

\n

Here is a minimal example (without error checking) that opens a mailbox and\nretrieves and prints all messages:

\n
import getpass, poplib\n\nM = poplib.POP3('localhost')\nM.user(getpass.getuser())\nM.pass_(getpass.getpass())\nnumMessages = len(M.list()[1])\nfor i in range(numMessages):\n    for j in M.retr(i+1)[1]:\n        print j\n
\n
\n

At the end of the module, there is a test section that contains a more extensive\nexample of usage.

\n
\n
", "searchableItems": [ { "name": "poplib.POP3", "domId": "poplib_poplib.POP3" }, { "name": "poplib.POP3.apop", "domId": "poplib_poplib.POP3.apop" }, { "name": "poplib.POP3.dele", "domId": "poplib_poplib.POP3.dele" }, { "name": "poplib.POP3.getwelcome", "domId": "poplib_poplib.POP3.getwelcome" }, { "name": "poplib.POP3.list", "domId": "poplib_poplib.POP3.list" }, { "name": "poplib.POP3.noop", "domId": "poplib_poplib.POP3.noop" }, { "name": "poplib.POP3.pass_", "domId": "poplib_poplib.POP3.pass_" }, { "name": "poplib.POP3.quit", "domId": "poplib_poplib.POP3.quit" }, { "name": "poplib.POP3.retr", "domId": "poplib_poplib.POP3.retr" }, { "name": "poplib.POP3.rpop", "domId": "poplib_poplib.POP3.rpop" }, { "name": "poplib.POP3.rset", "domId": "poplib_poplib.POP3.rset" }, { "name": "poplib.POP3.set_debuglevel", "domId": "poplib_poplib.POP3.set_debuglevel" }, { "name": "poplib.POP3.stat", "domId": "poplib_poplib.POP3.stat" }, { "name": "poplib.POP3.top", "domId": "poplib_poplib.POP3.top" }, { "name": "poplib.POP3.uidl", "domId": "poplib_poplib.POP3.uidl" }, { "name": "poplib.POP3.user", "domId": "poplib_poplib.POP3.user" }, { "name": "poplib.POP3_SSL", "domId": "poplib_poplib.POP3_SSL" } ] }, { "url": "http://docs.python.org/library/smtpd.html", "title": "smtpd", "html": "
\n

20.13. smtpd — SMTP Server

\n

Source code: Lib/smtpd.py

\n
\n

This module offers several classes to implement SMTP servers. One is a generic\ndo-nothing implementation, which can be overridden, while the other two offer\nspecific mail-sending strategies.

\n
\n

20.13.1. SMTPServer Objects

\n
\n
\nclass smtpd.SMTPServer(localaddr, remoteaddr)
\n

Create a new SMTPServer object, which binds to local address\nlocaladdr. It will treat remoteaddr as an upstream SMTP relayer. It\ninherits from asyncore.dispatcher, and so will insert itself into\nasyncore‘s event loop on instantiation.

\n
\n
\nprocess_message(peer, mailfrom, rcpttos, data)
\n
Raise NotImplementedError exception. Override this in subclasses to\ndo something useful with this message. Whatever was passed in the\nconstructor as remoteaddr will be available as the _remoteaddr\nattribute. peer is the remote host’s address, mailfrom is the envelope\noriginator, rcpttos are the envelope recipients and data is a string\ncontaining the contents of the e-mail (which should be in RFC 2822\nformat).
\n\n
\n\n
\n
\n

20.13.2. DebuggingServer Objects

\n
\n
\nclass smtpd.DebuggingServer(localaddr, remoteaddr)
\n
Create a new debugging server. Arguments are as per SMTPServer.\nMessages will be discarded, and printed on stdout.
\n\n
\n
\n

20.13.3. PureProxy Objects

\n
\n
\nclass smtpd.PureProxy(localaddr, remoteaddr)
\n
Create a new pure proxy server. Arguments are as per SMTPServer.\nEverything will be relayed to remoteaddr. Note that running this has a good\nchance to make you into an open relay, so please be careful.
\n\n
\n
\n

20.13.4. MailmanProxy Objects

\n
\n
\nclass smtpd.MailmanProxy(localaddr, remoteaddr)
\n
Create a new pure proxy server. Arguments are as per SMTPServer.\nEverything will be relayed to remoteaddr, unless local mailman configurations\nknows about an address, in which case it will be handled via mailman. Note that\nrunning this has a good chance to make you into an open relay, so please be\ncareful.
\n\n
\n
", "searchableItems": [ { "name": "smtpd.DebuggingServer", "domId": "smtpd_smtpd.DebuggingServer" }, { "name": "smtpd.MailmanProxy", "domId": "smtpd_smtpd.MailmanProxy" }, { "name": "smtpd.PureProxy", "domId": "smtpd_smtpd.PureProxy" }, { "name": "smtpd.SMTPServer", "domId": "smtpd_smtpd.SMTPServer" }, { "name": "smtpd.SMTPServer.process_message", "domId": "smtpd_smtpd.SMTPServer.process_message" } ] }, { "url": "http://docs.python.org/library/telnetlib.html", "title": "telnetlib", "html": "
\n

20.14. telnetlib — Telnet client

\n

Source code: Lib/telnetlib.py

\n
\n

The telnetlib module provides a Telnet class that implements the\nTelnet protocol. See RFC 854 for details about the protocol. In addition, it\nprovides symbolic constants for the protocol characters (see below), and for the\ntelnet options. The symbolic names of the telnet options follow the definitions\nin arpa/telnet.h, with the leading TELOPT_ removed. For symbolic names\nof options which are traditionally not included in arpa/telnet.h, see the\nmodule source itself.

\n

The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL,\nSE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP\n(Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase\nCharacter), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).

\n
\n
\nclass telnetlib.Telnet([host[, port[, timeout]]])
\n

Telnet represents a connection to a Telnet server. The instance is\ninitially not connected by default; the open() method must be used to\nestablish a connection. Alternatively, the host name and optional port\nnumber can be passed to the constructor, to, in which case the connection to\nthe server will be established before the constructor returns. The optional\ntimeout parameter specifies a timeout in seconds for blocking operations\nlike the connection attempt (if not specified, the global default timeout\nsetting will be used).

\n

Do not reopen an already connected instance.

\n

This class has many read_*() methods. Note that some of them raise\nEOFError when the end of the connection is read, because they can return\nan empty string for other reasons. See the individual descriptions below.

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n

See also

\n
\n
RFC 854 - Telnet Protocol Specification
\n
Definition of the Telnet protocol.
\n
\n
\n
\n

20.14.1. Telnet Objects

\n

Telnet instances have the following methods:

\n
\n
\nTelnet.read_until(expected[, timeout])
\n

Read until a given string, expected, is encountered or until timeout seconds\nhave passed.

\n

When no match is found, return whatever is available instead, possibly the empty\nstring. Raise EOFError if the connection is closed and no cooked data is\navailable.

\n
\n\n
\n
\nTelnet.read_all()
\n
Read all data until EOF; block until connection closed.
\n\n
\n
\nTelnet.read_some()
\n
Read at least one byte of cooked data unless EOF is hit. Return '' if EOF is\nhit. Block if no data is immediately available.
\n\n
\n
\nTelnet.read_very_eager()
\n

Read everything that can be without blocking in I/O (eager).

\n

Raise EOFError if connection closed and no cooked data available. Return\n'' if no cooked data available otherwise. Do not block unless in the midst\nof an IAC sequence.

\n
\n\n
\n
\nTelnet.read_eager()
\n

Read readily available data.

\n

Raise EOFError if connection closed and no cooked data available. Return\n'' if no cooked data available otherwise. Do not block unless in the midst\nof an IAC sequence.

\n
\n\n
\n
\nTelnet.read_lazy()
\n

Process and return data already in the queues (lazy).

\n

Raise EOFError if connection closed and no data available. Return ''\nif no cooked data available otherwise. Do not block unless in the midst of an\nIAC sequence.

\n
\n\n
\n
\nTelnet.read_very_lazy()
\n

Return any data available in the cooked queue (very lazy).

\n

Raise EOFError if connection closed and no data available. Return ''\nif no cooked data available otherwise. This method never blocks.

\n
\n\n
\n
\nTelnet.read_sb_data()
\n

Return the data collected between a SB/SE pair (suboption begin/end). The\ncallback should access these data when it was invoked with a SE command.\nThis method never blocks.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nTelnet.open(host[, port[, timeout]])
\n

Connect to a host. The optional second argument is the port number, which\ndefaults to the standard Telnet port (23). The optional timeout parameter\nspecifies a timeout in seconds for blocking operations like the connection\nattempt (if not specified, the global default timeout setting will be used).

\n

Do not try to reopen an already connected instance.

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n
\nTelnet.msg(msg[, *args])
\n
Print a debug message when the debug level is > 0. If extra arguments are\npresent, they are substituted in the message using the standard string\nformatting operator.
\n\n
\n
\nTelnet.set_debuglevel(debuglevel)
\n
Set the debug level. The higher the value of debuglevel, the more debug\noutput you get (on sys.stdout).
\n\n
\n
\nTelnet.close()
\n
Close the connection.
\n\n
\n
\nTelnet.get_socket()
\n
Return the socket object used internally.
\n\n
\n
\nTelnet.fileno()
\n
Return the file descriptor of the socket object used internally.
\n\n
\n
\nTelnet.write(buffer)
\n
Write a string to the socket, doubling any IAC characters. This can block if the\nconnection is blocked. May raise socket.error if the connection is\nclosed.
\n\n
\n
\nTelnet.interact()
\n
Interaction function, emulates a very dumb Telnet client.
\n\n
\n
\nTelnet.mt_interact()
\n
Multithreaded version of interact().
\n\n
\n
\nTelnet.expect(list[, timeout])
\n

Read until one from a list of a regular expressions matches.

\n

The first argument is a list of regular expressions, either compiled\n(re.RegexObject instances) or uncompiled (strings). The optional second\nargument is a timeout, in seconds; the default is to block indefinitely.

\n

Return a tuple of three items: the index in the list of the first regular\nexpression that matches; the match object returned; and the text read up till\nand including the match.

\n

If end of file is found and no text was read, raise EOFError. Otherwise,\nwhen nothing matches, return (-1, None, text) where text is the text\nreceived so far (may be the empty string if a timeout happened).

\n

If a regular expression ends with a greedy match (such as .*) or if more\nthan one expression can match the same input, the results are\nnon-deterministic, and may depend on the I/O timing.

\n
\n\n
\n
\nTelnet.set_option_negotiation_callback(callback)
\n
Each time a telnet option is read on the input flow, this callback (if set) is\ncalled with the following parameters : callback(telnet socket, command\n(DO/DONT/WILL/WONT), option). No other action is done afterwards by telnetlib.
\n\n
\n
\n

20.14.2. Telnet Example

\n

A simple example illustrating typical use:

\n
import getpass\nimport sys\nimport telnetlib\n\nHOST = "localhost"\nuser = raw_input("Enter your remote account: ")\npassword = getpass.getpass()\n\ntn = telnetlib.Telnet(HOST)\n\ntn.read_until("login: ")\ntn.write(user + "\\n")\nif password:\n    tn.read_until("Password: ")\n    tn.write(password + "\\n")\n\ntn.write("ls\\n")\ntn.write("exit\\n")\n\nprint tn.read_all()\n
\n
\n
\n
", "searchableItems": [ { "name": "telnetlib.Telnet", "domId": "telnetlib_telnetlib.Telnet" }, { "name": "telnetlib.Telnet.close", "domId": "telnetlib_telnetlib.Telnet.close" }, { "name": "telnetlib.Telnet.expect", "domId": "telnetlib_telnetlib.Telnet.expect" }, { "name": "telnetlib.Telnet.fileno", "domId": "telnetlib_telnetlib.Telnet.fileno" }, { "name": "telnetlib.Telnet.get_socket", "domId": "telnetlib_telnetlib.Telnet.get_socket" }, { "name": "telnetlib.Telnet.interact", "domId": "telnetlib_telnetlib.Telnet.interact" }, { "name": "telnetlib.Telnet.msg", "domId": "telnetlib_telnetlib.Telnet.msg" }, { "name": "telnetlib.Telnet.mt_interact", "domId": "telnetlib_telnetlib.Telnet.mt_interact" }, { "name": "telnetlib.Telnet.open", "domId": "telnetlib_telnetlib.Telnet.open" }, { "name": "telnetlib.Telnet.read_all", "domId": "telnetlib_telnetlib.Telnet.read_all" }, { "name": "telnetlib.Telnet.read_eager", "domId": "telnetlib_telnetlib.Telnet.read_eager" }, { "name": "telnetlib.Telnet.read_lazy", "domId": "telnetlib_telnetlib.Telnet.read_lazy" }, { "name": "telnetlib.Telnet.read_sb_data", "domId": "telnetlib_telnetlib.Telnet.read_sb_data" }, { "name": "telnetlib.Telnet.read_some", "domId": "telnetlib_telnetlib.Telnet.read_some" }, { "name": "telnetlib.Telnet.read_until", "domId": "telnetlib_telnetlib.Telnet.read_until" }, { "name": "telnetlib.Telnet.read_very_eager", "domId": "telnetlib_telnetlib.Telnet.read_very_eager" }, { "name": "telnetlib.Telnet.read_very_lazy", "domId": "telnetlib_telnetlib.Telnet.read_very_lazy" }, { "name": "telnetlib.Telnet.set_debuglevel", "domId": "telnetlib_telnetlib.Telnet.set_debuglevel" }, { "name": "telnetlib.Telnet.set_option_negotiation_callback", "domId": "telnetlib_telnetlib.Telnet.set_option_negotiation_callback" }, { "name": "telnetlib.Telnet.write", "domId": "telnetlib_telnetlib.Telnet.write" } ] }, { "url": "http://docs.python.org/library/imaplib.html", "title": "imaplib", "html": "
\n

20.10. imaplib — IMAP4 protocol client

\n

Source code: Lib/imaplib.py

\n
\n

This module defines three classes, IMAP4, IMAP4_SSL and\nIMAP4_stream, which encapsulate a connection to an IMAP4 server and\nimplement a large subset of the IMAP4rev1 client protocol as defined in\nRFC 2060. It is backward compatible with IMAP4 (RFC 1730) servers, but\nnote that the STATUS command is not supported in IMAP4.

\n

Three classes are provided by the imaplib module, IMAP4 is the\nbase class:

\n
\n
\nclass imaplib.IMAP4([host[, port]])
\n
This class implements the actual IMAP4 protocol. The connection is created and\nprotocol version (IMAP4 or IMAP4rev1) is determined when the instance is\ninitialized. If host is not specified, '' (the local host) is used. If\nport is omitted, the standard IMAP4 port (143) is used.
\n\n

Three exceptions are defined as attributes of the IMAP4 class:

\n
\n
\nexception IMAP4.error
\n
Exception raised on any errors. The reason for the exception is passed to the\nconstructor as a string.
\n\n
\n
\nexception IMAP4.abort
\n
IMAP4 server errors cause this exception to be raised. This is a sub-class of\nIMAP4.error. Note that closing the instance and instantiating a new one\nwill usually allow recovery from this exception.
\n\n
\n
\nexception IMAP4.readonly
\n
This exception is raised when a writable mailbox has its status changed by the\nserver. This is a sub-class of IMAP4.error. Some other client now has\nwrite permission, and the mailbox will need to be re-opened to re-obtain write\npermission.
\n\n

There’s also a subclass for secure connections:

\n
\n
\nclass imaplib.IMAP4_SSL([host[, port[, keyfile[, certfile]]]])
\n
This is a subclass derived from IMAP4 that connects over an SSL\nencrypted socket (to use this class you need a socket module that was compiled\nwith SSL support). If host is not specified, '' (the local host) is used.\nIf port is omitted, the standard IMAP4-over-SSL port (993) is used. keyfile\nand certfile are also optional - they can contain a PEM formatted private key\nand certificate chain file for the SSL connection.
\n\n

The second subclass allows for connections created by a child process:

\n
\n
\nclass imaplib.IMAP4_stream(command)
\n

This is a subclass derived from IMAP4 that connects to the\nstdin/stdout file descriptors created by passing command to\nos.popen2().

\n

\nNew in version 2.3.

\n
\n\n

The following utility functions are defined:

\n
\n
\nimaplib.Internaldate2tuple(datestr)
\n
Parse an IMAP4 INTERNALDATE string and return corresponding local\ntime. The return value is a time.struct_time instance or\nNone if the string has wrong format.
\n\n
\n
\nimaplib.Int2AP(num)
\n
Converts an integer into a string representation using characters from the set\n[A .. P].
\n\n
\n
\nimaplib.ParseFlags(flagstr)
\n
Converts an IMAP4 FLAGS response to a tuple of individual flags.
\n\n
\n
\nimaplib.Time2Internaldate(date_time)
\n
Convert date_time to an IMAP4 INTERNALDATE representation. The\nreturn value is a string in the form: "DD-Mmm-YYYY HH:MM:SS\n+HHMM" (including double-quotes). The date_time argument can be a\nnumber (int or float) representing seconds since epoch (as returned\nby time.time()), a 9-tuple representing local time (as returned by\ntime.localtime()), or a double-quoted string. In the last case, it\nis assumed to already be in the correct format.
\n\n

Note that IMAP4 message numbers change as the mailbox changes; in particular,\nafter an EXPUNGE command performs deletions the remaining messages are\nrenumbered. So it is highly advisable to use UIDs instead, with the UID command.

\n

At the end of the module, there is a test section that contains a more extensive\nexample of usage.

\n
\n

See also

\n

Documents describing the protocol, and sources and binaries for servers\nimplementing it, can all be found at the University of Washington’s IMAP\nInformation Center (http://www.washington.edu/imap/).

\n
\n
\n

20.10.1. IMAP4 Objects

\n

All IMAP4rev1 commands are represented by methods of the same name, either\nupper-case or lower-case.

\n

All arguments to commands are converted to strings, except for AUTHENTICATE,\nand the last argument to APPEND which is passed as an IMAP4 literal. If\nnecessary (the string contains IMAP4 protocol-sensitive characters and isn’t\nenclosed with either parentheses or double quotes) each string is quoted.\nHowever, the password argument to the LOGIN command is always quoted. If\nyou want to avoid having an argument string quoted (eg: the flags argument to\nSTORE) then enclose the string in parentheses (eg: r'(\\Deleted)').

\n

Each command returns a tuple: (type, [data, ...]) where type is usually\n'OK' or 'NO', and data is either the text from the command response,\nor mandated results from the command. Each data is either a string, or a\ntuple. If a tuple, then the first part is the header of the response, and the\nsecond part contains the data (ie: ‘literal’ value).

\n

The message_set options to commands below is a string specifying one or more\nmessages to be acted upon. It may be a simple message number ('1'), a range\nof message numbers ('2:4'), or a group of non-contiguous ranges separated by\ncommas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite\nupper bound ('3:*').

\n

An IMAP4 instance has the following methods:

\n
\n
\nIMAP4.append(mailbox, flags, date_time, message)
\n
Append message to named mailbox.
\n\n
\n
\nIMAP4.authenticate(mechanism, authobject)
\n

Authenticate command — requires response processing.

\n

mechanism specifies which authentication mechanism is to be used - it should\nappear in the instance variable capabilities in the form AUTH=mechanism.

\n

authobject must be a callable object:

\n
data = authobject(response)\n
\n
\n

It will be called to process server continuation responses. It should return\ndata that will be encoded and sent to server. It should return None if\nthe client abort response * should be sent instead.

\n
\n\n
\n
\nIMAP4.check()
\n
Checkpoint mailbox on server.
\n\n
\n
\nIMAP4.close()
\n
Close currently selected mailbox. Deleted messages are removed from writable\nmailbox. This is the recommended command before LOGOUT.
\n\n
\n
\nIMAP4.copy(message_set, new_mailbox)
\n
Copy message_set messages onto end of new_mailbox.
\n\n
\n
\nIMAP4.create(mailbox)
\n
Create new mailbox named mailbox.
\n\n
\n
\nIMAP4.delete(mailbox)
\n
Delete old mailbox named mailbox.
\n\n
\n
\nIMAP4.deleteacl(mailbox, who)
\n

Delete the ACLs (remove any rights) set for who on mailbox.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nIMAP4.expunge()
\n
Permanently remove deleted items from selected mailbox. Generates an EXPUNGE\nresponse for each deleted message. Returned data contains a list of EXPUNGE\nmessage numbers in order received.
\n\n
\n
\nIMAP4.fetch(message_set, message_parts)
\n
Fetch (parts of) messages. message_parts should be a string of message part\nnames enclosed within parentheses, eg: "(UID BODY[TEXT])". Returned data\nare tuples of message part envelope and data.
\n\n
\n
\nIMAP4.getacl(mailbox)
\n
Get the ACLs for mailbox. The method is non-standard, but is supported\nby the Cyrus server.
\n\n
\n
\nIMAP4.getannotation(mailbox, entry, attribute)
\n

Retrieve the specified ANNOTATIONs for mailbox. The method is\nnon-standard, but is supported by the Cyrus server.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nIMAP4.getquota(root)
\n

Get the quota root‘s resource usage and limits. This method is part of the\nIMAP4 QUOTA extension defined in rfc2087.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nIMAP4.getquotaroot(mailbox)
\n

Get the list of quota roots for the named mailbox. This method is part\nof the IMAP4 QUOTA extension defined in rfc2087.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nIMAP4.list([directory[, pattern]])
\n
List mailbox names in directory matching pattern. directory defaults to\nthe top-level mail folder, and pattern defaults to match anything. Returned\ndata contains a list of LIST responses.
\n\n
\n
\nIMAP4.login(user, password)
\n
Identify the client using a plaintext password. The password will be quoted.
\n\n
\n
\nIMAP4.login_cram_md5(user, password)
\n

Force use of CRAM-MD5 authentication when identifying the client to protect\nthe password. Will only work if the server CAPABILITY response includes the\nphrase AUTH=CRAM-MD5.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nIMAP4.logout()
\n
Shutdown connection to server. Returns server BYE response.
\n\n
\n
\nIMAP4.lsub([directory[, pattern]])
\n
List subscribed mailbox names in directory matching pattern. directory\ndefaults to the top level directory and pattern defaults to match any mailbox.\nReturned data are tuples of message part envelope and data.
\n\n
\n
\nIMAP4.myrights(mailbox)
\n

Show my ACLs for a mailbox (i.e. the rights that I have on mailbox).

\n

\nNew in version 2.4.

\n
\n\n
\n
\nIMAP4.namespace()
\n

Returns IMAP namespaces as defined in RFC2342.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nIMAP4.noop()
\n
Send NOOP to server.
\n\n
\n
\nIMAP4.open(host, port)
\n
Opens socket to port at host. This method is implicitly called by\nthe IMAP4 constructor. The connection objects established by this\nmethod will be used in the read, readline, send, and shutdown\nmethods. You may override this method.
\n\n
\n
\nIMAP4.partial(message_num, message_part, start, length)
\n
Fetch truncated part of a message. Returned data is a tuple of message part\nenvelope and data.
\n\n
\n
\nIMAP4.proxyauth(user)
\n

Assume authentication as user. Allows an authorised administrator to proxy\ninto any user’s mailbox.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nIMAP4.read(size)
\n
Reads size bytes from the remote server. You may override this method.
\n\n
\n
\nIMAP4.readline()
\n
Reads one line from the remote server. You may override this method.
\n\n
\n
\nIMAP4.recent()
\n
Prompt server for an update. Returned data is None if no new messages, else\nvalue of RECENT response.
\n\n
\n
\nIMAP4.rename(oldmailbox, newmailbox)
\n
Rename mailbox named oldmailbox to newmailbox.
\n\n
\n
\nIMAP4.response(code)
\n
Return data for response code if received, or None. Returns the given\ncode, instead of the usual type.
\n\n
\n
\nIMAP4.search(charset, criterion[, ...])
\n

Search mailbox for matching messages. charset may be None, in which case\nno CHARSET will be specified in the request to the server. The IMAP\nprotocol requires that at least one criterion be specified; an exception will be\nraised when the server returns an error.

\n

Example:

\n
# M is a connected IMAP4 instance...\ntyp, msgnums = M.search(None, 'FROM', '"LDJ"')\n\n# or:\ntyp, msgnums = M.search(None, '(FROM "LDJ")')\n
\n
\n
\n\n
\n
\nIMAP4.select([mailbox[, readonly]])
\n
Select a mailbox. Returned data is the count of messages in mailbox\n(EXISTS response). The default mailbox is 'INBOX'. If the readonly\nflag is set, modifications to the mailbox are not allowed.
\n\n
\n
\nIMAP4.send(data)
\n
Sends data to the remote server. You may override this method.
\n\n
\n
\nIMAP4.setacl(mailbox, who, what)
\n
Set an ACL for mailbox. The method is non-standard, but is supported by\nthe Cyrus server.
\n\n
\n
\nIMAP4.setannotation(mailbox, entry, attribute[, ...])
\n

Set ANNOTATIONs for mailbox. The method is non-standard, but is\nsupported by the Cyrus server.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nIMAP4.setquota(root, limits)
\n

Set the quota root‘s resource limits. This method is part of the IMAP4\nQUOTA extension defined in rfc2087.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nIMAP4.shutdown()
\n
Close connection established in open. This method is implicitly\ncalled by IMAP4.logout(). You may override this method.
\n\n
\n
\nIMAP4.socket()
\n
Returns socket instance used to connect to server.
\n\n
\n
\nIMAP4.sort(sort_criteria, charset, search_criterion[, ...])
\n

The sort command is a variant of search with sorting semantics for the\nresults. Returned data contains a space separated list of matching message\nnumbers.

\n

Sort has two arguments before the search_criterion argument(s); a\nparenthesized list of sort_criteria, and the searching charset. Note that\nunlike search, the searching charset argument is mandatory. There is also\na uid sort command which corresponds to sort the way that uid search\ncorresponds to search. The sort command first searches the mailbox for\nmessages that match the given searching criteria using the charset argument for\nthe interpretation of strings in the searching criteria. It then returns the\nnumbers of matching messages.

\n

This is an IMAP4rev1 extension command.

\n
\n\n
\n
\nIMAP4.status(mailbox, names)
\n
Request named status conditions for mailbox.
\n\n
\n
\nIMAP4.store(message_set, command, flag_list)
\n

Alters flag dispositions for messages in mailbox. command is specified by\nsection 6.4.6 of RFC 2060 as being one of “FLAGS”, “+FLAGS”, or “-FLAGS”,\noptionally with a suffix of “.SILENT”.

\n

For example, to set the delete flag on all messages:

\n
typ, data = M.search(None, 'ALL')\nfor num in data[0].split():\n   M.store(num, '+FLAGS', '\\\\Deleted')\nM.expunge()\n
\n
\n
\n\n
\n
\nIMAP4.subscribe(mailbox)
\n
Subscribe to new mailbox.
\n\n
\n
\nIMAP4.thread(threading_algorithm, charset, search_criterion[, ...])
\n

The thread command is a variant of search with threading semantics for\nthe results. Returned data contains a space separated list of thread members.

\n

Thread members consist of zero or more messages numbers, delimited by spaces,\nindicating successive parent and child.

\n

Thread has two arguments before the search_criterion argument(s); a\nthreading_algorithm, and the searching charset. Note that unlike\nsearch, the searching charset argument is mandatory. There is also a\nuid thread command which corresponds to thread the way that uid\nsearch corresponds to search. The thread command first searches the\nmailbox for messages that match the given searching criteria using the charset\nargument for the interpretation of strings in the searching criteria. It then\nreturns the matching messages threaded according to the specified threading\nalgorithm.

\n

This is an IMAP4rev1 extension command.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nIMAP4.uid(command, arg[, ...])
\n
Execute command args with messages identified by UID, rather than message\nnumber. Returns response appropriate to command. At least one argument must be\nsupplied; if none are provided, the server will return an error and an exception\nwill be raised.
\n\n
\n
\nIMAP4.unsubscribe(mailbox)
\n
Unsubscribe from old mailbox.
\n\n
\n
\nIMAP4.xatom(name[, arg[, ...]])
\n
Allow simple extension commands notified by server in CAPABILITY response.
\n\n

Instances of IMAP4_SSL have just one additional method:

\n
\n
\nIMAP4_SSL.ssl()
\n
Returns SSLObject instance used for the secure connection with the server.
\n\n

The following attributes are defined on instances of IMAP4:

\n
\n
\nIMAP4.PROTOCOL_VERSION
\n
The most recent supported protocol in the CAPABILITY response from the\nserver.
\n\n
\n
\nIMAP4.debug
\n
Integer value to control debugging output. The initialize value is taken from\nthe module variable Debug. Values greater than three trace each command.
\n\n
\n
\n

20.10.2. IMAP4 Example

\n

Here is a minimal example (without error checking) that opens a mailbox and\nretrieves and prints all messages:

\n
import getpass, imaplib\n\nM = imaplib.IMAP4()\nM.login(getpass.getuser(), getpass.getpass())\nM.select()\ntyp, data = M.search(None, 'ALL')\nfor num in data[0].split():\n    typ, data = M.fetch(num, '(RFC822)')\n    print 'Message %s\\n%s\\n'  (num, data[0][1])\nM.close()\nM.logout()\n
\n
\n
\n
", "searchableItems": [ { "name": "imaplib.IMAP4", "domId": "imaplib_imaplib.IMAP4" }, { "name": "imaplib.IMAP4.append", "domId": "imaplib_imaplib.IMAP4.append" }, { "name": "imaplib.IMAP4.authenticate", "domId": "imaplib_imaplib.IMAP4.authenticate" }, { "name": "imaplib.IMAP4.check", "domId": "imaplib_imaplib.IMAP4.check" }, { "name": "imaplib.IMAP4.close", "domId": "imaplib_imaplib.IMAP4.close" }, { "name": "imaplib.IMAP4.copy", "domId": "imaplib_imaplib.IMAP4.copy" }, { "name": "imaplib.IMAP4.create", "domId": "imaplib_imaplib.IMAP4.create" }, { "name": "imaplib.IMAP4.delete", "domId": "imaplib_imaplib.IMAP4.delete" }, { "name": "imaplib.IMAP4.deleteacl", "domId": "imaplib_imaplib.IMAP4.deleteacl" }, { "name": "imaplib.IMAP4.expunge", "domId": "imaplib_imaplib.IMAP4.expunge" }, { "name": "imaplib.IMAP4.fetch", "domId": "imaplib_imaplib.IMAP4.fetch" }, { "name": "imaplib.IMAP4.getacl", "domId": "imaplib_imaplib.IMAP4.getacl" }, { "name": "imaplib.IMAP4.getannotation", "domId": "imaplib_imaplib.IMAP4.getannotation" }, { "name": "imaplib.IMAP4.getquota", "domId": "imaplib_imaplib.IMAP4.getquota" }, { "name": "imaplib.IMAP4.getquotaroot", "domId": "imaplib_imaplib.IMAP4.getquotaroot" }, { "name": "imaplib.IMAP4.list", "domId": "imaplib_imaplib.IMAP4.list" }, { "name": "imaplib.IMAP4.login", "domId": "imaplib_imaplib.IMAP4.login" }, { "name": "imaplib.IMAP4.login_cram_md5", "domId": "imaplib_imaplib.IMAP4.login_cram_md5" }, { "name": "imaplib.IMAP4.logout", "domId": "imaplib_imaplib.IMAP4.logout" }, { "name": "imaplib.IMAP4.lsub", "domId": "imaplib_imaplib.IMAP4.lsub" }, { "name": "imaplib.IMAP4.myrights", "domId": "imaplib_imaplib.IMAP4.myrights" }, { "name": "imaplib.IMAP4.namespace", "domId": "imaplib_imaplib.IMAP4.namespace" }, { "name": "imaplib.IMAP4.noop", "domId": "imaplib_imaplib.IMAP4.noop" }, { "name": "imaplib.IMAP4.open", "domId": "imaplib_imaplib.IMAP4.open" }, { "name": "imaplib.IMAP4.partial", "domId": "imaplib_imaplib.IMAP4.partial" }, { "name": "imaplib.IMAP4.proxyauth", "domId": "imaplib_imaplib.IMAP4.proxyauth" }, { "name": "imaplib.IMAP4.read", "domId": "imaplib_imaplib.IMAP4.read" }, { "name": "imaplib.IMAP4.readline", "domId": "imaplib_imaplib.IMAP4.readline" }, { "name": "imaplib.IMAP4.recent", "domId": "imaplib_imaplib.IMAP4.recent" }, { "name": "imaplib.IMAP4.rename", "domId": "imaplib_imaplib.IMAP4.rename" }, { "name": "imaplib.IMAP4.response", "domId": "imaplib_imaplib.IMAP4.response" }, { "name": "imaplib.IMAP4.search", "domId": "imaplib_imaplib.IMAP4.search" }, { "name": "imaplib.IMAP4.select", "domId": "imaplib_imaplib.IMAP4.select" }, { "name": "imaplib.IMAP4.send", "domId": "imaplib_imaplib.IMAP4.send" }, { "name": "imaplib.IMAP4.setacl", "domId": "imaplib_imaplib.IMAP4.setacl" }, { "name": "imaplib.IMAP4.setannotation", "domId": "imaplib_imaplib.IMAP4.setannotation" }, { "name": "imaplib.IMAP4.setquota", "domId": "imaplib_imaplib.IMAP4.setquota" }, { "name": "imaplib.IMAP4.shutdown", "domId": "imaplib_imaplib.IMAP4.shutdown" }, { "name": "imaplib.IMAP4.socket", "domId": "imaplib_imaplib.IMAP4.socket" }, { "name": "imaplib.IMAP4.sort", "domId": "imaplib_imaplib.IMAP4.sort" }, { "name": "imaplib.IMAP4.status", "domId": "imaplib_imaplib.IMAP4.status" }, { "name": "imaplib.IMAP4.store", "domId": "imaplib_imaplib.IMAP4.store" }, { "name": "imaplib.IMAP4.subscribe", "domId": "imaplib_imaplib.IMAP4.subscribe" }, { "name": "imaplib.IMAP4.thread", "domId": "imaplib_imaplib.IMAP4.thread" }, { "name": "imaplib.IMAP4.uid", "domId": "imaplib_imaplib.IMAP4.uid" }, { "name": "imaplib.IMAP4.unsubscribe", "domId": "imaplib_imaplib.IMAP4.unsubscribe" }, { "name": "imaplib.IMAP4.xatom", "domId": "imaplib_imaplib.IMAP4.xatom" }, { "name": "imaplib.IMAP4_SSL", "domId": "imaplib_imaplib.IMAP4_SSL" }, { "name": "imaplib.IMAP4_SSL.ssl", "domId": "imaplib_imaplib.IMAP4_SSL.ssl" }, { "name": "imaplib.IMAP4_stream", "domId": "imaplib_imaplib.IMAP4_stream" }, { "name": "imaplib.Int2AP", "domId": "imaplib_imaplib.Int2AP" }, { "name": "imaplib.Internaldate2tuple", "domId": "imaplib_imaplib.Internaldate2tuple" }, { "name": "imaplib.ParseFlags", "domId": "imaplib_imaplib.ParseFlags" }, { "name": "imaplib.Time2Internaldate", "domId": "imaplib_imaplib.Time2Internaldate" } ] }, { "url": "http://docs.python.org/library/nntplib.html", "title": "nntplib", "html": "
\n

20.11. nntplib — NNTP protocol client

\n

Source code: Lib/nntplib.py

\n
\n

This module defines the class NNTP which implements the client side of\nthe NNTP protocol. It can be used to implement a news reader or poster, or\nautomated news processors. For more information on NNTP (Network News Transfer\nProtocol), see Internet RFC 977.

\n

Here are two small examples of how it can be used. To list some statistics\nabout a newsgroup and print the subjects of the last 10 articles:

\n
>>> s = NNTP('news.gmane.org')\n>>> resp, count, first, last, name = s.group('gmane.comp.python.committers')\n>>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last\nGroup gmane.comp.python.committers has 1071 articles, range 1 to 1071\n>>> resp, subs = s.xhdr('subject', first + '-' + last)\n>>> for id, sub in subs[-10:]: print id, sub\n...\n1062 Re: Mercurial Status?\n1063 Re: [python-committers]  (Windows) buildbots on 3.x\n1064 Re: Mercurial Status?\n1065 Re: Mercurial Status?\n1066 Python 2.6.6 status\n1067 Commit Privileges for Ask Solem\n1068 Re: Commit Privileges for Ask Solem\n1069 Re: Commit Privileges for Ask Solem\n1070 Re: Commit Privileges for Ask Solem\n1071 2.6.6 rc 2\n>>> s.quit()\n'205 Bye!'\n
\n
\n

To post an article from a file (this assumes that the article has valid\nheaders, and that you have right to post on the particular newsgroup):

\n
>>> s = NNTP('news.gmane.org')\n>>> f = open('/tmp/article')\n>>> s.post(f)\n'240 Article posted successfully.'\n>>> s.quit()\n'205 Bye!'\n
\n
\n

The module itself defines the following items:

\n
\n
\nclass nntplib.NNTP(host[, port[, user[, password[, readermode][, usenetrc]]]])
\n

Return a new instance of the NNTP class, representing a connection\nto the NNTP server running on host host, listening at port port. The\ndefault port is 119. If the optional user and password are provided,\nor if suitable credentials are present in /.netrc and the optional\nflag usenetrc is true (the default), the AUTHINFO USER and AUTHINFO\nPASS commands are used to identify and authenticate the user to the server.\nIf the optional flag readermode is true, then a mode reader command is\nsent before authentication is performed. Reader mode is sometimes necessary\nif you are connecting to an NNTP server on the local machine and intend to\ncall reader-specific commands, such as group. If you get unexpected\nNNTPPermanentErrors, you might need to set readermode.\nreadermode defaults to None. usenetrc defaults to True.

\n

\nChanged in version 2.4: usenetrc argument added.

\n
\n\n
\n
\nexception nntplib.NNTPError
\n
Derived from the standard exception Exception, this is the base class for\nall exceptions raised by the nntplib module.
\n\n
\n
\nexception nntplib.NNTPReplyError
\n
Exception raised when an unexpected reply is received from the server. For\nbackwards compatibility, the exception error_reply is equivalent to this\nclass.
\n\n
\n
\nexception nntplib.NNTPTemporaryError
\n
Exception raised when an error code in the range 400–499 is received. For\nbackwards compatibility, the exception error_temp is equivalent to this\nclass.
\n\n
\n
\nexception nntplib.NNTPPermanentError
\n
Exception raised when an error code in the range 500–599 is received. For\nbackwards compatibility, the exception error_perm is equivalent to this\nclass.
\n\n
\n
\nexception nntplib.NNTPProtocolError
\n
Exception raised when a reply is received from the server that does not begin\nwith a digit in the range 1–5. For backwards compatibility, the exception\nerror_proto is equivalent to this class.
\n\n
\n
\nexception nntplib.NNTPDataError
\n
Exception raised when there is some error in the response data. For backwards\ncompatibility, the exception error_data is equivalent to this class.
\n\n
\n

20.11.1. NNTP Objects

\n

NNTP instances have the following methods. The response that is returned as\nthe first item in the return tuple of almost all methods is the server’s\nresponse: a string beginning with a three-digit code. If the server’s response\nindicates an error, the method raises one of the above exceptions.

\n
\n
\nNNTP.getwelcome()
\n
Return the welcome message sent by the server in reply to the initial\nconnection. (This message sometimes contains disclaimers or help information\nthat may be relevant to the user.)
\n\n
\n
\nNNTP.set_debuglevel(level)
\n
Set the instance’s debugging level. This controls the amount of debugging\noutput printed. The default, 0, produces no debugging output. A value of\n1 produces a moderate amount of debugging output, generally a single line\nper request or response. A value of 2 or higher produces the maximum amount\nof debugging output, logging each line sent and received on the connection\n(including message text).
\n\n
\n
\nNNTP.newgroups(date, time[, file])
\n
Send a NEWGROUPS command. The date argument should be a string of the\nform 'yymmdd' indicating the date, and time should be a string of the form\n'hhmmss' indicating the time. Return a pair (response, groups) where\ngroups is a list of group names that are new since the given date and time. If\nthe file parameter is supplied, then the output of the NEWGROUPS command\nis stored in a file. If file is a string, then the method will open a file\nobject with that name, write to it then close it. If file is a file object,\nthen it will start calling write() on it to store the lines of the command\noutput. If file is supplied, then the returned list is an empty list.
\n\n
\n
\nNNTP.newnews(group, date, time[, file])
\n
Send a NEWNEWS command. Here, group is a group name or '*', and\ndate and time have the same meaning as for newgroups(). Return a pair\n(response, articles) where articles is a list of message ids. If the\nfile parameter is supplied, then the output of the NEWNEWS command is\nstored in a file. If file is a string, then the method will open a file\nobject with that name, write to it then close it. If file is a file object,\nthen it will start calling write() on it to store the lines of the command\noutput. If file is supplied, then the returned list is an empty list.
\n\n
\n
\nNNTP.list([file])
\n
Send a LIST command. Return a pair (response, list) where list is a\nlist of tuples. Each tuple has the form (group, last, first, flag), where\ngroup is a group name, last and first are the last and first article\nnumbers (as strings), and flag is 'y' if posting is allowed, 'n' if\nnot, and 'm' if the newsgroup is moderated. (Note the ordering: last,\nfirst.) If the file parameter is supplied, then the output of the LIST\ncommand is stored in a file. If file is a string, then the method will open\na file object with that name, write to it then close it. If file is a file\nobject, then it will start calling write() on it to store the lines of the\ncommand output. If file is supplied, then the returned list is an empty\nlist.
\n\n
\n
\nNNTP.descriptions(grouppattern)
\n

Send a LIST NEWSGROUPS command, where grouppattern is a wildmat string as\nspecified in RFC2980 (it’s essentially the same as DOS or UNIX shell wildcard\nstrings). Return a pair (response, list), where list is a list of tuples\ncontaining (name, title).

\n

\nNew in version 2.4.

\n
\n\n
\n
\nNNTP.description(group)
\n

Get a description for a single group group. If more than one group matches\n(if ‘group’ is a real wildmat string), return the first match. If no group\nmatches, return an empty string.

\n

This elides the response code from the server. If the response code is needed,\nuse descriptions().

\n

\nNew in version 2.4.

\n
\n\n
\n
\nNNTP.group(name)
\n
Send a GROUP command, where name is the group name. Return a tuple\n(response, count, first, last, name) where count is the (estimated) number\nof articles in the group, first is the first article number in the group,\nlast is the last article number in the group, and name is the group name.\nThe numbers are returned as strings.
\n\n
\n
\nNNTP.help([file])
\n
Send a HELP command. Return a pair (response, list) where list is a\nlist of help strings. If the file parameter is supplied, then the output of\nthe HELP command is stored in a file. If file is a string, then the\nmethod will open a file object with that name, write to it then close it. If\nfile is a file object, then it will start calling write() on it to store\nthe lines of the command output. If file is supplied, then the returned list\nis an empty list.
\n\n
\n
\nNNTP.stat(id)
\n
Send a STAT command, where id is the message id (enclosed in '<' and\n'>') or an article number (as a string). Return a triple (response,\nnumber, id) where number is the article number (as a string) and id is the\nmessage id (enclosed in '<' and '>').
\n\n
\n
\nNNTP.next()
\n
Send a NEXT command. Return as for stat().
\n\n
\n
\nNNTP.last()
\n
Send a LAST command. Return as for stat().
\n\n
\n
\nNNTP.head(id)
\n
Send a HEAD command, where id has the same meaning as for stat().\nReturn a tuple (response, number, id, list) where the first three are the\nsame as for stat(), and list is a list of the article’s headers (an\nuninterpreted list of lines, without trailing newlines).
\n\n
\n
\nNNTP.body(id[, file])
\n
Send a BODY command, where id has the same meaning as for stat().\nIf the file parameter is supplied, then the body is stored in a file. If\nfile is a string, then the method will open a file object with that name,\nwrite to it then close it. If file is a file object, then it will start\ncalling write() on it to store the lines of the body. Return as for\nhead(). If file is supplied, then the returned list is an empty list.
\n\n
\n
\nNNTP.article(id)
\n
Send an ARTICLE command, where id has the same meaning as for\nstat(). Return as for head().
\n\n
\n
\nNNTP.slave()
\n
Send a SLAVE command. Return the server’s response.
\n\n
\n
\nNNTP.xhdr(header, string[, file])
\n
Send an XHDR command. This command is not defined in the RFC but is a\ncommon extension. The header argument is a header keyword, e.g.\n'subject'. The string argument should have the form 'first-last'\nwhere first and last are the first and last article numbers to search.\nReturn a pair (response, list), where list is a list of pairs (id,\ntext), where id is an article number (as a string) and text is the text of\nthe requested header for that article. If the file parameter is supplied, then\nthe output of the XHDR command is stored in a file. If file is a string,\nthen the method will open a file object with that name, write to it then close\nit. If file is a file object, then it will start calling write() on it\nto store the lines of the command output. If file is supplied, then the\nreturned list is an empty list.
\n\n
\n
\nNNTP.post(file)
\n
Post an article using the POST command. The file argument is an open file\nobject which is read until EOF using its readline() method. It should be\na well-formed news article, including the required headers. The post()\nmethod automatically escapes lines beginning with ..
\n\n
\n
\nNNTP.ihave(id, file)
\n
Send an IHAVE command. id is a message id (enclosed in '<' and\n'>'). If the response is not an error, treat file exactly as for the\npost() method.
\n\n
\n
\nNNTP.date()
\n
Return a triple (response, date, time), containing the current date and time\nin a form suitable for the newnews() and newgroups() methods. This\nis an optional NNTP extension, and may not be supported by all servers.
\n\n
\n
\nNNTP.xgtitle(name[, file])
\n

Process an XGTITLE command, returning a pair (response, list), where\nlist is a list of tuples containing (name, title). If the file parameter\nis supplied, then the output of the XGTITLE command is stored in a file.\nIf file is a string, then the method will open a file object with that name,\nwrite to it then close it. If file is a file object, then it will start\ncalling write() on it to store the lines of the command output. If file\nis supplied, then the returned list is an empty list. This is an optional NNTP\nextension, and may not be supported by all servers.

\n

RFC2980 says “It is suggested that this extension be deprecated”. Use\ndescriptions() or description() instead.

\n
\n\n
\n
\nNNTP.xover(start, end[, file])
\n
Return a pair (resp, list). list is a list of tuples, one for each\narticle in the range delimited by the start and end article numbers. Each\ntuple is of the form (article number, subject, poster, date, id, references,\nsize, lines). If the file parameter is supplied, then the output of the\nXOVER command is stored in a file. If file is a string, then the method\nwill open a file object with that name, write to it then close it. If file\nis a file object, then it will start calling write() on it to store the\nlines of the command output. If file is supplied, then the returned list is\nan empty list. This is an optional NNTP extension, and may not be supported by\nall servers.
\n\n
\n
\nNNTP.xpath(id)
\n
Return a pair (resp, path), where path is the directory path to the\narticle with message ID id. This is an optional NNTP extension, and may not\nbe supported by all servers.
\n\n
\n
\nNNTP.quit()
\n
Send a QUIT command and close the connection. Once this method has been\ncalled, no other methods of the NNTP object should be called.
\n\n
\n
", "searchableItems": [ { "name": "nntplib.NNTP", "domId": "nntplib_nntplib.NNTP" }, { "name": "nntplib.NNTP.article", "domId": "nntplib_nntplib.NNTP.article" }, { "name": "nntplib.NNTP.body", "domId": "nntplib_nntplib.NNTP.body" }, { "name": "nntplib.NNTP.date", "domId": "nntplib_nntplib.NNTP.date" }, { "name": "nntplib.NNTP.description", "domId": "nntplib_nntplib.NNTP.description" }, { "name": "nntplib.NNTP.descriptions", "domId": "nntplib_nntplib.NNTP.descriptions" }, { "name": "nntplib.NNTP.getwelcome", "domId": "nntplib_nntplib.NNTP.getwelcome" }, { "name": "nntplib.NNTP.group", "domId": "nntplib_nntplib.NNTP.group" }, { "name": "nntplib.NNTP.head", "domId": "nntplib_nntplib.NNTP.head" }, { "name": "nntplib.NNTP.help", "domId": "nntplib_nntplib.NNTP.help" }, { "name": "nntplib.NNTP.ihave", "domId": "nntplib_nntplib.NNTP.ihave" }, { "name": "nntplib.NNTP.last", "domId": "nntplib_nntplib.NNTP.last" }, { "name": "nntplib.NNTP.list", "domId": "nntplib_nntplib.NNTP.list" }, { "name": "nntplib.NNTP.newgroups", "domId": "nntplib_nntplib.NNTP.newgroups" }, { "name": "nntplib.NNTP.newnews", "domId": "nntplib_nntplib.NNTP.newnews" }, { "name": "nntplib.NNTP.next", "domId": "nntplib_nntplib.NNTP.next" }, { "name": "nntplib.NNTP.post", "domId": "nntplib_nntplib.NNTP.post" }, { "name": "nntplib.NNTP.quit", "domId": "nntplib_nntplib.NNTP.quit" }, { "name": "nntplib.NNTP.set_debuglevel", "domId": "nntplib_nntplib.NNTP.set_debuglevel" }, { "name": "nntplib.NNTP.slave", "domId": "nntplib_nntplib.NNTP.slave" }, { "name": "nntplib.NNTP.stat", "domId": "nntplib_nntplib.NNTP.stat" }, { "name": "nntplib.NNTP.xgtitle", "domId": "nntplib_nntplib.NNTP.xgtitle" }, { "name": "nntplib.NNTP.xhdr", "domId": "nntplib_nntplib.NNTP.xhdr" }, { "name": "nntplib.NNTP.xover", "domId": "nntplib_nntplib.NNTP.xover" }, { "name": "nntplib.NNTP.xpath", "domId": "nntplib_nntplib.NNTP.xpath" } ] }, { "url": "http://docs.python.org/library/uuid.html", "title": "uuid", "html": "
\n

20.15. uuid — UUID objects according to RFC 4122

\n

\nNew in version 2.5.

\n

This module provides immutable UUID objects (the UUID class)\nand the functions uuid1(), uuid3(), uuid4(), uuid5() for\ngenerating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122.

\n

If all you want is a unique ID, you should probably call uuid1() or\nuuid4(). Note that uuid1() may compromise privacy since it creates\na UUID containing the computer’s network address. uuid4() creates a\nrandom UUID.

\n
\n
\nclass uuid.UUID([hex[, bytes[, bytes_le[, fields[, int[, version]]]]]])
\n

Create a UUID from either a string of 32 hexadecimal digits, a string of 16\nbytes as the bytes argument, a string of 16 bytes in little-endian order as\nthe bytes_le argument, a tuple of six integers (32-bit time_low, 16-bit\ntime_mid, 16-bit time_hi_version, 8-bit clock_seq_hi_variant, 8-bit\nclock_seq_low, 48-bit node) as the fields argument, or a single 128-bit\ninteger as the int argument. When a string of hex digits is given, curly\nbraces, hyphens, and a URN prefix are all optional. For example, these\nexpressions all yield the same UUID:

\n
UUID('{12345678-1234-5678-1234-567812345678}')\nUUID('12345678123456781234567812345678')\nUUID('urn:uuid:12345678-1234-5678-1234-567812345678')\nUUID(bytes='\\x12\\x34\\x56\\x78'*4)\nUUID(bytes_le='\\x78\\x56\\x34\\x12\\x34\\x12\\x78\\x56' +\n              '\\x12\\x34\\x56\\x78\\x12\\x34\\x56\\x78')\nUUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))\nUUID(int=0x12345678123456781234567812345678)\n
\n
\n

Exactly one of hex, bytes, bytes_le, fields, or int must be given.\nThe version argument is optional; if given, the resulting UUID will have its\nvariant and version number set according to RFC 4122, overriding bits in the\ngiven hex, bytes, bytes_le, fields, or int.

\n
\n\n

UUID instances have these read-only attributes:

\n
\n
\nUUID.bytes
\n
The UUID as a 16-byte string (containing the six integer fields in big-endian\nbyte order).
\n\n
\n
\nUUID.bytes_le
\n
The UUID as a 16-byte string (with time_low, time_mid, and time_hi_version\nin little-endian byte order).
\n\n
\n
\nUUID.fields
\n

A tuple of the six integer fields of the UUID, which are also available as six\nindividual attributes and two derived attributes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FieldMeaning
time_lowthe first 32 bits of the UUID
time_midthe next 16 bits of the UUID
time_hi_versionthe next 16 bits of the UUID
clock_seq_hi_variantthe next 8 bits of the UUID
clock_seq_lowthe next 8 bits of the UUID
nodethe last 48 bits of the UUID
timethe 60-bit timestamp
clock_seqthe 14-bit sequence number
\n
\n\n
\n
\nUUID.hex
\n
The UUID as a 32-character hexadecimal string.
\n\n
\n
\nUUID.int
\n
The UUID as a 128-bit integer.
\n\n
\n
\nUUID.urn
\n
The UUID as a URN as specified in RFC 4122.
\n\n
\n
\nUUID.variant
\n
The UUID variant, which determines the internal layout of the UUID. This will be\none of the integer constants RESERVED_NCS, RFC_4122,\nRESERVED_MICROSOFT, or RESERVED_FUTURE.
\n\n
\n
\nUUID.version
\n
The UUID version number (1 through 5, meaningful only when the variant is\nRFC_4122).
\n\n

The uuid module defines the following functions:

\n
\n
\nuuid.getnode()
\n
Get the hardware address as a 48-bit positive integer. The first time this\nruns, it may launch a separate program, which could be quite slow. If all\nattempts to obtain the hardware address fail, we choose a random 48-bit number\nwith its eighth bit set to 1 as recommended in RFC 4122. “Hardware address”\nmeans the MAC address of a network interface, and on a machine with multiple\nnetwork interfaces the MAC address of any one of them may be returned.
\n\n
\n
\nuuid.uuid1([node[, clock_seq]])
\n
Generate a UUID from a host ID, sequence number, and the current time. If node\nis not given, getnode() is used to obtain the hardware address. If\nclock_seq is given, it is used as the sequence number; otherwise a random\n14-bit sequence number is chosen.
\n\n
\n
\nuuid.uuid3(namespace, name)
\n
Generate a UUID based on the MD5 hash of a namespace identifier (which is a\nUUID) and a name (which is a string).
\n\n
\n
\nuuid.uuid4()
\n
Generate a random UUID.
\n\n
\n
\nuuid.uuid5(namespace, name)
\n
Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a\nUUID) and a name (which is a string).
\n\n

The uuid module defines the following namespace identifiers for use with\nuuid3() or uuid5().

\n
\n
\nuuid.NAMESPACE_DNS
\n
When this namespace is specified, the name string is a fully-qualified domain\nname.
\n\n
\n
\nuuid.NAMESPACE_URL
\n
When this namespace is specified, the name string is a URL.
\n\n
\n
\nuuid.NAMESPACE_OID
\n
When this namespace is specified, the name string is an ISO OID.
\n\n
\n
\nuuid.NAMESPACE_X500
\n
When this namespace is specified, the name string is an X.500 DN in DER or a\ntext output format.
\n\n

The uuid module defines the following constants for the possible values\nof the variant attribute:

\n
\n
\nuuid.RESERVED_NCS
\n
Reserved for NCS compatibility.
\n\n
\n
\nuuid.RFC_4122
\n
Specifies the UUID layout given in RFC 4122.
\n\n
\n
\nuuid.RESERVED_MICROSOFT
\n
Reserved for Microsoft compatibility.
\n\n
\n
\nuuid.RESERVED_FUTURE
\n
Reserved for future definition.
\n\n
\n

See also

\n
\n
RFC 4122 - A Universally Unique IDentifier (UUID) URN Namespace
\n
This specification defines a Uniform Resource Name namespace for UUIDs, the\ninternal format of UUIDs, and methods of generating UUIDs.
\n
\n
\n
\n

20.15.1. Example

\n

Here are some examples of typical usage of the uuid module:

\n
>>> import uuid\n\n>>> # make a UUID based on the host ID and current time\n>>> uuid.uuid1()\nUUID('a8098c1a-f86e-11da-bd1a-00112444be1e')\n\n>>> # make a UUID using an MD5 hash of a namespace UUID and a name\n>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')\nUUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')\n\n>>> # make a random UUID\n>>> uuid.uuid4()\nUUID('16fd2706-8baf-433b-82eb-8c7fada847da')\n\n>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name\n>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')\nUUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')\n\n>>> # make a UUID from a string of hex digits (braces and hyphens ignored)\n>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')\n\n>>> # convert a UUID to a string of hex digits in standard form\n>>> str(x)\n'00010203-0405-0607-0809-0a0b0c0d0e0f'\n\n>>> # get the raw 16 bytes of the UUID\n>>> x.bytes\n'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f'\n\n>>> # make a UUID from a 16-byte string\n>>> uuid.UUID(bytes=x.bytes)\nUUID('00010203-0405-0607-0809-0a0b0c0d0e0f')\n
\n
\n
\n
", "searchableItems": [ { "name": "uuid.getnode", "domId": "uuid_uuid.getnode" }, { "name": "uuid.UUID", "domId": "uuid_uuid.UUID" }, { "name": "uuid.uuid1", "domId": "uuid_uuid.uuid1" }, { "name": "uuid.uuid3", "domId": "uuid_uuid.uuid3" }, { "name": "uuid.uuid4", "domId": "uuid_uuid.uuid4" }, { "name": "uuid.uuid5", "domId": "uuid_uuid.uuid5" } ] }, { "url": "http://docs.python.org/library/urlparse.html", "title": "urlparse", "html": "
\n

20.16. urlparse — Parse URLs into components

\n
\n

Note

\n

The urlparse module is renamed to urllib.parse in Python 3.0.\nThe 2to3 tool will automatically adapt imports when converting\nyour sources to 3.0.

\n
\n

Source code: Lib/urlparse.py

\n
\n

This module defines a standard interface to break Uniform Resource Locator (URL)\nstrings up in components (addressing scheme, network location, path etc.), to\ncombine the components back into a URL string, and to convert a “relative URL”\nto an absolute URL given a “base URL.”

\n

The module has been designed to match the Internet RFC on Relative Uniform\nResource Locators (and discovered a bug in an earlier draft!). It supports the\nfollowing URL schemes: file, ftp, gopher, hdl, http,\nhttps, imap, mailto, mms, news, nntp, prospero,\nrsync, rtsp, rtspu, sftp, shttp, sip, sips,\nsnews, svn, svn+ssh, telnet, wais.

\n

\nNew in version 2.5: Support for the sftp and sips schemes.

\n

The urlparse module defines the following functions:

\n
\n
\nurlparse.urlparse(urlstring[, scheme[, allow_fragments]])
\n

Parse a URL into six components, returning a 6-tuple. This corresponds to the\ngeneral structure of a URL: scheme://netloc/path;parameters?query#fragment.\nEach tuple item is a string, possibly empty. The components are not broken up in\nsmaller parts (for example, the network location is a single string), and %\nescapes are not expanded. The delimiters as shown above are not part of the\nresult, except for a leading slash in the path component, which is retained if\npresent. For example:

\n
>>> from urlparse import urlparse\n>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')\n>>> o   # doctest: +NORMALIZE_WHITESPACE\nParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',\n            params='', query='', fragment='')\n>>> o.scheme\n'http'\n>>> o.port\n80\n>>> o.geturl()\n'http://www.cwi.nl:80/%7Eguido/Python.html'\n
\n
\n

Following the syntax specifications in RFC 1808, urlparse recognizes\na netloc only if it is properly introduced by ‘//’. Otherwise the\ninput is presumed to be a relative URL and thus to start with\na path component.

\n
>>> from urlparse import urlparse\n>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')\nParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',\n           params='', query='', fragment='')\n>>> urlparse('www.cwi.nl:80/%7Eguido/Python.html')\nParseResult(scheme='', netloc='', path='www.cwi.nl:80/%7Eguido/Python.html',\n           params='', query='', fragment='')\n>>> urlparse('help/Python.html')\nParseResult(scheme='', netloc='', path='help/Python.html', params='',\n           query='', fragment='')\n
\n
\n

If the scheme argument is specified, it gives the default addressing\nscheme, to be used only if the URL does not specify one. The default value for\nthis argument is the empty string.

\n

If the allow_fragments argument is false, fragment identifiers are not\nallowed, even if the URL’s addressing scheme normally does support them. The\ndefault value for this argument is True.

\n

The return value is actually an instance of a subclass of tuple. This\nclass has the following additional read-only convenience attributes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeIndexValueValue if not present
scheme0URL scheme specifierempty string
netloc1Network location partempty string
path2Hierarchical pathempty string
params3Parameters for last path\nelementempty string
query4Query componentempty string
fragment5Fragment identifierempty string
username User nameNone
password PasswordNone
hostname Host name (lower case)None
port Port number as integer,\nif presentNone
\n

See section Results of urlparse() and urlsplit() for more information on the result\nobject.

\n

\nChanged in version 2.5: Added attributes to return value.

\n

\nChanged in version 2.7: Added IPv6 URL parsing capabilities.

\n
\n\n
\n
\nurlparse.parse_qs(qs[, keep_blank_values[, strict_parsing]])
\n

Parse a query string given as a string argument (data of type\napplication/x-www-form-urlencoded). Data are returned as a\ndictionary. The dictionary keys are the unique query variable names and the\nvalues are lists of values for each name.

\n

The optional argument keep_blank_values is a flag indicating whether blank\nvalues in percent-encoded queries should be treated as blank strings. A true value\nindicates that blanks should be retained as blank strings. The default false\nvalue indicates that blank values are to be ignored and treated as if they were\nnot included.

\n

The optional argument strict_parsing is a flag indicating what to do with\nparsing errors. If false (the default), errors are silently ignored. If true,\nerrors raise a ValueError exception.

\n

Use the urllib.urlencode() function to convert such dictionaries into\nquery strings.

\n

\nNew in version 2.6: Copied from the cgi module.

\n
\n\n
\n
\nurlparse.parse_qsl(qs[, keep_blank_values[, strict_parsing]])
\n

Parse a query string given as a string argument (data of type\napplication/x-www-form-urlencoded). Data are returned as a list of\nname, value pairs.

\n

The optional argument keep_blank_values is a flag indicating whether blank\nvalues in percent-encoded queries should be treated as blank strings. A true value\nindicates that blanks should be retained as blank strings. The default false\nvalue indicates that blank values are to be ignored and treated as if they were\nnot included.

\n

The optional argument strict_parsing is a flag indicating what to do with\nparsing errors. If false (the default), errors are silently ignored. If true,\nerrors raise a ValueError exception.

\n

Use the urllib.urlencode() function to convert such lists of pairs into\nquery strings.

\n

\nNew in version 2.6: Copied from the cgi module.

\n
\n\n
\n
\nurlparse.urlunparse(parts)
\n
Construct a URL from a tuple as returned by urlparse(). The parts argument\ncan be any six-item iterable. This may result in a slightly different, but\nequivalent URL, if the URL that was parsed originally had unnecessary delimiters\n(for example, a ? with an empty query; the RFC states that these are\nequivalent).
\n\n
\n
\nurlparse.urlsplit(urlstring[, scheme[, allow_fragments]])
\n

This is similar to urlparse(), but does not split the params from the URL.\nThis should generally be used instead of urlparse() if the more recent URL\nsyntax allowing parameters to be applied to each segment of the path portion\nof the URL (see RFC 2396) is wanted. A separate function is needed to\nseparate the path segments and parameters. This function returns a 5-tuple:\n(addressing scheme, network location, path, query, fragment identifier).

\n

The return value is actually an instance of a subclass of tuple. This\nclass has the following additional read-only convenience attributes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeIndexValueValue if not present
scheme0URL scheme specifierempty string
netloc1Network location partempty string
path2Hierarchical pathempty string
query3Query componentempty string
fragment4Fragment identifierempty string
username User nameNone
password PasswordNone
hostname Host name (lower case)None
port Port number as integer,\nif presentNone
\n

See section Results of urlparse() and urlsplit() for more information on the result\nobject.

\n

\nNew in version 2.2.

\n

\nChanged in version 2.5: Added attributes to return value.

\n
\n\n
\n
\nurlparse.urlunsplit(parts)
\n

Combine the elements of a tuple as returned by urlsplit() into a complete\nURL as a string. The parts argument can be any five-item iterable. This may\nresult in a slightly different, but equivalent URL, if the URL that was parsed\noriginally had unnecessary delimiters (for example, a ? with an empty query; the\nRFC states that these are equivalent).

\n

\nNew in version 2.2.

\n
\n\n
\n
\nurlparse.urljoin(base, url[, allow_fragments])
\n

Construct a full (“absolute”) URL by combining a “base URL” (base) with\nanother URL (url). Informally, this uses components of the base URL, in\nparticular the addressing scheme, the network location and (part of) the path,\nto provide missing components in the relative URL. For example:

\n
>>> from urlparse import urljoin\n>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')\n'http://www.cwi.nl/%7Eguido/FAQ.html'\n
\n
\n

The allow_fragments argument has the same meaning and default as for\nurlparse().

\n
\n

Note

\n

If url is an absolute URL (that is, starting with // or scheme://),\nthe url‘s host name and/or scheme will be present in the result. For example:

\n
\n
>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',\n...         '//www.python.org/%7Eguido')\n'http://www.python.org/%7Eguido'\n
\n
\n

If you do not want that behavior, preprocess the url with urlsplit() and\nurlunsplit(), removing possible scheme and netloc parts.

\n
\n\n
\n
\nurlparse.urldefrag(url)
\n
If url contains a fragment identifier, returns a modified version of url\nwith no fragment identifier, and the fragment identifier as a separate string.\nIf there is no fragment identifier in url, returns url unmodified and an\nempty string.
\n\n
\n

See also

\n
\n
RFC 3986 - Uniform Resource Identifiers
\n
This is the current standard (STD66). Any changes to urlparse module\nshould conform to this. Certain deviations could be observed, which are\nmostly due backward compatiblity purposes and for certain de-facto\nparsing requirements as commonly observed in major browsers.
\n
RFC 2732 - Format for Literal IPv6 Addresses in URL’s.
\n
This specifies the parsing requirements of IPv6 URLs.
\n
RFC 2396 - Uniform Resource Identifiers (URI): Generic Syntax
\n
Document describing the generic syntactic requirements for both Uniform Resource\nNames (URNs) and Uniform Resource Locators (URLs).
\n
RFC 2368 - The mailto URL scheme.
\n
Parsing requirements for mailto url schemes.
\n
RFC 1808 - Relative Uniform Resource Locators
\n
This Request For Comments includes the rules for joining an absolute and a\nrelative URL, including a fair number of “Abnormal Examples” which govern the\ntreatment of border cases.
\n
RFC 1738 - Uniform Resource Locators (URL)
\n
This specifies the formal syntax and semantics of absolute URLs.
\n
\n
\n
\n

20.16.1. Results of urlparse() and urlsplit()

\n

The result objects from the urlparse() and urlsplit() functions are\nsubclasses of the tuple type. These subclasses add the attributes\ndescribed in those functions, as well as provide an additional method:

\n
\n
\nParseResult.geturl()
\n

Return the re-combined version of the original URL as a string. This may differ\nfrom the original URL in that the scheme will always be normalized to lower case\nand empty components may be dropped. Specifically, empty parameters, queries,\nand fragment identifiers will be removed.

\n

The result of this method is a fixpoint if passed back through the original\nparsing function:

\n
\n
>>> import urlparse\n>>> url = 'HTTP://www.Python.org/doc/#'\n
\n
\n
>>> r1 = urlparse.urlsplit(url)\n>>> r1.geturl()\n'http://www.Python.org/doc/'\n
\n
\n
>>> r2 = urlparse.urlsplit(r1.geturl())\n>>> r2.geturl()\n'http://www.Python.org/doc/'\n
\n
\n
\n

\nNew in version 2.5.

\n
\n\n

The following classes provide the implementations of the parse results:

\n
\n
\nclass urlparse.BaseResult
\n
Base class for the concrete result classes. This provides most of the attribute\ndefinitions. It does not provide a geturl() method. It is derived from\ntuple, but does not override the __init__() or __new__()\nmethods.
\n\n
\n
\nclass urlparse.ParseResult(scheme, netloc, path, params, query, fragment)
\n
Concrete class for urlparse() results. The __new__() method is\noverridden to support checking that the right number of arguments are passed.
\n\n
\n
\nclass urlparse.SplitResult(scheme, netloc, path, query, fragment)
\n
Concrete class for urlsplit() results. The __new__() method is\noverridden to support checking that the right number of arguments are passed.
\n\n
\n
", "searchableItems": [ { "name": "urlparse.BaseResult", "domId": "urlparse_urlparse.BaseResult" }, { "name": "urlparse.parse_qs", "domId": "urlparse_urlparse.parse_qs" }, { "name": "urlparse.parse_qsl", "domId": "urlparse_urlparse.parse_qsl" }, { "name": "urlparse.ParseResult", "domId": "urlparse_urlparse.ParseResult" }, { "name": "urlparse.ParseResult.geturl", "domId": "urlparse_urlparse.ParseResult.geturl" }, { "name": "urlparse.SplitResult", "domId": "urlparse_urlparse.SplitResult" }, { "name": "urlparse.urldefrag", "domId": "urlparse_urlparse.urldefrag" }, { "name": "urlparse.urljoin", "domId": "urlparse_urlparse.urljoin" }, { "name": "urlparse.urlparse", "domId": "urlparse_urlparse.urlparse" }, { "name": "urlparse.urlsplit", "domId": "urlparse_urlparse.urlsplit" }, { "name": "urlparse.urlunparse", "domId": "urlparse_urlparse.urlunparse" }, { "name": "urlparse.urlunsplit", "domId": "urlparse_urlparse.urlunsplit" } ] }, { "url": "http://docs.python.org/library/simplehttpserver.html", "title": "SimpleHTTPServer", "html": "
\n

20.19. SimpleHTTPServer — Simple HTTP request handler

\n
\n

Note

\n

The SimpleHTTPServer module has been merged into http.server in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

The SimpleHTTPServer module defines a single class,\nSimpleHTTPRequestHandler, which is interface-compatible with\nBaseHTTPServer.BaseHTTPRequestHandler.

\n

The SimpleHTTPServer module defines the following class:

\n
\n
\nclass SimpleHTTPServer.SimpleHTTPRequestHandler(request, client_address, server)
\n

This class serves files from the current directory and below, directly\nmapping the directory structure to HTTP requests.

\n

A lot of the work, such as parsing the request, is done by the base class\nBaseHTTPServer.BaseHTTPRequestHandler. This class implements the\ndo_GET() and do_HEAD() functions.

\n

The following are defined as class-level attributes of\nSimpleHTTPRequestHandler:

\n
\n
\nserver_version
\n
\n\n

This will be "SimpleHTTP/" + __version__, where __version__ is\ndefined at the module level.

\n
\n
\nextensions_map
\n
A dictionary mapping suffixes into MIME types. The default is\nsignified by an empty string, and is considered to be\napplication/octet-stream. The mapping is used case-insensitively,\nand so should contain only lower-cased keys.
\n\n

The SimpleHTTPRequestHandler class defines the following methods:

\n
\n
\ndo_HEAD()
\n
This method serves the 'HEAD' request type: it sends the headers it\nwould send for the equivalent GET request. See the do_GET()\nmethod for a more complete explanation of the possible headers.
\n\n
\n
\ndo_GET()
\n

The request is mapped to a local file by interpreting the request as a\npath relative to the current working directory.

\n

If the request was mapped to a directory, the directory is checked for a\nfile named index.html or index.htm (in that order). If found, the\nfile’s contents are returned; otherwise a directory listing is generated\nby calling the list_directory() method. This method uses\nos.listdir() to scan the directory, and returns a 404 error\nresponse if the listdir() fails.

\n

If the request was mapped to a file, it is opened and the contents are\nreturned. Any IOError exception in opening the requested file is\nmapped to a 404, 'File not found' error. Otherwise, the content\ntype is guessed by calling the guess_type() method, which in turn\nuses the extensions_map variable.

\n

A 'Content-type:' header with the guessed content type is output,\nfollowed by a 'Content-Length:' header with the file’s size and a\n'Last-Modified:' header with the file’s modification time.

\n

Then follows a blank line signifying the end of the headers, and then the\ncontents of the file are output. If the file’s MIME type starts with\ntext/ the file is opened in text mode; otherwise binary mode is used.

\n

The test() function in the SimpleHTTPServer module is an\nexample which creates a server using the SimpleHTTPRequestHandler\nas the Handler.

\n

\nNew in version 2.5: The 'Last-Modified' header.

\n
\n\n
\n\n

The SimpleHTTPServer module can be used in the following manner in order\nto set up a very basic web server serving files relative to the current\ndirectory.

\n
import SimpleHTTPServer\nimport SocketServer\n\nPORT = 8000\n\nHandler = SimpleHTTPServer.SimpleHTTPRequestHandler\n\nhttpd = SocketServer.TCPServer(("", PORT), Handler)\n\nprint "serving at port", PORT\nhttpd.serve_forever()\n
\n
\n

The SimpleHTTPServer module can also be invoked directly using the\n-m switch of the interpreter with a port number argument.\nSimilar to the previous example, this serves the files relative to the\ncurrent directory.

\n
python -m SimpleHTTPServer 8000
\n
\n
\n

See also

\n
\n
Module BaseHTTPServer
\n
Base class implementation for Web server and request handler.
\n
\n
\n
", "searchableItems": [ { "name": "SimpleHTTPServer.SimpleHTTPRequestHandler", "domId": "SimpleHTTPServer_SimpleHTTPServer.SimpleHTTPRequestHandler" }, { "name": "SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET", "domId": "SimpleHTTPServer_SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" }, { "name": "SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD", "domId": "SimpleHTTPServer_SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD" } ] }, { "url": "http://docs.python.org/library/smtplib.html", "title": "smtplib", "html": "
\n

20.12. smtplib — SMTP protocol client

\n

Source code: Lib/smtplib.py

\n
\n

The smtplib module defines an SMTP client session object that can be used\nto send mail to any Internet machine with an SMTP or ESMTP listener daemon. For\ndetails of SMTP and ESMTP operation, consult RFC 821 (Simple Mail Transfer\nProtocol) and RFC 1869 (SMTP Service Extensions).

\n
\n
\nclass smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])
\n

A SMTP instance encapsulates an SMTP connection. It has methods\nthat support a full repertoire of SMTP and ESMTP operations. If the optional\nhost and port parameters are given, the SMTP connect() method is called\nwith those parameters during initialization. An SMTPConnectError is\nraised if the specified host doesn’t respond correctly. The optional\ntimeout parameter specifies a timeout in seconds for blocking operations\nlike the connection attempt (if not specified, the global default timeout\nsetting will be used).

\n

For normal use, you should only require the initialization/connect,\nsendmail(), and quit() methods. An example is included below.

\n

\nChanged in version 2.6: timeout was added.

\n
\n\n
\n
\nclass smtplib.SMTP_SSL([host[, port[, local_hostname[, keyfile[, certfile[, timeout]]]]]])
\n

A SMTP_SSL instance behaves exactly the same as instances of\nSMTP. SMTP_SSL should be used for situations where SSL is\nrequired from the beginning of the connection and using starttls() is\nnot appropriate. If host is not specified, the local host is used. If\nport is omitted, the standard SMTP-over-SSL port (465) is used. keyfile\nand certfile are also optional, and can contain a PEM formatted private key\nand certificate chain file for the SSL connection. The optional timeout\nparameter specifies a timeout in seconds for blocking operations like the\nconnection attempt (if not specified, the global default timeout setting\nwill be used).

\n

\nNew in version 2.6.

\n
\n\n
\n
\nclass smtplib.LMTP([host[, port[, local_hostname]]])
\n

The LMTP protocol, which is very similar to ESMTP, is heavily based on the\nstandard SMTP client. It’s common to use Unix sockets for LMTP, so our connect()\nmethod must support that as well as a regular host:port server. To specify a\nUnix socket, you must use an absolute path for host, starting with a ‘/’.

\n

Authentication is supported, using the regular SMTP mechanism. When using a Unix\nsocket, LMTP generally don’t support or require any authentication, but your\nmileage might vary.

\n

\nNew in version 2.6.

\n
\n\n

A nice selection of exceptions is defined as well:

\n
\n
\nexception smtplib.SMTPException
\n
Base exception class for all exceptions raised by this module.
\n\n
\n
\nexception smtplib.SMTPServerDisconnected
\n
This exception is raised when the server unexpectedly disconnects, or when an\nattempt is made to use the SMTP instance before connecting it to a\nserver.
\n\n
\n
\nexception smtplib.SMTPResponseException
\n
Base class for all exceptions that include an SMTP error code. These exceptions\nare generated in some instances when the SMTP server returns an error code. The\nerror code is stored in the smtp_code attribute of the error, and the\nsmtp_error attribute is set to the error message.
\n\n
\n
\nexception smtplib.SMTPSenderRefused
\n
Sender address refused. In addition to the attributes set by on all\nSMTPResponseException exceptions, this sets ‘sender’ to the string that\nthe SMTP server refused.
\n\n
\n
\nexception smtplib.SMTPRecipientsRefused
\n
All recipient addresses refused. The errors for each recipient are accessible\nthrough the attribute recipients, which is a dictionary of exactly the\nsame sort as SMTP.sendmail() returns.
\n\n
\n
\nexception smtplib.SMTPDataError
\n
The SMTP server refused to accept the message data.
\n\n
\n
\nexception smtplib.SMTPConnectError
\n
Error occurred during establishment of a connection with the server.
\n\n
\n
\nexception smtplib.SMTPHeloError
\n
The server refused our HELO message.
\n\n
\n
\nexception smtplib.SMTPAuthenticationError
\n
SMTP authentication went wrong. Most probably the server didn’t accept the\nusername/password combination provided.
\n\n
\n

See also

\n
\n
RFC 821 - Simple Mail Transfer Protocol
\n
Protocol definition for SMTP. This document covers the model, operating\nprocedure, and protocol details for SMTP.
\n
RFC 1869 - SMTP Service Extensions
\n
Definition of the ESMTP extensions for SMTP. This describes a framework for\nextending SMTP with new commands, supporting dynamic discovery of the commands\nprovided by the server, and defines a few additional commands.
\n
\n
\n
\n

20.12.1. SMTP Objects

\n

An SMTP instance has the following methods:

\n
\n
\nSMTP.set_debuglevel(level)
\n
Set the debug output level. A true value for level results in debug messages\nfor connection and for all messages sent to and received from the server.
\n\n
\n
\nSMTP.connect([host[, port]])
\n
Connect to a host on a given port. The defaults are to connect to the local\nhost at the standard SMTP port (25). If the hostname ends with a colon (':')\nfollowed by a number, that suffix will be stripped off and the number\ninterpreted as the port number to use. This method is automatically invoked by\nthe constructor if a host is specified during instantiation.
\n\n
\n
\nSMTP.docmd(cmd[, argstring])
\n

Send a command cmd to the server. The optional argument argstring is simply\nconcatenated to the command, separated by a space.

\n

This returns a 2-tuple composed of a numeric response code and the actual\nresponse line (multiline responses are joined into one long line.)

\n

In normal operation it should not be necessary to call this method explicitly.\nIt is used to implement other methods and may be useful for testing private\nextensions.

\n

If the connection to the server is lost while waiting for the reply,\nSMTPServerDisconnected will be raised.

\n
\n\n
\n
\nSMTP.helo([hostname])
\n

Identify yourself to the SMTP server using HELO. The hostname argument\ndefaults to the fully qualified domain name of the local host.\nThe message returned by the server is stored as the helo_resp attribute\nof the object.

\n

In normal operation it should not be necessary to call this method explicitly.\nIt will be implicitly called by the sendmail() when necessary.

\n
\n\n
\n
\nSMTP.ehlo([hostname])
\n

Identify yourself to an ESMTP server using EHLO. The hostname argument\ndefaults to the fully qualified domain name of the local host. Examine the\nresponse for ESMTP option and store them for use by has_extn().\nAlso sets several informational attributes: the message returned by\nthe server is stored as the ehlo_resp attribute, does_esmtp\nis set to true or false depending on whether the server supports ESMTP, and\nesmtp_features will be a dictionary containing the names of the\nSMTP service extensions this server supports, and their\nparameters (if any).

\n

Unless you wish to use has_extn() before sending mail, it should not be\nnecessary to call this method explicitly. It will be implicitly called by\nsendmail() when necessary.

\n
\n\n
\n
\nSMTP.ehlo_or_helo_if_needed()
\n

This method call ehlo() and or helo() if there has been no\nprevious EHLO or HELO command this session. It tries ESMTP EHLO\nfirst.

\n
\n
SMTPHeloError
\n
The server didn’t reply properly to the HELO greeting.
\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nSMTP.has_extn(name)
\n
Return True if name is in the set of SMTP service extensions returned\nby the server, False otherwise. Case is ignored.
\n\n
\n
\nSMTP.verify(address)
\n

Check the validity of an address on this server using SMTP VRFY. Returns a\ntuple consisting of code 250 and a full RFC 822 address (including human\nname) if the user address is valid. Otherwise returns an SMTP error code of 400\nor greater and an error string.

\n
\n

Note

\n

Many sites disable SMTP VRFY in order to foil spammers.

\n
\n
\n\n
\n
\nSMTP.login(user, password)
\n

Log in on an SMTP server that requires authentication. The arguments are the\nusername and the password to authenticate with. If there has been no previous\nEHLO or HELO command this session, this method tries ESMTP EHLO\nfirst. This method will return normally if the authentication was successful, or\nmay raise the following exceptions:

\n
\n
SMTPHeloError
\n
The server didn’t reply properly to the HELO greeting.
\n
SMTPAuthenticationError
\n
The server didn’t accept the username/password combination.
\n
SMTPException
\n
No suitable authentication method was found.
\n
\n
\n\n
\n
\nSMTP.starttls([keyfile[, certfile]])
\n

Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP\ncommands that follow will be encrypted. You should then call ehlo()\nagain.

\n

If keyfile and certfile are provided, these are passed to the socket\nmodule’s ssl() function.

\n

If there has been no previous EHLO or HELO command this session,\nthis method tries ESMTP EHLO first.

\n

\nChanged in version 2.6.

\n
\n
SMTPHeloError
\n
The server didn’t reply properly to the HELO greeting.
\n
SMTPException
\n
The server does not support the STARTTLS extension.
\n
\n

\nChanged in version 2.6.

\n
\n
RuntimeError
\n
SSL/TLS support is not available to your Python interpreter.
\n
\n
\n\n
\n
\nSMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
\n

Send mail. The required arguments are an RFC 822 from-address string, a list\nof RFC 822 to-address strings (a bare string will be treated as a list with 1\naddress), and a message string. The caller may pass a list of ESMTP options\n(such as 8bitmime) to be used in MAIL FROM commands as mail_options.\nESMTP options (such as DSN commands) that should be used with all RCPT\ncommands can be passed as rcpt_options. (If you need to use different ESMTP\noptions to different recipients you have to use the low-level methods such as\nmail(), rcpt() and data() to send the message.)

\n
\n

Note

\n

The from_addr and to_addrs parameters are used to construct the message\nenvelope used by the transport agents. The SMTP does not modify the\nmessage headers in any way.

\n
\n

If there has been no previous EHLO or HELO command this session, this\nmethod tries ESMTP EHLO first. If the server does ESMTP, message size and\neach of the specified options will be passed to it (if the option is in the\nfeature set the server advertises). If EHLO fails, HELO will be tried\nand ESMTP options suppressed.

\n

This method will return normally if the mail is accepted for at least one\nrecipient. Otherwise it will raise an exception. That is, if this method does\nnot raise an exception, then someone should get your mail. If this method does\nnot raise an exception, it returns a dictionary, with one entry for each\nrecipient that was refused. Each entry contains a tuple of the SMTP error code\nand the accompanying error message sent by the server.

\n

This method may raise the following exceptions:

\n
\n
SMTPRecipientsRefused
\n
All recipients were refused. Nobody got the mail. The recipients\nattribute of the exception object is a dictionary with information about the\nrefused recipients (like the one returned when at least one recipient was\naccepted).
\n
SMTPHeloError
\n
The server didn’t reply properly to the HELO greeting.
\n
SMTPSenderRefused
\n
The server didn’t accept the from_addr.
\n
SMTPDataError
\n
The server replied with an unexpected error code (other than a refusal of a\nrecipient).
\n
\n

Unless otherwise noted, the connection will be open even after an exception is\nraised.

\n
\n\n
\n
\nSMTP.quit()
\n

Terminate the SMTP session and close the connection. Return the result of\nthe SMTP QUIT command.

\n

\nChanged in version 2.6: Return a value.

\n
\n\n

Low-level methods corresponding to the standard SMTP/ESMTP commands HELP,\nRSET, NOOP, MAIL, RCPT, and DATA are also supported.\nNormally these do not need to be called directly, so they are not documented\nhere. For details, consult the module code.

\n
\n
\n

20.12.2. SMTP Example

\n

This example prompts the user for addresses needed in the message envelope (‘To’\nand ‘From’ addresses), and the message to be delivered. Note that the headers\nto be included with the message must be included in the message as entered; this\nexample doesn’t do any processing of the RFC 822 headers. In particular, the\n‘To’ and ‘From’ addresses must be included in the message headers explicitly.

\n
import smtplib\n\ndef prompt(prompt):\n    return raw_input(prompt).strip()\n\nfromaddr = prompt("From: ")\ntoaddrs  = prompt("To: ").split()\nprint "Enter message, end with ^D (Unix) or ^Z (Windows):"\n\n# Add the From: and To: headers at the start!\nmsg = ("From: %s\\r\\nTo: %s\\r\\n\\r\\n"\n        (fromaddr, ", ".join(toaddrs)))\nwhile 1:\n    try:\n        line = raw_input()\n    except EOFError:\n        break\n    if not line:\n        break\n    msg = msg + line\n\nprint "Message length is " + repr(len(msg))\n\nserver = smtplib.SMTP('localhost')\nserver.set_debuglevel(1)\nserver.sendmail(fromaddr, toaddrs, msg)\nserver.quit()\n
\n
\n
\n

Note

\n

In general, you will want to use the email package’s features to\nconstruct an email message, which you can then convert to a string and send\nvia sendmail(); see email: Examples.

\n
\n
\n
", "searchableItems": [ { "name": "smtplib.LMTP", "domId": "smtplib_smtplib.LMTP" }, { "name": "smtplib.SMTP", "domId": "smtplib_smtplib.SMTP" }, { "name": "smtplib.SMTP.connect", "domId": "smtplib_smtplib.SMTP.connect" }, { "name": "smtplib.SMTP.docmd", "domId": "smtplib_smtplib.SMTP.docmd" }, { "name": "smtplib.SMTP.ehlo", "domId": "smtplib_smtplib.SMTP.ehlo" }, { "name": "smtplib.SMTP.ehlo_or_helo_if_needed", "domId": "smtplib_smtplib.SMTP.ehlo_or_helo_if_needed" }, { "name": "smtplib.SMTP.has_extn", "domId": "smtplib_smtplib.SMTP.has_extn" }, { "name": "smtplib.SMTP.helo", "domId": "smtplib_smtplib.SMTP.helo" }, { "name": "smtplib.SMTP.login", "domId": "smtplib_smtplib.SMTP.login" }, { "name": "smtplib.SMTP.quit", "domId": "smtplib_smtplib.SMTP.quit" }, { "name": "smtplib.SMTP.sendmail", "domId": "smtplib_smtplib.SMTP.sendmail" }, { "name": "smtplib.SMTP.set_debuglevel", "domId": "smtplib_smtplib.SMTP.set_debuglevel" }, { "name": "smtplib.SMTP.starttls", "domId": "smtplib_smtplib.SMTP.starttls" }, { "name": "smtplib.SMTP.verify", "domId": "smtplib_smtplib.SMTP.verify" }, { "name": "smtplib.SMTP_SSL", "domId": "smtplib_smtplib.SMTP_SSL" } ] }, { "url": "http://docs.python.org/library/basehttpserver.html", "title": "BaseHTTPServer", "html": "
\n

20.18. BaseHTTPServer — Basic HTTP server

\n
\n

Note

\n

The BaseHTTPServer module has been merged into http.server in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

Source code: Lib/BaseHTTPServer.py

\n
\n

This module defines two classes for implementing HTTP servers (Web servers).\nUsually, this module isn’t used directly, but is used as a basis for building\nfunctioning Web servers. See the SimpleHTTPServer and\nCGIHTTPServer modules.

\n

The first class, HTTPServer, is a SocketServer.TCPServer\nsubclass, and therefore implements the SocketServer.BaseServer\ninterface. It creates and listens at the HTTP socket, dispatching the requests\nto a handler. Code to create and run the server looks like this:

\n
def run(server_class=BaseHTTPServer.HTTPServer,\n        handler_class=BaseHTTPServer.BaseHTTPRequestHandler):\n    server_address = ('', 8000)\n    httpd = server_class(server_address, handler_class)\n    httpd.serve_forever()\n
\n
\n
\n
\nclass BaseHTTPServer.HTTPServer(server_address, RequestHandlerClass)
\n
This class builds on the TCPServer class by storing the server\naddress as instance variables named server_name and\nserver_port. The server is accessible by the handler, typically\nthrough the handler’s server instance variable.
\n\n
\n
\nclass BaseHTTPServer.BaseHTTPRequestHandler(request, client_address, server)
\n

This class is used to handle the HTTP requests that arrive at the server. By\nitself, it cannot respond to any actual HTTP requests; it must be subclassed\nto handle each request method (e.g. GET or\nPOST). BaseHTTPRequestHandler provides a number of class and\ninstance variables, and methods for use by subclasses.

\n

The handler will parse the request and the headers, then call a method\nspecific to the request type. The method name is constructed from the\nrequest. For example, for the request method SPAM, the do_SPAM()\nmethod will be called with no arguments. All of the relevant information is\nstored in instance variables of the handler. Subclasses should not need to\noverride or extend the __init__() method.

\n

BaseHTTPRequestHandler has the following instance variables:

\n
\n
\nclient_address
\n
Contains a tuple of the form (host, port) referring to the client’s\naddress.
\n\n
\n
\nserver
\n
Contains the server instance.
\n\n
\n
\ncommand
\n
Contains the command (request type). For example, 'GET'.
\n\n
\n
\npath
\n
Contains the request path.
\n\n
\n
\nrequest_version
\n
Contains the version string from the request. For example, 'HTTP/1.0'.
\n\n
\n
\nheaders
\n
Holds an instance of the class specified by the MessageClass class\nvariable. This instance parses and manages the headers in the HTTP\nrequest.
\n\n
\n
\nrfile
\n
Contains an input stream, positioned at the start of the optional input\ndata.
\n\n
\n
\nwfile
\n
Contains the output stream for writing a response back to the\nclient. Proper adherence to the HTTP protocol must be used when writing to\nthis stream.
\n\n

BaseHTTPRequestHandler has the following class variables:

\n
\n
\nserver_version
\n
Specifies the server software version. You may want to override this. The\nformat is multiple whitespace-separated strings, where each string is of\nthe form name[/version]. For example, 'BaseHTTP/0.2'.
\n\n
\n
\nsys_version
\n
Contains the Python system version, in a form usable by the\nversion_string method and the server_version class\nvariable. For example, 'Python/1.4'.
\n\n
\n
\nerror_message_format
\n
Specifies a format string for building an error response to the client. It\nuses parenthesized, keyed format specifiers, so the format operand must be\na dictionary. The code key should be an integer, specifying the numeric\nHTTP error code value. message should be a string containing a\n(detailed) error message of what occurred, and explain should be an\nexplanation of the error code number. Default message and explain\nvalues can found in the responses class variable.
\n\n
\n
\nerror_content_type
\n

Specifies the Content-Type HTTP header of error responses sent to the\nclient. The default value is 'text/html'.

\n

\nNew in version 2.6: Previously, the content type was always 'text/html'.

\n
\n\n
\n
\nprotocol_version
\n
This specifies the HTTP protocol version used in responses. If set to\n'HTTP/1.1', the server will permit HTTP persistent connections;\nhowever, your server must then include an accurate Content-Length\nheader (using send_header()) in all of its responses to clients.\nFor backwards compatibility, the setting defaults to 'HTTP/1.0'.
\n\n
\n
\nMessageClass
\n

Specifies a rfc822.Message-like class to parse HTTP headers.\nTypically, this is not overridden, and it defaults to\nmimetools.Message.

\n
\n\n
\n
\nresponses
\n
This variable contains a mapping of error code integers to two-element tuples\ncontaining a short and long message. For example, {code: (shortmessage,\nlongmessage)}. The shortmessage is usually used as the message key in an\nerror response, and longmessage as the explain key (see the\nerror_message_format class variable).
\n\n

A BaseHTTPRequestHandler instance has the following methods:

\n
\n
\nhandle()
\n
Calls handle_one_request() once (or, if persistent connections are\nenabled, multiple times) to handle incoming HTTP requests. You should\nnever need to override it; instead, implement appropriate do_*()\nmethods.
\n\n
\n
\nhandle_one_request()
\n
This method will parse and dispatch the request to the appropriate\ndo_*() method. You should never need to override it.
\n\n
\n
\nsend_error(code[, message])
\n
Sends and logs a complete error reply to the client. The numeric code\nspecifies the HTTP error code, with message as optional, more specific text. A\ncomplete set of headers is sent, followed by text composed using the\nerror_message_format class variable.
\n\n
\n
\nsend_response(code[, message])
\n
Sends a response header and logs the accepted request. The HTTP response\nline is sent, followed by Server and Date headers. The values for\nthese two headers are picked up from the version_string() and\ndate_time_string() methods, respectively.
\n\n
\n
\nsend_header(keyword, value)
\n
Writes a specific HTTP header to the output stream. keyword should\nspecify the header keyword, with value specifying its value.
\n\n
\n
\nend_headers()
\n
Sends a blank line, indicating the end of the HTTP headers in the\nresponse.
\n\n
\n
\nlog_request([code[, size]])
\n
Logs an accepted (successful) request. code should specify the numeric\nHTTP code associated with the response. If a size of the response is\navailable, then it should be passed as the size parameter.
\n\n
\n
\nlog_error(...)
\n
Logs an error when a request cannot be fulfilled. By default, it passes\nthe message to log_message(), so it takes the same arguments\n(format and additional values).
\n\n
\n
\nlog_message(format, ...)
\n
Logs an arbitrary message to sys.stderr. This is typically overridden\nto create custom error logging mechanisms. The format argument is a\nstandard printf-style format string, where the additional arguments to\nlog_message() are applied as inputs to the formatting. The client\naddress and current date and time are prefixed to every message logged.
\n\n
\n
\nversion_string()
\n
Returns the server software’s version string. This is a combination of the\nserver_version and sys_version class variables.
\n\n
\n
\ndate_time_string([timestamp])
\n

Returns the date and time given by timestamp (which must be in the\nformat returned by time.time()), formatted for a message header. If\ntimestamp is omitted, it uses the current date and time.

\n

The result looks like 'Sun, 06 Nov 1994 08:49:37 GMT'.

\n

\nNew in version 2.5: The timestamp parameter.

\n
\n\n
\n
\nlog_date_time_string()
\n
Returns the current date and time, formatted for logging.
\n\n
\n
\naddress_string()
\n
Returns the client address, formatted for logging. A name lookup is\nperformed on the client’s IP address.
\n\n
\n\n
\n

20.18.1. More examples

\n

To create a server that doesn’t run forever, but until some condition is\nfulfilled:

\n
def run_while_true(server_class=BaseHTTPServer.HTTPServer,\n                   handler_class=BaseHTTPServer.BaseHTTPRequestHandler):\n    """\n    This assumes that keep_running() is a function of no arguments which\n    is tested initially and after each request.  If its return value\n    is true, the server continues.\n    """\n    server_address = ('', 8000)\n    httpd = server_class(server_address, handler_class)\n    while keep_running():\n        httpd.handle_request()\n
\n
\n
\n

See also

\n
\n
Module CGIHTTPServer
\n
Extended request handler that supports CGI scripts.
\n
Module SimpleHTTPServer
\n
Basic request handler that limits response to files actually under the\ndocument root.
\n
\n
\n
\n
", "searchableItems": [ { "name": "BaseHTTPServer.BaseHTTPRequestHandler", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.address_string", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.address_string" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.date_time_string", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.date_time_string" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.end_headers", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.end_headers" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.handle", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.handle" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.log_date_time_string", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.log_date_time_string" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.log_error", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.log_error" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.log_message", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.log_message" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.log_request", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.log_request" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.send_error", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.send_error" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.send_header", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.send_header" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.send_response", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.send_response" }, { "name": "BaseHTTPServer.BaseHTTPRequestHandler.version_string", "domId": "BaseHTTPServer_BaseHTTPServer.BaseHTTPRequestHandler.version_string" }, { "name": "BaseHTTPServer.HTTPServer", "domId": "BaseHTTPServer_BaseHTTPServer.HTTPServer" } ] }, { "url": "http://docs.python.org/library/cgihttpserver.html", "title": "CGIHTTPServer", "html": "
\n

20.20. CGIHTTPServer — CGI-capable HTTP request handler

\n
\n

Note

\n

The CGIHTTPServer module has been merged into http.server in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

The CGIHTTPServer module defines a request-handler class, interface\ncompatible with BaseHTTPServer.BaseHTTPRequestHandler and inherits\nbehavior from SimpleHTTPServer.SimpleHTTPRequestHandler but can also\nrun CGI scripts.

\n
\n

Note

\n

This module can run CGI scripts on Unix and Windows systems.

\n
\n
\n

Note

\n

CGI scripts run by the CGIHTTPRequestHandler class cannot execute\nredirects (HTTP code 302), because code 200 (script output follows) is sent\nprior to execution of the CGI script. This pre-empts the status code.

\n
\n

The CGIHTTPServer module defines the following class:

\n
\n
\nclass CGIHTTPServer.CGIHTTPRequestHandler(request, client_address, server)
\n

This class is used to serve either files or output of CGI scripts from the\ncurrent directory and below. Note that mapping HTTP hierarchic structure to\nlocal directory structure is exactly as in\nSimpleHTTPServer.SimpleHTTPRequestHandler.

\n

The class will however, run the CGI script, instead of serving it as a file, if\nit guesses it to be a CGI script. Only directory-based CGI are used — the\nother common server configuration is to treat special extensions as denoting CGI\nscripts.

\n

The do_GET() and do_HEAD() functions are modified to run CGI scripts\nand serve the output, instead of serving files, if the request leads to\nsomewhere below the cgi_directories path.

\n

The CGIHTTPRequestHandler defines the following data member:

\n
\n
\ncgi_directories
\n
This defaults to ['/cgi-bin', '/htbin'] and describes directories to\ntreat as containing CGI scripts.
\n\n

The CGIHTTPRequestHandler defines the following methods:

\n
\n
\ndo_POST()
\n
This method serves the 'POST' request type, only allowed for CGI\nscripts. Error 501, “Can only POST to CGI scripts”, is output when trying\nto POST to a non-CGI url.
\n\n
\n\n

Note that CGI scripts will be run with UID of user nobody, for security reasons.\nProblems with the CGI script will be translated to error 403.

\n

For example usage, see the implementation of the test() function.

\n
\n

See also

\n
\n
Module BaseHTTPServer
\n
Base class implementation for Web server and request handler.
\n
\n
\n
", "searchableItems": [ { "name": "CGIHTTPServer.CGIHTTPRequestHandler", "domId": "CGIHTTPServer_CGIHTTPServer.CGIHTTPRequestHandler" }, { "name": "CGIHTTPServer.CGIHTTPRequestHandler.do_POST", "domId": "CGIHTTPServer_CGIHTTPServer.CGIHTTPRequestHandler.do_POST" } ] }, { "url": "http://docs.python.org/library/socketserver.html", "title": "SocketServer", "html": "
\n

20.17. SocketServer — A framework for network servers

\n
\n

Note

\n

The SocketServer module has been renamed to socketserver in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

Source code: Lib/SocketServer.py

\n
\n

The SocketServer module simplifies the task of writing network servers.

\n

There are four basic server classes: TCPServer uses the Internet TCP\nprotocol, which provides for continuous streams of data between the client and\nserver. UDPServer uses datagrams, which are discrete packets of\ninformation that may arrive out of order or be lost while in transit. The more\ninfrequently used UnixStreamServer and UnixDatagramServer\nclasses are similar, but use Unix domain sockets; they’re not available on\nnon-Unix platforms. For more details on network programming, consult a book\nsuch as\nW. Richard Steven’s UNIX Network Programming or Ralph Davis’s Win32 Network\nProgramming.

\n

These four classes process requests synchronously; each request must be\ncompleted before the next request can be started. This isn’t suitable if each\nrequest takes a long time to complete, because it requires a lot of computation,\nor because it returns a lot of data which the client is slow to process. The\nsolution is to create a separate process or thread to handle each request; the\nForkingMixIn and ThreadingMixIn mix-in classes can be used to\nsupport asynchronous behaviour.

\n

Creating a server requires several steps. First, you must create a request\nhandler class by subclassing the BaseRequestHandler class and\noverriding its handle() method; this method will process incoming\nrequests. Second, you must instantiate one of the server classes, passing it\nthe server’s address and the request handler class. Finally, call the\nhandle_request() or serve_forever() method of the server object to\nprocess one or many requests.

\n

When inheriting from ThreadingMixIn for threaded connection behavior,\nyou should explicitly declare how you want your threads to behave on an abrupt\nshutdown. The ThreadingMixIn class defines an attribute\ndaemon_threads, which indicates whether or not the server should wait for\nthread termination. You should set the flag explicitly if you would like threads\nto behave autonomously; the default is False, meaning that Python will\nnot exit until all threads created by ThreadingMixIn have exited.

\n

Server classes have the same external methods and attributes, no matter what\nnetwork protocol they use.

\n
\n

20.17.1. Server Creation Notes

\n

There are five classes in an inheritance diagram, four of which represent\nsynchronous servers of four types:

\n
+------------+\n| BaseServer |\n+------------+\n      |\n      v\n+-----------+        +------------------+\n| TCPServer |------->| UnixStreamServer |\n+-----------+        +------------------+\n      |\n      v\n+-----------+        +--------------------+\n| UDPServer |------->| UnixDatagramServer |\n+-----------+        +--------------------+
\n
\n

Note that UnixDatagramServer derives from UDPServer, not from\nUnixStreamServer — the only difference between an IP and a Unix\nstream server is the address family, which is simply repeated in both Unix\nserver classes.

\n

Forking and threading versions of each type of server can be created using the\nForkingMixIn and ThreadingMixIn mix-in classes. For instance,\na threading UDP server class is created as follows:

\n
class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass\n
\n
\n

The mix-in class must come first, since it overrides a method defined in\nUDPServer. Setting the various attributes also change the\nbehavior of the underlying server mechanism.

\n

To implement a service, you must derive a class from BaseRequestHandler\nand redefine its handle() method. You can then run various versions of\nthe service by combining one of the server classes with your request handler\nclass. The request handler class must be different for datagram or stream\nservices. This can be hidden by using the handler subclasses\nStreamRequestHandler or DatagramRequestHandler.

\n

Of course, you still have to use your head! For instance, it makes no sense to\nuse a forking server if the service contains state in memory that can be\nmodified by different requests, since the modifications in the child process\nwould never reach the initial state kept in the parent process and passed to\neach child. In this case, you can use a threading server, but you will probably\nhave to use locks to protect the integrity of the shared data.

\n

On the other hand, if you are building an HTTP server where all data is stored\nexternally (for instance, in the file system), a synchronous class will\nessentially render the service “deaf” while one request is being handled –\nwhich may be for a very long time if a client is slow to receive all the data it\nhas requested. Here a threading or forking server is appropriate.

\n

In some cases, it may be appropriate to process part of a request synchronously,\nbut to finish processing in a forked child depending on the request data. This\ncan be implemented by using a synchronous server and doing an explicit fork in\nthe request handler class handle() method.

\n

Another approach to handling multiple simultaneous requests in an environment\nthat supports neither threads nor fork() (or where these are too expensive\nor inappropriate for the service) is to maintain an explicit table of partially\nfinished requests and to use select() to decide which request to work on\nnext (or whether to handle a new incoming request). This is particularly\nimportant for stream services where each client can potentially be connected for\na long time (if threads or subprocesses cannot be used). See asyncore for\nanother way to manage this.

\n
\n
\n

20.17.2. Server Objects

\n
\n
\nclass SocketServer.BaseServer
\n
This is the superclass of all Server objects in the module. It defines the\ninterface, given below, but does not implement most of the methods, which is\ndone in subclasses.
\n\n
\n
\nBaseServer.fileno()
\n
Return an integer file descriptor for the socket on which the server is\nlistening. This function is most commonly passed to select.select(), to\nallow monitoring multiple servers in the same process.
\n\n
\n
\nBaseServer.handle_request()
\n
Process a single request. This function calls the following methods in\norder: get_request(), verify_request(), and\nprocess_request(). If the user-provided handle() method of the\nhandler class raises an exception, the server’s handle_error() method\nwill be called. If no request is received within self.timeout\nseconds, handle_timeout() will be called and handle_request()\nwill return.
\n\n
\n
\nBaseServer.serve_forever(poll_interval=0.5)
\n
Handle requests until an explicit shutdown() request.\nPoll for shutdown every poll_interval seconds. Ignores self.timeout.\nIf you need to do periodic tasks, do them in another thread.
\n\n
\n
\nBaseServer.shutdown()
\n

Tell the serve_forever() loop to stop and wait until it does.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nBaseServer.address_family
\n
The family of protocols to which the server’s socket belongs.\nCommon examples are socket.AF_INET and socket.AF_UNIX.
\n\n
\n
\nBaseServer.RequestHandlerClass
\n
The user-provided request handler class; an instance of this class is created\nfor each request.
\n\n
\n
\nBaseServer.server_address
\n
The address on which the server is listening. The format of addresses varies\ndepending on the protocol family; see the documentation for the socket module\nfor details. For Internet protocols, this is a tuple containing a string giving\nthe address, and an integer port number: ('127.0.0.1', 80), for example.
\n\n
\n
\nBaseServer.socket
\n
The socket object on which the server will listen for incoming requests.
\n\n

The server classes support the following class variables:

\n
\n
\nBaseServer.allow_reuse_address
\n
Whether the server will allow the reuse of an address. This defaults to\nFalse, and can be set in subclasses to change the policy.
\n\n
\n
\nBaseServer.request_queue_size
\n
The size of the request queue. If it takes a long time to process a single\nrequest, any requests that arrive while the server is busy are placed into a\nqueue, up to request_queue_size requests. Once the queue is full,\nfurther requests from clients will get a “Connection denied” error. The default\nvalue is usually 5, but this can be overridden by subclasses.
\n\n
\n
\nBaseServer.socket_type
\n
The type of socket used by the server; socket.SOCK_STREAM and\nsocket.SOCK_DGRAM are two common values.
\n\n
\n
\nBaseServer.timeout
\n
Timeout duration, measured in seconds, or None if no timeout is\ndesired. If handle_request() receives no incoming requests within the\ntimeout period, the handle_timeout() method is called.
\n\n

There are various server methods that can be overridden by subclasses of base\nserver classes like TCPServer; these methods aren’t useful to external\nusers of the server object.

\n
\n
\nBaseServer.finish_request()
\n
Actually processes the request by instantiating RequestHandlerClass and\ncalling its handle() method.
\n\n
\n
\nBaseServer.get_request()
\n
Must accept a request from the socket, and return a 2-tuple containing the new\nsocket object to be used to communicate with the client, and the client’s\naddress.
\n\n
\n
\nBaseServer.handle_error(request, client_address)
\n
This function is called if the RequestHandlerClass‘s handle()\nmethod raises an exception. The default action is to print the traceback to\nstandard output and continue handling further requests.
\n\n
\n
\nBaseServer.handle_timeout()
\n
This function is called when the timeout attribute has been set to a\nvalue other than None and the timeout period has passed with no\nrequests being received. The default action for forking servers is\nto collect the status of any child processes that have exited, while\nin threading servers this method does nothing.
\n\n
\n
\nBaseServer.process_request(request, client_address)
\n
Calls finish_request() to create an instance of the\nRequestHandlerClass. If desired, this function can create a new process\nor thread to handle the request; the ForkingMixIn and\nThreadingMixIn classes do this.
\n\n
\n
\nBaseServer.server_activate()
\n
Called by the server’s constructor to activate the server. The default behavior\njust listen()s to the server’s socket. May be overridden.
\n\n
\n
\nBaseServer.server_bind()
\n
Called by the server’s constructor to bind the socket to the desired address.\nMay be overridden.
\n\n
\n
\nBaseServer.verify_request(request, client_address)
\n
Must return a Boolean value; if the value is True, the request will be\nprocessed, and if it’s False, the request will be denied. This function\ncan be overridden to implement access controls for a server. The default\nimplementation always returns True.
\n\n
\n
\n

20.17.3. RequestHandler Objects

\n

The request handler class must define a new handle() method, and can\noverride any of the following methods. A new instance is created for each\nrequest.

\n
\n
\nRequestHandler.finish()
\n
Called after the handle() method to perform any clean-up actions\nrequired. The default implementation does nothing. If setup() or\nhandle() raise an exception, this function will not be called.
\n\n
\n
\nRequestHandler.handle()
\n

This function must do all the work required to service a request. The\ndefault implementation does nothing. Several instance attributes are\navailable to it; the request is available as self.request; the client\naddress as self.client_address; and the server instance as\nself.server, in case it needs access to per-server information.

\n

The type of self.request is different for datagram or stream\nservices. For stream services, self.request is a socket object; for\ndatagram services, self.request is a pair of string and socket.\nHowever, this can be hidden by using the request handler subclasses\nStreamRequestHandler or DatagramRequestHandler, which\noverride the setup() and finish() methods, and provide\nself.rfile and self.wfile attributes. self.rfile and\nself.wfile can be read or written, respectively, to get the request\ndata or return data to the client.

\n
\n\n
\n
\nRequestHandler.setup()
\n
Called before the handle() method to perform any initialization actions\nrequired. The default implementation does nothing.
\n\n
\n
\n

20.17.4. Examples

\n
\n

20.17.4.1. SocketServer.TCPServer Example

\n

This is the server side:

\n
import SocketServer\n\nclass MyTCPHandler(SocketServer.BaseRequestHandler):\n    """\n    The RequestHandler class for our server.\n\n    It is instantiated once per connection to the server, and must\n    override the handle() method to implement communication to the\n    client.\n    """\n\n    def handle(self):\n        # self.request is the TCP socket connected to the client\n        self.data = self.request.recv(1024).strip()\n        print "{} wrote:".format(self.client_address[0])\n        print self.data\n        # just send back the same data, but upper-cased\n        self.request.send(self.data.upper())\n\nif __name__ == "__main__":\n    HOST, PORT = "localhost", 9999\n\n    # Create the server, binding to localhost on port 9999\n    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)\n\n    # Activate the server; this will keep running until you\n    # interrupt the program with Ctrl-C\n    server.serve_forever()\n
\n
\n

An alternative request handler class that makes use of streams (file-like\nobjects that simplify communication by providing the standard file interface):

\n
class MyTCPHandler(SocketServer.StreamRequestHandler):\n\n    def handle(self):\n        # self.rfile is a file-like object created by the handler;\n        # we can now use e.g. readline() instead of raw recv() calls\n        self.data = self.rfile.readline().strip()\n        print "{} wrote:".format(self.client_address[0])\n        print self.data\n        # Likewise, self.wfile is a file-like object used to write back\n        # to the client\n        self.wfile.write(self.data.upper())\n
\n
\n

The difference is that the readline() call in the second handler will call\nrecv() multiple times until it encounters a newline character, while the\nsingle recv() call in the first handler will just return what has been sent\nfrom the client in one send() call.

\n

This is the client side:

\n
import socket\nimport sys\n\nHOST, PORT = "localhost", 9999\ndata = " ".join(sys.argv[1:])\n\n# Create a socket (SOCK_STREAM means a TCP socket)\nsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n\ntry:\n    # Connect to server and send data\n    sock.connect((HOST, PORT))\n    sock.send(data + "\\n")\n\n    # Receive data from the server and shut down\n    received = sock.recv(1024)\nfinally:\n    sock.close()\n\nprint "Sent:     {}".format(data)\nprint "Received: {}".format(received)\n
\n
\n

The output of the example should look something like this:

\n

Server:

\n
$ python TCPServer.py\n127.0.0.1 wrote:\nhello world with TCP\n127.0.0.1 wrote:\npython is nice
\n
\n

Client:

\n
$ python TCPClient.py hello world with TCP\nSent:     hello world with TCP\nReceived: HELLO WORLD WITH TCP\n$ python TCPClient.py python is nice\nSent:     python is nice\nReceived: PYTHON IS NICE
\n
\n
\n
\n

20.17.4.2. SocketServer.UDPServer Example

\n

This is the server side:

\n
import SocketServer\n\nclass MyUDPHandler(SocketServer.BaseRequestHandler):\n    """\n    This class works similar to the TCP handler class, except that\n    self.request consists of a pair of data and client socket, and since\n    there is no connection the client address must be given explicitly\n    when sending data back via sendto().\n    """\n\n    def handle(self):\n        data = self.request[0].strip()\n        socket = self.request[1]\n        print "{} wrote:".format(self.client_address[0])\n        print data\n        socket.sendto(data.upper(), self.client_address)\n\nif __name__ == "__main__":\n    HOST, PORT = "localhost", 9999\n    server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)\n    server.serve_forever()\n
\n
\n

This is the client side:

\n
import socket\nimport sys\n\nHOST, PORT = "localhost", 9999\ndata = " ".join(sys.argv[1:])\n\n# SOCK_DGRAM is the socket type to use for UDP sockets\nsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\n\n# As you can see, there is no connect() call; UDP has no connections.\n# Instead, data is directly sent to the recipient via sendto().\nsock.sendto(data + "\\n", (HOST, PORT))\nreceived = sock.recv(1024)\n\nprint "Sent:     {}".format(data)\nprint "Received: {}".format(received)\n
\n
\n

The output of the example should look exactly like for the TCP server example.

\n
\n
\n

20.17.4.3. Asynchronous Mixins

\n

To build asynchronous handlers, use the ThreadingMixIn and\nForkingMixIn classes.

\n

An example for the ThreadingMixIn class:

\n
import socket\nimport threading\nimport SocketServer\n\nclass ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):\n\n    def handle(self):\n        data = self.request.recv(1024)\n        cur_thread = threading.current_thread()\n        response = "{}: {}".format(cur_thread.name, data)\n        self.request.send(response)\n\nclass ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):\n    pass\n\ndef client(ip, port, message):\n    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    sock.connect((ip, port))\n    try:\n        sock.send(message)\n        response = sock.recv(1024)\n        print "Received: {}".format(response)\n    finally:\n        sock.close()\n\nif __name__ == "__main__":\n    # Port 0 means to select an arbitrary unused port\n    HOST, PORT = "localhost", 0\n\n    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)\n    ip, port = server.server_address\n\n    # Start a thread with the server -- that thread will then start one\n    # more thread for each request\n    server_thread = threading.Thread(target=server.serve_forever)\n    # Exit the server thread when the main thread terminates\n    server_thread.daemon = True\n    server_thread.start()\n    print "Server loop running in thread:", server_thread.name\n\n    client(ip, port, "Hello World 1")\n    client(ip, port, "Hello World 2")\n    client(ip, port, "Hello World 3")\n\n    server.shutdown()\n
\n
\n

The output of the example should look something like this:

\n
$ python ThreadedTCPServer.py\nServer loop running in thread: Thread-1\nReceived: Thread-2: Hello World 1\nReceived: Thread-3: Hello World 2\nReceived: Thread-4: Hello World 3
\n
\n

The ForkingMixIn class is used in the same way, except that the server\nwill spawn a new process for each request.

\n
\n
\n
", "searchableItems": [ { "name": "SocketServer.BaseServer", "domId": "SocketServer_SocketServer.BaseServer" }, { "name": "SocketServer.BaseServer.fileno", "domId": "SocketServer_SocketServer.BaseServer.fileno" }, { "name": "SocketServer.BaseServer.finish_request", "domId": "SocketServer_SocketServer.BaseServer.finish_request" }, { "name": "SocketServer.BaseServer.get_request", "domId": "SocketServer_SocketServer.BaseServer.get_request" }, { "name": "SocketServer.BaseServer.handle_error", "domId": "SocketServer_SocketServer.BaseServer.handle_error" }, { "name": "SocketServer.BaseServer.handle_request", "domId": "SocketServer_SocketServer.BaseServer.handle_request" }, { "name": "SocketServer.BaseServer.handle_timeout", "domId": "SocketServer_SocketServer.BaseServer.handle_timeout" }, { "name": "SocketServer.BaseServer.process_request", "domId": "SocketServer_SocketServer.BaseServer.process_request" }, { "name": "SocketServer.BaseServer.serve_forever", "domId": "SocketServer_SocketServer.BaseServer.serve_forever" }, { "name": "SocketServer.BaseServer.server_activate", "domId": "SocketServer_SocketServer.BaseServer.server_activate" }, { "name": "SocketServer.BaseServer.server_bind", "domId": "SocketServer_SocketServer.BaseServer.server_bind" }, { "name": "SocketServer.BaseServer.shutdown", "domId": "SocketServer_SocketServer.BaseServer.shutdown" }, { "name": "SocketServer.BaseServer.verify_request", "domId": "SocketServer_SocketServer.BaseServer.verify_request" }, { "name": "SocketServer.RequestHandler.finish", "domId": "SocketServer_SocketServer.RequestHandler.finish" }, { "name": "SocketServer.RequestHandler.handle", "domId": "SocketServer_SocketServer.RequestHandler.handle" }, { "name": "SocketServer.RequestHandler.setup", "domId": "SocketServer_SocketServer.RequestHandler.setup" } ] }, { "url": "http://docs.python.org/library/docxmlrpcserver.html", "title": "DocXMLRPCServer", "html": "
\n

20.25. DocXMLRPCServer — Self-documenting XML-RPC server

\n
\n

Note

\n

The DocXMLRPCServer module has been merged into xmlrpc.server\nin Python 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

\nNew in version 2.3.

\n

The DocXMLRPCServer module extends the classes found in\nSimpleXMLRPCServer to serve HTML documentation in response to HTTP GET\nrequests. Servers can either be free standing, using DocXMLRPCServer,\nor embedded in a CGI environment, using DocCGIXMLRPCRequestHandler.

\n
\n
\nclass DocXMLRPCServer.DocXMLRPCServer(addr[, requestHandler[, logRequests[, allow_none[, encoding[, bind_and_activate]]]]])
\n
Create a new server instance. All parameters have the same meaning as for\nSimpleXMLRPCServer.SimpleXMLRPCServer; requestHandler defaults to\nDocXMLRPCRequestHandler.
\n\n
\n
\nclass DocXMLRPCServer.DocCGIXMLRPCRequestHandler
\n
Create a new instance to handle XML-RPC requests in a CGI environment.
\n\n
\n
\nclass DocXMLRPCServer.DocXMLRPCRequestHandler
\n
Create a new request handler instance. This request handler supports XML-RPC\nPOST requests, documentation GET requests, and modifies logging so that the\nlogRequests parameter to the DocXMLRPCServer constructor parameter is\nhonored.
\n\n
\n

20.25.1. DocXMLRPCServer Objects

\n

The DocXMLRPCServer class is derived from\nSimpleXMLRPCServer.SimpleXMLRPCServer and provides a means of creating\nself-documenting, stand alone XML-RPC servers. HTTP POST requests are handled as\nXML-RPC method calls. HTTP GET requests are handled by generating pydoc-style\nHTML documentation. This allows a server to provide its own web-based\ndocumentation.

\n
\n
\nDocXMLRPCServer.set_server_title(server_title)
\n
Set the title used in the generated HTML documentation. This title will be used\ninside the HTML “title” element.
\n\n
\n
\nDocXMLRPCServer.set_server_name(server_name)
\n
Set the name used in the generated HTML documentation. This name will appear at\nthe top of the generated documentation inside a “h1” element.
\n\n
\n
\nDocXMLRPCServer.set_server_documentation(server_documentation)
\n
Set the description used in the generated HTML documentation. This description\nwill appear as a paragraph, below the server name, in the documentation.
\n\n
\n
\n

20.25.2. DocCGIXMLRPCRequestHandler

\n

The DocCGIXMLRPCRequestHandler class is derived from\nSimpleXMLRPCServer.CGIXMLRPCRequestHandler and provides a means of\ncreating self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled\nas XML-RPC method calls. HTTP GET requests are handled by generating pydoc-style\nHTML documentation. This allows a server to provide its own web-based\ndocumentation.

\n
\n
\nDocCGIXMLRPCRequestHandler.set_server_title(server_title)
\n
Set the title used in the generated HTML documentation. This title will be used\ninside the HTML “title” element.
\n\n
\n
\nDocCGIXMLRPCRequestHandler.set_server_name(server_name)
\n
Set the name used in the generated HTML documentation. This name will appear at\nthe top of the generated documentation inside a “h1” element.
\n\n
\n
\nDocCGIXMLRPCRequestHandler.set_server_documentation(server_documentation)
\n
Set the description used in the generated HTML documentation. This description\nwill appear as a paragraph, below the server name, in the documentation.
\n\n
\n
", "searchableItems": [ { "name": "DocXMLRPCServer.DocCGIXMLRPCRequestHandler", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocCGIXMLRPCRequestHandler" }, { "name": "DocXMLRPCServer.DocCGIXMLRPCRequestHandler.set_server_documentation", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocCGIXMLRPCRequestHandler.set_server_documentation" }, { "name": "DocXMLRPCServer.DocCGIXMLRPCRequestHandler.set_server_name", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocCGIXMLRPCRequestHandler.set_server_name" }, { "name": "DocXMLRPCServer.DocCGIXMLRPCRequestHandler.set_server_title", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocCGIXMLRPCRequestHandler.set_server_title" }, { "name": "DocXMLRPCServer.DocXMLRPCRequestHandler", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocXMLRPCRequestHandler" }, { "name": "DocXMLRPCServer.DocXMLRPCServer", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocXMLRPCServer" }, { "name": "DocXMLRPCServer.DocXMLRPCServer.set_server_documentation", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocXMLRPCServer.set_server_documentation" }, { "name": "DocXMLRPCServer.DocXMLRPCServer.set_server_name", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocXMLRPCServer.set_server_name" }, { "name": "DocXMLRPCServer.DocXMLRPCServer.set_server_title", "domId": "DocXMLRPCServer_DocXMLRPCServer.DocXMLRPCServer.set_server_title" } ] }, { "url": "http://docs.python.org/library/simplexmlrpcserver.html", "title": "SimpleXMLRPCServer", "html": "
\n

20.24. SimpleXMLRPCServer — Basic XML-RPC server

\n
\n

Note

\n

The SimpleXMLRPCServer module has been merged into\nxmlrpc.server in Python 3.0. The 2to3 tool will automatically\nadapt imports when converting your sources to 3.0.

\n
\n

\nNew in version 2.2.

\n

Source code: Lib/SimpleXMLRPCServer.py

\n
\n

The SimpleXMLRPCServer module provides a basic server framework for\nXML-RPC servers written in Python. Servers can either be free standing, using\nSimpleXMLRPCServer, or embedded in a CGI environment, using\nCGIXMLRPCRequestHandler.

\n
\n
\nSimpleXMLRPCServer(addr[, requestHandler[, logRequests[, allow_none[, encoding[, bind_and_activate]]]])
\n

Create a new server instance. This class provides methods for registration of\nfunctions that can be called by the XML-RPC protocol. The requestHandler\nparameter should be a factory for request handler instances; it defaults to\nSimpleXMLRPCRequestHandler. The addr and requestHandler parameters\nare passed to the SocketServer.TCPServer constructor. If logRequests\nis true (the default), requests will be logged; setting this parameter to false\nwill turn off logging. The allow_none and encoding parameters are passed\non to xmlrpclib and control the XML-RPC responses that will be returned\nfrom the server. The bind_and_activate parameter controls whether\nserver_bind() and server_activate() are called immediately by the\nconstructor; it defaults to true. Setting it to false allows code to manipulate\nthe allow_reuse_address class variable before the address is bound.

\n

\nChanged in version 2.5: The allow_none and encoding parameters were added.

\n

\nChanged in version 2.6: The bind_and_activate parameter was added.

\n
\n\n
\n
\nclass SimpleXMLRPCServer.CGIXMLRPCRequestHandler([allow_none[, encoding]])
\n

Create a new instance to handle XML-RPC requests in a CGI environment. The\nallow_none and encoding parameters are passed on to xmlrpclib and\ncontrol the XML-RPC responses that will be returned from the server.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.5: The allow_none and encoding parameters were added.

\n
\n\n
\n
\nclass SimpleXMLRPCServer.SimpleXMLRPCRequestHandler
\n
Create a new request handler instance. This request handler supports POST\nrequests and modifies logging so that the logRequests parameter to the\nSimpleXMLRPCServer constructor parameter is honored.
\n\n
\n

20.24.1. SimpleXMLRPCServer Objects

\n

The SimpleXMLRPCServer class is based on\nSocketServer.TCPServer and provides a means of creating simple, stand\nalone XML-RPC servers.

\n
\n
\nSimpleXMLRPCServer.register_function(function[, name])
\n
Register a function that can respond to XML-RPC requests. If name is given,\nit will be the method name associated with function, otherwise\nfunction.__name__ will be used. name can be either a normal or Unicode\nstring, and may contain characters not legal in Python identifiers, including\nthe period character.
\n\n
\n
\nSimpleXMLRPCServer.register_instance(instance[, allow_dotted_names])
\n

Register an object which is used to expose method names which have not been\nregistered using register_function(). If instance contains a\n_dispatch() method, it is called with the requested method name and the\nparameters from the request. Its API is def _dispatch(self, method, params)\n(note that params does not represent a variable argument list). If it calls\nan underlying function to perform its task, that function is called as\nfunc(*params), expanding the parameter list. The return value from\n_dispatch() is returned to the client as the result. If instance does\nnot have a _dispatch() method, it is searched for an attribute matching\nthe name of the requested method.

\n

If the optional allow_dotted_names argument is true and the instance does not\nhave a _dispatch() method, then if the requested method name contains\nperiods, each component of the method name is searched for individually, with\nthe effect that a simple hierarchical search is performed. The value found from\nthis search is then called with the parameters from the request, and the return\nvalue is passed back to the client.

\n
\n

Warning

\n

Enabling the allow_dotted_names option allows intruders to access your\nmodule’s global variables and may allow intruders to execute arbitrary code on\nyour machine. Only use this option on a secure, closed network.

\n
\n

\nChanged in version 2.3.5,: 2.4.1\nallow_dotted_names was added to plug a security hole; prior versions are\ninsecure.

\n
\n\n
\n
\nSimpleXMLRPCServer.register_introspection_functions()
\n

Registers the XML-RPC introspection functions system.listMethods,\nsystem.methodHelp and system.methodSignature.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nSimpleXMLRPCServer.register_multicall_functions()
\n
Registers the XML-RPC multicall function system.multicall.
\n\n
\n
\nSimpleXMLRPCRequestHandler.rpc_paths
\n

An attribute value that must be a tuple listing valid path portions of the URL\nfor receiving XML-RPC requests. Requests posted to other paths will result in a\n404 “no such page” HTTP error. If this tuple is empty, all paths will be\nconsidered valid. The default value is ('/', '/RPC2').

\n

\nNew in version 2.5.

\n
\n\n
\n
\nSimpleXMLRPCRequestHandler.encode_threshold
\n

If this attribute is not None, responses larger than this value\nwill be encoded using the gzip transfer encoding, if permitted by\nthe client. The default is 1400 which corresponds roughly\nto a single TCP packet.

\n

\nNew in version 2.7.

\n
\n\n
\n

20.24.1.1. SimpleXMLRPCServer Example

\n

Server code:

\n
from SimpleXMLRPCServer import SimpleXMLRPCServer\nfrom SimpleXMLRPCServer import SimpleXMLRPCRequestHandler\n\n# Restrict to a particular path.\nclass RequestHandler(SimpleXMLRPCRequestHandler):\n    rpc_paths = ('/RPC2',)\n\n# Create server\nserver = SimpleXMLRPCServer(("localhost", 8000),\n                            requestHandler=RequestHandler)\nserver.register_introspection_functions()\n\n# Register pow() function; this will use the value of\n# pow.__name__ as the name, which is just 'pow'.\nserver.register_function(pow)\n\n# Register a function under a different name\ndef adder_function(x,y):\n    return x + y\nserver.register_function(adder_function, 'add')\n\n# Register an instance; all the methods of the instance are\n# published as XML-RPC methods (in this case, just 'div').\nclass MyFuncs:\n    def div(self, x, y):\n        return x // y\n\nserver.register_instance(MyFuncs())\n\n# Run the server's main loop\nserver.serve_forever()\n
\n
\n

The following client code will call the methods made available by the preceding\nserver:

\n
import xmlrpclib\n\ns = xmlrpclib.ServerProxy('http://localhost:8000')\nprint s.pow(2,3)  # Returns 2**3 = 8\nprint s.add(2,3)  # Returns 5\nprint s.div(5,2)  # Returns 5//2 = 2\n\n# Print list of available methods\nprint s.system.listMethods()\n
\n
\n
\n
\n
\n

20.24.2. CGIXMLRPCRequestHandler

\n

The CGIXMLRPCRequestHandler class can be used to handle XML-RPC\nrequests sent to Python CGI scripts.

\n
\n
\nCGIXMLRPCRequestHandler.register_function(function[, name])
\n
Register a function that can respond to XML-RPC requests. If name is given,\nit will be the method name associated with function, otherwise\nfunction.__name__ will be used. name can be either a normal or Unicode\nstring, and may contain characters not legal in Python identifiers, including\nthe period character.
\n\n
\n
\nCGIXMLRPCRequestHandler.register_instance(instance)
\n
Register an object which is used to expose method names which have not been\nregistered using register_function(). If instance contains a\n_dispatch() method, it is called with the requested method name and the\nparameters from the request; the return value is returned to the client as the\nresult. If instance does not have a _dispatch() method, it is searched\nfor an attribute matching the name of the requested method; if the requested\nmethod name contains periods, each component of the method name is searched for\nindividually, with the effect that a simple hierarchical search is performed.\nThe value found from this search is then called with the parameters from the\nrequest, and the return value is passed back to the client.
\n\n
\n
\nCGIXMLRPCRequestHandler.register_introspection_functions()
\n
Register the XML-RPC introspection functions system.listMethods,\nsystem.methodHelp and system.methodSignature.
\n\n
\n
\nCGIXMLRPCRequestHandler.register_multicall_functions()
\n
Register the XML-RPC multicall function system.multicall.
\n\n
\n
\nCGIXMLRPCRequestHandler.handle_request([request_text = None])
\n
Handle a XML-RPC request. If request_text is given, it should be the POST\ndata provided by the HTTP server, otherwise the contents of stdin will be used.
\n\n

Example:

\n
class MyFuncs:\n    def div(self, x, y) : return x // y\n\n\nhandler = CGIXMLRPCRequestHandler()\nhandler.register_function(pow)\nhandler.register_function(lambda x,y: x+y, 'add')\nhandler.register_introspection_functions()\nhandler.register_instance(MyFuncs())\nhandler.handle_request()\n
\n
\n
\n
", "searchableItems": [ { "name": "SimpleXMLRPCServer.CGIXMLRPCRequestHandler", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.CGIXMLRPCRequestHandler" }, { "name": "SimpleXMLRPCServer.CGIXMLRPCRequestHandler.handle_request", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.CGIXMLRPCRequestHandler.handle_request" }, { "name": "SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_function", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_function" }, { "name": "SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_instance", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_instance" }, { "name": "SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_introspection_functions", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_introspection_functions" }, { "name": "SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_multicall_functions", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.CGIXMLRPCRequestHandler.register_multicall_functions" }, { "name": "SimpleXMLRPCServer.SimpleXMLRPCRequestHandler", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.SimpleXMLRPCRequestHandler" }, { "name": "SimpleXMLRPCServer.SimpleXMLRPCServer.register_function", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.SimpleXMLRPCServer.register_function" }, { "name": "SimpleXMLRPCServer.SimpleXMLRPCServer.register_instance", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.SimpleXMLRPCServer.register_instance" }, { "name": "SimpleXMLRPCServer.SimpleXMLRPCServer.register_introspection_functions", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.SimpleXMLRPCServer.register_introspection_functions" }, { "name": "SimpleXMLRPCServer.SimpleXMLRPCServer.register_multicall_functions", "domId": "SimpleXMLRPCServer_SimpleXMLRPCServer.SimpleXMLRPCServer.register_multicall_functions" } ] }, { "url": "http://docs.python.org/library/xmlrpclib.html", "title": "xmlrpclib", "html": "
\n

20.23. xmlrpclib — XML-RPC client access

\n
\n

Note

\n

The xmlrpclib module has been renamed to xmlrpc.client in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

\nNew in version 2.2.

\n

Source code: Lib/xmlrpclib.py

\n
\n

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a\ntransport. With it, a client can call methods with parameters on a remote\nserver (the server is named by a URI) and get back structured data. This module\nsupports writing XML-RPC client code; it handles all the details of translating\nbetween conformable Python objects and XML on the wire.

\n
\n
\nclass xmlrpclib.ServerProxy(uri[, transport[, encoding[, verbose[, allow_none[, use_datetime]]]]])
\n

A ServerProxy instance is an object that manages communication with a\nremote XML-RPC server. The required first argument is a URI (Uniform Resource\nIndicator), and will normally be the URL of the server. The optional second\nargument is a transport factory instance; by default it is an internal\nSafeTransport instance for https: URLs and an internal HTTP\nTransport instance otherwise. The optional third argument is an\nencoding, by default UTF-8. The optional fourth argument is a debugging flag.\nIf allow_none is true, the Python constant None will be translated into\nXML; the default behaviour is for None to raise a TypeError. This is\na commonly-used extension to the XML-RPC specification, but isn’t supported by\nall clients and servers; see http://ontosys.com/xml-rpc/extensions.php for a\ndescription. The use_datetime flag can be used to cause date/time values to\nbe presented as datetime.datetime objects; this is false by default.\ndatetime.datetime objects may be passed to calls.

\n

Both the HTTP and HTTPS transports support the URL syntax extension for HTTP\nBasic Authentication: http://user:pass@host:port/path. The user:pass\nportion will be base64-encoded as an HTTP ‘Authorization’ header, and sent to\nthe remote server as part of the connection process when invoking an XML-RPC\nmethod. You only need to use this if the remote server requires a Basic\nAuthentication user and password.

\n

The returned instance is a proxy object with methods that can be used to invoke\ncorresponding RPC calls on the remote server. If the remote server supports the\nintrospection API, the proxy can also be used to query the remote server for the\nmethods it supports (service discovery) and fetch other server-associated\nmetadata.

\n

ServerProxy instance methods take Python basic types and objects as\narguments and return Python basic types and classes. Types that are conformable\n(e.g. that can be marshalled through XML), include the following (and except\nwhere noted, they are unmarshalled as the same Python type):

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameMeaning
booleanThe True and False\nconstants
integersPass in directly
floating-point numbersPass in directly
stringsPass in directly
arraysAny Python sequence type containing\nconformable elements. Arrays are returned\nas lists
structuresA Python dictionary. Keys must be strings,\nvalues may be any conformable type. Objects\nof user-defined classes can be passed in;\nonly their __dict__ attribute is\ntransmitted.
datesin seconds since the epoch (pass in an\ninstance of the DateTime class) or\na datetime.datetime instance.
binary datapass in an instance of the Binary\nwrapper class
\n

This is the full set of data types supported by XML-RPC. Method calls may also\nraise a special Fault instance, used to signal XML-RPC server errors, or\nProtocolError used to signal an error in the HTTP/HTTPS transport layer.\nBoth Fault and ProtocolError derive from a base class called\nError. Note that even though starting with Python 2.2 you can subclass\nbuilt-in types, the xmlrpclib module currently does not marshal instances of such\nsubclasses.

\n

When passing strings, characters special to XML such as <, >, and &\nwill be automatically escaped. However, it’s the caller’s responsibility to\nensure that the string is free of characters that aren’t allowed in XML, such as\nthe control characters with ASCII values between 0 and 31 (except, of course,\ntab, newline and carriage return); failing to do this will result in an XML-RPC\nrequest that isn’t well-formed XML. If you have to pass arbitrary strings via\nXML-RPC, use the Binary wrapper class described below.

\n

Server is retained as an alias for ServerProxy for backwards\ncompatibility. New code should use ServerProxy.

\n

\nChanged in version 2.5: The use_datetime flag was added.

\n

\nChanged in version 2.6: Instances of new-style classes can be passed in if they have an\n__dict__ attribute and don’t have a base class that is marshalled in a\nspecial way.

\n
\n\n
\n

See also

\n
\n
XML-RPC HOWTO
\n
A good description of XML-RPC operation and client software in several languages.\nContains pretty much everything an XML-RPC client developer needs to know.
\n
XML-RPC Introspection
\n
Describes the XML-RPC protocol extension for introspection.
\n
XML-RPC Specification
\n
The official specification.
\n
Unofficial XML-RPC Errata
\n
Fredrik Lundh’s “unofficial errata, intended to clarify certain\ndetails in the XML-RPC specification, as well as hint at\n‘best practices’ to use when designing your own XML-RPC\nimplementations.”
\n
\n
\n
\n

20.23.1. ServerProxy Objects

\n

A ServerProxy instance has a method corresponding to each remote\nprocedure call accepted by the XML-RPC server. Calling the method performs an\nRPC, dispatched by both name and argument signature (e.g. the same method name\ncan be overloaded with multiple argument signatures). The RPC finishes by\nreturning a value, which may be either returned data in a conformant type or a\nFault or ProtocolError object indicating an error.

\n

Servers that support the XML introspection API support some common methods\ngrouped under the reserved system attribute:

\n
\n
\nServerProxy.system.listMethods()
\n
This method returns a list of strings, one for each (non-system) method\nsupported by the XML-RPC server.
\n\n
\n
\nServerProxy.system.methodSignature(name)
\n

This method takes one parameter, the name of a method implemented by the XML-RPC\nserver. It returns an array of possible signatures for this method. A signature\nis an array of types. The first of these types is the return type of the method,\nthe rest are parameters.

\n

Because multiple signatures (ie. overloading) is permitted, this method returns\na list of signatures rather than a singleton.

\n

Signatures themselves are restricted to the top level parameters expected by a\nmethod. For instance if a method expects one array of structs as a parameter,\nand it returns a string, its signature is simply “string, array”. If it expects\nthree integers and returns a string, its signature is “string, int, int, int”.

\n

If no signature is defined for the method, a non-array value is returned. In\nPython this means that the type of the returned value will be something other\nthan list.

\n
\n\n
\n
\nServerProxy.system.methodHelp(name)
\n
This method takes one parameter, the name of a method implemented by the XML-RPC\nserver. It returns a documentation string describing the use of that method. If\nno such string is available, an empty string is returned. The documentation\nstring may contain HTML markup.
\n\n
\n
\n

20.23.2. Boolean Objects

\n

This class may be initialized from any Python value; the instance returned\ndepends only on its truth value. It supports various Python operators through\n__cmp__(), __repr__(), __int__(), and __nonzero__()\nmethods, all implemented in the obvious ways.

\n

It also has the following method, supported mainly for internal use by the\nunmarshalling code:

\n
\n
\nBoolean.encode(out)
\n
Write the XML-RPC encoding of this Boolean item to the out stream object.
\n\n

A working example follows. The server code:

\n
import xmlrpclib\nfrom SimpleXMLRPCServer import SimpleXMLRPCServer\n\ndef is_even(n):\n    return n2 == 0\n\nserver = SimpleXMLRPCServer(("localhost", 8000))\nprint "Listening on port 8000..."\nserver.register_function(is_even, "is_even")\nserver.serve_forever()\n
\n
\n

The client code for the preceding server:

\n
import xmlrpclib\n\nproxy = xmlrpclib.ServerProxy("http://localhost:8000/")\nprint "3 is even: %s"  str(proxy.is_even(3))\nprint "100 is even: %s"  str(proxy.is_even(100))\n
\n
\n
\n
\n

20.23.3. DateTime Objects

\n

This class may be initialized with seconds since the epoch, a time\ntuple, an ISO 8601 time/date string, or a datetime.datetime\ninstance. It has the following methods, supported mainly for internal\nuse by the marshalling/unmarshalling code:

\n
\n
\nDateTime.decode(string)
\n
Accept a string as the instance’s new time value.
\n\n
\n
\nDateTime.encode(out)
\n
Write the XML-RPC encoding of this DateTime item to the out stream\nobject.
\n\n

It also supports certain of Python’s built-in operators through __cmp__()\nand __repr__() methods.

\n

A working example follows. The server code:

\n
import datetime\nfrom SimpleXMLRPCServer import SimpleXMLRPCServer\nimport xmlrpclib\n\ndef today():\n    today = datetime.datetime.today()\n    return xmlrpclib.DateTime(today)\n\nserver = SimpleXMLRPCServer(("localhost", 8000))\nprint "Listening on port 8000..."\nserver.register_function(today, "today")\nserver.serve_forever()\n
\n
\n

The client code for the preceding server:

\n
import xmlrpclib\nimport datetime\n\nproxy = xmlrpclib.ServerProxy("http://localhost:8000/")\n\ntoday = proxy.today()\n# convert the ISO8601 string to a datetime object\nconverted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")\nprint "Today: %s"  converted.strftime("%d.%m.%Y, %H:%M")\n
\n
\n
\n
\n

20.23.4. Binary Objects

\n

This class may be initialized from string data (which may include NULs). The\nprimary access to the content of a Binary object is provided by an\nattribute:

\n
\n
\nBinary.data
\n
The binary data encapsulated by the Binary instance. The data is\nprovided as an 8-bit string.
\n\n

Binary objects have the following methods, supported mainly for\ninternal use by the marshalling/unmarshalling code:

\n
\n
\nBinary.decode(string)
\n
Accept a base64 string and decode it as the instance’s new data.
\n\n
\n
\nBinary.encode(out)
\n

Write the XML-RPC base 64 encoding of this binary item to the out stream object.

\n

The encoded data will have newlines every 76 characters as per\nRFC 2045 section 6.8,\nwhich was the de facto standard base64 specification when the\nXML-RPC spec was written.

\n
\n\n

It also supports certain of Python’s built-in operators through a\n__cmp__() method.

\n

Example usage of the binary objects. We’re going to transfer an image over\nXMLRPC:

\n
from SimpleXMLRPCServer import SimpleXMLRPCServer\nimport xmlrpclib\n\ndef python_logo():\n     with open("python_logo.jpg", "rb") as handle:\n         return xmlrpclib.Binary(handle.read())\n\nserver = SimpleXMLRPCServer(("localhost", 8000))\nprint "Listening on port 8000..."\nserver.register_function(python_logo, 'python_logo')\n\nserver.serve_forever()\n
\n
\n

The client gets the image and saves it to a file:

\n
import xmlrpclib\n\nproxy = xmlrpclib.ServerProxy("http://localhost:8000/")\nwith open("fetched_python_logo.jpg", "wb") as handle:\n    handle.write(proxy.python_logo().data)\n
\n
\n
\n
\n

20.23.5. Fault Objects

\n

A Fault object encapsulates the content of an XML-RPC fault tag. Fault\nobjects have the following attributes:

\n
\n
\nFault.faultCode
\n
A string indicating the fault type.
\n\n
\n
\nFault.faultString
\n
A string containing a diagnostic message associated with the fault.
\n\n

In the following example we’re going to intentionally cause a Fault by\nreturning a complex type object. The server code:

\n
from SimpleXMLRPCServer import SimpleXMLRPCServer\n\n# A marshalling error is going to occur because we're returning a\n# complex number\ndef add(x,y):\n    return x+y+0j\n\nserver = SimpleXMLRPCServer(("localhost", 8000))\nprint "Listening on port 8000..."\nserver.register_function(add, 'add')\n\nserver.serve_forever()\n
\n
\n

The client code for the preceding server:

\n
import xmlrpclib\n\nproxy = xmlrpclib.ServerProxy("http://localhost:8000/")\ntry:\n    proxy.add(2, 5)\nexcept xmlrpclib.Fault, err:\n    print "A fault occurred"\n    print "Fault code: %d"  err.faultCode\n    print "Fault string: %s"  err.faultString\n
\n
\n
\n
\n

20.23.6. ProtocolError Objects

\n

A ProtocolError object describes a protocol error in the underlying\ntransport layer (such as a 404 ‘not found’ error if the server named by the URI\ndoes not exist). It has the following attributes:

\n
\n
\nProtocolError.url
\n
The URI or URL that triggered the error.
\n\n
\n
\nProtocolError.errcode
\n
The error code.
\n\n
\n
\nProtocolError.errmsg
\n
The error message or diagnostic string.
\n\n
\n
\nProtocolError.headers
\n
A string containing the headers of the HTTP/HTTPS request that triggered the\nerror.
\n\n

In the following example we’re going to intentionally cause a ProtocolError\nby providing an URI that doesn’t point to an XMLRPC server:

\n
import xmlrpclib\n\n# create a ServerProxy with an URI that doesn't respond to XMLRPC requests\nproxy = xmlrpclib.ServerProxy("http://www.google.com/")\n\ntry:\n    proxy.some_method()\nexcept xmlrpclib.ProtocolError, err:\n    print "A protocol error occurred"\n    print "URL: %s"  err.url\n    print "HTTP/HTTPS headers: %s"  err.headers\n    print "Error code: %d"  err.errcode\n    print "Error message: %s"  err.errmsg\n
\n
\n
\n
\n

20.23.7. MultiCall Objects

\n

\nNew in version 2.4.

\n

The MultiCall object provides a way to encapsulate multiple calls to a\nremote server into a single request [1].

\n
\n
\nclass xmlrpclib.MultiCall(server)
\n
Create an object used to boxcar method calls. server is the eventual target of\nthe call. Calls can be made to the result object, but they will immediately\nreturn None, and only store the call name and parameters in the\nMultiCall object. Calling the object itself causes all stored calls to\nbe transmitted as a single system.multicall request. The result of this call\nis a generator; iterating over this generator yields the individual\nresults.
\n\n

A usage example of this class follows. The server code

\n
from SimpleXMLRPCServer import SimpleXMLRPCServer\n\ndef add(x,y):\n    return x+y\n\ndef subtract(x, y):\n    return x-y\n\ndef multiply(x, y):\n    return x*y\n\ndef divide(x, y):\n    return x/y\n\n# A simple server with simple arithmetic functions\nserver = SimpleXMLRPCServer(("localhost", 8000))\nprint "Listening on port 8000..."\nserver.register_multicall_functions()\nserver.register_function(add, 'add')\nserver.register_function(subtract, 'subtract')\nserver.register_function(multiply, 'multiply')\nserver.register_function(divide, 'divide')\nserver.serve_forever()\n
\n
\n

The client code for the preceding server:

\n
import xmlrpclib\n\nproxy = xmlrpclib.ServerProxy("http://localhost:8000/")\nmulticall = xmlrpclib.MultiCall(proxy)\nmulticall.add(7,3)\nmulticall.subtract(7,3)\nmulticall.multiply(7,3)\nmulticall.divide(7,3)\nresult = multicall()\n\nprint "7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d"  tuple(result)\n
\n
\n
\n
\n

20.23.8. Convenience Functions

\n
\n
\nxmlrpclib.boolean(value)
\n
Convert any Python value to one of the XML-RPC Boolean constants, True or\nFalse.
\n\n
\n
\nxmlrpclib.dumps(params[, methodname[, methodresponse[, encoding[, allow_none]]]])
\n
Convert params into an XML-RPC request. or into a response if methodresponse\nis true. params can be either a tuple of arguments or an instance of the\nFault exception class. If methodresponse is true, only a single value\ncan be returned, meaning that params must be of length 1. encoding, if\nsupplied, is the encoding to use in the generated XML; the default is UTF-8.\nPython’s None value cannot be used in standard XML-RPC; to allow using\nit via an extension, provide a true value for allow_none.
\n\n
\n
\nxmlrpclib.loads(data[, use_datetime])
\n

Convert an XML-RPC request or response into Python objects, a (params,\nmethodname). params is a tuple of argument; methodname is a string, or\nNone if no method name is present in the packet. If the XML-RPC packet\nrepresents a fault condition, this function will raise a Fault exception.\nThe use_datetime flag can be used to cause date/time values to be presented as\ndatetime.datetime objects; this is false by default.

\n

\nChanged in version 2.5: The use_datetime flag was added.

\n
\n\n
\n
\n

20.23.9. Example of Client Usage

\n
# simple test program (from the XML-RPC specification)\nfrom xmlrpclib import ServerProxy, Error\n\n# server = ServerProxy("http://localhost:8000") # local server\nserver = ServerProxy("http://betty.userland.com")\n\nprint server\n\ntry:\n    print server.examples.getStateName(41)\nexcept Error, v:\n    print "ERROR", v\n
\n
\n

To access an XML-RPC server through a proxy, you need to define a custom\ntransport. The following example shows how:

\n
import xmlrpclib, httplib\n\nclass ProxiedTransport(xmlrpclib.Transport):\n    def set_proxy(self, proxy):\n        self.proxy = proxy\n    def make_connection(self, host):\n        self.realhost = host\n        h = httplib.HTTP(self.proxy)\n        return h\n    def send_request(self, connection, handler, request_body):\n        connection.putrequest("POST", 'http://%s%s'  (self.realhost, handler))\n    def send_host(self, connection, host):\n        connection.putheader('Host', self.realhost)\n\np = ProxiedTransport()\np.set_proxy('proxy-server:8080')\nserver = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)\nprint server.currentTime.getCurrentTime()\n
\n
\n
\n
\n

20.23.10. Example of Client and Server Usage

\n

See SimpleXMLRPCServer Example.

\n

Footnotes

\n\n\n\n\n\n
[1]This approach has been first presented in a discussion on xmlrpc.com.
\n
\n
", "searchableItems": [ { "name": "xmlrpclib.Binary.decode", "domId": "xmlrpclib_xmlrpclib.Binary.decode" }, { "name": "xmlrpclib.Binary.encode", "domId": "xmlrpclib_xmlrpclib.Binary.encode" }, { "name": "xmlrpclib.boolean", "domId": "xmlrpclib_xmlrpclib.boolean" }, { "name": "xmlrpclib.Boolean.encode", "domId": "xmlrpclib_xmlrpclib.Boolean.encode" }, { "name": "xmlrpclib.DateTime.decode", "domId": "xmlrpclib_xmlrpclib.DateTime.decode" }, { "name": "xmlrpclib.DateTime.encode", "domId": "xmlrpclib_xmlrpclib.DateTime.encode" }, { "name": "xmlrpclib.dumps", "domId": "xmlrpclib_xmlrpclib.dumps" }, { "name": "xmlrpclib.loads", "domId": "xmlrpclib_xmlrpclib.loads" }, { "name": "xmlrpclib.MultiCall", "domId": "xmlrpclib_xmlrpclib.MultiCall" }, { "name": "xmlrpclib.ServerProxy", "domId": "xmlrpclib_xmlrpclib.ServerProxy" }, { "name": "xmlrpclib.ServerProxy.system.listMethods", "domId": "xmlrpclib_xmlrpclib.ServerProxy.system.listMethods" }, { "name": "xmlrpclib.ServerProxy.system.methodHelp", "domId": "xmlrpclib_xmlrpclib.ServerProxy.system.methodHelp" }, { "name": "xmlrpclib.ServerProxy.system.methodSignature", "domId": "xmlrpclib_xmlrpclib.ServerProxy.system.methodSignature" } ] }, { "url": "http://docs.python.org/library/audioop.html", "title": "audioop", "html": "
\n

21.1. audioop — Manipulate raw audio data

\n

The audioop module contains some useful operations on sound fragments.\nIt operates on sound fragments consisting of signed integer samples 8, 16 or 32\nbits wide, stored in Python strings. This is the same format as used by the\nal and sunaudiodev modules. All scalar items are integers, unless\nspecified otherwise.

\n

This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.

\n

A few of the more complicated operations only take 16-bit samples, otherwise the\nsample size (in bytes) is always a parameter of the operation.

\n

The module defines the following variables and functions:

\n
\n
\nexception audioop.error
\n
This exception is raised on all errors, such as unknown number of bytes per\nsample, etc.
\n\n
\n
\naudioop.add(fragment1, fragment2, width)
\n
Return a fragment which is the addition of the two samples passed as parameters.\nwidth is the sample width in bytes, either 1, 2 or 4. Both\nfragments should have the same length.
\n\n
\n
\naudioop.adpcm2lin(adpcmfragment, width, state)
\n
Decode an Intel/DVI ADPCM coded fragment to a linear fragment. See the\ndescription of lin2adpcm() for details on ADPCM coding. Return a tuple\n(sample, newstate) where the sample has the width specified in width.
\n\n
\n
\naudioop.alaw2lin(fragment, width)
\n

Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.\na-LAW encoding always uses 8 bits samples, so width refers only to the sample\nwidth of the output fragment here.

\n

\nNew in version 2.5.

\n
\n\n
\n
\naudioop.avg(fragment, width)
\n
Return the average over all samples in the fragment.
\n\n
\n
\naudioop.avgpp(fragment, width)
\n
Return the average peak-peak value over all samples in the fragment. No\nfiltering is done, so the usefulness of this routine is questionable.
\n\n
\n
\naudioop.bias(fragment, width, bias)
\n
Return a fragment that is the original fragment with a bias added to each\nsample.
\n\n
\n
\naudioop.cross(fragment, width)
\n
Return the number of zero crossings in the fragment passed as an argument.
\n\n
\n
\naudioop.findfactor(fragment, reference)
\n

Return a factor F such that rms(add(fragment, mul(reference, -F))) is\nminimal, i.e., return the factor with which you should multiply reference to\nmake it match as well as possible to fragment. The fragments should both\ncontain 2-byte samples.

\n

The time taken by this routine is proportional to len(fragment).

\n
\n\n
\n
\naudioop.findfit(fragment, reference)
\n
Try to match reference as well as possible to a portion of fragment (which\nshould be the longer fragment). This is (conceptually) done by taking slices\nout of fragment, using findfactor() to compute the best match, and\nminimizing the result. The fragments should both contain 2-byte samples.\nReturn a tuple (offset, factor) where offset is the (integer) offset into\nfragment where the optimal match started and factor is the (floating-point)\nfactor as per findfactor().
\n\n
\n
\naudioop.findmax(fragment, length)
\n

Search fragment for a slice of length length samples (not bytes!) with\nmaximum energy, i.e., return i for which rms(fragment[i*2:(i+length)*2])\nis maximal. The fragments should both contain 2-byte samples.

\n

The routine takes time proportional to len(fragment).

\n
\n\n
\n
\naudioop.getsample(fragment, width, index)
\n
Return the value of sample index from the fragment.
\n\n
\n
\naudioop.lin2adpcm(fragment, width, state)
\n

Convert samples to 4 bit Intel/DVI ADPCM encoding. ADPCM coding is an adaptive\ncoding scheme, whereby each 4 bit number is the difference between one sample\nand the next, divided by a (varying) step. The Intel/DVI ADPCM algorithm has\nbeen selected for use by the IMA, so it may well become a standard.

\n

state is a tuple containing the state of the coder. The coder returns a tuple\n(adpcmfrag, newstate), and the newstate should be passed to the next call\nof lin2adpcm(). In the initial call, None can be passed as the state.\nadpcmfrag is the ADPCM coded fragment packed 2 4-bit values per byte.

\n
\n\n
\n
\naudioop.lin2alaw(fragment, width)
\n

Convert samples in the audio fragment to a-LAW encoding and return this as a\nPython string. a-LAW is an audio encoding format whereby you get a dynamic\nrange of about 13 bits using only 8 bit samples. It is used by the Sun audio\nhardware, among others.

\n

\nNew in version 2.5.

\n
\n\n
\n
\naudioop.lin2lin(fragment, width, newwidth)
\n

Convert samples between 1-, 2- and 4-byte formats.

\n
\n

Note

\n

In some audio formats, such as .WAV files, 16 and 32 bit samples are\nsigned, but 8 bit samples are unsigned. So when converting to 8 bit wide\nsamples for these formats, you need to also add 128 to the result:

\n
new_frames = audioop.lin2lin(frames, old_width, 1)\nnew_frames = audioop.bias(new_frames, 1, 128)\n
\n
\n

The same, in reverse, has to be applied when converting from 8 to 16 or 32\nbit width samples.

\n
\n
\n\n
\n
\naudioop.lin2ulaw(fragment, width)
\n
Convert samples in the audio fragment to u-LAW encoding and return this as a\nPython string. u-LAW is an audio encoding format whereby you get a dynamic\nrange of about 14 bits using only 8 bit samples. It is used by the Sun audio\nhardware, among others.
\n\n
\n
\naudioop.minmax(fragment, width)
\n
Return a tuple consisting of the minimum and maximum values of all samples in\nthe sound fragment.
\n\n
\n
\naudioop.max(fragment, width)
\n
Return the maximum of the absolute value of all samples in a fragment.
\n\n
\n
\naudioop.maxpp(fragment, width)
\n
Return the maximum peak-peak value in the sound fragment.
\n\n
\n
\naudioop.mul(fragment, width, factor)
\n
Return a fragment that has all samples in the original fragment multiplied by\nthe floating-point value factor. Overflow is silently ignored.
\n\n
\n
\naudioop.ratecv(fragment, width, nchannels, inrate, outrate, state[, weightA[, weightB]])
\n

Convert the frame rate of the input fragment.

\n

state is a tuple containing the state of the converter. The converter returns\na tuple (newfragment, newstate), and newstate should be passed to the next\ncall of ratecv(). The initial call should pass None as the state.

\n

The weightA and weightB arguments are parameters for a simple digital filter\nand default to 1 and 0 respectively.

\n
\n\n
\n
\naudioop.reverse(fragment, width)
\n
Reverse the samples in a fragment and returns the modified fragment.
\n\n
\n
\naudioop.rms(fragment, width)
\n

Return the root-mean-square of the fragment, i.e. sqrt(sum(S_i^2)/n).

\n

This is a measure of the power in an audio signal.

\n
\n\n
\n
\naudioop.tomono(fragment, width, lfactor, rfactor)
\n
Convert a stereo fragment to a mono fragment. The left channel is multiplied by\nlfactor and the right channel by rfactor before adding the two channels to\ngive a mono signal.
\n\n
\n
\naudioop.tostereo(fragment, width, lfactor, rfactor)
\n
Generate a stereo fragment from a mono fragment. Each pair of samples in the\nstereo fragment are computed from the mono sample, whereby left channel samples\nare multiplied by lfactor and right channel samples by rfactor.
\n\n
\n
\naudioop.ulaw2lin(fragment, width)
\n
Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.\nu-LAW encoding always uses 8 bits samples, so width refers only to the sample\nwidth of the output fragment here.
\n\n

Note that operations such as mul() or max() make no distinction\nbetween mono and stereo fragments, i.e. all samples are treated equal. If this\nis a problem the stereo fragment should be split into two mono fragments first\nand recombined later. Here is an example of how to do that:

\n
def mul_stereo(sample, width, lfactor, rfactor):\n    lsample = audioop.tomono(sample, width, 1, 0)\n    rsample = audioop.tomono(sample, width, 0, 1)\n    lsample = audioop.mul(lsample, width, lfactor)\n    rsample = audioop.mul(rsample, width, rfactor)\n    lsample = audioop.tostereo(lsample, width, 1, 0)\n    rsample = audioop.tostereo(rsample, width, 0, 1)\n    return audioop.add(lsample, rsample, width)\n
\n
\n

If you use the ADPCM coder to build network packets and you want your protocol\nto be stateless (i.e. to be able to tolerate packet loss) you should not only\ntransmit the data but also the state. Note that you should send the initial\nstate (the one you passed to lin2adpcm()) along to the decoder, not the\nfinal state (as returned by the coder). If you want to use\nstruct.struct() to store the state in binary you can code the first\nelement (the predicted value) in 16 bits and the second (the delta index) in 8.

\n

The ADPCM coders have never been tried against other ADPCM coders, only against\nthemselves. It could well be that I misinterpreted the standards in which case\nthey will not be interoperable with the respective standards.

\n

The find*() routines might look a bit funny at first sight. They are\nprimarily meant to do echo cancellation. A reasonably fast way to do this is to\npick the most energetic piece of the output sample, locate that in the input\nsample and subtract the whole output sample from the input sample:

\n
def echocancel(outputdata, inputdata):\n    pos = audioop.findmax(outputdata, 800)    # one tenth second\n    out_test = outputdata[pos*2:]\n    in_test = inputdata[pos*2:]\n    ipos, factor = audioop.findfit(in_test, out_test)\n    # Optional (for better cancellation):\n    # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],\n    #              out_test)\n    prefill = '\\0'*(pos+ipos)*2\n    postfill = '\\0'*(len(inputdata)-len(prefill)-len(outputdata))\n    outputdata = prefill + audioop.mul(outputdata,2,-factor) + postfill\n    return audioop.add(inputdata, outputdata, 2)\n
\n
\n
", "searchableItems": [ { "name": "audioop.add", "domId": "audioop_audioop.add" }, { "name": "audioop.adpcm2lin", "domId": "audioop_audioop.adpcm2lin" }, { "name": "audioop.alaw2lin", "domId": "audioop_audioop.alaw2lin" }, { "name": "audioop.avg", "domId": "audioop_audioop.avg" }, { "name": "audioop.avgpp", "domId": "audioop_audioop.avgpp" }, { "name": "audioop.bias", "domId": "audioop_audioop.bias" }, { "name": "audioop.cross", "domId": "audioop_audioop.cross" }, { "name": "audioop.findfactor", "domId": "audioop_audioop.findfactor" }, { "name": "audioop.findfit", "domId": "audioop_audioop.findfit" }, { "name": "audioop.findmax", "domId": "audioop_audioop.findmax" }, { "name": "audioop.getsample", "domId": "audioop_audioop.getsample" }, { "name": "audioop.lin2adpcm", "domId": "audioop_audioop.lin2adpcm" }, { "name": "audioop.lin2alaw", "domId": "audioop_audioop.lin2alaw" }, { "name": "audioop.lin2lin", "domId": "audioop_audioop.lin2lin" }, { "name": "audioop.lin2ulaw", "domId": "audioop_audioop.lin2ulaw" }, { "name": "audioop.max", "domId": "audioop_audioop.max" }, { "name": "audioop.maxpp", "domId": "audioop_audioop.maxpp" }, { "name": "audioop.minmax", "domId": "audioop_audioop.minmax" }, { "name": "audioop.mul", "domId": "audioop_audioop.mul" }, { "name": "audioop.ratecv", "domId": "audioop_audioop.ratecv" }, { "name": "audioop.reverse", "domId": "audioop_audioop.reverse" }, { "name": "audioop.rms", "domId": "audioop_audioop.rms" }, { "name": "audioop.tomono", "domId": "audioop_audioop.tomono" }, { "name": "audioop.tostereo", "domId": "audioop_audioop.tostereo" }, { "name": "audioop.ulaw2lin", "domId": "audioop_audioop.ulaw2lin" } ] }, { "url": "http://docs.python.org/library/imageop.html", "title": "imageop", "html": "
\n

21.2. imageop — Manipulate raw image data

\n

\nDeprecated since version 2.6: The imageop module has been removed in Python 3.0.

\n

The imageop module contains some useful operations on images. It operates\non images consisting of 8 or 32 bit pixels stored in Python strings. This is\nthe same format as used by gl.lrectwrite() and the imgfile module.

\n

The module defines the following variables and functions:

\n
\n
\nexception imageop.error
\n
This exception is raised on all errors, such as unknown number of bits per\npixel, etc.
\n\n
\n
\nimageop.crop(image, psize, width, height, x0, y0, x1, y1)
\n
Return the selected part of image, which should be width by height in size\nand consist of pixels of psize bytes. x0, y0, x1 and y1 are like the\ngl.lrectread() parameters, i.e. the boundary is included in the new image.\nThe new boundaries need not be inside the picture. Pixels that fall outside the\nold image will have their value set to zero. If x0 is bigger than x1 the\nnew image is mirrored. The same holds for the y coordinates.
\n\n
\n
\nimageop.scale(image, psize, width, height, newwidth, newheight)
\n
Return image scaled to size newwidth by newheight. No interpolation is\ndone, scaling is done by simple-minded pixel duplication or removal. Therefore,\ncomputer-generated images or dithered images will not look nice after scaling.
\n\n
\n
\nimageop.tovideo(image, psize, width, height)
\n
Run a vertical low-pass filter over an image. It does so by computing each\ndestination pixel as the average of two vertically-aligned source pixels. The\nmain use of this routine is to forestall excessive flicker if the image is\ndisplayed on a video device that uses interlacing, hence the name.
\n\n
\n
\nimageop.grey2mono(image, width, height, threshold)
\n
Convert a 8-bit deep greyscale image to a 1-bit deep image by thresholding all\nthe pixels. The resulting image is tightly packed and is probably only useful\nas an argument to mono2grey().
\n\n
\n
\nimageop.dither2mono(image, width, height)
\n
Convert an 8-bit greyscale image to a 1-bit monochrome image using a\n(simple-minded) dithering algorithm.
\n\n
\n
\nimageop.mono2grey(image, width, height, p0, p1)
\n
Convert a 1-bit monochrome image to an 8 bit greyscale or color image. All\npixels that are zero-valued on input get value p0 on output and all one-value\ninput pixels get value p1 on output. To convert a monochrome black-and-white\nimage to greyscale pass the values 0 and 255 respectively.
\n\n
\n
\nimageop.grey2grey4(image, width, height)
\n
Convert an 8-bit greyscale image to a 4-bit greyscale image without dithering.
\n\n
\n
\nimageop.grey2grey2(image, width, height)
\n
Convert an 8-bit greyscale image to a 2-bit greyscale image without dithering.
\n\n
\n
\nimageop.dither2grey2(image, width, height)
\n
Convert an 8-bit greyscale image to a 2-bit greyscale image with dithering. As\nfor dither2mono(), the dithering algorithm is currently very simple.
\n\n
\n
\nimageop.grey42grey(image, width, height)
\n
Convert a 4-bit greyscale image to an 8-bit greyscale image.
\n\n
\n
\nimageop.grey22grey(image, width, height)
\n
Convert a 2-bit greyscale image to an 8-bit greyscale image.
\n\n
\n
\nimageop.backward_compatible
\n
If set to 0, the functions in this module use a non-backward compatible way\nof representing multi-byte pixels on little-endian systems. The SGI for\nwhich this module was originally written is a big-endian system, so setting\nthis variable will have no effect. However, the code wasn’t originally\nintended to run on anything else, so it made assumptions about byte order\nwhich are not universal. Setting this variable to 0 will cause the byte\norder to be reversed on little-endian systems, so that it then is the same as\non big-endian systems.
\n\n
", "searchableItems": [ { "name": "imageop.crop", "domId": "imageop_imageop.crop" }, { "name": "imageop.dither2grey2", "domId": "imageop_imageop.dither2grey2" }, { "name": "imageop.dither2mono", "domId": "imageop_imageop.dither2mono" }, { "name": "imageop.grey22grey", "domId": "imageop_imageop.grey22grey" }, { "name": "imageop.grey2grey2", "domId": "imageop_imageop.grey2grey2" }, { "name": "imageop.grey2grey4", "domId": "imageop_imageop.grey2grey4" }, { "name": "imageop.grey2mono", "domId": "imageop_imageop.grey2mono" }, { "name": "imageop.grey42grey", "domId": "imageop_imageop.grey42grey" }, { "name": "imageop.mono2grey", "domId": "imageop_imageop.mono2grey" }, { "name": "imageop.scale", "domId": "imageop_imageop.scale" }, { "name": "imageop.tovideo", "domId": "imageop_imageop.tovideo" } ] }, { "url": "http://docs.python.org/library/cookie.html", "title": "Cookie", "html": "
\n

20.22. Cookie — HTTP state management

\n
\n

Note

\n

The Cookie module has been renamed to http.cookies in Python\n3.0. The 2to3 tool will automatically adapt imports when converting\nyour sources to 3.0.

\n
\n

Source code: Lib/Cookie.py

\n
\n

The Cookie module defines classes for abstracting the concept of\ncookies, an HTTP state management mechanism. It supports both simple string-only\ncookies, and provides an abstraction for having any serializable data-type as\ncookie value.

\n

The module formerly strictly applied the parsing rules described in the\nRFC 2109 and RFC 2068 specifications. It has since been discovered that\nMSIE 3.0x doesn’t follow the character rules outlined in those specs. As a\nresult, the parsing rules used are a bit less strict.

\n
\n

Note

\n

On encountering an invalid cookie, CookieError is raised, so if your\ncookie data comes from a browser you should always prepare for invalid data\nand catch CookieError on parsing.

\n
\n
\n
\nexception Cookie.CookieError
\n
Exception failing because of RFC 2109 invalidity: incorrect attributes,\nincorrect Set-Cookie header, etc.
\n\n
\n
\nclass Cookie.BaseCookie([input])
\n

This class is a dictionary-like object whose keys are strings and whose values\nare Morsel instances. Note that upon setting a key to a value, the\nvalue is first converted to a Morsel containing the key and the value.

\n

If input is given, it is passed to the load() method.

\n
\n\n
\n
\nclass Cookie.SimpleCookie([input])
\n
This class derives from BaseCookie and overrides value_decode()\nand value_encode() to be the identity and str() respectively.
\n\n
\n
\nclass Cookie.SerialCookie([input])
\n

This class derives from BaseCookie and overrides value_decode()\nand value_encode() to be the pickle.loads() and\npickle.dumps().

\n

\nDeprecated since version 2.3: Reading pickled values from untrusted cookie data is a huge security hole, as\npickle strings can be crafted to cause arbitrary code to execute on your server.\nIt is supported for backwards compatibility only, and may eventually go away.

\n
\n\n
\n
\nclass Cookie.SmartCookie([input])
\n

This class derives from BaseCookie. It overrides value_decode()\nto be pickle.loads() if it is a valid pickle, and otherwise the value\nitself. It overrides value_encode() to be pickle.dumps() unless it\nis a string, in which case it returns the value itself.

\n

\nDeprecated since version 2.3: The same security warning from SerialCookie applies here.

\n
\n\n

A further security note is warranted. For backwards compatibility, the\nCookie module exports a class named Cookie which is just an\nalias for SmartCookie. This is probably a mistake and will likely be\nremoved in a future version. You should not use the Cookie class in\nyour applications, for the same reason why you should not use the\nSerialCookie class.

\n
\n

See also

\n
\n
Module cookielib
\n
HTTP cookie handling for web clients. The cookielib and Cookie\nmodules do not depend on each other.
\n
RFC 2109 - HTTP State Management Mechanism
\n
This is the state management specification implemented by this module.
\n
\n
\n
\n

20.22.1. Cookie Objects

\n
\n
\nBaseCookie.value_decode(val)
\n
Return a decoded value from a string representation. Return value can be any\ntype. This method does nothing in BaseCookie — it exists so it can be\noverridden.
\n\n
\n
\nBaseCookie.value_encode(val)
\n

Return an encoded value. val can be any type, but return value must be a\nstring. This method does nothing in BaseCookie — it exists so it can\nbe overridden

\n

In general, it should be the case that value_encode() and\nvalue_decode() are inverses on the range of value_decode.

\n
\n\n
\n
\nBaseCookie.output([attrs[, header[, sep]]])
\n

Return a string representation suitable to be sent as HTTP headers. attrs and\nheader are sent to each Morsel‘s output() method. sep is used\nto join the headers together, and is by default the combination '\\r\\n'\n(CRLF).

\n

\nChanged in version 2.5: The default separator has been changed from '\\n' to match the cookie\nspecification.

\n
\n\n
\n
\nBaseCookie.js_output([attrs])
\n

Return an embeddable JavaScript snippet, which, if run on a browser which\nsupports JavaScript, will act the same as if the HTTP headers was sent.

\n

The meaning for attrs is the same as in output().

\n
\n\n
\n
\nBaseCookie.load(rawdata)
\n

If rawdata is a string, parse it as an HTTP_COOKIE and add the values\nfound there as Morsels. If it is a dictionary, it is equivalent to:

\n
for k, v in rawdata.items():\n    cookie[k] = v\n
\n
\n
\n\n
\n
\n

20.22.2. Morsel Objects

\n
\n
\nclass Cookie.Morsel
\n

Abstract a key/value pair, which has some RFC 2109 attributes.

\n

Morsels are dictionary-like objects, whose set of keys is constant — the valid\nRFC 2109 attributes, which are

\n
    \n
  • expires
  • \n
  • path
  • \n
  • comment
  • \n
  • domain
  • \n
  • max-age
  • \n
  • secure
  • \n
  • version
  • \n
  • httponly
  • \n
\n

The attribute httponly specifies that the cookie is only transfered\nin HTTP requests, and is not accessible through JavaScript. This is intended\nto mitigate some forms of cross-site scripting.

\n

The keys are case-insensitive.

\n

\nNew in version 2.6: The httponly attribute was added.

\n
\n\n
\n
\nMorsel.value
\n
The value of the cookie.
\n\n
\n
\nMorsel.coded_value
\n
The encoded value of the cookie — this is what should be sent.
\n\n
\n
\nMorsel.key
\n
The name of the cookie.
\n\n
\n
\nMorsel.set(key, value, coded_value)
\n
Set the key, value and coded_value attributes.
\n\n
\n
\nMorsel.isReservedKey(K)
\n
Whether K is a member of the set of keys of a Morsel.
\n\n
\n
\nMorsel.output([attrs[, header]])
\n
Return a string representation of the Morsel, suitable to be sent as an HTTP\nheader. By default, all the attributes are included, unless attrs is given, in\nwhich case it should be a list of attributes to use. header is by default\n"Set-Cookie:".
\n\n
\n
\nMorsel.js_output([attrs])
\n

Return an embeddable JavaScript snippet, which, if run on a browser which\nsupports JavaScript, will act the same as if the HTTP header was sent.

\n

The meaning for attrs is the same as in output().

\n
\n\n
\n
\nMorsel.OutputString([attrs])
\n

Return a string representing the Morsel, without any surrounding HTTP or\nJavaScript.

\n

The meaning for attrs is the same as in output().

\n
\n\n
\n
\n

20.22.3. Example

\n

The following example demonstrates how to use the Cookie module.

\n
>>> import Cookie\n>>> C = Cookie.SimpleCookie()\n>>> C["fig"] = "newton"\n>>> C["sugar"] = "wafer"\n>>> print C # generate HTTP headers\nSet-Cookie: fig=newton\nSet-Cookie: sugar=wafer\n>>> print C.output() # same thing\nSet-Cookie: fig=newton\nSet-Cookie: sugar=wafer\n>>> C = Cookie.SimpleCookie()\n>>> C["rocky"] = "road"\n>>> C["rocky"]["path"] = "/cookie"\n>>> print C.output(header="Cookie:")\nCookie: rocky=road; Path=/cookie\n>>> print C.output(attrs=[], header="Cookie:")\nCookie: rocky=road\n>>> C = Cookie.SimpleCookie()\n>>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)\n>>> print C\nSet-Cookie: chips=ahoy\nSet-Cookie: vienna=finger\n>>> C = Cookie.SimpleCookie()\n>>> C.load('keebler="E=everybody; L=\\\\"Loves\\\\"; fudge=\\\\012;";')\n>>> print C\nSet-Cookie: keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;"\n>>> C = Cookie.SimpleCookie()\n>>> C["oreo"] = "doublestuff"\n>>> C["oreo"]["path"] = "/"\n>>> print C\nSet-Cookie: oreo=doublestuff; Path=/\n>>> C["twix"] = "none for you"\n>>> C["twix"].value\n'none for you'\n>>> C = Cookie.SimpleCookie()\n>>> C["number"] = 7 # equivalent to C["number"] = str(7)\n>>> C["string"] = "seven"\n>>> C["number"].value\n'7'\n>>> C["string"].value\n'seven'\n>>> print C\nSet-Cookie: number=7\nSet-Cookie: string=seven\n>>> # SerialCookie and SmartCookie are deprecated\n>>> # using it can cause security loopholes in your code.\n>>> C = Cookie.SerialCookie()\n>>> C["number"] = 7\n>>> C["string"] = "seven"\n>>> C["number"].value\n7\n>>> C["string"].value\n'seven'\n>>> print C\nSet-Cookie: number="I7\\012."\nSet-Cookie: string="S'seven'\\012p1\\012."\n>>> C = Cookie.SmartCookie()\n>>> C["number"] = 7\n>>> C["string"] = "seven"\n>>> C["number"].value\n7\n>>> C["string"].value\n'seven'\n>>> print C\nSet-Cookie: number="I7\\012."\nSet-Cookie: string=seven\n
\n
\n
\n
", "searchableItems": [ { "name": "Cookie.BaseCookie", "domId": "Cookie_Cookie.BaseCookie" }, { "name": "Cookie.BaseCookie.js_output", "domId": "Cookie_Cookie.BaseCookie.js_output" }, { "name": "Cookie.BaseCookie.load", "domId": "Cookie_Cookie.BaseCookie.load" }, { "name": "Cookie.BaseCookie.output", "domId": "Cookie_Cookie.BaseCookie.output" }, { "name": "Cookie.BaseCookie.value_decode", "domId": "Cookie_Cookie.BaseCookie.value_decode" }, { "name": "Cookie.BaseCookie.value_encode", "domId": "Cookie_Cookie.BaseCookie.value_encode" }, { "name": "Cookie.Morsel", "domId": "Cookie_Cookie.Morsel" }, { "name": "Cookie.Morsel.isReservedKey", "domId": "Cookie_Cookie.Morsel.isReservedKey" }, { "name": "Cookie.Morsel.js_output", "domId": "Cookie_Cookie.Morsel.js_output" }, { "name": "Cookie.Morsel.output", "domId": "Cookie_Cookie.Morsel.output" }, { "name": "Cookie.Morsel.OutputString", "domId": "Cookie_Cookie.Morsel.OutputString" }, { "name": "Cookie.Morsel.set", "domId": "Cookie_Cookie.Morsel.set" }, { "name": "Cookie.SerialCookie", "domId": "Cookie_Cookie.SerialCookie" }, { "name": "Cookie.SimpleCookie", "domId": "Cookie_Cookie.SimpleCookie" }, { "name": "Cookie.SmartCookie", "domId": "Cookie_Cookie.SmartCookie" } ] }, { "url": "http://docs.python.org/library/sunau.html", "title": "sunau", "html": "
\n

21.4. sunau — Read and write Sun AU files

\n

Source code: Lib/sunau.py

\n
\n

The sunau module provides a convenient interface to the Sun AU sound\nformat. Note that this module is interface-compatible with the modules\naifc and wave.

\n

An audio file consists of a header followed by the data. The fields of the\nheader are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FieldContents
magic wordThe four bytes .snd.
header sizeSize of the header, including info, in bytes.
data sizePhysical size of the data, in bytes.
encodingIndicates how the audio samples are encoded.
sample rateThe sampling rate.
# of channelsThe number of channels in the samples.
infoASCII string giving a description of the\naudio file (padded with null bytes).
\n

Apart from the info field, all header fields are 4 bytes in size. They are all\n32-bit unsigned integers encoded in big-endian byte order.

\n

The sunau module defines the following functions:

\n
\n
\nsunau.open(file, mode)
\n

If file is a string, open the file by that name, otherwise treat it as a\nseekable file-like object. mode can be any of

\n
\n
'r'
\n
Read only mode.
\n
'w'
\n
Write only mode.
\n
\n

Note that it does not allow read/write files.

\n

A mode of 'r' returns a AU_read object, while a mode of 'w'\nor 'wb' returns a AU_write object.

\n
\n\n
\n
\nsunau.openfp(file, mode)
\n
A synonym for open(), maintained for backwards compatibility.
\n\n

The sunau module defines the following exception:

\n
\n
\nexception sunau.Error
\n
An error raised when something is impossible because of Sun AU specs or\nimplementation deficiency.
\n\n

The sunau module defines the following data items:

\n
\n
\nsunau.AUDIO_FILE_MAGIC
\n
An integer every valid Sun AU file begins with, stored in big-endian form. This\nis the string .snd interpreted as an integer.
\n\n
\n
\nsunau.AUDIO_FILE_ENCODING_MULAW_8
\n
\nsunau.AUDIO_FILE_ENCODING_LINEAR_8
\n
\nsunau.AUDIO_FILE_ENCODING_LINEAR_16
\n
\nsunau.AUDIO_FILE_ENCODING_LINEAR_24
\n
\nsunau.AUDIO_FILE_ENCODING_LINEAR_32
\n
\nsunau.AUDIO_FILE_ENCODING_ALAW_8
\n
Values of the encoding field from the AU header which are supported by this\nmodule.
\n\n
\n
\nsunau.AUDIO_FILE_ENCODING_FLOAT
\n
\nsunau.AUDIO_FILE_ENCODING_DOUBLE
\n
\nsunau.AUDIO_FILE_ENCODING_ADPCM_G721
\n
\nsunau.AUDIO_FILE_ENCODING_ADPCM_G722
\n
\nsunau.AUDIO_FILE_ENCODING_ADPCM_G723_3
\n
\nsunau.AUDIO_FILE_ENCODING_ADPCM_G723_5
\n
Additional known values of the encoding field from the AU header, but which are\nnot supported by this module.
\n\n
\n

21.4.1. AU_read Objects

\n

AU_read objects, as returned by open() above, have the following methods:

\n
\n
\nAU_read.close()
\n
Close the stream, and make the instance unusable. (This is called automatically\non deletion.)
\n\n
\n
\nAU_read.getnchannels()
\n
Returns number of audio channels (1 for mone, 2 for stereo).
\n\n
\n
\nAU_read.getsampwidth()
\n
Returns sample width in bytes.
\n\n
\n
\nAU_read.getframerate()
\n
Returns sampling frequency.
\n\n
\n
\nAU_read.getnframes()
\n
Returns number of audio frames.
\n\n
\n
\nAU_read.getcomptype()
\n
Returns compression type. Supported compression types are 'ULAW', 'ALAW'\nand 'NONE'.
\n\n
\n
\nAU_read.getcompname()
\n
Human-readable version of getcomptype(). The supported types have the\nrespective names 'CCITT G.711 u-law', 'CCITT G.711 A-law' and 'not\ncompressed'.
\n\n
\n
\nAU_read.getparams()
\n
Returns a tuple (nchannels, sampwidth, framerate, nframes, comptype,\ncompname), equivalent to output of the get*() methods.
\n\n
\n
\nAU_read.readframes(n)
\n
Reads and returns at most n frames of audio, as a string of bytes. The data\nwill be returned in linear format. If the original data is in u-LAW format, it\nwill be converted.
\n\n
\n
\nAU_read.rewind()
\n
Rewind the file pointer to the beginning of the audio stream.
\n\n

The following two methods define a term “position” which is compatible between\nthem, and is otherwise implementation dependent.

\n
\n
\nAU_read.setpos(pos)
\n
Set the file pointer to the specified position. Only values returned from\ntell() should be used for pos.
\n\n
\n
\nAU_read.tell()
\n
Return current file pointer position. Note that the returned value has nothing\nto do with the actual position in the file.
\n\n

The following two functions are defined for compatibility with the aifc,\nand don’t do anything interesting.

\n
\n
\nAU_read.getmarkers()
\n
Returns None.
\n\n
\n
\nAU_read.getmark(id)
\n
Raise an error.
\n\n
\n
\n

21.4.2. AU_write Objects

\n

AU_write objects, as returned by open() above, have the following methods:

\n
\n
\nAU_write.setnchannels(n)
\n
Set the number of channels.
\n\n
\n
\nAU_write.setsampwidth(n)
\n
Set the sample width (in bytes.)
\n\n
\n
\nAU_write.setframerate(n)
\n
Set the frame rate.
\n\n
\n
\nAU_write.setnframes(n)
\n
Set the number of frames. This can be later changed, when and if more frames\nare written.
\n\n
\n
\nAU_write.setcomptype(type, name)
\n
Set the compression type and description. Only 'NONE' and 'ULAW' are\nsupported on output.
\n\n
\n
\nAU_write.setparams(tuple)
\n
The tuple should be (nchannels, sampwidth, framerate, nframes, comptype,\ncompname), with values valid for the set*() methods. Set all\nparameters.
\n\n
\n
\nAU_write.tell()
\n
Return current position in the file, with the same disclaimer for the\nAU_read.tell() and AU_read.setpos() methods.
\n\n
\n
\nAU_write.writeframesraw(data)
\n
Write audio frames, without correcting nframes.
\n\n
\n
\nAU_write.writeframes(data)
\n
Write audio frames and make sure nframes is correct.
\n\n
\n
\nAU_write.close()
\n

Make sure nframes is correct, and close the file.

\n

This method is called upon deletion.

\n
\n\n

Note that it is invalid to set any parameters after calling writeframes()\nor writeframesraw().

\n
\n
", "searchableItems": [ { "name": "sunau.AU_read.close", "domId": "sunau_sunau.AU_read.close" }, { "name": "sunau.AU_read.getcompname", "domId": "sunau_sunau.AU_read.getcompname" }, { "name": "sunau.AU_read.getcomptype", "domId": "sunau_sunau.AU_read.getcomptype" }, { "name": "sunau.AU_read.getframerate", "domId": "sunau_sunau.AU_read.getframerate" }, { "name": "sunau.AU_read.getmark", "domId": "sunau_sunau.AU_read.getmark" }, { "name": "sunau.AU_read.getmarkers", "domId": "sunau_sunau.AU_read.getmarkers" }, { "name": "sunau.AU_read.getnchannels", "domId": "sunau_sunau.AU_read.getnchannels" }, { "name": "sunau.AU_read.getnframes", "domId": "sunau_sunau.AU_read.getnframes" }, { "name": "sunau.AU_read.getparams", "domId": "sunau_sunau.AU_read.getparams" }, { "name": "sunau.AU_read.getsampwidth", "domId": "sunau_sunau.AU_read.getsampwidth" }, { "name": "sunau.AU_read.readframes", "domId": "sunau_sunau.AU_read.readframes" }, { "name": "sunau.AU_read.rewind", "domId": "sunau_sunau.AU_read.rewind" }, { "name": "sunau.AU_read.setpos", "domId": "sunau_sunau.AU_read.setpos" }, { "name": "sunau.AU_read.tell", "domId": "sunau_sunau.AU_read.tell" }, { "name": "sunau.AU_write.close", "domId": "sunau_sunau.AU_write.close" }, { "name": "sunau.AU_write.setcomptype", "domId": "sunau_sunau.AU_write.setcomptype" }, { "name": "sunau.AU_write.setframerate", "domId": "sunau_sunau.AU_write.setframerate" }, { "name": "sunau.AU_write.setnchannels", "domId": "sunau_sunau.AU_write.setnchannels" }, { "name": "sunau.AU_write.setnframes", "domId": "sunau_sunau.AU_write.setnframes" }, { "name": "sunau.AU_write.setparams", "domId": "sunau_sunau.AU_write.setparams" }, { "name": "sunau.AU_write.setsampwidth", "domId": "sunau_sunau.AU_write.setsampwidth" }, { "name": "sunau.AU_write.tell", "domId": "sunau_sunau.AU_write.tell" }, { "name": "sunau.AU_write.writeframes", "domId": "sunau_sunau.AU_write.writeframes" }, { "name": "sunau.AU_write.writeframesraw", "domId": "sunau_sunau.AU_write.writeframesraw" }, { "name": "sunau.open", "domId": "sunau_sunau.open" }, { "name": "sunau.openfp", "domId": "sunau_sunau.openfp" } ] }, { "url": "http://docs.python.org/library/aifc.html", "title": "aifc", "html": "
\n

21.3. aifc — Read and write AIFF and AIFC files

\n

Source code: Lib/aifc.py

\n
\n

This module provides support for reading and writing AIFF and AIFF-C files.\nAIFF is Audio Interchange File Format, a format for storing digital audio\nsamples in a file. AIFF-C is a newer version of the format that includes the\nability to compress the audio data.

\n
\n

Note

\n

Some operations may only work under IRIX; these will raise ImportError\nwhen attempting to import the cl module, which is only available on\nIRIX.

\n
\n

Audio files have a number of parameters that describe the audio data. The\nsampling rate or frame rate is the number of times per second the sound is\nsampled. The number of channels indicate if the audio is mono, stereo, or\nquadro. Each frame consists of one sample per channel. The sample size is the\nsize in bytes of each sample. Thus a frame consists of\nnchannels**samplesize* bytes, and a second’s worth of audio consists of\nnchannels**samplesize***framerate* bytes.

\n

For example, CD quality audio has a sample size of two bytes (16 bits), uses two\nchannels (stereo) and has a frame rate of 44,100 frames/second. This gives a\nframe size of 4 bytes (2*2), and a second’s worth occupies 2*2*44100 bytes\n(176,400 bytes).

\n

Module aifc defines the following function:

\n
\n
\naifc.open(file[, mode])
\n
Open an AIFF or AIFF-C file and return an object instance with methods that are\ndescribed below. The argument file is either a string naming a file or a file\nobject. mode must be 'r' or 'rb' when the file must be opened for\nreading, or 'w' or 'wb' when the file must be opened for writing. If\nomitted, file.mode is used if it exists, otherwise 'rb' is used. When\nused for writing, the file object should be seekable, unless you know ahead of\ntime how many samples you are going to write in total and use\nwriteframesraw() and setnframes().
\n\n

Objects returned by open() when a file is opened for reading have the\nfollowing methods:

\n
\n
\naifc.getnchannels()
\n
Return the number of audio channels (1 for mono, 2 for stereo).
\n\n
\n
\naifc.getsampwidth()
\n
Return the size in bytes of individual samples.
\n\n
\n
\naifc.getframerate()
\n
Return the sampling rate (number of audio frames per second).
\n\n
\n
\naifc.getnframes()
\n
Return the number of audio frames in the file.
\n\n
\n
\naifc.getcomptype()
\n
Return a four-character string describing the type of compression used in the\naudio file. For AIFF files, the returned value is 'NONE'.
\n\n
\n
\naifc.getcompname()
\n
Return a human-readable description of the type of compression used in the audio\nfile. For AIFF files, the returned value is 'not compressed'.
\n\n
\n
\naifc.getparams()
\n
Return a tuple consisting of all of the above values in the above order.
\n\n
\n
\naifc.getmarkers()
\n
Return a list of markers in the audio file. A marker consists of a tuple of\nthree elements. The first is the mark ID (an integer), the second is the mark\nposition in frames from the beginning of the data (an integer), the third is the\nname of the mark (a string).
\n\n
\n
\naifc.getmark(id)
\n
Return the tuple as described in getmarkers() for the mark with the given\nid.
\n\n
\n
\naifc.readframes(nframes)
\n
Read and return the next nframes frames from the audio file. The returned\ndata is a string containing for each frame the uncompressed samples of all\nchannels.
\n\n
\n
\naifc.rewind()
\n
Rewind the read pointer. The next readframes() will start from the\nbeginning.
\n\n
\n
\naifc.setpos(pos)
\n
Seek to the specified frame number.
\n\n
\n
\naifc.tell()
\n
Return the current frame number.
\n\n
\n
\naifc.close()
\n
Close the AIFF file. After calling this method, the object can no longer be\nused.
\n\n

Objects returned by open() when a file is opened for writing have all the\nabove methods, except for readframes() and setpos(). In addition\nthe following methods exist. The get*() methods can only be called after\nthe corresponding set*() methods have been called. Before the first\nwriteframes() or writeframesraw(), all parameters except for the\nnumber of frames must be filled in.

\n
\n
\naifc.aiff()
\n
Create an AIFF file. The default is that an AIFF-C file is created, unless the\nname of the file ends in '.aiff' in which case the default is an AIFF file.
\n\n
\n
\naifc.aifc()
\n
Create an AIFF-C file. The default is that an AIFF-C file is created, unless\nthe name of the file ends in '.aiff' in which case the default is an AIFF\nfile.
\n\n
\n
\naifc.setnchannels(nchannels)
\n
Specify the number of channels in the audio file.
\n\n
\n
\naifc.setsampwidth(width)
\n
Specify the size in bytes of audio samples.
\n\n
\n
\naifc.setframerate(rate)
\n
Specify the sampling frequency in frames per second.
\n\n
\n
\naifc.setnframes(nframes)
\n
Specify the number of frames that are to be written to the audio file. If this\nparameter is not set, or not set correctly, the file needs to support seeking.
\n\n
\n
\naifc.setcomptype(type, name)
\n

Specify the compression type. If not specified, the audio data will not be\ncompressed. In AIFF files, compression is not possible. The name parameter\nshould be a human-readable description of the compression type, the type\nparameter should be a four-character string. Currently the following\ncompression types are supported: NONE, ULAW, ALAW, G722.

\n
\n\n
\n
\naifc.setparams(nchannels, sampwidth, framerate, comptype, compname)
\n
Set all the above parameters at once. The argument is a tuple consisting of the\nvarious parameters. This means that it is possible to use the result of a\ngetparams() call as argument to setparams().
\n\n
\n
\naifc.setmark(id, pos, name)
\n
Add a mark with the given id (larger than 0), and the given name at the given\nposition. This method can be called at any time before close().
\n\n
\n
\naifc.tell()
\n
Return the current write position in the output file. Useful in combination\nwith setmark().
\n\n
\n
\naifc.writeframes(data)
\n
Write data to the output file. This method can only be called after the audio\nfile parameters have been set.
\n\n
\n
\naifc.writeframesraw(data)
\n
Like writeframes(), except that the header of the audio file is not\nupdated.
\n\n
\n
\naifc.close()
\n
Close the AIFF file. The header of the file is updated to reflect the actual\nsize of the audio data. After calling this method, the object can no longer be\nused.
\n\n
", "searchableItems": [ { "name": "aifc.aifc.aifc", "domId": "aifc_aifc.aifc.aifc" }, { "name": "aifc.aifc.aiff", "domId": "aifc_aifc.aifc.aiff" }, { "name": "aifc.aifc.close", "domId": "aifc_aifc.aifc.close" }, { "name": "aifc.aifc.getcompname", "domId": "aifc_aifc.aifc.getcompname" }, { "name": "aifc.aifc.getcomptype", "domId": "aifc_aifc.aifc.getcomptype" }, { "name": "aifc.aifc.getframerate", "domId": "aifc_aifc.aifc.getframerate" }, { "name": "aifc.aifc.getmark", "domId": "aifc_aifc.aifc.getmark" }, { "name": "aifc.aifc.getmarkers", "domId": "aifc_aifc.aifc.getmarkers" }, { "name": "aifc.aifc.getnchannels", "domId": "aifc_aifc.aifc.getnchannels" }, { "name": "aifc.aifc.getnframes", "domId": "aifc_aifc.aifc.getnframes" }, { "name": "aifc.aifc.getparams", "domId": "aifc_aifc.aifc.getparams" }, { "name": "aifc.aifc.getsampwidth", "domId": "aifc_aifc.aifc.getsampwidth" }, { "name": "aifc.aifc.readframes", "domId": "aifc_aifc.aifc.readframes" }, { "name": "aifc.aifc.rewind", "domId": "aifc_aifc.aifc.rewind" }, { "name": "aifc.aifc.setcomptype", "domId": "aifc_aifc.aifc.setcomptype" }, { "name": "aifc.aifc.setframerate", "domId": "aifc_aifc.aifc.setframerate" }, { "name": "aifc.aifc.setmark", "domId": "aifc_aifc.aifc.setmark" }, { "name": "aifc.aifc.setnchannels", "domId": "aifc_aifc.aifc.setnchannels" }, { "name": "aifc.aifc.setnframes", "domId": "aifc_aifc.aifc.setnframes" }, { "name": "aifc.aifc.setparams", "domId": "aifc_aifc.aifc.setparams" }, { "name": "aifc.aifc.setpos", "domId": "aifc_aifc.aifc.setpos" }, { "name": "aifc.aifc.setsampwidth", "domId": "aifc_aifc.aifc.setsampwidth" }, { "name": "aifc.aifc.tell", "domId": "aifc_aifc.aifc.tell" }, { "name": "aifc.aifc.writeframes", "domId": "aifc_aifc.aifc.writeframes" }, { "name": "aifc.aifc.writeframesraw", "domId": "aifc_aifc.aifc.writeframesraw" }, { "name": "aifc.open", "domId": "aifc_aifc.open" } ] }, { "url": "http://docs.python.org/library/chunk.html", "title": "chunk", "html": "
\n

21.6. chunk — Read IFF chunked data

\n

This module provides an interface for reading files that use EA IFF 85 chunks.\n[1] This format is used in at least the Audio Interchange File Format\n(AIFF/AIFF-C) and the Real Media File Format (RMFF). The WAVE audio file format\nis closely related and can also be read using this module.

\n

A chunk has the following structure:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OffsetLengthContents
04Chunk ID
44Size of chunk in big-endian\nbyte order, not including the\nheader
8nData bytes, where n is the\nsize given in the preceding\nfield
8 + n0 or 1Pad byte needed if n is odd\nand chunk alignment is used
\n

The ID is a 4-byte string which identifies the type of chunk.

\n

The size field (a 32-bit value, encoded using big-endian byte order) gives the\nsize of the chunk data, not including the 8-byte header.

\n

Usually an IFF-type file consists of one or more chunks. The proposed usage of\nthe Chunk class defined here is to instantiate an instance at the start\nof each chunk and read from the instance until it reaches the end, after which a\nnew instance can be instantiated. At the end of the file, creating a new\ninstance will fail with a EOFError exception.

\n
\n
\nclass chunk.Chunk(file[, align, bigendian, inclheader])
\n

Class which represents a chunk. The file argument is expected to be a\nfile-like object. An instance of this class is specifically allowed. The\nonly method that is needed is read(). If the methods seek() and\ntell() are present and don’t raise an exception, they are also used.\nIf these methods are present and raise an exception, they are expected to not\nhave altered the object. If the optional argument align is true, chunks\nare assumed to be aligned on 2-byte boundaries. If align is false, no\nalignment is assumed. The default value is true. If the optional argument\nbigendian is false, the chunk size is assumed to be in little-endian order.\nThis is needed for WAVE audio files. The default value is true. If the\noptional argument inclheader is true, the size given in the chunk header\nincludes the size of the header. The default value is false.

\n

A Chunk object supports the following methods:

\n
\n
\ngetname()
\n
Returns the name (ID) of the chunk. This is the first 4 bytes of the\nchunk.
\n\n
\n
\ngetsize()
\n
Returns the size of the chunk.
\n\n
\n
\nclose()
\n
Close and skip to the end of the chunk. This does not close the\nunderlying file.
\n\n

The remaining methods will raise IOError if called after the\nclose() method has been called.

\n
\n
\nisatty()
\n
Returns False.
\n\n
\n
\nseek(pos[, whence])
\n
Set the chunk’s current position. The whence argument is optional and\ndefaults to 0 (absolute file positioning); other values are 1\n(seek relative to the current position) and 2 (seek relative to the\nfile’s end). There is no return value. If the underlying file does not\nallow seek, only forward seeks are allowed.
\n\n
\n
\ntell()
\n
Return the current position into the chunk.
\n\n
\n
\nread([size])
\n
Read at most size bytes from the chunk (less if the read hits the end of\nthe chunk before obtaining size bytes). If the size argument is\nnegative or omitted, read all data until the end of the chunk. The bytes\nare returned as a string object. An empty string is returned when the end\nof the chunk is encountered immediately.
\n\n
\n
\nskip()
\n
Skip to the end of the chunk. All further calls to read() for the\nchunk will return ''. If you are not interested in the contents of\nthe chunk, this method should be called so that the file points to the\nstart of the next chunk.
\n\n
\n\n

Footnotes

\n\n\n\n\n\n
[1]“EA IFF 85” Standard for Interchange Format Files, Jerry Morrison, Electronic\nArts, January 1985.
\n
", "searchableItems": [ { "name": "chunk.Chunk", "domId": "chunk_chunk.Chunk" }, { "name": "chunk.Chunk.close", "domId": "chunk_chunk.Chunk.close" }, { "name": "chunk.Chunk.getname", "domId": "chunk_chunk.Chunk.getname" }, { "name": "chunk.Chunk.getsize", "domId": "chunk_chunk.Chunk.getsize" }, { "name": "chunk.Chunk.isatty", "domId": "chunk_chunk.Chunk.isatty" }, { "name": "chunk.Chunk.read", "domId": "chunk_chunk.Chunk.read" }, { "name": "chunk.Chunk.seek", "domId": "chunk_chunk.Chunk.seek" }, { "name": "chunk.Chunk.skip", "domId": "chunk_chunk.Chunk.skip" }, { "name": "chunk.Chunk.tell", "domId": "chunk_chunk.Chunk.tell" } ] }, { "url": "http://docs.python.org/library/wave.html", "title": "wave", "html": "
\n

21.5. wave — Read and write WAV files

\n

Source code: Lib/wave.py

\n
\n

The wave module provides a convenient interface to the WAV sound format.\nIt does not support compression/decompression, but it does support mono/stereo.

\n

The wave module defines the following function and exception:

\n
\n
\nwave.open(file[, mode])
\n

If file is a string, open the file by that name, otherwise treat it as a\nseekable file-like object. mode can be any of

\n
\n
'r', 'rb'
\n
Read only mode.
\n
'w', 'wb'
\n
Write only mode.
\n
\n

Note that it does not allow read/write WAV files.

\n

A mode of 'r' or 'rb' returns a Wave_read object, while a\nmode of 'w' or 'wb' returns a Wave_write object. If\nmode is omitted and a file-like object is passed as file, file.mode\nis used as the default value for mode (the 'b' flag is still added if\nnecessary).

\n

If you pass in a file-like object, the wave object will not close it when its\nclose() method is called; it is the caller’s responsibility to close\nthe file object.

\n
\n\n
\n
\nwave.openfp(file, mode)
\n
A synonym for open(), maintained for backwards compatibility.
\n\n
\n
\nexception wave.Error
\n
An error raised when something is impossible because it violates the WAV\nspecification or hits an implementation deficiency.
\n\n
\n

21.5.1. Wave_read Objects

\n

Wave_read objects, as returned by open(), have the following methods:

\n
\n
\nWave_read.close()
\n
Close the stream if it was opened by wave, and make the instance\nunusable. This is called automatically on object collection.
\n\n
\n
\nWave_read.getnchannels()
\n
Returns number of audio channels (1 for mono, 2 for stereo).
\n\n
\n
\nWave_read.getsampwidth()
\n
Returns sample width in bytes.
\n\n
\n
\nWave_read.getframerate()
\n
Returns sampling frequency.
\n\n
\n
\nWave_read.getnframes()
\n
Returns number of audio frames.
\n\n
\n
\nWave_read.getcomptype()
\n
Returns compression type ('NONE' is the only supported type).
\n\n
\n
\nWave_read.getcompname()
\n
Human-readable version of getcomptype(). Usually 'not compressed'\nparallels 'NONE'.
\n\n
\n
\nWave_read.getparams()
\n
Returns a tuple (nchannels, sampwidth, framerate, nframes, comptype,\ncompname), equivalent to output of the get*() methods.
\n\n
\n
\nWave_read.readframes(n)
\n
Reads and returns at most n frames of audio, as a string of bytes.
\n\n
\n
\nWave_read.rewind()
\n
Rewind the file pointer to the beginning of the audio stream.
\n\n

The following two methods are defined for compatibility with the aifc\nmodule, and don’t do anything interesting.

\n
\n
\nWave_read.getmarkers()
\n
Returns None.
\n\n
\n
\nWave_read.getmark(id)
\n
Raise an error.
\n\n

The following two methods define a term “position” which is compatible between\nthem, and is otherwise implementation dependent.

\n
\n
\nWave_read.setpos(pos)
\n
Set the file pointer to the specified position.
\n\n
\n
\nWave_read.tell()
\n
Return current file pointer position.
\n\n
\n
\n

21.5.2. Wave_write Objects

\n

Wave_write objects, as returned by open(), have the following methods:

\n
\n
\nWave_write.close()
\n
Make sure nframes is correct, and close the file if it was opened by\nwave. This method is called upon object collection.
\n\n
\n
\nWave_write.setnchannels(n)
\n
Set the number of channels.
\n\n
\n
\nWave_write.setsampwidth(n)
\n
Set the sample width to n bytes.
\n\n
\n
\nWave_write.setframerate(n)
\n
Set the frame rate to n.
\n\n
\n
\nWave_write.setnframes(n)
\n
Set the number of frames to n. This will be changed later if more frames are\nwritten.
\n\n
\n
\nWave_write.setcomptype(type, name)
\n
Set the compression type and description. At the moment, only compression type\nNONE is supported, meaning no compression.
\n\n
\n
\nWave_write.setparams(tuple)
\n
The tuple should be (nchannels, sampwidth, framerate, nframes, comptype,\ncompname), with values valid for the set*() methods. Sets all\nparameters.
\n\n
\n
\nWave_write.tell()
\n
Return current position in the file, with the same disclaimer for the\nWave_read.tell() and Wave_read.setpos() methods.
\n\n
\n
\nWave_write.writeframesraw(data)
\n
Write audio frames, without correcting nframes.
\n\n
\n
\nWave_write.writeframes(data)
\n
Write audio frames and make sure nframes is correct.
\n\n

Note that it is invalid to set any parameters after calling writeframes()\nor writeframesraw(), and any attempt to do so will raise\nwave.Error.

\n
\n
", "searchableItems": [ { "name": "wave.open", "domId": "wave_wave.open" }, { "name": "wave.openfp", "domId": "wave_wave.openfp" }, { "name": "wave.Wave_read.close", "domId": "wave_wave.Wave_read.close" }, { "name": "wave.Wave_read.getcompname", "domId": "wave_wave.Wave_read.getcompname" }, { "name": "wave.Wave_read.getcomptype", "domId": "wave_wave.Wave_read.getcomptype" }, { "name": "wave.Wave_read.getframerate", "domId": "wave_wave.Wave_read.getframerate" }, { "name": "wave.Wave_read.getmark", "domId": "wave_wave.Wave_read.getmark" }, { "name": "wave.Wave_read.getmarkers", "domId": "wave_wave.Wave_read.getmarkers" }, { "name": "wave.Wave_read.getnchannels", "domId": "wave_wave.Wave_read.getnchannels" }, { "name": "wave.Wave_read.getnframes", "domId": "wave_wave.Wave_read.getnframes" }, { "name": "wave.Wave_read.getparams", "domId": "wave_wave.Wave_read.getparams" }, { "name": "wave.Wave_read.getsampwidth", "domId": "wave_wave.Wave_read.getsampwidth" }, { "name": "wave.Wave_read.readframes", "domId": "wave_wave.Wave_read.readframes" }, { "name": "wave.Wave_read.rewind", "domId": "wave_wave.Wave_read.rewind" }, { "name": "wave.Wave_read.setpos", "domId": "wave_wave.Wave_read.setpos" }, { "name": "wave.Wave_read.tell", "domId": "wave_wave.Wave_read.tell" }, { "name": "wave.Wave_write.close", "domId": "wave_wave.Wave_write.close" }, { "name": "wave.Wave_write.setcomptype", "domId": "wave_wave.Wave_write.setcomptype" }, { "name": "wave.Wave_write.setframerate", "domId": "wave_wave.Wave_write.setframerate" }, { "name": "wave.Wave_write.setnchannels", "domId": "wave_wave.Wave_write.setnchannels" }, { "name": "wave.Wave_write.setnframes", "domId": "wave_wave.Wave_write.setnframes" }, { "name": "wave.Wave_write.setparams", "domId": "wave_wave.Wave_write.setparams" }, { "name": "wave.Wave_write.setsampwidth", "domId": "wave_wave.Wave_write.setsampwidth" }, { "name": "wave.Wave_write.tell", "domId": "wave_wave.Wave_write.tell" }, { "name": "wave.Wave_write.writeframes", "domId": "wave_wave.Wave_write.writeframes" }, { "name": "wave.Wave_write.writeframesraw", "domId": "wave_wave.Wave_write.writeframesraw" } ] }, { "url": "http://docs.python.org/library/colorsys.html", "title": "colorsys", "html": "
\n

21.7. colorsys — Conversions between color systems

\n

Source code: Lib/colorsys.py

\n
\n

The colorsys module defines bidirectional conversions of color values\nbetween colors expressed in the RGB (Red Green Blue) color space used in\ncomputer monitors and three other coordinate systems: YIQ, HLS (Hue Lightness\nSaturation) and HSV (Hue Saturation Value). Coordinates in all of these color\nspaces are floating point values. In the YIQ space, the Y coordinate is between\n0 and 1, but the I and Q coordinates can be positive or negative. In all other\nspaces, the coordinates are all between 0 and 1.

\n
\n

See also

\n

More information about color spaces can be found at\nhttp://www.poynton.com/ColorFAQ.html and\nhttp://www.cambridgeincolour.com/tutorials/color-spaces.htm.

\n
\n

The colorsys module defines the following functions:

\n
\n
\ncolorsys.rgb_to_yiq(r, g, b)
\n
Convert the color from RGB coordinates to YIQ coordinates.
\n\n
\n
\ncolorsys.yiq_to_rgb(y, i, q)
\n
Convert the color from YIQ coordinates to RGB coordinates.
\n\n
\n
\ncolorsys.rgb_to_hls(r, g, b)
\n
Convert the color from RGB coordinates to HLS coordinates.
\n\n
\n
\ncolorsys.hls_to_rgb(h, l, s)
\n
Convert the color from HLS coordinates to RGB coordinates.
\n\n
\n
\ncolorsys.rgb_to_hsv(r, g, b)
\n
Convert the color from RGB coordinates to HSV coordinates.
\n\n
\n
\ncolorsys.hsv_to_rgb(h, s, v)
\n
Convert the color from HSV coordinates to RGB coordinates.
\n\n

Example:

\n
>>> import colorsys\n>>> colorsys.rgb_to_hsv(.3, .4, .2)\n(0.25, 0.5, 0.4)\n>>> colorsys.hsv_to_rgb(0.25, 0.5, 0.4)\n(0.3, 0.4, 0.2)\n
\n
\n
", "searchableItems": [ { "name": "colorsys.hls_to_rgb", "domId": "colorsys_colorsys.hls_to_rgb" }, { "name": "colorsys.hsv_to_rgb", "domId": "colorsys_colorsys.hsv_to_rgb" }, { "name": "colorsys.rgb_to_hls", "domId": "colorsys_colorsys.rgb_to_hls" }, { "name": "colorsys.rgb_to_hsv", "domId": "colorsys_colorsys.rgb_to_hsv" }, { "name": "colorsys.rgb_to_yiq", "domId": "colorsys_colorsys.rgb_to_yiq" }, { "name": "colorsys.yiq_to_rgb", "domId": "colorsys_colorsys.yiq_to_rgb" } ] }, { "url": "http://docs.python.org/library/imghdr.html", "title": "imghdr", "html": "
\n

21.8. imghdr — Determine the type of an image

\n

Source code: Lib/imghdr.py

\n
\n

The imghdr module determines the type of image contained in a file or\nbyte stream.

\n

The imghdr module defines the following function:

\n
\n
\nimghdr.what(filename[, h])
\n
Tests the image data contained in the file named by filename, and returns a\nstring describing the image type. If optional h is provided, the filename\nis ignored and h is assumed to contain the byte stream to test.
\n\n

The following image types are recognized, as listed below with the return value\nfrom what():

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueImage format
'rgb'SGI ImgLib Files
'gif'GIF 87a and 89a Files
'pbm'Portable Bitmap Files
'pgm'Portable Graymap Files
'ppm'Portable Pixmap Files
'tiff'TIFF Files
'rast'Sun Raster Files
'xbm'X Bitmap Files
'jpeg'JPEG data in JFIF or Exif formats
'bmp'BMP files
'png'Portable Network Graphics
\n

\nNew in version 2.5: Exif detection.

\n

You can extend the list of file types imghdr can recognize by appending\nto this variable:

\n
\n
\nimghdr.tests
\n

A list of functions performing the individual tests. Each function takes two\narguments: the byte-stream and an open file-like object. When what() is\ncalled with a byte-stream, the file-like object will be None.

\n

The test function should return a string describing the image type if the test\nsucceeded, or None if it failed.

\n
\n\n

Example:

\n
>>> import imghdr\n>>> imghdr.what('/tmp/bass.gif')\n'gif'\n
\n
\n
", "searchableItems": [ { "name": "imghdr.what", "domId": "imghdr_imghdr.what" } ] }, { "url": "http://docs.python.org/library/cookielib.html", "title": "cookielib", "html": "
\n

20.21. cookielib — Cookie handling for HTTP clients

\n
\n

Note

\n

The cookielib module has been renamed to http.cookiejar in\nPython 3.0. The 2to3 tool will automatically adapt imports when\nconverting your sources to 3.0.

\n
\n

\nNew in version 2.4.

\n

Source code: Lib/cookielib.py

\n
\n

The cookielib module defines classes for automatic handling of HTTP\ncookies. It is useful for accessing web sites that require small pieces of data\n– cookies – to be set on the client machine by an HTTP response from a\nweb server, and then returned to the server in later HTTP requests.

\n

Both the regular Netscape cookie protocol and the protocol defined by\nRFC 2965 are handled. RFC 2965 handling is switched off by default.\nRFC 2109 cookies are parsed as Netscape cookies and subsequently treated\neither as Netscape or RFC 2965 cookies according to the ‘policy’ in effect.\nNote that the great majority of cookies on the Internet are Netscape cookies.\ncookielib attempts to follow the de-facto Netscape cookie protocol (which\ndiffers substantially from that set out in the original Netscape specification),\nincluding taking note of the max-age and port cookie-attributes\nintroduced with RFC 2965.

\n
\n

Note

\n

The various named parameters found in Set-Cookie and\nSet-Cookie2 headers (eg. domain and expires) are\nconventionally referred to as attributes. To distinguish them from\nPython attributes, the documentation for this module uses the term\ncookie-attribute instead.

\n
\n

The module defines the following exception:

\n
\n
\nexception cookielib.LoadError
\n

Instances of FileCookieJar raise this exception on failure to load\ncookies from a file.

\n
\n

Note

\n

For backwards-compatibility with Python 2.4 (which raised an IOError),\nLoadError is a subclass of IOError.

\n
\n
\n\n

The following classes are provided:

\n
\n
\nclass cookielib.CookieJar(policy=None)
\n

policy is an object implementing the CookiePolicy interface.

\n

The CookieJar class stores HTTP cookies. It extracts cookies from HTTP\nrequests, and returns them in HTTP responses. CookieJar instances\nautomatically expire contained cookies when necessary. Subclasses are also\nresponsible for storing and retrieving cookies from a file or database.

\n
\n\n
\n
\nclass cookielib.FileCookieJar(filename, delayload=None, policy=None)
\n

policy is an object implementing the CookiePolicy interface. For the\nother arguments, see the documentation for the corresponding attributes.

\n

A CookieJar which can load cookies from, and perhaps save cookies to, a\nfile on disk. Cookies are NOT loaded from the named file until either the\nload() or revert() method is called. Subclasses of this class are\ndocumented in section FileCookieJar subclasses and co-operation with web browsers.

\n
\n\n
\n
\nclass cookielib.CookiePolicy
\n
This class is responsible for deciding whether each cookie should be accepted\nfrom / returned to the server.
\n\n
\n
\nclass cookielib.DefaultCookiePolicy(blocked_domains=None, allowed_domains=None, netscape=True, rfc2965=False, rfc2109_as_netscape=None, hide_cookie2=False, strict_domain=False, strict_rfc2965_unverifiable=True, strict_ns_unverifiable=False, strict_ns_domain=DefaultCookiePolicy.DomainLiberal, strict_ns_set_initial_dollar=False, strict_ns_set_path=False)
\n

Constructor arguments should be passed as keyword arguments only.\nblocked_domains is a sequence of domain names that we never accept cookies\nfrom, nor return cookies to. allowed_domains if not None, this is a\nsequence of the only domains for which we accept and return cookies. For all\nother arguments, see the documentation for CookiePolicy and\nDefaultCookiePolicy objects.

\n

DefaultCookiePolicy implements the standard accept / reject rules for\nNetscape and RFC 2965 cookies. By default, RFC 2109 cookies (ie. cookies\nreceived in a Set-Cookie header with a version cookie-attribute of\n1) are treated according to the RFC 2965 rules. However, if RFC 2965 handling\nis turned off or rfc2109_as_netscape is True, RFC 2109 cookies are\n‘downgraded’ by the CookieJar instance to Netscape cookies, by\nsetting the version attribute of the Cookie instance to 0.\nDefaultCookiePolicy also provides some parameters to allow some\nfine-tuning of policy.

\n
\n\n
\n
\nclass cookielib.Cookie
\n
This class represents Netscape, RFC 2109 and RFC 2965 cookies. It is not\nexpected that users of cookielib construct their own Cookie\ninstances. Instead, if necessary, call make_cookies() on a\nCookieJar instance.
\n\n
\n

See also

\n
\n
Module urllib2
\n
URL opening with automatic cookie handling.
\n
Module Cookie
\n
HTTP cookie classes, principally useful for server-side code. The\ncookielib and Cookie modules do not depend on each other.
\n
http://wp.netscape.com/newsref/std/cookie_spec.html
\n
The specification of the original Netscape cookie protocol. Though this is\nstill the dominant protocol, the ‘Netscape cookie protocol’ implemented by all\nthe major browsers (and cookielib) only bears a passing resemblance to\nthe one sketched out in cookie_spec.html.
\n
RFC 2109 - HTTP State Management Mechanism
\n
Obsoleted by RFC 2965. Uses Set-Cookie with version=1.
\n
RFC 2965 - HTTP State Management Mechanism
\n
The Netscape protocol with the bugs fixed. Uses Set-Cookie2 in\nplace of Set-Cookie. Not widely used.
\n
http://kristol.org/cookie/errata.html
\n
Unfinished errata to RFC 2965.
\n
\n

RFC 2964 - Use of HTTP State Management

\n
\n
\n

20.21.1. CookieJar and FileCookieJar Objects

\n

CookieJar objects support the iterator protocol for iterating over\ncontained Cookie objects.

\n

CookieJar has the following methods:

\n
\n\n

Add correct Cookie header to request.

\n

If policy allows (ie. the rfc2965 and hide_cookie2 attributes of\nthe CookieJar‘s CookiePolicy instance are true and false\nrespectively), the Cookie2 header is also added when appropriate.

\n

The request object (usually a urllib2.Request instance) must support\nthe methods get_full_url(), get_host(), get_type(),\nunverifiable(), get_origin_req_host(), has_header(),\nget_header(), header_items(), and add_unredirected_header(),as\ndocumented by urllib2.

\n
\n\n
\n
\nCookieJar.extract_cookies(response, request)
\n

Extract cookies from HTTP response and store them in the CookieJar,\nwhere allowed by policy.

\n

The CookieJar will look for allowable Set-Cookie and\nSet-Cookie2 headers in the response argument, and store cookies\nas appropriate (subject to the CookiePolicy.set_ok() method’s approval).

\n

The response object (usually the result of a call to urllib2.urlopen(),\nor similar) should support an info() method, which returns an object with\na getallmatchingheaders() method (usually a mimetools.Message\ninstance).

\n

The request object (usually a urllib2.Request instance) must support\nthe methods get_full_url(), get_host(), unverifiable(), and\nget_origin_req_host(), as documented by urllib2. The request is\nused to set default values for cookie-attributes as well as for checking that\nthe cookie is allowed to be set.

\n
\n\n
\n
\nCookieJar.set_policy(policy)
\n
Set the CookiePolicy instance to be used.
\n\n
\n
\nCookieJar.make_cookies(response, request)
\n

Return sequence of Cookie objects extracted from response object.

\n

See the documentation for extract_cookies() for the interfaces required of\nthe response and request arguments.

\n
\n\n
\n\n
Set a Cookie if policy says it’s OK to do so.
\n\n
\n
\nCookieJar.set_cookie(cookie)
\n
Set a Cookie, without checking with policy to see whether or not it\nshould be set.
\n\n
\n
\nCookieJar.clear([domain[, path[, name]]])
\n

Clear some cookies.

\n

If invoked without arguments, clear all cookies. If given a single argument,\nonly cookies belonging to that domain will be removed. If given two arguments,\ncookies belonging to the specified domain and URL path are removed. If\ngiven three arguments, then the cookie with the specified domain, path and\nname is removed.

\n

Raises KeyError if no matching cookie exists.

\n
\n\n
\n
\nCookieJar.clear_session_cookies()
\n

Discard all session cookies.

\n

Discards all contained cookies that have a true discard attribute\n(usually because they had either no max-age or expires cookie-attribute,\nor an explicit discard cookie-attribute). For interactive browsers, the end\nof a session usually corresponds to closing the browser window.

\n

Note that the save() method won’t save session cookies anyway, unless you\nask otherwise by passing a true ignore_discard argument.

\n
\n\n

FileCookieJar implements the following additional methods:

\n
\n
\nFileCookieJar.save(filename=None, ignore_discard=False, ignore_expires=False)
\n

Save cookies to a file.

\n

This base class raises NotImplementedError. Subclasses may leave this\nmethod unimplemented.

\n

filename is the name of file in which to save cookies. If filename is not\nspecified, self.filename is used (whose default is the value passed to\nthe constructor, if any); if self.filename is None,\nValueError is raised.

\n

ignore_discard: save even cookies set to be discarded. ignore_expires: save\neven cookies that have expired

\n

The file is overwritten if it already exists, thus wiping all the cookies it\ncontains. Saved cookies can be restored later using the load() or\nrevert() methods.

\n
\n\n
\n
\nFileCookieJar.load(filename=None, ignore_discard=False, ignore_expires=False)
\n

Load cookies from a file.

\n

Old cookies are kept unless overwritten by newly loaded ones.

\n

Arguments are as for save().

\n

The named file must be in the format understood by the class, or\nLoadError will be raised. Also, IOError may be raised, for\nexample if the file does not exist.

\n
\n

Note

\n

For backwards-compatibility with Python 2.4 (which raised an IOError),\nLoadError is a subclass of IOError.

\n
\n
\n\n
\n
\nFileCookieJar.revert(filename=None, ignore_discard=False, ignore_expires=False)
\n

Clear all cookies and reload cookies from a saved file.

\n

revert() can raise the same exceptions as load(). If there is a\nfailure, the object’s state will not be altered.

\n
\n\n

FileCookieJar instances have the following public attributes:

\n
\n
\nFileCookieJar.filename
\n
Filename of default file in which to keep cookies. This attribute may be\nassigned to.
\n\n
\n
\nFileCookieJar.delayload
\n
If true, load cookies lazily from disk. This attribute should not be assigned\nto. This is only a hint, since this only affects performance, not behaviour\n(unless the cookies on disk are changing). A CookieJar object may\nignore it. None of the FileCookieJar classes included in the standard\nlibrary lazily loads cookies.
\n\n
\n
\n

20.21.2. FileCookieJar subclasses and co-operation with web browsers

\n

The following CookieJar subclasses are provided for reading and\nwriting .

\n
\n
\nclass cookielib.MozillaCookieJar(filename, delayload=None, policy=None)
\n

A FileCookieJar that can load from and save cookies to disk in the\nMozilla cookies.txt file format (which is also used by the Lynx and Netscape\nbrowsers).

\n
\n

Note

\n

Version 3 of the Firefox web browser no longer writes cookies in the\ncookies.txt file format.

\n
\n
\n

Note

\n

This loses information about RFC 2965 cookies, and also about newer or\nnon-standard cookie-attributes such as port.

\n
\n
\n

Warning

\n

Back up your cookies before saving if you have cookies whose loss / corruption\nwould be inconvenient (there are some subtleties which may lead to slight\nchanges in the file over a load / save round-trip).

\n
\n

Also note that cookies saved while Mozilla is running will get clobbered by\nMozilla.

\n
\n\n
\n
\nclass cookielib.LWPCookieJar(filename, delayload=None, policy=None)
\n
A FileCookieJar that can load from and save cookies to disk in format\ncompatible with the libwww-perl library’s Set-Cookie3 file format. This is\nconvenient if you want to store cookies in a human-readable file.
\n\n
\n
\n

20.21.3. CookiePolicy Objects

\n

Objects implementing the CookiePolicy interface have the following\nmethods:

\n
\n
\nCookiePolicy.set_ok(cookie, request)
\n

Return boolean value indicating whether cookie should be accepted from server.

\n

cookie is a cookielib.Cookie instance. request is an object\nimplementing the interface defined by the documentation for\nCookieJar.extract_cookies().

\n
\n\n
\n
\nCookiePolicy.return_ok(cookie, request)
\n

Return boolean value indicating whether cookie should be returned to server.

\n

cookie is a cookielib.Cookie instance. request is an object\nimplementing the interface defined by the documentation for\nCookieJar.add_cookie_header().

\n
\n\n
\n
\nCookiePolicy.domain_return_ok(domain, request)
\n

Return false if cookies should not be returned, given cookie domain.

\n

This method is an optimization. It removes the need for checking every cookie\nwith a particular domain (which might involve reading many files). Returning\ntrue from domain_return_ok() and path_return_ok() leaves all the\nwork to return_ok().

\n

If domain_return_ok() returns true for the cookie domain,\npath_return_ok() is called for the cookie path. Otherwise,\npath_return_ok() and return_ok() are never called for that cookie\ndomain. If path_return_ok() returns true, return_ok() is called\nwith the Cookie object itself for a full check. Otherwise,\nreturn_ok() is never called for that cookie path.

\n

Note that domain_return_ok() is called for every cookie domain, not just\nfor the request domain. For example, the function might be called with both\n".example.com" and "www.example.com" if the request domain is\n"www.example.com". The same goes for path_return_ok().

\n

The request argument is as documented for return_ok().

\n
\n\n
\n
\nCookiePolicy.path_return_ok(path, request)
\n

Return false if cookies should not be returned, given cookie path.

\n

See the documentation for domain_return_ok().

\n
\n\n

In addition to implementing the methods above, implementations of the\nCookiePolicy interface must also supply the following attributes,\nindicating which protocols should be used, and how. All of these attributes may\nbe assigned to.

\n
\n
\nCookiePolicy.netscape
\n
Implement Netscape protocol.
\n\n
\n
\nCookiePolicy.rfc2965
\n
Implement RFC 2965 protocol.
\n\n
\n
\nCookiePolicy.hide_cookie2
\n
Don’t add Cookie2 header to requests (the presence of this header\nindicates to the server that we understand RFC 2965 cookies).
\n\n

The most useful way to define a CookiePolicy class is by subclassing\nfrom DefaultCookiePolicy and overriding some or all of the methods\nabove. CookiePolicy itself may be used as a ‘null policy’ to allow\nsetting and receiving any and all cookies (this is unlikely to be useful).

\n
\n
\n

20.21.4. DefaultCookiePolicy Objects

\n

Implements the standard rules for accepting and returning cookies.

\n

Both RFC 2965 and Netscape cookies are covered. RFC 2965 handling is switched\noff by default.

\n

The easiest way to provide your own policy is to override this class and call\nits methods in your overridden implementations before adding your own additional\nchecks:

\n
import cookielib\nclass MyCookiePolicy(cookielib.DefaultCookiePolicy):\n    def set_ok(self, cookie, request):\n        if not cookielib.DefaultCookiePolicy.set_ok(self, cookie, request):\n            return False\n        if i_dont_want_to_store_this_cookie(cookie):\n            return False\n        return True\n
\n
\n

In addition to the features required to implement the CookiePolicy\ninterface, this class allows you to block and allow domains from setting and\nreceiving cookies. There are also some strictness switches that allow you to\ntighten up the rather loose Netscape protocol rules a little bit (at the cost of\nblocking some benign cookies).

\n

A domain blacklist and whitelist is provided (both off by default). Only domains\nnot in the blacklist and present in the whitelist (if the whitelist is active)\nparticipate in cookie setting and returning. Use the blocked_domains\nconstructor argument, and blocked_domains() and\nset_blocked_domains() methods (and the corresponding argument and methods\nfor allowed_domains). If you set a whitelist, you can turn it off again by\nsetting it to None.

\n

Domains in block or allow lists that do not start with a dot must equal the\ncookie domain to be matched. For example, "example.com" matches a blacklist\nentry of "example.com", but "www.example.com" does not. Domains that do\nstart with a dot are matched by more specific domains too. For example, both\n"www.example.com" and "www.coyote.example.com" match ".example.com"\n(but "example.com" itself does not). IP addresses are an exception, and\nmust match exactly. For example, if blocked_domains contains "192.168.1.2"\nand ".168.1.2", 192.168.1.2 is blocked, but 193.168.1.2 is not.

\n

DefaultCookiePolicy implements the following additional methods:

\n
\n
\nDefaultCookiePolicy.blocked_domains()
\n
Return the sequence of blocked domains (as a tuple).
\n\n
\n
\nDefaultCookiePolicy.set_blocked_domains(blocked_domains)
\n
Set the sequence of blocked domains.
\n\n
\n
\nDefaultCookiePolicy.is_blocked(domain)
\n
Return whether domain is on the blacklist for setting or receiving cookies.
\n\n
\n
\nDefaultCookiePolicy.allowed_domains()
\n
Return None, or the sequence of allowed domains (as a tuple).
\n\n
\n
\nDefaultCookiePolicy.set_allowed_domains(allowed_domains)
\n
Set the sequence of allowed domains, or None.
\n\n
\n
\nDefaultCookiePolicy.is_not_allowed(domain)
\n
Return whether domain is not on the whitelist for setting or receiving\ncookies.
\n\n

DefaultCookiePolicy instances have the following attributes, which are\nall initialised from the constructor arguments of the same name, and which may\nall be assigned to.

\n
\n
\nDefaultCookiePolicy.rfc2109_as_netscape
\n

If true, request that the CookieJar instance downgrade RFC 2109 cookies\n(ie. cookies received in a Set-Cookie header with a version\ncookie-attribute of 1) to Netscape cookies by setting the version attribute of\nthe Cookie instance to 0. The default value is None, in which\ncase RFC 2109 cookies are downgraded if and only if RFC 2965 handling is turned\noff. Therefore, RFC 2109 cookies are downgraded by default.

\n

\nNew in version 2.5.

\n
\n\n

General strictness switches:

\n
\n
\nDefaultCookiePolicy.strict_domain
\n
Don’t allow sites to set two-component domains with country-code top-level\ndomains like .co.uk, .gov.uk, .co.nz.etc. This is far from perfect\nand isn’t guaranteed to work!
\n\n

RFC 2965 protocol strictness switches:

\n
\n
\nDefaultCookiePolicy.strict_rfc2965_unverifiable
\n
Follow RFC 2965 rules on unverifiable transactions (usually, an unverifiable\ntransaction is one resulting from a redirect or a request for an image hosted on\nanother site). If this is false, cookies are never blocked on the basis of\nverifiability
\n\n

Netscape protocol strictness switches:

\n
\n
\nDefaultCookiePolicy.strict_ns_unverifiable
\n
apply RFC 2965 rules on unverifiable transactions even to Netscape cookies
\n\n
\n
\nDefaultCookiePolicy.strict_ns_domain
\n
Flags indicating how strict to be with domain-matching rules for Netscape\ncookies. See below for acceptable values.
\n\n
\n
\nDefaultCookiePolicy.strict_ns_set_initial_dollar
\n
Ignore cookies in Set-Cookie: headers that have names starting with '$'.
\n\n
\n
\nDefaultCookiePolicy.strict_ns_set_path
\n
Don’t allow setting cookies whose path doesn’t path-match request URI.
\n\n

strict_ns_domain is a collection of flags. Its value is constructed by\nor-ing together (for example, DomainStrictNoDots|DomainStrictNonDomain means\nboth flags are set).

\n
\n
\nDefaultCookiePolicy.DomainStrictNoDots
\n
When setting cookies, the ‘host prefix’ must not contain a dot (eg.\nwww.foo.bar.com can’t set a cookie for .bar.com, because www.foo\ncontains a dot).
\n\n
\n
\nDefaultCookiePolicy.DomainStrictNonDomain
\n
Cookies that did not explicitly specify a domain cookie-attribute can only\nbe returned to a domain equal to the domain that set the cookie (eg.\nspam.example.com won’t be returned cookies from example.com that had no\ndomain cookie-attribute).
\n\n
\n
\nDefaultCookiePolicy.DomainRFC2965Match
\n
When setting cookies, require a full RFC 2965 domain-match.
\n\n

The following attributes are provided for convenience, and are the most useful\ncombinations of the above flags:

\n
\n
\nDefaultCookiePolicy.DomainLiberal
\n
Equivalent to 0 (ie. all of the above Netscape domain strictness flags switched\noff).
\n\n
\n
\nDefaultCookiePolicy.DomainStrict
\n
Equivalent to DomainStrictNoDots|DomainStrictNonDomain.
\n\n
\n
\n

20.21.5. Cookie Objects

\n

Cookie instances have Python attributes roughly corresponding to the\nstandard cookie-attributes specified in the various cookie standards. The\ncorrespondence is not one-to-one, because there are complicated rules for\nassigning default values, because the max-age and expires\ncookie-attributes contain equivalent information, and because RFC 2109 cookies\nmay be ‘downgraded’ by cookielib from version 1 to version 0 (Netscape)\ncookies.

\n

Assignment to these attributes should not be necessary other than in rare\ncircumstances in a CookiePolicy method. The class does not enforce\ninternal consistency, so you should know what you’re doing if you do that.

\n
\n
\nCookie.version
\n
Integer or None. Netscape cookies have version 0. RFC 2965 and\nRFC 2109 cookies have a version cookie-attribute of 1. However, note that\ncookielib may ‘downgrade’ RFC 2109 cookies to Netscape cookies, in which\ncase version is 0.
\n\n
\n
\nCookie.name
\n
Cookie name (a string).
\n\n
\n
\nCookie.value
\n
Cookie value (a string), or None.
\n\n
\n
\nCookie.port
\n
String representing a port or a set of ports (eg. ‘80’, or ‘80,8080’), or\nNone.
\n\n
\n
\nCookie.path
\n
Cookie path (a string, eg. '/acme/rocket_launchers').
\n\n
\n
\nCookie.secure
\n
True if cookie should only be returned over a secure connection.
\n\n
\n
\nCookie.expires
\n
Integer expiry date in seconds since epoch, or None. See also the\nis_expired() method.
\n\n
\n
\nCookie.discard
\n
True if this is a session cookie.
\n\n
\n
\nCookie.comment
\n
String comment from the server explaining the function of this cookie, or\nNone.
\n\n
\n
\nCookie.comment_url
\n
URL linking to a comment from the server explaining the function of this cookie,\nor None.
\n\n
\n
\nCookie.rfc2109
\n

True if this cookie was received as an RFC 2109 cookie (ie. the cookie\narrived in a Set-Cookie header, and the value of the Version\ncookie-attribute in that header was 1). This attribute is provided because\ncookielib may ‘downgrade’ RFC 2109 cookies to Netscape cookies, in\nwhich case version is 0.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nCookie.port_specified
\n
True if a port or set of ports was explicitly specified by the server (in the\nSet-Cookie / Set-Cookie2 header).
\n\n
\n
\nCookie.domain_specified
\n
True if a domain was explicitly specified by the server.
\n\n
\n
\nCookie.domain_initial_dot
\n
True if the domain explicitly specified by the server began with a dot\n('.').
\n\n

Cookies may have additional non-standard cookie-attributes. These may be\naccessed using the following methods:

\n
\n
\nCookie.has_nonstandard_attr(name)
\n
Return true if cookie has the named cookie-attribute.
\n\n
\n
\nCookie.get_nonstandard_attr(name, default=None)
\n
If cookie has the named cookie-attribute, return its value. Otherwise, return\ndefault.
\n\n
\n
\nCookie.set_nonstandard_attr(name, value)
\n
Set the value of the named cookie-attribute.
\n\n

The Cookie class also defines the following method:

\n
\n
\nCookie.is_expired([now=None])
\n
True if cookie has passed the time at which the server requested it should\nexpire. If now is given (in seconds since the epoch), return whether the\ncookie has expired at the specified time.
\n\n
\n
\n

20.21.6. Examples

\n

The first example shows the most common usage of cookielib:

\n
import cookielib, urllib2\ncj = cookielib.CookieJar()\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\nr = opener.open("http://example.com/")\n
\n
\n

This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx\ncookies (assumes Unix/Netscape convention for location of the cookies file):

\n
import os, cookielib, urllib2\ncj = cookielib.MozillaCookieJar()\ncj.load(os.path.join(os.path.expanduser("~"), ".netscape", "cookies.txt"))\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\nr = opener.open("http://example.com/")\n
\n
\n

The next example illustrates the use of DefaultCookiePolicy. Turn on\nRFC 2965 cookies, be more strict about domains when setting and returning\nNetscape cookies, and block some domains from setting cookies or having them\nreturned:

\n
import urllib2\nfrom cookielib import CookieJar, DefaultCookiePolicy\npolicy = DefaultCookiePolicy(\n    rfc2965=True, strict_ns_domain=DefaultCookiePolicy.DomainStrict,\n    blocked_domains=["ads.net", ".ads.net"])\ncj = CookieJar(policy)\nopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))\nr = opener.open("http://example.com/")\n
\n
\n
\n
", "searchableItems": [ { "name": "cookielib.Cookie", "domId": "cookielib_cookielib.Cookie" }, { "name": "cookielib.Cookie.get_nonstandard_attr", "domId": "cookielib_cookielib.Cookie.get_nonstandard_attr" }, { "name": "cookielib.Cookie.has_nonstandard_attr", "domId": "cookielib_cookielib.Cookie.has_nonstandard_attr" }, { "name": "cookielib.Cookie.is_expired", "domId": "cookielib_cookielib.Cookie.is_expired" }, { "name": "cookielib.Cookie.set_nonstandard_attr", "domId": "cookielib_cookielib.Cookie.set_nonstandard_attr" }, { "name": "cookielib.CookieJar", "domId": "cookielib_cookielib.CookieJar" }, { "name": "cookielib.CookieJar.add_cookie_header", "domId": "cookielib_cookielib.CookieJar.add_cookie_header" }, { "name": "cookielib.CookieJar.clear", "domId": "cookielib_cookielib.CookieJar.clear" }, { "name": "cookielib.CookieJar.clear_session_cookies", "domId": "cookielib_cookielib.CookieJar.clear_session_cookies" }, { "name": "cookielib.CookieJar.extract_cookies", "domId": "cookielib_cookielib.CookieJar.extract_cookies" }, { "name": "cookielib.CookieJar.make_cookies", "domId": "cookielib_cookielib.CookieJar.make_cookies" }, { "name": "cookielib.CookieJar.set_cookie", "domId": "cookielib_cookielib.CookieJar.set_cookie" }, { "name": "cookielib.CookieJar.set_cookie_if_ok", "domId": "cookielib_cookielib.CookieJar.set_cookie_if_ok" }, { "name": "cookielib.CookieJar.set_policy", "domId": "cookielib_cookielib.CookieJar.set_policy" }, { "name": "cookielib.CookiePolicy", "domId": "cookielib_cookielib.CookiePolicy" }, { "name": "cookielib.CookiePolicy.domain_return_ok", "domId": "cookielib_cookielib.CookiePolicy.domain_return_ok" }, { "name": "cookielib.CookiePolicy.path_return_ok", "domId": "cookielib_cookielib.CookiePolicy.path_return_ok" }, { "name": "cookielib.CookiePolicy.return_ok", "domId": "cookielib_cookielib.CookiePolicy.return_ok" }, { "name": "cookielib.CookiePolicy.set_ok", "domId": "cookielib_cookielib.CookiePolicy.set_ok" }, { "name": "cookielib.DefaultCookiePolicy", "domId": "cookielib_cookielib.DefaultCookiePolicy" }, { "name": "cookielib.DefaultCookiePolicy.allowed_domains", "domId": "cookielib_cookielib.DefaultCookiePolicy.allowed_domains" }, { "name": "cookielib.DefaultCookiePolicy.blocked_domains", "domId": "cookielib_cookielib.DefaultCookiePolicy.blocked_domains" }, { "name": "cookielib.DefaultCookiePolicy.is_blocked", "domId": "cookielib_cookielib.DefaultCookiePolicy.is_blocked" }, { "name": "cookielib.DefaultCookiePolicy.is_not_allowed", "domId": "cookielib_cookielib.DefaultCookiePolicy.is_not_allowed" }, { "name": "cookielib.DefaultCookiePolicy.set_allowed_domains", "domId": "cookielib_cookielib.DefaultCookiePolicy.set_allowed_domains" }, { "name": "cookielib.DefaultCookiePolicy.set_blocked_domains", "domId": "cookielib_cookielib.DefaultCookiePolicy.set_blocked_domains" }, { "name": "cookielib.FileCookieJar", "domId": "cookielib_cookielib.FileCookieJar" }, { "name": "cookielib.FileCookieJar.load", "domId": "cookielib_cookielib.FileCookieJar.load" }, { "name": "cookielib.FileCookieJar.revert", "domId": "cookielib_cookielib.FileCookieJar.revert" }, { "name": "cookielib.FileCookieJar.save", "domId": "cookielib_cookielib.FileCookieJar.save" }, { "name": "cookielib.LWPCookieJar", "domId": "cookielib_cookielib.LWPCookieJar" }, { "name": "cookielib.MozillaCookieJar", "domId": "cookielib_cookielib.MozillaCookieJar" } ] }, { "url": "http://docs.python.org/library/sndhdr.html", "title": "sndhdr", "html": "
\n

21.9. sndhdr — Determine type of sound file

\n

Source code: Lib/sndhdr.py

\n
\n

The sndhdr provides utility functions which attempt to determine the type\nof sound data which is in a file. When these functions are able to determine\nwhat type of sound data is stored in a file, they return a tuple (type,\nsampling_rate, channels, frames, bits_per_sample). The value for type\nindicates the data type and will be one of the strings 'aifc', 'aiff',\n'au', 'hcom', 'sndr', 'sndt', 'voc', 'wav', '8svx',\n'sb', 'ub', or 'ul'. The sampling_rate will be either the actual\nvalue or 0 if unknown or difficult to decode. Similarly, channels will be\neither the number of channels or 0 if it cannot be determined or if the\nvalue is difficult to decode. The value for frames will be either the number\nof frames or -1. The last item in the tuple, bits_per_sample, will either\nbe the sample size in bits or 'A' for A-LAW or 'U' for u-LAW.

\n
\n
\nsndhdr.what(filename)
\n
Determines the type of sound data stored in the file filename using\nwhathdr(). If it succeeds, returns a tuple as described above, otherwise\nNone is returned.
\n\n
\n
\nsndhdr.whathdr(filename)
\n
Determines the type of sound data stored in a file based on the file header.\nThe name of the file is given by filename. This function returns a tuple as\ndescribed above on success, or None.
\n\n
", "searchableItems": [ { "name": "sndhdr.what", "domId": "sndhdr_sndhdr.what" }, { "name": "sndhdr.whathdr", "domId": "sndhdr_sndhdr.whathdr" } ] }, { "url": "http://docs.python.org/library/gettext.html", "title": "gettext", "html": "
\n

22.1. gettext — Multilingual internationalization services

\n

Source code: Lib/gettext.py

\n
\n

The gettext module provides internationalization (I18N) and localization\n(L10N) services for your Python modules and applications. It supports both the\nGNU gettext message catalog API and a higher level, class-based API that may\nbe more appropriate for Python files. The interface described below allows you\nto write your module and application messages in one natural language, and\nprovide a catalog of translated messages for running under different natural\nlanguages.

\n

Some hints on localizing your Python modules and applications are also given.

\n
\n

22.1.1. GNU gettext API

\n

The gettext module defines the following API, which is very similar to\nthe GNU gettext API. If you use this API you will affect the\ntranslation of your entire application globally. Often this is what you want if\nyour application is monolingual, with the choice of language dependent on the\nlocale of your user. If you are localizing a Python module, or if your\napplication needs to switch languages on the fly, you probably want to use the\nclass-based API instead.

\n
\n
\ngettext.bindtextdomain(domain[, localedir])
\n

Bind the domain to the locale directory localedir. More concretely,\ngettext will look for binary .mo files for the given domain using\nthe path (on Unix): localedir/language/LC_MESSAGES/domain.mo, where\nlanguages is searched for in the environment variables LANGUAGE,\nLC_ALL, LC_MESSAGES, and LANG respectively.

\n

If localedir is omitted or None, then the current binding for domain is\nreturned. [1]

\n
\n\n
\n
\ngettext.bind_textdomain_codeset(domain[, codeset])
\n

Bind the domain to codeset, changing the encoding of strings returned by the\ngettext() family of functions. If codeset is omitted, then the current\nbinding is returned.

\n

\nNew in version 2.4.

\n
\n\n
\n
\ngettext.textdomain([domain])
\n
Change or query the current global domain. If domain is None, then the\ncurrent global domain is returned, otherwise the global domain is set to\ndomain, which is returned.
\n\n
\n
\ngettext.gettext(message)
\n
Return the localized translation of message, based on the current global\ndomain, language, and locale directory. This function is usually aliased as\n_() in the local namespace (see examples below).
\n\n
\n
\ngettext.lgettext(message)
\n

Equivalent to gettext(), but the translation is returned in the preferred\nsystem encoding, if no other encoding was explicitly set with\nbind_textdomain_codeset().

\n

\nNew in version 2.4.

\n
\n\n
\n
\ngettext.dgettext(domain, message)
\n
Like gettext(), but look the message up in the specified domain.
\n\n
\n
\ngettext.ldgettext(domain, message)
\n

Equivalent to dgettext(), but the translation is returned in the preferred\nsystem encoding, if no other encoding was explicitly set with\nbind_textdomain_codeset().

\n

\nNew in version 2.4.

\n
\n\n
\n
\ngettext.ngettext(singular, plural, n)
\n

Like gettext(), but consider plural forms. If a translation is found,\napply the plural formula to n, and return the resulting message (some\nlanguages have more than two plural forms). If no translation is found, return\nsingular if n is 1; return plural otherwise.

\n

The Plural formula is taken from the catalog header. It is a C or Python\nexpression that has a free variable n; the expression evaluates to the index\nof the plural in the catalog. See the GNU gettext documentation for the precise\nsyntax to be used in .po files and the formulas for a variety of\nlanguages.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ngettext.lngettext(singular, plural, n)
\n

Equivalent to ngettext(), but the translation is returned in the preferred\nsystem encoding, if no other encoding was explicitly set with\nbind_textdomain_codeset().

\n

\nNew in version 2.4.

\n
\n\n
\n
\ngettext.dngettext(domain, singular, plural, n)
\n

Like ngettext(), but look the message up in the specified domain.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ngettext.ldngettext(domain, singular, plural, n)
\n

Equivalent to dngettext(), but the translation is returned in the\npreferred system encoding, if no other encoding was explicitly set with\nbind_textdomain_codeset().

\n

\nNew in version 2.4.

\n
\n\n

Note that GNU gettext also defines a dcgettext() method, but\nthis was deemed not useful and so it is currently unimplemented.

\n

Here’s an example of typical usage for this API:

\n
import gettext\ngettext.bindtextdomain('myapplication', '/path/to/my/language/directory')\ngettext.textdomain('myapplication')\n_ = gettext.gettext\n# ...\nprint _('This is a translatable string.')\n
\n
\n
\n
\n

22.1.2. Class-based API

\n

The class-based API of the gettext module gives you more flexibility and\ngreater convenience than the GNU gettext API. It is the recommended\nway of localizing your Python applications and modules. gettext defines\na “translations” class which implements the parsing of GNU .mo format\nfiles, and has methods for returning either standard 8-bit strings or Unicode\nstrings. Instances of this “translations” class can also install themselves in\nthe built-in namespace as the function _().

\n
\n
\ngettext.find(domain[, localedir[, languages[, all]]])
\n

This function implements the standard .mo file search algorithm. It\ntakes a domain, identical to what textdomain() takes. Optional\nlocaledir is as in bindtextdomain() Optional languages is a list of\nstrings, where each string is a language code.

\n

If localedir is not given, then the default system locale directory is used.\n[2] If languages is not given, then the following environment variables are\nsearched: LANGUAGE, LC_ALL, LC_MESSAGES, and\nLANG. The first one returning a non-empty value is used for the\nlanguages variable. The environment variables should contain a colon separated\nlist of languages, which will be split on the colon to produce the expected list\nof language code strings.

\n

find() then expands and normalizes the languages, and then iterates\nthrough them, searching for an existing file built of these components:

\n

localedir/language/LC_MESSAGES/domain.mo

\n

The first such file name that exists is returned by find(). If no such\nfile is found, then None is returned. If all is given, it returns a list\nof all file names, in the order in which they appear in the languages list or\nthe environment variables.

\n
\n\n
\n
\ngettext.translation(domain[, localedir[, languages[, class_[, fallback[, codeset]]]]])
\n

Return a Translations instance based on the domain, localedir, and\nlanguages, which are first passed to find() to get a list of the\nassociated .mo file paths. Instances with identical .mo file\nnames are cached. The actual class instantiated is either class_ if provided,\notherwise GNUTranslations. The class’s constructor must take a single\nfile object argument. If provided, codeset will change the charset used to\nencode translated strings.

\n

If multiple files are found, later files are used as fallbacks for earlier ones.\nTo allow setting the fallback, copy.copy() is used to clone each\ntranslation object from the cache; the actual instance data is still shared with\nthe cache.

\n

If no .mo file is found, this function raises IOError if\nfallback is false (which is the default), and returns a\nNullTranslations instance if fallback is true.

\n

\nChanged in version 2.4: Added the codeset parameter.

\n
\n\n
\n
\ngettext.install(domain[, localedir[, unicode[, codeset[, names]]]])
\n

This installs the function _() in Python’s builtins namespace, based on\ndomain, localedir, and codeset which are passed to the function\ntranslation(). The unicode flag is passed to the resulting translation\nobject’s install() method.

\n

For the names parameter, please see the description of the translation\nobject’s install() method.

\n

As seen below, you usually mark the strings in your application that are\ncandidates for translation, by wrapping them in a call to the _()\nfunction, like this:

\n
print _('This string will be translated.')\n
\n
\n

For convenience, you want the _() function to be installed in Python’s\nbuiltins namespace, so it is easily accessible in all modules of your\napplication.

\n

\nChanged in version 2.4: Added the codeset parameter.

\n

\nChanged in version 2.5: Added the names parameter.

\n
\n\n
\n

22.1.2.1. The NullTranslations class

\n

Translation classes are what actually implement the translation of original\nsource file message strings to translated message strings. The base class used\nby all translation classes is NullTranslations; this provides the basic\ninterface you can use to write your own specialized translation classes. Here\nare the methods of NullTranslations:

\n
\n
\nclass gettext.NullTranslations([fp])
\n

Takes an optional file object fp, which is ignored by the base class.\nInitializes “protected” instance variables _info and _charset which are set\nby derived classes, as well as _fallback, which is set through\nadd_fallback(). It then calls self._parse(fp) if fp is not\nNone.

\n
\n
\n_parse(fp)
\n
No-op’d in the base class, this method takes file object fp, and reads\nthe data from the file, initializing its message catalog. If you have an\nunsupported message catalog file format, you should override this method\nto parse your format.
\n\n
\n
\nadd_fallback(fallback)
\n
Add fallback as the fallback object for the current translation\nobject. A translation object should consult the fallback if it cannot provide a\ntranslation for a given message.
\n\n
\n
\ngettext(message)
\n
If a fallback has been set, forward gettext() to the\nfallback. Otherwise, return the translated message. Overridden in derived\nclasses.
\n\n
\n
\nlgettext(message)
\n

If a fallback has been set, forward lgettext() to the\nfallback. Otherwise, return the translated message. Overridden in derived\nclasses.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nugettext(message)
\n
If a fallback has been set, forward ugettext() to the\nfallback. Otherwise, return the translated message as a Unicode\nstring. Overridden in derived classes.
\n\n
\n
\nngettext(singular, plural, n)
\n

If a fallback has been set, forward ngettext() to the\nfallback. Otherwise, return the translated message. Overridden in derived\nclasses.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nlngettext(singular, plural, n)
\n

If a fallback has been set, forward lngettext() to the\nfallback. Otherwise, return the translated message. Overridden in derived\nclasses.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nungettext(singular, plural, n)
\n

If a fallback has been set, forward ungettext() to the fallback.\nOtherwise, return the translated message as a Unicode string. Overridden\nin derived classes.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ninfo()
\n
Return the “protected” _info variable.
\n\n
\n
\ncharset()
\n
Return the “protected” _charset variable.
\n\n
\n
\noutput_charset()
\n

Return the “protected” _output_charset variable, which defines the\nencoding used to return translated messages.

\n

\nNew in version 2.4.

\n
\n\n
\n
\nset_output_charset(charset)
\n

Change the “protected” _output_charset variable, which defines the\nencoding used to return translated messages.

\n

\nNew in version 2.4.

\n
\n\n
\n
\ninstall([unicode[, names]])
\n

If the unicode flag is false, this method installs self.gettext()\ninto the built-in namespace, binding it to _. If unicode is true,\nit binds self.ugettext() instead. By default, unicode is false.

\n

If the names parameter is given, it must be a sequence containing the\nnames of functions you want to install in the builtins namespace in\naddition to _(). Supported names are 'gettext' (bound to\nself.gettext() or self.ugettext() according to the unicode\nflag), 'ngettext' (bound to self.ngettext() or\nself.ungettext() according to the unicode flag), 'lgettext'\nand 'lngettext'.

\n

Note that this is only one way, albeit the most convenient way, to make\nthe _() function available to your application. Because it affects\nthe entire application globally, and specifically the built-in namespace,\nlocalized modules should never install _(). Instead, they should use\nthis code to make _() available to their module:

\n
import gettext\nt = gettext.translation('mymodule', ...)\n_ = t.gettext\n
\n
\n

This puts _() only in the module’s global namespace and so only\naffects calls within this module.

\n

\nChanged in version 2.5: Added the names parameter.

\n
\n\n
\n\n
\n
\n

22.1.2.2. The GNUTranslations class

\n

The gettext module provides one additional class derived from\nNullTranslations: GNUTranslations. This class overrides\n_parse() to enable reading GNU gettext format .mo files\nin both big-endian and little-endian format. It also coerces both message ids\nand message strings to Unicode.

\n

GNUTranslations parses optional meta-data out of the translation\ncatalog. It is convention with GNU gettext to include meta-data as\nthe translation for the empty string. This meta-data is in RFC 822-style\nkey: value pairs, and should contain the Project-Id-Version key. If the\nkey Content-Type is found, then the charset property is used to\ninitialize the “protected” _charset instance variable, defaulting to\nNone if not found. If the charset encoding is specified, then all message\nids and message strings read from the catalog are converted to Unicode using\nthis encoding. The ugettext() method always returns a Unicode, while the\ngettext() returns an encoded 8-bit string. For the message id arguments\nof both methods, either Unicode strings or 8-bit strings containing only\nUS-ASCII characters are acceptable. Note that the Unicode version of the\nmethods (i.e. ugettext() and ungettext()) are the recommended\ninterface to use for internationalized Python programs.

\n

The entire set of key/value pairs are placed into a dictionary and set as the\n“protected” _info instance variable.

\n

If the .mo file’s magic number is invalid, or if other problems occur\nwhile reading the file, instantiating a GNUTranslations class can raise\nIOError.

\n

The following methods are overridden from the base class implementation:

\n
\n
\nGNUTranslations.gettext(message)
\n
Look up the message id in the catalog and return the corresponding message\nstring, as an 8-bit string encoded with the catalog’s charset encoding, if\nknown. If there is no entry in the catalog for the message id, and a fallback\nhas been set, the look up is forwarded to the fallback’s gettext() method.\nOtherwise, the message id is returned.
\n\n
\n
\nGNUTranslations.lgettext(message)
\n

Equivalent to gettext(), but the translation is returned in the preferred\nsystem encoding, if no other encoding was explicitly set with\nset_output_charset().

\n

\nNew in version 2.4.

\n
\n\n
\n
\nGNUTranslations.ugettext(message)
\n
Look up the message id in the catalog and return the corresponding message\nstring, as a Unicode string. If there is no entry in the catalog for the\nmessage id, and a fallback has been set, the look up is forwarded to the\nfallback’s ugettext() method. Otherwise, the message id is returned.
\n\n
\n
\nGNUTranslations.ngettext(singular, plural, n)
\n

Do a plural-forms lookup of a message id. singular is used as the message id\nfor purposes of lookup in the catalog, while n is used to determine which\nplural form to use. The returned message string is an 8-bit string encoded with\nthe catalog’s charset encoding, if known.

\n

If the message id is not found in the catalog, and a fallback is specified, the\nrequest is forwarded to the fallback’s ngettext() method. Otherwise, when\nn is 1 singular is returned, and plural is returned in all other cases.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nGNUTranslations.lngettext(singular, plural, n)
\n

Equivalent to gettext(), but the translation is returned in the preferred\nsystem encoding, if no other encoding was explicitly set with\nset_output_charset().

\n

\nNew in version 2.4.

\n
\n\n
\n
\nGNUTranslations.ungettext(singular, plural, n)
\n

Do a plural-forms lookup of a message id. singular is used as the message id\nfor purposes of lookup in the catalog, while n is used to determine which\nplural form to use. The returned message string is a Unicode string.

\n

If the message id is not found in the catalog, and a fallback is specified, the\nrequest is forwarded to the fallback’s ungettext() method. Otherwise,\nwhen n is 1 singular is returned, and plural is returned in all other\ncases.

\n

Here is an example:

\n
n = len(os.listdir('.'))\ncat = GNUTranslations(somefile)\nmessage = cat.ungettext(\n    'There is %(num)d file in this directory',\n    'There are %(num)d files in this directory',\n    n)  {'num': n}\n
\n
\n

\nNew in version 2.3.

\n
\n\n
\n
\n

22.1.2.3. Solaris message catalog support

\n

The Solaris operating system defines its own binary .mo file format, but\nsince no documentation can be found on this format, it is not supported at this\ntime.

\n
\n
\n

22.1.2.4. The Catalog constructor

\n

GNOME uses a version of the gettext module by James Henstridge, but this\nversion has a slightly different API. Its documented usage was:

\n
import gettext\ncat = gettext.Catalog(domain, localedir)\n_ = cat.gettext\nprint _('hello world')\n
\n
\n

For compatibility with this older module, the function Catalog() is an\nalias for the translation() function described above.

\n

One difference between this module and Henstridge’s: his catalog objects\nsupported access through a mapping API, but this appears to be unused and so is\nnot currently supported.

\n
\n
\n
\n

22.1.3. Internationalizing your programs and modules

\n

Internationalization (I18N) refers to the operation by which a program is made\naware of multiple languages. Localization (L10N) refers to the adaptation of\nyour program, once internationalized, to the local language and cultural habits.\nIn order to provide multilingual messages for your Python programs, you need to\ntake the following steps:

\n
    \n
  1. prepare your program or module by specially marking translatable strings
  2. \n
  3. run a suite of tools over your marked files to generate raw messages catalogs
  4. \n
  5. create language specific translations of the message catalogs
  6. \n
  7. use the gettext module so that message strings are properly translated
  8. \n
\n

In order to prepare your code for I18N, you need to look at all the strings in\nyour files. Any string that needs to be translated should be marked by wrapping\nit in _('...') — that is, a call to the function _(). For example:

\n
filename = 'mylog.txt'\nmessage = _('writing a log message')\nfp = open(filename, 'w')\nfp.write(message)\nfp.close()\n
\n
\n

In this example, the string 'writing a log message' is marked as a candidate\nfor translation, while the strings 'mylog.txt' and 'w' are not.

\n

The Python distribution comes with two tools which help you generate the message\ncatalogs once you’ve prepared your source code. These may or may not be\navailable from a binary distribution, but they can be found in a source\ndistribution, in the Tools/i18n directory.

\n

The pygettext [3] program scans all your Python source code looking\nfor the strings you previously marked as translatable. It is similar to the GNU\ngettext program except that it understands all the intricacies of\nPython source code, but knows nothing about C or C++ source code. You don’t\nneed GNU gettext unless you’re also going to be translating C code (such as\nC extension modules).

\n

pygettext generates textual Uniforum-style human readable message\ncatalog .pot files, essentially structured human readable files which\ncontain every marked string in the source code, along with a placeholder for the\ntranslation strings. pygettext is a command line script that supports\na similar command line interface as xgettext; for details on its use,\nrun:

\n
pygettext.py --help\n
\n
\n

Copies of these .pot files are then handed over to the individual human\ntranslators who write language-specific versions for every supported natural\nlanguage. They send you back the filled in language-specific versions as a\n.po file. Using the msgfmt.py [4] program (in the\nTools/i18n directory), you take the .po files from your\ntranslators and generate the machine-readable .mo binary catalog files.\nThe .mo files are what the gettext module uses for the actual\ntranslation processing during run-time.

\n

How you use the gettext module in your code depends on whether you are\ninternationalizing a single module or your entire application. The next two\nsections will discuss each case.

\n
\n

22.1.3.1. Localizing your module

\n

If you are localizing your module, you must take care not to make global\nchanges, e.g. to the built-in namespace. You should not use the GNU gettext\nAPI but instead the class-based API.

\n

Let’s say your module is called “spam” and the module’s various natural language\ntranslation .mo files reside in /usr/share/locale in GNU\ngettext format. Here’s what you would put at the top of your\nmodule:

\n
import gettext\nt = gettext.translation('spam', '/usr/share/locale')\n_ = t.lgettext\n
\n
\n

If your translators were providing you with Unicode strings in their .po\nfiles, you’d instead do:

\n
import gettext\nt = gettext.translation('spam', '/usr/share/locale')\n_ = t.ugettext\n
\n
\n
\n
\n

22.1.3.2. Localizing your application

\n

If you are localizing your application, you can install the _() function\nglobally into the built-in namespace, usually in the main driver file of your\napplication. This will let all your application-specific files just use\n_('...') without having to explicitly install it in each file.

\n

In the simple case then, you need only add the following bit of code to the main\ndriver file of your application:

\n
import gettext\ngettext.install('myapplication')\n
\n
\n

If you need to set the locale directory or the unicode flag, you can pass\nthese into the install() function:

\n
import gettext\ngettext.install('myapplication', '/usr/share/locale', unicode=1)\n
\n
\n
\n
\n

22.1.3.3. Changing languages on the fly

\n

If your program needs to support many languages at the same time, you may want\nto create multiple translation instances and then switch between them\nexplicitly, like so:

\n
import gettext\n\nlang1 = gettext.translation('myapplication', languages=['en'])\nlang2 = gettext.translation('myapplication', languages=['fr'])\nlang3 = gettext.translation('myapplication', languages=['de'])\n\n# start by using language1\nlang1.install()\n\n# ... time goes by, user selects language 2\nlang2.install()\n\n# ... more time goes by, user selects language 3\nlang3.install()\n
\n
\n
\n
\n

22.1.3.4. Deferred translations

\n

In most coding situations, strings are translated where they are coded.\nOccasionally however, you need to mark strings for translation, but defer actual\ntranslation until later. A classic example is:

\n
animals = ['mollusk',\n           'albatross',\n           'rat',\n           'penguin',\n           'python', ]\n# ...\nfor a in animals:\n    print a\n
\n
\n

Here, you want to mark the strings in the animals list as being\ntranslatable, but you don’t actually want to translate them until they are\nprinted.

\n

Here is one way you can handle this situation:

\n
def _(message): return message\n\nanimals = [_('mollusk'),\n           _('albatross'),\n           _('rat'),\n           _('penguin'),\n           _('python'), ]\n\ndel _\n\n# ...\nfor a in animals:\n    print _(a)\n
\n
\n

This works because the dummy definition of _() simply returns the string\nunchanged. And this dummy definition will temporarily override any definition\nof _() in the built-in namespace (until the del command). Take\ncare, though if you have a previous definition of _() in the local\nnamespace.

\n

Note that the second use of _() will not identify “a” as being\ntranslatable to the pygettext program, since it is not a string.

\n

Another way to handle this is with the following example:

\n
def N_(message): return message\n\nanimals = [N_('mollusk'),\n           N_('albatross'),\n           N_('rat'),\n           N_('penguin'),\n           N_('python'), ]\n\n# ...\nfor a in animals:\n    print _(a)\n
\n
\n

In this case, you are marking translatable strings with the function N_(),\n[5] which won’t conflict with any definition of _(). However, you will\nneed to teach your message extraction program to look for translatable strings\nmarked with N_(). pygettext and xpot both support\nthis through the use of command line switches.

\n
\n
\n

22.1.3.5. gettext() vs. lgettext()

\n

In Python 2.4 the lgettext() family of functions were introduced. The\nintention of these functions is to provide an alternative which is more\ncompliant with the current implementation of GNU gettext. Unlike\ngettext(), which returns strings encoded with the same codeset used in the\ntranslation file, lgettext() will return strings encoded with the\npreferred system encoding, as returned by locale.getpreferredencoding().\nAlso notice that Python 2.4 introduces new functions to explicitly choose the\ncodeset used in translated strings. If a codeset is explicitly set, even\nlgettext() will return translated strings in the requested codeset, as\nwould be expected in the GNU gettext implementation.

\n
\n
\n
\n

22.1.4. Acknowledgements

\n

The following people contributed code, feedback, design suggestions, previous\nimplementations, and valuable experience to the creation of this module:

\n\n

Footnotes

\n\n\n\n\n\n
[1]The default locale directory is system dependent; for example, on RedHat Linux\nit is /usr/share/locale, but on Solaris it is /usr/lib/locale.\nThe gettext module does not try to support these system dependent\ndefaults; instead its default is sys.prefix/share/locale. For this\nreason, it is always best to call bindtextdomain() with an explicit\nabsolute path at the start of your application.
\n\n\n\n\n\n
[2]See the footnote for bindtextdomain() above.
\n\n\n\n\n\n
[3]François Pinard has written a program called xpot which does a\nsimilar job. It is available as part of his po-utils package.
\n\n\n\n\n\n
[4]msgfmt.py is binary compatible with GNU msgfmt except that\nit provides a simpler, all-Python implementation. With this and\npygettext.py, you generally won’t need to install the GNU\ngettext package to internationalize your Python applications.
\n\n\n\n\n\n
[5]The choice of N_() here is totally arbitrary; it could have just as easily\nbeen MarkThisStringForTranslation().
\n
\n
", "searchableItems": [ { "name": "gettext.bind_textdomain_codeset", "domId": "gettext_gettext.bind_textdomain_codeset" }, { "name": "gettext.bindtextdomain", "domId": "gettext_gettext.bindtextdomain" }, { "name": "gettext.dgettext", "domId": "gettext_gettext.dgettext" }, { "name": "gettext.dngettext", "domId": "gettext_gettext.dngettext" }, { "name": "gettext.find", "domId": "gettext_gettext.find" }, { "name": "gettext.gettext", "domId": "gettext_gettext.gettext" }, { "name": "gettext.GNUTranslations.gettext", "domId": "gettext_gettext.GNUTranslations.gettext" }, { "name": "gettext.GNUTranslations.lgettext", "domId": "gettext_gettext.GNUTranslations.lgettext" }, { "name": "gettext.GNUTranslations.lngettext", "domId": "gettext_gettext.GNUTranslations.lngettext" }, { "name": "gettext.GNUTranslations.ngettext", "domId": "gettext_gettext.GNUTranslations.ngettext" }, { "name": "gettext.GNUTranslations.ugettext", "domId": "gettext_gettext.GNUTranslations.ugettext" }, { "name": "gettext.GNUTranslations.ungettext", "domId": "gettext_gettext.GNUTranslations.ungettext" }, { "name": "gettext.install", "domId": "gettext_gettext.install" }, { "name": "gettext.ldgettext", "domId": "gettext_gettext.ldgettext" }, { "name": "gettext.ldngettext", "domId": "gettext_gettext.ldngettext" }, { "name": "gettext.lgettext", "domId": "gettext_gettext.lgettext" }, { "name": "gettext.lngettext", "domId": "gettext_gettext.lngettext" }, { "name": "gettext.ngettext", "domId": "gettext_gettext.ngettext" }, { "name": "gettext.NullTranslations", "domId": "gettext_gettext.NullTranslations" }, { "name": "gettext.NullTranslations._parse", "domId": "gettext_gettext.NullTranslations._parse" }, { "name": "gettext.NullTranslations.add_fallback", "domId": "gettext_gettext.NullTranslations.add_fallback" }, { "name": "gettext.NullTranslations.charset", "domId": "gettext_gettext.NullTranslations.charset" }, { "name": "gettext.NullTranslations.gettext", "domId": "gettext_gettext.NullTranslations.gettext" }, { "name": "gettext.NullTranslations.info", "domId": "gettext_gettext.NullTranslations.info" }, { "name": "gettext.NullTranslations.install", "domId": "gettext_gettext.NullTranslations.install" }, { "name": "gettext.NullTranslations.lgettext", "domId": "gettext_gettext.NullTranslations.lgettext" }, { "name": "gettext.NullTranslations.lngettext", "domId": "gettext_gettext.NullTranslations.lngettext" }, { "name": "gettext.NullTranslations.ngettext", "domId": "gettext_gettext.NullTranslations.ngettext" }, { "name": "gettext.NullTranslations.output_charset", "domId": "gettext_gettext.NullTranslations.output_charset" }, { "name": "gettext.NullTranslations.set_output_charset", "domId": "gettext_gettext.NullTranslations.set_output_charset" }, { "name": "gettext.NullTranslations.ugettext", "domId": "gettext_gettext.NullTranslations.ugettext" }, { "name": "gettext.NullTranslations.ungettext", "domId": "gettext_gettext.NullTranslations.ungettext" }, { "name": "gettext.textdomain", "domId": "gettext_gettext.textdomain" }, { "name": "gettext.translation", "domId": "gettext_gettext.translation" } ] }, { "url": "http://docs.python.org/library/ossaudiodev.html", "title": "ossaudiodev", "html": "
\n

21.10. ossaudiodev — Access to OSS-compatible audio devices

\n

Platforms: Linux, FreeBSD

\n

\nNew in version 2.3.

\n

This module allows you to access the OSS (Open Sound System) audio interface.\nOSS is available for a wide range of open-source and commercial Unices, and is\nthe standard audio interface for Linux and recent versions of FreeBSD.

\n
\n

See also

\n
\n
Open Sound System Programmer’s Guide
\n
the official documentation for the OSS C API
\n
\n

The module defines a large number of constants supplied by the OSS device\ndriver; see <sys/soundcard.h> on either Linux or FreeBSD for a listing .

\n
\n

ossaudiodev defines the following variables and functions:

\n
\n
\nexception ossaudiodev.OSSAudioError
\n

This exception is raised on certain errors. The argument is a string describing\nwhat went wrong.

\n

(If ossaudiodev receives an error from a system call such as\nopen(), write(), or ioctl(), it raises IOError.\nErrors detected directly by ossaudiodev result in OSSAudioError.)

\n

(For backwards compatibility, the exception class is also available as\nossaudiodev.error.)

\n
\n\n
\n
\nossaudiodev.open([device], mode)
\n

Open an audio device and return an OSS audio device object. This object\nsupports many file-like methods, such as read(), write(), and\nfileno() (although there are subtle differences between conventional Unix\nread/write semantics and those of OSS audio devices). It also supports a number\nof audio-specific methods; see below for the complete list of methods.

\n

device is the audio device filename to use. If it is not specified, this\nmodule first looks in the environment variable AUDIODEV for a device\nto use. If not found, it falls back to /dev/dsp.

\n

mode is one of 'r' for read-only (record) access, 'w' for\nwrite-only (playback) access and 'rw' for both. Since many sound cards\nonly allow one process to have the recorder or player open at a time, it is a\ngood idea to open the device only for the activity needed. Further, some\nsound cards are half-duplex: they can be opened for reading or writing, but\nnot both at once.

\n

Note the unusual calling syntax: the first argument is optional, and the\nsecond is required. This is a historical artifact for compatibility with the\nolder linuxaudiodev module which ossaudiodev supersedes.

\n
\n\n
\n
\nossaudiodev.openmixer([device])
\n
Open a mixer device and return an OSS mixer device object. device is the\nmixer device filename to use. If it is not specified, this module first looks\nin the environment variable MIXERDEV for a device to use. If not\nfound, it falls back to /dev/mixer.
\n\n
\n

21.10.1. Audio Device Objects

\n

Before you can write to or read from an audio device, you must call three\nmethods in the correct order:

\n
    \n
  1. setfmt() to set the output format
  2. \n
  3. channels() to set the number of channels
  4. \n
  5. speed() to set the sample rate
  6. \n
\n

Alternately, you can use the setparameters() method to set all three audio\nparameters at once. This is more convenient, but may not be as flexible in all\ncases.

\n

The audio device objects returned by open() define the following methods\nand (read-only) attributes:

\n
\n
\noss_audio_device.close()
\n
Explicitly close the audio device. When you are done writing to or reading from\nan audio device, you should explicitly close it. A closed device cannot be used\nagain.
\n\n
\n
\noss_audio_device.fileno()
\n
Return the file descriptor associated with the device.
\n\n
\n
\noss_audio_device.read(size)
\n
Read size bytes from the audio input and return them as a Python string.\nUnlike most Unix device drivers, OSS audio devices in blocking mode (the\ndefault) will block read() until the entire requested amount of data is\navailable.
\n\n
\n
\noss_audio_device.write(data)
\n
Write the Python string data to the audio device and return the number of\nbytes written. If the audio device is in blocking mode (the default), the\nentire string is always written (again, this is different from usual Unix device\nsemantics). If the device is in non-blocking mode, some data may not be written\n—see writeall().
\n\n
\n
\noss_audio_device.writeall(data)
\n
Write the entire Python string data to the audio device: waits until the audio\ndevice is able to accept data, writes as much data as it will accept, and\nrepeats until data has been completely written. If the device is in blocking\nmode (the default), this has the same effect as write(); writeall()\nis only useful in non-blocking mode. Has no return value, since the amount of\ndata written is always equal to the amount of data supplied.
\n\n

The following methods each map to exactly one ioctl() system call. The\ncorrespondence is obvious: for example, setfmt() corresponds to the\nSNDCTL_DSP_SETFMT ioctl, and sync() to SNDCTL_DSP_SYNC (this can\nbe useful when consulting the OSS documentation). If the underlying\nioctl() fails, they all raise IOError.

\n
\n
\noss_audio_device.nonblock()
\n
Put the device into non-blocking mode. Once in non-blocking mode, there is no\nway to return it to blocking mode.
\n\n
\n
\noss_audio_device.getfmts()
\n

Return a bitmask of the audio output formats supported by the soundcard. Some\nof the formats supported by OSS are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
FormatDescription
AFMT_MU_LAWa logarithmic encoding (used by Sun .au\nfiles and /dev/audio)
AFMT_A_LAWa logarithmic encoding
AFMT_IMA_ADPCMa 4:1 compressed format defined by the\nInteractive Multimedia Association
AFMT_U8Unsigned, 8-bit audio
AFMT_S16_LESigned, 16-bit audio, little-endian byte\norder (as used by Intel processors)
AFMT_S16_BESigned, 16-bit audio, big-endian byte order\n(as used by 68k, PowerPC, Sparc)
AFMT_S8Signed, 8 bit audio
AFMT_U16_LEUnsigned, 16-bit little-endian audio
AFMT_U16_BEUnsigned, 16-bit big-endian audio
\n

Consult the OSS documentation for a full list of audio formats, and note that\nmost devices support only a subset of these formats. Some older devices only\nsupport AFMT_U8; the most common format used today is\nAFMT_S16_LE.

\n
\n\n
\n
\noss_audio_device.setfmt(format)
\n
Try to set the current audio format to format—see getfmts() for a\nlist. Returns the audio format that the device was set to, which may not be the\nrequested format. May also be used to return the current audio format—do this\nby passing an “audio format” of AFMT_QUERY.
\n\n
\n
\noss_audio_device.channels(nchannels)
\n
Set the number of output channels to nchannels. A value of 1 indicates\nmonophonic sound, 2 stereophonic. Some devices may have more than 2 channels,\nand some high-end devices may not support mono. Returns the number of channels\nthe device was set to.
\n\n
\n
\noss_audio_device.speed(samplerate)
\n

Try to set the audio sampling rate to samplerate samples per second. Returns\nthe rate actually set. Most sound devices don’t support arbitrary sampling\nrates. Common rates are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
RateDescription
8000default rate for /dev/audio
11025speech recording
22050 
44100CD quality audio (at 16 bits/sample and 2\nchannels)
96000DVD quality audio (at 24 bits/sample)
\n
\n\n
\n
\noss_audio_device.sync()
\n
Wait until the sound device has played every byte in its buffer. (This happens\nimplicitly when the device is closed.) The OSS documentation recommends closing\nand re-opening the device rather than using sync().
\n\n
\n
\noss_audio_device.reset()
\n
Immediately stop playing or recording and return the device to a state where it\ncan accept commands. The OSS documentation recommends closing and re-opening\nthe device after calling reset().
\n\n
\n
\noss_audio_device.post()
\n
Tell the driver that there is likely to be a pause in the output, making it\npossible for the device to handle the pause more intelligently. You might use\nthis after playing a spot sound effect, before waiting for user input, or before\ndoing disk I/O.
\n\n

The following convenience methods combine several ioctls, or one ioctl and some\nsimple calculations.

\n
\n
\noss_audio_device.setparameters(format, nchannels, samplerate[, strict=False])
\n

Set the key audio sampling parameters—sample format, number of channels, and\nsampling rate—in one method call. format, nchannels, and samplerate\nshould be as specified in the setfmt(), channels(), and\nspeed() methods. If strict is true, setparameters() checks to\nsee if each parameter was actually set to the requested value, and raises\nOSSAudioError if not. Returns a tuple (format, nchannels,\nsamplerate) indicating the parameter values that were actually set by the\ndevice driver (i.e., the same as the return values of setfmt(),\nchannels(), and speed()).

\n

For example,

\n
(fmt, channels, rate) = dsp.setparameters(fmt, channels, rate)\n
\n
\n

is equivalent to

\n
fmt = dsp.setfmt(fmt)\nchannels = dsp.channels(channels)\nrate = dsp.rate(channels)\n
\n
\n
\n\n
\n
\noss_audio_device.bufsize()
\n
Returns the size of the hardware buffer, in samples.
\n\n
\n
\noss_audio_device.obufcount()
\n
Returns the number of samples that are in the hardware buffer yet to be played.
\n\n
\n
\noss_audio_device.obuffree()
\n
Returns the number of samples that could be queued into the hardware buffer to\nbe played without blocking.
\n\n

Audio device objects also support several read-only attributes:

\n
\n
\noss_audio_device.closed
\n
Boolean indicating whether the device has been closed.
\n\n
\n
\noss_audio_device.name
\n
String containing the name of the device file.
\n\n
\n
\noss_audio_device.mode
\n
The I/O mode for the file, either "r", "rw", or "w".
\n\n
\n
\n

21.10.2. Mixer Device Objects

\n

The mixer object provides two file-like methods:

\n
\n
\noss_mixer_device.close()
\n
This method closes the open mixer device file. Any further attempts to use the\nmixer after this file is closed will raise an IOError.
\n\n
\n
\noss_mixer_device.fileno()
\n
Returns the file handle number of the open mixer device file.
\n\n

The remaining methods are specific to audio mixing:

\n
\n
\noss_mixer_device.controls()
\n

This method returns a bitmask specifying the available mixer controls (“Control”\nbeing a specific mixable “channel”, such as SOUND_MIXER_PCM or\nSOUND_MIXER_SYNTH). This bitmask indicates a subset of all available\nmixer controls—the SOUND_MIXER_* constants defined at module level.\nTo determine if, for example, the current mixer object supports a PCM mixer, use\nthe following Python code:

\n
mixer=ossaudiodev.openmixer()\nif mixer.controls() & (1 << ossaudiodev.SOUND_MIXER_PCM):\n    # PCM is supported\n    ... code ...\n
\n
\n

For most purposes, the SOUND_MIXER_VOLUME (master volume) and\nSOUND_MIXER_PCM controls should suffice—but code that uses the mixer\nshould be flexible when it comes to choosing mixer controls. On the Gravis\nUltrasound, for example, SOUND_MIXER_VOLUME does not exist.

\n
\n\n
\n
\noss_mixer_device.stereocontrols()
\n

Returns a bitmask indicating stereo mixer controls. If a bit is set, the\ncorresponding control is stereo; if it is unset, the control is either\nmonophonic or not supported by the mixer (use in combination with\ncontrols() to determine which).

\n

See the code example for the controls() function for an example of getting\ndata from a bitmask.

\n
\n\n
\n
\noss_mixer_device.reccontrols()
\n
Returns a bitmask specifying the mixer controls that may be used to record. See\nthe code example for controls() for an example of reading from a bitmask.
\n\n
\n
\noss_mixer_device.get(control)
\n

Returns the volume of a given mixer control. The returned volume is a 2-tuple\n(left_volume,right_volume). Volumes are specified as numbers from 0\n(silent) to 100 (full volume). If the control is monophonic, a 2-tuple is still\nreturned, but both volumes are the same.

\n

Raises OSSAudioError if an invalid control was is specified, or\nIOError if an unsupported control is specified.

\n
\n\n
\n
\noss_mixer_device.set(control, (left, right))
\n

Sets the volume for a given mixer control to (left,right). left and\nright must be ints and between 0 (silent) and 100 (full volume). On\nsuccess, the new volume is returned as a 2-tuple. Note that this may not be\nexactly the same as the volume specified, because of the limited resolution of\nsome soundcard’s mixers.

\n

Raises OSSAudioError if an invalid mixer control was specified, or if the\nspecified volumes were out-of-range.

\n
\n\n
\n
\noss_mixer_device.get_recsrc()
\n
This method returns a bitmask indicating which control(s) are currently being\nused as a recording source.
\n\n
\n
\noss_mixer_device.set_recsrc(bitmask)
\n

Call this function to specify a recording source. Returns a bitmask indicating\nthe new recording source (or sources) if successful; raises IOError if an\ninvalid source was specified. To set the current recording source to the\nmicrophone input:

\n
mixer.setrecsrc (1 << ossaudiodev.SOUND_MIXER_MIC)\n
\n
\n
\n\n
\n
", "searchableItems": [ { "name": "ossaudiodev.open", "domId": "ossaudiodev_ossaudiodev.open" }, { "name": "ossaudiodev.openmixer", "domId": "ossaudiodev_ossaudiodev.openmixer" }, { "name": "ossaudiodev.oss_audio_device.bufsize", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.bufsize" }, { "name": "ossaudiodev.oss_audio_device.channels", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.channels" }, { "name": "ossaudiodev.oss_audio_device.close", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.close" }, { "name": "ossaudiodev.oss_audio_device.fileno", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.fileno" }, { "name": "ossaudiodev.oss_audio_device.getfmts", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.getfmts" }, { "name": "ossaudiodev.oss_audio_device.nonblock", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.nonblock" }, { "name": "ossaudiodev.oss_audio_device.obufcount", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.obufcount" }, { "name": "ossaudiodev.oss_audio_device.obuffree", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.obuffree" }, { "name": "ossaudiodev.oss_audio_device.post", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.post" }, { "name": "ossaudiodev.oss_audio_device.read", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.read" }, { "name": "ossaudiodev.oss_audio_device.reset", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.reset" }, { "name": "ossaudiodev.oss_audio_device.setfmt", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.setfmt" }, { "name": "ossaudiodev.oss_audio_device.setparameters", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.setparameters" }, { "name": "ossaudiodev.oss_audio_device.speed", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.speed" }, { "name": "ossaudiodev.oss_audio_device.sync", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.sync" }, { "name": "ossaudiodev.oss_audio_device.write", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.write" }, { "name": "ossaudiodev.oss_audio_device.writeall", "domId": "ossaudiodev_ossaudiodev.oss_audio_device.writeall" }, { "name": "ossaudiodev.oss_mixer_device.close", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.close" }, { "name": "ossaudiodev.oss_mixer_device.controls", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.controls" }, { "name": "ossaudiodev.oss_mixer_device.fileno", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.fileno" }, { "name": "ossaudiodev.oss_mixer_device.get", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.get" }, { "name": "ossaudiodev.oss_mixer_device.get_recsrc", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.get_recsrc" }, { "name": "ossaudiodev.oss_mixer_device.reccontrols", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.reccontrols" }, { "name": "ossaudiodev.oss_mixer_device.set", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.set" }, { "name": "ossaudiodev.oss_mixer_device.set_recsrc", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.set_recsrc" }, { "name": "ossaudiodev.oss_mixer_device.stereocontrols", "domId": "ossaudiodev_ossaudiodev.oss_mixer_device.stereocontrols" } ] }, { "url": "http://docs.python.org/library/cmd.html", "title": "cmd", "html": "
\n

23.1. cmd — Support for line-oriented command interpreters

\n

Source code: Lib/cmd.py

\n
\n

The Cmd class provides a simple framework for writing line-oriented\ncommand interpreters. These are often useful for test harnesses, administrative\ntools, and prototypes that will later be wrapped in a more sophisticated\ninterface.

\n
\n
\nclass cmd.Cmd([completekey[, stdin[, stdout]]])
\n

A Cmd instance or subclass instance is a line-oriented interpreter\nframework. There is no good reason to instantiate Cmd itself; rather,\nit’s useful as a superclass of an interpreter class you define yourself in order\nto inherit Cmd‘s methods and encapsulate action methods.

\n

The optional argument completekey is the readline name of a completion\nkey; it defaults to Tab. If completekey is not None and\nreadline is available, command completion is done automatically.

\n

The optional arguments stdin and stdout specify the input and output file\nobjects that the Cmd instance or subclass instance will use for input and\noutput. If not specified, they will default to sys.stdin and\nsys.stdout.

\n

If you want a given stdin to be used, make sure to set the instance’s\nuse_rawinput attribute to False, otherwise stdin will be\nignored.

\n

\nChanged in version 2.3: The stdin and stdout parameters were added.

\n
\n\n
\n

23.1.1. Cmd Objects

\n

A Cmd instance has the following methods:

\n
\n
\nCmd.cmdloop([intro])
\n

Repeatedly issue a prompt, accept input, parse an initial prefix off the\nreceived input, and dispatch to action methods, passing them the remainder of\nthe line as argument.

\n

The optional argument is a banner or intro string to be issued before the first\nprompt (this overrides the intro class attribute).

\n

If the readline module is loaded, input will automatically inherit\nbash-like history-list editing (e.g. Control-P scrolls back\nto the last command, Control-N forward to the next one, Control-F\nmoves the cursor to the right non-destructively, Control-B moves the\ncursor to the left non-destructively, etc.).

\n

An end-of-file on input is passed back as the string 'EOF'.

\n

An interpreter instance will recognize a command name foo if and only if it\nhas a method do_foo(). As a special case, a line beginning with the\ncharacter '?' is dispatched to the method do_help(). As another\nspecial case, a line beginning with the character '!' is dispatched to the\nmethod do_shell() (if such a method is defined).

\n

This method will return when the postcmd() method returns a true value.\nThe stop argument to postcmd() is the return value from the command’s\ncorresponding do_*() method.

\n

If completion is enabled, completing commands will be done automatically, and\ncompleting of commands args is done by calling complete_foo() with\narguments text, line, begidx, and endidx. text is the string prefix\nwe are attempting to match: all returned matches must begin with it. line is\nthe current input line with leading whitespace removed, begidx and endidx\nare the beginning and ending indexes of the prefix text, which could be used to\nprovide different completion depending upon which position the argument is in.

\n

All subclasses of Cmd inherit a predefined do_help(). This\nmethod, called with an argument 'bar', invokes the corresponding method\nhelp_bar(), and if that is not present, prints the docstring of\ndo_bar(), if available. With no argument, do_help() lists all\navailable help topics (that is, all commands with corresponding\nhelp_*() methods or commands that have docstrings), and also lists any\nundocumented commands.

\n
\n\n
\n
\nCmd.onecmd(str)
\n
Interpret the argument as though it had been typed in response to the prompt.\nThis may be overridden, but should not normally need to be; see the\nprecmd() and postcmd() methods for useful execution hooks. The\nreturn value is a flag indicating whether interpretation of commands by the\ninterpreter should stop. If there is a do_*() method for the command\nstr, the return value of that method is returned, otherwise the return value\nfrom the default() method is returned.
\n\n
\n
\nCmd.emptyline()
\n
Method called when an empty line is entered in response to the prompt. If this\nmethod is not overridden, it repeats the last nonempty command entered.
\n\n
\n
\nCmd.default(line)
\n
Method called on an input line when the command prefix is not recognized. If\nthis method is not overridden, it prints an error message and returns.
\n\n
\n
\nCmd.completedefault(text, line, begidx, endidx)
\n
Method called to complete an input line when no command-specific\ncomplete_*() method is available. By default, it returns an empty list.
\n\n
\n
\nCmd.precmd(line)
\n
Hook method executed just before the command line line is interpreted, but\nafter the input prompt is generated and issued. This method is a stub in\nCmd; it exists to be overridden by subclasses. The return value is\nused as the command which will be executed by the onecmd() method; the\nprecmd() implementation may re-write the command or simply return line\nunchanged.
\n\n
\n
\nCmd.postcmd(stop, line)
\n
Hook method executed just after a command dispatch is finished. This method is\na stub in Cmd; it exists to be overridden by subclasses. line is the\ncommand line which was executed, and stop is a flag which indicates whether\nexecution will be terminated after the call to postcmd(); this will be the\nreturn value of the onecmd() method. The return value of this method will\nbe used as the new value for the internal flag which corresponds to stop;\nreturning false will cause interpretation to continue.
\n\n
\n
\nCmd.preloop()
\n
Hook method executed once when cmdloop() is called. This method is a stub\nin Cmd; it exists to be overridden by subclasses.
\n\n
\n
\nCmd.postloop()
\n
Hook method executed once when cmdloop() is about to return. This method\nis a stub in Cmd; it exists to be overridden by subclasses.
\n\n

Instances of Cmd subclasses have some public instance variables:

\n
\n
\nCmd.prompt
\n
The prompt issued to solicit input.
\n\n
\n
\nCmd.identchars
\n
The string of characters accepted for the command prefix.
\n\n
\n
\nCmd.lastcmd
\n
The last nonempty command prefix seen.
\n\n
\n
\nCmd.intro
\n
A string to issue as an intro or banner. May be overridden by giving the\ncmdloop() method an argument.
\n\n
\n
\nCmd.doc_header
\n
The header to issue if the help output has a section for documented commands.
\n\n
\n
\nCmd.misc_header
\n
The header to issue if the help output has a section for miscellaneous help\ntopics (that is, there are help_*() methods without corresponding\ndo_*() methods).
\n\n
\n
\nCmd.undoc_header
\n
The header to issue if the help output has a section for undocumented commands\n(that is, there are do_*() methods without corresponding help_*()\nmethods).
\n\n
\n
\nCmd.ruler
\n
The character used to draw separator lines under the help-message headers. If\nempty, no ruler line is drawn. It defaults to '='.
\n\n
\n
\nCmd.use_rawinput
\n
A flag, defaulting to true. If true, cmdloop() uses raw_input() to\ndisplay a prompt and read the next command; if false, sys.stdout.write()\nand sys.stdin.readline() are used. (This means that by importing\nreadline, on systems that support it, the interpreter will automatically\nsupport Emacs-like line editing and command-history keystrokes.)
\n\n
\n
", "searchableItems": [ { "name": "cmd.Cmd", "domId": "cmd_cmd.Cmd" }, { "name": "cmd.Cmd.cmdloop", "domId": "cmd_cmd.Cmd.cmdloop" }, { "name": "cmd.Cmd.completedefault", "domId": "cmd_cmd.Cmd.completedefault" }, { "name": "cmd.Cmd.default", "domId": "cmd_cmd.Cmd.default" }, { "name": "cmd.Cmd.emptyline", "domId": "cmd_cmd.Cmd.emptyline" }, { "name": "cmd.Cmd.onecmd", "domId": "cmd_cmd.Cmd.onecmd" }, { "name": "cmd.Cmd.postcmd", "domId": "cmd_cmd.Cmd.postcmd" }, { "name": "cmd.Cmd.postloop", "domId": "cmd_cmd.Cmd.postloop" }, { "name": "cmd.Cmd.precmd", "domId": "cmd_cmd.Cmd.precmd" }, { "name": "cmd.Cmd.preloop", "domId": "cmd_cmd.Cmd.preloop" } ] }, { "url": "http://docs.python.org/library/locale.html", "title": "locale", "html": "
\n

22.2. locale — Internationalization services

\n

The locale module opens access to the POSIX locale database and\nfunctionality. The POSIX locale mechanism allows programmers to deal with\ncertain cultural issues in an application, without requiring the programmer to\nknow all the specifics of each country where the software is executed.

\n

The locale module is implemented on top of the _locale module,\nwhich in turn uses an ANSI C locale implementation if available.

\n

The locale module defines the following exception and functions:

\n
\n
\nexception locale.Error
\n
Exception raised when the locale passed to setlocale() is not\nrecognized.
\n\n
\n
\nlocale.setlocale(category[, locale])
\n

If locale is given and not None, setlocale() modifies the locale\nsetting for the category. The available categories are listed in the data\ndescription below. locale may be a string, or an iterable of two strings\n(language code and encoding). If it’s an iterable, it’s converted to a locale\nname using the locale aliasing engine. An empty string specifies the user’s\ndefault settings. If the modification of the locale fails, the exception\nError is raised. If successful, the new locale setting is returned.

\n

If locale is omitted or None, the current setting for category is\nreturned.

\n

setlocale() is not thread-safe on most systems. Applications typically\nstart with a call of

\n
import locale\nlocale.setlocale(locale.LC_ALL, '')\n
\n
\n

This sets the locale for all categories to the user’s default setting (typically\nspecified in the LANG environment variable). If the locale is not\nchanged thereafter, using multithreading should not cause problems.

\n

\nChanged in version 2.0: Added support for iterable values of the locale parameter.

\n
\n\n
\n
\nlocale.localeconv()
\n

Returns the database of the local conventions as a dictionary. This dictionary\nhas the following strings as keys:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CategoryKeyMeaning
LC_NUMERIC'decimal_point'Decimal point character.
 'grouping'Sequence of numbers specifying\nwhich relative positions the\n'thousands_sep' is\nexpected. If the sequence is\nterminated with\nCHAR_MAX, no further\ngrouping is performed. If the\nsequence terminates with a\n0, the last group size is\nrepeatedly used.
 'thousands_sep'Character used between groups.
LC_MONETARY'int_curr_symbol'International currency symbol.
 'currency_symbol'Local currency symbol.
 'p_cs_precedes/n_cs_precedes'Whether the currency symbol\nprecedes the value (for\npositive resp. negative\nvalues).
 'p_sep_by_space/n_sep_by_space'Whether the currency symbol is\nseparated from the value by a\nspace (for positive resp.\nnegative values).
 'mon_decimal_point'Decimal point used for\nmonetary values.
 'frac_digits'Number of fractional digits\nused in local formatting of\nmonetary values.
 'int_frac_digits'Number of fractional digits\nused in international\nformatting of monetary values.
 'mon_thousands_sep'Group separator used for\nmonetary values.
 'mon_grouping'Equivalent to 'grouping',\nused for monetary values.
 'positive_sign'Symbol used to annotate a\npositive monetary value.
 'negative_sign'Symbol used to annotate a\nnegative monetary value.
 'p_sign_posn/n_sign_posn'The position of the sign (for\npositive resp. negative\nvalues), see below.
\n

All numeric values can be set to CHAR_MAX to indicate that there is no\nvalue specified in this locale.

\n

The possible values for 'p_sign_posn' and 'n_sign_posn' are given below.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ValueExplanation
0Currency and value are surrounded by\nparentheses.
1The sign should precede the value and\ncurrency symbol.
2The sign should follow the value and\ncurrency symbol.
3The sign should immediately precede the\nvalue.
4The sign should immediately follow the\nvalue.
CHAR_MAXNothing is specified in this locale.
\n
\n\n
\n
\nlocale.nl_langinfo(option)
\n

Return some locale-specific information as a string. This function is not\navailable on all systems, and the set of possible options might also vary\nacross platforms. The possible argument values are numbers, for which\nsymbolic constants are available in the locale module.

\n

The nl_langinfo() function accepts one of the following keys. Most\ndescriptions are taken from the corresponding description in the GNU C\nlibrary.

\n
\n
\nlocale.CODESET
\n
Get a string with the name of the character encoding used in the\nselected locale.
\n\n
\n
\nlocale.D_T_FMT
\n
Get a string that can be used as a format string for strftime() to\nrepresent date and time in a locale-specific way.
\n\n
\n
\nlocale.D_FMT
\n
Get a string that can be used as a format string for strftime() to\nrepresent a date in a locale-specific way.
\n\n
\n
\nlocale.T_FMT
\n
Get a string that can be used as a format string for strftime() to\nrepresent a time in a locale-specific way.
\n\n
\n
\nlocale.T_FMT_AMPM
\n
Get a format string for strftime() to represent time in the am/pm\nformat.
\n\n
\n
\nDAY_1 ... DAY_7
\n

Get the name of the n-th day of the week.

\n
\n

Note

\n

This follows the US convention of DAY_1 being Sunday, not the\ninternational convention (ISO 8601) that Monday is the first day of the\nweek.

\n
\n
\n\n
\n
\nABDAY_1 ... ABDAY_7
\n
Get the abbreviated name of the n-th day of the week.
\n\n
\n
\nMON_1 ... MON_12
\n
Get the name of the n-th month.
\n\n
\n
\nABMON_1 ... ABMON_12
\n
Get the abbreviated name of the n-th month.
\n\n
\n
\nlocale.RADIXCHAR
\n
Get the radix character (decimal dot, decimal comma, etc.)
\n\n
\n
\nlocale.THOUSEP
\n
Get the separator character for thousands (groups of three digits).
\n\n
\n
\nlocale.YESEXPR
\n

Get a regular expression that can be used with the regex function to\nrecognize a positive response to a yes/no question.

\n
\n

Note

\n

The expression is in the syntax suitable for the regex() function\nfrom the C library, which might differ from the syntax used in re.

\n
\n
\n\n
\n
\nlocale.NOEXPR
\n
Get a regular expression that can be used with the regex(3) function to\nrecognize a negative response to a yes/no question.
\n\n
\n
\nlocale.CRNCYSTR
\n
Get the currency symbol, preceded by “-” if the symbol should appear before\nthe value, “+” if the symbol should appear after the value, or “.” if the\nsymbol should replace the radix character.
\n\n
\n
\nlocale.ERA
\n

Get a string that represents the era used in the current locale.

\n

Most locales do not define this value. An example of a locale which does\ndefine this value is the Japanese one. In Japan, the traditional\nrepresentation of dates includes the name of the era corresponding to the\nthen-emperor’s reign.

\n

Normally it should not be necessary to use this value directly. Specifying\nthe E modifier in their format strings causes the strftime()\nfunction to use this information. The format of the returned string is not\nspecified, and therefore you should not assume knowledge of it on different\nsystems.

\n
\n\n
\n
\nlocale.ERA_D_T_FMT
\n
Get a format string for strftime() to represent date and time in a\nlocale-specific era-based way.
\n\n
\n
\nlocale.ERA_D_FMT
\n
Get a format string for strftime() to represent a date in a\nlocale-specific era-based way.
\n\n
\n
\nlocale.ERA_T_FMT
\n
Get a format string for strftime() to represent a time in a\nlocale-specific era-based way.
\n\n
\n
\nlocale.ALT_DIGITS
\n
Get a representation of up to 100 values used to represent the values\n0 to 99.
\n\n
\n\n
\n
\nlocale.getdefaultlocale([envvars])
\n

Tries to determine the default locale settings and returns them as a tuple of\nthe form (language code, encoding).

\n

According to POSIX, a program which has not called setlocale(LC_ALL, '')\nruns using the portable 'C' locale. Calling setlocale(LC_ALL, '') lets\nit use the default locale as defined by the LANG variable. Since we\ndo not want to interfere with the current locale setting we thus emulate the\nbehavior in the way described above.

\n

To maintain compatibility with other platforms, not only the LANG\nvariable is tested, but a list of variables given as envvars parameter. The\nfirst found to be defined will be used. envvars defaults to the search path\nused in GNU gettext; it must always contain the variable name LANG. The GNU\ngettext search path contains 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', and\n'LANG', in that order.

\n

Except for the code 'C', the language code corresponds to RFC 1766.\nlanguage code and encoding may be None if their values cannot be\ndetermined.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nlocale.getlocale([category])
\n

Returns the current setting for the given locale category as sequence containing\nlanguage code, encoding. category may be one of the LC_* values\nexcept LC_ALL. It defaults to LC_CTYPE.

\n

Except for the code 'C', the language code corresponds to RFC 1766.\nlanguage code and encoding may be None if their values cannot be\ndetermined.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nlocale.getpreferredencoding([do_setlocale])
\n

Return the encoding used for text data, according to user preferences. User\npreferences are expressed differently on different systems, and might not be\navailable programmatically on some systems, so this function only returns a\nguess.

\n

On some systems, it is necessary to invoke setlocale() to obtain the user\npreferences, so this function is not thread-safe. If invoking setlocale is not\nnecessary or desired, do_setlocale should be set to False.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nlocale.normalize(localename)
\n

Returns a normalized locale code for the given locale name. The returned locale\ncode is formatted for use with setlocale(). If normalization fails, the\noriginal name is returned unchanged.

\n

If the given encoding is not known, the function defaults to the default\nencoding for the locale code just like setlocale().

\n

\nNew in version 2.0.

\n
\n\n
\n
\nlocale.resetlocale([category])
\n

Sets the locale for category to the default setting.

\n

The default setting is determined by calling getdefaultlocale().\ncategory defaults to LC_ALL.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nlocale.strcoll(string1, string2)
\n
Compares two strings according to the current LC_COLLATE setting. As\nany other compare function, returns a negative, or a positive value, or 0,\ndepending on whether string1 collates before or after string2 or is equal to\nit.
\n\n
\n
\nlocale.strxfrm(string)
\n

Transforms a string to one that can be used for the built-in function\ncmp(), and still returns locale-aware results. This function can be used\nwhen the same string is compared repeatedly, e.g. when collating a sequence of\nstrings.

\n
\n\n
\n
\nlocale.format(format, val[, grouping[, monetary]])
\n

Formats a number val according to the current LC_NUMERIC setting.\nThe format follows the conventions of the operator. For floating point\nvalues, the decimal point is modified if appropriate. If grouping is true,\nalso takes the grouping into account.

\n

If monetary is true, the conversion uses monetary thousands separator and\ngrouping strings.

\n

Please note that this function will only work for exactly one %char specifier.\nFor whole format strings, use format_string().

\n

\nChanged in version 2.5: Added the monetary parameter.

\n
\n\n
\n
\nlocale.format_string(format, val[, grouping])
\n

Processes formatting specifiers as in format val, but takes the current\nlocale settings into account.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nlocale.currency(val[, symbol[, grouping[, international]]])
\n

Formats a number val according to the current LC_MONETARY settings.

\n

The returned string includes the currency symbol if symbol is true, which is\nthe default. If grouping is true (which is not the default), grouping is done\nwith the value. If international is true (which is not the default), the\ninternational currency symbol is used.

\n

Note that this function will not work with the ‘C’ locale, so you have to set a\nlocale via setlocale() first.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nlocale.str(float)
\n
Formats a floating point number using the same format as the built-in function\nstr(float), but takes the decimal point into account.
\n\n
\n
\nlocale.atof(string)
\n
Converts a string to a floating point number, following the LC_NUMERIC\nsettings.
\n\n
\n
\nlocale.atoi(string)
\n
Converts a string to an integer, following the LC_NUMERIC conventions.
\n\n
\n
\nlocale.LC_CTYPE
\n

Locale category for the character type functions. Depending on the settings of\nthis category, the functions of module string dealing with case change\ntheir behaviour.

\n
\n\n
\n
\nlocale.LC_COLLATE
\n
Locale category for sorting strings. The functions strcoll() and\nstrxfrm() of the locale module are affected.
\n\n
\n
\nlocale.LC_TIME
\n
Locale category for the formatting of time. The function time.strftime()\nfollows these conventions.
\n\n
\n
\nlocale.LC_MONETARY
\n
Locale category for formatting of monetary values. The available options are\navailable from the localeconv() function.
\n\n
\n
\nlocale.LC_MESSAGES
\n
Locale category for message display. Python currently does not support\napplication specific locale-aware messages. Messages displayed by the operating\nsystem, like those returned by os.strerror() might be affected by this\ncategory.
\n\n
\n
\nlocale.LC_NUMERIC
\n
Locale category for formatting numbers. The functions format(),\natoi(), atof() and str() of the locale module are\naffected by that category. All other numeric formatting operations are not\naffected.
\n\n
\n
\nlocale.LC_ALL
\n
Combination of all locale settings. If this flag is used when the locale is\nchanged, setting the locale for all categories is attempted. If that fails for\nany category, no category is changed at all. When the locale is retrieved using\nthis flag, a string indicating the setting for all categories is returned. This\nstring can be later used to restore the settings.
\n\n
\n
\nlocale.CHAR_MAX
\n
This is a symbolic constant used for different values returned by\nlocaleconv().
\n\n

Example:

\n
>>> import locale\n>>> loc = locale.getlocale() # get current locale\n# use German locale; name might vary with platform\n>>> locale.setlocale(locale.LC_ALL, 'de_DE')\n>>> locale.strcoll('f\\xe4n', 'foo') # compare a string containing an umlaut\n>>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale\n>>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale\n>>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale\n
\n
\n
\n

22.2.1. Background, details, hints, tips and caveats

\n

The C standard defines the locale as a program-wide property that may be\nrelatively expensive to change. On top of that, some implementation are broken\nin such a way that frequent locale changes may cause core dumps. This makes the\nlocale somewhat painful to use correctly.

\n

Initially, when a program is started, the locale is the C locale, no matter\nwhat the user’s preferred locale is. The program must explicitly say that it\nwants the user’s preferred locale settings by calling setlocale(LC_ALL, '').

\n

It is generally a bad idea to call setlocale() in some library routine,\nsince as a side effect it affects the entire program. Saving and restoring it\nis almost as bad: it is expensive and affects other threads that happen to run\nbefore the settings have been restored.

\n

If, when coding a module for general use, you need a locale independent version\nof an operation that is affected by the locale (such as string.lower(), or\ncertain formats used with time.strftime()), you will have to find a way to\ndo it without using the standard library routine. Even better is convincing\nyourself that using locale settings is okay. Only as a last resort should you\ndocument that your module is not compatible with non-C locale settings.

\n

The case conversion functions in the string module are affected by the\nlocale settings. When a call to the setlocale() function changes the\nLC_CTYPE settings, the variables string.lowercase,\nstring.uppercase and string.letters are recalculated. Note that code\nthat uses these variable through ‘from ... import ...’,\ne.g. from string import letters, is not affected by subsequent\nsetlocale() calls.

\n

The only way to perform numeric operations according to the locale is to use the\nspecial functions defined by this module: atof(), atoi(),\nformat(), str().

\n
\n
\n

22.2.2. For extension writers and programs that embed Python

\n

Extension modules should never call setlocale(), except to find out what\nthe current locale is. But since the return value can only be used portably to\nrestore it, that is not very useful (except perhaps to find out whether or not\nthe locale is C).

\n

When Python code uses the locale module to change the locale, this also\naffects the embedding application. If the embedding application doesn’t want\nthis to happen, it should remove the _locale extension module (which does\nall the work) from the table of built-in modules in the config.c file,\nand make sure that the _locale module is not accessible as a shared\nlibrary.

\n
\n
\n

22.2.3. Access to message catalogs

\n

The locale module exposes the C library’s gettext interface on systems that\nprovide this interface. It consists of the functions gettext(),\ndgettext(), dcgettext(), textdomain(), bindtextdomain(),\nand bind_textdomain_codeset(). These are similar to the same functions in\nthe gettext module, but use the C library’s binary format for message\ncatalogs, and the C library’s search algorithms for locating message catalogs.

\n

Python applications should normally find no need to invoke these functions, and\nshould use gettext instead. A known exception to this rule are\napplications that link with additional C libraries which internally invoke\ngettext() or dcgettext(). For these applications, it may be\nnecessary to bind the text domain, so that the libraries can properly locate\ntheir message catalogs.

\n
\n
", "searchableItems": [ { "name": "locale.atof", "domId": "locale_locale.atof" }, { "name": "locale.atoi", "domId": "locale_locale.atoi" }, { "name": "locale.currency", "domId": "locale_locale.currency" }, { "name": "locale.format", "domId": "locale_locale.format" }, { "name": "locale.format_string", "domId": "locale_locale.format_string" }, { "name": "locale.getdefaultlocale", "domId": "locale_locale.getdefaultlocale" }, { "name": "locale.getlocale", "domId": "locale_locale.getlocale" }, { "name": "locale.getpreferredencoding", "domId": "locale_locale.getpreferredencoding" }, { "name": "locale.localeconv", "domId": "locale_locale.localeconv" }, { "name": "locale.nl_langinfo", "domId": "locale_locale.nl_langinfo" }, { "name": "locale.normalize", "domId": "locale_locale.normalize" }, { "name": "locale.resetlocale", "domId": "locale_locale.resetlocale" }, { "name": "locale.setlocale", "domId": "locale_locale.setlocale" }, { "name": "locale.str", "domId": "locale_locale.str" }, { "name": "locale.strcoll", "domId": "locale_locale.strcoll" }, { "name": "locale.strxfrm", "domId": "locale_locale.strxfrm" } ] }, { "url": "http://docs.python.org/library/shlex.html", "title": "shlex", "html": "
\n

23.2. shlex — Simple lexical analysis

\n

\nNew in version 1.5.2.

\n

Source code: Lib/shlex.py

\n
\n

The shlex class makes it easy to write lexical analyzers for simple\nsyntaxes resembling that of the Unix shell. This will often be useful for\nwriting minilanguages, (for example, in run control files for Python\napplications) or for parsing quoted strings.

\n

Prior to Python 2.7.3, this module did not support Unicode input.

\n

The shlex module defines the following functions:

\n
\n
\nshlex.split(s[, comments[, posix]])
\n

Split the string s using shell-like syntax. If comments is False\n(the default), the parsing of comments in the given string will be disabled\n(setting the commenters attribute of the shlex instance to\nthe empty string). This function operates in POSIX mode by default, but uses\nnon-POSIX mode if the posix argument is false.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.6: Added the posix parameter.

\n
\n

Note

\n

Since the split() function instantiates a shlex instance, passing\nNone for s will read the string to split from standard input.

\n
\n
\n\n

The shlex module defines the following class:

\n
\n
\nclass shlex.shlex([instream[, infile[, posix]]])
\n
A shlex instance or subclass instance is a lexical analyzer object.\nThe initialization argument, if present, specifies where to read characters\nfrom. It must be a file-/stream-like object with read() and\nreadline() methods, or a string (strings are accepted since Python 2.3).\nIf no argument is given, input will be taken from sys.stdin. The second\noptional argument is a filename string, which sets the initial value of the\ninfile attribute. If the instream argument is omitted or equal to\nsys.stdin, this second argument defaults to “stdin”. The posix argument\nwas introduced in Python 2.3, and defines the operational mode. When posix is\nnot true (default), the shlex instance will operate in compatibility\nmode. When operating in POSIX mode, shlex will try to be as close as\npossible to the POSIX shell parsing rules.
\n\n
\n

See also

\n
\n
Module ConfigParser
\n
Parser for configuration files similar to the Windows .ini files.
\n
\n
\n
\n

23.2.1. shlex Objects

\n

A shlex instance has the following methods:

\n
\n
\nshlex.get_token()
\n
Return a token. If tokens have been stacked using push_token(), pop a\ntoken off the stack. Otherwise, read one from the input stream. If reading\nencounters an immediate end-of-file, self.eof is returned (the empty\nstring ('') in non-POSIX mode, and None in POSIX mode).
\n\n
\n
\nshlex.push_token(str)
\n
Push the argument onto the token stack.
\n\n
\n
\nshlex.read_token()
\n
Read a raw token. Ignore the pushback stack, and do not interpret source\nrequests. (This is not ordinarily a useful entry point, and is documented here\nonly for the sake of completeness.)
\n\n
\n
\nshlex.sourcehook(filename)
\n

When shlex detects a source request (see source below) this\nmethod is given the following token as argument, and expected to return a tuple\nconsisting of a filename and an open file-like object.

\n

Normally, this method first strips any quotes off the argument. If the result\nis an absolute pathname, or there was no previous source request in effect, or\nthe previous source was a stream (such as sys.stdin), the result is left\nalone. Otherwise, if the result is a relative pathname, the directory part of\nthe name of the file immediately before it on the source inclusion stack is\nprepended (this behavior is like the way the C preprocessor handles #include\n"file.h").

\n

The result of the manipulations is treated as a filename, and returned as the\nfirst component of the tuple, with open() called on it to yield the second\ncomponent. (Note: this is the reverse of the order of arguments in instance\ninitialization!)

\n

This hook is exposed so that you can use it to implement directory search paths,\naddition of file extensions, and other namespace hacks. There is no\ncorresponding ‘close’ hook, but a shlex instance will call the close()\nmethod of the sourced input stream when it returns EOF.

\n

For more explicit control of source stacking, use the push_source() and\npop_source() methods.

\n
\n\n
\n
\nshlex.push_source(stream[, filename])
\n

Push an input source stream onto the input stack. If the filename argument is\nspecified it will later be available for use in error messages. This is the\nsame method used internally by the sourcehook() method.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nshlex.pop_source()
\n

Pop the last-pushed input source from the input stack. This is the same method\nused internally when the lexer reaches EOF on a stacked input stream.

\n

\nNew in version 2.1.

\n
\n\n
\n
\nshlex.error_leader([file[, line]])
\n

This method generates an error message leader in the format of a Unix C compiler\nerror label; the format is '"%s", line %d: ', where the %s is replaced\nwith the name of the current source file and the %d with the current input\nline number (the optional arguments can be used to override these).

\n

This convenience is provided to encourage shlex users to generate error\nmessages in the standard, parseable format understood by Emacs and other Unix\ntools.

\n
\n\n

Instances of shlex subclasses have some public instance variables which\neither control lexical analysis or can be used for debugging:

\n
\n
\nshlex.commenters
\n
The string of characters that are recognized as comment beginners. All\ncharacters from the comment beginner to end of line are ignored. Includes just\n'#' by default.
\n\n
\n
\nshlex.wordchars
\n
The string of characters that will accumulate into multi-character tokens. By\ndefault, includes all ASCII alphanumerics and underscore.
\n\n
\n
\nshlex.whitespace
\n
Characters that will be considered whitespace and skipped. Whitespace bounds\ntokens. By default, includes space, tab, linefeed and carriage-return.
\n\n
\n
\nshlex.escape
\n

Characters that will be considered as escape. This will be only used in POSIX\nmode, and includes just '\\' by default.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nshlex.quotes
\n
Characters that will be considered string quotes. The token accumulates until\nthe same quote is encountered again (thus, different quote types protect each\nother as in the shell.) By default, includes ASCII single and double quotes.
\n\n
\n
\nshlex.escapedquotes
\n

Characters in quotes that will interpret escape characters defined in\nescape. This is only used in POSIX mode, and includes just '"' by\ndefault.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nshlex.whitespace_split
\n

If True, tokens will only be split in whitespaces. This is useful, for\nexample, for parsing command lines with shlex, getting tokens in a\nsimilar way to shell arguments.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nshlex.infile
\n
The name of the current input file, as initially set at class instantiation time\nor stacked by later source requests. It may be useful to examine this when\nconstructing error messages.
\n\n
\n
\nshlex.instream
\n
The input stream from which this shlex instance is reading characters.
\n\n
\n
\nshlex.source
\n
This attribute is None by default. If you assign a string to it, that\nstring will be recognized as a lexical-level inclusion request similar to the\nsource keyword in various shells. That is, the immediately following token\nwill opened as a filename and input taken from that stream until EOF, at which\npoint the close() method of that stream will be called and the input\nsource will again become the original input stream. Source requests may be\nstacked any number of levels deep.
\n\n
\n
\nshlex.debug
\n
If this attribute is numeric and 1 or more, a shlex instance will\nprint verbose progress output on its behavior. If you need to use this, you can\nread the module source code to learn the details.
\n\n
\n
\nshlex.lineno
\n
Source line number (count of newlines seen so far plus one).
\n\n
\n
\nshlex.token
\n
The token buffer. It may be useful to examine this when catching exceptions.
\n\n
\n
\nshlex.eof
\n

Token used to determine end of file. This will be set to the empty string\n(''), in non-POSIX mode, and to None in POSIX mode.

\n

\nNew in version 2.3.

\n
\n\n
\n
\n

23.2.2. Parsing Rules

\n

When operating in non-POSIX mode, shlex will try to obey to the\nfollowing rules.

\n\n

When operating in POSIX mode, shlex will try to obey to the following\nparsing rules.

\n\n
\n
", "searchableItems": [ { "name": "shlex.shlex", "domId": "shlex_shlex.shlex" }, { "name": "shlex.shlex.error_leader", "domId": "shlex_shlex.shlex.error_leader" }, { "name": "shlex.shlex.get_token", "domId": "shlex_shlex.shlex.get_token" }, { "name": "shlex.shlex.pop_source", "domId": "shlex_shlex.shlex.pop_source" }, { "name": "shlex.shlex.push_source", "domId": "shlex_shlex.shlex.push_source" }, { "name": "shlex.shlex.push_token", "domId": "shlex_shlex.shlex.push_token" }, { "name": "shlex.shlex.read_token", "domId": "shlex_shlex.shlex.read_token" }, { "name": "shlex.shlex.sourcehook", "domId": "shlex_shlex.shlex.sourcehook" }, { "name": "shlex.split", "domId": "shlex_shlex.split" } ] }, { "url": "http://docs.python.org/library/scrolledtext.html", "title": "ScrolledText", "html": "
\n

24.4. ScrolledText — Scrolled Text Widget

\n

Platforms: Tk

\n

The ScrolledText module provides a class of the same name which\nimplements a basic text widget which has a vertical scroll bar configured to do\nthe “right thing.” Using the ScrolledText class is a lot easier than\nsetting up a text widget and scroll bar directly. The constructor is the same\nas that of the Tkinter.Text class.

\n
\n

Note

\n

ScrolledText has been renamed to tkinter.scrolledtext in Python\n3.0. The 2to3 tool will automatically adapt imports when converting\nyour sources to 3.0.

\n
\n

The text widget and scrollbar are packed together in a Frame, and the\nmethods of the Grid and Pack geometry managers are acquired\nfrom the Frame object. This allows the ScrolledText widget to\nbe used directly to achieve most normal geometry management behavior.

\n

Should more specific control be necessary, the following attributes are\navailable:

\n
\n
\nScrolledText.frame
\n
The frame which surrounds the text and scroll bar widgets.
\n\n
\n
\nScrolledText.vbar
\n
The scroll bar widget.
\n\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/tkinter.html", "title": "Tkinter", "html": "
\n

24.1. Tkinter — Python interface to Tcl/Tk

\n

The Tkinter module (“Tk interface”) is the standard Python interface to\nthe Tk GUI toolkit. Both Tk and Tkinter are available on most Unix\nplatforms, as well as on Windows systems. (Tk itself is not part of Python; it\nis maintained at ActiveState.)

\n
\n

Note

\n

Tkinter has been renamed to tkinter in Python 3.0. The\n2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n
\n

See also

\n
\n
Python Tkinter Resources
\n
The Python Tkinter Topic Guide provides a great deal of information on using Tk\nfrom Python and links to other sources of information on Tk.
\n
An Introduction to Tkinter
\n
Fredrik Lundh’s on-line reference material.
\n
Tkinter reference: a GUI for Python
\n
On-line reference material.
\n
Python and Tkinter Programming
\n
The book by John Grayson (ISBN 1-884777-81-3).
\n
\n
\n
\n

24.1.1. Tkinter Modules

\n

Most of the time, the Tkinter module is all you really need, but a number\nof additional modules are available as well. The Tk interface is located in a\nbinary module named _tkinter. This module contains the low-level\ninterface to Tk, and should never be used directly by application programmers.\nIt is usually a shared library (or DLL), but might in some cases be statically\nlinked with the Python interpreter.

\n

In addition to the Tk interface module, Tkinter includes a number of\nPython modules. The two most important modules are the Tkinter module\nitself, and a module called Tkconstants. The former automatically imports\nthe latter, so to use Tkinter, all you need to do is to import one module:

\n
import Tkinter\n
\n
\n

Or, more often:

\n
from Tkinter import *\n
\n
\n
\n
\nclass Tkinter.Tk(screenName=None, baseName=None, className='Tk', useTk=1)
\n

The Tk class is instantiated without arguments. This creates a toplevel\nwidget of Tk which usually is the main window of an application. Each instance\nhas its own associated Tcl interpreter.

\n

\nChanged in version 2.4: The useTk parameter was added.

\n
\n\n
\n
\nTkinter.Tcl(screenName=None, baseName=None, className='Tk', useTk=0)
\n

The Tcl() function is a factory function which creates an object much like\nthat created by the Tk class, except that it does not initialize the Tk\nsubsystem. This is most often useful when driving the Tcl interpreter in an\nenvironment where one doesn’t want to create extraneous toplevel windows, or\nwhere one cannot (such as Unix/Linux systems without an X server). An object\ncreated by the Tcl() object can have a Toplevel window created (and the Tk\nsubsystem initialized) by calling its loadtk() method.

\n

\nNew in version 2.4.

\n
\n\n

Other modules that provide Tk support include:

\n
\n
ScrolledText
\n
Text widget with a vertical scroll bar built in.
\n
tkColorChooser
\n
Dialog to let the user choose a color.
\n
tkCommonDialog
\n
Base class for the dialogs defined in the other modules listed here.
\n
tkFileDialog
\n
Common dialogs to allow the user to specify a file to open or save.
\n
tkFont
\n
Utilities to help work with fonts.
\n
tkMessageBox
\n
Access to standard Tk dialog boxes.
\n
tkSimpleDialog
\n
Basic dialogs and convenience functions.
\n
Tkdnd
\n
Drag-and-drop support for Tkinter. This is experimental and should become\ndeprecated when it is replaced with the Tk DND.
\n
turtle
\n
Turtle graphics in a Tk window.
\n
\n

These have been renamed as well in Python 3.0; they were all made submodules of\nthe new tkinter package.

\n
\n
\n

24.1.2. Tkinter Life Preserver

\n

This section is not designed to be an exhaustive tutorial on either Tk or\nTkinter. Rather, it is intended as a stop gap, providing some introductory\norientation on the system.

\n

Credits:

\n\n
\n

24.1.2.1. How To Use This Section

\n

This section is designed in two parts: the first half (roughly) covers\nbackground material, while the second half can be taken to the keyboard as a\nhandy reference.

\n

When trying to answer questions of the form “how do I do blah”, it is often best\nto find out how to do”blah” in straight Tk, and then convert this back into the\ncorresponding Tkinter call. Python programmers can often guess at the\ncorrect Python command by looking at the Tk documentation. This means that in\norder to use Tkinter, you will have to know a little bit about Tk. This document\ncan’t fulfill that role, so the best we can do is point you to the best\ndocumentation that exists. Here are some hints:

\n
    \n
  • The authors strongly suggest getting a copy of the Tk man pages. Specifically,\nthe man pages in the mann directory are most useful. The man3 man pages\ndescribe the C interface to the Tk library and thus are not especially helpful\nfor script writers.
  • \n
  • Addison-Wesley publishes a book called Tcl and the Tk Toolkit by John\nOusterhout (ISBN 0-201-63337-X) which is a good introduction to Tcl and Tk for\nthe novice. The book is not exhaustive, and for many details it defers to the\nman pages.
  • \n
  • Tkinter.py is a last resort for most, but can be a good place to go\nwhen nothing else makes sense.
  • \n
\n
\n

See also

\n
\n
ActiveState Tcl Home Page
\n
The Tk/Tcl development is largely taking place at ActiveState.
\n
Tcl and the Tk Toolkit
\n
The book by John Ousterhout, the inventor of Tcl .
\n
Practical Programming in Tcl and Tk
\n
Brent Welch’s encyclopedic book.
\n
\n
\n
\n
\n

24.1.2.2. A Simple Hello World Program

\n
from Tkinter import *\n\nclass Application(Frame):\n    def say_hi(self):\n        print "hi there, everyone!"\n\n    def createWidgets(self):\n        self.QUIT = Button(self)\n        self.QUIT["text"] = "QUIT"\n        self.QUIT["fg"]   = "red"\n        self.QUIT["command"] =  self.quit\n\n        self.QUIT.pack({"side": "left"})\n\n        self.hi_there = Button(self)\n        self.hi_there["text"] = "Hello",\n        self.hi_there["command"] = self.say_hi\n\n        self.hi_there.pack({"side": "left"})\n\n    def __init__(self, master=None):\n        Frame.__init__(self, master)\n        self.pack()\n        self.createWidgets()\n\nroot = Tk()\napp = Application(master=root)\napp.mainloop()\nroot.destroy()\n
\n
\n
\n
\n
\n

24.1.3. A (Very) Quick Look at Tcl/Tk

\n

The class hierarchy looks complicated, but in actual practice, application\nprogrammers almost always refer to the classes at the very bottom of the\nhierarchy.

\n

Notes:

\n\n

To make use of this reference material, there will be times when you will need\nto know how to read short passages of Tk and how to identify the various parts\nof a Tk command. (See section Mapping Basic Tk into Tkinter for the\nTkinter equivalents of what’s below.)

\n

Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are just lists\nof tokens separated by spaces. A Tk widget is just its class, the options\nthat help configure it, and the actions that make it do useful things.

\n

To make a widget in Tk, the command is always of the form:

\n
classCommand newPathname options
\n
\n
\n
classCommand
\n
denotes which kind of widget to make (a button, a label, a menu...)
\n
newPathname
\n
is the new name for this widget. All names in Tk must be unique. To help\nenforce this, widgets in Tk are named with pathnames, just like files in a\nfile system. The top level widget, the root, is called . (period) and\nchildren are delimited by more periods. For example,\n.myApp.controlPanel.okButton might be the name of a widget.
\n
options
\n
configure the widget’s appearance and in some cases, its behavior. The options\ncome in the form of a list of flags and values. Flags are preceded by a ‘-‘,\nlike Unix shell command flags, and values are put in quotes if they are more\nthan one word.
\n
\n

For example:

\n
button   .fred   -fg red -text \"hi there\"\n   ^       ^     \\_____________________/\n   |       |                |\n class    new            options\ncommand  widget  (-opt val -opt val ...)
\n
\n

Once created, the pathname to the widget becomes a new command. This new\nwidget command is the programmer’s handle for getting the new widget to\nperform some action. In C, you’d express this as someAction(fred,\nsomeOptions), in C++, you would express this as fred.someAction(someOptions),\nand in Tk, you say:

\n
.fred someAction someOptions
\n
\n

Note that the object name, .fred, starts with a dot.

\n

As you’d expect, the legal values for someAction will depend on the widget’s\nclass: .fred disable works if fred is a button (fred gets greyed out), but\ndoes not work if fred is a label (disabling of labels is not supported in Tk).

\n

The legal values of someOptions is action dependent. Some actions, like\ndisable, require no arguments, others, like a text-entry box’s delete\ncommand, would need arguments to specify what range of text to delete.

\n
\n
\n

24.1.4. Mapping Basic Tk into Tkinter

\n

Class commands in Tk correspond to class constructors in Tkinter.

\n
button .fred                =====>  fred = Button()
\n
\n

The master of an object is implicit in the new name given to it at creation\ntime. In Tkinter, masters are specified explicitly.

\n
button .panel.fred          =====>  fred = Button(panel)
\n
\n

The configuration options in Tk are given in lists of hyphened tags followed by\nvalues. In Tkinter, options are specified as keyword-arguments in the instance\nconstructor, and keyword-args for configure calls or as instance indices, in\ndictionary style, for established instances. See section\nSetting Options on setting options.

\n
button .fred -fg red        =====>  fred = Button(panel, fg = \"red\")\n.fred configure -fg red     =====>  fred[\"fg\"] = red\n                            OR ==>  fred.config(fg = \"red\")
\n
\n

In Tk, to perform an action on a widget, use the widget name as a command, and\nfollow it with an action name, possibly with arguments (options). In Tkinter,\nyou call methods on the class instance to invoke actions on the widget. The\nactions (methods) that a given widget can perform are listed in the Tkinter.py\nmodule.

\n
.fred invoke                =====>  fred.invoke()
\n
\n

To give a widget to the packer (geometry manager), you call pack with optional\narguments. In Tkinter, the Pack class holds all this functionality, and the\nvarious forms of the pack command are implemented as methods. All widgets in\nTkinter are subclassed from the Packer, and so inherit all the packing\nmethods. See the Tix module documentation for additional information on\nthe Form geometry manager.

\n
pack .fred -side left       =====>  fred.pack(side = \"left\")
\n
\n
\n
\n

24.1.5. How Tk and Tkinter are Related

\n

From the top down:

\n
\n
Your App Here (Python)
\n
A Python application makes a Tkinter call.
\n
Tkinter (Python Module)
\n
This call (say, for example, creating a button widget), is implemented in the\nTkinter module, which is written in Python. This Python function will parse\nthe commands and the arguments and convert them into a form that makes them look\nas if they had come from a Tk script instead of a Python script.
\n
tkinter (C)
\n
These commands and their arguments will be passed to a C function in the\ntkinter - note the lowercase - extension module.
\n
Tk Widgets (C and Tcl)
\n
This C function is able to make calls into other C modules, including the C\nfunctions that make up the Tk library. Tk is implemented in C and some Tcl.\nThe Tcl part of the Tk widgets is used to bind certain default behaviors to\nwidgets, and is executed once at the point where the Python Tkinter\nmodule is imported. (The user never sees this stage).
\n
Tk (C)
\n
The Tk part of the Tk Widgets implement the final mapping to ...
\n
Xlib (C)
\n
the Xlib library to draw graphics on the screen.
\n
\n
\n
\n

24.1.6. Handy Reference

\n
\n

24.1.6.1. Setting Options

\n

Options control things like the color and border width of a widget. Options can\nbe set in three ways:

\n
\n
At object creation time, using keyword arguments
\n
fred = Button(self, fg = "red", bg = "blue")\n
\n
\n
\n
After object creation, treating the option name like a dictionary index
\n
fred["fg"] = "red"\nfred["bg"] = "blue"\n
\n
\n
\n
Use the config() method to update multiple attrs subsequent to object creation
\n
fred.config(fg = "red", bg = "blue")\n
\n
\n
\n
\n

For a complete explanation of a given option and its behavior, see the Tk man\npages for the widget in question.

\n

Note that the man pages list “STANDARD OPTIONS” and “WIDGET SPECIFIC OPTIONS”\nfor each widget. The former is a list of options that are common to many\nwidgets, the latter are the options that are idiosyncratic to that particular\nwidget. The Standard Options are documented on the options(3) man\npage.

\n

No distinction between standard and widget-specific options is made in this\ndocument. Some options don’t apply to some kinds of widgets. Whether a given\nwidget responds to a particular option depends on the class of the widget;\nbuttons have a command option, labels do not.

\n

The options supported by a given widget are listed in that widget’s man page, or\ncan be queried at runtime by calling the config() method without\narguments, or by calling the keys() method on that widget. The return\nvalue of these calls is a dictionary whose key is the name of the option as a\nstring (for example, 'relief') and whose values are 5-tuples.

\n

Some options, like bg are synonyms for common options with long names\n(bg is shorthand for “background”). Passing the config() method the name\nof a shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple passed\nback will contain the name of the synonym and the “real” option (such as\n('bg', 'background')).

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
IndexMeaningExample
0option name'relief'
1option name for database lookup'relief'
2option class for database\nlookup'Relief'
3default value'raised'
4current value'groove'
\n

Example:

\n
>>> print fred.config()\n{'relief' : ('relief', 'relief', 'Relief', 'raised', 'groove')}\n
\n
\n

Of course, the dictionary printed will include all the options available and\ntheir values. This is meant only as an example.

\n
\n
\n

24.1.6.2. The Packer

\n

The packer is one of Tk’s geometry-management mechanisms. Geometry managers\nare used to specify the relative positioning of the positioning of widgets\nwithin their container - their mutual master. In contrast to the more\ncumbersome placer (which is used less commonly, and we do not cover here), the\npacker takes qualitative relationship specification - above, to the left of,\nfilling, etc - and works everything out to determine the exact placement\ncoordinates for you.

\n

The size of any master widget is determined by the size of the “slave widgets”\ninside. The packer is used to control where slave widgets appear inside the\nmaster into which they are packed. You can pack widgets into frames, and frames\ninto other frames, in order to achieve the kind of layout you desire.\nAdditionally, the arrangement is dynamically adjusted to accommodate incremental\nchanges to the configuration, once it is packed.

\n

Note that widgets do not appear until they have had their geometry specified\nwith a geometry manager. It’s a common early mistake to leave out the geometry\nspecification, and then be surprised when the widget is created but nothing\nappears. A widget will appear only after it has had, for example, the packer’s\npack() method applied to it.

\n

The pack() method can be called with keyword-option/value pairs that control\nwhere the widget is to appear within its container, and how it is to behave when\nthe main application window is resized. Here are some examples:

\n
fred.pack()                     # defaults to side = "top"\nfred.pack(side = "left")\nfred.pack(expand = 1)\n
\n
\n
\n
\n

24.1.6.3. Packer Options

\n

For more extensive information on the packer and the options that it can take,\nsee the man pages and page 183 of John Ousterhout’s book.

\n
\n
anchor
\n
Anchor type. Denotes where the packer is to place each slave in its parcel.
\n
expand
\n
Boolean, 0 or 1.
\n
fill
\n
Legal values: 'x', 'y', 'both', 'none'.
\n
ipadx and ipady
\n
A distance - designating internal padding on each side of the slave widget.
\n
padx and pady
\n
A distance - designating external padding on each side of the slave widget.
\n
side
\n
Legal values are: 'left', 'right', 'top', 'bottom'.
\n
\n
\n
\n

24.1.6.4. Coupling Widget Variables

\n

The current-value setting of some widgets (like text entry widgets) can be\nconnected directly to application variables by using special options. These\noptions are variable, textvariable, onvalue, offvalue, and\nvalue. This connection works both ways: if the variable changes for any\nreason, the widget it’s connected to will be updated to reflect the new value.

\n

Unfortunately, in the current implementation of Tkinter it is not\npossible to hand over an arbitrary Python variable to a widget through a\nvariable or textvariable option. The only kinds of variables for which\nthis works are variables that are subclassed from a class called Variable,\ndefined in the Tkinter module.

\n

There are many useful subclasses of Variable already defined:\nStringVar, IntVar, DoubleVar, and\nBooleanVar. To read the current value of such a variable, call the\nget() method on it, and to change its value you call the set()\nmethod. If you follow this protocol, the widget will always track the value of\nthe variable, with no further intervention on your part.

\n

For example:

\n
class App(Frame):\n    def __init__(self, master=None):\n        Frame.__init__(self, master)\n        self.pack()\n\n        self.entrythingy = Entry()\n        self.entrythingy.pack()\n\n        # here is the application variable\n        self.contents = StringVar()\n        # set it to some value\n        self.contents.set("this is a variable")\n        # tell the entry widget to watch this variable\n        self.entrythingy["textvariable"] = self.contents\n\n        # and here we get a callback when the user hits return.\n        # we will have the program print out the value of the\n        # application variable when the user hits return\n        self.entrythingy.bind('<Key-Return>',\n                              self.print_contents)\n\n    def print_contents(self, event):\n        print "hi. contents of entry is now ---->", \\\n              self.contents.get()\n
\n
\n
\n
\n

24.1.6.5. The Window Manager

\n

In Tk, there is a utility command, wm, for interacting with the window\nmanager. Options to the wm command allow you to control things like titles,\nplacement, icon bitmaps, and the like. In Tkinter, these commands have\nbeen implemented as methods on the Wm class. Toplevel widgets are\nsubclassed from the Wm class, and so can call the Wm methods\ndirectly.

\n

To get at the toplevel window that contains a given widget, you can often just\nrefer to the widget’s master. Of course if the widget has been packed inside of\na frame, the master won’t represent a toplevel window. To get at the toplevel\nwindow that contains an arbitrary widget, you can call the _root() method.\nThis method begins with an underscore to denote the fact that this function is\npart of the implementation, and not an interface to Tk functionality.

\n

Here are some examples of typical usage:

\n
from Tkinter import *\nclass App(Frame):\n    def __init__(self, master=None):\n        Frame.__init__(self, master)\n        self.pack()\n\n\n# create the application\nmyapp = App()\n\n#\n# here are method calls to the window manager class\n#\nmyapp.master.title("My Do-Nothing Application")\nmyapp.master.maxsize(1000, 400)\n\n# start the program\nmyapp.mainloop()\n
\n
\n
\n
\n

24.1.6.6. Tk Option Data Types

\n
\n
anchor
\n
Legal values are points of the compass: "n", "ne", "e", "se",\n"s", "sw", "w", "nw", and also "center".
\n
bitmap
\n
There are eight built-in, named bitmaps: 'error', 'gray25',\n'gray50', 'hourglass', 'info', 'questhead', 'question',\n'warning'. To specify an X bitmap filename, give the full path to the file,\npreceded with an @, as in "@/usr/contrib/bitmap/gumby.bit".
\n
boolean
\n
You can pass integers 0 or 1 or the strings "yes" or "no" .
\n
callback
\n

This is any Python function that takes no arguments. For example:

\n
def print_it():\n        print "hi there"\nfred["command"] = print_it\n
\n
\n
\n
color
\n
Colors can be given as the names of X colors in the rgb.txt file, or as strings\nrepresenting RGB values in 4 bit: "#RGB", 8 bit: "#RRGGBB", 12 bit”\n"#RRRGGGBBB", or 16 bit "#RRRRGGGGBBBB" ranges, where R,G,B here\nrepresent any legal hex digit. See page 160 of Ousterhout’s book for details.
\n
cursor
\n
The standard X cursor names from cursorfont.h can be used, without the\nXC_ prefix. For example to get a hand cursor (XC_hand2), use the\nstring "hand2". You can also specify a bitmap and mask file of your own.\nSee page 179 of Ousterhout’s book.
\n
distance
\n
Screen distances can be specified in either pixels or absolute distances.\nPixels are given as numbers and absolute distances as strings, with the trailing\ncharacter denoting units: c for centimetres, i for inches, m for\nmillimetres, p for printer’s points. For example, 3.5 inches is expressed\nas "3.5i".
\n
font
\n
Tk uses a list font name format, such as {courier 10 bold}. Font sizes with\npositive numbers are measured in points; sizes with negative numbers are\nmeasured in pixels.
\n
geometry
\n
This is a string of the form widthxheight, where width and height are\nmeasured in pixels for most widgets (in characters for widgets displaying text).\nFor example: fred["geometry"] = "200x100".
\n
justify
\n
Legal values are the strings: "left", "center", "right", and\n"fill".
\n
region
\n
This is a string with four space-delimited elements, each of which is a legal\ndistance (see above). For example: "2 3 4 5" and "3i 2i 4.5i 2i" and\n"3c 2c 4c 10.43c" are all legal regions.
\n
relief
\n
Determines what the border style of a widget will be. Legal values are:\n"raised", "sunken", "flat", "groove", and "ridge".
\n
scrollcommand
\n
This is almost always the set() method of some scrollbar widget, but can\nbe any widget method that takes a single argument. Refer to the file\nDemo/tkinter/matt/canvas-with-scrollbars.py in the Python source\ndistribution for an example.
\n
wrap:
\n
Must be one of: "none", "char", or "word".
\n
\n
\n
\n

24.1.6.7. Bindings and Events

\n

The bind method from the widget command allows you to watch for certain events\nand to have a callback function trigger when that event type occurs. The form\nof the bind method is:

\n
def bind(self, sequence, func, add=''):
\n
\n

where:

\n
\n
sequence
\n
is a string that denotes the target kind of event. (See the bind man page and\npage 201 of John Ousterhout’s book for details).
\n
func
\n
is a Python function, taking one argument, to be invoked when the event occurs.\nAn Event instance will be passed as the argument. (Functions deployed this way\nare commonly known as callbacks.)
\n
add
\n
is optional, either '' or '+'. Passing an empty string denotes that\nthis binding is to replace any other bindings that this event is associated\nwith. Passing a '+' means that this function is to be added to the list\nof functions bound to this event type.
\n
\n

For example:

\n
def turnRed(self, event):\n    event.widget["activeforeground"] = "red"\n\nself.button.bind("<Enter>", self.turnRed)\n
\n
\n

Notice how the widget field of the event is being accessed in the\nturnRed() callback. This field contains the widget that caught the X\nevent. The following table lists the other event fields you can access, and how\nthey are denoted in Tk, which can be useful when referring to the Tk man pages.

\n
Tk      Tkinter Event Field             Tk      Tkinter Event Field\n--      -------------------             --      -------------------\n%f      focus                           %A      char\n%h      height                          %E      send_event\n%k      keycode                         %K      keysym\n%s      state                           %N      keysym_num\n%t      time                            %T      type\n%w      width                           %W      widget\n%x      x                               %X      x_root\n%y      y                               %Y      y_root
\n
\n
\n
\n

24.1.6.8. The index Parameter

\n

A number of widgets require”index” parameters to be passed. These are used to\npoint at a specific place in a Text widget, or to particular characters in an\nEntry widget, or to particular menu items in a Menu widget.

\n
\n
Entry widget indexes (index, view index, etc.)
\n

Entry widgets have options that refer to character positions in the text being\ndisplayed. You can use these Tkinter functions to access these special\npoints in text widgets:

\n
\n
AtEnd()
\n
refers to the last position in the text
\n
AtInsert()
\n
refers to the point where the text cursor is
\n
AtSelFirst()
\n
indicates the beginning point of the selected text
\n
AtSelLast()
\n
denotes the last point of the selected text and finally
\n
At(x[, y])
\n
refers to the character at pixel location x, y (with y not used in the\ncase of a text entry widget, which contains a single line of text).
\n
\n
\n
Text widget indexes
\n
The index notation for Text widgets is very rich and is best described in the Tk\nman pages.
\n
Menu indexes (menu.invoke(), menu.entryconfig(), etc.)
\n

Some options and methods for menus manipulate specific menu entries. Anytime a\nmenu index is needed for an option or a parameter, you may pass in:

\n
    \n
  • an integer which refers to the numeric position of the entry in the widget,\ncounted from the top, starting with 0;
  • \n
  • the string 'active', which refers to the menu position that is currently\nunder the cursor;
  • \n
  • the string "last" which refers to the last menu item;
  • \n
  • An integer preceded by @, as in @6, where the integer is interpreted\nas a y pixel coordinate in the menu’s coordinate system;
  • \n
  • the string "none", which indicates no menu entry at all, most often used\nwith menu.activate() to deactivate all entries, and finally,
  • \n
  • a text string that is pattern matched against the label of the menu entry, as\nscanned from the top of the menu to the bottom. Note that this index type is\nconsidered after all the others, which means that matches for menu items\nlabelled last, active, or none may be interpreted as the above\nliterals, instead.
  • \n
\n
\n
\n
\n
\n

24.1.6.9. Images

\n

Bitmap/Pixelmap images can be created through the subclasses of\nTkinter.Image:

\n
    \n
  • BitmapImage can be used for X11 bitmap data.
  • \n
  • PhotoImage can be used for GIF and PPM/PGM color bitmaps.
  • \n
\n

Either type of image is created through either the file or the data\noption (other options are available as well).

\n

The image object can then be used wherever an image option is supported by\nsome widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a\nreference to the image. When the last Python reference to the image object is\ndeleted, the image data is deleted as well, and Tk will display an empty box\nwherever the image was used.

\n
\n
\n
", "searchableItems": [ { "name": "Tkinter.Tcl", "domId": "Tkinter_Tkinter.Tcl" }, { "name": "Tkinter.Tk", "domId": "Tkinter_Tkinter.Tk" } ] }, { "url": "http://docs.python.org/library/tix.html", "title": "Tix", "html": "
\n

24.3. Tix — Extension widgets for Tk

\n

The Tix (Tk Interface Extension) module provides an additional rich set\nof widgets. Although the standard Tk library has many useful widgets, they are\nfar from complete. The Tix library provides most of the commonly needed\nwidgets that are missing from standard Tk: HList, ComboBox,\nControl (a.k.a. SpinBox) and an assortment of scrollable widgets.\nTix also includes many more widgets that are generally useful in a wide\nrange of applications: NoteBook, FileEntry,\nPanedWindow, etc; there are more than 40 of them.

\n

With all these new widgets, you can introduce new interaction techniques into\napplications, creating more useful and more intuitive user interfaces. You can\ndesign your application by choosing the most appropriate widgets to match the\nspecial needs of your application and users.

\n
\n

Note

\n

Tix has been renamed to tkinter.tix in Python 3.0. The\n2to3 tool will automatically adapt imports when converting your\nsources to 3.0.

\n
\n
\n

See also

\n
\n
Tix Homepage
\n
The home page for Tix. This includes links to additional documentation\nand downloads.
\n
Tix Man Pages
\n
On-line version of the man pages and reference material.
\n
Tix Programming Guide
\n
On-line version of the programmer’s reference material.
\n
Tix Development Applications
\n
Tix applications for development of Tix and Tkinter programs. Tide applications\nwork under Tk or Tkinter, and include TixInspect, an inspector to\nremotely modify and debug Tix/Tk/Tkinter applications.
\n
\n
\n
\n

24.3.1. Using Tix

\n
\n
\nclass Tix.Tix(screenName[, baseName[, className]])
\n

Toplevel widget of Tix which represents mostly the main window of an\napplication. It has an associated Tcl interpreter.

\n

Classes in the Tix module subclasses the classes in the Tkinter\nmodule. The former imports the latter, so to use Tix with Tkinter, all\nyou need to do is to import one module. In general, you can just import\nTix, and replace the toplevel call to Tkinter.Tk with\nTix.Tk:

\n
import Tix\nfrom Tkconstants import *\nroot = Tix.Tk()\n
\n
\n
\n\n

To use Tix, you must have the Tix widgets installed, usually\nalongside your installation of the Tk widgets. To test your installation, try\nthe following:

\n
import Tix\nroot = Tix.Tk()\nroot.tk.eval('package require Tix')\n
\n
\n

If this fails, you have a Tk installation problem which must be resolved before\nproceeding. Use the environment variable TIX_LIBRARY to point to the\ninstalled Tix library directory, and make sure you have the dynamic\nobject library (tix8183.dll or libtix8183.so) in the same\ndirectory that contains your Tk dynamic object library (tk8183.dll or\nlibtk8183.so). The directory with the dynamic object library should also\nhave a file called pkgIndex.tcl (case sensitive), which contains the\nline:

\n
package ifneeded Tix 8.1 [list load \"[file join $dir tix8183.dll]\" Tix]
\n
\n
\n
\n

24.3.2. Tix Widgets

\n

Tix\nintroduces over 40 widget classes to the Tkinter repertoire. There is a\ndemo of all the Tix widgets in the Demo/tix directory of the\nstandard distribution.

\n
\n

24.3.2.1. Basic Widgets

\n
\n
\nclass Tix.Balloon
\n
A Balloon that\npops up over a widget to provide help. When the user moves the cursor inside a\nwidget to which a Balloon widget has been bound, a small pop-up window with a\ndescriptive message will be shown on the screen.
\n\n
\n
\nclass Tix.ButtonBox
\n
The ButtonBox\nwidget creates a box of buttons, such as is commonly used for Ok Cancel.
\n\n
\n
\nclass Tix.ComboBox
\n
The ComboBox\nwidget is similar to the combo box control in MS Windows. The user can select a\nchoice by either typing in the entry subwdget or selecting from the listbox\nsubwidget.
\n\n
\n
\nclass Tix.Control
\n
The Control\nwidget is also known as the SpinBox widget. The user can adjust the\nvalue by pressing the two arrow buttons or by entering the value directly into\nthe entry. The new value will be checked against the user-defined upper and\nlower limits.
\n\n
\n
\nclass Tix.LabelEntry
\n
The LabelEntry\nwidget packages an entry widget and a label into one mega widget. It can be used\nbe used to simplify the creation of “entry-form” type of interface.
\n\n
\n
\nclass Tix.LabelFrame
\n
The LabelFrame\nwidget packages a frame widget and a label into one mega widget. To create\nwidgets inside a LabelFrame widget, one creates the new widgets relative to the\nframe subwidget and manage them inside the frame subwidget.
\n\n
\n
\nclass Tix.Meter
\n
The Meter widget\ncan be used to show the progress of a background job which may take a long time\nto execute.
\n\n
\n
\nclass Tix.OptionMenu
\n
The OptionMenu\ncreates a menu button of options.
\n\n
\n
\nclass Tix.PopupMenu
\n
The PopupMenu\nwidget can be used as a replacement of the tk_popup command. The advantage\nof the Tix PopupMenu widget is it requires less application code\nto manipulate.
\n\n
\n
\nclass Tix.Select
\n
The Select widget\nis a container of button subwidgets. It can be used to provide radio-box or\ncheck-box style of selection options for the user.
\n\n
\n
\nclass Tix.StdButtonBox
\n
The StdButtonBox\nwidget is a group of standard buttons for Motif-like dialog boxes.
\n\n
\n
\n

24.3.2.2. File Selectors

\n
\n
\nclass Tix.DirList
\n
The DirList\nwidget displays a list view of a directory, its previous directories and its\nsub-directories. The user can choose one of the directories displayed in the\nlist or change to another directory.
\n\n
\n
\nclass Tix.DirTree
\n
The DirTree\nwidget displays a tree view of a directory, its previous directories and its\nsub-directories. The user can choose one of the directories displayed in the\nlist or change to another directory.
\n\n
\n
\nclass Tix.DirSelectDialog
\n
The DirSelectDialog\nwidget presents the directories in the file system in a dialog window. The user\ncan use this dialog window to navigate through the file system to select the\ndesired directory.
\n\n
\n
\nclass Tix.DirSelectBox
\n
The DirSelectBox is similar to the standard Motif(TM)\ndirectory-selection box. It is generally used for the user to choose a\ndirectory. DirSelectBox stores the directories mostly recently selected into\na ComboBox widget so that they can be quickly selected again.
\n\n
\n
\nclass Tix.ExFileSelectBox
\n
The ExFileSelectBox\nwidget is usually embedded in a tixExFileSelectDialog widget. It provides an\nconvenient method for the user to select files. The style of the\nExFileSelectBox widget is very similar to the standard file dialog on\nMS Windows 3.1.
\n\n
\n
\nclass Tix.FileSelectBox
\n
The FileSelectBox\nis similar to the standard Motif(TM) file-selection box. It is generally used\nfor the user to choose a file. FileSelectBox stores the files mostly recently\nselected into a ComboBox widget so that they can be quickly selected\nagain.
\n\n
\n
\nclass Tix.FileEntry
\n
The FileEntry\nwidget can be used to input a filename. The user can type in the filename\nmanually. Alternatively, the user can press the button widget that sits next to\nthe entry, which will bring up a file selection dialog.
\n\n
\n
\n

24.3.2.3. Hierarchical ListBox

\n
\n
\nclass Tix.HList
\n
The HList widget\ncan be used to display any data that have a hierarchical structure, for example,\nfile system directory trees. The list entries are indented and connected by\nbranch lines according to their places in the hierarchy.
\n\n
\n
\nclass Tix.CheckList
\n
The CheckList\nwidget displays a list of items to be selected by the user. CheckList acts\nsimilarly to the Tk checkbutton or radiobutton widgets, except it is capable of\nhandling many more items than checkbuttons or radiobuttons.
\n\n
\n
\nclass Tix.Tree
\n
The Tree widget\ncan be used to display hierarchical data in a tree form. The user can adjust the\nview of the tree by opening or closing parts of the tree.
\n\n
\n
\n

24.3.2.4. Tabular ListBox

\n
\n
\nclass Tix.TList
\n
The TList widget\ncan be used to display data in a tabular format. The list entries of a\nTList widget are similar to the entries in the Tk listbox widget. The\nmain differences are (1) the TList widget can display the list entries\nin a two dimensional format and (2) you can use graphical images as well as\nmultiple colors and fonts for the list entries.
\n\n
\n
\n

24.3.2.5. Manager Widgets

\n
\n
\nclass Tix.PanedWindow
\n
The PanedWindow\nwidget allows the user to interactively manipulate the sizes of several panes.\nThe panes can be arranged either vertically or horizontally. The user changes\nthe sizes of the panes by dragging the resize handle between two panes.
\n\n
\n
\nclass Tix.ListNoteBook
\n
The ListNoteBook\nwidget is very similar to the TixNoteBook widget: it can be used to\ndisplay many windows in a limited space using a notebook metaphor. The notebook\nis divided into a stack of pages (windows). At one time only one of these pages\ncan be shown. The user can navigate through these pages by choosing the name of\nthe desired page in the hlist subwidget.
\n\n
\n
\nclass Tix.NoteBook
\n
The NoteBook\nwidget can be used to display many windows in a limited space using a notebook\nmetaphor. The notebook is divided into a stack of pages. At one time only one of\nthese pages can be shown. The user can navigate through these pages by choosing\nthe visual “tabs” at the top of the NoteBook widget.
\n\n
\n
\n

24.3.2.6. Image Types

\n

The Tix module adds:

\n
    \n
  • pixmap\ncapabilities to all Tix and Tkinter widgets to create color images\nfrom XPM files.
  • \n
  • Compound image\ntypes can be used to create images that consists of multiple horizontal lines;\neach line is composed of a series of items (texts, bitmaps, images or spaces)\narranged from left to right. For example, a compound image can be used to\ndisplay a bitmap and a text string simultaneously in a Tk Button\nwidget.
  • \n
\n
\n
\n

24.3.2.7. Miscellaneous Widgets

\n
\n
\nclass Tix.InputOnly
\n
The InputOnly\nwidgets are to accept inputs from the user, which can be done with the bind\ncommand (Unix only).
\n\n
\n
\n

24.3.2.8. Form Geometry Manager

\n

In addition, Tix augments Tkinter by providing:

\n
\n
\nclass Tix.Form
\n
The Form geometry\nmanager based on attachment rules for all Tk widgets.
\n\n
\n
\n
\n

24.3.3. Tix Commands

\n
\n
\nclass Tix.tixCommand
\n

The tix commands provide\naccess to miscellaneous elements of Tix‘s internal state and the\nTix application context. Most of the information manipulated by these\nmethods pertains to the application as a whole, or to a screen or display,\nrather than to a particular window.

\n

To view the current settings, the common usage is:

\n
import Tix\nroot = Tix.Tk()\nprint root.tix_configure()\n
\n
\n
\n\n
\n
\ntixCommand.tix_configure([cnf], **kw)
\n
Query or modify the configuration options of the Tix application context. If no\noption is specified, returns a dictionary all of the available options. If\noption is specified with no value, then the method returns a list describing the\none named option (this list will be identical to the corresponding sublist of\nthe value returned if no option is specified). If one or more option-value\npairs are specified, then the method modifies the given option(s) to have the\ngiven value(s); in this case the method returns an empty string. Option may be\nany of the configuration options.
\n\n
\n
\ntixCommand.tix_cget(option)
\n
Returns the current value of the configuration option given by option. Option\nmay be any of the configuration options.
\n\n
\n
\ntixCommand.tix_getbitmap(name)
\n
Locates a bitmap file of the name name.xpm or name in one of the bitmap\ndirectories (see the tix_addbitmapdir() method). By using\ntix_getbitmap(), you can avoid hard coding the pathnames of the bitmap\nfiles in your application. When successful, it returns the complete pathname of\nthe bitmap file, prefixed with the character @. The returned value can be\nused to configure the bitmap option of the Tk and Tix widgets.
\n\n
\n
\ntixCommand.tix_addbitmapdir(directory)
\n
Tix maintains a list of directories under which the tix_getimage() and\ntix_getbitmap() methods will search for image files. The standard bitmap\ndirectory is $TIX_LIBRARY/bitmaps. The tix_addbitmapdir() method\nadds directory into this list. By using this method, the image files of an\napplications can also be located using the tix_getimage() or\ntix_getbitmap() method.
\n\n
\n
\ntixCommand.tix_filedialog([dlgclass])
\n
Returns the file selection dialog that may be shared among different calls from\nthis application. This method will create a file selection dialog widget when\nit is called the first time. This dialog will be returned by all subsequent\ncalls to tix_filedialog(). An optional dlgclass parameter can be passed\nas a string to specified what type of file selection dialog widget is desired.\nPossible options are tix, FileSelectDialog or tixExFileSelectDialog.
\n\n
\n
\ntixCommand.tix_getimage(self, name)
\n
Locates an image file of the name name.xpm, name.xbm or\nname.ppm in one of the bitmap directories (see the\ntix_addbitmapdir() method above). If more than one file with the same name\n(but different extensions) exist, then the image type is chosen according to the\ndepth of the X display: xbm images are chosen on monochrome displays and color\nimages are chosen on color displays. By using tix_getimage(), you can\navoid hard coding the pathnames of the image files in your application. When\nsuccessful, this method returns the name of the newly created image, which can\nbe used to configure the image option of the Tk and Tix widgets.
\n\n
\n
\ntixCommand.tix_option_get(name)
\n
Gets the options maintained by the Tix scheme mechanism.
\n\n
\n
\ntixCommand.tix_resetoptions(newScheme, newFontSet[, newScmPrio])
\n

Resets the scheme and fontset of the Tix application to newScheme and\nnewFontSet, respectively. This affects only those widgets created after this\ncall. Therefore, it is best to call the resetoptions method before the creation\nof any widgets in a Tix application.

\n

The optional parameter newScmPrio can be given to reset the priority level of\nthe Tk options set by the Tix schemes.

\n

Because of the way Tk handles the X option database, after Tix has been has\nimported and inited, it is not possible to reset the color schemes and font sets\nusing the tix_config() method. Instead, the tix_resetoptions()\nmethod must be used.

\n
\n\n
\n
", "searchableItems": [ { "name": "Tix.Balloon", "domId": "Tix_Tix.Balloon" }, { "name": "Tix.ButtonBox", "domId": "Tix_Tix.ButtonBox" }, { "name": "Tix.CheckList", "domId": "Tix_Tix.CheckList" }, { "name": "Tix.ComboBox", "domId": "Tix_Tix.ComboBox" }, { "name": "Tix.Control", "domId": "Tix_Tix.Control" }, { "name": "Tix.DirList", "domId": "Tix_Tix.DirList" }, { "name": "Tix.DirSelectBox", "domId": "Tix_Tix.DirSelectBox" }, { "name": "Tix.DirSelectDialog", "domId": "Tix_Tix.DirSelectDialog" }, { "name": "Tix.DirTree", "domId": "Tix_Tix.DirTree" }, { "name": "Tix.ExFileSelectBox", "domId": "Tix_Tix.ExFileSelectBox" }, { "name": "Tix.FileEntry", "domId": "Tix_Tix.FileEntry" }, { "name": "Tix.FileSelectBox", "domId": "Tix_Tix.FileSelectBox" }, { "name": "Tix.Form", "domId": "Tix_Tix.Form" }, { "name": "Tix.HList", "domId": "Tix_Tix.HList" }, { "name": "Tix.InputOnly", "domId": "Tix_Tix.InputOnly" }, { "name": "Tix.LabelEntry", "domId": "Tix_Tix.LabelEntry" }, { "name": "Tix.LabelFrame", "domId": "Tix_Tix.LabelFrame" }, { "name": "Tix.ListNoteBook", "domId": "Tix_Tix.ListNoteBook" }, { "name": "Tix.Meter", "domId": "Tix_Tix.Meter" }, { "name": "Tix.NoteBook", "domId": "Tix_Tix.NoteBook" }, { "name": "Tix.OptionMenu", "domId": "Tix_Tix.OptionMenu" }, { "name": "Tix.PanedWindow", "domId": "Tix_Tix.PanedWindow" }, { "name": "Tix.PopupMenu", "domId": "Tix_Tix.PopupMenu" }, { "name": "Tix.Select", "domId": "Tix_Tix.Select" }, { "name": "Tix.StdButtonBox", "domId": "Tix_Tix.StdButtonBox" }, { "name": "Tix.Tix", "domId": "Tix_Tix.Tix" }, { "name": "Tix.tixCommand", "domId": "Tix_Tix.tixCommand" }, { "name": "Tix.tixCommand.tix_addbitmapdir", "domId": "Tix_Tix.tixCommand.tix_addbitmapdir" }, { "name": "Tix.tixCommand.tix_cget", "domId": "Tix_Tix.tixCommand.tix_cget" }, { "name": "Tix.tixCommand.tix_configure", "domId": "Tix_Tix.tixCommand.tix_configure" }, { "name": "Tix.tixCommand.tix_filedialog", "domId": "Tix_Tix.tixCommand.tix_filedialog" }, { "name": "Tix.tixCommand.tix_getbitmap", "domId": "Tix_Tix.tixCommand.tix_getbitmap" }, { "name": "Tix.tixCommand.tix_getimage", "domId": "Tix_Tix.tixCommand.tix_getimage" }, { "name": "Tix.tixCommand.tix_option_get", "domId": "Tix_Tix.tixCommand.tix_option_get" }, { "name": "Tix.tixCommand.tix_resetoptions", "domId": "Tix_Tix.tixCommand.tix_resetoptions" }, { "name": "Tix.TList", "domId": "Tix_Tix.TList" }, { "name": "Tix.Tree", "domId": "Tix_Tix.Tree" } ] }, { "url": "http://docs.python.org/library/idle.html", "title": "", "html": "
\n

24.6. IDLE

\n

IDLE is the Python IDE built with the tkinter GUI toolkit.

\n

IDLE has the following features:

\n\n
\n

24.6.1. Menus

\n
\n

24.6.1.1. File menu

\n
\n
New window
\n
create a new editing window
\n
Open...
\n
open an existing file
\n
Open module...
\n
open an existing module (searches sys.path)
\n
Class browser
\n
show classes and methods in current file
\n
Path browser
\n
show sys.path directories, modules, classes and methods
\n
\n
\n
Save
\n
save current window to the associated file (unsaved windows have a * before and\nafter the window title)
\n
Save As...
\n
save current window to new file, which becomes the associated file
\n
Save Copy As...
\n
save current window to different file without changing the associated file
\n
Close
\n
close current window (asks to save if unsaved)
\n
Exit
\n
close all windows and quit IDLE (asks to save if unsaved)
\n
\n
\n
\n

24.6.1.2. Edit menu

\n
\n
Undo
\n
Undo last change to current window (max 1000 changes)
\n
Redo
\n
Redo last undone change to current window
\n
Cut
\n
Copy selection into system-wide clipboard; then delete selection
\n
Copy
\n
Copy selection into system-wide clipboard
\n
Paste
\n
Insert system-wide clipboard into window
\n
Select All
\n
Select the entire contents of the edit buffer
\n
Find...
\n
Open a search dialog box with many options
\n
Find again
\n
Repeat last search
\n
Find selection
\n
Search for the string in the selection
\n
Find in Files...
\n
Open a search dialog box for searching files
\n
Replace...
\n
Open a search-and-replace dialog box
\n
Go to line
\n
Ask for a line number and show that line
\n
Indent region
\n
Shift selected lines right 4 spaces
\n
Dedent region
\n
Shift selected lines left 4 spaces
\n
Comment out region
\n
Insert ## in front of selected lines
\n
Uncomment region
\n
Remove leading # or ## from selected lines
\n
Tabify region
\n
Turns leading stretches of spaces into tabs
\n
Untabify region
\n
Turn all tabs into the right number of spaces
\n
Expand word
\n
Expand the word you have typed to match another word in the same buffer; repeat\nto get a different expansion
\n
Format Paragraph
\n
Reformat the current blank-line-separated paragraph
\n
Import module
\n
Import or reload the current module
\n
Run script
\n
Execute the current file in the __main__ namespace
\n
\n
\n
\n

24.6.1.3. Windows menu

\n
\n
Zoom Height
\n
toggles the window between normal size (24x80) and maximum height.
\n
\n

The rest of this menu lists the names of all open windows; select one to bring\nit to the foreground (deiconifying it if necessary).

\n
\n
\n

24.6.1.4. Debug menu (in the Python Shell window only)

\n
\n
Go to file/line
\n
look around the insert point for a filename and linenumber, open the file, and\nshow the line.
\n
Open stack viewer
\n
show the stack traceback of the last exception
\n
Debugger toggle
\n
Run commands in the shell under the debugger
\n
JIT Stack viewer toggle
\n
Open stack viewer on traceback
\n
\n
\n
\n
\n

24.6.2. Basic editing and navigation

\n\n
\n

24.6.2.1. Automatic indentation

\n

After a block-opening statement, the next line is indented by 4 spaces (in the\nPython Shell window by one tab). After certain keywords (break, return etc.)\nthe next line is dedented. In leading indentation, Backspace deletes up\nto 4 spaces if they are there. Tab inserts 1-4 spaces (in the Python\nShell window one tab). See also the indent/dedent region commands in the edit\nmenu.

\n
\n
\n

24.6.2.2. Python Shell window

\n
    \n
  • C-C interrupts executing command
  • \n
  • C-D sends end-of-file; closes window if typed at a >>> prompt
  • \n
  • Alt-p retrieves previous command matching what you have typed
  • \n
  • Alt-n retrieves next
  • \n
  • Return while on any previous command retrieves that command
  • \n
  • Alt-/ (Expand word) is also useful here
  • \n
\n
\n
\n
\n

24.6.3. Syntax colors

\n

The coloring is applied in a background “thread,” so you may occasionally see\nuncolorized text. To change the color scheme, edit the [Colors] section in\nconfig.txt.

\n
\n
Python syntax colors:
\n
\n
Keywords
\n
orange
\n
Strings
\n
green
\n
Comments
\n
red
\n
Definitions
\n
blue
\n
\n
\n
Shell colors:
\n
\n
Console output
\n
brown
\n
stdout
\n
blue
\n
stderr
\n
dark green
\n
stdin
\n
black
\n
\n
\n
\n
\n
\n

24.6.4. Startup

\n

Upon startup with the -s option, IDLE will execute the file referenced by\nthe environment variables IDLESTARTUP or PYTHONSTARTUP.\nIdle first checks for IDLESTARTUP; if IDLESTARTUP is present the file\nreferenced is run. If IDLESTARTUP is not present, Idle checks for\nPYTHONSTARTUP. Files referenced by these environment variables are\nconvenient places to store functions that are used frequently from the Idle\nshell, or for executing import statements to import common modules.

\n

In addition, Tk also loads a startup file if it is present. Note that the\nTk file is loaded unconditionally. This additional file is .Idle.py and is\nlooked for in the user’s home directory. Statements in this file will be\nexecuted in the Tk namespace, so this file is not useful for importing functions\nto be used from Idle’s Python shell.

\n
\n

24.6.4.1. Command line usage

\n
idle.py [-c command] [-d] [-e] [-s] [-t title] [arg] ...\n\n-c command  run this command\n-d          enable debugger\n-e          edit mode; arguments are files to be edited\n-s          run $IDLESTARTUP or $PYTHONSTARTUP first\n-t title    set title of shell window
\n
\n

If there are arguments:

\n
    \n
  1. If -e is used, arguments are files opened for editing and\nsys.argv reflects the arguments passed to IDLE itself.
  2. \n
  3. Otherwise, if -c is used, all arguments are placed in\nsys.argv[1:...], with sys.argv[0] set to '-c'.
  4. \n
  5. Otherwise, if neither -e nor -c is used, the first\nargument is a script which is executed with the remaining arguments in\nsys.argv[1:...] and sys.argv[0] set to the script name. If the script\nname is ‘-‘, no script is executed but an interactive Python session is started;\nthe arguments are still available in sys.argv.
  6. \n
\n
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/othergui.html", "title": "", "html": "
\n

24.7. Other Graphical User Interface Packages

\n

Major cross-platform (Windows, Mac OS X, Unix-like) GUI toolkits are\navailable for Python:

\n
\n

See also

\n
\n
PyGTK
\n
is a set of bindings for the GTK widget set. It\nprovides an object oriented interface that is slightly higher level than\nthe C one. It comes with many more widgets than Tkinter provides, and has\ngood Python-specific reference documentation. There are also bindings to\nGNOME. One well known PyGTK application is\nPythonCAD. An online tutorial is available.
\n
PyQt
\n
PyQt is a sip-wrapped binding to the Qt toolkit. Qt is an\nextensive C++ GUI application development framework that is\navailable for Unix, Windows and Mac OS X. sip is a tool\nfor generating bindings for C++ libraries as Python classes, and\nis specifically designed for Python. The PyQt3 bindings have a\nbook, GUI Programming with Python: QT Edition by Boudewijn\nRempt. The PyQt4 bindings also have a book, Rapid GUI Programming\nwith Python and Qt, by Mark\nSummerfield.
\n
wxPython
\n
wxPython is a cross-platform GUI toolkit for Python that is built around\nthe popular wxWidgets (formerly wxWindows)\nC++ toolkit. It provides a native look and feel for applications on\nWindows, Mac OS X, and Unix systems by using each platform’s native\nwidgets where ever possible, (GTK+ on Unix-like systems). In addition to\nan extensive set of widgets, wxPython provides classes for online\ndocumentation and context sensitive help, printing, HTML viewing,\nlow-level device context drawing, drag and drop, system clipboard access,\nan XML-based resource format and more, including an ever growing library\nof user-contributed modules. wxPython has a book, wxPython in Action, by Noel Rappin and\nRobin Dunn.
\n
\n
\n

PyGTK, PyQt, and wxPython, all have a modern look and feel and more\nwidgets than Tkinter. In addition, there are many other GUI toolkits for\nPython, both cross-platform, and platform-specific. See the GUI Programming page in the Python Wiki for a\nmuch more complete list, and also for links to documents where the\ndifferent GUI toolkits are compared.

\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/ttk.html", "title": "ttk", "html": "
\n

24.2. ttk — Tk themed widgets

\n

The ttk module provides access to the Tk themed widget set, which has\nbeen introduced in Tk 8.5. If Python is not compiled against Tk 8.5 code may\nstill use this module as long as Tile is installed. However, some features\nprovided by the new Tk, like anti-aliased font rendering under X11, window\ntransparency (on X11 you will need a composition window manager) will be\nmissing.

\n

The basic idea of ttk is to separate, to the extent possible, the code\nimplementing a widget’s behavior from the code implementing its appearance.

\n
\n

See also

\n
\n
Tk Widget Styling Support
\n
The document which brought up theming support for Tk
\n
\n
\n
\n

24.2.1. Using Ttk

\n

To start using Ttk, import its module:

\n
import ttk\n
\n
\n

But code like this:

\n
from Tkinter import *\n
\n
\n

may optionally want to use this:

\n
from Tkinter import *\nfrom ttk import *\n
\n
\n

And then several ttk widgets (Button, Checkbutton,\nEntry, Frame, Label, LabelFrame,\nMenubutton, PanedWindow, Radiobutton, Scale\nand Scrollbar) will automatically substitute for the Tk widgets.

\n

This has the direct benefit of using the new widgets, giving better look & feel\nacross platforms, but be aware that they are not totally compatible. The main\ndifference is that widget options such as “fg”, “bg” and others related to\nwidget styling are no longer present in Ttk widgets. Use ttk.Style to\nachieve the same (or better) styling.

\n
\n

See also

\n
\n
Converting existing applications to use the Tile widgets
\n
A text which talks in Tcl terms about differences typically found when\nconverting applications to use the new widgets.
\n
\n
\n
\n
\n

24.2.2. Ttk Widgets

\n

Ttk comes with 17 widgets, 11 of which already exist in Tkinter:\nButton, Checkbutton, Entry, Frame,\nLabel, LabelFrame, Menubutton,\nPanedWindow, Radiobutton, Scale and\nScrollbar. The 6 new widget classes are: Combobox,\nNotebook, Progressbar, Separator,\nSizegrip and Treeview. All of these classes are\nsubclasses of Widget.

\n

As said previously, you will notice changes in look-and-feel as well in the\nstyling code. To demonstrate the latter, a very simple example is shown below.

\n

Tk code:

\n
l1 = Tkinter.Label(text="Test", fg="black", bg="white")\nl2 = Tkinter.Label(text="Test", fg="black", bg="white")\n
\n
\n

Corresponding Ttk code:

\n
style = ttk.Style()\nstyle.configure("BW.TLabel", foreground="black", background="white")\n\nl1 = ttk.Label(text="Test", style="BW.TLabel")\nl2 = ttk.Label(text="Test", style="BW.TLabel")\n
\n
\n

For more information about TtkStyling read the Style class\ndocumentation.

\n
\n
\n

24.2.3. Widget

\n

ttk.Widget defines standard options and methods supported by Tk\nthemed widgets and is not supposed to be directly instantiated.

\n
\n

24.2.3.1. Standard Options

\n

All the ttk widgets accept the following options:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
OptionDescription
classSpecifies the window class. The class is used when querying\nthe option database for the window’s other options, to\ndetermine the default bindtags for the window, and to select\nthe widget’s default layout and style. This is a read-only\noption which may only be specified when the window is\ncreated.
cursorSpecifies the mouse cursor to be used for the widget. If set\nto the empty string (the default), the cursor is inherited\nfrom the parent widget.
takefocusDetermines whether the window accepts the focus during\nkeyboard traversal. 0, 1 or an empty string is returned.\nIf 0, the window should be skipped entirely\nduring keyboard traversal. If 1, the window\nshould receive the input focus as long as it is viewable.\nAn empty string means that the traversal scripts make the\ndecision about whether or not to focus on the window.
styleMay be used to specify a custom widget style.
\n
\n
\n
\n

24.2.3.2. Scrollable Widget Options

\n

The following options are supported by widgets that are controlled by a\nscrollbar.

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
xscrollcommand

Used to communicate with horizontal scrollbars.

\n

When the view in the widget’s window changes, the widget\nwill generate a Tcl command based on the scrollcommand.

\n

Usually this option consists of the\nScrollbar.set() method of some scrollbar. This\nwill cause\nthe scrollbar to be updated whenever the view in the\nwindow changes.

\n
yscrollcommandUsed to communicate with vertical scrollbars.\nFor more information, see above.
\n
\n
\n
\n

24.2.3.3. Label Options

\n

The following options are supported by labels, buttons and other button-like\nwidgets.

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
textSpecifies a text string to be displayed inside the widget.
textvariableSpecifies a name whose value will be used in place of the\ntext option resource.
underlineIf set, specifies the index (0-based) of a character to\nunderline in the text string. The underline character is\nused for mnemonic activation.
imageSpecifies an image to display. This is a list of 1 or more\nelements. The first element is the default image name. The\nrest of the list is a sequence of statespec/value pairs as\ndefined by Style.map(), specifying different images\nto use when the widget is in a particular state or a\ncombination of states. All images in the list should have\nthe same size.
compound

Specifies how to display the image relative to the text,\nin the case both text and image options are present.\nValid values are:

\n
    \n
  • text: display text only
  • \n
  • image: display image only
  • \n
  • top, bottom, left, right: display image above, below,\nleft of, or right of the text, respectively.
  • \n
  • none: the default. display the image if present,\notherwise the text.
  • \n
\n
widthIf greater than zero, specifies how much space, in\ncharacter widths, to allocate for the text label; if less\nthan zero, specifies a minimum width. If zero or\nunspecified, the natural width of the text label is used.
\n
\n
\n
\n

24.2.3.4. Compatibility Options

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
stateMay be set to “normal” or “disabled” to control the “disabled”\nstate bit. This is a write-only option: setting it changes the\nwidget state, but the Widget.state() method does not\naffect this option.
\n
\n
\n
\n

24.2.3.5. Widget States

\n

The widget state is a bitmap of independent state flags.

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
flagdescription
activeThe mouse cursor is over the widget and pressing a mouse\nbutton will cause some action to occur.
disabledWidget is disabled under program control.
focusWidget has keyboard focus.
pressedWidget is being pressed.
selected“On”, “true”, or “current” for things like Checkbuttons and\nradiobuttons.
backgroundWindows and Mac have a notion of an “active” or foreground\nwindow. The background state is set for widgets in a\nbackground window, and cleared for those in the foreground\nwindow.
readonlyWidget should not allow user modification.
alternateA widget-specific alternate display format.
invalidThe widget’s value is invalid.
\n
\n

A state specification is a sequence of state names, optionally prefixed with\nan exclamation point indicating that the bit is off.

\n
\n
\n

24.2.3.6. ttk.Widget

\n

Besides the methods described below, the ttk.Widget class supports the\nTkinter.Widget.cget() and Tkinter.Widget.configure() methods.

\n
\n
\nclass ttk.Widget
\n
\n
\nidentify(x, y)
\n

Returns the name of the element at position x y, or the empty string\nif the point does not lie within any element.

\n

x and y are pixel coordinates relative to the widget.

\n
\n\n
\n
\ninstate(statespec[, callback=None[, *args[, **kw]]])
\n
Test the widget’s state. If a callback is not specified, returns True\nif the widget state matches statespec and False otherwise. If callback\nis specified then it is called with args if widget state matches\nstatespec.
\n\n
\n
\nstate([statespec=None])
\n
Modify or read widget state. If statespec is specified, sets the\nwidget state accordingly and returns a new statespec indicating\nwhich flags were changed. If statespec is not specified, returns\nthe currently-enabled state flags.
\n\n

statespec will usually be a list or a tuple.

\n
\n\n
\n
\n
\n

24.2.4. Combobox

\n

The ttk.Combobox widget combines a text field with a pop-down list of\nvalues. This widget is a subclass of Entry.

\n

Besides the methods inherited from Widget (Widget.cget(),\nWidget.configure(), Widget.identify(), Widget.instate()\nand Widget.state()) and those inherited from Entry\n(Entry.bbox(), Entry.delete(), Entry.icursor(),\nEntry.index(), Entry.inset(), Entry.selection(),\nEntry.xview()), this class has some other methods, described at\nttk.Combobox.

\n
\n

24.2.4.1. Options

\n

This widget accepts the following options:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
exportselectionBoolean value. If set, the widget selection is linked\nto the Window Manager selection (which can be returned\nby invoking Misc.selection_get(), for example).
justifySpecifies how the text is aligned within the widget.\nOne of “left”, “center”, or “right”.
heightSpecifies the height of the pop-down listbox, in rows.
postcommandA script (possibly registered with\nMisc.register()) that\nis called immediately before displaying the values. It\nmay specify which values to display.
stateOne of “normal”, “readonly”, or “disabled”. In the\n“readonly” state, the value may not be edited directly,\nand the user can only select one of the values from the\ndropdown list. In the “normal” state, the text field is\ndirectly editable. In the “disabled” state, no\ninteraction is possible.
textvariableSpecifies a name whose value is linked to the widget\nvalue. Whenever the value associated with that name\nchanges, the widget value is updated, and vice versa.\nSee Tkinter.StringVar.
valuesSpecifies the list of values to display in the\ndrop-down listbox.
widthSpecifies an integer value indicating the desired width\nof the entry window, in average-size characters of the\nwidget’s font.
\n
\n
\n
\n

24.2.4.2. Virtual events

\n

The combobox widget generates a <<ComboboxSelected>> virtual event\nwhen the user selects an element from the list of values.

\n
\n
\n

24.2.4.3. ttk.Combobox

\n
\n
\nclass ttk.Combobox
\n
\n
\ncurrent([newindex=None])
\n
If newindex is specified, sets the combobox value to the element\nposition newindex. Otherwise, returns the index of the current value or\n-1 if the current value is not in the values list.
\n\n
\n
\nget()
\n
Returns the current value of the combobox.
\n\n
\n
\nset(value)
\n
Sets the value of the combobox to value.
\n\n
\n\n
\n
\n
\n

24.2.5. Notebook

\n

The Ttk Notebook widget manages a collection of windows and displays a single\none at a time. Each child window is associated with a tab, which the user\nmay select to change the currently-displayed window.

\n
\n

24.2.5.1. Options

\n

This widget accepts the following specific options:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
heightIf present and greater than zero, specifies the desired height\nof the pane area (not including internal padding or tabs).\nOtherwise, the maximum height of all panes is used.
paddingSpecifies the amount of extra space to add around the outside\nof the notebook. The padding is a list of up to four length\nspecifications: left top right bottom. If fewer than four\nelements are specified, bottom defaults to top, right defaults\nto left, and top defaults to left.
widthIf present and greater than zero, specifies the desired width\nof the pane area (not including internal padding). Otherwise,\nthe maximum width of all panes is used.
\n
\n
\n
\n

24.2.5.2. Tab Options

\n

There are also specific options for tabs:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
stateEither “normal”, “disabled” or “hidden”. If “disabled”, then\nthe tab is not selectable. If “hidden”, then the tab is not\nshown.
stickySpecifies how the child window is positioned within the pane\narea. Value is a string containing zero or more of the\ncharacters “n”, “s”, “e” or “w”. Each letter refers to a\nside (north, south, east or west) that the child window will\nstick to, as per the grid() geometry manager.
paddingSpecifies the amount of extra space to add between the\nnotebook and this pane. Syntax is the same as for the option\npadding used by this widget.
textSpecifies a text to be displayed in the tab.
imageSpecifies an image to display in the tab. See the option\nimage described in Widget.
compoundSpecifies how to display the image relative to the text, in\nthe case both text and image options are present. See\nLabel Options for legal values.
underlineSpecifies the index (0-based) of a character to underline in\nthe text string. The underlined character is used for\nmnemonic activation if Notebook.enable_traversal() is\ncalled.
\n
\n
\n
\n

24.2.5.3. Tab Identifiers

\n

The tab_id present in several methods of ttk.Notebook may take any\nof the following forms:

\n
    \n
  • An integer between zero and the number of tabs.
  • \n
  • The name of a child window.
  • \n
  • A positional specification of the form “@x,y”, which identifies the tab.
  • \n
  • The literal string “current”, which identifies the currently-selected tab.
  • \n
  • The literal string “end”, which returns the number of tabs (only valid for\nNotebook.index()).
  • \n
\n
\n
\n

24.2.5.4. Virtual Events

\n

This widget generates a <<NotebookTabChanged>> virtual event after a new\ntab is selected.

\n
\n
\n

24.2.5.5. ttk.Notebook

\n
\n
\nclass ttk.Notebook
\n
\n
\nadd(child, **kw)
\n

Adds a new tab to the notebook.

\n

If window is currently managed by the notebook but hidden, it is\nrestored to its previous position.

\n

See Tab Options for the list of available options.

\n
\n\n
\n
\nforget(tab_id)
\n
Removes the tab specified by tab_id, unmaps and unmanages the\nassociated window.
\n\n
\n
\nhide(tab_id)
\n

Hides the tab specified by tab_id.

\n

The tab will not be displayed, but the associated window remains\nmanaged by the notebook and its configuration remembered. Hidden tabs\nmay be restored with the add() command.

\n
\n\n
\n
\nidentify(x, y)
\n
Returns the name of the tab element at position x, y, or the empty\nstring if none.
\n\n
\n
\nindex(tab_id)
\n
Returns the numeric index of the tab specified by tab_id, or the total\nnumber of tabs if tab_id is the string “end”.
\n\n
\n
\ninsert(pos, child, **kw)
\n

Inserts a pane at the specified position.

\n

pos is either the string “end”, an integer index, or the name of a\nmanaged child. If child is already managed by the notebook, moves it to\nthe specified position.

\n

See Tab Options for the list of available options.

\n
\n\n
\n
\nselect([tab_id])
\n

Selects the specified tab_id.

\n

The associated child window will be displayed, and the\npreviously-selected window (if different) is unmapped. If tab_id is\nomitted, returns the widget name of the currently selected pane.

\n
\n\n
\n
\ntab(tab_id[, option=None[, **kw]])
\n

Query or modify the options of the specific tab_id.

\n

If kw is not given, returns a dictionary of the tab option values. If\noption is specified, returns the value of that option. Otherwise,\nsets the options to the corresponding values.

\n
\n\n
\n
\ntabs()
\n
Returns a list of windows managed by the notebook.
\n\n
\n
\nenable_traversal()
\n

Enable keyboard traversal for a toplevel window containing this notebook.

\n

This will extend the bindings for the toplevel window containing the\nnotebook as follows:

\n
    \n
  • Control-Tab: selects the tab following the currently selected one.
  • \n
  • Shift-Control-Tab: selects the tab preceding the currently selected one.
  • \n
  • Alt-K: where K is the mnemonic (underlined) character of any tab, will\nselect that tab.
  • \n
\n

Multiple notebooks in a single toplevel may be enabled for traversal,\nincluding nested notebooks. However, notebook traversal only works\nproperly if all panes have the notebook they are in as master.

\n
\n\n
\n\n
\n
\n
\n

24.2.6. Progressbar

\n

The ttk.Progressbar widget shows the status of a long-running\noperation. It can operate in two modes: determinate mode shows the amount\ncompleted relative to the total amount of work to be done, and indeterminate\nmode provides an animated display to let the user know that something is\nhappening.

\n
\n

24.2.6.1. Options

\n

This widget accepts the following specific options:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
orientOne of “horizontal” or “vertical”. Specifies the orientation\nof the progress bar.
lengthSpecifies the length of the long axis of the progress bar\n(width if horizontal, height if vertical).
modeOne of “determinate” or “indeterminate”.
maximumA number specifying the maximum value. Defaults to 100.
valueThe current value of the progress bar. In “determinate” mode,\nthis represents the amount of work completed. In\n“indeterminate” mode, it is interpreted as modulo maximum;\nthat is, the progress bar completes one “cycle” when its value\nincreases by maximum.
variableA name which is linked to the option value. If specified, the\nvalue of the progress bar is automatically set to the value of\nthis name whenever the latter is modified.
phaseRead-only option. The widget periodically increments the value\nof this option whenever its value is greater than 0 and, in\ndeterminate mode, less than maximum. This option may be used\nby the current theme to provide additional animation effects.
\n
\n
\n
\n

24.2.6.2. ttk.Progressbar

\n
\n
\nclass ttk.Progressbar
\n
\n
\nstart([interval])
\n
Begin autoincrement mode: schedules a recurring timer event that calls\nProgressbar.step() every interval milliseconds. If omitted,\ninterval defaults to 50 milliseconds.
\n\n
\n
\nstep([amount])
\n

Increments the progress bar’s value by amount.

\n

amount defaults to 1.0 if omitted.

\n
\n\n
\n
\nstop()
\n
Stop autoincrement mode: cancels any recurring timer event initiated by\nProgressbar.start() for this progress bar.
\n\n
\n\n
\n
\n
\n

24.2.7. Separator

\n

The ttk.Separator widget displays a horizontal or vertical separator\nbar.

\n

It has no other methods besides the ones inherited from ttk.Widget.

\n
\n

24.2.7.1. Options

\n

This widget accepts the following specific option:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
orientOne of “horizontal” or “vertical”. Specifies the orientation of\nthe separator.
\n
\n
\n
\n
\n

24.2.8. Sizegrip

\n

The ttk.Sizegrip widget (also known as a grow box) allows the user to\nresize the containing toplevel window by pressing and dragging the grip.

\n

This widget has neither specific options nor specific methods, besides the\nones inherited from ttk.Widget.

\n
\n

24.2.8.1. Platform-specific notes

\n
    \n
  • On Mac OS X, toplevel windows automatically include a built-in size grip\nby default. Adding a Sizegrip is harmless, since the built-in\ngrip will just mask the widget.
  • \n
\n
\n
\n

24.2.8.2. Bugs

\n
    \n
  • If the containing toplevel’s position was specified relative to the right\nor bottom of the screen (e.g. ....), the Sizegrip widget will\nnot resize the window.
  • \n
  • This widget supports only “southeast” resizing.
  • \n
\n
\n
\n
\n

24.2.9. Treeview

\n

The ttk.Treeview widget displays a hierarchical collection of items.\nEach item has a textual label, an optional image, and an optional list of data\nvalues. The data values are displayed in successive columns after the tree\nlabel.

\n

The order in which data values are displayed may be controlled by setting\nthe widget option displaycolumns. The tree widget can also display column\nheadings. Columns may be accessed by number or symbolic names listed in the\nwidget option columns. See Column Identifiers.

\n

Each item is identified by an unique name. The widget will generate item IDs\nif they are not supplied by the caller. There is a distinguished root item,\nnamed {}. The root item itself is not displayed; its children appear at the\ntop level of the hierarchy.

\n

Each item also has a list of tags, which can be used to associate event bindings\nwith individual items and control the appearance of the item.

\n

The Treeview widget supports horizontal and vertical scrolling, according to\nthe options described in Scrollable Widget Options and the methods\nTreeview.xview() and Treeview.yview().

\n
\n

24.2.9.1. Options

\n

This widget accepts the following specific options:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
columnsA list of column identifiers, specifying the number of\ncolumns and their names.
displaycolumnsA list of column identifiers (either symbolic or\ninteger indices) specifying which data columns are\ndisplayed and the order in which they appear, or the\nstring “#all”.
heightSpecifies the number of rows which should be visible.\nNote: the requested width is determined from the sum\nof the column widths.
paddingSpecifies the internal padding for the widget. The\npadding is a list of up to four length specifications.
selectmode

Controls how the built-in class bindings manage the\nselection. One of “extended”, “browse” or “none”.\nIf set to “extended” (the default), multiple items may\nbe selected. If “browse”, only a single item will be\nselected at a time. If “none”, the selection will not\nbe changed.

\n

Note that the application code and tag bindings can set\nthe selection however they wish, regardless of the\nvalue of this option.

\n
show

A list containing zero or more of the following values,\nspecifying which elements of the tree to display.

\n
    \n
  • tree: display tree labels in column #0.
  • \n
  • headings: display the heading row.
  • \n
\n

The default is “tree headings”, i.e., show all\nelements.

\n

Note: Column #0 always refers to the tree column,\neven if show=”tree” is not specified.

\n
\n
\n
\n
\n

24.2.9.2. Item Options

\n

The following item options may be specified for items in the insert and item\nwidget commands.

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
textThe textual label to display for the item.
imageA Tk Image, displayed to the left of the label.
values

The list of values associated with the item.

\n

Each item should have the same number of values as the widget\noption columns. If there are fewer values than columns, the\nremaining values are assumed empty. If there are more values\nthan columns, the extra values are ignored.

\n
openTrue/False value indicating whether the item’s children should\nbe displayed or hidden.
tagsA list of tags associated with this item.
\n
\n
\n
\n

24.2.9.3. Tag Options

\n

The following options may be specified on tags:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
optiondescription
foregroundSpecifies the text foreground color.
backgroundSpecifies the cell or item background color.
fontSpecifies the font to use when drawing text.
imageSpecifies the item image, in case the item’s image option\nis empty.
\n
\n
\n
\n

24.2.9.4. Column Identifiers

\n

Column identifiers take any of the following forms:

\n
    \n
  • A symbolic name from the list of columns option.
  • \n
  • An integer n, specifying the nth data column.
  • \n
  • A string of the form #n, where n is an integer, specifying the nth display\ncolumn.
  • \n
\n

Notes:

\n
    \n
  • Item’s option values may be displayed in a different order than the order\nin which they are stored.
  • \n
  • Column #0 always refers to the tree column, even if show=”tree” is not\nspecified.
  • \n
\n

A data column number is an index into an item’s option values list; a display\ncolumn number is the column number in the tree where the values are displayed.\nTree labels are displayed in column #0. If option displaycolumns is not set,\nthen data column n is displayed in column #n+1. Again, column #0 always\nrefers to the tree column.

\n
\n
\n

24.2.9.5. Virtual Events

\n

The Treeview widget generates the following virtual events.

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
eventdescription
<<TreeviewSelect>>Generated whenever the selection changes.
<<TreeviewOpen>>Generated just before settings the focus item to\nopen=True.
<<TreeviewClose>>Generated just after setting the focus item to\nopen=False.
\n
\n

The Treeview.focus() and Treeview.selection() methods can be used\nto determine the affected item or items.

\n
\n
\n

24.2.9.6. ttk.Treeview

\n
\n
\nclass ttk.Treeview
\n
\n
\nbbox(item[, column=None])
\n

Returns the bounding box (relative to the treeview widget’s window) of\nthe specified item in the form (x, y, width, height).

\n

If column is specified, returns the bounding box of that cell. If the\nitem is not visible (i.e., if it is a descendant of a closed item or is\nscrolled offscreen), returns an empty string.

\n
\n\n
\n
\nget_children([item])
\n

Returns the list of children belonging to item.

\n

If item is not specified, returns root children.

\n
\n\n
\n
\nset_children(item, *newchildren)
\n

Replaces item‘s child with newchildren.

\n

Children present in item that are not present in newchildren are\ndetached from the tree. No items in newchildren may be an ancestor of\nitem. Note that not specifying newchildren results in detaching\nitem‘s children.

\n
\n\n
\n
\ncolumn(column[, option=None[, **kw]])
\n

Query or modify the options for the specified column.

\n

If kw is not given, returns a dict of the column option values. If\noption is specified then the value for that option is returned.\nOtherwise, sets the options to the corresponding values.

\n

The valid options/values are:

\n
    \n
  • \n
    id
    \n

    Returns the column name. This is a read-only option.

    \n
    \n
    \n
  • \n
  • \n
    anchor: One of the standard Tk anchor values.
    \n

    Specifies how the text in this column should be aligned with respect\nto the cell.

    \n
    \n
    \n
  • \n
  • \n
    minwidth: width
    \n

    The minimum width of the column in pixels. The treeview widget will\nnot make the column any smaller than specified by this option when\nthe widget is resized or the user drags a column.

    \n
    \n
    \n
  • \n
  • \n
    stretch: True/False
    \n

    Specifies whether the column’s width should be adjusted when\nthe widget is resized.

    \n
    \n
    \n
  • \n
  • \n
    width: width
    \n

    The width of the column in pixels.

    \n
    \n
    \n
  • \n
\n

To configure the tree column, call this with column = “#0”

\n
\n\n
\n
\ndelete(*items)
\n

Delete all specified items and all their descendants.

\n

The root item may not be deleted.

\n
\n\n
\n
\ndetach(*items)
\n

Unlinks all of the specified items from the tree.

\n

The items and all of their descendants are still present, and may be\nreinserted at another point in the tree, but will not be displayed.

\n

The root item may not be detached.

\n
\n\n
\n
\nexists(item)
\n
Returns True if the specified item is present in the tree.
\n\n
\n
\nfocus([item=None])
\n
If item is specified, sets the focus item to item. Otherwise, returns\nthe current focus item, or ‘’ if there is none.
\n\n
\n
\nheading(column[, option=None[, **kw]])
\n

Query or modify the heading options for the specified column.

\n

If kw is not given, returns a dict of the heading option values. If\noption is specified then the value for that option is returned.\nOtherwise, sets the options to the corresponding values.

\n

The valid options/values are:

\n
    \n
  • \n
    text: text
    \n

    The text to display in the column heading.

    \n
    \n
    \n
  • \n
  • \n
    image: imageName
    \n

    Specifies an image to display to the right of the column heading.

    \n
    \n
    \n
  • \n
  • \n
    anchor: anchor
    \n

    Specifies how the heading text should be aligned. One of the standard\nTk anchor values.

    \n
    \n
    \n
  • \n
  • \n
    command: callback
    \n

    A callback to be invoked when the heading label is pressed.

    \n
    \n
    \n
  • \n
\n

To configure the tree column heading, call this with column = “#0”.

\n
\n\n
\n
\nidentify(component, x, y)
\n
Returns a description of the specified component under the point given\nby x and y, or the empty string if no such component is present at\nthat position.
\n\n
\n
\nidentify_row(y)
\n
Returns the item ID of the item at position y.
\n\n
\n
\nidentify_column(x)
\n

Returns the data column identifier of the cell at position x.

\n

The tree column has ID #0.

\n
\n\n
\n
\nidentify_region(x, y)
\n

Returns one of:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
regionmeaning
headingTree heading area.
separatorSpace between two columns headings.
treeThe tree area.
cellA data cell.
\n

Availability: Tk 8.6.

\n
\n\n
\n
\nidentify_element(x, y)
\n

Returns the element at position x, y.

\n

Availability: Tk 8.6.

\n
\n\n
\n
\nindex(item)
\n
Returns the integer index of item within its parent’s list of children.
\n\n
\n
\ninsert(parent, index[, iid=None[, **kw]])
\n

Creates a new item and returns the item identifier of the newly created\nitem.

\n

parent is the item ID of the parent item, or the empty string to create\na new top-level item. index is an integer, or the value “end”,\nspecifying where in the list of parent’s children to insert the new item.\nIf index is less than or equal to zero, the new node is inserted at\nthe beginning; if index is greater than or equal to the current number\nof children, it is inserted at the end. If iid is specified, it is used\nas the item identifier; iid must not already exist in the tree.\nOtherwise, a new unique identifier is generated.

\n

See Item Options for the list of available points.

\n
\n\n
\n
\nitem(item[, option[, **kw]])
\n

Query or modify the options for the specified item.

\n

If no options are given, a dict with options/values for the item is\nreturned.\nIf option is specified then the value for that option is returned.\nOtherwise, sets the options to the corresponding values as given by kw.

\n
\n\n
\n
\nmove(item, parent, index)
\n

Moves item to position index in parent‘s list of children.

\n

It is illegal to move an item under one of its descendants. If index is\nless than or equal to zero, item is moved to the beginning; if greater\nthan or equal to the number of children, it is moved to the end. If item\nwas detached it is reattached.

\n
\n\n
\n
\nnext(item)
\n
Returns the identifier of item‘s next sibling, or ‘’ if item is the\nlast child of its parent.
\n\n
\n
\nparent(item)
\n
Returns the ID of the parent of item, or ‘’ if item is at the top\nlevel of the hierarchy.
\n\n
\n
\nprev(item)
\n
Returns the identifier of item‘s previous sibling, or ‘’ if item is\nthe first child of its parent.
\n\n
\n
\nreattach(item, parent, index)
\n
An alias for Treeview.move().
\n\n
\n
\nsee(item)
\n

Ensure that item is visible.

\n

Sets all of item‘s ancestors open option to True, and scrolls the\nwidget if necessary so that item is within the visible portion of\nthe tree.

\n
\n\n
\n
\nselection([selop=None[, items=None]])
\n
If selop is not specified, returns selected items. Otherwise, it will\nact according to the following selection methods.
\n\n
\n
\nselection_set(items)
\n
items becomes the new selection.
\n\n
\n
\nselection_add(items)
\n
Add items to the selection.
\n\n
\n
\nselection_remove(items)
\n
Remove items from the selection.
\n\n
\n
\nselection_toggle(items)
\n
Toggle the selection state of each item in items.
\n\n
\n
\nset(item[, column=None[, value=None]])
\n
With one argument, returns a dictionary of column/value pairs for the\nspecified item. With two arguments, returns the current value of the\nspecified column. With three arguments, sets the value of given\ncolumn in given item to the specified value.
\n\n
\n
\ntag_bind(tagname[, sequence=None[, callback=None]])
\n
Bind a callback for the given event sequence to the tag tagname.\nWhen an event is delivered to an item, the callbacks for each of the\nitem’s tags option are called.
\n\n
\n
\ntag_configure(tagname[, option=None[, **kw]])
\n

Query or modify the options for the specified tagname.

\n

If kw is not given, returns a dict of the option settings for\ntagname. If option is specified, returns the value for that option\nfor the specified tagname. Otherwise, sets the options to the\ncorresponding values for the given tagname.

\n
\n\n
\n
\ntag_has(tagname[, item])
\n

If item is specified, returns 1 or 0 depending on whether the specified\nitem has the given tagname. Otherwise, returns a list of all items\nthat have the specified tag.

\n

Availability: Tk 8.6

\n
\n\n
\n
\nxview(*args)
\n
Query or modify horizontal position of the treeview.
\n\n
\n
\nyview(*args)
\n
Query or modify vertical position of the treeview.
\n\n
\n\n
\n
\n
\n

24.2.10. Ttk Styling

\n

Each widget in ttk is assigned a style, which specifies the set of\nelements making up the widget and how they are arranged, along with dynamic and\ndefault settings for element options. By default the style name is the same as\nthe widget’s class name, but it may be overridden by the widget’s style\noption. If the class name of a widget is unknown, use the method\nMisc.winfo_class() (somewidget.winfo_class()).

\n
\n

See also

\n
\n
Tcl‘2004 conference presentation
\n
This document explains how the theme engine works
\n
\n
\n
\n
\nclass ttk.Style
\n

This class is used to manipulate the style database.

\n
\n
\nconfigure(style, query_opt=None, **kw)
\n

Query or set the default value of the specified option(s) in style.

\n

Each key in kw is an option and each value is a string identifying\nthe value for that option.

\n

For example, to change every default button to be a flat button with some\npadding and a different background color do:

\n
import ttk\nimport Tkinter\n\nroot = Tkinter.Tk()\n\nttk.Style().configure("TButton", padding=6, relief="flat",\n   background="#ccc")\n\nbtn = ttk.Button(text="Sample")\nbtn.pack()\n\nroot.mainloop()\n
\n
\n
\n\n
\n
\nmap(style, query_opt=None, **kw)
\n

Query or sets dynamic values of the specified option(s) in style.

\n

Each key in kw is an option and each value should be a list or a\ntuple (usually) containing statespecs grouped in tuples, lists, or\nsomething else of your preference. A statespec is a compound of one\nor more states and then a value.

\n

An example:

\n
import Tkinter\nimport ttk\n\nroot = Tkinter.Tk()\n\nstyle = ttk.Style()\nstyle.map("C.TButton",\n    foreground=[('pressed', 'red'), ('active', 'blue')],\n    background=[('pressed', '!disabled', 'black'), ('active', 'white')]\n    )\n\ncolored_btn = ttk.Button(text="Test", style="C.TButton").pack()\n\nroot.mainloop()\n
\n
\n

Note that the order of the (states, value) sequences for an\noption matters. In the previous example, if you change the\norder to [('active', 'blue'), ('pressed', 'red')] in the\nforeground option, for example, you would get a blue foreground\nwhen the widget is in the active or pressed states.

\n
\n\n
\n
\nlookup(style, option[, state=None[, default=None]])
\n

Returns the value specified for option in style.

\n

If state is specified, it is expected to be a sequence of one or more\nstates. If the default argument is set, it is used as a fallback value\nin case no specification for option is found.

\n

To check what font a Button uses by default, do:

\n
import ttk\n\nprint ttk.Style().lookup("TButton", "font")\n
\n
\n
\n\n
\n
\nlayout(style[, layoutspec=None])
\n

Define the widget layout for given style. If layoutspec is omitted,\nreturn the layout specification for given style.

\n

layoutspec, if specified, is expected to be a list or some other\nsequence type (excluding strings), where each item should be a tuple and\nthe first item is the layout name and the second item should have the\nformat described in Layouts.

\n

To understand the format, see the following example (it is not\nintended to do anything useful):

\n
import ttk\nimport Tkinter\n\nroot = Tkinter.Tk()\n\nstyle = ttk.Style()\nstyle.layout("TMenubutton", [\n   ("Menubutton.background", None),\n   ("Menubutton.button", {"children":\n       [("Menubutton.focus", {"children":\n           [("Menubutton.padding", {"children":\n               [("Menubutton.label", {"side": "left", "expand": 1})]\n           })]\n       })]\n   }),\n])\n\nmbtn = ttk.Menubutton(text='Text')\nmbtn.pack()\nroot.mainloop()\n
\n
\n
\n\n
\n
\nelement_create(elementname, etype, *args, **kw)
\n

Create a new element in the current theme, of the given etype which is\nexpected to be either “image”, “from” or “vsapi”. The latter is only\navailable in Tk 8.6a for Windows XP and Vista and is not described here.

\n

If “image” is used, args should contain the default image name followed\nby statespec/value pairs (this is the imagespec), and kw may have the\nfollowing options:

\n
\n
    \n
  • \n
    border=padding
    \n

    padding is a list of up to four integers, specifying the left, top,\nright, and bottom borders, respectively.

    \n
    \n
    \n
  • \n
  • \n
    height=height
    \n

    Specifies a minimum height for the element. If less than zero, the\nbase image’s height is used as a default.

    \n
    \n
    \n
  • \n
  • \n
    padding=padding
    \n

    Specifies the element’s interior padding. Defaults to border’s value\nif not specified.

    \n
    \n
    \n
  • \n
  • \n
    sticky=spec
    \n

    Specifies how the image is placed within the final parcel. spec\ncontains zero or more characters “n”, “s”, “w”, or “e”.

    \n
    \n
    \n
  • \n
  • \n
    width=width
    \n

    Specifies a minimum width for the element. If less than zero, the\nbase image’s width is used as a default.

    \n
    \n
    \n
  • \n
\n
\n

If “from” is used as the value of etype,\nelement_create() will clone an existing\nelement. args is expected to contain a themename, from which\nthe element will be cloned, and optionally an element to clone from.\nIf this element to clone from is not specified, an empty element will\nbe used. kw is discarded.

\n
\n\n
\n
\nelement_names()
\n
Returns the list of elements defined in the current theme.
\n\n
\n
\nelement_options(elementname)
\n
Returns the list of elementname‘s options.
\n\n
\n
\ntheme_create(themename[, parent=None[, settings=None]])
\n

Create a new theme.

\n

It is an error if themename already exists. If parent is specified,\nthe new theme will inherit styles, elements and layouts from the parent\ntheme. If settings are present they are expected to have the same\nsyntax used for theme_settings().

\n
\n\n
\n
\ntheme_settings(themename, settings)
\n

Temporarily sets the current theme to themename, apply specified\nsettings and then restore the previous theme.

\n

Each key in settings is a style and each value may contain the keys\n‘configure’, ‘map’, ‘layout’ and ‘element create’ and they are expected\nto have the same format as specified by the methods\nStyle.configure(), Style.map(), Style.layout() and\nStyle.element_create() respectively.

\n

As an example, let’s change the Combobox for the default theme a bit:

\n
import ttk\nimport Tkinter\n\nroot = Tkinter.Tk()\n\nstyle = ttk.Style()\nstyle.theme_settings("default", {\n   "TCombobox": {\n       "configure": {"padding": 5},\n       "map": {\n           "background": [("active", "green2"),\n                          ("!disabled", "green4")],\n           "fieldbackground": [("!disabled", "green3")],\n           "foreground": [("focus", "OliveDrab1"),\n                          ("!disabled", "OliveDrab2")]\n       }\n   }\n})\n\ncombo = ttk.Combobox().pack()\n\nroot.mainloop()\n
\n
\n
\n\n
\n
\ntheme_names()
\n
Returns a list of all known themes.
\n\n
\n
\ntheme_use([themename])
\n
If themename is not given, returns the theme in use. Otherwise, sets\nthe current theme to themename, refreshes all widgets and emits a\n<<ThemeChanged>> event.
\n\n
\n\n
\n

24.2.10.1. Layouts

\n

A layout can be just None, if it takes no options, or a dict of\noptions specifying how to arrange the element. The layout mechanism\nuses a simplified version of the pack geometry manager: given an\ninitial cavity, each element is allocated a parcel. Valid\noptions/values are:

\n
\n
    \n
  • \n
    side: whichside
    \n

    Specifies which side of the cavity to place the element; one of\ntop, right, bottom or left. If omitted, the element occupies the\nentire cavity.

    \n
    \n
    \n
  • \n
  • \n
    sticky: nswe
    \n

    Specifies where the element is placed inside its allocated parcel.

    \n
    \n
    \n
  • \n
  • \n
    unit: 0 or 1
    \n

    If set to 1, causes the element and all of its descendants to be treated as\na single element for the purposes of Widget.identify() et al. It’s\nused for things like scrollbar thumbs with grips.

    \n
    \n
    \n
  • \n
  • \n
    children: [sublayout... ]
    \n

    Specifies a list of elements to place inside the element. Each\nelement is a tuple (or other sequence type) where the first item is\nthe layout name, and the other is a Layout.

    \n
    \n
    \n
  • \n
\n
\n
\n
\n
", "searchableItems": [ { "name": "ttk.Combobox", "domId": "ttk_ttk.Combobox" }, { "name": "ttk.Combobox.current", "domId": "ttk_ttk.Combobox.current" }, { "name": "ttk.Combobox.get", "domId": "ttk_ttk.Combobox.get" }, { "name": "ttk.Combobox.set", "domId": "ttk_ttk.Combobox.set" }, { "name": "ttk.Notebook", "domId": "ttk_ttk.Notebook" }, { "name": "ttk.Notebook.add", "domId": "ttk_ttk.Notebook.add" }, { "name": "ttk.Notebook.enable_traversal", "domId": "ttk_ttk.Notebook.enable_traversal" }, { "name": "ttk.Notebook.forget", "domId": "ttk_ttk.Notebook.forget" }, { "name": "ttk.Notebook.hide", "domId": "ttk_ttk.Notebook.hide" }, { "name": "ttk.Notebook.identify", "domId": "ttk_ttk.Notebook.identify" }, { "name": "ttk.Notebook.index", "domId": "ttk_ttk.Notebook.index" }, { "name": "ttk.Notebook.insert", "domId": "ttk_ttk.Notebook.insert" }, { "name": "ttk.Notebook.select", "domId": "ttk_ttk.Notebook.select" }, { "name": "ttk.Notebook.tab", "domId": "ttk_ttk.Notebook.tab" }, { "name": "ttk.Notebook.tabs", "domId": "ttk_ttk.Notebook.tabs" }, { "name": "ttk.Progressbar", "domId": "ttk_ttk.Progressbar" }, { "name": "ttk.Progressbar.start", "domId": "ttk_ttk.Progressbar.start" }, { "name": "ttk.Progressbar.step", "domId": "ttk_ttk.Progressbar.step" }, { "name": "ttk.Progressbar.stop", "domId": "ttk_ttk.Progressbar.stop" }, { "name": "ttk.Style", "domId": "ttk_ttk.Style" }, { "name": "ttk.Style.configure", "domId": "ttk_ttk.Style.configure" }, { "name": "ttk.Style.element_create", "domId": "ttk_ttk.Style.element_create" }, { "name": "ttk.Style.element_names", "domId": "ttk_ttk.Style.element_names" }, { "name": "ttk.Style.element_options", "domId": "ttk_ttk.Style.element_options" }, { "name": "ttk.Style.layout", "domId": "ttk_ttk.Style.layout" }, { "name": "ttk.Style.lookup", "domId": "ttk_ttk.Style.lookup" }, { "name": "ttk.Style.map", "domId": "ttk_ttk.Style.map" }, { "name": "ttk.Style.theme_create", "domId": "ttk_ttk.Style.theme_create" }, { "name": "ttk.Style.theme_names", "domId": "ttk_ttk.Style.theme_names" }, { "name": "ttk.Style.theme_settings", "domId": "ttk_ttk.Style.theme_settings" }, { "name": "ttk.Style.theme_use", "domId": "ttk_ttk.Style.theme_use" }, { "name": "ttk.Treeview", "domId": "ttk_ttk.Treeview" }, { "name": "ttk.Treeview.bbox", "domId": "ttk_ttk.Treeview.bbox" }, { "name": "ttk.Treeview.column", "domId": "ttk_ttk.Treeview.column" }, { "name": "ttk.Treeview.delete", "domId": "ttk_ttk.Treeview.delete" }, { "name": "ttk.Treeview.detach", "domId": "ttk_ttk.Treeview.detach" }, { "name": "ttk.Treeview.exists", "domId": "ttk_ttk.Treeview.exists" }, { "name": "ttk.Treeview.focus", "domId": "ttk_ttk.Treeview.focus" }, { "name": "ttk.Treeview.get_children", "domId": "ttk_ttk.Treeview.get_children" }, { "name": "ttk.Treeview.heading", "domId": "ttk_ttk.Treeview.heading" }, { "name": "ttk.Treeview.identify", "domId": "ttk_ttk.Treeview.identify" }, { "name": "ttk.Treeview.identify_column", "domId": "ttk_ttk.Treeview.identify_column" }, { "name": "ttk.Treeview.identify_element", "domId": "ttk_ttk.Treeview.identify_element" }, { "name": "ttk.Treeview.identify_region", "domId": "ttk_ttk.Treeview.identify_region" }, { "name": "ttk.Treeview.identify_row", "domId": "ttk_ttk.Treeview.identify_row" }, { "name": "ttk.Treeview.index", "domId": "ttk_ttk.Treeview.index" }, { "name": "ttk.Treeview.insert", "domId": "ttk_ttk.Treeview.insert" }, { "name": "ttk.Treeview.item", "domId": "ttk_ttk.Treeview.item" }, { "name": "ttk.Treeview.move", "domId": "ttk_ttk.Treeview.move" }, { "name": "ttk.Treeview.next", "domId": "ttk_ttk.Treeview.next" }, { "name": "ttk.Treeview.parent", "domId": "ttk_ttk.Treeview.parent" }, { "name": "ttk.Treeview.prev", "domId": "ttk_ttk.Treeview.prev" }, { "name": "ttk.Treeview.reattach", "domId": "ttk_ttk.Treeview.reattach" }, { "name": "ttk.Treeview.see", "domId": "ttk_ttk.Treeview.see" }, { "name": "ttk.Treeview.selection", "domId": "ttk_ttk.Treeview.selection" }, { "name": "ttk.Treeview.selection_add", "domId": "ttk_ttk.Treeview.selection_add" }, { "name": "ttk.Treeview.selection_remove", "domId": "ttk_ttk.Treeview.selection_remove" }, { "name": "ttk.Treeview.selection_set", "domId": "ttk_ttk.Treeview.selection_set" }, { "name": "ttk.Treeview.selection_toggle", "domId": "ttk_ttk.Treeview.selection_toggle" }, { "name": "ttk.Treeview.set", "domId": "ttk_ttk.Treeview.set" }, { "name": "ttk.Treeview.set_children", "domId": "ttk_ttk.Treeview.set_children" }, { "name": "ttk.Treeview.tag_bind", "domId": "ttk_ttk.Treeview.tag_bind" }, { "name": "ttk.Treeview.tag_configure", "domId": "ttk_ttk.Treeview.tag_configure" }, { "name": "ttk.Treeview.tag_has", "domId": "ttk_ttk.Treeview.tag_has" }, { "name": "ttk.Treeview.xview", "domId": "ttk_ttk.Treeview.xview" }, { "name": "ttk.Treeview.yview", "domId": "ttk_ttk.Treeview.yview" }, { "name": "ttk.Widget", "domId": "ttk_ttk.Widget" }, { "name": "ttk.Widget.identify", "domId": "ttk_ttk.Widget.identify" }, { "name": "ttk.Widget.instate", "domId": "ttk_ttk.Widget.instate" }, { "name": "ttk.Widget.state", "domId": "ttk_ttk.Widget.state" } ] }, { "url": "http://docs.python.org/library/pydoc.html", "title": "pydoc", "html": "
\n

25.1. pydoc — Documentation generator and online help system

\n

\nNew in version 2.1.

\n

Source code: Lib/pydoc.py

\n
\n

The pydoc module automatically generates documentation from Python\nmodules. The documentation can be presented as pages of text on the console,\nserved to a Web browser, or saved to HTML files.

\n

The built-in function help() invokes the online help system in the\ninteractive interpreter, which uses pydoc to generate its documentation\nas text on the console. The same text documentation can also be viewed from\noutside the Python interpreter by running pydoc as a script at the\noperating system’s command prompt. For example, running

\n
pydoc sys
\n
\n

at a shell prompt will display documentation on the sys module, in a\nstyle similar to the manual pages shown by the Unix man command. The\nargument to pydoc can be the name of a function, module, or package,\nor a dotted reference to a class, method, or function within a module or module\nin a package. If the argument to pydoc looks like a path (that is,\nit contains the path separator for your operating system, such as a slash in\nUnix), and refers to an existing Python source file, then documentation is\nproduced for that file.

\n
\n

Note

\n

In order to find objects and their documentation, pydoc imports the\nmodule(s) to be documented. Therefore, any code on module level will be\nexecuted on that occasion. Use an if __name__ == '__main__': guard to\nonly execute code when a file is invoked as a script and not just imported.

\n
\n

Specifying a -w flag before the argument will cause HTML documentation\nto be written out to a file in the current directory, instead of displaying text\non the console.

\n

Specifying a -k flag before the argument will search the synopsis\nlines of all available modules for the keyword given as the argument, again in a\nmanner similar to the Unix man command. The synopsis line of a\nmodule is the first line of its documentation string.

\n

You can also use pydoc to start an HTTP server on the local machine\nthat will serve documentation to visiting Web browsers. pydoc -p 1234\nwill start a HTTP server on port 1234, allowing you to browse\nthe documentation at http://localhost:1234/ in your preferred Web browser.\npydoc -g will start the server and additionally bring up a\nsmall Tkinter-based graphical interface to help you search for\ndocumentation pages.

\n

When pydoc generates documentation, it uses the current environment\nand path to locate modules. Thus, invoking pydoc spam\ndocuments precisely the version of the module you would get if you started the\nPython interpreter and typed import spam.

\n

Module docs for core modules are assumed to reside in\nhttp://docs.python.org/library/. This can be overridden by setting the\nPYTHONDOCS environment variable to a different URL or to a local\ndirectory containing the Library Reference Manual pages.

\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/turtle.html", "title": "turtle", "html": "
\n

24.5. turtle — Turtle graphics for Tk

\n
\n

24.5.1. Introduction

\n

Turtle graphics is a popular way for introducing programming to kids. It was\npart of the original Logo programming language developed by Wally Feurzig and\nSeymour Papert in 1966.

\n

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the\ncommand turtle.forward(15), and it moves (on-screen!) 15 pixels in the\ndirection it is facing, drawing a line as it moves. Give it the command\nturtle.right(25), and it rotates in-place 25 degrees clockwise.

\n

By combining together these and similar commands, intricate shapes and pictures\ncan easily be drawn.

\n

The turtle module is an extended reimplementation of the same-named\nmodule from the Python standard distribution up to version Python 2.5.

\n

It tries to keep the merits of the old turtle module and to be (nearly) 100%\ncompatible with it. This means in the first place to enable the learning\nprogrammer to use all the commands, classes and methods interactively when using\nthe module from within IDLE run with the -n switch.

\n

The turtle module provides turtle graphics primitives, in both object-oriented\nand procedure-oriented ways. Because it uses Tkinter for the underlying\ngraphics, it needs a version of Python installed with Tk support.

\n

The object-oriented interface uses essentially two+two classes:

\n
    \n
  1. The TurtleScreen class defines graphics windows as a playground for\nthe drawing turtles. Its constructor needs a Tkinter.Canvas or a\nScrolledCanvas as argument. It should be used when turtle is\nused as part of some application.

    \n

    The function Screen() returns a singleton object of a\nTurtleScreen subclass. This function should be used when\nturtle is used as a standalone tool for doing graphics.\nAs a singleton object, inheriting from its class is not possible.

    \n

    All methods of TurtleScreen/Screen also exist as functions, i.e. as part of\nthe procedure-oriented interface.

    \n
  2. \n
  3. RawTurtle (alias: RawPen) defines Turtle objects which draw\non a TurtleScreen. Its constructor needs a Canvas, ScrolledCanvas\nor TurtleScreen as argument, so the RawTurtle objects know where to draw.

    \n

    Derived from RawTurtle is the subclass Turtle (alias: Pen),\nwhich draws on “the” Screen - instance which is automatically\ncreated, if not already present.

    \n

    All methods of RawTurtle/Turtle also exist as functions, i.e. part of the\nprocedure-oriented interface.

    \n
  4. \n
\n

The procedural interface provides functions which are derived from the methods\nof the classes Screen and Turtle. They have the same names as\nthe corresponding methods. A screen object is automatically created whenever a\nfunction derived from a Screen method is called. An (unnamed) turtle object is\nautomatically created whenever any of the functions derived from a Turtle method\nis called.

\n

To use multiple turtles an a screen one has to use the object-oriented interface.

\n
\n

Note

\n

In the following documentation the argument list for functions is given.\nMethods, of course, have the additional first argument self which is\nomitted here.

\n
\n
\n
\n

24.5.2. Overview over available Turtle and Screen methods

\n
\n

24.5.2.1. Turtle methods

\n
\n
Turtle motion
\n
\n
Move and draw
\n
\n
\n
Tell Turtle’s state
\n
\n
\n
Setting and measurement
\n
\n
\n
\n
\n
Pen control
\n
\n
Drawing state
\n
\n
\n
Color control
\n
\n
\n
Filling
\n
\n
\n
More drawing control
\n
\n
\n
\n
\n
Turtle state
\n
\n
Visibility
\n
\n
\n
Appearance
\n
\n
\n
\n
\n
Using events
\n
\n
\n
Special Turtle methods
\n
\n
\n
\n
\n
\n

24.5.2.2. Methods of TurtleScreen/Screen

\n
\n
Window control
\n
\n
\n
Animation control
\n
\n
\n
Using screen events
\n
\n
\n
Settings and special methods
\n
\n
\n
Methods specific to Screen
\n
\n
\n
\n
\n
\n
\n

24.5.3. Methods of RawTurtle/Turtle and corresponding functions

\n

Most of the examples in this section refer to a Turtle instance called\nturtle.

\n
\n

24.5.3.1. Turtle motion

\n
\n
\nturtle.forward(distance)
\n
\nturtle.fd(distance)
\n
\n\n\n\n\n\n\n
Parameter:distance – a number (integer or float)
\n

Move the turtle forward by the specified distance, in the direction the\nturtle is headed.

\n
>>> turtle.position()\n(0.00,0.00)\n>>> turtle.forward(25)\n>>> turtle.position()\n(25.00,0.00)\n>>> turtle.forward(-75)\n>>> turtle.position()\n(-50.00,0.00)\n
\n
\n
\n\n
\n
\nturtle.back(distance)
\n
\nturtle.bk(distance)
\n
\nturtle.backward(distance)
\n
\n\n\n\n\n\n\n
Parameter:distance – a number
\n

Move the turtle backward by distance, opposite to the direction the\nturtle is headed. Do not change the turtle’s heading.

\n
>>> turtle.position()\n(0.00,0.00)\n>>> turtle.backward(30)\n>>> turtle.position()\n(-30.00,0.00)\n
\n
\n
\n\n
\n
\nturtle.right(angle)
\n
\nturtle.rt(angle)
\n
\n\n\n\n\n\n\n
Parameter:angle – a number (integer or float)
\n

Turn turtle right by angle units. (Units are by default degrees, but\ncan be set via the degrees() and radians() functions.) Angle\norientation depends on the turtle mode, see mode().

\n
>>> turtle.heading()\n22.0\n>>> turtle.right(45)\n>>> turtle.heading()\n337.0\n
\n
\n
\n\n
\n
\nturtle.left(angle)
\n
\nturtle.lt(angle)
\n
\n\n\n\n\n\n\n
Parameter:angle – a number (integer or float)
\n

Turn turtle left by angle units. (Units are by default degrees, but\ncan be set via the degrees() and radians() functions.) Angle\norientation depends on the turtle mode, see mode().

\n
>>> turtle.heading()\n22.0\n>>> turtle.left(45)\n>>> turtle.heading()\n67.0\n
\n
\n
\n\n
\n
\nturtle.goto(x, y=None)
\n
\nturtle.setpos(x, y=None)
\n
\nturtle.setposition(x, y=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • x – a number or a pair/vector of numbers
  • \n
  • y – a number or None
  • \n
\n
\n

If y is None, x must be a pair of coordinates or a Vec2D\n(e.g. as returned by pos()).

\n

Move turtle to an absolute position. If the pen is down, draw line. Do\nnot change the turtle’s orientation.

\n
>>> tp = turtle.pos()\n>>> tp\n(0.00,0.00)\n>>> turtle.setpos(60,30)\n>>> turtle.pos()\n(60.00,30.00)\n>>> turtle.setpos((20,80))\n>>> turtle.pos()\n(20.00,80.00)\n>>> turtle.setpos(tp)\n>>> turtle.pos()\n(0.00,0.00)\n
\n
\n
\n\n
\n
\nturtle.setx(x)
\n
\n\n\n\n\n\n\n
Parameter:x – a number (integer or float)
\n

Set the turtle’s first coordinate to x, leave second coordinate\nunchanged.

\n
>>> turtle.position()\n(0.00,240.00)\n>>> turtle.setx(10)\n>>> turtle.position()\n(10.00,240.00)\n
\n
\n
\n\n
\n
\nturtle.sety(y)
\n
\n\n\n\n\n\n\n
Parameter:y – a number (integer or float)
\n

Set the turtle’s second coordinate to y, leave first coordinate unchanged.

\n
>>> turtle.position()\n(0.00,40.00)\n>>> turtle.sety(-10)\n>>> turtle.position()\n(0.00,-10.00)\n
\n
\n
\n\n
\n
\nturtle.setheading(to_angle)
\n
\nturtle.seth(to_angle)
\n
\n\n\n\n\n\n\n
Parameter:to_angle – a number (integer or float)
\n

Set the orientation of the turtle to to_angle. Here are some common\ndirections in degrees:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
standard modelogo mode
0 - east0 - north
90 - north90 - east
180 - west180 - south
270 - south270 - west
\n
>>> turtle.setheading(90)\n>>> turtle.heading()\n90.0\n
\n
\n
\n\n
\n
\nturtle.home()
\n

Move turtle to the origin – coordinates (0,0) – and set its heading to\nits start-orientation (which depends on the mode, see mode()).

\n
>>> turtle.heading()\n90.0\n>>> turtle.position()\n(0.00,-10.00)\n>>> turtle.home()\n>>> turtle.position()\n(0.00,0.00)\n>>> turtle.heading()\n0.0\n
\n
\n
\n\n
\n
\nturtle.circle(radius, extent=None, steps=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • radius – a number
  • \n
  • extent – a number (or None)
  • \n
  • steps – an integer (or None)
  • \n
\n
\n

Draw a circle with given radius. The center is radius units left of\nthe turtle; extent – an angle – determines which part of the circle\nis drawn. If extent is not given, draw the entire circle. If extent\nis not a full circle, one endpoint of the arc is the current pen\nposition. Draw the arc in counterclockwise direction if radius is\npositive, otherwise in clockwise direction. Finally the direction of the\nturtle is changed by the amount of extent.

\n

As the circle is approximated by an inscribed regular polygon, steps\ndetermines the number of steps to use. If not given, it will be\ncalculated automatically. May be used to draw regular polygons.

\n
>>> turtle.home()\n>>> turtle.position()\n(0.00,0.00)\n>>> turtle.heading()\n0.0\n>>> turtle.circle(50)\n>>> turtle.position()\n(-0.00,0.00)\n>>> turtle.heading()\n0.0\n>>> turtle.circle(120, 180)  # draw a semicircle\n>>> turtle.position()\n(0.00,240.00)\n>>> turtle.heading()\n180.0\n
\n
\n
\n\n
\n
\nturtle.dot(size=None, *color)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • size – an integer >= 1 (if given)
  • \n
  • color – a colorstring or a numeric color tuple
  • \n
\n
\n

Draw a circular dot with diameter size, using color. If size is\nnot given, the maximum of pensize+4 and 2*pensize is used.

\n
>>> turtle.home()\n>>> turtle.dot()\n>>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)\n>>> turtle.position()\n(100.00,-0.00)\n>>> turtle.heading()\n0.0\n
\n
\n
\n\n
\n
\nturtle.stamp()
\n

Stamp a copy of the turtle shape onto the canvas at the current turtle\nposition. Return a stamp_id for that stamp, which can be used to delete\nit by calling clearstamp(stamp_id).

\n
>>> turtle.color("blue")\n>>> turtle.stamp()\n11\n>>> turtle.fd(50)\n
\n
\n
\n\n
\n
\nturtle.clearstamp(stampid)
\n
\n\n\n\n\n\n\n
Parameter:stampid – an integer, must be return value of previous\nstamp() call
\n

Delete stamp with given stampid.

\n
>>> turtle.position()\n(150.00,-0.00)\n>>> turtle.color("blue")\n>>> astamp = turtle.stamp()\n>>> turtle.fd(50)\n>>> turtle.position()\n(200.00,-0.00)\n>>> turtle.clearstamp(astamp)\n>>> turtle.position()\n(200.00,-0.00)\n
\n
\n
\n\n
\n
\nturtle.clearstamps(n=None)
\n
\n\n\n\n\n\n\n
Parameter:n – an integer (or None)
\n

Delete all or first/last n of turtle’s stamps. If n is None, delete\nall stamps, if n > 0 delete first n stamps, else if n < 0 delete\nlast n stamps.

\n
>>> for i in range(8):\n...     turtle.stamp(); turtle.fd(30)\n13\n14\n15\n16\n17\n18\n19\n20\n>>> turtle.clearstamps(2)\n>>> turtle.clearstamps(-2)\n>>> turtle.clearstamps()\n
\n
\n
\n\n
\n
\nturtle.undo()
\n

Undo (repeatedly) the last turtle action(s). Number of available\nundo actions is determined by the size of the undobuffer.

\n
>>> for i in range(4):\n...     turtle.fd(50); turtle.lt(80)\n...\n>>> for i in range(8):\n...     turtle.undo()\n
\n
\n
\n\n
\n
\nturtle.speed(speed=None)
\n
\n\n\n\n\n\n\n
Parameter:speed – an integer in the range 0..10 or a speedstring (see below)
\n

Set the turtle’s speed to an integer value in the range 0..10. If no\nargument is given, return current speed.

\n

If input is a number greater than 10 or smaller than 0.5, speed is set\nto 0. Speedstrings are mapped to speedvalues as follows:

\n
    \n
  • “fastest”: 0
  • \n
  • “fast”: 10
  • \n
  • “normal”: 6
  • \n
  • “slow”: 3
  • \n
  • “slowest”: 1
  • \n
\n

Speeds from 1 to 10 enforce increasingly faster animation of line drawing\nand turtle turning.

\n

Attention: speed = 0 means that no animation takes\nplace. forward/back makes turtle jump and likewise left/right make the\nturtle turn instantly.

\n
>>> turtle.speed()\n3\n>>> turtle.speed('normal')\n>>> turtle.speed()\n6\n>>> turtle.speed(9)\n>>> turtle.speed()\n9\n
\n
\n
\n\n
\n
\n

24.5.3.2. Tell Turtle’s state

\n
\n
\nturtle.position()
\n
\nturtle.pos()
\n

Return the turtle’s current location (x,y) (as a Vec2D vector).

\n
>>> turtle.pos()\n(440.00,-0.00)\n
\n
\n
\n\n
\n
\nturtle.towards(x, y=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • x – a number or a pair/vector of numbers or a turtle instance
  • \n
  • y – a number if x is a number, else None
  • \n
\n
\n

Return the angle between the line from turtle position to position specified\nby (x,y), the vector or the other turtle. This depends on the turtle’s start\norientation which depends on the mode - “standard”/”world” or “logo”).

\n
>>> turtle.goto(10, 10)\n>>> turtle.towards(0,0)\n225.0\n
\n
\n
\n\n
\n
\nturtle.xcor()
\n

Return the turtle’s x coordinate.

\n
>>> turtle.home()\n>>> turtle.left(50)\n>>> turtle.forward(100)\n>>> turtle.pos()\n(64.28,76.60)\n>>> print turtle.xcor()\n64.2787609687\n
\n
\n
\n\n
\n
\nturtle.ycor()
\n

Return the turtle’s y coordinate.

\n
>>> turtle.home()\n>>> turtle.left(60)\n>>> turtle.forward(100)\n>>> print turtle.pos()\n(50.00,86.60)\n>>> print turtle.ycor()\n86.6025403784\n
\n
\n
\n\n
\n
\nturtle.heading()
\n

Return the turtle’s current heading (value depends on the turtle mode, see\nmode()).

\n
>>> turtle.home()\n>>> turtle.left(67)\n>>> turtle.heading()\n67.0\n
\n
\n
\n\n
\n
\nturtle.distance(x, y=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • x – a number or a pair/vector of numbers or a turtle instance
  • \n
  • y – a number if x is a number, else None
  • \n
\n
\n

Return the distance from the turtle to (x,y), the given vector, or the given\nother turtle, in turtle step units.

\n
>>> turtle.home()\n>>> turtle.distance(30,40)\n50.0\n>>> turtle.distance((30,40))\n50.0\n>>> joe = Turtle()\n>>> joe.forward(77)\n>>> turtle.distance(joe)\n77.0\n
\n
\n
\n\n
\n
\n

24.5.3.3. Settings for measurement

\n
\n
\nturtle.degrees(fullcircle=360.0)
\n
\n\n\n\n\n\n\n
Parameter:fullcircle – a number
\n

Set angle measurement units, i.e. set number of “degrees” for a full circle.\nDefault value is 360 degrees.

\n
>>> turtle.home()\n>>> turtle.left(90)\n>>> turtle.heading()\n90.0\n\nChange angle measurement unit to grad (also known as gon,\ngrade, or gradian and equals 1/100-th of the right angle.)\n>>> turtle.degrees(400.0)\n>>> turtle.heading()\n100.0\n>>> turtle.degrees(360)\n>>> turtle.heading()\n90.0\n
\n
\n
\n\n
\n
\nturtle.radians()
\n

Set the angle measurement units to radians. Equivalent to\ndegrees(2*math.pi).

\n
>>> turtle.home()\n>>> turtle.left(90)\n>>> turtle.heading()\n90.0\n>>> turtle.radians()\n>>> turtle.heading()\n1.5707963267948966\n
\n
\n
\n\n
\n
\n

24.5.3.4. Pen control

\n
\n

24.5.3.4.1. Drawing state

\n
\n
\nturtle.pendown()
\n
\nturtle.pd()
\n
\nturtle.down()
\n
Pull the pen down – drawing when moving.
\n\n
\n
\nturtle.penup()
\n
\nturtle.pu()
\n
\nturtle.up()
\n
Pull the pen up – no drawing when moving.
\n\n
\n
\nturtle.pensize(width=None)
\n
\nturtle.width(width=None)
\n
\n\n\n\n\n\n\n
Parameter:width – a positive number
\n

Set the line thickness to width or return it. If resizemode is set to\n“auto” and turtleshape is a polygon, that polygon is drawn with the same line\nthickness. If no argument is given, the current pensize is returned.

\n
>>> turtle.pensize()\n1\n>>> turtle.pensize(10)   # from here on lines of width 10 are drawn\n
\n
\n
\n\n
\n
\nturtle.pen(pen=None, **pendict)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • pen – a dictionary with some or all of the below listed keys
  • \n
  • pendict – one or more keyword-arguments with the below listed keys as keywords
  • \n
\n
\n

Return or set the pen’s attributes in a “pen-dictionary” with the following\nkey/value pairs:

\n
    \n
  • “shown”: True/False
  • \n
  • “pendown”: True/False
  • \n
  • “pencolor”: color-string or color-tuple
  • \n
  • “fillcolor”: color-string or color-tuple
  • \n
  • “pensize”: positive number
  • \n
  • “speed”: number in range 0..10
  • \n
  • “resizemode”: “auto” or “user” or “noresize”
  • \n
  • “stretchfactor”: (positive number, positive number)
  • \n
  • “outline”: positive number
  • \n
  • “tilt”: number
  • \n
\n

This dictionary can be used as argument for a subsequent call to pen()\nto restore the former pen-state. Moreover one or more of these attributes\ncan be provided as keyword-arguments. This can be used to set several pen\nattributes in one statement.

\n
>>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)\n>>> sorted(turtle.pen().items())\n[('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),\n ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),\n ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]\n>>> penstate=turtle.pen()\n>>> turtle.color("yellow", "")\n>>> turtle.penup()\n>>> sorted(turtle.pen().items())\n[('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow'),\n ('pendown', False), ('pensize', 10), ('resizemode', 'noresize'),\n ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]\n>>> turtle.pen(penstate, fillcolor="green")\n>>> sorted(turtle.pen().items())\n[('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red'),\n ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),\n ('shown', True), ('speed', 9), ('stretchfactor', (1, 1)), ('tilt', 0)]\n
\n
\n
\n\n
\n
\nturtle.isdown()
\n

Return True if pen is down, False if it’s up.

\n
>>> turtle.penup()\n>>> turtle.isdown()\nFalse\n>>> turtle.pendown()\n>>> turtle.isdown()\nTrue\n
\n
\n
\n\n
\n
\n

24.5.3.4.2. Color control

\n
\n
\nturtle.pencolor(*args)
\n

Return or set the pencolor.

\n

Four input formats are allowed:

\n
\n
pencolor()
\n
Return the current pencolor as color specification string or\nas a tuple (see example). May be used as input to another\ncolor/pencolor/fillcolor call.
\n
pencolor(colorstring)
\n
Set pencolor to colorstring, which is a Tk color specification string,\nsuch as "red", "yellow", or "#33cc8c".
\n
pencolor((r, g, b))
\n
Set pencolor to the RGB color represented by the tuple of r, g, and\nb. Each of r, g, and b must be in the range 0..colormode, where\ncolormode is either 1.0 or 255 (see colormode()).
\n
pencolor(r, g, b)
\n
\nSet pencolor to the RGB color represented by r, g, and b. Each of\nr, g, and b must be in the range 0..colormode.
\n

If turtleshape is a polygon, the outline of that polygon is drawn with the\nnewly set pencolor.

\n
\n
\n
>>> colormode()\n1.0\n>>> turtle.pencolor()\n'red'\n>>> turtle.pencolor("brown")\n>>> turtle.pencolor()\n'brown'\n>>> tup = (0.2, 0.8, 0.55)\n>>> turtle.pencolor(tup)\n>>> turtle.pencolor()\n(0.2, 0.8, 0.5490196078431373)\n>>> colormode(255)\n>>> turtle.pencolor()\n(51, 204, 140)\n>>> turtle.pencolor('#32c18f')\n>>> turtle.pencolor()\n(50, 193, 143)\n
\n
\n
\n\n
\n
\nturtle.fillcolor(*args)
\n

Return or set the fillcolor.

\n

Four input formats are allowed:

\n
\n
fillcolor()
\n
Return the current fillcolor as color specification string, possibly\nin tuple format (see example). May be used as input to another\ncolor/pencolor/fillcolor call.
\n
fillcolor(colorstring)
\n
Set fillcolor to colorstring, which is a Tk color specification string,\nsuch as "red", "yellow", or "#33cc8c".
\n
fillcolor((r, g, b))
\n
Set fillcolor to the RGB color represented by the tuple of r, g, and\nb. Each of r, g, and b must be in the range 0..colormode, where\ncolormode is either 1.0 or 255 (see colormode()).
\n
fillcolor(r, g, b)
\n
\nSet fillcolor to the RGB color represented by r, g, and b. Each of\nr, g, and b must be in the range 0..colormode.
\n

If turtleshape is a polygon, the interior of that polygon is drawn\nwith the newly set fillcolor.

\n
\n
\n
>>> turtle.fillcolor("violet")\n>>> turtle.fillcolor()\n'violet'\n>>> col = turtle.pencolor()\n>>> col\n(50, 193, 143)\n>>> turtle.fillcolor(col)\n>>> turtle.fillcolor()\n(50, 193, 143)\n>>> turtle.fillcolor('#ffffff')\n>>> turtle.fillcolor()\n(255, 255, 255)\n
\n
\n
\n\n
\n
\nturtle.color(*args)
\n

Return or set pencolor and fillcolor.

\n

Several input formats are allowed. They use 0 to 3 arguments as\nfollows:

\n
\n
color()
\n
Return the current pencolor and the current fillcolor as a pair of color\nspecification strings or tuples as returned by pencolor() and\nfillcolor().
\n
color(colorstring), color((r,g,b)), color(r,g,b)
\n
Inputs as in pencolor(), set both, fillcolor and pencolor, to the\ngiven value.
\n
color(colorstring1, colorstring2), color((r1,g1,b1), (r2,g2,b2))
\n
\nEquivalent to pencolor(colorstring1) and fillcolor(colorstring2)\nand analogously if the other input format is used.
\n

If turtleshape is a polygon, outline and interior of that polygon is drawn\nwith the newly set colors.

\n
\n
\n
>>> turtle.color("red", "green")\n>>> turtle.color()\n('red', 'green')\n>>> color("#285078", "#a0c8f0")\n>>> color()\n((40, 80, 120), (160, 200, 240))\n
\n
\n
\n\n

See also: Screen method colormode().

\n
\n
\n

24.5.3.4.3. Filling

\n
\n
\nturtle.fill(flag)
\n
\n\n\n\n\n\n\n
Parameter:flag – True/False (or 1/0 respectively)
\n

Call fill(True) before drawing the shape you want to fill, and\nfill(False) when done. When used without argument: return fillstate\n(True if filling, False else).

\n
>>> turtle.fill(True)\n>>> for _ in range(3):\n...    turtle.forward(100)\n...    turtle.left(120)\n...\n>>> turtle.fill(False)\n
\n
\n
\n\n
\n
\nturtle.begin_fill()
\n
Call just before drawing a shape to be filled. Equivalent to fill(True).
\n\n
\n
\nturtle.end_fill()
\n

Fill the shape drawn after the last call to begin_fill(). Equivalent\nto fill(False).

\n
>>> turtle.color("black", "red")\n>>> turtle.begin_fill()\n>>> turtle.circle(80)\n>>> turtle.end_fill()\n
\n
\n
\n\n
\n
\n

24.5.3.4.4. More drawing control

\n
\n
\nturtle.reset()
\n

Delete the turtle’s drawings from the screen, re-center the turtle and set\nvariables to the default values.

\n
>>> turtle.goto(0,-22)\n>>> turtle.left(100)\n>>> turtle.position()\n(0.00,-22.00)\n>>> turtle.heading()\n100.0\n>>> turtle.reset()\n>>> turtle.position()\n(0.00,0.00)\n>>> turtle.heading()\n0.0\n
\n
\n
\n\n
\n
\nturtle.clear()
\n
Delete the turtle’s drawings from the screen. Do not move turtle. State and\nposition of the turtle as well as drawings of other turtles are not affected.
\n\n
\n
\nturtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • arg – object to be written to the TurtleScreen
  • \n
  • move – True/False
  • \n
  • align – one of the strings “left”, “center” or right”
  • \n
  • font – a triple (fontname, fontsize, fonttype)
  • \n
\n
\n

Write text - the string representation of arg - at the current turtle\nposition according to align (“left”, “center” or right”) and with the given\nfont. If move is True, the pen is moved to the bottom-right corner of the\ntext. By default, move is False.

\n
>>> turtle.write("Home = ", True, align="center")\n>>> turtle.write((0,0), True)\n
\n
\n
\n\n
\n
\n
\n

24.5.3.5. Turtle state

\n
\n

24.5.3.5.1. Visibility

\n
\n
\nturtle.hideturtle()
\n
\nturtle.ht()
\n

Make the turtle invisible. It’s a good idea to do this while you’re in the\nmiddle of doing some complex drawing, because hiding the turtle speeds up the\ndrawing observably.

\n
>>> turtle.hideturtle()\n
\n
\n
\n\n
\n
\nturtle.showturtle()
\n
\nturtle.st()
\n

Make the turtle visible.

\n
>>> turtle.showturtle()\n
\n
\n
\n\n
\n
\nturtle.isvisible()
\n

Return True if the Turtle is shown, False if it’s hidden.

\n
>>> turtle.hideturtle()\n>>> turtle.isvisible()\nFalse\n>>> turtle.showturtle()\n>>> turtle.isvisible()\nTrue\n
\n
\n
\n\n
\n
\n

24.5.3.5.2. Appearance

\n
\n
\nturtle.shape(name=None)
\n
\n\n\n\n\n\n\n
Parameter:name – a string which is a valid shapename
\n

Set turtle shape to shape with given name or, if name is not given, return\nname of current shape. Shape with name must exist in the TurtleScreen’s\nshape dictionary. Initially there are the following polygon shapes: “arrow”,\n“turtle”, “circle”, “square”, “triangle”, “classic”. To learn about how to\ndeal with shapes see Screen method register_shape().

\n
>>> turtle.shape()\n'classic'\n>>> turtle.shape("turtle")\n>>> turtle.shape()\n'turtle'\n
\n
\n
\n\n
\n
\nturtle.resizemode(rmode=None)
\n
\n\n\n\n\n\n\n
Parameter:rmode – one of the strings “auto”, “user”, “noresize”
\n

Set resizemode to one of the values: “auto”, “user”, “noresize”. If rmode\nis not given, return current resizemode. Different resizemodes have the\nfollowing effects:

\n
    \n
  • “auto”: adapts the appearance of the turtle corresponding to the value of pensize.
  • \n
  • “user”: adapts the appearance of the turtle according to the values of\nstretchfactor and outlinewidth (outline), which are set by\nshapesize().
  • \n
  • “noresize”: no adaption of the turtle’s appearance takes place.
  • \n
\n

resizemode(“user”) is called by shapesize() when used with arguments.

\n
>>> turtle.resizemode()\n'noresize'\n>>> turtle.resizemode("auto")\n>>> turtle.resizemode()\n'auto'\n
\n
\n
\n\n
\n
\nturtle.shapesize(stretch_wid=None, stretch_len=None, outline=None)
\n
\nturtle.turtlesize(stretch_wid=None, stretch_len=None, outline=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • stretch_wid – positive number
  • \n
  • stretch_len – positive number
  • \n
  • outline – positive number
  • \n
\n
\n

Return or set the pen’s attributes x/y-stretchfactors and/or outline. Set\nresizemode to “user”. If and only if resizemode is set to “user”, the turtle\nwill be displayed stretched according to its stretchfactors: stretch_wid is\nstretchfactor perpendicular to its orientation, stretch_len is\nstretchfactor in direction of its orientation, outline determines the width\nof the shapes’s outline.

\n
>>> turtle.shapesize()\n(1, 1, 1)\n>>> turtle.resizemode("user")\n>>> turtle.shapesize(5, 5, 12)\n>>> turtle.shapesize()\n(5, 5, 12)\n>>> turtle.shapesize(outline=8)\n>>> turtle.shapesize()\n(5, 5, 8)\n
\n
\n
\n\n
\n
\nturtle.tilt(angle)
\n
\n\n\n\n\n\n\n
Parameter:angle – a number
\n

Rotate the turtleshape by angle from its current tilt-angle, but do not\nchange the turtle’s heading (direction of movement).

\n
>>> turtle.reset()\n>>> turtle.shape("circle")\n>>> turtle.shapesize(5,2)\n>>> turtle.tilt(30)\n>>> turtle.fd(50)\n>>> turtle.tilt(30)\n>>> turtle.fd(50)\n
\n
\n
\n\n
\n
\nturtle.settiltangle(angle)
\n
\n\n\n\n\n\n\n
Parameter:angle – a number
\n

Rotate the turtleshape to point in the direction specified by angle,\nregardless of its current tilt-angle. Do not change the turtle’s heading\n(direction of movement).

\n
>>> turtle.reset()\n>>> turtle.shape("circle")\n>>> turtle.shapesize(5,2)\n>>> turtle.settiltangle(45)\n>>> turtle.fd(50)\n>>> turtle.settiltangle(-45)\n>>> turtle.fd(50)\n
\n
\n
\n\n
\n
\nturtle.tiltangle()
\n

Return the current tilt-angle, i.e. the angle between the orientation of the\nturtleshape and the heading of the turtle (its direction of movement).

\n
>>> turtle.reset()\n>>> turtle.shape("circle")\n>>> turtle.shapesize(5,2)\n>>> turtle.tilt(45)\n>>> turtle.tiltangle()\n45.0\n
\n
\n
\n\n
\n
\n
\n

24.5.3.6. Using events

\n
\n
\nturtle.onclick(fun, btn=1, add=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • fun – a function with two arguments which will be called with the\ncoordinates of the clicked point on the canvas
  • \n
  • num – number of the mouse-button, defaults to 1 (left mouse button)
  • \n
  • addTrue or False – if True, a new binding will be\nadded, otherwise it will replace a former binding
  • \n
\n
\n

Bind fun to mouse-click events on this turtle. If fun is None,\nexisting bindings are removed. Example for the anonymous turtle, i.e. the\nprocedural way:

\n
>>> def turn(x, y):\n...     left(180)\n...\n>>> onclick(turn)  # Now clicking into the turtle will turn it.\n>>> onclick(None)  # event-binding will be removed\n
\n
\n
\n\n
\n
\nturtle.onrelease(fun, btn=1, add=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • fun – a function with two arguments which will be called with the\ncoordinates of the clicked point on the canvas
  • \n
  • num – number of the mouse-button, defaults to 1 (left mouse button)
  • \n
  • addTrue or False – if True, a new binding will be\nadded, otherwise it will replace a former binding
  • \n
\n
\n

Bind fun to mouse-button-release events on this turtle. If fun is\nNone, existing bindings are removed.

\n
>>> class MyTurtle(Turtle):\n...     def glow(self,x,y):\n...         self.fillcolor("red")\n...     def unglow(self,x,y):\n...         self.fillcolor("")\n...\n>>> turtle = MyTurtle()\n>>> turtle.onclick(turtle.glow)     # clicking on turtle turns fillcolor red,\n>>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.\n
\n
\n
\n\n
\n
\nturtle.ondrag(fun, btn=1, add=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • fun – a function with two arguments which will be called with the\ncoordinates of the clicked point on the canvas
  • \n
  • num – number of the mouse-button, defaults to 1 (left mouse button)
  • \n
  • addTrue or False – if True, a new binding will be\nadded, otherwise it will replace a former binding
  • \n
\n
\n

Bind fun to mouse-move events on this turtle. If fun is None,\nexisting bindings are removed.

\n

Remark: Every sequence of mouse-move-events on a turtle is preceded by a\nmouse-click event on that turtle.

\n
>>> turtle.ondrag(turtle.goto)\n
\n
\n

Subsequently, clicking and dragging the Turtle will move it across\nthe screen thereby producing handdrawings (if pen is down).

\n
\n\n
\n
\nturtle.mainloop()
\n
\nturtle.done()
\n

Starts event loop - calling Tkinter’s mainloop function. Must be the last\nstatement in a turtle graphics program.

\n
>>> turtle.mainloop()\n
\n
\n
\n\n
\n
\n

24.5.3.7. Special Turtle methods

\n
\n
\nturtle.begin_poly()
\n
Start recording the vertices of a polygon. Current turtle position is first\nvertex of polygon.
\n\n
\n
\nturtle.end_poly()
\n
Stop recording the vertices of a polygon. Current turtle position is last\nvertex of polygon. This will be connected with the first vertex.
\n\n
\n
\nturtle.get_poly()
\n

Return the last recorded polygon.

\n
>>> turtle.home()\n>>> turtle.begin_poly()\n>>> turtle.fd(100)\n>>> turtle.left(20)\n>>> turtle.fd(30)\n>>> turtle.left(60)\n>>> turtle.fd(50)\n>>> turtle.end_poly()\n>>> p = turtle.get_poly()\n>>> register_shape("myFavouriteShape", p)\n
\n
\n
\n\n
\n
\nturtle.clone()
\n

Create and return a clone of the turtle with same position, heading and\nturtle properties.

\n
>>> mick = Turtle()\n>>> joe = mick.clone()\n
\n
\n
\n\n
\n
\nturtle.getturtle()
\n
\nturtle.getpen()
\n

Return the Turtle object itself. Only reasonable use: as a function to\nreturn the “anonymous turtle”:

\n
>>> pet = getturtle()\n>>> pet.fd(50)\n>>> pet\n<turtle.Turtle object at 0x...>\n
\n
\n
\n\n
\n
\nturtle.getscreen()
\n

Return the TurtleScreen object the turtle is drawing on.\nTurtleScreen methods can then be called for that object.

\n
>>> ts = turtle.getscreen()\n>>> ts\n<turtle._Screen object at 0x...>\n>>> ts.bgcolor("pink")\n
\n
\n
\n\n
\n
\nturtle.setundobuffer(size)
\n
\n\n\n\n\n\n\n
Parameter:size – an integer or None
\n

Set or disable undobuffer. If size is an integer an empty undobuffer of\ngiven size is installed. size gives the maximum number of turtle actions\nthat can be undone by the undo() method/function. If size is\nNone, the undobuffer is disabled.

\n
>>> turtle.setundobuffer(42)\n
\n
\n
\n\n
\n
\nturtle.undobufferentries()
\n

Return number of entries in the undobuffer.

\n
>>> while undobufferentries():\n...     undo()\n
\n
\n
\n\n
\n
\nturtle.tracer(flag=None, delay=None)
\n

A replica of the corresponding TurtleScreen method.

\n

\nDeprecated since version 2.6.

\n
\n\n
\n
\nturtle.window_width()
\n
\nturtle.window_height()
\n

Both are replicas of the corresponding TurtleScreen methods.

\n

\nDeprecated since version 2.6.

\n
\n\n
\n
\n

24.5.3.8. Excursus about the use of compound shapes

\n

To use compound turtle shapes, which consist of several polygons of different\ncolor, you must use the helper class Shape explicitly as described\nbelow:

\n
    \n
  1. Create an empty Shape object of type “compound”.

    \n
  2. \n
  3. Add as many components to this object as desired, using the\naddcomponent() method.

    \n

    For example:

    \n
    >>> s = Shape("compound")\n>>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))\n>>> s.addcomponent(poly1, "red", "blue")\n>>> poly2 = ((0,0),(10,-5),(-10,-5))\n>>> s.addcomponent(poly2, "blue", "red")\n
    \n
    \n
  4. \n
  5. Now add the Shape to the Screen’s shapelist and use it:

    \n
    >>> register_shape("myshape", s)\n>>> shape("myshape")\n
    \n
    \n
  6. \n
\n
\n

Note

\n

The Shape class is used internally by the register_shape()\nmethod in different ways. The application programmer has to deal with the\nShape class only when using compound shapes like shown above!

\n
\n
\n
\n
\n

24.5.4. Methods of TurtleScreen/Screen and corresponding functions

\n

Most of the examples in this section refer to a TurtleScreen instance called\nscreen.

\n
\n

24.5.4.1. Window control

\n
\n
\nturtle.bgcolor(*args)
\n
\n\n\n\n\n\n\n
Parameter:args – a color string or three numbers in the range 0..colormode or a\n3-tuple of such numbers
\n

Set or return background color of the TurtleScreen.

\n
>>> screen.bgcolor("orange")\n>>> screen.bgcolor()\n'orange'\n>>> screen.bgcolor("#800080")\n>>> screen.bgcolor()\n(128, 0, 128)\n
\n
\n
\n\n
\n
\nturtle.bgpic(picname=None)
\n
\n\n\n\n\n\n\n
Parameter:picname – a string, name of a gif-file or "nopic", or None
\n

Set background image or return name of current backgroundimage. If picname\nis a filename, set the corresponding image as background. If picname is\n"nopic", delete background image, if present. If picname is None,\nreturn the filename of the current backgroundimage.

\n
>>> screen.bgpic()\n'nopic'\n>>> screen.bgpic("landscape.gif")\n>>> screen.bgpic()\n"landscape.gif"\n
\n
\n
\n\n
\n
\nturtle.clear()
\n
\nturtle.clearscreen()
\n

Delete all drawings and all turtles from the TurtleScreen. Reset the now\nempty TurtleScreen to its initial state: white background, no background\nimage, no event bindings and tracing on.

\n
\n

Note

\n

This TurtleScreen method is available as a global function only under the\nname clearscreen. The global function clear is another one\nderived from the Turtle method clear.

\n
\n
\n\n
\n
\nturtle.reset()
\n
\nturtle.resetscreen()
\n

Reset all Turtles on the Screen to their initial state.

\n
\n

Note

\n

This TurtleScreen method is available as a global function only under the\nname resetscreen. The global function reset is another one\nderived from the Turtle method reset.

\n
\n
\n\n
\n
\nturtle.screensize(canvwidth=None, canvheight=None, bg=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • canvwidth – positive integer, new width of canvas in pixels
  • \n
  • canvheight – positive integer, new height of canvas in pixels
  • \n
  • bg – colorstring or color-tuple, new background color
  • \n
\n
\n

If no arguments are given, return current (canvaswidth, canvasheight). Else\nresize the canvas the turtles are drawing on. Do not alter the drawing\nwindow. To observe hidden parts of the canvas, use the scrollbars. With this\nmethod, one can make visible those parts of a drawing which were outside the\ncanvas before.

\n
>>> screen.screensize()\n(400, 300)\n>>> screen.screensize(2000,1500)\n>>> screen.screensize()\n(2000, 1500)\n
\n
\n

e.g. to search for an erroneously escaped turtle ;-)

\n
\n\n
\n
\nturtle.setworldcoordinates(llx, lly, urx, ury)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • llx – a number, x-coordinate of lower left corner of canvas
  • \n
  • lly – a number, y-coordinate of lower left corner of canvas
  • \n
  • urx – a number, x-coordinate of upper right corner of canvas
  • \n
  • ury – a number, y-coordinate of upper right corner of canvas
  • \n
\n
\n

Set up user-defined coordinate system and switch to mode “world” if\nnecessary. This performs a screen.reset(). If mode “world” is already\nactive, all drawings are redrawn according to the new coordinates.

\n

ATTENTION: in user-defined coordinate systems angles may appear\ndistorted.

\n
>>> screen.reset()\n>>> screen.setworldcoordinates(-50,-7.5,50,7.5)\n>>> for _ in range(72):\n...     left(10)\n...\n>>> for _ in range(8):\n...     left(45); fd(2)   # a regular octagon\n
\n
\n
\n\n
\n
\n

24.5.4.2. Animation control

\n
\n
\nturtle.delay(delay=None)
\n
\n\n\n\n\n\n\n
Parameter:delay – positive integer
\n

Set or return the drawing delay in milliseconds. (This is approximately\nthe time interval between two consecutive canvas updates.) The longer the\ndrawing delay, the slower the animation.

\n

Optional argument:

\n
>>> screen.delay()\n10\n>>> screen.delay(5)\n>>> screen.delay()\n5\n
\n
\n
\n\n
\n
\nturtle.tracer(n=None, delay=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • n – nonnegative integer
  • \n
  • delay – nonnegative integer
  • \n
\n
\n

Turn turtle animation on/off and set delay for update drawings. If n is\ngiven, only each n-th regular screen update is really performed. (Can be\nused to accelerate the drawing of complex graphics.) Second argument sets\ndelay value (see delay()).

\n
>>> screen.tracer(8, 25)\n>>> dist = 2\n>>> for i in range(200):\n...     fd(dist)\n...     rt(90)\n...     dist += 2\n
\n
\n
\n\n
\n
\nturtle.update()
\n
Perform a TurtleScreen update. To be used when tracer is turned off.
\n\n

See also the RawTurtle/Turtle method speed().

\n
\n
\n

24.5.4.3. Using screen events

\n
\n
\nturtle.listen(xdummy=None, ydummy=None)
\n
Set focus on TurtleScreen (in order to collect key-events). Dummy arguments\nare provided in order to be able to pass listen() to the onclick method.
\n\n
\n
\nturtle.onkey(fun, key)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • fun – a function with no arguments or None
  • \n
  • key – a string: key (e.g. “a”) or key-symbol (e.g. “space”)
  • \n
\n
\n

Bind fun to key-release event of key. If fun is None, event bindings\nare removed. Remark: in order to be able to register key-events, TurtleScreen\nmust have the focus. (See method listen().)

\n
>>> def f():\n...     fd(50)\n...     lt(60)\n...\n>>> screen.onkey(f, "Up")\n>>> screen.listen()\n
\n
\n
\n\n
\n
\nturtle.onclick(fun, btn=1, add=None)
\n
\nturtle.onscreenclick(fun, btn=1, add=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • fun – a function with two arguments which will be called with the\ncoordinates of the clicked point on the canvas
  • \n
  • num – number of the mouse-button, defaults to 1 (left mouse button)
  • \n
  • addTrue or False – if True, a new binding will be\nadded, otherwise it will replace a former binding
  • \n
\n
\n

Bind fun to mouse-click events on this screen. If fun is None,\nexisting bindings are removed.

\n

Example for a TurtleScreen instance named screen and a Turtle instance\nnamed turtle:

\n
>>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will\n>>>                             # make the turtle move to the clicked point.\n>>> screen.onclick(None)        # remove event binding again\n
\n
\n
\n

Note

\n

This TurtleScreen method is available as a global function only under the\nname onscreenclick. The global function onclick is another one\nderived from the Turtle method onclick.

\n
\n
\n\n
\n
\nturtle.ontimer(fun, t=0)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • fun – a function with no arguments
  • \n
  • t – a number >= 0
  • \n
\n
\n

Install a timer that calls fun after t milliseconds.

\n
>>> running = True\n>>> def f():\n...     if running:\n...         fd(50)\n...         lt(60)\n...         screen.ontimer(f, 250)\n>>> f()   ### makes the turtle march around\n>>> running = False\n
\n
\n
\n\n
\n
\n

24.5.4.4. Settings and special methods

\n
\n
\nturtle.mode(mode=None)
\n
\n\n\n\n\n\n\n
Parameter:mode – one of the strings “standard”, “logo” or “world”
\n

Set turtle mode (“standard”, “logo” or “world”) and perform reset. If mode\nis not given, current mode is returned.

\n

Mode “standard” is compatible with old turtle. Mode “logo” is\ncompatible with most Logo turtle graphics. Mode “world” uses user-defined\n“world coordinates”. Attention: in this mode angles appear distorted if\nx/y unit-ratio doesn’t equal 1.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ModeInitial turtle headingpositive angles
“standard”to the right (east)counterclockwise
“logo”upward (north)clockwise
\n
>>> mode("logo")   # resets turtle heading to north\n>>> mode()\n'logo'\n
\n
\n
\n\n
\n
\nturtle.colormode(cmode=None)
\n
\n\n\n\n\n\n\n
Parameter:cmode – one of the values 1.0 or 255
\n

Return the colormode or set it to 1.0 or 255. Subsequently r, g, b\nvalues of color triples have to be in the range 0..cmode.

\n
>>> screen.colormode(1)\n>>> turtle.pencolor(240, 160, 80)\nTraceback (most recent call last):\n     ...\nTurtleGraphicsError: bad color sequence: (240, 160, 80)\n>>> screen.colormode()\n1.0\n>>> screen.colormode(255)\n>>> screen.colormode()\n255\n>>> turtle.pencolor(240,160,80)\n
\n
\n
\n\n
\n
\nturtle.getcanvas()
\n

Return the Canvas of this TurtleScreen. Useful for insiders who know what to\ndo with a Tkinter Canvas.

\n
>>> cv = screen.getcanvas()\n>>> cv\n<turtle.ScrolledCanvas instance at 0x...>\n
\n
\n
\n\n
\n
\nturtle.getshapes()
\n

Return a list of names of all currently available turtle shapes.

\n
>>> screen.getshapes()\n['arrow', 'blank', 'circle', ..., 'turtle']\n
\n
\n
\n\n
\n
\nturtle.register_shape(name, shape=None)
\n
\nturtle.addshape(name, shape=None)
\n

There are three different ways to call this function:

\n
    \n
  1. name is the name of a gif-file and shape is None: Install the\ncorresponding image shape.

    \n
    >>> screen.register_shape("turtle.gif")\n
    \n
    \n
    \n

    Note

    \n

    Image shapes do not rotate when turning the turtle, so they do not\ndisplay the heading of the turtle!

    \n
    \n
  2. \n
  3. name is an arbitrary string and shape is a tuple of pairs of\ncoordinates: Install the corresponding polygon shape.

    \n
    >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))\n
    \n
    \n
  4. \n
  5. name is an arbitrary string and shape is a (compound) Shape\nobject: Install the corresponding compound shape.

    \n
  6. \n
\n

Add a turtle shape to TurtleScreen’s shapelist. Only thusly registered\nshapes can be used by issuing the command shape(shapename).

\n
\n\n
\n
\nturtle.turtles()
\n

Return the list of turtles on the screen.

\n
>>> for turtle in screen.turtles():\n...     turtle.color("red")\n
\n
\n
\n\n
\n
\nturtle.window_height()
\n

Return the height of the turtle window.

\n
>>> screen.window_height()\n480\n
\n
\n
\n\n
\n
\nturtle.window_width()
\n

Return the width of the turtle window.

\n
>>> screen.window_width()\n640\n
\n
\n
\n\n
\n
\n

24.5.4.5. Methods specific to Screen, not inherited from TurtleScreen

\n
\n
\nturtle.bye()
\n
Shut the turtlegraphics window.
\n\n
\n
\nturtle.exitonclick()
\n

Bind bye() method to mouse clicks on the Screen.

\n

If the value “using_IDLE” in the configuration dictionary is False\n(default value), also enter mainloop. Remark: If IDLE with the -n switch\n(no subprocess) is used, this value should be set to True in\nturtle.cfg. In this case IDLE’s own mainloop is active also for the\nclient script.

\n
\n\n
\n
\nturtle.setup(width=_CFG[, "width"], height=_CFG[, "height"], startx=_CFG[, "leftright"], starty=_CFG[, "topbottom"])
\n

Set the size and position of the main window. Default values of arguments\nare stored in the configuration dictionary and can be changed via a\nturtle.cfg file.

\n\n\n\n\n\n\n\n
Parameters:
    \n
  • width – if an integer, a size in pixels, if a float, a fraction of the\nscreen; default is 50% of screen
  • \n
  • height – if an integer, the height in pixels, if a float, a fraction of\nthe screen; default is 75% of screen
  • \n
  • startx – if positive, starting position in pixels from the left\nedge of the screen, if negative from the right edge, if None,\ncenter window horizontally
  • \n
  • startx – if positive, starting position in pixels from the top\nedge of the screen, if negative from the bottom edge, if None,\ncenter window vertically
  • \n
\n
\n
>>> screen.setup (width=200, height=200, startx=0, starty=0)\n>>>              # sets window to 200x200 pixels, in upper left of screen\n>>> screen.setup(width=.75, height=0.5, startx=None, starty=None)\n>>>              # sets window to 75% of screen by 50% of screen and centers\n
\n
\n
\n\n
\n
\nturtle.title(titlestring)
\n
\n\n\n\n\n\n\n
Parameter:titlestring – a string that is shown in the titlebar of the turtle\ngraphics window
\n

Set title of turtle window to titlestring.

\n
>>> screen.title("Welcome to the turtle zoo!")\n
\n
\n
\n\n
\n
\n
\n

24.5.5. The public classes of the module turtle

\n
\n
\nclass turtle.RawTurtle(canvas)
\n
\nclass turtle.RawPen(canvas)
\n
\n\n\n\n\n\n\n
Parameter:canvas – a Tkinter.Canvas, a ScrolledCanvas or a\nTurtleScreen
\n

Create a turtle. The turtle has all methods described above as “methods of\nTurtle/RawTurtle”.

\n
\n\n
\n
\nclass turtle.Turtle
\n
Subclass of RawTurtle, has the same interface but draws on a default\nScreen object created automatically when needed for the first time.
\n\n
\n
\nclass turtle.TurtleScreen(cv)
\n
\n\n\n\n\n\n\n
Parameter:cv – a Tkinter.Canvas
\n

Provides screen oriented methods like setbg() etc. that are described\nabove.

\n
\n\n
\n
\nclass turtle.Screen
\n
Subclass of TurtleScreen, with four methods added.
\n\n
\n
\nclass turtle.ScrolledCanvas(master)
\n
\n\n\n\n\n\n\n
Parameter:master – some Tkinter widget to contain the ScrolledCanvas, i.e.\na Tkinter-canvas with scrollbars added
\n

Used by class Screen, which thus automatically provides a ScrolledCanvas as\nplayground for the turtles.

\n
\n\n
\n
\nclass turtle.Shape(type_, data)
\n
\n\n\n\n\n\n\n
Parameter:type_ – one of the strings “polygon”, “image”, “compound”
\n

Data structure modeling shapes. The pair (type_, data) must follow this\nspecification:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
type_data
“polygon”a polygon-tuple, i.e. a tuple of pairs of coordinates
“image”an image (in this form only used internally!)
“compound”None (a compound shape has to be constructed using the\naddcomponent() method)
\n
\n
\naddcomponent(poly, fill, outline=None)
\n
\n\n\n\n\n\n\n
Parameters:
    \n
  • poly – a polygon, i.e. a tuple of pairs of numbers
  • \n
  • fill – a color the poly will be filled with
  • \n
  • outline – a color for the poly’s outline (if given)
  • \n
\n
\n

Example:

\n
>>> poly = ((0,0),(10,-5),(0,10),(-10,-5))\n>>> s = Shape("compound")\n>>> s.addcomponent(poly, "red", "blue")\n>>> # ... add more components and then use register_shape()\n
\n
\n

See Excursus about the use of compound shapes.

\n
\n\n
\n\n
\n
\nclass turtle.Vec2D(x, y)
\n

A two-dimensional vector class, used as a helper class for implementing\nturtle graphics. May be useful for turtle graphics programs too. Derived\nfrom tuple, so a vector is a tuple!

\n

Provides (for a, b vectors, k number):

\n
    \n
  • a + b vector addition
  • \n
  • a - b vector subtraction
  • \n
  • a * b inner product
  • \n
  • k * a and a * k multiplication with scalar
  • \n
  • abs(a) absolute value of a
  • \n
  • a.rotate(angle) rotation
  • \n
\n
\n\n
\n
\n

24.5.6. Help and configuration

\n
\n

24.5.6.1. How to use help

\n

The public methods of the Screen and Turtle classes are documented extensively\nvia docstrings. So these can be used as online-help via the Python help\nfacilities:

\n
    \n
  • When using IDLE, tooltips show the signatures and first lines of the\ndocstrings of typed in function-/method calls.

    \n
  • \n
  • Calling help() on methods or functions displays the docstrings:

    \n
    >>> help(Screen.bgcolor)\nHelp on method bgcolor in module turtle:\n\nbgcolor(self, *args) unbound turtle.Screen method\n    Set or return backgroundcolor of the TurtleScreen.\n\n    Arguments (if given): a color string or three numbers\n    in the range 0..colormode or a 3-tuple of such numbers.\n\n\n      >>> screen.bgcolor("orange")\n      >>> screen.bgcolor()\n      "orange"\n      >>> screen.bgcolor(0.5,0,0.5)\n      >>> screen.bgcolor()\n      "#800080"\n\n>>> help(Turtle.penup)\nHelp on method penup in module turtle:\n\npenup(self) unbound turtle.Turtle method\n    Pull the pen up -- no drawing when moving.\n\n    Aliases: penup | pu | up\n\n    No argument\n\n    >>> turtle.penup()\n
    \n
    \n
  • \n
  • The docstrings of the functions which are derived from methods have a modified\nform:

    \n
    >>> help(bgcolor)\nHelp on function bgcolor in module turtle:\n\nbgcolor(*args)\n    Set or return backgroundcolor of the TurtleScreen.\n\n    Arguments (if given): a color string or three numbers\n    in the range 0..colormode or a 3-tuple of such numbers.\n\n    Example::\n\n      >>> bgcolor("orange")\n      >>> bgcolor()\n      "orange"\n      >>> bgcolor(0.5,0,0.5)\n      >>> bgcolor()\n      "#800080"\n\n>>> help(penup)\nHelp on function penup in module turtle:\n\npenup()\n    Pull the pen up -- no drawing when moving.\n\n    Aliases: penup | pu | up\n\n    No argument\n\n    Example:\n    >>> penup()\n
    \n
    \n
  • \n
\n

These modified docstrings are created automatically together with the function\ndefinitions that are derived from the methods at import time.

\n
\n
\n

24.5.6.2. Translation of docstrings into different languages

\n

There is a utility to create a dictionary the keys of which are the method names\nand the values of which are the docstrings of the public methods of the classes\nScreen and Turtle.

\n
\n
\nturtle.write_docstringdict(filename="turtle_docstringdict")
\n
\n\n\n\n\n\n\n
Parameter:filename – a string, used as filename
\n

Create and write docstring-dictionary to a Python script with the given\nfilename. This function has to be called explicitly (it is not used by the\nturtle graphics classes). The docstring dictionary will be written to the\nPython script filename.py. It is intended to serve as a template\nfor translation of the docstrings into different languages.

\n
\n\n

If you (or your students) want to use turtle with online help in your\nnative language, you have to translate the docstrings and save the resulting\nfile as e.g. turtle_docstringdict_german.py.

\n

If you have an appropriate entry in your turtle.cfg file this dictionary\nwill be read in at import time and will replace the original English docstrings.

\n

At the time of this writing there are docstring dictionaries in German and in\nItalian. (Requests please to glingl@aon.at.)

\n
\n
\n

24.5.6.3. How to configure Screen and Turtles

\n

The built-in default configuration mimics the appearance and behaviour of the\nold turtle module in order to retain best possible compatibility with it.

\n

If you want to use a different configuration which better reflects the features\nof this module or which better fits to your needs, e.g. for use in a classroom,\nyou can prepare a configuration file turtle.cfg which will be read at import\ntime and modify the configuration according to its settings.

\n

The built in configuration would correspond to the following turtle.cfg:

\n
width = 0.5\nheight = 0.75\nleftright = None\ntopbottom = None\ncanvwidth = 400\ncanvheight = 300\nmode = standard\ncolormode = 1.0\ndelay = 10\nundobuffersize = 1000\nshape = classic\npencolor = black\nfillcolor = black\nresizemode = noresize\nvisible = True\nlanguage = english\nexampleturtle = turtle\nexamplescreen = screen\ntitle = Python Turtle Graphics\nusing_IDLE = False
\n
\n

Short explanation of selected entries:

\n
    \n
  • The first four lines correspond to the arguments of the Screen.setup()\nmethod.
  • \n
  • Line 5 and 6 correspond to the arguments of the method\nScreen.screensize().
  • \n
  • shape can be any of the built-in shapes, e.g: arrow, turtle, etc. For more\ninfo try help(shape).
  • \n
  • If you want to use no fillcolor (i.e. make the turtle transparent), you have\nto write fillcolor = "" (but all nonempty strings must not have quotes in\nthe cfg-file).
  • \n
  • If you want to reflect the turtle its state, you have to use resizemode =\nauto.
  • \n
  • If you set e.g. language = italian the docstringdict\nturtle_docstringdict_italian.py will be loaded at import time (if\npresent on the import path, e.g. in the same directory as turtle.
  • \n
  • The entries exampleturtle and examplescreen define the names of these\nobjects as they occur in the docstrings. The transformation of\nmethod-docstrings to function-docstrings will delete these names from the\ndocstrings.
  • \n
  • using_IDLE: Set this to True if you regularly work with IDLE and its -n\nswitch (“no subprocess”). This will prevent exitonclick() to enter the\nmainloop.
  • \n
\n

There can be a turtle.cfg file in the directory where turtle is\nstored and an additional one in the current working directory. The latter will\noverride the settings of the first one.

\n

The Demo/turtle directory contains a turtle.cfg file. You can\nstudy it as an example and see its effects when running the demos (preferably\nnot from within the demo-viewer).

\n
\n
\n
\n

24.5.7. Demo scripts

\n

There is a set of demo scripts in the turtledemo directory located in the\nDemo/turtle directory in the source distribution.

\n

It contains:

\n\n

The demoscripts are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionFeatures
bytedesigncomplex classical\nturtlegraphics patterntracer(), delay,\nupdate()
chaosgraphs Verhulst dynamics,\nshows that computer’s\ncomputations can generate\nresults sometimes against the\ncommon sense expectationsworld coordinates
clockanalog clock showing time\nof your computerturtles as clock’s\nhands, ontimer
colormixerexperiment with r, g, bondrag()
fractalcurvesHilbert & Koch curvesrecursion
lindenmayerethnomathematics\n(indian kolams)L-System
minimal_hanoiTowers of HanoiRectangular Turtles\nas Hanoi discs\n(shape, shapesize)
paintsuper minimalistic\ndrawing programonclick()
peaceelementaryturtle: appearance\nand animation
penroseaperiodic tiling with\nkites and dartsstamp()
planet_and_moonsimulation of\ngravitational systemcompound shapes,\nVec2D
treea (graphical) breadth\nfirst tree (using generators)clone()
wikipediaa pattern from the wikipedia\narticle on turtle graphicsclone(),\nundo()
yingyanganother elementary examplecircle()
\n

Have fun!

\n
\n
", "searchableItems": [ { "name": "turtle.back", "domId": "turtle_turtle.back" }, { "name": "turtle.begin_fill", "domId": "turtle_turtle.begin_fill" }, { "name": "turtle.begin_poly", "domId": "turtle_turtle.begin_poly" }, { "name": "turtle.bgcolor", "domId": "turtle_turtle.bgcolor" }, { "name": "turtle.bgpic", "domId": "turtle_turtle.bgpic" }, { "name": "turtle.bye", "domId": "turtle_turtle.bye" }, { "name": "turtle.circle", "domId": "turtle_turtle.circle" }, { "name": "turtle.clear", "domId": "turtle_turtle.clear" }, { "name": "turtle.clearstamp", "domId": "turtle_turtle.clearstamp" }, { "name": "turtle.clearstamps", "domId": "turtle_turtle.clearstamps" }, { "name": "turtle.clone", "domId": "turtle_turtle.clone" }, { "name": "turtle.color", "domId": "turtle_turtle.color" }, { "name": "turtle.colormode", "domId": "turtle_turtle.colormode" }, { "name": "turtle.degrees", "domId": "turtle_turtle.degrees" }, { "name": "turtle.delay", "domId": "turtle_turtle.delay" }, { "name": "turtle.distance", "domId": "turtle_turtle.distance" }, { "name": "turtle.dot", "domId": "turtle_turtle.dot" }, { "name": "turtle.end_fill", "domId": "turtle_turtle.end_fill" }, { "name": "turtle.end_poly", "domId": "turtle_turtle.end_poly" }, { "name": "turtle.exitonclick", "domId": "turtle_turtle.exitonclick" }, { "name": "turtle.fill", "domId": "turtle_turtle.fill" }, { "name": "turtle.fillcolor", "domId": "turtle_turtle.fillcolor" }, { "name": "turtle.forward", "domId": "turtle_turtle.forward" }, { "name": "turtle.get_poly", "domId": "turtle_turtle.get_poly" }, { "name": "turtle.getcanvas", "domId": "turtle_turtle.getcanvas" }, { "name": "turtle.getscreen", "domId": "turtle_turtle.getscreen" }, { "name": "turtle.getshapes", "domId": "turtle_turtle.getshapes" }, { "name": "turtle.getturtle", "domId": "turtle_turtle.getturtle" }, { "name": "turtle.goto", "domId": "turtle_turtle.goto" }, { "name": "turtle.heading", "domId": "turtle_turtle.heading" }, { "name": "turtle.hideturtle", "domId": "turtle_turtle.hideturtle" }, { "name": "turtle.home", "domId": "turtle_turtle.home" }, { "name": "turtle.isdown", "domId": "turtle_turtle.isdown" }, { "name": "turtle.isvisible", "domId": "turtle_turtle.isvisible" }, { "name": "turtle.left", "domId": "turtle_turtle.left" }, { "name": "turtle.listen", "domId": "turtle_turtle.listen" }, { "name": "turtle.mainloop", "domId": "turtle_turtle.mainloop" }, { "name": "turtle.mode", "domId": "turtle_turtle.mode" }, { "name": "turtle.onclick", "domId": "turtle_turtle.onclick" }, { "name": "turtle.ondrag", "domId": "turtle_turtle.ondrag" }, { "name": "turtle.onkey", "domId": "turtle_turtle.onkey" }, { "name": "turtle.onrelease", "domId": "turtle_turtle.onrelease" }, { "name": "turtle.ontimer", "domId": "turtle_turtle.ontimer" }, { "name": "turtle.pen", "domId": "turtle_turtle.pen" }, { "name": "turtle.pencolor", "domId": "turtle_turtle.pencolor" }, { "name": "turtle.pendown", "domId": "turtle_turtle.pendown" }, { "name": "turtle.pensize", "domId": "turtle_turtle.pensize" }, { "name": "turtle.penup", "domId": "turtle_turtle.penup" }, { "name": "turtle.position", "domId": "turtle_turtle.position" }, { "name": "turtle.radians", "domId": "turtle_turtle.radians" }, { "name": "turtle.RawTurtle", "domId": "turtle_turtle.RawTurtle" }, { "name": "turtle.register_shape", "domId": "turtle_turtle.register_shape" }, { "name": "turtle.reset", "domId": "turtle_turtle.reset" }, { "name": "turtle.resizemode", "domId": "turtle_turtle.resizemode" }, { "name": "turtle.right", "domId": "turtle_turtle.right" }, { "name": "turtle.Screen", "domId": "turtle_turtle.Screen" }, { "name": "turtle.screensize", "domId": "turtle_turtle.screensize" }, { "name": "turtle.ScrolledCanvas", "domId": "turtle_turtle.ScrolledCanvas" }, { "name": "turtle.setheading", "domId": "turtle_turtle.setheading" }, { "name": "turtle.settiltangle", "domId": "turtle_turtle.settiltangle" }, { "name": "turtle.setundobuffer", "domId": "turtle_turtle.setundobuffer" }, { "name": "turtle.setup", "domId": "turtle_turtle.setup" }, { "name": "turtle.setworldcoordinates", "domId": "turtle_turtle.setworldcoordinates" }, { "name": "turtle.setx", "domId": "turtle_turtle.setx" }, { "name": "turtle.sety", "domId": "turtle_turtle.sety" }, { "name": "turtle.Shape", "domId": "turtle_turtle_turtle.Shape" }, { "name": "turtle.shape", "domId": "turtle_turtle_turtle.shape" }, { "name": "turtle.Shape", "domId": "turtle_turtle_turtle.Shape" }, { "name": "turtle.shape", "domId": "turtle_turtle_turtle.shape" }, { "name": "turtle.Shape.addcomponent", "domId": "turtle_turtle.Shape.addcomponent" }, { "name": "turtle.shapesize", "domId": "turtle_turtle.shapesize" }, { "name": "turtle.showturtle", "domId": "turtle_turtle.showturtle" }, { "name": "turtle.speed", "domId": "turtle_turtle.speed" }, { "name": "turtle.stamp", "domId": "turtle_turtle.stamp" }, { "name": "turtle.tilt", "domId": "turtle_turtle.tilt" }, { "name": "turtle.tiltangle", "domId": "turtle_turtle.tiltangle" }, { "name": "turtle.title", "domId": "turtle_turtle.title" }, { "name": "turtle.towards", "domId": "turtle_turtle.towards" }, { "name": "turtle.tracer", "domId": "turtle_turtle.tracer" }, { "name": "turtle.Turtle", "domId": "turtle_turtle.Turtle" }, { "name": "turtle.turtles", "domId": "turtle_turtle.turtles" }, { "name": "turtle.TurtleScreen", "domId": "turtle_turtle.TurtleScreen" }, { "name": "turtle.undo", "domId": "turtle_turtle.undo" }, { "name": "turtle.undobufferentries", "domId": "turtle_turtle.undobufferentries" }, { "name": "turtle.update", "domId": "turtle_turtle.update" }, { "name": "turtle.Vec2D", "domId": "turtle_turtle.Vec2D" }, { "name": "turtle.window_width", "domId": "turtle_turtle.window_width" }, { "name": "turtle.write", "domId": "turtle_turtle.write" }, { "name": "turtle.write_docstringdict", "domId": "turtle_turtle.write_docstringdict" }, { "name": "turtle.xcor", "domId": "turtle_turtle.xcor" }, { "name": "turtle.ycor", "domId": "turtle_turtle.ycor" } ] }, { "url": "http://docs.python.org/library/2to3.html", "title": "", "html": "
\n

25.4. 2to3 - Automated Python 2 to 3 code translation

\n

2to3 is a Python program that reads Python 2.x source code and applies a series\nof fixers to transform it into valid Python 3.x code. The standard library\ncontains a rich set of fixers that will handle almost all code. 2to3 supporting\nlibrary lib2to3 is, however, a flexible and generic library, so it is\npossible to write your own fixers for 2to3. lib2to3 could also be\nadapted to custom applications in which Python code needs to be edited\nautomatically.

\n
\n

25.4.1. Using 2to3

\n

2to3 will usually be installed with the Python interpreter as a script. It is\nalso located in the Tools/scripts directory of the Python root.

\n

2to3’s basic arguments are a list of files or directories to transform. The\ndirectories are to recursively traversed for Python sources.

\n

Here is a sample Python 2.x source file, example.py:

\n
def greet(name):\n    print "Hello, {0}!".format(name)\nprint "What's your name?"\nname = raw_input()\ngreet(name)\n
\n
\n

It can be converted to Python 3.x code via 2to3 on the command line:

\n
$ 2to3 example.py
\n
\n

A diff against the original source file is printed. 2to3 can also write the\nneeded modifications right back to the source file. (A backup of the original\nfile is made unless -n is also given.) Writing the changes back is\nenabled with the -w flag:

\n
$ 2to3 -w example.py
\n
\n

After transformation, example.py looks like this:

\n
def greet(name):\n    print("Hello, {0}!".format(name))\nprint("What's your name?")\nname = input()\ngreet(name)\n
\n
\n

Comments and exact indentation are preserved throughout the translation process.

\n

By default, 2to3 runs a set of predefined fixers. The\n-l flag lists all available fixers. An explicit set of fixers to run\ncan be given with -f. Likewise the -x explicitly disables a\nfixer. The following example runs only the imports and has_key fixers:

\n
$ 2to3 -f imports -f has_key example.py
\n
\n

This command runs every fixer except the apply fixer:

\n
$ 2to3 -x apply example.py
\n
\n

Some fixers are explicit, meaning they aren’t run by default and must be\nlisted on the command line to be run. Here, in addition to the default fixers,\nthe idioms fixer is run:

\n
$ 2to3 -f all -f idioms example.py
\n
\n

Notice how passing all enables all default fixers.

\n

Sometimes 2to3 will find a place in your source code that needs to be changed,\nbut 2to3 cannot fix automatically. In this case, 2to3 will print a warning\nbeneath the diff for a file. You should address the warning in order to have\ncompliant 3.x code.

\n

2to3 can also refactor doctests. To enable this mode, use the -d\nflag. Note that only doctests will be refactored. This also doesn’t require\nthe module to be valid Python. For example, doctest like examples in a reST\ndocument could also be refactored with this option.

\n

The -v option enables output of more information on the translation\nprocess.

\n

Since some print statements can be parsed as function calls or statements, 2to3\ncannot always read files containing the print function. When 2to3 detects the\npresence of the from __future__ import print_function compiler directive, it\nmodifies its internal grammar to interpret print() as a function. This\nchange can also be enabled manually with the -p flag. Use\n-p to run fixers on code that already has had its print statements\nconverted.

\n
\n
\n

25.4.2. Fixers

\n

Each step of transforming code is encapsulated in a fixer. The command 2to3\n-l lists them. As documented above, each can be turned on\nand off individually. They are described here in more detail.

\n
\n
\napply
\n
Removes usage of apply(). For example apply(function, *args,\n**kwargs) is converted to function(*args, **kwargs).
\n\n
\n
\nbasestring
\n
Converts basestring to str.
\n\n
\n
\nbuffer
\n
Converts buffer to memoryview. This fixer is optional\nbecause the memoryview API is similar but not exactly the same as\nthat of buffer.
\n\n
\n
\ncallable
\n
Converts callable(x) to isinstance(x, collections.Callable), adding\nan import to collections if needed. Note callable(x) has returned\nin Python 3.2, so if you do not intend to support Python 3.1, you can disable\nthis fixer.
\n\n
\n
\ndict
\n
Fixes dictionary iteration methods. dict.iteritems() is converted to\ndict.items(), dict.iterkeys() to dict.keys(), and\ndict.itervalues() to dict.values(). Similarly,\ndict.viewitems(), dict.viewkeys() and dict.viewvalues() are\nconverted respectively to dict.items(), dict.keys() and\ndict.values(). It also wraps existing usages of dict.items(),\ndict.keys(), and dict.values() in a call to list.
\n\n
\n
\nexcept
\n
Converts except X, T to except X as T.
\n\n
\n
\nexec
\n
Converts the exec statement to the exec() function.
\n\n
\n
\nexecfile
\n
Removes usage of execfile(). The argument to execfile() is\nwrapped in calls to open(), compile(), and exec().
\n\n
\n
\nexitfunc
\n
Changes assignment of sys.exitfunc to use of the atexit\nmodule.
\n\n
\n
\nfilter
\n
Wraps filter() usage in a list call.
\n\n
\n
\nfuncattrs
\n
Fixes function attributes that have been renamed. For example,\nmy_function.func_closure is converted to my_function.__closure__.
\n\n
\n
\nfuture
\n
Removes from __future__ import new_feature statements.
\n\n
\n
\ngetcwdu
\n
Renames os.getcwdu() to os.getcwd().
\n\n
\n
\nhas_key
\n
Changes dict.has_key(key) to key in dict.
\n\n
\n
\nidioms
\n

This optional fixer performs several transformations that make Python code\nmore idiomatic. Type comparisons like type(x) is SomeClass and\ntype(x) == SomeClass are converted to isinstance(x, SomeClass).\nwhile 1 becomes while True. This fixer also tries to make use of\nsorted() in appropriate places. For example, this block

\n
L = list(some_iterable)\nL.sort()\n
\n
\n

is changed to

\n
L = sorted(some_iterable)\n
\n
\n
\n\n
\n
\nimport
\n
Detects sibling imports and converts them to relative imports.
\n\n
\n
\nimports
\n
Handles module renames in the standard library.
\n\n
\n
\nimports2
\n
Handles other modules renames in the standard library. It is separate from\nthe imports fixer only because of technical limitations.
\n\n
\n
\ninput
\n
Converts input(prompt) to eval(input(prompt))
\n\n
\n
\nintern
\n
Converts intern() to sys.intern().
\n\n
\n
\nisinstance
\n
Fixes duplicate types in the second argument of isinstance(). For\nexample, isinstance(x, (int, int)) is converted to isinstance(x,\n(int)).
\n\n
\n
\nitertools_imports
\n
Removes imports of itertools.ifilter(), itertools.izip(), and\nitertools.imap(). Imports of itertools.ifilterfalse() are also\nchanged to itertools.filterfalse().
\n\n
\n
\nitertools
\n
Changes usage of itertools.ifilter(), itertools.izip(), and\nitertools.imap() to their built-in equivalents.\nitertools.ifilterfalse() is changed to itertools.filterfalse().
\n\n
\n
\nlong
\n
Strips the L suffix on long literals and renames long to\nint.
\n\n
\n
\nmap
\n
Wraps map() in a list call. It also changes map(None, x)\nto list(x). Using from future_builtins import map disables this\nfixer.
\n\n
\n
\nmetaclass
\n
Converts the old metaclass syntax (__metaclass__ = Meta in the class\nbody) to the new (class X(metaclass=Meta)).
\n\n
\n
\nmethodattrs
\n
Fixes old method attribute names. For example, meth.im_func is converted\nto meth.__func__.
\n\n
\n
\nne
\n
Converts the old not-equal syntax, <>, to !=.
\n\n
\n
\nnext
\n
Converts the use of iterator’s next() methods to the\nnext() function. It also renames next() methods to\n__next__().
\n\n
\n
\nnonzero
\n
Renames __nonzero__() to __bool__().
\n\n
\n
\nnumliterals
\n
Converts octal literals into the new syntax.
\n\n
\n
\nparen
\n
Add extra parenthesis where they are required in list comprehensions. For\nexample, [x for x in 1, 2] becomes [x for x in (1, 2)].
\n\n
\n
\nprint
\n
Converts the print statement to the print() function.
\n\n
\n
\nraise
\n
Converts raise E, V to raise E(V), and raise E, V, T to raise\nE(V).with_traceback(T). If E is a tuple, the translation will be\nincorrect because substituting tuples for exceptions has been removed in 3.0.
\n\n
\n
\nraw_input
\n
Converts raw_input() to input().
\n\n
\n
\nreduce
\n
Handles the move of reduce() to functools.reduce().
\n\n
\n
\nrenames
\n
Changes sys.maxint to sys.maxsize.
\n\n
\n
\nrepr
\n
Replaces backtick repr with the repr() function.
\n\n
\n
\nset_literal
\n
Replaces use of the set constructor with set literals. This fixer\nis optional.
\n\n
\n
\nstandard_error
\n
Renames StandardError to Exception.
\n\n
\n
\nsys_exc
\n
Changes the deprecated sys.exc_value, sys.exc_type,\nsys.exc_traceback to use sys.exc_info().
\n\n
\n
\nthrow
\n
Fixes the API change in generator’s throw() method.
\n\n
\n
\ntuple_params
\n
Removes implicit tuple parameter unpacking. This fixer inserts temporary\nvariables.
\n\n
\n
\ntypes
\n
Fixes code broken from the removal of some members in the types\nmodule.
\n\n
\n
\nunicode
\n
Renames unicode to str.
\n\n
\n
\nurllib
\n
Handles the rename of urllib and urllib2 to the urllib\npackage.
\n\n
\n
\nws_comma
\n
Removes excess whitespace from comma separated items. This fixer is\noptional.
\n\n
\n
\nxrange
\n
Renames xrange() to range() and wraps existing range()\ncalls with list.
\n\n
\n
\nxreadlines
\n
Changes for x in file.xreadlines() to for x in file.
\n\n
\n
\nzip
\n
Wraps zip() usage in a list call. This is disabled when\nfrom future_builtins import zip appears.
\n\n
\n
\n

25.4.3. lib2to3 - 2to3’s library

\n
\n

Note

\n

The lib2to3 API should be considered unstable and may change\ndrastically in the future.

\n
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/test.html", "title": "test", "html": "
\n

25.5. test — Regression tests package for Python

\n
\n

Note

\n

The test package is meant for internal use by Python only. It is\ndocumented for the benefit of the core developers of Python. Any use of\nthis package outside of Python’s standard library is discouraged as code\nmentioned here can change or be removed without notice between releases of\nPython.

\n
\n

The test package contains all regression tests for Python as well as the\nmodules test.test_support and test.regrtest.\ntest.test_support is used to enhance your tests while\ntest.regrtest drives the testing suite.

\n

Each module in the test package whose name starts with test_ is a\ntesting suite for a specific module or feature. All new tests should be written\nusing the unittest or doctest module. Some older tests are\nwritten using a “traditional” testing style that compares output printed to\nsys.stdout; this style of test is considered deprecated.

\n
\n

See also

\n
\n
Module unittest
\n
Writing PyUnit regression tests.
\n
Module doctest
\n
Tests embedded in documentation strings.
\n
\n
\n
\n

25.5.1. Writing Unit Tests for the test package

\n

It is preferred that tests that use the unittest module follow a few\nguidelines. One is to name the test module by starting it with test_ and end\nit with the name of the module being tested. The test methods in the test module\nshould start with test_ and end with a description of what the method is\ntesting. This is needed so that the methods are recognized by the test driver as\ntest methods. Also, no documentation string for the method should be included. A\ncomment (such as # Tests function returns only True or False) should be used\nto provide documentation for test methods. This is done because documentation\nstrings get printed out if they exist and thus what test is being run is not\nstated.

\n

A basic boilerplate is often used:

\n
import unittest\nfrom test import test_support\n\nclass MyTestCase1(unittest.TestCase):\n\n    # Only use setUp() and tearDown() if necessary\n\n    def setUp(self):\n        ... code to execute in preparation for tests ...\n\n    def tearDown(self):\n        ... code to execute to clean up after tests ...\n\n    def test_feature_one(self):\n        # Test feature one.\n        ... testing code ...\n\n    def test_feature_two(self):\n        # Test feature two.\n        ... testing code ...\n\n    ... more test methods ...\n\nclass MyTestCase2(unittest.TestCase):\n    ... same structure as MyTestCase1 ...\n\n... more test classes ...\n\ndef test_main():\n    test_support.run_unittest(MyTestCase1,\n                              MyTestCase2,\n                              ... list other tests ...\n                             )\n\nif __name__ == '__main__':\n    test_main()\n
\n
\n

This boilerplate code allows the testing suite to be run by test.regrtest\nas well as on its own as a script.

\n

The goal for regression testing is to try to break code. This leads to a few\nguidelines to be followed:

\n\n
\n

See also

\n
\n
Test Driven Development
\n
A book by Kent Beck on writing tests before code.
\n
\n
\n
\n
\n

25.5.2. Running tests using the command-line interface

\n

The test.regrtest module can be run as a script to drive Python’s regression\ntest suite, thanks to the -m option: python -m test.regrtest.\nRunning the script by itself automatically starts running all regression\ntests in the test package. It does this by finding all modules in the\npackage whose name starts with test_, importing them, and executing the\nfunction test_main() if present. The names of tests to execute may also\nbe passed to the script. Specifying a single regression test (python\n-m test.regrtest test_spam) will minimize output and only print whether\nthe test passed or failed and thus minimize output.

\n

Running test.regrtest directly allows what resources are available for\ntests to use to be set. You do this by using the -u command-line\noption. Run python -m test.regrtest -uall to turn on all\nresources; specifying all as an option for -u enables all\npossible resources. If all but one resource is desired (a more common case), a\ncomma-separated list of resources that are not desired may be listed after\nall. The command python -m test.regrtest -uall,-audio,-largefile\nwill run test.regrtest with all resources except the audio and\nlargefile resources. For a list of all resources and more command-line\noptions, run python -m test.regrtest -h.

\n

Some other ways to execute the regression tests depend on what platform the\ntests are being executed on. On Unix, you can run make test at the\ntop-level directory where Python was built. On Windows, executing\nrt.bat from your PCBuild directory will run all regression\ntests.

\n
\n
\n

25.6. test.test_support — Utility functions for tests

\n
\n

Note

\n

The test.test_support module has been renamed to test.support\nin Python 3.x.

\n
\n

The test.test_support module provides support for Python’s regression\ntests.

\n

This module defines the following exceptions:

\n
\n
\nexception test.test_support.TestFailed
\n
Exception to be raised when a test fails. This is deprecated in favor of\nunittest-based tests and unittest.TestCase‘s assertion\nmethods.
\n\n
\n
\nexception test.test_support.ResourceDenied
\n
Subclass of unittest.SkipTest. Raised when a resource (such as a\nnetwork connection) is not available. Raised by the requires()\nfunction.
\n\n

The test.test_support module defines the following constants:

\n
\n
\ntest.test_support.verbose
\n
True when verbose output is enabled. Should be checked when more\ndetailed information is desired about a running test. verbose is set by\ntest.regrtest.
\n\n
\n
\ntest.test_support.have_unicode
\n
True when Unicode support is available.
\n\n
\n
\ntest.test_support.is_jython
\n
True if the running interpreter is Jython.
\n\n
\n
\ntest.test_support.TESTFN
\n
Set to a name that is safe to use as the name of a temporary file. Any\ntemporary file that is created should be closed and unlinked (removed).
\n\n

The test.test_support module defines the following functions:

\n
\n
\ntest.test_support.forget(module_name)
\n
Remove the module named module_name from sys.modules and delete any\nbyte-compiled files of the module.
\n\n
\n
\ntest.test_support.is_resource_enabled(resource)
\n
Return True if resource is enabled and available. The list of\navailable resources is only set when test.regrtest is executing the\ntests.
\n\n
\n
\ntest.test_support.requires(resource[, msg])
\n
Raise ResourceDenied if resource is not available. msg is the\nargument to ResourceDenied if it is raised. Always returns\nTrue if called by a function whose __name__ is '__main__'.\nUsed when tests are executed by test.regrtest.
\n\n
\n
\ntest.test_support.findfile(filename)
\n
Return the path to the file named filename. If no match is found\nfilename is returned. This does not equal a failure since it could be the\npath to the file.
\n\n
\n
\ntest.test_support.run_unittest(*classes)
\n

Execute unittest.TestCase subclasses passed to the function. The\nfunction scans the classes for methods starting with the prefix test_\nand executes the tests individually.

\n

It is also legal to pass strings as parameters; these should be keys in\nsys.modules. Each associated module will be scanned by\nunittest.TestLoader.loadTestsFromModule(). This is usually seen in the\nfollowing test_main() function:

\n
def test_main():\n    test_support.run_unittest(__name__)\n
\n
\n

This will run all tests defined in the named module.

\n
\n\n
\n
\ntest.test_support.check_warnings(*filters, quiet=True)
\n

A convenience wrapper for warnings.catch_warnings() that makes it\neasier to test that a warning was correctly raised. It is approximately\nequivalent to calling warnings.catch_warnings(record=True) with\nwarnings.simplefilter() set to always and with the option to\nautomatically validate the results that are recorded.

\n

check_warnings accepts 2-tuples of the form ("message regexp",\nWarningCategory) as positional arguments. If one or more filters are\nprovided, or if the optional keyword argument quiet is False,\nit checks to make sure the warnings are as expected: each specified filter\nmust match at least one of the warnings raised by the enclosed code or the\ntest fails, and if any warnings are raised that do not match any of the\nspecified filters the test fails. To disable the first of these checks,\nset quiet to True.

\n

If no arguments are specified, it defaults to:

\n
check_warnings(("", Warning), quiet=True)\n
\n
\n

In this case all warnings are caught and no errors are raised.

\n

On entry to the context manager, a WarningRecorder instance is\nreturned. The underlying warnings list from\ncatch_warnings() is available via the recorder object’s\nwarnings attribute. As a convenience, the attributes of the object\nrepresenting the most recent warning can also be accessed directly through\nthe recorder object (see example below). If no warning has been raised,\nthen any of the attributes that would otherwise be expected on an object\nrepresenting a warning will return None.

\n

The recorder object also has a reset() method, which clears the\nwarnings list.

\n

The context manager is designed to be used like this:

\n
with check_warnings(("assertion is always true", SyntaxWarning),\n                    ("", UserWarning)):\n    exec('assert(False, "Hey!")')\n    warnings.warn(UserWarning("Hide me!"))\n
\n
\n

In this case if either warning was not raised, or some other warning was\nraised, check_warnings() would raise an error.

\n

When a test needs to look more deeply into the warnings, rather than\njust checking whether or not they occurred, code like this can be used:

\n
with check_warnings(quiet=True) as w:\n    warnings.warn("foo")\n    assert str(w.args[0]) == "foo"\n    warnings.warn("bar")\n    assert str(w.args[0]) == "bar"\n    assert str(w.warnings[0].args[0]) == "foo"\n    assert str(w.warnings[1].args[0]) == "bar"\n    w.reset()\n    assert len(w.warnings) == 0\n
\n
\n

Here all warnings will be caught, and the test code tests the captured\nwarnings directly.

\n

\nNew in version 2.6.

\n

\nChanged in version 2.7: New optional arguments filters and quiet.

\n
\n\n
\n
\ntest.test_support.check_py3k_warnings(*filters, quiet=False)
\n

Similar to check_warnings(), but for Python 3 compatibility warnings.\nIf sys.py3kwarning == 1, it checks if the warning is effectively raised.\nIf sys.py3kwarning == 0, it checks that no warning is raised. It\naccepts 2-tuples of the form ("message regexp", WarningCategory) as\npositional arguments. When the optional keyword argument quiet is\nTrue, it does not fail if a filter catches nothing. Without\narguments, it defaults to:

\n
check_py3k_warnings(("", DeprecationWarning), quiet=False)\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\ntest.test_support.captured_stdout()
\n

This is a context manager that runs the with statement body using\na StringIO.StringIO object as sys.stdout. That object can be\nretrieved using the as clause of the with statement.

\n

Example use:

\n
with captured_stdout() as s:\n    print "hello"\nassert s.getvalue() == "hello"\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\ntest.test_support.import_module(name, deprecated=False)
\n

This function imports and returns the named module. Unlike a normal\nimport, this function raises unittest.SkipTest if the module\ncannot be imported.

\n

Module and package deprecation messages are suppressed during this import\nif deprecated is True.

\n

\nNew in version 2.7.

\n
\n\n
\n
\ntest.test_support.import_fresh_module(name, fresh=(), blocked=(), deprecated=False)
\n

This function imports and returns a fresh copy of the named Python module\nby removing the named module from sys.modules before doing the import.\nNote that unlike reload(), the original module is not affected by\nthis operation.

\n

fresh is an iterable of additional module names that are also removed\nfrom the sys.modules cache before doing the import.

\n

blocked is an iterable of module names that are replaced with 0\nin the module cache during the import to ensure that attempts to import\nthem raise ImportError.

\n

The named module and any modules named in the fresh and blocked\nparameters are saved before starting the import and then reinserted into\nsys.modules when the fresh import is complete.

\n

Module and package deprecation messages are suppressed during this import\nif deprecated is True.

\n

This function will raise unittest.SkipTest is the named module\ncannot be imported.

\n

Example use:

\n
# Get copies of the warnings module for testing without\n# affecting the version being used by the rest of the test suite\n# One copy uses the C implementation, the other is forced to use\n# the pure Python fallback implementation\npy_warnings = import_fresh_module('warnings', blocked=['_warnings'])\nc_warnings = import_fresh_module('warnings', fresh=['_warnings'])\n
\n
\n

\nNew in version 2.7.

\n
\n\n

The test.test_support module defines the following classes:

\n
\n
\nclass test.test_support.TransientResource(exc[, **kwargs])
\n

Instances are a context manager that raises ResourceDenied if the\nspecified exception type is raised. Any keyword arguments are treated as\nattribute/value pairs to be compared against any exception raised within the\nwith statement. Only if all pairs match properly against\nattributes on the exception is ResourceDenied raised.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nclass test.test_support.EnvironmentVarGuard
\n

Class used to temporarily set or unset environment variables. Instances can\nbe used as a context manager and have a complete dictionary interface for\nquerying/modifying the underlying os.environ. After exit from the\ncontext manager all changes to environment variables done through this\ninstance will be rolled back.

\n

\nNew in version 2.6.

\n

\nChanged in version 2.7: Added dictionary interface.

\n
\n\n
\n
\nEnvironmentVarGuard.set(envvar, value)
\n
Temporarily set the environment variable envvar to the value of\nvalue.
\n\n
\n
\nEnvironmentVarGuard.unset(envvar)
\n
Temporarily unset the environment variable envvar.
\n\n
\n
\nclass test.test_support.WarningsRecorder
\n

Class used to record warnings for unit tests. See documentation of\ncheck_warnings() above for more details.

\n

\nNew in version 2.6.

\n
\n\n
", "searchableItems": [ { "name": "test.test_support.captured_stdout", "domId": "test_test.test_support.captured_stdout" }, { "name": "test.test_support.check_py3k_warnings", "domId": "test_test.test_support.check_py3k_warnings" }, { "name": "test.test_support.check_warnings", "domId": "test_test.test_support.check_warnings" }, { "name": "test.test_support.EnvironmentVarGuard", "domId": "test_test.test_support.EnvironmentVarGuard" }, { "name": "test.test_support.EnvironmentVarGuard.set", "domId": "test_test.test_support.EnvironmentVarGuard.set" }, { "name": "test.test_support.EnvironmentVarGuard.unset", "domId": "test_test.test_support.EnvironmentVarGuard.unset" }, { "name": "test.test_support.findfile", "domId": "test_test.test_support.findfile" }, { "name": "test.test_support.forget", "domId": "test_test.test_support.forget" }, { "name": "test.test_support.import_fresh_module", "domId": "test_test.test_support.import_fresh_module" }, { "name": "test.test_support.import_module", "domId": "test_test.test_support.import_module" }, { "name": "test.test_support.is_resource_enabled", "domId": "test_test.test_support.is_resource_enabled" }, { "name": "test.test_support.requires", "domId": "test_test.test_support.requires" }, { "name": "test.test_support.run_unittest", "domId": "test_test.test_support.run_unittest" }, { "name": "test.test_support.TransientResource", "domId": "test_test.test_support.TransientResource" }, { "name": "test.test_support.WarningsRecorder", "domId": "test_test.test_support.WarningsRecorder" } ] }, { "url": "http://docs.python.org/library/doctest.html", "title": "doctest", "html": "
\n

25.2. doctest — Test interactive Python examples

\n

The doctest module searches for pieces of text that look like interactive\nPython sessions, and then executes those sessions to verify that they work\nexactly as shown. There are several common ways to use doctest:

\n\n

Here’s a complete but small example module:

\n
"""\nThis is the "example" module.\n\nThe example module supplies one function, factorial().  For example,\n\n>>> factorial(5)\n120\n"""\n\ndef factorial(n):\n    """Return the factorial of n, an exact integer >= 0.\n\n    If the result is small enough to fit in an int, return an int.\n    Else return a long.\n\n    >>> [factorial(n) for n in range(6)]\n    [1, 1, 2, 6, 24, 120]\n    >>> [factorial(long(n)) for n in range(6)]\n    [1, 1, 2, 6, 24, 120]\n    >>> factorial(30)\n    265252859812191058636308480000000L\n    >>> factorial(30L)\n    265252859812191058636308480000000L\n    >>> factorial(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: n must be >= 0\n\n    Factorials of floats are OK, but the float must be an exact integer:\n    >>> factorial(30.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: n must be exact integer\n    >>> factorial(30.0)\n    265252859812191058636308480000000L\n\n    It must also not be ridiculously large:\n    >>> factorial(1e100)\n    Traceback (most recent call last):\n        ...\n    OverflowError: n too large\n    """\n\n    import math\n    if not n >= 0:\n        raise ValueError("n must be >= 0")\n    if math.floor(n) != n:\n        raise ValueError("n must be exact integer")\n    if n+1 == n:  # catch a value like 1e300\n        raise OverflowError("n too large")\n    result = 1\n    factor = 2\n    while factor <= n:\n        result *= factor\n        factor += 1\n    return result\n\n\nif __name__ == "__main__":\n    import doctest\n    doctest.testmod()\n
\n
\n

If you run example.py directly from the command line, doctest\nworks its magic:

\n
$ python example.py\n$
\n
\n

There’s no output! That’s normal, and it means all the examples worked. Pass\n-v to the script, and doctest prints a detailed log of what\nit’s trying, and prints a summary at the end:

\n
$ python example.py -v\nTrying:\n    factorial(5)\nExpecting:\n    120\nok\nTrying:\n    [factorial(n) for n in range(6)]\nExpecting:\n    [1, 1, 2, 6, 24, 120]\nok\nTrying:\n    [factorial(long(n)) for n in range(6)]\nExpecting:\n    [1, 1, 2, 6, 24, 120]\nok
\n
\n

And so on, eventually ending with:

\n
Trying:\n    factorial(1e100)\nExpecting:\n    Traceback (most recent call last):\n        ...\n    OverflowError: n too large\nok\n2 items passed all tests:\n   1 tests in __main__\n   8 tests in __main__.factorial\n9 tests in 2 items.\n9 passed and 0 failed.\nTest passed.\n$
\n
\n

That’s all you need to know to start making productive use of doctest!\nJump in. The following sections provide full details. Note that there are many\nexamples of doctests in the standard Python test suite and libraries.\nEspecially useful examples can be found in the standard test file\nLib/test/test_doctest.py.

\n
\n

25.2.1. Simple Usage: Checking Examples in Docstrings

\n

The simplest way to start using doctest (but not necessarily the way you’ll\ncontinue to do it) is to end each module M with:

\n
if __name__ == "__main__":\n    import doctest\n    doctest.testmod()\n
\n
\n

doctest then examines docstrings in module M.

\n

Running the module as a script causes the examples in the docstrings to get\nexecuted and verified:

\n
python M.py
\n
\n

This won’t display anything unless an example fails, in which case the failing\nexample(s) and the cause(s) of the failure(s) are printed to stdout, and the\nfinal line of output is ***Test Failed*** N failures., where N is the\nnumber of examples that failed.

\n

Run it with the -v switch instead:

\n
python M.py -v
\n
\n

and a detailed report of all examples tried is printed to standard output, along\nwith assorted summaries at the end.

\n

You can force verbose mode by passing verbose=True to testmod(), or\nprohibit it by passing verbose=False. In either of those cases,\nsys.argv is not examined by testmod() (so passing -v or not\nhas no effect).

\n

Since Python 2.6, there is also a command line shortcut for running\ntestmod(). You can instruct the Python interpreter to run the doctest\nmodule directly from the standard library and pass the module name(s) on the\ncommand line:

\n
python -m doctest -v example.py
\n
\n

This will import example.py as a standalone module and run\ntestmod() on it. Note that this may not work correctly if the file is\npart of a package and imports other submodules from that package.

\n

For more information on testmod(), see section Basic API.

\n
\n
\n

25.2.2. Simple Usage: Checking Examples in a Text File

\n

Another simple application of doctest is testing interactive examples in a text\nfile. This can be done with the testfile() function:

\n
import doctest\ndoctest.testfile("example.txt")\n
\n
\n

That short script executes and verifies any interactive Python examples\ncontained in the file example.txt. The file content is treated as if it\nwere a single giant docstring; the file doesn’t need to contain a Python\nprogram! For example, perhaps example.txt contains this:

\n
The ``example`` module\n======================\n\nUsing ``factorial``\n-------------------\n\nThis is an example text file in reStructuredText format.  First import\n``factorial`` from the ``example`` module:\n\n    >>> from example import factorial\n\nNow use it:\n\n    >>> factorial(6)\n    120
\n
\n

Running doctest.testfile("example.txt") then finds the error in this\ndocumentation:

\n
File \"./example.txt\", line 14, in example.txt\nFailed example:\n    factorial(6)\nExpected:\n    120\nGot:\n    720
\n
\n

As with testmod(), testfile() won’t display anything unless an\nexample fails. If an example does fail, then the failing example(s) and the\ncause(s) of the failure(s) are printed to stdout, using the same format as\ntestmod().

\n

By default, testfile() looks for files in the calling module’s directory.\nSee section Basic API for a description of the optional arguments\nthat can be used to tell it to look for files in other locations.

\n

Like testmod(), testfile()‘s verbosity can be set with the\n-v command-line switch or with the optional keyword argument\nverbose.

\n

Since Python 2.6, there is also a command line shortcut for running\ntestfile(). You can instruct the Python interpreter to run the doctest\nmodule directly from the standard library and pass the file name(s) on the\ncommand line:

\n
python -m doctest -v example.txt
\n
\n

Because the file name does not end with .py, doctest infers that\nit must be run with testfile(), not testmod().

\n

For more information on testfile(), see section Basic API.

\n
\n
\n

25.2.3. How It Works

\n

This section examines in detail how doctest works: which docstrings it looks at,\nhow it finds interactive examples, what execution context it uses, how it\nhandles exceptions, and how option flags can be used to control its behavior.\nThis is the information that you need to know to write doctest examples; for\ninformation about actually running doctest on these examples, see the following\nsections.

\n
\n

25.2.3.1. Which Docstrings Are Examined?

\n

The module docstring, and all function, class and method docstrings are\nsearched. Objects imported into the module are not searched.

\n

In addition, if M.__test__ exists and “is true”, it must be a dict, and each\nentry maps a (string) name to a function object, class object, or string.\nFunction and class object docstrings found from M.__test__ are searched, and\nstrings are treated as if they were docstrings. In output, a key K in\nM.__test__ appears with name

\n
<name of M>.__test__.K
\n
\n

Any classes found are recursively searched similarly, to test docstrings in\ntheir contained methods and nested classes.

\n

\nChanged in version 2.4: A “private name” concept is deprecated and no longer documented.

\n
\n
\n

25.2.3.2. How are Docstring Examples Recognized?

\n

In most cases a copy-and-paste of an interactive console session works fine,\nbut doctest isn’t trying to do an exact emulation of any specific Python shell.

\n
>>> # comments are ignored\n>>> x = 12\n>>> x\n12\n>>> if x == 13:\n...     print "yes"\n... else:\n...     print "no"\n...     print "NO"\n...     print "NO!!!"\n...\nno\nNO\nNO!!!\n>>>\n
\n
\n

Any expected output must immediately follow the final '>>> ' or '... '\nline containing the code, and the expected output (if any) extends to the next\n'>>> ' or all-whitespace line.

\n

The fine print:

\n
    \n
  • Expected output cannot contain an all-whitespace line, since such a line is\ntaken to signal the end of expected output. If expected output does contain a\nblank line, put <BLANKLINE> in your doctest example each place a blank line\nis expected.

    \n

    \nNew in version 2.4: <BLANKLINE> was added; there was no way to use expected output containing\nempty lines in previous versions.

    \n
  • \n
  • All hard tab characters are expanded to spaces, using 8-column tab stops.\nTabs in output generated by the tested code are not modified. Because any\nhard tabs in the sample output are expanded, this means that if the code\noutput includes hard tabs, the only way the doctest can pass is if the\nNORMALIZE_WHITESPACE option or directive is in effect.\nAlternatively, the test can be rewritten to capture the output and compare it\nto an expected value as part of the test. This handling of tabs in the\nsource was arrived at through trial and error, and has proven to be the least\nerror prone way of handling them. It is possible to use a different\nalgorithm for handling tabs by writing a custom DocTestParser class.

    \n

    \nChanged in version 2.4: Expanding tabs to spaces is new; previous versions tried to preserve hard tabs,\nwith confusing results.

    \n
  • \n
  • Output to stdout is captured, but not output to stderr (exception tracebacks\nare captured via a different means).

    \n
  • \n
  • If you continue a line via backslashing in an interactive session, or for any\nother reason use a backslash, you should use a raw docstring, which will\npreserve your backslashes exactly as you type them:

    \n
    >>> def f(x):\n...     r'''Backslashes in a raw docstring: m\\n'''\n>>> print f.__doc__\nBackslashes in a raw docstring: m\\n\n
    \n
    \n

    Otherwise, the backslash will be interpreted as part of the string. For example,\nthe “\\” above would be interpreted as a newline character. Alternatively, you\ncan double each backslash in the doctest version (and not use a raw string):

    \n
    >>> def f(x):\n...     '''Backslashes in a raw docstring: m\\\\n'''\n>>> print f.__doc__\nBackslashes in a raw docstring: m\\n\n
    \n
    \n
  • \n
  • The starting column doesn’t matter:

    \n
    >>> assert "Easy!"\n      >>> import math\n          >>> math.floor(1.9)\n          1.0\n
    \n
    \n

    and as many leading whitespace characters are stripped from the expected output\nas appeared in the initial '>>> ' line that started the example.

    \n
  • \n
\n
\n
\n

25.2.3.3. What’s the Execution Context?

\n

By default, each time doctest finds a docstring to test, it uses a\nshallow copy of M‘s globals, so that running tests doesn’t change the\nmodule’s real globals, and so that one test in M can’t leave behind\ncrumbs that accidentally allow another test to work. This means examples can\nfreely use any names defined at top-level in M, and names defined earlier\nin the docstring being run. Examples cannot see names defined in other\ndocstrings.

\n

You can force use of your own dict as the execution context by passing\nglobs=your_dict to testmod() or testfile() instead.

\n
\n
\n

25.2.3.4. What About Exceptions?

\n

No problem, provided that the traceback is the only output produced by the\nexample: just paste in the traceback. [1] Since tracebacks contain details\nthat are likely to change rapidly (for example, exact file paths and line\nnumbers), this is one case where doctest works hard to be flexible in what it\naccepts.

\n

Simple example:

\n
>>> [1, 2, 3].remove(42)\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: list.remove(x): x not in list\n
\n
\n

That doctest succeeds if ValueError is raised, with the list.remove(x):\nx not in list detail as shown.

\n

The expected output for an exception must start with a traceback header, which\nmay be either of the following two lines, indented the same as the first line of\nthe example:

\n
Traceback (most recent call last):\nTraceback (innermost last):
\n
\n

The traceback header is followed by an optional traceback stack, whose contents\nare ignored by doctest. The traceback stack is typically omitted, or copied\nverbatim from an interactive session.

\n

The traceback stack is followed by the most interesting part: the line(s)\ncontaining the exception type and detail. This is usually the last line of a\ntraceback, but can extend across multiple lines if the exception has a\nmulti-line detail:

\n
>>> raise ValueError('multi\\n    line\\ndetail')\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nValueError: multi\n    line\ndetail\n
\n
\n

The last three lines (starting with ValueError) are compared against the\nexception’s type and detail, and the rest are ignored.

\n

\nChanged in version 2.4: Previous versions were unable to handle multi-line exception details.

\n

Best practice is to omit the traceback stack, unless it adds significant\ndocumentation value to the example. So the last example is probably better as:

\n
>>> raise ValueError('multi\\n    line\\ndetail')\nTraceback (most recent call last):\n    ...\nValueError: multi\n    line\ndetail\n
\n
\n

Note that tracebacks are treated very specially. In particular, in the\nrewritten example, the use of ... is independent of doctest’s\nELLIPSIS option. The ellipsis in that example could be left out, or\ncould just as well be three (or three hundred) commas or digits, or an indented\ntranscript of a Monty Python skit.

\n

Some details you should read once, but won’t need to remember:

\n
    \n
  • Doctest can’t guess whether your expected output came from an exception\ntraceback or from ordinary printing. So, e.g., an example that expects\nValueError: 42 is prime will pass whether ValueError is actually\nraised or if the example merely prints that traceback text. In practice,\nordinary output rarely begins with a traceback header line, so this doesn’t\ncreate real problems.

    \n
  • \n
  • Each line of the traceback stack (if present) must be indented further than\nthe first line of the example, or start with a non-alphanumeric character.\nThe first line following the traceback header indented the same and starting\nwith an alphanumeric is taken to be the start of the exception detail. Of\ncourse this does the right thing for genuine tracebacks.

    \n
  • \n
  • When the IGNORE_EXCEPTION_DETAIL doctest option is specified,\neverything following the leftmost colon and any module information in the\nexception name is ignored.

    \n
  • \n
  • The interactive shell omits the traceback header line for some\nSyntaxErrors. But doctest uses the traceback header line to\ndistinguish exceptions from non-exceptions. So in the rare case where you need\nto test a SyntaxError that omits the traceback header, you will need to\nmanually add the traceback header line to your test example.

    \n
  • \n
  • For some SyntaxErrors, Python displays the character position of the\nsyntax error, using a ^ marker:

    \n
    >>> 1 1\n  File "<stdin>", line 1\n    1 1\n      ^\nSyntaxError: invalid syntax\n
    \n
    \n

    Since the lines showing the position of the error come before the exception type\nand detail, they are not checked by doctest. For example, the following test\nwould pass, even though it puts the ^ marker in the wrong location:

    \n
    >>> 1 1\n  File "<stdin>", line 1\n    1 1\n    ^\nSyntaxError: invalid syntax\n
    \n
    \n
  • \n
\n
\n
\n

25.2.3.5. Option Flags and Directives

\n

A number of option flags control various aspects of doctest’s behavior.\nSymbolic names for the flags are supplied as module constants, which can be\nor’ed together and passed to various functions. The names can also be used in\ndoctest directives (see below).

\n

The first group of options define test semantics, controlling aspects of how\ndoctest decides whether actual output matches an example’s expected output:

\n
\n
\ndoctest.DONT_ACCEPT_TRUE_FOR_1
\n
By default, if an expected output block contains just 1, an actual output\nblock containing just 1 or just True is considered to be a match, and\nsimilarly for 0 versus False. When DONT_ACCEPT_TRUE_FOR_1 is\nspecified, neither substitution is allowed. The default behavior caters to that\nPython changed the return type of many functions from integer to boolean;\ndoctests expecting “little integer” output still work in these cases. This\noption will probably go away, but not for several years.
\n\n
\n
\ndoctest.DONT_ACCEPT_BLANKLINE
\n
By default, if an expected output block contains a line containing only the\nstring <BLANKLINE>, then that line will match a blank line in the actual\noutput. Because a genuinely blank line delimits the expected output, this is\nthe only way to communicate that a blank line is expected. When\nDONT_ACCEPT_BLANKLINE is specified, this substitution is not allowed.
\n\n
\n
\ndoctest.NORMALIZE_WHITESPACE
\n
When specified, all sequences of whitespace (blanks and newlines) are treated as\nequal. Any sequence of whitespace within the expected output will match any\nsequence of whitespace within the actual output. By default, whitespace must\nmatch exactly. NORMALIZE_WHITESPACE is especially useful when a line of\nexpected output is very long, and you want to wrap it across multiple lines in\nyour source.
\n\n
\n
\ndoctest.ELLIPSIS
\n
When specified, an ellipsis marker (...) in the expected output can match\nany substring in the actual output. This includes substrings that span line\nboundaries, and empty substrings, so it’s best to keep usage of this simple.\nComplicated uses can lead to the same kinds of “oops, it matched too much!”\nsurprises that .* is prone to in regular expressions.
\n\n
\n
\ndoctest.IGNORE_EXCEPTION_DETAIL
\n

When specified, an example that expects an exception passes if an exception of\nthe expected type is raised, even if the exception detail does not match. For\nexample, an example expecting ValueError: 42 will pass if the actual\nexception raised is ValueError: 3*14, but will fail, e.g., if\nTypeError is raised.

\n

It will also ignore the module name used in Python 3 doctest reports. Hence\nboth these variations will work regardless of whether the test is run under\nPython 2.7 or Python 3.2 (or later versions):

\n
\n
>>> raise CustomError('message') #doctest: +IGNORE_EXCEPTION_DETAIL\nTraceback (most recent call last):\nCustomError: message\n
\n
\n
>>> raise CustomError('message') #doctest: +IGNORE_EXCEPTION_DETAIL\nTraceback (most recent call last):\nmy_module.CustomError: message\n
\n
\n
\n

Note that ELLIPSIS can also be used to ignore the\ndetails of the exception message, but such a test may still fail based\non whether or not the module details are printed as part of the\nexception name. Using IGNORE_EXCEPTION_DETAIL and the details\nfrom Python 2.3 is also the only clear way to write a doctest that doesn’t\ncare about the exception detail yet continues to pass under Python 2.3 or\nearlier (those releases do not support doctest directives and ignore them\nas irrelevant comments). For example,

\n
>>> (1, 2)[3] = 'moo' #doctest: +IGNORE_EXCEPTION_DETAIL\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nTypeError: object doesn't support item assignment\n
\n
\n

passes under Python 2.3 and later Python versions, even though the detail\nchanged in Python 2.4 to say “does not” instead of “doesn’t”.

\n

\nChanged in version 2.7: IGNORE_EXCEPTION_DETAIL now also ignores any information\nrelating to the module containing the exception under test

\n
\n\n
\n
\ndoctest.SKIP
\n

When specified, do not run the example at all. This can be useful in contexts\nwhere doctest examples serve as both documentation and test cases, and an\nexample should be included for documentation purposes, but should not be\nchecked. E.g., the example’s output might be random; or the example might\ndepend on resources which would be unavailable to the test driver.

\n

The SKIP flag can also be used for temporarily “commenting out” examples.

\n
\n\n

\nNew in version 2.5.

\n
\n
\ndoctest.COMPARISON_FLAGS
\n
A bitmask or’ing together all the comparison flags above.
\n\n

The second group of options controls how test failures are reported:

\n
\n
\ndoctest.REPORT_UDIFF
\n
When specified, failures that involve multi-line expected and actual outputs are\ndisplayed using a unified diff.
\n\n
\n
\ndoctest.REPORT_CDIFF
\n
When specified, failures that involve multi-line expected and actual outputs\nwill be displayed using a context diff.
\n\n
\n
\ndoctest.REPORT_NDIFF
\n
When specified, differences are computed by difflib.Differ, using the same\nalgorithm as the popular ndiff.py utility. This is the only method that\nmarks differences within lines as well as across lines. For example, if a line\nof expected output contains digit 1 where actual output contains letter\nl, a line is inserted with a caret marking the mismatching column positions.
\n\n
\n
\ndoctest.REPORT_ONLY_FIRST_FAILURE
\n
When specified, display the first failing example in each doctest, but suppress\noutput for all remaining examples. This will prevent doctest from reporting\ncorrect examples that break because of earlier failures; but it might also hide\nincorrect examples that fail independently of the first failure. When\nREPORT_ONLY_FIRST_FAILURE is specified, the remaining examples are\nstill run, and still count towards the total number of failures reported; only\nthe output is suppressed.
\n\n
\n
\ndoctest.REPORTING_FLAGS
\n
A bitmask or’ing together all the reporting flags above.
\n\n

“Doctest directives” may be used to modify the option flags for individual\nexamples. Doctest directives are expressed as a special Python comment\nfollowing an example’s source code:

\n
\ndirective             ::=  "#" "doctest:" directive_options\ndirective_options     ::=  directive_option ("," directive_option)\\*\ndirective_option      ::=  on_or_off directive_option_name\non_or_off             ::=  "+" \\| "-"\ndirective_option_name ::=  "DONT_ACCEPT_BLANKLINE" \\| "NORMALIZE_WHITESPACE" \\| ...\n
\n

Whitespace is not allowed between the + or - and the directive option\nname. The directive option name can be any of the option flag names explained\nabove.

\n

An example’s doctest directives modify doctest’s behavior for that single\nexample. Use + to enable the named behavior, or - to disable it.

\n

For example, this test passes:

\n
>>> print range(20) #doctest: +NORMALIZE_WHITESPACE\n[0,   1,  2,  3,  4,  5,  6,  7,  8,  9,\n10,  11, 12, 13, 14, 15, 16, 17, 18, 19]\n
\n
\n

Without the directive it would fail, both because the actual output doesn’t have\ntwo blanks before the single-digit list elements, and because the actual output\nis on a single line. This test also passes, and also requires a directive to do\nso:

\n
>>> print range(20) # doctest:+ELLIPSIS\n[0, 1, ..., 18, 19]\n
\n
\n

Multiple directives can be used on a single physical line, separated by commas:

\n
>>> print range(20) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE\n[0,    1, ...,   18,    19]\n
\n
\n

If multiple directive comments are used for a single example, then they are\ncombined:

\n
>>> print range(20) # doctest: +ELLIPSIS\n...                 # doctest: +NORMALIZE_WHITESPACE\n[0,    1, ...,   18,    19]\n
\n
\n

As the previous example shows, you can add ... lines to your example\ncontaining only directives. This can be useful when an example is too long for\na directive to comfortably fit on the same line:

\n
>>> print range(5) + range(10,20) + range(30,40) + range(50,60)\n... # doctest: +ELLIPSIS\n[0, ..., 4, 10, ..., 19, 30, ..., 39, 50, ..., 59]\n
\n
\n

Note that since all options are disabled by default, and directives apply only\nto the example they appear in, enabling options (via + in a directive) is\nusually the only meaningful choice. However, option flags can also be passed to\nfunctions that run doctests, establishing different defaults. In such cases,\ndisabling an option via - in a directive can be useful.

\n

\nNew in version 2.4: Doctest directives and the associated constants\nDONT_ACCEPT_BLANKLINE, NORMALIZE_WHITESPACE,\nELLIPSIS, IGNORE_EXCEPTION_DETAIL, REPORT_UDIFF,\nREPORT_CDIFF, REPORT_NDIFF,\nREPORT_ONLY_FIRST_FAILURE, COMPARISON_FLAGS and\nREPORTING_FLAGS were added.

\n

There’s also a way to register new option flag names, although this isn’t useful\nunless you intend to extend doctest internals via subclassing:

\n
\n
\ndoctest.register_optionflag(name)
\n

Create a new option flag with a given name, and return the new flag’s integer\nvalue. register_optionflag() can be used when subclassing\nOutputChecker or DocTestRunner to create new options that are\nsupported by your subclasses. register_optionflag() should always be\ncalled using the following idiom:

\n
MY_FLAG = register_optionflag('MY_FLAG')\n
\n
\n

\nNew in version 2.4.

\n
\n\n
\n
\n

25.2.3.6. Warnings

\n

doctest is serious about requiring exact matches in expected output. If\neven a single character doesn’t match, the test fails. This will probably\nsurprise you a few times, as you learn exactly what Python does and doesn’t\nguarantee about output. For example, when printing a dict, Python doesn’t\nguarantee that the key-value pairs will be printed in any particular order, so a\ntest like

\n
>>> foo()\n{"Hermione": "hippogryph", "Harry": "broomstick"}\n
\n
\n

is vulnerable! One workaround is to do

\n
>>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"}\nTrue\n
\n
\n

instead. Another is to do

\n
>>> d = foo().items()\n>>> d.sort()\n>>> d\n[('Harry', 'broomstick'), ('Hermione', 'hippogryph')]\n
\n
\n

There are others, but you get the idea.

\n

Another bad idea is to print things that embed an object address, like

\n
>>> id(1.0) # certain to fail some of the time\n7948648\n>>> class C: pass\n>>> C()   # the default repr() for instances embeds an address\n<__main__.C instance at 0x00AC18F0>\n
\n
\n

The ELLIPSIS directive gives a nice approach for the last example:

\n
>>> C() #doctest: +ELLIPSIS\n<__main__.C instance at 0x...>\n
\n
\n

Floating-point numbers are also subject to small output variations across\nplatforms, because Python defers to the platform C library for float formatting,\nand C libraries vary widely in quality here.

\n
>>> 1./7  # risky\n0.14285714285714285\n>>> print 1./7 # safer\n0.142857142857\n>>> print round(1./7, 6) # much safer\n0.142857\n
\n
\n

Numbers of the form I/2.**J are safe across all platforms, and I often\ncontrive doctest examples to produce numbers of that form:

\n
>>> 3./4  # utterly safe\n0.75\n
\n
\n

Simple fractions are also easier for people to understand, and that makes for\nbetter documentation.

\n
\n
\n
\n

25.2.4. Basic API

\n

The functions testmod() and testfile() provide a simple interface to\ndoctest that should be sufficient for most basic uses. For a less formal\nintroduction to these two functions, see sections Simple Usage: Checking Examples in Docstrings\nand Simple Usage: Checking Examples in a Text File.

\n
\n
\ndoctest.testfile(filename[, module_relative][, name][, package][, globs][, verbose][, report][, optionflags][, extraglobs][, raise_on_error][, parser][, encoding])
\n

All arguments except filename are optional, and should be specified in keyword\nform.

\n

Test examples in the file named filename. Return (failure_count,\ntest_count).

\n

Optional argument module_relative specifies how the filename should be\ninterpreted:

\n
    \n
  • If module_relative is True (the default), then filename specifies an\nOS-independent module-relative path. By default, this path is relative to the\ncalling module’s directory; but if the package argument is specified, then it\nis relative to that package. To ensure OS-independence, filename should use\n/ characters to separate path segments, and may not be an absolute path\n(i.e., it may not begin with /).
  • \n
  • If module_relative is False, then filename specifies an OS-specific\npath. The path may be absolute or relative; relative paths are resolved with\nrespect to the current working directory.
  • \n
\n

Optional argument name gives the name of the test; by default, or if None,\nos.path.basename(filename) is used.

\n

Optional argument package is a Python package or the name of a Python package\nwhose directory should be used as the base directory for a module-relative\nfilename. If no package is specified, then the calling module’s directory is\nused as the base directory for module-relative filenames. It is an error to\nspecify package if module_relative is False.

\n

Optional argument globs gives a dict to be used as the globals when executing\nexamples. A new shallow copy of this dict is created for the doctest, so its\nexamples start with a clean slate. By default, or if None, a new empty dict\nis used.

\n

Optional argument extraglobs gives a dict merged into the globals used to\nexecute examples. This works like dict.update(): if globs and\nextraglobs have a common key, the associated value in extraglobs appears in\nthe combined dict. By default, or if None, no extra globals are used. This\nis an advanced feature that allows parameterization of doctests. For example, a\ndoctest can be written for a base class, using a generic name for the class,\nthen reused to test any number of subclasses by passing an extraglobs dict\nmapping the generic name to the subclass to be tested.

\n

Optional argument verbose prints lots of stuff if true, and prints only\nfailures if false; by default, or if None, it’s true if and only if '-v'\nis in sys.argv.

\n

Optional argument report prints a summary at the end when true, else prints\nnothing at the end. In verbose mode, the summary is detailed, else the summary\nis very brief (in fact, empty if all tests passed).

\n

Optional argument optionflags or’s together option flags. See section\nOption Flags and Directives.

\n

Optional argument raise_on_error defaults to false. If true, an exception is\nraised upon the first failure or unexpected exception in an example. This\nallows failures to be post-mortem debugged. Default behavior is to continue\nrunning examples.

\n

Optional argument parser specifies a DocTestParser (or subclass) that\nshould be used to extract tests from the files. It defaults to a normal parser\n(i.e., DocTestParser()).

\n

Optional argument encoding specifies an encoding that should be used to\nconvert the file to unicode.

\n

\nNew in version 2.4.

\n

\nChanged in version 2.5: The parameter encoding was added.

\n
\n\n
\n
\ndoctest.testmod([m][, name][, globs][, verbose][, report][, optionflags][, extraglobs][, raise_on_error][, exclude_empty])
\n

All arguments are optional, and all except for m should be specified in\nkeyword form.

\n

Test examples in docstrings in functions and classes reachable from module m\n(or module __main__ if m is not supplied or is None), starting with\nm.__doc__.

\n

Also test examples reachable from dict m.__test__, if it exists and is not\nNone. m.__test__ maps names (strings) to functions, classes and\nstrings; function and class docstrings are searched for examples; strings are\nsearched directly, as if they were docstrings.

\n

Only docstrings attached to objects belonging to module m are searched.

\n

Return (failure_count, test_count).

\n

Optional argument name gives the name of the module; by default, or if\nNone, m.__name__ is used.

\n

Optional argument exclude_empty defaults to false. If true, objects for which\nno doctests are found are excluded from consideration. The default is a backward\ncompatibility hack, so that code still using doctest.master.summarize() in\nconjunction with testmod() continues to get output for objects with no\ntests. The exclude_empty argument to the newer DocTestFinder\nconstructor defaults to true.

\n

Optional arguments extraglobs, verbose, report, optionflags,\nraise_on_error, and globs are the same as for function testfile()\nabove, except that globs defaults to m.__dict__.

\n

\nChanged in version 2.3: The parameter optionflags was added.

\n

\nChanged in version 2.4: The parameters extraglobs, raise_on_error and exclude_empty were added.

\n

\nChanged in version 2.5: The optional argument isprivate, deprecated in 2.4, was removed.

\n
\n\n

There’s also a function to run the doctests associated with a single object.\nThis function is provided for backward compatibility. There are no plans to\ndeprecate it, but it’s rarely useful:

\n
\n
\ndoctest.run_docstring_examples(f, globs[, verbose][, name][, compileflags][, optionflags])
\n

Test examples associated with object f; for example, f may be a module,\nfunction, or class object.

\n

A shallow copy of dictionary argument globs is used for the execution context.

\n

Optional argument name is used in failure messages, and defaults to\n"NoName".

\n

If optional argument verbose is true, output is generated even if there are no\nfailures. By default, output is generated only in case of an example failure.

\n

Optional argument compileflags gives the set of flags that should be used by\nthe Python compiler when running the examples. By default, or if None,\nflags are deduced corresponding to the set of future features found in globs.

\n

Optional argument optionflags works as for function testfile() above.

\n
\n\n
\n
\n

25.2.5. Unittest API

\n

As your collection of doctest’ed modules grows, you’ll want a way to run all\ntheir doctests systematically. Prior to Python 2.4, doctest had a barely\ndocumented Tester class that supplied a rudimentary way to combine\ndoctests from multiple modules. Tester was feeble, and in practice most\nserious Python testing frameworks build on the unittest module, which\nsupplies many flexible ways to combine tests from multiple sources. So, in\nPython 2.4, doctest‘s Tester class is deprecated, and\ndoctest provides two functions that can be used to create unittest\ntest suites from modules and text files containing doctests. To integrate with\nunittest test discovery, include a load_tests() function in your\ntest module:

\n
import unittest\nimport doctest\nimport my_module_with_doctests\n\ndef load_tests(loader, tests, ignore):\n    tests.addTests(doctest.DocTestSuite(my_module_with_doctests))\n    return tests\n
\n
\n

There are two main functions for creating unittest.TestSuite instances\nfrom text files and modules with doctests:

\n
\n
\ndoctest.DocFileSuite(*paths[, module_relative][, package][, setUp][, tearDown][, globs][, optionflags][, parser][, encoding])
\n

Convert doctest tests from one or more text files to a\nunittest.TestSuite.

\n

The returned unittest.TestSuite is to be run by the unittest framework\nand runs the interactive examples in each file. If an example in any file\nfails, then the synthesized unit test fails, and a failureException\nexception is raised showing the name of the file containing the test and a\n(sometimes approximate) line number.

\n

Pass one or more paths (as strings) to text files to be examined.

\n

Options may be provided as keyword arguments:

\n

Optional argument module_relative specifies how the filenames in paths\nshould be interpreted:

\n
    \n
  • If module_relative is True (the default), then each filename in\npaths specifies an OS-independent module-relative path. By default, this\npath is relative to the calling module’s directory; but if the package\nargument is specified, then it is relative to that package. To ensure\nOS-independence, each filename should use / characters to separate path\nsegments, and may not be an absolute path (i.e., it may not begin with\n/).
  • \n
  • If module_relative is False, then each filename in paths specifies\nan OS-specific path. The path may be absolute or relative; relative paths\nare resolved with respect to the current working directory.
  • \n
\n

Optional argument package is a Python package or the name of a Python\npackage whose directory should be used as the base directory for\nmodule-relative filenames in paths. If no package is specified, then the\ncalling module’s directory is used as the base directory for module-relative\nfilenames. It is an error to specify package if module_relative is\nFalse.

\n

Optional argument setUp specifies a set-up function for the test suite.\nThis is called before running the tests in each file. The setUp function\nwill be passed a DocTest object. The setUp function can access the\ntest globals as the globs attribute of the test passed.

\n

Optional argument tearDown specifies a tear-down function for the test\nsuite. This is called after running the tests in each file. The tearDown\nfunction will be passed a DocTest object. The setUp function can\naccess the test globals as the globs attribute of the test passed.

\n

Optional argument globs is a dictionary containing the initial global\nvariables for the tests. A new copy of this dictionary is created for each\ntest. By default, globs is a new empty dictionary.

\n

Optional argument optionflags specifies the default doctest options for the\ntests, created by or-ing together individual option flags. See section\nOption Flags and Directives. See function set_unittest_reportflags() below\nfor a better way to set reporting options.

\n

Optional argument parser specifies a DocTestParser (or subclass)\nthat should be used to extract tests from the files. It defaults to a normal\nparser (i.e., DocTestParser()).

\n

Optional argument encoding specifies an encoding that should be used to\nconvert the file to unicode.

\n

\nNew in version 2.4.

\n

\nChanged in version 2.5: The global __file__ was added to the globals provided to doctests\nloaded from a text file using DocFileSuite().

\n

\nChanged in version 2.5: The parameter encoding was added.

\n
\n\n
\n
\ndoctest.DocTestSuite([module][, globs][, extraglobs][, test_finder][, setUp][, tearDown][, checker])
\n

Convert doctest tests for a module to a unittest.TestSuite.

\n

The returned unittest.TestSuite is to be run by the unittest framework\nand runs each doctest in the module. If any of the doctests fail, then the\nsynthesized unit test fails, and a failureException exception is raised\nshowing the name of the file containing the test and a (sometimes approximate)\nline number.

\n

Optional argument module provides the module to be tested. It can be a module\nobject or a (possibly dotted) module name. If not specified, the module calling\nthis function is used.

\n

Optional argument globs is a dictionary containing the initial global\nvariables for the tests. A new copy of this dictionary is created for each\ntest. By default, globs is a new empty dictionary.

\n

Optional argument extraglobs specifies an extra set of global variables, which\nis merged into globs. By default, no extra globals are used.

\n

Optional argument test_finder is the DocTestFinder object (or a\ndrop-in replacement) that is used to extract doctests from the module.

\n

Optional arguments setUp, tearDown, and optionflags are the same as for\nfunction DocFileSuite() above.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.4: The parameters globs, extraglobs, test_finder, setUp, tearDown, and\noptionflags were added; this function now uses the same search technique as\ntestmod().

\n
\n\n

Under the covers, DocTestSuite() creates a unittest.TestSuite out\nof doctest.DocTestCase instances, and DocTestCase is a\nsubclass of unittest.TestCase. DocTestCase isn’t documented\nhere (it’s an internal detail), but studying its code can answer questions about\nthe exact details of unittest integration.

\n

Similarly, DocFileSuite() creates a unittest.TestSuite out of\ndoctest.DocFileCase instances, and DocFileCase is a subclass\nof DocTestCase.

\n

So both ways of creating a unittest.TestSuite run instances of\nDocTestCase. This is important for a subtle reason: when you run\ndoctest functions yourself, you can control the doctest options in\nuse directly, by passing option flags to doctest functions. However, if\nyou’re writing a unittest framework, unittest ultimately controls\nwhen and how tests get run. The framework author typically wants to control\ndoctest reporting options (perhaps, e.g., specified by command line\noptions), but there’s no way to pass options through unittest to\ndoctest test runners.

\n

For this reason, doctest also supports a notion of doctest\nreporting flags specific to unittest support, via this function:

\n
\n
\ndoctest.set_unittest_reportflags(flags)
\n

Set the doctest reporting flags to use.

\n

Argument flags or’s together option flags. See section\nOption Flags and Directives. Only “reporting flags” can be used.

\n

This is a module-global setting, and affects all future doctests run by module\nunittest: the runTest() method of DocTestCase looks at\nthe option flags specified for the test case when the DocTestCase\ninstance was constructed. If no reporting flags were specified (which is the\ntypical and expected case), doctest‘s unittest reporting flags are\nor’ed into the option flags, and the option flags so augmented are passed to the\nDocTestRunner instance created to run the doctest. If any reporting\nflags were specified when the DocTestCase instance was constructed,\ndoctest‘s unittest reporting flags are ignored.

\n

The value of the unittest reporting flags in effect before the function\nwas called is returned by the function.

\n

\nNew in version 2.4.

\n
\n\n
\n
\n

25.2.6. Advanced API

\n

The basic API is a simple wrapper that’s intended to make doctest easy to use.\nIt is fairly flexible, and should meet most users’ needs; however, if you\nrequire more fine-grained control over testing, or wish to extend doctest’s\ncapabilities, then you should use the advanced API.

\n

The advanced API revolves around two container classes, which are used to store\nthe interactive examples extracted from doctest cases:

\n\n

Additional processing classes are defined to find, parse, and run, and check\ndoctest examples:

\n\n

The relationships among these processing classes are summarized in the following\ndiagram:

\n
                            list of:\n+------+                   +---------+\n|module| --DocTestFinder-> | DocTest | --DocTestRunner-> results\n+------+    |        ^     +---------+     |       ^    (printed)\n            |        |     | Example |     |       |\n            v        |     |   ...   |     v       |\n           DocTestParser   | Example |   OutputChecker\n                           +---------+
\n
\n
\n

25.2.6.1. DocTest Objects

\n
\n
\nclass doctest.DocTest(examples, globs, name, filename, lineno, docstring)
\n

A collection of doctest examples that should be run in a single namespace. The\nconstructor arguments are used to initialize the attributes of the same names.

\n

\nNew in version 2.4.

\n

DocTest defines the following attributes. They are initialized by\nthe constructor, and should not be modified directly.

\n
\n
\nexamples
\n
A list of Example objects encoding the individual interactive Python\nexamples that should be run by this test.
\n\n
\n
\nglobs
\n
The namespace (aka globals) that the examples should be run in. This is a\ndictionary mapping names to values. Any changes to the namespace made by the\nexamples (such as binding new variables) will be reflected in globs\nafter the test is run.
\n\n
\n
\nname
\n
A string name identifying the DocTest. Typically, this is the name\nof the object or file that the test was extracted from.
\n\n
\n
\nfilename
\n
The name of the file that this DocTest was extracted from; or\nNone if the filename is unknown, or if the DocTest was not\nextracted from a file.
\n\n
\n
\nlineno
\n
The line number within filename where this DocTest begins, or\nNone if the line number is unavailable. This line number is zero-based\nwith respect to the beginning of the file.
\n\n
\n
\ndocstring
\n
The string that the test was extracted from, or ‘None’ if the string is\nunavailable, or if the test was not extracted from a string.
\n\n
\n\n
\n
\n

25.2.6.2. Example Objects

\n
\n
\nclass doctest.Example(source, want[, exc_msg][, lineno][, indent][, options])
\n

A single interactive example, consisting of a Python statement and its expected\noutput. The constructor arguments are used to initialize the attributes of the\nsame names.

\n

\nNew in version 2.4.

\n

Example defines the following attributes. They are initialized by\nthe constructor, and should not be modified directly.

\n
\n
\nsource
\n
A string containing the example’s source code. This source code consists of a\nsingle Python statement, and always ends with a newline; the constructor adds\na newline when necessary.
\n\n
\n
\nwant
\n
The expected output from running the example’s source code (either from\nstdout, or a traceback in case of exception). want ends with a\nnewline unless no output is expected, in which case it’s an empty string. The\nconstructor adds a newline when necessary.
\n\n
\n
\nexc_msg
\n
The exception message generated by the example, if the example is expected to\ngenerate an exception; or None if it is not expected to generate an\nexception. This exception message is compared against the return value of\ntraceback.format_exception_only(). exc_msg ends with a newline\nunless it’s None. The constructor adds a newline if needed.
\n\n
\n
\nlineno
\n
The line number within the string containing this example where the example\nbegins. This line number is zero-based with respect to the beginning of the\ncontaining string.
\n\n
\n
\nindent
\n
The example’s indentation in the containing string, i.e., the number of space\ncharacters that precede the example’s first prompt.
\n\n
\n
\noptions
\n
A dictionary mapping from option flags to True or False, which is used\nto override default options for this example. Any option flags not contained\nin this dictionary are left at their default value (as specified by the\nDocTestRunner‘s optionflags). By default, no options are set.
\n\n
\n\n
\n
\n

25.2.6.3. DocTestFinder objects

\n
\n
\nclass doctest.DocTestFinder([verbose][, parser][, recurse][, exclude_empty])
\n

A processing class used to extract the DocTests that are relevant to\na given object, from its docstring and the docstrings of its contained objects.\nDocTests can currently be extracted from the following object types:\nmodules, functions, classes, methods, staticmethods, classmethods, and\nproperties.

\n

The optional argument verbose can be used to display the objects searched by\nthe finder. It defaults to False (no output).

\n

The optional argument parser specifies the DocTestParser object (or a\ndrop-in replacement) that is used to extract doctests from docstrings.

\n

If the optional argument recurse is false, then DocTestFinder.find()\nwill only examine the given object, and not any contained objects.

\n

If the optional argument exclude_empty is false, then\nDocTestFinder.find() will include tests for objects with empty docstrings.

\n

\nNew in version 2.4.

\n

DocTestFinder defines the following method:

\n
\n
\nfind(obj[, name][, module][, globs][, extraglobs])
\n

Return a list of the DocTests that are defined by obj‘s\ndocstring, or by any of its contained objects’ docstrings.

\n

The optional argument name specifies the object’s name; this name will be\nused to construct names for the returned DocTests. If name is\nnot specified, then obj.__name__ is used.

\n

The optional parameter module is the module that contains the given object.\nIf the module is not specified or is None, then the test finder will attempt\nto automatically determine the correct module. The object’s module is used:

\n
    \n
  • As a default namespace, if globs is not specified.
  • \n
  • To prevent the DocTestFinder from extracting DocTests from objects that are\nimported from other modules. (Contained objects with modules other than\nmodule are ignored.)
  • \n
  • To find the name of the file containing the object.
  • \n
  • To help find the line number of the object within its file.
  • \n
\n

If module is False, no attempt to find the module will be made. This is\nobscure, of use mostly in testing doctest itself: if module is False, or\nis None but cannot be found automatically, then all objects are considered\nto belong to the (non-existent) module, so all contained objects will\n(recursively) be searched for doctests.

\n

The globals for each DocTest is formed by combining globs and\nextraglobs (bindings in extraglobs override bindings in globs). A new\nshallow copy of the globals dictionary is created for each DocTest.\nIf globs is not specified, then it defaults to the module’s __dict__, if\nspecified, or {} otherwise. If extraglobs is not specified, then it\ndefaults to {}.

\n
\n\n
\n\n
\n
\n

25.2.6.4. DocTestParser objects

\n
\n
\nclass doctest.DocTestParser
\n

A processing class used to extract interactive examples from a string, and use\nthem to create a DocTest object.

\n

\nNew in version 2.4.

\n

DocTestParser defines the following methods:

\n
\n
\nget_doctest(string, globs, name, filename, lineno)
\n

Extract all doctest examples from the given string, and collect them into a\nDocTest object.

\n

globs, name, filename, and lineno are attributes for the new\nDocTest object. See the documentation for DocTest for more\ninformation.

\n
\n\n
\n
\nget_examples(string[, name])
\n
Extract all doctest examples from the given string, and return them as a list\nof Example objects. Line numbers are 0-based. The optional argument\nname is a name identifying this string, and is only used for error messages.
\n\n
\n
\nparse(string[, name])
\n
Divide the given string into examples and intervening text, and return them as\na list of alternating Examples and strings. Line numbers for the\nExamples are 0-based. The optional argument name is a name\nidentifying this string, and is only used for error messages.
\n\n
\n\n
\n
\n

25.2.6.5. DocTestRunner objects

\n
\n
\nclass doctest.DocTestRunner([checker][, verbose][, optionflags])
\n

A processing class used to execute and verify the interactive examples in a\nDocTest.

\n

The comparison between expected outputs and actual outputs is done by an\nOutputChecker. This comparison may be customized with a number of\noption flags; see section Option Flags and Directives for more information. If the\noption flags are insufficient, then the comparison may also be customized by\npassing a subclass of OutputChecker to the constructor.

\n

The test runner’s display output can be controlled in two ways. First, an output\nfunction can be passed to TestRunner.run(); this function will be called\nwith strings that should be displayed. It defaults to sys.stdout.write. If\ncapturing the output is not sufficient, then the display output can be also\ncustomized by subclassing DocTestRunner, and overriding the methods\nreport_start(), report_success(),\nreport_unexpected_exception(), and report_failure().

\n

The optional keyword argument checker specifies the OutputChecker\nobject (or drop-in replacement) that should be used to compare the expected\noutputs to the actual outputs of doctest examples.

\n

The optional keyword argument verbose controls the DocTestRunner‘s\nverbosity. If verbose is True, then information is printed about each\nexample, as it is run. If verbose is False, then only failures are\nprinted. If verbose is unspecified, or None, then verbose output is used\niff the command-line switch -v is used.

\n

The optional keyword argument optionflags can be used to control how the test\nrunner compares expected output to actual output, and how it displays failures.\nFor more information, see section Option Flags and Directives.

\n

\nNew in version 2.4.

\n

DocTestParser defines the following methods:

\n
\n
\nreport_start(out, test, example)
\n

Report that the test runner is about to process the given example. This method\nis provided to allow subclasses of DocTestRunner to customize their\noutput; it should not be called directly.

\n

example is the example about to be processed. test is the test\ncontaining example. out is the output function that was passed to\nDocTestRunner.run().

\n
\n\n
\n
\nreport_success(out, test, example, got)
\n

Report that the given example ran successfully. This method is provided to\nallow subclasses of DocTestRunner to customize their output; it\nshould not be called directly.

\n

example is the example about to be processed. got is the actual output\nfrom the example. test is the test containing example. out is the\noutput function that was passed to DocTestRunner.run().

\n
\n\n
\n
\nreport_failure(out, test, example, got)
\n

Report that the given example failed. This method is provided to allow\nsubclasses of DocTestRunner to customize their output; it should not\nbe called directly.

\n

example is the example about to be processed. got is the actual output\nfrom the example. test is the test containing example. out is the\noutput function that was passed to DocTestRunner.run().

\n
\n\n
\n
\nreport_unexpected_exception(out, test, example, exc_info)
\n

Report that the given example raised an unexpected exception. This method is\nprovided to allow subclasses of DocTestRunner to customize their\noutput; it should not be called directly.

\n

example is the example about to be processed. exc_info is a tuple\ncontaining information about the unexpected exception (as returned by\nsys.exc_info()). test is the test containing example. out is the\noutput function that was passed to DocTestRunner.run().

\n
\n\n
\n
\nrun(test[, compileflags][, out][, clear_globs])
\n

Run the examples in test (a DocTest object), and display the\nresults using the writer function out.

\n

The examples are run in the namespace test.globs. If clear_globs is\ntrue (the default), then this namespace will be cleared after the test runs,\nto help with garbage collection. If you would like to examine the namespace\nafter the test completes, then use clear_globs=False.

\n

compileflags gives the set of flags that should be used by the Python\ncompiler when running the examples. If not specified, then it will default to\nthe set of future-import flags that apply to globs.

\n

The output of each example is checked using the DocTestRunner‘s\noutput checker, and the results are formatted by the\nDocTestRunner.report_*() methods.

\n
\n\n
\n
\nsummarize([verbose])
\n

Print a summary of all the test cases that have been run by this DocTestRunner,\nand return a named tuple TestResults(failed, attempted).

\n

The optional verbose argument controls how detailed the summary is. If the\nverbosity is not specified, then the DocTestRunner‘s verbosity is\nused.

\n

\nChanged in version 2.6: Use a named tuple.

\n
\n\n
\n\n
\n
\n

25.2.6.6. OutputChecker objects

\n
\n
\nclass doctest.OutputChecker
\n

A class used to check the whether the actual output from a doctest example\nmatches the expected output. OutputChecker defines two methods:\ncheck_output(), which compares a given pair of outputs, and returns true\nif they match; and output_difference(), which returns a string describing\nthe differences between two outputs.

\n

\nNew in version 2.4.

\n

OutputChecker defines the following methods:

\n
\n
\ncheck_output(want, got, optionflags)
\n
Return True iff the actual output from an example (got) matches the\nexpected output (want). These strings are always considered to match if\nthey are identical; but depending on what option flags the test runner is\nusing, several non-exact match types are also possible. See section\nOption Flags and Directives for more information about option flags.
\n\n
\n
\noutput_difference(example, got, optionflags)
\n
Return a string describing the differences between the expected output for a\ngiven example (example) and the actual output (got). optionflags is the\nset of option flags used to compare want and got.
\n\n
\n\n
\n
\n
\n

25.2.7. Debugging

\n

Doctest provides several mechanisms for debugging doctest examples:

\n\n

Functions that convert doctests to Python code, and possibly run the synthesized\ncode under the debugger:

\n
\n
\ndoctest.script_from_examples(s)
\n

Convert text with examples to a script.

\n

Argument s is a string containing doctest examples. The string is converted\nto a Python script, where doctest examples in s are converted to regular code,\nand everything else is converted to Python comments. The generated script is\nreturned as a string. For example,

\n
import doctest\nprint doctest.script_from_examples(r"""\n    Set x and y to 1 and 2.\n    >>> x, y = 1, 2\n\n    Print their sum:\n    >>> print x+y\n    3\n""")\n
\n
\n

displays:

\n
# Set x and y to 1 and 2.\nx, y = 1, 2\n#\n# Print their sum:\nprint x+y\n# Expected:\n## 3\n
\n
\n

This function is used internally by other functions (see below), but can also be\nuseful when you want to transform an interactive Python session into a Python\nscript.

\n

\nNew in version 2.4.

\n
\n\n
\n
\ndoctest.testsource(module, name)
\n

Convert the doctest for an object to a script.

\n

Argument module is a module object, or dotted name of a module, containing the\nobject whose doctests are of interest. Argument name is the name (within the\nmodule) of the object with the doctests of interest. The result is a string,\ncontaining the object’s docstring converted to a Python script, as described for\nscript_from_examples() above. For example, if module a.py\ncontains a top-level function f(), then

\n
import a, doctest\nprint doctest.testsource(a, "a.f")\n
\n
\n

prints a script version of function f()‘s docstring, with doctests\nconverted to code, and the rest placed in comments.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ndoctest.debug(module, name[, pm])
\n

Debug the doctests for an object.

\n

The module and name arguments are the same as for function\ntestsource() above. The synthesized Python script for the named object’s\ndocstring is written to a temporary file, and then that file is run under the\ncontrol of the Python debugger, pdb.

\n

A shallow copy of module.__dict__ is used for both local and global\nexecution context.

\n

Optional argument pm controls whether post-mortem debugging is used. If pm\nhas a true value, the script file is run directly, and the debugger gets\ninvolved only if the script terminates via raising an unhandled exception. If\nit does, then post-mortem debugging is invoked, via pdb.post_mortem(),\npassing the traceback object from the unhandled exception. If pm is not\nspecified, or is false, the script is run under the debugger from the start, via\npassing an appropriate execfile() call to pdb.run().

\n

\nNew in version 2.3.

\n

\nChanged in version 2.4: The pm argument was added.

\n
\n\n
\n
\ndoctest.debug_src(src[, pm][, globs])
\n

Debug the doctests in a string.

\n

This is like function debug() above, except that a string containing\ndoctest examples is specified directly, via the src argument.

\n

Optional argument pm has the same meaning as in function debug() above.

\n

Optional argument globs gives a dictionary to use as both local and global\nexecution context. If not specified, or None, an empty dictionary is used.\nIf specified, a shallow copy of the dictionary is used.

\n

\nNew in version 2.4.

\n
\n\n

The DebugRunner class, and the special exceptions it may raise, are of\nmost interest to testing framework authors, and will only be sketched here. See\nthe source code, and especially DebugRunner‘s docstring (which is a\ndoctest!) for more details:

\n
\n
\nclass doctest.DebugRunner([checker][, verbose][, optionflags])
\n

A subclass of DocTestRunner that raises an exception as soon as a\nfailure is encountered. If an unexpected exception occurs, an\nUnexpectedException exception is raised, containing the test, the\nexample, and the original exception. If the output doesn’t match, then a\nDocTestFailure exception is raised, containing the test, the example, and\nthe actual output.

\n

For information about the constructor parameters and methods, see the\ndocumentation for DocTestRunner in section Advanced API.

\n
\n\n

There are two exceptions that may be raised by DebugRunner instances:

\n
\n
\nexception doctest.DocTestFailure(test, example, got)
\n
An exception raised by DocTestRunner to signal that a doctest example’s\nactual output did not match its expected output. The constructor arguments are\nused to initialize the attributes of the same names.
\n\n

DocTestFailure defines the following attributes:

\n
\n
\nDocTestFailure.test
\n
The DocTest object that was being run when the example failed.
\n\n
\n
\nDocTestFailure.example
\n
The Example that failed.
\n\n
\n
\nDocTestFailure.got
\n
The example’s actual output.
\n\n
\n
\nexception doctest.UnexpectedException(test, example, exc_info)
\n
An exception raised by DocTestRunner to signal that a doctest\nexample raised an unexpected exception. The constructor arguments are used\nto initialize the attributes of the same names.
\n\n

UnexpectedException defines the following attributes:

\n
\n
\nUnexpectedException.test
\n
The DocTest object that was being run when the example failed.
\n\n
\n
\nUnexpectedException.example
\n
The Example that failed.
\n\n
\n
\nUnexpectedException.exc_info
\n
A tuple containing information about the unexpected exception, as returned by\nsys.exc_info().
\n\n
\n
\n

25.2.8. Soapbox

\n

As mentioned in the introduction, doctest has grown to have three primary\nuses:

\n
    \n
  1. Checking examples in docstrings.
  2. \n
  3. Regression testing.
  4. \n
  5. Executable documentation / literate testing.
  6. \n
\n

These uses have different requirements, and it is important to distinguish them.\nIn particular, filling your docstrings with obscure test cases makes for bad\ndocumentation.

\n

When writing a docstring, choose docstring examples with care. There’s an art to\nthis that needs to be learned—it may not be natural at first. Examples should\nadd genuine value to the documentation. A good example can often be worth many\nwords. If done with care, the examples will be invaluable for your users, and\nwill pay back the time it takes to collect them many times over as the years go\nby and things change. I’m still amazed at how often one of my doctest\nexamples stops working after a “harmless” change.

\n

Doctest also makes an excellent tool for regression testing, especially if you\ndon’t skimp on explanatory text. By interleaving prose and examples, it becomes\nmuch easier to keep track of what’s actually being tested, and why. When a test\nfails, good prose can make it much easier to figure out what the problem is, and\nhow it should be fixed. It’s true that you could write extensive comments in\ncode-based testing, but few programmers do. Many have found that using doctest\napproaches instead leads to much clearer tests. Perhaps this is simply because\ndoctest makes writing prose a little easier than writing code, while writing\ncomments in code is a little harder. I think it goes deeper than just that:\nthe natural attitude when writing a doctest-based test is that you want to\nexplain the fine points of your software, and illustrate them with examples.\nThis in turn naturally leads to test files that start with the simplest\nfeatures, and logically progress to complications and edge cases. A coherent\nnarrative is the result, instead of a collection of isolated functions that test\nisolated bits of functionality seemingly at random. It’s a different attitude,\nand produces different results, blurring the distinction between testing and\nexplaining.

\n

Regression testing is best confined to dedicated objects or files. There are\nseveral options for organizing tests:

\n\n

Footnotes

\n\n\n\n\n\n
[1]Examples containing both expected output and an exception are not supported.\nTrying to guess where one ends and the other begins is too error-prone, and that\nalso makes for a confusing test.
\n
\n
", "searchableItems": [ { "name": "doctest.debug", "domId": "doctest_doctest.debug" }, { "name": "doctest.debug_src", "domId": "doctest_doctest.debug_src" }, { "name": "doctest.DebugRunner", "domId": "doctest_doctest.DebugRunner" }, { "name": "doctest.DocFileSuite", "domId": "doctest_doctest.DocFileSuite" }, { "name": "doctest.DocTest", "domId": "doctest_doctest.DocTest" }, { "name": "doctest.DocTestFinder", "domId": "doctest_doctest.DocTestFinder" }, { "name": "doctest.DocTestFinder.find", "domId": "doctest_doctest.DocTestFinder.find" }, { "name": "doctest.DocTestParser", "domId": "doctest_doctest.DocTestParser" }, { "name": "doctest.DocTestParser.get_doctest", "domId": "doctest_doctest.DocTestParser.get_doctest" }, { "name": "doctest.DocTestParser.get_examples", "domId": "doctest_doctest.DocTestParser.get_examples" }, { "name": "doctest.DocTestParser.parse", "domId": "doctest_doctest.DocTestParser.parse" }, { "name": "doctest.DocTestRunner", "domId": "doctest_doctest.DocTestRunner" }, { "name": "doctest.DocTestRunner.report_failure", "domId": "doctest_doctest.DocTestRunner.report_failure" }, { "name": "doctest.DocTestRunner.report_start", "domId": "doctest_doctest.DocTestRunner.report_start" }, { "name": "doctest.DocTestRunner.report_success", "domId": "doctest_doctest.DocTestRunner.report_success" }, { "name": "doctest.DocTestRunner.report_unexpected_exception", "domId": "doctest_doctest.DocTestRunner.report_unexpected_exception" }, { "name": "doctest.DocTestRunner.run", "domId": "doctest_doctest.DocTestRunner.run" }, { "name": "doctest.DocTestRunner.summarize", "domId": "doctest_doctest.DocTestRunner.summarize" }, { "name": "doctest.DocTestSuite", "domId": "doctest_doctest.DocTestSuite" }, { "name": "doctest.Example", "domId": "doctest_doctest.Example" }, { "name": "doctest.OutputChecker", "domId": "doctest_doctest.OutputChecker" }, { "name": "doctest.OutputChecker.check_output", "domId": "doctest_doctest.OutputChecker.check_output" }, { "name": "doctest.OutputChecker.output_difference", "domId": "doctest_doctest.OutputChecker.output_difference" }, { "name": "doctest.register_optionflag", "domId": "doctest_doctest.register_optionflag" }, { "name": "doctest.run_docstring_examples", "domId": "doctest_doctest.run_docstring_examples" }, { "name": "doctest.script_from_examples", "domId": "doctest_doctest.script_from_examples" }, { "name": "doctest.set_unittest_reportflags", "domId": "doctest_doctest.set_unittest_reportflags" }, { "name": "doctest.testfile", "domId": "doctest_doctest.testfile" }, { "name": "doctest.testmod", "domId": "doctest_doctest.testmod" }, { "name": "doctest.testsource", "domId": "doctest_doctest.testsource" } ] }, { "url": "http://docs.python.org/library/unittest.html", "title": "unittest", "html": "
\n

25.3. unittest — Unit testing framework

\n

\nNew in version 2.1.

\n

(If you are already familiar with the basic concepts of testing, you might want\nto skip to the list of assert methods.)

\n

The Python unit testing framework, sometimes referred to as “PyUnit,” is a\nPython language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in\nturn, a Java version of Kent’s Smalltalk testing framework. Each is the de\nfacto standard unit testing framework for its respective language.

\n

unittest supports test automation, sharing of setup and shutdown code for\ntests, aggregation of tests into collections, and independence of the tests from\nthe reporting framework. The unittest module provides classes that make\nit easy to support these qualities for a set of tests.

\n

To achieve this, unittest supports some important concepts:

\n
\n
test fixture
\n
A test fixture represents the preparation needed to perform one or more\ntests, and any associate cleanup actions. This may involve, for example,\ncreating temporary or proxy databases, directories, or starting a server\nprocess.
\n
test case
\n
A test case is the smallest unit of testing. It checks for a specific\nresponse to a particular set of inputs. unittest provides a base class,\nTestCase, which may be used to create new test cases.
\n
test suite
\n
A test suite is a collection of test cases, test suites, or both. It is\nused to aggregate tests that should be executed together.
\n
test runner
\n
A test runner is a component which orchestrates the execution of tests\nand provides the outcome to the user. The runner may use a graphical interface,\na textual interface, or return a special value to indicate the results of\nexecuting the tests.
\n
\n

The test case and test fixture concepts are supported through the\nTestCase and FunctionTestCase classes; the former should be\nused when creating new tests, and the latter can be used when integrating\nexisting test code with a unittest-driven framework. When building test\nfixtures using TestCase, the setUp() and\ntearDown() methods can be overridden to provide initialization\nand cleanup for the fixture. With FunctionTestCase, existing functions\ncan be passed to the constructor for these purposes. When the test is run, the\nfixture initialization is run first; if it succeeds, the cleanup method is run\nafter the test has been executed, regardless of the outcome of the test. Each\ninstance of the TestCase will only be used to run a single test method,\nso a new fixture is created for each test.

\n

Test suites are implemented by the TestSuite class. This class allows\nindividual tests and test suites to be aggregated; when the suite is executed,\nall tests added directly to the suite and in “child” test suites are run.

\n

A test runner is an object that provides a single method,\nrun(), which accepts a TestCase or TestSuite\nobject as a parameter, and returns a result object. The class\nTestResult is provided for use as the result object. unittest\nprovides the TextTestRunner as an example test runner which reports\ntest results on the standard error stream by default. Alternate runners can be\nimplemented for other environments (such as graphical environments) without any\nneed to derive from a specific class.

\n
\n

See also

\n
\n
Module doctest
\n
Another test-support module with a very different flavor.
\n
unittest2: A backport of new unittest features for Python 2.4-2.6
\n
Many new features were added to unittest in Python 2.7, including test\ndiscovery. unittest2 allows you to use these features with earlier\nversions of Python.
\n
Simple Smalltalk Testing: With Patterns
\n
Kent Beck’s original paper on testing frameworks using the pattern shared\nby unittest.
\n
Nose and py.test
\n
Third-party unittest frameworks with a lighter-weight syntax for writing\ntests. For example, assert func(10) == 42.
\n
The Python Testing Tools Taxonomy
\n
An extensive list of Python testing tools including functional testing\nframeworks and mock object libraries.
\n
Testing in Python Mailing List
\n
A special-interest-group for discussion of testing, and testing tools,\nin Python.
\n
\n
\n
\n

25.3.1. Basic example

\n

The unittest module provides a rich set of tools for constructing and\nrunning tests. This section demonstrates that a small subset of the tools\nsuffice to meet the needs of most users.

\n

Here is a short script to test three functions from the random module:

\n
import random\nimport unittest\n\nclass TestSequenceFunctions(unittest.TestCase):\n\n    def setUp(self):\n        self.seq = range(10)\n\n    def test_shuffle(self):\n        # make sure the shuffled sequence does not lose any elements\n        random.shuffle(self.seq)\n        self.seq.sort()\n        self.assertEqual(self.seq, range(10))\n\n        # should raise an exception for an immutable sequence\n        self.assertRaises(TypeError, random.shuffle, (1,2,3))\n\n    def test_choice(self):\n        element = random.choice(self.seq)\n        self.assertTrue(element in self.seq)\n\n    def test_sample(self):\n        with self.assertRaises(ValueError):\n            random.sample(self.seq, 20)\n        for element in random.sample(self.seq, 5):\n            self.assertTrue(element in self.seq)\n\nif __name__ == '__main__':\n    unittest.main()\n
\n
\n

A testcase is created by subclassing unittest.TestCase. The three\nindividual tests are defined with methods whose names start with the letters\ntest. This naming convention informs the test runner about which methods\nrepresent tests.

\n

The crux of each test is a call to assertEqual() to check for an\nexpected result; assertTrue() to verify a condition; or\nassertRaises() to verify that an expected exception gets raised.\nThese methods are used instead of the assert statement so the test\nrunner can accumulate all test results and produce a report.

\n

When a setUp() method is defined, the test runner will run that\nmethod prior to each test. Likewise, if a tearDown() method is\ndefined, the test runner will invoke that method after each test. In the\nexample, setUp() was used to create a fresh sequence for each\ntest.

\n

The final block shows a simple way to run the tests. unittest.main()\nprovides a command-line interface to the test script. When run from the command\nline, the above script produces an output that looks like this:

\n
...\n----------------------------------------------------------------------\nRan 3 tests in 0.000s\n\nOK
\n
\n

Instead of unittest.main(), there are other ways to run the tests with a\nfiner level of control, less terse output, and no requirement to be run from the\ncommand line. For example, the last two lines may be replaced with:

\n
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)\nunittest.TextTestRunner(verbosity=2).run(suite)\n
\n
\n

Running the revised script from the interpreter or another script produces the\nfollowing output:

\n
test_choice (__main__.TestSequenceFunctions) ... ok\ntest_sample (__main__.TestSequenceFunctions) ... ok\ntest_shuffle (__main__.TestSequenceFunctions) ... ok\n\n----------------------------------------------------------------------\nRan 3 tests in 0.110s\n\nOK
\n
\n

The above examples show the most commonly used unittest features which\nare sufficient to meet many everyday testing needs. The remainder of the\ndocumentation explores the full feature set from first principles.

\n
\n
\n

25.3.2. Command-Line Interface

\n

The unittest module can be used from the command line to run tests from\nmodules, classes or even individual test methods:

\n
python -m unittest test_module1 test_module2\npython -m unittest test_module.TestClass\npython -m unittest test_module.TestClass.test_method
\n
\n

You can pass in a list with any combination of module names, and fully\nqualified class or method names.

\n

You can run tests with more detail (higher verbosity) by passing in the -v flag:

\n
python -m unittest -v test_module
\n
\n

For a list of all the command-line options:

\n
python -m unittest -h
\n
\n

\nChanged in version 2.7: In earlier versions it was only possible to run individual test methods and\nnot modules or classes.

\n
\n

25.3.2.1. Command-line options

\n

unittest supports these command-line options:

\n
\n
\n-b, --buffer
\n
The standard output and standard error streams are buffered during the test\nrun. Output during a passing test is discarded. Output is echoed normally\non test fail or error and is added to the failure messages.
\n\n
\n
\n-c, --catch
\n

Control-C during the test run waits for the current test to end and then\nreports all the results so far. A second control-C raises the normal\nKeyboardInterrupt exception.

\n

See Signal Handling for the functions that provide this functionality.

\n
\n\n
\n
\n-f, --failfast
\n
Stop the test run on the first error or failure.
\n\n

\nNew in version 2.7: The command-line options -b, -c and -f were added.

\n

The command line can also be used for test discovery, for running all of the\ntests in a project or just a subset.

\n
\n
\n
\n

25.3.3. Test Discovery

\n

\nNew in version 2.7.

\n

Unittest supports simple test discovery. In order to be compatible with test\ndiscovery, all of the test files must be modules or\npackages importable from the top-level directory of\nthe project (this means that their filenames must be valid\nidentifiers).

\n

Test discovery is implemented in TestLoader.discover(), but can also be\nused from the command line. The basic command-line usage is:

\n
cd project_directory\npython -m unittest discover
\n
\n

The discover sub-command has the following options:

\n
\n
\n-v, --verbose
\n
Verbose output
\n\n
\n
\n-s directory
\n
Directory to start discovery (‘.’ default)
\n\n
\n
\n-p pattern
\n
Pattern to match test files (‘test*.py’ default)
\n\n
\n
\n-t directory
\n
Top level directory of project (defaults to start directory)
\n\n

The -s, -p, and -t options can be passed in\nas positional arguments in that order. The following two command lines\nare equivalent:

\n
python -m unittest discover -s project_directory -p '*_test.py'\npython -m unittest discover project_directory '*_test.py'
\n
\n

As well as being a path it is possible to pass a package name, for example\nmyproject.subpackage.test, as the start directory. The package name you\nsupply will then be imported and its location on the filesystem will be used\nas the start directory.

\n
\n

Caution

\n

Test discovery loads tests by importing them. Once test discovery has\nfound all the test files from the start directory you specify it turns the\npaths into package names to import. For example foo/bar/baz.py will be\nimported as foo.bar.baz.

\n

If you have a package installed globally and attempt test discovery on\na different copy of the package then the import could happen from the\nwrong place. If this happens test discovery will warn you and exit.

\n

If you supply the start directory as a package name rather than a\npath to a directory then discover assumes that whichever location it\nimports from is the location you intended, so you will not get the\nwarning.

\n
\n

Test modules and packages can customize test loading and discovery by through\nthe load_tests protocol.

\n
\n
\n

25.3.4. Organizing test code

\n

The basic building blocks of unit testing are test cases — single\nscenarios that must be set up and checked for correctness. In unittest,\ntest cases are represented by instances of unittest‘s TestCase\nclass. To make your own test cases you must write subclasses of\nTestCase, or use FunctionTestCase.

\n

An instance of a TestCase-derived class is an object that can\ncompletely run a single test method, together with optional set-up and tidy-up\ncode.

\n

The testing code of a TestCase instance should be entirely self\ncontained, such that it can be run either in isolation or in arbitrary\ncombination with any number of other test cases.

\n

The simplest TestCase subclass will simply override the\nrunTest() method in order to perform specific testing code:

\n
import unittest\n\nclass DefaultWidgetSizeTestCase(unittest.TestCase):\n    def runTest(self):\n        widget = Widget('The widget')\n        self.assertEqual(widget.size(), (50, 50), 'incorrect default size')\n
\n
\n

Note that in order to test something, we use the one of the assert*()\nmethods provided by the TestCase base class. If the test fails, an\nexception will be raised, and unittest will identify the test case as a\nfailure. Any other exceptions will be treated as errors. This\nhelps you identify where the problem is: failures are caused by incorrect\nresults - a 5 where you expected a 6. Errors are caused by incorrect\ncode - e.g., a TypeError caused by an incorrect function call.

\n

The way to run a test case will be described later. For now, note that to\nconstruct an instance of such a test case, we call its constructor without\narguments:

\n
testCase = DefaultWidgetSizeTestCase()\n
\n
\n

Now, such test cases can be numerous, and their set-up can be repetitive. In\nthe above case, constructing a Widget in each of 100 Widget test case\nsubclasses would mean unsightly duplication.

\n

Luckily, we can factor out such set-up code by implementing a method called\nsetUp(), which the testing framework will automatically call for\nus when we run the test:

\n
import unittest\n\nclass SimpleWidgetTestCase(unittest.TestCase):\n    def setUp(self):\n        self.widget = Widget('The widget')\n\nclass DefaultWidgetSizeTestCase(SimpleWidgetTestCase):\n    def runTest(self):\n        self.assertEqual(self.widget.size(), (50,50),\n                         'incorrect default size')\n\nclass WidgetResizeTestCase(SimpleWidgetTestCase):\n    def runTest(self):\n        self.widget.resize(100,150)\n        self.assertEqual(self.widget.size(), (100,150),\n                         'wrong size after resize')\n
\n
\n

If the setUp() method raises an exception while the test is\nrunning, the framework will consider the test to have suffered an error, and the\nrunTest() method will not be executed.

\n

Similarly, we can provide a tearDown() method that tidies up\nafter the runTest() method has been run:

\n
import unittest\n\nclass SimpleWidgetTestCase(unittest.TestCase):\n    def setUp(self):\n        self.widget = Widget('The widget')\n\n    def tearDown(self):\n        self.widget.dispose()\n        self.widget = None\n
\n
\n

If setUp() succeeded, the tearDown() method will\nbe run whether runTest() succeeded or not.

\n

Such a working environment for the testing code is called a fixture.

\n

Often, many small test cases will use the same fixture. In this case, we would\nend up subclassing SimpleWidgetTestCase into many small one-method\nclasses such as DefaultWidgetSizeTestCase. This is time-consuming and\ndiscouraging, so in the same vein as JUnit, unittest provides a simpler\nmechanism:

\n
import unittest\n\nclass WidgetTestCase(unittest.TestCase):\n    def setUp(self):\n        self.widget = Widget('The widget')\n\n    def tearDown(self):\n        self.widget.dispose()\n        self.widget = None\n\n    def test_default_size(self):\n        self.assertEqual(self.widget.size(), (50,50),\n                         'incorrect default size')\n\n    def test_resize(self):\n        self.widget.resize(100,150)\n        self.assertEqual(self.widget.size(), (100,150),\n                         'wrong size after resize')\n
\n
\n

Here we have not provided a runTest() method, but have instead\nprovided two different test methods. Class instances will now each run one of\nthe test_*() methods, with self.widget created and destroyed\nseparately for each instance. When creating an instance we must specify the\ntest method it is to run. We do this by passing the method name in the\nconstructor:

\n
defaultSizeTestCase = WidgetTestCase('test_default_size')\nresizeTestCase = WidgetTestCase('test_resize')\n
\n
\n

Test case instances are grouped together according to the features they test.\nunittest provides a mechanism for this: the test suite,\nrepresented by unittest‘s TestSuite class:

\n
widgetTestSuite = unittest.TestSuite()\nwidgetTestSuite.addTest(WidgetTestCase('test_default_size'))\nwidgetTestSuite.addTest(WidgetTestCase('test_resize'))\n
\n
\n

For the ease of running tests, as we will see later, it is a good idea to\nprovide in each test module a callable object that returns a pre-built test\nsuite:

\n
def suite():\n    suite = unittest.TestSuite()\n    suite.addTest(WidgetTestCase('test_default_size'))\n    suite.addTest(WidgetTestCase('test_resize'))\n    return suite\n
\n
\n

or even:

\n
def suite():\n    tests = ['test_default_size', 'test_resize']\n\n    return unittest.TestSuite(map(WidgetTestCase, tests))\n
\n
\n

Since it is a common pattern to create a TestCase subclass with many\nsimilarly named test functions, unittest provides a TestLoader\nclass that can be used to automate the process of creating a test suite and\npopulating it with individual tests. For example,

\n
suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)\n
\n
\n

will create a test suite that will run WidgetTestCase.test_default_size() and\nWidgetTestCase.test_resize. TestLoader uses the 'test' method\nname prefix to identify test methods automatically.

\n

Note that the order in which the various test cases will be run is\ndetermined by sorting the test function names with respect to the\nbuilt-in ordering for strings.

\n

Often it is desirable to group suites of test cases together, so as to run tests\nfor the whole system at once. This is easy, since TestSuite instances\ncan be added to a TestSuite just as TestCase instances can be\nadded to a TestSuite:

\n
suite1 = module1.TheTestSuite()\nsuite2 = module2.TheTestSuite()\nalltests = unittest.TestSuite([suite1, suite2])\n
\n
\n

You can place the definitions of test cases and test suites in the same modules\nas the code they are to test (such as widget.py), but there are several\nadvantages to placing the test code in a separate module, such as\ntest_widget.py:

\n\n
\n
\n

25.3.5. Re-using old test code

\n

Some users will find that they have existing test code that they would like to\nrun from unittest, without converting every old test function to a\nTestCase subclass.

\n

For this reason, unittest provides a FunctionTestCase class.\nThis subclass of TestCase can be used to wrap an existing test\nfunction. Set-up and tear-down functions can also be provided.

\n

Given the following test function:

\n
def testSomething():\n    something = makeSomething()\n    assert something.name is not None\n    # ...\n
\n
\n

one can create an equivalent test case instance as follows:

\n
testcase = unittest.FunctionTestCase(testSomething)\n
\n
\n

If there are additional set-up and tear-down methods that should be called as\npart of the test case’s operation, they can also be provided like so:

\n
testcase = unittest.FunctionTestCase(testSomething,\n                                     setUp=makeSomethingDB,\n                                     tearDown=deleteSomethingDB)\n
\n
\n

To make migrating existing test suites easier, unittest supports tests\nraising AssertionError to indicate test failure. However, it is\nrecommended that you use the explicit TestCase.fail*() and\nTestCase.assert*() methods instead, as future versions of unittest\nmay treat AssertionError differently.

\n
\n

Note

\n

Even though FunctionTestCase can be used to quickly convert an\nexisting test base over to a unittest-based system, this approach is\nnot recommended. Taking the time to set up proper TestCase\nsubclasses will make future test refactorings infinitely easier.

\n
\n

In some cases, the existing tests may have been written using the doctest\nmodule. If so, doctest provides a DocTestSuite class that can\nautomatically build unittest.TestSuite instances from the existing\ndoctest-based tests.

\n
\n
\n

25.3.6. Skipping tests and expected failures

\n

\nNew in version 2.7.

\n

Unittest supports skipping individual test methods and even whole classes of\ntests. In addition, it supports marking a test as a “expected failure,” a test\nthat is broken and will fail, but shouldn’t be counted as a failure on a\nTestResult.

\n

Skipping a test is simply a matter of using the skip() decorator\nor one of its conditional variants.

\n

Basic skipping looks like this:

\n
class MyTestCase(unittest.TestCase):\n\n    @unittest.skip("demonstrating skipping")\n    def test_nothing(self):\n        self.fail("shouldn't happen")\n\n    @unittest.skipIf(mylib.__version__ < (1, 3),\n                     "not supported in this library version")\n    def test_format(self):\n        # Tests that work for only a certain version of the library.\n        pass\n\n    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")\n    def test_windows_support(self):\n        # windows specific testing code\n        pass\n
\n
\n

This is the output of running the example above in verbose mode:

\n
test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'\ntest_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'\ntest_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'\n\n----------------------------------------------------------------------\nRan 3 tests in 0.005s\n\nOK (skipped=3)
\n
\n

Classes can be skipped just like methods:

\n
@skip("showing class skipping")\nclass MySkippedTestCase(unittest.TestCase):\n    def test_not_run(self):\n        pass\n
\n
\n

TestCase.setUp() can also skip the test. This is useful when a resource\nthat needs to be set up is not available.

\n

Expected failures use the expectedFailure() decorator.

\n
class ExpectedFailureTestCase(unittest.TestCase):\n    @unittest.expectedFailure\n    def test_fail(self):\n        self.assertEqual(1, 0, "broken")\n
\n
\n

It’s easy to roll your own skipping decorators by making a decorator that calls\nskip() on the test when it wants it to be skipped. This decorator skips\nthe test unless the passed object has a certain attribute:

\n
def skipUnlessHasattr(obj, attr):\n    if hasattr(obj, attr):\n        return lambda func: func\n    return unittest.skip("{0!r} doesn't have {1!r}".format(obj, attr))\n
\n
\n

The following decorators implement test skipping and expected failures:

\n
\n
\nunittest.skip(reason)
\n
Unconditionally skip the decorated test. reason should describe why the\ntest is being skipped.
\n\n
\n
\nunittest.skipIf(condition, reason)
\n
Skip the decorated test if condition is true.
\n\n
\n
\nunittest.skipUnless(condition, reason)
\n
Skip the decorated test unless condition is true.
\n\n
\n
\nunittest.expectedFailure()
\n
Mark the test as an expected failure. If the test fails when run, the test\nis not counted as a failure.
\n\n

Skipped tests will not have setUp() or tearDown() run around them.\nSkipped classes will not have setUpClass() or tearDownClass() run.

\n
\n
\n

25.3.7. Classes and functions

\n

This section describes in depth the API of unittest.

\n
\n

25.3.7.1. Test cases

\n
\n
\nclass unittest.TestCase(methodName='runTest')
\n

Instances of the TestCase class represent the smallest testable units\nin the unittest universe. This class is intended to be used as a base\nclass, with specific tests being implemented by concrete subclasses. This class\nimplements the interface needed by the test runner to allow it to drive the\ntest, and methods that the test code can use to check for and report various\nkinds of failure.

\n

Each instance of TestCase will run a single test method: the method\nnamed methodName. If you remember, we had an earlier example that went\nsomething like this:

\n
def suite():\n    suite = unittest.TestSuite()\n    suite.addTest(WidgetTestCase('test_default_size'))\n    suite.addTest(WidgetTestCase('test_resize'))\n    return suite\n
\n
\n

Here, we create two instances of WidgetTestCase, each of which runs a\nsingle test.

\n

methodName defaults to runTest().

\n

TestCase instances provide three groups of methods: one group used\nto run the test, another used by the test implementation to check conditions\nand report failures, and some inquiry methods allowing information about the\ntest itself to be gathered.

\n

Methods in the first group (running the test) are:

\n
\n
\nsetUp()
\n
Method called to prepare the test fixture. This is called immediately\nbefore calling the test method; any exception raised by this method will\nbe considered an error rather than a test failure. The default\nimplementation does nothing.
\n\n
\n
\ntearDown()
\n
Method called immediately after the test method has been called and the\nresult recorded. This is called even if the test method raised an\nexception, so the implementation in subclasses may need to be particularly\ncareful about checking internal state. Any exception raised by this\nmethod will be considered an error rather than a test failure. This\nmethod will only be called if the setUp() succeeds, regardless of\nthe outcome of the test method. The default implementation does nothing.
\n\n
\n
\nsetUpClass()
\n

A class method called before tests in an individual class run.\nsetUpClass is called with the class as the only argument\nand must be decorated as a classmethod():

\n
@classmethod\ndef setUpClass(cls):\n    ...\n
\n
\n

See Class and Module Fixtures for more details.

\n

\nNew in version 2.7.

\n
\n\n
\n
\ntearDownClass()
\n

A class method called after tests in an individual class have run.\ntearDownClass is called with the class as the only argument\nand must be decorated as a classmethod():

\n
@classmethod\ndef tearDownClass(cls):\n    ...\n
\n
\n

See Class and Module Fixtures for more details.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nrun(result=None)
\n

Run the test, collecting the result into the test result object passed as\nresult. If result is omitted or None, a temporary result\nobject is created (by calling the defaultTestResult() method) and\nused. The result object is not returned to run()‘s caller.

\n

The same effect may be had by simply calling the TestCase\ninstance.

\n
\n\n
\n
\nskipTest(reason)
\n

Calling this during a test method or setUp() skips the current\ntest. See Skipping tests and expected failures for more information.

\n

\nNew in version 2.7.

\n
\n\n
\n
\ndebug()
\n
Run the test without collecting the result. This allows exceptions raised\nby the test to be propagated to the caller, and can be used to support\nrunning tests under a debugger.
\n\n

The TestCase class provides a number of methods to check for and\nreport failures, such as:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
MethodChecks thatNew in
assertEqual(a, b)a == b 
assertNotEqual(a, b)a != b 
assertTrue(x)bool(x) is True 
assertFalse(x)bool(x) is False 
assertIs(a, b)a is b2.7
assertIsNot(a, b)a is not b2.7
assertIsNone(x)x is None2.7
assertIsNotNone(x)x is not None2.7
assertIn(a, b)a in b2.7
assertNotIn(a, b)a not in b2.7
assertIsInstance(a, b)isinstance(a, b)2.7
assertNotIsInstance(a, b)not isinstance(a, b)2.7
\n

All the assert methods (except assertRaises(),\nassertRaisesRegexp())\naccept a msg argument that, if specified, is used as the error message on\nfailure (see also longMessage).

\n
\n
\nassertEqual(first, second, msg=None)
\n

Test that first and second are equal. If the values do not compare\nequal, the test will fail.

\n

In addition, if first and second are the exact same type and one of\nlist, tuple, dict, set, frozenset or unicode or any type that a subclass\nregisters with addTypeEqualityFunc() the type specific equality\nfunction will be called in order to generate a more useful default\nerror message (see also the list of type-specific methods).

\n

\nChanged in version 2.7: Added the automatic calling of type specific equality function.

\n
\n\n
\n
\nassertNotEqual(first, second, msg=None)
\n
Test that first and second are not equal. If the values do compare\nequal, the test will fail.
\n\n
\n
\nassertTrue(expr, msg=None)
\n
\nassertFalse(expr, msg=None)
\n

Test that expr is true (or false).

\n

Note that this is equivalent to bool(expr) is True and not to expr\nis True (use assertIs(expr, True) for the latter). This method\nshould also be avoided when more specific methods are available (e.g.\nassertEqual(a, b) instead of assertTrue(a == b)), because they\nprovide a better error message in case of failure.

\n
\n\n
\n
\nassertIs(first, second, msg=None)
\n
\nassertIsNot(first, second, msg=None)
\n

Test that first and second evaluate (or don’t evaluate) to the same object.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertIsNone(expr, msg=None)
\n
\nassertIsNotNone(expr, msg=None)
\n

Test that expr is (or is not) None.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertIn(first, second, msg=None)
\n
\nassertNotIn(first, second, msg=None)
\n

Test that first is (or is not) in second.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertIsInstance(obj, cls, msg=None)
\n
\nassertNotIsInstance(obj, cls, msg=None)
\n

Test that obj is (or is not) an instance of cls (which can be a\nclass or a tuple of classes, as supported by isinstance()).\nTo check for the exact type, use assertIs(type(obj), cls).

\n

\nNew in version 2.7.

\n
\n\n

It is also possible to check that exceptions and warnings are raised using\nthe following methods:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
MethodChecks thatNew in
assertRaises(exc, fun, *args, **kwds)fun(*args, **kwds) raises exc 
assertRaisesRegexp(exc, re, fun, *args, **kwds)fun(*args, **kwds) raises exc\nand the message matches re2.7
\n
\n
\nassertRaises(exception, callable, *args, **kwds)
\n
\nassertRaises(exception)
\n

Test that an exception is raised when callable is called with any\npositional or keyword arguments that are also passed to\nassertRaises(). The test passes if exception is raised, is an\nerror if another exception is raised, or fails if no exception is raised.\nTo catch any of a group of exceptions, a tuple containing the exception\nclasses may be passed as exception.

\n

If only the exception argument is given, returns a context manager so\nthat the code under test can be written inline rather than as a function:

\n
with self.assertRaises(SomeException):\n    do_something()\n
\n
\n

The context manager will store the caught exception object in its\nexception attribute. This can be useful if the intention\nis to perform additional checks on the exception raised:

\n
with self.assertRaises(SomeException) as cm:\n    do_something()\n\nthe_exception = cm.exception\nself.assertEqual(the_exception.error_code, 3)\n
\n
\n

\nChanged in version 2.7: Added the ability to use assertRaises() as a context manager.

\n
\n\n
\n
\nassertRaisesRegexp(exception, regexp, callable, *args, **kwds)
\n
\nassertRaisesRegexp(exception, regexp)
\n

Like assertRaises() but also tests that regexp matches\non the string representation of the raised exception. regexp may be\na regular expression object or a string containing a regular expression\nsuitable for use by re.search(). Examples:

\n
self.assertRaisesRegexp(ValueError, 'invalid literal for.*XYZ$',\n                        int, 'XYZ')\n
\n
\n

or:

\n
with self.assertRaisesRegexp(ValueError, 'literal'):\n   int('XYZ')\n
\n
\n

\nNew in version 2.7.

\n
\n\n

There are also other methods used to perform more specific checks, such as:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
MethodChecks thatNew in
assertAlmostEqual(a, b)round(a-b, 7) == 0 
assertNotAlmostEqual(a, b)round(a-b, 7) != 0 
assertGreater(a, b)a > b2.7
assertGreaterEqual(a, b)a >= b2.7
assertLess(a, b)a < b2.7
assertLessEqual(a, b)a <= b2.7
assertRegexpMatches(s, re)regex.search(s)2.7
assertNotRegexpMatches(s, re)not regex.search(s)2.7
assertItemsEqual(a, b)sorted(a) == sorted(b) and\nworks with unhashable objs2.7
assertDictContainsSubset(a, b)all the key/value pairs\nin a exist in b2.7
\n
\n
\nassertAlmostEqual(first, second, places=7, msg=None, delta=None)
\n
\nassertNotAlmostEqual(first, second, places=7, msg=None, delta=None)
\n

Test that first and second are approximately (or not approximately)\nequal by computing the difference, rounding to the given number of\ndecimal places (default 7), and comparing to zero. Note that these\nmethods round the values to the given number of decimal places (i.e.\nlike the round() function) and not significant digits.

\n

If delta is supplied instead of places then the difference\nbetween first and second must be less (or more) than delta.

\n

Supplying both delta and places raises a TypeError.

\n

\nChanged in version 2.7: assertAlmostEqual() automatically considers almost equal objects\nthat compare equal. assertNotAlmostEqual() automatically fails\nif the objects compare equal. Added the delta keyword argument.

\n
\n\n
\n
\nassertGreater(first, second, msg=None)
\n
\nassertGreaterEqual(first, second, msg=None)
\n
\nassertLess(first, second, msg=None)
\n
\nassertLessEqual(first, second, msg=None)
\n

Test that first is respectively >, >=, < or <= than second depending\non the method name. If not, the test will fail:

\n
>>> self.assertGreaterEqual(3, 4)\nAssertionError: "3" unexpectedly not greater than or equal to "4"\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertRegexpMatches(text, regexp, msg=None)
\n

Test that a regexp search matches text. In case\nof failure, the error message will include the pattern and the text (or\nthe pattern and the part of text that unexpectedly matched). regexp\nmay be a regular expression object or a string containing a regular\nexpression suitable for use by re.search().

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertNotRegexpMatches(text, regexp, msg=None)
\n

Verifies that a regexp search does not match text. Fails with an error\nmessage including the pattern and the part of text that matches. regexp\nmay be a regular expression object or a string containing a regular\nexpression suitable for use by re.search().

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertItemsEqual(actual, expected, msg=None)
\n

Test that sequence expected contains the same elements as actual,\nregardless of their order. When they don’t, an error message listing the\ndifferences between the sequences will be generated.

\n

Duplicate elements are not ignored when comparing actual and\nexpected. It verifies if each element has the same count in both\nsequences. It is the equivalent of assertEqual(sorted(expected),\nsorted(actual)) but it works with sequences of unhashable objects as\nwell.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertDictContainsSubset(expected, actual, msg=None)
\n

Tests whether the key/value pairs in dictionary actual are a\nsuperset of those in expected. If not, an error message listing\nthe missing keys and mismatched values is generated.

\n

\nNew in version 2.7.

\n

\nDeprecated since version 3.2.

\n
\n\n

The assertEqual() method dispatches the equality check for objects of\nthe same type to different type-specific methods. These methods are already\nimplemented for most of the built-in types, but it’s also possible to\nregister new methods using addTypeEqualityFunc():

\n
\n
\naddTypeEqualityFunc(typeobj, function)
\n

Registers a type-specific method called by assertEqual() to check\nif two objects of exactly the same typeobj (not subclasses) compare\nequal. function must take two positional arguments and a third msg=None\nkeyword argument just as assertEqual() does. It must raise\nself.failureException(msg) when inequality\nbetween the first two parameters is detected – possibly providing useful\ninformation and explaining the inequalities in details in the error\nmessage.

\n

\nNew in version 2.7.

\n
\n\n

The list of type-specific methods automatically used by\nassertEqual() are summarized in the following table. Note\nthat it’s usually not necessary to invoke these methods directly.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
MethodUsed to compareNew in
assertMultiLineEqual(a, b)strings2.7
assertSequenceEqual(a, b)sequences2.7
assertListEqual(a, b)lists2.7
assertTupleEqual(a, b)tuples2.7
assertSetEqual(a, b)sets or frozensets2.7
assertDictEqual(a, b)dicts2.7
\n
\n
\nassertMultiLineEqual(first, second, msg=None)
\n

Test that the multiline string first is equal to the string second.\nWhen not equal a diff of the two strings highlighting the differences\nwill be included in the error message. This method is used by default\nwhen comparing strings with assertEqual().

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertSequenceEqual(seq1, seq2, msg=None, seq_type=None)
\n

Tests that two sequences are equal. If a seq_type is supplied, both\nseq1 and seq2 must be instances of seq_type or a failure will\nbe raised. If the sequences are different an error message is\nconstructed that shows the difference between the two.

\n

This method is not called directly by assertEqual(), but\nit’s used to implement assertListEqual() and\nassertTupleEqual().

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertListEqual(list1, list2, msg=None)
\n
\nassertTupleEqual(tuple1, tuple2, msg=None)
\n

Tests that two lists or tuples are equal. If not an error message is\nconstructed that shows only the differences between the two. An error\nis also raised if either of the parameters are of the wrong type.\nThese methods are used by default when comparing lists or tuples with\nassertEqual().

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertSetEqual(set1, set2, msg=None)
\n

Tests that two sets are equal. If not, an error message is constructed\nthat lists the differences between the sets. This method is used by\ndefault when comparing sets or frozensets with assertEqual().

\n

Fails if either of set1 or set2 does not have a set.difference()\nmethod.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nassertDictEqual(expected, actual, msg=None)
\n

Test that two dictionaries are equal. If not, an error message is\nconstructed that shows the differences in the dictionaries. This\nmethod will be used by default to compare dictionaries in\ncalls to assertEqual().

\n

\nNew in version 2.7.

\n
\n\n

Finally the TestCase provides the following methods and attributes:

\n
\n
\nfail(msg=None)
\n
Signals a test failure unconditionally, with msg or None for\nthe error message.
\n\n
\n
\nfailureException
\n
This class attribute gives the exception raised by the test method. If a\ntest framework needs to use a specialized exception, possibly to carry\nadditional information, it must subclass this exception in order to “play\nfair” with the framework. The initial value of this attribute is\nAssertionError.
\n\n
\n
\nlongMessage
\n

If set to True then any explicit failure message you pass in to the\nassert methods will be appended to the end of the\nnormal failure message. The normal messages contain useful information\nabout the objects involved, for example the message from assertEqual\nshows you the repr of the two unequal objects. Setting this attribute\nto True allows you to have a custom error message in addition to the\nnormal one.

\n

This attribute defaults to False, meaning that a custom message passed\nto an assert method will silence the normal message.

\n

The class setting can be overridden in individual tests by assigning an\ninstance attribute to True or False before calling the assert methods.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nmaxDiff
\n

This attribute controls the maximum length of diffs output by assert\nmethods that report diffs on failure. It defaults to 80*8 characters.\nAssert methods affected by this attribute are\nassertSequenceEqual() (including all the sequence comparison\nmethods that delegate to it), assertDictEqual() and\nassertMultiLineEqual().

\n

Setting maxDiff to None means that there is no maximum length of\ndiffs.

\n

\nNew in version 2.7.

\n
\n\n

Testing frameworks can use the following methods to collect information on\nthe test:

\n
\n
\ncountTestCases()
\n
Return the number of tests represented by this test object. For\nTestCase instances, this will always be 1.
\n\n
\n
\ndefaultTestResult()
\n

Return an instance of the test result class that should be used for this\ntest case class (if no other result instance is provided to the\nrun() method).

\n

For TestCase instances, this will always be an instance of\nTestResult; subclasses of TestCase should override this\nas necessary.

\n
\n\n
\n
\nid()
\n
Return a string identifying the specific test case. This is usually the\nfull name of the test method, including the module and class name.
\n\n
\n
\nshortDescription()
\n
Returns a description of the test, or None if no description\nhas been provided. The default implementation of this method\nreturns the first line of the test method’s docstring, if available,\nor None.
\n\n
\n
\naddCleanup(function, *args, **kwargs)
\n

Add a function to be called after tearDown() to cleanup resources\nused during the test. Functions will be called in reverse order to the\norder they are added (LIFO). They are called with any arguments and\nkeyword arguments passed into addCleanup() when they are\nadded.

\n

If setUp() fails, meaning that tearDown() is not called,\nthen any cleanup functions added will still be called.

\n

\nNew in version 2.7.

\n
\n\n
\n
\ndoCleanups()
\n

This method is called unconditionally after tearDown(), or\nafter setUp() if setUp() raises an exception.

\n

It is responsible for calling all the cleanup functions added by\naddCleanup(). If you need cleanup functions to be called\nprior to tearDown() then you can call doCleanups()\nyourself.

\n

doCleanups() pops methods off the stack of cleanup\nfunctions one at a time, so it can be called at any time.

\n

\nNew in version 2.7.

\n
\n\n
\n\n
\n
\nclass unittest.FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)
\n
This class implements the portion of the TestCase interface which\nallows the test runner to drive the test, but does not provide the methods\nwhich test code can use to check and report errors. This is used to create\ntest cases using legacy test code, allowing it to be integrated into a\nunittest-based test framework.
\n\n
\n

25.3.7.1.1. Deprecated aliases

\n

For historical reasons, some of the TestCase methods had one or more\naliases that are now deprecated. The following table lists the correct names\nalong with their deprecated aliases:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Method NameDeprecated alias(es)
assertEqual()failUnlessEqual, assertEquals
assertNotEqual()failIfEqual
assertTrue()failUnless, assert_
assertFalse()failIf
assertRaises()failUnlessRaises
assertAlmostEqual()failUnlessAlmostEqual
assertNotAlmostEqual()failIfAlmostEqual
\n

\nDeprecated since version 2.7: the aliases listed in the second column

\n
\n
\n
\n
\n

25.3.7.2. Grouping tests

\n
\n
\nclass unittest.TestSuite(tests=())
\n

This class represents an aggregation of individual tests cases and test suites.\nThe class presents the interface needed by the test runner to allow it to be run\nas any other test case. Running a TestSuite instance is the same as\niterating over the suite, running each test individually.

\n

If tests is given, it must be an iterable of individual test cases or other\ntest suites that will be used to build the suite initially. Additional methods\nare provided to add test cases and suites to the collection later on.

\n

TestSuite objects behave much like TestCase objects, except\nthey do not actually implement a test. Instead, they are used to aggregate\ntests into groups of tests that should be run together. Some additional\nmethods are available to add tests to TestSuite instances:

\n
\n
\naddTest(test)
\n
Add a TestCase or TestSuite to the suite.
\n\n
\n
\naddTests(tests)
\n

Add all the tests from an iterable of TestCase and TestSuite\ninstances to this test suite.

\n

This is equivalent to iterating over tests, calling addTest() for\neach element.

\n
\n\n

TestSuite shares the following methods with TestCase:

\n
\n
\nrun(result)
\n
Run the tests associated with this suite, collecting the result into the\ntest result object passed as result. Note that unlike\nTestCase.run(), TestSuite.run() requires the result object to\nbe passed in.
\n\n
\n
\ndebug()
\n
Run the tests associated with this suite without collecting the\nresult. This allows exceptions raised by the test to be propagated to the\ncaller and can be used to support running tests under a debugger.
\n\n
\n
\ncountTestCases()
\n
Return the number of tests represented by this test object, including all\nindividual tests and sub-suites.
\n\n
\n
\n__iter__()
\n

Tests grouped by a TestSuite are always accessed by iteration.\nSubclasses can lazily provide tests by overriding __iter__(). Note\nthat this method maybe called several times on a single suite\n(for example when counting tests or comparing for equality)\nso the tests returned must be the same for repeated iterations.

\n

\nChanged in version 2.7: In earlier versions the TestSuite accessed tests directly rather\nthan through iteration, so overriding __iter__() wasn’t sufficient\nfor providing tests.

\n
\n\n

In the typical usage of a TestSuite object, the run() method\nis invoked by a TestRunner rather than by the end-user test harness.

\n
\n\n
\n
\n

25.3.7.3. Loading and running tests

\n
\n
\nclass unittest.TestLoader
\n

The TestLoader class is used to create test suites from classes and\nmodules. Normally, there is no need to create an instance of this class; the\nunittest module provides an instance that can be shared as\nunittest.defaultTestLoader. Using a subclass or instance, however, allows\ncustomization of some configurable properties.

\n

TestLoader objects have the following methods:

\n
\n
\nloadTestsFromTestCase(testCaseClass)
\n
Return a suite of all tests cases contained in the TestCase-derived\ntestCaseClass.
\n\n
\n
\nloadTestsFromModule(module)
\n

Return a suite of all tests cases contained in the given module. This\nmethod searches module for classes derived from TestCase and\ncreates an instance of the class for each test method defined for the\nclass.

\n
\n

Note

\n

While using a hierarchy of TestCase-derived classes can be\nconvenient in sharing fixtures and helper functions, defining test\nmethods on base classes that are not intended to be instantiated\ndirectly does not play well with this method. Doing so, however, can\nbe useful when the fixtures are different and defined in subclasses.

\n
\n

If a module provides a load_tests function it will be called to\nload the tests. This allows modules to customize test loading.\nThis is the load_tests protocol.

\n

\nChanged in version 2.7: Support for load_tests added.

\n
\n\n
\n
\nloadTestsFromName(name, module=None)
\n

Return a suite of all tests cases given a string specifier.

\n

The specifier name is a “dotted name” that may resolve either to a\nmodule, a test case class, a test method within a test case class, a\nTestSuite instance, or a callable object which returns a\nTestCase or TestSuite instance. These checks are\napplied in the order listed here; that is, a method on a possible test\ncase class will be picked up as “a test method within a test case class”,\nrather than “a callable object”.

\n

For example, if you have a module SampleTests containing a\nTestCase-derived class SampleTestCase with three test\nmethods (test_one(), test_two(), and test_three()), the\nspecifier 'SampleTests.SampleTestCase' would cause this method to\nreturn a suite which will run all three test methods. Using the specifier\n'SampleTests.SampleTestCase.test_two' would cause it to return a test\nsuite which will run only the test_two() test method. The specifier\ncan refer to modules and packages which have not been imported; they will\nbe imported as a side-effect.

\n

The method optionally resolves name relative to the given module.

\n
\n\n
\n
\nloadTestsFromNames(names, module=None)
\n
Similar to loadTestsFromName(), but takes a sequence of names rather\nthan a single name. The return value is a test suite which supports all\nthe tests defined for each name.
\n\n
\n
\ngetTestCaseNames(testCaseClass)
\n
Return a sorted sequence of method names found within testCaseClass;\nthis should be a subclass of TestCase.
\n\n
\n
\ndiscover(start_dir, pattern='test*.py', top_level_dir=None)
\n

Find and return all test modules from the specified start directory,\nrecursing into subdirectories to find them. Only test files that match\npattern will be loaded. (Using shell style pattern matching.) Only\nmodule names that are importable (i.e. are valid Python identifiers) will\nbe loaded.

\n

All test modules must be importable from the top level of the project. If\nthe start directory is not the top level directory then the top level\ndirectory must be specified separately.

\n

If importing a module fails, for example due to a syntax error, then this\nwill be recorded as a single error and discovery will continue.

\n

If a test package name (directory with __init__.py) matches the\npattern then the package will be checked for a load_tests\nfunction. If this exists then it will be called with loader, tests,\npattern.

\n

If load_tests exists then discovery does not recurse into the package,\nload_tests is responsible for loading all tests in the package.

\n

The pattern is deliberately not stored as a loader attribute so that\npackages can continue discovery themselves. top_level_dir is stored so\nload_tests does not need to pass this argument in to\nloader.discover().

\n

start_dir can be a dotted module name as well as a directory.

\n

\nNew in version 2.7.

\n
\n\n

The following attributes of a TestLoader can be configured either by\nsubclassing or assignment on an instance:

\n
\n
\ntestMethodPrefix
\n

String giving the prefix of method names which will be interpreted as test\nmethods. The default value is 'test'.

\n

This affects getTestCaseNames() and all the loadTestsFrom*()\nmethods.

\n
\n\n
\n
\nsortTestMethodsUsing
\n
Function to be used to compare method names when sorting them in\ngetTestCaseNames() and all the loadTestsFrom*() methods. The\ndefault value is the built-in cmp() function; the attribute can also\nbe set to None to disable the sort.
\n\n
\n
\nsuiteClass
\n

Callable object that constructs a test suite from a list of tests. No\nmethods on the resulting object are needed. The default value is the\nTestSuite class.

\n

This affects all the loadTestsFrom*() methods.

\n
\n\n
\n\n
\n
\nclass unittest.TestResult
\n

This class is used to compile information about which tests have succeeded\nand which have failed.

\n

A TestResult object stores the results of a set of tests. The\nTestCase and TestSuite classes ensure that results are\nproperly recorded; test authors do not need to worry about recording the\noutcome of tests.

\n

Testing frameworks built on top of unittest may want access to the\nTestResult object generated by running a set of tests for reporting\npurposes; a TestResult instance is returned by the\nTestRunner.run() method for this purpose.

\n

TestResult instances have the following attributes that will be of\ninterest when inspecting the results of running a set of tests:

\n
\n
\nerrors
\n

A list containing 2-tuples of TestCase instances and strings\nholding formatted tracebacks. Each tuple represents a test which raised an\nunexpected exception.

\n

\nChanged in version 2.2: Contains formatted tracebacks instead of sys.exc_info() results.

\n
\n\n
\n
\nfailures
\n

A list containing 2-tuples of TestCase instances and strings\nholding formatted tracebacks. Each tuple represents a test where a failure\nwas explicitly signalled using the TestCase.fail*() or\nTestCase.assert*() methods.

\n

\nChanged in version 2.2: Contains formatted tracebacks instead of sys.exc_info() results.

\n
\n\n
\n
\nskipped
\n

A list containing 2-tuples of TestCase instances and strings\nholding the reason for skipping the test.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nexpectedFailures
\n
A list containing 2-tuples of TestCase instances and strings\nholding formatted tracebacks. Each tuple represents an expected failure\nof the test case.
\n\n
\n
\nunexpectedSuccesses
\n
A list containing TestCase instances that were marked as expected\nfailures, but succeeded.
\n\n
\n
\nshouldStop
\n
Set to True when the execution of tests should stop by stop().
\n\n
\n
\ntestsRun
\n
The total number of tests run so far.
\n\n
\n
\nbuffer
\n

If set to true, sys.stdout and sys.stderr will be buffered in between\nstartTest() and stopTest() being called. Collected output will\nonly be echoed onto the real sys.stdout and sys.stderr if the test\nfails or errors. Any output is also attached to the failure / error message.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nfailfast
\n

If set to true stop() will be called on the first failure or error,\nhalting the test run.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nwasSuccessful()
\n
Return True if all tests run so far have passed, otherwise returns\nFalse.
\n\n
\n
\nstop()
\n

This method can be called to signal that the set of tests being run should\nbe aborted by setting the shouldStop attribute to True.\nTestRunner objects should respect this flag and return without\nrunning any additional tests.

\n

For example, this feature is used by the TextTestRunner class to\nstop the test framework when the user signals an interrupt from the\nkeyboard. Interactive tools which provide TestRunner\nimplementations can use this in a similar manner.

\n
\n\n

The following methods of the TestResult class are used to maintain\nthe internal data structures, and may be extended in subclasses to support\nadditional reporting requirements. This is particularly useful in building\ntools which support interactive reporting while tests are being run.

\n
\n
\nstartTest(test)
\n
Called when the test case test is about to be run.
\n\n
\n
\nstopTest(test)
\n
Called after the test case test has been executed, regardless of the\noutcome.
\n\n
\n
\nstartTestRun(test)
\n

Called once before any tests are executed.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nstopTestRun(test)
\n

Called once after all tests are executed.

\n

\nNew in version 2.7.

\n
\n\n
\n
\naddError(test, err)
\n

Called when the test case test raises an unexpected exception err is a\ntuple of the form returned by sys.exc_info(): (type, value,\ntraceback).

\n

The default implementation appends a tuple (test, formatted_err) to\nthe instance’s errors attribute, where formatted_err is a\nformatted traceback derived from err.

\n
\n\n
\n
\naddFailure(test, err)
\n

Called when the test case test signals a failure. err is a tuple of\nthe form returned by sys.exc_info(): (type, value, traceback).

\n

The default implementation appends a tuple (test, formatted_err) to\nthe instance’s failures attribute, where formatted_err is a\nformatted traceback derived from err.

\n
\n\n
\n
\naddSuccess(test)
\n

Called when the test case test succeeds.

\n

The default implementation does nothing.

\n
\n\n
\n
\naddSkip(test, reason)
\n

Called when the test case test is skipped. reason is the reason the\ntest gave for skipping.

\n

The default implementation appends a tuple (test, reason) to the\ninstance’s skipped attribute.

\n
\n\n
\n
\naddExpectedFailure(test, err)
\n

Called when the test case test fails, but was marked with the\nexpectedFailure() decorator.

\n

The default implementation appends a tuple (test, formatted_err) to\nthe instance’s expectedFailures attribute, where formatted_err\nis a formatted traceback derived from err.

\n
\n\n
\n
\naddUnexpectedSuccess(test)
\n

Called when the test case test was marked with the\nexpectedFailure() decorator, but succeeded.

\n

The default implementation appends the test to the instance’s\nunexpectedSuccesses attribute.

\n
\n\n
\n\n
\n
\nclass unittest.TextTestResult(stream, descriptions, verbosity)
\n

A concrete implementation of TestResult used by the\nTextTestRunner.

\n

\nNew in version 2.7: This class was previously named _TextTestResult. The old name still\nexists as an alias but is deprecated.

\n
\n\n
\n
\nunittest.defaultTestLoader
\n
Instance of the TestLoader class intended to be shared. If no\ncustomization of the TestLoader is needed, this instance can be used\ninstead of repeatedly creating new instances.
\n\n
\n
\nclass unittest.TextTestRunner(stream=sys.stderr, descriptions=True, verbosity=1)
\n

A basic test runner implementation which prints results on standard error. It\nhas a few configurable parameters, but is essentially very simple. Graphical\napplications which run test suites should provide alternate implementations.

\n
\n
\n_makeResult()
\n

This method returns the instance of TestResult used by run().\nIt is not intended to be called directly, but can be overridden in\nsubclasses to provide a custom TestResult.

\n

_makeResult() instantiates the class or callable passed in the\nTextTestRunner constructor as the resultclass argument. It\ndefaults to TextTestResult if no resultclass is provided.\nThe result class is instantiated with the following arguments:

\n
stream, descriptions, verbosity\n
\n
\n
\n\n
\n\n
\n
\nunittest.main([module[, defaultTest[, argv[, testRunner[, testLoader[, exit[, verbosity[, failfast[, catchbreak[, buffer]]]]]]]]]])
\n

A command-line program that runs a set of tests; this is primarily for making\ntest modules conveniently executable. The simplest use for this function is to\ninclude the following line at the end of a test script:

\n
if __name__ == '__main__':\n    unittest.main()\n
\n
\n

You can run tests with more detailed information by passing in the verbosity\nargument:

\n
if __name__ == '__main__':\n    unittest.main(verbosity=2)\n
\n
\n

The testRunner argument can either be a test runner class or an already\ncreated instance of it. By default main calls sys.exit() with\nan exit code indicating success or failure of the tests run.

\n

main supports being used from the interactive interpreter by passing in the\nargument exit=False. This displays the result on standard output without\ncalling sys.exit():

\n
>>> from unittest import main\n>>> main(module='test_module', exit=False)\n
\n
\n

The failfast, catchbreak and buffer parameters have the same\neffect as the same-name command-line options.

\n

Calling main actually returns an instance of the TestProgram class.\nThis stores the result of the tests run as the result attribute.

\n

\nChanged in version 2.7: The exit, verbosity, failfast, catchbreak and buffer\nparameters were added.

\n
\n\n
\n

25.3.7.3.1. load_tests Protocol

\n

\nNew in version 2.7.

\n

Modules or packages can customize how tests are loaded from them during normal\ntest runs or test discovery by implementing a function called load_tests.

\n

If a test module defines load_tests it will be called by\nTestLoader.loadTestsFromModule() with the following arguments:

\n
load_tests(loader, standard_tests, None)\n
\n
\n

It should return a TestSuite.

\n

loader is the instance of TestLoader doing the loading.\nstandard_tests are the tests that would be loaded by default from the\nmodule. It is common for test modules to only want to add or remove tests\nfrom the standard set of tests.\nThe third argument is used when loading packages as part of test discovery.

\n

A typical load_tests function that loads tests from a specific set of\nTestCase classes may look like:

\n
test_cases = (TestCase1, TestCase2, TestCase3)\n\ndef load_tests(loader, tests, pattern):\n    suite = TestSuite()\n    for test_class in test_cases:\n        tests = loader.loadTestsFromTestCase(test_class)\n        suite.addTests(tests)\n    return suite\n
\n
\n

If discovery is started, either from the command line or by calling\nTestLoader.discover(), with a pattern that matches a package\nname then the package __init__.py will be checked for load_tests.

\n
\n

Note

\n

The default pattern is ‘test*.py’. This matches all Python files\nthat start with ‘test’ but won’t match any test directories.

\n

A pattern like ‘test*’ will match test packages as well as\nmodules.

\n
\n

If the package __init__.py defines load_tests then it will be\ncalled and discovery not continued into the package. load_tests\nis called with the following arguments:

\n
load_tests(loader, standard_tests, pattern)\n
\n
\n

This should return a TestSuite representing all the tests\nfrom the package. (standard_tests will only contain tests\ncollected from __init__.py.)

\n

Because the pattern is passed into load_tests the package is free to\ncontinue (and potentially modify) test discovery. A ‘do nothing’\nload_tests function for a test package would look like:

\n
def load_tests(loader, standard_tests, pattern):\n    # top level directory cached on loader instance\n    this_dir = os.path.dirname(__file__)\n    package_tests = loader.discover(start_dir=this_dir, pattern=pattern)\n    standard_tests.addTests(package_tests)\n    return standard_tests\n
\n
\n
\n
\n
\n
\n

25.3.8. Class and Module Fixtures

\n

Class and module level fixtures are implemented in TestSuite. When\nthe test suite encounters a test from a new class then tearDownClass()\nfrom the previous class (if there is one) is called, followed by\nsetUpClass() from the new class.

\n

Similarly if a test is from a different module from the previous test then\ntearDownModule from the previous module is run, followed by\nsetUpModule from the new module.

\n

After all the tests have run the final tearDownClass and\ntearDownModule are run.

\n

Note that shared fixtures do not play well with [potential] features like test\nparallelization and they break test isolation. They should be used with care.

\n

The default ordering of tests created by the unittest test loaders is to group\nall tests from the same modules and classes together. This will lead to\nsetUpClass / setUpModule (etc) being called exactly once per class and\nmodule. If you randomize the order, so that tests from different modules and\nclasses are adjacent to each other, then these shared fixture functions may be\ncalled multiple times in a single test run.

\n

Shared fixtures are not intended to work with suites with non-standard\nordering. A BaseTestSuite still exists for frameworks that don’t want to\nsupport shared fixtures.

\n

If there are any exceptions raised during one of the shared fixture functions\nthe test is reported as an error. Because there is no corresponding test\ninstance an _ErrorHolder object (that has the same interface as a\nTestCase) is created to represent the error. If you are just using\nthe standard unittest test runner then this detail doesn’t matter, but if you\nare a framework author it may be relevant.

\n
\n

25.3.8.1. setUpClass and tearDownClass

\n

These must be implemented as class methods:

\n
import unittest\n\nclass Test(unittest.TestCase):\n    @classmethod\n    def setUpClass(cls):\n        cls._connection = createExpensiveConnectionObject()\n\n    @classmethod\n    def tearDownClass(cls):\n        cls._connection.destroy()\n
\n
\n

If you want the setUpClass and tearDownClass on base classes called\nthen you must call up to them yourself. The implementations in\nTestCase are empty.

\n

If an exception is raised during a setUpClass then the tests in the class\nare not run and the tearDownClass is not run. Skipped classes will not\nhave setUpClass or tearDownClass run. If the exception is a\nSkipTest exception then the class will be reported as having been skipped\ninstead of as an error.

\n
\n
\n

25.3.8.2. setUpModule and tearDownModule

\n

These should be implemented as functions:

\n
def setUpModule():\n    createConnection()\n\ndef tearDownModule():\n    closeConnection()\n
\n
\n

If an exception is raised in a setUpModule then none of the tests in the\nmodule will be run and the tearDownModule will not be run. If the exception is a\nSkipTest exception then the module will be reported as having been skipped\ninstead of as an error.

\n
\n
\n
\n

25.3.9. Signal Handling

\n

The -c/--catch command-line option to unittest,\nalong with the catchbreak parameter to unittest.main(), provide\nmore friendly handling of control-C during a test run. With catch break\nbehavior enabled control-C will allow the currently running test to complete,\nand the test run will then end and report all the results so far. A second\ncontrol-c will raise a KeyboardInterrupt in the usual way.

\n

The control-c handling signal handler attempts to remain compatible with code or\ntests that install their own signal.SIGINT handler. If the unittest\nhandler is called but isn’t the installed signal.SIGINT handler,\ni.e. it has been replaced by the system under test and delegated to, then it\ncalls the default handler. This will normally be the expected behavior by code\nthat replaces an installed handler and delegates to it. For individual tests\nthat need unittest control-c handling disabled the removeHandler()\ndecorator can be used.

\n

There are a few utility functions for framework authors to enable control-c\nhandling functionality within test frameworks.

\n
\n
\nunittest.installHandler()
\n

Install the control-c handler. When a signal.SIGINT is received\n(usually in response to the user pressing control-c) all registered results\nhave stop() called.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nunittest.registerResult(result)
\n

Register a TestResult object for control-c handling. Registering a\nresult stores a weak reference to it, so it doesn’t prevent the result from\nbeing garbage collected.

\n

Registering a TestResult object has no side-effects if control-c\nhandling is not enabled, so test frameworks can unconditionally register\nall results they create independently of whether or not handling is enabled.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nunittest.removeResult(result)
\n

Remove a registered result. Once a result has been removed then\nstop() will no longer be called on that result object in\nresponse to a control-c.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nunittest.removeHandler(function=None)
\n

When called without arguments this function removes the control-c handler\nif it has been installed. This function can also be used as a test decorator\nto temporarily remove the handler whilst the test is being executed:

\n
@unittest.removeHandler\ndef test_signal_handling(self):\n    ...\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
", "searchableItems": [ { "name": "unittest.expectedFailure", "domId": "unittest_unittest.expectedFailure" }, { "name": "unittest.FunctionTestCase", "domId": "unittest_unittest.FunctionTestCase" }, { "name": "unittest.installHandler", "domId": "unittest_unittest.installHandler" }, { "name": "unittest.main", "domId": "unittest_unittest.main" }, { "name": "unittest.registerResult", "domId": "unittest_unittest.registerResult" }, { "name": "unittest.removeHandler", "domId": "unittest_unittest.removeHandler" }, { "name": "unittest.removeResult", "domId": "unittest_unittest.removeResult" }, { "name": "unittest.skip", "domId": "unittest_unittest.skip" }, { "name": "unittest.skipIf", "domId": "unittest_unittest.skipIf" }, { "name": "unittest.skipUnless", "domId": "unittest_unittest.skipUnless" }, { "name": "unittest.TestCase", "domId": "unittest_unittest.TestCase" }, { "name": "unittest.TestCase.addCleanup", "domId": "unittest_unittest.TestCase.addCleanup" }, { "name": "unittest.TestCase.addTypeEqualityFunc", "domId": "unittest_unittest.TestCase.addTypeEqualityFunc" }, { "name": "unittest.TestCase.assertAlmostEqual", "domId": "unittest_unittest.TestCase.assertAlmostEqual" }, { "name": "unittest.TestCase.assertDictContainsSubset", "domId": "unittest_unittest.TestCase.assertDictContainsSubset" }, { "name": "unittest.TestCase.assertDictEqual", "domId": "unittest_unittest.TestCase.assertDictEqual" }, { "name": "unittest.TestCase.assertEqual", "domId": "unittest_unittest.TestCase.assertEqual" }, { "name": "unittest.TestCase.assertGreater", "domId": "unittest_unittest.TestCase.assertGreater" }, { "name": "unittest.TestCase.assertIn", "domId": "unittest_unittest.TestCase.assertIn" }, { "name": "unittest.TestCase.assertIs", "domId": "unittest_unittest.TestCase.assertIs" }, { "name": "unittest.TestCase.assertIsInstance", "domId": "unittest_unittest.TestCase.assertIsInstance" }, { "name": "unittest.TestCase.assertIsNone", "domId": "unittest_unittest.TestCase.assertIsNone" }, { "name": "unittest.TestCase.assertItemsEqual", "domId": "unittest_unittest.TestCase.assertItemsEqual" }, { "name": "unittest.TestCase.assertListEqual", "domId": "unittest_unittest.TestCase.assertListEqual" }, { "name": "unittest.TestCase.assertMultiLineEqual", "domId": "unittest_unittest.TestCase.assertMultiLineEqual" }, { "name": "unittest.TestCase.assertNotEqual", "domId": "unittest_unittest.TestCase.assertNotEqual" }, { "name": "unittest.TestCase.assertNotRegexpMatches", "domId": "unittest_unittest.TestCase.assertNotRegexpMatches" }, { "name": "unittest.TestCase.assertRaises", "domId": "unittest_unittest.TestCase.assertRaises" }, { "name": "unittest.TestCase.assertRaisesRegexp", "domId": "unittest_unittest.TestCase.assertRaisesRegexp" }, { "name": "unittest.TestCase.assertRegexpMatches", "domId": "unittest_unittest.TestCase.assertRegexpMatches" }, { "name": "unittest.TestCase.assertSequenceEqual", "domId": "unittest_unittest.TestCase.assertSequenceEqual" }, { "name": "unittest.TestCase.assertSetEqual", "domId": "unittest_unittest.TestCase.assertSetEqual" }, { "name": "unittest.TestCase.assertTrue", "domId": "unittest_unittest.TestCase.assertTrue" }, { "name": "unittest.TestCase.countTestCases", "domId": "unittest_unittest.TestCase.countTestCases" }, { "name": "unittest.TestCase.debug", "domId": "unittest_unittest.TestCase.debug" }, { "name": "unittest.TestCase.defaultTestResult", "domId": "unittest_unittest.TestCase.defaultTestResult" }, { "name": "unittest.TestCase.doCleanups", "domId": "unittest_unittest.TestCase.doCleanups" }, { "name": "unittest.TestCase.fail", "domId": "unittest_unittest.TestCase.fail" }, { "name": "unittest.TestCase.id", "domId": "unittest_unittest.TestCase.id" }, { "name": "unittest.TestCase.run", "domId": "unittest_unittest.TestCase.run" }, { "name": "unittest.TestCase.setUp", "domId": "unittest_unittest.TestCase.setUp" }, { "name": "unittest.TestCase.setUpClass", "domId": "unittest_unittest.TestCase.setUpClass" }, { "name": "unittest.TestCase.shortDescription", "domId": "unittest_unittest.TestCase.shortDescription" }, { "name": "unittest.TestCase.skipTest", "domId": "unittest_unittest.TestCase.skipTest" }, { "name": "unittest.TestCase.tearDown", "domId": "unittest_unittest.TestCase.tearDown" }, { "name": "unittest.TestCase.tearDownClass", "domId": "unittest_unittest.TestCase.tearDownClass" }, { "name": "unittest.TestLoader", "domId": "unittest_unittest.TestLoader" }, { "name": "unittest.TestLoader.discover", "domId": "unittest_unittest.TestLoader.discover" }, { "name": "unittest.TestLoader.getTestCaseNames", "domId": "unittest_unittest.TestLoader.getTestCaseNames" }, { "name": "unittest.TestLoader.loadTestsFromModule", "domId": "unittest_unittest.TestLoader.loadTestsFromModule" }, { "name": "unittest.TestLoader.loadTestsFromName", "domId": "unittest_unittest.TestLoader.loadTestsFromName" }, { "name": "unittest.TestLoader.loadTestsFromNames", "domId": "unittest_unittest.TestLoader.loadTestsFromNames" }, { "name": "unittest.TestLoader.loadTestsFromTestCase", "domId": "unittest_unittest.TestLoader.loadTestsFromTestCase" }, { "name": "unittest.TestResult", "domId": "unittest_unittest.TestResult" }, { "name": "unittest.TestResult.addError", "domId": "unittest_unittest.TestResult.addError" }, { "name": "unittest.TestResult.addExpectedFailure", "domId": "unittest_unittest.TestResult.addExpectedFailure" }, { "name": "unittest.TestResult.addFailure", "domId": "unittest_unittest.TestResult.addFailure" }, { "name": "unittest.TestResult.addSkip", "domId": "unittest_unittest.TestResult.addSkip" }, { "name": "unittest.TestResult.addSuccess", "domId": "unittest_unittest.TestResult.addSuccess" }, { "name": "unittest.TestResult.addUnexpectedSuccess", "domId": "unittest_unittest.TestResult.addUnexpectedSuccess" }, { "name": "unittest.TestResult.startTest", "domId": "unittest_unittest.TestResult.startTest" }, { "name": "unittest.TestResult.startTestRun", "domId": "unittest_unittest.TestResult.startTestRun" }, { "name": "unittest.TestResult.stop", "domId": "unittest_unittest.TestResult.stop" }, { "name": "unittest.TestResult.stopTest", "domId": "unittest_unittest.TestResult.stopTest" }, { "name": "unittest.TestResult.stopTestRun", "domId": "unittest_unittest.TestResult.stopTestRun" }, { "name": "unittest.TestResult.wasSuccessful", "domId": "unittest_unittest.TestResult.wasSuccessful" }, { "name": "unittest.TestSuite", "domId": "unittest_unittest.TestSuite" }, { "name": "unittest.TestSuite.__iter__", "domId": "unittest_unittest.TestSuite.__iter__" }, { "name": "unittest.TestSuite.addTest", "domId": "unittest_unittest.TestSuite.addTest" }, { "name": "unittest.TestSuite.addTests", "domId": "unittest_unittest.TestSuite.addTests" }, { "name": "unittest.TestSuite.countTestCases", "domId": "unittest_unittest.TestSuite.countTestCases" }, { "name": "unittest.TestSuite.debug", "domId": "unittest_unittest.TestSuite.debug" }, { "name": "unittest.TestSuite.run", "domId": "unittest_unittest.TestSuite.run" }, { "name": "unittest.TextTestResult", "domId": "unittest_unittest.TextTestResult" }, { "name": "unittest.TextTestRunner", "domId": "unittest_unittest.TextTestRunner" }, { "name": "unittest.TextTestRunner._makeResult", "domId": "unittest_unittest.TextTestRunner._makeResult" } ] }, { "url": "http://docs.python.org/library/bdb.html", "title": "bdb", "html": "
\n

26.1. bdb — Debugger framework

\n

Source code: Lib/bdb.py

\n
\n

The bdb module handles basic debugger functions, like setting breakpoints\nor managing execution via the debugger.

\n

The following exception is defined:

\n
\n
\nexception bdb.BdbQuit
\n
Exception raised by the Bdb class for quitting the debugger.
\n\n

The bdb module also defines two classes:

\n
\n
\nclass bdb.Breakpoint(self, file, line[, temporary=0[, cond=None[, funcname=None]]])
\n

This class implements temporary breakpoints, ignore counts, disabling and\n(re-)enabling, and conditionals.

\n

Breakpoints are indexed by number through a list called bpbynumber\nand by (file, line) pairs through bplist. The former points to a\nsingle instance of class Breakpoint. The latter points to a list of\nsuch instances since there may be more than one breakpoint per line.

\n

When creating a breakpoint, its associated filename should be in canonical\nform. If a funcname is defined, a breakpoint hit will be counted when the\nfirst line of that function is executed. A conditional breakpoint always\ncounts a hit.

\n

Breakpoint instances have the following methods:

\n
\n
\ndeleteMe()
\n
Delete the breakpoint from the list associated to a file/line. If it is\nthe last breakpoint in that position, it also deletes the entry for the\nfile/line.
\n\n
\n
\nenable()
\n
Mark the breakpoint as enabled.
\n\n
\n
\ndisable()
\n
Mark the breakpoint as disabled.
\n\n
\n
\npprint([out])
\n

Print all the information about the breakpoint:

\n
    \n
  • The breakpoint number.
  • \n
  • If it is temporary or not.
  • \n
  • Its file,line position.
  • \n
  • The condition that causes a break.
  • \n
  • If it must be ignored the next N times.
  • \n
  • The breakpoint hit count.
  • \n
\n
\n\n
\n\n
\n
\nclass bdb.Bdb(skip=None)
\n

The Bdb class acts as a generic Python debugger base class.

\n

This class takes care of the details of the trace facility; a derived class\nshould implement user interaction. The standard debugger class\n(pdb.Pdb) is an example.

\n

The skip argument, if given, must be an iterable of glob-style\nmodule name patterns. The debugger will not step into frames that\noriginate in a module that matches one of these patterns. Whether a\nframe is considered to originate in a certain module is determined\nby the __name__ in the frame globals.

\n

\nNew in version 2.7: The skip argument.

\n

The following methods of Bdb normally don’t need to be overridden.

\n
\n
\ncanonic(filename)
\n
Auxiliary method for getting a filename in a canonical form, that is, as a\ncase-normalized (on case-insensitive filesystems) absolute path, stripped\nof surrounding angle brackets.
\n\n
\n
\nreset()
\n
Set the botframe, stopframe, returnframe and\nquitting attributes with values ready to start debugging.
\n\n
\n
\ntrace_dispatch(frame, event, arg)
\n

This function is installed as the trace function of debugged frames. Its\nreturn value is the new trace function (in most cases, that is, itself).

\n

The default implementation decides how to dispatch a frame, depending on\nthe type of event (passed as a string) that is about to be executed.\nevent can be one of the following:

\n
    \n
  • "line": A new line of code is going to be executed.
  • \n
  • "call": A function is about to be called, or another code block\nentered.
  • \n
  • "return": A function or other code block is about to return.
  • \n
  • "exception": An exception has occurred.
  • \n
  • "c_call": A C function is about to be called.
  • \n
  • "c_return": A C function has returned.
  • \n
  • "c_exception": A C function has raised an exception.
  • \n
\n

For the Python events, specialized functions (see below) are called. For\nthe C events, no action is taken.

\n

The arg parameter depends on the previous event.

\n

See the documentation for sys.settrace() for more information on the\ntrace function. For more information on code and frame objects, refer to\nThe standard type hierarchy.

\n
\n\n
\n
\ndispatch_line(frame)
\n
If the debugger should stop on the current line, invoke the\nuser_line() method (which should be overridden in subclasses).\nRaise a BdbQuit exception if the Bdb.quitting flag is set\n(which can be set from user_line()). Return a reference to the\ntrace_dispatch() method for further tracing in that scope.
\n\n
\n
\ndispatch_call(frame, arg)
\n
If the debugger should stop on this function call, invoke the\nuser_call() method (which should be overridden in subclasses).\nRaise a BdbQuit exception if the Bdb.quitting flag is set\n(which can be set from user_call()). Return a reference to the\ntrace_dispatch() method for further tracing in that scope.
\n\n
\n
\ndispatch_return(frame, arg)
\n
If the debugger should stop on this function return, invoke the\nuser_return() method (which should be overridden in subclasses).\nRaise a BdbQuit exception if the Bdb.quitting flag is set\n(which can be set from user_return()). Return a reference to the\ntrace_dispatch() method for further tracing in that scope.
\n\n
\n
\ndispatch_exception(frame, arg)
\n
If the debugger should stop at this exception, invokes the\nuser_exception() method (which should be overridden in subclasses).\nRaise a BdbQuit exception if the Bdb.quitting flag is set\n(which can be set from user_exception()). Return a reference to the\ntrace_dispatch() method for further tracing in that scope.
\n\n

Normally derived classes don’t override the following methods, but they may\nif they want to redefine the definition of stopping and breakpoints.

\n
\n
\nstop_here(frame)
\n
This method checks if the frame is somewhere below botframe in\nthe call stack. botframe is the frame in which debugging started.
\n\n
\n
\nbreak_here(frame)
\n
This method checks if there is a breakpoint in the filename and line\nbelonging to frame or, at least, in the current function. If the\nbreakpoint is a temporary one, this method deletes it.
\n\n
\n
\nbreak_anywhere(frame)
\n
This method checks if there is a breakpoint in the filename of the current\nframe.
\n\n

Derived classes should override these methods to gain control over debugger\noperation.

\n
\n
\nuser_call(frame, argument_list)
\n
This method is called from dispatch_call() when there is the\npossibility that a break might be necessary anywhere inside the called\nfunction.
\n\n
\n
\nuser_line(frame)
\n
This method is called from dispatch_line() when either\nstop_here() or break_here() yields True.
\n\n
\n
\nuser_return(frame, return_value)
\n
This method is called from dispatch_return() when stop_here()\nyields True.
\n\n
\n
\nuser_exception(frame, exc_info)
\n
This method is called from dispatch_exception() when\nstop_here() yields True.
\n\n
\n
\ndo_clear(arg)
\n

Handle how a breakpoint must be removed when it is a temporary one.

\n

This method must be implemented by derived classes.

\n
\n\n

Derived classes and clients can call the following methods to affect the\nstepping state.

\n
\n
\nset_step()
\n
Stop after one line of code.
\n\n
\n
\nset_next(frame)
\n
Stop on the next line in or below the given frame.
\n\n
\n
\nset_return(frame)
\n
Stop when returning from the given frame.
\n\n
\n
\nset_until(frame)
\n
Stop when the line with the line no greater than the current one is\nreached or when returning from current frame
\n\n
\n
\nset_trace([frame])
\n
Start debugging from frame. If frame is not specified, debugging\nstarts from caller’s frame.
\n\n
\n
\nset_continue()
\n
Stop only at breakpoints or when finished. If there are no breakpoints,\nset the system trace function to None.
\n\n
\n
\nset_quit()
\n
Set the quitting attribute to True. This raises BdbQuit in\nthe next call to one of the dispatch_*() methods.
\n\n

Derived classes and clients can call the following methods to manipulate\nbreakpoints. These methods return a string containing an error message if\nsomething went wrong, or None if all is well.

\n
\n
\nset_break(filename, lineno[, temporary=0[, cond[, funcname]]])
\n
Set a new breakpoint. If the lineno line doesn’t exist for the\nfilename passed as argument, return an error message. The filename\nshould be in canonical form, as described in the canonic() method.
\n\n
\n
\nclear_break(filename, lineno)
\n
Delete the breakpoints in filename and lineno. If none were set, an\nerror message is returned.
\n\n
\n
\nclear_bpbynumber(arg)
\n
Delete the breakpoint which has the index arg in the\nBreakpoint.bpbynumber. If arg is not numeric or out of range,\nreturn an error message.
\n\n
\n
\nclear_all_file_breaks(filename)
\n
Delete all breakpoints in filename. If none were set, an error message\nis returned.
\n\n
\n
\nclear_all_breaks()
\n
Delete all existing breakpoints.
\n\n
\n
\nget_break(filename, lineno)
\n
Check if there is a breakpoint for lineno of filename.
\n\n
\n
\nget_breaks(filename, lineno)
\n
Return all breakpoints for lineno in filename, or an empty list if\nnone are set.
\n\n
\n
\nget_file_breaks(filename)
\n
Return all breakpoints in filename, or an empty list if none are set.
\n\n
\n
\nget_all_breaks()
\n
Return all breakpoints that are set.
\n\n

Derived classes and clients can call the following methods to get a data\nstructure representing a stack trace.

\n
\n
\nget_stack(f, t)
\n
Get a list of records for a frame and all higher (calling) and lower\nframes, and the size of the higher part.
\n\n
\n
\nformat_stack_entry(frame_lineno[, lprefix=': '])
\n

Return a string with information about a stack entry, identified by a\n(frame, lineno) tuple:

\n
    \n
  • The canonical form of the filename which contains the frame.
  • \n
  • The function name, or "<lambda>".
  • \n
  • The input arguments.
  • \n
  • The return value.
  • \n
  • The line of code (if it exists).
  • \n
\n
\n\n

The following two methods can be called by clients to use a debugger to debug\na statement, given as a string.

\n
\n
\nrun(cmd[, globals[, locals]])
\n
Debug a statement executed via the exec statement. globals\ndefaults to __main__.__dict__, locals defaults to globals.
\n\n
\n
\nruneval(expr[, globals[, locals]])
\n
Debug an expression executed via the eval() function. globals and\nlocals have the same meaning as in run().
\n\n
\n
\nrunctx(cmd, globals, locals)
\n
For backwards compatibility. Calls the run() method.
\n\n
\n
\nruncall(func, *args, **kwds)
\n
Debug a single function call, and return its result.
\n\n
\n\n

Finally, the module defines the following functions:

\n
\n
\nbdb.checkfuncname(b, frame)
\n

Check whether we should break here, depending on the way the breakpoint b\nwas set.

\n

If it was set via line number, it checks if b.line is the same as the one\nin the frame also passed as argument. If the breakpoint was set via function\nname, we have to check we are in the right frame (the right function) and if\nwe are in its first executable line.

\n
\n\n
\n
\nbdb.effective(file, line, frame)
\n
Determine if there is an effective (active) breakpoint at this line of code.\nReturn a tuple of the breakpoint and a boolean that indicates if it is ok\nto delete a temporary breakpoint. Return (None, None) if there is no\nmatching breakpoint.
\n\n
\n
\nbdb.set_trace()
\n
Start debugging with a Bdb instance from caller’s frame.
\n\n
", "searchableItems": [ { "name": "bdb.Bdb", "domId": "bdb_bdb.Bdb" }, { "name": "bdb.Bdb.break_anywhere", "domId": "bdb_bdb.Bdb.break_anywhere" }, { "name": "bdb.Bdb.break_here", "domId": "bdb_bdb.Bdb.break_here" }, { "name": "bdb.Bdb.canonic", "domId": "bdb_bdb.Bdb.canonic" }, { "name": "bdb.Bdb.clear_all_breaks", "domId": "bdb_bdb.Bdb.clear_all_breaks" }, { "name": "bdb.Bdb.clear_all_file_breaks", "domId": "bdb_bdb.Bdb.clear_all_file_breaks" }, { "name": "bdb.Bdb.clear_bpbynumber", "domId": "bdb_bdb.Bdb.clear_bpbynumber" }, { "name": "bdb.Bdb.clear_break", "domId": "bdb_bdb.Bdb.clear_break" }, { "name": "bdb.Bdb.dispatch_call", "domId": "bdb_bdb.Bdb.dispatch_call" }, { "name": "bdb.Bdb.dispatch_exception", "domId": "bdb_bdb.Bdb.dispatch_exception" }, { "name": "bdb.Bdb.dispatch_line", "domId": "bdb_bdb.Bdb.dispatch_line" }, { "name": "bdb.Bdb.dispatch_return", "domId": "bdb_bdb.Bdb.dispatch_return" }, { "name": "bdb.Bdb.do_clear", "domId": "bdb_bdb.Bdb.do_clear" }, { "name": "bdb.Bdb.format_stack_entry", "domId": "bdb_bdb.Bdb.format_stack_entry" }, { "name": "bdb.Bdb.get_all_breaks", "domId": "bdb_bdb.Bdb.get_all_breaks" }, { "name": "bdb.Bdb.get_break", "domId": "bdb_bdb.Bdb.get_break" }, { "name": "bdb.Bdb.get_breaks", "domId": "bdb_bdb.Bdb.get_breaks" }, { "name": "bdb.Bdb.get_file_breaks", "domId": "bdb_bdb.Bdb.get_file_breaks" }, { "name": "bdb.Bdb.get_stack", "domId": "bdb_bdb.Bdb.get_stack" }, { "name": "bdb.Bdb.reset", "domId": "bdb_bdb.Bdb.reset" }, { "name": "bdb.Bdb.run", "domId": "bdb_bdb.Bdb.run" }, { "name": "bdb.Bdb.runcall", "domId": "bdb_bdb.Bdb.runcall" }, { "name": "bdb.Bdb.runctx", "domId": "bdb_bdb.Bdb.runctx" }, { "name": "bdb.Bdb.runeval", "domId": "bdb_bdb.Bdb.runeval" }, { "name": "bdb.Bdb.set_break", "domId": "bdb_bdb.Bdb.set_break" }, { "name": "bdb.Bdb.set_continue", "domId": "bdb_bdb.Bdb.set_continue" }, { "name": "bdb.Bdb.set_next", "domId": "bdb_bdb.Bdb.set_next" }, { "name": "bdb.Bdb.set_quit", "domId": "bdb_bdb.Bdb.set_quit" }, { "name": "bdb.Bdb.set_return", "domId": "bdb_bdb.Bdb.set_return" }, { "name": "bdb.Bdb.set_step", "domId": "bdb_bdb.Bdb.set_step" }, { "name": "bdb.Bdb.set_trace", "domId": "bdb_bdb.Bdb.set_trace" }, { "name": "bdb.Bdb.set_until", "domId": "bdb_bdb.Bdb.set_until" }, { "name": "bdb.Bdb.stop_here", "domId": "bdb_bdb.Bdb.stop_here" }, { "name": "bdb.Bdb.trace_dispatch", "domId": "bdb_bdb.Bdb.trace_dispatch" }, { "name": "bdb.Bdb.user_call", "domId": "bdb_bdb.Bdb.user_call" }, { "name": "bdb.Bdb.user_exception", "domId": "bdb_bdb.Bdb.user_exception" }, { "name": "bdb.Bdb.user_line", "domId": "bdb_bdb.Bdb.user_line" }, { "name": "bdb.Bdb.user_return", "domId": "bdb_bdb.Bdb.user_return" }, { "name": "bdb.Breakpoint", "domId": "bdb_bdb.Breakpoint" }, { "name": "bdb.Breakpoint.deleteMe", "domId": "bdb_bdb.Breakpoint.deleteMe" }, { "name": "bdb.Breakpoint.disable", "domId": "bdb_bdb.Breakpoint.disable" }, { "name": "bdb.Breakpoint.enable", "domId": "bdb_bdb.Breakpoint.enable" }, { "name": "bdb.Breakpoint.pprint", "domId": "bdb_bdb.Breakpoint.pprint" }, { "name": "bdb.checkfuncname", "domId": "bdb_bdb.checkfuncname" }, { "name": "bdb.effective", "domId": "bdb_bdb.effective" }, { "name": "bdb.set_trace", "domId": "bdb_bdb.set_trace" } ] }, { "url": "http://docs.python.org/library/pdb.html", "title": "pdb", "html": "
\n

26.2. pdb — The Python Debugger

\n

The module pdb defines an interactive source code debugger for Python\nprograms. It supports setting (conditional) breakpoints and single stepping at\nthe source line level, inspection of stack frames, source code listing, and\nevaluation of arbitrary Python code in the context of any stack frame. It also\nsupports post-mortem debugging and can be called under program control.

\n

The debugger is extensible — it is actually defined as the class Pdb.\nThis is currently undocumented but easily understood by reading the source. The\nextension interface uses the modules bdb and cmd.

\n

The debugger’s prompt is (Pdb). Typical usage to run a program under control\nof the debugger is:

\n
>>> import pdb\n>>> import mymodule\n>>> pdb.run('mymodule.test()')\n> <string>(0)?()\n(Pdb) continue\n> <string>(1)?()\n(Pdb) continue\nNameError: 'spam'\n> <string>(1)?()\n(Pdb)\n
\n
\n

pdb.py can also be invoked as a script to debug other scripts. For\nexample:

\n
python -m pdb myscript.py
\n
\n

When invoked as a script, pdb will automatically enter post-mortem debugging if\nthe program being debugged exits abnormally. After post-mortem debugging (or\nafter normal exit of the program), pdb will restart the program. Automatic\nrestarting preserves pdb’s state (such as breakpoints) and in most cases is more\nuseful than quitting the debugger upon program’s exit.

\n

\nNew in version 2.4: Restarting post-mortem behavior added.

\n

The typical usage to break into the debugger from a running program is to\ninsert

\n
import pdb; pdb.set_trace()\n
\n
\n

at the location you want to break into the debugger. You can then step through\nthe code following this statement, and continue running without the debugger using\nthe c command.

\n

The typical usage to inspect a crashed program is:

\n
>>> import pdb\n>>> import mymodule\n>>> mymodule.test()\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\n  File "./mymodule.py", line 4, in test\n    test2()\n  File "./mymodule.py", line 3, in test2\n    print spam\nNameError: spam\n>>> pdb.pm()\n> ./mymodule.py(3)test2()\n-> print spam\n(Pdb)\n
\n
\n

The module defines the following functions; each enters the debugger in a\nslightly different way:

\n
\n
\npdb.run(statement[, globals[, locals]])
\n
Execute the statement (given as a string) under debugger control. The\ndebugger prompt appears before any code is executed; you can set breakpoints and\ntype continue, or you can step through the statement using step or\nnext (all these commands are explained below). The optional globals and\nlocals arguments specify the environment in which the code is executed; by\ndefault the dictionary of the module __main__ is used. (See the\nexplanation of the exec statement or the eval() built-in\nfunction.)
\n\n
\n
\npdb.runeval(expression[, globals[, locals]])
\n
Evaluate the expression (given as a string) under debugger control. When\nruneval() returns, it returns the value of the expression. Otherwise this\nfunction is similar to run().
\n\n
\n
\npdb.runcall(function[, argument, ...])
\n
Call the function (a function or method object, not a string) with the given\narguments. When runcall() returns, it returns whatever the function call\nreturned. The debugger prompt appears as soon as the function is entered.
\n\n
\n
\npdb.set_trace()
\n
Enter the debugger at the calling stack frame. This is useful to hard-code a\nbreakpoint at a given point in a program, even if the code is not otherwise\nbeing debugged (e.g. when an assertion fails).
\n\n
\n
\npdb.post_mortem([traceback])
\n
Enter post-mortem debugging of the given traceback object. If no\ntraceback is given, it uses the one of the exception that is currently\nbeing handled (an exception must be being handled if the default is to be\nused).
\n\n
\n
\npdb.pm()
\n
Enter post-mortem debugging of the traceback found in\nsys.last_traceback.
\n\n

The run* functions and set_trace() are aliases for instantiating the\nPdb class and calling the method of the same name. If you want to\naccess further features, you have to do this yourself:

\n
\n
\nclass pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None)
\n

Pdb is the debugger class.

\n

The completekey, stdin and stdout arguments are passed to the\nunderlying cmd.Cmd class; see the description there.

\n

The skip argument, if given, must be an iterable of glob-style module name\npatterns. The debugger will not step into frames that originate in a module\nthat matches one of these patterns. [1]

\n

Example call to enable tracing with skip:

\n
import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n
\n
\n

\nNew in version 2.7: The skip argument.

\n
\n
\nrun(statement[, globals[, locals]])
\n
\nruneval(expression[, globals[, locals]])
\n
\nruncall(function[, argument, ...])
\n
\nset_trace()
\n
See the documentation for the functions explained above.
\n\n
\n\n
\n

26.3. Debugger Commands

\n

The debugger recognizes the following commands. Most commands can be\nabbreviated to one or two letters; e.g. h(elp) means that either h or\nhelp can be used to enter the help command (but not he or hel, nor\nH or Help or HELP). Arguments to commands must be separated by\nwhitespace (spaces or tabs). Optional arguments are enclosed in square brackets\n([]) in the command syntax; the square brackets must not be typed.\nAlternatives in the command syntax are separated by a vertical bar (|).

\n

Entering a blank line repeats the last command entered. Exception: if the last\ncommand was a list command, the next 11 lines are listed.

\n

Commands that the debugger doesn’t recognize are assumed to be Python statements\nand are executed in the context of the program being debugged. Python\nstatements can also be prefixed with an exclamation point (!). This is a\npowerful way to inspect the program being debugged; it is even possible to\nchange a variable or call a function. When an exception occurs in such a\nstatement, the exception name is printed but the debugger’s state is not\nchanged.

\n

Multiple commands may be entered on a single line, separated by ;;. (A\nsingle ; is not used as it is the separator for multiple commands in a line\nthat is passed to the Python parser.) No intelligence is applied to separating\nthe commands; the input is split at the first ;; pair, even if it is in the\nmiddle of a quoted string.

\n

The debugger supports aliases. Aliases can have parameters which allows one a\ncertain level of adaptability to the context under examination.

\n

If a file .pdbrc exists in the user’s home directory or in the current\ndirectory, it is read in and executed as if it had been typed at the debugger\nprompt. This is particularly useful for aliases. If both files exist, the one\nin the home directory is read first and aliases defined there can be overridden\nby the local file.

\n
\n
h(elp) [command]
\n
Without argument, print the list of available commands. With a command as\nargument, print help about that command. help pdb displays the full\ndocumentation file; if the environment variable PAGER is defined, the\nfile is piped through that command instead. Since the command argument must\nbe an identifier, help exec must be entered to get help on the !\ncommand.
\n
w(here)
\n
Print a stack trace, with the most recent frame at the bottom. An arrow\nindicates the current frame, which determines the context of most commands.
\n
d(own)
\n
Move the current frame one level down in the stack trace (to a newer frame).
\n
u(p)
\n
Move the current frame one level up in the stack trace (to an older frame).
\n
b(reak) [[filename:]lineno | function[, condition]]
\n

With a lineno argument, set a break there in the current file. With a\nfunction argument, set a break at the first executable statement within that\nfunction. The line number may be prefixed with a filename and a colon, to\nspecify a breakpoint in another file (probably one that hasn’t been loaded yet).\nThe file is searched on sys.path. Note that each breakpoint is assigned a\nnumber to which all the other breakpoint commands refer.

\n

If a second argument is present, it is an expression which must evaluate to true\nbefore the breakpoint is honored.

\n

Without argument, list all breaks, including for each breakpoint, the number of\ntimes that breakpoint has been hit, the current ignore count, and the associated\ncondition if any.

\n
\n
tbreak [[filename:]lineno | function[, condition]]
\n
Temporary breakpoint, which is removed automatically when it is first hit. The\narguments are the same as break.
\n
cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
\n
With a filename:lineno argument, clear all the breakpoints at this line.\nWith a space separated list of breakpoint numbers, clear those breakpoints.\nWithout argument, clear all breaks (but first ask confirmation).
\n
disable [bpnumber [bpnumber ...]]
\n
Disables the breakpoints given as a space separated list of breakpoint numbers.\nDisabling a breakpoint means it cannot cause the program to stop execution, but\nunlike clearing a breakpoint, it remains in the list of breakpoints and can be\n(re-)enabled.
\n
enable [bpnumber [bpnumber ...]]
\n
Enables the breakpoints specified.
\n
ignore bpnumber [count]
\n
Sets the ignore count for the given breakpoint number. If count is omitted, the\nignore count is set to 0. A breakpoint becomes active when the ignore count is\nzero. When non-zero, the count is decremented each time the breakpoint is\nreached and the breakpoint is not disabled and any associated condition\nevaluates to true.
\n
condition bpnumber [condition]
\n
Condition is an expression which must evaluate to true before the breakpoint is\nhonored. If condition is absent, any existing condition is removed; i.e., the\nbreakpoint is made unconditional.
\n
commands [bpnumber]
\n

Specify a list of commands for breakpoint number bpnumber. The commands\nthemselves appear on the following lines. Type a line containing just ‘end’ to\nterminate the commands. An example:

\n
(Pdb) commands 1\n(com) print some_variable\n(com) end\n(Pdb)
\n
\n

To remove all commands from a breakpoint, type commands and follow it\nimmediately with end; that is, give no commands.

\n

With no bpnumber argument, commands refers to the last breakpoint set.

\n

You can use breakpoint commands to start your program up again. Simply use the\ncontinue command, or step, or any other command that resumes execution.

\n

Specifying any command resuming execution (currently continue, step, next,\nreturn, jump, quit and their abbreviations) terminates the command list (as if\nthat command was immediately followed by end). This is because any time you\nresume execution (even with a simple next or step), you may encounter another\nbreakpoint–which could have its own command list, leading to ambiguities about\nwhich list to execute.

\n

If you use the ‘silent’ command in the command list, the usual message about\nstopping at a breakpoint is not printed. This may be desirable for breakpoints\nthat are to print a specific message and then continue. If none of the other\ncommands print anything, you see no sign that the breakpoint was reached.

\n

\nNew in version 2.5.

\n
\n
s(tep)
\n
Execute the current line, stop at the first possible occasion (either in a\nfunction that is called or on the next line in the current function).
\n
n(ext)
\n
Continue execution until the next line in the current function is reached or it\nreturns. (The difference between next and step is that step stops\ninside a called function, while next executes called functions at (nearly)\nfull speed, only stopping at the next line in the current function.)
\n
unt(il)
\n

Continue execution until the line with the line number greater than the\ncurrent one is reached or when returning from current frame.

\n

\nNew in version 2.6.

\n
\n
r(eturn)
\n
Continue execution until the current function returns.
\n
c(ont(inue))
\n
Continue execution, only stop when a breakpoint is encountered.
\n
j(ump) lineno
\n

Set the next line that will be executed. Only available in the bottom-most\nframe. This lets you jump back and execute code again, or jump forward to skip\ncode that you don’t want to run.

\n

It should be noted that not all jumps are allowed — for instance it is not\npossible to jump into the middle of a for loop or out of a\nfinally clause.

\n
\n
l(ist) [first[, last]]
\n
List source code for the current file. Without arguments, list 11 lines around\nthe current line or continue the previous listing. With one argument, list 11\nlines around at that line. With two arguments, list the given range; if the\nsecond argument is less than the first, it is interpreted as a count.
\n
a(rgs)
\n
Print the argument list of the current function.
\n
p expression
\n

Evaluate the expression in the current context and print its value.

\n
\n

Note

\n

print can also be used, but is not a debugger command — this executes the\nPython print statement.

\n
\n
\n
pp expression
\n
Like the p command, except the value of the expression is pretty-printed\nusing the pprint module.
\n
alias [name [command]]
\n

Creates an alias called name that executes command. The command must not\nbe enclosed in quotes. Replaceable parameters can be indicated by %1,\n%2, and so on, while %* is replaced by all the parameters. If no\ncommand is given, the current alias for name is shown. If no arguments are\ngiven, all aliases are listed.

\n

Aliases may be nested and can contain anything that can be legally typed at the\npdb prompt. Note that internal pdb commands can be overridden by aliases.\nSuch a command is then hidden until the alias is removed. Aliasing is\nrecursively applied to the first word of the command line; all other words in\nthe line are left alone.

\n

As an example, here are two useful aliases (especially when placed in the\n.pdbrc file):

\n
#Print instance variables (usage \"pi classInst\")\nalias pi for k in %1.__dict__.keys(): print \"%1.\",k,\"=\",%1.__dict__[k]\n#Print instance variables in self\nalias ps pi self
\n
\n
\n
unalias name
\n
Deletes the specified alias.
\n
[!]statement
\n

Execute the (one-line) statement in the context of the current stack frame.\nThe exclamation point can be omitted unless the first word of the statement\nresembles a debugger command. To set a global variable, you can prefix the\nassignment command with a global command on the same line, e.g.:

\n
(Pdb) global list_options; list_options = ['-l']\n(Pdb)
\n
\n
\n
run [args ...]
\n

Restart the debugged Python program. If an argument is supplied, it is split\nwith “shlex” and the result is used as the new sys.argv. History, breakpoints,\nactions and debugger options are preserved. “restart” is an alias for “run”.

\n

\nNew in version 2.6.

\n
\n
q(uit)
\n
Quit from the debugger. The program being executed is aborted.
\n
\n

Footnotes

\n\n\n\n\n\n
[1]Whether a frame is considered to originate in a certain module\nis determined by the __name__ in the frame globals.
\n
", "searchableItems": [ { "name": "pdb.Pdb", "domId": "pdb_pdb.Pdb" }, { "name": "pdb.Pdb.run", "domId": "pdb_pdb.Pdb.run" }, { "name": "pdb.pm", "domId": "pdb_pdb.pm" }, { "name": "pdb.post_mortem", "domId": "pdb_pdb.post_mortem" }, { "name": "pdb.run", "domId": "pdb_pdb.run" }, { "name": "pdb.runcall", "domId": "pdb_pdb.runcall" }, { "name": "pdb.runeval", "domId": "pdb_pdb.runeval" }, { "name": "pdb.set_trace", "domId": "pdb_pdb.set_trace" } ] }, { "url": "http://docs.python.org/library/profile.html", "title": "", "html": "
\n

26.4. The Python Profilers

\n

Source code: Lib/profile.py and Lib/pstats.py

\n
\n
\n

26.4.1. Introduction to the profilers

\n

A profiler is a program that describes the run time performance\nof a program, providing a variety of statistics. This documentation\ndescribes the profiler functionality provided in the modules\ncProfile, profile and pstats. This profiler\nprovides deterministic profiling of Python programs. It also\nprovides a series of report generation tools to allow users to rapidly\nexamine the results of a profile operation.

\n

The Python standard library provides three different profilers:

\n
    \n
  1. cProfile is recommended for most users; it’s a C extension\nwith reasonable overhead\nthat makes it suitable for profiling long-running programs.\nBased on lsprof,\ncontributed by Brett Rosen and Ted Czotter.

    \n

    \nNew in version 2.5.

    \n
  2. \n
  3. profile, a pure Python module whose interface is imitated by\ncProfile. Adds significant overhead to profiled programs.\nIf you’re trying to extend\nthe profiler in some way, the task might be easier with this module.

    \n

    \nChanged in version 2.4: Now also reports the time spent in calls to built-in functions and methods.

    \n
  4. \n
  5. hotshot was an experimental C module that focused on minimizing\nthe overhead of profiling, at the expense of longer data\npost-processing times. It is no longer maintained and may be\ndropped in a future version of Python.

    \n

    \nChanged in version 2.5: The results should be more meaningful than in the past: the timing core\ncontained a critical bug.

    \n
  6. \n
\n

The profile and cProfile modules export the same interface, so\nthey are mostly interchangeable; cProfile has a much lower overhead but\nis newer and might not be available on all systems.\ncProfile is really a compatibility layer on top of the internal\n_lsprof module. The hotshot module is reserved for specialized\nusage.

\n
\n
\n

26.4.2. Instant User’s Manual

\n

This section is provided for users that “don’t want to read the manual.” It\nprovides a very brief overview, and allows a user to rapidly perform profiling\non an existing application.

\n

To profile an application with a main entry point of foo(), you would add\nthe following to your module:

\n
import cProfile\ncProfile.run('foo()')\n
\n
\n

(Use profile instead of cProfile if the latter is not available on\nyour system.)

\n

The above action would cause foo() to be run, and a series of informative\nlines (the profile) to be printed. The above approach is most useful when\nworking with the interpreter. If you would like to save the results of a\nprofile into a file for later examination, you can supply a file name as the\nsecond argument to the run() function:

\n
import cProfile\ncProfile.run('foo()', 'fooprof')\n
\n
\n

The file cProfile.py can also be invoked as a script to profile another\nscript. For example:

\n
python -m cProfile myscript.py
\n
\n

cProfile.py accepts two optional arguments on the command line:

\n
cProfile.py [-o output_file] [-s sort_order]
\n
\n

-s only applies to standard output (-o is not supplied).\nLook in the Stats documentation for valid sort values.

\n

When you wish to review the profile, you should use the methods in the\npstats module. Typically you would load the statistics data as follows:

\n
import pstats\np = pstats.Stats('fooprof')\n
\n
\n

The class Stats (the above code just created an instance of this class)\nhas a variety of methods for manipulating and printing the data that was just\nread into p. When you ran cProfile.run() above, what was printed was\nthe result of three method calls:

\n
p.strip_dirs().sort_stats(-1).print_stats()\n
\n
\n

The first method removed the extraneous path from all the module names. The\nsecond method sorted all the entries according to the standard module/line/name\nstring that is printed. The third method printed out all the statistics. You\nmight try the following sort calls:

\n
p.sort_stats('name')\np.print_stats()\n
\n
\n

The first call will actually sort the list by function name, and the second call\nwill print out the statistics. The following are some interesting calls to\nexperiment with:

\n
p.sort_stats('cumulative').print_stats(10)\n
\n
\n

This sorts the profile by cumulative time in a function, and then only prints\nthe ten most significant lines. If you want to understand what algorithms are\ntaking time, the above line is what you would use.

\n

If you were looking to see what functions were looping a lot, and taking a lot\nof time, you would do:

\n
p.sort_stats('time').print_stats(10)\n
\n
\n

to sort according to time spent within each function, and then print the\nstatistics for the top ten functions.

\n

You might also try:

\n
p.sort_stats('file').print_stats('__init__')\n
\n
\n

This will sort all the statistics by file name, and then print out statistics\nfor only the class init methods (since they are spelled with __init__ in\nthem). As one final example, you could try:

\n
p.sort_stats('time', 'cum').print_stats(.5, 'init')\n
\n
\n

This line sorts statistics with a primary key of time, and a secondary key of\ncumulative time, and then prints out some of the statistics. To be specific, the\nlist is first culled down to 50% (re: .5) of its original size, then only\nlines containing init are maintained, and that sub-sub-list is printed.

\n

If you wondered what functions called the above functions, you could now (p\nis still sorted according to the last criteria) do:

\n
p.print_callers(.5, 'init')\n
\n
\n

and you would get a list of callers for each of the listed functions.

\n

If you want more functionality, you’re going to have to read the manual, or\nguess what the following functions do:

\n
p.print_callees()\np.add('fooprof')\n
\n
\n

Invoked as a script, the pstats module is a statistics browser for\nreading and examining profile dumps. It has a simple line-oriented interface\n(implemented using cmd) and interactive help.

\n
\n
\n

26.4.3. What Is Deterministic Profiling?

\n

Deterministic profiling is meant to reflect the fact that all function\ncall, function return, and exception events are monitored, and precise\ntimings are made for the intervals between these events (during which time the\nuser’s code is executing). In contrast, statistical profiling (which is\nnot done by this module) randomly samples the effective instruction pointer, and\ndeduces where time is being spent. The latter technique traditionally involves\nless overhead (as the code does not need to be instrumented), but provides only\nrelative indications of where time is being spent.

\n

In Python, since there is an interpreter active during execution, the presence\nof instrumented code is not required to do deterministic profiling. Python\nautomatically provides a hook (optional callback) for each event. In\naddition, the interpreted nature of Python tends to add so much overhead to\nexecution, that deterministic profiling tends to only add small processing\noverhead in typical applications. The result is that deterministic profiling is\nnot that expensive, yet provides extensive run time statistics about the\nexecution of a Python program.

\n

Call count statistics can be used to identify bugs in code (surprising counts),\nand to identify possible inline-expansion points (high call counts). Internal\ntime statistics can be used to identify “hot loops” that should be carefully\noptimized. Cumulative time statistics should be used to identify high level\nerrors in the selection of algorithms. Note that the unusual handling of\ncumulative times in this profiler allows statistics for recursive\nimplementations of algorithms to be directly compared to iterative\nimplementations.

\n
\n
\n

26.4.4. Reference Manual – profile and cProfile

\n

The primary entry point for the profiler is the global function\nprofile.run() (resp. cProfile.run()). It is typically used to create\nany profile information. The reports are formatted and printed using methods of\nthe class pstats.Stats. The following is a description of all of these\nstandard entry points and functions. For a more in-depth view of some of the\ncode, consider reading the later section on Profiler Extensions, which includes\ndiscussion of how to derive “better” profilers from the classes presented, or\nreading the source code for these modules.

\n
\n
\ncProfile.run(command[, filename])
\n

This function takes a single argument that can be passed to the\nexec statement, and an optional file name. In all cases this\nroutine attempts to exec its first argument, and gather profiling\nstatistics from the execution. If no file name is present, then this function\nautomatically prints a simple profiling report, sorted by the standard name\nstring (file/line/function-name) that is presented in each line. The\nfollowing is a typical output from such a call:

\n
      2706 function calls (2004 primitive calls) in 4.504 CPU seconds\n\nOrdered by: standard name\n\nncalls  tottime  percall  cumtime  percall filename:lineno(function)\n     2    0.006    0.003    0.953    0.477 pobject.py:75(save_objects)\n  43/3    0.533    0.012    0.749    0.250 pobject.py:99(evaluate)\n ...
\n
\n

The first line indicates that 2706 calls were monitored. Of those calls, 2004\nwere primitive. We define primitive to mean that the call was not\ninduced via recursion. The next line: Ordered by: standard name, indicates\nthat the text string in the far right column was used to sort the output. The\ncolumn headings include:

\n
\n
ncalls
\n
for the number of calls,
\n
tottime
\n
for the total time spent in the given function (and excluding time made in calls\nto sub-functions),
\n
percall
\n
is the quotient of tottime divided by ncalls
\n
cumtime
\n
is the total time spent in this and all subfunctions (from invocation till\nexit). This figure is accurate even for recursive functions.
\n
percall
\n
is the quotient of cumtime divided by primitive calls
\n
filename:lineno(function)
\n
provides the respective data of each function
\n
\n

When there are two numbers in the first column (for example, 43/3), then the\nlatter is the number of primitive calls, and the former is the actual number of\ncalls. Note that when the function does not recurse, these two values are the\nsame, and only the single figure is printed.

\n
\n\n
\n
\ncProfile.runctx(command, globals, locals[, filename])
\n
This function is similar to run(), with added arguments to supply the\nglobals and locals dictionaries for the command string.
\n\n

Analysis of the profiler data is done using the Stats class.

\n
\n

Note

\n

The Stats class is defined in the pstats module.

\n
\n
\n
\nclass pstats.Stats(filename[, stream=sys.stdout[, ...]])
\n

This class constructor creates an instance of a “statistics object” from a\nfilename (or set of filenames). Stats objects are manipulated by\nmethods, in order to print useful reports. You may specify an alternate output\nstream by giving the keyword argument, stream.

\n

The file selected by the above constructor must have been created by the\ncorresponding version of profile or cProfile. To be specific,\nthere is no file compatibility guaranteed with future versions of this\nprofiler, and there is no compatibility with files produced by other profilers.\nIf several files are provided, all the statistics for identical functions will\nbe coalesced, so that an overall view of several processes can be considered in\na single report. If additional files need to be combined with data in an\nexisting Stats object, the add() method can be used.

\n

\nChanged in version 2.5: The stream parameter was added.

\n
\n\n
\n

26.4.4.1. The Stats Class

\n

Stats objects have the following methods:

\n
\n
\nStats.strip_dirs()
\n
This method for the Stats class removes all leading path information\nfrom file names. It is very useful in reducing the size of the printout to fit\nwithin (close to) 80 columns. This method modifies the object, and the stripped\ninformation is lost. After performing a strip operation, the object is\nconsidered to have its entries in a “random” order, as it was just after object\ninitialization and loading. If strip_dirs() causes two function names to\nbe indistinguishable (they are on the same line of the same filename, and have\nthe same function name), then the statistics for these two entries are\naccumulated into a single entry.
\n\n
\n
\nStats.add(filename[, ...])
\n
This method of the Stats class accumulates additional profiling\ninformation into the current profiling object. Its arguments should refer to\nfilenames created by the corresponding version of profile.run() or\ncProfile.run(). Statistics for identically named (re: file, line, name)\nfunctions are automatically accumulated into single function statistics.
\n\n
\n
\nStats.dump_stats(filename)
\n

Save the data loaded into the Stats object to a file named filename.\nThe file is created if it does not exist, and is overwritten if it already\nexists. This is equivalent to the method of the same name on the\nprofile.Profile and cProfile.Profile classes.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nStats.sort_stats(key[, ...])
\n

This method modifies the Stats object by sorting it according to the\nsupplied criteria. The argument is typically a string identifying the basis of\na sort (example: 'time' or 'name').

\n

When more than one key is provided, then additional keys are used as secondary\ncriteria when there is equality in all keys selected before them. For example,\nsort_stats('name', 'file') will sort all the entries according to their\nfunction name, and resolve all ties (identical function names) by sorting by\nfile name.

\n

Abbreviations can be used for any key names, as long as the abbreviation is\nunambiguous. The following are the keys currently defined:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Valid ArgMeaning
'calls'call count
'cumulative'cumulative time
'file'file name
'module'file name
'pcalls'primitive call count
'line'line number
'name'function name
'nfl'name/file/line
'stdname'standard name
'time'internal time
\n

Note that all sorts on statistics are in descending order (placing most time\nconsuming items first), where as name, file, and line number searches are in\nascending order (alphabetical). The subtle distinction between 'nfl' and\n'stdname' is that the standard name is a sort of the name as printed, which\nmeans that the embedded line numbers get compared in an odd way. For example,\nlines 3, 20, and 40 would (if the file names were the same) appear in the string\norder 20, 3 and 40. In contrast, 'nfl' does a numeric compare of the line\nnumbers. In fact, sort_stats('nfl') is the same as sort_stats('name',\n'file', 'line').

\n

For backward-compatibility reasons, the numeric arguments -1, 0, 1,\nand 2 are permitted. They are interpreted as 'stdname', 'calls',\n'time', and 'cumulative' respectively. If this old style format\n(numeric) is used, only one sort key (the numeric key) will be used, and\nadditional arguments will be silently ignored.

\n
\n\n
\n
\nStats.reverse_order()
\n
This method for the Stats class reverses the ordering of the basic list\nwithin the object. Note that by default ascending vs descending order is\nproperly selected based on the sort key of choice.
\n\n
\n
\nStats.print_stats([restriction, ...])
\n

This method for the Stats class prints out a report as described in the\nprofile.run() definition.

\n

The order of the printing is based on the last sort_stats() operation done\non the object (subject to caveats in add() and strip_dirs()).

\n

The arguments provided (if any) can be used to limit the list down to the\nsignificant entries. Initially, the list is taken to be the complete set of\nprofiled functions. Each restriction is either an integer (to select a count of\nlines), or a decimal fraction between 0.0 and 1.0 inclusive (to select a\npercentage of lines), or a regular expression (to pattern match the standard\nname that is printed; as of Python 1.5b1, this uses the Perl-style regular\nexpression syntax defined by the re module). If several restrictions are\nprovided, then they are applied sequentially. For example:

\n
print_stats(.1, 'foo:')\n
\n
\n

would first limit the printing to first 10% of list, and then only print\nfunctions that were part of filename .*foo:. In contrast, the\ncommand:

\n
print_stats('foo:', .1)\n
\n
\n

would limit the list to all functions having file names .*foo:, and\nthen proceed to only print the first 10% of them.

\n
\n\n
\n
\nStats.print_callers([restriction, ...])
\n

This method for the Stats class prints a list of all functions that\ncalled each function in the profiled database. The ordering is identical to\nthat provided by print_stats(), and the definition of the restricting\nargument is also identical. Each caller is reported on its own line. The\nformat differs slightly depending on the profiler that produced the stats:

\n
    \n
  • With profile, a number is shown in parentheses after each caller to\nshow how many times this specific call was made. For convenience, a second\nnon-parenthesized number repeats the cumulative time spent in the function\nat the right.
  • \n
  • With cProfile, each caller is preceded by three numbers: the number of\ntimes this specific call was made, and the total and cumulative times spent in\nthe current function while it was invoked by this specific caller.
  • \n
\n
\n\n
\n
\nStats.print_callees([restriction, ...])
\n
This method for the Stats class prints a list of all function that were\ncalled by the indicated function. Aside from this reversal of direction of\ncalls (re: called vs was called by), the arguments and ordering are identical to\nthe print_callers() method.
\n\n
\n
\n
\n

26.4.5. Limitations

\n

One limitation has to do with accuracy of timing information. There is a\nfundamental problem with deterministic profilers involving accuracy. The most\nobvious restriction is that the underlying “clock” is only ticking at a rate\n(typically) of about .001 seconds. Hence no measurements will be more accurate\nthan the underlying clock. If enough measurements are taken, then the “error”\nwill tend to average out. Unfortunately, removing this first error induces a\nsecond source of error.

\n

The second problem is that it “takes a while” from when an event is dispatched\nuntil the profiler’s call to get the time actually gets the state of the\nclock. Similarly, there is a certain lag when exiting the profiler event\nhandler from the time that the clock’s value was obtained (and then squirreled\naway), until the user’s code is once again executing. As a result, functions\nthat are called many times, or call many functions, will typically accumulate\nthis error. The error that accumulates in this fashion is typically less than\nthe accuracy of the clock (less than one clock tick), but it can accumulate\nand become very significant.

\n

The problem is more important with profile than with the lower-overhead\ncProfile. For this reason, profile provides a means of\ncalibrating itself for a given platform so that this error can be\nprobabilistically (on the average) removed. After the profiler is calibrated, it\nwill be more accurate (in a least square sense), but it will sometimes produce\nnegative numbers (when call counts are exceptionally low, and the gods of\nprobability work against you :-). ) Do not be alarmed by negative numbers in\nthe profile. They should only appear if you have calibrated your profiler,\nand the results are actually better than without calibration.

\n
\n
\n

26.4.6. Calibration

\n

The profiler of the profile module subtracts a constant from each event\nhandling time to compensate for the overhead of calling the time function, and\nsocking away the results. By default, the constant is 0. The following\nprocedure can be used to obtain a better constant for a given platform (see\ndiscussion in section Limitations above).

\n
import profile\npr = profile.Profile()\nfor i in range(5):\n    print pr.calibrate(10000)\n
\n
\n

The method executes the number of Python calls given by the argument, directly\nand again under the profiler, measuring the time for both. It then computes the\nhidden overhead per profiler event, and returns that as a float. For example,\non an 800 MHz Pentium running Windows 2000, and using Python’s time.clock() as\nthe timer, the magical number is about 12.5e-6.

\n

The object of this exercise is to get a fairly consistent result. If your\ncomputer is very fast, or your timer function has poor resolution, you might\nhave to pass 100000, or even 1000000, to get consistent results.

\n

When you have a consistent answer, there are three ways you can use it: [1]

\n
import profile\n\n# 1. Apply computed bias to all Profile instances created hereafter.\nprofile.Profile.bias = your_computed_bias\n\n# 2. Apply computed bias to a specific Profile instance.\npr = profile.Profile()\npr.bias = your_computed_bias\n\n# 3. Specify computed bias in instance constructor.\npr = profile.Profile(bias=your_computed_bias)\n
\n
\n

If you have a choice, you are better off choosing a smaller constant, and then\nyour results will “less often” show up as negative in profile statistics.

\n
\n
\n

26.4.7. Extensions — Deriving Better Profilers

\n

The Profile class of both modules, profile and cProfile,\nwere written so that derived classes could be developed to extend the profiler.\nThe details are not described here, as doing this successfully requires an\nexpert understanding of how the Profile class works internally. Study\nthe source code of the module carefully if you want to pursue this.

\n

If all you want to do is change how current time is determined (for example, to\nforce use of wall-clock time or elapsed process time), pass the timing function\nyou want to the Profile class constructor:

\n
pr = profile.Profile(your_time_func)\n
\n
\n

The resulting profiler will then call your_time_func().

\n
\n
profile.Profile
\n

your_time_func() should return a single number, or a list of numbers whose\nsum is the current time (like what os.times() returns). If the function\nreturns a single time number, or the list of returned numbers has length 2, then\nyou will get an especially fast version of the dispatch routine.

\n

Be warned that you should calibrate the profiler class for the timer function\nthat you choose. For most machines, a timer that returns a lone integer value\nwill provide the best results in terms of low overhead during profiling.\n(os.times() is pretty bad, as it returns a tuple of floating point\nvalues). If you want to substitute a better timer in the cleanest fashion,\nderive a class and hardwire a replacement dispatch method that best handles your\ntimer call, along with the appropriate calibration constant.

\n
\n
cProfile.Profile
\n

your_time_func() should return a single number. If it returns plain\nintegers, you can also invoke the class constructor with a second argument\nspecifying the real duration of one unit of time. For example, if\nyour_integer_time_func() returns times measured in thousands of seconds,\nyou would construct the Profile instance as follows:

\n
pr = profile.Profile(your_integer_time_func, 0.001)\n
\n
\n

As the cProfile.Profile class cannot be calibrated, custom timer\nfunctions should be used with care and should be as fast as possible. For the\nbest results with a custom timer, it might be necessary to hard-code it in the C\nsource of the internal _lsprof module.

\n
\n
\n

Footnotes

\n\n\n\n\n\n
[1]Updated and converted to LaTeX by Guido van Rossum. Further updated by Armin\nRigo to integrate the documentation for the new cProfile module of Python\n2.5.
\n\n\n\n\n\n
[2]Prior to Python 2.2, it was necessary to edit the profiler source code to embed\nthe bias as a literal number. You still can, but that method is no longer\ndescribed, because no longer needed.
\n
\n
", "searchableItems": [ { "name": "cProfile.run", "domId": "_cProfile.run" }, { "name": "cProfile.runctx", "domId": "_cProfile.runctx" }, { "name": "pstats.Stats", "domId": "_pstats.Stats" }, { "name": "pstats.Stats.add", "domId": "_pstats.Stats.add" }, { "name": "pstats.Stats.dump_stats", "domId": "_pstats.Stats.dump_stats" }, { "name": "pstats.Stats.print_callees", "domId": "_pstats.Stats.print_callees" }, { "name": "pstats.Stats.print_callers", "domId": "_pstats.Stats.print_callers" }, { "name": "pstats.Stats.print_stats", "domId": "_pstats.Stats.print_stats" }, { "name": "pstats.Stats.reverse_order", "domId": "_pstats.Stats.reverse_order" }, { "name": "pstats.Stats.sort_stats", "domId": "_pstats.Stats.sort_stats" }, { "name": "pstats.Stats.strip_dirs", "domId": "_pstats.Stats.strip_dirs" } ] }, { "url": "http://docs.python.org/library/hotshot.html", "title": "hotshot", "html": "
\n

26.5. hotshot — High performance logging profiler

\n

\nNew in version 2.2.

\n

This module provides a nicer interface to the _hotshot C module. Hotshot\nis a replacement for the existing profile module. As it’s written mostly\nin C, it should result in a much smaller performance impact than the existing\nprofile module.

\n
\n

Note

\n

The hotshot module focuses on minimizing the overhead while profiling, at\nthe expense of long data post-processing times. For common usage it is\nrecommended to use cProfile instead. hotshot is not maintained and\nmight be removed from the standard library in the future.

\n
\n

\nChanged in version 2.5: The results should be more meaningful than in the past: the timing core\ncontained a critical bug.

\n
\n

Note

\n

The hotshot profiler does not yet work well with threads. It is useful to\nuse an unthreaded script to run the profiler over the code you’re interested in\nmeasuring if at all possible.

\n
\n
\n
\nclass hotshot.Profile(logfile[, lineevents[, linetimings]])
\n
The profiler object. The argument logfile is the name of a log file to use for\nlogged profile data. The argument lineevents specifies whether to generate\nevents for every source line, or just on function call/return. It defaults to\n0 (only log function call/return). The argument linetimings specifies\nwhether to record timing information. It defaults to 1 (store timing\ninformation).
\n\n
\n

26.5.1. Profile Objects

\n

Profile objects have the following methods:

\n
\n
\nProfile.addinfo(key, value)
\n
Add an arbitrary labelled value to the profile output.
\n\n
\n
\nProfile.close()
\n
Close the logfile and terminate the profiler.
\n\n
\n
\nProfile.fileno()
\n
Return the file descriptor of the profiler’s log file.
\n\n
\n
\nProfile.run(cmd)
\n
Profile an exec-compatible string in the script environment. The\nglobals from the __main__ module are used as both the globals and locals\nfor the script.
\n\n
\n
\nProfile.runcall(func, *args, **keywords)
\n
Profile a single call of a callable. Additional positional and keyword arguments\nmay be passed along; the result of the call is returned, and exceptions are\nallowed to propagate cleanly, while ensuring that profiling is disabled on the\nway out.
\n\n
\n
\nProfile.runctx(cmd, globals, locals)
\n
Evaluate an exec-compatible string in a specific environment. The\nstring is compiled before profiling begins.
\n\n
\n
\nProfile.start()
\n
Start the profiler.
\n\n
\n
\nProfile.stop()
\n
Stop the profiler.
\n\n
\n
\n

26.5.2. Using hotshot data

\n

\nNew in version 2.2.

\n

This module loads hotshot profiling data into the standard pstats Stats\nobjects.

\n
\n
\nhotshot.stats.load(filename)
\n
Load hotshot data from filename. Returns an instance of the\npstats.Stats class.
\n\n
\n

See also

\n
\n
Module profile
\n
The profile module’s Stats class
\n
\n
\n
\n
\n

26.5.3. Example Usage

\n

Note that this example runs the Python “benchmark” pystones. It can take some\ntime to run, and will produce large output files.

\n
>>> import hotshot, hotshot.stats, test.pystone\n>>> prof = hotshot.Profile("stones.prof")\n>>> benchtime, stones = prof.runcall(test.pystone.pystones)\n>>> prof.close()\n>>> stats = hotshot.stats.load("stones.prof")\n>>> stats.strip_dirs()\n>>> stats.sort_stats('time', 'calls')\n>>> stats.print_stats(20)\n         850004 function calls in 10.090 CPU seconds\n\n   Ordered by: internal time, call count\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n        1    3.295    3.295   10.090   10.090 pystone.py:79(Proc0)\n   150000    1.315    0.000    1.315    0.000 pystone.py:203(Proc7)\n    50000    1.313    0.000    1.463    0.000 pystone.py:229(Func2)\n .\n .\n .\n
\n
\n
\n
", "searchableItems": [ { "name": "hotshot.Profile", "domId": "hotshot_hotshot.Profile" }, { "name": "hotshot.Profile.addinfo", "domId": "hotshot_hotshot.Profile.addinfo" }, { "name": "hotshot.Profile.close", "domId": "hotshot_hotshot.Profile.close" }, { "name": "hotshot.Profile.fileno", "domId": "hotshot_hotshot.Profile.fileno" }, { "name": "hotshot.Profile.run", "domId": "hotshot_hotshot.Profile.run" }, { "name": "hotshot.Profile.runcall", "domId": "hotshot_hotshot.Profile.runcall" }, { "name": "hotshot.Profile.runctx", "domId": "hotshot_hotshot.Profile.runctx" }, { "name": "hotshot.Profile.start", "domId": "hotshot_hotshot.Profile.start" }, { "name": "hotshot.Profile.stop", "domId": "hotshot_hotshot.Profile.stop" }, { "name": "hotshot.stats.load", "domId": "hotshot_hotshot.stats.load" } ] }, { "url": "http://docs.python.org/library/timeit.html", "title": "timeit", "html": "
\n

26.6. timeit — Measure execution time of small code snippets

\n

\nNew in version 2.3.

\n

Source code: Lib/timeit.py

\n
\n

This module provides a simple way to time small bits of Python code. It has both\ncommand line as well as callable interfaces. It avoids a number of common traps\nfor measuring execution times. See also Tim Peters’ introduction to the\n“Algorithms” chapter in the Python Cookbook, published by O’Reilly.

\n

The module defines the following public class:

\n
\n
\nclass timeit.Timer([stmt='pass'[, setup='pass'[, timer=<timer function>]]])
\n

Class for timing execution speed of small code snippets.

\n

The constructor takes a statement to be timed, an additional statement used for\nsetup, and a timer function. Both statements default to 'pass'; the timer\nfunction is platform-dependent (see the module doc string). stmt and setup\nmay also contain multiple statements separated by ; or newlines, as long as\nthey don’t contain multi-line string literals.

\n

To measure the execution time of the first statement, use the timeit()\nmethod. The repeat() method is a convenience to call timeit()\nmultiple times and return a list of results.

\n

\nChanged in version 2.6: The stmt and setup parameters can now also take objects that are callable\nwithout arguments. This will embed calls to them in a timer function that will\nthen be executed by timeit(). Note that the timing overhead is a little\nlarger in this case because of the extra function calls.

\n
\n\n
\n
\nTimer.print_exc([file=None])
\n

Helper to print a traceback from the timed code.

\n

Typical use:

\n
t = Timer(...)       # outside the try/except\ntry:\n    t.timeit(...)    # or t.repeat(...)\nexcept:\n    t.print_exc()\n
\n
\n

The advantage over the standard traceback is that source lines in the compiled\ntemplate will be displayed. The optional file argument directs where the\ntraceback is sent; it defaults to sys.stderr.

\n
\n\n
\n
\nTimer.repeat([repeat=3[, number=1000000]])
\n

Call timeit() a few times.

\n

This is a convenience function that calls the timeit() repeatedly,\nreturning a list of results. The first argument specifies how many times to\ncall timeit(). The second argument specifies the number argument for\ntimeit().

\n
\n

Note

\n

It’s tempting to calculate mean and standard deviation from the result vector\nand report these. However, this is not very useful. In a typical case, the\nlowest value gives a lower bound for how fast your machine can run the given\ncode snippet; higher values in the result vector are typically not caused by\nvariability in Python’s speed, but by other processes interfering with your\ntiming accuracy. So the min() of the result is probably the only number\nyou should be interested in. After that, you should look at the entire vector\nand apply common sense rather than statistics.

\n
\n
\n\n
\n
\nTimer.timeit([number=1000000])
\n

Time number executions of the main statement. This executes the setup\nstatement once, and then returns the time it takes to execute the main statement\na number of times, measured in seconds as a float. The argument is the number\nof times through the loop, defaulting to one million. The main statement, the\nsetup statement and the timer function to be used are passed to the constructor.

\n
\n

Note

\n

By default, timeit() temporarily turns off garbage collection\nduring the timing. The advantage of this approach is that it makes\nindependent timings more comparable. This disadvantage is that GC may be\nan important component of the performance of the function being measured.\nIf so, GC can be re-enabled as the first statement in the setup string.\nFor example:

\n
timeit.Timer('for i in xrange(10): oct(i)', 'gc.enable()').timeit()\n
\n
\n
\n
\n\n

Starting with version 2.6, the module also defines two convenience functions:

\n
\n
\ntimeit.repeat(stmt[, setup[, timer[, repeat=3[, number=1000000]]]])
\n

Create a Timer instance with the given statement, setup code and timer\nfunction and run its repeat() method with the given repeat count and\nnumber executions.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ntimeit.timeit(stmt[, setup[, timer[, number=1000000]]])
\n

Create a Timer instance with the given statement, setup code and timer\nfunction and run its timeit() method with number executions.

\n

\nNew in version 2.6.

\n
\n\n
\n

26.6.1. Command Line Interface

\n

When called as a program from the command line, the following form is used:

\n
python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
\n
\n

Where the following options are understood:

\n
\n
\n-n N, --number=N
\n
how many times to execute ‘statement’
\n\n
\n
\n-r N, --repeat=N
\n
how many times to repeat the timer (default 3)
\n\n
\n
\n-s S, --setup=S
\n
statement to be executed once initially (default pass)
\n\n
\n
\n-t, --time
\n
use time.time() (default on all platforms but Windows)
\n\n
\n
\n-c, --clock
\n
use time.clock() (default on Windows)
\n\n
\n
\n-v, --verbose
\n
print raw timing results; repeat for more digits precision
\n\n
\n
\n-h, --help
\n
print a short usage message and exit
\n\n

A multi-line statement may be given by specifying each line as a separate\nstatement argument; indented lines are possible by enclosing an argument in\nquotes and using leading spaces. Multiple -s options are treated\nsimilarly.

\n

If -n is not given, a suitable number of loops is calculated by trying\nsuccessive powers of 10 until the total time is at least 0.2 seconds.

\n

The default timer function is platform dependent. On Windows,\ntime.clock() has microsecond granularity but time.time()‘s\ngranularity is 1/60th of a second; on Unix, time.clock() has 1/100th of a\nsecond granularity and time.time() is much more precise. On either\nplatform, the default timer functions measure wall clock time, not the CPU time.\nThis means that other processes running on the same computer may interfere with\nthe timing. The best thing to do when accurate timing is necessary is to repeat\nthe timing a few times and use the best time. The -r option is good\nfor this; the default of 3 repetitions is probably enough in most cases. On\nUnix, you can use time.clock() to measure CPU time.

\n
\n

Note

\n

There is a certain baseline overhead associated with executing a pass statement.\nThe code here doesn’t try to hide it, but you should be aware of it. The\nbaseline overhead can be measured by invoking the program without arguments.

\n
\n

The baseline overhead differs between Python versions! Also, to fairly compare\nolder Python versions to Python 2.3, you may want to use Python’s -O\noption for the older versions to avoid timing SET_LINENO instructions.

\n
\n
\n

26.6.2. Examples

\n

Here are two example sessions (one using the command line, one using the module\ninterface) that compare the cost of using hasattr() vs.\ntry/except to test for missing and present object\nattributes.

\n
$ python -m timeit 'try:' '  str.__nonzero__' 'except AttributeError:' '  pass'\n100000 loops, best of 3: 15.7 usec per loop\n$ python -m timeit 'if hasattr(str, \"__nonzero__\"): pass'\n100000 loops, best of 3: 4.26 usec per loop\n$ python -m timeit 'try:' '  int.__nonzero__' 'except AttributeError:' '  pass'\n1000000 loops, best of 3: 1.43 usec per loop\n$ python -m timeit 'if hasattr(int, \"__nonzero__\"): pass'\n100000 loops, best of 3: 2.23 usec per loop
\n
\n
>>> import timeit\n>>> s = """\\\n... try:\n...     str.__nonzero__\n... except AttributeError:\n...     pass\n... """\n>>> t = timeit.Timer(stmt=s)\n>>> print "%.2f usec/pass"  (1000000 * t.timeit(number=100000)/100000)\n17.09 usec/pass\n>>> s = """\\\n... if hasattr(str, '__nonzero__'): pass\n... """\n>>> t = timeit.Timer(stmt=s)\n>>> print "%.2f usec/pass"  (1000000 * t.timeit(number=100000)/100000)\n4.85 usec/pass\n>>> s = """\\\n... try:\n...     int.__nonzero__\n... except AttributeError:\n...     pass\n... """\n>>> t = timeit.Timer(stmt=s)\n>>> print "%.2f usec/pass"  (1000000 * t.timeit(number=100000)/100000)\n1.97 usec/pass\n>>> s = """\\\n... if hasattr(int, '__nonzero__'): pass\n... """\n>>> t = timeit.Timer(stmt=s)\n>>> print "%.2f usec/pass"  (1000000 * t.timeit(number=100000)/100000)\n3.15 usec/pass\n
\n
\n

To give the timeit module access to functions you define, you can pass a\nsetup parameter which contains an import statement:

\n
def test():\n    """Stupid test function"""\n    L = []\n    for i in range(100):\n        L.append(i)\n\nif __name__ == '__main__':\n    from timeit import Timer\n    t = Timer("test()", "from __main__ import test")\n    print t.timeit()\n
\n
\n
\n
", "searchableItems": [ { "name": "timeit.repeat", "domId": "timeit_timeit.repeat" }, { "name": "timeit.timeit", "domId": "timeit_timeit.timeit" }, { "name": "timeit.Timer", "domId": "timeit_timeit.Timer" }, { "name": "timeit.Timer.print_exc", "domId": "timeit_timeit.Timer.print_exc" }, { "name": "timeit.Timer.repeat", "domId": "timeit_timeit.Timer.repeat" }, { "name": "timeit.Timer.timeit", "domId": "timeit_timeit.Timer.timeit" } ] }, { "url": "http://docs.python.org/library/__builtin__.html", "title": "__builtin__", "html": "
\n

27.3. __builtin__ — Built-in objects

\n

This module provides direct access to all ‘built-in’ identifiers of Python; for\nexample, __builtin__.open is the full name for the built-in function\nopen(). See Built-in Functions and Built-in Constants for\ndocumentation.

\n

This module is not normally accessed explicitly by most applications, but can be\nuseful in modules that provide objects with the same name as a built-in value,\nbut in which the built-in of that name is also needed. For example, in a module\nthat wants to implement an open() function that wraps the built-in\nopen(), this module can be used directly:

\n
import __builtin__\n\ndef open(path):\n    f = __builtin__.open(path, 'r')\n    return UpperCaser(f)\n\nclass UpperCaser:\n    '''Wrapper around a file that converts output to upper-case.'''\n\n    def __init__(self, f):\n        self._f = f\n\n    def read(self, count=-1):\n        return self._f.read(count).upper()\n\n    # ...\n
\n
\n
\n

CPython implementation detail: Most modules have the name __builtins__ (note the 's') made available\nas part of their globals. The value of __builtins__ is normally either\nthis module or the value of this modules’s __dict__ attribute. Since\nthis is an implementation detail, it may not be used by alternate\nimplementations of Python.

\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/future_builtins.html", "title": "future_builtins", "html": "
\n

27.4. future_builtins — Python 3 builtins

\n

\nNew in version 2.6.

\n

This module provides functions that exist in 2.x, but have different behavior in\nPython 3, so they cannot be put into the 2.x builtins namespace.

\n

Instead, if you want to write code compatible with Python 3 builtins, import\nthem from this module, like this:

\n
from future_builtins import map, filter\n\n... code using Python 3-style map and filter ...\n
\n
\n

The 2to3 tool that ports Python 2 code to Python 3 will recognize\nthis usage and leave the new builtins alone.

\n
\n

Note

\n

The Python 3 print() function is already in the builtins, but cannot be\naccessed from Python 2 code unless you use the appropriate future statement:

\n
from __future__ import print_function\n
\n
\n
\n

Available builtins are:

\n
\n
\nfuture_builtins.ascii(object)
\n
Returns the same as repr(). In Python 3, repr() will return\nprintable Unicode characters unescaped, while ascii() will always\nbackslash-escape them. Using future_builtins.ascii() instead of\nrepr() in 2.6 code makes it clear that you need a pure ASCII return\nvalue.
\n\n
\n
\nfuture_builtins.filter(function, iterable)
\n
Works like itertools.ifilter().
\n\n
\n
\nfuture_builtins.hex(object)
\n
Works like the built-in hex(), but instead of __hex__() it will\nuse the __index__() method on its argument to get an integer that is\nthen converted to hexadecimal.
\n\n
\n
\nfuture_builtins.map(function, iterable, ...)
\n
Works like itertools.imap().
\n\n
\n
\nfuture_builtins.oct(object)
\n
Works like the built-in oct(), but instead of __oct__() it will\nuse the __index__() method on its argument to get an integer that is\nthen converted to octal.
\n\n
\n
\nfuture_builtins.zip(*iterables)
\n
Works like itertools.izip().
\n\n
", "searchableItems": [ { "name": "future_builtins.ascii", "domId": "future_builtins_future_builtins.ascii" }, { "name": "future_builtins.filter", "domId": "future_builtins_future_builtins.filter" }, { "name": "future_builtins.hex", "domId": "future_builtins_future_builtins.hex" }, { "name": "future_builtins.map", "domId": "future_builtins_future_builtins.map" }, { "name": "future_builtins.oct", "domId": "future_builtins_future_builtins.oct" }, { "name": "future_builtins.zip", "domId": "future_builtins_future_builtins.zip" } ] }, { "url": "http://docs.python.org/library/sysconfig.html", "title": "sysconfig", "html": "
\n

27.2. sysconfig — Provide access to Python’s configuration information

\n

\nNew in version 2.7.

\n

Source code: Lib/sysconfig.py

\n
\n

The sysconfig module provides access to Python’s configuration\ninformation like the list of installation paths and the configuration variables\nrelevant for the current platform.

\n
\n

27.2.1. Configuration variables

\n

A Python distribution contains a Makefile and a pyconfig.h\nheader file that are necessary to build both the Python binary itself and\nthird-party C extensions compiled using distutils.

\n

sysconfig puts all variables found in these files in a dictionary that\ncan be accessed using get_config_vars() or get_config_var().

\n

Notice that on Windows, it’s a much smaller set.

\n
\n
\nsysconfig.get_config_vars(*args)
\n

With no arguments, return a dictionary of all configuration variables\nrelevant for the current platform.

\n

With arguments, return a list of values that result from looking up each\nargument in the configuration variable dictionary.

\n

For each argument, if the value is not found, return None.

\n
\n\n
\n
\nsysconfig.get_config_var(name)
\n

Return the value of a single variable name. Equivalent to\nget_config_vars().get(name).

\n

If name is not found, return None.

\n
\n\n

Example of usage:

\n
>>> import sysconfig\n>>> sysconfig.get_config_var('Py_ENABLE_SHARED')\n0\n>>> sysconfig.get_config_var('LIBDIR')\n'/usr/local/lib'\n>>> sysconfig.get_config_vars('AR', 'CXX')\n['ar', 'g++']\n
\n
\n
\n
\n

27.2.2. Installation paths

\n

Python uses an installation scheme that differs depending on the platform and on\nthe installation options. These schemes are stored in sysconfig under\nunique identifiers based on the value returned by os.name.

\n

Every new component that is installed using distutils or a\nDistutils-based system will follow the same scheme to copy its file in the right\nplaces.

\n

Python currently supports seven schemes:

\n\n

Each scheme is itself composed of a series of paths and each path has a unique\nidentifier. Python currently uses eight paths:

\n\n

sysconfig provides some functions to determine these paths.

\n
\n
\nsysconfig.get_scheme_names()
\n
Return a tuple containing all schemes currently supported in\nsysconfig.
\n\n
\n
\nsysconfig.get_path_names()
\n
Return a tuple containing all path names currently supported in\nsysconfig.
\n\n
\n
\nsysconfig.get_path(name[, scheme[, vars[, expand]]])
\n

Return an installation path corresponding to the path name, from the\ninstall scheme named scheme.

\n

name has to be a value from the list returned by get_path_names().

\n

sysconfig stores installation paths corresponding to each path name,\nfor each platform, with variables to be expanded. For instance the stdlib\npath for the nt scheme is: {base}/Lib.

\n

get_path() will use the variables returned by get_config_vars()\nto expand the path. All variables have default values for each platform so\none may call this function and get the default value.

\n

If scheme is provided, it must be a value from the list returned by\nget_path_names(). Otherwise, the default scheme for the current\nplatform is used.

\n

If vars is provided, it must be a dictionary of variables that will update\nthe dictionary return by get_config_vars().

\n

If expand is set to False, the path will not be expanded using the\nvariables.

\n

If name is not found, return None.

\n
\n\n
\n
\nsysconfig.get_paths([scheme[, vars[, expand]]])
\n

Return a dictionary containing all installation paths corresponding to an\ninstallation scheme. See get_path() for more information.

\n

If scheme is not provided, will use the default scheme for the current\nplatform.

\n

If vars is provided, it must be a dictionary of variables that will\nupdate the dictionary used to expand the paths.

\n

If expand is set to False, the paths will not be expanded.

\n

If scheme is not an existing scheme, get_paths() will raise a\nKeyError.

\n
\n\n
\n
\n

27.2.3. Other functions

\n
\n
\nsysconfig.get_python_version()
\n
Return the MAJOR.MINOR Python version number as a string. Similar to\nsys.version[:3].
\n\n
\n
\nsysconfig.get_platform()
\n

Return a string that identifies the current platform.

\n

This is used mainly to distinguish platform-specific build directories and\nplatform-specific built distributions. Typically includes the OS name and\nversion and the architecture (as supplied by os.uname()), although the\nexact information included depends on the OS; e.g. for IRIX the architecture\nisn’t particularly important (IRIX only runs on SGI hardware), but for Linux\nthe kernel version isn’t particularly important.

\n

Examples of returned values:

\n
    \n
  • linux-i586
  • \n
  • linux-alpha (?)
  • \n
  • solaris-2.6-sun4u
  • \n
  • irix-5.3
  • \n
  • irix64-6.2
  • \n
\n

Windows will return one of:

\n
    \n
  • win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
  • \n
  • win-ia64 (64bit Windows on Itanium)
  • \n
  • win32 (all others - specifically, sys.platform is returned)
  • \n
\n

Mac OS X can return:

\n
    \n
  • macosx-10.6-ppc
  • \n
  • macosx-10.4-ppc64
  • \n
  • macosx-10.3-i386
  • \n
  • macosx-10.4-fat
  • \n
\n

For other non-POSIX platforms, currently just returns sys.platform.

\n
\n\n
\n
\nsysconfig.is_python_build()
\n
Return True if the current Python installation was built from source.
\n\n
\n
\nsysconfig.parse_config_h(fp[, vars])
\n

Parse a config.h-style file.

\n

fp is a file-like object pointing to the config.h-like file.

\n

A dictionary containing name/value pairs is returned. If an optional\ndictionary is passed in as the second argument, it is used instead of a new\ndictionary, and updated with the values read in the file.

\n
\n\n
\n
\nsysconfig.get_config_h_filename()
\n
Return the path of pyconfig.h.
\n\n
\n
", "searchableItems": [ { "name": "sysconfig.get_config_h_filename", "domId": "sysconfig_sysconfig.get_config_h_filename" }, { "name": "sysconfig.get_config_var", "domId": "sysconfig_sysconfig.get_config_var" }, { "name": "sysconfig.get_config_vars", "domId": "sysconfig_sysconfig.get_config_vars" }, { "name": "sysconfig.get_path", "domId": "sysconfig_sysconfig.get_path" }, { "name": "sysconfig.get_path_names", "domId": "sysconfig_sysconfig.get_path_names" }, { "name": "sysconfig.get_paths", "domId": "sysconfig_sysconfig.get_paths" }, { "name": "sysconfig.get_platform", "domId": "sysconfig_sysconfig.get_platform" }, { "name": "sysconfig.get_python_version", "domId": "sysconfig_sysconfig.get_python_version" }, { "name": "sysconfig.get_scheme_names", "domId": "sysconfig_sysconfig.get_scheme_names" }, { "name": "sysconfig.is_python_build", "domId": "sysconfig_sysconfig.is_python_build" }, { "name": "sysconfig.parse_config_h", "domId": "sysconfig_sysconfig.parse_config_h" } ] }, { "url": "http://docs.python.org/library/sys.html", "title": "sys", "html": "
\n

27.1. sys — System-specific parameters and functions

\n

This module provides access to some variables used or maintained by the\ninterpreter and to functions that interact strongly with the interpreter. It is\nalways available.

\n
\n
\nsys.argv
\n

The list of command line arguments passed to a Python script. argv[0] is the\nscript name (it is operating system dependent whether this is a full pathname or\nnot). If the command was executed using the -c command line option to\nthe interpreter, argv[0] is set to the string '-c'. If no script name\nwas passed to the Python interpreter, argv[0] is the empty string.

\n

To loop over the standard input, or the list of files given on the\ncommand line, see the fileinput module.

\n
\n\n
\n
\nsys.byteorder
\n

An indicator of the native byte order. This will have the value 'big' on\nbig-endian (most-significant byte first) platforms, and 'little' on\nlittle-endian (least-significant byte first) platforms.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nsys.builtin_module_names
\n
A tuple of strings giving the names of all modules that are compiled into this\nPython interpreter. (This information is not available in any other way —\nmodules.keys() only lists the imported modules.)
\n\n
\n
\nsys.call_tracing(func, args)
\n
Call func(*args), while tracing is enabled. The tracing state is saved,\nand restored afterwards. This is intended to be called from a debugger from\na checkpoint, to recursively debug some other code.
\n\n
\n
\nsys.copyright
\n
A string containing the copyright pertaining to the Python interpreter.
\n\n
\n
\nsys._clear_type_cache()
\n

Clear the internal type cache. The type cache is used to speed up attribute\nand method lookups. Use the function only to drop unnecessary references\nduring reference leak debugging.

\n

This function should be used for internal and specialized purposes only.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys._current_frames()
\n

Return a dictionary mapping each thread’s identifier to the topmost stack frame\ncurrently active in that thread at the time the function is called. Note that\nfunctions in the traceback module can build the call stack given such a\nframe.

\n

This is most useful for debugging deadlock: this function does not require the\ndeadlocked threads’ cooperation, and such threads’ call stacks are frozen for as\nlong as they remain deadlocked. The frame returned for a non-deadlocked thread\nmay bear no relationship to that thread’s current activity by the time calling\ncode examines the frame.

\n

This function should be used for internal and specialized purposes only.

\n

\nNew in version 2.5.

\n
\n\n
\n
\nsys.dllhandle
\n
Integer specifying the handle of the Python DLL. Availability: Windows.
\n\n
\n
\nsys.displayhook(value)
\n

If value is not None, this function prints it to sys.stdout, and saves\nit in __builtin__._.

\n

sys.displayhook is called on the result of evaluating an expression\nentered in an interactive Python session. The display of these values can be\ncustomized by assigning another one-argument function to sys.displayhook.

\n
\n\n
\n
\nsys.dont_write_bytecode
\n

If this is true, Python won’t try to write .pyc or .pyo files on the\nimport of source modules. This value is initially set to True or\nFalse depending on the -B command line option and the\nPYTHONDONTWRITEBYTECODE environment variable, but you can set it\nyourself to control bytecode file generation.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys.excepthook(type, value, traceback)
\n

This function prints out a given traceback and exception to sys.stderr.

\n

When an exception is raised and uncaught, the interpreter calls\nsys.excepthook with three arguments, the exception class, exception\ninstance, and a traceback object. In an interactive session this happens just\nbefore control is returned to the prompt; in a Python program this happens just\nbefore the program exits. The handling of such top-level exceptions can be\ncustomized by assigning another three-argument function to sys.excepthook.

\n
\n\n
\n
\nsys.__displayhook__
\n
\nsys.__excepthook__
\n
These objects contain the original values of displayhook and excepthook\nat the start of the program. They are saved so that displayhook and\nexcepthook can be restored in case they happen to get replaced with broken\nobjects.
\n\n
\n
\nsys.exc_info()
\n

This function returns a tuple of three values that give information about the\nexception that is currently being handled. The information returned is specific\nboth to the current thread and to the current stack frame. If the current stack\nframe is not handling an exception, the information is taken from the calling\nstack frame, or its caller, and so on until a stack frame is found that is\nhandling an exception. Here, “handling an exception” is defined as “executing\nor having executed an except clause.” For any stack frame, only information\nabout the most recently handled exception is accessible.

\n

If no exception is being handled anywhere on the stack, a tuple containing three\nNone values is returned. Otherwise, the values returned are (type, value,\ntraceback). Their meaning is: type gets the exception type of the exception\nbeing handled (a class object); value gets the exception parameter (its\nassociated value or the second argument to raise, which is\nalways a class instance if the exception type is a class object); traceback\ngets a traceback object (see the Reference Manual) which encapsulates the call\nstack at the point where the exception originally occurred.

\n

If exc_clear() is called, this function will return three None values\nuntil either another exception is raised in the current thread or the execution\nstack returns to a frame where another exception is being handled.

\n
\n

Warning

\n

Assigning the traceback return value to a local variable in a function that is\nhandling an exception will cause a circular reference. This will prevent\nanything referenced by a local variable in the same function or by the traceback\nfrom being garbage collected. Since most functions don’t need access to the\ntraceback, the best solution is to use something like exctype, value =\nsys.exc_info()[:2] to extract only the exception type and value. If you do\nneed the traceback, make sure to delete it after use (best done with a\ntry ... finally statement) or to call exc_info() in\na function that does not itself handle an exception.

\n
\n
\n

Note

\n

Beginning with Python 2.2, such cycles are automatically reclaimed when garbage\ncollection is enabled and they become unreachable, but it remains more efficient\nto avoid creating cycles.

\n
\n
\n\n
\n
\nsys.exc_clear()
\n

This function clears all information relating to the current or last exception\nthat occurred in the current thread. After calling this function,\nexc_info() will return three None values until another exception is\nraised in the current thread or the execution stack returns to a frame where\nanother exception is being handled.

\n

This function is only needed in only a few obscure situations. These include\nlogging and error handling systems that report information on the last or\ncurrent exception. This function can also be used to try to free resources and\ntrigger object finalization, though no guarantee is made as to what objects will\nbe freed, if any.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsys.exc_type
\n
\nsys.exc_value
\n
\nsys.exc_traceback
\n

\nDeprecated since version 1.5: Use exc_info() instead.

\n

Since they are global variables, they are not specific to the current thread, so\ntheir use is not safe in a multi-threaded program. When no exception is being\nhandled, exc_type is set to None and the other two are undefined.

\n
\n\n
\n
\nsys.exec_prefix
\n
A string giving the site-specific directory prefix where the platform-dependent\nPython files are installed; by default, this is also '/usr/local'. This can\nbe set at build time with the --exec-prefix argument to the\nconfigure script. Specifically, all configuration files (e.g. the\npyconfig.h header file) are installed in the directory\nexec_prefix/lib/pythonX.Y/config', and shared library modules are\ninstalled in :file:`exec_prefix/lib/pythonX.Y/lib-dynload, where X.Y\nis the version number of Python, for example 2.7.
\n\n
\n
\nsys.executable
\n
A string giving the name of the executable binary for the Python interpreter, on\nsystems where this makes sense.
\n\n
\n
\nsys.exit([arg])
\n

Exit from Python. This is implemented by raising the SystemExit\nexception, so cleanup actions specified by finally clauses of try\nstatements are honored, and it is possible to intercept the exit attempt at\nan outer level.

\n

The optional argument arg can be an integer giving the exit status\n(defaulting to zero), or another type of object. If it is an integer, zero\nis considered “successful termination” and any nonzero value is considered\n“abnormal termination” by shells and the like. Most systems require it to be\nin the range 0-127, and produce undefined results otherwise. Some systems\nhave a convention for assigning specific meanings to specific exit codes, but\nthese are generally underdeveloped; Unix programs generally use 2 for command\nline syntax errors and 1 for all other kind of errors. If another type of\nobject is passed, None is equivalent to passing zero, and any other\nobject is printed to stderr and results in an exit code of 1. In\nparticular, sys.exit("some error message") is a quick way to exit a\nprogram when an error occurs.

\n

Since exit() ultimately “only” raises an exception, it will only exit\nthe process when called from the main thread, and the exception is not\nintercepted.

\n
\n\n
\n
\nsys.exitfunc
\n

This value is not actually defined by the module, but can be set by the user (or\nby a program) to specify a clean-up action at program exit. When set, it should\nbe a parameterless function. This function will be called when the interpreter\nexits. Only one function may be installed in this way; to allow multiple\nfunctions which will be called at termination, use the atexit module.

\n
\n

Note

\n

The exit function is not called when the program is killed by a signal, when a\nPython fatal internal error is detected, or when os._exit() is called.

\n
\n

\nDeprecated since version 2.4: Use atexit instead.

\n
\n\n
\n
\nsys.flags
\n

The struct sequence flags exposes the status of command line flags. The\nattributes are read only.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
attributeflag
debug-d
py3k_warning-3
division_warning-Q
division_new-Qnew
inspect-i
interactive-i
optimize-O or -OO
dont_write_bytecode-B
no_user_site-s
no_site-S
ignore_environment-E
tabcheck-t or -tt
verbose-v
unicode-U
bytes_warning-b
\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys.float_info
\n

A structseq holding information about the float type. It contains low level\ninformation about the precision and internal representation. The values\ncorrespond to the various floating-point constants defined in the standard\nheader file float.h for the ‘C’ programming language; see section\n5.2.4.2.2 of the 1999 ISO/IEC C standard [C99], ‘Characteristics of\nfloating types’, for details.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
attributefloat.h macroexplanation
epsilonDBL_EPSILONdifference between 1 and the least value greater\nthan 1 that is representable as a float
digDBL_DIGmaximum number of decimal digits that can be\nfaithfully represented in a float; see below
mant_digDBL_MANT_DIGfloat precision: the number of base-radix\ndigits in the significand of a float
maxDBL_MAXmaximum representable finite float
max_expDBL_MAX_EXPmaximum integer e such that radix**(e-1) is\na representable finite float
max_10_expDBL_MAX_10_EXPmaximum integer e such that 10**e is in the\nrange of representable finite floats
minDBL_MINminimum positive normalized float
min_expDBL_MIN_EXPminimum integer e such that radix**(e-1) is\na normalized float
min_10_expDBL_MIN_10_EXPminimum integer e such that 10**e is a\nnormalized float
radixFLT_RADIXradix of exponent representation
roundsFLT_ROUNDSinteger constant representing the rounding mode\nused for arithmetic operations. This reflects\nthe value of the system FLT_ROUNDS macro at\ninterpreter startup time. See section 5.2.4.2.2\nof the C99 standard for an explanation of the\npossible values and their meanings.
\n

The attribute sys.float_info.dig needs further explanation. If\ns is any string representing a decimal number with at most\nsys.float_info.dig significant digits, then converting s to a\nfloat and back again will recover a string representing the same decimal\nvalue:

\n
>>> import sys\n>>> sys.float_info.dig\n15\n>>> s = '3.14159265358979'    # decimal string with 15 significant digits\n>>> format(float(s), '.15g')  # convert to float and back -> same value\n'3.14159265358979'\n
\n
\n

But for strings with more than sys.float_info.dig significant digits,\nthis isn’t always true:

\n
>>> s = '9876543211234567'    # 16 significant digits is too many!\n>>> format(float(s), '.16g')  # conversion changes value\n'9876543211234568'\n
\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys.float_repr_style
\n

A string indicating how the repr() function behaves for\nfloats. If the string has value 'short' then for a finite\nfloat x, repr(x) aims to produce a short string with the\nproperty that float(repr(x)) == x. This is the usual behaviour\nin Python 2.7 and later. Otherwise, float_repr_style has value\n'legacy' and repr(x) behaves in the same way as it did in\nversions of Python prior to 2.7.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nsys.getcheckinterval()
\n

Return the interpreter’s “check interval”; see setcheckinterval().

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsys.getdefaultencoding()
\n

Return the name of the current default string encoding used by the Unicode\nimplementation.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nsys.getdlopenflags()
\n

Return the current value of the flags that are used for dlopen() calls.\nThe flag constants are defined in the dl and DLFCN modules.\nAvailability: Unix.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nsys.getfilesystemencoding()
\n

Return the name of the encoding used to convert Unicode filenames into system\nfile names, or None if the system default encoding is used. The result value\ndepends on the operating system:

\n
    \n
  • On Mac OS X, the encoding is 'utf-8'.
  • \n
  • On Unix, the encoding is the user’s preference according to the result of\nnl_langinfo(CODESET), or None if the nl_langinfo(CODESET)\nfailed.
  • \n
  • On Windows NT+, file names are Unicode natively, so no conversion is\nperformed. getfilesystemencoding() still returns 'mbcs', as\nthis is the encoding that applications should use when they explicitly\nwant to convert Unicode strings to byte strings that are equivalent when\nused as file names.
  • \n
  • On Windows 9x, the encoding is 'mbcs'.
  • \n
\n

\nNew in version 2.3.

\n
\n\n
\n
\nsys.getrefcount(object)
\n
Return the reference count of the object. The count returned is generally one\nhigher than you might expect, because it includes the (temporary) reference as\nan argument to getrefcount().
\n\n
\n
\nsys.getrecursionlimit()
\n
Return the current value of the recursion limit, the maximum depth of the Python\ninterpreter stack. This limit prevents infinite recursion from causing an\noverflow of the C stack and crashing Python. It can be set by\nsetrecursionlimit().
\n\n
\n
\nsys.getsizeof(object[, default])
\n

Return the size of an object in bytes. The object can be any type of\nobject. All built-in objects will return correct results, but this\ndoes not have to hold true for third-party extensions as it is implementation\nspecific.

\n

If given, default will be returned if the object does not provide means to\nretrieve the size. Otherwise a TypeError will be raised.

\n

getsizeof() calls the object’s __sizeof__ method and adds an\nadditional garbage collector overhead if the object is managed by the garbage\ncollector.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys._getframe([depth])
\n

Return a frame object from the call stack. If optional integer depth is\ngiven, return the frame object that many calls below the top of the stack. If\nthat is deeper than the call stack, ValueError is raised. The default\nfor depth is zero, returning the frame at the top of the call stack.

\n
\n

CPython implementation detail: This function should be used for internal and specialized purposes only.\nIt is not guaranteed to exist in all implementations of Python.

\n
\n
\n\n
\n
\nsys.getprofile()
\n

Get the profiler function as set by setprofile().

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys.gettrace()
\n

Get the trace function as set by settrace().

\n
\n

CPython implementation detail: The gettrace() function is intended only for implementing debuggers,\nprofilers, coverage tools and the like. Its behavior is part of the\nimplementation platform, rather than part of the language definition, and\nthus may not be available in all Python implementations.

\n
\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys.getwindowsversion()
\n

Return a named tuple describing the Windows version\ncurrently running. The named elements are major, minor,\nbuild, platform, service_pack, service_pack_minor,\nservice_pack_major, suite_mask, and product_type.\nservice_pack contains a string while all other values are\nintegers. The components can also be accessed by name, so\nsys.getwindowsversion()[0] is equivalent to\nsys.getwindowsversion().major. For compatibility with prior\nversions, only the first 5 elements are retrievable by indexing.

\n

platform may be one of the following values:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantPlatform
0 (VER_PLATFORM_WIN32s)Win32s on Windows 3.1
1 (VER_PLATFORM_WIN32_WINDOWS)Windows 95/98/ME
2 (VER_PLATFORM_WIN32_NT)Windows NT/2000/XP/x64
3 (VER_PLATFORM_WIN32_CE)Windows CE
\n

product_type may be one of the following values:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ConstantMeaning
1 (VER_NT_WORKSTATION)The system is a workstation.
2 (VER_NT_DOMAIN_CONTROLLER)The system is a domain\ncontroller.
3 (VER_NT_SERVER)The system is a server, but not\na domain controller.
\n

This function wraps the Win32 GetVersionEx() function; see the\nMicrosoft documentation on OSVERSIONINFOEX() for more information\nabout these fields.

\n

Availability: Windows.

\n

\nNew in version 2.3.

\n

\nChanged in version 2.7: Changed to a named tuple and added service_pack_minor,\nservice_pack_major, suite_mask, and product_type.

\n
\n\n
\n
\nsys.hexversion
\n

The version number encoded as a single integer. This is guaranteed to increase\nwith each version, including proper support for non-production releases. For\nexample, to test that the Python interpreter is at least version 1.5.2, use:

\n
if sys.hexversion >= 0x010502F0:\n    # use some advanced feature\n    ...\nelse:\n    # use an alternative implementation or warn the user\n    ...\n
\n
\n

This is called hexversion since it only really looks meaningful when viewed\nas the result of passing it to the built-in hex() function. The\nversion_info value may be used for a more human-friendly encoding of the\nsame information.

\n

The hexversion is a 32-bit number with the following layout:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Bits (big endian order)Meaning
1-8PY_MAJOR_VERSION (the 2 in\n2.1.0a3)
9-16PY_MINOR_VERSION (the 1 in\n2.1.0a3)
17-24PY_MICRO_VERSION (the 0 in\n2.1.0a3)
25-28PY_RELEASE_LEVEL (0xA for alpha,\n0xB for beta, 0xC for release\ncandidate and 0xF for final)
29-32PY_RELEASE_SERIAL (the 3 in\n2.1.0a3, zero for final releases)
\n

Thus 2.1.0a3 is hexversion 0x020100a3.

\n

\nNew in version 1.5.2.

\n
\n\n
\n
\nsys.long_info
\n

A struct sequence that holds information about Python’s\ninternal representation of integers. The attributes are read only.

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
AttributeExplanation
bits_per_digitnumber of bits held in each digit. Python\nintegers are stored internally in base\n2**long_info.bits_per_digit
sizeof_digitsize in bytes of the C type used to\nrepresent a digit
\n

\nNew in version 2.7.

\n
\n\n
\n
\nsys.last_type
\n
\nsys.last_value
\n
\nsys.last_traceback
\n

These three variables are not always defined; they are set when an exception is\nnot handled and the interpreter prints an error message and a stack traceback.\nTheir intended use is to allow an interactive user to import a debugger module\nand engage in post-mortem debugging without having to re-execute the command\nthat caused the error. (Typical use is import pdb; pdb.pm() to enter the\npost-mortem debugger; see chapter pdb — The Python Debugger for\nmore information.)

\n

The meaning of the variables is the same as that of the return values from\nexc_info() above. (Since there is only one interactive thread,\nthread-safety is not a concern for these variables, unlike for exc_type\netc.)

\n
\n\n
\n
\nsys.maxint
\n
The largest positive integer supported by Python’s regular integer type. This\nis at least 2**31-1. The largest negative integer is -maxint-1 — the\nasymmetry results from the use of 2’s complement binary arithmetic.
\n\n
\n
\nsys.maxsize
\n
The largest positive integer supported by the platform’s Py_ssize_t type,\nand thus the maximum size lists, strings, dicts, and many other containers\ncan have.
\n\n
\n
\nsys.maxunicode
\n
An integer giving the largest supported code point for a Unicode character. The\nvalue of this depends on the configuration option that specifies whether Unicode\ncharacters are stored as UCS-2 or UCS-4.
\n\n
\n
\nsys.meta_path
\n

A list of finder objects that have their find_module()\nmethods called to see if one of the objects can find the module to be\nimported. The find_module() method is called at least with the\nabsolute name of the module being imported. If the module to be imported is\ncontained in package then the parent package’s __path__ attribute\nis passed in as a second argument. The method returns None if\nthe module cannot be found, else returns a loader.

\n

sys.meta_path is searched before any implicit default finders or\nsys.path.

\n

See PEP 302 for the original specification.

\n
\n\n
\n
\nsys.modules
\n

This is a dictionary that maps module names to modules which have already been\nloaded. This can be manipulated to force reloading of modules and other tricks.\nNote that removing a module from this dictionary is not the same as calling\nreload() on the corresponding module object.

\n
\n\n
\n
\nsys.path
\n

A list of strings that specifies the search path for modules. Initialized from\nthe environment variable PYTHONPATH, plus an installation-dependent\ndefault.

\n

As initialized upon program startup, the first item of this list, path[0],\nis the directory containing the script that was used to invoke the Python\ninterpreter. If the script directory is not available (e.g. if the interpreter\nis invoked interactively or if the script is read from standard input),\npath[0] is the empty string, which directs Python to search modules in the\ncurrent directory first. Notice that the script directory is inserted before\nthe entries inserted as a result of PYTHONPATH.

\n

A program is free to modify this list for its own purposes.

\n

\nChanged in version 2.3: Unicode strings are no longer ignored.

\n
\n

See also

\n

Module site This describes how to use .pth files to extend\nsys.path.

\n
\n
\n\n
\n
\nsys.path_hooks
\n

A list of callables that take a path argument to try to create a\nfinder for the path. If a finder can be created, it is to be\nreturned by the callable, else raise ImportError.

\n

Originally specified in PEP 302.

\n
\n\n
\n
\nsys.path_importer_cache
\n

A dictionary acting as a cache for finder objects. The keys are\npaths that have been passed to sys.path_hooks and the values are\nthe finders that are found. If a path is a valid file system path but no\nexplicit finder is found on sys.path_hooks then None is\nstored to represent the implicit default finder should be used. If the path\nis not an existing path then imp.NullImporter is set.

\n

Originally specified in PEP 302.

\n
\n\n
\n
\nsys.platform
\n

This string contains a platform identifier that can be used to append\nplatform-specific components to sys.path, for instance.

\n

For most Unix systems, this is the lowercased OS name as returned by uname\n-s with the first part of the version as returned by uname -r appended,\ne.g. 'sunos5', at the time when Python was built. Unless you want to\ntest for a specific system version, it is therefore recommended to use the\nfollowing idiom:

\n
if sys.platform.startswith('freebsd'):\n    # FreeBSD-specific code here...\nelif sys.platform.startswith('linux'):\n    # Linux-specific code here...
\n
\n

\nChanged in version 2.7.3: Since lots of code check for sys.platform == 'linux2', and there is\nno essential change between Linux 2.x and 3.x, sys.platform is always\nset to 'linux2', even on Linux 3.x. In Python 3.3 and later, the\nvalue will always be set to 'linux', so it is recommended to always\nuse the startswith idiom presented above.

\n

For other systems, the values are:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Systemplatform value
Linux (2.x and 3.x)'linux2'
Windows'win32'
Windows/Cygwin'cygwin'
Mac OS X'darwin'
OS/2'os2'
OS/2 EMX'os2emx'
RiscOS'riscos'
AtheOS'atheos'
\n
\n

See also

\n

os.name has a coarser granularity. os.uname() gives\nsystem-dependent version information.

\n

The platform module provides detailed checks for the\nsystem’s identity.

\n
\n
\n\n
\n
\nsys.prefix
\n
A string giving the site-specific directory prefix where the platform\nindependent Python files are installed; by default, this is the string\n'/usr/local'. This can be set at build time with the --prefix\nargument to the configure script. The main collection of Python\nlibrary modules is installed in the directory prefix/lib/pythonX.Y`\nwhile the platform independent header files (all except pyconfig.h) are\nstored in prefix/include/pythonX.Y`, where X.Y is the version\nnumber of Python, for example 2.7.
\n\n
\n
\nsys.ps1
\n
\nsys.ps2
\n

Strings specifying the primary and secondary prompt of the interpreter. These\nare only defined if the interpreter is in interactive mode. Their initial\nvalues in this case are '>>> ' and '... '. If a non-string object is\nassigned to either variable, its str() is re-evaluated each time the\ninterpreter prepares to read a new interactive command; this can be used to\nimplement a dynamic prompt.

\n
\n\n
\n
\nsys.py3kwarning
\n

Bool containing the status of the Python 3.0 warning flag. It’s True\nwhen Python is started with the -3 option. (This should be considered\nread-only; setting it to a different value doesn’t have an effect on\nPython 3.0 warnings.)

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsys.setcheckinterval(interval)
\n
Set the interpreter’s “check interval”. This integer value determines how often\nthe interpreter checks for periodic things such as thread switches and signal\nhandlers. The default is 100, meaning the check is performed every 100\nPython virtual instructions. Setting it to a larger value may increase\nperformance for programs using threads. Setting it to a value <= 0 checks\nevery virtual instruction, maximizing responsiveness as well as overhead.
\n\n
\n
\nsys.setdefaultencoding(name)
\n

Set the current default string encoding used by the Unicode implementation. If\nname does not match any available encoding, LookupError is raised.\nThis function is only intended to be used by the site module\nimplementation and, where needed, by sitecustomize. Once used by the\nsite module, it is removed from the sys module’s namespace.

\n

\nNew in version 2.0.

\n
\n\n
\n
\nsys.setdlopenflags(n)
\n

Set the flags used by the interpreter for dlopen() calls, such as when\nthe interpreter loads extension modules. Among other things, this will enable a\nlazy resolving of symbols when importing a module, if called as\nsys.setdlopenflags(0). To share symbols across extension modules, call as\nsys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL). Symbolic names for the\nflag modules can be either found in the dl module, or in the DLFCN\nmodule. If DLFCN is not available, it can be generated from\n/usr/include/dlfcn.h using the h2py script. Availability:\nUnix.

\n

\nNew in version 2.2.

\n
\n\n
\n
\nsys.setprofile(profilefunc)
\n

Set the system’s profile function, which allows you to implement a Python source\ncode profiler in Python. See chapter The Python Profilers for more information on the\nPython profiler. The system’s profile function is called similarly to the\nsystem’s trace function (see settrace()), but it isn’t called for each\nexecuted line of code (only on call and return, but the return event is reported\neven when an exception has been set). The function is thread-specific, but\nthere is no way for the profiler to know about context switches between threads,\nso it does not make sense to use this in the presence of multiple threads. Also,\nits return value is not used, so it can simply return None.

\n
\n\n
\n
\nsys.setrecursionlimit(limit)
\n

Set the maximum depth of the Python interpreter stack to limit. This limit\nprevents infinite recursion from causing an overflow of the C stack and crashing\nPython.

\n

The highest possible limit is platform-dependent. A user may need to set the\nlimit higher when she has a program that requires deep recursion and a platform\nthat supports a higher limit. This should be done with care, because a too-high\nlimit can lead to a crash.

\n
\n\n
\n
\nsys.settrace(tracefunc)
\n

Set the system’s trace function, which allows you to implement a Python\nsource code debugger in Python. The function is thread-specific; for a\ndebugger to support multiple threads, it must be registered using\nsettrace() for each thread being debugged.

\n

Trace functions should have three arguments: frame, event, and\narg. frame is the current stack frame. event is a string: 'call',\n'line', 'return', 'exception', 'c_call', 'c_return', or\n'c_exception'. arg depends on the event type.

\n

The trace function is invoked (with event set to 'call') whenever a new\nlocal scope is entered; it should return a reference to a local trace\nfunction to be used that scope, or None if the scope shouldn’t be traced.

\n

The local trace function should return a reference to itself (or to another\nfunction for further tracing in that scope), or None to turn off tracing\nin that scope.

\n

The events have the following meaning:

\n
\n
'call'
\n
A function is called (or some other code block entered). The\nglobal trace function is called; arg is None; the return value\nspecifies the local trace function.
\n
'line'
\n
The interpreter is about to execute a new line of code or re-execute the\ncondition of a loop. The local trace function is called; arg is\nNone; the return value specifies the new local trace function. See\nObjects/lnotab_notes.txt for a detailed explanation of how this\nworks.
\n
'return'
\n
A function (or other code block) is about to return. The local trace\nfunction is called; arg is the value that will be returned, or None\nif the event is caused by an exception being raised. The trace function’s\nreturn value is ignored.
\n
'exception'
\n
An exception has occurred. The local trace function is called; arg is a\ntuple (exception, value, traceback); the return value specifies the\nnew local trace function.
\n
'c_call'
\n
A C function is about to be called. This may be an extension function or\na built-in. arg is the C function object.
\n
'c_return'
\n
A C function has returned. arg is the C function object.
\n
'c_exception'
\n
A C function has raised an exception. arg is the C function object.
\n
\n

Note that as an exception is propagated down the chain of callers, an\n'exception' event is generated at each level.

\n

For more information on code and frame objects, refer to The standard type hierarchy.

\n
\n

CPython implementation detail: The settrace() function is intended only for implementing debuggers,\nprofilers, coverage tools and the like. Its behavior is part of the\nimplementation platform, rather than part of the language definition, and\nthus may not be available in all Python implementations.

\n
\n
\n\n
\n
\nsys.settscdump(on_flag)
\n

Activate dumping of VM measurements using the Pentium timestamp counter, if\non_flag is true. Deactivate these dumps if on_flag is off. The function is\navailable only if Python was compiled with --with-tsc. To understand\nthe output of this dump, read Python/ceval.c in the Python sources.

\n

\nNew in version 2.4.

\n
\n

CPython implementation detail: This function is intimately bound to CPython implementation details and\nthus not likely to be implemented elsewhere.

\n
\n
\n\n
\n
\nsys.stdin
\n
\nsys.stdout
\n
\nsys.stderr
\n

File objects corresponding to the interpreter’s standard input, output and error\nstreams. stdin is used for all interpreter input except for scripts but\nincluding calls to input() and raw_input(). stdout is used for\nthe output of print and expression statements and for the\nprompts of input() and raw_input(). The interpreter’s own prompts\nand (almost all of) its error messages go to stderr. stdout and\nstderr needn’t be built-in file objects: any object is acceptable as long\nas it has a write() method that takes a string argument. (Changing these\nobjects doesn’t affect the standard I/O streams of processes executed by\nos.popen(), os.system() or the exec*() family of functions in\nthe os module.)

\n
\n\n
\n
\nsys.__stdin__
\n
\nsys.__stdout__
\n
\nsys.__stderr__
\n

These objects contain the original values of stdin, stderr and\nstdout at the start of the program. They are used during finalization,\nand could be useful to print to the actual standard stream no matter if the\nsys.std* object has been redirected.

\n

It can also be used to restore the actual files to known working file objects\nin case they have been overwritten with a broken object. However, the\npreferred way to do this is to explicitly save the previous stream before\nreplacing it, and restore the saved object.

\n
\n\n
\n
\nsys.subversion
\n

A triple (repo, branch, version) representing the Subversion information of the\nPython interpreter. repo is the name of the repository, 'CPython'.\nbranch is a string of one of the forms 'trunk', 'branches/name' or\n'tags/name'. version is the output of svnversion, if the interpreter\nwas built from a Subversion checkout; it contains the revision number (range)\nand possibly a trailing ‘M’ if there were local modifications. If the tree was\nexported (or svnversion was not available), it is the revision of\nInclude/patchlevel.h if the branch is a tag. Otherwise, it is None.

\n

\nNew in version 2.5.

\n
\n

Note

\n

Python is now developed using\nMercurial. In recent Python 2.7 bugfix releases, subversion\ntherefore contains placeholder information. It is removed in Python\n3.3.

\n
\n
\n\n
\n
\nsys.tracebacklimit
\n
When this variable is set to an integer value, it determines the maximum number\nof levels of traceback information printed when an unhandled exception occurs.\nThe default is 1000. When set to 0 or less, all traceback information\nis suppressed and only the exception type and value are printed.
\n\n
\n
\nsys.version
\n
A string containing the version number of the Python interpreter plus additional\ninformation on the build number and compiler used. This string is displayed\nwhen the interactive interpreter is started. Do not extract version information\nout of it, rather, use version_info and the functions provided by the\nplatform module.
\n\n
\n
\nsys.api_version
\n

The C API version for this interpreter. Programmers may find this useful when\ndebugging version conflicts between Python and extension modules.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nsys.version_info
\n

A tuple containing the five components of the version number: major, minor,\nmicro, releaselevel, and serial. All values except releaselevel are\nintegers; the release level is 'alpha', 'beta', 'candidate', or\n'final'. The version_info value corresponding to the Python version 2.0\nis (2, 0, 0, 'final', 0). The components can also be accessed by name,\nso sys.version_info[0] is equivalent to sys.version_info.major\nand so on.

\n

\nNew in version 2.0.

\n

\nChanged in version 2.7: Added named component attributes

\n
\n\n
\n
\nsys.warnoptions
\n
This is an implementation detail of the warnings framework; do not modify this\nvalue. Refer to the warnings module for more information on the warnings\nframework.
\n\n
\n
\nsys.winver
\n
The version number used to form registry keys on Windows platforms. This is\nstored as string resource 1000 in the Python DLL. The value is normally the\nfirst three characters of version. It is provided in the sys\nmodule for informational purposes; modifying this value has no effect on the\nregistry keys used by Python. Availability: Windows.
\n\n

Citations

\n\n\n\n\n\n
[C99]ISO/IEC 9899:1999. “Programming languages – C.” A public draft of this standard is available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf .
\n
", "searchableItems": [ { "name": "sys._clear_type_cache", "domId": "sys_sys._clear_type_cache" }, { "name": "sys._current_frames", "domId": "sys_sys._current_frames" }, { "name": "sys._getframe", "domId": "sys_sys._getframe" }, { "name": "sys.call_tracing", "domId": "sys_sys.call_tracing" }, { "name": "sys.displayhook", "domId": "sys_sys.displayhook" }, { "name": "sys.exc_clear", "domId": "sys_sys.exc_clear" }, { "name": "sys.exc_info", "domId": "sys_sys.exc_info" }, { "name": "sys.excepthook", "domId": "sys_sys.excepthook" }, { "name": "sys.exit", "domId": "sys_sys.exit" }, { "name": "sys.getcheckinterval", "domId": "sys_sys.getcheckinterval" }, { "name": "sys.getdefaultencoding", "domId": "sys_sys.getdefaultencoding" }, { "name": "sys.getdlopenflags", "domId": "sys_sys.getdlopenflags" }, { "name": "sys.getfilesystemencoding", "domId": "sys_sys.getfilesystemencoding" }, { "name": "sys.getprofile", "domId": "sys_sys.getprofile" }, { "name": "sys.getrecursionlimit", "domId": "sys_sys.getrecursionlimit" }, { "name": "sys.getrefcount", "domId": "sys_sys.getrefcount" }, { "name": "sys.getsizeof", "domId": "sys_sys.getsizeof" }, { "name": "sys.gettrace", "domId": "sys_sys.gettrace" }, { "name": "sys.getwindowsversion", "domId": "sys_sys.getwindowsversion" }, { "name": "sys.setcheckinterval", "domId": "sys_sys.setcheckinterval" }, { "name": "sys.setdefaultencoding", "domId": "sys_sys.setdefaultencoding" }, { "name": "sys.setdlopenflags", "domId": "sys_sys.setdlopenflags" }, { "name": "sys.setprofile", "domId": "sys_sys.setprofile" }, { "name": "sys.setrecursionlimit", "domId": "sys_sys.setrecursionlimit" }, { "name": "sys.settrace", "domId": "sys_sys.settrace" }, { "name": "sys.settscdump", "domId": "sys_sys.settscdump" } ] }, { "url": "http://docs.python.org/library/__main__.html", "title": "__main__", "html": "
\n

27.5. __main__ — Top-level script environment

\n

This module represents the (otherwise anonymous) scope in which the\ninterpreter’s main program executes — commands read either from standard\ninput, from a script file, or from an interactive prompt. It is this\nenvironment in which the idiomatic “conditional script” stanza causes a script\nto run:

\n
if __name__ == "__main__":\n    main()\n
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/trace.html", "title": "trace", "html": "
\n

26.7. trace — Trace or track Python statement execution

\n

Source code: Lib/trace.py

\n
\n

The trace module allows you to trace program execution, generate\nannotated statement coverage listings, print caller/callee relationships and\nlist functions executed during a program run. It can be used in another program\nor from the command line.

\n
\n

26.7.1. Command-Line Usage

\n

The trace module can be invoked from the command line. It can be as\nsimple as

\n
python -m trace --count -C . somefile.py ...
\n
\n

The above will execute somefile.py and generate annotated listings of\nall Python modules imported during the execution into the current directory.

\n
\n
\n--help
\n
Display usage and exit.
\n\n
\n
\n--version
\n
Display the version of the module and exit.
\n\n
\n

26.7.1.1. Main options

\n

At least one of the following options must be specified when invoking\ntrace. The --listfuncs option is mutually exclusive with\nthe --trace and --counts options . When\n--listfuncs is provided, neither --counts nor\n--trace are accepted, and vice versa.

\n
\n
\n-c, --count
\n
Produce a set of annotated listing files upon program completion that shows\nhow many times each statement was executed. See also\n--coverdir, --file and\n--no-report below.
\n\n
\n
\n-t, --trace
\n
Display lines as they are executed.
\n\n
\n
\n-l, --listfuncs
\n
Display the functions executed by running the program.
\n\n
\n
\n-r, --report
\n
Produce an annotated list from an earlier program run that used the\n--count and --file option. This does not\nexecute any code.
\n\n
\n
\n-T, --trackcalls
\n
Display the calling relationships exposed by running the program.
\n\n
\n
\n

26.7.1.2. Modifiers

\n
\n
\n-f, --file=<file>
\n
Name of a file to accumulate counts over several tracing runs. Should be\nused with the --count option.
\n\n
\n
\n-C, --coverdir=<dir>
\n
Directory where the report files go. The coverage report for\npackage.module is written to file dir/package/module.cover.
\n\n
\n
\n-m, --missing
\n
When generating annotated listings, mark lines which were not executed with\n>>>>>>.
\n\n
\n
\n-s, --summary
\n
When using --count or --report, write a brief\nsummary to stdout for each file processed.
\n\n
\n
\n-R, --no-report
\n
Do not generate annotated listings. This is useful if you intend to make\nseveral runs with --count, and then produce a single set of\nannotated listings at the end.
\n\n
\n
\n-g, --timing
\n
Prefix each line with the time since the program started. Only used while\ntracing.
\n\n
\n
\n

26.7.1.3. Filters

\n

These options may be repeated multiple times.

\n
\n
\n--ignore-module=<mod>
\n
Ignore each of the given module names and its submodules (if it is a\npackage). The argument can be a list of names separated by a comma.
\n\n
\n
\n--ignore-dir=<dir>
\n
Ignore all modules and packages in the named directory and subdirectories.\nThe argument can be a list of directories separated by os.pathsep.
\n\n
\n
\n
\n

26.7.2. Programmatic Interface

\n
\n
\nclass trace.Trace([count=1[, trace=1[, countfuncs=0[, countcallers=0[, ignoremods=()[, ignoredirs=()[, infile=None[, outfile=None[, timing=False]]]]]]]]])
\n

Create an object to trace execution of a single statement or expression. All\nparameters are optional. count enables counting of line numbers. trace\nenables line execution tracing. countfuncs enables listing of the\nfunctions called during the run. countcallers enables call relationship\ntracking. ignoremods is a list of modules or packages to ignore.\nignoredirs is a list of directories whose modules or packages should be\nignored. infile is the name of the file from which to read stored count\ninformation. outfile is the name of the file in which to write updated\ncount information. timing enables a timestamp relative to when tracing was\nstarted to be displayed.

\n
\n
\n
\nrun(cmd)
\n
Execute the command and gather statistics from the execution with\nthe current tracing parameters. cmd must be a string or code object,\nsuitable for passing into exec().
\n\n
\n
\nrunctx(cmd[, globals=None[, locals=None]])
\n
Execute the command and gather statistics from the execution with the\ncurrent tracing parameters, in the defined global and local\nenvironments. If not defined, globals and locals default to empty\ndictionaries.
\n\n
\n
\nrunfunc(func, *args, **kwds)
\n
Call func with the given arguments under control of the Trace\nobject with the current tracing parameters.
\n\n
\n
\nresults()
\n
Return a CoverageResults object that contains the cumulative\nresults of all previous calls to run, runctx and runfunc\nfor the given Trace instance. Does not reset the accumulated\ntrace results.
\n\n
\n
\n\n
\n
\nclass trace.CoverageResults
\n

A container for coverage results, created by Trace.results(). Should\nnot be created directly by the user.

\n
\n
\n
\nupdate(other)
\n
Merge in data from another CoverageResults object.
\n\n
\n
\nwrite_results([show_missing=True[, summary=False[, coverdir=None]]])
\n
Write coverage results. Set show_missing to show lines that had no\nhits. Set summary to include in the output the coverage summary per\nmodule. coverdir specifies the directory into which the coverage\nresult files will be output. If None, the results for each source\nfile are placed in its directory.
\n\n
\n
\n\n

A simple example demonstrating the use of the programmatic interface:

\n
import sys\nimport trace\n\n# create a Trace object, telling it what to ignore, and whether to\n# do tracing or line-counting or both.\ntracer = trace.Trace(\n    ignoredirs=[sys.prefix, sys.exec_prefix],\n    trace=0,\n    count=1)\n\n# run the new command using the given tracer\ntracer.run('main()')\n\n# make a report, placing output in /tmp\nr = tracer.results()\nr.write_results(show_missing=True, coverdir="/tmp")\n
\n
\n
\n
", "searchableItems": [ { "name": "trace.CoverageResults", "domId": "trace_trace.CoverageResults" }, { "name": "trace.CoverageResults.update", "domId": "trace_trace.CoverageResults.update" }, { "name": "trace.CoverageResults.write_results", "domId": "trace_trace.CoverageResults.write_results" }, { "name": "trace.Trace", "domId": "trace_trace.Trace" }, { "name": "trace.Trace.results", "domId": "trace_trace.Trace.results" }, { "name": "trace.Trace.run", "domId": "trace_trace.Trace.run" }, { "name": "trace.Trace.runctx", "domId": "trace_trace.Trace.runctx" }, { "name": "trace.Trace.runfunc", "domId": "trace_trace.Trace.runfunc" } ] }, { "url": "http://docs.python.org/library/traceback.html", "title": "traceback", "html": "
\n

27.10. traceback — Print or retrieve a stack traceback

\n

This module provides a standard interface to extract, format and print stack\ntraces of Python programs. It exactly mimics the behavior of the Python\ninterpreter when it prints a stack trace. This is useful when you want to print\nstack traces under program control, such as in a “wrapper” around the\ninterpreter.

\n

The module uses traceback objects — this is the object type that is stored in\nthe variables sys.exc_traceback (deprecated) and sys.last_traceback and\nreturned as the third item from sys.exc_info().

\n

The module defines the following functions:

\n
\n
\ntraceback.print_tb(traceback[, limit[, file]])
\n
Print up to limit stack trace entries from traceback. If limit is omitted\nor None, all entries are printed. If file is omitted or None, the\noutput goes to sys.stderr; otherwise it should be an open file or file-like\nobject to receive the output.
\n\n
\n
\ntraceback.print_exception(type, value, traceback[, limit[, file]])
\n
Print exception information and up to limit stack trace entries from\ntraceback to file. This differs from print_tb() in the following ways:\n(1) if traceback is not None, it prints a header Traceback (most recent\ncall last):; (2) it prints the exception type and value after the stack\ntrace; (3) if type is SyntaxError and value has the appropriate\nformat, it prints the line where the syntax error occurred with a caret\nindicating the approximate position of the error.
\n\n
\n
\ntraceback.print_exc([limit[, file]])
\n
This is a shorthand for print_exception(sys.exc_type, sys.exc_value,\nsys.exc_traceback, limit, file). (In fact, it uses sys.exc_info() to\nretrieve the same information in a thread-safe way instead of using the\ndeprecated variables.)
\n\n
\n
\ntraceback.format_exc([limit])
\n

This is like print_exc(limit) but returns a string instead of printing to a\nfile.

\n

\nNew in version 2.4.

\n
\n\n
\n
\ntraceback.print_last([limit[, file]])
\n
This is a shorthand for print_exception(sys.last_type, sys.last_value,\nsys.last_traceback, limit, file). In general it will work only after\nan exception has reached an interactive prompt (see sys.last_type).
\n\n
\n
\ntraceback.print_stack([f[, limit[, file]]])
\n
This function prints a stack trace from its invocation point. The optional f\nargument can be used to specify an alternate stack frame to start. The optional\nlimit and file arguments have the same meaning as for\nprint_exception().
\n\n
\n
\ntraceback.extract_tb(traceback[, limit])
\n
Return a list of up to limit “pre-processed” stack trace entries extracted\nfrom the traceback object traceback. It is useful for alternate formatting of\nstack traces. If limit is omitted or None, all entries are extracted. A\n“pre-processed” stack trace entry is a quadruple (filename, line number,\nfunction name, text) representing the information that is usually printed\nfor a stack trace. The text is a string with leading and trailing whitespace\nstripped; if the source is not available it is None.
\n\n
\n
\ntraceback.extract_stack([f[, limit]])
\n
Extract the raw traceback from the current stack frame. The return value has\nthe same format as for extract_tb(). The optional f and limit\narguments have the same meaning as for print_stack().
\n\n
\n
\ntraceback.format_list(list)
\n
Given a list of tuples as returned by extract_tb() or\nextract_stack(), return a list of strings ready for printing. Each string\nin the resulting list corresponds to the item with the same index in the\nargument list. Each string ends in a newline; the strings may contain internal\nnewlines as well, for those items whose source text line is not None.
\n\n
\n
\ntraceback.format_exception_only(type, value)
\n
Format the exception part of a traceback. The arguments are the exception type\nand value such as given by sys.last_type and sys.last_value. The return\nvalue is a list of strings, each ending in a newline. Normally, the list\ncontains a single string; however, for SyntaxError exceptions, it\ncontains several lines that (when printed) display detailed information about\nwhere the syntax error occurred. The message indicating which exception\noccurred is the always last string in the list.
\n\n
\n
\ntraceback.format_exception(type, value, tb[, limit])
\n
Format a stack trace and the exception information. The arguments have the\nsame meaning as the corresponding arguments to print_exception(). The\nreturn value is a list of strings, each ending in a newline and some containing\ninternal newlines. When these lines are concatenated and printed, exactly the\nsame text is printed as does print_exception().
\n\n
\n
\ntraceback.format_tb(tb[, limit])
\n
A shorthand for format_list(extract_tb(tb, limit)).
\n\n
\n
\ntraceback.format_stack([f[, limit]])
\n
A shorthand for format_list(extract_stack(f, limit)).
\n\n
\n
\ntraceback.tb_lineno(tb)
\n
This function returns the current line number set in the traceback object. This\nfunction was necessary because in versions of Python prior to 2.3 when the\n-O flag was passed to Python the tb.tb_lineno was not updated\ncorrectly. This function has no use in versions past 2.3.
\n\n
\n

27.10.1. Traceback Examples

\n

This simple example implements a basic read-eval-print loop, similar to (but\nless useful than) the standard Python interactive interpreter loop. For a more\ncomplete implementation of the interpreter loop, refer to the code\nmodule.

\n
import sys, traceback\n\ndef run_user_code(envdir):\n    source = raw_input(">>> ")\n    try:\n        exec source in envdir\n    except:\n        print "Exception in user code:"\n        print '-'*60\n        traceback.print_exc(file=sys.stdout)\n        print '-'*60\n\nenvdir = {}\nwhile 1:\n    run_user_code(envdir)\n
\n
\n

The following example demonstrates the different ways to print and format the\nexception and traceback:

\n
import sys, traceback\n\ndef lumberjack():\n    bright_side_of_death()\n\ndef bright_side_of_death():\n    return tuple()[0]\n\ntry:\n    lumberjack()\nexcept IndexError:\n    exc_type, exc_value, exc_traceback = sys.exc_info()\n    print "*** print_tb:"\n    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)\n    print "*** print_exception:"\n    traceback.print_exception(exc_type, exc_value, exc_traceback,\n                              limit=2, file=sys.stdout)\n    print "*** print_exc:"\n    traceback.print_exc()\n    print "*** format_exc, first and last line:"\n    formatted_lines = traceback.format_exc().splitlines()\n    print formatted_lines[0]\n    print formatted_lines[-1]\n    print "*** format_exception:"\n    print repr(traceback.format_exception(exc_type, exc_value,\n                                          exc_traceback))\n    print "*** extract_tb:"\n    print repr(traceback.extract_tb(exc_traceback))\n    print "*** format_tb:"\n    print repr(traceback.format_tb(exc_traceback))\n    print "*** tb_lineno:", exc_traceback.tb_lineno\n
\n
\n

The output for the example would look similar to this:

\n
*** print_tb:\n  File \"<doctest...>\", line 10, in <module>\n    lumberjack()\n*** print_exception:\nTraceback (most recent call last):\n  File \"<doctest...>\", line 10, in <module>\n    lumberjack()\n  File \"<doctest...>\", line 4, in lumberjack\n    bright_side_of_death()\nIndexError: tuple index out of range\n*** print_exc:\nTraceback (most recent call last):\n  File \"<doctest...>\", line 10, in <module>\n    lumberjack()\n  File \"<doctest...>\", line 4, in lumberjack\n    bright_side_of_death()\nIndexError: tuple index out of range\n*** format_exc, first and last line:\nTraceback (most recent call last):\nIndexError: tuple index out of range\n*** format_exception:\n['Traceback (most recent call last):\\n',\n '  File \"<doctest...>\", line 10, in <module>\\n    lumberjack()\\n',\n '  File \"<doctest...>\", line 4, in lumberjack\\n    bright_side_of_death()\\n',\n '  File \"<doctest...>\", line 7, in bright_side_of_death\\n    return tuple()[0]\\n',\n 'IndexError: tuple index out of range\\n']\n*** extract_tb:\n[('<doctest...>', 10, '<module>', 'lumberjack()'),\n ('<doctest...>', 4, 'lumberjack', 'bright_side_of_death()'),\n ('<doctest...>', 7, 'bright_side_of_death', 'return tuple()[0]')]\n*** format_tb:\n['  File \"<doctest...>\", line 10, in <module>\\n    lumberjack()\\n',\n '  File \"<doctest...>\", line 4, in lumberjack\\n    bright_side_of_death()\\n',\n '  File \"<doctest...>\", line 7, in bright_side_of_death\\n    return tuple()[0]\\n']\n*** tb_lineno: 10
\n
\n

The following example shows the different ways to print and format the stack:

\n
>>> import traceback\n>>> def another_function():\n...     lumberstack()\n...\n>>> def lumberstack():\n...     traceback.print_stack()\n...     print repr(traceback.extract_stack())\n...     print repr(traceback.format_stack())\n...\n>>> another_function()\n  File "<doctest>", line 10, in <module>\n    another_function()\n  File "<doctest>", line 3, in another_function\n    lumberstack()\n  File "<doctest>", line 6, in lumberstack\n    traceback.print_stack()\n[('<doctest>', 10, '<module>', 'another_function()'),\n ('<doctest>', 3, 'another_function', 'lumberstack()'),\n ('<doctest>', 7, 'lumberstack', 'print repr(traceback.extract_stack())')]\n['  File "<doctest>", line 10, in <module>\\n    another_function()\\n',\n '  File "<doctest>", line 3, in another_function\\n    lumberstack()\\n',\n '  File "<doctest>", line 8, in lumberstack\\n    print repr(traceback.format_stack())\\n']\n
\n
\n

This last example demonstrates the final few formatting functions:

\n
>>> import traceback\n>>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'),\n...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])\n['  File "spam.py", line 3, in <module>\\n    spam.eggs()\\n',\n '  File "eggs.py", line 42, in eggs\\n    return "bacon"\\n']\n>>> an_error = IndexError('tuple index out of range')\n>>> traceback.format_exception_only(type(an_error), an_error)\n['IndexError: tuple index out of range\\n']\n
\n
\n
\n
", "searchableItems": [ { "name": "traceback.extract_stack", "domId": "traceback_traceback.extract_stack" }, { "name": "traceback.extract_tb", "domId": "traceback_traceback.extract_tb" }, { "name": "traceback.format_exc", "domId": "traceback_traceback.format_exc" }, { "name": "traceback.format_exception", "domId": "traceback_traceback.format_exception" }, { "name": "traceback.format_exception_only", "domId": "traceback_traceback.format_exception_only" }, { "name": "traceback.format_list", "domId": "traceback_traceback.format_list" }, { "name": "traceback.format_stack", "domId": "traceback_traceback.format_stack" }, { "name": "traceback.format_tb", "domId": "traceback_traceback.format_tb" }, { "name": "traceback.print_exc", "domId": "traceback_traceback.print_exc" }, { "name": "traceback.print_exception", "domId": "traceback_traceback.print_exception" }, { "name": "traceback.print_last", "domId": "traceback_traceback.print_last" }, { "name": "traceback.print_stack", "domId": "traceback_traceback.print_stack" }, { "name": "traceback.print_tb", "domId": "traceback_traceback.print_tb" }, { "name": "traceback.tb_lineno", "domId": "traceback_traceback.tb_lineno" } ] }, { "url": "http://docs.python.org/library/atexit.html", "title": "atexit", "html": "
\n

27.9. atexit — Exit handlers

\n

\nNew in version 2.0.

\n

Source code: Lib/atexit.py

\n
\n

The atexit module defines a single function to register cleanup\nfunctions. Functions thus registered are automatically executed upon normal\ninterpreter termination. The order in which the functions are called is not\ndefined; if you have cleanup operations that depend on each other, you should\nwrap them in a function and register that one. This keeps atexit simple.

\n

Note: the functions registered via this module are not called when the program\nis killed by a signal not handled by Python, when a Python fatal internal error\nis detected, or when os._exit() is called.

\n

This is an alternate interface to the functionality provided by the\nsys.exitfunc variable.

\n

Note: This module is unlikely to work correctly when used with other code that\nsets sys.exitfunc. In particular, other core Python modules are free to use\natexit without the programmer’s knowledge. Authors who use\nsys.exitfunc should convert their code to use atexit instead. The\nsimplest way to convert code that sets sys.exitfunc is to import\natexit and register the function that had been bound to sys.exitfunc.

\n
\n
\natexit.register(func[, *args[, **kargs]])
\n

Register func as a function to be executed at termination. Any optional\narguments that are to be passed to func must be passed as arguments to\nregister().

\n

At normal program termination (for instance, if sys.exit() is called or\nthe main module’s execution completes), all functions registered are called in\nlast in, first out order. The assumption is that lower level modules will\nnormally be imported before higher level modules and thus must be cleaned up\nlater.

\n

If an exception is raised during execution of the exit handlers, a traceback is\nprinted (unless SystemExit is raised) and the exception information is\nsaved. After all exit handlers have had a chance to run the last exception to\nbe raised is re-raised.

\n

\nChanged in version 2.6: This function now returns func which makes it possible to use it as a\ndecorator without binding the original name to None.

\n
\n\n
\n

See also

\n
\n
Module readline
\n
Useful example of atexit to read and write readline history files.
\n
\n
\n
\n

27.9.1. atexit Example

\n

The following simple example demonstrates how a module can initialize a counter\nfrom a file when it is imported and save the counter’s updated value\nautomatically when the program terminates without relying on the application\nmaking an explicit call into this module at termination.

\n
try:\n    _count = int(open("/tmp/counter").read())\nexcept IOError:\n    _count = 0\n\ndef incrcounter(n):\n    global _count\n    _count = _count + n\n\ndef savecounter():\n    open("/tmp/counter", "w").write("%d"  _count)\n\nimport atexit\natexit.register(savecounter)\n
\n
\n

Positional and keyword arguments may also be passed to register() to be\npassed along to the registered function when it is called:

\n
def goodbye(name, adjective):\n    print 'Goodbye, %s, it was %s to meet you.'  (name, adjective)\n\nimport atexit\natexit.register(goodbye, 'Donny', 'nice')\n\n# or:\natexit.register(goodbye, adjective='nice', name='Donny')\n
\n
\n

Usage as a decorator:

\n
import atexit\n\n@atexit.register\ndef goodbye():\n    print "You are now leaving the Python sector."\n
\n
\n

This obviously only works with functions that don’t take arguments.

\n
\n
", "searchableItems": [ { "name": "atexit.register", "domId": "atexit_atexit.register" } ] }, { "url": "http://docs.python.org/library/warnings.html", "title": "warnings", "html": "
\n

27.6. warnings — Warning control

\n

\nNew in version 2.1.

\n

Source code: Lib/warnings.py

\n
\n

Warning messages are typically issued in situations where it is useful to alert\nthe user of some condition in a program, where that condition (normally) doesn’t\nwarrant raising an exception and terminating the program. For example, one\nmight want to issue a warning when a program uses an obsolete module.

\n

Python programmers issue warnings by calling the warn() function defined\nin this module. (C programmers use PyErr_WarnEx(); see\nException Handling for details).

\n

Warning messages are normally written to sys.stderr, but their disposition\ncan be changed flexibly, from ignoring all warnings to turning them into\nexceptions. The disposition of warnings can vary based on the warning category\n(see below), the text of the warning message, and the source location where it\nis issued. Repetitions of a particular warning for the same source location are\ntypically suppressed.

\n

There are two stages in warning control: first, each time a warning is issued, a\ndetermination is made whether a message should be issued or not; next, if a\nmessage is to be issued, it is formatted and printed using a user-settable hook.

\n

The determination whether to issue a warning message is controlled by the\nwarning filter, which is a sequence of matching rules and actions. Rules can be\nadded to the filter by calling filterwarnings() and reset to its default\nstate by calling resetwarnings().

\n

The printing of warning messages is done by calling showwarning(), which\nmay be overridden; the default implementation of this function formats the\nmessage by calling formatwarning(), which is also available for use by\ncustom implementations.

\n
\n

See also

\n

logging.captureWarnings() allows you to handle all warnings with\nthe standard logging infrastructure.

\n
\n
\n

27.6.1. Warning Categories

\n

There are a number of built-in exceptions that represent warning categories.\nThis categorization is useful to be able to filter out groups of warnings. The\nfollowing warnings category classes are currently defined:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ClassDescription
WarningThis is the base class of all warning\ncategory classes. It is a subclass of\nException.
UserWarningThe default category for warn().
DeprecationWarningBase category for warnings about deprecated\nfeatures (ignored by default).
SyntaxWarningBase category for warnings about dubious\nsyntactic features.
RuntimeWarningBase category for warnings about dubious\nruntime features.
FutureWarningBase category for warnings about constructs\nthat will change semantically in the future.
PendingDeprecationWarningBase category for warnings about features\nthat will be deprecated in the future\n(ignored by default).
ImportWarningBase category for warnings triggered during\nthe process of importing a module (ignored by\ndefault).
UnicodeWarningBase category for warnings related to\nUnicode.
\n

While these are technically built-in exceptions, they are documented here,\nbecause conceptually they belong to the warnings mechanism.

\n

User code can define additional warning categories by subclassing one of the\nstandard warning categories. A warning category must always be a subclass of\nthe Warning class.

\n

\nChanged in version 2.7: DeprecationWarning is ignored by default.

\n
\n
\n

27.6.2. The Warnings Filter

\n

The warnings filter controls whether warnings are ignored, displayed, or turned\ninto errors (raising an exception).

\n

Conceptually, the warnings filter maintains an ordered list of filter\nspecifications; any specific warning is matched against each filter\nspecification in the list in turn until a match is found; the match determines\nthe disposition of the match. Each entry is a tuple of the form (action,\nmessage, category, module, lineno), where:

\n\n

Since the Warning class is derived from the built-in Exception\nclass, to turn a warning into an error we simply raise category(message).

\n

The warnings filter is initialized by -W options passed to the Python\ninterpreter command line. The interpreter saves the arguments for all\n-W options without interpretation in sys.warnoptions; the\nwarnings module parses these when it is first imported (invalid options\nare ignored, after printing a message to sys.stderr).

\n
\n

27.6.2.1. Default Warning Filters

\n

By default, Python installs several warning filters, which can be overridden by\nthe command-line options passed to -W and calls to\nfilterwarnings().

\n
    \n
  • PendingDeprecationWarning, and ImportWarning are ignored.
  • \n
  • BytesWarning is ignored unless the -b option is given once or\ntwice; in this case this warning is either printed (-b) or turned into an\nexception (-bb).
  • \n
\n
\n
\n
\n

27.6.3. Temporarily Suppressing Warnings

\n

If you are using code that you know will raise a warning, such as a deprecated\nfunction, but do not want to see the warning, then it is possible to suppress\nthe warning using the catch_warnings context manager:

\n
import warnings\n\ndef fxn():\n    warnings.warn("deprecated", DeprecationWarning)\n\nwith warnings.catch_warnings():\n    warnings.simplefilter("ignore")\n    fxn()\n
\n
\n

While within the context manager all warnings will simply be ignored. This\nallows you to use known-deprecated code without having to see the warning while\nnot suppressing the warning for other code that might not be aware of its use\nof deprecated code. Note: this can only be guaranteed in a single-threaded\napplication. If two or more threads use the catch_warnings context\nmanager at the same time, the behavior is undefined.

\n
\n
\n

27.6.4. Testing Warnings

\n

To test warnings raised by code, use the catch_warnings context\nmanager. With it you can temporarily mutate the warnings filter to facilitate\nyour testing. For instance, do the following to capture all raised warnings to\ncheck:

\n
import warnings\n\ndef fxn():\n    warnings.warn("deprecated", DeprecationWarning)\n\nwith warnings.catch_warnings(record=True) as w:\n    # Cause all warnings to always be triggered.\n    warnings.simplefilter("always")\n    # Trigger a warning.\n    fxn()\n    # Verify some things\n    assert len(w) == 1\n    assert issubclass(w[-1].category, DeprecationWarning)\n    assert "deprecated" in str(w[-1].message)\n
\n
\n

One can also cause all warnings to be exceptions by using error instead of\nalways. One thing to be aware of is that if a warning has already been\nraised because of a once/default rule, then no matter what filters are\nset the warning will not be seen again unless the warnings registry related to\nthe warning has been cleared.

\n

Once the context manager exits, the warnings filter is restored to its state\nwhen the context was entered. This prevents tests from changing the warnings\nfilter in unexpected ways between tests and leading to indeterminate test\nresults. The showwarning() function in the module is also restored to\nits original value. Note: this can only be guaranteed in a single-threaded\napplication. If two or more threads use the catch_warnings context\nmanager at the same time, the behavior is undefined.

\n

When testing multiple operations that raise the same kind of warning, it\nis important to test them in a manner that confirms each operation is raising\na new warning (e.g. set warnings to be raised as exceptions and check the\noperations raise exceptions, check that the length of the warning list\ncontinues to increase after each operation, or else delete the previous\nentries from the warnings list before each new operation).

\n
\n
\n

27.6.5. Updating Code For New Versions of Python

\n

Warnings that are only of interest to the developer are ignored by default. As\nsuch you should make sure to test your code with typically ignored warnings\nmade visible. You can do this from the command-line by passing -Wd\nto the interpreter (this is shorthand for -W default). This enables\ndefault handling for all warnings, including those that are ignored by default.\nTo change what action is taken for encountered warnings you simply change what\nargument is passed to -W, e.g. -W error. See the\n-W flag for more details on what is possible.

\n

To programmatically do the same as -Wd, use:

\n
warnings.simplefilter('default')\n
\n
\n

Make sure to execute this code as soon as possible. This prevents the\nregistering of what warnings have been raised from unexpectedly influencing how\nfuture warnings are treated.

\n

Having certain warnings ignored by default is done to prevent a user from\nseeing warnings that are only of interest to the developer. As you do not\nnecessarily have control over what interpreter a user uses to run their code,\nit is possible that a new version of Python will be released between your\nrelease cycles. The new interpreter release could trigger new warnings in your\ncode that were not there in an older interpreter, e.g.\nDeprecationWarning for a module that you are using. While you as a\ndeveloper want to be notified that your code is using a deprecated module, to a\nuser this information is essentially noise and provides no benefit to them.

\n
\n
\n

27.6.6. Available Functions

\n
\n
\nwarnings.warn(message[, category[, stacklevel]])
\n

Issue a warning, or maybe ignore it or raise an exception. The category\nargument, if given, must be a warning category class (see above); it defaults to\nUserWarning. Alternatively message can be a Warning instance,\nin which case category will be ignored and message.__class__ will be used.\nIn this case the message text will be str(message). This function raises an\nexception if the particular warning issued is changed into an error by the\nwarnings filter see above. The stacklevel argument can be used by wrapper\nfunctions written in Python, like this:

\n
def deprecation(message):\n    warnings.warn(message, DeprecationWarning, stacklevel=2)\n
\n
\n

This makes the warning refer to deprecation()‘s caller, rather than to the\nsource of deprecation() itself (since the latter would defeat the purpose\nof the warning message).

\n
\n\n
\n
\nwarnings.warn_explicit(message, category, filename, lineno[, module[, registry[, module_globals]]])
\n

This is a low-level interface to the functionality of warn(), passing in\nexplicitly the message, category, filename and line number, and optionally the\nmodule name and the registry (which should be the __warningregistry__\ndictionary of the module). The module name defaults to the filename with\n.py stripped; if no registry is passed, the warning is never suppressed.\nmessage must be a string and category a subclass of Warning or\nmessage may be a Warning instance, in which case category will be\nignored.

\n

module_globals, if supplied, should be the global namespace in use by the code\nfor which the warning is issued. (This argument is used to support displaying\nsource for modules found in zipfiles or other non-filesystem import\nsources).

\n

\nChanged in version 2.5: Added the module_globals parameter.

\n
\n\n
\n
\nwarnings.warnpy3k(message[, category[, stacklevel]])
\n

Issue a warning related to Python 3.x deprecation. Warnings are only shown\nwhen Python is started with the -3 option. Like warn() message must\nbe a string and category a subclass of Warning. warnpy3k()\nis using DeprecationWarning as default warning class.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nwarnings.showwarning(message, category, filename, lineno[, file[, line]])
\n

Write a warning to a file. The default implementation calls\nformatwarning(message, category, filename, lineno, line) and writes the\nresulting string to file, which defaults to sys.stderr. You may replace\nthis function with an alternative implementation by assigning to\nwarnings.showwarning.\nline is a line of source code to be included in the warning\nmessage; if line is not supplied, showwarning() will\ntry to read the line specified by filename and lineno.

\n

\nChanged in version 2.7: The line argument is required to be supported.

\n
\n\n
\n
\nwarnings.formatwarning(message, category, filename, lineno[, line])
\n

Format a warning the standard way. This returns a string which may contain\nembedded newlines and ends in a newline. line is a line of source code to\nbe included in the warning message; if line is not supplied,\nformatwarning() will try to read the line specified by filename and\nlineno.

\n

\nChanged in version 2.6: Added the line argument.

\n
\n\n
\n
\nwarnings.filterwarnings(action[, message[, category[, module[, lineno[, append]]]]])
\n
Insert an entry into the list of warnings filter specifications. The entry is inserted at the front by default; if\nappend is true, it is inserted at the end. This checks the types of the\narguments, compiles the message and module regular expressions, and\ninserts them as a tuple in the list of warnings filters. Entries closer to\nthe front of the list override entries later in the list, if both match a\nparticular warning. Omitted arguments default to a value that matches\neverything.
\n\n
\n
\nwarnings.simplefilter(action[, category[, lineno[, append]]])
\n
Insert a simple entry into the list of warnings filter specifications. The meaning of the function parameters is as for\nfilterwarnings(), but regular expressions are not needed as the filter\ninserted always matches any message in any module as long as the category and\nline number match.
\n\n
\n
\nwarnings.resetwarnings()
\n
Reset the warnings filter. This discards the effect of all previous calls to\nfilterwarnings(), including that of the -W command line options\nand calls to simplefilter().
\n\n
\n
\n

27.6.7. Available Context Managers

\n
\n
\nclass warnings.catch_warnings([*, record=False, module=None])
\n

A context manager that copies and, upon exit, restores the warnings filter\nand the showwarning() function.\nIf the record argument is False (the default) the context manager\nreturns None on entry. If record is True, a list is\nreturned that is progressively populated with objects as seen by a custom\nshowwarning() function (which also suppresses output to sys.stdout).\nEach object in the list has attributes with the same names as the arguments to\nshowwarning().

\n

The module argument takes a module that will be used instead of the\nmodule returned when you import warnings whose filter will be\nprotected. This argument exists primarily for testing the warnings\nmodule itself.

\n
\n

Note

\n

The catch_warnings manager works by replacing and\nthen later restoring the module’s\nshowwarning() function and internal list of filter\nspecifications. This means the context manager is modifying\nglobal state and therefore is not thread-safe.

\n
\n
\n

Note

\n

In Python 3.0, the arguments to the constructor for\ncatch_warnings are keyword-only arguments.

\n
\n

\nNew in version 2.6.

\n
\n\n
\n
", "searchableItems": [ { "name": "warnings.catch_warnings", "domId": "warnings_warnings.catch_warnings" }, { "name": "warnings.filterwarnings", "domId": "warnings_warnings.filterwarnings" }, { "name": "warnings.formatwarning", "domId": "warnings_warnings.formatwarning" }, { "name": "warnings.resetwarnings", "domId": "warnings_warnings.resetwarnings" }, { "name": "warnings.showwarning", "domId": "warnings_warnings.showwarning" }, { "name": "warnings.simplefilter", "domId": "warnings_warnings.simplefilter" }, { "name": "warnings.warn", "domId": "warnings_warnings.warn" }, { "name": "warnings.warn_explicit", "domId": "warnings_warnings.warn_explicit" }, { "name": "warnings.warnpy3k", "domId": "warnings_warnings.warnpy3k" } ] }, { "url": "http://docs.python.org/library/abc.html", "title": "abc", "html": "
\n

27.8. abc — Abstract Base Classes

\n

\nNew in version 2.6.

\n

Source code: Lib/abc.py

\n
\n

This module provides the infrastructure for defining abstract base\nclasses (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this\nwas added to Python. (See also PEP 3141 and the numbers module\nregarding a type hierarchy for numbers based on ABCs.)

\n

The collections module has some concrete classes that derive from\nABCs; these can, of course, be further derived. In addition the\ncollections module has some ABCs that can be used to test whether\na class or instance provides a particular interface, for example, is it\nhashable or a mapping.

\n

This module provides the following class:

\n
\n
\nclass abc.ABCMeta
\n

Metaclass for defining Abstract Base Classes (ABCs).

\n

Use this metaclass to create an ABC. An ABC can be subclassed directly, and\nthen acts as a mix-in class. You can also register unrelated concrete\nclasses (even built-in classes) and unrelated ABCs as “virtual subclasses” –\nthese and their descendants will be considered subclasses of the registering\nABC by the built-in issubclass() function, but the registering ABC\nwon’t show up in their MRO (Method Resolution Order) nor will method\nimplementations defined by the registering ABC be callable (not even via\nsuper()). [1]

\n

Classes created with a metaclass of ABCMeta have the following method:

\n
\n
\nregister(subclass)
\n

Register subclass as a “virtual subclass” of this ABC. For\nexample:

\n
from abc import ABCMeta\n\nclass MyABC:\n    __metaclass__ = ABCMeta\n\nMyABC.register(tuple)\n\nassert issubclass(tuple, MyABC)\nassert isinstance((), MyABC)\n
\n
\n
\n\n

You can also override this method in an abstract base class:

\n
\n
\n__subclasshook__(subclass)
\n

(Must be defined as a class method.)

\n

Check whether subclass is considered a subclass of this ABC. This means\nthat you can customize the behavior of issubclass further without the\nneed to call register() on every class you want to consider a\nsubclass of the ABC. (This class method is called from the\n__subclasscheck__() method of the ABC.)

\n

This method should return True, False or NotImplemented. If\nit returns True, the subclass is considered a subclass of this ABC.\nIf it returns False, the subclass is not considered a subclass of\nthis ABC, even if it would normally be one. If it returns\nNotImplemented, the subclass check is continued with the usual\nmechanism.

\n
\n\n

For a demonstration of these concepts, look at this example ABC definition:

\n
class Foo(object):\n    def __getitem__(self, index):\n        ...\n    def __len__(self):\n        ...\n    def get_iterator(self):\n        return iter(self)\n\nclass MyIterable:\n    __metaclass__ = ABCMeta\n\n    @abstractmethod\n    def __iter__(self):\n        while False:\n            yield None\n\n    def get_iterator(self):\n        return self.__iter__()\n\n    @classmethod\n    def __subclasshook__(cls, C):\n        if cls is MyIterable:\n            if any("__iter__" in B.__dict__ for B in C.__mro__):\n                return True\n        return NotImplemented\n\nMyIterable.register(Foo)\n
\n
\n

The ABC MyIterable defines the standard iterable method,\n__iter__(), as an abstract method. The implementation given here can\nstill be called from subclasses. The get_iterator() method is also\npart of the MyIterable abstract base class, but it does not have to be\noverridden in non-abstract derived classes.

\n

The __subclasshook__() class method defined here says that any class\nthat has an __iter__() method in its __dict__ (or in that of\none of its base classes, accessed via the __mro__ list) is\nconsidered a MyIterable too.

\n

Finally, the last line makes Foo a virtual subclass of MyIterable,\neven though it does not define an __iter__() method (it uses the\nold-style iterable protocol, defined in terms of __len__() and\n__getitem__()). Note that this will not make get_iterator\navailable as a method of Foo, so it is provided separately.

\n
\n\n

It also provides the following decorators:

\n
\n
\nabc.abstractmethod(function)
\n

A decorator indicating abstract methods.

\n

Using this decorator requires that the class’s metaclass is ABCMeta or\nis derived from it.\nA class that has a metaclass derived from ABCMeta\ncannot be instantiated unless all of its abstract methods and\nproperties are overridden.\nThe abstract methods can be called using any of the normal ‘super’ call\nmechanisms.

\n

Dynamically adding abstract methods to a class, or attempting to modify the\nabstraction status of a method or class once it is created, are not\nsupported. The abstractmethod() only affects subclasses derived using\nregular inheritance; “virtual subclasses” registered with the ABC’s\nregister() method are not affected.

\n

Usage:

\n
class C:\n    __metaclass__ = ABCMeta\n    @abstractmethod\n    def my_abstract_method(self, ...):\n        ...\n
\n
\n
\n

Note

\n

Unlike Java abstract methods, these abstract\nmethods may have an implementation. This implementation can be\ncalled via the super() mechanism from the class that\noverrides it. This could be useful as an end-point for a\nsuper-call in a framework that uses cooperative\nmultiple-inheritance.

\n
\n
\n\n
\n
\nabc.abstractproperty([fget[, fset[, fdel[, doc]]]])
\n

A subclass of the built-in property(), indicating an abstract property.

\n

Using this function requires that the class’s metaclass is ABCMeta or\nis derived from it.\nA class that has a metaclass derived from ABCMeta cannot be\ninstantiated unless all of its abstract methods and properties are overridden.\nThe abstract properties can be called using any of the normal\n‘super’ call mechanisms.

\n

Usage:

\n
class C:\n    __metaclass__ = ABCMeta\n    @abstractproperty\n    def my_abstract_property(self):\n        ...\n
\n
\n

This defines a read-only property; you can also define a read-write abstract\nproperty using the ‘long’ form of property declaration:

\n
class C:\n    __metaclass__ = ABCMeta\n    def getx(self): ...\n    def setx(self, value): ...\n    x = abstractproperty(getx, setx)\n
\n
\n
\n\n

Footnotes

\n\n\n\n\n\n
[1]C++ programmers should note that Python’s virtual base class\nconcept is not the same as C++’s.
\n
", "searchableItems": [ { "name": "abc.ABCMeta", "domId": "abc_abc.ABCMeta" }, { "name": "abc.ABCMeta.__subclasshook__", "domId": "abc_abc.ABCMeta.__subclasshook__" }, { "name": "abc.ABCMeta.register", "domId": "abc_abc.ABCMeta.register" }, { "name": "abc.abstractmethod", "domId": "abc_abc.abstractmethod" }, { "name": "abc.abstractproperty", "domId": "abc_abc.abstractproperty" } ] }, { "url": "http://docs.python.org/library/__future__.html", "title": "__future__", "html": "
\n

27.11. __future__ — Future statement definitions

\n

Source code: Lib/__future__.py

\n
\n

__future__ is a real module, and serves three purposes:

\n\n

Each statement in __future__.py is of the form:

\n
FeatureName = _Feature(OptionalRelease, MandatoryRelease,\n                       CompilerFlag)\n
\n
\n

where, normally, OptionalRelease is less than MandatoryRelease, and both are\n5-tuples of the same form as sys.version_info:

\n
(PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int\n PY_MINOR_VERSION, # the 1; an int\n PY_MICRO_VERSION, # the 0; an int\n PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string\n PY_RELEASE_SERIAL # the 3; an int\n)\n
\n
\n

OptionalRelease records the first release in which the feature was accepted.

\n

In the case of a MandatoryRelease that has not yet occurred,\nMandatoryRelease predicts the release in which the feature will become part of\nthe language.

\n

Else MandatoryRelease records when the feature became part of the language; in\nreleases at or after that, modules no longer need a future statement to use the\nfeature in question, but may continue to use such imports.

\n

MandatoryRelease may also be None, meaning that a planned feature got\ndropped.

\n

Instances of class _Feature have two corresponding methods,\ngetOptionalRelease() and getMandatoryRelease().

\n

CompilerFlag is the (bitfield) flag that should be passed in the fourth\nargument to the built-in function compile() to enable the feature in\ndynamically compiled code. This flag is stored in the compiler_flag\nattribute on _Feature instances.

\n

No feature description will ever be deleted from __future__. Since its\nintroduction in Python 2.1 the following features have found their way into the\nlanguage using this mechanism:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
featureoptional inmandatory ineffect
nested_scopes2.1.0b12.2PEP 227:\nStatically Nested Scopes
generators2.2.0a12.3PEP 255:\nSimple Generators
division2.2.0a23.0PEP 238:\nChanging the Division Operator
absolute_import2.5.0a12.7PEP 328:\nImports: Multi-Line and Absolute/Relative
with_statement2.5.0a12.6PEP 343:\nThe “with” Statement
print_function2.6.0a23.0PEP 3105:\nMake print a function
unicode_literals2.6.0a23.0PEP 3112:\nBytes literals in Python 3000
\n
\n

See also

\n
\n
Future statements
\n
How the compiler treats future imports.
\n
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/contextlib.html", "title": "contextlib", "html": "
\n

27.7. contextlib — Utilities for with-statement contexts

\n

\nNew in version 2.5.

\n

Source code: Lib/contextlib.py

\n
\n

This module provides utilities for common tasks involving the with\nstatement. For more information see also Context Manager Types and\nWith Statement Context Managers.

\n

Functions provided:

\n
\n
\ncontextlib.contextmanager(func)
\n

This function is a decorator that can be used to define a factory\nfunction for with statement context managers, without needing to\ncreate a class or separate __enter__() and __exit__() methods.

\n

A simple example (this is not recommended as a real way of generating HTML!):

\n
from contextlib import contextmanager\n\n@contextmanager\ndef tag(name):\n    print \"<%s>\" % name\n    yield\n    print \"</%s>\" % name\n\n>>> with tag(\"h1\"):\n...    print \"foo\"\n...\n<h1>\nfoo\n</h1>
\n
\n

The function being decorated must return a generator-iterator when\ncalled. This iterator must yield exactly one value, which will be bound to\nthe targets in the with statement’s as clause, if any.

\n

At the point where the generator yields, the block nested in the with\nstatement is executed. The generator is then resumed after the block is exited.\nIf an unhandled exception occurs in the block, it is reraised inside the\ngenerator at the point where the yield occurred. Thus, you can use a\ntry...except...finally statement to trap\nthe error (if any), or ensure that some cleanup takes place. If an exception is\ntrapped merely in order to log it or to perform some action (rather than to\nsuppress it entirely), the generator must reraise that exception. Otherwise the\ngenerator context manager will indicate to the with statement that\nthe exception has been handled, and execution will resume with the statement\nimmediately following the with statement.

\n
\n\n
\n
\ncontextlib.nested(mgr1[, mgr2[, ...]])
\n

Combine multiple context managers into a single nested context manager.

\n

This function has been deprecated in favour of the multiple manager form\nof the with statement.

\n

The one advantage of this function over the multiple manager form of the\nwith statement is that argument unpacking allows it to be\nused with a variable number of context managers as follows:

\n
from contextlib import nested\n\nwith nested(*managers):\n    do_something()\n
\n
\n

Note that if the __exit__() method of one of the nested context managers\nindicates an exception should be suppressed, no exception information will be\npassed to any remaining outer context managers. Similarly, if the\n__exit__() method of one of the nested managers raises an exception, any\nprevious exception state will be lost; the new exception will be passed to the\n__exit__() methods of any remaining outer context managers. In general,\n__exit__() methods should avoid raising exceptions, and in particular they\nshould not re-raise a passed-in exception.

\n

This function has two major quirks that have led to it being deprecated. Firstly,\nas the context managers are all constructed before the function is invoked, the\n__new__() and __init__() methods of the inner context managers are\nnot actually covered by the scope of the outer context managers. That means, for\nexample, that using nested() to open two files is a programming error as the\nfirst file will not be closed promptly if an exception is thrown when opening\nthe second file.

\n

Secondly, if the __enter__() method of one of the inner context managers\nraises an exception that is caught and suppressed by the __exit__() method\nof one of the outer context managers, this construct will raise\nRuntimeError rather than skipping the body of the with\nstatement.

\n

Developers that need to support nesting of a variable number of context managers\ncan either use the warnings module to suppress the DeprecationWarning\nraised by this function or else use this function as a model for an application\nspecific implementation.

\n

\nDeprecated since version 2.7: The with-statement now supports this functionality directly (without the\nconfusing error prone quirks).

\n
\n\n
\n
\ncontextlib.closing(thing)
\n

Return a context manager that closes thing upon completion of the block. This\nis basically equivalent to:

\n
from contextlib import contextmanager\n\n@contextmanager\ndef closing(thing):\n    try:\n        yield thing\n    finally:\n        thing.close()\n
\n
\n

And lets you write code like this:

\n
from contextlib import closing\nimport urllib\n\nwith closing(urllib.urlopen('http://www.python.org')) as page:\n    for line in page:\n        print line\n
\n
\n

without needing to explicitly close page. Even if an error occurs,\npage.close() will be called when the with block is exited.

\n
\n\n
\n

See also

\n
\n
PEP 0343 - The “with” statement
\n
The specification, background, and examples for the Python with\nstatement.
\n
\n
\n
", "searchableItems": [ { "name": "contextlib.closing", "domId": "contextlib_contextlib.closing" }, { "name": "contextlib.contextmanager", "domId": "contextlib_contextlib.contextmanager" }, { "name": "contextlib.nested", "domId": "contextlib_contextlib.nested" } ] }, { "url": "http://docs.python.org/library/gc.html", "title": "gc", "html": "
\n

27.12. gc — Garbage Collector interface

\n

This module provides an interface to the optional garbage collector. It\nprovides the ability to disable the collector, tune the collection frequency,\nand set debugging options. It also provides access to unreachable objects that\nthe collector found but cannot free. Since the collector supplements the\nreference counting already used in Python, you can disable the collector if you\nare sure your program does not create reference cycles. Automatic collection\ncan be disabled by calling gc.disable(). To debug a leaking program call\ngc.set_debug(gc.DEBUG_LEAK). Notice that this includes\ngc.DEBUG_SAVEALL, causing garbage-collected objects to be saved in\ngc.garbage for inspection.

\n

The gc module provides the following functions:

\n
\n
\ngc.enable()
\n
Enable automatic garbage collection.
\n\n
\n
\ngc.disable()
\n
Disable automatic garbage collection.
\n\n
\n
\ngc.isenabled()
\n
Returns true if automatic collection is enabled.
\n\n
\n
\ngc.collect([generation])
\n

With no arguments, run a full collection. The optional argument generation\nmay be an integer specifying which generation to collect (from 0 to 2). A\nValueError is raised if the generation number is invalid. The number of\nunreachable objects found is returned.

\n

\nChanged in version 2.5: The optional generation argument was added.

\n

\nChanged in version 2.6: The free lists maintained for a number of built-in types are cleared\nwhenever a full collection or collection of the highest generation (2)\nis run. Not all items in some free lists may be freed due to the\nparticular implementation, in particular int and float.

\n
\n\n
\n
\ngc.set_debug(flags)
\n
Set the garbage collection debugging flags. Debugging information will be\nwritten to sys.stderr. See below for a list of debugging flags which can be\ncombined using bit operations to control debugging.
\n\n
\n
\ngc.get_debug()
\n
Return the debugging flags currently set.
\n\n
\n
\ngc.get_objects()
\n

Returns a list of all objects tracked by the collector, excluding the list\nreturned.

\n

\nNew in version 2.2.

\n
\n\n
\n
\ngc.set_threshold(threshold0[, threshold1[, threshold2]])
\n

Set the garbage collection thresholds (the collection frequency). Setting\nthreshold0 to zero disables collection.

\n

The GC classifies objects into three generations depending on how many\ncollection sweeps they have survived. New objects are placed in the youngest\ngeneration (generation 0). If an object survives a collection it is moved\ninto the next older generation. Since generation 2 is the oldest\ngeneration, objects in that generation remain there after a collection. In\norder to decide when to run, the collector keeps track of the number object\nallocations and deallocations since the last collection. When the number of\nallocations minus the number of deallocations exceeds threshold0, collection\nstarts. Initially only generation 0 is examined. If generation 0 has\nbeen examined more than threshold1 times since generation 1 has been\nexamined, then generation 1 is examined as well. Similarly, threshold2\ncontrols the number of collections of generation 1 before collecting\ngeneration 2.

\n
\n\n
\n
\ngc.get_count()
\n

Return the current collection counts as a tuple of (count0, count1,\ncount2).

\n

\nNew in version 2.5.

\n
\n\n
\n
\ngc.get_threshold()
\n
Return the current collection thresholds as a tuple of (threshold0,\nthreshold1, threshold2).
\n\n
\n
\ngc.get_referrers(*objs)
\n

Return the list of objects that directly refer to any of objs. This function\nwill only locate those containers which support garbage collection; extension\ntypes which do refer to other objects but do not support garbage collection will\nnot be found.

\n

Note that objects which have already been dereferenced, but which live in cycles\nand have not yet been collected by the garbage collector can be listed among the\nresulting referrers. To get only currently live objects, call collect()\nbefore calling get_referrers().

\n

Care must be taken when using objects returned by get_referrers() because\nsome of them could still be under construction and hence in a temporarily\ninvalid state. Avoid using get_referrers() for any purpose other than\ndebugging.

\n

\nNew in version 2.2.

\n
\n\n
\n
\ngc.get_referents(*objs)
\n

Return a list of objects directly referred to by any of the arguments. The\nreferents returned are those objects visited by the arguments’ C-level\ntp_traverse methods (if any), and may not be all objects actually\ndirectly reachable. tp_traverse methods are supported only by objects\nthat support garbage collection, and are only required to visit objects that may\nbe involved in a cycle. So, for example, if an integer is directly reachable\nfrom an argument, that integer object may or may not appear in the result list.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ngc.is_tracked(obj)
\n

Returns True if the object is currently tracked by the garbage collector,\nFalse otherwise. As a general rule, instances of atomic types aren’t\ntracked and instances of non-atomic types (containers, user-defined\nobjects...) are. However, some type-specific optimizations can be present\nin order to suppress the garbage collector footprint of simple instances\n(e.g. dicts containing only atomic keys and values):

\n
>>> gc.is_tracked(0)\nFalse\n>>> gc.is_tracked("a")\nFalse\n>>> gc.is_tracked([])\nTrue\n>>> gc.is_tracked({})\nFalse\n>>> gc.is_tracked({"a": 1})\nFalse\n>>> gc.is_tracked({"a": []})\nTrue\n
\n
\n

\nNew in version 2.7.

\n
\n\n

The following variable is provided for read-only access (you can mutate its\nvalue but should not rebind it):

\n
\n
\ngc.garbage
\n

A list of objects which the collector found to be unreachable but could not be\nfreed (uncollectable objects). By default, this list contains only objects with\n__del__() methods. [1] Objects that have __del__() methods and are\npart of a reference cycle cause the entire reference cycle to be uncollectable,\nincluding objects not necessarily in the cycle but reachable only from it.\nPython doesn’t collect such cycles automatically because, in general, it isn’t\npossible for Python to guess a safe order in which to run the __del__()\nmethods. If you know a safe order, you can force the issue by examining the\ngarbage list, and explicitly breaking cycles due to your objects within the\nlist. Note that these objects are kept alive even so by virtue of being in the\ngarbage list, so they should be removed from garbage too. For example,\nafter breaking cycles, do del gc.garbage[:] to empty the list. It’s\ngenerally better to avoid the issue by not creating cycles containing objects\nwith __del__() methods, and garbage can be examined in that case to\nverify that no such cycles are being created.

\n

If DEBUG_SAVEALL is set, then all unreachable objects will be added to\nthis list rather than freed.

\n
\n\n

The following constants are provided for use with set_debug():

\n
\n
\ngc.DEBUG_STATS
\n
Print statistics during collection. This information can be useful when tuning\nthe collection frequency.
\n\n
\n
\ngc.DEBUG_COLLECTABLE
\n
Print information on collectable objects found.
\n\n
\n
\ngc.DEBUG_UNCOLLECTABLE
\n
Print information of uncollectable objects found (objects which are not\nreachable but cannot be freed by the collector). These objects will be added to\nthe garbage list.
\n\n
\n
\ngc.DEBUG_INSTANCES
\n
When DEBUG_COLLECTABLE or DEBUG_UNCOLLECTABLE is set, print\ninformation about instance objects found.
\n\n
\n
\ngc.DEBUG_OBJECTS
\n
When DEBUG_COLLECTABLE or DEBUG_UNCOLLECTABLE is set, print\ninformation about objects other than instance objects found.
\n\n
\n
\ngc.DEBUG_SAVEALL
\n
When set, all unreachable objects found will be appended to garbage rather\nthan being freed. This can be useful for debugging a leaking program.
\n\n
\n
\ngc.DEBUG_LEAK
\n
The debugging flags necessary for the collector to print information about a\nleaking program (equal to DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |\nDEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL).
\n\n

Footnotes

\n\n\n\n\n\n
[1]Prior to Python 2.2, the list contained all instance objects in unreachable\ncycles, not only those with __del__() methods.
\n
", "searchableItems": [ { "name": "gc.collect", "domId": "gc_gc.collect" }, { "name": "gc.disable", "domId": "gc_gc.disable" }, { "name": "gc.enable", "domId": "gc_gc.enable" }, { "name": "gc.get_count", "domId": "gc_gc.get_count" }, { "name": "gc.get_debug", "domId": "gc_gc.get_debug" }, { "name": "gc.get_objects", "domId": "gc_gc.get_objects" }, { "name": "gc.get_referents", "domId": "gc_gc.get_referents" }, { "name": "gc.get_referrers", "domId": "gc_gc.get_referrers" }, { "name": "gc.get_threshold", "domId": "gc_gc.get_threshold" }, { "name": "gc.is_tracked", "domId": "gc_gc.is_tracked" }, { "name": "gc.isenabled", "domId": "gc_gc.isenabled" }, { "name": "gc.set_debug", "domId": "gc_gc.set_debug" }, { "name": "gc.set_threshold", "domId": "gc_gc.set_threshold" } ] }, { "url": "http://docs.python.org/library/user.html", "title": "user", "html": "
\n

27.15. user — User-specific configuration hook

\n

\nDeprecated since version 2.6: The user module has been removed in Python 3.0.

\n

As a policy, Python doesn’t run user-specified code on startup of Python\nprograms. (Only interactive sessions execute the script specified in the\nPYTHONSTARTUP environment variable if it exists).

\n

However, some programs or sites may find it convenient to allow users to have a\nstandard customization file, which gets run when a program requests it. This\nmodule implements such a mechanism. A program that wishes to use the mechanism\nmust execute the statement

\n
import user\n
\n
\n

The user module looks for a file .pythonrc.py in the user’s home\ndirectory and if it can be opened, executes it (using execfile()) in its\nown (the module user‘s) global namespace. Errors during this phase are\nnot caught; that’s up to the program that imports the user module, if it\nwishes. The home directory is assumed to be named by the HOME\nenvironment variable; if this is not set, the current directory is used.

\n

The user’s .pythonrc.py could conceivably test for sys.version if it\nwishes to do different things depending on the Python version.

\n

A warning to users: be very conservative in what you place in your\n.pythonrc.py file. Since you don’t know which programs will use it,\nchanging the behavior of standard modules or functions is generally not a good\nidea.

\n

A suggestion for programmers who wish to use this mechanism: a simple way to let\nusers specify options for your package is to have them define variables in their\n.pythonrc.py file that you test in your module. For example, a module\nspam that has a verbosity level can look for a variable\nuser.spam_verbose, as follows:

\n
import user\n\nverbose = bool(getattr(user, "spam_verbose", 0))\n
\n
\n

(The three-argument form of getattr() is used in case the user has not\ndefined spam_verbose in their .pythonrc.py file.)

\n

Programs with extensive customization needs are better off reading a\nprogram-specific customization file.

\n

Programs with security or privacy concerns should not import this module; a\nuser can easily break into a program by placing arbitrary code in the\n.pythonrc.py file.

\n

Modules for general use should not import this module; it may interfere with\nthe operation of the importing program.

\n
\n

See also

\n
\n
Module site
\n
Site-wide customization mechanism.
\n
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/site.html", "title": "site", "html": "
\n

27.14. site — Site-specific configuration hook

\n

Source code: Lib/site.py

\n
\n

This module is automatically imported during initialization. The automatic\nimport can be suppressed using the interpreter’s -S option.

\n

Importing this module will append site-specific paths to the module search path\nand add a few builtins.

\n

It starts by constructing up to four directories from a head and a tail part.\nFor the head part, it uses sys.prefix and sys.exec_prefix; empty heads\nare skipped. For the tail part, it uses the empty string and then\nlib/site-packages (on Windows) or\nlib/python|version|/site-packages and then lib/site-python (on\nUnix and Macintosh). For each of the distinct head-tail combinations, it sees\nif it refers to an existing directory, and if so, adds it to sys.path and\nalso inspects the newly added path for configuration files.

\n

A path configuration file is a file whose name has the form name.pth\nand exists in one of the four directories mentioned above; its contents are\nadditional items (one per line) to be added to sys.path. Non-existing items\nare never added to sys.path, and no check is made that the item refers to a\ndirectory rather than a file. No item is added to sys.path more than\nonce. Blank lines and lines beginning with # are skipped. Lines starting\nwith import (followed by space or tab) are executed.

\n

\nChanged in version 2.6: A space or tab is now required after the import keyword.

\n

For example, suppose sys.prefix and sys.exec_prefix are set to\n/usr/local. The Python X.Y library is then installed in\n/usr/local/lib/pythonX.Y. Suppose this has\na subdirectory /usr/local/lib/pythonX.Y/site-packages with three\nsubsubdirectories, foo, bar and spam, and two path\nconfiguration files, foo.pth and bar.pth. Assume\nfoo.pth contains the following:

\n
# foo package configuration\n\nfoo\nbar\nbletch\n
\n
\n

and bar.pth contains:

\n
# bar package configuration\n\nbar\n
\n
\n

Then the following version-specific directories are added to\nsys.path, in this order:

\n
/usr/local/lib/pythonX.Y/site-packages/bar\n/usr/local/lib/pythonX.Y/site-packages/foo\n
\n
\n

Note that bletch is omitted because it doesn’t exist; the bar\ndirectory precedes the foo directory because bar.pth comes\nalphabetically before foo.pth; and spam is omitted because it is\nnot mentioned in either path configuration file.

\n

After these path manipulations, an attempt is made to import a module named\nsitecustomize, which can perform arbitrary site-specific customizations.\nIt is typically created by a system administrator in the site-packages\ndirectory. If this import fails with an ImportError exception, it is\nsilently ignored.

\n

After this, an attempt is made to import a module named usercustomize,\nwhich can perform arbitrary user-specific customizations, if\nENABLE_USER_SITE is true. This file is intended to be created in the\nuser site-packages directory (see below), which is part of sys.path unless\ndisabled by -s. An ImportError will be silently ignored.

\n

Note that for some non-Unix systems, sys.prefix and sys.exec_prefix are\nempty, and the path manipulations are skipped; however the import of\nsitecustomize and usercustomize is still attempted.

\n
\n
\nsite.PREFIXES
\n

A list of prefixes for site-packages directories.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsite.ENABLE_USER_SITE
\n

Flag showing the status of the user site-packages directory. True means\nthat it is enabled and was added to sys.path. False means that it\nwas disabled by user request (with -s or\nPYTHONNOUSERSITE). None means it was disabled for security\nreasons (mismatch between user or group id and effective id) or by an\nadministrator.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsite.USER_SITE
\n

Path to the user site-packages for the running Python. Can be None if\ngetusersitepackages() hasn’t been called yet. Default value is\n~/.local/lib/pythonX.Y/site-packages for UNIX and non-framework Mac\nOS X builds, ~/Library/Python/X.Y/lib/python/site-packages for Mac\nframework builds, and \\Python\\PythonXY\\site-packages\non Windows. This directory is a site directory, which means that\n.pth files in it will be processed.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsite.USER_BASE
\n

Path to the base directory for the user site-packages. Can be None if\ngetuserbase() hasn’t been called yet. Default value is\n~/.local for UNIX and Mac OS X non-framework builds,\n~/Library/Python/X.Y for Mac framework builds, and\n\\Python for Windows. This value is used by Distutils to\ncompute the installation directories for scripts, data files, Python modules,\netc. for the user installation scheme. See\nalso PYTHONUSERBASE.

\n

\nNew in version 2.6.

\n
\n\n
\n
\nsite.addsitedir(sitedir, known_paths=None)
\n
Add a directory to sys.path and process its .pth files. Typically\nused in sitecustomize or usercustomize (see above).
\n\n
\n
\nsite.getsitepackages()
\n

Return a list containing all global site-packages directories (and possibly\nsite-python).

\n

\nNew in version 2.7.

\n
\n\n
\n
\nsite.getuserbase()
\n

Return the path of the user base directory, USER_BASE. If it is not\ninitialized yet, this function will also set it, respecting\nPYTHONUSERBASE.

\n

\nNew in version 2.7.

\n
\n\n
\n
\nsite.getusersitepackages()
\n

Return the path of the user-specific site-packages directory,\nUSER_SITE. If it is not initialized yet, this function will also set\nit, respecting PYTHONNOUSERSITE and USER_BASE.

\n

\nNew in version 2.7.

\n
\n\n

The site module also provides a way to get the user directories from the\ncommand line:

\n
$ python3 -m site --user-site\n/home/user/.local/lib/python3.3/site-packages\n
\n
\n

If it is called without arguments, it will print the contents of\nsys.path on the standard output, followed by the value of\nUSER_BASE and whether the directory exists, then the same thing for\nUSER_SITE, and finally the value of ENABLE_USER_SITE.

\n
\n
\n--user-base
\n
Print the path to the user base directory.
\n\n
\n
\n--user-site
\n
Print the path to the user site-packages directory.
\n\n

If both options are given, user base and user site will be printed (always in\nthis order), separated by os.pathsep.

\n

If any option is given, the script will exit with one of these values: O if\nthe user site-packages directory is enabled, 1 if it was disabled by the\nuser, 2 if it is disabled for security reasons or by an administrator, and a\nvalue greater than 2 if there is an error.

\n
\n

See also

\n

PEP 370 – Per user site-packages directory

\n
\n
", "searchableItems": [ { "name": "site.addsitedir", "domId": "site_site.addsitedir" }, { "name": "site.getsitepackages", "domId": "site_site.getsitepackages" }, { "name": "site.getuserbase", "domId": "site_site.getuserbase" }, { "name": "site.getusersitepackages", "domId": "site_site.getusersitepackages" } ] }, { "url": "http://docs.python.org/library/distutils.html", "title": "distutils", "html": "
\n

27.17. distutils — Building and installing Python modules

\n

The distutils package provides support for building and installing\nadditional modules into a Python installation. The new modules may be either\n100%-pure Python, or may be extension modules written in C, or may be\ncollections of Python packages which include modules coded in both Python and C.

\n

This package is discussed in two separate chapters:

\n
\n

See also

\n
\n
Distributing Python Modules
\n
The manual for developers and packagers of Python modules. This describes how\nto prepare distutils-based packages so that they may be easily\ninstalled into an existing Python installation.
\n
Installing Python Modules
\n
An “administrators” manual which includes information on installing modules into\nan existing Python installation. You do not need to be a Python programmer to\nread this manual.
\n
\n
\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/fpectl.html", "title": "fpectl", "html": "
\n

27.16. fpectl — Floating point exception control

\n

Platforms: Unix

\n
\n

Note

\n

The fpectl module is not built by default, and its usage is discouraged\nand may be dangerous except in the hands of experts. See also the section\nLimitations and other considerations on limitations for more details.

\n
\n

Most computers carry out floating point operations in conformance with the\nso-called IEEE-754 standard. On any real computer, some floating point\noperations produce results that cannot be expressed as a normal floating point\nvalue. For example, try

\n
>>> import math\n>>> math.exp(1000)\ninf\n>>> math.exp(1000) / math.exp(1000)\nnan\n
\n
\n

(The example above will work on many platforms. DEC Alpha may be one exception.)\n“Inf” is a special, non-numeric value in IEEE-754 that stands for “infinity”,\nand “nan” means “not a number.” Note that, other than the non-numeric results,\nnothing special happened when you asked Python to carry out those calculations.\nThat is in fact the default behaviour prescribed in the IEEE-754 standard, and\nif it works for you, stop reading now.

\n

In some circumstances, it would be better to raise an exception and stop\nprocessing at the point where the faulty operation was attempted. The\nfpectl module is for use in that situation. It provides control over\nfloating point units from several hardware manufacturers, allowing the user to\nturn on the generation of SIGFPE whenever any of the IEEE-754\nexceptions Division by Zero, Overflow, or Invalid Operation occurs. In tandem\nwith a pair of wrapper macros that are inserted into the C code comprising your\npython system, SIGFPE is trapped and converted into the Python\nFloatingPointError exception.

\n

The fpectl module defines the following functions and may raise the given\nexception:

\n
\n
\nfpectl.turnon_sigfpe()
\n
Turn on the generation of SIGFPE, and set up an appropriate signal\nhandler.
\n\n
\n
\nfpectl.turnoff_sigfpe()
\n
Reset default handling of floating point exceptions.
\n\n
\n
\nexception fpectl.FloatingPointError
\n
After turnon_sigfpe() has been executed, a floating point operation that\nraises one of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid\noperation will in turn raise this standard Python exception.
\n\n
\n

27.16.1. Example

\n

The following example demonstrates how to start up and test operation of the\nfpectl module.

\n
>>> import fpectl\n>>> import fpetest\n>>> fpectl.turnon_sigfpe()\n>>> fpetest.test()\noverflow        PASS\nFloatingPointError: Overflow\n\ndiv by 0        PASS\nFloatingPointError: Division by zero\n  [ more output from test elided ]\n>>> import math\n>>> math.exp(1000)\nTraceback (most recent call last):\n  File "<stdin>", line 1, in ?\nFloatingPointError: in math_1\n
\n
\n
\n
\n

27.16.2. Limitations and other considerations

\n

Setting up a given processor to trap IEEE-754 floating point errors currently\nrequires custom code on a per-architecture basis. You may have to modify\nfpectl to control your particular hardware.

\n

Conversion of an IEEE-754 exception to a Python exception requires that the\nwrapper macros PyFPE_START_PROTECT and PyFPE_END_PROTECT be inserted\ninto your code in an appropriate fashion. Python itself has been modified to\nsupport the fpectl module, but many other codes of interest to numerical\nanalysts have not.

\n

The fpectl module is not thread-safe.

\n
\n

See also

\n

Some files in the source distribution may be interesting in learning more about\nhow this module operates. The include file Include/pyfpe.h discusses the\nimplementation of this module at some length. Modules/fpetestmodule.c\ngives several examples of use. Many additional examples can be found in\nObjects/floatobject.c.

\n
\n
\n
", "searchableItems": [ { "name": "fpectl.turnoff_sigfpe", "domId": "fpectl_fpectl.turnoff_sigfpe" }, { "name": "fpectl.turnon_sigfpe", "domId": "fpectl_fpectl.turnon_sigfpe" } ] }, { "url": "http://docs.python.org/library/bastion.html", "title": "Bastion", "html": "
\n

29.2. Bastion — Restricting access to objects

\n

\nDeprecated since version 2.6: The Bastion module has been removed in Python 3.0.

\n

\nChanged in version 2.3: Disabled module.

\n
\n

Note

\n

The documentation has been left in place to help in reading old code that uses\nthe module.

\n
\n

According to the dictionary, a bastion is “a fortified area or position”, or\n“something that is considered a stronghold.” It’s a suitable name for this\nmodule, which provides a way to forbid access to certain attributes of an\nobject. It must always be used with the rexec module, in order to allow\nrestricted-mode programs access to certain safe attributes of an object, while\ndenying access to other, unsafe attributes.

\n
\n
\nBastion.Bastion(object[, filter[, name[, class]]])
\n

Protect the object object, returning a bastion for the object. Any attempt to\naccess one of the object’s attributes will have to be approved by the filter\nfunction; if the access is denied an AttributeError exception will be\nraised.

\n

If present, filter must be a function that accepts a string containing an\nattribute name, and returns true if access to that attribute will be permitted;\nif filter returns false, the access is denied. The default filter denies\naccess to any function beginning with an underscore ('_'). The bastion’s\nstring representation will be <Bastion for name> if a value for name is\nprovided; otherwise, repr(object) will be used.

\n

class, if present, should be a subclass of BastionClass; see the\ncode in bastion.py for the details. Overriding the default\nBastionClass will rarely be required.

\n
\n\n
\n
\nclass Bastion.BastionClass(getfunc, name)
\n
Class which actually implements bastion objects. This is the default class used\nby Bastion(). The getfunc parameter is a function which returns the\nvalue of an attribute which should be exposed to the restricted execution\nenvironment when called with the name of the attribute as the only parameter.\nname is used to construct the repr() of the BastionClass\ninstance.
\n\n
", "searchableItems": [ { "name": "Bastion.Bastion", "domId": "Bastion_Bastion.Bastion" }, { "name": "Bastion.BastionClass", "domId": "Bastion_Bastion.BastionClass" } ] }, { "url": "http://docs.python.org/library/codeop.html", "title": "codeop", "html": "
\n

28.2. codeop — Compile Python code

\n

The codeop module provides utilities upon which the Python\nread-eval-print loop can be emulated, as is done in the code module. As\na result, you probably don’t want to use the module directly; if you want to\ninclude such a loop in your program you probably want to use the code\nmodule instead.

\n

There are two parts to this job:

\n
    \n
  1. Being able to tell if a line of input completes a Python statement: in\nshort, telling whether to print ‘>>>‘ or ‘...‘ next.
  2. \n
  3. Remembering which future statements the user has entered, so subsequent\ninput can be compiled with these in effect.
  4. \n
\n

The codeop module provides a way of doing each of these things, and a way\nof doing them both.

\n

To do just the former:

\n
\n
\ncodeop.compile_command(source[, filename[, symbol]])
\n

Tries to compile source, which should be a string of Python code and return a\ncode object if source is valid Python code. In that case, the filename\nattribute of the code object will be filename, which defaults to\n'<input>'. Returns None if source is not valid Python code, but is a\nprefix of valid Python code.

\n

If there is a problem with source, an exception will be raised.\nSyntaxError is raised if there is invalid Python syntax, and\nOverflowError or ValueError if there is an invalid literal.

\n

The symbol argument determines whether source is compiled as a statement\n('single', the default) or as an expression ('eval'). Any\nother value will cause ValueError to be raised.

\n
\n

Note

\n

It is possible (but not likely) that the parser stops parsing with a\nsuccessful outcome before reaching the end of the source; in this case,\ntrailing symbols may be ignored instead of causing an error. For example,\na backslash followed by two newlines may be followed by arbitrary garbage.\nThis will be fixed once the API for the parser is better.

\n
\n
\n\n
\n
\nclass codeop.Compile
\n
Instances of this class have __call__() methods identical in signature to\nthe built-in function compile(), but with the difference that if the\ninstance compiles program text containing a __future__ statement, the\ninstance ‘remembers’ and compiles all subsequent program texts with the\nstatement in force.
\n\n
\n
\nclass codeop.CommandCompiler
\n
Instances of this class have __call__() methods identical in signature to\ncompile_command(); the difference is that if the instance compiles program\ntext containing a __future__ statement, the instance ‘remembers’ and\ncompiles all subsequent program texts with the statement in force.
\n\n

A note on version compatibility: the Compile and\nCommandCompiler are new in Python 2.2. If you want to enable the\nfuture-tracking features of 2.2 but also retain compatibility with 2.1 and\nearlier versions of Python you can either write

\n
try:\n    from codeop import CommandCompiler\n    compile_command = CommandCompiler()\n    del CommandCompiler\nexcept ImportError:\n    from codeop import compile_command\n
\n
\n

which is a low-impact change, but introduces possibly unwanted global state into\nyour program, or you can write:

\n
try:\n    from codeop import CommandCompiler\nexcept ImportError:\n    def CommandCompiler():\n        from codeop import compile_command\n        return compile_command\n
\n
\n

and then call CommandCompiler every time you need a fresh compiler object.

\n
", "searchableItems": [ { "name": "codeop.CommandCompiler", "domId": "codeop_codeop.CommandCompiler" }, { "name": "codeop.Compile", "domId": "codeop_codeop.Compile" }, { "name": "codeop.compile_command", "domId": "codeop_codeop.compile_command" } ] }, { "url": "http://docs.python.org/library/code.html", "title": "code", "html": "
\n

28.1. code — Interpreter base classes

\n

The code module provides facilities to implement read-eval-print loops in\nPython. Two classes and convenience functions are included which can be used to\nbuild applications which provide an interactive interpreter prompt.

\n
\n
\nclass code.InteractiveInterpreter([locals])
\n
This class deals with parsing and interpreter state (the user’s namespace); it\ndoes not deal with input buffering or prompting or input file naming (the\nfilename is always passed in explicitly). The optional locals argument\nspecifies the dictionary in which code will be executed; it defaults to a newly\ncreated dictionary with key '__name__' set to '__console__' and key\n'__doc__' set to None.
\n\n
\n
\nclass code.InteractiveConsole([locals[, filename]])
\n
Closely emulate the behavior of the interactive Python interpreter. This class\nbuilds on InteractiveInterpreter and adds prompting using the familiar\nsys.ps1 and sys.ps2, and input buffering.
\n\n
\n
\ncode.interact([banner[, readfunc[, local]]])
\n
Convenience function to run a read-eval-print loop. This creates a new instance\nof InteractiveConsole and sets readfunc to be used as the\nraw_input() method, if provided. If local is provided, it is passed to\nthe InteractiveConsole constructor for use as the default namespace for\nthe interpreter loop. The interact() method of the instance is then run\nwith banner passed as the banner to use, if provided. The console object is\ndiscarded after use.
\n\n
\n
\ncode.compile_command(source[, filename[, symbol]])
\n

This function is useful for programs that want to emulate Python’s interpreter\nmain loop (a.k.a. the read-eval-print loop). The tricky part is to determine\nwhen the user has entered an incomplete command that can be completed by\nentering more text (as opposed to a complete command or a syntax error). This\nfunction almost always makes the same decision as the real interpreter main\nloop.

\n

source is the source string; filename is the optional filename from which\nsource was read, defaulting to '<input>'; and symbol is the optional\ngrammar start symbol, which should be either 'single' (the default) or\n'eval'.

\n

Returns a code object (the same as compile(source, filename, symbol)) if the\ncommand is complete and valid; None if the command is incomplete; raises\nSyntaxError if the command is complete and contains a syntax error, or\nraises OverflowError or ValueError if the command contains an\ninvalid literal.

\n
\n\n
\n

28.1.1. Interactive Interpreter Objects

\n
\n
\nInteractiveInterpreter.runsource(source[, filename[, symbol]])
\n

Compile and run some source in the interpreter. Arguments are the same as for\ncompile_command(); the default for filename is '<input>', and for\nsymbol is 'single'. One several things can happen:

\n\n

The return value can be used to decide whether to use sys.ps1 or sys.ps2\nto prompt the next line.

\n
\n\n
\n
\nInteractiveInterpreter.runcode(code)
\n

Execute a code object. When an exception occurs, showtraceback() is called\nto display a traceback. All exceptions are caught except SystemExit,\nwhich is allowed to propagate.

\n

A note about KeyboardInterrupt: this exception may occur elsewhere in\nthis code, and may not always be caught. The caller should be prepared to deal\nwith it.

\n
\n\n
\n
\nInteractiveInterpreter.showsyntaxerror([filename])
\n
Display the syntax error that just occurred. This does not display a stack\ntrace because there isn’t one for syntax errors. If filename is given, it is\nstuffed into the exception instead of the default filename provided by Python’s\nparser, because it always uses '<string>' when reading from a string. The\noutput is written by the write() method.
\n\n
\n
\nInteractiveInterpreter.showtraceback()
\n
Display the exception that just occurred. We remove the first stack item\nbecause it is within the interpreter object implementation. The output is\nwritten by the write() method.
\n\n
\n
\nInteractiveInterpreter.write(data)
\n
Write a string to the standard error stream (sys.stderr). Derived classes\nshould override this to provide the appropriate output handling as needed.
\n\n
\n
\n

28.1.2. Interactive Console Objects

\n

The InteractiveConsole class is a subclass of\nInteractiveInterpreter, and so offers all the methods of the\ninterpreter objects as well as the following additions.

\n
\n
\nInteractiveConsole.interact([banner])
\n
Closely emulate the interactive Python console. The optional banner argument\nspecify the banner to print before the first interaction; by default it prints a\nbanner similar to the one printed by the standard Python interpreter, followed\nby the class name of the console object in parentheses (so as not to confuse\nthis with the real interpreter – since it’s so close!).
\n\n
\n
\nInteractiveConsole.push(line)
\n
Push a line of source text to the interpreter. The line should not have a\ntrailing newline; it may have internal newlines. The line is appended to a\nbuffer and the interpreter’s runsource() method is called with the\nconcatenated contents of the buffer as source. If this indicates that the\ncommand was executed or invalid, the buffer is reset; otherwise, the command is\nincomplete, and the buffer is left as it was after the line was appended. The\nreturn value is True if more input is required, False if the line was\ndealt with in some way (this is the same as runsource()).
\n\n
\n
\nInteractiveConsole.resetbuffer()
\n
Remove any unhandled source text from the input buffer.
\n\n
\n
\nInteractiveConsole.raw_input([prompt])
\n
Write a prompt and read a line. The returned line does not include the trailing\nnewline. When the user enters the EOF key sequence, EOFError is raised.\nThe base implementation uses the built-in function raw_input(); a subclass\nmay replace this with a different implementation.
\n\n
\n
", "searchableItems": [ { "name": "code.compile_command", "domId": "code_code.compile_command" }, { "name": "code.interact", "domId": "code_code.interact" }, { "name": "code.InteractiveConsole", "domId": "code_code.InteractiveConsole" }, { "name": "code.InteractiveConsole.interact", "domId": "code_code.InteractiveConsole.interact" }, { "name": "code.InteractiveConsole.push", "domId": "code_code.InteractiveConsole.push" }, { "name": "code.InteractiveConsole.raw_input", "domId": "code_code.InteractiveConsole.raw_input" }, { "name": "code.InteractiveConsole.resetbuffer", "domId": "code_code.InteractiveConsole.resetbuffer" }, { "name": "code.InteractiveInterpreter", "domId": "code_code.InteractiveInterpreter" }, { "name": "code.InteractiveInterpreter.runcode", "domId": "code_code.InteractiveInterpreter.runcode" }, { "name": "code.InteractiveInterpreter.runsource", "domId": "code_code.InteractiveInterpreter.runsource" }, { "name": "code.InteractiveInterpreter.showsyntaxerror", "domId": "code_code.InteractiveInterpreter.showsyntaxerror" }, { "name": "code.InteractiveInterpreter.showtraceback", "domId": "code_code.InteractiveInterpreter.showtraceback" }, { "name": "code.InteractiveInterpreter.write", "domId": "code_code.InteractiveInterpreter.write" } ] }, { "url": "http://docs.python.org/library/inspect.html", "title": "inspect", "html": "
\n

27.13. inspect — Inspect live objects

\n

\nNew in version 2.1.

\n

Source code: Lib/inspect.py

\n
\n

The inspect module provides several useful functions to help get\ninformation about live objects such as modules, classes, methods, functions,\ntracebacks, frame objects, and code objects. For example, it can help you\nexamine the contents of a class, retrieve the source code of a method, extract\nand format the argument list for a function, or get all the information you need\nto display a detailed traceback.

\n

There are four main kinds of services provided by this module: type checking,\ngetting source code, inspecting classes and functions, and examining the\ninterpreter stack.

\n
\n

27.13.1. Types and members

\n

The getmembers() function retrieves the members of an object such as a\nclass or module. The sixteen functions whose names begin with “is” are mainly\nprovided as convenient choices for the second argument to getmembers().\nThey also help you determine when you can expect to find the following special\nattributes:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
TypeAttributeDescriptionNotes
module__doc__documentation string 
 __file__filename (missing for\nbuilt-in modules) 
class__doc__documentation string 
 __module__name of module in which\nthis class was defined 
method__doc__documentation string 
 __name__name with which this\nmethod was defined 
 im_classclass object that asked\nfor this method(1)
 im_func or\n__func__function object\ncontaining implementation\nof method 
 im_self or\n__self__instance to which this\nmethod is bound, or\nNone 
function__doc__documentation string 
 __name__name with which this\nfunction was defined 
 func_codecode object containing\ncompiled function\nbytecode 
 func_defaultstuple of any default\nvalues for arguments 
 func_doc(same as __doc__) 
 func_globalsglobal namespace in which\nthis function was defined 
 func_name(same as __name__) 
generator__iter__defined to support\niteration over container 
 closeraises new GeneratorExit\nexception inside the\ngenerator to terminate\nthe iteration 
 gi_codecode object 
 gi_frameframe object or possibly\nNone once the generator\nhas been exhausted 
 gi_runningset to 1 when generator\nis executing, 0 otherwise 
 nextreturn the next item from\nthe container 
 sendresumes the generator and\n“sends” a value that\nbecomes the result of the\ncurrent yield-expression 
 throwused to raise an\nexception inside the\ngenerator 
tracebacktb_frameframe object at this\nlevel 
 tb_lastiindex of last attempted\ninstruction in bytecode 
 tb_linenocurrent line number in\nPython source code 
 tb_nextnext inner traceback\nobject (called by this\nlevel) 
framef_backnext outer frame object\n(this frame’s caller) 
 f_builtinsbuiltins namespace seen\nby this frame 
 f_codecode object being\nexecuted in this frame 
 f_exc_tracebacktraceback if raised in\nthis frame, or None 
 f_exc_typeexception type if raised\nin this frame, or\nNone 
 f_exc_valueexception value if raised\nin this frame, or\nNone 
 f_globalsglobal namespace seen by\nthis frame 
 f_lastiindex of last attempted\ninstruction in bytecode 
 f_linenocurrent line number in\nPython source code 
 f_localslocal namespace seen by\nthis frame 
 f_restricted0 or 1 if frame is in\nrestricted execution mode 
 f_tracetracing function for this\nframe, or None 
codeco_argcountnumber of arguments (not\nincluding * or **\nargs) 
 co_codestring of raw compiled\nbytecode 
 co_conststuple of constants used\nin the bytecode 
 co_filenamename of file in which\nthis code object was\ncreated 
 co_firstlinenonumber of first line in\nPython source code 
 co_flagsbitmap: 1=optimized |\n2=newlocals | 4=*arg\n| 8=**arg 
 co_lnotabencoded mapping of line\nnumbers to bytecode\nindices 
 co_namename with which this code\nobject was defined 
 co_namestuple of names of local\nvariables 
 co_nlocalsnumber of local variables 
 co_stacksizevirtual machine stack\nspace required 
 co_varnamestuple of names of\narguments and local\nvariables 
builtin__doc__documentation string 
 __name__original name of this\nfunction or method 
 __self__instance to which a\nmethod is bound, or\nNone 
\n

Note:

\n
    \n
  1. \nChanged in version 2.2: im_class used to refer to the class that defined the method.

    \n
  2. \n
\n
\n
\ninspect.getmembers(object[, predicate])
\n

Return all the members of an object in a list of (name, value) pairs sorted by\nname. If the optional predicate argument is supplied, only members for which\nthe predicate returns a true value are included.

\n
\n

Note

\n

getmembers() does not return metaclass attributes when the argument\nis a class (this behavior is inherited from the dir() function).

\n
\n
\n\n
\n
\ninspect.getmoduleinfo(path)
\n

Return a tuple of values that describe how Python will interpret the file\nidentified by path if it is a module, or None if it would not be\nidentified as a module. The return tuple is (name, suffix, mode,\nmodule_type), where name is the name of the module without the name of\nany enclosing package, suffix is the trailing part of the file name (which\nmay not be a dot-delimited extension), mode is the open() mode that\nwould be used ('r' or 'rb'), and module_type is an integer giving\nthe type of the module. module_type will have a value which can be\ncompared to the constants defined in the imp module; see the\ndocumentation for that module for more information on module types.

\n

\nChanged in version 2.6: Returns a named tuple ModuleInfo(name, suffix, mode,\nmodule_type).

\n
\n\n
\n
\ninspect.getmodulename(path)
\n
Return the name of the module named by the file path, without including the\nnames of enclosing packages. This uses the same algorithm as the interpreter\nuses when searching for modules. If the name cannot be matched according to the\ninterpreter’s rules, None is returned.
\n\n
\n
\ninspect.ismodule(object)
\n
Return true if the object is a module.
\n\n
\n
\ninspect.isclass(object)
\n
Return true if the object is a class, whether built-in or created in Python\ncode.
\n\n
\n
\ninspect.ismethod(object)
\n
Return true if the object is a bound method written in Python.
\n\n
\n
\ninspect.isfunction(object)
\n
Return true if the object is a Python function, which includes functions\ncreated by a lambda expression.
\n\n
\n
\ninspect.isgeneratorfunction(object)
\n

Return true if the object is a Python generator function.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ninspect.isgenerator(object)
\n

Return true if the object is a generator.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ninspect.istraceback(object)
\n
Return true if the object is a traceback.
\n\n
\n
\ninspect.isframe(object)
\n
Return true if the object is a frame.
\n\n
\n
\ninspect.iscode(object)
\n
Return true if the object is a code.
\n\n
\n
\ninspect.isbuiltin(object)
\n
Return true if the object is a built-in function or a bound built-in method.
\n\n
\n
\ninspect.isroutine(object)
\n
Return true if the object is a user-defined or built-in function or method.
\n\n
\n
\ninspect.isabstract(object)
\n

Return true if the object is an abstract base class.

\n

\nNew in version 2.6.

\n
\n\n
\n
\ninspect.ismethoddescriptor(object)
\n

Return true if the object is a method descriptor, but not if\nismethod(), isclass(), isfunction() or isbuiltin()\nare true.

\n

This is new as of Python 2.2, and, for example, is true of\nint.__add__. An object passing this test has a __get__ attribute\nbut not a __set__ attribute, but beyond that the set of attributes\nvaries. __name__ is usually sensible, and __doc__ often is.

\n

Methods implemented via descriptors that also pass one of the other tests\nreturn false from the ismethoddescriptor() test, simply because the\nother tests promise more – you can, e.g., count on having the\nim_func attribute (etc) when an object passes ismethod().

\n
\n\n
\n
\ninspect.isdatadescriptor(object)
\n

Return true if the object is a data descriptor.

\n

Data descriptors have both a __get__ and a __set__ attribute.\nExamples are properties (defined in Python), getsets, and members. The\nlatter two are defined in C and there are more specific tests available for\nthose types, which is robust across Python implementations. Typically, data\ndescriptors will also have __name__ and __doc__ attributes\n(properties, getsets, and members have both of these attributes), but this is\nnot guaranteed.

\n

\nNew in version 2.3.

\n
\n\n
\n
\ninspect.isgetsetdescriptor(object)
\n

Return true if the object is a getset descriptor.

\n
\n

CPython implementation detail: getsets are attributes defined in extension modules via\nPyGetSetDef structures. For Python implementations without such\ntypes, this method will always return False.

\n
\n

\nNew in version 2.5.

\n
\n\n
\n
\ninspect.ismemberdescriptor(object)
\n

Return true if the object is a member descriptor.

\n
\n

CPython implementation detail: Member descriptors are attributes defined in extension modules via\nPyMemberDef structures. For Python implementations without such\ntypes, this method will always return False.

\n
\n

\nNew in version 2.5.

\n
\n\n
\n
\n

27.13.2. Retrieving source code

\n
\n
\ninspect.getdoc(object)
\n
Get the documentation string for an object, cleaned up with cleandoc().
\n\n
\n
\ninspect.getcomments(object)
\n
Return in a single string any lines of comments immediately preceding the\nobject’s source code (for a class, function, or method), or at the top of the\nPython source file (if the object is a module).
\n\n
\n
\ninspect.getfile(object)
\n
Return the name of the (text or binary) file in which an object was defined.\nThis will fail with a TypeError if the object is a built-in module,\nclass, or function.
\n\n
\n
\ninspect.getmodule(object)
\n
Try to guess which module an object was defined in.
\n\n
\n
\ninspect.getsourcefile(object)
\n
Return the name of the Python source file in which an object was defined. This\nwill fail with a TypeError if the object is a built-in module, class, or\nfunction.
\n\n
\n
\ninspect.getsourcelines(object)
\n
Return a list of source lines and starting line number for an object. The\nargument may be a module, class, method, function, traceback, frame, or code\nobject. The source code is returned as a list of the lines corresponding to the\nobject and the line number indicates where in the original source file the first\nline of code was found. An IOError is raised if the source code cannot\nbe retrieved.
\n\n
\n
\ninspect.getsource(object)
\n
Return the text of the source code for an object. The argument may be a module,\nclass, method, function, traceback, frame, or code object. The source code is\nreturned as a single string. An IOError is raised if the source code\ncannot be retrieved.
\n\n
\n
\ninspect.cleandoc(doc)
\n

Clean up indentation from docstrings that are indented to line up with blocks\nof code. Any whitespace that can be uniformly removed from the second line\nonwards is removed. Also, all tabs are expanded to spaces.

\n

\nNew in version 2.6.

\n
\n\n
\n
\n

27.13.3. Classes and functions

\n
\n
\ninspect.getclasstree(classes[, unique])
\n
Arrange the given list of classes into a hierarchy of nested lists. Where a\nnested list appears, it contains classes derived from the class whose entry\nimmediately precedes the list. Each entry is a 2-tuple containing a class and a\ntuple of its base classes. If the unique argument is true, exactly one entry\nappears in the returned structure for each class in the given list. Otherwise,\nclasses using multiple inheritance and their descendants will appear multiple\ntimes.
\n\n
\n
\ninspect.getargspec(func)
\n

Get the names and default values of a Python function’s arguments. A tuple of\nfour things is returned: (args, varargs, keywords, defaults). args is a\nlist of the argument names (it may contain nested lists). varargs and\nkeywords are the names of the * and ** arguments or\nNone. defaults is a tuple of default argument values or None if there\nare no default arguments; if this tuple has n elements, they correspond to\nthe last n elements listed in args.

\n

\nChanged in version 2.6: Returns a named tuple ArgSpec(args, varargs, keywords,\ndefaults).

\n
\n\n
\n
\ninspect.getargvalues(frame)
\n

Get information about arguments passed into a particular frame. A tuple of\nfour things is returned: (args, varargs, keywords, locals). args is a\nlist of the argument names (it may contain nested lists). varargs and\nkeywords are the names of the * and ** arguments or None.\nlocals is the locals dictionary of the given frame.

\n

\nChanged in version 2.6: Returns a named tuple ArgInfo(args, varargs, keywords,\nlocals).

\n
\n\n
\n
\ninspect.formatargspec(args[, varargs, varkw, defaults, formatarg, formatvarargs, formatvarkw, formatvalue, join])
\n
Format a pretty argument spec from the four values returned by\ngetargspec(). The format* arguments are the corresponding optional\nformatting functions that are called to turn names and values into strings.
\n\n
\n
\ninspect.formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue, join])
\n
Format a pretty argument spec from the four values returned by\ngetargvalues(). The format* arguments are the corresponding optional\nformatting functions that are called to turn names and values into strings.
\n\n
\n
\ninspect.getmro(cls)
\n
Return a tuple of class cls’s base classes, including cls, in method resolution\norder. No class appears more than once in this tuple. Note that the method\nresolution order depends on cls’s type. Unless a very peculiar user-defined\nmetatype is in use, cls will be the first element of the tuple.
\n\n
\n
\ninspect.getcallargs(func[, *args][, **kwds])
\n

Bind the args and kwds to the argument names of the Python function or\nmethod func, as if it was called with them. For bound methods, bind also the\nfirst argument (typically named self) to the associated instance. A dict\nis returned, mapping the argument names (including the names of the * and\n** arguments, if any) to their values from args and kwds. In case of\ninvoking func incorrectly, i.e. whenever func(*args, **kwds) would raise\nan exception because of incompatible signature, an exception of the same type\nand the same or similar message is raised. For example:

\n
>>> from inspect import getcallargs\n>>> def f(a, b=1, *pos, **named):\n...     pass\n>>> getcallargs(f, 1, 2, 3)\n{'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}\n>>> getcallargs(f, a=2, x=4)\n{'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}\n>>> getcallargs(f)\nTraceback (most recent call last):\n...\nTypeError: f() takes at least 1 argument (0 given)\n
\n
\n

\nNew in version 2.7.

\n
\n\n
\n
\n

27.13.4. The interpreter stack

\n

When the following functions return “frame records,” each record is a tuple of\nsix items: the frame object, the filename, the line number of the current line,\nthe function name, a list of lines of context from the source code, and the\nindex of the current line within that list.

\n
\n

Note

\n

Keeping references to frame objects, as found in the first element of the frame\nrecords these functions return, can cause your program to create reference\ncycles. Once a reference cycle has been created, the lifespan of all objects\nwhich can be accessed from the objects which form the cycle can become much\nlonger even if Python’s optional cycle detector is enabled. If such cycles must\nbe created, it is important to ensure they are explicitly broken to avoid the\ndelayed destruction of objects and increased memory consumption which occurs.

\n

Though the cycle detector will catch these, destruction of the frames (and local\nvariables) can be made deterministic by removing the cycle in a\nfinally clause. This is also important if the cycle detector was\ndisabled when Python was compiled or using gc.disable(). For example:

\n
def handle_stackframe_without_leak():\n    frame = inspect.currentframe()\n    try:\n        # do something with the frame\n    finally:\n        del frame
\n
\n
\n

The optional context argument supported by most of these functions specifies\nthe number of lines of context to return, which are centered around the current\nline.

\n
\n
\ninspect.getframeinfo(frame[, context])
\n

Get information about a frame or traceback object. A 5-tuple is returned, the\nlast five elements of the frame’s frame record.

\n

\nChanged in version 2.6: Returns a named tuple Traceback(filename, lineno, function,\ncode_context, index).

\n
\n\n
\n
\ninspect.getouterframes(frame[, context])
\n
Get a list of frame records for a frame and all outer frames. These frames\nrepresent the calls that lead to the creation of frame. The first entry in the\nreturned list represents frame; the last entry represents the outermost call\non frame‘s stack.
\n\n
\n
\ninspect.getinnerframes(traceback[, context])
\n
Get a list of frame records for a traceback’s frame and all inner frames. These\nframes represent calls made as a consequence of frame. The first entry in the\nlist represents traceback; the last entry represents where the exception was\nraised.
\n\n
\n
\ninspect.currentframe()
\n

Return the frame object for the caller’s stack frame.

\n
\n

CPython implementation detail: This function relies on Python stack frame support in the interpreter,\nwhich isn’t guaranteed to exist in all implementations of Python. If\nrunning in an implementation without Python stack frame support this\nfunction returns None.

\n
\n
\n\n
\n
\ninspect.stack([context])
\n
Return a list of frame records for the caller’s stack. The first entry in the\nreturned list represents the caller; the last entry represents the outermost\ncall on the stack.
\n\n
\n
\ninspect.trace([context])
\n
Return a list of frame records for the stack between the current frame and the\nframe in which an exception currently being handled was raised in. The first\nentry in the list represents the caller; the last entry represents where the\nexception was raised.
\n\n
\n
", "searchableItems": [ { "name": "inspect.cleandoc", "domId": "inspect_inspect.cleandoc" }, { "name": "inspect.currentframe", "domId": "inspect_inspect.currentframe" }, { "name": "inspect.formatargspec", "domId": "inspect_inspect.formatargspec" }, { "name": "inspect.formatargvalues", "domId": "inspect_inspect.formatargvalues" }, { "name": "inspect.getargspec", "domId": "inspect_inspect.getargspec" }, { "name": "inspect.getargvalues", "domId": "inspect_inspect.getargvalues" }, { "name": "inspect.getcallargs", "domId": "inspect_inspect.getcallargs" }, { "name": "inspect.getclasstree", "domId": "inspect_inspect.getclasstree" }, { "name": "inspect.getcomments", "domId": "inspect_inspect.getcomments" }, { "name": "inspect.getdoc", "domId": "inspect_inspect.getdoc" }, { "name": "inspect.getfile", "domId": "inspect_inspect.getfile" }, { "name": "inspect.getframeinfo", "domId": "inspect_inspect.getframeinfo" }, { "name": "inspect.getinnerframes", "domId": "inspect_inspect.getinnerframes" }, { "name": "inspect.getmembers", "domId": "inspect_inspect.getmembers" }, { "name": "inspect.getmodule", "domId": "inspect_inspect.getmodule" }, { "name": "inspect.getmoduleinfo", "domId": "inspect_inspect.getmoduleinfo" }, { "name": "inspect.getmodulename", "domId": "inspect_inspect.getmodulename" }, { "name": "inspect.getmro", "domId": "inspect_inspect.getmro" }, { "name": "inspect.getouterframes", "domId": "inspect_inspect.getouterframes" }, { "name": "inspect.getsource", "domId": "inspect_inspect.getsource" }, { "name": "inspect.getsourcefile", "domId": "inspect_inspect.getsourcefile" }, { "name": "inspect.getsourcelines", "domId": "inspect_inspect.getsourcelines" }, { "name": "inspect.isabstract", "domId": "inspect_inspect.isabstract" }, { "name": "inspect.isbuiltin", "domId": "inspect_inspect.isbuiltin" }, { "name": "inspect.isclass", "domId": "inspect_inspect.isclass" }, { "name": "inspect.iscode", "domId": "inspect_inspect.iscode" }, { "name": "inspect.isdatadescriptor", "domId": "inspect_inspect.isdatadescriptor" }, { "name": "inspect.isframe", "domId": "inspect_inspect.isframe" }, { "name": "inspect.isfunction", "domId": "inspect_inspect.isfunction" }, { "name": "inspect.isgenerator", "domId": "inspect_inspect.isgenerator" }, { "name": "inspect.isgeneratorfunction", "domId": "inspect_inspect.isgeneratorfunction" }, { "name": "inspect.isgetsetdescriptor", "domId": "inspect_inspect.isgetsetdescriptor" }, { "name": "inspect.ismemberdescriptor", "domId": "inspect_inspect.ismemberdescriptor" }, { "name": "inspect.ismethod", "domId": "inspect_inspect.ismethod" }, { "name": "inspect.ismethoddescriptor", "domId": "inspect_inspect.ismethoddescriptor" }, { "name": "inspect.ismodule", "domId": "inspect_inspect.ismodule" }, { "name": "inspect.isroutine", "domId": "inspect_inspect.isroutine" }, { "name": "inspect.istraceback", "domId": "inspect_inspect.istraceback" }, { "name": "inspect.stack", "domId": "inspect_inspect.stack" }, { "name": "inspect.trace", "domId": "inspect_inspect.trace" } ] }, { "url": "http://docs.python.org/library/importlib.html", "title": "importlib", "html": "
\n

30.2. importlib – Convenience wrappers for __import__()

\n

\nNew in version 2.7.

\n

This module is a minor subset of what is available in the more full-featured\npackage of the same name from Python 3.1 that provides a complete\nimplementation of import. What is here has been provided to\nhelp ease in transitioning from 2.7 to 3.1.

\n
\n
\nimportlib.import_module(name, package=None)
\n
Import a module. The name argument specifies what module to\nimport in absolute or relative terms\n(e.g. either pkg.mod or ..mod). If the name is\nspecified in relative terms, then the package argument must be\nspecified to the package which is to act as the anchor for resolving the\npackage name (e.g. import_module('..mod', 'pkg.subpkg') will import\npkg.mod). The specified module will be inserted into\nsys.modules and returned.
\n\n
", "searchableItems": [ { "name": "importlib.import_module", "domId": "importlib_importlib.import_module" } ] }, { "url": "http://docs.python.org/library/zipimport.html", "title": "zipimport", "html": "
\n

30.4. zipimport — Import modules from Zip archives

\n

\nNew in version 2.3.

\n

This module adds the ability to import Python modules (*.py,\n*.py[co]) and packages from ZIP-format archives. It is usually not\nneeded to use the zipimport module explicitly; it is automatically used\nby the built-in import mechanism for sys.path items that are paths\nto ZIP archives.

\n

Typically, sys.path is a list of directory names as strings. This module\nalso allows an item of sys.path to be a string naming a ZIP file archive.\nThe ZIP archive can contain a subdirectory structure to support package imports,\nand a path within the archive can be specified to only import from a\nsubdirectory. For example, the path /tmp/example.zip/lib/ would only\nimport from the lib/ subdirectory within the archive.

\n

Any files may be present in the ZIP archive, but only files .py and\n.py[co] are available for import. ZIP import of dynamic modules\n(.pyd, .so) is disallowed. Note that if an archive only contains\n.py files, Python will not attempt to modify the archive by adding the\ncorresponding .pyc or .pyo file, meaning that if a ZIP archive\ndoesn’t contain .pyc files, importing may be rather slow.

\n

Using the built-in reload() function will fail if called on a module\nloaded from a ZIP archive; it is unlikely that reload() would be needed,\nsince this would imply that the ZIP has been altered during runtime.

\n

ZIP archives with an archive comment are currently not supported.

\n
\n

See also

\n
\n
PKZIP Application Note
\n
Documentation on the ZIP file format by Phil Katz, the creator of the format and\nalgorithms used.
\n
PEP 273 - Import Modules from Zip Archives
\n
Written by James C. Ahlstrom, who also provided an implementation. Python 2.3\nfollows the specification in PEP 273, but uses an implementation written by Just\nvan Rossum that uses the import hooks described in PEP 302.
\n
PEP 302 - New Import Hooks
\n
The PEP to add the import hooks that help this module work.
\n
\n
\n

This module defines an exception:

\n
\n
\nexception zipimport.ZipImportError
\n
Exception raised by zipimporter objects. It’s a subclass of ImportError,\nso it can be caught as ImportError, too.
\n\n
\n

30.4.1. zipimporter Objects

\n

zipimporter is the class for importing ZIP files.

\n
\n
\nclass zipimport.zipimporter(archivepath)
\n

Create a new zipimporter instance. archivepath must be a path to a ZIP\nfile, or to a specific path within a ZIP file. For example, an archivepath\nof foo/bar.zip/lib will look for modules in the lib directory\ninside the ZIP file foo/bar.zip (provided that it exists).

\n

ZipImportError is raised if archivepath doesn’t point to a valid ZIP\narchive.

\n
\n
\nfind_module(fullname[, path])
\n
Search for a module specified by fullname. fullname must be the fully\nqualified (dotted) module name. It returns the zipimporter instance itself\nif the module was found, or None if it wasn’t. The optional\npath argument is ignored—it’s there for compatibility with the\nimporter protocol.
\n\n
\n
\nget_code(fullname)
\n
Return the code object for the specified module. Raise\nZipImportError if the module couldn’t be found.
\n\n
\n
\nget_data(pathname)
\n
Return the data associated with pathname. Raise IOError if the\nfile wasn’t found.
\n\n
\n
\nget_filename(fullname)
\n
Return the value __file__ would be set to if the specified module\nwas imported. Raise ZipImportError if the module couldn’t be\nfound.
\n\n

\nNew in version 2.7.

\n
\n
\nget_source(fullname)
\n
Return the source code for the specified module. Raise\nZipImportError if the module couldn’t be found, return\nNone if the archive does contain the module, but has no source\nfor it.
\n\n
\n
\nis_package(fullname)
\n
Return True if the module specified by fullname is a package. Raise\nZipImportError if the module couldn’t be found.
\n\n
\n
\nload_module(fullname)
\n
Load the module specified by fullname. fullname must be the fully\nqualified (dotted) module name. It returns the imported module, or raises\nZipImportError if it wasn’t found.
\n\n
\n
\narchive
\n
The file name of the importer’s associated ZIP file, without a possible\nsubpath.
\n\n
\n
\nprefix
\n
The subpath within the ZIP file where modules are searched. This is the\nempty string for zipimporter objects which point to the root of the ZIP\nfile.
\n\n

The archive and prefix attributes, when combined with a\nslash, equal the original archivepath argument given to the\nzipimporter constructor.

\n
\n\n
\n
\n

30.4.2. Examples

\n

Here is an example that imports a module from a ZIP archive - note that the\nzipimport module is not explicitly used.

\n
$ unzip -l /tmp/example.zip\nArchive:  /tmp/example.zip\n  Length     Date   Time    Name\n --------    ----   ----    ----\n     8467  11-26-02 22:30   jwzthreading.py\n --------                   -------\n     8467                   1 file\n$ ./python\nPython 2.3 (#1, Aug 1 2003, 19:54:32)\n>>> import sys\n>>> sys.path.insert(0, '/tmp/example.zip')  # Add .zip file to front of path\n>>> import jwzthreading\n>>> jwzthreading.__file__\n'/tmp/example.zip/jwzthreading.py'
\n
\n
\n
", "searchableItems": [ { "name": "zipimport.zipimporter", "domId": "zipimport_zipimport.zipimporter" }, { "name": "zipimport.zipimporter.find_module", "domId": "zipimport_zipimport.zipimporter.find_module" }, { "name": "zipimport.zipimporter.get_code", "domId": "zipimport_zipimport.zipimporter.get_code" }, { "name": "zipimport.zipimporter.get_data", "domId": "zipimport_zipimport.zipimporter.get_data" }, { "name": "zipimport.zipimporter.get_filename", "domId": "zipimport_zipimport.zipimporter.get_filename" }, { "name": "zipimport.zipimporter.get_source", "domId": "zipimport_zipimport.zipimporter.get_source" }, { "name": "zipimport.zipimporter.is_package", "domId": "zipimport_zipimport.zipimporter.is_package" }, { "name": "zipimport.zipimporter.load_module", "domId": "zipimport_zipimport.zipimporter.load_module" } ] }, { "url": "http://docs.python.org/library/imp.html", "title": "imp", "html": "
\n

30.1. imp — Access the import internals

\n

This module provides an interface to the mechanisms used to implement the\nimport statement. It defines the following constants and functions:

\n
\n
\nimp.get_magic()
\n

Return the magic string value used to recognize byte-compiled code files\n(.pyc files). (This value may be different for each Python version.)

\n
\n\n
\n
\nimp.get_suffixes()
\n
Return a list of 3-element tuples, each describing a particular type of\nmodule. Each triple has the form (suffix, mode, type), where suffix is\na string to be appended to the module name to form the filename to search\nfor, mode is the mode string to pass to the built-in open() function\nto open the file (this can be 'r' for text files or 'rb' for binary\nfiles), and type is the file type, which has one of the values\nPY_SOURCE, PY_COMPILED, or C_EXTENSION, described\nbelow.
\n\n
\n
\nimp.find_module(name[, path])
\n

Try to find the module name. If path is omitted or None, the list of\ndirectory names given by sys.path is searched, but first a few special\nplaces are searched: the function tries to find a built-in module with the\ngiven name (C_BUILTIN), then a frozen module (PY_FROZEN),\nand on some systems some other places are looked in as well (on Windows, it\nlooks in the registry which may point to a specific file).

\n

Otherwise, path must be a list of directory names; each directory is\nsearched for files with any of the suffixes returned by get_suffixes()\nabove. Invalid names in the list are silently ignored (but all list items\nmust be strings).

\n

If search is successful, the return value is a 3-element tuple (file,\npathname, description):

\n

file is an open file object positioned at the beginning, pathname is the\npathname of the file found, and description is a 3-element tuple as\ncontained in the list returned by get_suffixes() describing the kind of\nmodule found.

\n

If the module does not live in a file, the returned file is None,\npathname is the empty string, and the description tuple contains empty\nstrings for its suffix and mode; the module type is indicated as given in\nparentheses above. If the search is unsuccessful, ImportError is\nraised. Other exceptions indicate problems with the arguments or\nenvironment.

\n

If the module is a package, file is None, pathname is the package\npath and the last item in the description tuple is PKG_DIRECTORY.

\n

This function does not handle hierarchical module names (names containing\ndots). In order to find P.*M*, that is, submodule M of package P, use\nfind_module() and load_module() to find and load package P, and\nthen use find_module() with the path argument set to P.__path__.\nWhen P itself has a dotted name, apply this recipe recursively.

\n
\n\n
\n
\nimp.load_module(name, file, pathname, description)
\n

Load a module that was previously found by find_module() (or by an\notherwise conducted search yielding compatible results). This function does\nmore than importing the module: if the module was already imported, it is\nequivalent to a reload()! The name argument indicates the full\nmodule name (including the package name, if this is a submodule of a\npackage). The file argument is an open file, and pathname is the\ncorresponding file name; these can be None and '', respectively, when\nthe module is a package or not being loaded from a file. The description\nargument is a tuple, as would be returned by get_suffixes(), describing\nwhat kind of module must be loaded.

\n

If the load is successful, the return value is the module object; otherwise,\nan exception (usually ImportError) is raised.

\n

Important: the caller is responsible for closing the file argument, if\nit was not None, even when an exception is raised. This is best done\nusing a try ... finally statement.

\n
\n\n
\n
\nimp.new_module(name)
\n
Return a new empty module object called name. This object is not inserted\nin sys.modules.
\n\n
\n
\nimp.lock_held()
\n

Return True if the import lock is currently held, else False. On\nplatforms without threads, always return False.

\n

On platforms with threads, a thread executing an import holds an internal lock\nuntil the import is complete. This lock blocks other threads from doing an\nimport until the original import completes, which in turn prevents other threads\nfrom seeing incomplete module objects constructed by the original thread while\nin the process of completing its import (and the imports, if any, triggered by\nthat).

\n
\n\n
\n
\nimp.acquire_lock()
\n

Acquire the interpreter’s import lock for the current thread. This lock should\nbe used by import hooks to ensure thread-safety when importing modules.

\n

Once a thread has acquired the import lock, the same thread may acquire it\nagain without blocking; the thread must release it once for each time it has\nacquired it.

\n

On platforms without threads, this function does nothing.

\n

\nNew in version 2.3.

\n
\n\n
\n
\nimp.release_lock()
\n

Release the interpreter’s import lock. On platforms without threads, this\nfunction does nothing.

\n

\nNew in version 2.3.

\n
\n\n

The following constants with integer values, defined in this module, are used to\nindicate the search result of find_module().

\n
\n
\nimp.PY_SOURCE
\n
The module was found as a source file.
\n\n
\n
\nimp.PY_COMPILED
\n
The module was found as a compiled code object file.
\n\n
\n
\nimp.C_EXTENSION
\n
The module was found as dynamically loadable shared library.
\n\n
\n
\nimp.PKG_DIRECTORY
\n
The module was found as a package directory.
\n\n
\n
\nimp.C_BUILTIN
\n
The module was found as a built-in module.
\n\n
\n
\nimp.PY_FROZEN
\n
The module was found as a frozen module (see init_frozen()).
\n\n

The following constant and functions are obsolete; their functionality is\navailable through find_module() or load_module(). They are kept\naround for backward compatibility:

\n
\n
\nimp.SEARCH_ERROR
\n
Unused.
\n\n
\n
\nimp.init_builtin(name)
\n
Initialize the built-in module called name and return its module object along\nwith storing it in sys.modules. If the module was already initialized, it\nwill be initialized again. Re-initialization involves the copying of the\nbuilt-in module’s __dict__ from the cached module over the module’s entry in\nsys.modules. If there is no built-in module called name, None is\nreturned.
\n\n
\n
\nimp.init_frozen(name)
\n
Initialize the frozen module called name and return its module object. If\nthe module was already initialized, it will be initialized again. If there\nis no frozen module called name, None is returned. (Frozen modules are\nmodules written in Python whose compiled byte-code object is incorporated\ninto a custom-built Python interpreter by Python’s freeze\nutility. See Tools/freeze/ for now.)
\n\n
\n
\nimp.is_builtin(name)
\n
Return 1 if there is a built-in module called name which can be\ninitialized again. Return -1 if there is a built-in module called name\nwhich cannot be initialized again (see init_builtin()). Return 0 if\nthere is no built-in module called name.
\n\n
\n
\nimp.is_frozen(name)
\n
Return True if there is a frozen module (see init_frozen()) called\nname, or False if there is no such module.
\n\n
\n
\nimp.load_compiled(name, pathname[, file])
\n

Load and initialize a module implemented as a byte-compiled code file and return\nits module object. If the module was already initialized, it will be\ninitialized again. The name argument is used to create or access a module\nobject. The pathname argument points to the byte-compiled code file. The\nfile argument is the byte-compiled code file, open for reading in binary mode,\nfrom the beginning. It must currently be a real file object, not a user-defined\nclass emulating a file.

\n
\n\n
\n
\nimp.load_dynamic(name, pathname[, file])
\n
Load and initialize a module implemented as a dynamically loadable shared\nlibrary and return its module object. If the module was already initialized, it\nwill be initialized again. Re-initialization involves copying the __dict__\nattribute of the cached instance of the module over the value used in the module\ncached in sys.modules. The pathname argument must point to the shared\nlibrary. The name argument is used to construct the name of the\ninitialization function: an external C function called initname() in the\nshared library is called. The optional file argument is ignored. (Note:\nusing shared libraries is highly system dependent, and not all systems support\nit.)
\n\n
\n
\nimp.load_source(name, pathname[, file])
\n
Load and initialize a module implemented as a Python source file and return its\nmodule object. If the module was already initialized, it will be initialized\nagain. The name argument is used to create or access a module object. The\npathname argument points to the source file. The file argument is the\nsource file, open for reading as text, from the beginning. It must currently be\na real file object, not a user-defined class emulating a file. Note that if a\nproperly matching byte-compiled file (with suffix .pyc or .pyo)\nexists, it will be used instead of parsing the given source file.
\n\n
\n
\nclass imp.NullImporter(path_string)
\n

The NullImporter type is a PEP 302 import hook that handles\nnon-directory path strings by failing to find any modules. Calling this type\nwith an existing directory or empty string raises ImportError.\nOtherwise, a NullImporter instance is returned.

\n

Python adds instances of this type to sys.path_importer_cache for any path\nentries that are not directories and are not handled by any other path hooks on\nsys.path_hooks. Instances have only one method:

\n
\n
\nfind_module(fullname[, path])
\n
This method always returns None, indicating that the requested module could\nnot be found.
\n\n

\nNew in version 2.5.

\n
\n\n
\n

30.1.1. Examples

\n

The following function emulates what was the standard import statement up to\nPython 1.4 (no hierarchical module names). (This implementation wouldn’t work\nin that version, since find_module() has been extended and\nload_module() has been added in 1.4.)

\n
import imp\nimport sys\n\ndef __import__(name, globals=None, locals=None, fromlist=None):\n    # Fast path: see if the module has already been imported.\n    try:\n        return sys.modules[name]\n    except KeyError:\n        pass\n\n    # If any of the following calls raises an exception,\n    # there's a problem we can't handle -- let the caller handle it.\n\n    fp, pathname, description = imp.find_module(name)\n\n    try:\n        return imp.load_module(name, fp, pathname, description)\n    finally:\n        # Since we may exit via an exception, close fp explicitly.\n        if fp:\n            fp.close()\n
\n
\n

A more complete example that implements hierarchical module names and includes a\nreload() function can be found in the module knee. The knee\nmodule can be found in Demo/imputil/ in the Python source distribution.

\n
\n
", "searchableItems": [ { "name": "imp.acquire_lock", "domId": "imp_imp.acquire_lock" }, { "name": "imp.find_module", "domId": "imp_imp.find_module" }, { "name": "imp.get_magic", "domId": "imp_imp.get_magic" }, { "name": "imp.get_suffixes", "domId": "imp_imp.get_suffixes" }, { "name": "imp.init_builtin", "domId": "imp_imp.init_builtin" }, { "name": "imp.init_frozen", "domId": "imp_imp.init_frozen" }, { "name": "imp.is_builtin", "domId": "imp_imp.is_builtin" }, { "name": "imp.is_frozen", "domId": "imp_imp.is_frozen" }, { "name": "imp.load_compiled", "domId": "imp_imp.load_compiled" }, { "name": "imp.load_dynamic", "domId": "imp_imp.load_dynamic" }, { "name": "imp.load_module", "domId": "imp_imp.load_module" }, { "name": "imp.load_source", "domId": "imp_imp.load_source" }, { "name": "imp.lock_held", "domId": "imp_imp.lock_held" }, { "name": "imp.new_module", "domId": "imp_imp.new_module" }, { "name": "imp.NullImporter", "domId": "imp_imp.NullImporter" }, { "name": "imp.NullImporter.find_module", "domId": "imp_imp.NullImporter.find_module" }, { "name": "imp.release_lock", "domId": "imp_imp.release_lock" } ] }, { "url": "http://docs.python.org/library/rexec.html", "title": "rexec", "html": "
\n

29.1. rexec — Restricted execution framework

\n

\nDeprecated since version 2.6: The rexec module has been removed in Python 3.0.

\n

\nChanged in version 2.3: Disabled module.

\n
\n

Warning

\n

The documentation has been left in place to help in reading old code that uses\nthe module.

\n
\n

This module contains the RExec class, which supports r_eval(),\nr_execfile(), r_exec(), and r_import() methods, which are\nrestricted versions of the standard Python functions eval(),\nexecfile() and the exec and import statements. Code\nexecuted in this restricted environment will only have access to modules and\nfunctions that are deemed safe; you can subclass RExec to add or remove\ncapabilities as desired.

\n
\n

Warning

\n

While the rexec module is designed to perform as described below, it does\nhave a few known vulnerabilities which could be exploited by carefully written\ncode. Thus it should not be relied upon in situations requiring “production\nready” security. In such situations, execution via sub-processes or very\ncareful “cleansing” of both code and data to be processed may be necessary.\nAlternatively, help in patching known rexec vulnerabilities would be\nwelcomed.

\n
\n
\n

Note

\n

The RExec class can prevent code from performing unsafe operations like\nreading or writing disk files, or using TCP/IP sockets. However, it does not\nprotect against code using extremely large amounts of memory or processor time.

\n
\n
\n
\nclass rexec.RExec([hooks[, verbose]])
\n

Returns an instance of the RExec class.

\n

hooks is an instance of the RHooks class or a subclass of it. If it\nis omitted or None, the default RHooks class is instantiated.\nWhenever the rexec module searches for a module (even a built-in one) or\nreads a module’s code, it doesn’t actually go out to the file system itself.\nRather, it calls methods of an RHooks instance that was passed to or\ncreated by its constructor. (Actually, the RExec object doesn’t make\nthese calls — they are made by a module loader object that’s part of the\nRExec object. This allows another level of flexibility, which can be\nuseful when changing the mechanics of import within the restricted\nenvironment.)

\n

By providing an alternate RHooks object, we can control the file system\naccesses made to import a module, without changing the actual algorithm that\ncontrols the order in which those accesses are made. For instance, we could\nsubstitute an RHooks object that passes all filesystem requests to a\nfile server elsewhere, via some RPC mechanism such as ILU. Grail’s applet\nloader uses this to support importing applets from a URL for a directory.

\n

If verbose is true, additional debugging output may be sent to standard\noutput.

\n
\n\n

It is important to be aware that code running in a restricted environment can\nstill call the sys.exit() function. To disallow restricted code from\nexiting the interpreter, always protect calls that cause restricted code to run\nwith a try/except statement that catches the\nSystemExit exception. Removing the sys.exit() function from the\nrestricted environment is not sufficient — the restricted code could still use\nraise SystemExit. Removing SystemExit is not a reasonable option;\nsome library code makes use of this and would break were it not available.

\n
\n

See also

\n
\n
Grail Home Page
\n
Grail is a Web browser written entirely in Python. It uses the rexec\nmodule as a foundation for supporting Python applets, and can be used as an\nexample usage of this module.
\n
\n
\n
\n

29.1.1. RExec Objects

\n

RExec instances support the following methods:

\n
\n
\nRExec.r_eval(code)
\n
code must either be a string containing a Python expression, or a compiled\ncode object, which will be evaluated in the restricted environment’s\n__main__ module. The value of the expression or code object will be\nreturned.
\n\n
\n
\nRExec.r_exec(code)
\n
code must either be a string containing one or more lines of Python code, or a\ncompiled code object, which will be executed in the restricted environment’s\n__main__ module.
\n\n
\n
\nRExec.r_execfile(filename)
\n
Execute the Python code contained in the file filename in the restricted\nenvironment’s __main__ module.
\n\n

Methods whose names begin with s_ are similar to the functions beginning\nwith r_, but the code will be granted access to restricted versions of the\nstandard I/O streams sys.stdin, sys.stderr, and sys.stdout.

\n
\n
\nRExec.s_eval(code)
\n
code must be a string containing a Python expression, which will be evaluated\nin the restricted environment.
\n\n
\n
\nRExec.s_exec(code)
\n
code must be a string containing one or more lines of Python code, which will\nbe executed in the restricted environment.
\n\n
\n
\nRExec.s_execfile(code)
\n
Execute the Python code contained in the file filename in the restricted\nenvironment.
\n\n

RExec objects must also support various methods which will be\nimplicitly called by code executing in the restricted environment. Overriding\nthese methods in a subclass is used to change the policies enforced by a\nrestricted environment.

\n
\n
\nRExec.r_import(modulename[, globals[, locals[, fromlist]]])
\n
Import the module modulename, raising an ImportError exception if the\nmodule is considered unsafe.
\n\n
\n
\nRExec.r_open(filename[, mode[, bufsize]])
\n
Method called when open() is called in the restricted environment. The\narguments are identical to those of open(), and a file object (or a class\ninstance compatible with file objects) should be returned. RExec‘s\ndefault behaviour is allow opening any file for reading, but forbidding any\nattempt to write a file. See the example below for an implementation of a less\nrestrictive r_open().
\n\n
\n
\nRExec.r_reload(module)
\n
Reload the module object module, re-parsing and re-initializing it.
\n\n
\n
\nRExec.r_unload(module)
\n
Unload the module object module (remove it from the restricted environment’s\nsys.modules dictionary).
\n\n

And their equivalents with access to restricted standard I/O streams:

\n
\n
\nRExec.s_import(modulename[, globals[, locals[, fromlist]]])
\n
Import the module modulename, raising an ImportError exception if the\nmodule is considered unsafe.
\n\n
\n
\nRExec.s_reload(module)
\n
Reload the module object module, re-parsing and re-initializing it.
\n\n
\n
\nRExec.s_unload(module)
\n
Unload the module object module.
\n\n
\n
\n

29.1.2. Defining restricted environments

\n

The RExec class has the following class attributes, which are used by\nthe __init__() method. Changing them on an existing instance won’t have\nany effect; instead, create a subclass of RExec and assign them new\nvalues in the class definition. Instances of the new class will then use those\nnew values. All these attributes are tuples of strings.

\n
\n
\nRExec.nok_builtin_names
\n
Contains the names of built-in functions which will not be available to\nprograms running in the restricted environment. The value for RExec is\n('open', 'reload', '__import__'). (This gives the exceptions, because by far\nthe majority of built-in functions are harmless. A subclass that wants to\noverride this variable should probably start with the value from the base class\nand concatenate additional forbidden functions — when new dangerous built-in\nfunctions are added to Python, they will also be added to this module.)
\n\n
\n
\nRExec.ok_builtin_modules
\n
Contains the names of built-in modules which can be safely imported. The value\nfor RExec is ('audioop', 'array', 'binascii', 'cmath', 'errno',\n'imageop', 'marshal', 'math', 'md5', 'operator', 'parser', 'regex', 'select',\n'sha', '_sre', 'strop', 'struct', 'time'). A similar remark about overriding\nthis variable applies — use the value from the base class as a starting point.
\n\n
\n
\nRExec.ok_path
\n
Contains the directories which will be searched when an import is\nperformed in the restricted environment. The value for RExec is the\nsame as sys.path (at the time the module is loaded) for unrestricted code.
\n\n
\n
\nRExec.ok_posix_names
\n
Contains the names of the functions in the os module which will be\navailable to programs running in the restricted environment. The value for\nRExec is ('error', 'fstat', 'listdir', 'lstat', 'readlink', 'stat',\n'times', 'uname', 'getpid', 'getppid', 'getcwd', 'getuid', 'getgid', 'geteuid',\n'getegid').
\n\n
\n
\nRExec.ok_sys_names
\n
Contains the names of the functions and variables in the sys module which\nwill be available to programs running in the restricted environment. The value\nfor RExec is ('ps1', 'ps2', 'copyright', 'version', 'platform',\n'exit', 'maxint').
\n\n
\n
\nRExec.ok_file_types
\n
Contains the file types from which modules are allowed to be loaded. Each file\ntype is an integer constant defined in the imp module. The meaningful\nvalues are PY_SOURCE, PY_COMPILED, and C_EXTENSION.\nThe value for RExec is (C_EXTENSION, PY_SOURCE). Adding\nPY_COMPILED in subclasses is not recommended; an attacker could exit\nthe restricted execution mode by putting a forged byte-compiled file\n(.pyc) anywhere in your file system, for example by writing it to\n/tmp or uploading it to the /incoming directory of your public\nFTP server.
\n\n
\n
\n

29.1.3. An example

\n

Let us say that we want a slightly more relaxed policy than the standard\nRExec class. For example, if we’re willing to allow files in\n/tmp to be written, we can subclass the RExec class:

\n
class TmpWriterRExec(rexec.RExec):\n    def r_open(self, file, mode='r', buf=-1):\n        if mode in ('r', 'rb'):\n            pass\n        elif mode in ('w', 'wb', 'a', 'ab'):\n            # check filename : must begin with /tmp/\n            if file[:5]!='/tmp/':\n                raise IOError("can't write outside /tmp")\n            elif (string.find(file, '/../') >= 0 or\n                 file[:3] == '../' or file[-3:] == '/..'):\n                raise IOError("'..' in filename forbidden")\n        else: raise IOError("Illegal open() mode")\n        return open(file, mode, buf)\n
\n
\n

Notice that the above code will occasionally forbid a perfectly valid filename;\nfor example, code in the restricted environment won’t be able to open a file\ncalled /tmp/foo/../bar. To fix this, the r_open() method would\nhave to simplify the filename to /tmp/bar, which would require splitting\napart the filename and performing various operations on it. In cases where\nsecurity is at stake, it may be preferable to write simple code which is\nsometimes overly restrictive, instead of more general code that is also more\ncomplex and may harbor a subtle security hole.

\n
\n
", "searchableItems": [ { "name": "rexec.RExec", "domId": "rexec_rexec.RExec" }, { "name": "rexec.RExec.r_eval", "domId": "rexec_rexec.RExec.r_eval" }, { "name": "rexec.RExec.r_exec", "domId": "rexec_rexec.RExec.r_exec" }, { "name": "rexec.RExec.r_execfile", "domId": "rexec_rexec.RExec.r_execfile" }, { "name": "rexec.RExec.r_import", "domId": "rexec_rexec.RExec.r_import" }, { "name": "rexec.RExec.r_open", "domId": "rexec_rexec.RExec.r_open" }, { "name": "rexec.RExec.r_reload", "domId": "rexec_rexec.RExec.r_reload" }, { "name": "rexec.RExec.r_unload", "domId": "rexec_rexec.RExec.r_unload" }, { "name": "rexec.RExec.s_eval", "domId": "rexec_rexec.RExec.s_eval" }, { "name": "rexec.RExec.s_exec", "domId": "rexec_rexec.RExec.s_exec" }, { "name": "rexec.RExec.s_execfile", "domId": "rexec_rexec.RExec.s_execfile" }, { "name": "rexec.RExec.s_import", "domId": "rexec_rexec.RExec.s_import" }, { "name": "rexec.RExec.s_reload", "domId": "rexec_rexec.RExec.s_reload" }, { "name": "rexec.RExec.s_unload", "domId": "rexec_rexec.RExec.s_unload" } ] }, { "url": "http://docs.python.org/library/imputil.html", "title": "imputil", "html": "
\n

30.3. imputil — Import utilities

\n

\nDeprecated since version 2.6: The imputil module has been removed in Python 3.0.

\n

This module provides a very handy and useful mechanism for custom\nimport hooks. Compared to the older ihooks module,\nimputil takes a dramatically simpler and more straight-forward\napproach to custom import functions.

\n
\n
\nclass imputil.ImportManager([fs_imp])
\n

Manage the import process.

\n
\n
\ninstall([namespace])
\n
Install this ImportManager into the specified namespace.
\n\n
\n
\nuninstall()
\n
Restore the previous import mechanism.
\n\n
\n
\nadd_suffix(suffix, importFunc)
\n
Undocumented.
\n\n
\n\n
\n
\nclass imputil.Importer
\n

Base class for replacing standard import functions.

\n
\n
\nimport_top(name)
\n
Import a top-level module.
\n\n
\n
\nget_code(parent, modname, fqname)
\n

Find and retrieve the code for the given module.

\n

parent specifies a parent module to define a context for importing.\nIt may be None, indicating no particular context for the search.

\n

modname specifies a single module (not dotted) within the parent.

\n

fqname specifies the fully-qualified module name. This is a\n(potentially) dotted name from the “root” of the module namespace\ndown to the modname.

\n

If there is no parent, then modname==fqname.

\n

This method should return None, or a 3-tuple.

\n
\n
    \n
  • If the module was not found, then None should be returned.
  • \n
  • The first item of the 2- or 3-tuple should be the integer 0 or 1,\nspecifying whether the module that was found is a package or not.
  • \n
  • The second item is the code object for the module (it will be\nexecuted within the new module’s namespace). This item can also\nbe a fully-loaded module object (e.g. loaded from a shared lib).
  • \n
  • The third item is a dictionary of name/value pairs that will be\ninserted into new module before the code object is executed. This\nis provided in case the module’s code expects certain values (such\nas where the module was found). When the second item is a module\nobject, then these names/values will be inserted after the module\nhas been loaded/initialized.
  • \n
\n
\n
\n\n
\n\n
\n
\nclass imputil.BuiltinImporter
\n

Emulate the import mechanism for built-in and frozen modules. This is a\nsub-class of the Importer class.

\n
\n
\nget_code(parent, modname, fqname)
\n
Undocumented.
\n\n
\n\n
\n
\nimputil.py_suffix_importer(filename, finfo, fqname)
\n
Undocumented.
\n\n
\n
\nclass imputil.DynLoadSuffixImporter([desc])
\n

Undocumented.

\n
\n
\nimport_file(filename, finfo, fqname)
\n
Undocumented.
\n\n
\n\n
\n

30.3.1. Examples

\n

This is a re-implementation of hierarchical module import.

\n

This code is intended to be read, not executed. However, it does work\n– all you need to do to enable it is “import knee”.

\n

(The name is a pun on the clunkier predecessor of this module, “ni”.)

\n
import sys, imp, __builtin__\n\n# Replacement for __import__()\ndef import_hook(name, globals=None, locals=None, fromlist=None):\n    parent = determine_parent(globals)\n    q, tail = find_head_package(parent, name)\n    m = load_tail(q, tail)\n    if not fromlist:\n        return q\n    if hasattr(m, "__path__"):\n        ensure_fromlist(m, fromlist)\n    return m\n\ndef determine_parent(globals):\n    if not globals or  not globals.has_key("__name__"):\n        return None\n    pname = globals['__name__']\n    if globals.has_key("__path__"):\n        parent = sys.modules[pname]\n        assert globals is parent.__dict__\n        return parent\n    if '.' in pname:\n        i = pname.rfind('.')\n        pname = pname[:i]\n        parent = sys.modules[pname]\n        assert parent.__name__ == pname\n        return parent\n    return None\n\ndef find_head_package(parent, name):\n    if '.' in name:\n        i = name.find('.')\n        head = name[:i]\n        tail = name[i+1:]\n    else:\n        head = name\n        tail = ""\n    if parent:\n        qname = "%s.%s"  (parent.__name__, head)\n    else:\n        qname = head\n    q = import_module(head, qname, parent)\n    if q: return q, tail\n    if parent:\n        qname = head\n        parent = None\n        q = import_module(head, qname, parent)\n        if q: return q, tail\n    raise ImportError("No module named " + qname)\n\ndef load_tail(q, tail):\n    m = q\n    while tail:\n        i = tail.find('.')\n        if i < 0: i = len(tail)\n        head, tail = tail[:i], tail[i+1:]\n        mname = "%s.%s"  (m.__name__, head)\n        m = import_module(head, mname, m)\n        if not m:\n            raise ImportError("No module named " + mname)\n    return m\n\ndef ensure_fromlist(m, fromlist, recursive=0):\n    for sub in fromlist:\n        if sub == "*":\n            if not recursive:\n                try:\n                    all = m.__all__\n                except AttributeError:\n                    pass\n                else:\n                    ensure_fromlist(m, all, 1)\n            continue\n        if sub != "*" and not hasattr(m, sub):\n            subname = "%s.%s"  (m.__name__, sub)\n            submod = import_module(sub, subname, m)\n            if not submod:\n                raise ImportError("No module named " + subname)\n\ndef import_module(partname, fqname, parent):\n    try:\n        return sys.modules[fqname]\n    except KeyError:\n        pass\n    try:\n        fp, pathname, stuff = imp.find_module(partname,\n                                              parent and parent.__path__)\n    except ImportError:\n        return None\n    try:\n        m = imp.load_module(fqname, fp, pathname, stuff)\n    finally:\n        if fp: fp.close()\n    if parent:\n        setattr(parent, partname, m)\n    return m\n\n\n# Replacement for reload()\ndef reload_hook(module):\n    name = module.__name__\n    if '.' not in name:\n        return import_module(name, name, None)\n    i = name.rfind('.')\n    pname = name[:i]\n    parent = sys.modules[pname]\n    return import_module(name[i+1:], name, parent)\n\n\n# Save the original hooks\noriginal_import = __builtin__.__import__\noriginal_reload = __builtin__.reload\n\n# Now install our hooks\n__builtin__.__import__ = import_hook\n__builtin__.reload = reload_hook\n
\n
\n

Also see the importers module (which can be found\nin Demo/imputil/ in the Python source distribution) for additional\nexamples.

\n
\n
", "searchableItems": [ { "name": "imputil.BuiltinImporter", "domId": "imputil_imputil.BuiltinImporter" }, { "name": "imputil.BuiltinImporter.get_code", "domId": "imputil_imputil.BuiltinImporter.get_code" }, { "name": "imputil.DynLoadSuffixImporter", "domId": "imputil_imputil.DynLoadSuffixImporter" }, { "name": "imputil.DynLoadSuffixImporter.import_file", "domId": "imputil_imputil.DynLoadSuffixImporter.import_file" }, { "name": "imputil.Importer", "domId": "imputil_imputil.Importer" }, { "name": "imputil.Importer.get_code", "domId": "imputil_imputil.Importer.get_code" }, { "name": "imputil.Importer.import_top", "domId": "imputil_imputil.Importer.import_top" }, { "name": "imputil.ImportManager", "domId": "imputil_imputil.ImportManager" }, { "name": "imputil.ImportManager.add_suffix", "domId": "imputil_imputil.ImportManager.add_suffix" }, { "name": "imputil.ImportManager.install", "domId": "imputil_imputil.ImportManager.install" }, { "name": "imputil.ImportManager.uninstall", "domId": "imputil_imputil.ImportManager.uninstall" }, { "name": "imputil.py_suffix_importer", "domId": "imputil_imputil.py_suffix_importer" } ] }, { "url": "http://docs.python.org/library/modulefinder.html", "title": "modulefinder", "html": "
\n

30.6. modulefinder — Find modules used by a script

\n

\nNew in version 2.3.

\n

Source code: Lib/modulefinder.py

\n
\n

This module provides a ModuleFinder class that can be used to determine\nthe set of modules imported by a script. modulefinder.py can also be run as\na script, giving the filename of a Python script as its argument, after which a\nreport of the imported modules will be printed.

\n
\n
\nmodulefinder.AddPackagePath(pkg_name, path)
\n
Record that the package named pkg_name can be found in the specified path.
\n\n
\n
\nmodulefinder.ReplacePackage(oldname, newname)
\n
Allows specifying that the module named oldname is in fact the package named\nnewname. The most common usage would be to handle how the _xmlplus\npackage replaces the xml package.
\n\n
\n
\nclass modulefinder.ModuleFinder([path=None, debug=0, excludes=[], replace_paths=[]])
\n

This class provides run_script() and report() methods to determine\nthe set of modules imported by a script. path can be a list of directories to\nsearch for modules; if not specified, sys.path is used. debug sets the\ndebugging level; higher values make the class print debugging messages about\nwhat it’s doing. excludes is a list of module names to exclude from the\nanalysis. replace_paths is a list of (oldpath, newpath) tuples that will\nbe replaced in module paths.

\n
\n
\nreport()
\n
Print a report to standard output that lists the modules imported by the\nscript and their paths, as well as modules that are missing or seem to be\nmissing.
\n\n
\n
\nrun_script(pathname)
\n
Analyze the contents of the pathname file, which must contain Python\ncode.
\n\n
\n
\nmodules
\n
A dictionary mapping module names to modules. See\nExample usage of ModuleFinder
\n\n
\n\n
\n

30.6.1. Example usage of ModuleFinder

\n

The script that is going to get analyzed later on (bacon.py):

\n
import re, itertools\n\ntry:\n    import baconhameggs\nexcept ImportError:\n    pass\n\ntry:\n    import guido.python.ham\nexcept ImportError:\n    pass\n
\n
\n

The script that will output the report of bacon.py:

\n
from modulefinder import ModuleFinder\n\nfinder = ModuleFinder()\nfinder.run_script('bacon.py')\n\nprint 'Loaded modules:'\nfor name, mod in finder.modules.iteritems():\n    print '%s: '  name,\n    print ','.join(mod.globalnames.keys()[:3])\n\nprint '-'*50\nprint 'Modules not imported:'\nprint '\\n'.join(finder.badmodules.iterkeys())\n
\n
\n

Sample output (may vary depending on the architecture):

\n
Loaded modules:\n_types:\ncopy_reg:  _inverted_registry,_slotnames,__all__\nsre_compile:  isstring,_sre,_optimize_unicode\n_sre:\nsre_constants:  REPEAT_ONE,makedict,AT_END_LINE\nsys:\nre:  __module__,finditer,_expand\nitertools:\n__main__:  re,itertools,baconhameggs\nsre_parse:  __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE\narray:\ntypes:  __module__,IntType,TypeType\n---------------------------------------------------\nModules not imported:\nguido.python.ham\nbaconhameggs
\n
\n
\n
", "searchableItems": [ { "name": "modulefinder.AddPackagePath", "domId": "modulefinder_modulefinder.AddPackagePath" }, { "name": "modulefinder.ModuleFinder", "domId": "modulefinder_modulefinder.ModuleFinder" }, { "name": "modulefinder.ModuleFinder.report", "domId": "modulefinder_modulefinder.ModuleFinder.report" }, { "name": "modulefinder.ModuleFinder.run_script", "domId": "modulefinder_modulefinder.ModuleFinder.run_script" }, { "name": "modulefinder.ReplacePackage", "domId": "modulefinder_modulefinder.ReplacePackage" } ] }, { "url": "http://docs.python.org/library/pkgutil.html", "title": "pkgutil", "html": "
\n

30.5. pkgutil — Package extension utility

\n

\nNew in version 2.3.

\n

Source code: Lib/pkgutil.py

\n
\n

This module provides utilities for the import system, in particular package\nsupport.

\n
\n
\npkgutil.extend_path(path, name)
\n

Extend the search path for the modules which comprise a package. Intended\nuse is to place the following code in a package’s __init__.py:

\n
from pkgutil import extend_path\n__path__ = extend_path(__path__, __name__)\n
\n
\n

This will add to the package’s __path__ all subdirectories of directories\non sys.path named after the package. This is useful if one wants to\ndistribute different parts of a single logical package as multiple\ndirectories.

\n

It also looks for *.pkg files beginning where * matches the\nname argument. This feature is similar to *.pth files (see the\nsite module for more information), except that it doesn’t special-case\nlines starting with import. A *.pkg file is trusted at face\nvalue: apart from checking for duplicates, all entries found in a\n*.pkg file are added to the path, regardless of whether they exist\non the filesystem. (This is a feature.)

\n

If the input path is not a list (as is the case for frozen packages) it is\nreturned unchanged. The input path is not modified; an extended copy is\nreturned. Items are only appended to the copy at the end.

\n

It is assumed that sys.path is a sequence. Items of sys.path\nthat are not (Unicode or 8-bit) strings referring to existing directories are\nignored. Unicode items on sys.path that cause errors when used as\nfilenames may cause this function to raise an exception (in line with\nos.path.isdir() behavior).

\n
\n\n
\n
\nclass pkgutil.ImpImporter(dirname=None)
\n

PEP 302 Importer that wraps Python’s “classic” import algorithm.

\n

If dirname is a string, a PEP 302 importer is created that searches that\ndirectory. If dirname is None, a PEP 302 importer is created that\nsearches the current sys.path, plus any modules that are frozen or\nbuilt-in.

\n

Note that ImpImporter does not currently support being used by\nplacement on sys.meta_path.

\n
\n\n
\n
\nclass pkgutil.ImpLoader(fullname, file, filename, etc)
\n
PEP 302 Loader that wraps Python’s “classic” import algorithm.
\n\n
\n
\npkgutil.find_loader(fullname)
\n

Find a PEP 302 “loader” object for fullname.

\n

If fullname contains dots, path must be the containing package’s\n__path__. Returns None if the module cannot be found or imported.\nThis function uses iter_importers(), and is thus subject to the same\nlimitations regarding platform-specific special import locations such as the\nWindows registry.

\n
\n\n
\n
\npkgutil.get_importer(path_item)
\n

Retrieve a PEP 302 importer for the given path_item.

\n

The returned importer is cached in sys.path_importer_cache if it was\nnewly created by a path hook.

\n

If there is no importer, a wrapper around the basic import machinery is\nreturned. This wrapper is never inserted into the importer cache (None\nis inserted instead).

\n

The cache (or part of it) can be cleared manually if a rescan of\nsys.path_hooks is necessary.

\n
\n\n
\n
\npkgutil.get_loader(module_or_name)
\n

Get a PEP 302 “loader” object for module_or_name.

\n

If the module or package is accessible via the normal import mechanism, a\nwrapper around the relevant part of that machinery is returned. Returns\nNone if the module cannot be found or imported. If the named module is\nnot already imported, its containing package (if any) is imported, in order\nto establish the package __path__.

\n

This function uses iter_importers(), and is thus subject to the same\nlimitations regarding platform-specific special import locations such as the\nWindows registry.

\n
\n\n
\n
\npkgutil.iter_importers(fullname='')
\n

Yield PEP 302 importers for the given module name.

\n

If fullname contains a ‘.’, the importers will be for the package containing\nfullname, otherwise they will be importers for sys.meta_path,\nsys.path, and Python’s “classic” import machinery, in that order. If\nthe named module is in a package, that package is imported as a side effect\nof invoking this function.

\n

Non-PEP 302 mechanisms (e.g. the Windows registry) used by the standard\nimport machinery to find files in alternative locations are partially\nsupported, but are searched after sys.path. Normally, these\nlocations are searched before sys.path, preventing sys.path\nentries from shadowing them.

\n

For this to cause a visible difference in behaviour, there must be a module\nor package name that is accessible via both sys.path and one of the\nnon-PEP 302 file system mechanisms. In this case, the emulation will find\nthe former version, while the builtin import mechanism will find the latter.

\n

Items of the following types can be affected by this discrepancy:\nimp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED,\nimp.PKG_DIRECTORY.

\n
\n\n
\n
\npkgutil.iter_modules(path=None, prefix='')
\n

Yields (module_loader, name, ispkg) for all submodules on path, or, if\npath is None, all top-level modules on sys.path.

\n

path should be either None or a list of paths to look for modules in.

\n

prefix is a string to output on the front of every module name on output.

\n
\n\n
\n
\npkgutil.walk_packages(path=None, prefix='', onerror=None)
\n

Yields (module_loader, name, ispkg) for all modules recursively on\npath, or, if path is None, all accessible modules.

\n

path should be either None or a list of paths to look for modules in.

\n

prefix is a string to output on the front of every module name on output.

\n

Note that this function must import all packages (not all modules!) on\nthe given path, in order to access the __path__ attribute to find\nsubmodules.

\n

onerror is a function which gets called with one argument (the name of the\npackage which was being imported) if any exception occurs while trying to\nimport a package. If no onerror function is supplied, ImportErrors\nare caught and ignored, while all other exceptions are propagated,\nterminating the search.

\n

Examples:

\n
# list all modules python can access\nwalk_packages()\n\n# list all submodules of ctypes\nwalk_packages(ctypes.__path__, ctypes.__name__ + '.')\n
\n
\n
\n\n
\n
\npkgutil.get_data(package, resource)
\n

Get a resource from a package.

\n

This is a wrapper for the PEP 302 loader get_data() API. The\npackage argument should be the name of a package, in standard module format\n(foo.bar). The resource argument should be in the form of a relative\nfilename, using / as the path separator. The parent directory name\n.. is not allowed, and nor is a rooted name (starting with a /).

\n

The function returns a binary string that is the contents of the specified\nresource.

\n

For packages located in the filesystem, which have already been imported,\nthis is the rough equivalent of:

\n
d = os.path.dirname(sys.modules[package].__file__)\ndata = open(os.path.join(d, resource), 'rb').read()\n
\n
\n

If the package cannot be located or loaded, or it uses a PEP 302 loader\nwhich does not support get_data(), then None is returned.

\n

\nNew in version 2.6.

\n
\n\n
", "searchableItems": [ { "name": "pkgutil.extend_path", "domId": "pkgutil_pkgutil.extend_path" }, { "name": "pkgutil.find_loader", "domId": "pkgutil_pkgutil.find_loader" }, { "name": "pkgutil.get_data", "domId": "pkgutil_pkgutil.get_data" }, { "name": "pkgutil.get_importer", "domId": "pkgutil_pkgutil.get_importer" }, { "name": "pkgutil.get_loader", "domId": "pkgutil_pkgutil.get_loader" }, { "name": "pkgutil.ImpImporter", "domId": "pkgutil_pkgutil.ImpImporter" }, { "name": "pkgutil.ImpLoader", "domId": "pkgutil_pkgutil.ImpLoader" }, { "name": "pkgutil.iter_importers", "domId": "pkgutil_pkgutil.iter_importers" }, { "name": "pkgutil.iter_modules", "domId": "pkgutil_pkgutil.iter_modules" }, { "name": "pkgutil.walk_packages", "domId": "pkgutil_pkgutil.walk_packages" } ] }, { "url": "http://docs.python.org/library/runpy.html", "title": "runpy", "html": "
\n

30.7. runpy — Locating and executing Python modules

\n

\nNew in version 2.5.

\n

Source code: Lib/runpy.py

\n
\n

The runpy module is used to locate and run Python modules without\nimporting them first. Its main use is to implement the -m command\nline switch that allows scripts to be located using the Python module\nnamespace rather than the filesystem.

\n

The runpy module provides two functions:

\n
\n
\nrunpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
\n

Execute the code of the specified module and return the resulting module\nglobals dictionary. The module’s code is first located using the standard\nimport mechanism (refer to PEP 302 for details) and then executed in a\nfresh module namespace.

\n

If the supplied module name refers to a package rather than a normal\nmodule, then that package is imported and the __main__ submodule within\nthat package is then executed and the resulting module globals dictionary\nreturned.

\n

The optional dictionary argument init_globals may be used to pre-populate\nthe module’s globals dictionary before the code is executed. The supplied\ndictionary will not be modified. If any of the special global variables\nbelow are defined in the supplied dictionary, those definitions are\noverridden by run_module().

\n

The special global variables __name__, __file__, __loader__\nand __package__ are set in the globals dictionary before the module\ncode is executed (Note that this is a minimal set of variables - other\nvariables may be set implicitly as an interpreter implementation detail).

\n

__name__ is set to run_name if this optional argument is not\nNone, to mod_name + '.__main__' if the named module is a\npackage and to the mod_name argument otherwise.

\n

__file__ is set to the name provided by the module loader. If the\nloader does not make filename information available, this variable is set\nto None.

\n

__loader__ is set to the PEP 302 module loader used to retrieve the\ncode for the module (This loader may be a wrapper around the standard\nimport mechanism).

\n

__package__ is set to mod_name if the named module is a package and\nto mod_name.rpartition('.')[0] otherwise.

\n

If the argument alter_sys is supplied and evaluates to True,\nthen sys.argv[0] is updated with the value of __file__ and\nsys.modules[__name__] is updated with a temporary module object for the\nmodule being executed. Both sys.argv[0] and sys.modules[__name__]\nare restored to their original values before the function returns.

\n

Note that this manipulation of sys is not thread-safe. Other threads\nmay see the partially initialised module, as well as the altered list of\narguments. It is recommended that the sys module be left alone when\ninvoking this function from threaded code.

\n

\nChanged in version 2.7: Added ability to execute packages by looking for a __main__\nsubmodule

\n
\n\n
\n
\nrunpy.run_path(file_path, init_globals=None, run_name=None)
\n

Execute the code at the named filesystem location and return the resulting\nmodule globals dictionary. As with a script name supplied to the CPython\ncommand line, the supplied path may refer to a Python source file, a\ncompiled bytecode file or a valid sys.path entry containing a __main__\nmodule (e.g. a zipfile containing a top-level __main__.py file).

\n

For a simple script, the specified code is simply executed in a fresh\nmodule namespace. For a valid sys.path entry (typically a zipfile or\ndirectory), the entry is first added to the beginning of sys.path. The\nfunction then looks for and executes a __main__ module using the\nupdated path. Note that there is no special protection against invoking\nan existing __main__ entry located elsewhere on sys.path if\nthere is no such module at the specified location.

\n

The optional dictionary argument init_globals may be used to pre-populate\nthe module’s globals dictionary before the code is executed. The supplied\ndictionary will not be modified. If any of the special global variables\nbelow are defined in the supplied dictionary, those definitions are\noverridden by run_path().

\n

The special global variables __name__, __file__, __loader__\nand __package__ are set in the globals dictionary before the module\ncode is executed (Note that this is a minimal set of variables - other\nvariables may be set implicitly as an interpreter implementation detail).

\n

__name__ is set to run_name if this optional argument is not\nNone and to '<run_path>' otherwise.

\n

__file__ is set to the name provided by the module loader. If the\nloader does not make filename information available, this variable is set\nto None. For a simple script, this will be set to file_path.

\n

__loader__ is set to the PEP 302 module loader used to retrieve the\ncode for the module (This loader may be a wrapper around the standard\nimport mechanism). For a simple script, this will be set to None.

\n

__package__ is set to __name__.rpartition('.')[0].

\n

A number of alterations are also made to the sys module. Firstly,\nsys.path may be altered as described above. sys.argv[0] is updated\nwith the value of file_path and sys.modules[__name__] is updated\nwith a temporary module object for the module being executed. All\nmodifications to items in sys are reverted before the function\nreturns.

\n

Note that, unlike run_module(), the alterations made to sys\nare not optional in this function as these adjustments are essential to\nallowing the execution of sys.path entries. As the thread-safety\nlimitations still apply, use of this function in threaded code should be\neither serialised with the import lock or delegated to a separate process.

\n

\nNew in version 2.7.

\n
\n\n
\n

See also

\n
\n
PEP 338 - Executing modules as scripts
\n
PEP written and implemented by Nick Coghlan.
\n
PEP 366 - Main module explicit relative imports
\n
PEP written and implemented by Nick Coghlan.
\n
\n

Command line and environment - CPython command line details

\n
\n
", "searchableItems": [ { "name": "runpy.run_module", "domId": "runpy_runpy.run_module" }, { "name": "runpy.run_path", "domId": "runpy_runpy.run_path" } ] }, { "url": "http://docs.python.org/library/parser.html", "title": "parser", "html": "
\n

31.1. parser — Access Python parse trees

\n

The parser module provides an interface to Python’s internal parser and\nbyte-code compiler. The primary purpose for this interface is to allow Python\ncode to edit the parse tree of a Python expression and create executable code\nfrom this. This is better than trying to parse and modify an arbitrary Python\ncode fragment as a string because parsing is performed in a manner identical to\nthe code forming the application. It is also faster.

\n
\n

Note

\n

From Python 2.5 onward, it’s much more convenient to cut in at the Abstract\nSyntax Tree (AST) generation and compilation stage, using the ast\nmodule.

\n

The parser module exports the names documented here also with “st”\nreplaced by “ast”; this is a legacy from the time when there was no other\nAST and has nothing to do with the AST found in Python 2.5. This is also the\nreason for the functions’ keyword arguments being called ast, not st.\nThe “ast” functions will be removed in Python 3.0.

\n
\n

There are a few things to note about this module which are important to making\nuse of the data structures created. This is not a tutorial on editing the parse\ntrees for Python code, but some examples of using the parser module are\npresented.

\n

Most importantly, a good understanding of the Python grammar processed by the\ninternal parser is required. For full information on the language syntax, refer\nto The Python Language Reference. The parser\nitself is created from a grammar specification defined in the file\nGrammar/Grammar in the standard Python distribution. The parse trees\nstored in the ST objects created by this module are the actual output from the\ninternal parser when created by the expr() or suite() functions,\ndescribed below. The ST objects created by sequence2st() faithfully\nsimulate those structures. Be aware that the values of the sequences which are\nconsidered “correct” will vary from one version of Python to another as the\nformal grammar for the language is revised. However, transporting code from one\nPython version to another as source text will always allow correct parse trees\nto be created in the target version, with the only restriction being that\nmigrating to an older version of the interpreter will not support more recent\nlanguage constructs. The parse trees are not typically compatible from one\nversion to another, whereas source code has always been forward-compatible.

\n

Each element of the sequences returned by st2list() or st2tuple()\nhas a simple form. Sequences representing non-terminal elements in the grammar\nalways have a length greater than one. The first element is an integer which\nidentifies a production in the grammar. These integers are given symbolic names\nin the C header file Include/graminit.h and the Python module\nsymbol. Each additional element of the sequence represents a component\nof the production as recognized in the input string: these are always sequences\nwhich have the same form as the parent. An important aspect of this structure\nwhich should be noted is that keywords used to identify the parent node type,\nsuch as the keyword if in an if_stmt, are included in the\nnode tree without any special treatment. For example, the if keyword\nis represented by the tuple (1, 'if'), where 1 is the numeric value\nassociated with all NAME tokens, including variable and function names\ndefined by the user. In an alternate form returned when line number information\nis requested, the same token might be represented as (1, 'if', 12), where\nthe 12 represents the line number at which the terminal symbol was found.

\n

Terminal elements are represented in much the same way, but without any child\nelements and the addition of the source text which was identified. The example\nof the if keyword above is representative. The various types of\nterminal symbols are defined in the C header file Include/token.h and\nthe Python module token.

\n

The ST objects are not required to support the functionality of this module,\nbut are provided for three purposes: to allow an application to amortize the\ncost of processing complex parse trees, to provide a parse tree representation\nwhich conserves memory space when compared to the Python list or tuple\nrepresentation, and to ease the creation of additional modules in C which\nmanipulate parse trees. A simple “wrapper” class may be created in Python to\nhide the use of ST objects.

\n

The parser module defines functions for a few distinct purposes. The\nmost important purposes are to create ST objects and to convert ST objects to\nother representations such as parse trees and compiled code objects, but there\nare also functions which serve to query the type of parse tree represented by an\nST object.

\n
\n

See also

\n
\n
Module symbol
\n
Useful constants representing internal nodes of the parse tree.
\n
Module token
\n
Useful constants representing leaf nodes of the parse tree and functions for\ntesting node values.
\n
\n
\n
\n

31.1.1. Creating ST Objects

\n

ST objects may be created from source code or from a parse tree. When creating\nan ST object from source, different functions are used to create the 'eval'\nand 'exec' forms.

\n
\n
\nparser.expr(source)
\n
The expr() function parses the parameter source as if it were an input\nto compile(source, 'file.py', 'eval'). If the parse succeeds, an ST object\nis created to hold the internal parse tree representation, otherwise an\nappropriate exception is raised.
\n\n
\n
\nparser.suite(source)
\n
The suite() function parses the parameter source as if it were an input\nto compile(source, 'file.py', 'exec'). If the parse succeeds, an ST object\nis created to hold the internal parse tree representation, otherwise an\nappropriate exception is raised.
\n\n
\n
\nparser.sequence2st(sequence)
\n

This function accepts a parse tree represented as a sequence and builds an\ninternal representation if possible. If it can validate that the tree conforms\nto the Python grammar and all nodes are valid node types in the host version of\nPython, an ST object is created from the internal representation and returned\nto the called. If there is a problem creating the internal representation, or\nif the tree cannot be validated, a ParserError exception is raised. An\nST object created this way should not be assumed to compile correctly; normal\nexceptions raised by compilation may still be initiated when the ST object is\npassed to compilest(). This may indicate problems not related to syntax\n(such as a MemoryError exception), but may also be due to constructs such\nas the result of parsing del f(0), which escapes the Python parser but is\nchecked by the bytecode compiler.

\n

Sequences representing terminal tokens may be represented as either two-element\nlists of the form (1, 'name') or as three-element lists of the form (1,\n'name', 56). If the third element is present, it is assumed to be a valid\nline number. The line number may be specified for any subset of the terminal\nsymbols in the input tree.

\n
\n\n
\n
\nparser.tuple2st(sequence)
\n
This is the same function as sequence2st(). This entry point is\nmaintained for backward compatibility.
\n\n
\n
\n

31.1.2. Converting ST Objects

\n

ST objects, regardless of the input used to create them, may be converted to\nparse trees represented as list- or tuple- trees, or may be compiled into\nexecutable code objects. Parse trees may be extracted with or without line\nnumbering information.

\n
\n
\nparser.st2list(ast[, line_info])
\n

This function accepts an ST object from the caller in ast and returns a\nPython list representing the equivalent parse tree. The resulting list\nrepresentation can be used for inspection or the creation of a new parse tree in\nlist form. This function does not fail so long as memory is available to build\nthe list representation. If the parse tree will only be used for inspection,\nst2tuple() should be used instead to reduce memory consumption and\nfragmentation. When the list representation is required, this function is\nsignificantly faster than retrieving a tuple representation and converting that\nto nested lists.

\n

If line_info is true, line number information will be included for all\nterminal tokens as a third element of the list representing the token. Note\nthat the line number provided specifies the line on which the token ends.\nThis information is omitted if the flag is false or omitted.

\n
\n\n
\n
\nparser.st2tuple(ast[, line_info])
\n

This function accepts an ST object from the caller in ast and returns a\nPython tuple representing the equivalent parse tree. Other than returning a\ntuple instead of a list, this function is identical to st2list().

\n

If line_info is true, line number information will be included for all\nterminal tokens as a third element of the list representing the token. This\ninformation is omitted if the flag is false or omitted.

\n
\n\n
\n
\nparser.compilest(ast[, filename='<syntax-tree>'])
\n

The Python byte compiler can be invoked on an ST object to produce code objects\nwhich can be used as part of an exec statement or a call to the\nbuilt-in eval() function. This function provides the interface to the\ncompiler, passing the internal parse tree from ast to the parser, using the\nsource file name specified by the filename parameter. The default value\nsupplied for filename indicates that the source was an ST object.

\n

Compiling an ST object may result in exceptions related to compilation; an\nexample would be a SyntaxError caused by the parse tree for del f(0):\nthis statement is considered legal within the formal grammar for Python but is\nnot a legal language construct. The SyntaxError raised for this\ncondition is actually generated by the Python byte-compiler normally, which is\nwhy it can be raised at this point by the parser module. Most causes of\ncompilation failure can be diagnosed programmatically by inspection of the parse\ntree.

\n
\n\n
\n
\n

31.1.3. Queries on ST Objects

\n

Two functions are provided which allow an application to determine if an ST was\ncreated as an expression or a suite. Neither of these functions can be used to\ndetermine if an ST was created from source code via expr() or\nsuite() or from a parse tree via sequence2st().

\n
\n
\nparser.isexpr(ast)
\n

When ast represents an 'eval' form, this function returns true, otherwise\nit returns false. This is useful, since code objects normally cannot be queried\nfor this information using existing built-in functions. Note that the code\nobjects created by compilest() cannot be queried like this either, and\nare identical to those created by the built-in compile() function.

\n
\n\n
\n
\nparser.issuite(ast)
\n
This function mirrors isexpr() in that it reports whether an ST object\nrepresents an 'exec' form, commonly known as a “suite.” It is not safe to\nassume that this function is equivalent to not isexpr(ast), as additional\nsyntactic fragments may be supported in the future.
\n\n
\n
\n

31.1.4. Exceptions and Error Handling

\n

The parser module defines a single exception, but may also pass other built-in\nexceptions from other portions of the Python runtime environment. See each\nfunction for information about the exceptions it can raise.

\n
\n
\nexception parser.ParserError
\n
Exception raised when a failure occurs within the parser module. This is\ngenerally produced for validation failures rather than the built-in\nSyntaxError raised during normal parsing. The exception argument is\neither a string describing the reason of the failure or a tuple containing a\nsequence causing the failure from a parse tree passed to sequence2st()\nand an explanatory string. Calls to sequence2st() need to be able to\nhandle either type of exception, while calls to other functions in the module\nwill only need to be aware of the simple string values.
\n\n

Note that the functions compilest(), expr(), and suite() may\nraise exceptions which are normally raised by the parsing and compilation\nprocess. These include the built in exceptions MemoryError,\nOverflowError, SyntaxError, and SystemError. In these\ncases, these exceptions carry all the meaning normally associated with them.\nRefer to the descriptions of each function for detailed information.

\n
\n
\n

31.1.5. ST Objects

\n

Ordered and equality comparisons are supported between ST objects. Pickling of\nST objects (using the pickle module) is also supported.

\n
\n
\nparser.STType
\n
The type of the objects returned by expr(), suite() and\nsequence2st().
\n\n

ST objects have the following methods:

\n
\n
\nST.compile([filename])
\n
Same as compilest(st, filename).
\n\n
\n
\nST.isexpr()
\n
Same as isexpr(st).
\n\n
\n
\nST.issuite()
\n
Same as issuite(st).
\n\n
\n
\nST.tolist([line_info])
\n
Same as st2list(st, line_info).
\n\n
\n
\nST.totuple([line_info])
\n
Same as st2tuple(st, line_info).
\n\n
\n
\n

31.1.6. Example: Emulation of compile()

\n

While many useful operations may take place between parsing and bytecode\ngeneration, the simplest operation is to do nothing. For this purpose, using\nthe parser module to produce an intermediate data structure is equivalent\nto the code

\n
>>> code = compile('a + 5', 'file.py', 'eval')\n>>> a = 5\n>>> eval(code)\n10\n
\n
\n

The equivalent operation using the parser module is somewhat longer, and\nallows the intermediate internal parse tree to be retained as an ST object:

\n
>>> import parser\n>>> st = parser.expr('a + 5')\n>>> code = st.compile('file.py')\n>>> a = 5\n>>> eval(code)\n10\n
\n
\n

An application which needs both ST and code objects can package this code into\nreadily available functions:

\n
import parser\n\ndef load_suite(source_string):\n    st = parser.suite(source_string)\n    return st, st.compile()\n\ndef load_expression(source_string):\n    st = parser.expr(source_string)\n    return st, st.compile()\n
\n
\n
\n
", "searchableItems": [ { "name": "parser.compilest", "domId": "parser_parser.compilest" }, { "name": "parser.expr", "domId": "parser_parser.expr" }, { "name": "parser.isexpr", "domId": "parser_parser.isexpr" }, { "name": "parser.issuite", "domId": "parser_parser.issuite" }, { "name": "parser.sequence2st", "domId": "parser_parser.sequence2st" }, { "name": "parser.ST.compile", "domId": "parser_parser.ST.compile" }, { "name": "parser.ST.isexpr", "domId": "parser_parser.ST.isexpr" }, { "name": "parser.ST.issuite", "domId": "parser_parser.ST.issuite" }, { "name": "parser.ST.tolist", "domId": "parser_parser.ST.tolist" }, { "name": "parser.ST.totuple", "domId": "parser_parser.ST.totuple" }, { "name": "parser.st2list", "domId": "parser_parser.st2list" }, { "name": "parser.st2tuple", "domId": "parser_parser.st2tuple" }, { "name": "parser.suite", "domId": "parser_parser.suite" }, { "name": "parser.tuple2st", "domId": "parser_parser.tuple2st" } ] }, { "url": "http://docs.python.org/library/symbol.html", "title": "symbol", "html": "
\n

31.4. symbol — Constants used with Python parse trees

\n

Source code: Lib/symbol.py

\n
\n

This module provides constants which represent the numeric values of internal\nnodes of the parse tree. Unlike most Python constants, these use lower-case\nnames. Refer to the file Grammar/Grammar in the Python distribution for\nthe definitions of the names in the context of the language grammar. The\nspecific numeric values which the names map to may change between Python\nversions.

\n

This module also provides one additional data object:

\n
\n
\nsymbol.sym_name
\n
Dictionary mapping the numeric values of the constants defined in this module\nback to name strings, allowing more human-readable representation of parse trees\nto be generated.
\n\n
", "searchableItems": [] }, { "url": "http://docs.python.org/library/ast.html", "title": "ast", "html": "
\n

31.2. ast — Abstract Syntax Trees

\n

\nNew in version 2.5: The low-level _ast module containing only the node classes.

\n

\nNew in version 2.6: The high-level ast module containing all helpers.

\n

Source code: Lib/ast.py

\n
\n

The ast module helps Python applications to process trees of the Python\nabstract syntax grammar. The abstract syntax itself might change with each\nPython release; this module helps to find out programmatically what the current\ngrammar looks like.

\n

An abstract syntax tree can be generated by passing ast.PyCF_ONLY_AST as\na flag to the compile() built-in function, or using the parse()\nhelper provided in this module. The result will be a tree of objects whose\nclasses all inherit from ast.AST. An abstract syntax tree can be\ncompiled into a Python code object using the built-in compile() function.

\n
\n

31.2.1. Node classes

\n
\n
\nclass ast.AST
\n

This is the base of all AST node classes. The actual node classes are\nderived from the Parser/Python.asdl file, which is reproduced\nbelow. They are defined in the _ast C\nmodule and re-exported in ast.

\n

There is one class defined for each left-hand side symbol in the abstract\ngrammar (for example, ast.stmt or ast.expr). In addition,\nthere is one class defined for each constructor on the right-hand side; these\nclasses inherit from the classes for the left-hand side trees. For example,\nast.BinOp inherits from ast.expr. For production rules\nwith alternatives (aka “sums”), the left-hand side class is abstract: only\ninstances of specific constructor nodes are ever created.

\n
\n
\n_fields
\n

Each concrete class has an attribute _fields which gives the names\nof all child nodes.

\n

Each instance of a concrete class has one attribute for each child node,\nof the type as defined in the grammar. For example, ast.BinOp\ninstances have an attribute left of type ast.expr.

\n

If these attributes are marked as optional in the grammar (using a\nquestion mark), the value might be None. If the attributes can have\nzero-or-more values (marked with an asterisk), the values are represented\nas Python lists. All possible attributes must be present and have valid\nvalues when compiling an AST with compile().

\n
\n\n
\n
\nlineno
\n
\ncol_offset
\n
Instances of ast.expr and ast.stmt subclasses have\nlineno and col_offset attributes. The lineno is\nthe line number of source text (1-indexed so the first line is line 1) and\nthe col_offset is the UTF-8 byte offset of the first token that\ngenerated the node. The UTF-8 offset is recorded because the parser uses\nUTF-8 internally.
\n\n

The constructor of a class ast.T parses its arguments as follows:

\n
    \n
  • If there are positional arguments, there must be as many as there are items\nin T._fields; they will be assigned as attributes of these names.
  • \n
  • If there are keyword arguments, they will set the attributes of the same\nnames to the given values.
  • \n
\n

For example, to create and populate an ast.UnaryOp node, you could\nuse

\n
node = ast.UnaryOp()\nnode.op = ast.USub()\nnode.operand = ast.Num()\nnode.operand.n = 5\nnode.operand.lineno = 0\nnode.operand.col_offset = 0\nnode.lineno = 0\nnode.col_offset = 0\n
\n
\n

or the more compact

\n
node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),\n                   lineno=0, col_offset=0)\n
\n
\n

\nNew in version 2.6: The constructor as explained above was added. In Python 2.5 nodes had\nto be created by calling the class constructor without arguments and\nsetting the attributes afterwards.

\n
\n\n
\n
\n

31.2.2. Abstract Grammar

\n

The module defines a string constant __version__ which is the decimal\nSubversion revision number of the file shown below.

\n

The abstract grammar is currently defined as follows:

\n
-- ASDL's five builtin types are identifier, int, string, object, bool\n\nmodule Python version \"$Revision$\"\n{\n\tmod = Module(stmt* body)\n\t    | Interactive(stmt* body)\n\t    | Expression(expr body)\n\n\t    -- not really an actual node but useful in Jython's typesystem.\n\t    | Suite(stmt* body)\n\n\tstmt = FunctionDef(identifier name, arguments args, \n                            stmt* body, expr* decorator_list)\n\t      | ClassDef(identifier name, expr* bases, stmt* body, expr* decorator_list)\n\t      | Return(expr? value)\n\n\t      | Delete(expr* targets)\n\t      | Assign(expr* targets, expr value)\n\t      | AugAssign(expr target, operator op, expr value)\n\n\t      -- not sure if bool is allowed, can always use int\n \t      | Print(expr? dest, expr* values, bool nl)\n\n\t      -- use 'orelse' because else is a keyword in target languages\n\t      | For(expr target, expr iter, stmt* body, stmt* orelse)\n\t      | While(expr test, stmt* body, stmt* orelse)\n\t      | If(expr test, stmt* body, stmt* orelse)\n\t      | With(expr context_expr, expr? optional_vars, stmt* body)\n\n\t      -- 'type' is a bad name\n\t      | Raise(expr? type, expr? inst, expr? tback)\n\t      | TryExcept(stmt* body, excepthandler* handlers, stmt* orelse)\n\t      | TryFinally(stmt* body, stmt* finalbody)\n\t      | Assert(expr test, expr? msg)\n\n\t      | Import(alias* names)\n\t      | ImportFrom(identifier? module, alias* names, int? level)\n\n\t      -- Doesn't capture requirement that locals must be\n\t      -- defined if globals is\n\t      -- still supports use as a function!\n\t      | Exec(expr body, expr? globals, expr? locals)\n\n\t      | Global(identifier* names)\n\t      | Expr(expr value)\n\t      | Pass | Break | Continue\n\n\t      -- XXX Jython will be different\n\t      -- col_offset is the byte offset in the utf8 string the parser uses\n\t      attributes (int lineno, int col_offset)\n\n\t      -- BoolOp() can use left & right?\n\texpr = BoolOp(boolop op, expr* values)\n\t     | BinOp(expr left, operator op, expr right)\n\t     | UnaryOp(unaryop op, expr operand)\n\t     | Lambda(arguments args, expr body)\n\t     | IfExp(expr test, expr body, expr orelse)\n\t     | Dict(expr* keys, expr* values)\n\t     | Set(expr* elts)\n\t     | ListComp(expr elt, comprehension* generators)\n\t     | SetComp(expr elt, comprehension* generators)\n\t     | DictComp(expr key, expr value, comprehension* generators)\n\t     | GeneratorExp(expr elt, comprehension* generators)\n\t     -- the grammar constrains where yield expressions can occur\n\t     | Yield(expr? value)\n\t     -- need sequences for compare to distinguish between\n\t     -- x < 4 < 3 and (x < 4) < 3\n\t     | Compare(expr left, cmpop* ops, expr* comparators)\n\t     | Call(expr func, expr* args, keyword* keywords,\n\t\t\t expr? starargs, expr? kwargs)\n\t     | Repr(expr value)\n\t     | Num(object n) -- a number as a PyObject.\n\t     | Str(string s) -- need to specify raw, unicode, etc?\n\t     -- other literals? bools?\n\n\t     -- the following expression can appear in assignment context\n\t     | Attribute(expr value, identifier attr, expr_context ctx)\n\t     | Subscript(expr value, slice slice, expr_context ctx)\n\t     | Name(identifier id, expr_context ctx)\n\t     | List(expr* elts, expr_context ctx) \n\t     | Tuple(expr* elts, expr_context ctx)\n\n\t      -- col_offset is the byte offset in the utf8 string the parser uses\n\t      attributes (int lineno, int col_offset)\n\n\texpr_context = Load | Store | Del | AugLoad | AugStore | Param\n\n\tslice = Ellipsis | Slice(expr? lower, expr? upper, expr? step) \n\t      | ExtSlice(slice* dims) \n\t      | Index(expr value) \n\n\tboolop = And | Or \n\n\toperator = Add | Sub | Mult | Div | Mod | Pow | LShift \n                 | RShift | BitOr | BitXor | BitAnd | FloorDiv\n\n\tunaryop = Invert | Not | UAdd | USub\n\n\tcmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn\n\n\tcomprehension = (expr target, expr iter, expr* ifs)\n\n\t-- not sure what to call the first argument for raise and except\n\texcepthandler = ExceptHandler(expr? type, expr? name, stmt* body)\n\t                attributes (int lineno, int col_offset)\n\n\targuments = (expr* args, identifier? vararg, \n\t\t     identifier? kwarg, expr* defaults)\n\n        -- keyword arguments supplied to call\n        keyword = (identifier arg, expr value)\n\n        -- import name with optional 'as' alias.\n        alias = (identifier name, identifier? asname)\n}\n
\n
\n
\n
\n

31.2.3. ast Helpers

\n

\nNew in version 2.6.

\n

Apart from the node classes, ast module defines these utility functions\nand classes for traversing abstract syntax trees:

\n
\n
\nast.parse(source, filename='<unknown>', mode='exec')
\n
Parse the source into an AST node. Equivalent to compile(source,\nfilename, mode, ast.PyCF_ONLY_AST).
\n\n
\n
\nast.literal_eval(node_or_string)
\n

Safely evaluate an expression node or a string containing a Python\nexpression. The string or node provided may only consist of the following\nPython literal structures: strings, numbers, tuples, lists, dicts, booleans,\nand None.

\n

This can be used for safely evaluating strings containing Python expressions\nfrom untrusted sources without the need to parse the values oneself.

\n
\n\n
\n
\nast.get_docstring(node, clean=True)
\n
Return the docstring of the given node (which must be a\nFunctionDef, ClassDef or Module node), or None\nif it has no docstring. If clean is true, clean up the docstring’s\nindentation with inspect.cleandoc().
\n\n
\n
\nast.fix_missing_locations(node)
\n
When you compile a node tree with compile(), the compiler expects\nlineno and col_offset attributes for every node that supports\nthem. This is rather tedious to fill in for generated nodes, so this helper\nadds these attributes recursively where not already set, by setting them to\nthe values of the parent node. It works recursively starting at node.
\n\n
\n
\nast.increment_lineno(node, n=1)
\n
Increment the line number of each node in the tree starting at node by n.\nThis is useful to “move code” to a different location in a file.
\n\n
\n
\nast.copy_location(new_node, old_node)
\n
Copy source location (lineno and col_offset) from old_node\nto new_node if possible, and return new_node.
\n\n
\n
\nast.iter_fields(node)
\n
Yield a tuple of (fieldname, value) for each field in node._fields\nthat is present on node.
\n\n
\n
\nast.iter_child_nodes(node)
\n
Yield all direct child nodes of node, that is, all fields that are nodes\nand all items of fields that are lists of nodes.
\n\n
\n
\nast.walk(node)
\n
Recursively yield all descendant nodes in the tree starting at node\n(including node itself), in no specified order. This is useful if you only\nwant to modify nodes in place and don’t care about the context.
\n\n
\n
\nclass ast.NodeVisitor
\n

A node visitor base class that walks the abstract syntax tree and calls a\nvisitor function for every node found. This function may return a value\nwhich is forwarded by the visit() method.

\n

This class is meant to be subclassed, with the subclass adding visitor\nmethods.

\n
\n
\nvisit(node)
\n
Visit a node. The default implementation calls the method called\nself.visit_classname where classname is the name of the node\nclass, or generic_visit() if that method doesn’t exist.
\n\n
\n
\ngeneric_visit(node)
\n

This visitor calls visit() on all children of the node.

\n

Note that child nodes of nodes that have a custom visitor method won’t be\nvisited unless the visitor calls generic_visit() or visits them\nitself.

\n
\n\n

Don’t use the NodeVisitor if you want to apply changes to nodes\nduring traversal. For this a special visitor exists\n(NodeTransformer) that allows modifications.

\n
\n\n
\n
\nclass ast.NodeTransformer
\n

A NodeVisitor subclass that walks the abstract syntax tree and\nallows modification of nodes.

\n

The NodeTransformer will walk the AST and use the return value of\nthe visitor methods to replace or remove the old node. If the return value\nof the visitor method is None, the node will be removed from its\nlocation, otherwise it is replaced with the return value. The return value\nmay be the original node in which case no replacement takes place.

\n

Here is an example transformer that rewrites all occurrences of name lookups\n(foo) to data['foo']:

\n
class RewriteName(NodeTransformer):\n\n    def visit_Name(self, node):\n        return copy_location(Subscript(\n            value=Name(id='data', ctx=Load()),\n            slice=Index(value=Str(s=node.id)),\n            ctx=node.ctx\n        ), node)\n
\n
\n

Keep in mind that if the node you’re operating on has child nodes you must\neither transform the child nodes yourself or call the generic_visit()\nmethod for the node first.

\n

For nodes that were part of a collection of statements (that applies to all\nstatement nodes), the visitor may also return a list of nodes rather than\njust a single node.

\n

Usually you use the transformer like this:

\n
node = YourTransformer().visit(node)\n
\n
\n
\n\n
\n
\nast.dump(node, annotate_fields=True, include_attributes=False)
\n
Return a formatted dump of the tree in node. This is mainly useful for\ndebugging purposes. The returned string will show the names and the values\nfor fields. This makes the code impossible to evaluate, so if evaluation is\nwanted annotate_fields must be set to False. Attributes such as line\nnumbers and column offsets are not dumped by default. If this is wanted,\ninclude_attributes can be set to True.
\n\n
\n
", "searchableItems": [ { "name": "ast.AST", "domId": "ast_ast.AST" }, { "name": "ast.copy_location", "domId": "ast_ast.copy_location" }, { "name": "ast.dump", "domId": "ast_ast.dump" }, { "name": "ast.fix_missing_locations", "domId": "ast_ast.fix_missing_locations" }, { "name": "ast.get_docstring", "domId": "ast_ast.get_docstring" }, { "name": "ast.increment_lineno", "domId": "ast_ast.increment_lineno" }, { "name": "ast.iter_child_nodes", "domId": "ast_ast.iter_child_nodes" }, { "name": "ast.iter_fields", "domId": "ast_ast.iter_fields" }, { "name": "ast.literal_eval", "domId": "ast_ast.literal_eval" }, { "name": "ast.NodeTransformer", "domId": "ast_ast.NodeTransformer" }, { "name": "ast.NodeVisitor", "domId": "ast_ast.NodeVisitor" }, { "name": "ast.NodeVisitor.generic_visit", "domId": "ast_ast.NodeVisitor.generic_visit" }, { "name": "ast.NodeVisitor.visit", "domId": "ast_ast.NodeVisitor.visit" }, { "name": "ast.parse", "domId": "ast_ast.parse" }, { "name": "ast.walk", "domId": "ast_ast.walk" } ] }, { "url": "http://docs.python.org/library/symtable.html", "title": "symtable", "html": "
\n

31.3. symtable — Access to the compiler’s symbol tables

\n

Symbol tables are generated by the compiler from AST just before bytecode is\ngenerated. The symbol table is responsible for calculating the scope of every\nidentifier in the code. symtable provides an interface to examine these\ntables.

\n
\n

31.3.1. Generating Symbol Tables

\n
\n
\nsymtable.symtable(code, filename, compile_type)
\n
Return the toplevel SymbolTable for the Python source code.\nfilename is the name of the file containing the code. compile_type is\nlike the mode argument to compile().
\n\n
\n
\n

31.3.2. Examining Symbol Tables

\n
\n
\nclass symtable.SymbolTable
\n

A namespace table for a block. The constructor is not public.

\n
\n
\nget_type()
\n
Return the type of the symbol table. Possible values are 'class',\n'module', and 'function'.
\n\n
\n
\nget_id()
\n
Return the table’s identifier.
\n\n
\n
\nget_name()
\n
Return the table’s name. This is the name of the class if the table is\nfor a class, the name of the function if the table is for a function, or\n'top' if the table is global (get_type() returns 'module').
\n\n
\n
\nget_lineno()
\n
Return the number of the first line in the block this table represents.
\n\n
\n
\nis_optimized()
\n
Return True if the locals in this table can be optimized.
\n\n
\n
\nis_nested()
\n
Return True if the block is a nested class or function.
\n\n
\n
\nhas_children()
\n
Return True if the block has nested namespaces within it. These can\nbe obtained with get_children().
\n\n
\n
\nhas_exec()
\n
Return True if the block uses exec.
\n\n
\n
\nhas_import_star()
\n
Return True if the block uses a starred from-import.
\n\n
\n
\nget_identifiers()
\n
Return a list of names of symbols in this table.
\n\n
\n
\nlookup(name)
\n
Lookup name in the table and return a Symbol instance.
\n\n
\n
\nget_symbols()
\n
Return a list of Symbol instances for names in the table.
\n\n
\n
\nget_children()
\n
Return a list of the nested symbol tables.
\n\n
\n\n
\n
\nclass symtable.Function
\n

A namespace for a function or method. This class inherits\nSymbolTable.

\n
\n
\nget_parameters()
\n
Return a tuple containing names of parameters to this function.
\n\n
\n
\nget_locals()
\n
Return a tuple containing names of locals in this function.
\n\n
\n
\nget_globals()
\n
Return a tuple containing names of globals in this function.
\n\n
\n
\nget_frees()
\n
Return a tuple containing names of free variables in this function.
\n\n
\n\n
\n
\nclass symtable.Class
\n

A namespace of a class. This class inherits SymbolTable.

\n
\n
\nget_methods()
\n
Return a tuple containing the names of methods declared in the class.
\n\n
\n\n
\n
\nclass symtable.Symbol
\n

An entry in a SymbolTable corresponding to an identifier in the\nsource. The constructor is not public.

\n
\n
\nget_name()
\n
Return the symbol’s name.
\n\n
\n
\nis_referenced()
\n
Return True if the symbol is used in its block.
\n\n
\n
\nis_imported()
\n
Return True if the symbol is created from an import statement.
\n\n
\n
\nis_parameter()
\n
Return True if the symbol is a parameter.
\n\n
\n
\nis_global()
\n
Return True if the symbol is global.
\n\n
\n
\nis_declared_global()
\n
Return True if the symbol is declared global with a global statement.
\n\n
\n
\nis_local()
\n
Return True if the symbol is local to its block.
\n\n
\n
\nis_free()
\n
Return True if the symbol is referenced in its block, but not assigned\nto.
\n\n
\n
\nis_assigned()
\n
Return True if the symbol is assigned to in its block.
\n\n
\n
\nis_namespace()
\n

Return True if name binding introduces new namespace.

\n

If the name is used as the target of a function or class statement, this\nwill be true.

\n

For example:

\n
>>> table = symtable.symtable("def some_func(): pass", "string", "exec")\n>>> table.lookup("some_func").is_namespace()\nTrue\n
\n
\n

Note that a single name can be bound to multiple objects. If the result\nis True, the name may also be bound to other objects, like an int or\nlist, that does not introduce a new namespace.

\n
\n\n
\n
\nget_namespaces()
\n
Return a list of namespaces bound to this name.
\n\n
\n
\nget_namespace()
\n
Return the namespace bound to this name. If more than one namespace is\nbound, a ValueError is raised.
\n\n
\n\n
\n
", "searchableItems": [ { "name": "symtable.Class", "domId": "symtable_symtable.Class" }, { "name": "symtable.Class.get_methods", "domId": "symtable_symtable.Class.get_methods" }, { "name": "symtable.Function", "domId": "symtable_symtable.Function" }, { "name": "symtable.Function.get_frees", "domId": "symtable_symtable.Function.get_frees" }, { "name": "symtable.Function.get_globals", "domId": "symtable_symtable.Function.get_globals" }, { "name": "symtable.Function.get_locals", "domId": "symtable_symtable.Function.get_locals" }, { "name": "symtable.Function.get_parameters", "domId": "symtable_symtable.Function.get_parameters" }, { "name": "symtable.Symbol", "domId": "symtable_symtable.Symbol" }, { "name": "symtable.Symbol.get_name", "domId": "symtable_symtable.Symbol.get_name" }, { "name": "symtable.Symbol.get_namespace", "domId": "symtable_symtable.Symbol.get_namespace" }, { "name": "symtable.Symbol.get_namespaces", "domId": "symtable_symtable.Symbol.get_namespaces" }, { "name": "symtable.Symbol.is_assigned", "domId": "symtable_symtable.Symbol.is_assigned" }, { "name": "symtable.Symbol.is_declared_global", "domId": "symtable_symtable.Symbol.is_declared_global" }, { "name": "symtable.Symbol.is_free", "domId": "symtable_symtable.Symbol.is_free" }, { "name": "symtable.Symbol.is_global", "domId": "symtable_symtable.Symbol.is_global" }, { "name": "symtable.Symbol.is_imported", "domId": "symtable_symtable.Symbol.is_imported" }, { "name": "symtable.Symbol.is_local", "domId": "symtable_symtable.Symbol.is_local" }, { "name": "symtable.Symbol.is_namespace", "domId": "symtable_symtable.Symbol.is_namespace" }, { "name": "symtable.Symbol.is_parameter", "domId": "symtable_symtable.Symbol.is_parameter" }, { "name": "symtable.Symbol.is_referenced", "domId": "symtable_symtable.Symbol.is_referenced" }, { "name": "symtable.SymbolTable", "domId": "symtable_symtable.SymbolTable" }, { "name": "symtable.SymbolTable.get_children", "domId": "symtable_symtable.SymbolTable.get_children" }, { "name": "symtable.SymbolTable.get_id", "domId": "symtable_symtable.SymbolTable.get_id" }, { "name": "symtable.SymbolTable.get_identifiers", "domId": "symtable_symtable.SymbolTable.get_identifiers" }, { "name": "symtable.SymbolTable.get_lineno", "domId": "symtable_symtable.SymbolTable.get_lineno" }, { "name": "symtable.SymbolTable.get_name", "domId": "symtable_symtable.SymbolTable.get_name" }, { "name": "symtable.SymbolTable.get_symbols", "domId": "symtable_symtable.SymbolTable.get_symbols" }, { "name": "symtable.SymbolTable.get_type", "domId": "symtable_symtable.SymbolTable.get_type" }, { "name": "symtable.SymbolTable.has_children", "domId": "symtable_symtable.SymbolTable.has_children" }, { "name": "symtable.SymbolTable.has_exec", "domId": "symtable_symtable.SymbolTable.has_exec" }, { "name": "symtable.SymbolTable.has_import_star", "domId": "symtable_symtable.SymbolTable.has_import_star" }, { "name": "symtable.SymbolTable.is_nested", "domId": "symtable_symtable.SymbolTable.is_nested" }, { "name": "symtable.SymbolTable.is_optimized", "domId": "symtable_symtable.SymbolTable.is_optimized" }, { "name": "symtable.SymbolTable.lookup", "domId": "symtable_symtable.SymbolTable.lookup" }, { "name": "symtable.symtable", "domId": "symtable_symtable.symtable" } ] }, { "url": "http://docs.python.org/library/keyword.html", "title": "keyword", "html": "
\n

31.6. keyword — Testing for Python keywords

\n

Source code: Lib/keyword.py

\n
\n

This module allows a Python program to determine if a string is a keyword.

\n
\n
\nkeyword.iskeyword(s)
\n
Return true if s is a Python keyword.
\n\n
\n
\nkeyword.kwlist
\n
Sequence containing all the keywords defined for the interpreter. If any\nkeywords are defined to only be active when particular __future__\nstatements are in effect, these will be included as well.
\n\n
", "searchableItems": [ { "name": "keyword.iskeyword", "domId": "keyword_keyword.iskeyword" } ] }, { "url": "http://docs.python.org/library/token.html", "title": "token", "html": "
\n

31.5. token — Constants used with Python parse trees

\n

Source code: Lib/token.py

\n
\n

This module provides constants which represent the numeric values of leaf nodes\nof the parse tree (terminal tokens). Refer to the file Grammar/Grammar\nin the Python distribution for the definitions of the names in the context of\nthe language grammar. The specific numeric values which the names map to may\nchange between Python versions.

\n

The module also provides a mapping from numeric codes to names and some\nfunctions. The functions mirror definitions in the Python C header files.

\n
\n
\ntoken.tok_name
\n
Dictionary mapping the numeric values of the constants defined in this module\nback to name strings, allowing more human-readable representation of parse trees\nto be generated.
\n\n
\n
\ntoken.ISTERMINAL(x)
\n
Return true for terminal token values.
\n\n
\n
\ntoken.ISNONTERMINAL(x)
\n
Return true for non-terminal token values.
\n\n
\n
\ntoken.ISEOF(x)
\n
Return true if x is the marker indicating the end of input.
\n\n

The token constants are:

\n
\n
\ntoken.ENDMARKER
\n
\ntoken.NAME
\n
\ntoken.NUMBER
\n
\ntoken.STRING
\n
\ntoken.NEWLINE
\n
\ntoken.INDENT
\n
\ntoken.DEDENT
\n
\ntoken.LPAR
\n
\ntoken.RPAR
\n
\ntoken.LSQB
\n
\ntoken.RSQB
\n
\ntoken.COLON
\n
\ntoken.COMMA
\n
\ntoken.SEMI
\n
\ntoken.PLUS
\n
\ntoken.MINUS
\n
\ntoken.STAR
\n
\ntoken.SLASH
\n
\ntoken.VBAR
\n
\ntoken.AMPER
\n
\ntoken.LESS
\n
\ntoken.GREATER
\n
\ntoken.EQUAL
\n
\ntoken.DOT
\n
\ntoken.PERCENT
\n
\ntoken.BACKQUOTE
\n
\ntoken.LBRACE
\n
\ntoken.RBRACE
\n
\ntoken.EQEQUAL
\n
\ntoken.NOTEQUAL
\n
\ntoken.LESSEQUAL
\n
\ntoken.GREATEREQUAL
\n
\ntoken.TILDE
\n
\ntoken.CIRCUMFLEX
\n
\ntoken.LEFTSHIFT
\n
\ntoken.RIGHTSHIFT
\n
\ntoken.DOUBLESTAR
\n
\ntoken.PLUSEQUAL
\n
\ntoken.MINEQUAL
\n
\ntoken.STAREQUAL
\n
\ntoken.SLASHEQUAL
\n
\ntoken.PERCENTEQUAL
\n
\ntoken.AMPEREQUAL
\n
\ntoken.VBAREQUAL
\n
\ntoken.CIRCUMFLEXEQUAL
\n
\ntoken.LEFTSHIFTEQUAL
\n
\ntoken.RIGHTSHIFTEQUAL
\n
\ntoken.DOUBLESTAREQUAL
\n
\ntoken.DOUBLESLASH
\n
\ntoken.DOUBLESLASHEQUAL
\n
\ntoken.AT
\n
\ntoken.OP
\n
\ntoken.ERRORTOKEN
\n
\ntoken.N_TOKENS
\n
\ntoken.NT_OFFSET
\n
\n\n
\n

See also

\n
\n
Module parser
\n
The second example for the parser module shows how to use the\nsymbol module.
\n
\n
\n
", "searchableItems": [ { "name": "token.ISEOF", "domId": "token_token.ISEOF" }, { "name": "token.ISNONTERMINAL", "domId": "token_token.ISNONTERMINAL" }, { "name": "token.ISTERMINAL", "domId": "token_token.ISTERMINAL" } ] }, { "url": "http://docs.python.org/library/tabnanny.html", "title": "tabnanny", "html": "
\n

31.8. tabnanny — Detection of ambiguous indentation

\n

Source code: Lib/tabnanny.py

\n
\n

For the time being this module is intended to be called as a script. However it\nis possible to import it into an IDE and use the function check()\ndescribed below.

\n
\n

Note

\n

The API provided by this module is likely to change in future releases; such\nchanges may not be backward compatible.

\n
\n
\n
\ntabnanny.check(file_or_dir)
\n
If file_or_dir is a directory and not a symbolic link, then recursively\ndescend the directory tree named by file_or_dir, checking all .py\nfiles along the way. If file_or_dir is an ordinary Python source file, it is\nchecked for whitespace related problems. The diagnostic messages are written to\nstandard output using the print statement.
\n\n
\n
\ntabnanny.verbose
\n
Flag indicating whether to print verbose messages. This is incremented by the\n-v option if called as a script.
\n\n
\n
\ntabnanny.filename_only
\n
Flag indicating whether to print only the filenames of files containing\nwhitespace related problems. This is set to true by the -q option if called\nas a script.
\n\n
\n
\nexception tabnanny.NannyNag
\n
Raised by tokeneater() if detecting an ambiguous indent. Captured and\nhandled in check().
\n\n
\n
\ntabnanny.tokeneater(type, token, start, end, line)
\n
This function is used by check() as a callback parameter to the function\ntokenize.tokenize().
\n\n
\n

See also

\n
\n
Module tokenize
\n
Lexical scanner for Python source code.
\n
\n
\n
", "searchableItems": [ { "name": "tabnanny.check", "domId": "tabnanny_tabnanny.check" }, { "name": "tabnanny.tokeneater", "domId": "tabnanny_tabnanny.tokeneater" } ] }, { "url": "http://docs.python.org/library/pyclbr.html", "title": "pyclbr", "html": "
\n

31.9. pyclbr — Python class browser support

\n

Source code: Lib/pyclbr.py

\n
\n

The pyclbr module can be used to determine some limited information\nabout the classes, methods and top-level functions defined in a module. The\ninformation provided is sufficient to implement a traditional three-pane\nclass browser. The information is extracted from the source code rather\nthan by importing the module, so this module is safe to use with untrusted\ncode. This restriction makes it impossible to use this module with modules\nnot implemented in Python, including all standard and optional extension\nmodules.

\n
\n
\npyclbr.readmodule(module[, path=None])
\n
Read a module and return a dictionary mapping class names to class\ndescriptor objects. The parameter module should be the name of a\nmodule as a string; it may be the name of a module within a package. The\npath parameter should be a sequence, and is used to augment the value\nof sys.path, which is used to locate module source code.
\n\n
\n
\npyclbr.readmodule_ex(module[, path=None])
\n
Like readmodule(), but the returned dictionary, in addition to\nmapping class names to class descriptor objects, also maps top-level\nfunction names to function descriptor objects. Moreover, if the module\nbeing read is a package, the key '__path__' in the returned\ndictionary has as its value a list which contains the package search\npath.
\n\n
\n

31.9.1. Class Objects

\n

The Class objects used as values in the dictionary returned by\nreadmodule() and readmodule_ex() provide the following data\nattributes:

\n
\n
\nClass.module
\n
The name of the module defining the class described by the class descriptor.
\n\n
\n
\nClass.name
\n
The name of the class.
\n\n
\n
\nClass.super
\n
A list of Class objects which describe the immediate base\nclasses of the class being described. Classes which are named as\nsuperclasses but which are not discoverable by readmodule() are\nlisted as a string with the class name instead of as Class\nobjects.
\n\n
\n
\nClass.methods
\n
A dictionary mapping method names to line numbers.
\n\n
\n
\nClass.file
\n
Name of the file containing the class statement defining the class.
\n\n
\n
\nClass.lineno
\n
The line number of the class statement within the file named by\nfile.
\n\n
\n
\n

31.9.2. Function Objects

\n

The Function objects used as values in the dictionary returned by\nreadmodule_ex() provide the following attributes:

\n
\n
\nFunction.module
\n
The name of the module defining the function described by the function\ndescriptor.
\n\n
\n
\nFunction.name
\n
The name of the function.
\n\n
\n
\nFunction.file
\n
Name of the file containing the def statement defining the function.
\n\n
\n
\nFunction.lineno
\n
The line number of the def statement within the file named by\nfile.
\n\n
\n
", "searchableItems": [ { "name": "pyclbr.readmodule", "domId": "pyclbr_pyclbr.readmodule" }, { "name": "pyclbr.readmodule_ex", "domId": "pyclbr_pyclbr.readmodule_ex" } ] }, { "url": "http://docs.python.org/library/py_compile.html", "title": "py_compile", "html": "
\n

31.10. py_compile — Compile Python source files

\n

Source code: Lib/py_compile.py

\n
\n

The py_compile module provides a function to generate a byte-code file\nfrom a source file, and another function used when the module source file is\ninvoked as a script.

\n

Though not often needed, this function can be useful when installing modules for\nshared use, especially if some of the users may not have permission to write the\nbyte-code cache files in the directory containing the source code.

\n
\n
\nexception py_compile.PyCompileError
\n
Exception raised when an error occurs while attempting to compile the file.
\n\n
\n
\npy_compile.compile(file[, cfile[, dfile[, doraise]]])
\n
Compile a source file to byte-code and write out the byte-code cache file. The\nsource code is loaded from the file name file. The byte-code is written to\ncfile, which defaults to file + 'c' ('o' if optimization is\nenabled in the current interpreter). If dfile is specified, it is used as the\nname of the source file in error messages instead of file. If doraise is\ntrue, a PyCompileError is raised when an error is encountered while\ncompiling file. If doraise is false (the default), an error string is\nwritten to sys.stderr, but no exception is raised.
\n\n
\n
\npy_compile.main([args])
\n

Compile several source files. The files named in args (or on the command\nline, if args is not specified) are compiled and the resulting bytecode is\ncached in the normal manner. This function does not search a directory\nstructure to locate source files; it only compiles files named explicitly.\nIf '-' is the only parameter in args, the list of files is taken from\nstandard input.

\n

\nChanged in version 2.7: Added support for '-'.

\n
\n\n

When this module is run as a script, the main() is used to compile all the\nfiles named on the command line. The exit status is nonzero if one of the files\ncould not be compiled.

\n

\nChanged in version 2.6: Added the nonzero exit status when module is run as a script.

\n
\n

See also

\n
\n
Module compileall
\n
Utilities to compile all Python source files in a directory tree.
\n
\n
\n
", "searchableItems": [ { "name": "py_compile.compile", "domId": "py_compile_py_compile.compile" }, { "name": "py_compile.main", "domId": "py_compile_py_compile.main" } ] }, { "url": "http://docs.python.org/library/tokenize.html", "title": "tokenize", "html": "
\n

31.7. tokenize — Tokenizer for Python source

\n

Source code: Lib/tokenize.py

\n
\n

The tokenize module provides a lexical scanner for Python source code,\nimplemented in Python. The scanner in this module returns comments as tokens as\nwell, making it useful for implementing “pretty-printers,” including colorizers\nfor on-screen displays.

\n

The primary entry point is a generator:

\n
\n
\ntokenize.generate_tokens(readline)
\n

The generate_tokens() generator requires one argument, readline,\nwhich must be a callable object which provides the same interface as the\nreadline() method of built-in file objects (see section\nFile Objects). Each call to the function should return one line\nof input as a string.

\n

The generator produces 5-tuples with these members: the token type; the token\nstring; a 2-tuple (srow, scol) of ints specifying the row and column\nwhere the token begins in the source; a 2-tuple (erow, ecol) of ints\nspecifying the row and column where the token ends in the source; and the\nline on which the token was found. The line passed (the last tuple item) is\nthe logical line; continuation lines are included.

\n

\nNew in version 2.2.

\n
\n\n

An older entry point is retained for backward compatibility:

\n
\n
\ntokenize.tokenize(readline[, tokeneater])
\n

The tokenize() function accepts two parameters: one representing the input\nstream, and one providing an output mechanism for tokenize().

\n

The first parameter, readline, must be a callable object which provides the\nsame interface as the readline() method of built-in file objects (see\nsection File Objects). Each call to the function should return one\nline of input as a string. Alternately, readline may be a callable object that\nsignals completion by raising StopIteration.

\n

\nChanged in version 2.5: Added StopIteration support.

\n

The second parameter, tokeneater, must also be a callable object. It is\ncalled once for each token, with five arguments, corresponding to the tuples\ngenerated by generate_tokens().

\n
\n\n

All constants from the token module are also exported from\ntokenize, as are two additional token type values that might be passed to\nthe tokeneater function by tokenize():

\n
\n
\ntokenize.COMMENT
\n
Token value used to indicate a comment.
\n\n
\n
\ntokenize.NL
\n
Token value used to indicate a non-terminating newline. The NEWLINE token\nindicates the end of a logical line of Python code; NL tokens are generated when\na logical line of code is continued over multiple physical lines.
\n\n

Another function is provided to reverse the tokenization process. This is useful\nfor creating tools that tokenize a script, modify the token stream, and write\nback the modified script.

\n
\n
\ntokenize.untokenize(iterable)
\n

Converts tokens back into Python source code. The iterable must return\nsequences with at least two elements, the token type and the token string. Any\nadditional sequence elements are ignored.

\n

The reconstructed script is returned as a single string. The result is\nguaranteed to tokenize back to match the input so that the conversion is\nlossless and round-trips are assured. The guarantee applies only to the token\ntype and token string as the spacing between tokens (column positions) may\nchange.

\n

\nNew in version 2.5.

\n
\n\n

Example of a script re-writer that transforms float literals into Decimal\nobjects:

\n
def decistmt(s):\n    """Substitute Decimals for floats in a string of statements.\n\n    >>> from decimal import Decimal\n    >>> s = 'print +21.3e-5*-.1234/81.7'\n    >>> decistmt(s)\n    "print +Decimal ('21.3e-5')*-Decimal ('.1234')/Decimal ('81.7')"\n\n    >>> exec(s)\n    -3.21716034272e-007\n    >>> exec(decistmt(s))\n    -3.217160342717258261933904529E-7\n\n    """\n    result = []\n    g = generate_tokens(StringIO(s).readline)   # tokenize the string\n    for toknum, tokval, _, _, _  in g:\n        if toknum == NUMBER and '.' in tokval:  # replace NUMBER tokens\n            result.extend([\n                (NAME, 'Decimal'),\n                (OP, '('),\n                (STRING, repr(tokval)),\n                (OP, ')')\n            ])\n        else:\n            result.append((toknum, tokval))\n    return untokenize(result)\n
\n
\n
", "searchableItems": [ { "name": "tokenize.generate_tokens", "domId": "tokenize_tokenize.generate_tokens" }, { "name": "tokenize.tokenize", "domId": "tokenize_tokenize.tokenize" }, { "name": "tokenize.untokenize", "domId": "tokenize_tokenize.untokenize" } ] }, { "url": "http://docs.python.org/library/compileall.html", "title": "compileall", "html": "
\n

31.11. compileall — Byte-compile Python libraries

\n

This module provides some utility functions to support installing Python\nlibraries. These functions compile Python source files in a directory tree.\nThis module can be used to create the cached byte-code files at library\ninstallation time, which makes them available for use even by users who don’t\nhave write permission to the library directories.

\n
\n

31.11.1. Command-line use

\n

This module can work as a script (using python -m compileall) to\ncompile Python sources.

\n
\n
\n[directory|file]...
\n
Positional arguments are files to compile or directories that contain\nsource files, traversed recursively. If no argument is given, behave as if\nthe command line was -l <directories from sys.path>.
\n\n
\n
\n-l
\n
Do not recurse into subdirectories, only compile source code files directly\ncontained in the named or implied directories.
\n\n
\n
\n-f
\n
Force rebuild even if timestamps are up-to-date.
\n\n
\n
\n-q
\n
Do not print the list of files compiled, print only error messages.
\n\n
\n
\n-d destdir
\n
Directory prepended to the path to each file being compiled. This will\nappear in compilation time tracebacks, and is also compiled in to the\nbyte-code file, where it will be used in tracebacks and other messages in\ncases where the source file does not exist at the time the byte-code file is\nexecuted.
\n\n
\n
\n-x regex
\n
regex is used to search the full path to each file considered for\ncompilation, and if the regex produces a match, the file is skipped.
\n\n
\n
\n-i list
\n
Read the file list and add each line that it contains to the list of\nfiles and directories to compile. If list is -, read lines from\nstdin.
\n\n

\nChanged in version 2.7: Added the -i option.

\n
\n
\n

31.11.2. Public functions

\n
\n
\ncompileall.compile_dir(dir[, maxlevels[, ddir[, force[, rx[, quiet]]]]])
\n

Recursively descend the directory tree named by dir, compiling all .py\nfiles along the way.

\n

The maxlevels parameter is used to limit the depth of the recursion; it\ndefaults to 10.

\n

If ddir is given, it is prepended to the path to each file being compiled\nfor use in compilation time tracebacks, and is also compiled in to the\nbyte-code file, where it will be used in tracebacks and other messages in\ncases where the source file does not exist at the time the byte-code file is\nexecuted.

\n

If force is true, modules are re-compiled even if the timestamps are up to\ndate.

\n

If rx is given, its search method is called on the complete path to each\nfile considered for compilation, and if it returns a true value, the file\nis skipped.

\n

If quiet is true, nothing is printed to the standard output unless errors\noccur.

\n
\n\n
\n
\ncompileall.compile_file(fullname[, ddir[, force[, rx[, quiet]]]])
\n

Compile the file with path fullname.

\n

If ddir is given, it is prepended to the path to the file being compiled\nfor use in compilation time tracebacks, and is also compiled in to the\nbyte-code file, where it will be used in tracebacks and other messages in\ncases where the source file does not exist at the time the byte-code file is\nexecuted.

\n

If rx is given, its search method is passed the full path name to the\nfile being compiled, and if it returns a true value, the file is not\ncompiled and True is returned.

\n

If quiet is true, nothing is printed to the standard output unless errors\noccur.

\n

\nNew in version 2.7.

\n
\n\n
\n
\ncompileall.compile_path([skip_curdir[, maxlevels[, force]]])
\n
Byte-compile all the .py files found along sys.path. If\nskip_curdir is true (the default), the current directory is not included\nin the search. All other parameters are passed to the compile_dir()\nfunction. Note that unlike the other compile functions, maxlevels\ndefaults to 0.
\n\n

To force a recompile of all the .py files in the Lib/\nsubdirectory and all its subdirectories:

\n
import compileall\n\ncompileall.compile_dir('Lib/', force=True)\n\n# Perform same compilation, excluding files in .svn directories.\nimport re\ncompileall.compile_dir('Lib/', rx=re.compile('/[.]svn'), force=True)\n
\n
\n
\n

See also

\n
\n
Module py_compile
\n
Byte-compile a single source file.
\n
\n
\n
\n
", "searchableItems": [ { "name": "compileall.compile_dir", "domId": "compileall_compileall.compile_dir" }, { "name": "compileall.compile_file", "domId": "compileall_compileall.compile_file" }, { "name": "compileall.compile_path", "domId": "compileall_compileall.compile_path" } ] }, { "url": "http://docs.python.org/library/pickletools.html", "title": "pickletools", "html": "
\n

31.13. pickletools — Tools for pickle developers

\n

\nNew in version 2.3.

\n

Source code: Lib/pickletools.py

\n
\n

This module contains various constants relating to the intimate details of the\npickle module, some lengthy comments about the implementation, and a few\nuseful functions for analyzing pickled data. The contents of this module are\nuseful for Python core developers who are working on the pickle and\ncPickle implementations; ordinary users of the pickle module\nprobably won’t find the pickletools module relevant.

\n
\n
\npickletools.dis(pickle[, out=None, memo=None, indentlevel=4])
\n
Outputs a symbolic disassembly of the pickle to the file-like object out,\ndefaulting to sys.stdout. pickle can be a string or a file-like object.\nmemo can be a Python dictionary that will be used as the pickle’s memo; it can\nbe used to perform disassemblies across multiple pickles created by the same\npickler. Successive levels, indicated by MARK opcodes in the stream, are\nindented by indentlevel spaces.
\n\n
\n
\npickletools.genops(pickle)
\n
Provides an iterator over all of the opcodes in a pickle, returning a\nsequence of (opcode, arg, pos) triples. opcode is an instance of an\nOpcodeInfo class; arg is the decoded value, as a Python object, of\nthe opcode’s argument; pos is the position at which this opcode is located.\npickle can be a string or a file-like object.
\n\n
\n
\npickletools.optimize(picklestring)
\n

Returns a new equivalent pickle string after eliminating unused PUT\nopcodes. The optimized pickle is shorter, takes less transmission time,\nrequires less storage space, and unpickles more efficiently.

\n

\nNew in version 2.6.

\n
\n\n
", "searchableItems": [ { "name": "pickletools.dis", "domId": "pickletools_pickletools.dis" }, { "name": "pickletools.genops", "domId": "pickletools_pickletools.genops" }, { "name": "pickletools.optimize", "domId": "pickletools_pickletools.optimize" } ] }, { "url": "http://docs.python.org/library/dis.html", "title": "dis", "html": "
\n

31.12. dis — Disassembler for Python bytecode

\n

Source code: Lib/dis.py

\n
\n

The dis module supports the analysis of CPython bytecode by\ndisassembling it. The CPython bytecode which this module takes as an\ninput is defined in the file Include/opcode.h and used by the compiler\nand the interpreter.

\n
\n

CPython implementation detail: Bytecode is an implementation detail of the CPython interpreter! No\nguarantees are made that bytecode will not be added, removed, or changed\nbetween versions of Python. Use of this module should not be considered to\nwork across Python VMs or Python releases.

\n
\n

Example: Given the function myfunc():

\n
def myfunc(alist):\n    return len(alist)\n
\n
\n

the following command can be used to get the disassembly of myfunc():

\n
>>> dis.dis(myfunc)\n  2           0 LOAD_GLOBAL              0 (len)\n              3 LOAD_FAST                0 (alist)\n              6 CALL_FUNCTION            1\n              9 RETURN_VALUE\n
\n
\n

(The “2” is a line number).

\n

The dis module defines the following functions and constants:

\n
\n
\ndis.dis([bytesource])
\n
Disassemble the bytesource object. bytesource can denote either a module, a\nclass, a method, a function, or a code object. For a module, it disassembles\nall functions. For a class, it disassembles all methods. For a single code\nsequence, it prints one line per bytecode instruction. If no object is\nprovided, it disassembles the last traceback.
\n\n
\n
\ndis.distb([tb])
\n
Disassembles the top-of-stack function of a traceback, using the last traceback\nif none was passed. The instruction causing the exception is indicated.
\n\n
\n
\ndis.disassemble(code[, lasti])
\n

Disassembles a code object, indicating the last instruction if lasti was\nprovided. The output is divided in the following columns:

\n
    \n
  1. the line number, for the first instruction of each line
  2. \n
  3. the current instruction, indicated as -->,
  4. \n
  5. a labelled instruction, indicated with >>,
  6. \n
  7. the address of the instruction,
  8. \n
  9. the operation code name,
  10. \n
  11. operation parameters, and
  12. \n
  13. interpretation of the parameters in parentheses.
  14. \n
\n

The parameter interpretation recognizes local and global variable names,\nconstant values, branch targets, and compare operators.

\n
\n\n
\n
\ndis.disco(code[, lasti])
\n
A synonym for disassemble(). It is more convenient to type, and kept\nfor compatibility with earlier Python releases.
\n\n
\n
\ndis.findlinestarts(code)
\n
This generator function uses the co_firstlineno and co_lnotab\nattributes of the code object code to find the offsets which are starts of\nlines in the source code. They are generated as (offset, lineno) pairs.
\n\n
\n
\ndis.findlabels(code)
\n
Detect all offsets in the code object code which are jump targets, and\nreturn a list of these offsets.
\n\n
\n
\ndis.opname
\n
Sequence of operation names, indexable using the bytecode.
\n\n
\n
\ndis.opmap
\n
Dictionary mapping operation names to bytecodes.
\n\n
\n
\ndis.cmp_op
\n
Sequence of all compare operation names.
\n\n
\n
\ndis.hasconst
\n
Sequence of bytecodes that have a constant parameter.
\n\n
\n
\ndis.hasfree
\n
Sequence of bytecodes that access a free variable.
\n\n
\n
\ndis.hasname
\n
Sequence of bytecodes that access an attribute by name.
\n\n
\n
\ndis.hasjrel
\n
Sequence of bytecodes that have a relative jump target.
\n\n
\n
\ndis.hasjabs
\n
Sequence of bytecodes that have an absolute jump target.
\n\n
\n
\ndis.haslocal
\n
Sequence of bytecodes that access a local variable.
\n\n
\n
\ndis.hascompare
\n
Sequence of bytecodes of Boolean operations.
\n\n
\n

31.12.1. Python Bytecode Instructions

\n

The Python compiler currently generates the following bytecode instructions.

\n
\n
\nSTOP_CODE()
\n
Indicates end-of-code to the compiler, not used by the interpreter.
\n\n
\n
\nNOP()
\n
Do nothing code. Used as a placeholder by the bytecode optimizer.
\n\n
\n
\nPOP_TOP()
\n
Removes the top-of-stack (TOS) item.
\n\n
\n
\nROT_TWO()
\n
Swaps the two top-most stack items.
\n\n
\n
\nROT_THREE()
\n
Lifts second and third stack item one position up, moves top down to position\nthree.
\n\n
\n
\nROT_FOUR()
\n
Lifts second, third and forth stack item one position up, moves top down to\nposition four.
\n\n
\n
\nDUP_TOP()
\n
Duplicates the reference on top of the stack.
\n\n

Unary Operations take the top of the stack, apply the operation, and push the\nresult back on the stack.

\n
\n
\nUNARY_POSITIVE()
\n
Implements TOS = +TOS.
\n\n
\n
\nUNARY_NEGATIVE()
\n
Implements TOS = -TOS.
\n\n
\n
\nUNARY_NOT()
\n
Implements TOS = not TOS.
\n\n
\n
\nUNARY_CONVERT()
\n
Implements TOS = `TOS`.
\n\n
\n
\nUNARY_INVERT()
\n
Implements TOS = ~TOS.
\n\n
\n
\nGET_ITER()
\n
Implements TOS = iter(TOS).
\n\n

Binary operations remove the top of the stack (TOS) and the second top-most\nstack item (TOS1) from the stack. They perform the operation, and put the\nresult back on the stack.

\n
\n
\nBINARY_POWER()
\n
Implements TOS = TOS1 ** TOS.
\n\n
\n
\nBINARY_MULTIPLY()
\n
Implements TOS = TOS1 * TOS.
\n\n
\n
\nBINARY_DIVIDE()
\n
Implements TOS = TOS1 / TOS when from __future__ import division is not\nin effect.
\n\n
\n
\nBINARY_FLOOR_DIVIDE()
\n
Implements TOS = TOS1 // TOS.
\n\n
\n
\nBINARY_TRUE_DIVIDE()
\n
Implements TOS = TOS1 / TOS when from __future__ import division is in\neffect.
\n\n
\n
\nBINARY_MODULO()
\n
Implements TOS = TOS1 TOS.
\n\n
\n
\nBINARY_ADD()
\n
Implements TOS = TOS1 + TOS.
\n\n
\n
\nBINARY_SUBTRACT()
\n
Implements TOS = TOS1 - TOS.
\n\n
\n
\nBINARY_SUBSCR()
\n
Implements TOS = TOS1[TOS].
\n\n
\n
\nBINARY_LSHIFT()
\n
Implements TOS = TOS1 << TOS.
\n\n
\n
\nBINARY_RSHIFT()
\n
Implements TOS = TOS1 >> TOS.
\n\n
\n
\nBINARY_AND()
\n
Implements TOS = TOS1 & TOS.
\n\n
\n
\nBINARY_XOR()
\n
Implements TOS = TOS1 ^ TOS.
\n\n
\n
\nBINARY_OR()
\n
Implements TOS = TOS1 | TOS.
\n\n

In-place operations are like binary operations, in that they remove TOS and\nTOS1, and push the result back on the stack, but the operation is done in-place\nwhen TOS1 supports it, and the resulting TOS may be (but does not have to be)\nthe original TOS1.

\n
\n
\nINPLACE_POWER()
\n
Implements in-place TOS = TOS1 ** TOS.
\n\n
\n
\nINPLACE_MULTIPLY()
\n
Implements in-place TOS = TOS1 * TOS.
\n\n
\n
\nINPLACE_DIVIDE()
\n
Implements in-place TOS = TOS1 / TOS when from __future__ import\ndivision is not in effect.
\n\n
\n
\nINPLACE_FLOOR_DIVIDE()
\n
Implements in-place TOS = TOS1 // TOS.
\n\n
\n
\nINPLACE_TRUE_DIVIDE()
\n
Implements in-place TOS = TOS1 / TOS when from __future__ import\ndivision is in effect.
\n\n
\n
\nINPLACE_MODULO()
\n
Implements in-place TOS = TOS1 TOS.
\n\n
\n
\nINPLACE_ADD()
\n
Implements in-place TOS = TOS1 + TOS.
\n\n
\n
\nINPLACE_SUBTRACT()
\n
Implements in-place TOS = TOS1 - TOS.
\n\n
\n
\nINPLACE_LSHIFT()
\n
Implements in-place TOS = TOS1 << TOS.
\n\n
\n
\nINPLACE_RSHIFT()
\n
Implements in-place TOS = TOS1 >> TOS.
\n\n
\n
\nINPLACE_AND()
\n
Implements in-place TOS = TOS1 & TOS.
\n\n
\n
\nINPLACE_XOR()
\n
Implements in-place TOS = TOS1 ^ TOS.
\n\n
\n
\nINPLACE_OR()
\n
Implements in-place TOS = TOS1 | TOS.
\n\n

The slice opcodes take up to three parameters.

\n
\n
\nSLICE+0()
\n
Implements TOS = TOS[:].
\n\n
\n
\nSLICE+1()
\n
Implements TOS = TOS1[TOS:].
\n\n
\n
\nSLICE+2()
\n
Implements TOS = TOS1[:TOS].
\n\n
\n
\nSLICE+3()
\n
Implements TOS = TOS2[TOS1:TOS].
\n\n

Slice assignment needs even an additional parameter. As any statement, they put\nnothing on the stack.

\n
\n
\nSTORE_SLICE+0()
\n
Implements TOS[:] = TOS1.
\n\n
\n
\nSTORE_SLICE+1()
\n
Implements TOS1[TOS:] = TOS2.
\n\n
\n
\nSTORE_SLICE+2()
\n
Implements TOS1[:TOS] = TOS2.
\n\n
\n
\nSTORE_SLICE+3()
\n
Implements TOS2[TOS1:TOS] = TOS3.
\n\n
\n
\nDELETE_SLICE+0()
\n
Implements del TOS[:].
\n\n
\n
\nDELETE_SLICE+1()
\n
Implements del TOS1[TOS:].
\n\n
\n
\nDELETE_SLICE+2()
\n
Implements del TOS1[:TOS].
\n\n
\n
\nDELETE_SLICE+3()
\n
Implements del TOS2[TOS1:TOS].
\n\n
\n
\nSTORE_SUBSCR()
\n
Implements TOS1[TOS] = TOS2.
\n\n
\n
\nDELETE_SUBSCR()
\n
Implements del TOS1[TOS].
\n\n

Miscellaneous opcodes.

\n
\n
\nPRINT_EXPR()
\n
Implements the expression statement for the interactive mode. TOS is removed\nfrom the stack and printed. In non-interactive mode, an expression statement is\nterminated with POP_STACK.
\n\n
\n
\nPRINT_ITEM()
\n
Prints TOS to the file-like object bound to sys.stdout. There is one such\ninstruction for each item in the print statement.
\n\n
\n
\nPRINT_ITEM_TO()
\n
Like PRINT_ITEM, but prints the item second from TOS to the file-like object\nat TOS. This is used by the extended print statement.
\n\n
\n
\nPRINT_NEWLINE()
\n
Prints a new line on sys.stdout. This is generated as the last operation of\na print statement, unless the statement ends with a comma.
\n\n
\n
\nPRINT_NEWLINE_TO()
\n
Like PRINT_NEWLINE, but prints the new line on the file-like object on the\nTOS. This is used by the extended print statement.
\n\n
\n
\nBREAK_LOOP()
\n
Terminates a loop due to a break statement.
\n\n
\n
\nCONTINUE_LOOP(target)
\n
Continues a loop due to a continue statement. target is the\naddress to jump to (which should be a FOR_ITER instruction).
\n\n
\n
\nLIST_APPEND(i)
\n
Calls list.append(TOS[-i], TOS). Used to implement list comprehensions.\nWhile the appended value is popped off, the list object remains on the\nstack so that it is available for further iterations of the loop.
\n\n
\n
\nLOAD_LOCALS()
\n
Pushes a reference to the locals of the current scope on the stack. This is used\nin the code for a class definition: After the class body is evaluated, the\nlocals are passed to the class definition.
\n\n
\n
\nRETURN_VALUE()
\n
Returns with TOS to the caller of the function.
\n\n
\n
\nYIELD_VALUE()
\n
Pops TOS and yields it from a generator.
\n\n
\n
\nIMPORT_STAR()
\n
Loads all symbols not starting with '_' directly from the module TOS to the\nlocal namespace. The module is popped after loading all names. This opcode\nimplements from module import *.
\n\n
\n
\nEXEC_STMT()
\n
Implements exec TOS2,TOS1,TOS. The compiler fills missing optional\nparameters with None.
\n\n
\n
\nPOP_BLOCK()
\n
Removes one block from the block stack. Per frame, there is a stack of blocks,\ndenoting nested loops, try statements, and such.
\n\n
\n
\nEND_FINALLY()
\n
Terminates a finally clause. The interpreter recalls whether the\nexception has to be re-raised, or whether the function returns, and continues\nwith the outer-next block.
\n\n
\n
\nBUILD_CLASS()
\n
Creates a new class object. TOS is the methods dictionary, TOS1 the tuple of\nthe names of the base classes, and TOS2 the class name.
\n\n
\n
\nSETUP_WITH(delta)
\n
This opcode performs several operations before a with block starts. First,\nit loads __exit__() from the context manager and pushes it onto\nthe stack for later use by WITH_CLEANUP. Then,\n__enter__() is called, and a finally block pointing to delta\nis pushed. Finally, the result of calling the enter method is pushed onto\nthe stack. The next opcode will either ignore it (POP_TOP), or\nstore it in (a) variable(s) (STORE_FAST, STORE_NAME, or\nUNPACK_SEQUENCE).
\n\n
\n
\nWITH_CLEANUP()
\n

Cleans up the stack when a with statement block exits. On top of\nthe stack are 1–3 values indicating how/why the finally clause was entered:

\n
    \n
  • TOP = None
  • \n
  • (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
  • \n
  • TOP = WHY_*; no retval below it
  • \n
  • (TOP, SECOND, THIRD) = exc_info()
  • \n
\n

Under them is EXIT, the context manager’s __exit__() bound method.

\n

In the last case, EXIT(TOP, SECOND, THIRD) is called, otherwise\nEXIT(None, None, None).

\n

EXIT is removed from the stack, leaving the values above it in the same\norder. In addition, if the stack represents an exception, and the function\ncall returns a ‘true’ value, this information is “zapped”, to prevent\nEND_FINALLY from re-raising the exception. (But non-local gotos should\nstill be resumed.)

\n
\n\n

All of the following opcodes expect arguments. An argument is two bytes, with\nthe more significant byte last.

\n
\n
\nSTORE_NAME(namei)
\n
Implements name = TOS. namei is the index of name in the attribute\nco_names of the code object. The compiler tries to use STORE_FAST\nor STORE_GLOBAL if possible.
\n\n
\n
\nDELETE_NAME(namei)
\n
Implements del name, where namei is the index into co_names\nattribute of the code object.
\n\n
\n
\nUNPACK_SEQUENCE(count)
\n
Unpacks TOS into count individual values, which are put onto the stack\nright-to-left.
\n\n
\n
\nDUP_TOPX(count)
\n
Duplicate count items, keeping them in the same order. Due to implementation\nlimits, count should be between 1 and 5 inclusive.
\n\n
\n
\nSTORE_ATTR(namei)
\n
Implements TOS.name = TOS1, where namei is the index of name in\nco_names.
\n\n
\n
\nDELETE_ATTR(namei)
\n
Implements del TOS.name, using namei as index into co_names.
\n\n
\n
\nSTORE_GLOBAL(namei)
\n
Works as STORE_NAME, but stores the name as a global.
\n\n
\n
\nDELETE_GLOBAL(namei)
\n
Works as DELETE_NAME, but deletes a global name.
\n\n
\n
\nLOAD_CONST(consti)
\n
Pushes co_consts[consti] onto the stack.
\n\n
\n
\nLOAD_NAME(namei)
\n
Pushes the value associated with co_names[namei] onto the stack.
\n\n
\n
\nBUILD_TUPLE(count)
\n
Creates a tuple consuming count items from the stack, and pushes the resulting\ntuple onto the stack.
\n\n
\n
\nBUILD_LIST(count)
\n
Works as BUILD_TUPLE, but creates a list.
\n\n
\n
\nBUILD_MAP(count)
\n
Pushes a new dictionary object onto the stack. The dictionary is pre-sized\nto hold count entries.
\n\n
\n
\nLOAD_ATTR(namei)
\n
Replaces TOS with getattr(TOS, co_names[namei]).
\n\n
\n
\nCOMPARE_OP(opname)
\n
Performs a Boolean operation. The operation name can be found in\ncmp_op[opname].
\n\n
\n
\nIMPORT_NAME(namei)
\n
Imports the module co_names[namei]. TOS and TOS1 are popped and provide\nthe fromlist and level arguments of __import__(). The module\nobject is pushed onto the stack. The current namespace is not affected:\nfor a proper import statement, a subsequent STORE_FAST instruction\nmodifies the namespace.
\n\n
\n
\nIMPORT_FROM(namei)
\n
Loads the attribute co_names[namei] from the module found in TOS. The\nresulting object is pushed onto the stack, to be subsequently stored by a\nSTORE_FAST instruction.
\n\n
\n
\nJUMP_FORWARD(delta)
\n
Increments bytecode counter by delta.
\n\n
\n
\nPOP_JUMP_IF_TRUE(target)
\n
If TOS is true, sets the bytecode counter to target. TOS is popped.
\n\n
\n
\nPOP_JUMP_IF_FALSE(target)
\n
If TOS is false, sets the bytecode counter to target. TOS is popped.
\n\n
\n
\nJUMP_IF_TRUE_OR_POP(target)
\n
If TOS is true, sets the bytecode counter to target and leaves TOS\non the stack. Otherwise (TOS is false), TOS is popped.
\n\n
\n
\nJUMP_IF_FALSE_OR_POP(target)
\n
If TOS is false, sets the bytecode counter to target and leaves\nTOS on the stack. Otherwise (TOS is true), TOS is popped.
\n\n
\n
\nJUMP_ABSOLUTE(target)
\n
Set bytecode counter to target.
\n\n
\n
\nFOR_ITER(delta)
\n
TOS is an iterator. Call its next() method. If this\nyields a new value, push it on the stack (leaving the iterator below it). If\nthe iterator indicates it is exhausted TOS is popped, and the bytecode\ncounter is incremented by delta.
\n\n
\n
\nLOAD_GLOBAL(namei)
\n
Loads the global named co_names[namei] onto the stack.
\n\n
\n
\nSETUP_LOOP(delta)
\n
Pushes a block for a loop onto the block stack. The block spans from the\ncurrent instruction with a size of delta bytes.
\n\n
\n
\nSETUP_EXCEPT(delta)
\n
Pushes a try block from a try-except clause onto the block stack. delta points\nto the first except block.
\n\n
\n
\nSETUP_FINALLY(delta)
\n
Pushes a try block from a try-except clause onto the block stack. delta points\nto the finally block.
\n\n
\n
\nSTORE_MAP()
\n
Store a key and value pair in a dictionary. Pops the key and value while leaving\nthe dictionary on the stack.
\n\n
\n
\nLOAD_FAST(var_num)
\n
Pushes a reference to the local co_varnames[var_num] onto the stack.
\n\n
\n
\nSTORE_FAST(var_num)
\n
Stores TOS into the local co_varnames[var_num].
\n\n
\n
\nDELETE_FAST(var_num)
\n
Deletes local co_varnames[var_num].
\n\n
\n
\nLOAD_CLOSURE(i)
\n
Pushes a reference to the cell contained in slot i of the cell and free\nvariable storage. The name of the variable is co_cellvars[i] if i is\nless than the length of co_cellvars. Otherwise it is co_freevars[i -\nlen(co_cellvars)].
\n\n
\n
\nLOAD_DEREF(i)
\n
Loads the cell contained in slot i of the cell and free variable storage.\nPushes a reference to the object the cell contains on the stack.
\n\n
\n
\nSTORE_DEREF(i)
\n
Stores TOS into the cell contained in slot i of the cell and free variable\nstorage.
\n\n
\n
\nSET_LINENO(lineno)
\n
This opcode is obsolete.
\n\n
\n
\nRAISE_VARARGS(argc)
\n
Raises an exception. argc indicates the number of parameters to the raise\nstatement, ranging from 0 to 3. The handler will find the traceback as TOS2,\nthe parameter as TOS1, and the exception as TOS.
\n\n
\n
\nCALL_FUNCTION(argc)
\n
Calls a function. The low byte of argc indicates the number of positional\nparameters, the high byte the number of keyword parameters. On the stack, the\nopcode finds the keyword parameters first. For each keyword argument, the value\nis on top of the key. Below the keyword parameters, the positional parameters\nare on the stack, with the right-most parameter on top. Below the parameters,\nthe function object to call is on the stack. Pops all function arguments, and\nthe function itself off the stack, and pushes the return value.
\n\n
\n
\nMAKE_FUNCTION(argc)
\n
Pushes a new function object on the stack. TOS is the code associated with the\nfunction. The function object is defined to have argc default parameters,\nwhich are found below TOS.
\n\n
\n
\nMAKE_CLOSURE(argc)
\n
Creates a new function object, sets its func_closure slot, and pushes it on\nthe stack. TOS is the code associated with the function, TOS1 the tuple\ncontaining cells for the closure’s free variables. The function also has\nargc default parameters, which are found below the cells.
\n\n
\n
\nBUILD_SLICE(argc)
\n

Pushes a slice object on the stack. argc must be 2 or 3. If it is 2,\nslice(TOS1, TOS) is pushed; if it is 3, slice(TOS2, TOS1, TOS) is\npushed. See the slice() built-in function for more information.

\n
\n\n
\n
\nEXTENDED_ARG(ext)
\n
Prefixes any opcode which has an argument too big to fit into the default two\nbytes. ext holds two additional bytes which, taken together with the\nsubsequent opcode’s argument, comprise a four-byte argument, ext being the two\nmost-significant bytes.
\n\n
\n
\nCALL_FUNCTION_VAR(argc)
\n
Calls a function. argc is interpreted as in CALL_FUNCTION. The top element\non the stack contains the variable argument list, followed by keyword and\npositional arguments.
\n\n
\n
\nCALL_FUNCTION_KW(argc)
\n
Calls a function. argc is interpreted as in CALL_FUNCTION. The top element\non the stack contains the keyword arguments dictionary, followed by explicit\nkeyword and positional arguments.
\n\n
\n
\nCALL_FUNCTION_VAR_KW(argc)
\n
Calls a function. argc is interpreted as in CALL_FUNCTION. The top\nelement on the stack contains the keyword arguments dictionary, followed by the\nvariable-arguments tuple, followed by explicit keyword and positional arguments.
\n\n
\n
\nHAVE_ARGUMENT()
\n
This is not really an opcode. It identifies the dividing line between opcodes\nwhich don’t take arguments < HAVE_ARGUMENT and those which do >=\nHAVE_ARGUMENT.
\n\n
\n
", "searchableItems": [ { "name": "dis.dis", "domId": "dis_dis.dis" }, { "name": "dis.disassemble", "domId": "dis_dis.disassemble" }, { "name": "dis.disco", "domId": "dis_dis.disco" }, { "name": "dis.distb", "domId": "dis_dis.distb" }, { "name": "dis.findlabels", "domId": "dis_dis.findlabels" }, { "name": "dis.findlinestarts", "domId": "dis_dis.findlinestarts" } ] } ]