Advancing Python "fifth chapter": Python's Advanced Application (ii) Common modules

Source: Internet
Author: User
Tags python script

Python's advanced Application (ii) Common module learning

Key points to learn in this chapter:

    1. Definition of Python module
    2. Time &datetime Module
    3. Random module
    4. OS Module
    5. SYS module
    6. Shutil Module
    7. Configparser Module
    8. Shelve module
    9. XML processing
    10. Re-regular expression
I. Definition of the Python module

C language Programming experience of friends know that in C language if you want to reference sqrt this function, you must use the phrase "#include <math.h>" to introduce math.h this header file, otherwise it will not be called normally. So in Python, what if you want to refer to some of the built-in functions? There is a concept in Python called module, which is similar to a header file in C and a package in Java, such as calling the SQRT function in Python, which must be introduced into the math module with the Import keyword. Here's a look at the modules in Python.

Modules are divided into three types
    • Custom Modules
    • Built-in standard module (also known as standard library)
    • Open source Module
the invocation of the module

Using the keyword import in Python to introduce a module, such as referencing module math, can be introduced with import math at the very beginning of the file. When you call a function in the math module, you must refer to this:

Module name. function name

Why do I have to add a module name to call it? Because there might be a situation where a function with the same name is in multiple modules, and if it is only called by the function name, the interpreter cannot know exactly which function to invoke. So if the module is introduced as described above, the calling function must add the module name.

Sometimes we just need to use a function in the module, we just need to introduce the function, at this point we can pass the statement

From Module name Import function name 1, function name 2 ....
Custom Modules

In Python, each Python file can be used as a module whose name is the name of the file.

For example, there is a file test.py that defines the function add in test.py:

#test. Pydef Add (b):    return a+b

In other files, you can import test first, then call it through Test.add (A, B), and of course, by using the From test import Add.

What happened when the module was introduced

First look at an example of the code in the file test.py:

#test. Pydef display ():    print ' Hello World '    display ()

Introduce the module test in test1.py:

#test1. Pyimport Test

Then run test1.py, which will output "Hello World". That is, when importing a module with import, the code in the introduced module file is executed once. Note, however, that the code in the module file is executed only the first time, because it is loaded only when it is first introduced, which is easy to understand and saves time and memory.

second, time &datetime module

Time Module Method:
Time.time (): Gets the timestamp of the current time

Time.localtime (): Accept a timestamp and convert it to a tuple of the current time. If you do not give arguments, Time.time () is passed in as a parameter by default

Time.localtime ():
Index Property Meaning
0 Tm_year Years
1 Tm_mon Month
2 Tm_mday Day
3 Tm_hour When
4 Tm_min Score of
5 Tm_sec Seconds
6 Tm_wday The day of the week
7 Tm_yday The day ordinal of a year
8 Tm_isdst Daylight saving time

    1. Time.mktime (): and Time.localtime () instead, it converts a time tuple into a timestamp (which must be given to a parameter)
    2. Time.asctime (): A time tuple is represented as: "Sun Jul 28 03:35:26 2013" In this format, the default is to pass Time.localtime () as a parameter.
    3. Time.ctime (): Converts a timestamp to the expression format of the Time.asctime (), and by default the Time.time () is passed as a parameter.
    4. Time.gmtime (): Converts a timestamp to utc+0 time zone (China should be + 8 time zone, 8 hours apart), and will default to Time.time () as a parameter.
    5. Time.strftime (Format,time.localtime ()): Converts a time tuple to a formatted time character, without giving the time tuple argument the default to pass Time.localtime () as a parameter

For example, the time format inside the Web log is time.strftime ('%d/%b/%y:%x ')

return Result:

>>> time.strftime ('%d/%b/%y:%x ') ' 21/aug/2016:20:06:56 '

Format

Property Format Meaning Range of values (format)
Year %y Remove the years of the century 00-99
%Y The full year
%j The day ordinal of a year 001-366
Month %m Month January 12
%b Name of the local simplified month Abbreviated English Month
%B Name of the local full month Full English month
Date %d The day of the one month January 31
Hours %H The first hours of the day (24-hour system) 00-23
%l Number of hours (12-hour system) "01-12"
Minutes %M Number of minutes 00-59
Seconds %s Seconds 00-59
Week %u Number of weeks in the year (starting from Sunday) 00-53
%W Number of weeks in the year (starting from Monday)
%w Day of the week one 0-6
Time %Z China: It should be gmt+8 (China standard Time) Seeking Greater God's literacy
Other %x Local corresponding date Day/month/year
%x Local Phase printing time Hours: minutes: seconds
%c Detailed date and time Day/month/year: minutes: Seconds
%% '% ' character '% ' character
%p Local AM or PM's corresponding character AM or PM


Time.strptime (Stringtime,format): Converts the time string to an array of times according to the specified formatter.
For example:

>>> time.strptime (' 21/aug/2016:20:06:56 ', '%d/%b/%y:%x ') time.struct_time (tm_year=2016, tm_mon=8, tm_mday= Tm_hour=20, Tm_min=6, tm_sec=56, tm_wday=6, tm_yday=234, Tm_isdst=-1)

Time.clock (): Returns the processor clock time, typically used for performance testing and benchmarking, as they reflect the actual time used by the program, which is not normally used.

Time.sleep (): Delay the specified time run, in seconds

Import Timeprint (Time.time ()) #打印时间戳print (Time.localtime ()) #打印本地时间元组print (Time.gmtime ()) #答应UTC + 0 time zone timeline print ( Time.ctime ()) #打印asctime格式化时间print (Time.mktime (Time.localtime ())) #将时间元组转换为时间戳print (Time.asctime ()) #打印格式化时间print (Time.strftime ('%d/%b/%y:%x ')) #打印指定格式的时间格式 # Translate the time string and its format into a time-tuple print (Time.strptime (' 21/aug/2016:20:06:56 ', '%d/%b /%y:%x ') print ('%0.5f '%time.clock ()) #打印处理器时间for I in range (100000): Passprint ('%0.5f '%time.clock ()) #打印处理器时间

Results:

1471781504.4066072time.struct_time (tm_year=2016, tm_mon=8, tm_mday=21, tm_hour=20, tm_min=11, tm_sec=44, Tm_wday=6, tm_yday=234, tm_isdst=0) time.struct_time (tm_year=2016, tm_mon=8, tm_mday=21, tm_hour=12, tm_min=11, tm_sec=44, tm_ Wday=6, tm_yday=234, tm_isdst=0) Sun 20:11:44 20161471781504.0Sun 20:11:44 201621/aug/ 2016:20:11:44time.struct_time (tm_year=2016, tm_mon=8, tm_mday=21, tm_hour=20, tm_min=6, tm_sec=56, tm_wday=6, Tm_yday =234, Tm_isdst=-1) 0.000000.00337

DateTime module
Datetime.time (): Generates a Time object. This time can be set by us, the default is 0 (this class is only for the time)

#coding: Utf-8import datetimeprint datetime.time () t = datetime.time (1, 3, 5,) print Tprint t.hour #时print t.minute #分prin T t.second #秒print t.microsecond #毫秒print datetime.time.max #一天的结束时间print datetime.time.min #一天的开始时间

Results:

00:00:0001:03:05.000025 23:59:59.99999900:00:00

Datetime.date (): Generates a Date object. This date will be set by us (this class is only for dates)

Import datetime# Set Date t = Datetime.date (8, +) #打印设置日期的和元组print (T.timetuple ()) #日期元组print (t) print (t.year) #年print ( T.month) #月print (t.day) #日 # Get today's Day date = Datetime.date.today () print (today) print (Datetime.datetime.now ()) # This prints to the millisecond level # gets the tuple of today's date T1 = Today.timetuple () print (T1) #打印成ctime格式 (time.ctime () format) # '%a%b%d%h:%m:%s%Y ' Print (T.ctime ()) Print (Today.ctime ()

Results:

Time.struct_time (tm_year=2016, tm_mon=8, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=234, tm_isdst=- 1) 2016-08-2120168212016-08-212016-08-21 20:18:10.550607time.struct_time (tm_year=2016, tm_mon=8, tm_mday=21, tm_ Hour=0, Tm_min=0, tm_sec=0, tm_wday=6, tm_yday=234, tm_isdst=-1) Sun 00:00:00 2016Sun 21 00:00:00 2016
third, random module

Get a floating-point number less than 1

>>> Import random>>> Print (Random.random ()) 0.958520379647631

Get an integer from 1 to 10

>>> Print (Random.randint (1,10)) 5

Gets a floating-point number greater than 0 and less than 2

>>> Print (Random.uniform (0,2)) 1.6072811216275604

Get a random number from 1 to 10 step 4

>>> Print (Random.randrange (1,10,4)) 9

Remove 3 elements from List A in random order (one element can only be removed once, so the number of extracts cannot be greater than the number of elements contained in the list)

>>> a=[1,2,3,4,5]>>> Print (Random.sample (a,3)) [5, 2, 1]
iv. OS module

I. Overview of OS Modules

The Python OS module contains common operating system features. This module is especially important if you want your program to be platform-agnostic. (in one's language)

Second, common methods

1, Os.name

The output string indicates the platform being used. If the window is ' NT ', it is ' POSIX ' for Linux/unix users.

2, OS.GETCWD ()

The function gets the current working directory, which is the directory path of the current Python script work.

3, Os.listdir ()

Returns all file and directory names under the specified directory.

>>> Import os>>> Os.listdir (OS.GETCWD ()) ['. Idea ', ' day01 ', ' day02 ', ' day02_note ', ' day03 ', ' day04 ', ' Day04_atm ', ' day05 ']

4, Os.remove ()

Deletes a file.

5, Os.system ()

Run the shell command.

>>> os.system (' dir ') 0>>> os.system (' cmd ') #启动dos

6. OS.SEP can replace operating system-specific path separators.

7. Os.linesep string gives the line terminator used by the current platform

>>> os.linesep ' \ r \ n '            #Windows使用 ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '. >>> os.sep ' \ \ '              

8, Os.path.split ()

function returns the directory name and file name of a path

>>> os.path.split (' C:\\python35\\abc.txt ') (' c:\\python35 ', ' abc.txt ')

The 9, Os.path.isfile (), and Os.path.isdir () functions respectively verify that the given path is a file or a directory.

>>> Os.path.isdir (OS.GETCWD ()) true>>> os.path.isfile (' a.txt ') False

10, Os.path. The exists() function is used to verify that the given path really exists

11. Os.path.abspath (name): Get absolute path

12, Os.path.normpath (PATH): Canonical path string form

13, Os.path.getsize (name): Get file size, if name is directory return 0L

14, Os.path.splitext (): Detach file name and extension

>>> os.path.splitext (' a.txt ') (' a ', '. txt ')

15, Os.path.join (path,name): Connection directory and file name or directory

16. Os.path.basename (PATH): Return file name

17. Os.path.dirname (PATH): Return file path

>>> os.path.dirname (' c:\\python\\a.txt ') ' C:\\python '
Five, sys module
SYS.ARGV           command line argument list, the first element is the program itself path Sys.exit (n)        exits the program, exit normally (0) sys.version        Gets the version information of the Python interpreter Sys.maxint         the largest int value Sys.path           returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing Sys.platform       Returns the operating system platform name Sys.stdout.write (' please: ') val = Sys.stdin.readline () [:-1]
vi.. Shutil module

Seven, Configparser module

Used to generate and modify common configuration documents, the name of the current module is changed to Configparser in Python version 3.x

Linux configuration files are basically the following format, how to use Python to generate the following configuration file?

[DEFAULT] Serveraliveinterval = 45Compression = Yescompressionlevel = 9forwardx11 = yes [Bitbucket.org]user = HG [topsecret.server.c Om]port = 50022forwardx11 = no

With the Configparser module, you can

Import Configparser config = Configparser. Configparser () config["DEFAULT"] = {' Serveraliveinterval ': ' A ',                      ' Compression ': ' Yes ',                     ' compressionlevel ': ' 9 '} config[' bitbucket.org ' = {}config[' bitbucket.org ' [' User '] = ' HG ' config[' topsecret.server.com '] = {}topsecret = config[' topsecret.server.com ']topsecret[' Host Port ' = ' 50022 '     # mutates the parsertopsecret[' ForwardX11 '] = ' no ' c3/># same hereconfig[' DEFAULT ' [' ForwardX11 '] = ' yes ' with open (' Example.ini ', ' W ') as ConfigFile:   config.write ( ConfigFile)
Eight, shelve module

Shelve is a simple data storage scheme, he has only one function is open (), the function receives a parameter is the file name, and then return a shelf object, you can use him to store things, you can simply think of him as a dictionary, when you are stored, Just call the close function to close the

Import Shelve D = Shelve.open (' shelve_test ') #打开一个文件 class Test (object):    def __init__ (self,n):        SELF.N = n  t = Test (123) t2 = Test (123334) name = ["Alex", "Rain", "Test"]d["test"] = name #持久化列表d ["T1"] = t      #持久化类d ["t2"] = T2 d.close ()
ix. processing of XML files

What is XML?

XML Extensible Markup Language, which can be used to tag data, define data types, is a source language that allows users to define their own markup language.

parsed XML file (country.xml):

<?xml version= "1.0"?> <data>   <country name= "Singapore" >     <rank>4</rank>     <year>2011</year>     <gdppc>59900</gdppc>     <neighbor name= "Malaysia" direction= "N "/>   </country>   <country name=" Panama ">     <rank>68</rank>     <year> 2011</year>     <gdppc>13600</gdppc>     <neighbor name= "Costa Rica" direction= "W"/>     <neighbor name= "Colombia" direction= "E"/>   

Working with Xml.etree.ElementTree XML

ElementTree is born to deal with XML, which has two implementations in the Python standard library: one that is pure python, such as Xml.etree.ElementTree, and the other a faster xml.etree.cElementTree. Note: Try to use the C language as much as possible, because it is faster and consumes less memory.

#!/usr/bin/evn python #coding: Utf-8   try:   import xml.etree.cElementTree as ET except Importerror:   Import Xml.etree.ElementTree as ET import sys   try:   tree = et.parse ("Country.xml")     #打开xml文档   #root = Et.fromstring (country_string) #从字符串传递xml   root = Tree.getroot ()         #获得root节点  except Exception, E:   Print ("Error:cannot parse File:country.xml.")  Sys.exit (1) print (Root.tag, "---", root.attrib) for child in Root:   print (Child.tag, "---", child.attrib)   Print ("*" *10) print (root[0][1].text)   #通过下标访问 print (Root[0].tag, root[0].text) print ("*" *10) for  country in Root.findall (' Country '): #找到root节点下的所有country节点   rank = country.find (' rank '). Text   #子节点下节点rank的值   Name = Country.get (' name ')      #子节点下属性name的值   Print (name, rank)      #修改xml文件 for Country in Root.findall (' Country '):   rank = int (country.find (' rank '). Text)   If rank >:     root.remove (country)   

Creating an XML file

Import xml.etree.ElementTree as et  new_xml = et. Element ("NameList") name = ET. Subelement (New_xml, "name", attrib={"enrolled": "Yes"}) Age = ET. subelement (Name, "Age", attrib={"checked": "No"}) Sex = ET. subelement (name, "sex") Sex.text = ' name2 ' = ET. Subelement (New_xml, "name", attrib={"enrolled": "No"}) Age = ET. Subelement (name2, "age") Age.text = ' + ' et = et. ElementTree (New_xml) #生成文档对象et. Write ("Test.xml", encoding= "Utf-8", Xml_declaration=true) Et.dump (new_xml) #打印生成的格式
Nine, re regular expressions

Common Regular Expression symbols

‘.‘ The default match is any character except \ n, if you specify flag Dotall, match any character, including the newline ' ^ ' match character beginning, and if you specify flags MULTILINE, this can also match (r "^a", "\nabc\neee", Flags=re. MULTILINE) ' $ ' matches the end of the character, or E.search ("foo$", "BFOO\NSDFSF", Flags=re. MULTILINE). Group () can also ' * ' match the character before the * number 0 or more times, Re.findall ("ab*", "Cabb3abcbbac") results for [' ABB ', ' ab ', ' a '] ' + ' matches the previous character 1 times or more, re.     FindAll ("ab+", "Ab+cd+abb+bba") results [' AB ', ' ABB '] '? '     Match previous character 1 times or 0 times ' {m} ' match previous character M times ' {n,m} ' matches previous character N to M times, Re.findall ("ab{1,3}", "ABB ABC abbcbbb") Results ' ABB ', ' AB ', ' ABB '] ' | ' Match | left or | Right character, re.search ("abc| ABC "," ABCBABCCD "). Group () Results ' ABC ' (...) ' Group match, Re.search (" (ABC) {2}a (123|456) C "," abcabca456c "). Group () results abcabca456c ' \a ' matches only from the beginning of the character, Re.search ("\aabc", "ALEXABC") is not matched to the ' \z ' match character end, the same as $ ' \d ' matches the number 0-9 ' \d ' matches the non-numeric ' \w ' match [A -ZA-Z0-9] ' \w ' matches non-[a-za-z0-9] ' s ' matching whitespace characters, \ t, \ n, \ r, Re.search ("\s+", "Ab\tc1\n3"). Group () result ' \ t ' (? P<name&gt, ...) ' Group Matching Re.search (? P<province>[0-9]{4}) (? P<city>[0-9]{2}) (? P&LT;BIRTHDAY&GT;[0-9]{4}) "," 371481199306143242 "). Groupdict (" city ") result {' Province ': ' 3714 ', ' City ': ' Bayi ', ' Birthday ': ' 1993 '} 

The main function function of RE

Common function functions include: Compile, search, match, split, FindAll (Finditer), Sub (SUBN)
Compile
Re.compile (pattern[, flags])
Function: Converts regular expression syntax into regular expression objects
The flags definition includes:
Re. I: Ignore case
Re. L: Represents a special character set \w, \w, \b, \b, \s, \s dependent on the current environment
Re. M: Multi-line mode
Re. S: '. ' and any character including newline characters (note: '. ' Do not include newline characters.
Re. U: Represents a special character set \w, \w, \b, \b, \d, \d, \s, \s dependent on Unicode character Property database

Search
Re.search (pattern, string[, flags])
Search (string[, pos[, Endpos])
Function: Finds the position in the string that matches the regular expression pattern, returns an instance of Matchobject, or none if no matching position is found.

Match
Re.match (pattern, string[, flags])
Match (string[, pos[, Endpos])
Function: the match () function attempts to match the regular expression only at the beginning of the string, that is, only the match that starts at position 0 is reported, and the search () function scans the entire string to find a match. If you want to search the entire string for a match, you should use Search ().

Here are a few examples:

#!/usr/bin/env Pythonimport rer1 = re.compile (R ' World ') if R1.match (' HelloWorld '):    print ' match succeeds ' else:    print ' match fails ' if R1.search (' HelloWorld '):    print ' Search succeeds ' else:    

Note:R is the meaning of raw (raw). Because there are some escape characters in the representation string, such as the carriage return ' \ n '. If you want to indicate \ table needs to be written as ' \ \ '. But if I just need to represent a ' \ ' + ' n ', do not use the R method to write: ' \\n '. But using R means R ' \ n ' is much clearer.

Advancing Python "fifth chapter": Python's Advanced Application (ii) Common modules

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.