Changeset 3126

Show
Ignore:
Timestamp:
05/11/07 16:59:38 (2 years ago)
Author:
ed
Message:

Updated the bizIterator() method to return an iterator object. This code is based on the code submitted by Dj Gilcrease.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/biz/dBizobj.py

    r3125 r3126  
    544544 
    545545    def bizIterator(self): 
    546         """ EXPERIMENTAL!! """ 
    547         while True: 
    548             try: 
    549                 firstTime 
    550                 try: 
    551                     self.next() 
    552                 except dException.EndOfFileException: 
    553                     raise StopIteration 
    554             except NameError: 
    555                 self.first() 
    556                 firstTime = False 
    557             yield self.RowNumber 
     546        """Returns an iterator that moves the bizobj's record pointer from 
     547        the first record to the last. You may call the iterator's reverse() method 
     548        before beginning iteration in order to iterate from the last record 
     549        back to the first. 
     550        """ 
     551        return _bizIterator(self) 
    558552         
    559553         
    560     def scan(self, func, *args, **kwargs): 
     554    def scan(self, func, reverse=False, *args, **kwargs): 
    561555        """Iterate over all records and apply the passed function to each. 
    562556 
     
    575569        Set self.exitScan to True to exit the scan on the next iteration. 
    576570        """ 
    577  
    578571        # Flag that the function can set to prematurely exit the scan 
    579572        self.exitScan = False 
     
    19531946            The specified function will be called when getFieldVal() is called on  
    19541947            the specified virtual field name.""")) 
     1948 
     1949 
     1950 
     1951class _bizIterator(object): 
     1952    def __init__(self, obj): 
     1953        self.obj = obj 
     1954        self.__firstpass = True 
     1955        self.__nextfunc = self._next 
     1956 
     1957    def reverse(self): 
     1958        if not self.__firstpass: 
     1959            raise RuntimeError, _("Cannot reverse in the middle of iteration.") 
     1960        self.__nextfunc = self._prior 
     1961        return self 
     1962         
     1963 
     1964    def _prior(self): 
     1965        if self.__firstpass: 
     1966            try: 
     1967                self.obj.last() 
     1968                self.__firstpass = False 
     1969            except dException.NoRecordsException: 
     1970                raise StopIteration 
     1971        else: 
     1972            try: 
     1973                self.obj.prior() 
     1974            except dException.BeginningOfFileException: 
     1975                raise StopIteration 
     1976        return self.obj.RowNumber 
     1977                 
     1978 
     1979    def _next(self): 
     1980        if self.__firstpass: 
     1981            try: 
     1982                self.obj.first() 
     1983                self.__firstpass = False 
     1984            except dException.NoRecordsException: 
     1985                raise StopIteration 
     1986        else: 
     1987            try: 
     1988                self.obj.next() 
     1989            except dException.EndOfFileException: 
     1990                raise StopIteration 
     1991        return self.obj.RowNumber 
     1992     
     1993     
     1994    def next(self): 
     1995        return self.__nextfunc() 
     1996         
     1997 
     1998    def __iter__(self): 
     1999        self.__firstpass = True 
     2000        return self 
     2001