Develop Python with the old boy's education model [Article 5]: module, the old boy's python

Source: Internet
Author: User

Develop Python with the old boy's education model [Article 5]: module, the old boy's python

Recursive case: factorial

1*2*3*4*5*6*7...

Reflection

F1: function name

'F1': String

Commons:

Index:

Optimize with reflection and use strings to operate (Search) members in objects (modules.

Index:

Hasattr (): Search

Getattr (): Check

Delattr (): Delete

Setattr (): Set

Further Optimization: String import.

Index:

When you need to import the following folders:

The routing system of the web framework.

Special Variable _ file _ special Variable _ name _ sys module

Provides operations related to the python interpreter.

Sys. argv: list of command line parameters. The first element is the program path.

Sys. exit (n): exit the program. exit (0) when the program Exits normally)

Sys. version: get the version information of the python interpreter.

Sys. maxint: Maximum int Value

Sys. path: return the search path of the module. The value of the PYTHONPATH environment variable is used during initialization.

Sys. platform: returns the name of the operating system platform.

Sys. stdin: Output correlation

Sys. stdout: output-related

Sys. stderror: error related

Progress percentage:

OS Module

Provides system-level operations.

OS. getcwd (): Get the current working directory, that is, the directory path of the current python script.

OS. chdir ("dirname"): change the current script working directory, which is equivalent to cd under shell.

OS. curdir: returns the current directory ('.').

OS. pardir: gets the string name of the parent directory of the current directory ('..').

OS. makedirs ('dir1/dir2'): multiple-layer recursive directories can be generated.

OS. removedirs ('dirname1'): If the directory is empty, delete it and recursively go to the upper-level directory. If the directory is empty, delete it, and so on.

OS. mkdir ('dirname'): generate a single-level Directory, which is equivalent to mkdir and dirname in shell.

OS. rmdir ('dirname'): deletes a single-level directory. If the directory is not empty, it cannot be deleted. An error is returned, which is equivalent to rmdir dirname in shell.

OS. listdir ('dirname'): lists all files and subdirectories in a specified directory, including hidden files, and prints them in a list.

OS. remove (): delete an object.

OS. rename ("oldname", "newname"): rename a file/directory.

OS. stat ('path/filename '): gets the file/directory information.

OS. sep: Specifies the path delimiter of the operating system. In Windows, it is \, and in linux, It is "/".

OS. linesep: the row Terminator used by the current platform. The line terminator is "\ t \ n" in win, and the line terminator is "\ n" in linux ".

OS. pathsep: the string used to separate the file path ";".

OS. name: String indicates the platform currently in use. In win, it is "nt", and in linux it is "posix ".

OS. system ("bash command"): run the shell command and display it directly.

OS. environ: gets system environment variables.

OS. path. abspath (path): returns the absolute path of path normalization.

OS. path. split (path): splits the path into two groups of directories and file names and returns them.

OS. path. dirname (path): returns the path Directory, which is actually the first element of OS. path. split (path.

OS. path. basename (path): returns the final file name of the path. If the path ends with a slash (/) or slash (\), a null value is returned. That is, the second element of OS. path. split (path.

OS. path. exists (path): If the path exists, True is returned. If the path does not exist, False is returned.

OS. path. isabs (path): returns True if path is an absolute path.

OS. path. isfile (path): If path is an existing file, True is returned; otherwise, False is returned.

OS. path. isdir (path): If path is an existing Directory, True is returned; otherwise, False is returned.

OS. path. join (path1 [, path2 [,…]) : Returns the result after combining multiple paths. The parameters before the first absolute path are ignored.

OS. path. getatime (path): returns the last obtaining time of the file or directory pointed to by path.

OS. path. getmtime (path): returns the last modification time of the file or directory pointed to by path.

Hashlib

It is used for encryption-related operations, instead of the md5 module and sha module. It mainly provides SHA1, sha1_, SHA256, SHA512, and MD5 algorithms.

MD5 encryption:

Regular Expression

Character matching: Common and metacharacters.

Normal character match metacharacter match

.: Any character except linefeed

^: Start with something

$: End

*: Repeat * the first character 0 to n times

+: Repeated + 1 to n times of the previous character

? : Repeated? 0 or 1 times of the previous character

{}: Specifies the number of repetitions. {1, 5} repeats 1 to 5 times, {5} repeats 5 times, {, 5} repeats 0 to 5 times, {5 ,} repeat five to n times.

[]: Or, [a-z] matches any one of a to z, and [AB] matches a or B.

[] Special meaning

-: Indicates the range. For example, [0-9] indicates 0 to 9.

^: Indicates not and excluded. For example, [^ d] indicates to exclude d;

\: Special functions are removed from the backend of the backslash and metacharacters. Special functions are implemented from the backend of the backslash and common characters. Strings matching the word group corresponding to the serial number are referenced, such as re. search (r "(alex) (eric) com \ 2", "alexericcomeric ").

\ D: matches any decimal number, which is equivalent to [0-9].

\ D: matches any non-numeric characters, which is equivalent to [^ 0-9].

\ S: matches any blank characters, which is equivalent to [\ t \ n \ r \ f \ v].

\ S: matches any non-blank characters, which is equivalent to [^ \ t \ n \ r \ f \ v].

\ W: match any alphanumeric character, equivalent to [a-zA-Z0-9 _].

\ W: match any non-alphanumeric character, equivalent to [^ a-zA-Z0-9 _].

\ B: match the boundary of a string, that is, the position between a word and a space. A string is composed of consecutive letters, numbers, and underscores. Note that the definition of \ B is the junction between \ w and \ W.

The zero-width assertions is only used to match the header and tail of a string. The end is identified by a blank or non-alphanumeric character.

(): Specifies a matched group.

Function match

Re. match (pattern, string, flags = 0). match matches only the starting position.

Flags compiled flag, used to modify the matching mode of regular expressions, such as case-sensitive and multi-line matching.

Search

Once the match is successful, it is a match object.

Match object has the following methods:

Group () returns the string matched by RE.

Group () returns the string matching the overall re. Multiple group numbers can be entered at a time, corresponding to the string matching the group number.

1. group () returns the string matching the overall re.

2. group (n, m) returns the string matching group numbers n and m. If the group number does not exist, indexError is returned.

3. groups () returns a tuples that contain all the group strings in the regular expression, ranging from 1 to the group number contained. Normally, groups () does not require a parameter and returns a tuple, the element in the tuples is the group defined in the regular expression.

Start () returns the position where the matching starts.

End () returns the position at which the matching ends.

Span () returns the position where a tuples contain a match (START, end.

Findall

Re. findall returns all matched strings in the form of a list.

Sub

Match and replace.

Re. sub (pattern, repl, string, max = 0)

Set the maximum number of replicas

Subnsplitcompile

Re. compile (strPattern [, flag])

This method is a factory method of the Pattern class. It is used to compile a regular expression in the string form into a Pattern object. The second parameter flag is the matching mode. The value can use the bitwise OR operator '|', indicating that the parameter takes effect at the same time, such as re. I | re. m, you can compile regular expressions into regular expression objects, which can improve efficiency.

Re. I: Make the matching case insensitive.

Re. L: Perform locale-aware matching.

Re. M: multi-row matching, affecting ^ and $.

Re. S: Make "." match all characters including line breaks.

Re. U: parses characters according to the Unicode Character Set. This identifier affects \ w, \ W, \ B, \ B.

Regular grouping

Extract data from the matched data.

Match group match

Group match of findall

 

Group match of split

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.