0

The problem statement is:

Given 2 python files 'A.py' and 'B.py' (modified A.py), is there a way we can find out the:

1.Added methods

2.Removed methods

3.Modified methods : (a) Change in method prototype (b) Change in method content

Similarly for classes(changed/removed/modified) as well.

My Solution:

I was thinking if i could use a good diff tool, and find out the added/removed/modified lines, i can parse them to find out the required details. I tried with git-diff but it gives line-wise diff. So if a method got shifted because some other method was added before that, it shows the method as deleted from the original file and added in the later file.

I saw 'meld' gives good diff between files which i could use easily, but i could not find a way to programmatically capture the output of meld.

Please provide any follow up on my solution, or any other solution for the problem

FYI: I want to automate this as there are many such files. Also, this has to be done on a linux box.

0

4 Answers 4

2

Using

sdiff File1 File2

command in unix we can find difference between two files.

ex: if we change the add module and then compare two files the changes will appear with > symbol

def add(x,y):                           def add(x,y):
                                     >      print x,y
    return x+y                              return x+y

def subtract(x, y):                     def subtract(x, y):
    return x-y                              return x-y

def multiply(x, y):                     def multiply(x, y):
    return x*y                              return x*y
class MyFuncs:                          class MyFuncs:

  def divide(self,x, y):                      def divide(self,x, y):
      return x/y                              return x/y
1

Easiest way to do this would be to use a merge tool such as winmerge This would enable you to see the two side by side and any differences would be highlighted.

0

Git can do that, check out github its exactly what you look for

0

You can use the difflib module:

from difflib import ndiff

with open("a.py") as a, open("b.py") as b:
    for dif in ndiff(a.readlines(),b.readlines()):
        print(dif)


- def foobar():

+ def foobar1():

?           +

-    a = 3

?        ^

+    a = 22

?        ^^

-    b = 4

+    b = 44

?        +

     print("foo")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.