Ticket 733: Modify to work with python3
authorAnders Henja <anders@henjab.se>
Sun, 11 Mar 2018 16:16:04 +0000 (17:16 +0100)
committerAnders Henja <anders@henjab.se>
Sun, 11 Mar 2018 16:16:04 +0000 (17:16 +0100)
common/src/baltrad/bdbcommon/expr.py
common/src/baltrad/bdbcommon/oh5/io.py
common/src/baltrad/bdbcommon/oh5/meta.py
common/src/baltrad/bdbcommon/oh5/node.py
common/test/oh5/meta_test.py

index f738e56..fc1cd14 100644 (file)
@@ -199,7 +199,7 @@ class Evaluator(object):
                 proc = xpr.pop(0)
                 try:
                     return proc(*xpr)
-                except TypeError, e:
+                except TypeError as e:
                     raise EvaluationError(str(e))
             else:
                 return xpr
index 495ba5f..3f187ec 100644 (file)
@@ -21,7 +21,7 @@ import warnings
 
 try:
     import _pyhl as pyhl
-except ImportError, e:
+except ImportError as e:
     warnings.warn("couldn't import pyhl, hdf5 IO disabled: %s" % e)
     pyhl = None
 
index 4de1b3c..b6b9f92 100644 (file)
@@ -159,7 +159,7 @@ class Source(collections.MutableMapping):
         return self.to_string()
 
     def to_string(self):
-        return ",".join([":".join(kv) for kv in sorted(self._values.iteritems())])
+        return ",".join([":".join(kv) for kv in sorted(self._values.items())])
     
     @classmethod
     def from_string(cls, value):
index af0e4eb..6bb36b6 100644 (file)
@@ -66,7 +66,19 @@ class Node(object):
     def children(self):
         """list of this node's children
         """
-        return self._children.values()
+        children = list(self._children.values())
+        children.sort()
+        return children
+
+    def __lt__(self, other):
+        if other:
+            return self.name < other.name
+        return False  
+    
+    def __eq__(self, other):
+        if other:
+            return self.name == other.name
+        return False
     
     def _get_parent(self):
         if self._parent:
@@ -144,7 +156,7 @@ class NodeIterator(object):
     def __iter__(self):
         return self
     
-    def next(self):
+    def __next__(self):
         try:
             node = self._nodes.pop(0)
         except IndexError:
@@ -153,3 +165,7 @@ class NodeIterator(object):
         if node.children:
             self._nodes.extend(node.children())
         return node
+        
+    def next(self):
+        return self.__next__()
+
index 898982d..d20db09 100644 (file)
@@ -48,14 +48,38 @@ class TestMetadata(object):
         eq_(None, self.meta.find_node("/a/b"))
 
     def test_iterator(self):
-       self.meta.add_node("/", Group("a"))
-       self.meta.add_node("/", Group("b"))
-       self.meta.add_node("/a", Group("b"))
-       self.meta.add_node("/a/b", Group("c"))
-
-       expected = ["/", "/a", "/b", "/a/b", "/a/b/c"]
-       nodepaths = [node.path() for node in self.meta.iternodes()]
-       eq_(expected, nodepaths)
+        self.meta.add_node("/", Group("a"))
+        self.meta.add_node("/", Group("b"))
+        self.meta.add_node("/a", Group("b"))
+        self.meta.add_node("/a/b", Group("c"))
+
+        expected = ["/", "/a", "/b", "/a/b", "/a/b/c"]
+        nodepaths = [node.path() for node in self.meta.iternodes()]
+        eq_(expected, nodepaths)
+
+    def test_iterator_2(self):
+        self.meta.add_node("/", Group("a"))
+        self.meta.add_node("/", Group("b"))
+        self.meta.add_node("/", Group("ab"))
+        self.meta.add_node("/", Group("ba"))
+        self.meta.add_node("/a", Group("b"))
+        self.meta.add_node("/a/b", Group("c"))
+
+        expected = ["/", "/a", "/ab", "/b", "/ba", "/a/b", "/a/b/c"]
+        nodepaths = [node.path() for node in self.meta.iternodes()]
+        eq_(expected, nodepaths)
+
+    def test_iterator_3(self):
+        self.meta.add_node("/", Group("a"))
+        self.meta.add_node("/", Group("b"))
+        self.meta.add_node("/", Attribute("a1", 1))
+        self.meta.add_node("/", Group("ba"))
+        self.meta.add_node("/a", Attribute("a2", 1))
+        self.meta.add_node("/a", Group("c"))
+
+        expected = ["/", "/a", "/a1", "/b", "/ba", "/a/a2", "/a/c"]
+        nodepaths = [node.path() for node in self.meta.iternodes()]
+        eq_(expected, nodepaths)
             
     def test_source(self):
         self.meta.add_node("/", Group("what"))
@@ -81,8 +105,8 @@ class TestMetadata(object):
             {"path": "/", "type": "group"},
             {"path": "/a", "type": "group"},
             {"path": "/attr1", "type": "attribute", "value": 1},
-            {"path": "/a/b", "type": "group"},
-            {"path": "/a/attr2", "type": "attribute", "value": 2}
+            {"path": "/a/attr2", "type": "attribute", "value": 2},
+            {"path": "/a/b", "type": "group"}
         ]
 
         eq_(expected, self.meta.json_repr())