[Python-de] difflib problem

Berthold Hoellmann berthold at despammed.com
Fr Nov 2 14:51:41 CET 2012


Moin,

Ich versuche eine spezielles diff zu schreiben und wollte dazu die
python difflib nutzen. Allerdings macht sie (natürlich?) nicht das, was
ich von ihr erwarte;

Python 2.7.3 (default, Jul 24 2012, 15:00:49) 
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import difflib
>>> class line(object):
...     def __init__(self, val):
...         self.val = val
...     def __eq__(self, other):
...         print '__eq__', self.val, other.val
...         return abs(self.val - other.val) < 1.
... 
>>> f1 = [line(i) for i in [1., 2., 3., 4.]]
>>> f2 = [line(i) for i in [1.1, 3.1, 3.3, 4.4]]
>>> 
>>> match = difflib.SequenceMatcher(None, f1, f2)
>>> for opcode in match.get_opcodes():
...     print "%-7s a[%d:%d] b[%d:%d]" % opcode
... 
__eq__ 1.0 1.1
__eq__ 2.0 3.1
__eq__ 2.0 3.1
equal   a[0:1] b[0:1]
replace a[1:4] b[1:4]
>>> 


ich hätte deutlich mehr Aufrufe von '__eq__' erwartet und dass Ergebnis:

equal   a[0:1] b[0:1]
replace a[1:2] b[1:2]
equal   a[2:4] b[2:4]

Warum gibts das nicht?

Danke
Berthold
-- 
                            __   Address:
                         G /  \ L Germanischer Lloyd SE
phone: +49-40-36149-7374 -+----+- Brooktorkai 18
fax  : +49-40-36149-7320   \__/   D-20457 Hamburg


Mehr Informationen über die Mailingliste python-de