Question: Improve the code documentation and naming of the City class. The current variable naming, commenting and code structure in the City class is terrible. Perhaps
Improve the code documentation and naming of the City class. The current variable naming, commenting and code structure in the City class is terrible. Perhaps the author was rushing to finish it, or gave up on the project. Whatever the reason, it needs to be significantly improved to
aid in understanding and testing. you should improve the overall documentation and naming of the City class definition. This should also include proper documentation . You may have to research what some of the existing methods do in order to properly document them. You may have to
choose between keeping a bad variable name and a wide scale refactoring. Be sure to document the reasons for your decision in such cases.
Code Below:
class City(object): """ Basic class for modelling a population centre. """ def __init__(self, lat, long, name, population): self.name = name self.lat = lat self.long = long self.infected = 0 self.incoming_infected = 0 self.survivors = 0 self.cured = 0 self.dead = 0 self.initial_population = population self.healthy_population = population self.neighbours = set() # These are other instances of the city class. def __hash__(self): return hash(self.name) def __ne__(self, other): return self.name != other.name def __eq__(self, other): return self.name == other.name def __lt__(self, other): return self.name < other.name def add_neighbour(self, neighbour): self.neighbours.add(neighbour) def remove_neighbour(self, neighbour): self.neighbours.remove(neighbour) def start_of_turn(self): # Needs to happen here self.infected += self.incoming_infected self.incoming_infected = 0 def run_turn(self, turn_number): # Each turn: # A proportion of infected cases move to a neighbouring city # Then a proportion of infected cases either die or recover # Each remaining infected case contacts other people based on infection rate. # - if the contact is a survivor or has been cured, nothing happens. # - if the contact a healthy person, they get the disease. # Two other rules to stop the simulation running forever, or going very slowly. # The minimum number of infected cases that either die or recover is 5 (or all cases if less than 5 infected). # If there are less than 10 infected people and no healthy people in the city, set the number of infected to 0. self.move_infected() self.change_in_infected_numbers() self.spread_infection() def move_infected(self): num_moving = int(self.infected * MOVEMENT_PROPORTION) num_per_neighbour = num_moving // len(self.neighbours) for nbr in self.neighbours: nbr.incoming_infected += num_per_neighbour self.infected -= num_per_neighbour def change_in_infected_numbers(self): if self.infected > 5: num_resolved = max(self.infected // AVERAGE_DURATION, 5) elif self.infected > 0: num_resolved = self.infected else: num_resolved = 0 num_die = int(num_resolved * MORTALITY_RATE) self.infected -= num_resolved self.survivors += (num_resolved - num_die) self.dead += num_die def spread_infection(self): if self.healthy_population + self.survivors + self.cured > 0: r = self.healthy_population / (self.healthy_population + self.survivors + self.cured) hc = int(r * self.infected * INFECTION_RATE) if self.healthy_population < hc: self.infected += self.healthy_population self.healthy_population = 0 elif self.infected > 0: self.healthy_population -= hc self.infected += hc if self.infected < 10 and self.healthy_population == 0: self.dead += int(self.infected * MORTALITY_RATE) self.survivors += (self.infected - int(self.infected * MORTALITY_RATE)) self.infected = 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
