0
class Country(object):
    """ Modeling a country immigration. """

    def __init__(name, immigrants, population, disease_numbers):

        self.name = name
        self.immigrants = immigrants
        self.population = population
        self.disease_numbers = disease_numbers

I have the follow class , that are more attributes .. Every year the population changes and at the end of x years im trying to built o ordered dictionary that will show me which country have the highest population and the least people with diseases... How do I built a dictionary that updates on every year/ turn and give me this info at the end.

How can I get information of the stats on the last turn?

Let me clarify the question.

All i need is at the end of the simulation to have a ordered dictionary.

  d = {'self.name' : London
       ('self.immigrants' : 15000
       'self.population' : 500000
       'self.disease_numbers' :3000) , 
        'self.name' : Madrid
       ('self.immigrants' : 17000
       'self.population' : 5000
       'self.disease_numbers' :300)}

Then be able to pick for example in this case London because of higher number of people with a disease. So thinking through it could almost be a new method that return the city with the higher number of people with the disease.

Ian_De_Oliveira
  • 291
  • 5
  • 16
  • 2
    [Here](https://stackoverflow.com/questions/8628123/counting-instances-of-a-class) are some answers that might help up – yorodm Sep 10 '18 at 19:40
  • 1
    From what I understood, [this](https://stackoverflow.com/questions/2675028/list-attributes-of-an-object) may help you – Lucas Wieloch Sep 10 '18 at 19:42
  • You probably want to add a method to the class, to model the change of the data each year. Then make all the instances you're interested in studying, and call the "update" method for each several times in a loop. After that you can write some code to inspect the instances' data and return the information you want in the form you want it. But without details as to the logic as to immigration and spread of disease, or how you want the final dictionary to be structured, I couldn't say any more. – Robin Zigmond Sep 10 '18 at 19:42
  • @Robin Zigmond , im trying to find which country has more population and disease numbers. – Ian_De_Oliveira Sep 10 '18 at 19:45
  • What else does your class do? Is it just to store the data? If so then this kind of task may be more easily done using a `pandas` dataframe than a dictionary of objects. – Stuart Sep 10 '18 at 19:54

2 Answers2

1
class Country(object):
    """ Modeling a country immigration. """

    def __init__(name, immigrants, population, disease_numbers):

        self.name = name
        self.immigrants = immigrants
        self.infected = population
        self.disease_numbers = disease_numbers

    def update_pop(self, year, rate):
        self.infected = self.infected * year * rate

Would adding a function to the class work?

vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
1

Not entirely clear how you intend to update the country data, but it sounds like what you need is just to store the country data objects in a dictionary of dictionaries, with a series of database-like functions to query it, like this:

country_data = {}

def add_country_data(name, year, *args, **kwargs):
    country = Country(name, *args, **kwargs)
    if name in country_data:
        country_data[name][year] = country
    else:
        country_data[name] = {year: country}

def get_latest_data(country_name):
    years = country_data[country_name].keys()
    return country_data[country_name][max(years)]

def get_max_country(attr, year=None):
    """ Returns the county with the max value of the given attribute
    in the given year or (if year is ommitted) any year """
    r = None
    highest = None
    for name, country in country_data.items():
        if year is None:
            max_v = max(getattr(country[y], attr) for y in country.keys())
        else:
            max_v = getattr(country[year], attr)
        if highest is None or max_v > highest:
            highest = max_v
            r = name
    return r, highest

def get_latest_dicts():
    return {name: get_latest_data(name).__dict__ 
            for name in country_data.keys()}

add_country_data("Venezuela", 1989, 100, 20, 50)
add_country_data("Venezuela", 2009, 120, 30, 40)
add_country_data("Brazil", 2008, 110, 40, 90)

print get_latest_data("Venezuela").immigrants   # 120
print get_max_country("infected")     # ('Brazil', 40)
print get_latest_dicts()            # ('Brazil': {'immigrants: 110 ... 

If you want you could add these functions and the data dictionary to the class as class methods

class Country(object):
    """ Modeling a country immigration. """
    data = {}

    def __init__(self, name, immigrants, population, disease_numbers):
        self.name = name
        self.immigrants = immigrants
        self.infected = population
        self.disease_numbers = disease_numbers

    @classmethod
    def add_data(cls, year, *args, **kwargs):
        obj = cls(*args, **kwargs)
        cls.data[obj.name, year] = obj

    # ...

 Country.add_data("Venezuela", 1989, 100, 20, 50)

This is convenient because all the functions relating to storage and querying of country data can be stored in the Country class together with any modelling methods you need.

Stuart
  • 9,597
  • 1
  • 21
  • 30