Python Common modular usage Analysis _python

Source: Internet
Author: User
Tags flush readline stdin in python

This article describes in more detail Python commonly used modules, to share for everyone to consult for reference. Specifically as follows:

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

Common built-in functions:

Help (obj) online assistance, obj is any type
Callable (obj) to see if an obj can be called like a function
Repr (obj) Gets the representation string for obj, which allows you to reconstruct a copy of the object using this string eval
Eval_r (str) represents a valid Python expression that returns the expression
Dir (obj) to view the name visible in the name space of obj
Hasattr (obj,name) to see if there is a name in the name space of obj
GetAttr (obj,name) gets a name in the name space of obj
SetAttr (Obj,name,value) is a name in obj's name space that points to the Vale object
Delattr (obj,name) deletes a name from OBJ's name space
VARs (obj) returns the name space of an object. to express by dictionary
Locals () returns a local name space, expressed in dictionary
Globals () returns a global name space, represented by dictionary
Type (obj) to see the types of an obj
Isinstance (OBJ,CLS) to see if obj is a CLS instance
Issubclass (SUBCLS,SUPCLS) to see if Subcls is Supcls subclass

Type Conversion Functions:

Chr (i) turn an ASCII value into a character
Ord (i) turns a character or Unicode character into an ASCII value
Oct (x) turns an integer x into a octal-represented string
Hex (x) A string that turns integer x to hexadecimal
STR (obj) gets the string description of obj
List (seq) converts a sequence into a list
Tuple (seq) converts a sequence into a tuple
Dict (), Dict (list) into a dictionary
Int (x) converted to an integer
Long (x) converted to a long interger
Float (x) converted to a floating-point number
Convert complex (x) to plural
Max (...) to find the maximum value
Min (...) to find the minimum value

Built-in functions for executing programs:

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

2. Calls associated with the operating system

System-related Information module, import sys

SYS.ARGV is a list that contains all the command-line arguments.
The Sys.stdout Sys.stdin Sys.stderr represents the standard input output, the file object of the error output, respectively.
Sys.stdin.readline () read one line from standard input sys.stdout.write ("a") screen output a
Sys.exit (Exit_code) Exit program
Sys.modules is a dictionary that represents all the available module in the system
Sys.platform the operating system environment to be run
Sys.path is a list that indicates the path to all lookup module,package.

Operating system-related calls and operations import OS

