python生产环境禁用assert断言的方法

目录
  • 1. 背景
  • 2.解决方案
    • 2.1 禁用assert的策略
    • 2.2 禁用的原理
  • 3. 实施禁用策略
    • 3.1 启动命令行的参数中,添加-O
    • 3.2 设置PYTHONOPTIMIZE环境变量
  • 4 使用断言的坑

1. 背景

在潜意识中, assert 是应用在unittest或pytest环境中, 不能应用到业务代码中, 因为断言会导致运行中断,对业务有损,并且消耗内存, 影响性能.

但不可否认, 使用断言非常方便调试代码

通过研读assert的文档, 发现断言是可以被关闭的,特此记录下

详细的介绍参考这个文档: 参考这篇文档

2.解决方案

2.1 禁用assert的策略

有两种方案

  1. 启动命令行的参数中,添加-O, 也就是大写的o
  2. 设置PYTHONOPTIMIZE环境变量为合适的值

2.2 禁用的原理

在python 内部机制中, 有一常量__debug__,值为BOOL类型的, 与断言是强关联的, 因为__debug__是常量, 所以当python 解释器运行时, 不能修改它的值.

Python 3.8.13 (default, Mar 28 2022, 06:16:26)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> __debug__
True
>>> __debug__ = False
  File "<stdin>", line 1
SyntaxError: cannot assign to __debug__
>>>

既然运行时不能修改, 那没有运行时可以修改吗?当然可以的, 这取决于python 运行模式,python运行模式有两种,和__debug__的关系如下表所示:

Mode Value of debug
Normal (or debug)(调试模式) True
Optimized(优化模式) False

那__debug__的值和断言有什么关系呢?

看源码:

if __debug__:
    if not expression:
        raise AssertionError(assertion_message)

所以__debug__==True时会抛出断言异常, __debug__==False时就啥也不干,跳过了

这下清晰了

3. 实施禁用策略

3.1 启动命令行的参数中,添加-O

验证方案:

1、先写这样一个测试脚本,命名为demo.py

# demo.py
print(f"__debug__=={__debug__}"

2、在命令行使用 禁用断言的启动方式

python -O demo.py

输出结果

__debug__==False

辅助理解: 在命令行中运行python -h,其结果也体现了这样的使用方式

mac$ python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)
         and comparing bytes/bytearray with str. (-bb: issue errors)
-B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I     : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O     : remove assert and __debug__-dependent statements; add .opt-1 before
         .pyc extension; also PYTHONOPTIMIZE=x
-OO    : do -O changes and also discard docstrings; add .opt-2 before
         .pyc extension
-q     : don't print version and copyright messages on interactive startup
-s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S     : don't imply 'import site' on initialization
-u     : force the stdout and stderr streams to be unbuffered;
         this option has no effect on stdin; also PYTHONUNBUFFERED=x
-v     : verbose (trace import statements); also PYTHONVERBOSE=x
         can be supplied multiple times to increase verbosity
-V     : print the Python version number and exit (also --version)
         when given twice, print more information about the build
-W arg : warning control; arg is action:message:category:module:lineno
         also PYTHONWARNINGS=arg
-x     : skip first line of source, allowing use of non-Unix forms of #!cmd
-X opt : set implementation-specific option. The following options are available:

         -X faulthandler: enable faulthandler
         -X showrefcount: output the total reference count and number of used
             memory blocks when the program finishes or after each statement in the
             interactive interpreter. This only works on debug builds
         -X tracemalloc: start tracing Python memory allocations using the
             tracemalloc module. By default, only the most recent frame is stored in a
             traceback of a trace. Use -X tracemalloc=NFRAME to start tracing with a
             traceback limit of NFRAME frames
         -X showalloccount: output the total count of allocated objects for each
             type when the program finishes. This only works when Python was built with
             COUNT_ALLOCS defined
         -X importtime: show how long each import takes. It shows module name,
             cumulative time (including nested imports) and self time (excluding
             nested imports). Note that its output may be broken in multi-threaded
             application. Typical usage is python3 -X importtime -c 'import asyncio'
         -X dev: enable CPython's "development mode", introducing additional runtime
             checks which are too expensive to be enabled by default. Effect of the
             developer mode:
                * Add default warning filter, as -W default
                * Install debug hooks on memory allocators: see the PyMem_SetupDebugHooks() C function
                * Enable the faulthandler module to dump the Python traceback on a crash
                * Enable asyncio debug mode
                * Set the dev_mode attribute of sys.flags to True
                * io.IOBase destructor logs close() exceptions
         -X utf8: enable UTF-8 mode for operating system interfaces, overriding the default
             locale-aware mode. -X utf8=0 explicitly disables UTF-8 mode (even when it would
             otherwise activate automatically)
         -X pycache_prefix=PATH: enable writing .pyc files to a parallel tree rooted at the
             given directory instead of to the code tree

--check-hash-based-pycs always|default|never:
    control how Python invalidates hash-based .pyc files
