<commit_before>from xml.dom.minidom import Document, parseString


def create_element(doc, parent, tag, value=None, attributes=None):
    """
    Creates an XML element
    """
    ele = doc.createElement(tag)
    parent.appendChild(ele)
    if value:
        text = doc.createTextNode(str(value))
        ele.appendChild(text)
    if attributes:
        [ele.setAttribute(k, str(v)) for k, v in attributes.items()]
    return ele


def add_fraud_fields(doc=None, element=None, customer_info=None, delivery_info=None,
                     billing_info=None, account_info=None, order_info=None,
                     **kwargs):
    """
    Submit a transaction to The3rdMan for Batch Fraud processing

    Doesn't support (yet):
        - Previous orders
        - Additional delivery addreses
        - Alternative payments

    See section 2.4.7 of the Developer Guide
    """
    # Build the request XML
    if doc is None:
        doc = Document()
    if element is None:
        element = Document()
    envelope = create_element(doc, element, 'The3rdMan')
    add_customer_information(doc, envelope, customer_info)
    add_delivery_address(doc, envelope, delivery_info)
    add_billing_address(doc, envelope, billing_info)
    add_account_information(doc, envelope, account_info)
    add_order_information(doc, envelope, order_info)
    return doc


def add_xml_fields(doc, parent, fields, values):
    for field in fields:
        if field in values and values[field] is not None:
            create_element(doc, parent, field, values[field])


def intersects(fields, values):
    """
    Test if there are any fields in the values
    """
    overlap = set(fields).intersection(values.keys())
    return len(overlap) > 0


def add_customer_information(doc, envelope, customer_info):
    if not customer_info:
        return
    cust_ele = create_element(doc, envelope, 'CustomerInformation')
    cust_fields = (
        'alt_telephone', 'customer_dob', 'customer_reference',<commit_msg>Add Realtime information in request XML<commit_after>from xml.dom.minidom import Document, parseString


def create_element(doc, parent, tag, value=None, attributes=None):
    """
    Creates an XML element
    """
    ele = doc.createElement(tag)
    parent.appendChild(ele)
    if value:
        text = doc.createTextNode(str(value))
        ele.appendChild(text)
    if attributes:
        [ele.setAttribute(k, str(v)) for k, v in attributes.items()]
    return ele


def add_fraud_fields(doc=None, element=None, customer_info=None, delivery_info=None,
                     billing_info=None, account_info=None, order_info=None,
                     **kwargs):
    """
    Submit a transaction to The3rdMan for Batch Fraud processing

    Doesn't support (yet):
        - Previous orders
        - Additional delivery addreses
        - Alternative payments

    See section 2.4.7 of the Developer Guide
    """
    # Build the request XML
    if doc is None:
        doc = Document()
    if element is None:
        element = Document()

    # Request a realtime response
    envelope = create_element(
        doc, element, 'The3rdMan', attributes={'type': 'realtime'})

    # Realtime details
    if 'callback_url' in kwargs:
        callback_format = kwargs.get('callback_format', 'XML')
        callback_url = kwargs['callback_url']
        add_realtime_information(doc, envelope, callback_format, callback_url)

    add_customer_information(doc, envelope, customer_info)
    add_delivery_address(doc, envelope, delivery_info)
    add_billing_address(doc, envelope, billing_info)
    add_account_information(doc, envelope, account_info)
    add_order_information(doc, envelope, order_info)
    return doc


def add_realtime_information(doc, ele, format, url):
    rt_ele = create_element(doc, envelope, 'Realtime')
    create_element(doc, rt_ele, 'real_time_callback_format', format)
    create_element(doc, rt_ele, 'real_time_callback', url)


def add_xml_fields(doc, parent, fields, values):
    for field in fields:
        if field in values and values[field] is not None:
            create_element(doc, parent, field, values[field])


def intersects(fields, values):
    """
    Test if there are any fields in the values
    """
    overlap = set(fields).intersection(values.keys())
    return len(overlap) > 0


def add_customer_information(doc, envelope, customer_info):
    if not customer_info:
        return
    cust_ele = create_element(doc, envelope, 'CustomerInformation')
    cust_fields = (
        'alt_telephone', 'customer_dob', 'customer_reference',
{'fraction_non_alphanumeric': 0.09363377816670312, 'fraction_numerical': 0.0026252461168234523, 'mean_word_length': 3.2889305816135086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '337270', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1318, 'n_words': 365}