Okay, so I currently am working on a large python 2.7 codebase where my current task is as follows:
There are 2 modules say A
and B
which have a lot of similar code (I mean almost all methods are identical). My goal is to create a Base
module to contain all the common code.
For this purpose I currently am manually reading and comparing functions (of the same name) between A
and B
to see if they are same (or even almot same)or not.
So is there a some way to automate this? Like a library like diff
which is built to compare 2 python function codes to tell me if their content is same/almost same or not?
So for example currently I am working on 2 modules ResourceStatusDB.py
and ResourceStatusManagement.py
:
ResourceStatusDB.py
class ResourceManagementDB(BaseRSSDB):
.
.
.
def select(self, table, params):
session = self.sessionMaker_o()
XYZ()
# finding the table
found = False
for ext in self.extensions:
try:
table_c = getattr(__import__(ext + __name__, globals(), locals(), [table]), table)
found = True
break
except (ImportError, AttributeError):
continue
# If not found in extensions, import it from DIRAC base (this same module).
if not found:
table_c = getattr(__import__(__name__, globals(), locals(), [table]), table)
And ResourceStatusManagement.py def select(self, table, params):
session = self.sessionMaker_o()
# finding the table
found = False
for ext in self.extensions:
try:
table_c = getattr(__import__(ext + __name__, globals(), locals(), [table]), table)
found = True
break
except (ImportError, AttributeError):
continue
# If not found in extensions, import it from DIRAC base (this same module).
if not found:
table_c = getattr(__import__(__name__, globals(), locals(), [table]), table)
Notice that the 2 functions are not exactly the same. (A difference of a few lines maybe possible). But still a majority of the logic is same at the function level. Any way to detect this?