471,349 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Best way to generate alternate toggling values in a loop?

I'm writing this little Python program which will pull values from a
database and generate some XHTML.

I'm generating a <tablewhere I would like the alternate <tr>'s to be

<tr class="Even">
and
<tr class="Odd">

What is the best way to do this?

I wrote a little generator (code snippet follows). Is there a better
(more "Pythonic") way to do this?
# Start of Code

def evenOdd():
values = ["Even", "Odd"]
state = 0
while True:
yield values[state]
state = (state + 1) % 2
# Snippet

trClass = evenOdd()
stringBuffer = cStringIO.StringIO()

for id, name in result:
stringBuffer.write('''
<tr class="%s">
<td>%d</td>
<td>%s</td>
</tr>
'''
%
(trClass.next(), id, name))
# End of Code

Oct 18 '07 #1
10 3339
On Wed, 2007-10-17 at 23:55 +0000, Debajit Adhikary wrote:
I'm writing this little Python program which will pull values from a
database and generate some XHTML.

I'm generating a <tablewhere I would like the alternate <tr>'s to be

<tr class="Even">
and
<tr class="Odd">

What is the best way to do this?

I wrote a little generator (code snippet follows). Is there a better
(more "Pythonic") way to do this?
# Start of Code

def evenOdd():
values = ["Even", "Odd"]
state = 0
while True:
yield values[state]
state = (state + 1) % 2
# Snippet

trClass = evenOdd()
stringBuffer = cStringIO.StringIO()

for id, name in result:
stringBuffer.write('''
<tr class="%s">
<td>%d</td>
<td>%s</td>
</tr>
'''
%
(trClass.next(), id, name))
This is a respectable first attempt, but I recommend you familiarize
yourself with the itertools module. It has lots of useful tools for
making your code more elegant and concise.

Rather than spelling out the final result, I'll give you hints: Look at
itertools.cycle and itertools.izip.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 18 '07 #2
On 2007-10-17, Debajit Adhikary <de******@gmail.comwrote:
# Start of Code

def evenOdd():
values = ["Even", "Odd"]
state = 0
while True:
yield values[state]
state = (state + 1) % 2
I'd replace the last line with

state ^= 1

to save a couple instructions, but I spend too much time
working with micoroprocessors running on clocks measured in the
KHz.

There are probably other more Pythonic ways...

--
Grant Edwards grante Yow! My EARS are GONE!!
at
visi.com
Oct 18 '07 #3
On Oct 18, 1:55 am, Debajit Adhikary <debaj...@gmail.comwrote:
I'm writing this little Python program which will pull values from a
database and generate some XHTML.

I'm generating a <tablewhere I would like the alternate <tr>'s to be

<tr class="Even">
and
<tr class="Odd">

What is the best way to do this?

from itertools import izip

def toggle(start=True):
flag = start
while 1:
flag = not flag
yield flag
CSS = ("even", "odd")

HTML = '<tr class="%s"><td>%d</td><td>%s</td></tr>'

result = [(1, 'One'), (2, 'two'), (3, 'Three'), (4, 'Four'), (5,
'Five')]

for flag, (id, name) in izip(toggle(), result):
print HTML % (CSS[flag], id, name)
<tr class="even"><td>1</td><td>One</td></tr>
<tr class="odd"><td>2</td><td>two</td></tr>
<tr class="even"><td>3</td><td>Three</td></tr>
<tr class="odd"><td>4</td><td>Four</td></tr>
<tr class="even"><td>5</td><td>Five</td></tr>

Oct 18 '07 #4
On 10/18/07, Carsten Haese <ca*****@uniqsys.comwrote:
On Wed, 2007-10-17 at 23:55 +0000, Debajit Adhikary wrote:
I'm writing this little Python program which will pull values from a
database and generate some XHTML.

I'm generating a <tablewhere I would like the alternate <tr>'s to be

<tr class="Even">
and
<tr class="Odd">

What is the best way to do this?

I wrote a little generator (code snippet follows). Is there a better
(more "Pythonic") way to do this?
# Start of Code

def evenOdd():
values = ["Even", "Odd"]
state = 0
while True:
yield values[state]
state = (state + 1) % 2
# Snippet

trClass = evenOdd()
stringBuffer = cStringIO.StringIO()

for id, name in result:
stringBuffer.write('''
<tr class="%s">
<td>%d</td>
<td>%s</td>
</tr>
'''
%
(trClass.next(), id, name))

This is a respectable first attempt, but I recommend you familiarize
yourself with the itertools module. It has lots of useful tools for
making your code more elegant and concise.

