#006 - Introduction to modules and packages

Introduction to modules and packages

write picture description here

Decorator:

  • is a function whose purpose is to wrap another function to transparently modify or enhance the wrapped object
  • @Decorator name, appearing before the definition of the function
  • definition:
def deco(func):
def wrapper():
print(“deco begain……”)
func()
print(“deco end….”)
return wrapper
>>>@deco
· · · def show():
· · · print(“func…..”)
>>>show()
deco begain…..
func….
deco end……
  • The show function uses a decorator and wraps the show function with a wrapper. Originally, show can only output "func....", but after decoration, it outputs "deco begin.... func.... deco end...."

Python module

  • A program with a large amount of code can be divided into multiple organized code fragments that are independent of each other but can interact with each other. These self-contained organized code fragments are modules
  • Modules are physically represented as code files ending in .py
    • A file is seen as a separate module, and a module can also be seen as a file
    • The filename of a module is the name of the module plus the extension .py
    • Each module has its own namespace
  • Python allows to "import" other modules for code reuse, thus also enabling independent code files to be organized into larger program systems
    • In python, modules are also objects
    • All variables defined at the top level of a module become properties of the imported module when imported

module execution environment

  • Modules are imported, but modules can also import and use other modules, which can be written in python or other programming languages
  • Modules can contain variables, functions, and classes to do their work, and functions and classes can contain variables and other elements

import module:

  • import : Importing the specified module as a whole will introduce a new namespace, that is, the module name;
    • import copy
      • copy.deepcopy()
    • import mod1[,mod2,…]
    • import mod1 as mod1_alias
  • from - import : Import some attributes of the specified module, and directly introduce the attributes of the module into the current namespace
    • from copy import deepcopy
      • deepcopy()

module search

  • The Python interpreter must first find the corresponding module file when importing a module
    • the main directory of the program
    • PYTHONPATH directory (if these two variables are set)
    • standard link library directory
    • Contents of any .pth file (if .pth exists)
  • The combination of these four components is the path contained in sys.path, and Python will select the first file in the search path that matches the imported file name

Python package

  • Merge a group of modules into a directory, this directory is the package, and the directory is the package name
    • A package is a hierarchical file directory structure that defines a python application execution environment consisting of modules and subpackages
    • Based on the package, python can specify the import path of the module when executing the module import
      • import dir.dir2.mod1
  • To use package1 as shown, the Py_pkg_mod container must be in the module search path
    • import package1.mod1
  • There must be an init .py file in each directory within the path of the package import statement
    • init .py can contain python code, but is usually empty and is only used to act as a hook for package initialization, to generate module namespaces for directories, and to implement from * behavior when using directory imports

Top-level execution of modules and imported

  • A module file can both support top-level execution (as a top-level file) or be imported (as a module file)
    • Every module has a built-in property named name , which is automatically set by python
      • If the file is executed as a top-level program file, at startup, the value of name is " main "
      • If it is imported, the value of name is the module name
    • You can detect your own name attribute in the module file to run the specified code at execution time
    • The top level executes self-tests that are often used for modules
!/bin/python
#
def testFunc():
print (“Hello world!”)

if __name__ == “__main__”:
testFunc()

Module packaging
* distutils modules can help complete a module or program distribution
* "distribution" refers to a collection of files that together can be used to build, package and distribute modules using distutils
* A created distribution can be used for installation or uploaded to Share PyPI with others
* Create distributions
* Organize code files into module containers
* Prepare a README or README.txt file
* Then create a setup.py file in the container

from distutils.core import setup

set up(
name               =  ’testmod’,
version            =  ‘0.0.1’,
author             =  ‘xiaoc’,
author_email       =  ‘example@gmail.com’,
py_modules         =  [’testmod’],
url                = ‘ http://www.example.com',
description        = ‘A simple module’,
)  
* 在要发布的目录中执行:python setup.py sdist     

Install third-party modules

  • easyinstall
  • pip
    • Search for mysql in the pip repository
      • pip search mysql
    • install pymysql
      • pip install pymysql
    • See what modules are in the python library
      • help(“modules”)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325734719&siteId=291194637