I've got to refactor this code as seeing from SOLID viewpoint it is not a good practice(not extensible), what are the alternatives?
I have a text file from which i read the commands, its format goes like:
ADD_CHILD name1 name2 gender
GET_RELATIONSHIP name1 relation ..
The problem here i think is when i pass words to the function because if the format of text file changes my code will break. One thing that comes to mind is use of if-elif statements but they are not advised by open-closed principle. What are other possible approaches?
path_str=input("Enter the path of input file ")
fileinput=open(path_str,"r")
for line in fileinput:
words=line.split()
function=set1solution.function_select_map.get(words[0])
result=function(family,words)
function_select_map={
"ADD_CHILD":add_child,
"GET_RELATIONSHIP":get_relationship
}
relation_map={
"Paternal-Uncle":Family.get_Paternal_Uncle,
......}
def add_child(family,words):
#find person just returns an object of person class
parent=family.find_person(words[1])
new_addition=Person(words[2],words[3],words[1])
result=family.add_external_child(words[1],new_addition.name,new_addition.gender)
return result
def get_relationship(family,words):
person=family.find_person(words[1])
function=set1solution.relation_map.get(words[2])
result=function(family,person)
return result