我该如何使用python-WikEdDiff?

7

我最近在我的系统上安装了python-WikEdDiff包。我了解到它是原始JavaScript WikEdDiff工具的Python扩展。我尝试使用它,但我找不到任何关于它的文档。我卡在使用WikEdDiff.diff()上。我希望能够使用此类的其他函数,例如getFragments()等,但检查后显示以下错误:

 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/WikEdDiff/diff.py", line 1123, in detectBlocks
    self.getSameBlocks()
  File "/usr/local/lib/python3.4/dist-packages/WikEdDiff/diff.py", line 1211, in getSameBlocks
    while j is not None and self.oldText.tokens[j].link is None:
IndexError: list index out of range

经过检查,我发现对象中的tokens[]结构仍然是空的,而它应该已被初始化。

我需要调用除默认构造函数之外的初始化函数吗?或者这与我传递给构造函数的`WikEdDiffConfig'配置结构有关吗?


这里有一个Python3的示例,查看源代码:https://github.com/lahwaacz/python-wikeddiff/blob/master/WikEdDiff/__init__.py - Padraic Cunningham
1个回答

3
你会得到这个错误,是因为在 diff() 方法内部清除了 WikEdDiff 对象,就像代码的 这部分 所示:
def diff( self, oldString, newString ):
    ...
    # Free memory
    self.newText.tokens.clear()
    self.oldText.tokens.clear()
    # Assemble blocks into fragment table
    fragments = self.getDiffFragments()
    # Free memory
    self.blocks.clear()
    self.groups.clear()
    self.sections.clear()
    ...
    return fragments

如果您只需要片段,请像这样使用diff()的返回变量:
import WikEdDiff as WED
config=WED.WikEdDiffConfig()
w = WED.WikEdDiff(config)
f = w.diff("abc", "efg")
# do whatever you want with f, but don't use w
print(' '.join([i.text+i.type for i in f]))
# outputs '{ [ (> abc-  ) abc< efg+ ] }'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接