Python's Path to Growth third (4) _ Scope, recursion, module, built-in module (os,configparser,hashlib), with file operation

Source: Internet
Author: User
Tags hmac sha1 python script





Make an ad. Welcome to join Linux,python Resource Share Group number: 478616847










Directory:

1. Scope


2. Recursion


3. Module Introduction


4. Built-in module-os


5. Built-in module-configparser


6. Built-in module-hashlib


7. With file operation


Code execution Environment defaults to 3.5.1


First, scope

(1) What is the scope, the official scope is that there is a different namespace, just like the following code, a=1 its scope is global, also in the global namespace, when the function of the action a=2 it is in the local scope, but also in the local namespace.


a = 1

def action ():

   a = 2

action ()

print (a)

Let's see, the above code, when we run it, the result we get is 1, which is the result of the scope, obviously the same variable but a = 2 is in the local scope, so when we print a, it is still 1.

(2) So what if we want to use the variables and contents of the global scope in the local scope, the global statement in Python is used to declare the global, and the global variable can be reassigned within the function.

a = 1

def action ():

   global a

   a = 2

action ()

print (a)

Second, recursion

Before we have learned about functions, we also introduced several built-in functions. Then we think that when we use functions, we always refer to the function name of this function, which is like action () in the above example, then we Say, can the function itself call the function itself? The answer is yes, then we call this method of recursion! The significance of recursion is to prompt the efficiency of code execution, here we use two classic examples


(1) Factorial


What is factorial, we can look at it this way, there is a number n, then the factorial definition of n is nx (n-1) x (n-2) x (n-3) ... x1


First of all, we use ordinary methods to achieve:


n = 4

def factorial ():

   global n #Set global variables

   for i in range (1, n):

       n * = i

factorial () #Execution function

print (n)

This is easy to understand, because it is a multiplication operation, whichever is the same is multiplied first, so we use range to generate 1-n numbers, then i is equal to 1, 2, 3 because of the characteristics of range, the final number will be smaller than n, Then use n * = i, that is n = n * i, and finally calculate the result

Let's look at the recursive version:

def factorial (n):

   if n == 1:

       return n

   else:

       return n * factorial (n-1)

c = factorial (5)

print (c)

First of all, regardless of the code amount and advantages of it and ordinary methods, let's first understand this recursion. Conditional students use pycharm breakpoints to look at the execution process, so let's explain this process:


Recursive return value:


Let's see that first we pass in the number n = 5, and then enter the function to execute the function to call its own operation, then according to my current understanding, when the fourth call to n equals 1, then according to the principle of execution Return n, then at this time c should be equal to 1, why c is not equal to 1 but 120?


Let's analyze the recursive process, please see the picture



Let's explain

① Call n = 4 when n = 5 Call n = 3 when n = 4 An analogy

② Then the expression of the return value of our call function is return n * factorial (n-1) so that we can know that each return is the result of the next execution, n * factorial (n-1) is equal to

 5 * (5-1) * (4-1) * (3-1) * (2-1) * 1

③Because of the characteristics of python, each time it returns the calculated value and it becomes

  fact (5)-> 5 * factorial (4)-> 5 * (4 * (3 * (2 * factorial (1)))) #This is the final result expression, 1 is returned from n = 1

            -> 5 * (4 * (3 * (2))) #Calculate the value of each layer and return to the upper layer

            -> 5 * (4 * (6))

            -> 5 * 24 #top layer

            -> 120 #final c = 120


(2) Binary search


In the above example, we can't see how recursion improves the execution efficiency of the code. So let's look at this example, what is binary search, we assume such a scenario, we need to judge whether a number is in a group of numbers, assuming that the group of numbers 100, then we need to compare one by one, you can use for loop, or if in, if in is also used to find the mechanism of the for loop, then there are 10,000 to join this group? And the number you are looking for is at the end, so do n’t you have to loop 10,000 times? This method is too low, so there is a binary search.


Recursive writing:


def seek (data, find_in): #define a function, receive array, and the value to be found

   mid = int (len (data) / 2) #divide this array by 2

   if len (data)> 1: #Determine whether the remaining array is greater than 1, if it is greater than 1, then perform a dichotomy algorithm otherwise it will not find the exit

       if data [mid]> find_in: #determine whether the number divided by 2 is greater or less than the number we are looking for

           seek (data [: mid], find_in) #If it is greater than, determine that the number is to the right of the middle position of the array, and pass the value to this function, and judge again

       elif data [mid] <find_in:

           seek (data [mid:], find_in) #If it is less than, determine that the number is to the left of the middle position of the array

       else: #When this array is not greater than or less than the number we are looking for, it means that this array is equal to the number we are looking for

           print ("ok") # So that means the number we are looking for is in this array

   else:

       print ("no")

