python Debug宏定义

2019-03-04  本文已影响0人  明燕南飞

前言

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

步骤:

# -*- 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()
# -*- 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)

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

上一篇下一篇

猜你喜欢

热点阅读