<commit_before># -*- coding: utf-8 *-*
"""Module with main classes related to Switches"""
import logging
from socket import error as SocketError
from socket import socket as Socket

from kyco.constants import CONNECTION_TIMEOUT
from kyco.utils import now

__all__ = ('Switch',)

log = logging.getLogger('Kyco')

class Connection(object):
        def __init__(self, address, port, socket, switch=None):
            self.address = address
            self.port = port
            self.socket = socket
            self.switch = switch

        @property
        def id(self):
            return (self.address, self.port)

        def send(self, buffer):
            try:
                self.socket.send(buffer)
            except (OSError, SocketError) as exception:
                self.close()
                # TODO: Raise or create an error event?
                raise exception

        def close(self):
            if self.socket is not None:
                self.socket.close()
                self.socket = None  # TODO: Is this really necessary?

            if self.switch.connection is self:
                self.switch.connection = None

        def is_connected(self):
            return self.socket is not None

        def update_switch(self, switch):
            self.switch = switch
            self.switch.connection = self


class Switch(object):
    """This is the main class related to Switches modeled on Kyco.

    A new Switch will be created every time the handshake process is done
    (after receiving the first FeaturesReply). Considering this, the
    :attr:`socket`, :attr:`connection_id`, :attr:`of_version` and<commit_msg>fixed wrong indentation<commit_after># -*- coding: utf-8 *-*
"""Module with main classes related to Switches"""
import logging
from socket import error as SocketError
from socket import socket as Socket

from kyco.constants import CONNECTION_TIMEOUT
from kyco.utils import now

__all__ = ('Switch',)

log = logging.getLogger('Kyco')

class Connection(object):
    def __init__(self, address, port, socket, switch=None):
        self.address = address
        self.port = port
        self.socket = socket
        self.switch = switch

    @property
    def id(self):
        return (self.address, self.port)

    def send(self, buffer):
        try:
            self.socket.send(buffer)
        except (OSError, SocketError) as exception:
            self.close()
            # TODO: Raise or create an error event?
            raise exception

    def close(self):
        if self.socket is not None:
            self.socket.close()
            self.socket = None  # TODO: Is this really necessary?

        if self.switch.connection is self:
            self.switch.connection = None

    def is_connected(self):
        return self.socket is not None

    def update_switch(self, switch):
        self.switch = switch
        self.switch.connection = self


class Switch(object):
    """This is the main class related to Switches modeled on Kyco.

    A new Switch will be created every time the handshake process is done
    (after receiving the first FeaturesReply). Considering this, the
    :attr:`socket`, :attr:`connection_id`, :attr:`of_version` and
{'fraction_non_alphanumeric': 0.08530510585305105, 'fraction_numerical': 0.0006226650062266501, 'mean_word_length': 2.308959835221421, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6394841', 'n_tokens_mistral': 861, 'n_tokens_neox': 823, 'n_words': 303}