python--Fifth Day Summary

Source: Internet
Author: User
Tags sha1 python script

Adorner--@ rename original function, return function object

is a function, at least two layers
Execute function, decorated function as parameter
----------------------------------------------------
1, the adorner is a function, at least 2 layers
2.
Execute auth function, decorated function as parameter auth (foo)
The return value of the AUTH function, assigned to the function name of the decorated function
@auth
def foo ():
Pass

3, dynamic parameters, can decorate a function containing n parameters
4. function return value
5. Multi-adorner--box model
6, at least 3 layers, 3 layers of how to use?
@w1
1. Execute W1 function
2. Assign the return value of the W1 function to the function name of the decorated function
@w2 (ARG)
1, execute W2 function, get return value, ret
2, create the adorner, @ + RET combination; @ret
3 、。。。
1. Perform RET function
2. Assign the return value of the RET function to the function name of the decorated function

module, which implements a set of code for a function with a mound code.

Like functional programming and process-oriented programming, functional programming accomplishes a function, and other code is used to invoke it, providing reusability of code and coupling between the code. For a complex function, multiple functions may be required to complete (functions can be in different. py files), and the Code collection consisting of N. py files is called a module.

such as: OS is a system-related module; file is a module related to the operation of files

The modules are divided into three types:

    1. Custom Modules
    2. Built-in Modules
    3. Open source Module

1. Define the module

Scenario One:

  

Scenario Two:

  

Scenario Three:

  

]2, Import module

Python is used more and more widely, and to a certain extent relies on it providing programmers with a large number of modules to use, and if you want to use modules, you need to import them. There are several ways to import a module:

    1. Import Module
    2. From module.xx.xx import xx
    3. From module.xx.xx import xx as rename
    4. From module.xx.xx Import *


The import module actually tells the Python interpreter to explain the py file.

Import a py file, the interpreter interprets the py file
Import a package, the interpreter interprets the __init__.py file under the package

So the question is, is the module being imported based on that path as a benchmark? namely: Sys.path

Import Sys
Print Sys.path

Results:

['/users/wupeiqi/pycharmprojects/calculator/p1/pp1','/usr/local/lib/python2.7/site-packages/setuptools-15.2-py2.7.egg','/usr/local/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg','/usr/local/lib/python2.7/site-packages/mysql_python-1.2.4b4-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/xlutils-1.7.1-py2.7.egg','/usr/local/lib/python2.7/site-packages/xlwt-1.0.0-py2.7.egg','/usr/local/lib/python2.7/site-packages/xlrd-0.9.3-py2.7.egg','/usr/local/lib/python2.7/site-packages/tornado-4.1-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/backports.ssl_match_hostname-3.4.0.2-py2.7.egg','/usr/local/lib/python2.7/site-packages/certifi-2015.4.28-py2.7.egg','/usr/local/lib/python2.7/site-packages/pyopenssl-0.15.1-py2.7.egg','/usr/local/lib/python2.7/site-packages/six-1.9.0-py2.7.egg','/usr/local/lib/python2.7/site-packages/cryptography-0.9.1-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/cffi-1.1.1-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/ipaddress-1.0.7-py2.7.egg','/usr/local/lib/python2.7/site-packages/enum34-1.0.4-py2.7.egg','/usr/local/lib/python2.7/site-packages/pyasn1-0.1.7-py2.7.egg','/usr/local/lib/python2.7/site-packages/idna-2.0-py2.7.egg','/usr/local/lib/python2.7/site-packages/pycparser-2.13-py2.7.egg','/usr/local/lib/python2.7/site-packages/django-1.7.8-py2.7.egg','/usr/local/lib/python2.7/site-packages/paramiko-1.10.1-py2.7.egg','/usr/local/lib/python2.7/site-packages/gevent-1.0.2-py2.7-macosx-10.10-x86_64.egg','/usr/local/lib/python2.7/site-packages/greenlet-0.4.7-py2.7-macosx-10.10-x86_64.egg','/users/wupeiqi/pycharmprojects/calculator','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python27.zip','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/plat-darwin','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/plat-mac','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/plat-mac/ Lib-scriptpackages','/USR/LOCAL/CELLAR/PYTHON/2.7.9/FRAMEWORKS/PYTHON.FRAMEWORK/VERSIONS/2.7/LIB/PYTHON2.7/LIB-TK','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/lib-old','/usr/local/cellar/python/2.7.9/frameworks/python.framework/versions/2.7/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/site-packages','/library/python/2.7/site-packages']
View Code

If the Sys.path path list does not have the path you want, it can be added by Sys.path.append (' path ').
Various directories can be obtained through the OS module, for example:

Import Sys
Import OS

Pre_path = Os.path.abspath ('.. /‘)
Sys.path.append (Pre_path)

Built-in Modules

First, OS

Used to provide system-level operations

OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work os.chdir ("dirname") To change the current script working directory, equivalent to the shell under Cdos.curdir return to the current directory: ('.') Os.pardir Gets the parent directory string name of the current directory: ('..') Os.makedirs ('dirname1/dirname2') to generate a multi-level recursive directory Os.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir Dirnameos.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir Dirnameos.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints a list of os.remove () deletes a file Os.rename ("oldname","newname") Rename File/Catalog Os.stat ('Path/filename') Get File/directory information OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/"os.linesep Output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n"the OS.PATHSEP output string that is used to split the file path Os.name the output strings indicates the current usage platform. Win-'NT'; Linux->'POSIX'Os.system ("Bash Command"run the shell command to directly display the Os.environ get system environment variable Os.path.abspath (PATH) return path normalized absolute path Os.path.split (path) Split path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, 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), returns True if path exists, or if path does not exist, returns Falseos.path.isabs if path is an absolute path, Returns Trueos.path.isfile (path) If path is an existing file and returns True. Otherwise, return Falseos.path.isdir (path) 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 of the file or directory to which path is pointing os.path.getmtime (path) Returns the last modified time of the file or directory to which path is pointing

Second, SYS

Used to provide an interpreter-related action

Copy Code sys.argv           command Line Arguments 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 interpreter 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 operating system platform name Sys.stdout.write ('please:'= Sys.stdin.readline () [:-1]

Third, Hashlib

For cryptographic related operations, instead of the MD5 module and the SHA module, mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm

    1. Import Hashlib
    2. # ######## MD5 ########
    3. hash = HASHLIB.MD5 ()
    4. Hash.update (' admin ')
    5. Print Hash.hexdigest ()
    6. # ######## SHA1 ########
    7. hash = HASHLIB.SHA1 ()
    8. Hash.update (' admin ')
    9. Print Hash.hexdigest ()
    10. # ######## sha256 ########
    11. hash = hashlib.sha256 ()
    12. Hash.update (' admin ')
    13. Print Hash.hexdigest ()
    14. # ######## sha384 ########
    15. hash = hashlib.sha384 ()
    16. Hash.update (' admin ')
    17. Print Hash.hexdigest ()
    18. # ######## sha512 ########
    19. hash = hashlib.sha512 ()
    20. Hash.update (' admin ')
    21. Print Hash.hexdigest ()

Iv. JSON and Pickle

Two modules for serialization

    • JSON, used to convert between string and Python data types
    • Pickle for conversion between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load

The Pickle module provides four functions: dumps, dump, loads, load

V. Execution of SYSTEM commands

The relevant modules and functions that can execute shell commands are:

    • Os.system
    • os.spawn*
    • os.popen*--Waste
    • popen2.*--Waste
    • commands.*-Discarded, removed in 3.x

python--Fifth Day Summary

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.