Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > n00bie wants advice.

Reply
Thread Tools

n00bie wants advice.

 
 
bsagert@gmail.com
Guest
Posts: n/a
 
      07-02-2008
This simple script writes html color codes that can be viewed in a
browser. I used short form hex codes (fff or 000, etc) and my list
has only six hex numbers otherwise the results get rather large. I
invite criticism as to whether my code is "pythonic". Are there other
ways to generate the hex combos besides the nested "for" loops? Thanks
in advance, Bill

list = ['3','6','9','b','d','f']

s = '<html><head><style>h1{margin:0}</style></head><body>\n'

for a in list:
for b in list:
for c in list:
s += '<h1 style="background:#'+ a + b + c +'">'+ a + b + c +'</h1>
\n'

s += '</body></html>'

f = open('c:/x/test.htm', 'w')
f.write(s)
f.close()
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      07-02-2008
On Tue, 01 Jul 2008 23:25:53 -0700, bsagert wrote:

> This simple script writes html color codes that can be viewed in a
> browser. I used short form hex codes (fff or 000, etc) and my list
> has only six hex numbers otherwise the results get rather large. I
> invite criticism as to whether my code is "pythonic".


You should not rebind the name `list` because it shadows the built in type
of that name then. A more descriptive name would be nice anyway, i.e.
`hex_digits`. And strings are iterable too, so it's a bit shorter and
easier to type the digits a string.

Repeatedly concatenating strings with ``+=`` might be performance problem.
Python strings are immutable so this operation has to copy the involved
and growing strings over and over again. Although the current CPython
implementation can optimize here in some cases, the usual idiom is to use
the `join()` method of strings to build a string from components in a list
or iterable.

Alternative implementation of your script:

from __future__ import with_statement

def main():
html_template = ('<html><head><style>h1{margin:0}</style></head><body>\n'
'%s\n'
'</body></html>\n')
header_template = '<h1 style="background:#%s">%s</h1>'
hex_digits = '369bdf'
colors = (a + b + c for a in hex_digits
for b in hex_digits
for c in hex_digits)
html = html_template % '\n'.join(header_template % (c, c) for c in colors)
with open('test.html', 'w') as html_file:
html_file.write(html)

if __name__ == '__main__':
main()
 
Reply With Quote
 
 
 
 
oj
Guest
Posts: n/a
 
      07-02-2008
On Jul 2, 7:25*am, (E-Mail Removed) wrote:
> This simple script writes html color codes that can be viewed in a
> browser. *I used short form hex codes (fff or 000, etc) and my list
> has only six hex numbers otherwise the results get rather large. I
> invite criticism as to whether my code is "pythonic". Are there other
> ways to generate the hex combos besides the nested "for" loops? Thanks
> in advance, Bill
>
> list = ['3','6','9','b','d','f']
>
> s = '<html><head><style>h1{margin:0}</style></head><body>\n'
>
> for a in list:
> * * * * for b in list:
> * * * * * * * * for c in list:
> * * * * * * * * * * * * s += '<h1 style="background:#'+ a + b + c +'">'+ a + b + c +'</h1>
> \n'
>
> s += '</body></html>'
>
> f = open('c:/x/test.htm', 'w')
> f.write(s)
> f.close()


You could write the loop like this:

for red, green, blue in [(r, g, b) for r in list for g in list for b
in list]:
s += blah blah blah

but, arguably, that isn't easier to read or understand. It's a matter
of taste, I guess.

As has already been mentioned, list is not a good name, because it is
already used.

Also, personally, I find it easier to read strings that aren't
constructed with concatenation, but using pythons string formatting
gubbins:

'<h1 style="background: #%s%s%s">' % (red, green, blue)

Again, I think this is mostly personal preference.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My n00bie brain hurts after "Python setup.py install". bsagert@gmail.com Python 2 06-22-2008 04:26 PM
Brand new - browser only wants to dial =?Utf-8?B?Z3Rw?= Wireless Networking 3 04-18-2006 10:52 AM
Re: Wants to get MCP. Need Help! nguyenpv Microsoft Certification 0 01-19-2005 07:01 PM
n00bie questions Se'noj C++ 5 08-28-2004 06:15 AM
Some file wants to load itself onto my computer sannidy Firefox 1 09-24-2003 11:39 AM



Advertisments