[转载]Python related programming tutorials
信息来源:Magnus’s Python PagePython is one of my favourite programming languages. Like with so many other parts of my life, I have many half-finished Python-projects, none of which deserves a place here. A selected few of those who have made it to a semblance of completion are presented below (hopefully more will appear in the future).
More information about the Python language and related tools can be found at [url]www.python.org[/url] and in the newsgroup comp.lang.python.
Instant Hacking
You want to learn how to program a computer? And you don't know where to begin? I'll heartily recommend that you start by experimenting with Python. Download the interpreter and start playing. If you feel you need some guidance in the world of computer programming, you might want to take a look at my article Instant Hacking. It's a programming tutorial which uses Python for its examples.
Note: This is not a tutorial of computer crime of any sort. That sort of activity should correctly be referred to as “cracking,” not “hacking”.
Instant Python
This document is for those who already know how to program, and are curious about Python. It describes (rather compactly) the essential features of the language and aims to give a good impression of how it works. Ideally, after reading this, an experienced programmer should be able to write Python programs with only the aid of the Python library reference.
Mini-Python
What's this then? It's a subset of Python which is supposed to be a useful language in itself (and, of course, compatible with Python). My original intention was to use it to teach algorithms, but I haven't used it for anything yet. If you thought the two “instant”-articles were too long, try this. It is short, and gives you enough knowledge of Python to get going.
Instant Praise
My articles Instant Hacking and Instant Python have received praise from several readers — in a blatant display of ego (wink), I have put some excerpts up here.
Anygui — a Generic GUI Module
Anygui is an attempt at creating a thin layer of abstraction on top of several available (and some as yet unavailable) packages for creating graphical user interfaces in Python. The point is that it should be possible to write GUI programs in Python without worrying about which GUI packages the user has installed. The name is inspired by the standard Python library module anydbm.
For more information, take a look at my Anygui page, or the official web site.
A Naive Bayesian Classifier
This is a tiny implementation of a naive Bayesian classifier. You can train it by adding (with the add method) training “vectors,” that is, iterable objects (all of the same length) that contain hashable objects. Associated with each training vector is a class (another hashable object). After training, you can then classify unseen vectors; in other words, you can make the classifier which class best suits the vector.
The classifier is available here.
A PGM Reader
A tiny library for reading PGM (portable gray map) files into numeric arrays (requires numarray). It's available here.
The Towers of Hanoi
I just had to do this one... The puzzle is well-known (a Web search should give you plenty of info), and it is easily solved with recursion. Take a look at source here.
ciphersaber2.py
A simple but powerful secret key encryption algorithm. The script is based on an original by Ka-Ping Yee, which implemented the CipherSaber-1 algorithm. In this script I've changed it to comply with the CipherSaber-2 algorithm, as well as using encoding the data in a simple form of "ASCII armour", and using getpass to read the password, so it won't end up in your UNIX history file. The script can be found here.
Video Analysis
This is a project which is still in the planning stage: I am thinking about wrapping a video library in some way conducive to performing video analysis in Python, using SWIG, Pyrex, or some similar technology. One attractive possibility would be to integrate such a system with Numerical Python (or, more specifically, numarray). A simple (temporary) solution would be to use a plain C program to convert video data to raw binary data that can be memory-mapped and read with numarray's fromfile function.
A candidate for the C part is the MSSG codec software. An alternative approach might be to use Jython together with the Java Media Framework. If you have any ideas about how such a system might be implemented, please don't hesitate to contact me.
Piddle
I was one of the original creators of Piddle, a generic, multi-platform drawing toolkit for Python. I originally implemented the PostScript backend, which is now maintained by Chris Lee.
Algorithms in Python
My plan is to implement several standard algorithms in Python, both to show how suited the language is to teaching this sort of thing, and to show how the algorithms work. And for fun, of course. For now, I have implemented the standard dynamic programming algorithms for computing the Levenshtein distance between two strings.
Parser for Minimal XML
Minimal XML is a subset of XML 1.0 which leaves out several non-essential features, such as attributes, mixed contents, empty tags, etc. A preliminary specification can be found here.
I've made a small parser for Minimal XML (or Simple Markup Language, as it's called). It basically assumes that the input is valid SML, and does little error checking. The resulting tree structure is a tuple (tag, kids), where the tag is a string (the tag name, or nodeName) while kids is a list of subtrees, each of the same form. Character data are stored in nodes of the form (None, data) where data is a string.
“Detect” in Python
"Detect" is a control structure invented by Arne Halaas in the seventies. I have been persuaded by him that it is useful, and, although I have never actually used it for anything useful, I have implemented a version of it for python. If nothing else, it might be an interesting example of Python source code.
A minimal CGI publisher
A minimal CGI publisher for python scripts. The usage is described in the doc string. It can be used to turn standard scripts into CGI-scripts by simply importing the module and appending a single function call at the end of the script. A simple example of its use can be found here. (This was an experiment, to see if it was possible to make a really tiny publisher that would still be useful. For a more complete publisher, see the ZPublisher component of Zope, previously known as "Bobo").
Self-Printing One-Liner
If you run this one-liner at a command prompt, it should print out a copy of itself (write it as one continuous line, without the line break):
python -c "x='python -c %sx=%s; print x%%(chr(34),repr(x),chr(34))%s';
print x%(chr(34),repr(x),chr(34))"
Not very useful, but kinda fun... I just saw some other self-printing programs and thought it would be interesting to make a one-liner version.
Sleepcat — cat with a delay
This is a little program that emulates the unix command cat, except that a delay (“sleep”) is added after printing out each line. I don't really remember why I wrote this, but I'm sure it might be useful for reading large files without moving a muscle :)
Usage: sleepcat [ -t secs ] [ file1 [file2 ... ] ]
页:
[1]