Category Archives: Programming

Hex your Mac

Want to edit binary files on your Mac? Hex Fiend looks neat.

Panic Coda

Panic Inc have just released their new Mac application, Coda. This came to our attention initially because we’re also developing something called CODA, albeit in a very different space. Pity, but there are lots of other codas out there (codi? codae?), so I guess there’s room for us all!

Anyway, I had a bit of a play with Panic’s app last night and it’s very cute. Panic are the guys who make Transmit, widely acknowledged as the best FTP app for the Mac, or, it is generally agreed, for any platform. So new products are watched with interest.

CODA is an editor for web developers. It’s not a Dreamweaver or anything like that, but for those (like me) who want to write HTML and CSS directly, it’s a very nice package. When you open a ‘site’ you get tabs for source editing, CSS editing, preview, and an SSH terminal, all with file access provided by their FTP/SFTP/Webdav engine.

Panic Coda

This sounds like the dreaded Swiss-Army-knife approach and could be a complex mess, but the creators have drawn it all together in a seamless and exceedingly elegant package, that I find rather tempting…

Here, and here are a couple of reviews if you want more details and some screenshots.

Tick-tock

SVG – scalable vector graphics – is an XML-based standard for storing images based on lines, curves and shapes (as opposed to photo-type pictures, which are arrays of pixels).

It’s been around for some time – I think I first experimented with it in around 1999 – but there were a very limited number of programs able to create or view SVG files. That’s changing, however, and SVG is gaining ground for a variety of reasons:

  • An SVG file can include Javascript, which can modify the graphical components to create an animation
  • It turns out to be quite a good format for delivery some types of graphics to devices like mobile phones
  • Firefox supports it – which means that a very large number of people are now able to see SVG images without installing any extra software

Martin sent me a link to this simple but very pleasing SVG animation by Tavmjong Bah, which you should be able to see if you’re using Firefox or similar browsers.

It assumes you’re on a large display, though, and if not, you might like this version, which I simply scaled down using the free Inkscape application. Note that the animation still runs, that it shows your timezone, and that if you were to scale it back up you’d get the full quality of the original.

Quicksort

Paul Bissex has a lovely example of the power of Python – a Quicksort implementation in 99 bytes.

>>> q=lambda s:s if len(s)<2 else q([x for x in s[1:]if x<s [0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]])
>>> print q([9,7,5,3,1,8,6,4,2])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

If you unwrap it to make it a bit more readable, it’s doing this:

q=lambda s:
   s if len(s)<2 
   else 
      q( [ x for x in s[1:] if x<s[0] ] )
     +[ s[0] ]
     +q( [ x for x in s[1:] if x>=s[0] ] )

Very nice! Needs Python 2.5. If you don’t know the Quicksort algorithm, here’s how it works.

Open Source database frontend

These days, most databases use a web browser as a frontend and something like Ruby on Rails or Django to link the web to the database. But if you’re on Linux and looking for a desktop equivalent to Microsoft Access, Knoda is a good place to start.

Keep It Simple, Stupid!

One of the great skills in Computer Science, and part of what makes it fun, is knowing when you need to create a highly sophisticated general-purpose algorithm and when you can get away with a much simpler one, perhaps by manipulating the problem domain so that you have an easier problem to solve.

This was part of the genius of the PalmPilot team: they realised that if you modified the letters people used for handwriting slightly, they became much easier to recognise, and so Graffiti was born. Actually, in this case, I suspect, the real credit goes to whomever it was who got the idea past the business and marketing guys. Can you imagine the conversation?

“We’ve got this really cool device, and all that the customers will need to do is learn a new alphabet before they can use it. It’s really not very different from the ABC they’ve been using from the age of four…” The VCs must have just loved that idea!

In computer vision, this simplification of the problem domain is particularly relevant because the algorithms can get very complicated very quickly, and complexity can require a lot more processing power and, often, result in less reliability in the real world.

So I was particularly impressed by the TAFFI (Thumb And ForeFinger Interface) developed by Andy Wilson at Microsoft Research in Redmond. He’s come up with a great way to avoid the need for complex hand-tracking algorithms. Have a look:

You can read more about it in his paper from the UIST conference: Robust Computer Vision-Based Detection of Pinching for One and Two-Handed Gesture Input.

The dangers of clever programming

There’s often a temptation for coders to come up with the cleverest solution to a problem, one which accomplishes the greatest amount in the fewest lines of code, for example, or takes advantage of the most obscure features of the programming language. Such solutions may be intellectually very satisfying, but are often not ideal for other reasons. I really like this quote from Brian Kernighan, which I heard for the first time last week:

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

Samba and the French Cafe Technique

To the London LinuxWorld today, which seemed miniscule after the San Francisco one only a few weeks ago. I was surprised at just how small it was, given the importance of Linux to the UK, and the importance of the UK to Linux, but it was still interesting.

On the train I was listening to podcasts, as I often do now while travelling. That has been the biggest impact of the iPod for me personally: the fact that I no longer consider waiting time and travelling time as wasted time, even if I’m driving or walking and couldn’t easily read a book. I spend much more time on my iPod listening to speech than I do to music…

Anyway, one of the interviews I listened to was with Jeremy Allison, a key developer of Samba. For those who don’t know, Samba is free software which shares files and printers over a network using Microsoft’s protocols, meaning that Windows machines can communicate with Macs, Linux & Unix boxes and a variety of other devices that use Samba under the hood. It’s a very important piece of software, and I’ve been using it for about 11 years.

