Modules and packages, modules

Source: Internet
Author: User

Modules and packages, modules
Module

A module is a file that contains python definitions and declarations. The file name is the extension of the module name and. py.

In fact, the import module is divided into four general categories:

1. code written in python (. py file)

2 C or C ++ extensions that have been compiled as shared libraries or DLL

3. Pack a group of modules

4. Use C to write and link to the python interpreter's built-in modules

Introduce the import module Concept

Exit the python interpreter and re-enter the interpreter. The functions or variables you previously defined will be lost. Therefore, we usually write the program to a file to save it permanently. If necessary, we use python test. in py mode, test. py is called script.

With the development of programs, more and more functions are available. To facilitate management, we usually divide programs into files, so that the program structure is clearer and easier to manage. In this case, we can not only execute these files as scripts, but also import them as modules to other modules to achieve reuse of functions.

Import Module

There is a spam. py file.

#spam.pyprint('from the spam.py')money=1000def read1():    print('spam->read1->money',money)def read2():    print('spam->read2 calling read')    read1()def change():    global money    money=0
Spam. py

It is executed only when the module name encounters an import Statement for the first time (the import statement can be used anywhere in the program, and it is imported many times for the same module. To prevent repeated import, the python optimization method is: after the first import, the module name is loaded into the memory. The subsequent import statement only adds a reference to the module object that has been loaded into large memory, does not re-execute the statements in the module ).

The import module has three things to do:

First, create a namespace to store the name defined in spam. py,
Second thing: Execute spam. py Based on the namespace you just created,
The third thing: Create a name spam pointing to the namespace, spam. The operation of the name is based on spam. py.

Import spam # Run spam only when the first import is made. in py, the explicit effect here is to print 'from the spam only once. py', of course, other top-level code is also executed, but the effect is not displayed. 3 import spam 4 import spam 5 import spam running result: from the spam. py
Import multiple times

Sys. module is a dictionary that contains the ing between the module name and the module object. This dictionary determines whether to re-import the module during import.

Each module is an independent namespace, which defines the functions in this module and regards the module namespace as a global namespace. In this way, when writing our own modules, there is no need to worry that the global variables defined in our module will conflict with the user's global variables when being imported.

import spammoney=1spam.change()print(money)

The value of money for the above operation is 1, and the function definition in the spam module is also the "executed" statement, the execution of the module-Level Function Definition puts the function name into the global namespace table of the module, which can be viewed using globals () without affecting the namespace of the import module.

Import spam as sm # change the name, no actual impact on print (sm. money)
If file_format = 'xml': import xmlreader as readerelif file_format = 'csv': import csvreader as readerdata = reader. read_date (filename) # No matter what format, I only need to know the reader.
Alias

You can use reflection to check whether the module has this attribute.

print(hasattr(spam,'money'))
From... import...

Compared with import spam, the source file namespace 'spam' will be taken to the current namespace, which must be in the form of spam. Name.

The from statement is equivalent to import and creates a new namespace, but imports the name in spam directly to the current namespace. In the current namespace, use the name directly.

Note the following points for this import method,

From spam import read1money = 1000read1 () # The executed read1 is from the spam module, and the value obtained by read1 is still the money in spam.
# The imported function read2 must call read1 () during execution and return to spam. find read1 () from spam import read2def read1 (): print ('=') read2 () in py ()
# The name defined currently overwrites the execution result of the module name from spam import read1def read1 (): print ('=') read1: from the spam. py ============
From spam import money, read1money = 100 # bind the name of the current position to 100 print (money) # print the current name read1 () # Read spam. in py, the name is money, which is still 1000.

This method also supports.

from spam import read1 as read

Multi-line import.

from spam import (read1,                  read2,                  money)

From spam import * imports all the names starting with not the following underscore (_) in spam to the current position. In most cases, this import method should not be used by our python program, because * you do not know what name you import, it is very likely that you will overwrite the previously defined name. It is extremely difficult to import data in an interactive environment.

Use _ all _ to control * content.

_ All __= ['money', 'read1'] # Use from spam import * in another file to import the two names specified in the list.

If _ is added before the name in spam. py, that is, _ money, from spam import *, _ money cannot be imported.

Modifying the module during the import process is invalid, and python does not support overloading.

If _ name _ = '_ main _': used to control the. py file to execute different logic in different application scenarios.

  The module search order is: the modules that have been loaded in the memory (sys. modules)-> built-in modules-> modules included in the sys. path.

The python program can modify sys. path, and put the path above the standard library to be loaded.

