problem using differ

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russell

    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)


  • Peter Otten

    #2
    Re: problem using differ

    Russell wrote:
    [color=blue]
    > 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)[/color]

    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

    Comment

    • Gary Richardson

      #3
      Re: problem using differ


      "Russell" <rphaturos@knol ogy.net> wrote in message
      news:vsa5jn2060 qe24@corp.super news.com...[color=blue]
      > 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[/color]
      differ.[color=blue]
      > I keep getting this error: NameError: name 'differ' is not defined. What[/color]
      am[color=blue]
      > 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)
      >
      >[/color]
      d=difflib.Diffe r()
      for k in range(4):
      dif = d.compare(a[k], b[k])
      for p in dif:
      print p,
      print


      Comment

      • Russell

        #4
        Re: problem using differ

        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.



        Comment

        Working...