Os.environ A dictionary mapping relationship that contains environment variables os.environ["Home" can get the value of the environment variable home
Os.chdir (dir) changes the current directory Os.chdir (' D:\\outlook ') Note that Windows uses escape
OS.GETCWD () Get current directory
Os.getegid () get a valid group ID os.getgid () get the group ID
Os.getuid () Get user ID Os.geteuid () get a valid user ID
Os.setegid Os.setegid () os.seteuid () Os.setuid ()
Os.getgruops () Get a list of user group names
Os.getlogin () get user login name
OS.GETENV Get Environment variables
OS.PUTENV Setting Environment variables
Os.umask Settings Umask
Os.system (CMD) use system call, run cmd command

Examples of operations:

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

import os, sys
cmd = sys.stdin.readline () while
  os.system (cmd)
  cmd = Sys.stdin.readline ()

Using 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 part of the directory and the file name in a directory name.
Os.path.join (OS.GETCWD (), Os.pardir, ' a ', ' A.doc ') are all path names.
Os.pardir represents the characters of the upper-level directory below the current platform.
Os.path.getctime ("/root/1.txt") returns a 1.txt CTime (creation time) timestamp
Os.path.exists (OS.GETCWD ()) to determine whether a file exists
Os.path.expanduser (' ~/dir ') expands to the user root directory
Os.path.expandvars (' $PATH ') Extended environment variable path
Os.path.isfile (OS.GETCWD ()) determines whether the file name, 1 is 0 no
Os.path.isdir (' c:\Python26\temp ') determines whether the directory, 1 is 0 no
Os.path.islink ('/home/huaying/111.sql ') is not available in symbolic connection windows
Os.path.ismout (OS.GETCWD ()) is not available at the file system installation point Windows
Os.path.samefile (OS.GETCWD (), '/home/huaying ') look at two file names is not referring to the same file
Os.path.walk ('/home/huaying ', Test_fun, "A.C")

All subdirectories under traversal/home/huaying include this directory, and function test_fun is called for each directory.

For example: In a directory, and in all of his subdirectories, look for files or directories whose names are A.C:

def test_fun (filename, dirname, names)://filename is walk a.c is accessed by directory name
  if filename in dirname: Names is a list, Contains all content in the DirName directory
 print os.path.join (dirname, filename)
os.path.walk ('/home/huaying ', Test_fun, "A.C")

File actions

1. Open the file

f = open ("FileName", "R") r Read Only W write RW read-write RB reading binary WB write binary w+ write append

2. Read and Write files

F.write ("a") f.write (str) writes a string F.writeline () F.readlines () is similar to the following read () read out (size) to read the size character from a file
F.readline () reads a line, ends with the file, and returns an empty string. F.readlines () reads all, returns a list. List each element represents a row, containing the "\ n" \
F.tell () returns the current file read location (off, where) locates the file read/write location. Off represents an offset, a positive number moves to the end of the file, and a negative representation moves toward the beginning. Where 0 means that from the beginning, 1 represents the current position, and 2 is the ending.
F.flush () flush cache

3. Close the file
F.close ()

Regular expression Regular expression import re

Simple RegExp.

p = re.compile ("abc") If P.match ("abc"): Print "Match"
In the example above, a pattern is generated first, and if it matches a string, it returns a Match object
Most character characters match itself, except for certain special characters metacharacter metacharacters.
These special characters are. ^ $ * + ? { [ ] \ | ( )

Character set combination (in [])

Lists characters, such as [ABC], that match A or B or C, and most metacharacter only represent and match themselves in []. Cases:

A = ". ^$*+? {\\| () "Most Metachar match itself in [], but" ^[]\ "differs
p = re.compile (" ["+a+"] ") for I in
  if P.match (i):
 print ' [%s] is ' Match '%i
 print ' [%s] is ' not match '%i

Include [] itself in [], which means "[" or "]" matches. expressed in \[and \].
^ appears at the beginning of [], which means reverse. [^ABC] represents all characters except A,b,c. ^ does not appear in the beginning, that is, match the body.
-can represent a range. [A-za-z] matches any one English letter. [0-9] matches any number.
In [] in the magical.
\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_]
\ t means and tab matches, and everything else is consistent with the notation of the string
\x20 representation and hexadecimal ASCII 0x20 matching
With \, you can represent any character in []. Note: A separate "." If it does not appear in [], a match is made to any character other than the newline \ n, similar to [^\n].

The repetition of the regexp

{M,n} indicates that more than M (including m), n below (including n). Matches, such as Ab{1,3}c and ABC,ABBC,ABBBC, do not match AC,ABBBC.
M is the lower bound and n is the upper bound. The lower bound of M province is 0,n ellipsis, and the upper bound of the table is infinitely large.
* represents {,} + represents {1,}? represents {0,1}
Maximum match and minimum match Python is the maximum match, if the minimum match, in the *,+,?, {m,n} plus one?
The end of match object gets the position of the last character to match.
Re.compile ("A *"). Match (' AAAA '). End () 4 maximum match
Re.compile ("A *?"). Match (' AAAA '). End () 0 minimum match

Use the original string

The string representation method uses \ n to represent the character \. Extensive use affects readability.
Workaround: Precede the string with an R to represent the raw format.
A = r "\a" print a turns out to be \a
A = R "\" a "print a result is \" a

Using the RE module

First, you get a regexobject with a re.compile to represent a regexp.
After using the Match,search method of pattern, get matchobject
Then use match object to get the matching position, the matching string and so on information

Regxobject Common functions:

>>> Re.compile ("a"). Match ("Abab") if the opening of Abab matches Re.compile ("a"), get Matchobject
<_sre. Sre_match Object at 0x81d43c8>
>>> Print Re.compile ("a"). Match ("Bbab")
None Note: Start the match from the beginning of the Str
>>> Re.compile ("a"). Search ("Abab") searches the Abab for the first and re_obj matching parts
<_sre. Sre_match Object at 0x81d43c8>
>>> Print Re.compile ("a"). Search ("Bbab")
<_sre. Sre_match object at 0x8184e18> and match () do not have to match from the beginning
Re_obj.findall (str) returns the search for all and re_obj matching parts of str.
Returns a tuple in which the element is a matching string.

Common functions of Matchobject

M.start () returns the starting position, M.end () returns the ending position (the character that does not contain the position).
M.span () returns a tuple representation (M.start (), M.end ())
M.pos (), M.endpos (), (), m.string () (). Search (M.string (), M.pos (), M.endpos ()) will get m itself
M.finditer () can return a iterator to traverse all found matchobject.
For M in Re.compile ("[AB]"). Finditer ("TATBXAXB"):
Print M.span ()

Advanced RegExp

| Represents the union of multiple RegExp. A B two regexp,a| b indicates a match with a or a B match.
^ means to match only the beginning of a line, ^ only at the beginning of this special meaning.
$ indicates matching only the end of a line
\a represents matching the beginning of only the first line of the string ^ matches the beginning of each line
\z indicates matching only the end of a line of string $ matches the end of the first row
\b Only matching words: \binfo\b will only match "info" does not match information
\b Indicates matching non-word boundaries

Examples are as follows:

 >>> Print Re.compile (r "\binfo\b"). Match ("info") #使用raw格式 \b Represents the word boundary
 <_sre. Sre_match object at 0x817aa98>
 >>> print re.compile ("\binfo\b"). Match ("info") #没有使用raw \b Represents the backspace symbol
 >>> Print re.compile ("\binfo\b"). Match ("\binfo\b")
 <_sre. Sre_match Object at 0x8174948>

Grouping (Group) Example:

Re.compile ("(A (b) c) d"). Match ("ABCD"). Groups ()  (' abc ', ' B ')    
Import re    
x = "" "

" "    
#p = Re.compile (r" ^name: ( . *) \n^address: (. *) \ n ", re. M)
p = re.compile (r "^name: (?) p<name>.*) \n^address: (? p<address>.*) \ 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 you get Regxobject with Re.compile, you can have some detailed features that flag use to adjust the regxobject.
  Dotall, S let. Match any character, including the newline character \ n
  IGNORECASE, I ignore the case
  LOCALES, L let \w \w \b and the current \b Agree
& nbsp MULTILINE, M Multi-line mode, only affects ^ and $ (see previous example)

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: 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.