Archive for the ‘software’ Category

Python fun

June 8, 2009

Earlier I had mentioned how much fun coding in python is. So I recently needed to do a few standard deviation calculations by inputting numbers the first time, and reading other numbers, one line at a time from a file. I could do this by plugging in numbers in some calculating device, use excel, or … But instead I figured I’d write a few lines of code and have an excuse to try out Eric, a Python IDE. It seems to support ruby as well, and the save as option suggests support for editing Java, javascript, tex, sql, etc. etc. Overall, I found using Eric to be a pleasure. It’s got all the usual stuff, setting breakpoints, stepping through code, project management, and even built-in support for source control (Subversion and cvs). Of course, the very name Eric, is in keeping with the Python theme, being named after Eric Idle of Monty Python fame. Further keeping with the fame, there exists a code refactoring menu item labeled Bicycle Repairman.

So, here’s a little taste of some code I scribbled out in about 5-10 minutes in Python.


def standardDeviation(mean, numberArray):
""" Calculate the standard deviation
Expects the mean to have already been calculated
"""
sumOfSquares = 0.0
for num in numberArray:
sumOfSquares = sumOfSquares + num *num

rootMeanSquare = sumOfSquares/len(numberArray)
return math.sqrt(rootMeanSquare – mean*mean )

def readInput():
print “Enter a white space seperated series of numbers”
numbers = raw_input(“==>”)
return [int(n) for n in numbers.split()]

def readData(theFile):
try:
myfile = open(theFile, ‘r’)
except:
print “Could not open file”
numlist = []
for line in myfile:
try:
numlist.append(int(line))
except:
print “Invalid input in file”

myfile.close()
return numlist

(Having just done the preview, I’ve noticed that the WordPress formatters did not keep my indentations. Those of you who already know Python know what I’m talking about. For the rest, just keep in mind that every block of code, for example, everything after a def and contained within in it, should be indented with respect to the def label. Simply trying to tab or add spaces doesn’t seem to work right on this editor. I’ve got about 2 seconds before needing to drive to work so later on, if I get time, I’ll see if I can make it look right.)

The def label indicates the start of a function. The readData function opens up a file and we’re prepared to print a warning if the file doesn’t exist. We append each number to an array called numlist. Reading the file was trivial. The line “for line in file” does the trick. Each line is stored in the variable line each step through the loop. Not shown here is the fact that later on I use this numbers to calculate a mean and pass the results to the standardDeviation function.

Later on, if I get time and am motivated, I can prettify this up quite a bit. I could encapsulate a lot of this in a full blown python class and provide more statistical calculation functionality, throw in TK to get some graphics and charting capability, and generally create a nice little statistics package. Of course, a lot of that functionality already exists elsewhere, but its always fun to roll your own to see if you can find a different take on things.

Advertisements

Waving at Google, etc.

June 4, 2009

After yet another unplanned hiatus from blogging (too crazed of a schedule), let’s see if I can get back on track. A few items going on in the news these days. Sadly, of course, the California Supreme Court decided to keep California backwards by denying marriage equality to a subset of people.

On a more positive note, Newsweek recently acknowledged that Oprah has been supporting charlatans, frauds, and, at best, highly questionable and possibly dangerous techniques. I’m sure if she thinks positively about it, the criticism will just go away, as that is her secret after all.

But in the best news (well, putting on my developer’s hat anyway), Google was just talking about the
Google Wave, an instant messaging, emailing, photo album building, document creating, bug tracking (one of the gadget extensions anyway), ad infinitum, shiny new tool with an API for developers to write applications for. The only thing it doesn’t seem to do is brew beer yet, but I’m sure somebody will be working on that. The actual launch date is still a bit out, and perhaps they did this to steal a little wind from Microsoft, which certainly isn’t a bad thing. A very cool tool tool; check it out.

Python for fun

May 1, 2009
Pining for the fjords

Pining for the fjords