file   : program read from script file
-      : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]

Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
PYTHONPATH   : ':'-separated list of directories prefixed to the
               default module search path.  The result is sys.path.
PYTHONHOME   : alternate <prefix> directory (or <prefix>:<exec_prefix>).
               The default module search path uses <prefix>/lib/pythonX.X.
PYTHONCASEOK : ignore case in 'import' statements (Windows).
PYTHONUTF8: if set to 1, enable the UTF-8 mode.
PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.
PYTHONHASHSEED: if this variable is set to 'random', a random value is used
   to seed the hashes of str and bytes objects.  It can also be set to an
   integer in the range [0,4294967295] to get hash values with a
   predictable seed.
PYTHONMALLOC: set the Python memory allocators and/or install debug hooks
   on Python memory allocators. Use PYTHONMALLOC=debug to install debug
   hooks.
PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale
   coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of
   locale coercion and locale compatibility warnings on stderr.
PYTHONBREAKPOINT: if this variable is set to 0, it disables the default
   debugger. It can be set to the callable of your debugger of choice.
PYTHONDEVMODE: enable the development mode.
PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.

5: 新的需求: 怎么在命令行中禁用断言,看下面的对比代码:

最关键是修改__debug__的值

(sse38) lzh mac$ python
Python 3.8.13 (default, Mar 28 2022, 06:16:26)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> __debug__
True
>>> exit()
(sse38) lzh mac$ python -O
Python 3.8.13 (default, Mar 28 2022, 06:16:26)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> __debug__
False
>>>

3.2 设置PYTHONOPTIMIZE环境变量

这部分是在命令行中操作的

mac or linux 系统中,

(sse38)  mac$ export PYTHONOPTIMIZE=0
(sse38)  mac$ python demo.py
True
(sse38)  mac$ export PYTHONOPTIMIZE=1
(sse38)  mac$ python demo.py
False
(sse38)  mac$ export PYTHONOPTIMIZE=2
(sse38)  mac$ python demo.py
False

window 系统中,

(sse38)  mac$ set PYTHONOPTIMIZE=0
(sse38)  mac$ python demo.py
True
(sse38)  mac$ set PYTHONOPTIMIZE=1
(sse38)  mac$ python demo.py
False
(sse38)  mac$ set PYTHONOPTIMIZE=2
(sse38)  mac$ python demo.py
False

4 使用断言的坑

处理或验证数据

处理异常和异常

运行常有副作用的操作

