Factored out IO
authorDmitriy Morozov <dmitriy@mrzv.org>
Sun, 06 Mar 2011 13:21:48 -0800
changeset 7 cbb51ef4dd6d
parent 6 6ce97bd3e70f
child 8 2eaa2bffa6f4
Factored out IO
edges.py
io.py
points.py
triangles.py
--- a/edges.py	Sat Mar 05 23:06:27 2011 -0800
+++ b/edges.py	Sun Mar 06 13:21:48 2011 -0800
@@ -6,20 +6,13 @@
 from        ViewerItem  import ViewerItem
 from        os.path     import basename
 from        itertools   import izip
+from        io          import line_blocks
 
 class Edges(ViewerItem):
     def __init__(self, filename, parent = None):
         super(Edges, self).__init__(basename(filename), parent, color = (0,0,255))
 
-        self.edges = []
-        points = []
-        with open(filename) as f:
-            for line in f:
-                if not line.strip() or line.startswith('#'): continue
-                points.append(Point(map(float, line.split())))
-
-            for (u,v) in izip(points[::2], points[1::2]):
-                self.edges.append((u,v))
+        self.read_edges(filename)
 
         self.create_display_list()
         self.center, self.min, self.max = centerMinMax(self.vertices())
@@ -51,3 +44,11 @@
 
     def __iter__(self):
         return self.edges
+
+    def read_edges(self, filename):
+        self.edges = []
+        for block in line_blocks(filename, 2):
+            u = Point(map(float, block[0].split()))
+            v = Point(map(float, block[1].split()))
+            self.edges.append((u,v))
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/io.py	Sun Mar 06 13:21:48 2011 -0800
@@ -0,0 +1,10 @@
+def line_blocks(filename, size = 1):
+    block = []
+    with open(filename) as f:
+        for line in f:
+            if not line.strip() or line.startswith('#'): continue
+            block.append(line)
+            if len(block) == size:
+                yield block
+                block = []
+        
--- a/points.py	Sat Mar 05 23:06:27 2011 -0800
+++ b/points.py	Sun Mar 06 13:21:48 2011 -0800
@@ -4,6 +4,7 @@
 from    PyQt4       import QtGui, QtCore
 from    ViewerItem  import ViewerItem
 from    os.path     import basename
+from    io          import line_blocks
 
 class Point(object):
     __slots__ = ('x','y','z')
@@ -40,10 +41,8 @@
         super(Points, self).__init__(basename(filename), parent, (255,0,255))
 
         self.points = []
-        with open(filename) as f:
-            for line in f:
-                if line.startswith('#'): continue
-                self.points.append(Point(map(float, line.split())))
+        for block in line_blocks(filename):
+            self.points.append(Point(map(float, block[0].split())))
         self.create_display_list()
         self.center, self.min, self.max = centerMinMax(self.points)
 
--- a/triangles.py	Sat Mar 05 23:06:27 2011 -0800
+++ b/triangles.py	Sun Mar 06 13:21:48 2011 -0800
@@ -6,23 +6,13 @@
 from        ViewerItem  import ViewerItem
 from        os.path     import basename
 from        itertools   import izip
+from        io          import line_blocks
 
 class Triangles(ViewerItem):
     def __init__(self, filename, parent = None):
         super(Triangles, self).__init__(basename(filename), parent, color = (200,200,200))
 
-        self.triangles = []
-        lines = []
-        with open(filename) as f:
-            for line in f:
-                if not line.strip() or line.startswith('#'): continue
-                lines.append(line)
-
-            for i in xrange(0,len(lines),7):
-                u = Point(map(float, lines[i+2].split()[1:4]))
-                v = Point(map(float, lines[i+3].split()[1:4]))
-                w = Point(map(float, lines[i+4].split()[1:4]))
-                self.triangles.append((u,v,w))
+        self.read_triangles(filename)
 
         self.create_display_lists()
         self.center, self.min, self.max = centerMinMax(self.vertices())
@@ -91,3 +81,11 @@
 
     def __iter__(self):
         return self.triangles
+
+    def read_triangles(self, filename):
+        self.triangles = []
+        for block in line_blocks(filename, 7):
+            u = Point(map(float, block[2].split()[1:4]))
+            v = Point(map(float, block[3].split()[1:4]))
+            w = Point(map(float, block[4].split()[1:4]))
+            self.triangles.append((u,v,w))