Python Basics Finishing Notes (v)

Source: Internet
Author: User

I. Some points of attention on the Hashlib module

The Hashlib module is used to encrypt related operations, instead of the MD5 module and the SHA module, mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm;

MD5 generates a 32-bit 16 character;

The result of SHA1 is a bit byte, usually represented by a 40-bit 16 binary string, and the more secure algorithm than SHA1 is SHA256 and SHA512, but the more secure the algorithm, the slower the cut length.

Take MD5 as an example, the general calculation method is as follows:

m ='test1'm.update (bytes (A, encoding='utf-8'  )#  hexdigest is the output of 16 binary results print(m.hexdigest ())# The result of Hexdigest is this print(M.digest ()) after being decode with Hex.

But sometimes we need to ask for a MD5 value for a large file, and then we can't read the whole file content into memory, which needs to be treated like this:

1m =hashlib.md5 ()2Read_len = 2 * * 163 With Open (File_path) as F:4      whileTrue:5BUF =F.read (Read_len)6         ifBUF:7 m.update (BUF)8         Else:9              BreakTen Print(M.hexdigest ())

Segment a certain length to read the entire file, and then continue to update until the end of all.

It is also important to note that the results of the various digest algorithms described above are not unique, meaning that different strings may calculate the same result, which is called collisions. So you can't use it as a unique key to function.

Two. exec and eval points of attention

EXEC and Eval feel very similar to us, and all are dynamically executing a string, but they are clearly different.

EXEC is a syntax, not a function (like if, for, etc.), it is the function of the received string as the PY code to run dynamically, so there is no return value.

Examples are as follows:

1 exec ("forI in range (5):p rint i") 2 0 3 14 25 36 4

Eval is a method that dynamically computes the value of an expression as a string, returns the result of an expression, and therefore the statement that is not an expression cannot run.

Examples are as follows:

1 #Eval cannot evaluate non-expression2>>> Eval ("For I in range (5):p rint i")3 Traceback (most recent):4File"<stdin>", Line 1,inch<module>5File"<string>", Line 16      forIinchRange (5):PrintI7^8 syntaxerror:invalid Syntax9 Ten  One #The result of the expression is output directly A>>> Eval ('5 + a', {'a': 1}) -6

Three. Subprogress Module

The Subprogress module is used to execute system commands, similar to Os.system, os.spawn*, but Subprogress is now more recommended, the official documentation says:

It can be seen that the above two old modules may be completely discarded in the future.

I wrote one with Subprogress. The PIPE (this method is suitable for executing complex commands) to perform a Git clone operation as an example of a small method as follows:

1 defcheckout_codes (Git_url, Tag_version, user_id, passwd, Dst_dir):2PIPE =subprocess. PIPE3Git_cmd ="Https://%s:%[email protected]%s"%(user_id, passwd, Git_url)4Process = subprocess. Popen (['git','Clone', Git_cmd, Dst_dir,'- b', Tag_version],5Stdout=pipe, stderr=PIPE)6Stdoutput, Stderroutput =process.communicate ()7     ifprocess.poll ():8         PrintStdoutput9         PrintStderroutputTen         returnFalse One     returnTrue

Three. Configparser Module

Onfigparser is used to process files in a particular format, essentially using open to manipulate files.

In Python2 Configparser library called Configparse, PIP is required to install

The library implements various methods of manipulating INI style files. An example of the INI style type is as follows:

  [111     2  password = 222   admin_flag = 0    5  [222   "  6  password = 333   7  Admin_flag = 0   8   9  [444  ]  10  password = 555  11  admin_flag = 1  

Each [] represents a section, and the following equals pair represents an attribute and a value for it. Examples of the various methods of using Configparser are as follows:

1CF =Configparser. Configparser ()2 #Read File3Cf.read ('a.conf')4 5 #The sections method returns a list of all the section names6  forSecinchcf.sections ():7     Print(sec)8 9 Ten #Add a new section OneCf.addsection ('555') A #Add properties to the new section ' 555 ' -Set'555','Password','3232') -  the  - #The properties of the original section can also be changed, and the added method is consistent -Set'444','Password','3232') -  + #the value of a property that can be taken to a section, such as the Get and Getint methods -Cf.get ('444','Password') +Cf.getint ('444','Password') A  at #All properties of a section can be obtained through items -Cf.items ('444') -  -  - #after the change is finished, modify the file with write -Cf.write (Open ('a.conf',"W")

Python Basics Finishing Notes (v)

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.