1. Matt Bone
  2. elementtree-data-binding

Commits

Matt Bone  committed 7274f81

adding some xpath things

  • Participants
  • Parent commits e4b44fc
  • Branches default

Comments (0)

Files changed (4)

File lecture/expression_parser.py

View file
  • Ignore whitespace
     element_tree = ElementTree.fromstring(blob)
     def _parse_xml(node):
         if node.tag == "expression":
-            return _parse_xml(node.getchildren()[0])
+            return _parse_xml(list(node)[0])
         elif node.tag == "add":
-            return AdditionNode([_parse_xml(child) for child in node.getchildren()])
+            return AdditionNode([_parse_xml(child) for child in node])
         elif node.tag == "subtract":
-            return SubtractionNode([_parse_xml(child) for child in node.getchildren()])
+            return SubtractionNode([_parse_xml(child) for child in node])
         elif node.tag == "multiply":
-            return MultiplicationNode([_parse_xml(child) for child in node.getchildren()])
+            return MultiplicationNode([_parse_xml(child) for child in node])
         elif node.tag == "integer":
             return IntegerNode(int(node.attrib["value"]))
     return _parse_xml(element_tree)
 class ExpressionParserTest(unittest.TestCase):
 
     def test_int_node(self):
-        self.assertEquals(40, IntegerNode(40).value)
+        self.assertEqual(40, IntegerNode(40).value)
     
 
     def test_mult_node(self):
-        self.assertEquals(40, MultiplicationNode([IntegerNode(2), IntegerNode(20)]).value)
+        self.assertEqual(40, MultiplicationNode([IntegerNode(2), IntegerNode(20)]).value)
 
 
     def test_sub_node(self):
-        self.assertEquals(8, SubtractionNode([IntegerNode(10), IntegerNode(2)]).value)
+        self.assertEqual(8, SubtractionNode([IntegerNode(10), IntegerNode(2)]).value)
 
 
     def test_add_node(self):
-        self.assertEquals(12, AdditionNode([IntegerNode(10), IntegerNode(2)]).value)
+        self.assertEqual(12, AdditionNode([IntegerNode(10), IntegerNode(2)]).value)
 
 
     def test_expression_tree(self):
         tree = SubtractionNode([AdditionNode([IntegerNode(3), MultiplicationNode([IntegerNode(2), IntegerNode(20)])]), IntegerNode(1)])
-        self.assertEquals(42, tree.value)
+        self.assertEqual(42, tree.value)
 
 
     def test_parse_json(self):
-        json_blob = open("expression.json").read()
-        tree = parse_json(json_blob)
-        self.assertEquals(42, tree.value)
+        with open("expression.json") as json_file:
+            json_blob = json_file.read()
+            tree = parse_json(json_blob)
+            self.assertEqual(42, tree.value)
 
     def test_parse_xml(self):
-        xml_blob = open("expression.xml").read()
-        tree = parse_xml(xml_blob)
-        self.assertEquals(42, tree.value)
+        with open("expression.xml") as xml_file:
+            xml_blob = xml_file.read()
+            tree = parse_xml(xml_blob)
+            self.assertEqual(42, tree.value)
 
 if __name__ == "__main__":
     unittest.main()

File lecture/xml_tree.py

View file
  • Ignore whitespace
+from xml.etree import ElementTree
+from collections import defaultdict
+
+
+class DotNode(object):
+    def __init__(self, element_id, element):
+        self.element_id = element_id
+        self.element = element
+
+    def __str__(self):
+        return """{id} [label="{label}"];""".format(id=self.element_id, label=self.element.tag)
+        #return self.element_id
+
+
+def get_element_id(element, element_dict):
+    fixed_name = "{}_{}".format(element.tag, element_dict[element.tag])
+    element_dict[element.tag] += 1
+    return fixed_name
+
+
+def parent_child_pairs(element, element_id, element_dict):                                          
+    children = list()
+
+    for child in element:
+        child_element_id = get_element_id(child, element_dict)
+        yield (DotNode(element_id, element), DotNode(child_element_id, child))
+        children.append((child_element_id, child))
+
+    for child_element_id, element in children:
+        for pair in parent_child_pairs(element, child_element_id, element_dict):
+            yield pair
+
+
+if __name__=="__main__":
+
+    element_counter = defaultdict(int)
+
+    with open("expression.xml") as xml_file:
+        tree = ElementTree.fromstring(xml_file.read())
+
+        declarations = set()
+        edges = []
+
+
+        element_dict = defaultdict(int)
+        for parent_node, child_node in parent_child_pairs(tree, get_element_id(tree, element_dict), element_dict):
+            declarations.add(str(parent_node))
+            declarations.add(str(child_node))            
+            edges.append("{parent} -> {child};".format(parent=parent_node.element_id, child=child_node.element_id))
+
+        print("digraph G {")
+        print("\n".join(declarations))
+        print("\n".join(edges))
+        print("}") 

File lecture/xpath_example.xml

View file
  • Ignore whitespace
+<?xml version="1.0" encoding="UTF-8"?>
+<cs366>
+    <professors>
+        <professor>
+            <name first="hubert" last="farnsworth"/>
+            <height>
+                <measurement value=".33" units="rods"/>
+            </height>
+            <age>
+                <measurement value="171" units="years"/>
+            </age>
+            <bio>
+                
+            </bio>
+        </professor>
+    </professors>
+    <students>
+        <student>
+            <name first="turanga" last="leela"/> 
+        </student>
+        <student >
+            <name first="john" last="zoidberg"/>
+        </student>
+    </students>    
+</cs366>

File lecture/xpaths.txt

View file
  • Ignore whitespace
+#coming soon