• Complain

Alex Martelli - Python cookbook

Here you can read online Alex Martelli - Python cookbook full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2005, publisher: OReilly, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Alex Martelli Python cookbook

Python cookbook: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Python cookbook" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Portable, powerful, and a breeze to use, Python is the popular open source object-oriented programming language used for both standalone programs and scripting applications. It is now being used by an increasing number of major organizations, including NASA and Google.Updated for Python 2.4, The Python Cookbook, 2nd Edition offers a wealth of useful code for all Python programmers, not just advanced practitioners. Like its predecessor, the new edition provides solutions to problems that Python programmers face everyday.It now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and building a templating system. This revised version also includes new chapters on topics such as time, money, and metaprogramming.Heres a list of additional topics covered:Manipulating text Searching and sorting Working with files and the filesystem Object-oriented programming Dealing with threads and processes System administration Interacting with databases Creating user interfaces Network and web programming Processing XML Distributed programming Debugging and testing Another advantage of The Python Cookbook, 2nd Edition is its trio of authors--three well-known Python programming experts, who are highly visible on email lists and in newsgroups, and speak often at Python conferences.With scores of practical examples and pertinent background information, The Python Cookbook, 2nd Edition is the one source you need if youre looking to build efficient, flexible, scalable, and well-integrated systems.

Alex Martelli: author's other books


Who wrote Python cookbook? Find out the surname, the name of the author of the book and a list of all author's works by series.

Python cookbook — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Python cookbook" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Index
[]accessingApacheapplicationsarithmeticarraysattributessettings forauthenticationremote logins
Index
[]backwards compatibilitybinary filesbinary large objects [See BLOBs]storing inbuilding
Index
[]C extensionsC programming language[See also cPickle module] [See also cPickle module]cachingscripts [See CGI scripts]classclasses [See also metaclasses] [See also metaclasses]codecode objectscollections.dequeCOMcommandsCommon Gateway Interface [See CGI]Common Object Request Broker Architecture [See CORBA]compressionconvertingcookiescopying[See also pickling] [See also pickling]creating
Introduction

Credit: Fred L. Drake, Jr., PythonLabs

Text-processing applications form a substantial part of theapplication space for any scripting language, if only becauseeveryone can agree that text processing is useful. Everyone has bitsof text that need to be reformatted or transformed in various ways.The catch, of course, is that every application is just a little bitdifferent from every other application, so it can be difficult tofind just the right reusable code to work with different fileformats, no matter how similar they are.

What Is Text?

Soundslike an easy question, doesn't it? After all, weknow it when we see it, don't we? Text is a sequenceof characters, and it is distinguished from binary data by that veryfact. Binary data, after all, is a sequence of bytes.

shows just such a heuristic.

Python strings are immutable sequences of bytes or characters. Mostof the ways we create and process strings treat them as sequences ofcharacters, but many are just as applicable to sequences of bytes.Unicode strings are immutable sequences of Unicode characters:transformations of Unicode strings into and from plain strings usecodecs (coder-decoders) objects that embodyknowledge about the many standard ways in which sequences ofcharacters can be represented by sequences of bytes (also known asencodings and charactersets). Note that Unicode strings do not serve double duty as sequences of bytes. illustrate the fundamentalsof Unicode in Python.

Okay, let's assume that our application knows fromthe context that it's looking at text.That's usually the best approach becausethat's where external input comes into play.We're looking at a file either because it has awell-known name and defined format (common in the"Unix" world) or because it has awell-known filename extension that indicates the format of thecontents (common on Windows). But now we have a problem: we had touse the word format to make the previousparagraph meaningful. Wasn't text supposed to besimple?

Let's face it: there's no suchthing as "pure" text, and if therewere, we probably wouldn't care about it (with thepossible exception of applications in the field of computationallinguistics, where pure text may indeed sometimes be studied for itsown sake). What we want to deal with in our applications isinformation contained in text. The text we care about may containconfiguration data, commands to control or define processes,documents for human consumption, or even tabular data. Text thatcontains configuration data or a series of commands usually can beexpected to conform to a fairly strict syntax that can be checkedbefore relying on the information in the text. Informing the user ofan error in the input text is typically sufficient to deal withthings that aren't what we were expecting.

Documents intended for humans tend to be simple, but they vary widelyin detail. Since they are usually written in a natural language,their syntax and grammar can be difficult to check, at best.Different texts may use different character sets or encodings, and itcan be difficult or even impossible to tell which character set orencoding was used to create a text if that information is notavailable in addition to the text itself. It is, however, necessaryto support proper representation of natural-language documents.Natural-language text has structure as well, but the structures areoften less explicit in the text and require at least someunderstanding of the language in which the text was written.Characters make up words, which make up sentences, which make upparagraphs, and still larger structures may be present as well.Paragraphs alone can be particularly difficult to locate unless youknow what typographical conventions were used for a document: is eachline a paragraph, or can multiple lines make up a paragraph? If thelatter, how do we tell which lines are grouped together to make aparagraph? Paragraphs may be separated by blank lines, indentation,or some other special mark. See for an example of reading atext file as a sequence of paragraphs separated by blank lines.

Tabular data has many issues that are similar to the problemsassociated with natural-language text, but it adds a second dimensionto the input format: the text is no longer linearit is nolonger a sequence of characters, but rather a matrix of charactersfrom which individual blocks of text must be identified andorganized.

Basic Textual Operations

As with any other data format, weneed to do different things with text at different times. However,there are still three basic operations:

  • Parsing the data into a structure internal to our application

  • Transforming the input into something similar in some way, but withchanges of some kind

  • Generating completely new data

.

Transforming text from one format to another is more interesting whenviewed as text processing, which is what we usually think of firstwhen we talk about text. In this chapter, we'll takea look at some ways to approach transformations that can be appliedfor different purposes. Sometimes we'll work withtext stored in external files, and other times we'llsimply work with it as strings in memory.

The generation of textual data from application-specific datastructures is most easily performed using Python's print statement or the write method of a file or file-like object. This is often done using amethod of the application object or a function, which takes theoutput file as a parameter. The function can then use statements suchas these:

print >>thefile, sometextthefile.write(sometext)

which generate output to the appropriate file. However, thisisn't generally thought of as text processing, ashere there is no input text to be processed. Examples of using both print and write can of coursebe found throughout this book.

Sources of Text

Working with text stored as a string inmemory can be easy when the text is not too large. Operations thatsearch the text can operate over multiple lines very easily andquickly, and there's no need to worry aboutsearching for something that might cross a buffer boundary. Beingable to keep the text in memory as a simple string makes it very easyto take advantage of the built-in string operations available asmethods of the string object.

Another interesting source for textualdata comes to light when we consider the network. Text is oftenretrieved from the network using a socket. While we can always view asocket as a file (using the makefile method of thesocket object), the data that is retrieved over a socket may come inchunks, or we may have to wait for more data to arrive. The textualdata may not consist of all data until the end of the data stream, soa file object created with makefile may not beentirely appropriate to pass to text-processing code. When workingwith text from a network connection, we often need to read the datafrom the connection before passing it along for further processing.If the data is large, it can be handled by saving it to a file as itarrives and then using that file when performing text-processingoperations. More elaborate solutions can be built when the textprocessing needs to be started before all the data is available.Examples of parsers that are useful in such situations may be foundin the htmllib and HTMLParser modules in the standardlibrary.

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Python cookbook»

Look at similar books to Python cookbook. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Python cookbook»

Discussion, reviews of the book Python cookbook and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.