コード例 #1
0
 def open(self):
     """
     Open voters file for on-the-fly parsing.
     """
     self.total_voters = count_lines(self._filename)
     self._voters_file = open_with_encoding(self._filename, 'rb',
                                            'iso8859-15')
     self._voters_iter = enumerate(self._voters_file, 1)
コード例 #2
0
ファイル: readers.py プロジェクト: mivotico/tse2sql
 def open(self):
     """
     Open voters file for on-the-fly parsing.
     """
     self.total_voters = count_lines(self._filename)
     self._voters_file = open_with_encoding(
         self._filename, 'rb', 'iso8859-15'
     )
     self._voters_iter = enumerate(self._voters_file, 1)
コード例 #3
0
ファイル: readers.py プロジェクト: mivotico/tse2sql
    def parse(self):
        """
        Open and parse the ``Distelec.txt`` file.

        After parsing the following attributes will be available:

        :var provinces: Dictionary with province id as key and name as value.
        :var cantons: Dictionary with a tuple ``(province id, canton id)`` as
         key and name as value.
        :var districts: Dictionary with a tuple
         ``(province id, canton id, district id)`` as key and name as value.
        """
        with open_with_encoding(self._filename, 'rb', 'iso8859-15') as fd:
            for linenum, line in enumerate(fd, 1):
                line = line.strip()

                if not line:
                    log.warning(
                        'Distelec.txt :: Ignoring empty line #{}'.format(
                            linenum
                        )
                    )
                    continue

                try:
                    parts = line.split(',')
                    assert len(parts) == 4

                    # Get codes
                    code = int(parts[0])

                    # Insert province
                    province_code = code // 100000
                    province_name = titleize(parts[1].strip())

                    if province_code in self.provinces:
                        assert self.provinces[province_code] == province_name
                    else:
                        self.provinces[province_code] = province_name

                    # Insert canton
                    canton_code = (code % 100000) // 1000
                    canton_key = (province_code, canton_code)
                    canton_name = titleize(parts[2].strip())

                    if canton_code in self.cantons:
                        assert self.cantons[canton_key] == canton_name
                    else:
                        self.cantons[canton_key] = canton_name

                    # Insert district
                    district_code = code % 1000
                    district_key = (province_code, canton_code, district_code)
                    district_name = titleize(parts[3].strip())
                    if district_code in self.districts:
                        assert self.districts[district_key] == district_name
                    else:
                        self.districts[district_key] = district_name

                except Exception:
                    self._bad_data.append(linenum)
                    log.error(
                        'Distelec.txt :: Bad data at line #{}:\n{}'.format(
                            linenum, line
                        )
                    )
                    log.debug(format_exc())
                    continue
コード例 #4
0
    def parse(self):
        """
        Open and parse the ``Distelec.txt`` file.

        After parsing the following attributes will be available:

        :var provinces: Dictionary with province id as key and name as value.
        :var cantons: Dictionary with a tuple ``(province id, canton id)`` as
         key and name as value.
        :var districts: Dictionary with a tuple
         ``(province id, canton id, district id)`` as key and name as value.
        """
        with open_with_encoding(self._filename, 'rb', 'iso8859-15') as fd:
            for linenum, line in enumerate(fd, 1):
                line = line.strip()

                if not line:
                    log.warning(
                        'Distelec.txt :: Ignoring empty line #{}'.format(
                            linenum))
                    continue

                try:
                    parts = line.split(',')
                    assert len(parts) == 4

                    # Get codes
                    code = int(parts[0])

                    # Insert province
                    province_code = code // 100000
                    province_name = titleize(parts[1].strip())

                    if province_code in self.provinces:
                        assert self.provinces[province_code] == province_name
                    else:
                        self.provinces[province_code] = province_name

                    # Insert canton
                    canton_code = (code % 100000) // 1000
                    canton_key = (province_code, canton_code)
                    canton_name = titleize(parts[2].strip())

                    if canton_code in self.cantons:
                        assert self.cantons[canton_key] == canton_name
                    else:
                        self.cantons[canton_key] = canton_name

                    # Insert district
                    district_code = code % 1000
                    district_key = (province_code, canton_code, district_code)
                    district_name = titleize(parts[3].strip())
                    if district_code in self.districts:
                        assert self.districts[district_key] == district_name
                    else:
                        self.districts[district_key] = district_name

                except Exception:
                    self._bad_data.append(linenum)
                    log.error(
                        'Distelec.txt :: Bad data at line #{}:\n{}'.format(
                            linenum, line))
                    log.debug(format_exc())
                    continue