A tutorial on the use of Python modules and standard libraries

Source: Internet
Author: User
Tags datetime hash json month name object model time interval pprint timedelta

#!/usr/bin/env python
# Coding=utf-8

Lang = "Python"
Introducing Modules

>>> Import Sys
>>> sys.path.append ("~/documents/vbs/startlearningpython/2code/pm.py")
>>> Import PM
>>> Pm.lang
' Python '
When the Python interpreter reads. A py file, first turning it into a. pyc file that consists of bytecode, and then this. pyc file is handed to something called a Python virtual machine (those so-called compiled languages are also the same process, but they have an obvious compilation process first, After the compilation is ready to run). If the. py file is modified, the Python interpreter will recompile, except that the compilation process is not fully displayed to you.

What I'm saying here is more general, and to get an insight into the execution of Python programs, read this article: Talk about the execution of a python program

With the. pyc file, you do not need to restart the interpreter to compile the. py file each time you run, unless the. py file has been modified. In this way, Python is running the compiled. pyc file.

ifname== "Main"

If you want to execute as a program, then __name__ = = "__main__"; if introduced as a module, then pm.__name__ = = "PM", that is, the value of the attribute __name__ is the module name.

The location of the module

>>> Import Sys
>>> Import Pprint
>>> pprint.pprint (Sys.path) #查看所有模块的位置
The role of __all__ in modules

#/usr/bin/env Python
# Coding:utf-8

__all__ = [' _private_variable ', ' Public_teacher ']

public_variable = "Hello, I am a public variable."
_private_variable = "Hi, I am a private variable."

Def public_teacher ():
Print "I am a public teacher, I am from JP." #Python 3:print ("I am a public teacher, I am from JP.")

Def _private_teacher ():
Print "I am a private teacher, I am from CN." #Python 3:print ("I am a private teacher, I am from CN.")
The __all__ property, and the corresponding value, contains the name of a private variable and the name of a function in the __all__ property list. This is to tell the interpreter referencing this module that these two things are accessed by permission, and only these two things.

Package or library

The package or library should be larger than the "module". It is true, generally speaking, a "package" inside there will be multiple modules, of course, "library" is a larger concept, such as the Python standard library of each library has a lot of packages, each package has several modules.

A package is made up of multiple modules, that is, multiple. py files, so this so-called "package" is a directory we are familiar with. Now you need to solve the problem of how to reference a module in a directory. The workaround is to place a __init__.py file in the directory. __init__.py is an empty file that is placed in a directory so that other. py files in that directory can be referenced as modules.

Self-carrying battery

When Python is installed, a number of modules are installed on the local computer as well. These things, like "energy" and "power", give Python a lot of life and make it easy to use many modules for free. So call it "self-powered battery."

The modules that are installed by default when you install Python are collectively referred to as "standard libraries."

Way of referencing


Import Pprint #引入模块

From Pprint import Pprint #引入该模块下的方法

From pprint Import * #引入该模块下的所有方法

Import Pprint as PR #重命名模块

From Pprint import Pprint as PT #重命名方法
Deeply explore

Dir () to view the properties and methods of the object
Help () View the meaning of an object
Help, documentation, and source code

Print pprint.__doc__ #查看文档
Print pprint.__file__ #查看模块的位置, according to this location to find the source code
Standard library

Sys

sys.argv

SYS.ARGV is specifically used to pass arguments to the Python interpreter, named "Command-Line arguments."

$ python--version #--veriosn is the command line argument
Python 2.7.6
Sys.exit ()

Exits the current program.

Return is used in most functions, meaning terminating the current function and returning the corresponding value to the location where the function is called (if none is not). But Sys.exit () means exiting the current program-not just a function, but a systemexit exception. This is the difference between the two.

If you use Sys.exit (0) to indicate a normal exit. If you need to have a friendly tip when exiting, you can use Sys.exit ("I wet out in here") and the string information will be printed.

Sys.stdout

In contrast to functional functions in Python, Sys.stdin gets input (equivalent to the raw_input () in Python 2, the input () in Python 3), and Sys.stdout is responsible for the output.

>>> f = open ("Stdout.md", "W")
>>> sys.stdout = f #重定向到文件
>>> print "Learn python:from beginner to Master" #Python 3:print ("Learn python:from beginner to Master" )
>>> F.close ()
Copy

Import Copy
Copy.copy () #浅拷贝
Copy.deepcopy () #深拷贝
Os

Manipulating files

Import OS
Os.rename ("22201.py", "newtemp.py") #重命名文件
Os.remove ("123.txt") #删除一个文件, cannot be a directory
Operations Directory

Os.listdir: Displaying content in a directory (including files and subdirectories)

OS.GETCWD: Get the current working directory;

Os.pardir: Get the top level catalogue

Os.chdir: Changing the current working directory

Os.makedirs, Os.removedirs: Creating and deleting Catalogs

File and Directory Properties

Os.stat (p) Displays the properties of a file or directory

Os.chmod () Change permissions

Operation Command

An OS module provides a way for a licensed program member to use the operating system's commands in a Python program.

>>> P
'/home/qw/documents/vbs/starterlearningpython '
>>> command = "ls" + P #命令复制给Command变量
>>> command
>>> os.system (command) #执行命令
It is to be noted that Os.system () executes the command in the current process until it finishes execution. If you need a new process, you can use Os.exec or OS.EXECVP. Readers who are interested in detailed information can view the help documentation. In addition, Os.system () executes the command through the shell and returns control back to the original process after execution, but Os.exec () and associated functions do not return control to the original inheritance after execution, causing Python to lose control.

#!/usr/bin/env python
# Coding=utf-8

Import WebBrowser
Webbrowser.open ("http://www.baidu.com") #跨平台打开浏览器
HEAPQ: Heap

HEADPQ Module

>>> Import HEAPQ
>>> heapq.__all__
[' Heappush ', ' heappop ', ' heapify ', ' heapreplace ', ' merge ', ' Nlargest ', ' nsmallest ', ' Heappushpop ']
Heappush (heap, x): pressing x into the heap heap

>>> Import HEAPQ
>>> heap = []
>>> Heapq.heappush (Heap, 3)
>>> Heapq.heappush (Heap, 9)
>>> Heapq.heappush (heap, 2)
>>> Heapq.heappush (Heap, 4)
>>> Heapq.heappush (heap, 0)
>>> Heapq.heappush (heap, 8)
>>> Heap
[0, 2, 3, 9, 4, 8]
Heappop (heap): Delete the smallest element

>>> Heapq.heappop (Heap)
0
>>> Heap
[2, 4, 3, 9, 8]
Heapify (): Convert list to heap

>>> hl = [2, 4, 6, 8, 9, 0, 1, 5, 3]
>>> heapq.heapify (HL)
>>> HL
[0, 3, 1, 4, 9, 6, 2, 5, 8]
Heapreplace () is the Union of Heappop () and Heappush (), which is to delete one and join a

>>> Heap
[2, 4, 3, 9, 8]
>>> heapq.heapreplace (heap, 3.14)
2
>>> Heap
[3, 4, 3.14, 9, 8]
Deque: two-terminal queue

>>> qlst.append (5) #从右边增加
>>> Qlst
Deque ([1, 2, 3, 4, 5])
>>> Qlst.appendleft (7) #从左边增加
>>> Qlst
Deque ([7, 1, 2, 3, 4, 5])

>>> Qlst.pop () #右边删除一个元素
5
>>> Qlst
Deque ([7, 1, 2, 3, 4])
>>> qlst.popleft () # Delete an element to the left
7
>>> Qlst
Deque ([1, 2, 3, 4])

>>> qlst.rotate (3) #循环移动n个位置
>>> Qlst
Deque ([2, 3, 4, 1])
Calendar: Calendars

Import Calendar
Cal = Calendar.month (2016,8)
Print cal

August 2016
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Calendar (year,w=2,l=1,c=6)

Return year calendar, 3-month line, distance of C. The daily width interval is w character. The length of each line is 21* w+18+2* c. L is the number of rows per week.

ISLEAP (year) returns True if it determines whether it is a leap year, or false.

Leapdays (y1, y2) returns the total number of leap years between y1,y2 two, including Y1, but excluding Y2.

Month (year, month, w=2, l=1) returns the annual month month calendar, two lines of headings, one row a week. The daily width interval is w character. The length of each line is 7* w+6,l is the number of rows per week.

MonthCalendar (year,month) Returns a list of elements or lists within the list. Each child list represents one weeks, from Monday to Sunday, or 0 if there is no date for this month.

Monthrange (year, month) returns a tuple with two integers inside. The first integer represents the first day of the month from the days of the week (starting from 0, in the order of Monday, Tuesday, until the 6 represents Sunday). The second integer is the total number of days of the month.

Weekday (Year,month,day) Enter the day of the month, knowing how many days of the week it is (note that the return value continues to correspond from 0 to 6 in turn from Monday to Saturday).

Time

Common methods

Time () Gets the current period (strictly a timestamp), but this time is unfriendly to people, it is based on January 1, 1970 0:0 0 seconds as the starting point, to the current length of time (regardless of leap seconds).

The result of localtime () can be called a time tuple (also with parentheses), and its meaning is:

Index attribute meaning
0 tm_year years
1 Tm_mon months
2 Tm_mday Day
3 Tm_hour,
4 Tm_min Points
5 tm_sec seconds
6 Tm_wday The first day of the week
7 Tm_yday The first day of the year
8 Tm_isdst Daylight Time
Gmtime () localtime () is the local time, if you want to internationalize, it is best to use GMT.

Asctime ()

>>> Time.asctime ()
' Mon May 4 21:46:13 2015 '

Time.asctime (h) #参数必须是时间元组, that is, the value returned by localtime
CTime ()

>>> Time.ctime ()
' Mon May 4 21:52:22 2015 '

>>> time.ctime (1000000) #参数是时间戳
' Mon 12 21:46:40 1970 '
Mktime () Mktime () is also an argument with a time tuple, but it returns a timestamp

Strftime () Converts a time tuple to a string according to the specified format requirements. If you do not specify a time tuple, the default is the LocalTime () value.

Format meaning value range (format)
%y The century 00-99, such as "15"
%Y full year like "2015"
%j specified date is the first day of the year 001-366
%m Return Month 01-12
%b the name of the local simplified month English month
%B Local Full month name complete English month
%d the day ordinal of the month returns "01" as of May 1
%H the date of the day (24-hour system) 00-23
%l the date of the day (12-hour system) 01-12
%m min 00-59
%s seconds 00-59
%u number of weeks in the year (with Sunday as the beginning of week) 00-53
%w Ibid., it's just a Monday-point 00-53.
%w Day of the week 0-6
%Z time zone Test in mainland China, return CST, Standard
%x date of day/month/year
%x Time: minutes: sec
%c Detail Date Time/month/year: minutes: sec
%% '% ' character '% ' character
%p on PM AM or PM
The Strptime () function is to convert a string into a time tuple with arguments specifying two, one as a time string, and the other in the format corresponding to the time string, which is used in the table above.

>>> today = Time.strftime ("%y/%m/%d")
>>> today
' 15/05/05 '
>>> Time.strptime (Today, "%y/%m/%d")
Time.struct_time (tm_year=2015, tm_mon=5, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, Tm_wday=1, tm_yday=125, Tm_isdst=-1 )
Datetime

There are several classes in the DateTime module:

Datetime.date: Date class, commonly used properties are Year/month/day
Datetime.time: Time class, commonly used to have Hour/minute/second/microsecond
Datetime.datetime: Date Time class
Datetime.timedelta: Time interval, that is, the length of time between two points of time
Datetime.tzinfo: Time Zone class
Date class


# Generate Date Objects
>>> Import datetime
>>> today = Datetime.date.today ()
>>> today
Datetime.date (2015, 5, 5)

# Manipulating Date Objects

>>> Print Today #Python 3:print
2015-05-05
>>> print today.ctime () #Python 3:print (Today.ctime ())
Tue May 5 00:00:00 2015
>>> print today.timetuple () #Python 3:print (Today.timetuple ())
Time.struct_time (tm_year=2015, tm_mon=5, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, Tm_wday=1, tm_yday=125, Tm_isdst=-1 )
>>> print today.toordinal () #Python 3:print (today.toordinal ())
735723

>>> Print Today.year
2015
>>> Print Today.month
5
>>> Print Today.day
5

# timestamp and format time format conversion

>>> to = today.toordinal ()
>>> to
735723
>>> print datetime.date.fromordinal (to)
2015-05-05

>>> Import Time
>>> t = time.time ()
>>> T
1430787994.80093
>>> print Datetime.date.fromtimestamp (t)
2015-05-05

# modification date.

>>> D1 = datetime.date (2015,5,1)
>>> Print D1
2015-05-01
>>> D2 = D1.replace (year=2005, day=5)
>>> Print D2
2005-05-05
Time class


# Generate Time Object

>>> t = datetime.time (1,2,3)
>>> Print T
01:02:03

# Common Properties:

>>> Print T.hour
1
>>> Print T.minute
2
>>> Print T.second
3
>>> T.microsecond
0
>>> Print T.tzinfo
None
Timedelta class

Mainly used to do the operation of time.

>>> now = Datetime.datetime.now ()
>>> Print now #Python 3:print (now)
2015-05-05 09:22:43.142520

# Add 5 hours to ' now ';

>>> B = Now + Datetime.timedelta (hours=5)
>>> Print B #Python 3:print (b)
2015-05-05 14:22:43.142520

# increase by two weeks;

>>> C = Now + Datetime.timedelta (weeks=2)
>>> Print C #Python 3:print (c)
2015-05-19 09:22:43.142520

# Calculate Time difference:

>>> d = c-b
>>> Print D #Python 3:print (d)
Days, 19:00:00
Urllib

The Urllib module is used to read data from the Web (on the server), for example, many people use Python as a crawler, which can be used in this module.

# in Python 2, this action:
>>> Import Urllib
>>> Itdiffer = Urllib.urlopen ("http://www.itdiffer.com")

# But if the reader is using Python 3, you must change your posture:

>>> Import Urllib.request
>>> Itdiffer = Urllib.request.urlopen ("http://www.itdiffer.com")

>>> print itdiffer.read () #得到网页的内容
Urlopen ()

Urlopen () is primarily used to open a URL file, then get the data for the specified URL, and then act as if it were an operation file, and the resulting object is called the class file object.

Parameter description:

URL: The path to the remote data, often the URL
Data: If the Post method is used, this is the submission
Proxies: Setting up agents
URL encoding, decoding

URLs have strict coding requirements for the characters, encoding and decoding the URLs.

QUOTE (string[, safe]): Encodes a string. Parameter safe specifies characters that do not need to be encoded
Urllib.unquote (String): Decoding a string
Quote_plus (string [, safe]): Similar to Urllib.quote, but this method replaces spaces with ' + ', and quote uses '%20 ' instead of spaces
Unquote_plus (String): decoding a string;
Urllib.urlencode (query[, Doseq]): Converts a dict or a list of tuples containing two elements to a URL parameter. For example {' name ': ' Laoqi ', ' Age ': 40} will be converted to ' name=laoqi&age=40 '
Pathname2url (PATH): Converts a local path to a URL path
Url2pathname (PATH): Converting a URL path to a cost path
Urlretrieve ()

Save the remote file in local storage.

Urllib.urlretrieve (url[, filename[, reporthook[, data]])

URL: The URL where the file resides
FileName: Optional. Saves the file to a local file name, and if not specified, Urllib generates a temporary file to save
Reporthook: Optional. Is the callback function that triggers this function when the linked server and the corresponding data transfer complete
Data: Optional. If the data sent by post means
function completed, the result returned is a tuple (filename, headers), filename is saved to the local file name, headers is the server response header information.

Urllib2

Just for Python 2, in Python 3, there's no urllib2 this module, it's urllib.request.

Request Class

>>>req = Urllib2. Request ("http://www.itdiffer.com")

# Python2
>>> response = Urllib2.urlopen (req)
>>> page = Response.read ()
>>> Print Page

Python 3:

>>> response = Urllib.request.urlopen (req)
>>> page = Response.read ()
>>> Print (page)
There are a lot of things urllib2 or urllib.request, for example:

Set HTTP Proxy
Set timeout value
Automatic redirect
Handling cookies
Xml

Python provides a variety of modules to handle XML.

xml.dom.* module: Document Object Model. Suitable for processing DOM APIs. It can parse XML data into a tree in memory, and then manipulate the XML by manipulating the tree. However, this approach is slower and consumes more memory because the XML data is mapped into an in-memory tree.
XML.SAX.* module: Simple APIs for XML. Because sax reads XML files in a streaming form, which is faster and less memory-intensive, the operation is slightly more complex, requiring the user to implement the callback function.
Xml.parser.expat: A direct, low-level, C-based expat parser. The expat interface is based on event feedback, a bit like SAX but not very much, because its interfaces are not fully regulated in the Expat library.
Xml.etree.ElementTree (hereinafter referred to as ET): element tree. It provides a lightweight Python API that's a lot faster than Dom,et.
, and there are a lot of pleasant APIs to use, compared to Sax,et also has et.iterparse provides "in the air" processing, there is no need to load the entire document to memory, saving memory. The average performance of ET is similar to that of sax, but the API is more efficient and easy to use.
There are two implementations of ElementTree in the standard library. One is the pure Python implementation: Xml.etree.ElementTree, the other one is faster: Xml.etree.cElementTree.

If you are using Python 2, you can introduce modules like this:

Try
Import Xml.etree.cElementTree as ET
Except Importerror:
Import Xml.etree.ElementTree as ET
If Python is more than 3, there is no need for this, just a word import xml.etree.ElementTree as ET can, and then by the module automatically to find the right way. It's clear that Python 3 has made a big progress relative to Python 2.

Summary of common properties and methods

ET has a lot of properties and methods, listed here are commonly used for future reference.

Element Object

Common Properties:

tag:string, element data type
Text:string, elements of the content
Attrib:dictionary, an attribute dictionary of elements
Tail:string, the tail shape of the element
Actions on Properties

Clear (): Clears the Descendants, attributes, text, and tail of the element to none
Get (Key, Default=none): Gets the property value corresponding to the key, and returns the default value if the property does not exist
Items (): Returns a list based on the property dictionary, with the list element (key, value)
Keys (): Returns a list containing all the element property keys
Set (key, value): Setting a new property key and value
Actions for descendants

Append (subelement): Adding a direct child element
Extend (subelements): Adds a string of element objects as child elements
Find (Match): Finds the first matching child element, which can be tagged or path
FindAll (Match): Find all matching child elements, matching objects can be tag or path
FindText (Match): Finds the first matching child element and returns its text value. Matching objects can be tag or path
Insert (index, Element): Inserts a child element at the specified position
ITER (Tag=none): an iterator that generates traversal of all descendants of the current element or a given tag's descendants
Iterfind (Match): Find all descendants based on tag or path
Itertext (): Iterate through all descendants and return the text value
Remove (subelement): Delete child elements
ElementTree objects

Find (Match)
FindAll (Match)
FindText (Match, Default=none)
Getroot (): Gets the root node.
ITER (Tag=none)
Iterfind (Match)
Parse (source, Parser=none): Loads an XML object, and source can be a file name or file type object.
Write (file, encoding= "Us-ascii", Xml_declaration=none, default_namespace=none,method= "xml")
Instance

<?xml version= "1.0" encoding= "UTF-8"?>


<bookstore liu= "a" >


<book category= "Cooking" >


<title lang= "en" >everyday italian</title>


<author>giada De laurentiis</author>


<year>2005</year>


<price>30.00</price>


</book>


<book category= "CHILDREN" >


<title lang= "en" >harry potter</title>


<author>j K. rowling</author>


<year>2005</year>


<price>29.99</price>


</book>


<book category= "WEB" >


<title lang= "en" >learning xml</title>


<author>erik T. ray</author>


<year>2003</year>


<price>39.95</price>


</book>


</bookstore>


#!/usr/bin/env python


# Coding=utf-8

Import Xml.etree.ElementTree as ET

FD = open ("Xml.xml")

data = Fd.read ()

Tree = ET. ElementTree (file= "Xml.xml")
Print Tree

#获得根元素
root = Tree.getroot ()
Print Root.tag
Print Root.attrib

#获得根元素下面的元素
For child in Root:
Print Child.tag,child.attrib
For Gen in:
Print Gen.tag,gen.text
Json

JSON is constructed in two structures:

A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).
The ordered list of values (an ordered list of values). In most languages, it is understood as an array.
The Python standard library has a JSON module that performs serialization and deserialization functions:

