Usage analysis of common Python modules

Source: Internet
Author: User
This article mainly introduces usage analysis of common Python modules, including built-in functions, file operations, and regular expression matching, for more information about the modules commonly used in Python, see this article. The details are as follows:

1. built-in modules (can be used directly without import)

Common built-in functions:

Help (obj) online help. obj is of any type.
Callable (obj) to check whether an obj can be called like a function
Repr (obj) to get the obj representation string. you can use this string eval to recreate a copy of this object.
Eval_r (str) indicates a valid python expression. this expression is returned.
Dir (obj) view the name visible in the name space of obj
Hasattr (obj, name) to check whether the name space of an obj has a name
Getattr (obj, name) obtains a name in the name space of an obj.
Setattr (obj, name, value) is a name in the name space of an obj pointing to the object vale
Delattr (obj, name) deletes a name from the name space of obj.
Vars (obj) returns the name space of an object. Represented by dictionary
Locals () returns a local name space, represented by dictionary
Globals () returns a global name space, represented by dictionary.
Type (obj) to view the type of an obj
Isinstance (obj, cls) check if obj is a cls instance
Issubclass (subcls, supcls) check whether subcls is a subclass of supcls

Type conversion functions:

Chr (I) converts an ASCII value into a character
Ord (I) converts a character or unicode character into an ASCII value.
Oct (x) converts integer x to a string represented by octal.
Hex (x) converts the integer x into a hexadecimal string.
Str (obj) returns the description of the obj string.
List (seq) converts a sequence into a list
Tuple (seq) converts a sequence into a tuple
Dict () and dict (list) are converted into a dictionary
Int (x) to an integer
Convert long (x) to a long interger
Float (x) is converted into a floating point number.
Complex (x) to plural
Max (...) calculates the maximum value.
Min (...) calculates the minimum value.

Built-in functions used to execute the program:

Complie if a piece of code is frequently used, it will be faster to compile and then run.

2. OS-related calls

System-related information module import sys

Sys. argv is a list containing all command line parameters.
Sys. stdout sys. stdin sys. stderr indicates the standard input and output file objects with incorrect output.
Sys. stdin. readline () reads a row from the standard input sys. stdout. write ("a") and outputs
Sys. exit (exit_code) exit the program
Sys. modules is a dictionary that indicates all available modules in the system.
Sys. platform: get the operating system environment
Sys. path is a list that specifies the path of all modules and packages.

Operating system-related calls and operations import OS

