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.




Sunday, February 16, 2014

Weather

Here's a thing...

Julia Slingo from the Met Office says global warming will make extreme weather more likely in the future.

David Cameron says money is no object.

My suggestion is the government give everybody whose house is uninhabitable a million and a half quid (or whatever it costs to get an equivalent property and furnish it) or so to move to higher ground. There are plenty of brownfield sites that can be developed. I see them as I travel up the East Cost Mainline.

Generate the money by cutting the budget for the EA and the Met Office.

Surely, long-term that is a more effective and cheaper solution than shoring-up.

Anyone who doesn't want to move can stay where they are. They still get the cash but they need to use it to make provision to bale themselves out when it floods.

The problem is, I don't believe either Julia Slingo or David Cameron. And deep down neither do they.

Saturday, February 1, 2014

Good and bad, strong and weak, right and wrong.

I had an interesting discussion today. It started here:

https://twitter.com/andybolton/status/429626053720813568

It was an interesting debate. Unfortunately it did not end satisfactorily for either of us.

I may not have being clear. 140 characters is a horrible format for discussing something serious. I do not blame victims. I have taken Surrey police to task several times for their 'lock it or lose it' type campaigns. I do believe that is blaming the victim.

I don't think suggesting that the world can be a dangerous place too outrageous. Every day the facts bear this out.

Knowing the dangers exist, I don't think suggesting you take measures to improve your safety is too outrageous.

I don't think arguing one sex, age group, or any other demographic should not do whatever they can to keep themselves safe is too outrageous.

Anyway, that aside, and I can agree to differ without ill feelings, it got me thinking about right and wrong, good and bad, strong and weak.

Good and bad, like all extremes on a continuum are tricky things to argue with. It's the land of differential calculus which words are hopeless at dealing with. Good people can do bad things and bad people can do good things. For the sake of convenience let's take a concrete example and that doing violence to someone weaker than you is inherently bad. Can we all agree on that? It avoids any judgement and noble cause stuff that is subjective.

So we have a definition of bad. Let's say that good is not doing violence to those weaker than you.

No diktat or law will stop the strong preying on the weak. 'Twas ever thus. If threats of retribution worked we would all live in some kind of utopia where no one was nasty to anyone else. And perhaps religious belief of a vengeful god would have a use. Now here's the thing: How do good people stop bad people being bad?

{scroll down}










{and down}
















{and down}


















Become stronger than the bad are and fight back? But now you have become bad by the definition I set out. Which you agreed with, didn't you?

Funny old world ain't it?















Thursday, January 16, 2014

Raspberry Pi - Part 1

PiStories Part 1

Getting up and running

I finally succumbed and bought a raspberry pi. I went for the Maplin kit n34qg. On unboxing I realised I had probably spent more than I had to in order to get started. The box contains:

  • Pi Model B
  • Case
  • Wireless keyboard
  • Wifi dongle
  • 4port powered USB hub
In fact in my odds and ends drawer I had a wifi dongle that I used to use on an old laptop and an 8 port hub which would work better because the footprint is pretty well the same size and the pi itself and it sits on top of it with a bit of bluetack.

The keyboard's ok, if a bit small and I wouldn't want to use it for any serious typing. It will come into it's own if I ever get the Pi linked to the TV and want a remote.

In terms of booting up everything worked pretty well out of the box.

I am running on a Toshiba TV and a Benq monitor. The monitor only has a DVI port but a cheap adaptor from Amazon sorted that out.

The sound didn't work on the TV immediately I had to add the line hdmi_drive=2 to the /boot/config.txt and things started working.

The wifi utility is on the desktop and it connected to the home wifi no problem.

During the setup I realised that the keyboard wasn't really working for me so I plugged in an old usb keyboard I had. It detected it straight away. I had to remove the wireless kb dongle because it was causing some strange behaviour with missed keys and key repeats.

One of the first things you should do is fire up raspi-config. Before you use it the first time go to the last menu item and get the update. The other thing you might want to do set the gui to start automatically.

Remoting

I decided early on that the footprint of yet another keyboard on the desk was not practical so I decided that remote desktop was the way to go.

The recommended remote server for Raspian is TightVNCServer. The command line to get it is

sudo apt-get install tightvncserver

and the instruction to start it is just

tightvncserver

The first time you use the password file is not present so it prompts you for a password for read/write access and an optional one for read access only.

When it starts it tells you it has started but not which port it is listening on or the ip the pi is sitting on. The best way to find out the port is to have a look at the log file like this:



I haven't quite got to the bottom of the last two errors but it works ok anyway.

So from that you can see that we are running on port 5901. You can try and connect using the machine name and DNS. That was not working for me so I needed to get the ip address of the Pi like this:


You will see from that that the wired Ethernet (eth0) is not connected. The bit we are interested in is the wlan0 connector and there you can see the internet address that has been assigned by the router. I have a long lease on the addresses so I usually don't bother setting up a fixed IP on the local network.