Rather than spelling out the final result, I'll give you hints: Look at
itertools.cycle and itertools.izip.
Why not just use enumerate ?

clvalues = ["Even", "Odd"]
for i, (id, name) in enumerate(result):
stringBuffer.write('''
<tr class="%s">
<td>%d</td>
<td>%s</td>
</tr>
'''
%
(clvalues[i % 2], id, name))

Cheers,

--
--
Amit Khemka
Oct 18 '07 #5
On Oct 18, 12:11 pm, "Amit Khemka" <khemkaa...@gmail.comwrote:
On 10/18/07, Carsten Haese <cars...@uniqsys.comwrote:
Rather than spelling out the final result, I'll give you hints: Look at
itertools.cycle and itertools.izip.

Why not just use enumerate ?

clvalues = ["Even", "Odd"]
for i, (id, name) in enumerate(result):
stringBuffer.write('''
<tr class="%s">
<td>%d</td>
<td>%s</td>
</tr>
'''
%
(clvalues[i % 2], id, name))
I like this code: straightforward and pragmatic. Everyone else seems
to be reinventing itertools.cycle - they should have listened to
Carsten, and written something like this:

import itertools

clvalues = itertools.cycle(['Even', 'Odd'])
for clvalue, (id, name) in itertools.izip(clvalues, result):
stringBuffer.write('''
<tr class="%(name)s">
<td>%(id)d</td>
<td>%(clvalue)s</td>
</tr>''' % locals())

--
Paul Hankin

Oct 18 '07 #6
On 2007-10-18, Gerard Flanagan <gr********@yahoo.co.ukwrote:
On Oct 18, 1:55 am, Debajit Adhikary <debaj...@gmail.comwrote:
>I'm writing this little Python program which will pull values from a
database and generate some XHTML.

I'm generating a <tablewhere I would like the alternate <tr>'s to be

<tr class="Even">
and
<tr class="Odd">

What is the best way to do this?


from itertools import izip

def toggle(start=True):
flag = start
while 1:
flag = not flag
yield flag
I like the solution somebody sent me via PM:

def toggle():
while 1:
yield "Even"
yield "Odd"

--
Grant Edwards grante Yow! Are we THERE yet?
at
visi.com
Oct 18 '07 #7
Grant Edwards <gr****@visi.comwrote:
...
I like the solution somebody sent me via PM:

def toggle():
while 1:
yield "Even"
yield "Odd"
I think the itertools-based solution is more elegant:

toggle = itertools.cycle(('Even', 'Odd'))

and use toggle rather than toggle() later; or, just use that
itertools.cycle call inside the expression instead of toggle().
Alex

Oct 18 '07 #8
On Oct 18, 2:29 am, Grant Edwards <gra...@visi.comwrote:
On 2007-10-17, Debajit Adhikary <debaj...@gmail.comwrote:
# Start of Code
def evenOdd():
values = ["Even", "Odd"]
state = 0
while True:
yield values[state]
state = (state + 1) % 2

I'd replace the last line with

state ^= 1

to save a couple instructions, but I spend too much time
working with micoroprocessors running on clocks measured in the
KHz.

There are probably other more Pythonic ways...

I always use:

state = 1 - state

for toggles. I doubt it's much more pythonic though :)

Iain

Oct 18 '07 #9
On Oct 18, 3:48 pm, Iain King <iaink...@gmail.comwrote:
On Oct 18, 2:29 am, Grant Edwards <gra...@visi.comwrote:
On 2007-10-17, Debajit Adhikary <debaj...@gmail.comwrote:
# Start of Code
def evenOdd():
values = ["Even", "Odd"]
state = 0
while True:
yield values[state]
state = (state + 1) % 2
I'd replace the last line with
state ^= 1
to save a couple instructions, but I spend too much time
working with micoroprocessors running on clocks measured in the
KHz.
There are probably other more Pythonic ways...

I always use:

state = 1 - state

for toggles. I doubt it's much more pythonic though :)

Iain
why not do
state = not state
?

Oct 18 '07 #10
On Thursday 18 October 2007 09:09, Grant Edwards wrote:
I like the solution somebody sent me via PM:

def toggle():
while 1:
yield "Even"
yield "Odd"
That was me.
Sorry, list, I meant to send it to everyone but I my webmail didn't respect
the list* headers :(.

Thanks, Grant!

--
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
Oct 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by madsgormlarsen | last post: by
1 post views Thread by Řyvind Isaksen | last post: by
13 posts views Thread by Patrick M. | last post: by
4 posts views Thread by sconeek | last post: by
6 posts views Thread by ash | last post: by

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.