Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Wednesday, July 8, 2009

Methinks it is like a Python

On Monday, my second book on Python programming arrived in the mail. My first, Learning Python (O'Reilly), is well written but slow- I learn best by plowing through some examples and then filling in the gory details. Learning Python works in the opposite direction, so I grabbed Python Scripting for Computational Science (Springer). This book has been great for getting up to speed post-haste.

Now that I've had some free time, I've spent the past two evenings playing with the Python language and was able to recreate Richard Dawkin's "Weasel" program. This program takes a target phrase, "Methinks it is like a weasel" (Shakespeare, anyone?), and a random group of letters of the same length. Over each iteration, an offspring of the jumbled phrase is created by randomly changing some of the letters. Of X offspring, the one nearest the target phrase (the "fittest") is chosen and another generation is made. Eventually, and rather quickly, you achieve the target phrase through a set of random mutations guided by a judgment of fitness -- a neat demonstration of the principles of evolution.

Although I was able to create this program quickly using Perl, I thought it would be an excellent Python exercise. I was able to hack together a version pretty quickly, but given the powerful matplotlib package, was able to go a step further and visualize some of the results.

Convergence to the target phrase in 8332 iterations. Each generation produced 10 children phrases; each letter for each child phrase had a 0.1% chance of mutation.

Not too shabby for a few hours work. Compared to my Perl version, the source code is shorter but feels much less elegant. They both run very quickly. Python gets points for the ease of visualization through matplotlib; Perl gets points for easily integrating into a CGI script for web browser interface (though Python supposedly does this equally as well.) The comparisons won't be fair until I've given Python more than a few days' worth of work.

Here's a good online version of Weasel.
Here's another.

Wednesday, May 20, 2009

More people should do things like this.

Following some links provided in a comment in my previous post led me to this study of the happiness of programmers using different languages from Dolores Labs. They surveyed Twitter posts that concerned various languages (e.g. "I sure love Fortran!" or "C is making me ANGRY!") and tallied the results to see what language garnered the most positive responses.

Here are the results:

The winner... PERL! I guess I can't leave my favorite language quite yet, eh?

Clearly, the results should be scrutinized and a formalized study would likely yield different results. Despite these criticisms, simple studies like this are fantastic. The results give clues as to what a broader, more rigorous study would uncover. The methodology provokes thought on ways to improve the study, the implications of the outcome, etc. And more importantly, the results are just neat. I am reminded of Mark Twain's famous quote on science, "One gets such wholesale returns of conjecture out of such a trifling investment of fact."

Please explore the Dolores Labs site so I feel better about hot linking their results.

Tuesday, May 19, 2009

Shhhh... I'm having an affair!

Yep, you read it correctly- I'm cheating on my one true programming love, and I may wind up leaving her altogether. I've been a Perl monk for 3 years now, and it has been my go-to language for everything except scientific numerics and visualization. There's a good reason, too: it's easy to use, intuitive, and exceptionally powerful.

This is changing as I type, however. I've been enticed by Python, which promises to do everything that Perl can do. More importantly, it is becoming a staple in scientific programming and has powerful visualization modules available. The allure of my glue language becoming my EVERYTHING language is too much to resist. Imagine a CGI script that performs the calculation, generates the plots, and returns the requested material - and it's all in the same language! It is for reasons such as these that Python is climbing up the most used language charts.

My first task with Python is to re-create Dawkin's Weasel program, a straightforward goal (Oh, look - the good folks at New Mexico Science Watch are already on it!). I have a working version in Perl now with a CGI frontend. Let's see how quickly I can get up to speed in Python. But don't tell Perl yet; I don't think she could take it. If Python lives up to its billing, I'll let Perl down slowly.