Beispiel #1
0
def main(argv):
    if len(argv) != 2:
        return 1

    input_file = argv[1]

    file_reader = FileReader(input_file)
    threads = []

    while True:
        try:
            raw_world = file_reader.get_world()
        except StopIteration:
            break
        except FormatError:
            continue

        world = World(len(threads) + 1, raw_world)

        thread = world.run()
        if thread:
            threads.append(thread)
            thread.start()
        else:
            Country.clean_cash(len(threads) + 1)

    for thread in threads:
        thread.join()

    res = sorted(World.result)

    for key in res:
        print("Case Number ", key)
        for country in sorted(World.result[key]):
            print(country[1], country[0])
Beispiel #2
0
 def run(self):
     if self.is_init:
         if self.check_connections():
             return Thread(target=self.world_handler)
         else:
             print("Not all country can be connect. World: {}".format(
                 self.countries_names()))
             Country.clean_cash(self.__world_id)
             return None
Beispiel #3
0
    def __parse(self, raw_world):
        reg_pattern = r'\b[A-Za-z]{1,25}\b(\s\d){4}'

        regex = re.compile(reg_pattern)

        if not raw_world:
            return None

        for raw_country in raw_world:
            if regex.match(raw_country):
                country_name, coords = raw_country.split(maxsplit=1)
                try:
                    xl, yl, xh, yh = coords.split()
                except ValueError:
                    print("Must be four cordinates: {}".format(coords))
                    Country.clean_cash(self.__world_id)
                    return

                xl, yl, xh, yh = int(xl), int(yl), int(xh), int(yh)

                if xl > xh or yl > yh:
                    print(
                        "Second coord must be bigger or equal that first: {}".
                        format(coords))
                    Country.clean_cash(self.__world_id)
                    return

                if xl <= 0 or xh <= 0 or yl <= 0 or yh <= 0:
                    print("Coords have to be positive".format(coords))
                    Country.clean_cash(self.__world_id)
                    return

                bottom_point = Point(xl, yl)
                top_point = Point(xh, yh)

                self._countries.append(
                    Country(bottom_point, top_point, country_name,
                            self.__world_id))
            else:
                print("Country format error: {}".format(raw_country))
                Country.clean_cash(self.__world_id)
                return

        self.is_init = True