I'm working on two coroutines -- one iterates through a huge stream, and
emits chunks in pieces. The other routine takes each chunk, then scores
it as good or bad and passes that score back to the original routine, so
it can make a copy of the stream with the score appended on.
I have the code working, but it just looks really ugly. Here's a vastly
simplified version. One function yields some numbers, and the other
function tells me if they are even or odd.
def parser():
"I just parse and wait for feedback."
for i in 1, 2, 3, 4, 5:
score = (yield i)
if score:
print "%d passed!" % i
def is_odd(n):
"I evaluate each number n, and return True if I like it."
if n and n % 2: return True
def m():
try:
number_generator = parser()
i = None
while 1:
i = number_generator.send(is_odd(i))
except StopIteration: pass
and here's the results when I run this:
In [90]: m()
1 passed!
3 passed!
5 passed!
So, clearly, the code works. But it is nonintuitive for the casual
reader.
I don't like the while 1 construct, I don't like manually
trapping the StopIteration exception, and this line is really ugly:
i = number_generator.send(is_odd(i))
I really like the old for i in parser(): deal, but I can't figure out
how to use .send(...) with that.
Can anyone help me pretty this up? I want to make this as intuitive as
possible.
TIA
Matt 1 1333
Matthew Wilson wrote:
I'm working on two coroutines -- one iterates through a huge stream, and
emits chunks in pieces. The other routine takes each chunk, then scores
it as good or bad and passes that score back to the original routine, so
it can make a copy of the stream with the score appended on.
I have the code working, but it just looks really ugly. Here's a vastly
simplified version. One function yields some numbers, and the other
function tells me if they are even or odd.
def parser():
"I just parse and wait for feedback."
for i in 1, 2, 3, 4, 5:
score = (yield i)
if score:
print "%d passed!" % i
def is_odd(n):
"I evaluate each number n, and return True if I like it."
if n and n % 2: return True
def m():
try:
number_generator = parser()
i = None
while 1:
i = number_generator.send(is_odd(i))
except StopIteration: pass
and here's the results when I run this:
In [90]: m()
1 passed!
3 passed!
5 passed!
So, clearly, the code works. But it is nonintuitive for the casual
reader.
I don't like the while 1 construct, I don't like manually
trapping the StopIteration exception, and this line is really ugly:
i = number_generator.send(is_odd(i))
I really like the old for i in parser(): deal, but I can't figure out
how to use .send(...) with that.
Can anyone help me pretty this up? I want to make this as intuitive as
possible.
Why use coroutines?
def parser(score):
for i in xrange(1, 6):
yield i
if score(i):
print "%d passed!" % i
def is_odd(n):
return n % 2
def m():
for i in parser(is_odd):
# Presumably do something here...
pass
--
Paul Hankin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Reedy |
last post by:
For everyone -
Apologies for the length of this message. If you don't want to look
at the long example, you can skip to the end of the message.
And for the Python gurus among you, if you can...
|
by: Heather Stovold |
last post by:
Wow - deciding to program in python sure requires a lot of decisions!
So far:
I've decided on python for the programming language.
I've decided on wxpython for the GUI....
I've decided on...
|
by: michaaal |
last post by:
When writing an ASP app, it has been my experience that usually I write code
that is compatible with IE6 but not previous versions of Internet Explorer.
Does anyone have any practical advice for...
|
by: James Fortune |
last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't
understand why people who know how to write code to completely replace
a front end do not write something that will automate...
|
by: Lau Lei Cheong |
last post by:
Hello,
I'm writing UserControls as stated earlier. Now I come to the problem
that I need to preserve viewstate in my controls.
If I want to save the informations on the controls, in which...
|
by: Q. John Chen |
last post by:
I have Vidation Controls
First One: Simple exluce certain special characters:
say no a or b or c in the string:
*
Second One:
I required date be entered in "MM/DD/YYYY" format:
//+4 How...
|
by: Billy |
last post by:
Hello,
I'm trying to decide if I need Visual Studio .Net or if
VB .NET will suffice. I've read the guide at
http://msdn.microsoft.com/vbasic/howtobuy/choosing.aspX
but still have a few...
|
by: Rizwan |
last post by:
I am writing this code for file upload in asp 3.0 on my webpage........
It is working on local system but when i want to upload an image on my
website from client machine it is not working ..........
|
by: Calvin Spealman |
last post by:
I was talking to some other people, who all were working on different
schedulers and such for coroutines. We decided to work out a common
API to give coroutines, and common rules to passing data...
|
by: Mike Silva |
last post by:
Hello all,
I'm a longtime programmer (embedded with a smattering of desktop stuff
as well) who knows very little about web programming. Right now I am
developing the prototype of a multi-user...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |