I’m learning Python

May 31st, 2008

Google opened up App Engine to all developers this week, so I decided it was an ideal time to check out Python.

This was something I’ve avoided until now for a few reasons; primarily because I didn’t have the time to learn a new language, but also because I thought I wouldn’t like the semantics and philosophy of Python. I’m certainly more enamoured with the language now, but there still some ideas I’m not totally sold on just yet.

Semantics

Python has a really clean coding style (it’s even tidier than Ruby). For example, a simple class definition looks like this:

class MyClass:
	aString = ''	# public variable
	__anInteger = 0	# private variable

	def __init(self):	# constructor(-ish) method
		pass

	def Hello(self):	# class method
		return 'Hello, World!'

As you can see, Python uses indentation as opposed to braces in C-style languages, or start/end blocks in Ruby. I wasn’t keen on this before, but after playing around with it for a week or so I think I like it. I still don’t think it makes the code that much simpler to read, but it certainly makes coding a lot faster.

Philosophy

There does seem to be one overriding philosophy evident in Python’s design: there are few rules, but many conventions. For example, private variables and class methods are not really private, just obscured. I don’t understand why this is the case: if a method is private, it’s private for a reason, there should be no cause to allow a developer to have access to it. Annoyingly, Python also doesn’t allow for protected members. Python has special __init__ methods instead of constructors (for all intents and purposes, they operate the same way) and no destructors. Otherwise, it behaves pretty much like you’d expect an interpreted, modern programming language to operate (aside from, of course, the aforementioned private members idiocy).

The Standard Library

So, Python as a language isn’t really ground-breaking. What sets it apart from the rest is it’s fantastic standard library (and also it’s adaptability; Python can be shaped to suit almost any task). The developers like to say that Python ships with ‘batteries included’; it’s an apt analogy. I won’t go into detail regarding the standard library (because it’s simply too exhausting to discuss it thoroughly), but if you want to know more it’s very well documented at python.org.

Learning Python

Learning Python is exceptionally easy to learn if you’ve ever programmed before., it really is a testament to the language’s simplicity. There are hundreds of tutorials on the web and elsewhere that will get you started, but I’d point you towards the free (cc licensed) book available diveintopython.org. You can download it or view it online, or even order a printed copy from Apress if you want to support the author.

Half of Ireland is computer illiterate

May 17th, 2008
Tags: ,
Uncategorized

Silicon Republic has an article about the widespread computer illiteracy among the older Irish generations.

Nearly half the country has virtually no computer skills whatsoever, citing every day examples of parents asking their offspring to conduct routine tasks on the Internet, such as booking flights and hotels and checking weather reports.
“The divide between technology savvy individuals and those in the other 50pc of the population is huge,” commented Liam McMahon, managing director, New Horizons Ireland. “The reasons are manifold as to why some people are being left out of the technology age; lack of education, lack of previous interest and lack of time are just a few.

Read the full article here.

Adopting an MVC architecture in PHP without a framework

August 3rd, 2007

The surprising success of Rails has given rise to a huge number of PHP Frameworks that have recently seen a surge in popularity.

One of the much-lauded features in Ruby, and it’s PHP clones, is the adoption of an MVC Architectural pattern, which, simply put, seeks to separate presentation from logic from data.

Sounds great doesn’t it? The only problem is, using a PHP framework incurs a huge overhead for your application. Of course, all the frameworks in question offer much more than an easy way so structure your application, but if this is all you want you could be left with a huge library of classes that you will never use; hardly an ideal situation.

This is a dilemma in which I found myself recently. I was developing an application for a group of musicians around my area. I needed it to be speedy and reliable, so I was reluctant to use third-party code; no matter how acclaimed. But I also wanted to use MVC patterns, along with clean urls. As I was adamant about knowing exactly what was going on in my app, and am familiar with how most frameworks implement MVC, i decided to code a simple MVC system that I could build upon later.

Here’s the basics of how it works:

  • .htaccess file: A rewrite rule is created to route all request (bar resource requests like .css files, .js files, images) to index.php, which handles all requests for the application; this is known as the bootstrapper.
  • Bootstapper: The bootstrapper (index.php), is where all requests from the client are routed. This script simply establishes environmental variables, and runs the FrontController.
  • FrontController: The FrontController is responsible for parsing the URI and forwarding requests to controllers and actions. The first piece of the URI (http://example.com/first) is mapped to a controller, the second (http://example.com/first/second) to an action, and all other elements are passed as parameters (http://example.com/first/second/parameter1/parameter2/parameter3). Once the FrontController has figured out what the URI should be pointing to, it runs the required controller and action. Note: if an action is not called, an action called Default, which is in every controller, is called.
  • Controllers and Actions: The controller, which is always in the format ‘Controllername’Controller.php, is a class that is instantiated by the FrontController when called by the browser. Actions are functions contained within this class. If a user visits the url http://example.com/news, the controller NewsController.php is called, and the Default action is run. If a user visits http://example.com/page/about, the controller PageController.php is called, and the function About is run. If the page http://example.com/user/view/conor is requested, the Controller UserController.php is instantiated, the function View is called, and ‘conor’ is passed as a variable.
  • The Model: The model always refers to the data and the code that manipulates it. I implemented a simple wrapper class for mySQL and that was it.
  • The View: The view is the presentation: the html, css, images, etc. Not wanting to completely reinvent the wheel, I just created another wrapper class that inherits Smarty. Although my aim with this application was efficiency, I didn’t mind using Smarty given its fantastic feature set like compiling and caching. I was already well-versed in its unusual syntax so there was no learning curve involved.

And thats it. This is a basic outline of how my system works; it is a near carbon-copy of similar systems like Ruby and the Zend Framework. I’m planning on writing up a simple tutorial for this; mainly so I have a reference to go back to in the likely event that I forget how I did it. In the meantime, you can see this in action here (Note: this site is nowhere near complete, so ignore everything except how the URLs work…).

Irish Times: Alarm as brightest students reject computing courses

July 19th, 2007

http://www.flickr.com/photos/conormulligan/851978627/

I saw this article in the Irish Times today (19th July 2007). It outlines the sharp decline in third level applications to computer science related courses among the highest acheiving Leaving Cert scorers (down to 0.8% from 6% in 2000).

This is course alarming for a great many people; but not me. In a couple of years when I’m finished University, it’ll be me and the few other comp-sci graduates taking our pick of the best jobs in the industry, he he he. Or we could establish a startup, do a few months of Ajax-heavy work, watch the users roll in and get aquired by Google…

I’ve included a scan of the article so you can check it out yourselves. Click the image for some embiggened Flickrised goodness.