Beispiel #1
0
    def av_gdp_growth_prod(self, countries_list, years_list, production_type):
        '''
        Returns a list with the countries and their average gdp, growth and production in years_list
        '''

        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        gdp = []
        growth = []
        prod = []
        f = Fao()
        result_list = {}
        for country in countries_list:
            growth.append(list(self.average_growth([country], years_list).values())[0])
            gdp.append(list(self.average_gdp([country], years_list).values())[0])
            prod.append(list(f.average_production([country], years_list, production_type, "Food").values())[0])

            result_list[country] = [
                "average growth: " + str(list(self.average_growth([country], years_list).values())[0]),
                "average gdp: " + str(list(self.average_gdp([country], years_list).values())[0]),
                "average production: " + str(
                    list(f.average_production([country], years_list, production_type, "Food").values())[0])]

        return result_list
        print(prod)
        session.close()
Beispiel #2
0
 def test_avg_gdp_growth_prod(self):
     datas = self.data.list_countries()
     a = [datas[15]]
     f = Fao()
     self.assertEqual(
         self.data.conclusion_gdp_growth_prod(
             a, [2000, 2001], [2003, 2006],
             f.list_products_countries(a))[datas[15]],
         "'average growth difference' : 0.029821664308110396, 'average "
         "production difference' : 1.9516826562373795")
Beispiel #3
0
    def conclusion_gdp_growth_prod(self, countries_list, year_range_2, production_type):
        '''
        Returns a list with the countries and their average gdp, growth and production in years_list
         '''

        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        growth = []
        prod = []
        diff_growth = []
        diff_prod = []
        f = Fao()
        diff=""
        year_range_1 = [year_range_2[0]-5, year_range_2[0]-1]

        for country in countries_list:

            for fao_country in f.list_countries():

                if self.similar(country, fao_country) == 1:
                    growth.append(list(self.average_growth([country], year_range_1).values())[0])
                    prod.append(
                        list(f.average_production([country], year_range_1).values())[0])

                    growth.append(list(self.average_growth([country], year_range_2).values())[0])
                    prod.append(
                        list(f.average_production([country], year_range_2).values())[0])
                    break
        for i in range(len(growth)):
            if growth[i] == None:
                growth[i] = 0
            if prod[i] == None:
                prod[i] = 0

        for i in range(0, len(growth), 2):
            diff_growth.append(growth[i + 1] - growth[i])
            diff_prod.append(prod[i + 1] - prod[i])

        for i in range(len(diff_prod)):
            diff += str(countries_list[i]) + ": 'average growth difference' : " + str(
                diff_growth[i]) + ", 'average production difference' : " + str(
                diff_prod[i]) + "\n"
        return diff

        session.close()
Beispiel #4
0
class TestFao(TestCase):
    def setUp(self):
        self.data = Fao()

    def test_countries(self):
        self.assertEqual(self.data.countries()[0], "Afghanistan")

    def test_products(self):
        self.assertEqual(self.data.products("Afghanistan")[0], "Wheat and products")

    def test_min(self):
        self.assertEqual(self.data.min(["Afghanistan"], ['Y1961', 'Y2013']), "a completer")

    def test_max(self):
        self.assertEqual(self.data.max(["Afghanistan"], ['Y1961', 'Y2013']), ["Wheat and products", "Y2013"])

    def test_av(self):
        self.assertEqual(self.data.av(["Afghanistan"], ['Y1961', 'Y1965'], "Wheat and products"), 1889.8)
