在sqlalchemy中关闭警告

编程入门 行业动态 更新时间:2024-10-09 13:30:45
本文介绍了在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中关闭警告

本文发布于:2023-11-13 06:13:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:sqlalchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!