Import sys. path. append (R'/a/B/c/D') sys. path. insert (0, R'/x/y/Z') # The first directory to be searched first.

Note: The Ghost archive file is processed as a directory.

Import syssys.path.append('lele.zip ') import foo,barrier can also use the specific position of the zip content structure to set sys.path.append('lele.zip/lib/python ')

Sys. path is initialized from the following locations

1. Current Directory of the execution File

2 PTYHONPATH (including a series of directory names, the same as the shell variable PATH syntax)

3. The default value for dependency installation is

Package

Before you start, you must be clear about three points.

1. whether in the import form or from... in the import format, whenever there is a dot in the import Statement (rather than in use), you must be alert immediately: this is about the package's import syntax.

2. A package is directory-level (Folder-level), and a folder is used to form a py file (the package is essentially a directory containing the _ init _. py file ).

3.IDuring the mport import file, the names in the generated namespace are from the file, the import package, and the names of the generated namespace are also from the file, that is, the _ init _ under the package __. py, the essence of the import package is to import the file.

Note: 1. in python3, even if no _ init __. the import package still does not report an error in the py file. In python2, the file must be included in the package; otherwise, the import package reports an error.

2. the purpose of creating a package is not to run but to be imported and used. Remember, a package is just a form of a module, namely, a package is a module.

3. There is no conflict between A and B modules with the same name. For example, A. a and B. a are from two namespaces.

Package import Method

Or two types of import and from... import ..

Perform a test under the same level of glance:

Import glance. db. models # import glance. db. models. register_models ('mysql') # Call
from glance.db import modelsmodels.register_models('mysql')from glance.db.models import register_modelsregister_models('mysql')
_ Init _. py file

As long as the package is imported for the first time or any other part of the package, the _ init _ under the package will be executed in sequence __. py file (we can print a line of content in each package file to verify). This file can be empty, but it can also store some initialization package code.

From glance. api import *

We want to import all data from the package api. In fact, this statement only imports _ init _ under the package api __. the name defined in The py file. In this file, we can define _ all ___:

_ All __= ['x', 'y', 'policy', 'versions'] # This allows these values to be imported from. import Export YX = 1y = 2

In this case, execute from glance in the file with the same level as glance. api import * imports the content in _ all _, but import does not trigger _ all __,__ all _ only matches.

Absolute import and relative import

The glance of the top-level package is intended for use by others. Then, the glance package also needs to import each other. At this time, there are two methods: absolute import and relative import:

Absolute import: starts with glance,

Relative import: the method of using... is the most initial (it can only be used in one package and cannot be used in different directories ).

Note: You can use import to import built-in or third-party modules (already in sys. path), but do not use import to import the sub-modules of the custom package (not in sys. path), use from... import... absolute or relative import, and the relative import of packages can only be in the form of from.

In glance/api/version. py # Absolutely import from glance. cmd import managemanage. main () # relatively import from .. cmd import manage #. and .. it refers to the current relative. It has nothing to do with who triggered the task. Only manage cannot be executed separately when the task is effective during import. main ()

 

1. package-related import statements are also divided into import and from... import... but either way, no matter where it is, you must follow the principle that the left side of the vertex must be a package if it has a dot during import. Otherwise, the package is invalid. A series of points, such as item. subitem. subsubitem, must be followed.

2. after the import, there is no such restriction during use. The left side of the vertex can be the package, module, function, and class (they can all call their own attributes by means of vertices ).

3. Compare the application scenarios of import item and from item import name: If you want to use name directly, you must use the latter.

Software Development specifications

 

Bin directory: stores execution scripts

import sys,osBASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.path.append(BASE_DIR)from core import corefrom conf import my_log_settingsif __name__ == '__main__':    my_log_settings.load_my_logging_cfg()    core.run()
Bin/start. py

Conf directory: stores configuration files

