<commit_before>from collections import defaultdict
from uuid import uuid4


class _Packet:
    _pid = 0

    @classmethod
    def _next_pid(cls):
        from uuid import uuid4

        return str(uuid4())

    @classmethod
    def ack(cls, request_id):
        return {'pid': cls._next_pid(), 'type': 'ack', 'request_id': request_id}

    @classmethod
    def pong(cls, node_id, payload=None):
        return cls._get_ping_pong(node_id, 'pong', payload=payload)

    @classmethod
    def ping(cls, node_id, payload=None):
        return cls._get_ping_pong(node_id, 'ping', payload=payload)

    @classmethod
    def _get_ping_pong(cls, node_id, packet_type, payload=None):
        if payload:
            return {'pid': cls._next_pid(), 'type': packet_type, 'node_id': node_id, 'payload': payload}
        return {'pid': cls._next_pid(), 'type': packet_type, 'node_id': node_id}


class ControlPacket(_Packet):

    @classmethod
    def registration(cls, ip: str, port: int, node_id, service: str, version: str, dependencies, service_type: str):
        v = [{'service': vendor.name, 'version': vendor.version} for vendor in dependencies]

        params = {'service': service,
                  'version': version,
                  'host': ip,
                  'port': port,
                  'node_id': node_id,
                  'dependencies': v,
                  'type': service_type}

        packet = {'pid': cls._next_pid(), 'type': 'register', 'params': params}
        return packet

    @classmethod
    def get_instances(cls, service, version):
        params = {'service': service, 'version': version}
        packet = {'pid': cls._next_pid(),
                  'type': 'get_instances',<commit_msg>Reduced code repetition in _Packet._get_ping_pong<commit_after>from collections import defaultdict
from uuid import uuid4


class _Packet:
    _pid = 0

    @classmethod
    def _next_pid(cls):
        from uuid import uuid4

        return str(uuid4())

    @classmethod
    def ack(cls, request_id):
        return {'pid': cls._next_pid(), 'type': 'ack', 'request_id': request_id}

    @classmethod
    def pong(cls, node_id, payload=None):
        return cls._get_ping_pong(node_id, 'pong', payload=payload)

    @classmethod
    def ping(cls, node_id, payload=None):
        return cls._get_ping_pong(node_id, 'ping', payload=payload)

    @classmethod
    def _get_ping_pong(cls, node_id, packet_type, payload=None):
        return_dict = {'pid': cls._next_pid(), 'type': packet_type, 'node_id': node_id}
        if payload:
            return_dict['payload'] = payload
        return return_dict


class ControlPacket(_Packet):

    @classmethod
    def registration(cls, ip: str, port: int, node_id, service: str, version: str, dependencies, service_type: str):
        v = [{'service': vendor.name, 'version': vendor.version} for vendor in dependencies]

        params = {'service': service,
                  'version': version,
                  'host': ip,
                  'port': port,
                  'node_id': node_id,
                  'dependencies': v,
                  'type': service_type}

        packet = {'pid': cls._next_pid(), 'type': 'register', 'params': params}
        return packet

    @classmethod
    def get_instances(cls, service, version):
        params = {'service': service, 'version': version}
        packet = {'pid': cls._next_pid(),
                  'type': 'get_instances',
{'fraction_non_alphanumeric': 0.12645348837209303, 'fraction_numerical': 0.002325581395348837, 'mean_word_length': 2.6297468354430378, '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': '3343051', 'n_tokens_mistral': 1051, 'n_tokens_neox': 1030, 'n_words': 276}