Creating class iterator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlm699
    Contributor
    • Jul 2007
    • 314

    Creating class iterator

    Greetings friends. I'm trying to create an iterator to iterate over a database of messages. I defined an __iter__() and a next() function as such:
    [code=python]import win32com.client as w32c
    from win32com.client import constants

    class DataBaseControl :
    def __init__( self, passwd, database='path_ to_database' ):
    # ... Initialization, etc...
    self.view = self.db.GetView ( "$Inbox" )

    def __iter__( self ):
    self.currDoc = self.view.GetFi rstDocument()
    return self.currDoc
    def next( self ):
    if not self.currDoc:
    raise StopIteration
    else:
    self.currDoc = self.view.GetNe xtDocument( self.currDoc )
    return self.currDoc[/code]

    so now when I do[code=python]>>> from DBClass import DataBaseControl
    >>> lb = DataBaseControl (my_passwd)
    >>> lb
    <DBClass.DataBa seControl instance at 0x01DC7FA8>
    >>> for mail in lb:
    ... print mail.getItemVal ue("Subject")
    ...
    Traceback (most recent call last):
    File "<input>", line 1, in ?
    TypeError: instance has no next() method
    >>> for mail in lb:
    ... print mail
    ...
    Traceback (most recent call last):
    File "<input>", line 1, in ?
    TypeError: instance has no next() method
    >>> lb.next()
    <COMObject GetNextDocument >
    >>> lb.next().getIt emValue("Subjec t")
    (u'Re: Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
    >>> lb.next().getIt emValue("Subjec t")
    (u'Re: Fw: 13wks shortage highlights -- memory shortage',)
    >>> lb.next().getIt emValue("Subjec t")
    (u'Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
    >>> [/code]
    so as you can see... I can access the next() method it's just not letting the iterator see it? I've never created an iterator before so I'm kinda lost as to why... any suggestions?

    EDIT: * I have a generator that will work but I just was hoping to have something clean to do something like -- for mail_item in lb: *
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by jlm699
    Greetings friends. I'm trying to create an iterator to iterate over a database of messages. I defined an __iter__() and a next() function as such:
    [code=python]import win32com.client as w32c
    from win32com.client import constants

    class DataBaseControl :
    def __init__( self, passwd, database='path_ to_database' ):
    # ... Initialization, etc...
    self.view = self.db.GetView ( "$Inbox" )

    def __iter__( self ):
    self.currDoc = self.view.GetFi rstDocument()
    return self.currDoc
    def next( self ):
    if not self.currDoc:
    raise StopIteration
    else:
    self.currDoc = self.view.GetNe xtDocument( self.currDoc )
    return self.currDoc[/code]

    so now when I do[code=python]>>> from DBClass import DataBaseControl
    >>> lb = DataBaseControl (my_passwd)
    >>> lb
    <DBClass.DataBa seControl instance at 0x01DC7FA8>
    >>> for mail in lb:
    ... print mail.getItemVal ue("Subject")
    ...
    Traceback (most recent call last):
    File "<input>", line 1, in ?
    TypeError: instance has no next() method
    >>> for mail in lb:
    ... print mail
    ...
    Traceback (most recent call last):
    File "<input>", line 1, in ?
    TypeError: instance has no next() method
    >>> lb.next()
    <COMObject GetNextDocument >
    >>> lb.next().getIt emValue("Subjec t")
    (u'Re: Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
    >>> lb.next().getIt emValue("Subjec t")
    (u'Re: Fw: 13wks shortage highlights -- memory shortage',)
    >>> lb.next().getIt emValue("Subjec t")
    (u'Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
    >>> [/code]
    so as you can see... I can access the next() method it's just not letting the iterator see it? I've never created an iterator before so I'm kinda lost as to why... any suggestions?

    EDIT: * I have a generator that will work but I just was hoping to have something clean to do something like -- for mail_item in lb: *
    Create a list of documents in __init__().[code=Python]
    self.docs = [self.view.GetFi rstDocument(),]
    while True:
    doc = GetNextDocument (self.docs[-1])
    if doc:
    self.docs.appen d()
    else:
    break

    def __iter__(self):
    for doc in self.docs:
    yield doc[/code]I am not sure if it will work with your application.

    Comment

    Working...