Python3.2 official documentation translation-standard library Overview (2)

Source: Internet
Author: User

7.5 String Matching

The re module provides regular expression matching for advanced string synthesis. For complex matching and processing, regular expressions can provide concise optimization methods:

>>> Import re

>>> Re. findall (R' \ bf [a-z] * ', 'which foot or hand fell fastest ')

['Foot', 'fell ', 'fastest']

>>> Re. sub (R' (\ B [a-z] +) \ 1', R' \ 1', 'cat in the hat ')

'Cat in the hat'

When you only need some simple functions, the string method is preferred because it is easier to read and debug.

>>> 'Tea for too'. replace ('too', 'two ')

'Tea for two'

7.6 mathematics

The Mathematical Module provides access to the underlying C function library for floating point operations.

>>> Import math

>>> Math. cos (math. pi/4)

0.70710678118654757

>>> Math. log (1024, 2)

10.0

The Random module provides a tool for generating Random selections.

>>> Import random

>>> Random. choice (['apple', 'pear ', 'Banana'])

'Apple'

>>> Random. sample (range (100), 10) # sampling without replacement

[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]

>>> Random. random () # random float

0.17970987693706186

>>> Random. randrange (6) # random integer chosen from range (6)

4

SciPy project The project contains many data computing modules.

10.7 Internet access

Python has many modules for accessing the Internet and processing internet protocols. The simplest two are urllib. request from the link and smtplib.

>>> From urllib. request import urlopen

>>> For line in urlopen ('HTTP: // tycho.usno.navy.mil/cgi-bin/timer.pl '):

... Line = line. decode ('utf-8') # Decoding the binary data to text.

... If 'est 'in line or 'edt' in line: # look for Eastern Time

... Print (line)


Nov. 25 and 09:43:32 EST

>>> Import smtplib

>>> Server = smtplib. SMTP ('localhost ')

>>> Server. sendmail ('soothsayer @ example.org ', 'jcaesar @ example.org ',

... "To: jcaesar@example.org

... From: soothsayer@example.org

...

... Beware the Ides of March.

...""")

>>> Server. quit ()

(Note that the second example requires an email server running locally)

7.8 time and date

The Datatime module provides classes for processing time and date in simple or complex ways. When processing date and time data, the key to formatting output and processing is to extract university members. This module also supports Time Zone processing.

>>># Dates are easily constructed and formatted

>>> From datetime import date

>>> Now = date. today ()

>>> Now

Datetime. date (2003, 12, 2)

>>> Now. strftime ("% m-% d-% y. % d % B % Y is a % A on the % d day of % B .")

'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December .'

>>># Dates support calendar arithmetic

>>> Birthday = date (1964, 7, 31)

>>> Age = now-birthday

>>> Age. days

14368

7.9 Data Compression

Python also supports packaging and compression of common data. It mainly involves the modular zlib, gzip, bz2, zipfile and tarfile.

>>> Import zlib

>>> S = B 'witch which has which witches wrist watch'

>>> Len (s)

41

>>> T = zlib. compress (s)

>>> Len (t)

37

>>> Zlib. decompress (t)

B 'witch which has which witches wrist watch'

>>> Zlib. crc32 (s)

226805979

7.10 Performance Evaluation

Some python users are interested in the performance of different solutions to the same problem. Python provides an evaluation tool to answer these questions immediately.

For example, when encapsulating parameters, you can replace the traditional method with the feature of encapsulation and unblocking of tuples. The Timeit module can quickly describe a performance advantage.

>>> From timeit import Timer

>>> Timer ('t= a; a = B; B = t', 'a = 1; B = 2'). timeit ()

0.57535828626024577

>>> Timer ('a, B = B, A', 'a = 1; B = 2'). timeit ()

0.54962537085770791

Compared with timeit, the profile and pstate modules provide tools to identify time-critical zones in large code blocks.

6.11 Quality Control

One way to develop high-quality software is to write test cases for each function. These use cases are frequently run during development.

The Doctest module provides a scanning module and verifies the test nested in program document characters. Testing is a simple way to cut and paste a typical Call and its results into a document string. This improves the document by providing an instance to the user and allows the doctext module to verify that the code matches the document.

The Unittest module is not as easy to use as the doctest module. However, it allows a more complex test to maintain separated files.

Read/write directly in the database format. In short, these modules and packages greatly simplify data exchange between python applications and other tools.

Several modules can be internationalized, including gettext, local, and codecs packages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.