源码解析Flask的配置文件(3)

mappings = []
    if len(mapping) == 1:
        if hasattr(mapping[0], 'items'):
            mappings.append(mapping[0].items())
        else:
            mappings.append(mapping[0])
    elif len(mapping) > 1:
        raise TypeError(
            'expected at most 1 positional argument, got %d' % len(mapping)
        )
    mappings.append(kwargs.items())
    for mapping in mappings:
        for (key, value) in mapping:
            if key.isupper():
                self[key] = value
    return True

把参数字典中的所有键值对添加到列表���,循环遍历列表,读取列表中每个元素的键和值

如果键为大写,则key为配置选项,value为配置选项的值

7.get_namespace:从名称空间中获取配置选项

get_namespace源码:
def get_namespace(self, namespace, lowercase=True, trim_namespace=True):

rv = {}
    for k, v in iteritems(self):
        if not k.startswith(namespace):
            continue
        if trim_namespace:
            key = k[len(namespace):]
        else:
            key = k
        if lowercase:
            key = key.lower()
        rv[key] = v
    return rv

get_namespace方法,是从指定的名称空间或前缀中进行匹配,返回包含配置项的子集的字典

迭代当前对象,获取key和v,把key转换为小写格式,然后把key和v包含在一个字典中

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/649cf92f761fe7aa627705494ff4e734.html