472,374 Members | 1,473 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,374 software developers and data experts.

n00bie wants advice.

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()
Jul 2 '08 #1
3 841
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()
Jul 2 '08 #2
On 2008-07-02, bs*****@gmail.com <bs*****@gmail.comwrote:
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
ok.

variable names of 1 letter are very bad. Use more meaningful names like 'red'
'green' etc.

'list' is better, but also a name reserved by Python, so change that too.

Indenting is normally 4 spaces in Python

You can see "a + b +c" twice, compute it once, and assign it to a intermediate
variable

Use string formatting for better readability.

In this case, you could also open the file earlier, and write all strings
directly to file instead of first creating a string in memory

Otherways of creating the colour code permutations: In this case, this is most
Pythonic imho. You could write a list comprehension of even a recursive
function, but I think it wouldn't increase readability.
Albert

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()
Jul 2 '08 #3
oj
On Jul 2, 7:25*am, bsag...@gmail.com 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.
Jul 2 '08 #4

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

Similar topics

6
by: Doug Mitchell | last post by:
Dear Group, My son who is in grade 7 has *just* started going through the book "Python for the Absolute Beginner" by Michael Dawson. He and I have no programming experience. He is beginning this...
27
by: xeys_00 | last post by:
I'm a manager where I work(one of the cogs in a food service company). The boss needed one of us to become the "tech guy", and part of that is writing small windows programs for the office. He...
18
by: Andy | last post by:
hello all I've just finished a reasonably simple site for a client, validated it, checked it across browsers/resolutions etc. The problem for me is that my clients want to keep it updated...
20
by: parametriceq | last post by:
Hello, I just realized there's only a "g" separating me from....me? Old Coder or Old Codger? Anyway, I haven't programmed since the late 80's. My experience then was with DEC PDP-11's and...
24
by: sundew | last post by:
Hi all, I am developing an open source DHTML project named Wednus Window. http://wednus.com Wednus Window is a DHTML Web-Application Windowing System. It shell websites/web-applications with...
0
by: Pip Wilson | last post by:
New online magazine wants RSS quote Webmaster and editor need advice and implementation solutions on RSS strategies, both to and from our site. Web site is php and mySQL driven, uses html 4 loose...
11
by: Mark | last post by:
Hi, For the last 2 years I've been developing vehicle tracking/telemetric software for a company as a self employed individual. The project is quiet big, and is going to be there flagship...
10
by: musosdev | last post by:
Hi guys I'm trying to migrate to VS2005... I've managed to do that, but realised I'd opened my web projects as file projects, and I'm getting the error about network BIOS command limit. ...
3
by: bsagert | last post by:
I downloaded BeautifulSoup.py from http://www.crummy.com/software/BeautifulSoup/ and being a n00bie, I just placed it in my Windows c:\python25\lib\ file. When I type "import beautifulsoup" from...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.