473,662 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difflib.ndiff broken?

Can anyone try the following in their python interpreter?

These give correct output:
print list(ndiff(['saving2 <<A'],['saving <<a>>'])) ['- saving2 <<A', '? - ^\n', '+ saving <<a>>', '? ^^^\n'] print list(ndiff(['saving2 <<AA'],['saving <<a>>'])) ['- saving2 <<AA', '? - ^^\n', '+ saving <<a>>', '? ^^^\n'] print list(ndiff(['saving2 <<A'],['saving <<aa>>'])) ['- saving2 <<A', '? - ^\n', '+ saving <<aa>>', '? ^^^^\n'] print list(ndiff(['saving <<A'],['saving <<aa>>'])) ['- saving <<A', '? ^\n', '+ saving <<aa>>', '? ^^^^\n']

Now try the very slight variations:
print list(ndiff(['saving2 <<AA'],['saving <<aa>>'])) ['- saving2 <<AA', '+ saving <<aa>>'] print list(ndiff(['saving2 <<AA'],['saving <<aa>>']))

['- saving2 <<AA', '+ saving <<aa>>']

This can't be right... or is it? Where are the '? ...' lines? It does this
for both Python 2.3.2 on Windows 2000 and Python 2.3.3 on SGI. If it's
correct, how come???

Oliver
Jul 18 '05 #1
3 1716
[Humpdydum]
Can anyone try the following in their python interpreter?

These give correct output:
print list(ndiff(['saving2 <<A'],['saving <<a>>'])) ['- saving2 <<A', '? - ^\n', '+ saving <<a>>', '? ^^^\n'] print list(ndiff(['saving2 <<AA'],['saving <<a>>'])) ['- saving2 <<AA', '? - ^^\n', '+ saving <<a>>', '? ^^^\n'] print list(ndiff(['saving2 <<A'],['saving <<aa>>'])) ['- saving2 <<A', '? - ^\n', '+ saving <<aa>>', '? ^^^^\n'] print list(ndiff(['saving <<A'],['saving <<aa>>'])) ['- saving <<A', '? ^\n', '+ saving <<aa>>', '? ^^^^\n']

Now try the very slight variations:
print list(ndiff(['saving2 <<AA'],['saving <<aa>>'])) ['- saving2 <<AA', '+ saving <<aa>>'] print list(ndiff(['saving2 <<AA'],['saving <<aa>>'])) ['- saving2 <<AA', '+ saving <<aa>>']

This can't be right... or is it? Where are the '? ...' lines? It does this
for both Python 2.3.2 on Windows 2000 and Python 2.3.3 on SGI. If it's
correct, how come???


ndiff produces intraline difference marking if and only if it thinks
the inputs are "reasonably close". The cutoff between "reasonably
close" and "not reasonably close" is necessarily heuristic. '?' lines
are more irritating than helpful when they have a lot of markup in
them, so it certainly wan't intended that '?' lines *always* be
produced. The '+' and '-' lines contain all the information about how
to change one sequence into another; the '?' lines are fluff (abeit
sometimes helpful fluff -- that's why they're (sometimes) there).

Concretely, ndiff produces intraline marking iff two lines have a
similarity ratio of at least 0.75. In your first examples, the lines
do:
import difflib
m = difflib.Sequenc eMatcher()
m.set_seqs('sav ing2 <<A', 'saving <<a>>')
print m.ratio() 0.782608695652

In your last examples, the lines don't:
m.set_seqs('sav ing2 <<AA', 'saving <<aa>>')
print m.ratio() 0.72


Internally, 0.75 is the default value of FancyReplacer's optional
minimal_cutoff argument.
Jul 18 '05 #2
OK, forget it, sorry it was my mistake: it wasn't obvious from the difflib
docs, but it appears that ndiff points out the sub-line differences (lines
that start with ?) only if it was able to figure out operations that could
be applied to substrings on the line. Though often such operations are
obvious by looking at the strings being compared, ndiff doesn't always find
them, and so marks the whole line as + or -.

