我怎么用Python怪?

2024-04-23 21:53:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近在我的系统中安装了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”配置结构有关?你知道吗


Tags: 函数inpyselfislibpackagesusr
1条回答
网友
1楼 · 发布于 2024-04-23 21:53:29

出现此错误是因为WikEdDiff对象在diff()内部被清除,如代码的this section所示:

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+ ] }'

相关问题 更多 >