Basic Primer _python-modules and packages. __import__ Dynamic import best practices in operational development?

Source: Internet
Author: User

General Import:

Import Module_name[,module1,...] from module_name import [*|child[,child1,...] From module_name import [*|child[,child1,...] as Alias_name

Note: Import statements can appear anywhere in the program, and custom packages are implemented The effect of the from module_name import * is that this module must be implemented in __init__.py __all__ = [' module_1 ', ' module_2 ']


Load once:

Note: When you reuse the import statement multiple times, the module is not reloaded, but the memory address of the module is referenced to the local environment variable

==> x.py <==#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51CTOBG:HTTP://XMDEVOPS.BLOG.51CT o.com/# purpose:# "" "# Description: Import Public Module Imports os# Description: Import other modules print ' Os in x.py ', ID (OS) ==> y.py <==#!/usr/bin/env python#-* -Coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# purpose:#" "# Description: Import Public Module Imports Ximp ORT os# Description: Import other modules if __name__ = = ' __main__ ': print ' os in y.py ', ID (OS) import X


Reload:

Description: The module has been loaded to reload, generally used for the original module changes and other special cases, reload before the module must have been import, but it should be noted that the used instance will also use the old module, and the newly generated instance will use the new module, Reload after the original memory address

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import Public Module Imports systry:sys.setdefaultencoding (' Utf-8 ') except Exception, e:print e reload (SYS) sys.setd Efaultencoding (' Utf-8 ') print sys.getdefaultencoding () # Description: Import other modules if __name__ = = ' __main__ ': Pass

Description: Many people do not name why reload () SYS is required to use setdefaultencoding to set the encoding, in fact, because the interpreter initialization is pre-executed/usr/lib64/python2.7/site.py, And in its 554 lines of code del sys.setdefaultencoding deleted this method, in fact, you import sys only points to the setdefaultencoding attribute of the SYS module address, So we need to reload again. Restore this method


Relative imports:

Description: Py uses the point in the module name to determine whether the package belongs to which package, when you use the from. XX Import Oo, where the point represents the hierarchy in the package structure, if the module name __MAIN__ indicates that it does not belong to any package, so at this time the module name should not contain a point, otherwise it will cause relative-import in Non-package error, This means that the file containing the relative import cannot be used as a portal file, but can be loaded by Python-m as a module

From.. Libs.database import Redisfrom. Libs.alarm import Alarm_templatefrom. LIBS.ALARM.API Import Weixin_notify


Absolute Import:

Note: An absolute import is also called a full import, and the 2.x version must use the From __future__ import Absolute_import to turn this mechanism on, and 3.x as the default mechanism

From x.y.z import o


Dynamic Import:

Description: __import__ is actually the internal implementation of import, usually used for dynamic loading, such as plug-in monitoring system only know the plug-in name how to execute the code inside the plug-in, you can do this by dynamic loading to get the function inside the plug-in and then go to call

__import__ (module_name[, globals[, locals[, FromList]]), object

Description: Module_name is the module name, but it is important to note that if Module_name contains submodules such as x.y, then the X object is returned by default, and if you want to return the Y object you need to set the FromList list to implement the From X import The effect of Y. Of course to implement dynamic import contains specialized imp and importlib modules. Can learn ~


Application Scenarios:

1. Zabbix/nagios and other monitoring system active monitoring must be manually configured agent, even if the custom plug-in is also the case, if you want to implement a automatic detection plug-in automatically call plug-in automatic reporting data monitoring system how to achieve?

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/88/33/wKioL1fru7vhZnohAAGAabRm-Pc794.png "title=" Res.png "alt=" Wkiol1fru7vhznohaagaabrm-pc794.png "/>

#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman#  51ctobg: http://my.51ctobg.net/pydevops/# purpose:# "" "#  description:  compatible absolute import from __future__  import absolute_import#  Description:  Import public module import jsonimport timeimport threading#   Description:  Import other modules from . LIBS.DATABASE&NBSP;IMPORT&NBSP;REDISFROM&NBSP, .... LIBS.ALARM&NBSP;IMPORT&NBSP;ALARM_TEMPLATEFROM&NBSP, .... libs.alarm.api import weixin_notify#  Description:  Client Monitoring class Class monitorclient (object):     def __init__ (Self, redis, agent, info, error):         self.info = info         Self.error = error        self.redis = redis (db= redis[' DB '],                            host=redis[' Host '],                             port=redis[' Port '],                            password=redis [' Password '])         self.agent_host = agent[' Host ']         self.redis_host = redis[' Host ']         self.clientconf = self._get_climconf ()          self.pubchannel = redis[' publish '] or  ' Xmdevops_channel '          self.info.info (' update key#climconf::%s val#%s '  %  (Self.agent_host,  self.clientconf)     def start (self):         self._plugins_ Handler ()     def _get_climconf (self):         redis_key =  ' climconf::%s '  %  (self.agent_host,)          while true:            redis_val =  self.redis.get (Redis_key)             if  not redis_val:                 message =  ' Getval key#%s with nothing,5 seconds try again '  %  (Redis_key,)                  self.info.info (message)                 &nbSp;self._report_exception (redis_key, message)                  time.sleep (5)                  continue             try:                conf _dict = json.loads (Redis_val)              except TypeError, e:                 message =  ' unpack key#%s val#%s with error %s '  %  (redis_key, redis_val, e)                  self.error.error (message)              &nBsp;   self._report_exception (redis_key, message)                  time.sleep (5)                  continue             break        return conf_dict     def _plugins_handler (self):        while  true:            for service_name,  Plugin_info in self.clientconf.iteritems ():                 if len (Plugin_info)  < 4:                      Self.clientconf[service_name].append (0) &NBsp;               plugin_name,  check_interval, add_data, last_runtime = plugin_info                 if time.time ()  - last_runtime  > check_interval:                     self.clientconf[service_name][-1] = time.time ()                       self.info.info (' plugin key#%s val#%s is called '  %  (service_name,  Plugin_info))                      cur_thread = threading. Thread (&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;           target=self._plugins_called, args= ( Service_name, plugin_name, add_data))                      cur_thread.start ()              time.sleep (1)              old_clientconf = self.clientconf             self.clientconf = self._get_climconf ()              for trigger_key, trigger_val in  Self.clientconf.iteritems ():                 if trigger_key in old_clientconf:                &Nbsp;    self.clientconf[trigger_key].append (Old_clientconf[trigger_key][-1])      def _plugins_called (Self, service_name, plugin_name, add_data):         plugin_path =  ' app.%s.%s '  %  (' plugins ',  service_ Name)         try:             plugin_mods = __import__ (Plugin_path, fromlist=[service_name])          except ValueError, e:             message =  ' Import key#%s val#%s with error  %s '  %  (plugin_path, e)              self.error.error (message)             self._ Report_exception (Plugin_path,  message)             return         try:             plugin_func = getattr (Plugin_mods, plugin_name)          except AttributeError, e:             message =  ' plugin key#%s val#%s not exists '  %  (Plugin_mods,  plugin_name)             self.error.error ( Message)             self._report_exception (plugin_ Func, message)             return         plugin_data = plugin_func (Add_data)          report_data = {             ' host ':  self.agent_ host,             ' Data ': plugin_data,              ' Service ': service_name         }        data =  Json.dumps (Report_data)         self.info.info (' publish key#%s  val#%s '  %  (service_name, data)          Self.redis.publish (Self.pubchannel, data)     def _report_exception (self,  Errors, details):        message = alarm_template  %  (            self.agent_host,  ' Critical ',  errors,    &Nbsp;       time.strftime ('%h:%m:%s ',  time.localtime ()),  details)         results = weixin_notify (message)          if results:             self.error.error (Results)

Description: As above is a self-written redis-based fully automated micro-monitoring framework part of the core code, first read the web-side monitoring configuration, and then use the thread through the __import__ dynamically call the plug-in in the Portal monitoring function, and then escalate the execution results to the corresponding region of Redis, Server-side re-processing threshold data and so on, can be used as a very good learning case

This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1857506

Basic Primer _python-modules and packages. __import__ Dynamic import best practices in operational development?

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.