Of course, Microsoft don’t publish the details of their protocols. If they had their way, then Windows machines would only be able to talk to other Windows machines. So Andrew Tridgell, the creator of Samba, has to work out what they’re doing through a variety of cunning techniques. He wrote a nice article about how he does it. I particularly liked his description of what he calls ‘The French Cafe Technique’:

Imagine you wanted to learn French, and there were no books, courses etc
available to teach you. You might decide to learn by flying to France and
sitting in a French Cafe and just listening to the conversations around you.
You take copious notes on what the customers say to the waiter and what food
arrives. That way you eventually learn the words for “bread”, “coffee”
etc.

We use the same technique to learn about protocol additions that Microsoft
makes. We use a network sniffer to listen in on conversations between
Microsoft clients and servers and over time we learn the “words” for “file
size”, “datestamp” as we observe what is sent for each query.

Now one problem with the “French Cafe” technique is that you can only learn
words that the customers use. What if you want to learn other words? Say for
example you want to learn to swear in French? You would try ordering something
at the cafe, then stepping on the waiters toe or poking him in the eye when he
gives you your order. As you are being kicked out you take copious notes on
the words he uses.

The equivalent of “swear words” in a network protocol are “error packets”.
When implementing Samba we need to know how to respond to error conditions. To
work this out we write a program that deliberately accesses a file that
doesn’t exist, or uses a buffer that is too small or accesses a file we don’t
own. Then we watch what error code is returned for each condition, and take
notes.

Making out?

Warning – geeky post ahead… I’ve been doing some coding this week…

Anyone who’s done any programming, at least if it’s outside the limited confines of an integrated development environment, will have come across the make utility, which was developed nearly 30 years ago at Bell Labs.

Make lets you list which bits of a program depend on which other bits, so that when you make a change, say, to one of your source code files, you can just type ‘make’ and the bits which need to be updated as a result all get rebuilt automatically.

People use it for other things, too; many years ago I had to produce Rose’s PhD thesis through a rather complicated process which started with floppies from a dedicated Panasonic wordprocessor, ran through a C program I wrote to decode the disk format, a Perl script to convert the files to LaTeX, and then Latex itself and finally dvi2ps to get the Postscript output for printing! Each stage generated different files, updated tables of contents etc, and when she fixed a typo on her Panasonic, I needed to ensure that it was propagated through the entire pipeline and made it into print. Make was good for that.

But anyone who’s built a project of any size will also know that make is far from perfect. It really hasn’t evolved much in its thirty years and the syntax, while elegant for very small projects, becomes unintelligible for large ones.

Here’s a small segment of a Makefile I was writing last week:

define PROGRAM_template
$(1): $$($(patsubst %,%_OBJS, $(notdir $(1)))) \\
        $$($(patsubst %,%_LIBS, $(notdir $(1))))
     g++ -o  $$@ $$^ $$($(patsubst %,%_LDFLAGS, $(notdir $(1)))) $(LDFLAGS)
all:: $(1)
$(foreach obj, $($(notdir $(1)_OBJS)), \\
     $(eval $(call OBJECT_template, $(obj),$(notdir $(1)) )) )
endef

# Build every program defined in PROGS
$(foreach prog,$(PROGS),$(eval $(call PROGRAM_template, $(prog))))

Don’t bother trying to understand this. The point is that it’s pretty arcane stuff and I wasn’t trying to do anything too sophisticated here.

If you’ve written Makefiles before you probably know roughly what’s going on, but do you know exactly what’s going on? Would you have got the right number of $ signs in the right places? Could you say why $$^ is in here and not $$< ? Why I have to call things and then eval them? Then try and imagine what it’s like for somebody seeing a Makefile for the first time!

And here’s the worst bit: for this to work at all, the gap at the beginning of the ‘g++’ line must be a tab, not spaces. So simple code that looks perfectly correct may not actually work when you try to run it. It’s a nightmare.

So last week I decided that using make in the 21st century was probably ridiculous, and it was time to search for alternatives.

Continue Reading

sshput

Warning – geeky post

I often configure SSH so that I can log in from one machine to another without typing a password. Those who have done it, though, will know that this takes a few steps and it’s easy to make mistakes.

I wrote a little script to help, and found I used it rather often. So with due humility I offer sshput to the world in the hope that others might also find it useful.

Why Johnny can’t code

David Brin laments the fact that modern computers are no good for kids wanting to learn to program, and explains why he bought his son Ben a $25 Commodore 64 from eBay.

Network-boot a Parallels Workstation client

Warning: Geeky stuff ahead!

The Parallels Workstation virtual machine software for the Intel Macs has a BIOS which doesn’t support network booting.

I wanted to simulate an LTSP workstation, which would boot over the network from our Linux server. Here’s how I did it:

On the ThinStation project site, I found a link to a Universal Network Boot package – a zip file containing disk images for floppy, CD and HD.

From this I grabbed the ISO CD-ROM image eb-net.iso and set up my virtual machine to use this as the CD instead of the physical CD drive. I also configured it to boot from CD first, and, for my purposes, I removed the hard disk from the config as well.

Sure enough, it boots up just fine and I have an LTSP terminal in a window. Much easier for experimentation than rebooting my embedded device all the time.

© Copyright Quentin Stafford-Fraser