A while back I had posted something about Paul Graham’s essay on good hacking environments. As an aside, one of the observations he made (with which I don’t necessarily agree) is that there will be a tendency to find better hackers coding Python than coding Java. I do more Java myself these days, but find I really like Python as well. So let’s talk about Python for a bit. This has become relevant for me because I want to get started on one of the coding projects that has been sitting on my queue at home. After pondering things for a while, I’ve decided to go Python (I already have several Java projects lined up). Python code is pretty, web friendly, object oriented (though you can write it like it isn’t if you really want), and has a lot of useful libraries.

The coolest thing about the language, of course, is that the creator, Guido van Rossum, named it after Monty Python, a little British comedy group that was responsible for finding things like the holy graile, the meaning of life, and starting a religion after a boy called Brian who grew up to be a man called Brian. One of the funky things about the language of Python is that all blocks must be indented. It is part of the syntax as it were. That took me some getting used to, but since pretty code should be nicely indented anyway, that’s not really that intrusive. There are editors that will assist with that, such as emacs (also xemacs), which will run under Windows as well. Python also has its own ide as well, called Eric, which coincidentally is also the name of one of the Monty Python members. Although Python is almost trivially on every Linux system (though you may need to select the software; I did for SuSE) you can have it Windows too.

One of the fun things about Python is its support for imaginary (complex) numbers (square root of -1, for well defined rules of multiplication). Stick a j on a number to make it imaginary. For example, in Python you can write a complex number such as a = 3.0 + j5.0 . Then establish that a.real is 3.0 and a.imag is 5.0. With that comes a lot of manipulations, etc. You can do the same thing in Java, but you would need to find a library for it, or write it yourself.

So what else does Python give you? One of the powerful web application servers out there is Zope, written in Python. I have not played with this yet, but am reasonably confident that if you are writing Python apps, you could easily leverage what Zope offers. You can do graphics using the TK (toolkit) interface arising out of TCL/TK by using TKInter, and I’ve recently found out about vpython, another library that allows you to do 3d graphics and animation. Apparently a lot of colleges are using this to have inexperienced (from a coding point of view) students write their own physics demos. I expect I’ll play with this a bit going forward.

What does code in Python look like? Here’s an example of some really simple throwaway code I did several years ago. This sends email to a specified address (maintained in a separate configuration file called mailconfig.py).

import smtplib, string, sys, time, mailconfig, os
mailserver = mailconfig.smtpservername

interestingStuff = sys.argv[1]

fd = os.popen('hostname')
myhostret = fd.readlines()[0].split('\n')

From = mailconfig.sender
To = mailconfig.recipient
Subj = 'Some Alert!'

date = time.ctime(time.time())
text = ('From: %s\nTo: %s\nDate: %s\nSubject: %s\n\n'
% (From, To, date, Subj))

text = text + "Something interesting happened on " + myhostret[0] + " for " + interestingStuff

text = text + "\n\n" + mailconfig.signature

print 'Connecting...'
server = smtplib.SMTP(mailserver)
failed = server.sendmail(From, To, text)
server.quit()

Of course, one can add to this and make it as complicated as one wants. The import at the beginning just specifies some libraries like the stmplib that the Python interpreter needs to do its work. Python can look even more simple. All you need to do is run the interpreter and type in equations to use it as a calculator (that’s what I often do for quick calculations…either that or Lisp).

So, go ahead, if you are not a software developer, try your hand at some simple Python code. I am of the opinion that everyone should know a little coding, just like everyone knows how to hammer a nail in a piece of wood. Not everybody is going to build fancy furniture and houses, but should be able to do a few simple things here and there. Python is a great language to get started playing around and pick up a few things. I promise it won’t kill your parrot.

Office space

April 23, 2009
Typical work environment

Typical work environment

I’ve barely had time to really put the time into really quality blogging lately, but here’s a topic I think I can whip out pretty quickly. A bit of a rant on work space environments. If nothing else, I hope it will be somewhat entertaining.

A friend sent me a link to an essay by Paul Graham (author of ANSI Common Lisp, sitting either on my bookshelf or buried in a stack papers on my desk. I’m not going to go looking for it at the moment.). The essay was dated 2004 but is still relevant today.

