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: - 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.GetFirstDocument()
-
return self.currDoc
-
def next( self ):
-
if not self.currDoc:
-
raise StopIteration
-
else:
-
self.currDoc = self.view.GetNextDocument( self.currDoc )
-
return self.currDoc
so now when I do - >>> from DBClass import DataBaseControl
-
>>> lb = DataBaseControl(my_passwd)
-
>>> lb
-
<DBClass.DataBaseControl instance at 0x01DC7FA8>
-
>>> for mail in lb:
-
... print mail.getItemValue("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().getItemValue("Subject")
-
(u'Re: Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
-
>>> lb.next().getItemValue("Subject")
-
(u'Re: Fw: 13wks shortage highlights -- memory shortage',)
-
>>> lb.next().getItemValue("Subject")
-
(u'Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
-
>>>
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: *
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: - 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.GetFirstDocument()
-
return self.currDoc
-
def next( self ):
-
if not self.currDoc:
-
raise StopIteration
-
else:
-
self.currDoc = self.view.GetNextDocument( self.currDoc )
-
return self.currDoc
so now when I do - >>> from DBClass import DataBaseControl
-
>>> lb = DataBaseControl(my_passwd)
-
>>> lb
-
<DBClass.DataBaseControl instance at 0x01DC7FA8>
-
>>> for mail in lb:
-
... print mail.getItemValue("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().getItemValue("Subject")
-
(u'Re: Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
-
>>> lb.next().getItemValue("Subject")
-
(u'Re: Fw: 13wks shortage highlights -- memory shortage',)
-
>>> lb.next().getItemValue("Subject")
-
(u'Fw: *IBM Confidential: Quality Issue (LS41) in Morgan Stanley ',)
-
>>>
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__(). -
self.docs = [self.view.GetFirstDocument(),]
-
while True:
-
doc = GetNextDocument(self.docs[-1])
-
if doc:
-
self.docs.append()
-
else:
-
break
-
-
def __iter__(self):
-
for doc in self.docs:
-
yield doc
I am not sure if it will work with your application.
Sign in to post your reply or Sign up for a free account.
Similar topics
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<...
|
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:...
|
by: cyberdave |
last post by:
Someone please help me!
I have a template class like this:
--------------------------------------------------
template<typename T>
class List
{
public:
|
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
|
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;...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |