Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Python (2005)

.pdf
Скачиваний:
177
Добавлен:
17.08.2013
Размер:
15.78 Mб
Скачать

What’s New in Python 2.4

Making Impor t a Bit Easier

Normally, Python is very fussy about where you can insert new lines. One such place in which it has been fussy in the past is when importing specific names from a module. Normally, importing a long list of functions or other names from a module would involve using a backslash character at the end of each line, such as the following:

from SomeModule import this, that, theother, yetanother, andonemore, \

andonemoreforgoodluck

This makes the import somewhat readable, but the backslash at the end of each line can become difficult to read.

As an alternative, Python 2.4 allows you to enclose all of the names that you want to import inside of parentheses, enabling you to do away with the need for the backslash. For example, the preceding code would become the slightly easier to work with form:

from SomeModule import (this, that, theother, yetanother, andonemore,

andonemoreforgoodluck)

One significant advantage is that this makes it easier for programming editors to automatically keep track of how a list of names is formatted. With backslashes, this can become messy.

None Really Is None

In the past, it was possible to assign a new value to the special name None. As of Python 2.4, this will raise an exception, so this mistake can’t be made.

611

TEAM LinG

TEAM LinG

Glossar y

The following terms are used in the book and are presented here for your convenience.

127.0.0.1A special IP address used to denote “this computer.” Also see “localhost.”

Anonymous Anonymous functions and variables are not bound to names. Examples of this are the functions created by the lambda function, a list or tuple created but never associated with a name.

Base64 An encoding strategy defined by MIME that escapes an entire string as a whole. More efficient than Quoted-printable for binary data.

BitTorrent A peer-to-peer protocol that distributes the cost of hosting a file among all the parties downloading it.

Call Stack When code is executing, the call stack is the list of functions that your code has executed to reach that point in the program. As functions or methods are entered, the location in the file is noted along with the parameters that the function was called with, and the entry point is marked in the call stack. When a function is exited, its entry in the call stack is removed. When an exception occurs, a stack trace is printed that indicates where in the program the problem occurred by printing out the state of the call stack.

CGI The Common Gateway Interface: A standard for web servers that makes it easy to expose web interfaces to scripts.

Class A class is a definition that can be used to create objects. A particular class definition contains the declarations of the data and the methods that objects that are instances of that particular class will have available to them. In Python, functions that appear within the context of a class are considered to be methods.

Class An object holds data as well as the methods that operate on that data. A class defines what data are stored and what methods are available. Python is a little looser than most programming languages, such as Java, C++, or C#, in that Python lets you break rules enforced in other languages. For example, Python, by default, lets you access data inside a class. This does violate some of the concepts of object-oriented programming but with good reason: Python aims first and foremost to be practical.

TEAM LinG

Glossary

Client-server Describes an architecture in which one actor (the server) is a repository for information requested and acted upon by other actors (the clients). fault: A term used in web services to denote an error condition. Similar to Python’s exceptions, and generally implemented as exceptions in Python. goals of REST.

