Question
given the class farm in the first pics write a function using python called clean_farm_data, details in second pics and also a second function called
given the class farm in the first pics write a function using python called clean_farm_data, details in second pics
and also a second function called create_farm_instances
here is the transcribed version of the farm class that has to be used for the two functions:
class Farm: company_name = "Orgo" #constructor to initialize all fields listed in question. def __init__(self,name,owner,country,size,num_of_livestock,num_of_workers,assets,compatible_livestock): self.name = name self.owner = owner self.counry = country self.size = size self.num_of_livestock = num_of_livestock self.num_of_workers = num_of_workers self.assets = assets self.compatible_livestock = compatible_livestock #overide greater than operator to return True #if self.assets is > other.assets if not False #which makes Farm class instances to be Sortable. def __gt__(self,other): if(self.assets > other.assets): return True return False #check two instances self,other are equal to each other. #by overiding == operator #as provided in question return True only if name is equal and owners are not equal. #if not return False def __eq__(self,other): if(self.name == other.name and self.owner != other.owner): return True else: return False #returning the name of the Farm instance #whenever it is called in console or print statement. #to do it we have to override __str__ method. def __str__(self): return self.name
#metohd to sort Farm instances array. def sort_farm_objects(array): #get length of array total_objs = len(array) for i in range(total_objs): for j in range(0,total_objs-i-1): #here > operator invokes __gt__() method in Farm class #which returns True if j_th Farm instance assets are greater than j+1_th Farm Instance #assets. if(array[j] > array[j+1]): array[j],array[j+1] = array[j+1],array[j]
def main(): array = [] #method 1 testing print(" Initilizing Farm objects using Constructor") array.append(Farm("Birch Wood","Jameson","UK",10,10,5,20,"deer;dog")) array.append(Farm("Magnolia","John","USA",20,30,8,40,"goat;sheep;mule;dog")) array.append(Farm("Magnolia","Mike","USA",20,30,8,10,"goat;sheep;mule;dog")) array.append(Farm("Roat","John","USA",20,30,8,5,"goat;sheep;mule;dog")) array.append(Farm("Oakdale","Simson","USA",20,30,8,30,"goat;sheep;mule;dog")) print(" Farm Instances Before sorting") for i in array: #method 4 testing. print i should return farm name. print("Farm Name:",i) #method 3 testing. print(" Farm - 1 Instances Details: Farm Name:",array[0].name,"Owner:",array[0].owner) print("Farm - 2 Instances Details: Farm Name:",array[1].name,"Owner:",array[1].owner) print("Farm - 3 Instances Details: Farm Name:",array[2].name,"Owner:",array[2].owner) #note: python is 0 based indexing. #output must be False because Farm1 and Farm 2 names are different. print("Is Farm - 1 and Farm - 2 are equal :",array[0] == array[1]) #output must be True because Farm2 and Farm 3 names are same. and owner names are different. print("Is Farm - 2 and Farm - 3 are equal :",array[1] == array[2])
#method 2 testing print(" Sorting Farm Instances using Method #2") sort_farm_objects(array) print(" Farm Instances After sorting") for i in array: print("Farm Name:",i) if __name__ == "__main__": main()
mme Functions Function name: clean_farm_data Parameters raw_farm_data (list) A list of lists with uncleaned farm data Return Type: list Description: Note that we have included the raw farm data in the template's main function. Each sublist contains the information for one Farm. This function should manipulate the raw data to meat the requirements of the instance attributes of Farm defined above. In other words, for each sublist, you need to make sure that each entry is of the correct type, contains the desired information, and is in the right format. Refer to the Farm definition above. Keep in mind that compatible livestock should be set to the bool None if the Farm has no compatible livestock. Do NOT change the order of the sublists and their entries. Return a new list of lists with cleaned data. Function name create_farm_instances Parameters farm data (list) A list of lists with cleaned farm data (returned from clean_farm_data()) Return Type: list Description: This function should take in a list of cleaned farm data and create a list of Farm instances using the data. Return a new list with the Farm instances. Create the instances in the order of the sublists. mme Functions Function name: clean_farm_data Parameters raw_farm_data (list) A list of lists with uncleaned farm data Return Type: list Description: Note that we have included the raw farm data in the template's main function. Each sublist contains the information for one Farm. This function should manipulate the raw data to meat the requirements of the instance attributes of Farm defined above. In other words, for each sublist, you need to make sure that each entry is of the correct type, contains the desired information, and is in the right format. Refer to the Farm definition above. Keep in mind that compatible livestock should be set to the bool None if the Farm has no compatible livestock. Do NOT change the order of the sublists and their entries. Return a new list of lists with cleaned data. Function name create_farm_instances Parameters farm data (list) A list of lists with cleaned farm data (returned from clean_farm_data()) Return Type: list Description: This function should take in a list of cleaned farm data and create a list of Farm instances using the data. Return a new list with the Farm instances. Create the instances in the order of the sublists
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started