Serialization: Encoding, converts a Python object encoding into a JSON string
Deserialization: Decoding, converting JSON format string decoding to Python data objects
Encoding:dumps ()

Data_json = json.dumps (data)
Json.dumps (data, Sort_keys=true, indent=2) #格式化输出json数据
Decoding:loads ()

Large JSON string

If the data is not very large, the above operation is sufficient. But now is the so-called "big data" era, any business is saying that they are large data, obviously can not always make JSON very small, in fact, the real big data, and then "big" JSON is not. The previous approach is to read the data into memory, if the data is too large will overflow memory. What to do? JSON provides the load () and dump () function to solve the problem, noting that it is different than the function that has been used above, please observe carefully.

>>> Import Tempfile #临时文件模块
>>> data
[{' Lang ': (' python ', ' Chinese '], ' age ':, ' name ': ' Qiwsir '}]
>>> f = tempfile. Namedtemporaryfile (mode= ' w+ ')
>>> json.dump (data, F)
>>> F.flush ()
>>> Print open (F.name, "R"). Read () #Python 3:print (open f.name, "R"). Read ())
[{"Lang": ["Python", "Chinese"], "age":, "name": "Qiwsir"}]
Instance

{"Code": "Data": "Liuguoquan", "person": [{"Name": "Zhang", "Age": "Sex": "Male"},{"name": "Zhang", "Age": "Sex" : "Male"}]}
#!/usr/bin/env python
# Coding=utf-8

Import JSON

Class B (object):
def __init__ (self):
Self.age = 0
Self.name = ""
Self.sex = ""

Class A (object):
def __init__ (self):
Self.code = 2
Self.data = ""
Self.person = []

f = open ("Sample.json")
Value = F.read ();
Print value

RET = json.loads (value)
Print type (ret)

A = A ()
#对象转为字典
a.__dict__ = RET
Print A.code
Print A.data
Print A.person
Print type (A.person)

For item in A.person:
b = B ()
b.__dict__ = Item;
Print B.age
Print B.name
Print B.sex
Third-party libraries

Installing third party libraries

Use Source to install

The github.com website can download the source code of the third party library and usually see a setup.py file.

Python setup.py Install
PIP Management Tools

The PIP is a software package management system written in Python computer programming languages that can install and manage packages, and many other packages can be found in the Python Package index (English: Python Package Index, abbreviated PYPI).

Pip Install XXXXXX (XXXXXX is the name of the third party library) to install the Third-party library

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.