Comment Comments are text in a program that python does not pay attention to. At any point outside of a string where a hash mark (#) appears, from that point until the end of the, the Python interpreter ignores all text.

Content type A MIME concept used to indicate the type of a file being sent encoded inside an email message. Also used by web servers to indicate the type of file being served.

DB API A Python API for accessing databases. The neat thing about this API is that you can use the same Python code to work with any database for which there is a DB-compliant driver. This includes Oracle, DB2, and so on. The only differences in your code will likely be the code to connect to the database, which differs by vendor.

DBM Short for database manager, DBM libraries provide a means to persist Python dictionaries.

Dictionary A data type in python that is indexed by an arbitrary value that is set by the programmer. The value can be any kind of python object. The index is called the “key” and the object that a key references is referred to as it’s “value.”

DNS Domain Name System. A service that runs on top of TCP and resolves hostnames (wrox.com) to IP addresses (208.215.179.178).

Document Model A way of describing the vocabulary and structure of a document. Defines the data elements that will be present in a document, what relationship they have to one another, and how many of them are expected.

DOM The Document Object Model, a tree-based API recommendation from the W3C for working with XML documents.

DTD Document Type Definition. A specification for producing a Document Model.

Dynamic port See ephemeral port.

Encapsulation Encapsulation is the idea that a class can hide the internal details and data necessary to perform a certain task. A class holds the necessary data, and you are not supposed to see that data under normal circumstances. Furthermore, a class provides a number of methods to operate on that data. These methods can hide the internal details, such as network protocols, disk access, and so on. Encapsulation is a technique to simplify your programs. At each step in creating your program, you can write code that concentrates on a single task. Encapsulation hides the complexity.

Encryption The act of hiding information so that it is difficult or impossible to recover without a secret password. Data is encrypted when it is recoverable. Data which is scrambled and unrecoverable should be thought of as lost, instead.

Ephemeral port High-numbered IP ports are often created to receive data over TCP/IP as part of a particular socket connection. Ephemeral ports are administered by the operating system, and have a lifetime of a single socket connection.

614

TEAM LinG

Glossary

Escape sequences Special characters that begin with the backslash such as \n for a new line.

Float A floating point number is a number with a fractional or decimal component. Fractions can be represented as decimal values using a float value. When arithmetic is done with a float and an integer, the integer will be promoted to being a float.

Function A function is a collection of code defined by using a name. It is invoked by using that name followed by open and close braces, such as function(). A function can be given parameters that are enclosed within the braces and separated by commas, such as function(parameter1, parameter2), that it can use to modify its output.

Header An item of metadata found both in email messages and in HTTP requests and responses. A header line consists of a key and value separated by a colon and a space. For instance: “Subject: Hello.”

Hexadecimal Base 16 notation, where the numbers are from 0 through 15, and are represented by the numbers 0-9, and once single digits are exhausted the letters A-F are used. So the number 11 in hex is b.

hostname A human-readable identifier for a computer on an IP network, for instance: wrox.com. Hostnames are administered through DNS.

HTTP body The data portion of an HTTP request or response.

HTTP headers The metadata portion of an HTTP request or response: a series of key-value pairs. HTTP defines some standard headers, and CGI defines some more: Applications can define their own.

HTTP HyperText Transfer Protocol, the protocol devised to let web browsers and web servers communicate.

HTTP request The string sent by an HTTP client to the server, requesting some operation on some resource.

HTTP response The string sent by an HTTP server to a client, in response to an HTTP request. In REST terminology, it contains either a representation of a resource or a document describing action taken on a resource.

HTTP status code A numeric code used in an HTTP response to denote the status of the corresponding request. There are 40 of these, defined in the HTTP standard.

HTTP verb A string used in an HTTP request to describe what the client wants to do to a resource (for instance, retrieve a representation of it or modify it).

Idempotent An idempotent action has no side effects. A term taken from mathematics: Multiplying a number by 1 is an idempotent action. So should be calling an object’s accessor method or (in REST) making an HTTP GET request.

Imaginary number a special number that acts like a float but cannot be mixed freely with floats or integers. If they are mixed, a complex number is the result, not an imaginary number.

IMAP The Internet Message Access Protocol. Also known as IMAP4. A protocol for retrieving and managing mail. IMAP4 intends for you to store your mail on the server. q.v. POP.

615

TEAM LinG

Glossary

Infinite loop a loop that has no termination clause, like “while True:”. Often an infinite loop is an accidental situation, but they can be useful as long as there are actions that will happen, and there is code being executed. One example is a server waiting for connections.

Inheritance Inheritance means that a class can inherit, or gain access to, data and methods defined in a parent class. This just follows common sense in classifying a problem domain. For example, a rectangle and a circle are both shapes. In this case, the base class would be Shape. The Rectangle class would then inherit from Shape, as would the Circle class. Inheritance allows you to treat objects of both the Rectangle and Circle classes as Shapes, meaning you can write more generic code. For the most part, the base class should be general and the subclasses specialized. Oftentimes inheritance is called specialization.

Input/Output an umbrella term that covers any kind of operation that reads or writes data. Writing to screen, input from the keyboard, and network connections are all examples of Input/Output.

Integer whole numbers, without a fractional or decimal component. In Python, if an integer becomes larger than its range (from -2,147,483,648 to 2,147,483,647 on 32-bit systems) it is automatically converted to a long integer. The long integer and the plain integer can be mixed freely. If two integers are used in division where one can’t be evenly divided into the other, an integer will be the result, not a float. The remainder is discarded. To obtain just the reminder, use the modulus operation. To obtain a float result, one of the numbers must be a float for the conversion to happen.

I/O See input/output.

IP address The location of a computer on an IP network. For instance, 208.215.179.178.

IP The Internet Protocol. Connects networks based on different technologies (for instance, Ethernet and wireless) into a single network.

IRC Internet Relay Chat. A protocol for online chat rooms.

Iterator Iterators are objects that you can use in certain contexts that generate a sequence of outputs. Unlike sequence objects, an iterator like xrange doesn’t have to return a finite list. The object can continue to create return values when its next method is invoked. Iterators can be used with for loops.

J2EE Java 2 Enterprise Edition, a set of standards for writing enterprise-worthy Java applications. There are no real corresponding Python standards, but the Twisted framework and others provide enterpriseworthy features for Python.

JVM Java Virtual Machine, the runtime engine of the Java platform. The java command runs Java applications similar to how the python command runs Python applications.

Jython An implementation of Python written in the Java language that runs on top of the Java platform.

List A list is a type of sequence, as well as being an iterator. It is similar to a tuple, except that it can be modified after it is created. A list is created by using the square brackets “[]”.

localhost A special hostname used to denote “this computer”. Also see “127.0.0.1.”

616

TEAM LinG

Glossary

Long Integer See Integer.

Loop A loop is a form of repetition where a set of operations is performed, and the operations are repeated until a set of conditions are set.

Method A method is a function inside the context of an object (it is also called a method when you write it inside of a class). It has automatic access to all of the data within the object that it was invoked from.

MIME Multipurpose Internet Mail Encoding. A set of standards that make it possible to send multiple files and international and binary data through email, while still complying with RFC 2822.

Module A module is a collection of code within a file. Modules can contain functions, named variables, and classes. When a module is used in a program, it is made available using the import built-in word, and it lives within a scope named after the module. So in a module named “mymodule” the function “myfunction” would be called by calling “mymodule.myfunction()”. This can be modified by how the module is imported with import the modifiers “from”, and “as” can modify the behavior of import so that the module is seen as having a different name. The current module can be found by looking at the variable name “__name__”, which is created locally in each module’s scope. If __name__ is “__main__” then the scope is currently the top level module — i.e., the program being run.

Module A module is just a Python source file. A module can contain variables, classes, functions, and any other element available in your Python scripts.

Multipart message A MIME message that contains more than one “document” (for instance, a text message and an image).

Object An object is an instance of a class. Objects contain data and methods that are defined in the class. Multiple objects of the same class may exist in the same program at the same time, using different names. Each object has data that will be different from other objects of the same type. Objects are bound to a name when they are created.

Octal Base 8 notation, where the numbers range from 0-7.

Package A package is a grouping of modules in a directory that contains a file called __init__.py. Together, all the files in the directory can act together to implement a combined package that appears, when it’s used, to act like a single module. The module can contain subdirectories that can also contain modules. The package offers an organizational structure for distributing more complex program structures, and it also allows for the conditional inclusion of code that may only work on one platform (for instance, if one file could not work except on a Mac OS X system, it could be put into its own file and called only after the correct platform had been verified).

Peer-to-peer Describes an architecture in which all actors have equal standing.

Polymorphism Polymorphism means that subclasses can override methods for more specialized behavior. For example, a Rectangle and a Circle are both Shapes. You may define a set of common operations such as move and draw that should apply to all shapes. But the draw method for a Circle will obviously be different from the draw method for a Rectangle. Polymorphism allows you to name both methods draw and then call these methods as if the Circle and the Rectangle were both Shapes (which they are, at least in this example).

617

TEAM LinG

Glossary

POP The Post Office Protocol. Also known as POP3. A protocol for downloading email from a server. POP intends that you delete the mail from the server after downloading it. q.v. IMAP.

Port Along with an IP address, a port number identifies a particular service on an Internet network.

Protocol A convention for structuring the data sent between parties on a network. HTTP and TCP/IP are examples of protocols.

Protocol stack A suite of protocols in which the higher-level protocols delegate to the lower-level ones.

Quoted-printable An encoding strategy defined by MIME that escapes each non-US ASCII character individually. More efficient than Base64 for text that contains mostly US ASCII characters.

Quotes In Python, strings are defined by being text within quotes. Quotes can be either single (‘), double (“), or triple quotes (“”” or ‘’’). If a string is started with a single quote, then it must be ended with single quotes. A string begun with a double quote must be terminated with a double quote. A string begun with a triple quote must be terminated with a triple quote of the same kind of quote (‘’’ must be matched by a ‘’’, and a “”” must be matched by a “””). Single and double quotes function in exactly the same way. Triple quotes are special because they can enclose multi-line strings (strings that contain newlines).

Range Range generates a list of numbers, by default from zero to the number it is given as a parameter, by one. It can also be instructed to start at a number other than zero and to increment in steps rather than by one.

RDBMS Relational Datbase Management System. See Relational Database.

Relational database In a relational database, data are stored in tables, two-dimensional data structures. Each table is made up of rows, also called records. Each row in turn is made up of columns. Typically, each record holds the information pertaining to one item, such as an audio CD, a person, a purchase order, an automobile, and so on.

Representation In REST terminology, a depiction of a resource. When you request a resource, what you get back is a representation. One resource may have multiple representations. For instance, a single document resource may have HTML, PostScript, and plain-text representations.

Resource identifier A string that uniquely identifies a resource. Generally equivalent to a URL. One resource may have multiple identifiers.

Resource In REST terminology, an object that can be accessed and/or manipulated from the web. Can take a number of forms: For instance, it may be a document located on the server, a row in a database, or even a physical object (such as an item you order in an online store).

RESTfulness An informal metric of how well a web application conforms to the design.

REST REpresentational State Transfer, a name for the architecture of the World Wide Web.

RFC 2822 The standard format for Internet email messages. Requires that email messages be formatted in US ASCII.

Robot A script that makes HTTP requests while not under the direct control of a human.

618

TEAM LinG

Glossary

RSS Rich Site Summary, or RDF Site Summary. An XML-based format for syndicating content.

SAX The Simple API for XML. A stream-based XML parser.

Scope Names of data and code; variable names, class names, function names, etc., have different levels of visibility. Names that are visible within a function or method are either in their scope or come from a scope that is at a level above the scope of the operation accessing it.

Sequence A sequence is a category of data types. A sequence can refer to any type of object that contains an ordered numerical index, starting from zero, which contains references to values. Each value referenced from an index number can be any python object that could normally be referenced by a variable name. Elements in a sequence are dereferenced by using the square brackets after the name of the sequence. So for a sequence named “seq” the fourth element is dereferenced when you see “seq[3]”. It is 3 instead of 4 because the first index number of a sequence is 0.

SMTP Simple Mail Transport Protocol. The standard protocol for sending Internet email.

SOAP Originally stood for Simple Object Access Protocol. A standard for making web service calls, similar to XML-RPC but more formally defined.

Socket A two-way connection over an IP network. Sockets allow programmers to treat network connections like files.

Spider Robot that, given a starting web page, follows links to find other web pages to operate on. Most search engines have implemented spiders.

SQL Structured query language, pronounced either sequel or S-Q-L.Language used to access relational databases.

SSL Secure Socket Layer. A protocol that runs between TCP/IP and some other protocol (such as SMTP or HTTP), providing end-to-end encryption.

Stack Trace See Call Stack.

String Any combination of letters or numbers enclosed in quotation marks (either single, double, or a series of three single or double quotes together). Most objects have a string representation that can be

used to print them. Strings can be treated as arrays when you want to access specific letters, or a range of letters, within the string. Also see unicode.

TCP/IP A term used to describe a very common protocol stack: TCP running on top of IP.

TCP Transport Control Protocol: Makes reliable, orderly communication possible between two points on an IP network.

Tuple A tuple is a type of sequence as well as an iterator. A tuple is similar to a list, except that once a tuple has been defined, the number of elements, and the references to elements in it cannot be changed (however, if it references an object whose data you can change, such as a list or a dictionary, the data within that other type can still be changed). Tuples are created with the parenthesis “()”. When you create a tuple that has only one element, you must put a comma after that single element. Failing to do this will create a string.

619

TEAM LinG

Glossary

Twisted A framework for Python networking. Where other Python networking frameworks use threads or subprocesses to handle multiple client requests simultaneously, Twisted does everything in a single thread.

UID Unique ID. Used in a variety of contexts to denote an ID that is unique and stable over time.

Unicode Unicode is a system for encoding strings so that the original letters can be determined, even if someone using a different character encoding by default reads that string later. (Think of someone using a computer localized for Russia trying to read a document written in Hebrew — internally characters can be thought of as numbers in a lookup table, and with different languages and character sets, character #100 in either character set is likely to be different.) Unicode strings are made by using the construct u’string’.

User agent A web browser or HTTP-enabled script.

Variable A variable is what data bound to a name is called. The name “variable” usually refers to the basic types and not more complex objects. This is true even though integers, longs, floats, imaginary numbers, and strings all objects in Python. This way of thinking is a convention that carries over from other languages where the distinction is made — some types are not objects.

Web application A program that exposes its interface through HTTP instead of through a commandline or GUI interface.

Web service A web application designed for use by HTTP-enabled scripts instead of human beings with web browsers.

Well-known port IP port numbers between 0 and 1023 are well-known ports. Popular services like web servers tend to run on well-known ports, and services running on well-known ports often run with administrator privileges.

Whitespace Whitespace are the names of the characters that you can’t see when you are typing or reading. Newlines, spaces, and tab characters are all whitespace. Python pays attention to whitespaces at the beginnings of lines, and it is aware of newlines at the ends of lines, except inside of list or tuple definitions, and except inside of triple-quoted strings.

wiki A web application that allows its users to create and edit web pages through a web interface.

WSDL Web Services Description Language, a way of representing method calls in XML.

XML eXtensible Markup Language. A specification for creating structured markup languages with customized vocabularies.

XML-RPC The RPC stands for Remote Procedure Call. XML-RPC is a standard for making web service calls. It defines a way of representing simple data structures in XML, sending data structures over HTTP as arguments to a function call, and getting another data structure back as a return value.

XML Schema A specification for producing a Document Model.

620

TEAM LinG