本文介绍了如何编写详细脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我发现自己在Python中编写命令行工具,我希望 包括详细输出到标准输出。 我从辅助函数开始: def print_(obj,level = 0): if _verbosity> =等级: 打印对象 然后我最终得到的函数或方法如下: def parrot(x) print _(" precondition",level = 2) do_something() print _(" status is good ..." ,等级= 1) 打印_(鹦鹉现在强烈嘎嘎叫,等级= 2) do_something_else() print _(" ; squawk squawk squawk",level = 3) do_more() print _(" postcondition",level = 1) 返回一些东西 这通常意味着我的函数最终会有比实际代码更多的消息打印代码 。整个事情似乎很乱,很难管理所有 但是最小的脚本。 最糟糕的是,有时候我想要打印的信息可能很贵要计算,我不想浪费时间计算它们,如果它们不打算打印,因为冗长度太低了。但我也不希望 用这个来填充我的代码: 如果_verbosity> = 3: x = calculate_complicated_thing( ) print_(x,level = 3) 有没有更好的方式来做这件事而不是我的方式呢? - 史蒂文I find myself writing command line tools in Python where I wish toinclude "verbose" output to stdout.I start with a helper function:def print_(obj, level=0):if _verbosity >= level:print objAnd then I end up with functions or methods looking like this:def parrot(x)print_("precondition", level=2)do_something()print_("status is good...", level=1)print_("parrot is squawking strongly now", level=2)do_something_else()print_("squawk squawk squawk", level=3)do_more()print_("postcondition", level=1)return somethingThat often means that my functions end up with more message printing codethan actual code. The whole thing seems messy and hard to manage for allbut the smallest scripts.Worst of all, sometimes the messages I wish to print may be expensive tocompute, and I don''t want to waste time computing them if they aren''tgoing to be printed because the verbosity is too low. But nor do I wishto fill my code with this:if _verbosity >= 3:x = calculate_complicated_thing()print_(x, level=3)Is there a better way of doing this than the way I am going about it?--Steven推荐答案 记录模块是否有帮助,只是将输出打印到stdout (或文件)?Would the logging module help, and just print the output to the stdout(or a file) instead? 我做这样的事情,虽然我不知道它是否会改善 。 def collat​​z(a,p): """ 3x + 1音序器,无环路检测 collat​​z(a,p) a:起始值 p:打印选项(二进制) ) 位0打印偶数(关闭功率分配) 位1打印奇数 位2打印调试(如果有的话) 返回:Collat​​zSequenceParameters [R1count,R2count] """ ONE = gmpy.mpz(1) TWO = gmpy.mpz(2) TWE = gmpy.mpz(3) a = gmpy.mpz(a) t = 0 u = 0 done = 0 if(p& 1)== 1: print_evens = True else: print_evens = False if(p& 2)== 2: print_odds = True else: print_odds = False if(p& 4)== 4: print_debug = True else: print_debug = False 完成时== 0: f = gmpy.scan1(a,0)#locate LS 1-bit 如果f> 0:#它甚至是 如果print_evens: 打印a, a = a> 1#没有电力部门 u + = 1 else: a = a> f#power division u + = f else: 如果print_odds: 打印a, 如果a == 1: done = 1 seq_end = t + u 否则: a = a * TWE + ONE t + = 1 返回[u,t ]I do something like this, although I don''t know if it wouldbe an improvement.def collatz(a,p):""" 3x+1 sequencer, no loop detectioncollatz(a,p)a: starting valuep: print options (binary)bit 0 print even numbers (turns off power division)bit 1 print odd numbersbit 2 print debug (if any)returns: CollatzSequenceParameters [R1count, R2count]"""ONE = gmpy.mpz(1)TWO = gmpy.mpz(2)TWE = gmpy.mpz(3)a = gmpy.mpz(a)t = 0u = 0done = 0if (p & 1)==1:print_evens = Trueelse:print_evens = Falseif (p & 2)==2:print_odds = Trueelse:print_odds = Falseif (p & 4)==4:print_debug = Trueelse:print_debug = Falsewhile done==0:f = gmpy.scan1(a,0) # locate LS 1-bitif f>0: # it''s evenif print_evens:print a,a = a >1 # no power divisionu += 1else:a = a >f # power divisionu += felse:if print_odds:print a,if a==1:done = 1seq_end = t + uelse:a = a*TWE + ONEt += 1return [u,t] 我使用日志模块。 关于昂贵的计算:像这样的maysomething帮助: class DeferredToString(object): def __init __(self,func): self.func = func def __repr __(自我): return repr(self.func()) def __str __(self): 返回str(self.func()) dts = DeferredToString 因为那样你就可以做到/> logger.debug("一些文字为:%r",dts(lambda:long_computation())) 因为AFAIK字符串只有当记录级别为实际推出的时才进行插值。 DiezI use the logging-module.Regarding the expensive computations: maysomething like this help:class DeferredToString(object):def __init__(self, func):self.func = funcdef __repr__(self):return repr(self.func())def __str__(self):return str(self.func())dts = DeferredToStringBecause then you can dologger.debug("Some text for an: %r", dts(lambda: long_computation()))Because AFAIK the string is only interpolated if the logging level isactually put out.Diez 这篇关于如何编写详细脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 23:05