python Debug宏定義

前言

調試python時,常碰到打印信息需手動刪除;且python沒有宏定義.
依據(jù)之前使用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和添加 函數(shù)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,參數(shù)格式與print一致

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容