Skip to main content

Python: Look What the Stork Dragged In

I have this Python program that I haven't run in a couple years. I wanted to see if I could make it run faster. Last time, it took 22 hours. Sure enough, using some advanced optimization techniques (and a swig of castor oil), my wife and I were able to run the program in a mere 6.5 hours! Here it is:
#!/usr/bin/env python

"""Help Gina-Marie time her contractions."""

import time

SECS_PER_MIN = 60


last_start = None
while True:
print "Press enter when the contraction starts.",
raw_input()
start = time.time()
if last_start:
print "It's been %s minutes %s seconds since last contraction." \
% divmod(int(start - last_start), SECS_PER_MIN)
last_start = start
print "Press enter when the contraction stops.",
raw_input()
stop = time.time()
print "Contraction lasted %s seconds." % int(stop - start)
print

Comments

jjinux said…
Jocylenn Grace Behrens was born at home on 5/16/2009 at 11:47PM after 6.5 hours of labor. She weighed 10lbs 8oz. Both her and my wife are recovering nicely, and we're all grateful for their good health.
Noah Gift said…
Wow, congratulations to both of you.
Anonymous said…
Congratulations !

>>> def i_wish_you(something):
... print ''.join([chr(i) for i in something])
...
>>> i_wish_you((72, 97, 112, 112, 105, 110, 101, 115, 115))
Unknown said…
Congrats! :)
Will Moffat said…
Congratulations!
P.S. That's one of the geekiest baby announcements I've ever read :-)
Unknown said…
Congratulations Friend, I'm very happy for both of you. I have been following the updates on Facebook. I miss you both and can't wait to see you all again. Take care. Your buddy and long time friend, Joe Murphy
Eric Walstad said…
Congratulations and best wishes to you and yours.
Leon Atkinson said…
Gratz!!!11!1!!!
Leon Atkinson said…
This comment has been removed by the author.
Carl Trachte said…
JJ,
Congratulations to you and the lady of the house.
That's great news.
Carl T.
jjinux said…
Tarek:

[84, 104, 97, 110, 107, 115, 44, 32, 84, 97, 114, 101, 107, 33]
jjinux said…
Thanks, everyone!
metapundit.net said…
Congratulations JJ!

-Simeon
Steve said…
Many congratulations. Glad mother and daughter are both doing well.
yacitus said…
Congratulations! How many children is that now?
Eddy Mulyono said…
Congrats, JJ.

May God richly bless you and your family for your generosity to the service of life!
Brandon Corfman said…
congratulations!
jjinux said…
The midwife corrected me. It turns out that she didn't grab the baby by the head. She slipped in her hand and hooked a finger under the baby's armpit. I was wondering how she could yank so hard without hurting the baby's neck ;) I guess those are the tricks you learn after you've delivered a few thousand kids!
BhogiToYogi said…
Hearty wishes and Congratulations!!!
jjinux said…
> Congratulations! How many children is that now?

5!

Tongue in cheek:
I figure now that I've got the process down, why should I stop? I'm going to tell everyone else to leave child rearing to the experts ;)

> May God richly bless you and your family for your generosity to the service of life!

Thanks, Eddy!
Joe Louderback said…
> 5!

120 kids? Wow :-)

Congratulations to both of you.
jjinux said…
> 120 kids? Wow :-)

Yeah, but to be fair, some of those were multiples ;)

/me giggles
Kelly Yancey said…
Congrats to you both! Knowing you, I suppose plans for the next release, JJ 6.0, are already in the works. :P
Bob Van Zant said…
Congrats dude! Heather and I are very happy for you guys. And holy crap, that's a big kid. A Behrens for sure. Can't wait to meet her.
Unknown said…
'eJxzzs9LL0osKVYoyVeozC9VSMxLUSjJSFXIzQeSRYoK4ZnFGZl56XA5IF2kkJaYm5lTqZCVXwkW\ny0gsKMjMSy0uVlSwcgEAuH0dDw==\n'.decode("base64").decode("zlib")
jjinux said…
Thanks Bob and Kelly!

Alex--cute ;)
Venkat said…
Congrates! Will she drink the AntiGravity Cola? :)
Ivan Babichuk said…
JJ, congratulations from Ukraine! Wish you God's Bless and good health for baby, mother and whole family.
jjinux said…
Thanks, Ivan :)
Shane said…
I wanted to thank you for the program you posted. I have added to it a little for my use and wanted to show you what I have done with it... This is being run from ipython console with the -pylab option.

import matplotlib.pyplot as plt
import time

spacing = [0]
dur = [0]
count = [0]
counter = 0
spacingholder = 0
SECS_PER_MIN = 60

last_start = None
while True:
print "Press enter when the contraction starts.",
raw_input()
start = time.time()
if last_start:
print "It's been %s minutes %s seconds since last contraction." \
% divmod(int(start - last_start), SECS_PER_MIN)
spacingholder = ((start - last_start)/60.0)
last_start = start
print "Press enter when the contraction stops.",
raw_input()
stop = time.time()
print "Contraction lasted %s seconds." % int(stop - start)
counter = counter + 1
dur.append(int(stop - start))
count.append(counter)
spacing.append(spacingholder)

plt.subplots_adjust(hspace=0.32,top=.93,bottom=.08) #tweak subplots so that labels fit
plt.figure(1)

plt.subplot(211)
plt.plot(count,spacing,"ro")
plt.xlabel("Contraction")
plt.ylabel("Minutes")
plt.title("Time Between Contractions")

plt.subplot(212)
plt.plot(count,dur,"ro")
plt.xlabel("Contraction")
plt.ylabel("Seconds")
plt.title("Duration of Contractions")
jjinux said…
Hahahaha!!! Congrats!!! Did you do this while she was in labor?

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit s…

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stal…

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;)

In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3, Erlang, and the original Lisp machine. The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts.

Now, I'm sure that many of these ideas have already been envisioned within Tunes.org, LLVM, Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway.

Rather than wax philosophical, let me just dump out some ideas:Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code." I bet it&…