Doing quick tasks in Python

Posted in coding, computers, programming, python, scripting on March 31st, 2006 by kerneljack

A lot of the time at work I have to come up with quick solutions to various programming problems, and I mean *real* quick. There is usually very little time for finding the right library or open source toolkit that has already solved the problem or some part of it and then to figure out how to integrate that into our own workflow. It is usually much quicker to simply write a few scripts that get the job done.

In such situations I increasingly find myself solving these problems using the Python programming language. I’ve usually whipped up scripts using bash scripts and even Perl but I often find that I have to “re-learn” Perl again and again, or at least the part that I’m temporarily using to solve some problem. Python just seems (and looks) so much natural and cleaner. I’ve been fascinated by Ruby recently after my brush with Ruby on Rails and I am seriously trying to find a project to use it in all the time!

Anyway I wanted to give some examples of Pythons’ ability to solve quick problems. I’ve used it to make remote backups, check if certain services are running remotely and to fetch and delete mail off a remote server. For example, the following snippet of code can be used to send mail:

import smtplib

mailServer = smtplib.SMTP(serverURL)
mailServer.sendmail(sender, to, message)
mailServer.quit()

Really sweet and simple. I’m sure you can do this in only 1 line or even half a line in Perl (or even Python!) but this is really clear and concise. When I needed to write a script to send mail, I simply googled for it and dug this up in literally 20 secs! That’s one other reason I like writing scripts in Python: you can always google for snippets of code to do stuff.

Similarly, the following can be used to check if SSH is running and accepting connections on a remote server:

IDENT_STRING = "SSH"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, int(PORT)))
data = s.recv(1024)
s.close()

if data.find(IDENT_STRING) == -1:
sys.exit (1)
# if everything OK, exit normally
sys.exit(0)

If you put this in a script and run it, you can use the exit code (echo #?) to determine if SSH was running or not. Very useful if you’re writing a script to make backups to a remote server; you need to make sure that the service is up and running on the server and that it is accepting connections from the local machine.

I will try to see if Ruby makes any of these tasks even easier to do or perhaps easier to write and comprehend. I might even give Groovy a try since Java is my day job and Groovy is described as a scripting language for Java programmers.

Windows XP on a Mac!

Posted in computers, news, operating systems on March 27th, 2006 by kerneljack

The motherboard on my server died 2 weeks ago and it’s taken me this long to find a replacement and get the server up and running again. A benefit of changing the motherboard is that now the server is even more quieter than before! It’s all because I took more time to isolate the noisier case fans and to re-install them.

So I haven’t been able to blog for 2 weeks and *so* many things have happened. narf and blanca finally managed to get Windows XP running on a Mac! This is awesome news, especially after disappointing news a while back that Vista will not support EFI either. It seems they have managed to emulate a regular BIOS for the XP on Mac competition. Soon after the announcement, Leo Laporte did a great segment for MacBreak where they install XP on a Mac Mini and that’s the first place where I saw it actually running. A lot of people can now dual-boot their new Intel Macs and have a choice of XP or OSX (and even Linux!). I’ve never seen such a versatile machine! These are interesting times indeed.

Ruby on Rails

Posted in coding, computers, software on March 7th, 2006 by kerneljack

“Rails is the most well thought-out web development framework I’ve ever used. And that’s in a decade of doing web applications for a living. I’ve built my own frameworks, helped develop the Servlet API, and have created more than a few web servers from scratch. Nobody has done it like this before.” -James Duncan Davidson, Creator of Tomcat and Ant

I tried out the Ruby on Rails (RoR) framework this weekend and I’m quite impressed. I used Apple’s new tutorial for developers which explains how to install and quickly get up and running with a simple Accounts/Expenses webapp. I did encounter one minor glitch while following the install instructions because I was installing on my Debian Linux webserver, instead of an Apple machine. It seems the default ruby install no longer comes with the ruby-zlib library, but that was quickly fixed by following these instructions.

Rails applications all have a consistent directory structure, so you always start by running a command to generate the directory structure for you. This creates directories like app, config, doc, test, etc. The names are very familiar and easy to remember and they reinforce the purpose of the directory, i.e. the test directory is used to hold unit tests and functional tests for our application.

Automatic test creation makes testing an obvious and integral part of the development process, not an afterthought. It encourages you to think of a testing strategy upfront. This is perhaps the single most appreciated aspect of Rails development for me. You are supposed to test your code in other environments, but programmers often write code and if time permits, write tests. Rails tries to make it easy and painless (as much as possible) to test your code.

The next thing I learned about was the fact that Rails, like the Struts framework, tries to explicitly embed the notion of Models, Controllers, and Views within the development workflow. The concept of Actions, which are sent to Controllers in Rails is familiar to Struts. It is very easy to take a Rails URL (as shown in the example) and figure out how the server is going to parse and execute it.

Validation came next and I was suitably impressed. You simply need to add a few lines to the model, describing what each field should validate as, sort of like describing a type for a variable. Simply restart your server and voila, type rubbish in a field and Rails will highlight the field in red and ask you to re-enter it.

After all that, the tutorial shows you how to create a relationship between two models, much like in a relational database. The relation is that ‘one account can have many expenses’. This is accomplished in Rails by adding a ‘belongs to’ field in the Expense model (an expense belongs to an Account) and a ‘has many’ field to the Account model (an Account has many expenses). Simple as that.

Other things covered in the tutorial were:

  • adding business logic to total up the expenses for the account
  • using helpers to change the view slightly and show the total in red if we are over budget
  • writing simple unit tests and running them

Overall an excellent introductory tutorial which has left me wanting more. I will definitely go through the Rails website and find out more about RoR and what it can do.

I know this is a small toy example and I want to know how RoR handles in a real mission critical business app as those are the only kinds of apps developers write these days :-) RoR is easy to code for, but is it easy to maintain? After we get past the simplicity of the example app, how hard is it to write huge apps in it, and how long does it take a new developer to become familiar with and productive with a new codebase? These are all questions that I need answers to and I have the feeling that RoR won’t disappoint.