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第三个知识点:
这个暂时放下
flask config笔记的更多相关文章
- Python Flask学习笔记之模板
Python Flask学习笔记之模板 Jinja2模板引擎 默认情况下,Flask在程序文件夹中的templates子文件夹中寻找模板.Flask提供的render_template函数把Jinja ...
- Python Flask学习笔记之Hello World
Python Flask学习笔记之Hello World 安装virtualenv,配置Flask开发环境 virtualenv 虚拟环境是Python解释器的一个私有副本,在这个环境中可以安装私有包 ...
- Flask之笔记集合
目录 一.简述 二.基本使用 三.配置文件 四.路由系统 2.自定义正则路由 五.模版语言 六.请求和响应 七.Session 2.自定义session 八.蓝图 九.message 十.中间件 十一 ...
- Flask入门笔记(一)
一.程序的基本结构 1.1 最简单的Flask程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #coding=utf-8 # 初始化 from flask import Fla ...
- Flask项目笔记
一.jsonify jsonify 是flask的函数,可以将字典转换成json数据返回给浏览器二. 钩子函数 @app.before_first_request:第一次请求调用,用于初始化数据 @ ...
- Flask学习笔记(2)--最简单的小应用
0x01 第一个小程序 PyCharm新建一个flask项目,第一个小程序,我们来看一下 #引入flask类 from flask import Flask #将Flask对象实例化 app = Fl ...
- flask config
# config配置 { 'DEBUG': False, # 是否开启Debug模式 'TESTING': False, # 是否开启测试模式 'PROPAGATE_EXCEPTIONS': None ...
- pythonweb框架Flask学习笔记05-简单登陆
源代码从下链接引用:https://www.cnblogs.com/felixwang2/p/9261493.html 我使用的是python3.6 在运行代码的时候遇到了以下问题 session[' ...
- flask开发笔记
目录 虚拟环境 Debug模式 配置文件 url传入参数 url反转 重定义向 模板 创建 jinjia2语法 模板继承 flash 加载静态文件 MySQL数据库命令 配置 更新.提交.删除 模型操 ...
随机推荐
- PHP设置Redis key在当天有效
本文援引自:https://blog.csdn.net/zpf_nevergiveup/article/details/81066500 $redis->set($key,$value); $e ...
- jquery 自定义右键菜单
如果要自定义右键菜单,那么就需要禁止原本的右键菜单,代码如下 document.oncontextmenu = new Function("return false;");//禁止 ...
- Redis——SpringBoot项目使用Lettuce和Jedis接入Redis集群
Jedis连接Redis: 非线程安全 如果是多线程环境下共用一个Jedis连接池,会产生线程安全问题,可以通过创建多个Jedis实例来解决,但是创建许多socket会影响性能,因此好一点的方法是使用 ...
- Rust学习-阶段1学习总结
学习Rust已经两周了,基本上是断断续续的在学,或者是在上下班坐公交时,或者是在ODC没事做时.现在已经学习了Rust程序设计语言的前5章,是时候做一个总结了.关于数据类型或者if else这种内容我 ...
- element-ui 弹出添加拖拽功能
1.新建dialog.js文件2.在main.js 中引入dialog.js import ‘./utils/dialog.js’3. 使用:<el-dialog v-dialogDrag&g ...
- TTTTTTTTTTTT CF 653D 送邮递员
链接:给一张n个点m条带权边的有向图,有x个人从起点出发到终点,每个人带的都带相同重量的货物, 规定一条边最多能经过其上权的重量的货物,问最多能带多重的货物? 2 ≤ n ≤ 50, 1 ≤ m ≤ ...
- 使用git将代码上传到GitHub
使用git将代码上传到GitHub 结束了前一段的时间的杂七杂八的生活,最近又快开始动一动已经吃了好长时间土的GitHub,以前的git指令基本上忘个差不多,现在记录一下,利用git将代码上传. ...
- Horizon7.9部署和克隆问题汇总
1 基础环境说明 采用Windows server +SQL Server 2014进行部署,对接现有环境中的AD预控,系统版本为Windows server .桌面虚拟化软件版本采用Horizon ...
- iOS13即将到来,iOS推送Device Token适配方案详解
关于提前适配iOS13 苹果推送DeviceToken的通知 随着苹果iOS13系统即将发布,个推提前推出DeviceToken适配方案,以确保新版本的兼容与APP推送服务的正常使用.iOS13的一个 ...
- Js基础知识(二) - 原型链与继承精彩的讲解
作用域.原型链.继承与闭包详解 注意:本章讲的是在es6之前的原型链与继承.es6引入了类的概念,只是在写法上有所不同,原理是一样的. 几个面试常问的几个问题,你是否知道 instanceof的原理 ...



文件的绝对路径