473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Creating class iterator

jlm699
314 100+
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:
Expand|Select|Wrap|Line Numbers
  1. import win32com.client as w32c
  2. from win32com.client import constants
  3.  
  4. class DataBaseControl:
  5.     def __init__( self, passwd, database='path_to_database' ):
  6.         # ... Initialization, etc...
  7.         self.view = self.db.GetView( "$Inbox" )
  8.  
  9.     def __iter__( self ):
  10.         self.currDoc = self.view.GetFirstDocument()
  11.         return self.currDoc
  12.     def next( self ):
  13.         if not self.currDoc:
  14.             raise StopIteration
  15.         else:
  16.             self.currDoc = self.view.GetNextDocument( self.currDoc )
  17.             return self.currDoc
so now when I do
Expand|Select|Wrap|Line Numbers
  1. >>> from DBClass import DataBaseControl
  2. >>> lb = DataBaseControl(my_passwd)
  3. >>> lb
  4. <DBClass.DataBaseControl instance at 0x01DC7FA8>
  5. >>> for mail in lb:
  6. ...     print mail.getItemValue("Subject")
  7. ...     
  8. Traceback (most recent call last):
  9.   File "<input>", line 1, in ?
  10. TypeError: instance has no next() method
  11. >>> for mail in lb:
  12. ...     print mail
  13. ...     
  14. Traceback (most recent call last):
  15.   File "<input>", line 1, in ?
  16. TypeError: instance has no next() method
  17. >>> lb.next()
  18. <COMObject GetNextDocument>
  19. >>> lb.next().getItemValue("Subject")
  20. (u'Re: Fw: *IBM Confidential:  Quality Issue (LS41) in Morgan Stanley ',)
  21. >>> lb.next().getItemValue("Subject")
  22. (u'Re: Fw: 13wks shortage highlights  -- memory shortage',)
  23. >>> lb.next().getItemValue("Subject")
  24. (u'Fw: *IBM Confidential:  Quality Issue (LS41) in Morgan Stanley ',)
  25. >>> 
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: *
May 29 '08 #1
1 4013
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. import win32com.client as w32c
  2. from win32com.client import constants
  3.  
  4. class DataBaseControl:
  5.     def __init__( self, passwd, database='path_to_database' ):
  6.         # ... Initialization, etc...
  7.         self.view = self.db.GetView( "$Inbox" )
  8.  
  9.     def __iter__( self ):
  10.         self.currDoc = self.view.GetFirstDocument()
  11.         return self.currDoc
  12.     def next( self ):
  13.         if not self.currDoc:
  14.             raise StopIteration
  15.         else:
  16.             self.currDoc = self.view.GetNextDocument( self.currDoc )
  17.             return self.currDoc
so now when I do
Expand|Select|Wrap|Line Numbers
  1. >>> from DBClass import DataBaseControl
  2. >>> lb = DataBaseControl(my_passwd)
  3. >>> lb
  4. <DBClass.DataBaseControl instance at 0x01DC7FA8>
  5. >>> for mail in lb:
  6. ...     print mail.getItemValue("Subject")
  7. ...     
  8. Traceback (most recent call last):
  9.   File "<input>", line 1, in ?
  10. TypeError: instance has no next() method
  11. >>> for mail in lb:
  12. ...     print mail
  13. ...     
  14. Traceback (most recent call last):
  15.   File "<input>", line 1, in ?
  16. TypeError: instance has no next() method
  17. >>> lb.next()
  18. <COMObject GetNextDocument>
  19. >>> lb.next().getItemValue("Subject")
  20. (u'Re: Fw: *IBM Confidential:  Quality Issue (LS41) in Morgan Stanley ',)
  21. >>> lb.next().getItemValue("Subject")
  22. (u'Re: Fw: 13wks shortage highlights  -- memory shortage',)
  23. >>> lb.next().getItemValue("Subject")
  24. (u'Fw: *IBM Confidential:  Quality Issue (LS41) in Morgan Stanley ',)
  25. >>> 
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__().
Expand|Select|Wrap|Line Numbers
  1. self.docs = [self.view.GetFirstDocument(),]
  2. while True:
  3.     doc = GetNextDocument(self.docs[-1])
  4.     if doc:
  5.         self.docs.append()
  6.     else:
  7.         break
  8.  
  9. def __iter__(self):
  10.     for doc in self.docs:
  11.         yield doc
I am not sure if it will work with your application.
May 29 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
3
by: John Smith | last post by:
Hey I have some code which I've been using on Microsoft VC++ for some time. Now I wanted to port my application to Mac OS X which offers gcc and the build fails. Here is the troublesome code:...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
9
by: arnuld | last post by:
problem: define a /struct Date/ to keep track of dates. provide functions that read Dates from input, write Dates to output & initialize a date with date. solution: i thought of a /vector/ of...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
6
thatos
by: thatos | last post by:
I designed a class called Row. This class has the following variable private String country,city; private String connections; private boolean sea_link; private int cumul,section; I...
3
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.