Beispiel #5
0
class TestFao(TestCase):
    def setUp(self):
        self.data = Fao()

    def test_list_countries(self):
        countries = self.data.list_countries()
        self.assertEqual(countries[0], "Afghanistan")
        self.assertEqual(countries[42], "Cyprus")
        self.assertEqual(countries[-1], "Zimbabwe")
        self.assertEqual(countries[137], "Senegal")
        self.assertEqual(len(countries), 174)

    def test_list_products_countries(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.list_products_countries([countries[0]])[countries[0]][0],
            "Wheat and products")
        self.assertEqual(
            self.data.list_products_countries([countries[-1]
                                               ])[countries[-1]][0],
            "Wheat and products")

    def test_min_production(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.min_production([countries[0]],
                                     [2010, 2013])[countries[0]][0][-1], 0)

    def test_max_production(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.max_production([countries[0]],
                                     [2010, 2013])[countries[0]][-1], 5495)

    def test_average_production(self):
        countries = self.data.list_countries()
        self.assertEqual(
            self.data.average_production([countries[0], countries[42]],
                                         [1961, 1965], "Wheat and products"), {
                                             countries[0]: 1889.8,
                                             countries[42]: 67.4
                                         })
Beispiel #6
0
class ApiFao:
    def __init__(self):
        '''
        Initializes the class
        '''
        self.fao1 = Fao()

    def list_presents_countries(self):
        return self.fao1.countries()

    def list_products_of_country(self, country_name):
        return self.fao1.products(country_name)

    def dict_products_of_countries(self, countries_list):
        return self.fao1.country_prod(countries_list)

    def dict_max_of_countries_productions(self, countries_list, years_filter):
        return self.fao1.max(countries_list, years_filter)

    def dict_min_of_countries_productions(self, countries_list, years_filter):
        return self.fao1.min(countries_list, years_filter)

    def dict_avg_of_countries_productions(self, countries_list, years_filter,
                                          production_filter, food_or_feed):
        return self.fao1.av(countries_list, years_filter, production_filter,
                            food_or_feed)
Beispiel #7
0
 def setUp(self):
     self.data = Fao()
 def __init__(self):
     self.F = Fao()
