29 March 2009

Reality

When art critics get together they talk about Form and Structure and Meaning. When artists get together they talk about where you can buy cheap turpentine. -- Picasso

24 February 2009

A Full Web Service with HTTP caching in 7 lines

Two lovely gems, sinatra and rack-cache. Sinatra is pretty easy web-service-creation, and Rack::Cache is pretty easy http caching. Together? Jubilation.


require 'rubygems'
require 'sinatra'
require 'rack/cache'

use Rack::Cache

get('/quadruple/:n') {
sleep 1
response.headers['Cache-Control'] = 'max-age=1000000'
(params[:n].to_i * 4).to_s
}

and then
ruby sinatra-add.rb -e production
and you're done.

There are, of course, many other fiddly bits to configure Rack::Cache with, like use Rack::Cache, :entitystore => 'file:/tmp/' if you don't want to keep it all in a hash in memory, and :verbose => false if you don't want that in your logs, but that's basically it.

It's pretty amazing: that's a real live web service, with HTTP caching, in 7 gentle lines. (I don't count the closing brace, nor the sleep 1, which is purely for effect.)

Anyone know an easier way in any other language? Either in number of lines, or in directness of code?

Also, how bad would it be to authbind that to port 80, and just let it go open to the world?



Update: It's interesting to me for the same reason PHP is interesting, both as a social commentary on the bits of plumbing we've agreed upon as useful, and as an aid to wrapping an HTTP interface around some Ada code with its own homegrown database.

24 November 2007

Anti-Features?, or, Never attribute to malice what can be explained by math.

Apparently, if you buy a cheap camera, and you can't save an uncompressed picture, that's treacherous computing, that's an anti-feature.

It's not. The camera won't stop working if you correctly patch in a different chip set.

And assuming the worst, assuming that the chips are exactly the same in an expensive camera, it still doesn't matter. The native resolution is still small on the cheap camera--that's mostly what makes it cheap--so you'll be better off buying the high-resolution expensive camera.

Of course, if you really want a camera that you can program, you might like an XO that drives a Roomba.

06 June 2007

cheap illustrations.

I'm a cheap nerd, so I printed myself an enormous wall decoration last August 2006: it's Pi, in a textual fractal. (This is it to two levels, and the one in my room is to five.) 3'x5', $10 from Kinko's. Nice conversation piece.

I've been printing more things recently, I've been thinking about combining text and images, and an author of two books on classical themes bemoaned the expensiveness of art in an email today. (Illustrating an book on Aesop's Fables, specifically.)

Most art schools have job boards; the artists can be of varying qualities, but sketches in pen are quick to do, and hard to do badly. (Lots of approximate lines.) Twenty seconds a sketch, which is a not-too-fast speed, scan one as you're doing the next one, and you've got a hundred in an hour.

Has anyone used Flickr for (cc)by-sa images? I wonder if that's cheaper.

21 May 2007

Amazingly bad APIs?

There are some amazingly bad APIs in Java, so Paul Buchheit says. The best API is system(), or backticks.

The end result is basically an ImageMagick conversion (kudos to a useful incantation for the less than sign):

mogrify -geometry "220x133>" -antialias -quality 90 /tmp/c.jpg /tmp/c-thumb.jpg

You wouldn't write an encyclopedia in limericks (though it's been done before), so there's no need to use a general purpose programming language to manipulate images. Small Pieces Loosely Joined is pretty popular for Unix, CGI/REST, Erlang, Messenger RNA, etc.

02 May 2007

microsoft knows drm.

I've been hearing a lot about this string of hexadecimal numbers. It starts with 09, ends with c0, and I think it has f9 and 88 somewhere inside.

Let's ask Microsoft search what it is!

require('open-uri') && puts(open('http://search.msn.com/results.aspx?q=09+f9+88+c0') {|f| f.read}.downcase.gsub(/<[^>]+>/,'').tr('^0-9a-f','').scan(/09.+?c0/).inject(Hash.new(0)) {|h,nu| h[nu]+=1;h}.sort_by {|str,freq| freq}.last.first)

Whew!

For non-Rubyists:
require we can open urls like files, and put this string: open the msn search page for all pages that have 09, f9, 88, and c0, read it in one gulp into lowercase, regexp out all html tags, translate out any character that's not a hex digit, scan for all substrings that start with 09 and end with c0, make a histogram* of the array, sort by most popular, and take the most popular string.
(* Inject a hash table through the array of scanned substrings; the strings are the keys, the frequencies are the values; add one to the value every time you see any string, starting at zero.)

Remember folks, this is Microsoft's suggested answer to the 128-bit programming challenge
posed earlier today, so like love, and Cambridge weather, it's just temporary.