DBMStyle Database Modules

Python includes a number of library modules for supporting UNIX DBM-style database files. Several standard types of these databases are supported. The dbm module is used to read standard UNIX-dbm database files. The gdbm module is used to read GNU dbm database files (http www.gnu.org software gdbm).The dbhash module is used to read database files created by the Berkeley DB library (http www.oracle.com database berkeley-db index.html).The dumbdbm module is a pure-Python module that implements a...

String Literals

String literals are used to specify a sequence of characters and are defined by enclosing text in single ('), double (), or triple (''' or ) quotes.There is no semantic difference between quoting styles other than the requirement that you use the same type of quote to start and terminate a string. Single- and double-quoted strings must be defined on a single line, whereas triple-quoted strings can span multiple lines and include all of the enclosed formatting (that is, newlines, tabs, spaces,...

Restkey Csv Python

The csv module is used to read and write files consisting of comma-separated values (CSV). A CSV file consists of rows of text, each row consisting of values separated by a delimiter character, typically a comma (,) or a tab. Here's an example Blues,Elwood,1060 W Addison,Chicago, IL Variants of this format commonly occur when working with databases and spreadsheets. For instance, a database might export tables in CSV format, allowing the tables to be read by other programs. Subtle complexities...

Python Json Reference

The json module is used to serialize and unserialize objects represented using JavaScript Object Notation (JSON). More information about JSON is available at http json.org, but the format is really just a subset of JavaScript syntax. Incidentally, it's almost the same as Python syntax for representing lists and dictionaries. For example, a JSON array is written as value1, value2, , and a JSON object is written as name-.value, name-.value, . The following list shows how JSON values and Python...

Type Conversion from Python to C

The following functions are used by extension modules to convert arguments passed from Python to C. Their prototypes are defined by including the Python.h header file. int PyArg_ParseTuple PyObject args, char format, Parses a tuple of positional arguments in args into a series of C variables. format is a format string containing zero or more of the specifier strings from Tables 26.1 26.3, which describe the expected contents of args. All the remaining arguments contain the addresses of C...

Urllib Package

The urllib package provides a high-level interface for writing clients that need to interact with HTTP servers, FTP servers, and local files. Typical applications include scraping data from web pages, automation, proxies, web crawlers, and so forth. This is one of the most highly configurable library modules, so every last detail is not presented here. Instead, the most common uses of the package are described. In Python 2, the urllib functionality is spread across several different library...

XML Example Document

The following example illustrates a typical XML document, in this case a description of a recipe. lt xml version 1.0 encoding iso-8859-1 gt lt recipe gt lt title gt lt item num 4 gt Large avocados, chopped lt item gt lt item num 1 gt Tomato, chopped lt item gt lt item num 1 2 units C gt White onion, chopped lt item gt lt item num 2 units tbl gt Fresh squeezed lemon juice lt item gt lt item num 1 gt Jalapeno pepper, diced lt item gt lt item num 1 units tbl gt Fresh cilantro, minced lt item gt lt...

Http Package

The http package consists of modules for writing HTTP clients and servers as well as support for state management cookies .The Hypertext Transfer Protocol HTTP is a simple text-based protocol that works as follows 1. A client makes a connection to an HTTP server and sends a request header of the following form GET document.html HTTP 1.0 Connection Keep-Alive User-Agent Mozilla 4.61 en X11 U SunOS 5.6 sun4u Host rustler.cs.uchicago.edu 8000 Accept image gif, image x-xbitmap, image jpeg, image...

Semaphore and Bounded Semaphore

A semaphore is a synchronization primitive based on a counter that's decremented by each acquire call and incremented by each release call. If the counter ever reaches zero, the acquire method blocks until some other thread calls release . Creates a new semaphore. value is the initial value for the counter. If omitted, the counter is set to a value of 1. A Semaphore instance, s, supports the following methods Acquires the semaphore. If the internal counter is larger than zero on entry, this...

Predefined Exception Classes

The following exceptions are raised by programs Failed attribute reference or assignment. EOFError End of file. Generated by the built-in functions input and raw_input . It should be noted that most other I O operations such as the read and readline methods of files return an empty string to signal EOF instead of raising an exception. Failed floating-point operation. It should be noted that floating-point exception-handling is a tricky problem and only that this exception only gets raised if...

Logging Level Method

CRITICAL log.critical fmt , args , exc_info , extra ERROR log.error fmt , args , exc_info , extra WARNING log.warning fmt , args , exc_info , extra INFO log.info fmt , args , exc_info , extra DEBUG log.debug fmt , args , exc_info , extra The fmt argument is a format string that specifies the format of the log message. Any remaining arguments in args serve as arguments for format specifiers in the format string. The string formatting operator is used to form the resulting message from these...

Managed Objects

Unlike threads, processes do not support shared objects. Although you can create shared values and arrays as shown in the previous section, this doesn't work for more advanced Python objects such as dictionaries, lists, or instances of user-defined classes.The multiprocessing module does, however, provide a way to work with shared objects if they run under the control of a so-called manager.A manager is a separate subprocess where the real objects exist and which operates as a server. Other...

Process Pools

The following class allows you to create a pool of processes to which various kind of data processing tasks can be submitted. The functionality provided by a pool is somewhat similar to that provided by list comprehensions and functional programming operations such as map-reduce. Pool numprocess ,initializer , initargs Creates a pool of worker processes. numprocessis the number of processes to create. If omitted, the value of cpu_count is used. initializer is a callable object that will be...

Error Handling

Extension modules indicate errors by returning NULL to the interpreter. Prior to returning NULL, an exception should be set using one of the following functions Raises a MemoryError exception. void PyErr_SetFromErrno PyObject exc Raises an exception. exc is an exception object.The value of the exception is taken from the errno variable in the C library. void PyErr_SetFromErrnoWithFilename PyObject exc, char filename Like PyErr_SetFromErrno , but includes the file name in the exception value as...