diff python/moving.py @ 23:5f2921ad4f7e

made Trajectory indexable and timeinterval iterable
author Nicolas Saunier <nico@confins.net>
date Fri, 04 Dec 2009 13:47:22 -0500
parents 169cd4679366
children 28e546861263
line wrap: on
line diff
--- a/python/moving.py	Fri Dec 04 01:01:11 2009 -0500
+++ b/python/moving.py	Fri Dec 04 13:47:22 2009 -0500
@@ -20,6 +20,17 @@
     def __str__(self):
         return '%d %d'%(self.first, self.last)
 
+    def __iter__(self):
+        self.iterInstantNum = 0
+        return self
+
+    def next(self):
+        if self.iterInstantNum >= self.length():
+            raise StopIteration
+        else:
+            self.iterInstantNum += 1
+            return self.first+self.iterInstantNum
+
     def empty(self):
         '''
         >>> TimeInterval().empty()
@@ -98,20 +109,19 @@
     def __str__(self):
         return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1]))
 
+    def __getitem__(self, i):
+        return [self.positions[0][i], self.positions[1][i]]        
+
     def __iter__(self):
         self.iterInstantNum = 0
         return self
 
     def next(self):
         if self.iterInstantNum >= self.length():
-            self.iterInstantNum = 0
             raise StopIteration
         else:
             self.iterInstantNum += 1
-            return self.getPosition(self.iterInstantNum-1)
-
-    def getPosition(self, i):
-        return [self.positions[0][i], self.positions[1][i]]
+            return self[self.iterInstantNum-1]
 
     def addPosition(self, point):
         if not self.positions:
@@ -125,7 +135,7 @@
         plot(self.positions[0], self.positions[1])
 
     def length(self):
-        return len(self.getXCoordinates())
+        return len(self.positions[0])
 
     def getXCoordinates(self):
         return self.positions[0]