Thursday, June 04, 2009

A Room Heater, in Python.

It is about 8 degrees C this morning. So cold, especially when last week we had high twenties.

To help solve the problem, a friend suggested I write a heater application to help warm me up. Not one to refuse a challenge, and always eager to show that Python can solve all your problems, I came up with a Room Heater, in Python, in 15 lines of code.

import time
import sys

try:
D = int(sys.argv[1])
except IndexError:
D = 10
except ValueError:
print "Usage: %s MINUTES"%sys.argv[0]
raise SystemExit

print "Starting Heater..."
start = time.time()
while (time.time() - start) < (D*60): pass
print "Heater is stopping."

It works very well, especially if you are using a laptop, on your lap of course! It takes about one minute or so for your CPU to get to hot enough to be felt, and if you are using a Mac Book Pro, like me, you'll really feel the heat!

10 comments:

Ορέστης said...

Planet Python in Google Reader showed the comparison line as:

while (time.time() - start) (D * 60): pass

Which led to me a console to see if timedelta objects were callable :)

Of course, time - time = float, but datetime - datetime = timedelta, so it was an irrelevant observation anyway.

Simon Wittber said...

Oops, those pesky angle brackets. Thanks for letting me know.

Chris said...

Next step is to use the multiprocessing module to use all the cores on a multi-core system. You'll be basking in the warmth in no time!

livibetter said...

At stage 3, after utilizing all CPU cores, you can focus on mem IO, GPU, audio chip, make harddisk or anything spinning start to spin madly, light up every led. Basically, use every electronics in the computer. :-D

Mike said...

LOL, This seems like such a disaster waiting to happen. I can see it now "Guy,s laptop goes up in flames while he was trying to keep warm"

Geekwad said...

How would you get the most heat out of a laptop display? Light to max, all black? Light to max, display a high-frequency, high-contrast image, and xor with 0xff every screen refresh?

Lars Marius Garshol said...

Nearly as cool as the beer heater, in Python.

Anonymous said...

perl -e 'alarm(60*$ARGV[0]); 1 while 1' 1

Anonymous said...

;redcode
;name Imp
;author A.K. Dewdney
;assert 1

imp     mov imp,imp+1

        end imp

PS: I just learned to hate Blogger.

room thermostat said...

heaters are good for spot heating and they don’t use very much energy. They’re small and portable and can be plugged into any nearby wall socket, allowing for easy and convenient use in any room of your house.

Popular Posts