Sunday, March 23, 2014

Python - the good the bad and the fugly

Background

I'm a fairly seasoned programmer. I cut my teeth on Algol 69, Oric Basic, Sinclair Basic, BBC Basic, Hand assembled 6502 code, MASM, QuickBasic C, C++ and Pascal so I think I know a good programming language when I see one.

Microsoft's .net languages are fairly well thought through but are far more complex than they need to be. I've reviewed a lot of code and had WTF moments where the programmer seems to have been more concerned with obfuscation of his intent than actually making something work.

And now I have come to Python. I've looked at it a couple of times as a quick and dirty scripting language to get things done. My initial reaction was that it was a total balls-up of a language but now I have my raspberry pi looking at it a little closer and I'm starting to find some redeeming features.

Libraries

First plus point is the libraries. They are mainly stable and and pretty useful and seem to work across platforms. They are pretty comprehensive and you can get things done with very little code. This weekend I wanted to get a website into a string variable so I could pick it apart. The following three lines did the trick:

#get a web page in Python
import urllib2
response=urllib2.urlopen(webaddress)
html=response.read()

Easy-peasy but urlopen? What happened to verb-noun naming? I had a cursory glance at some online documentation and and automatically translated it to openurl in my head. If I'd have been the code reviewer I'd have sent that back.

The pygame library for android does on implement mixer to play sound files and the mixer in the android library doesn't want to work. I need to try some tinkering which may well involve digging in to the library source.

I have only just got started with the TKinter GUI library, I've managed to put a label and a button on a screen and trap the button event. I feel like I'd achieved something getting that far.  I'm reserving judgement.

IDEs

The IDEs are not bad. On ubuntu I am using Stani's Python Editor (SPE) which allows running a debugging in in the IDE so it's a bit more than just an editor. I'd quite like to use eclipse but it is overweight and a bit picky about whether it wants to run the Python add-in or not on my setup.

I haven't got around to trying Stani's on Windows yet but most of what I do with Python is on Linux. I have IDLE, which I don't like as much.

On my Samsung Note 2 I have QPython which has an editor and interpretor. Coding on it is not bad using my little bluetooth keyboard.

Pretty code

Python forces you to use indents to mark blocks. On the upside this means that your code has to look nice. On the other hand, for me, it is less than intuitive. I have worked with either text block markers or curly braces. It's going to take me a little while to get used to indents.

The other side effect, whether intended or not is, is that, because indenting is  a pain it does make you more inclined to break up your code.

And there are rules about mixing spaces and tabs.  As you can't see them on the screen, tracking them down is a bit of a mission.

Lightweight

The interpreter is very small. Good when you are looking at the raspberry pi or a phone. And, while some libraries come as standard, most can be added as needed. 

Target Audience

If the target audience is script kiddies who can quickly knock out a few lines to get things done, it's fine. It's a bit more intuitive than Bash or Powershell I suppose.

If it is for learners I'd have hoped for better support. The base documentation is pretty well limited to documenting the primitives. A bit like someone learning C a copy of Kernighan and Ritchie. It's fine if you have a background in coding but it is a bit daunting. Luckily there is a community out there to working stuff out.