datas = range (1,10000000)

seek (datas, 1000000)

Then this way can be used to determine whether the number we are looking for is in the array in a small number of times


bisect module

The bisect module in Python's standard library is also very effective for binary search. This module is to determine where the number is in the array. If the number is less than or equal to the smallest number in the array, it returns 0. The number is greater than or equal to the largest number in the array. It returns the position of the largest number in the list, otherwise it returns the correct position of the number in the list.


Analysis of bisect.bisect source code:

bisect.bisect

def bisect_right (a, x, lo = 0, hi = None): #a is the incoming list, x is the number you are looking for, lo and hi do not need to be passed in



   if lo <0: #First determine the length of the lo list, the default is 0, here is an error message, the incoming lo cannot be smaller than 0

       raise ValueError (‘lo must be non-negative’)

   if hi is None: #determine whether hi is empty

       hi = len (a) #If it is empty, assign the length of list a to hi

   while lo <hi: #determine whether lo is less than hi

       mid = (lo + hi) // 2 #If it is not less than, execute mid = (lo + hi) // 2 is to take out the identifier of the middle number in the list

       if x <a [mid]: hi = mid #Determine whether the number you are looking for is less than the middle number of the incoming list, if less than then hi = mid is the identifier of the middle number and then loop again

       else: lo = mid + 1 #As long as the number you are looking for is not less than the middle number of the incoming list, execute this sentence lo = mid + 1,

   return lo


bisect = bisect_right #This sentence indicates backward compatibility, meaning that using the function bisect.bisect is equal to bisect.bisect_right

#Recommend that you pass variables to pycharm breakpoints to see the execution process



Three, module introduction

First of all, what is a module, the definition of a module? Here is a collection of codes that implement a function


(1) The module is divided into three types:

Custom module

Built-in module

Open source module

The custom module is to write the function ourselves, and then other code to call our function


Example: Separate different functions like the following picture


Built-in modules are some of the functions that come with Python




Open source module, which is a method function developed by others, we install it into our python to use


(installation method)


Install under linux:


When using source code installation, you need to use gcc compilation and python development environment


yum install gcc


yum install python-devel


Download source code

Unzip the source code

Enter the directory

Compile the source code python setup.py build

Install source code python setup.py install



(2) Import module

import

from xxx import xxx

from xxx import xxx as xxx

from xxx import *

import, and from xxx import xxx


Used to import the corresponding module, the module is actually a collection file of some functions and classes, it can achieve some corresponding functions, so what is the difference between them


Let's look at an example:


Here is an example of the os module to be described next. The rename method under the os module is used to rename files or directories. After we clearly see the module imported from from, we can directly use the rename method, and import is We need os.rename to use this rename method (that is, we need to specify the module name)


from os import rename


rename (‘method.py’, ‘test.py’)



import os


os.rename (‘method.py’, ‘test.py’)

Import is recommended to import here because it can make your program more readable and avoid name conflicts.

from xxx import xxx as xxx

We can find that this is to give the method or module an individual name

from os import rename as newname


newname (‘method.py’, ‘test.py’)


import os as os_name


os_name.rename (‘method.py‘, ‘test.py’)

There is no need to explain the last one, I believe everyone understands.

(3) sys.path

sys.path what the hell is this? We found that without the module that comes with the system, we only need to import it to import it directly. However, the module we write can only be imported under the current path. This is because the sys.path stores the path when the system finds the module. , So you can get the module that comes with the system, just like the linux search command, then can we add other paths to it, let us import custom modules in other directories, here is definitely possible , We use sys.path.append ('path') to add


There is one in the python module


The concept of package means that there is a __init__.py file in the directory, then we call this directory as a package




example:


First of all, our common directory structure is a folder followed by several packages like below, each package contains its own function code to be implemented


We use the following two methods to import the main file in test1 into any package in the test directory:


  os.path.abspath ()


  This function is to get the directory under the current file


  sys.path.append (‘path’)


  This method is to add the system default search path


