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 10 3461
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
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
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>
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
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
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
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
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
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
?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: madsgormlarsen |
last post by:
Hi
I am making a html table builder, and would like every second row to be
blue. So for that purpose I need a number that changes on every
iteration of a while loop, for exampel so that a value...
|
by: Řyvind Isaksen |
last post by:
Hello!
I need to dynamic generate a SQL statement based on how many images a user
select to upload.
Here you see an example with 2 images. It can be up to 50 images and I dont
want to write...
|
by: Matt B |
last post by:
I have a need to alternate output row colour where not every row in the
sequence is output, so I cannot base the colour on whether position() is odd
or even.
e.g.
....
<xsl:for-each...
|
by: Patrick M. |
last post by:
Hello. I just got the K&R 2nd Edition of "The C Programming Language". I
also just finished exercise 1-13, and I've coded a much shorter,
commented solution. I'm not sure if you're still updating...
|
by: David Helgason |
last post by:
I think those best practices threads are a treat to follow (might even
consider archiving some of them in a sort of best-practices faq), so
here's one more.
In coding an game asset server I want...
|
by: sconeek |
last post by:
Hi all,
I am generating a html based table with multiple rows of data coming
in real time from a postgres DB. The underlying technology is java
based however the front end is html.
now i am...
|
by: lennyw |
last post by:
Hi
I'm trying to use XSLT to do an xml to xml transformation where the
output xml contains summary data on the information in the input xml.
I've succesfully done a Muenchian grouping of the...
|
by: ash |
last post by:
i have a web page using frameset split into few pages. And I want to
generate one page of HTML code and send it through email. My question is
have to generate a HTML page using asp?
Thx very much.
|
by: Zytan |
last post by:
Obviously you can't just use a simple for loop, since you may skip
over elements.
You could modify the loop counter each time an element is deleted.
But, the loop ending condition must be...
|
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...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
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...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
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...
|
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...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |