F
Faber J. Fedor
Hi all,
I'm in the process of learning Python by writing a job queue program.
Nothing fancy, mind you, just read from a table, shell out to a program,
write back to the table.
I'm working off of the tutorial listed here (amongst many places):
http://www.devx.com/dbzone/Article/22093
In my Jobs class I have:
def __iter__(self):
"creates a data set, and returns an iterator (self)"
q = "select * from %s" % (self.name)
self._query(q)
return self # an Iterator is an object
# with a next() method
def next(self):
"returns the next item in the data set,
or tells Python to stop"
r = self.dbc.fetchone()
if not r:
raise StopIteration
return r
which works well, but what if I want to modify the __iter__ query? I
want to be able to do something like this (and I know this is not the
right syntax but you'll get my drift):
for job in jobs: print job # which the above code does
for job in jobs("status = running"): print job
for job in jobs("jobid = 4"): print job
What's the pythonic way of doing this?
I'm in the process of learning Python by writing a job queue program.
Nothing fancy, mind you, just read from a table, shell out to a program,
write back to the table.
I'm working off of the tutorial listed here (amongst many places):
http://www.devx.com/dbzone/Article/22093
In my Jobs class I have:
def __iter__(self):
"creates a data set, and returns an iterator (self)"
q = "select * from %s" % (self.name)
self._query(q)
return self # an Iterator is an object
# with a next() method
def next(self):
"returns the next item in the data set,
or tells Python to stop"
r = self.dbc.fetchone()
if not r:
raise StopIteration
return r
which works well, but what if I want to modify the __iter__ query? I
want to be able to do something like this (and I know this is not the
right syntax but you'll get my drift):
for job in jobs: print job # which the above code does
for job in jobs("status = running"): print job
for job in jobs("jobid = 4"): print job
What's the pythonic way of doing this?