python Debug宏定义

前言

调试python时,常碰到打印信息需手动删除;且python没有宏定义.
依据之前使用C的习惯,定义调试模式,仅调试模式下才打印调试信息.

步骤:

  • 1.增添const.py
# -*- coding: utf-8 -*-

import sys

class _const:

    class ConstError(TypeError):

        pass

    class ConstCaseError(ConstError):

        pass

    def __setattr__(self, name, value):

        if name in self.__dict__:

            raise self.ConstError("Can't change const.%s" % name)

        if not name.isupper():

            raise self.ConstCaseError(

                "const name '%s' is not all uppercase" % name)

        self.__dict__[name] = value

    def __delattr__(self, name):

        if name in self.__dict__:

            raise self.ConstError("can't unbind const(%s)" % name)

        raise NameError(name)

sys.modules[__name__] = _const()
  • 2.在python常量定义文件jmeterConst.py中添加const.DEBUG和添加 函数DEBUG_PRINT
# -*- coding: utf-8 -*-

import const

#======================================

#debug print

const.DEBUG=1

不需要打印是只需将const.DEBUG=1 改成const.DEBUG=0

#======================================

def DEBUG_PRINT(*kwargs):

    if(const.DEBUG):

        print(*kwargs)
  • 3.在其他文件中调用DEBUG_PRINT

导入from jmeterConst import DEBUG_PRINT as DEBUG_PRINT
直接调用DEBUG_PRINT,参数格式与print一致

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容