Now I have all the information I need to connect. On the laptop I have to run windows for work. I am using the TightVNC client for windows. When you start it up it looks like this:


Not the double colon before the port number...that had me guessing for a while.

When you have a good connection you can enter your password:


And bingo you should be connected.

...Coming soon

  • Connecting with the remmina remote desktop in Linux
  • ssh remoting
  • My appraisal of Python
  • tkinter GUI programming

Wednesday, January 1, 2014

Akademic Shokalsky


If you are going to argue you need facts

There have been lots of words written about  These are just facts. I present them to you without comment:

  • There is a ship stranded in ice in the Antarctic. 
  • It is mid summer in the southern hemisphere.. 
  • The Guardian and the BBC are represented on board. 
  • Along with eco-activists and scientists. 
  • The mission was a scientific one.
  • They said so when they failed to raise public funds through croudsourcing site (they raised $1000)
  • There were two icebreakers trying to break through to them. 
  • One of the icebreakers seems to be stuck itself and the other one is trying to free it but making only around 10 metres an hour. It has about 3 nm to cover
  • The BBC has reported that a third icebreaker is on its way. The USCG has failed to confirm this.
  • The chief scientist on board is claiming the ice that is trapping them is caused by the warming of the Antarctic. 
  • The thermometers are showing a cooling trend.
  • The same chap runs a company that seeks to make money by carbon capture
  • His family are stockholders
  • 100 years ago the area they are in was navigable.
  • The BBC and guardian would like you to think it is a cruise ship, they are either not allowing or deleting comments that contradict their view of the world.

I am  not a scientist. But I hope I can think critically and I know where the facts are leading me. 

You can refute the facts above present new facts. I have evidence to back all of these. If you want to join the discussion make sure you bring some evidence to the party. 

The truth will come out when someone has to pay for it all. The finger pointing has started already. Don't hold your breath though maritime insurance claims can go on 20 years or more.

Update Noon 2nd January

  • The 52 passengers have been airlifted by Chinese Helicopter to Australian icebreaker.
  • The crew remains on board.

Tuesday, January 24, 2012

Wacom Bamboo Fun Pen and Touch CTH-470/S


Intro
I've been looking for a decent pen and tablet for a long time but kept putting it off as injustifiable. Well I've been doing some graphics in GIMP and decided that a pressure sensitive input device would be a good idea.

Following the review here in The Register I found the item on Amazon. I bought the tablet and wireless kit as a bundle but from different suppliers.


The pad works very differently in windows and Ubuntu as some of the features are only available in the installed software so I'll separate them out.

Features
The tablet works in two modes, Pen and Touch. When the pen is within about 5mm of the surface of the tablet touch mode is turned off. So when using the pen you need to get it in range before you hand hits the tablet.

Pen Input
In pen mode the positioning is absolute. That is the tablet is mapped to the screen and when the pen is detected the cursor moves to the corresponding place. Touching the point to the pad is a click so it takes a little while to learn to hover the pen just high enough to be detected but not accidentally drag it which will select everything you wander over. Lift it too high and you will be in touch mode and all sorts of things happen, mostly unexpected zooming.

The pen is  a bit chunky for me but I find myself using it more than the touch input.

There is a single rocker button that emulates the left and right mouse buttons. For me it is a little high and a little too sensitive. The right-click is far too high and if you are, for instance cutting and pasting using context menus you need to arrange your hand higher up the pen with your forefinger or thumb over the top of the rocker before you do the select. Adjusting your grip when you have got a selection marked usually means your pen goes out of range and you revert to touch mode. I'd prefer a finger and thumb button with a very positive action. I'd also like some contours in the pen so it located your fingers repeatably

When I am a bit braver I'll have a look at the config options and perhaps swap the button functions. The left click is in the correct place but superfluous.


Touch Input
In touch input the cursor reacts like it would with a mouse. Moving one finger on the tablet moves the mouse in that direction. Removing your finger and replacing it does not move the cursor back to the corresponding position but continues from where it left off. A tap is a mouse click and a double tap is a double click, nobody dies.

Multi-touch is the clever bit. Touching with a second finger is a right click. Pinching is zoom in and un-pinching is zoom out. Dragging two fingers up/down or left/right is scroll. Just as you'd expect on a touch screen phone or tablet.


Buttons
To the left of the tablet there are 4 buttons which can be configured. By default two are the left and right mouse button. I haven't found a use for them yet because the functions are available with both the pen and the touch interfaces. Although the buttons are quite big (2cm x 2cm approx) using them, for me, would mean looking away from the screen.

Wireless
I went for the wireless adaptor because I thought it would be good for remote controlling the laptop when it is being the media player attached to the TV. I haven't tried it yet but I can't see why it can't work and it is a bit handier than balancing a keyboard and mouse on your lap in the armchair. The wireless kit comprises a rechargeable battery, a wireless plug-in for the tablet and a miniscule usb adaptor for the pc. Unless I keep it plugged in it will be very easy to lose. Charging is via the USB connection which overrides the wireless when it is connected.

