zoukankan      html  css  js  c++  java
  • flask config笔记

    #从flask这个包中导入Flask这个类
    #Flask这个类是项目的核心,以后很多操作都是基于这个类的对象
    #注册url、注册蓝图等都是基于这个类的对象
    from flask import Flask
    
    import config #配置文件
    
    #创建一个Flask对象,传递__name__参数进去
    #__name__参数的作用:
    #1.可以规定模板和静态文件的查找路劲
    #2.以后一些Flask插件,比如Flask-migrate、Flask-SQLAlchemy如果报错了,
    #那么Flask可以通过这个参数找到具体的报错位置
    app = Flask(__name__)
    
    # app.debug = True #第二中模式debug
    
    # app.config.update(DEBUG=True)#第三种模式
    #可以看出config其实是一个字典
    
    app.config.from_object(config) #第四种模式,从配置文件中导入 

    #为什么config的debug必须是大写

    我们看下
    from_object的源码

    
    
    
    
    
    def from_object(self, obj):
    """Updates the values from the given object. An object can be of one
    of the following two types:

    - a string: in this case the object with that name will be imported
    - an actual object reference: that object is used directly

    Objects are usually either modules or classes. :meth:`from_object`
    loads only the uppercase attributes of the module/class. A ``dict``
    object will not work with :meth:`from_object` because the keys of a
    ``dict`` are not attributes of the ``dict`` class.

    Example of module-based configuration::

    app.config.from_object('yourapplication.default_config')
    from yourapplication import default_config
    app.config.from_object(default_config)

    You should not use this function to load the actual configuration but
    rather configuration defaults. The actual config should be loaded
    with :meth:`from_pyfile` and ideally from a location not within the
    package because the package might be installed system wide.

    See :ref:`config-dev-prod` for an example of class-based configuration
    using :meth:`from_object`.

    :param obj: an import name or object
    """
    if isinstance(obj, string_types):
    obj = import_string(obj)
    for key in dir(obj):#这里用dir()内置函数查看文件参数
    if key.isupper():#这里判断了必须大写
    self[key] = getattr(obj, key)这里获取debug的值


     再看下from_pyfile的源码

     def from_pyfile(self, filename, silent=False):
            """Updates the values in the config from a Python file.  This function
            behaves as if the file was imported as module with the
            :meth:`from_object` function.
    
            :param filename: the filename of the config.  This can either be an
                             absolute filename or a filename relative to the
                             root path.
            :param silent: set to ``True`` if you want silent failure for missing
                           files.
    
            .. versionadded:: 0.7
               `silent` parameter.
            """
            filename = os.path.join(self.root_path, filename)
            d = types.ModuleType('config')
            d.__file__ = filename #
            try:
                with open(filename, mode='rb') as config_file:
                    exec(compile(config_file.read(), filename, 'exec'), d.__dict__)
            except IOError as e:
                if silent and e.errno in (
                    errno.ENOENT, errno.EISDIR, errno.ENOTDIR
                ):
                    return False
                e.strerror = 'Unable to load configuration file (%s)' % e.strerror
                raise
            self.from_object(d)
            return True

    第一个知识点:
    d.__file__ = filename #__file__
    文件的绝对路径
    第二个知识点:
    compile() 函数将一个字符串编译为字节代码。


    参数source:字符串或者AST(Abstract Syntax Trees)对象。

    参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。

    参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。如果是exec类型,表示这是一个序列语句,可以进行运行;如果是eval类型,表示这是一个单一的表达式语句,可以用来计算相应的值出来;如果是single类型,表示这是一个单一语句,采用交互模式执行,在这种情况下,如果是一个表达式,一般会输出结果,而不是打印为None输出。


    exec第三个知识点:
    这个暂时放下
     
  • 相关阅读:
    Maidsafe-去中心化互联网白皮书
    The Top 20 Cybersecurity Startups To Watch In 2021 Based On Crunchbase
    Top 10 Blockchain Security and Smart Contract Audit Companies
    The 20 Best Cybersecurity Startups To Watch In 2020
    Blockchain In Cybersecurity: 11 Startups To Watch In 2019
    004-STM32+BC26丨260Y基本控制篇(阿里云物联网平台)-在阿里云物联网平台上一型一密动态注册设备(Android)
    涂鸦开发-单片机+涂鸦模组开发+OTA
    000-ESP32学习开发-ESP32烧录板使用说明
    03-STM32+Air724UG远程升级篇OTA(阿里云物联网平台)-STM32+Air724UG使用阿里云物联网平台OTA远程更新STM32程序
    03-STM32+Air724UG远程升级篇OTA(自建物联网平台)-STM32+Air724UG实现利用http/https远程更新STM32程序(TCP指令,单片机程序检查更新)
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/9658276.html
Copyright © 2011-2022 走看看