commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnue-common/src/datasources GDataObjects.py


From: Jason Cater
Subject: gnue-common/src/datasources GDataObjects.py
Date: Mon, 04 Aug 2003 23:15:38 -0400

CVSROOT:        /cvsroot/gnue
Module name:    gnue-common
Branch:         
Changes by:     Jason Cater <address@hidden>    03/08/04 23:15:38

Modified files:
        src/datasources: GDataObjects.py 

Log message:
        added iterator and sequence support to ResultSet (i.e., you can do: 
print resultset[0]['foo']   and:   for record in resultset:  print 
record['foo'])

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gnue/gnue-common/src/datasources/GDataObjects.py.diff?tr1=1.69&tr2=1.70&r1=text&r2=text

Patches:
Index: gnue-common/src/datasources/GDataObjects.py
diff -c gnue-common/src/datasources/GDataObjects.py:1.69 
gnue-common/src/datasources/GDataObjects.py:1.70
*** gnue-common/src/datasources/GDataObjects.py:1.69    Thu Jul 17 01:24:17 2003
--- gnue-common/src/datasources/GDataObjects.py Mon Aug  4 23:15:38 2003
***************
*** 81,87 ****
    pass
  
  class NoWriteSchemaSupport(Error):
!   # Raised when a database adapter doesn't support 
    # writing Schema to datasource
    pass
  
--- 81,87 ----
    pass
  
  class NoWriteSchemaSupport(Error):
!   # Raised when a database adapter doesn't support
    # writing Schema to datasource
    pass
  
***************
*** 314,320 ****
--- 314,335 ----
       if masterRecordSet:
         masterRecordSet.addDetailResultSet(self)
  
+   # Since we are overriding __len__
+   def __nonzero__(self):
+     return 1
+ 
+   # Return the # of records
+   def __len__(self):
+     return self.getRecordCount()
+ 
+   def __getitem__(self, index):
+     rs = self.getRecord(index)
+     if not rs:
+       raise IndexError
+     else:
+       return rs
  
+       
    # Returns whether this result set is read only or not
    def isReadOnly(self):
      return self._readonly
***************
*** 563,568 ****
--- 578,587 ----
    def _createEmptyRecord(self):
      return self._recordSetClass(self)
  
+   # Iterator support (Python 2.2+)
+   def __iter__(self):
+     return _ResultSetIter(self)
+ 
  
  ###########################################################
  #
***************
*** 799,802 ****
--- 818,851 ----
  
  
  
+ # A simple resultset iterator
+ # Lets you use ResultSets as:
+ #
+ #   for record in myResultSet:
+ #      blah
+ #
+ # NOTE: Python 2.2+  (but it won't get called in
+ #    Python 2.1 or below, so not a problem)
+ #
+ class _ResultSetIter:
+   def __init__(self, resultset):
+     self.resultset = resultset
+     self.used = 0
+     self.done = 0
+ 
+   def __iter__(self):
+     return self
+ 
+   def next(self):
+     if self.done:
+       raise StopIteration
+     if self.used:
+       rs = self.resultset.firstRecord()
+     else:
+       rs = self.resultset.nextRecord()
+ 
+     if not rs:
+       raise StopIteration
+     else:
+       return rs
  




reply via email to

[Prev in Thread] Current Thread [Next in Thread]