Ubuntu 11.04 (Natty Narwhal)
On Ubuntu Natty Narwhal (11.04) it took a little research and, unusually, a system restart. For Ubuntu techies here are the commands to set up the repository and get the drivers:

sudo add-apt-repository ppa:doctormo/wacom-plus
sudo apt-get update

sudo apt-get install wacom-dkms


When you have done that and restarted the tablet should just plug 'n' play.

The package installs a configuration application that allows properties to be set. I've only used it to set the pressure sensitivity.

For drawing in GIMP (my justification) it works fine. Putting a picture on the pad allows you to trace.

Some of the multi-touch is only just OK. Scrolling often unexpectedly zooms. To zoom you need to wiggle you fingers a bit to get the driver to recognise you are zooming and then finely adjust when it catches up.

I haven't tried the wireless adaptor on the Ubuntu box yet. The driver is supposed to support it.

Windows
Getting the tablet working on Windows Vista Ultimate on the laptop took a few minutes. The accompanying disk loaded up and the plug and play worked first time. Likewise the wireless adaptor.

The install disks adds a few applications as well as drivers.

Bamboo doc is an apps management widget that contains the tutorial and a couple of games and a portal to download more stuff. Sumo Image manipulation is pretty well what you'd expect with the usual tools and filters. There's also a handwriting recognition widget and onscreen keyboard which works pretty well. The other apps are pretty trivial, I've never felt the need to deface the Mona Lisa.

Verdict
It does what I wanted it to. I'm persevering with it as a complete replacement for the mouse. I'm still doing accidental gestures and selecting and moving text when I don't want to but I think I'm improving. The sensitivity




Monday, April 4, 2011

A few words about Mind Maps


Is it just me, or is there a lot of muddy and unconnected thinking going on? I'd like to advocate a technique that IMHO should be taught in all schools. I came across it way back (whichever year it was the Open University used K as the prefix for its intake and Use Your Head was on the telly.)

They are called Mind Maps and are a graphical way of organising your thoughts. And thoughts might be notes for a speech, outlines for a piece of writing or just getting some clarity about something that is complex or confusing. They are great for taking minutes of meetings because when you come back to them you can pick out the important bits from the other fluff and dross that you get from he-said-she-said minute style. For the more formal stuff they can be structured to form a RAID* log.

The big idea is that thoughts are organised as a number of linked one-liners. Breaking down problems to manageable chunks is one of five* key problem solving skills. The map should use colours, fancy text decoration, pictures, highlights. This brings out the key points (pros in green, cons in red for instance) and allows you to get a bit artistic.

It works because our creative thinking isn't organised into straight line lists. Ideas pop into our heads sponaneously and unexpectedly. The Mind Map allows these apparently random ideas to be recorded, organised, linked and expanded on.

Sounds great. But there are some disadvantages. They are quite difficult to share. I have written a complete spec for a document management system as a map but when I shared it with the customers it became clear that they would much prefer a linear document of 10 pages with all the noise words to a one side of A4 containing the same information.

There is also intentionally no temporal dimension. This is a bad and a good thing. Sometimes things must come in a given order. The good news is you can use numbering schemes, colours or text size to impose an order.

There is no right way to create a Mind Map but here are a few starters to get you going. A map always starts with a central topic which should be in the centre of your piece of paper. It's a good idea to start in landscape mode as maps tend to 'expand widthways.' From the main topic you can start mapping out the high level ideas. Sometimes you will feel that an idea may need to be a 'hub' with it's own spokes. If it does make sure it is less prominent than the main topic. Keep coming back to the main topic and working down the branches of the ideas and let new idea form themselves. Add them to the map. Repeat until done.

I think the best maps are done on paper but attending a meeting with my pencil case of crayons got some very funny looks. And maps created electronically are easier to rearrange and update. There are a few good bits of free software out there that allow the basic notetaking. My current favourite is FreeMind:
There are some omissions the most notable is the ability to do freehand drawings on the map. On the plus side it has a great html export facility which can create javascript webpages as bullet lists to allow branches to be expanded and collapsed.

There's a big list here:
Here are some books that I find useful. A good start is Use Your Head which accompanied the BBC TV program in the 80's. There are some second hand ones for as little as 1p + p&p. It covers lots of other stuff too, like remembering lists and how important revision is.

Use Your Head on Amazon
Mind Maps for Kids
The Mind Map Book on Amazon

So next time you need to organise your thoughts either for yourself or to communicate with others you could give it a try. When I have converted enough people I might be able to get some fun out of writing my specification and proposal documents ;)

BTW the map in the diagram above my first 15 minutes of thoughts for a workshop I am doing with a some local authorities who want a 'Portal'* but have no idea what it is. Personally I dislike the use of the word Portal but I suppose it has the benefit of being a bit shorter than transactional website....


Big hat-tip to Tony Buzan.


*I might get round to writing these up one day.