Python_way Day6 reflex, regular module

Source: Internet
Author: User

Python_way Day6 Reflective Regular module First, the module:

1, sys & OS:

When we write the project, we often encounter the situation that the modules call each other, but under different modules we can find the location of these modules.

That's the environment variable!

How do I view the current environment variables?
A = Sys.path
Print (a)
[' d:\\ data \\python\\oldboy13\\jobs\\day5\\conf ', ' d:\\ information \\python\\python35\\lib\\site-packages\\ Requests-2.10.0-py3.5.egg ', 'd:\\ data \\python\\oldboy13\\day6 ', ' d:\\ data \\python\\oldboy13\\jobs\\day4 ', ' D : \ \ Data \\python\\oldboy13\\jobs\\day5 ', ' d:\\ information \\python\\oldboy13 ', ' d:\\ data \\python\\python35\\python35.zip ', ' d:\\ Information \\python\\python35\\DLLs ', ' d:\\ information \\python\\python35\\lib ', ' d:\\ data \\python\\python35 ', ' d:\\ data \\python\\ Python35\\lib\\site-packages ']
In Pycharm we can see all of this environment variables
But notice at this point:
' d:\\ data \\python\\oldboy13\\day6 ', the environment variable for this current project.
And then we'll use the command line to find this py file that reads the environment variables above and executes the following py file. Use Python3 path.py to look at the results of the output
should not see this environment variable, what's going on?
Actually, this is pycharm. The current project is automatically added to the Pycharm this built-in environment variable, in order to facilitate the use of pycharm people can quickly find the module under the current project.
So at this point if you do not configure the environment variables, but also use a different custom module, then the program to run the environment will certainly be an error.
What are we going to do with this?
Add the current project to the environment variable in the Python program
This is where we use the OS and SYS libraries.
First of all, a __file__ knowledge
A = __file__
b = Os.path.abspath (__file__)

d:/Data/python/oldboy13/jobs/day5/conf/test.py
D:\ Data \python\oldboy13\jobs\day5\conf\test.py

These two results seem to be identical except for the slash, which is the absolute path to the current file.

But we use the command line to do this and we get the result.

One is the file name, one is the absolute path!!!

This is pycharm, and we'll know if you want to go back to the absolute path of the file, use the second OS command.

If you want to get the file name, you can use __file__.

All right, the compass. Let's go on. OS, and SYS libraries

At this point we can get the absolute path of the current file. We just have to find the project where the file is located and add the project to the environment variable, so we can find all the locations under this project.
A = __file__ #查看当前文件的文件名, the absolute path of the current file will be given by default in Pycharm         
b = Os.path.abspath (__file__) #查看当前文件的绝对路径

c = os.path.dirname (b) #查看点钱上一级目录
Print (c)

D:\ Data \python\oldboy13\jobs\day5\conf\test.py
D:\ Data \python\oldboy13\jobs\day5\conf

See the Os.path.dirname is to see the current file is located in the directory, then we look up a layer of directory how to do?

Let's try one more layer.

D = Os.path.dirname (c)

Print (d)

D:\ Data \python\oldboy13\jobs\day5\conf\test.py
D:\ Data \python\oldboy13\jobs\day5\conf
D:\ Data \python\oldboy13\jobs\day5

This will be able to see the next level of the project directory!! haha just add this path into the environment variable is ready.

The result of Sys.path is that all environment variables are typed and displayed in a list.

So we can use this list to insert the above results into this list.

Sys.path.append (d), which puts the environment variable in.

These are the commands commonly used in Sys,os.

Hashlib

When we save the user password can not always use clear text to save, so I want to encrypt the user input password

