You are here: Home > Dive Into Python > A 5-minute review | << >> | ||||
Dive Into PythonPython from novice to pro |
The first thing you need to do with Python is install it. Or do you?
On Windows, you have a couple choices for installing Python.
On Mac OS X, you have two choices for installing Python: install it, or don't install it. You probably want to install it.
Mac OS 9 does not come with any version of Python, but installation is very simple, and there is only one choice.
Download the latest Python RPM by going to http://www.python.org/ftp/python/ and selecting the highest version number listed, then selecting the rpms/ directory within that. Then download the RPM with the highest version number. You can install it with the rpm command, as shown here:
If you are lucky enough to be running Debian GNU/Linux, you install Python through the apt command.
If you prefer to build from source, you can download the Python source code from http://www.python.org/ftp/python/. Select the highest version number listed, download the .tgz file), and then do the usual configure, make, make install dance.
Now that you have Python installed, what's this interactive shell thing you're running?
You should now have a version of Python installed that works for you.
以下が完全な動く Python プログラムです.
Python は他のほとんどの言語と同じように関数を持っています. しかし, C++ のような分離されたヘッダファイルはありませんし, Pascal のような interface/implementation 節もありません. 関数が必要なときは, 単に以下のように宣言すれば良いのです.
Python では関数に doc string と言われる文字列を与えることで, 説明を付けることができます.
関数は, 他の Python の要素と同じようにオブジェクトなのです.
Python の関数はコードの始まりと終わりを示すための明示的な begin と end や波括弧を持ちません. 区切り文字はコロン (:) とコードそのもののインデントです.
Python モジュールはオブジェクトであり, いくつかの便利な属性を持っています. これを使うと, あなたのモジュールを簡単に思い通りにテストできます. 以下は if __name__ トリックを使った例です.
Python の組み込みのデータ型の 1 つが辞書で, それを使ってキーと値の 1 対 1 の関係を定義します.
リストは Python の働き者のデータ型です. もし Visual Basic の配列や Powerbuilder のデータストア (なんと!) でしかリストに触ったことがないなら, 心して Python のリストに取り掛かってください.
タプルは変更不可能 (不変) なリストです. タプルはいったん作られたら, どんな方法でも変更されません.
他のほとんどの言語と同じように, Python にはローカル変数とグローバル変数がありますが, 明示的な変数宣言はありません. 変数は値を割り当てられると出現し, スコープを外れると自動的に破棄されます.
Python は値の文字列へのフォーマットをサポートしています. 非常に込み入った式も含むことはできますが, %s プレースホルダ (訳注. 値の代わりに仮に置かれる文字列) を使って文字列に値を挿入するという, 基本的な使い方がほとんどです.
Python の最も強力な特徴の 1 つはリストの内包表記であり, それを使うと, あるリストの個々の要素にある関数を適用して別のリストに変換する操作が簡潔に行えます.
key=value という形式のキー値の組のリストがあり, 1 つの文字列に結合したいとします. 文字列のリストを 1 つの文字列に結合するには, 文字列オブジェクトの join メソッドを使います.
ここまでで odbchelper.py プログラムとその出力の意味の全てが分かるようになりました.
以下は完全な動く Python プログラムです. あなたはプログラムの大部分を見ただけで理解できるはずです. 番号が付いている行はChapter 2, 初めての Python プログラムで扱った内容を示しています. 残りのコードが怖く思えても心配しないでください. この章を通してそれら全てについて学びます.
Python では関数の引数はデフォルト値を持てます. その引数無しで関数が呼び出されると, 引数はデフォルト値を取ります. さらに, 名前付きの引数を使って任意の順序で引数を並べることができます. SQL Server Transact/SQL のストアドプロシージャも同じことができるので, もしあなたが SQL Server スクリプトの仙人なら, この部分を流し読みできます.
Python は飛び抜けて有用な組み込み関数を少数揃えています. 他の全ての関数はモジュールに分配されています. これは他のスクリプト言語 (Visual Basic...ゲフンゲフン) のように言語の核となる部分が太り過ぎるのを防ぐための, 実に意図的な設計上の判断です.
すでにあなたは Python の関数はオブジェクトだということは知っています. まだ知らないのは, 実行時までに名前を知ることなく関数への参照を取得できる方法です. それは getattr 関数を使うことです.
知っての通り, Python にはリストの内包表記 (Section 3.6, “リストのマップ操作”) というリストを別のリストにマップする強力な構文があります. これにはフィルタリングの機能を付けられ, ある要素だけマップし, それ以外の要素はスキップするということができます.
Python では, and と or は期待通りに論理演算を行いますが, 論理値を返すわけではありません. その代わりに, 論理演算を行っている実際の値のうちの 1 つを返します.
Python はその場で 1 行のミニ関数を定義できる構文をサポートしています. その Lisp から拝借した所謂 lambda 関数は, 関数が書けるところならどこでも使えます.
コードの最後の行はまだ解体していない唯一の行なのですが, それは全ての仕事をしている行です. しかし必要なものはもうきっちり準備してあるので, 仕事は簡単です. 全てのドミノは配置されていて, 今こそ倒す時です.
ここまでで apihelper.py プログラムとその出力について完璧に理解したはずです.
以下は完全な動く Python プログラムです. モジュール, クラス, 関数の doc string を読み, プログラムが何をしどう動くのかの概要を掴んでください. いつも通り, 理解できないところがあっても怖がらないでください. その部分についてこの章で扱っていきます.
Python にはモジュールをインポートする方法が 2 つあります. 両方とも有用であり, それらの使い分けを知らなくてはなりません. 1 つの方法は, Section 2.4, “全てはオブジェクト” で既に見た import module です. もう 1 つの方法でも同じ結果は得ますが, わずかですが重要な違いがあります.
Python is fully object-oriented: you can define your own classes, inherit from your own or built-in classes, and instantiate the classes you've defined.
Instantiating classes in Python is straightforward. To instantiate a class, simply call the class as if it were a function, passing the arguments that the __init__ method defines. The return value will be the newly created object.
As you've seen, FileInfo is a class that acts like a dictionary. To explore this further, let's look at the UserDict class in the UserDict module, which is the ancestor of the FileInfo class. This is nothing special; the class is written in Python and stored in a .py file, just like any other Python code. In particular, it's stored in the lib directory in your Python installation.
In addition to normal class methods, there are a number of special methods that Python classes can define. Instead of being called directly by your code (like normal methods), special methods are called for you by Python in particular circumstances or when specific syntax is used.
Python has more special methods than just __getitem__ and __setitem__. Some of them let you emulate functionality that you may not even know about.
You already know about data attributes, which are variables owned by a specific instance of a class. Python also supports class attributes, which are variables owned by the class itself.
Unlike in most languages, whether a Python function, method, or attribute is private or public is determined entirely by its name.
That's it for the hard-core object trickery. You'll see a real-world application of special class methods in Chapter 12, which uses getattr to create a proxy to a remote web service.
Chapter 6. Exceptions and File Handling
Like many other programming languages, Python has exception handling via try...except blocks.
Python has a built-in function, open, for opening a file on disk. open returns a file object, which has methods and attributes for getting information about and manipulating the opened file.
Like most other languages, Python has for loops. The only reason you haven't seen them until now is that Python is good at so many other things that you don't need them as often.
Modules, like everything else in Python, are objects. Once imported, you can always get a reference to a module through the global dictionary sys.modules.
The os.path module has several functions for manipulating files and directories. Here, we're looking at handling pathnames and listing the contents of a directory.
Once again, all the dominoes are in place. You've seen how each line of code works. Now let's step back and see how it all fits together.
The fileinfo.py program introduced in 5 章 should now make perfect sense.
Chapter 7. Regular Expressions
If what you're trying to do can be accomplished with string functions, you should use them. They're fast and simple and easy to read, and there's a lot to be said for fast, simple, readable code. But if you find yourself using a lot of different string functions with if statements to handle special cases, or if you're combining them with split and join and list comprehensions in weird unreadable ways, you may need to move up to regular expressions.
This series of examples was inspired by a real-life problem I had in my day job several years ago, when I needed to scrub and standardize street addresses exported from a legacy system before importing them into a newer system. (See, I don't just make this stuff up; it's actually useful.) This example shows how I approached the problem.
You've most likely seen Roman numerals, even if you didn't recognize them. You may have seen them in copyrights of old movies and television shows (“Copyright MCMXLVI” instead of “Copyright 1946”), or on the dedication walls of libraries or universities (“established MDCCCLXXXVIII” instead of “established 1888”). You may also have seen them in outlines and bibliographical references. It's a system of representing numbers that really does date back to the ancient Roman empire (hence the name).
In the previous section, you were dealing with a pattern where the same character could be repeated up to three times. There is another way to express this in regular expressions, which some people find more readable. First look at the method we already used in the previous example.
So far you've just been dealing with what I'll call “compact” regular expressions. As you've seen, they are difficult to read, and even if you figure out what one does, that's no guarantee that you'll be able to understand it six months later. What you really need is inline documentation.
So far you've concentrated on matching whole patterns. Either the pattern matches, or it doesn't. But regular expressions are much more powerful than that. When a regular expression does match, you can pick out specific pieces of it. You can find out what matched where.
This is just the tiniest tip of the iceberg of what regular expressions can do. In other words, even though you're completely overwhelmed by them now, believe me, you ain't seen nothing yet.
I often see questions on comp.lang.python like “How can I list all the [headers|images|links] in my HTML document?” “How do I parse/translate/munge the text of my HTML document but leave the tags alone?” “How can I add/remove/quote attributes of all my HTML tags at once?” This chapter will answer all of these questions.
HTML processing is broken into three steps: breaking down the HTML into its constituent pieces, fiddling with the pieces, and reconstructing the pieces into HTML again. The first step is done by sgmllib.py, a part of the standard Python library.
To extract data from HTML documents, subclass the SGMLParser class and define methods for each tag or entity you want to capture.
SGMLParser doesn't produce anything by itself. It parses and parses and parses, and it calls a method for each interesting thing it finds, but the methods don't do anything. SGMLParser is an HTML consumer: it takes HTML and breaks it down into small, structured pieces. As you saw in the previous section, you can subclass SGMLParser to define classes that catch specific tags and produce useful things, like a list of all the links on a web page. Now you'll take this one step further by defining a class that catches everything SGMLParser throws at it and reconstructs the complete HTML document. In technical terms, this class will be an HTML producer.
Let's digress from HTML processing for a minute and talk about how Python handles variables. Python has two built-in functions, locals and globals, which provide dictionary-based access to local and global variables.
There is an alternative form of string formatting that uses dictionaries instead of tuples of values.
A common question on comp.lang.python is “I have a bunch of HTML documents with unquoted attribute values, and I want to properly quote them all. How can I do this?”[4] (This is generally precipitated by a project manager who has found the HTML-is-a-standard religion joining a large project and proclaiming that all pages must validate against an HTML validator. Unquoted attribute values are a common violation of the HTML standard.) Whatever the reason, unquoted attribute values are easy to fix by feeding HTML through BaseHTMLProcessor.
Dialectizer is a simple (and silly) descendant of BaseHTMLProcessor. It runs blocks of text through a series of substitutions, but it makes sure that anything within a <pre>...</pre> block passes through unaltered.
It's time to put everything you've learned so far to good use. I hope you were paying attention.
Python provides you with a powerful tool, sgmllib.py, to manipulate HTML by turning its structure into an object model. You can use this tool in many different ways.
There are two basic ways to work with XML. One is called SAX (“Simple API for XML”), and it works by reading the XML a little bit at a time and calling a method for each element it finds. (If you read Chapter 8, HTML Processing, this should sound familiar, because that's how the sgmllib module works.) The other is called DOM (“Document Object Model”), and it works by reading in the entire XML document at once and creating an internal representation of it using native Python classes linked in a tree structure. Python has standard modules for both kinds of parsing, but this chapter will only deal with using the DOM.
Actually parsing an XML document is very simple: one line of code. However, before you get to that line of code, you need to take a short detour to talk about packages.
As I was saying, actually parsing an XML document is very simple: one line of code. Where you go from there is up to you.
Unicode is a system to represent characters from all the world's different languages. When Python parses an XML document, all data is stored in memory as unicode.
Traversing XML documents by stepping through each node can be tedious. If you're looking for something in particular, buried deep within your XML document, there is a shortcut you can use to find it quickly: getElementsByTagName.
XML elements can have one or more attributes, and it is incredibly simple to access them once you have parsed an XML document.
OK, that's it for the hard-core XML stuff. The next chapter will continue to use these same example programs, but focus on other aspects that make the program more flexible: using streams for input processing, using getattr for method dispatching, and using command-line flags to allow users to reconfigure the program without changing the code.
Chapter 10. Scripts and Streams
One of Python's greatest strengths is its dynamic binding, and one powerful use of dynamic binding is the file-like object.
UNIX users are already familiar with the concept of standard input, standard output, and standard error. This section is for the rest of you.
kgp.py employs several tricks which may or may not be useful to you in your XML processing. The first one takes advantage of the consistent structure of the input documents to build a cache of nodes.
Another useful techique when parsing XML documents is finding all the direct child elements of a particular element. For instance, in the grammar files, a ref element can have several p elements, each of which can contain many things, including other p elements. You want to find just the p elements that are children of the ref, not p elements that are children of other p elements.
The third useful XML processing tip involves separating your code into logical functions, based on node types and element names. Parsed XML documents are made up of various types of nodes, each represented by a Python object. The root level of the document itself is represented by a Document object. The Document then contains one or more Element objects (for actual XML tags), each of which may contain other Element objects, Text objects (for bits of text), or Comment objects (for embedded comments). Python makes it easy to write a dispatcher to separate the logic for each node type.
Python fully supports creating programs that can be run on the command line, complete with command-line arguments and either short- or long-style flags to specify various options. None of this is XML-specific, but this script makes good use of command-line processing, so it seemed like a good time to mention it.
You've covered a lot of ground. Let's step back and see how all the pieces fit together.
Python comes with powerful libraries for parsing and manipulating XML documents. The minidom takes an XML file and parses it into Python objects, providing for random access to arbitrary elements. Furthermore, this chapter shows how Python can be used to create a "real" standalone command-line script, complete with command-line flags, command-line arguments, error handling, even the ability to take input from the piped result of a previous program.
You've learned about HTML processing and XML processing, and along the way you saw how to download a web page and how to parse XML from a URL, but let's dive into the more general topic of HTTP web services.
Let's say you want to download a resource over HTTP, such as a syndicated Atom feed. But you don't just want to download it once; you want to download it over and over again, every hour, to get the latest news from the site that's offering the news feed. Let's do it the quick-and-dirty way first, and then see how you can do better.
There are five important features of HTTP which you should support.
First, let's turn on the debugging features of Python's HTTP library and see what's being sent over the wire. This will be useful throughout the chapter, as you add more and more features.
The first step to improving your HTTP web services client is to identify yourself properly with a User-Agent. To do that, you need to move beyond the basic urllib and dive into urllib2.
Now that you know how to add custom HTTP headers to your web service requests, let's look at adding support for Last-Modified and ETag headers.
You can support permanent and temporary redirects using a different kind of custom URL handler.
The last important HTTP feature you want to support is compression. Many web services have the ability to send data compressed, which can cut down the amount of data sent over the wire by 60% or more. This is especially true of XML web services, since XML data compresses very well.
You've seen all the pieces for building an intelligent HTTP web services client. Now let's see how they all fit together.
The openanything.py and its functions should now make perfect sense.
You use Google, right? It's a popular search engine. Have you ever wished you could programmatically access Google search results? Now you can. Here is a program to search Google from Python.
Unlike the other code in this book, this chapter relies on libraries that do not come pre-installed with Python.
The heart of SOAP is the ability to call remote functions. There are a number of public access SOAP servers that provide simple functions for demonstration purposes.
The SOAP libraries provide an easy way to see what's going on behind the scenes.
The SOAPProxy class proxies local method calls and transparently turns then into invocations of remote SOAP methods. As you've seen, this is a lot of work, and SOAPProxy does it quickly and transparently. What it doesn't do is provide any means of method introspection.
Like many things in the web services arena, WSDL has a long and checkered history, full of political strife and intrigue. I will skip over this history entirely, since it bores me to tears. There were other standards that tried to do similar things, but WSDL won, so let's learn how to use it.
Let's finally turn to the sample code that you saw that the beginning of this chapter, which does something more useful and exciting than get the current temperature.
Of course, the world of SOAP web services is not all happiness and light. Sometimes things go wrong.
SOAP web services are very complicated. The specification is very ambitious and tries to cover many different use cases for web services. This chapter has touched on some of the simpler use cases.
In previous chapters, you “dived in” by immediately looking at code and trying to understand it as quickly as possible. Now that you have some Python under your belt, you're going to step back and look at the steps that happen before the code gets written.
Now that you've completely defined the behavior you expect from your conversion functions, you're going to do something a little unexpected: you're going to write a test suite that puts these functions through their paces and makes sure that they behave the way you want them to. You read that right: you're going to write code that tests code that you haven't written yet.
This is the complete test suite for your Roman numeral conversion functions, which are yet to be written but will eventually be in roman.py. It is not immediately obvious how it all fits together; none of these classes or methods reference any of the others. There are good reasons for this, as you'll see shortly.
The most fundamental part of unit testing is constructing individual test cases. A test case answers a single question about the code it is testing.
It is not enough to test that functions succeed when given good input; you must also test that they fail when given bad input. And not just any sort of failure; they must fail in the way you expect.
Often, you will find that a unit of code contains a set of reciprocal functions, usually in the form of conversion functions where one converts A to B and the other converts B to A. In these cases, it is useful to create a “sanity check” to make sure that you can convert A to B and back to A without losing precision, incurring rounding errors, or triggering any other sort of bug.
Chapter 14. Test-First Programming
Now that the unit tests are complete, it's time to start writing the code that the test cases are attempting to test. You're going to do this in stages, so you can see all the unit tests fail, then watch them pass one by one as you fill in the gaps in roman.py.
Now that you have the framework of the roman module laid out, it's time to start writing code and passing test cases.
Now that toRoman behaves correctly with good input (integers from 1 to 3999), it's time to make it behave correctly with bad input (everything else).
Now that toRoman is done, it's time to start coding fromRoman. Thanks to the rich data structure that maps individual Roman numerals to integer values, this is no more difficult than the toRoman function.
Now that fromRoman works properly with good input, it's time to fit in the last piece of the puzzle: making it work properly with bad input. That means finding a way to look at a string and determine if it's a valid Roman numeral. This is inherently more difficult than validating numeric input in toRoman, but you have a powerful tool at your disposal: regular expressions.
Despite your best efforts to write comprehensive unit tests, bugs happen. What do I mean by “bug”? A bug is a test case you haven't written yet.
Despite your best efforts to pin your customers to the ground and extract exact requirements from them on pain of horrible nasty things involving scissors and hot wax, requirements will change. Most customers don't know what they want until they see it, and even if they do, they aren't that good at articulating what they want precisely enough to be useful. And even if they do, they'll want more in the next release anyway. So be prepared to update your test cases as requirements change.
The best thing about comprehensive unit testing is not the feeling you get when all your test cases finally pass, or even the feeling you get when someone else blames you for breaking their code and you can actually prove that you didn't. The best thing about unit testing is that it gives you the freedom to refactor mercilessly.
A clever reader read the previous section and took it to the next level. The biggest headache (and performance drain) in the program as it is currently written is the regular expression, which is required because you have no other way of breaking down a Roman numeral. But there's only 5000 of them; why don't you just build a lookup table once, then simply read that? This idea gets even better when you realize that you don't need to use regular expressions at all. As you build the lookup table for converting integers to Roman numerals, you can build the reverse lookup table to convert Roman numerals to integers.
Unit testing is a powerful concept which, if properly implemented, can both reduce maintenance costs and increase flexibility in any long-term project. It is also important to understand that unit testing is not a panacea, a Magic Problem Solver, or a silver bullet. Writing good test cases is hard, and keeping them up to date takes discipline (especially when customers are screaming for critical bug fixes). Unit testing is not a replacement for other forms of testing, including functional testing, integration testing, and user acceptance testing. But it is feasible, and it does work, and once you've seen it work, you'll wonder how you ever got along without it.
Chapter 16. Functional Programming
In Chapter 13, Unit Testing, you learned about the philosophy of unit testing. In Chapter 14, Test-First Programming, you stepped through the implementation of basic unit tests in Python. In Chapter 15, Refactoring, you saw how unit testing makes large-scale refactoring easier. This chapter will build on those sample programs, but here we will focus more on advanced Python-specific techniques, rather than on unit testing itself.
When running Python scripts from the command line, it is sometimes useful to know where the currently running script is located on disk.
You're already familiar with using list comprehensions to filter lists. There is another way to accomplish this same thing, which some people feel is more expressive.
You're already familiar with using list comprehensions to map one list into another. There is another way to accomplish the same thing, using the built-in map function. It works much the same way as the filter function.
By now you're probably scratching your head wondering why this is better than using for loops and straight function calls. And that's a perfectly valid question. Mostly, it's a matter of perspective. Using map and filter forces you to center your thinking around your data.
OK, enough philosophizing. Let's talk about dynamically importing modules.
You've learned enough now to deconstruct the first seven lines of this chapter's code sample: reading a directory and importing selected modules within it.
The regression.py program and its output should now make perfect sense.
I want to talk about plural nouns. Also, functions that return other functions, advanced regular expressions, and generators. Generators are new in Python 2.3. But first, let's talk about how to make plural nouns.
So you're looking at words, which at least in English are strings of characters. And you have rules that say you need to find different combinations of characters, and then do different things to them. This sounds like a job for regular expressions.
Now you're going to add a level of abstraction. You started by defining a list of rules: if this, then do that, otherwise go to the next rule. Let's temporarily complicate part of the program so you can simplify another part.
Defining separate named functions for each match and apply rule isn't really necessary. You never call them directly; you define them in the rules list and call them through there. Let's streamline the rules definition by anonymizing those functions.
Let's factor out the duplication in the code so that defining new rules can be easier.
You've factored out all the duplicate code and added enough abstractions so that the pluralization rules are defined in a list of strings. The next logical step is to take these strings and put them in a separate file, where they can be maintained separately from the code that uses them.
Now you're ready to talk about generators.
You talked about several different advanced techniques in this chapter. Not all of them are appropriate for every situation.
Chapter 18. Performance Tuning
There are so many pitfalls involved in optimizing your code, it's hard to know where to start.
The most important thing you need to know about optimizing Python code is that you shouldn't write your own timing function.
The first thing the Soundex function checks is whether the input is a non-empty string of letters. What's the best way to do this?
The second step of the Soundex algorithm is to convert characters to digits in a specific pattern. What's the best way to do this?
The third step in the Soundex algorithm is eliminating consecutive duplicate digits. What's the best way to do this?
The final step of the Soundex algorithm is padding short results with zeros, and truncating long results. What is the best way to do this?
This chapter has illustrated several important aspects of performance tuning in Python, and performance tuning in general.
<< Further reading |
| | |
Tips and tricks >> |