码迷,mamicode.com
首页 > 编程语言 > 详细

python之importlib模块

时间:2018-10-06 13:15:50      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:ring   ogg   eve   design   let   ast   使用   实现   prope   

1、import_module方法

  解析字符串,并导入改路径的模块

# 根据字符串导入模块
# 通畅用来导入包下面的模块
o = importlib.import_module("aa.bb")
s2 = "Person"

# 由字符串找函数、方法、类  利用 反射
the_class = getattr(o, "Person")
p2 = the_class("小黑")
p2.dream()

  从"aa.bb"路径导入bb模块。

2、import_module在Django中的使用

2.1、Django中使用import_module实现import_string方法

  在Django源码目录"django/utils/module_loading.py",使用importlib中的import_module函数实现了Django中自定义的import_string(dotted_path)方法

  源码:

def import_string(dotted_path):
    """
    Import a dotted module path and return the attribute/class designated by the
    last name in the path. Raise ImportError if the import failed.
    """
    try:
        module_path, class_name = dotted_path.rsplit(., 1)
    except ValueError as err:
        raise ImportError("%s doesn‘t look like a module path" % dotted_path) from err

    module = import_module(module_path)

    try:
        return getattr(module, class_name)
    except AttributeError as err:
        raise ImportError(Module "%s" does not define a "%s" attribute/class % (
            module_path, class_name)
        ) from err

  解析:

    例如Django项目settings.py中的‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,在dotted_path.rsplit(‘.‘, 1)后生成元组(‘django.middleware.clickjacking‘,‘XFrameOptionsMiddleware‘),再使用import_module导入‘django.middleware.clickjacking‘模块,最后使用映射获取类XFrameOptionsMiddleware。

 2.2、Django中使用import_string导入模块中的类

  在"django\core\handlers\base.py"中class BaseHandler中有load_middleware()方法用来导入settings中的中间件,其中调用了上面的import_string()方法,源码如下:

def load_middleware(self):
    """
    Populate middleware lists from settings.MIDDLEWARE.

    Must be called after the environment is fixed (see __call__ in subclasses).
    """
    self._request_middleware = []
    self._view_middleware = []
    self._template_response_middleware = []
    self._response_middleware = []
    self._exception_middleware = []

    handler = convert_exception_to_response(self._get_response)
    for middleware_path in reversed(settings.MIDDLEWARE):
        middleware = import_string(middleware_path)
        try:
            mw_instance = middleware(handler)
        except MiddlewareNotUsed as exc:
            if settings.DEBUG:
                if str(exc):
                    logger.debug(MiddlewareNotUsed(%r): %s, middleware_path, exc)
                else:
                    logger.debug(MiddlewareNotUsed: %r, middleware_path)
            continue

        if mw_instance is None:
            raise ImproperlyConfigured(
                Middleware factory %s returned None. % middleware_path
            )

        if hasattr(mw_instance, process_view):
            self._view_middleware.insert(0, mw_instance.process_view)
        if hasattr(mw_instance, process_template_response):
            self._template_response_middleware.append(mw_instance.process_template_response)
        if hasattr(mw_instance, process_exception):
            self._exception_middleware.append(mw_instance.process_exception)

        handler = convert_exception_to_response(mw_instance)

    # We only assign to this when initialization is complete as it is used
    # as a flag for initialization being complete.

 

python之importlib模块

标签:ring   ogg   eve   design   let   ast   使用   实现   prope   

原文地址:https://www.cnblogs.com/bad-robot/p/9746870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!