First, I don’t agree when he implies that are probably no good Java hackers. I’ve known quite a few as I’ve been coding in Java (though I do not claim to be a great hacker myself). I’ve also written perl, python, and lisp and have seen really good code written in those languages as well. I’ve also seen really crappy code written in all those languages. We can get into whole lengthy discussion on what languages or means of expression are good in which type of circumstances, but that’s a completely different post with no clear cut answers. So, that aside, let’s move on to work space environments.

If you are a company whose main product is software, the software you create is only as good as your developers. Their thoughts and creativity are your product. One way to maximize the likelihood of crappy product is pollute those thoughts by putting them in a totally distracting environment where they are constantly interrupted and distracted by people walking around, random conversations taking place here and there surrounding them, etc. In a word, cubicles. Whoever designed the cubicle work environment was not a hacker. In Graham’s words,

The cartoon strip Dilbert has a lot to say about cubicles, and with good reason. All the hackers I know despise them. The mere prospect of being interrupted is enough to prevent hackers from working on hard problems. If you want to get real work done in an office with cubicles, you have two options: work at home, or come in early or late or on a weekend, when no one else is there. Don’t companies realize this is a sign that something is broken? An office environment is supposed to be something that helps you work, not something you work despite.

He is absolutely right. In spite of cubicles being exactly the wrong environment to put hackers in, practically every company I know of does it. Every since I started working in software, every company I’ve been in, with one exception, has been a cubicle farm. When was I the most productive? At home, or after hours, or in the one company I was at where I had an office. Good hackers like getting quality work done, and getting it done well and quickly. An environment that runs contrary to this goal is, I find, generally despised. If you are a hacker reading this who actually likes working in a fabric covered box, please let me know in the comments. Allow me to be the first to congratulate you as you’ll be the first I’ve ever met.

The rest of the essay continues on to make really valid points. I highly recommend a read.

As an addition to that, I would have to add one thing. If you are a manager who is determined to damage productivity while possibly decreasing morale in the process, here’s a sure fire method. Introduce heavy handed processes that run completely orthogonal to how software developers actually work. But I’ll cover that in another post sometime.

Herding Cats

April 1, 2009

From Slashdot, I’ve learned about a new book coming out all about mastering the cat command in Unix and Unix-like systems. This seems as fun as the tac bash shell command (well, I have on at least one or two occasions used tac before, for some strange reason or another). For the Unix uninitiated, cat is a command that simply prints a file on screen. For example, if you have a file called, someinterestingfile.txt, you would type “cat someinterestingfile.txt” to see the whole thing flash by. If it is too big, you could type “cat someinterestingfile.txt | more” or more simply, “more –or less– someinterestingfile.txt”. So why does cat need its own book? From the cat link:

O’Reilly Net: Isn’t mastering cat supposed to be quite easy? Does it really necessitates its own book?

Shlomi Fish: Hell no! Mastering cat is not easy at all. In fact, mastering cat is almost as difficult as herding cats.

For example, one case where I found that people truly underestimate the power of cat is in the prefixing a line example. You can do that with:

echo “This would be the first line” | cat – myfile.txt > myfile.txt.new
mv -f myfile.txt.new myfile.txt

But people do not realize that and instead opted to use sed, awk, or even perl (!). It can be taken further, of course. If the prefix is already in its own file, you can simply use cat prefix.txt myfile.txt

Of course, if you want to append the same text to both the start and the end of a file, you can’t do that with cat – myfile.txt -. It simply doesn’t work that way. So, I end up explaining a lot about UNIX pipeline concepts in the book.

I’m looking forward to his next book on echo.

On a completely unrelated note, Happy April 1st everybody!

New OS

March 29, 2009

Sort of off topic from anything I’ve written here, but, hey, I think it was kind of fun. I spent yesterday afternoon installing the openSuSE 11.1 Linux distribution on a new system I bought recently. So, I can now give a brief review so far.

Seems like a pretty stable distro so far. The installation went very smoothly. I had to spend a little time mucking about with the partitioning, since the interface is significantly different than what I’m used to with SuSE (10.2 and going back earlier). But I managed to create the / mount, swap, and stick everything else into logical volumes (/home, /usr, /opt, /{and so on}) for ease of expansion later. Once that was done, everything went as smooth as liquid helium. The sound card (82801G ICH7 family high definition audio controller) worked perfectly. My last sound card worked as well, but I do remember the days when sound cards on Linux was kind of hit and miss.