Anyone know of web site that explains ndiff output? I coulnd't figure out a
good set of search terms in google, didn't get anything useful. Thanks,

Oliver

"Humpdydum" <ol************ ***@utoronto.ca > wrote in message
news:cd******** **@nrc-news.nrc.ca...
Can anyone try the following in their python interpreter?

These give correct output:
print list(ndiff(['saving2 <<A'],['saving <<a>>'])) ['- saving2 <<A', '? - ^\n', '+ saving <<a>>', '? ^^^\n'] print list(ndiff(['saving2 <<AA'],['saving <<a>>'])) ['- saving2 <<AA', '? - ^^\n', '+ saving <<a>>', '?

^^^\n']
print list(ndiff(['saving2 <<A'],['saving <<aa>>'])) ['- saving2 <<A', '? - ^\n', '+ saving <<aa>>', '?

^^^^\n']
print list(ndiff(['saving <<A'],['saving <<aa>>'])) ['- saving <<A', '? ^\n', '+ saving <<aa>>', '? ^^^^\n']

Now try the very slight variations:
print list(ndiff(['saving2 <<AA'],['saving <<aa>>'])) ['- saving2 <<AA', '+ saving <<aa>>'] print list(ndiff(['saving2 <<AA'],['saving <<aa>>']))

['- saving2 <<AA', '+ saving <<aa>>']

This can't be right... or is it? Where are the '? ...' lines?

Jul 18 '05 #3
[Humpdydum]
OK, forget it, sorry it was my mistake:
I didn't see a mistake, just a question.
it wasn't obvious from the difflib docs, but it appears that ndiff points out the
sub-line differences (lines that start with ?) only if it was able to figure out
operations that could be applied to substrings on the line. Though often such
operations are obvious by looking at the strings being compared,
They can be for a program but often aren't for people. That's why
ndiff produces '?' lines when it thinks they might help. This is a
heuristic -- a guess. Sometimes it's not the same guess you'd make.
There's always a sequence of operations that can be applied to change
any line into any other line, but *usually* they're uninteresting.
'?' lines attempt to point out "minor edits".
ndiff doesn't always find them, and so marks the whole line as + or -.
It marks two input lines that differ with - and + regardless of
whether it produces two ? lines too.
Anyone know of web site that explains ndiff output? I coulnd't figure out a
good set of search terms in google, didn't get anything useful. Thanks,


ndiff is unique to Python, and you have the source code for it.
Because '?' lines are fluff, precise docs for them would be
counterproducti ve. They're meant to guide the eye to minor intraline
differences, and that's all.

If a ? line appears, there are always two of them, interleaved between
a -+ pair, in this pattern:

-
?
+
?

Each ? line implicitly refers to the line immediately above it. Four
meaningful characters appear in ? lines. A caret (^) means the
character immediately above it was replaced, in going from the - to
the + line. "-" means the character immediately above it was deleted;
'+' means it was inserted; and a blank means the character immediately
above it is the same in both (- and +) lines. A '-' can appear only
in the ? line following a - line, and a '+' can appear only in the ?
line following a + line, because we're picturing the edits needed to
change the - line into the + line.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8693
by: John Henry | last post by:
I am just wondering what's with get_close_matches() in difflib. What's the magic? How fuzzy do I need to get in order to get a match?
1
9427
by: Neilen Marais | last post by:
Hi I'm trying to compare some text to find differences other than whitespace. I seem to be misunderstanding something, since I can't even get a basic example to work: In : d = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK) In : list(d.compare(, )) Out:
7
3414
by: whitewave | last post by:
Hi Guys, I'm a bit confused in difflib. In most cases, the differences found using difflib works well but when I have come across the following set of text: .... problem even for the simple forms of the differential equation and the nonlocal conditions. Due to these facts, some serious difficulties arise in the application of the classical methods to such a problem.''' .... adjoint problem even for the simple forms of the differential
3
1833
by: n00m | last post by:
from random import randint s1 = '' s2 = '' for i in xrange(1000): s1 += chr(randint(97,122)) s2 += chr(randint(97,122)) print s1
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.