472,958 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Need help writing coroutine

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

Nov 7 '07 #1
1 1325

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

Nov 7 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
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...
13
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...
8
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...
19
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...
2
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...
18
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...
2
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...
1
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 ..........
0
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...
8
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.