[]RSS

About Archives Artwork Comic Contact Philosophy Projects Tags

Hack of the day: del.icio.us bashing

[Comment]

December 8th, 2007 in Micro Blog

I’ve been using del.icio.us and their slick Firefox plugin for over a year now, and they’re great. I’ve found the tools especially handy for tagging sites that I want to do something about1, or sites that make good reference material2. Unfortunately, the Del.icio.us pages are slow to load and a bit difficult to read.

Luckily, Del.icio.us provides several ways to get at your data, including RSS, JSON, a rich HTTP-API, and plain-old HTML. Today’s hack builds a simple set of cached, static pages using wget and the Delicious HTML interface.

Here’s a simple form of my caching script:

TAGS="todo readme postme watchme eatme buyme"

for tag in $TAGS; do

    mkdir -p $tag   
    wget -O$tag/$TAGS_OUT \
    "http://del.icio.us/html/madmaxx/$tag?count=5000&rssbutton=no&tags=no"

done

The full version of the script also generates a summary of the tags, and copies template pages to each of the tag folders for styling. The simple, finished site does most of what I wanted it to, now I only need to spend some more time styling the lists so they’re easier to read.

Total hack time: 15 minutes.

  1. With tags like readme, todo, postme
  2. Like sites with great layouts or inspiring artwork

Hack of the day: serial programming on Linux versus Windows

[Comment]

October 19th, 2007 in Programming

screenshotI borrowed a serial UPC scanner from work today for some Friday night hacking. It’s a ReaderWare LaserChamp/Flic Barcode reader re-branded under the SerialIO name. It came with a standard serial cable and didn’t have a manual.

First, I booted to my partition to see the raw data from the scanner.

$ cat /dev/ttyS0

No dice. I did some searching and found that the scanner likely ran at 4800 baud.

$ setserial /dev/ttyS0 baud_base 4800
$ cat /dev/ttyS0

^B043396143234

Ah, some data. I removed the control characters and had myself a UPC symbol. I wrote a script and scanned a bunch of books, CDs, and other crap sitting on my desk. I also found a manual for the scanner, which had some settings symbols1.

I booted to my XP partition and started . I wrote a Perl script to mimic what I did on Linux, except using the Win32::Serial module. I fired up CPAN to grab the library, but CPAN was down. I switched to ActiveState’s Perl distribution and grabbed their Win32:: PPM. I finished the script and tested it. It worked.

A while later I checked CPAN again, grabbing the Win32:: module using Cygwin’s Perl. It didn’t build. This seems typical for Cygwin and CPAN/Perl modules2. I moved on.

I switched to C# to see how it compared to Perl and the Linux/Bash hack. I whipped together a simple application3 that listened for data on a serial port, cleaned it up, and copied it to the clipboard. It worked4.

I also attempted to hook the C# code up to the Amazon API (to get ASINs), but found the networking libraries more complicated than I had time for5.

My favorite approach? Hands down, I liked the Bash script6 best. It was one line of code, including cleaning it up with sed. It’s not something I can ship, but it’s the way I’ll scan all of my books/CDs/DVDs.

The Perl Win32::Serial hack was longer (a dozen lines), but it also worked. It was no better than the 1-line Bash script, so I tossed it.

The C# hack consisted of a single form and its supporting class (a few dozen lines of code). It worked, and is easy to distribute. I’ll use this if I’m stuck doing something in Windows.

Total time for all 3 hacks? 3 hours and 25 minutes7.

  1. You print the page and scan the symbols to change the scanner’s settings
  2. Most of the tests failed for the Win32::Serial module
  3. And the .NET build of it
  4. C#’s serial code was nicer than Perl’s, but not as nice as the devfs hack
  5. Perl’s networking is much nicer than C#’s
  6. Bash and devfs’s /dev/ttyS0
  7. My wife and kids were out for the evening

Another good Bash FAQ

[Comment]

August 30th, 2007 in Links

A good Bash FAQ. Always fun to learn something new about your shell.

HOWTO iterate over command line parameters in Bash

[Comment]

August 9th, 2007 in Micro Blog

This one took me a few minutes to figure out, as it uses two Bash constructs I rarely touch ($# and ${!}). The snip below iterates the command line and rebuilds it with the parameters converted to absolute Windows paths. It’s handy for calling Windows editors from a Cygwin/Bash prompt (when not using Vim of course).

for1
do
    p="$p `cygpath -wa ${!at}`"
done
  1. at=1; at <= $#; at++

What’s in your Bash history?

[Comment]

January 5th, 2007 in Links

Some tips on improving how you use your Bash history (and some other random bash tricks).

Working more productively with bash 2.x/3.x

[Comment]

November 28th, 2005 in Links

Working more productively with bash 2.x/3.x.

Regex of the day: Optional HTML tags

[Comment]

November 11th, 2005 in Perl. Weblog

It’s one of those -laden days, and I’m really starting to more complicated expressions:

^(?:\s+|)<(\w+)(?:\s+|)((?:.*?|))>(.*?)(?:<\/(.*?)>|)(?:\s+|)$

This expression parses a line that contains tags based on the following logic, expecting that:

  1. There will be a start tag near the beginning of the line, possibly padded on the left with spaces that are ignored
  2. The opening tag may contain some HTML parameters
  3. There may be a closing tag on the line
  4. There may be spaces on the right of the closing tag that will be ignored

The expression will parse the following example into 4 parts:

<h1 id="test">This is a test</h1>
  1. h1
  2. id=”test”
  3. This is a test
  4. h1

Learning regex to the point of being able to write complex expressions has taken a couple of years, but has been well worth the effort. To define the same parsing logic in C or C++ (using standard mechanisms) would take 20-30 minutes, and would occupy a page of code. You just have to remember that a regex is a small script, and that it should be tested (and documented) like one.

Regex is like a lot of little languages too (like SQL, bash, m4). It’s terribly useful, succinct, and worth having in your toolkit. It’s not something to hide in layers of abstraction either, rather it’s something that deserves use alongside your ‘real’ tools. I find that developers are in the habit of hiding (or hiding from) little languages, something that results in the too-many-elbows syndrome: insulating yourself from the real power of your tools, making things more complicated in the process.

Simple, in the end, is in the knowledge of the beholder. If you understand regex, code that contains it can be simpler.