我们谈论的是烦恼 [英] While we're talking about annoyances

查看:65
本文介绍了我们谈论的是烦恼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是唯一一个发现我正在编写更多文档而不是代码的人吗?


我最近需要编写一个函数来从

列表。也就是说,一个排名列表,其中一个项目的排名是位置

如果列表被排序将会是这样的:


alist = list (''defabc'')

排名= [3,4,5,0,1,2]


要做到这一点,我需要生成首先是索引表。在书中

Pascal中的数字食谱作者:William Press等人有一个程序

来生成一个索引表(46行代码)和一个用于排名表

(五行)。


在Python中,我的索引函数是四行代码,我的等级函数是

五行。然后我写了三个函数来验证我的索引

和排名表是否正确计算(多17行)和另外四行

行调用doctest,总计30行代码。


我还有93行文档,包括doctests,或每行代码3行
行文档。


对于那些感兴趣的人,这里是如何生成一个索引表并在Python中排名

表:

def index(sequence):

decorated = zip(sequence,xrange(len(sequence)))

decorated.sort()

return [idx for(value,idx)装饰]


def rank(序列):

table = [None] * len(sequence)

for j,枚举中的idx(索引(序列)):

表[idx] = j

返回表格

您可以编写自己的该死的文档。 *眨眼*


-

史蒂文。

解决方案

< blockquote> Steven,


def index(sequence):

decorated = zip(sequence,xrange(len(sequence)))

decorated.sort()

返回[idx for(value,idx)in decorated]



will''那是等价的代码吗?


def索引(序列):

返回[c代表_,c代表排序((b,a)代表a, b in

枚举(序列))]


测试,得到相同的结果。但是恶化你的doc2code比率:)


Harald Armin Massa

-


GHUM写道:


史蒂文,


> def index(序列):
装饰= zip(序列,xrange(len(序列)))
...(

返回[装饰中的(值,idx)的idx]



不会是等价的代码吗?


def索引(序列):

返回[c代表_,c代表排序( (b,a)a,b in

枚举(序列))]



甚至这些:


def索引(序列):

返回排序(范围(len(序列)),key = sequence .__ getitem__)


def rank(sequence):

返回排序(范围(len(序列)),

key = index(sequence).__ getitem__)


提示:如果你发现自己使用了decorate-sort-undecorate模式,

已排序(key = func)或sequence.sort(key = func)可能是个更好的主意。

-

Michael Hoffman


2007年4月29日,Steven D''Aprano< st *** @ remove.this.cybersource.com.auwrote:


为此,我需要先生成一个索引表。在书中

Pascal中的数字食谱通过William Press等人有一个程序

来生成一个索引表(46行代码)和一个用于排名表

(五行)。



总共51行。


在Python中,我的索引函数是四行代码和我的等级功能是

五行。然后我写了三个函数来验证我的索引

和排名表是否正确计算(多17行)和另外四行

行调用doctest,总计30行代码。



因此Python的9行,不包括测试。


我还有93行文档,包括doctests,或每行代码3行
行文档。



然后,在没有文档的情况下,Python大约有560%(51/9)与Pascal一样高效。但是有了文档(假设你需要与Pascal代码相同的Python代码文档数量),

(51 + 93)/(9 + 93)= 1.41所以只有141%的效率与Pascal一样。


我想知道这意味着什么?也许Python的语言接近

上限,命令式编程语言的效率是多少?b $ b b?另一方面,似乎有一些进步可以减少编写文档的工作量。

世界语中的文档可能不是英文?


-

mvh Bj?


Am I the only one who finds that I''m writing more documentation than code?

I recently needed to write a function to generate a rank table from a
list. That is, a list of ranks, where the rank of an item is the position
it would be in if the list were sorted:

alist = list(''defabc'')
ranks = [3, 4, 5, 0, 1, 2]

To do that, I needed to generate an index table first. In the book
"Numerical Recipes in Pascal" by William Press et al there is a procedure
to generate an index table (46 lines of code) and one for a rank table
(five lines).

In Python, my index function is four lines of code and my rank function is
five lines. I then wrote three more functions for verifying that my index
and rank tables were calculated correctly (17 more lines) and four more
lines to call doctest, giving a total of 30 lines of code.

I also have 93 lines of documentation, including doctests, or three
lines of documentation per line of code.

For those interested, here is how to generate an index table and rank
table in Python:
def index(sequence):
decorated = zip(sequence, xrange(len(sequence)))
decorated.sort()
return [idx for (value, idx) in decorated]

def rank(sequence):
table = [None] * len(sequence)
for j, idx in enumerate(index(sequence)):
table[idx] = j
return table
You can write your own damn documentation. *wink*

--
Steven.

解决方案

Steven,

def index(sequence):
decorated = zip(sequence, xrange(len(sequence)))
decorated.sort()
return [idx for (value, idx) in decorated]

would''nt that be equivalent code?

def index(sequence):
return [c for _,c in sorted((b,a) for a, b in
enumerate(sequence))]

tested, gave same results. But worsens your doc2code ratio :)

Harald Armin Massa
--


GHUM wrote:

Steven,

>def index(sequence):
decorated = zip(sequence, xrange(len(sequence)))
decorated.sort()
return [idx for (value, idx) in decorated]


would''nt that be equivalent code?

def index(sequence):
return [c for _,c in sorted((b,a) for a, b in
enumerate(sequence))]

Or even these:

def index(sequence):
return sorted(range(len(sequence)), key=sequence.__getitem__)

def rank(sequence):
return sorted(range(len(sequence)),
key=index(sequence).__getitem__)

Hint: if you find yourself using a decorate-sort-undecorate pattern,
sorted(key=func) or sequence.sort(key=func) might be a better idea.
--
Michael Hoffman


On 4/29/07, Steven D''Aprano <st***@remove.this.cybersource.com.auwrote:

To do that, I needed to generate an index table first. In the book
"Numerical Recipes in Pascal" by William Press et al there is a procedure
to generate an index table (46 lines of code) and one for a rank table
(five lines).

51 lines total.

In Python, my index function is four lines of code and my rank function is
five lines. I then wrote three more functions for verifying that my index
and rank tables were calculated correctly (17 more lines) and four more
lines to call doctest, giving a total of 30 lines of code.

So 9 lines for Python, excluding tests.

I also have 93 lines of documentation, including doctests, or three
lines of documentation per line of code.

Then, without documentation, Python is roughly 560% (51/9) as
efficient as Pascal. But with documentation (assuming you need the
same amount of documentation for the Python code as the Pascal code),
(51 + 93)/(9 + 93) = 1.41 so only 141% as efficient as Pascal.

I wonder what that means? Maybe Python the language is approaching the
upper bound for how efficient an imperative programming language can
be? On the other hand, there seem to be some progress that could be
made to reduce the amount of work in writing documentation.
Documentation in Esperanto instead of English maybe?

--
mvh Bj?rn


这篇关于我们谈论的是烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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