在sqlalchemy中关闭警告 [英] Turn off a warning in sqlalchemy

查看:101
本文介绍了在sqlalchemy中关闭警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带反射功能的sqlalchemy,数据库中的几个局部索引使其像这样转储警告:

I'm using sqlalchemy with reflection, a couple of partial indices in my DB make it dump warnings like this:

SAWarning:谓词在反射过程中忽略的部分索引i_some_index

进入我的日志并保持混乱。它不妨碍我的应用程序行为。我想在开发时保留这些警告,但不在生产级别。有人知道如何关闭它吗?

into my logs and keep cluttering. It does not hinder my application behavior. I would like to keep these warnings while developing, but not at production level. Does anyone know how to turn this off?

推荐答案

Python的警告模块提供了方便的上下文管理器会为您捕获警告。

Python's warning module provides a handy context manager that catches warnings for you.

以下是过滤SQLAlchemy警告的方法。

Here's how to filter out the SQLAlchemy warning.

import warnings
from sqlalchemy import exc as sa_exc

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=sa_exc.SAWarning)
    # code here...

对于开发还是生产,您可以仅在应用程序的入口点或在生产环境中调用您的应用程序的外部脚本中使用此警告包装。

As for development vs production, you can just have this warning wrap around your application's entry point or an external script that invokes your application in your production environment.

通常,我通过使用环境变量来解决此问题,该环境变量执行的代码路径与开发时不同,例如,包装不同的中间件等。

Usually, I solve this by having an environment variable that executes a slightly different code path than when developing, for example, wrapping around different middleware etc.

这篇关于在sqlalchemy中关闭警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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