Exemplo n.º 1
0
 def add_point(self):
     if len(self.points) < get_settings().get("max_polygon_points"):
         self.points.append(
             Point(randint(0,
                           get_settings()["image_size"][0]),
                   randint(0,
                           get_settings()["image_size"][1])))
Exemplo n.º 2
0
    def create_random():
        """
        Creates a random polygon.
        :return: None.
        """
        point_amount = randint(2, get_settings()["max_polygon_points"])
        points = []
        fill = (randint(0, 255), randint(0,
                                         255), randint(0,
                                                       255), randint(0, 255))

        for i in range(point_amount):
            points.append(
                Point(randint(0,
                              get_settings()["image_size"][0]),
                      randint(0,
                              get_settings()["image_size"][1])))
        return Polygon(points, fill)
Exemplo n.º 3
0
 def produce_image(self):
     """
     This function produces an image made based on this drawing.
     :return:
     """
     img = Image.new('RGB', get_settings()["image_size"])
     draw = ImageDraw.Draw(img, 'RGBA')
     self.draw(draw)
     return img
Exemplo n.º 4
0
 def mutate(self):
     """
     Mutates this instance.
     :return: the offspring created by the mutation opeartion.
     """
     offspring = GeneticDrawing()
     for i in range(len(self.polygons)):
         offspring.polygons.append(self.polygons[i].copy())
         offspring.polygons[-1].mutate()
     if sample_probability(
             get_settings().get("drawing_add_polygon_mutation_rate")):
         offspring.add_polygon()
     if sample_probability(
             get_settings().get("drawing_remove_polygon_mutation_rate")):
         offspring.remove_polygon()
     if sample_probability(
             get_settings().get("drawing_move_polygon_mutation_rate")):
         offspring.move_polygon_order()
     return offspring
Exemplo n.º 5
0
def main(filename):
    settings = get_settings(filename)
    connection_string = settings["RabbitMqConnectionString"]
    exchange_name = settings["RabbitMqCommandExchangeName"]

    exchange = Exchange(exchange_name)

    with Connection(connection_string) as conn:
        producer = conn.Producer()
        producer.publish("Hello from within python3", exchange=exchange)
Exemplo n.º 6
0
 def mutate(self):
     """
     Mutates this instance in-place.
     :return: None.
     """
     if sample_probability(
             get_settings().get("polygon_add_point_mutation_rate")):
         self.add_point()
     if sample_probability(
             get_settings().get("polygon_remove_point_mutation_rate")):
         self.remove_point()
     if sample_probability(
             get_settings().get("polygon_color_mutation_rate")):
         self.mutate_color_red()
     if sample_probability(
             get_settings().get("polygon_color_mutation_rate")):
         self.mutate_color_green()
     if sample_probability(
             get_settings().get("polygon_color_mutation_rate")):
         self.mutate_color_blue()
     if sample_probability(
             get_settings().get("polygon_color_mutation_rate")):
         self.mutate_alpha()
     for point in self.points:
         point.mutate()
Exemplo n.º 7
0
def start_receiving(filename):
    # TODO: Send error to separate channel
    # TODO: Log to file in case of missing communication
    settings = get_settings(filename)
    connection_string = settings["RabbitMqConnectionString"]
    queue_name = settings["RabbitMqDeviceQueueName"]
    device_id = settings["DeviceId"]
    exchange = settings["RabbitMqDeviceResponseExchangeName"]
    heartbeat_interval_in_seconds = int(settings["HeartbeatIntervalInSeconds"])

    queue = Queue(queue_name)

    running = True
    heartbeat = datetime.datetime(1970, 1, 1)

    while running:
        with Connection(connection_string, heartbeat=(heartbeat_interval_in_seconds * 2)) as conn:
            reconnect(conn)
            while running:
                try:
                    with conn.Consumer(queue, callbacks=[process_message]) as consumer:
                        with producers[conn].acquire(block=True) as producer:
                            log_info("Running")
                            consumer.consume()
                            while running:
                                try:
                                    reconnect(conn)
                                    temp_heartbeat = datetime.datetime.utcnow()
                                    if (temp_heartbeat - heartbeat).total_seconds() > heartbeat_interval_in_seconds:
                                        send_heartbeat(producer, device_id, exchange)
                                        log_info("Heartbeat: {0}".format(temp_heartbeat))
                                        heartbeat = temp_heartbeat
                                    conn.heartbeat_check()
                                    conn.drain_events(timeout=1)
                                except KeyboardInterrupt:
                                    running = False
                                except socket.timeout:
                                    pass
                                except ConnectionResetError:
                                    log_error("ConnectionResetError")
                                    reconnect(conn)
                except RecoverableConnectionError:
                    log_error("RecoverableConnectionError")
                except BrokenPipeError:
                    log_error("BrokenPipeError")
                except TimeoutError:
                    log_error("TimeoutError")
Exemplo n.º 8
0
 def mutate(self):
     """
     Mutates this instance.
     :return: None.
     """
     if sample_probability(get_settings().get("point_move_mutation_rate")):
         self.x = randint(0, get_settings()["image_size"][0])
         self.y = randint(0, get_settings()["image_size"][1])
     if sample_probability(get_settings().get("point_move_mutation_rate")):
         self.x = max(
             0,
             min(get_settings()["image_size"][0],
                 self.x + randint(-20, 20)))
         self.y = max(
             0,
             min(get_settings()["image_size"][1],
                 self.y + randint(-20, 20)))
Exemplo n.º 9
0
 def add_polygon(self):
     if len(self.polygons) < get_settings()["max_polygons"]:
         self.polygons.append(Polygon.create_random())