Then we can write this in the main.py file


import os

import sys

PATH = os.path.dirname (os.path.dirname (__ file__))

#Because our directory structure is a second-level directory, we need to get the directory of the current file directory, so that we can get our parent directory test

sys.path.append (PATH)

#Add the obtained path to the system default search path, and then we can use the method in the package under the test directory


if __name__ == "__main__": #This sentence indicates that this file is an entry

   from test2 import hellow #Import this file in this package

   hellow.hello () #Execute the hello function in this file


# hellow.py Content:

def hello ():

   print ("How are you doing tomorrow !!")


Fourth, the built-in module-OS

This module provides Provides a portable way to use the operating system to handle directory and file related functions.


Official description


(1) os.getcwd ()


   Get the current working directory, that is, the directory path where the current python script works


    import os

    print (os.getcwd ())

    #Result is E: \ pycharm \ exercise


(2) os.chdir ("dirname")


   Change the current script working directory; equivalent to cd under shell


 #Please execute under linux

 import os

 path = "/ tmp"

 # View the current working directory

 retval = os.getcwd ()

 print ("Current working directory is% s"% retval)

 # Modify the current working directory

 os.chdir (path)

 # View the modified working directory

 retval = os.getcwd ()

 print ("Successfully modified directory% s"% retval)

(3) os.curdir


 Return to the current directory: The return value is (‘.’)

(4) os.pardir


    Get the parent directory string name of the current directory: the return value is (‘..’)


(5) os.makedirs (‘dir1 / dir2’)


   Can generate multi-layer recursive directory residual mkdir -d / dir1 / dir2


(6) os.removedirs (‘dir1 / dir2’)


   If the directory is empty, delete it and recurse to the previous directory, if it is also empty, delete it, and so on, if a file dir1 is created under dir1, it will not be deleted


(7) os.mkdir (‘dir’)


   Generate a single-level directory; equivalent to mkdir dir in the shell


(8) os.rmdir (‘dir’)

  Delete a single-level empty directory, if the directory is not empty, it cannot be deleted, an error is reported; equivalent to rmdir dir in the shell

(9) os.listdir (‘dirname’)

  List all files and subdirectories in the specified directory, including hidden files, and print them as a list

(10) os.remove ()

  Delete a file

(11) os.rename ("oldname", "newname")

   Rename file / directory

(12) os.stat (‘path / filename’)

   Get file / directory information

(13) os.sep

   The operating system specific path separator is output, "\\" under win, "/" under Linux

(14) os.linesep

   Output the line terminator used by the current platform, "\ t \ n" under win, "\ n" under Linux

(15) os.pathsep

   Output string used to split file path

(16) os.name

   The output string indicates the currently used platform. win-> ‘nt’; Linux-> ‘posix’

(17) os.system ("bash command")

   Run the shell command, display directly

    import os

    print (os.system ("ping www.baidu.com"))

(18) os.environ

   Get system environment variables

(19) os.path.abspath (path)

   Returns the absolute path normalized by path

    import os

    print (os.path.abspath (‘hellow.py’))

(20) os.path.split (path)

   Split path into a binary tuple of directory and file name and return, without judging whether the file or directory exists

    import os

    fileName = r "C: \ Users \ SS \ test.txt"

    print (os.path.split (fileName))

(21) os.path.dirname (path)

   Remove the file name and return to the directory path

    import os

    print (os.path.dirname (os .__ file__))

(22) os.path.basename (path)

   Returns the last file name of path. If the path ends with / or \, then a null value will be returned. That is the second element of os.path.split (path)

   import os

   print (os.path.basename (r "d \ aaa \ aa.txt"))

(23) os.path.exists (path)

   If path exists, return True; if path does not exist, return False

(24) os.path.isabs (path)

   If path is an absolute path, return True

(25) os.path.isfile (path)

   If path is an existing file, return True. Otherwise return False

(26) os.path.isdir (path)

   If path is an existing directory, return True. Otherwise return False

(27) os.path.join (path1 [, path2 [, ...]])

   Return after combining multiple paths, the parameters before the first absolute path will be ignored

    imprort os


    os.path.join (‘b‘, ‘b’, ‘ss.txt’) # Output # ‘b / b / ss.txt’

(28) os.path.getatime (path)


   Returns the last access time of the file or directory pointed to by path

(29) os.path.getmtime (path)

   Returns the last modification time of the file or directory pointed to by path

