flask学习:如何从config里载入配置
代码如下:
1.main.py
from flask import Flask
from config import DevConfig app=Flask(__name__) app.config.from_object(DevConfig) @app.route('/')
def home():
return "<h1>Hello Wcf!</h1>" if __name__=='__main__':
app.run()
2.config.py
class Config(object):
pass class ProdConfig(Config):
pass class DevConfig(Config):
DEBUG=True
想了解一下,app.config.from_boject...是如何运作的,跟踪到源代码中:
在flask的config.py中,有一个方法,是:
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):
if key.isupper():
self[key] = getattr(obj, key)
这里,用到了一个dir的python自带的函数,是其代码的关键,那么,dir是干嘛用的么?
在python中任何东西都是对像,一种数据类型,一个模块等,都有自己的属性和方法,除了常用方法外,其它的你不需要全部记住它,交给dir()函数就好了。
dir()函数操作方法很简单,只需要把你想要查询和对像添写到( )括号中就可以使用了。
flask学习:如何从config里载入配置的更多相关文章
- C# 类库下 读取不到config里节点的问题
前提:要在当前项目下App.config配置信息里读取连接数据库的字符串.忘记创的是个类库.导致一直报错(对象未实例) 解决办法: 程序运行不会读取本项目下的app.config文件(实际它只是个模板 ...
- 在web.config里使用configSource分隔各类配置
转:http://www.yongfa365.com/Item/using-configSource-Split-Configs.html 大型项目中,可能有多个Service,也就是会有一堆配置,而 ...
- 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数
1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...
- 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- Flask 学习篇二:学习Flask过程中的记录
Flask学习笔记: GitHub上面的Flask实践项目 https://github.com/SilentCC/FlaskWeb 1.Application and Request Context ...
- Flask学习之五 用户登录
英文博客地址:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins 中文翻译地址:http:// ...
- [ZHUAN]Flask学习记录之Flask-SQLAlchemy
From: http://www.cnblogs.com/agmcs/p/4445583.html 各种查询方式:http://www.360doc.com/content/12/0608/11/93 ...
- Flask学习【第2篇】:Flask基础
知识点回顾 flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 实例化Flask对象,里面是有参数的 app = Flask(__name__,template_ ...
- 无用之flask学习
一.认识flask 1.短小精悍.可扩展性强 的一个web框架 注意:上下文管理机制 2.依赖wsgi:werkzurg from werkzeug.wrappers import Request, ...
随机推荐
- Java 端口扫描器 TCP的实现方法
想必很多朋友都实现过一个简易的聊天室这个功能,其中涉及到Socket套接字这个类,我们通过一个特定的IP以及特定的端口创建一个服务端的套接字(ServerSocket),以此我们聊天个体的套接字(So ...
- lintcode-125-背包问题 II
125-背包问题 II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分. ...
- 【历史】- .NET之父 - Anders Hejlsberg
简介 安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi和.NET之父! 安德斯·海尔斯伯格曾在丹麦技术大学学习工 ...
- (转)mongdb性能优化收集
一.数据库最大连接数问题当你在后台日志中,发现大量“connection refused because too many open connections: 819”信息时,一般跟你没有设置合适的最 ...
- QThread中的互斥、读写锁、信号量、条件变量
该文出自:http://www.civilnet.cn/bbs/browse.php?topicno=78431 在gemfield的<从pthread到QThread>一文中我们了解了线 ...
- UDP收/发广播包原理及步骤
原文链接地址:http://www.2cto.com/net/201311/254834.html UDP收/发广播包原理及步骤 如果网络中两个主机上的应用程序要相互通信,其一要知道彼此的IP,其二要 ...
- CSS3中transform属性的用法
有时候网站也要愚弄一下访客,比如愚人节.下面我给大家推荐个效果,就是整个页面左右颠倒了.css3 很强大,简单的几行代码就可以帮我们实现这个效果. view source print? 01 &l ...
- boost 文件操作
void testFileSystem() { boost::filesystem::path path("/test/test1"); //初始化 boost::filesyst ...
- HDU 多校对抗赛 J Time Zone
Time Zone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- __cdecl,__stdcall,__fastcall,__pascal,__thiscall 的区别
关于函数的调用规则(调用约定),大多数时候是不需要了解的,但是如果需要跨语言的编程,比如VC写的dll要delphi调用,则需要了解. microsoft的vc默认的是__cdecl方式,而windo ...