我正在使用prospector来检查我的代码.Pylint返回了logging-not-lazy有关我的调试消息的警告.
Line: 31
pylint: logging-not-lazy / Specify string format arguments as logging function parameters (col 16) Line: 42
pylint: logging-not-lazy / Specify string format arguments as logging function parameters (col 12)
Run Code Online (Sandbox Code Playgroud)
我的代码是:
logging.debug("detect mimetypes faild because %s" % e )
Run Code Online (Sandbox Code Playgroud)
我如何修复logging-not-lazypylint?
我在我的python应用程序中使用标准的python日志记录模块:
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("log")
while True:
logger.debug('Stupid log message " + ' '.join([str(i) for i in range(20)]) )
# Do something
问题是,虽然调试级别不启用,那个愚蠢的日志消息是在每次循环迭代,这严重损害了性能评估.
这有什么解决方案吗?
在C++中,我们有log4cxx一个提供这样的宏的包:
LOG4CXX_DEBUG(logger, messasage)
有效地评估为
if (log4cxx::debugEnabled(logger)) {
log4cxx.log(logger,log4cxx::LOG4CXX_DEBUG, message)
}
但是由于Python(AFAIK)中没有宏,是否有一种有效的记录方法?
我真的很想深入研究代码风格,从现在开始知道从现在开始使用新风格会更好.
我.format()在我的Python 3.5项目中使用了很多,并且我担心在下一个Python版本中它会被弃用,因为这种新的字符串文字.
>>> name = "Test"
>>> f"My app name is {name}."
'My app name is Test.'
Run Code Online (Sandbox Code Playgroud)
格式化的字符串功能是否完全取代旧的format()?
我理解它基于这样的想法:
简单比复杂更好.
但是,性能问题如何,它们之间是否存在差异?或者它只是一个简单的相同功能的外观?
我想比较不同以在Python中用不同的变量构建一个字符串:
+来连接(被称为"加号")%"".join(list)format功能"{0.<attribute>}".format(object)我比较了3种类型的情景
我每次测量100万次操作并且平均执行超过6次测量.我想出了以下时间:
在每个场景中,我得出以下结论
%比使用format函数格式化要快得多我相信format比%(例如在这个问题中)要好得多,并且%几乎被弃用了.
因此,我有几个问题:
%真的比快format?"{} {}".format(var1, var2)效率更高"{0.attribute1} {0.attribute2}".format(object)?作为参考,我使用以下代码来测量不同的时序.
import time
def timing(f, n, show, *args):
if show: print f.__name__ + ":\t",
r = range(n/10)
t1 = time.clock()
for i in r:
f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); …Run Code Online (Sandbox Code Playgroud)