Note! : The time returned by 28 and 29 is a unix timestamp, you can use the detetime library to handle this timestamp

import datetime, os

a = os.path.getmtime (‘/‘)

aa = datetime.datetime.fromtimestamp (t)

aaa = aa.strftime (‘% Y’) #% Y is the time processing format and can only return the year. For more format writing, please Baidu

print aaa


Five, built-in module-ConfigParser

This built-in module is generally used for configuration file operations, so what configuration files can this module handle? Let's take a look


example:


Read operation


-read (filename) read the content of ini file directly

-sections () Get all the sections and return them as a list

-options (section) Get all options of this section

-items (section) get all key-value pairs of the section

-get (section, option) Get the value of option in section, and return it as string type

-getint (section, option) Get the value of option in section, and return it as type int


Write operation

-add_section (section) Add a new section

-set (section, option, value) Set the options in the section


example :

aaa file content

[a]

one = 1

two = ins


[b]

one = 2

two = ins

Sample code

#! / usr / bin / env python

#-*-coding: utf-8 – *-

#First we load the module

import configparser

#Create a config object


c = configparser.ConfigParser ()

#Read aaa file

c.read ("aaa")

#Read the section in aaa [‘a‘, ‘b’]

print (c.sections ())

#Read the section in aaa is the option of a (the meaning of the option)

print (c.options (‘a‘)) # [‘one‘, ‘two‘]

#Read all the key-value pairs (options and values) in aaa where section is a

print (c.items (‘a‘)) # [(‘one‘, ‘1‘), (‘two’, ‘int’)]

#Read the section in aaa is the value of a option is one, the return type is str

print (c.get (‘a‘, ‘one‘))

print (type (c.get (‘a‘, ‘one‘))) # 1

#Read the section in aaa is the value of a option is one, the returned type is int

print (c.getint (‘a‘, ‘one‘))

print (type (c.getint (‘a‘, ‘one‘))) # 1


write:

import configparser

#Create a config object


c = configparser.ConfigParser ()

#Read aaa file

c.read ("aaa")

#Add section to file

c.add_section ("c")

#Add key-value pairs in section

c.set ("c", "one", "2")

c.set ("c", "two", "1")

#Last write to file

c.write (open ("aaa", "w"))

#Delete the key-value pair one for section c

c.remove_option (‘c‘, ‘one’)

#Write file

c.write (open ("aaa", "w"))

Official entrance

Six, built-in module-hashlib

Hashlib is an encryption module, mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm


import hashlib

c = "test"

#Here is to convert the encoding

c = c.encode (encoding = ‘utf-8’)

# ######## md5 ########


hash = hashlib.md5 ()

hash.update (c)

print (hash.hexdigest ())


# ######## sha1 ########


hash = hashlib.sha1 ()

hash.update (c)

print (hash.hexdigest ())


# ######## sha256 ########


hash = hashlib.sha256 ()

hash.update (c)

print (hash.hexdigest ())



# ######## sha384 ########


hash = hashlib.sha384 ()

hash.update (c)

print (hash.hexdigest ())


# ######## sha512 ######## Add custom


hash = hashlib.sha512 ()

hash.update (c)

print (hash.hexdigest ())

Although the above algorithm is relatively strong, it can still be cracked by hitting the library, so in order to prevent this, you can also encrypt the key in the encryption algorithm.


import hashlib

c = "test"

c = c.encode (encoding = ‘utf-8’)

key = "aaa"

key = key.encode (encoding = ‘utf-8’)

# ######## md5 ########

hash = hashlib.md5 (key)

hash.update (c)

print (hash.hexdigest ())


hmac module

Python also has a hmac module, which internally processes the key and content we create and then encrypts

import hmac

c = "test"

c = c.encode (encoding = ‘utf-8’)

key = "aaa"

key = key.encode (encoding = ‘utf-8’)

hash = hmac.new (key)

hash.update (c)

print (hash.hexdigest ())

Seven, with file operations

For us, ordinary operation files are executed every time It is very troublesome to add the closing statement to the file, so python2.7 has been added with


with also supports the management of the context of multiple files at the same time, so it is very convenient


example


with open ("aaa") as test1, open ("ss", "w") as test2:

    test2.write (test1.read ())

The third chapter of the road of python growth (4) _ scope, recursion, module, built-in module (os, ConfigParser, hashlib), with file operation




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.