如何编写详细脚本 [英] How to write verbose scripts

查看:60
本文介绍了如何编写详细脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己在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 to
include "verbose" output to stdout.

I start with a helper function:
def print_(obj, level=0):
if _verbosity >= level:
print obj
And 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 something
That often means that my functions end up with more message printing code
than actual code. The whole thing seems messy and hard to manage for all
but the smallest scripts.

Worst of all, sometimes the messages I wish to print may be expensive to
compute, and I don''t want to waste time computing them if they aren''t
going to be printed because the verbosity is too low. But nor do I wish
to 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

推荐答案

2008年9月2日星期二12点:下午55点,Steven D''Aprano

< st *** @ remove-this-cybersource.com.auwrote:
On Tue, Sep 2, 2008 at 12:55 PM, Steven D''Aprano
<st***@remove-this-cybersource.com.auwrote:

Is这样做的方式比我的方式更好吗?
Is there a better way of doing this than the way I am going about it?



记录模块是否有帮助,只是将输出打印到stdout

(或文件)?

Would the logging module help, and just print the output to the stdout
(or a file) instead?


9月2日上午11:55,Steven D''Aprano< st ... @ REMOVE-THIS-

cybersource.com.auwrote:
On Sep 2, 11:55 am, Steven D''Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:

我发现自己在Python中编写命令行工具,我希望

包括详细输出到标准输出。


我从辅助函数开始:


def print_(obj,level = 0):

if _verbosity> = level:

print obj


然后我最终得到的函数或方法如下:
< br $>
def parrot(x)

print _(" precondition",level = 2)

do_something()

print _(状态好......,等级= 1)

打印_(parrot现在强烈嘎嘎叫,等级= 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 to
include "verbose" output to stdout.

I start with a helper function:

def print_(obj, level=0):
if _verbosity >= level:
print obj

And 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 something

That often means that my functions end up with more message printing code
than actual code. The whole thing seems messy and hard to manage for all
but the smallest scripts.

Worst of all, sometimes the messages I wish to print may be expensive to
compute, and I don''t want to waste time computing them if they aren''t
going to be printed because the verbosity is too low. But nor do I wish
to 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?



我做这样的事情,虽然我不知道它是否会改善




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 would
be an improvement.

def collatz(a,p):
""" 3x+1 sequencer, no loop detection

collatz(a,p)
a: starting value
p: print options (binary)
bit 0 print even numbers (turns off power division)
bit 1 print odd numbers
bit 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 = 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

while done==0:
f = gmpy.scan1(a,0) # locate LS 1-bit
if f>0: # it''s even
if print_evens:
print a,
a = a >1 # no power division
u += 1
else:
a = a >f # power division
u += f
else:
if print_odds:
print a,
if a==1:
done = 1
seq_end = t + u
else:
a = a*TWE + ONE
t += 1
return [u,t]


>

-

史蒂文
>
--
Steven


Steven D''Aprano schrieb:
Steven D''Aprano schrieb:

我发现自己在Python中编写命令行工具我希望

包括详细输出到标准输出。


我从辅助函数开始:


def print_(obj,level = 0):

if _verbosity> = level:

print obj


然后我最终得到的函数或方法如下:


def parrot(x)

print _(" precondition",level = 2)

do_something()

print _(状态好......,等级= 1)

打印_(parrot现在强烈嘎嘎叫,等级= 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 to
include "verbose" output to stdout.

I start with a helper function:
def print_(obj, level=0):
if _verbosity >= level:
print obj
And 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 something
That often means that my functions end up with more message printing code
than actual code. The whole thing seems messy and hard to manage for all
but the smallest scripts.

Worst of all, sometimes the messages I wish to print may be expensive to
compute, and I don''t want to waste time computing them if they aren''t
going to be printed because the verbosity is too low. But nor do I wish
to 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?



我使用日志模块。


关于昂贵的计算:像这样的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字符串只有当记录级别为实际推出的
时才进行插值。


Diez

I use the logging-module.

Regarding the expensive computations: maysomething like this help:

class DeferredToString(object):

def __init__(self, func):
self.func = func

def __repr__(self):
return repr(self.func())

def __str__(self):
return str(self.func())

dts = DeferredToString

Because then you can do

logger.debug("Some text for an: %r", dts(lambda: long_computation()))

Because AFAIK the string is only interpolated if the logging level is
actually put out.

Diez


这篇关于如何编写详细脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