OS. environ a dictionary ing relation containing environment variables OS. environ ["HOME"] can get the value of environment variable HOME
OS. chdir (dir) changes the current directory OS. chdir ('d: \ outlook). Note that escape is used in windows.
OS. getcwd () to get the current directory
OS. getegid () get the valid group id OS. getgid () get the group id
OS. getuid () get user ID OS. geteuid () get valid user ID
OS. setegid OS. setegid () OS. seteuid () OS. setuid ()
OS. getgruops () to get the user group name list
OS. getlogin () to get the user login name
OS. getenv: get the environment variable
OS. putenv: set environment variables
OS. umask setting umask
OS. system (cmd) uses the system call to run the cmd command

Example:

os.mkdir('/tmp/xx') os.system("echo 'hello' > /tmp/xx/a.txt") os.listdir('/tmp/xx')os.rename('/tmp/xx/a.txt','/tmp/xx/b.txt') os.remove('/tmp/xx/b.txt') os.rmdir('/tmp/xx')

Write a simple shell in python

#!/usr/bin/pythonimport os, syscmd = sys.stdin.readline()while cmd:  os.system(cmd)  cmd = sys.stdin.readline()

Use OS. path to write platform-independent programs

OS. path. abspath ("1.txt") = OS. path. join (OS. getcwd ()," 1.txt ")
OS. path. split (OS. getcwd () is used to separate the directory and file name in a directory name.
OS. path. join (OS. getcwd (), OS. pardir, 'A', 'a.doc ') are all named paths.
OS. pardir indicates the character of the upper-level directory under the current platform ..
OS. path. getctime ("/root/1.txt") returns the ctime (creation time) timestamp of 1.txt.
OS. path. exists (OS. getcwd ()
OS. path. expanduser ('~ /Dir') ~ Extended to user root directory
OS. path. expandvars ('$ path') extends the environment variable PATH.
OS. path. isfile (OS. getcwd () determines whether it is a file name, 1 is 0 No
OS. path. isdir ('C: \ Python26 \ temp ') to check whether it is a directory; 1 is 0 No
OS. path. islink ('/home/huaying/111. SQL') is a symbolic connection unavailable in windows
OS. path. ismout (OS. getcwd () is not available in windows.
OS. path. samefile (OS. getcwd (), '/home/huaying') to check whether the two file names refer to the same file.
OS. path. walk ('/home/huaying', test_fun, "a. c ")

Traverse all subdirectories under/home/huaying, including the local directory. for each directory, the test_fun function is called.

For example, in a directory and all its subdirectories, find the file or directory named a. c:

Def test_fun (filename, dirname, names): // filename is. c dirname is the name of the Accessed directory if filename in names: // names is a list that contains all the content in the dirname directory print OS. path. join (dirname, filename) OS. path. walk ('/home/huaying', test_fun, ". c ")

File operations

1. open the file

F = open ("filename", "r") r read-only w write rw read rb read binary wb write binary w + write append

2. read and write files

F. write ("a") f. write (str) write a string f. writeline () f. readlines () similar to the following read
F. read () all read out f. read (size) indicates reading size characters from the file
F. readline () reads a row and returns an empty string to the end of the file. f. readlines () reads all rows. returns a list. list. each element represents a row, including "\ n "\
F. tell () returns the current file read location
F. seek (off, where) locates the file read/write position. off indicates the offset, positive numbers move toward the end of the file, and negative numbers move toward the beginning. Where 0 indicates the start time, 1 indicates the current position, and 2 indicates the end time.
F. flush () refresh the cache

3. close the file
F. close ()

Regular expression import re

Simple regexp

P = re. compile ("abc") if p. match ("abc"): print "match"
In the above example, the first character is a pattern. if it matches a string, a match object is returned.
Except for some special metacharacter metacter, most of the characters match themselves.
These special characters are. ^ $ * +? {[] \ | ()

Character Set combination (expressed in)

Listing characters. for example, [abc] indicates matching a, B, or c. Most metacharacters only indicate matching with themselves in. Example:

A = ". ^ $ * +? {\\| () "Most metachar matches itself in [], but" ^ [] \ "is different from p = re. compile ("[" + a + "]") for I in a: if p. match (I): print "[% s] is match" % I else: print "[% s] is not match" % I

[] Contains [], indicating that "[" or "]" matches. \ [and \] indicates.
^ If It appears at the beginning of [], it indicates taking the reverse. [^ abc] indicates all characters except a, B, and c. ^ It does not appear at the beginning, that is, body matching.
-The range. [a-zA-Z] matches any English letter. [0-9] matches any number.
\ Wonderful use in.
\ D [0-9]
\ D [^ 0-9]
\ S [\ t \ n \ r \ f \ v]
\ S [^ \ t \ n \ r \ f \ v]
\ W [a-zA-Z0-9 _]
\ W [^ a-zA-Z0-9 _]
The \ t representation matches the tab, and the others are consistent with the string representation.
\ X20 indicates matching with hexadecimal ascii 0x20
With \, it can represent any character in. Note: If [] is not displayed for a single ".", it indicates that any character except the Newline \ n is matched, similar to [^ \ n].

Repeat of regexp

{M, n} indicates that more than m (including m) and less than n (including n) appear ). for example, if AB {} c matches abc, abbc, and abbbc, it does not match ac or abbbc.
M is the lower bound, and n is the upper bound. M: the lower bound of the omitted table is 0, n is omitted, and the upper bound of the table is infinitely large.
* {,}+ Indicates {1 ,}? {0, 1}
The maximum match and the minimum match are both the maximum match in python. if you want the minimum match, in *, + ,?, {M, n} is followed by ?.
Match the end of the object to obtain the position of the last matched character.
Re. compile ("a *"). match ('aaa'). end () 4 maximum match
Re. compile ("*? "). Match ('aaa'). end () 0 minimum match

Use the original string

In string representation, \ is used to represent characters \. a large amount of use affects readability.
Solution: Add an r in front of the string to indicate the raw format.
A = r "\ a" print a result is \
A = r "\" a "print a result is \"

Use re module

Use re. compile to get a RegexObject that represents a regexp.
Then, use the match and search methods of pattern to obtain the MatchObject.
Use match object to obtain the matching position, matched string, and other information.

Common RegxObject functions:

>>> Re. compile ("a"). match ("abab") if the beginning of AB matches re. compile ("a"), the MatchObject is obtained.
<_ Sre. SRE_Match object at 0x81d43c8>
>>> Print re. compile ("a"). match ("bbab ")
None Note: matching starts from the beginning of str
>>> Re. compile ("a"). search ("abab") for the first part matching re_obj in abab.
<_ Sre. SRE_Match object at 0x81d43c8>
>>> Print re. compile ("a"). search ("bbab ")
<_ Sre. SRE_Match object at 0x8184e18> Unlike match (),
Re_obj.findall (str) returns str to search for all matching parts of re_obj.
Returns a tuple, where the element is a matched string.

Common MatchObject functions

M. start () returns the start position, and m. end () returns the end position (excluding characters of this position ).
M. span () returns a tuple representation (m. start (), m. end ())
M. pos (), m. endpos (), m. re (), m. string ()
M. re (). search (m. string (), m. pos (), m. endpos () will get m itself
M. finditer () can return an iterator to traverse all the matchobjects found.
For m in re. compile ("[AB]"). finditer ("tatbxaxb "):
Print m. span ()

Advanced regexp

| Indicates joining multiple regexp. a B Two regexp, A | B indicates matching A or B.
^ Indicates that only the beginning of a row is matched. ^ indicates that only the beginning of a row has this special significance.
$ Indicates that only the end of a row is matched.
\ A indicates that only the beginning of the first line of the string is matched ^ match the beginning of each line
\ Z indicates that only the end of a line string matches the end of the first line.
\ B only matches the word boundary example: \ binfo \ B only matches "info" and does not match information
\ B indicates matching non-word boundary

Example:

>>> Print re. compile (r "\ binfo \ B "). match ("info") # Use raw format \ B to represent the word boundary <_ sre. SRE_Match object at 0x817aa98 >>> print re. compile ("\ binfo \ B "). match ("info") # raw \ B is not used to indicate the unsigned sign None >>> print re. compile ("\ binfo \ B "). match ("\ binfo \ B") <_ sre. SRE_Match object at 0x8174948>

Group example:

re.compile("(a(b)c)d").match("abcd").groups()  ('abc', 'b')    #!/usr/local/bin/python    import re    x = """name: CharlesAddress: BUPTname: AnnAddress: BUPT"""    #p = re.compile(r"^name:(.*)\n^Address:(.*)\n", re.M)p = re.compile(r"^name:(?P
 
  .*)\n^Address:(?P.*)\n", re.M)for m in p.finditer(x): print m.span() print "here is your friends list" print "%s, %s"%m.groups()
 

Compile Flag

When RegxObject is obtained using re. compile, some flags can be used to adjust the detailed features of RegxObject.
DOTALL and S allow. to match any character, including line break \ n
IGNORECASE, I ignore case sensitivity
LOCALES, L Make \ w \ W \ B \ B consistent with the current locale
MULTILINE, m multiline mode, only affect ^ and $ (see the previous example)
VERBOSE, X verbose mode

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.