[DEFAULT]user_timeout = 1000[egon]password = 123money = 10000000[alex]password = alex3714money=10000000000[yuanhao]password = ysb123money=10
Conf/config. init
import osconfig_path=r'%s\%s' %(os.path.dirname(os.path.abspath(__file__)),'config.ini')user_timeout=10user_db_path=r'%s\%s' %(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),\                     'db')
Conf/settings. py
"Logging configuration" import osimport logging. config # define three log output formats start standard_format = '[% (asctime) s] [% (threadName) s: % (thread) d] [task_id: % (name) s] [% (filename) s: % (lineno) d] '\' [% (levelname) s] [% (message) s] '# Where name is the name specified by getlogger simple_format =' [% (levelname) s] [% (asctime) s] [% (filename) s: % (lineno) d] % (message) s 'id _ simple_format = '[% (levelname) s] [% (asctime) s] % (message) s' # define the log output format to end logfile_dir = R' % s \ log' % o S. path. dirname (OS. path. dirname (OS. path. abspath (_ file _) # logfile_name = 'all2. log' # log file name # create an if not OS if the defined log directory does not exist. path. isdir (logfile_dir): OS. mkdir (logfile_dir) # Full log File Path logfile_path = OS. path. join (logfile_dir, logfile_name) # log configuration dictionary LOGGING_DIC = {'version': 1, 'Disable _ existing_loggers ': False, 'formatters': {'standard ': {'format': standard_format}, 'simple': {'format': si Mple_format },}, 'filters ':{}, 'handlers' :{# logs printed to the terminal 'console': {'level': 'debug', 'class ': 'logging. streamHandler ', # print to the screen 'formatter': 'simple'}, # print to the file log, collect info and above logs 'default': {'level ': 'debug', 'class': 'logging. handlers. rotatingFileHandler ', # Save It To The 'formatter': 'standard', 'filename': logfile_path, # Log File 'maxbytes ': 1024*1024*5, # Log Size 5 m'backupcount': 5, 'encoding': 'utf-8 ',# The encoding of log files. Do not worry about Chinese log garbled characters anymore},}, 'loggers': {# logging. the logger configuration obtained by getLogger (_ name _) '': {'handlers': ['default', 'console'], # add the two handler defined above, that is, log data is written to the file and printed to the screen 'level': 'debug', 'pagate': True, # pass up (higher-level logger) },},} def load_my_logging_cfg (): logging. config. dictConfig (LOGGING_DIC) # import the logging configuration defined above logger = logging. getLogger (_ name _) # generate a log instance logger.info ('It works! ') # Record the running status of the file if _ name _ =' _ main _ ': load_my_logging_cfg ()
Conf/my_log_settings.py

Core Directory: stores core logic

Import loggingimport timefrom conf import settingsfrom lib import read_iniconfig = read_ini.read (settings. config_path) logger = logging. getLogger (_ name _) current_user = {'user': None, 'login _ time': None, 'timeout': int (settings. user_timeout)} def auth (func): def wrapper (* args, ** kwargs): if current_user ['user']: interval = time. time ()-current_user ['login _ time'] if interval <current_user ['timeout']: return func (* args, ** kwargs) name = input ('name >>:') password = input ('password >>:') if config. has_section (name): if password = config. get (name, 'Password'): logger.info ('logon successfully') current_user ['user'] = name current_user ['login _ time'] = time. time () return func (* args, ** kwargs) else: logger. error ('user name does not exist') return wrapper @ authdef buy (): print ('buy... ') @ authdef run (): print ('''1 shopping 2 view Balance 3 Transfer ''') while True: choice = input (' >> :'). strip () if not choice: continue if choice = '1': buy () if _ name _ = '_ main _': run ()
Core/core. py

Db Directory: stores database files

Lib directory: stores custom modules and packages

import configparserdef read(config_file):    config=configparser.ConfigParser()    config.read(config_file)    return config
Lib/read_ini.py

Log directory: stores logs.

[00:31:40, 272] [MainThread: 11692] [task_id: conf. my_log_settings] [my_log_settings.py: 75] [INFO] [It works!] [00:31:41, 789] [MainThread: 11692] [task_id: core. core] [core. py: 25] [ERROR] [the user name does not exist] [00:31:46, 394] [MainThread: 12348] [task_id: conf. my_log_settings] [my_log_settings.py: 75] [INFO] [It works!] [00:31:47, 629] [MainThread: 12348] [task_id: core. core] [core. py: 25] [ERROR] [the user name does not exist] [00:31:57, 912] [MainThread: 10528] [task_id: conf. my_log_settings] [my_log_settings.py: 75] [INFO] [It works!] [00:32:03, 340] [MainThread: 12744] [task_id: conf. my_log_settings] [my_log_settings.py: 75] [INFO] [It works!] [00:32:05, 065] [MainThread: 12916] [task_id: conf. my_log_settings] [my_log_settings.py: 75] [INFO] [It works!] [00:32:08, 181] [MainThread: 12916] [task_id: core. core] [core. py: 25] [ERROR] [the user name does not exist] [00:32:13, 638] [MainThread: 7220] [task_id: conf. my_log_settings] [my_log_settings.py: 75] [INFO] [It works!]
Log/a112.log

 

Related Article

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.