<commit_before>import logging                                                  # type: ignore
import json                                                     # type: ignore
from kombu import BrokerConnection, Exchange, Queue, Producer   # type: ignore
from config import QUEUE_DICT                                   # type: ignore
from typing import Dict                                         # type: ignore
from datetime import datetime

logger = logging.getLogger(__name__)


def create_legacy_queue_connection():
    OUTGOING_QUEUE = QUEUE_DICT['OUTGOING_QUEUE']
    OUTGOING_QUEUE_HOSTNAME = QUEUE_DICT['OUTGOING_QUEUE_HOSTNAME']

    outgoing_exchange = Exchange("legacy_transmission", type='direct')

    queue = Queue(OUTGOING_QUEUE, outgoing_exchange, routing_key="legacy_transmission")

    connection = BrokerConnection(hostname=OUTGOING_QUEUE_HOSTNAME,
                                  userid=QUEUE_DICT['OUTGOING_QUEUE_USERID'],
                                  password=<PASSWORD>['<PASSWORD>'],
                                  virtual_host="/")
    channel = connection.channel()

    producer = Producer(channel, exchange=outgoing_exchange, routing_key="legacy_transmission")

    return producer


# Publishes the user_search on the legacy_transmission_queue
def send_legacy_transmission(user_search_result: Dict):
    producer = create_legacy_queue_connection()
    user_search_transmission = create_user_search_message(user_search_result)
    if user_search_transmission:
        producer.publish(user_search_transmission, serializer="json", compression="zlib")
        return True
    else:
        return False


def create_user_search_message(user_search_result: Dict):

    # Prepare for serialisation.
    user_search_transmission = {k.upper(): str(v) for k, v in user_search_result.items()}

    # Add the relevant event id.
    if user_search_transmission:
        user_search_transmission['EVENT_ID'] = 'user_search'

    return json.dumps(user_search_transmission)
<commit_msg>Minor cosmetic changes.<commit_after>import logging                                                  # type: ignore
import json                                                     # type: ignore
from kombu import BrokerConnection, Exchange, Queue, Producer   # type: ignore
from config import QUEUE_DICT                                   # type: ignore
from typing import Dict                                         # type: ignore

logger = logging.getLogger(__name__)

# Derived from kombu /examples/complete_send_manual.py
def create_legacy_queue_connection():
    OUTGOING_QUEUE = QUEUE_DICT['OUTGOING_QUEUE']
    OUTGOING_QUEUE_HOSTNAME = QUEUE_DICT['OUTGOING_QUEUE_HOSTNAME']

    outgoing_exchange = Exchange("legacy_transmission", type='direct')

    # Queue instance is not explicitly referenced; linkage is via exchange and routing key.
    queue = Queue(OUTGOING_QUEUE, outgoing_exchange, routing_key="legacy_transmission")

    connection = BrokerConnection(hostname=OUTGOING_QUEUE_HOSTNAME,
                                  userid=QUEUE_DICT['OUTGOING_QUEUE_USERID'],
                                  password=QUEUE_DICT['OUTGOING_QUEUE_PASSWORD'],
                                  virtual_host="/")
    channel = connection.channel()

    producer = Producer(channel, exchange=outgoing_exchange, routing_key="legacy_transmission")

    return producer


# Publishes the user_search on the legacy_transmission_queue
def send_legacy_transmission(user_search_result: Dict):
    producer = create_legacy_queue_connection()
    user_search_transmission = create_user_search_message(user_search_result)
    if user_search_transmission:
        producer.publish(user_search_transmission, serializer="json", compression="zlib")
        return True
    else:
        return False


def create_user_search_message(user_search_result: Dict):

    # Prepare for serialisation.
    user_search_transmission = {k.upper(): str(v) for k, v in user_search_result.items()}

    # Add the relevant event id.
    if user_search_transmission:
        user_search_transmission['EVENT_ID'] = 'user_search'

    return json.dumps(user_search_transmission)

{'fraction_non_alphanumeric': 0.0702091849002164, 'fraction_numerical': 0.0, 'mean_word_length': 2.81651376146789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6523215', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1105, 'n_words': 233}