Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use python-WikEdDiff?

I recently installed python-WikEdDiff package to my system. I understand it is a python extension of the original JavaScript WikEdDiff tool. I tried to use it but I couldn't find any documentation for it. I am stuck at using WikEdDiff.diff(). I wish to use the other functions of this class, such as getFragments() and others, but on checking, it shows the following error:

 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

On checking, I found out that the tokens[] structure in the object remains empty whereas it should have been initialized.

Is there an initialize function that I need to call apart from the default constructor? Or is it something to do with the `WikEdDiffConfig' config structure I passed to the constructor?

like image 836
AayDee Avatar asked Nov 07 '15 17:11

AayDee


1 Answers

You get this error because the WikEdDiff object was cleared internally inside diff(), as shown in this section of the code:

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

If you just need the fragments, use the returned variable of diff() like this:

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+ ] }'
like image 68
gdlmx Avatar answered Oct 07 '22 01:10

gdlmx