展开werkzeug useragent类 [英] expand werkzeug useragent class

查看:159
本文介绍了展开werkzeug useragent类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个浏览器扩展werkzeug UserAgent类。我怎样才能不修改werkzeug库的源代码?我是新来的python,所以我有关于mixin,继承,模块等的小想法。
我在文档中发现:

lockquote

创建BaseRequest的自定义子类并添加缺少的功能是个好主意通过mixin或直接执行。这里是这样一个子类的例子:



  from werkzeug.wrappers import BaseRequest,ETagRequestMixin 
class Request(BaseRequest,ETagRequestMixin):
pass

我把这个和如何扩展标准的UserAgent类?另外我用烧瓶与werkzeug。提前致谢。

解决方案

惊人的。一旦你提出问题,答案就会出现:)



     from werkzeug.wrappers import BaseRequest,ETagRequestMixin 
$ b $ class Request(BaseRequest,ETagRequestMixin):
pass
$ b $ app = Flask(__ name__)
app.request_class =请求


I want to expand werkzeug UserAgent class with one more browser. How can I do it without modifying the source code of werkzeug library? I'm new in python so I have small idea about mixins, inheritance, modules and so on. I've found in docs:

It’s a good idea to create a custom subclass of the BaseRequest and add missing functionality either via mixins or direct implementation. Here an example for such subclasses:

from werkzeug.wrappers import BaseRequest, ETagRequestMixin
class Request(BaseRequest, ETagRequestMixin):
    pass

At which part of my code should I put this and how to expand standart UserAgent class? Also I'm using werkzeug with Flask. Thanks in advance.

Amazing. Once you ask question the answer comes up itself :)

Flask.request_class is the answer:

from werkzeug.wrappers import BaseRequest, ETagRequestMixin

class Request(BaseRequest, ETagRequestMixin): 
    pass

app = Flask(__name__)
app.request_class = Request

这篇关于展开werkzeug useragent类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