Import hashlib #这个库就是用作加密的  = hashlib.md5 (b"a") #生成一个obj对象, this object is the MD5 method and uses the a string as the collision avoidance Library
Obj.update (b
"123"= obj.hexdigest () #取得加密后的数 Print (ret)

Second, Reflection

Scene:

A Web site that returns different pages by entering different URLs when a website has 1000 functions in the business process, we will use 1000 if, elif to receive user input and handle it too low! Is there anything that can save us money for 1000 if elif?Reflection:is to use the form of a string to go to the object (module) to find operations (find, set, delete, check) members Third, regular expression re module string match: Normal character: Import re
A = "Yuanledalexwuproqi"
f = Re.findall ("Alex", a)
Alex
This will find the matching text in the A string.

Metacharacters

...... ¥* +?. {} [] | () \ ^ $
Re.findall ()
Displays all matching list forms.
A = "aleUanledalexwuproqi"
f = Re.findall ("^ale", a)


A = "aleuanledalzexWuproqi"
f = Re.findall ("Al.ex", a)
A character that can match only one of the points in this position with any non-newline characters.

Repeat:
*
. * Indicates can match 0-multiple

   +* = 1-Multiple
?
. * Indicates that it can match 0-1 times

 {} 
Al.{3} indicates that it can match 3 times
al.{1,5} to match 1-5
Al.{1,} 1-Multiple
al.{,6} 0-Multiple matches only to 6 times

[]
A[bc]d match Any one of the characters in []
A[a-z]d matches any one in a-Z
-: Range
^: Represents a non-
\d: Represents a number

all metacharacters no longer have special meanings in []


\: Remove the special meaning of the following metacharacters with
\ followed by normal characters kerning Now Special features:
\d: Represents any 10 binary number
\d: matches any non-numeric character, equivalent to [^0-9]
\s: matches any whitespace character: it is equivalent to [\t\n\r\f\v]
\s: matches any non-whitespace character: equivalent to [^\t\n\r\ F\v]
\w: matches any alphanumeric character: equivalent to [a-za-z0-9_]
\w: Matches any non-alphanumeric character: equivalent to [^a-za-z0-9_]
\b: matches a word boundary, which means the position between the word and the space
matches a Location information.
For example a = "I am TIAN" I want to find out the individual word I
Re.findall (r "i\b", a)
\b is the special character information that matches the position after I, R is using the original characters
\b As long as the non-letters and numbers can match.

re.match (pattern,string,flags=0)
a= re.match ("com", "Comwww.runcomoob")
At this point, match gets the result of an object
<_sre. Sre_match object; Span= (0, 3), match= ' com ' >
Use Group () to get the result of this object
<class ' str ' > result is a string
There are several ways to manipulate the match
Group () returns a string that is matched by re
Start () returns the position where the match started
End () returns the position of the end of the match
Span () returns a tuple that contains (begins, ends) the position of the whole range
Print (A.group (), A.start (), A.end (), A.span ())
COM 0 3 (0, 3) The end position is Gu Tou regardless of the tail
Match is only from the beginning of the string, and will not match if the starting position does not match.
Re.search
a= re.search ("com", "Www.runcomoobcom")
Print (A.group (), A.start (), A.end (), A.span ())
COM 7 10 (7, 10)
Search starts from the head of the string, finds the location, returns, and returns only the first match to the


Re.finditer ()
The result of his and FindAll was one of the only he was getting an object
a= re.finditer ("com", "www.runcomoobcom")
For I in A:
A = I.start ()
b = I.end ()
c = I.span ()
D = I.group ()
Print (A,B,C,D)
7 (7, ten) com
(+) com






Re.sub () match substitution
A = Re.sub ("g.t", "have", "I get a,i got B,i gut C", 3)  3 is the maximum number of replacements
Rules match to what string to replace
Print (a)
I have a,i has b,i have C

A = RE.SUBN ("g.t", "have", "I get a,i got B,i gut C", 2)
Print (a)
(' I have a,i has b,i gut C ', 2)
SUBN and sub results are the same, but more realistic a replacement number

Re.split ()
A = "ONE1TWO2THREE3FOUR4"
c = Re.split (r "\d+", a)
Print (c)
[' One ', ' one ', ' three ', ' four ', ']
To split the match to


Re.compile ()
A = "Jgood is a handsome boy, he's cool, clever, and so on ...."
ret = Re.compile (r "\w*oo\w*")
Print (Ret.findall (a))
[' oo ', ' oo ']
Compile compiling rules into an object


Flag bit:
Re. I make the match case insensitive
Re. L do localization identification (locale-aware) matching
Re. M multiple lines match, affect ^ and $
Re. S make. Matches all characters including line breaks
Re. U parses characters based on Unicode character set, this flag affects \w,\w,\b,\b
Re. X This flag is easier to understand by giving you a more flexible format so that you can write Wang expressions
A = "ASD\NADF"
b = Re.findall ("D.a", a)
Print (b)
[]
At this point, the match is not d\na, because. Cannot match \ n line break.
If you want to match, add the above representation as re. s up
A = "ASD\NADF"
b = Re.findall ("D.a", A,re.) S)
Print (b)
[' d\na ']


Translation of the original string:
f = open ("D:\abc.txt")
This will be an error when opened in Python.
If we want to open it, we'll write a translation symbol.
f = open (R "D:\abc.txt")
So we turn this special meaning off.
Because the \a itself has its own meaning in the Python interpreter, the interpreter interprets the \a as a bell symbol in Python when it explains this step.
This is the form in which we are going to use the acoustic string, which is the front +r or the \a de-meaning, \\a

It says what we want is a string, so if we call the RE module in Python, how do we handle
A = "ADAF\COMNF"
What if we want to match \com?
Aside from the interpreter, the simple re
b = Re.findall ("\\com", a)
This is possible, because \ itself is the translation function if no longer add a \, we can only think of C translation, not to find \c this character
But we just found COM because there was a layer of Python interpreter in the middle.
In the Python interpreter \ also has a special meaning, so we want to pass a \ Enter into re also need a \+\ so the result is
b = Re.findall ("\\\com", a)
Print (b)
[' \\com ']
Hey? Why is it 2??
Because you see 2 of them in Python.
Group
Match the results you've already matched to the corresponding values.
A = "has ADAF\COMNF"
b = Re.match ("h\w+", a) #将匹配到的输出
b = Re.match ("H (\w+)", a)  #将匹配到的输出, but we just need to group the
b = Re.match ("H" (? p<name>\w+) ", a) #将匹配到的变为velvet, front? P<nane> is a key.

Print (B.group ())
Print (B.groups ())
Print (B.groupdict ())
Has
(' as ',)
{' name ': ' as '}

Re.findall ("")

A = "Hasabba asdfaf halabba 91293JF"
b = Re.findall ("H (\w+) (AB) Ba", a)
Print (b)
[(' as ', ' AB '), (' Al ', ' AB ')]
FindAll will find all of the matches, and the group is selected from the results you find.


Re.sub inside the group is not effective

Re.split ()

A = "Hello ABC asdfaf ABC; KJLK;JADF ASDFABCAF"
b = Re.split ("(ABC)", a)
Print (b)
' abc ' ' abc ' ' abc', ' AF ']
Ben should have split to ABC for the division, not to take ABC, because of the reasons for the group, the ABC was taken back.
What if the brackets are matched in parentheses?
A = "Hello ABC asdfaf ABC; KJLK;JADF ASDFABCAF"
b = Re.split ("(A (b) c)", a)
Print (b)

[' Hello ', ' abc', 'b', ' ASDFAF ', ' abc ', ' B ', '; KJLK;JADF asdf ', ' abc ', ' B ', ' AF ']

Which groupings are matched one by one in the outward. obtained separately.






Python_way Day6 reflex, regular module

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.