<commit_before>from .messages import parse, serialize


def _rindex(mylist, elem):
    return len(mylist) - mylist[::-1].index(elem) - 1


def to_delimited(header, payload):
    return tuple(header) + (b'',) + tuple(payload)


def from_delimited(msgs):
    delim = _rindex(msgs, b'')
    header, payload = tuple(msgs[:delim]), tuple(msgs[delim + 1:])
    return header, payload


class DealerRouterWrapper:
    """
    A wrapper for ZMQ dealer & router sockets used to send Hedgehog Protobuf messages.

    Each message consists of zero or more (opaque, binary) header frames, one (empty) delimiter frame,
    and one or more command frames (valid, Protobuf-encoded `HedgehogMessage`s).
    Serializing/parsing is handled by this class.
<commit_msg>add raw parameter to delimiter functions<commit_after>from .messages import parse, serialize


def _rindex(mylist, elem):
    return len(mylist) - mylist[::-1].index(elem) - 1


def to_delimited(header, payload, raw=True):
    msgs_raw = tuple(payload) if raw else tuple(serialize(msg) for msg in payload)
    return tuple(header) + (b'',) + msgs_raw


def from_delimited(msgs, raw=True):
    delim = _rindex(msgs, b'')
    header, payload = tuple(msgs[:delim]), tuple(msgs[delim + 1:])
    return header, payload if raw else tuple(parse(msg_raw) for msg_raw in payload)


class DealerRouterWrapper:
    """
    A wrapper for ZMQ dealer & router sockets used to send Hedgehog Protobuf messages.

    Each message consists of zero or more (opaque, binary) header frames, one (empty) delimiter frame,
    and one or more command frames (valid, Protobuf-encoded `HedgehogMessage`s).
    Serializing/parsing is handled by this class.

{'fraction_non_alphanumeric': 0.10541989279332936, 'fraction_numerical': 0.00357355568790947, 'mean_word_length': 4.233644859813084, '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': '3716884', 'n_tokens_mistral': 552, 'n_tokens_neox': 519, 'n_words': 179}