Reflection
The reflection functionality in Python is provided by the following four built-in functions: Hasattr, GetAttr, SetAttr, delattr, and four functions for internal execution of objects: Check for a member, get a member, set a member, delete a member.
Class Foo (object): def init (self): self.name = ' Wupeiqi ' def func: return ' func ' obj = Foo () # # # # # # # # # # # # # # # # # # # # # # # # # # #setattr (obj, ' age ', setattr) (obj, ' show ', Lambda Num:num + 1) # # # # # # # # # # # # # # # # #delattr (obj, ' name ') delattr (obj, ' F UNC ')
os module
OS.GETCWD () # Gets the current working directory, which is the directory path of the current Python script work os.chdir ("DirName") # Changes the current script working directory, equivalent to the shell Cdos.curdir # Return to current directory: ('. ') Os.pardir # Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' Dir1/dir2 ') # can generate multi-level recursive directory Os.removedirs (' dirname1 ') # If the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on Os.mkdir (' DirName ') # Generate a single-level directory, equivalent to the shell mkdir dirnameos.rmdir (' dirname ') # Delete single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir Dirna Meos.listdir (' dirname ') # Lists all files and subdirectories under the specified directory, including hidden files, and prints a list of os.remove () # Delete a file Os.rename ("Oldname", "N EW ") # Rename File/directory Os.stat (' Path/filename ') # Get File/directory information os.sep # OS specific path delimiter, win under" \ \ ", Linux for"/"OS.L INESEP # The line terminator used by the current platform, win under "\t\n", Linux under "\ n" os.pathsep # The string used to split the file path Os.name The # string indicates the current use of the platform. Win-> ' NT '; Linux-> ' POSIX ' Os.system ("Bash Command") # Run shell command, direct display Os.environ # GET system environment variable Os.path.abspath (PATH) # returns the path normalized absolute path Os.path.split (PATH) # Divide path into directory and file name two tuples return Os.path.dirname (PATH) # Returns the directory of path. It is actually the first element of Os.path.split (path) # os.path.basename # returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) # os.path.exists (path) # If path exists, returns true if path does not exist, return Falseos.path.isabs (PATH) # if P Ath is an absolute path that returns Trueos.path.isfile (path) # If path is an existing file, returns True. Otherwise returns Falseos.path.isdir (PATH) # returns True if path is a directory that exists. Otherwise return Falseos.path.join (path1[, path2[, ...]) # When multiple paths are combined, the parameters before the first absolute path are ignored Os.path.getatime (path) # Returns the last access time Os.path.getmtime (path) # of the file or directory to which path is pointing Last modified time of file or directory sys module
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 program 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 name of the operating system platform Sys.stdin # Input related sys.stdout # output related sys.stderror # error related
Hashlib Encryption Module
For cryptographic related operations, instead of the MD5 module and the SHA module, mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm
Import Hashlib # ######## MD5 ######## hash = HASHLIB.MD5 () hash.update (' admin ') print hash.hexdigest () # ######## SHA1 # # # # # # # hash = HASHLIB.SHA1 () hash.update (' admin ') print hash.hexdigest () # ######## sha256 ######## hash = hashlib.sha256 () ha Sh.update (' admin ') print hash.hexdigest () # ######## sha384 ######## hash = hashlib.sha384 () hash.update (' admin ') Print hash.hexdigest () # ######## sha512 ######## hash = hashlib.sha512 () hash.update (' admin ') print hash.hexdigest ()
Although the above encryption algorithm is still very strong, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.
Import Hashlib # ######## MD5 ######## hash = hashlib.md5 (' 898oafs09f ') hash.update (' admin ') print hash.hexdigest ()
Not enough to hang? Python also has an HMAC module that internally creates keys and content for us to process and then encrypt
Import Hmach = hmac.new (' Wueiqi ') h.update (' Hellowo ') print h.hexdigest ()
No more!!!
Import hashlibobj = Hashlib.md5 () obj.update (bytes (' admin ', encoding= ' utf-8 ') result = Obj.hexdigest () print (Result) # Add key Key obj = hashlib.md5 (bytes (' xxxxxxxx ', encoding= ' Utf-8 ')) obj.update (bytes (' admin ', encoding= ' utf-8 ')) result = Obj.hexdigest () print (result)
Regular expressions
The RE module is used for the operation of a Python regular expression.
Character:
. Match any character other than line break
\w match letters or numbers or underscores or kanji
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string
Number:
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times
ip:^ (25[0-5]|2[0-4]\d|[ 0-1]?\d?\d) (\. ( 25[0-5]|2[0-4]\d| [0-1]?\d?\d]) {3}$ phone number: ^1[3|4|5|8][0-9]\d{8}$
1. Match (pattern, string, flags=0)
Matches the specified content from the starting position based on the model to the string, matching a single
Import reobj = Re.match (' \d+ ', ' 123uuasf ') if obj: print Obj.group ()
2. Search (Pattern, string, flags=0)
Matches the specified content in a string according to the model, matching a single
Import reobj = Re.search (' \d+ ', ' u123uu888asf ') if obj: print Obj.group ()
3. Group and Groups
A = "123abc456" Print Re.search ("([0-9]*) ([a-z]*] ([0-9]*)", a). Group () Print Re.search ("([0-9]*) ([a-z]*] ([0-9]*)", a). Group (0) Print Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (1) Print Re.search ("([0-9]*) ([a-z]*] ([0-9]*)", a). Group (2) Print Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Groups ()
4, FindAll (pattern, string, flags=0)
Both of these methods are used to match single values, that is, only one of the strings can be matched, and if you want to match all eligible elements in a string, you need to use FindAll.
Import reobj = Re.findall (' \d+ ', ' fa123uu888asf ') print obj
5, sub (pattern, REPL, String, count=0, flags=0)
Used to replace a matching string
Content = "123abc456" new_content = re.sub (' \d+ ', ' SB ', content) # new_content = re.sub (' \d+ ', ' SB ', content, 1) print New_co Ntent
More powerful than the Str.replace function
6, Split (pattern, String, maxsplit=0, flags=0)
Grouping according to a specified match
Content = "' 1-2 * ((60-30+1* (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2)) '" New_content = Re.split (' \* ', content ) # new_content = Re.split (' \* ', content, 1) print new_content
Content = "' 1-2 * ((60-30+1* (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2)) '" New_content = Re.split (' [\+\-\*\/]+ ', content) # new_content = Re.split (' \* ', content, 1) print new_content
INPP = ' 1-2* ((60-30 + ( -40-5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2)) ' INPP = re.sub (' \s* ', ' ', INPP ') new _content = Re.split (' \ ([[\+\-\*\/]?\d+[\+\-\*\/]?\d+) {1}\] ', INPP, 1) Print new_content
More powerful than Str.split