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

查看:44
本文介绍了关闭 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 的 warning 模块 提供了一个方便的上下文管理器为您捕捉警告.

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天全站免登陆