data.py
def __fetch_data(self, nuts):
        """
        Download elections data from volby.cz based on selected NUTS (something between region and district) and extract
        usedful data
        :param nuts: something between region and district, e.g. "CZ0412", see nuts_dial.txt
        :return: array[Dict of extracted data]
        """

        url = "https://volby.cz/pls/ps2017nss/vysledky_okres?nuts={}".format(
            nuts)
        r = requests.get(url, allow_redirects=True)

        while r.status_code != 200:
            r = requests.get(url, allow_redirects=True)
            print('Retrying {}!'.format(nuts))

        filename = '{}.xml'.format(nuts)
        open(filename, 'wb').write(r.content)

        tmp_data = []  # performing concat once finished is cheaper that an append in foreach

        with open(filename) as xml_file:
            data_dict = xmltodict.parse(xml_file.read())
            res = data_dict.get("VYSLEDKY_OKRES")
            district = res.get("OKRES")
            cities = res.get("OBEC")

            for city in cities:
                votes = city.get("HLASY_STRANA")
                for party in votes:
                    tmp_data.append(
                        {
                            'district_name': district["@NAZ_OKRES"],
                            'city_id': city["@CIS_OBEC"],
                            'city_name': city["@NAZ_OBEC"],
                            'party': party["@KSTRANA"],
                            'party_votes_percent': party["@PROC_HLASU"],
                            'total_votes': city["UCAST"]["@PLATNE_HLASY"]
                        }
                    )

        os.remove(filename)
        # need to add data to pandas in the main thread, otherwise it causes data loss
        return tmp_data

        Download elections data from volby.cz based on selected NUTS (something between region and district) and extract
        usedful data
        :param nuts: something between region and district, e.g. "CZ0412", see nuts_dial.txt
        :return: array[Dict of extracted data]
        
Download elections data from volby.cz based on selected NUTS (something between region and district) and extract
usedful data
Download elections data from volby . cz based on selected NUTS ( something between region and district ) and extract usedful data
def __fetch_data(self, nuts):
        url = "https://volby.cz/pls/ps2017nss/vysledky_okres?nuts={}".format(
            nuts)
        r = requests.get(url, allow_redirects=True)
        while r.status_code != 200:
            r = requests.get(url, allow_redirects=True)
            print('Retrying {}!'.format(nuts))
        filename = '{}.xml'.format(nuts)
        open(filename, 'wb').write(r.content)
        tmp_data = []  
        with open(filename) as xml_file:
            data_dict = xmltodict.parse(xml_file.read())
            res = data_dict.get("VYSLEDKY_OKRES")
            district = res.get("OKRES")
            cities = res.get("OBEC")
            for city in cities:
                votes = city.get("HLASY_STRANA")
                for party in votes:
                    tmp_data.append(
                        {
                            'district_name': district["@NAZ_OKRES"],
                            'city_id': city["@CIS_OBEC"],
                            'city_name': city["@NAZ_OBEC"],
                            'party': party["@KSTRANA"],
                            'party_votes_percent': party["@PROC_HLASU"],
                            'total_votes': city["UCAST"]["@PLATNE_HLASY"]
                        }
                    )
        os.remove(filename)
        return tmp_data
def __fetch_data ( self , nuts ) : url = "https://volby.cz/pls/ps2017nss/vysledky_okres?nuts={}" . format ( nuts ) r = requests . get ( url , allow_redirects = True ) while r . status_code != 200 : r = requests . get ( url , allow_redirects = True ) print ( 'Retrying {}!' . format ( nuts ) ) filename = '{}.xml' . format ( nuts ) open ( filename , 'wb' ) . write ( r . content ) tmp_data = [ ] with open ( filename ) as xml_file : data_dict = xmltodict . parse ( xml_file . read ( ) ) res = data_dict . get ( "VYSLEDKY_OKRES" ) district = res . get ( "OKRES" ) cities = res . get ( "OBEC" ) for city in cities : votes = city . get ( "HLASY_STRANA" ) for party in votes : tmp_data . append ( { 'district_name' : district [ "@NAZ_OKRES" ] , 'city_id' : city [ "@CIS_OBEC" ] , 'city_name' : city [ "@NAZ_OBEC" ] , 'party' : party [ "@KSTRANA" ] , 'party_votes_percent' : party [ "@PROC_HLASU" ] , 'total_votes' : city [ "UCAST" ] [ "@PLATNE_HLASY" ] } ) os . remove ( filename ) return tmp_data
Download elections data from volby.cz based on selected NUTS (something between region and district) and extract
usedful data
Download elections data from volby . cz based on selected NUTS ( something between region and district ) and extract usedful data
"""
        Download elections data from volby.cz based on selected NUTS (something between region and district) and extract
        usedful data
        :param nuts: something between region and district, e.g. "CZ0412", see nuts_dial.txt
        :return: array[Dict of extracted data]
        """ # performing concat once finished is cheaper that an append in foreach # need to add data to pandas in the main thread, otherwise it causes data loss
array[Dict of extracted data]
array [ Dict of extracted data ]
self
nuts
something between region and district, e.g.
something between region and district e . g .