One of my pet peeves in software design is poor user interface design. There were a few glitches in this area. I opted to use a static IP address (for some future DNS set up, and didn’t want to fool around with callbacks, being kind of lazy) instead of a DHCP (obtaining a dynamic IP). In spite of a perfectly valid IP I entered, it kept complaining that it was invalid. Turns out I had an extra space before the address. How difficult could it be to trim out extra spaces? This is what we do in our code all the time. The most tedious part was getting online updates near the end of the installation. We have a cable network connection to “the cloud” and this just took forever. Which is o.k., I’ve got things to read while this is going on. But, when one connection fails it would throw up a dialog asking to retry, ignore, or skip. I’d usually hit retry and everything would be fine. It would also give a little beep when pressing the “Retry” button. In fact, it would have been more useful to have the beep when throwing up the dialog. I don’t need audio confirmation that I pressed a button; I have visual confirmation of that. The updates take so long that I need an audio cue that the dialog came up. There were many instances where I’d look up from my reading and chance to see the dialog.

It is still painful to grab all software to install (all the optional packages and such). In the old days, after selecting all, you wouldn’t actually get everything but would still need to go and select individual packages. I didn’t actually see a select all option this time, but had to go in from the beginning and select everything individually. I do tend to want to get everything out there, all kernel source, all development tools, etc., etc., so I’m not sure how much of an issue this would be for the average user. But I will say that compared to previous releases, there was much less in the way of having to slog through dependency hell this time. They seem to have fixed this up pretty nicely.

With KDE 4.1, the task bar disappeared when moving to multiple desktops. After researching this for a while and finding no clear answer applicable to what I was doing, I found rebooting fixed the problem. Probably restarting the windows manager would have been sufficient, but oh well. KDE 4.1 does seem pretty sexy (yes, I opted for KDE instead of gnome, although I went ahead and installed all the gnome software). Infinitely configurable as well, there’s a whole lot to play with here.

But all in all, those complaints are pretty minor compared to the big changes that seems to have gone into this distro. The NIC (network card) drivers worked great. On the old SuSE 10.2, my NIC card would not work unless I have a noapic parameter to the kernel on boot. Took a lot of headaches to figure that out. With 11.1, the network connection came right up with no headaches at all. It came with a lot of cool apps and server additions. I’ll have to spend more time playing around with it to see everything they got, but so far, responsive and solid. If you want to try a Linux distribution, you wouldn’t make a bad choice to go with this one.

After backing things up and moving things around, I’ll replace the old SuSE 10.2 with Ubuntu to play with and see how that goes. I keep hearing good things about Ubuntu and guess I should see what the fuss is about.

Computer Therapy

October 28, 2008

Fascinating. NASA has been working on a way to have private therapy sessions in space.. It looks basically like a keyword selection of video clips of an actual therapist to deal with specific problems related to depression or other problems. From the article:

Clinical tests on the four-year, $1.74 million project for NASA, called the Virtual Space Station, are expected to begin in the Boston area by next month.

The new program is nothing like science fiction’s infamous HAL, the onboard artificial intelligence that goes awry in “2001: A Space Odyssey.” The Virtual Space Station’s interaction between astronaut and computer is far less sophisticated and far more benevolent.

In the project, sponsored by the National Space Biomedical Research Institute, a recorded video therapist guides astronauts through a widely used depression therapy called “problem-solving treatment.”

The recording helps astronauts identify reasons for their depression. Then the program helps them make a plan to fight the depression, based on the descriptions the astronauts type in about their problems.

Astronauts also can learn strategies for handling conflict through interactive role-playing, and even read psychology books.

As a first step, not bad. I could envision going beyond pre-recorded video and have various rules based learning algorithms that could tailor solutions for specific individuals. That could probably only go so far for the forseeable future though. Still, it might be fun to have some Easter eggs. For example, if an astronaut types, “Let me in”, there would be the response, “I’m sorry. I can’t do that Dave.”

Or, if the astronaut’s laptop is Linux (though you could run this on Windows also), they could just use the emacs psychotherapist.