473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

problem using differ

I'm trying to automate the comparison of some source code but I'm having
trouble using the differ object. I'm new to Python so I'm probably doing
something wrong. I've created a small program (included below) that
demonstrates my problem. For some reason, Python doesn't recognize differ.
I keep getting this error: NameError: name 'differ' is not defined. What am
I doing wrong?

Test Code follows:
import difflib

a=[]
a.append('1. Beautiful is better than ugly.')
a.append('2. Explicit is better than implicit.')
a.append('3. Simple is better than complex.')
a.append('4. Complex is better than complicated.')

b=[]
b.append('1. Beautiful is better than ugly.')
b.append('3. Simple is better than complex.')
b.append('4. Complicated is better than complex.')
b.append('5. Flat is better than nested.')

d=differ()
d.compare(a,b)
Jul 18 '05 #1
3 3188
Russell wrote:
I'm trying to automate the comparison of some source code but I'm having
trouble using the differ object. I'm new to Python so I'm probably doing
something wrong. I've created a small program (included below) that
demonstrates my problem. For some reason, Python doesn't recognize
differ.
I keep getting this error: NameError: name 'differ' is not defined. What
am I doing wrong?

Test Code follows:
import difflib

a=[]
a.append('1. Beautiful is better than ugly.')
a.append('2. Explicit is better than implicit.')
a.append('3. Simple is better than complex.')
a.append('4. Complex is better than complicated.')

b=[]
b.append('1. Beautiful is better than ugly.')
b.append('3. Simple is better than complex.')
b.append('4. Complicated is better than complex.')
b.append('5. Flat is better than nested.')

d=differ()
d.compare(a,b)


You have to tell Python where to find Differ.
Python is case sensitive: Differ != differ.
The compare() method returns a generator which you are discarding, making
the last statement useless.

Assuming you want to print the result, you could write:

d = difflib.Differ()
for line in d.compare(a, b):
print line

Peter
Jul 18 '05 #2

"Russell" <rp*******@knology.net> wrote in message
news:vs************@corp.supernews.com...
I'm trying to automate the comparison of some source code but I'm having
trouble using the differ object. I'm new to Python so I'm probably doing
something wrong. I've created a small program (included below) that
demonstrates my problem. For some reason, Python doesn't recognize differ. I keep getting this error: NameError: name 'differ' is not defined. What am I doing wrong?

Test Code follows:
import difflib

a=[]
a.append('1. Beautiful is better than ugly.')
a.append('2. Explicit is better than implicit.')
a.append('3. Simple is better than complex.')
a.append('4. Complex is better than complicated.')

b=[]
b.append('1. Beautiful is better than ugly.')
b.append('3. Simple is better than complex.')
b.append('4. Complicated is better than complex.')
b.append('5. Flat is better than nested.')

d=differ()
d.compare(a,b)

d=difflib.Differ()
for k in range(4):
dif = d.compare(a[k], b[k])
for p in dif:
print p,
print
Jul 18 '05 #3
Gary and Peter

I appreciate your response. As you both point out in your replies I failed
to qualify my use of 'Differ()' by prefixing it with difflib (as well typing
the case properly). Once I made this correction I was able to continue.

Jul 18 '05 #4

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

Similar topics

17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
9
by: babak | last post by:
Hi everybody I'm working with a project in embedded Visual Studio 4 and I have a general problem which I hope that somebody can help me with. My problem is the following: My project works fine in...
1
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 =...
2
by: Jiwei06xie | last post by:
Hi All, I am trying to upgrade an Access 2000 DB to 2007 (using linked tables and ODBC to connect to backend SQL 2000 database). In my DB, I have a mainswitch board and some command buttons on it....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.