"Learning Notes--python"python standard library Concise tutorial Ii__python

Source: Internet
Author: User
Tags crc32 garbage collection locale stdin unpack pprint
A concise tutorial on Python standard library II Table of Contents1 output Format 2 template 3 processing binary Data 4 multithreading 5th log (Logging) 6 Weak reference (Weak References) 7 tools for use with list 80 in-line floating-point data operations 1 output Format

The repr module provides a customized version of Repr () for the abbreviated form of a container with a large amount of content

>>> import repr
>>> repr.repr (Set (' Helloword '))
' Set ([' d ', ' e ', ' h ', ' l ', ' o ', ' r ', ', ', ', ', ', ', ', ']] "

The pprint module provides more sophisticated controls for printing built-in and user-defined objects, and the output results are more readable.

>>> import pprint
>>> t = [[[[' Black ', ' cyan '], ' white ', [' green ', ' red ']]]]
>>> Pprint.pprint (t, width=30) [[[[[
' black ', ' cyan '],
   ' White ',
   [' Green ', ' red ']]]

The textwrap module can format paragraphs to fit the width of the screen

>>> import TextWrap
>>> doc = "" "The Wrap () is just like fill () except that it returns
. . A list of strings instead of one big string with newlines to separate
... the wrapped lines.
"" " >>> 
>>> Print Textwrap.fill (doc, width =) The
wrap () method was just like
fill () except th At it returns
a list of strings instead of one big string with newlines to separate the wrapped
lines.

The locacle module can determine the data format according to culture and region:

>>> Locale.setlocale (locale. Lc_all, ' Zh_cn.utf8 ')
' Zh_cn.utf8 '
>>> conv = locale.localeconv ()
>>> x = 1234567.8< C15/>>>> Locale.format ("%d", X, Grouping=true)
' 1,234,567 '
2 Templates

The string module provides a versatile Template class that is simple in syntax and can be easily edited by ordinary users, allowing users to customize their applications.

>>> from string import Template
>>> t = Template (' ${village}folk send $$10 to $cause. ')
>>> T.substitute (village= ' Nottingham ', case= ' Ditch Fund ')
' nottinghamfolk send $ to the ditch fund. '

Note that $ can begin to define a bit character that can be replaced by real data in the future. If you want to enter real, you can enter two consecutive. If you do not specify a value for the substitute in the template, the Keyerror is returned. If you use Safe_ substitute, no specified bit character is ignored.

>>> t = Template (' Return "$item to $owner. ')
>>> t.substitute (item= ' value ')
Traceback (most recent call last):
  File "<stdin>", line 1, in < module>
  File "/usr/lib/python2.7/string.py", line 172, in substitute return
    self.pattern.sub (convert, self.template)
  File "/usr/lib/python2.7/string.py", line 162, in convert
    val = mapping[named]
keyerror: ' owner '
>>> t.safe_substitute (item= ' value ')
' return the value to $owner. '

The Template subclass can specify qualifiers, that is, to replace $ with other symbols, such as the following batch renaming program.

Import time
import Os.path from
string import Template
photofiles = [' img_1074.jpg ', ' img_1076.jpg ', ' img_ ' 1077.jpg ']
class Batchrename (Template):
    delimiter = '% '
FMT = raw_input (' Enter rename style (%d-date% N-seqnum%f-format):  '


t = batchrename (FMT)
date = Time.strftime ('%d%b%y ')
for I, filename in Enumerate (photofiles):
    Base, ext = Os.path.splitext (filename)
    newname = T.substitute (D=date, n=i, F=ext)
    print ' {0}--> {1} '. Format (filename, newname)
>>> Import temp
Enter rename style (%d-date%n-seqnum%f-format):  steve_%n%f
img_1074.jpg- > steve_0.jpg
img_1076.jpg--> steve_1.jpg
img_1077.jpg--> steve_2.jpg
3 processing binary Data

The struct package provides a pack () and unpack () function that handles data in binary record formats of different lengths. The following code shows how to handle the header information of a ZIP file without using the ZipFile module. "H" and "I" represent two-byte, four-byte unsigned digits, respectively. "<" represents the standard size, small end byte order.

import struct

data = open (' Tzipfile.zip ', ' RB '). Read ()
start = 0 for
i in range (3): # Show the The ' The '                      3 fil E headers
    start +
    = Fields = Struct.unpack (' <iiihh ', data[start:start+16])
    CRC32, Comp_size, Uncomp_ Size, filenamesize, extra_size = Fields

    start = +
    filename = data[start:start+filenamesize]
    start = = Filenamesize
    extra = data[start:start+extra_size]
    print filename, hex (CRC32), comp_size,

    uncomp_size Start + = extra_size + comp_size     # Skip to the next header

$ zipinfo tzipfile.zip 
Archive:  tzipfile.zip
zip file size:442 bytes, number of Entries:3
-rw-rw-r--< c4/>3.0 unx        0 bx stor 13-mar-08 14:31 tzipfile.c-rw-rw-r--3.0  unx        0 bx stor
13-mar-08 14:38 f2.c-RW -rw-r--  3.0 unx        0 bx stor 13-mar-08 14:38 f3.c
3 files, 0 bytes uncompressed, 0 bytes compressed:  0%

$ python tstruct.py 
tzipfile.c 0x0 0 0
f2.c 0x0 0 0
f3.c 0x0 0 0
more than 4 threads

A thread is a technique that can be executed separately from tasks that do not trust the sequence, and can be used to improve the response of a program. For example, when you wait for user input, you run other tasks in the background.

Import threading, ZipFile

class Asynczip (threading. Thread):
    __init__(self, infile, outfile):
        Threading. Thread.__init__ (self)
        self.infile = infile
        self.outfile = outfile
    run(self):
        f = zipfile. ZipFile (Self.outfile, ' W ', ZipFile. zip_deflated)
        f.write (self.infile)
        f.close ()
        print ' Finished background zip of: ', Self.infile

Background = Asynczip (' mydata.txt ', ' myarchive.zip ')
background.start ()
print ' The main program continues to Run in foreground. '

Background.join () # Wait for the    "background task to finish
print" Main program waited until background is done. '

The challenge of multithreaded routines is to coordinate the threads that share data and resources. The thread module extracts a large number of synchronization primitives, such as locks, events, condition variables, semaphores. Although these tools are very powerful, low-level design errors often result in a number of errors that cannot be reproduced. So, the better way is to use one thread to access the resource and use the Queue module to get the data from the other threads in turn. 5th Chi (Logging)

The logging module provides a flexible and versatile logging system, in the simplest case of sending log information to a file or Sys.stderr:

>>> Import Logging
>>> logging.debug ("Debugging Infor")
>>> logging.info (" Infomation mess ")
>>> logging.warning (" Warning mess ")
WARNING:root:Warning mess
>>> Logging.error ("Error mess")
ERROR:root:Error mess
>>> logging.critical (' Critical error ')
CRITICAL:root:Critical Error

The logging system can be configured directly by Python, or it can be customized using an editable configuration file. 6 Weak references (Weak References)

Python automatically manages memory and frees up memory when the last reference is eliminated. This mechanism works well in most cases, but sometimes you need to track the object to see when it is referenced. But the trace object itself is a reference to the object. The weakref module provides trace objects, but does not create references, and automatically removes them from the Weakref table when the object is no longer needed.

>>> import Weakref, GC >>> class A:. Def  __init__  (SE 
LF, value): ... self.value = value ... def  __repr__  (self): ... return str (self.value) ... >>> a = A (a) # Create a reference >>> D = weakref.                Weakvaluedictionary () >>> d[' primary '] = a # does not create a reference >>> d[' primary '] 
# Fetch the object if it is still alive >>> del A # remove the one reference  >>> Gc.collect () # Run garbage collection right away 0 >>> d[' primary '] #
    Entry was automatically removed Traceback (most recent called last): File ' <stdin> ', line 1, in <module> 
    d[' primary '] # entry is automatically removed File "c:/python26/lib/weakref.py", line-, in __getitem__ o = Self.data[key] () Keyerror: ' Primary ' 
7 tools used in conjunction with List

The built-in list type can meet the needs of many data structures, but some other data structures similar to the list are sometimes needed for performance tradeoffs. The array module extracts an array () object that stores only the same type of data and can be stored in a more compact manner. The following example stores the unsigned binary number in 2 bytes per item, not the 16 byte of the list.

>>> from array Import array
>>> a = Array (' H ', [1,2,3])
>>> sum (a)
6
> >> A[1:3]
Array (' H ', [2, 3])
>>> 

The Collections module provides a deque () object that is very similar to the list, but can be added faster from the left to remove data, while searching from the center slows down. This object is suitable for implementing queue and width first search trees.

>>> from collections import deque
>>> d = deque (["Task1", "Task2", "TASK3"])
>>> D.append ("Task4")
>>> print "handleing", D.popleft ()
handleing Task1

In addition, the standard library extracts examples such as the Bisect module used to manipulate sorted list.

>>> import bisect
>>> scores = [(MB, ' Perl '), (' Tcl '), ("Python")]
>>> bise Ct.insort (scores, (+, ' Ruby '))
>>> scores
[(+, ' Perl '), ("Tcl"), (+, ' Ruby '), ("Python") )]

The HEAPQ module provides functions for implementing heaps on a common lists basis. Items with the smallest value are usually placed at position 0. This is useful when you are accessing the smallest element frequently, but you do not need to sort the entire list.

>>> from HEAPQ import heapify, Heappop, heappush
>>> data = [1,3,4,6,2,7]
>>> heapify (data)
>>> Heappush (data,-5)
>>> [Heappop (data) for I in range (3)]
[-5, 1, 2]
8 Decimal floating-point data operations

The decimal module provides *decimal* data types for decimal floating-point operations. Compared with the built-in float, this class is useful in the following ways: finance and other applications that require precise decimal notation to control rounding, to meet legally controllable requirements tracking important decimal users want results and manual calculations for the same application

For example, the following calculation uses decimal floating-point and binary floating-point computations to be different

>>> from decimal Import *
>>> x = decimal (' 0.70 ') * decimal (' 1.05 ')
>>> x
Decimal (' 0.7350 ')
>>> x.quantize (Decimal (' 0.01 '))
decimal (' 0.74 ')
>>> 0.7 * 1.05
0.735
>> > Round (. * 1.05, 2)
0.73
>>> 1.999999999+0.0000000000000000000000001
1.999999999
>>> decimal (' 1.999999999 ') + decimal (' 0.0000000000000000000000001 ')
decimal (' 1.9999999990000000000000001 ')

Original link: http://docs.python.org/2/tutorial/stdlib2.html

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.