前言
調試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一致