到此这篇关于python生产环境禁用assert断言的文章就介绍到这了,更多相关python禁用assert断言内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • python3 assert 断言的使用详解 (区别于python2)

    python3 和python以前的版本有点不同 如果你断言的 语句正确 则什么反应都没有 但是如果你出错之后 就会报出 AssertionError 并且错误可以自己填写 格式 : assert+空格+要判断语句+双引号"报错语句" 例子: 出错时候 assert 1>5, "chucuo" 输出值为: --------------------------------------------------------------------------- As

  • Python中断言Assertion的一些改进方案

    Python Assert 为何不尽如人意? Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛出异常. >>> assert 1 + 1 == 2 >>> assert isinstance('Hello', str) >>> assert isinstance('Hello', int) Traceback (most recent call last): File "<input>

  • Python3 assert断言实现原理解析

    语法格式如下: assert expression 等价于: if not expression: raise AssertionError assert 后面也可以紧跟参数: assert expression [, arguments] 等价于: if not expression: raise AssertionError(arguments) 以下为 assert 使用实例: >>> assert True # 条件为 true 正常执行 >>> assert

  • python中的断言(assert语句)

    目录 python断言assert语句 assert:python断言报错语句 1.设置assert报错语句 2.assert a,b python断言assert语句 assert语句的格式是[assert 表达式,返回数据],当表达式为False时则触发AssertionError异常 try: n=input("请输入一个数字:") assert n.isdigit(),"只能输入数字" print("你输入的是:",n) except E

  • Python中优雅使用assert断言的方法实例

    目录 什么是 assert 断言 断言和异常的使用场景 使用断言的几个原则 建议不使用断言的情况: 总结 什么是 assert 断言 Assert statements are a convenient way to insert debugging assertions into a program 断言声明是用于程序调试的一个便捷方式.断言可以看做是一个 debug 工具,Python 的实现也符合这个设计哲学,在 Python 中 assert 语句的执行是依赖于__debug__这个内置

  • python assert断言的实例用法

    断言声明是一种方便的程序调试方式. 1.可将断言视为debug工具,Python的实现也符合这种设计理念.assert语句的执行依赖于__debug__,且默认值为True. 2.如果__debug__为True,则仅执行assert语句. 实例 assert 可以同时声明两个 expression,例如 assert expression1, expression2 等价于 if __debug__: if not expression1: raise AssertionError(expre

  • Python断言assert的用法代码解析

    在开发一个程序时候,与其让它运行时崩溃,不如在它出现错误条件时就崩溃(返回错误).这时候断言assert 就显得非常有用. python assert断言是声明布尔值必须为真的判定,如果发生异常就说明表达式为假. 可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常. assert的语法格式: assert expression 它的等价语句为: if not expression: raise AssertionError 这段代码用来检测数据类型

  • python生产环境禁用assert断言的方法

    目录 1. 背景 2.解决方案 2.1 禁用assert的策略 2.2 禁用的原理 3. 实施禁用策略 3.1 启动命令行的参数中,添加-O 3.2 设置PYTHONOPTIMIZE环境变量 4 使用断言的坑 1. 背景 在潜意识中, assert 是应用在unittest或pytest环境中, 不能应用到业务代码中, 因为断言会导致运行中断,对业务有损,并且消耗内存, 影响性能. 但不可否认, 使用断言非常方便调试代码 通过研读assert的文档, 发现断言是可以被关闭的,特此记录下 详细的介

  • SpringMVC如何在生产环境禁用Swagger的方法

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步. Swagger 让部署管理和使用功能强大的API从未如此简单.好吧,以上是官方的说法,我直接复制的,在我看来swagger就是一个接口文档管理器,以前我们写接口一般都是world编写,但是有一个问题就是测试的时候需要依赖第三方工具,GET的接口还好,直接浏览

  • 一些Centos Python 生产环境的部署命令(推荐)

    Just notes 拿到一台干净的centos之后, 初始化Python环境, 一些命令和问题记录而已 可以搞成脚本自动初始化, 当然, 用docker更好 基础环境 1. 创建用户 sudo adduser newuser sudo passwd newuser # 设置授权不需要输入密码 sudo /usr/sbin/visudo newuser ALL=NOPASSWD: ALL 2. EPEL(Fedora Extra Packages for Enterprise Linux rep

  • vue中控制mock在开发环境使用,在生产环境禁用方式

    目录 vue控制mock在开发环境使用,在生产环境禁用 说下原因 解决方案 vue中使用mock(常用方式) 前期准备 安装axios和mock.js插件 在main.js中引入 编写mock.js 调用 成功 vue控制mock在开发环境使用,在生产环境禁用 说下原因 mock拦截所有的axios请求,根据请求,做出相应的响应.平时前后端分离开发,我们使用mock获得相应的数据,但当和后端联调的时候,不禁用mock,就无法获得后端数据. 解决方案 第一步.我们设置mock在开发developm

  • 详解为生产环境编译Angular2应用的方法

    Angular 2 已经发布了 2.1.2 版本, 相信很多人已经在使用(试用)了, 相比 AngularJS 1.x , Angular 2 在性能上有了长足的进步, 同时 Angular 2 也变得非常的庞大, 动辄几兆的脚本, 如何部署到生产环境? 接下来就介绍如何为生产环境编译 Angular 2 应用, 在本文中, 我们将 Angular 2 官方文档中的 Hello Angular 应用编译到 50K 以下, 以用于生产环境. 未经优化的应用 根据 Angular2 官方的 Quic

  • Mac中Python 3环境下安装scrapy的方法教程

    前言 最近抽空想学习一下python的爬虫框架scrapy,在mac下安装的时候遇到了问题,逐一解决了问题,分享一下,话不多说了,来一起看看详细的介绍吧. 步骤如下: 1. 从官网 下载最新版本Python 3.6.3(本地快速下载安装:http://www.jb51.net/softs/583651.html) # 在Mac上Python3环境下安装scrapy 2. 安装 Python3 在终端输入python3出现下面的内容表示安装成功 ➜ ~ python3 Python 3.6.3 (

  • python设置环境变量的原因和方法

    相信很多初学python的小伙伴都会遇到这样的坑:环境变量配置不好,无法正常启动python.那么环境变量究竟是个什么东西呢?为什么要设置它?下面我们来说一说. 1.什么是环境变量 引用百度百科里面的解释:环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.看到这里我相信大家可能还是有所疑惑,但是,不急,接着看. 2.为什么需要环境变量 windows系统下,假如我们安装了某一款软件,安装结束后,在安装目录会生成一个该软件的.exe文件,双击该文件,我们就

  • C++ Assert()断言机制原理以及使用方法

    MSDN原文如是说: Evaluates an expression and, when the result is false, prints a diagnostic message and aborts the program. (判断一个表达式,如果结果为假,输出诊断消息并中止程序.) void assert( int expression ); 参数:Expression (including pointers) that evaluates to nonzero or 0.(表达式[

  • SpringBoot在生产快速禁用Swagger2的方法步骤

    你还在生产节点开放Swagger吗,赶紧停止这种暴露接口的行为吧. 学习目标 快速学会使用注解关闭Swagger2,避免接口重复暴露. 使用教程 禁用方法1:使用注解@Profile({"dev","test"}) 表示在开发或测试环境开启,而在生产关闭.(推荐使用) 禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable", havingValue = "true") 

随机推荐