<commit_before># coding=utf-8
import pickle
import socket
import sys
import hmac
import traceback
import struct
import logging

import util

# From the docs:
#  Threads interact strangely with interrupts: the KeyboardInterrupt exception
#  will be received by an arbitrary thread. (When the signal module is
#  available, interrupts always go to the main thread.)

from threading import Thread

from .logger import Logger


class NetworkLogger(Logger):
    """Send our results over the network to another instance."""

    supports_batch = True

    def __init__(self, config_options):
        Logger.__init__(self, config_options)

        self.host = Logger.get_config_option(
            config_options,
            'host',
            required=True,
            allow_empty=False
        )
        self.port = Logger.get_config_option(
            config_options,
            'port',
            required_type='int',
            required=True
        )
        self.hostname = socket.gethostname()
        self.key = bytearray(
            Logger.get_config_option(
                config_options,
                'key',
                required=True,
                allow_empty=False),
            'utf-8'
        )

    def save_result2(self, name, monitor):
        if not self.doing_batch:
            self.logger_logger.error("NetworkLogger.save_result2() called while not doing batch.")
            return
        self.logger_logger.debug("network logger: %s %s", name, monitor)
        try:
            self.batch_data[monitor.name] = pickle.dumps(monitor)
        except Exception:
            self.logger_logger.exception('Failed to pickle monitor %s', name)

    def process_batch(self):
        try:
            p = pickle.dumps(self.batch_data)
            mac = hmac.new(self.key, p)
            send_bytes = struct.pack('B', mac.digest_size) + mac.digest() + p
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((self.host, self.port))
            s.send(send_bytes)
        except Exception as e:
            self.logger_logger.error("Failed to send network data")
        finally:
            s.close()


class Listener(Thread):<commit_msg>Support __str__ for network loggers<commit_after># coding=utf-8
import pickle
import socket
import sys
import hmac
import traceback
import struct
import logging

import util

# From the docs:
#  Threads interact strangely with interrupts: the KeyboardInterrupt exception
#  will be received by an arbitrary thread. (When the signal module is
#  available, interrupts always go to the main thread.)

from threading import Thread

from .logger import Logger


class NetworkLogger(Logger):
    """Send our results over the network to another instance."""

    supports_batch = True

    def __init__(self, config_options):
        Logger.__init__(self, config_options)

        self.host = Logger.get_config_option(
            config_options,
            'host',
            required=True,
            allow_empty=False
        )
        self.port = Logger.get_config_option(
            config_options,
            'port',
            required_type='int',
            required=True
        )
        self.hostname = socket.gethostname()
        self.key = bytearray(
            Logger.get_config_option(
                config_options,
                'key',
                required=True,
                allow_empty=False),
            'utf-8'
        )

    def describe(self):
        return "Sending monitor results to {0}:{1}".format(self.host, self.port)

    def save_result2(self, name, monitor):
        if not self.doing_batch:
            self.logger_logger.error("NetworkLogger.save_result2() called while not doing batch.")
            return
        self.logger_logger.debug("network logger: %s %s", name, monitor)
        try:
            self.batch_data[monitor.name] = pickle.dumps(monitor)
        except Exception:
            self.logger_logger.exception('Failed to pickle monitor %s', name)

    def process_batch(self):
        try:
            p = pickle.dumps(self.batch_data)
            mac = hmac.new(self.key, p)
            send_bytes = struct.pack('B', mac.digest_size) + mac.digest() + p
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((self.host, self.port))
            s.send(send_bytes)
        except Exception as e:
            self.logger_logger.error("Failed to send network data")
        finally:
            s.close()


class Listener(Thread):
{'fraction_non_alphanumeric': 0.08841732979664015, 'fraction_numerical': 0.0022104332449160036, 'mean_word_length': 2.138002773925104, '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': '2711508', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1226, 'n_words': 332}