Для этой цели можно использовать сокращение, если вы позаботитесь о том, чтобы оставить копию текущего элемента в результате функции сокращения.
def diff_summarize(revisionList, nextRevision):
'''helper function (adaptor) for using svn.diff_summarize with reduce'''
if revisionList:
# remove the previously tacked on item
r1 = revisionList.pop()
revisionList.append(svn.diff_summarize(
svn_path, revision1=r1, revision2=nextRevision))
# tack the current item onto the end of the list for use in next iteration
revisionList.append(nextRevision)
return revisionList
summaries = reduce(diff_summarize, dm_revisions, [])
EDIT: Да, но никто не говорил, что результат функции в reduce должен быть скалярным. Я изменил свой пример, чтобы использовать список. По сути, последний элемент всегда является предыдущей версией (кроме первого прохода), а все предыдущие элементы являются результатом вызова svn.diff_summarize. Таким образом, вы получите список результатов в качестве окончательного вывода...
EDIT2: Да, код действительно был сломан. У меня есть рабочий манекен:
>>> def compare(lst, nxt):
... if lst:
... prev = lst.pop()
... lst.append((prev, nxt))
... lst.append(nxt)
... return lst
...
>>> reduce(compare, "abcdefg", [])
[('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('f', 'g'), 'g']
Как видите, это было протестировано в оболочке. Вы захотите заменить (prev, nxt) в lst.append вызове compare, чтобы фактически добавить сводку вызова к svn.diff_summarize.
>>> help(reduce)
Help on built-in function reduce in module __builtin__:
reduce(...)
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
person
Daren Thomas
schedule
28.01.2010