class Analyse:
    '''
   Class with all the data analyse functions.
   '''
    def __init__(self):
        self.F = Fao()

    def list_countries(self):
        '''
        Return the list of all the countries
        :return: list_countries
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()
        list_countries = [
            x[0] for x in session.query(Countries.CountryName).all()
        ]
        session.close()
        return list_countries

    def geo_zone(self):
        '''
        Returns a dictionary that associates each country with it zone.
        :return: geo_dic
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        geo_dic = {}
        dataCountry = []
        dataZone = []

        with open("Book1.csv") as csv_file:
            for row in csv.reader(csv_file, delimiter=';'):
                dataCountry.append(row[0])
                dataZone.append(row[1])
        for i, country in enumerate(dataCountry):
            if country in self.list_countries():
                geo_dic[country] = dataZone[i]
        session.close()
        return geo_dic

    def countries_code(self):
        '''
        Returns the list of all the countries' codes.
        :return: countries_code
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()
        countries_code = [
            x[0] for x in session.query(Countries.CountryCode).all()
        ]
        session.close()
        return countries_code

    def code_to_name(self, code):
        '''
        Returns the name of the country from its country code.
        :param code:
        :return: name
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()
        name = session.query(
            Countries.CountryName).filter_by(CountryCode=code).first()[0]
        session.close()
        return name

    def name_to_code(self, name):
        '''
        Returns the name of the country from its country code.
        :param name:
        :return: code
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()
        code = session.query(
            Countries.CountryCode).filter_by(CountryName=name).first()[0]
        session.close()
        return code

    def similar(self, a, b):
        return SequenceMatcher(None, a, b).ratio()

    def countries_data(self, countries, years):
        '''
        Returns a dictionary of countries with each country a list of years with a data set (gdp and growth).
        :param countries:
        :param years:
        :return: countries_data_dic
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        countries_data_dic = {}
        years_vect = [x for x in range(years[0], years[1] + 1)]
        for country in countries:
            countries_data_dic[country] = list(session.query(Gdp.Year, Gdp.gdp, Gdp.growth)\
                .filter_by(CountryCode=self.name_to_code(country)).filter(Gdp.gdp != '').filter(Gdp.Year.in_(years_vect)).all())
            for i, elt in enumerate(countries_data_dic[country]):
                countries_data_dic[country][i] = list(
                    countries_data_dic[country][i])

        session.close()
        return countries_data_dic

    def average_gdp(self, countries, years):
        '''
        Returns the average value of the gdp for a country list and a fixed period.
        :param countries:
        :param years:
        :return: av_dic
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        av_dic = {}
        for country in countries:
            CC = self.name_to_code(country)
            years_vec = [x for x in range(years[0], years[1] + 1)]
            av_dic[country] = session.query(func.avg(Gdp.gdp))\
                .filter_by(CountryCode=CC).filter(Gdp.gdp != '').filter(Gdp.Year.in_(years_vec)).first()[0]
        session.close()
        return av_dic

    def average_growth(self, countries, years):
        '''
        Returns the average value of the growth for a country list and a fixed period.
        :param countries:
        :param years:
        :return: av_dic
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        av_dic = {}
        for country in countries:
            CC = self.name_to_code(country)
            years_vect = [x for x in range(years[0], years[1] + 1)]
            av_dic[country] = session.query(func.avg(Gdp.growth)) \
                .filter_by(CountryCode=CC).filter(Gdp.growth != '').filter(Gdp.Year.in_(years_vect)).first()[0]
        session.close()
        return av_dic

    def world_health(self, years):
        '''
        Returns the world health and the geographic place of the countries in crisis
        :param years:
        :return: world health, region dic and unknown countries number
        '''
        crisis = 0
        exception = 0
        health = 0
        unknown_countries = 0
        countries_crisis = []

        dic = self.geo_zone()
        dic_keys = list(dic.keys())
        region_dic ={'Asia & Pacific':0 ,'Europe':0 , 'Arab States':0 , 'Africa':0 , \
                      'South/Latin America':0 , 'Unknown':0, 'North America':0}

        for country in self.list_countries():

            past_gdp = self.average_growth(
                [country], [years[0] - 5, years[0] - 1])[country]
            now_gdp = self.average_growth([country], years)[country]

            if past_gdp == None or now_gdp == None:
                exception += 1
            else:

                if now_gdp < past_gdp:
                    crisis += 1
                    countries_crisis.append(country)

                    for elt in dic_keys:
                        if self.similar(elt, country) > 0.7:
                            region = dic[elt]
                            #print(country,region)
                            region_dic[region] += 1
                            break

                else:
                    health += 1

        # production_conclusion = self.production_growth(countries_crisis[:5], years)
        print('*********************************************************\n')
        # print(production_conclusion)

        list_countries_len = len(self.list_countries())
        health_percentage = (health / list_countries_len) * 100
        crisis_percentage = (crisis / list_countries_len) * 100
        exception_percentage = (exception / list_countries_len) * 100

        print('\nFor the year {} to {} :\n'.format(years[0], years[1]))

        print("Percentage of healthy countries : {}% \nPercentage of countries in crisis : {}% \nPercentage of not enougth data : {}%\n"\
              .format(round(health_percentage), round(crisis_percentage), round(exception_percentage)))

        max_of_three = max(
            [health_percentage, crisis_percentage, exception_percentage])

        if max_of_three == exception_percentage:
            return ('Not enought data')

        elif max_of_three == crisis_percentage:
            print('World in crisis !\n')
            return region_dic, unknown_countries

        elif max_of_three == health_percentage:
            print('World is in good shape !\n')
            return region_dic, unknown_countries

    def min_gdp(self, listOfCountries, years):
        '''
        Returns the minimum gdp of each countries of listOfCountries for the given period years, and the global minimum
        :param listOfCountries:
        :param years:
        :return: global min and min for each country
        '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        list_of_code = {}
        for country in listOfCountries:
            name = self.name_to_code(country)
            years_vect = [x for x in range(years[0], years[1] + 1)]
            list_of_code[country] = \
                session.query(func.min(Gdp.gdp)).filter_by(CountryCode=name).filter(Gdp.Year.in_(years_vect)).first()[0]
        for elt in list(list_of_code.items()):
            if elt[1] == '':
                list_of_code[elt[0]] = 0
        session.close()
        return min(list(list_of_code.values())), list_of_code

    def max_gdp(self, listOfCountries, years):
        '''
         Returns the maximum gdp of each countries of listOfCountries for the given period years, and the global maximum
         :param listOfCountries:
         :param years:
         :return: global max and max for each country
         '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        list_of_code = {}
        for country in listOfCountries:
            name = self.name_to_code(country)
            years_vect = [x for x in range(years[0], years[1] + 1)]
            list_of_code[country] = \
                session.query(func.max(Gdp.gdp)).filter_by(CountryCode=name).filter(Gdp.Year.in_(years_vect)).first()[0]
        for elt in list(list_of_code.items()):
            if elt[1] == '':
                list_of_code[elt[0]] = 0
        session.close()
        return max(list(list_of_code.values())), list_of_code

    def min_growth(self, listOfCountries, years):
        '''
         Returns the minimum growth of each countries of listOfCountries for the given period years, and the global minimum
         :param listOfCountries:
         :param years:
         :return: global min and min for each country
         '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        list_of_code = {}
        for country in listOfCountries:
            name = self.name_to_code(country)
            years_vect = [x for x in range(years[0], years[1] + 1)]
            list_of_code[country] = \
                session.query(func.min(Gdp.growth)).filter_by(CountryCode=name).filter(
                    Gdp.Year.in_(years_vect)).first()[0]
        for elt in list(list_of_code.items()):
            if elt[1] == '':
                list_of_code[elt[0]] = 0
        session.close()
        return min(list(list_of_code.values())), list_of_code

    def max_growth(self, listOfCountries, years):
        '''
         Returns the maximum growth of each countries of listOfCountries for the given period years, and the global maximum
         :param listOfCountries:
         :param years:
         :return: global max and max for each country
         '''
        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        list_of_code = {}
        for country in listOfCountries:
            name = self.name_to_code(country)
            years_vect = [x for x in range(years[0], years[1] + 1)]
            list_of_code[country] = \
                session.query(func.max(Gdp.growth)).filter_by(CountryCode=name).filter(
                    Gdp.Year.in_(years_vect)).first()[0]
        for elt in list(list_of_code.items()):
            if elt[1] == '':
                list_of_code[elt[0]] = 0
        session.close()
        return max(list(list_of_code.values())), list_of_code

    def production_growth(self, country_list, years):
        production_dic = {}
        for prod in self.F.list_products():
            production_dic[prod] = [0, 0]

        for country in country_list:
            for prod in self.F.list_products_country(country):
                production_dic[prod][0] += self.F.average_production(
                    [country], years, prod)[country]
                production_dic[prod][1] += self.F.average_production(
                    [country], [years[0] - 5, years[0] - 1], prod)[country]

        conclusion = {'last 5 years growth': 0, 'period growth': 0}
        for prod in list(production_dic.keys()):
            conclusion['last 5 years growth'] += production_dic[prod][1]
            conclusion['period growth'] += production_dic[prod][0]

        return conclusion

    def av_gdp_growth_prod(self, countries_list, years_list, production_type):
        '''
        Returns a list with the countries and their average gdp, growth and production in years_list
        '''

        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        gdp = []
        growth = []
        prod = []
        f = Fao()
        result_list = {}
        for country in countries_list:
            growth.append(
                list(self.average_growth([country], years_list).values())[0])
            gdp.append(
                list(self.average_gdp([country], years_list).values())[0])
            prod.append(
                list(
                    f.average_production([country], years_list,
                                         production_type, "Food").values())[0])

            result_list[country] = [
                "average growth: " + str(
                    list(self.average_growth(
                        [country], years_list).values())[0]), "average gdp: " +
                str(list(self.average_gdp([country], years_list).values())[0]),
                "average production: " + str(
                    list(
                        f.average_production([country], years_list,
                                             production_type,
                                             "Food").values())[0])
            ]

        return result_list
        print(prod)
        session.close()

    def conclusion_gdp_growth_prod(self, countries_list, year_range_1,
                                   year_range_2, production_type):
        '''
        Returns a list with the countries and their average gdp, growth and production in years_list
         '''

        data_tables = create_engine('sqlite:///world-gdp.db')
        Session = sessionmaker(bind=data_tables)
        session = Session()

        gdp = []
        growth = []
        prod = []
        diff_growth = []
        diff_gdp = []
        diff_prod = []
        f = Fao()
        diff = []

        for country in countries_list:

            for fao_country in f.list_countries():

                if self.similar(country, fao_country) == 1:
                    growth.append(
                        list(
                            self.average_growth([country],
                                                year_range_1).values())[0])
                    gdp.append(
                        list(
                            self.average_gdp([country],
                                             year_range_1).values())[0])
                    prod.append(
                        list(
                            f.average_production([country], year_range_1,
                                                 production_type).values())[0])

                    growth.append(
                        list(
                            self.average_growth([country],
                                                year_range_2).values())[0])
                    gdp.append(
                        list(
                            self.average_gdp([country],
                                             year_range_2).values())[0])
                    prod.append(
                        list(
                            f.average_production([country], year_range_2,
                                                 production_type).values())[0])
                    break
        for i in range(len(growth)):
            if growth[i] == None:
                growth[i] = 0
            if gdp[i] == None:
                gdp[i] = 0
            if prod[i] == None:
                prod[i] = 0

        for i in range(0, len(countries_list), 2):
            diff_growth.append(growth[i + 1] - growth[i])
            diff_gdp.append(gdp[i + 1] - gdp[i])
            diff_prod.append(prod[i + 1] - prod[i])

        for i in range(len(diff_prod)):
            diff += [
                str(countries_list[i]) + ": 'growth difference' : " +
                str(diff_growth[i]) + ": 'gdp difference' : " +
                str(diff_gdp[i]) + ", production difference : " +
                str(diff_prod[i])
            ]
            print('\n')
        return diff

        session.close()
Beispiel #10
0
        for i in range(len(diff_prod)):
            diff += [
                str(countries_list[i]) + ": 'growth difference' : " +
                str(diff_growth[i]) + ": 'gdp difference' : " +
                str(diff_gdp[i]) + ", production difference : " +
                str(diff_prod[i])
            ]
            print('\n')
        return diff

        session.close()


L = [
    'France', 'Brazil', 'Angola', 'Spain', 'Algeria', 'Madagascar', 'Mali',
    'Argentina'
]
a = ['Brazil', 'Angola', 'Algeria', 'Madagascar', 'Argentina']

A = Analyse()
f = Fao()

print(A.world_health([1981, 1983]))

print(
    A.conclusion_gdp_growth_prod(L, [1976, 1980], [1981, 1983],
                                 f.list_products_countries(L)))

print(
    A.conclusion_gdp_growth_prod(a, [2000, 2001], [2003, 2006],
                                 f.list_products_countries(a)))
Beispiel #11
0
                        list(f.average_production([country], year_range_2).values())[0])
                    break
        for i in range(len(growth)):
            if growth[i] == None:
                growth[i] = 0
            if prod[i] == None:
                prod[i] = 0

        for i in range(0, len(growth), 2):
            diff_growth.append(growth[i + 1] - growth[i])
            diff_prod.append(prod[i + 1] - prod[i])

        for i in range(len(diff_prod)):
            diff += str(countries_list[i]) + ": 'average growth difference' : " + str(
                diff_growth[i]) + ", 'average production difference' : " + str(
                diff_prod[i]) + "\n"
        return diff

        session.close()

L = ['France', 'Brazil', 'Angola', 'Spain', 'Algeria', 'Madagascar', 'Mali', 'Argentina']


A = Analyse()
f = Fao()

print(A.world_health([1981, 1983]))

print(A.conclusion_gdp_growth_prod(L, [1981, 1983], f.list_products_countries(L)))
print(A.conclusion_gdp_growth_prod(L, [2003, 2006], f.list_products_countries(L)))
Beispiel #12
0
 def __init__(self):
     '''
     Initializes the class
     '''
     self.fao1 = Fao()