美文网首页
python Debug宏定义

python Debug宏定义

作者: 明燕南飞 | 来源:发表于2019-03-04 17:22 被阅读0次

前言

调试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一致

相关文章

网友评论

      本文标题:python Debug宏定义

      本文链接:https://www.haomeiwen.com/subject/uggiuqtx.html