<commit_before>import xml.dom.minidom

sample_xml_file = 'sample_rspec.xml'
f = open(sample_xml_file, 'r')
lines = f.readlines()
xml = ""
for line in lines:
    xml += line.replace('\n', '',).repalce('\t', '').strip()
    
dom = xml.dom.minidom.parseString(xml)

def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

def handleRspec(rspec):
    # create rspec dict
    rdict = {}
    tempdict = {}    
    # loop through each network element 
    for i in rspec.getElementsByTagName("NetSpec"):
        # handle networks call
        tempdic[i] = (handleNetworks(rspec.getElementsByTagName("NetSpec")[i]))
    # append the temp dict
    rdict['networks'] = tempdic
    return rdict

def handleIfs(interf):
    # create if dict
    ifdict = {}
    # loop through attribs and put key value pair into array
    for i in interf.attributes:
        a = node.attributes[i]
        ifdict.append({a.name:a.value})
        
    return ifdict

def handleNodes(node):
    # create node dict
    nodict = {}
    # loop through attribs and put key value pair into array
    for i in node.attributes:
        a = node.attributes[i]
        nodict[a.name] = a.value
        
    # loop through each IF element
    for i in node.getElementsByTagName("IfSpec"):
        # handle ifs
        tempd[i] = handleIfs(node.getElementByTagName("IfSpec")[i])
    # append temp dict
    nodict['ifs'] = tempd
    return nodict

def handleNetworks(network):
    # create network dict
    ndict = {'name':network.nodeName}
    # loop through each node element
    for i in network.getElementsByTagName:
        # handle nodes
        tempdict[i] = handleNodes(network.getElementsByTagName("NodeSpec")[i])
    # append temp dict
    ndict['nodes'] = tempdict
    return ndict

def handleTest(slices):
    for slide in slices:
        sdict = slice.getElementsByTagName("slices")[0]
        print "<p>%s</p>" % getText(slice.childNodes)

handleRspec(dom)

<commit_msg>cleaner xml file handling<commit_after>import xml.dom.minidom

sample_xml_file = 'sample_rspec.xml'
f = open(sample_xml_file, 'r')
lines = f.readlines()
xml = ""
for line in lines:
    xml += line.replace('\n', '',).repalce('\t', '').strip()
    
dom = xml.dom.minidom.parseString(xml)

def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc

def handleRspec(rspec):
    # create rspec dict
    rdict = {}
    tempdict = {}    
    # loop through each network element 
    for i in rspec.getElementsByTagName("NetSpec"):
        # handle networks call
        tempdic[i] = (handleNetworks(rspec.getElementsByTagName("NetSpec")[i]))
    # append the temp dict
    rdict['networks'] = tempdic
    return rdict

def handleIfs(interf):
    # create if dict
    ifdict = {}
    # loop through attribs and put key value pair into array
    for i in interf.attributes:
        a = node.attributes[i]
        ifdict[a.name] = a.value
        
    return ifdict

def handleNodes(node):
    # create node dict
    nodict = {}
    # loop through attribs and put key value pair into array
    for i in node.attributes:
        a = node.attributes[i]
        nodict[a.name] = a.value
        
    # loop through each IF element
    for i in node.getElementsByTagName("IfSpec"):
        # handle ifs
        tempd[i] = handleIfs(node.getElementByTagName("IfSpec")[i])
    # append temp dict
    nodict['ifs'] = tempd
    return nodict

def handleNetworks(network):
    # create network dict
    ndict = {'name':network.nodeName}
    # loop through each node element
    for i in network.getElementsByTagName:
        # handle nodes
        tempdict[i] = handleNodes(network.getElementsByTagName("NodeSpec")[i])
    # append temp dict
    ndict['nodes'] = tempdict
    return ndict

def handleTest(slices):
    for slide in slices:
        sdict = slice.getElementsByTagName("slices")[0]
        print "<p>%s</p>" % getText(slice.childNodes)

handleRspec(dom)


{'fraction_non_alphanumeric': 0.11301789654327041, 'fraction_numerical': 0.0004903162539838195, 'mean_word_length': 2.746556473829201, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 17}, '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': '2706248', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1325, 'n_words': 348}