Random Etc. Notes to self. Work, play, and the rest.

Posts Tagged ‘Programming’

ArtNano (notes on Wordpress as a CMS)

I recently finished helping out on the back-end of the NISE Network's ArtNano site for San Francisco's Exploratorium. Working on a 'straight-up' website is a rare departure for us at Stamen, but we're big fans of the Exploratorium and were delighted at the opportunity to work with them.

ArtNano: New Approaches for Visualizing the Nanoscale

The ArtNano site features contributions from several artists tasked with exploring the nanoscale. My favourites are Semiconductor's gorgeously glitchy 200 Nanowebbers video, Scott Snibbe's multi-scale Three Drops installation and Santiago Ortiz's Time Spiral. Also featured are artworks by Eric Heller, Victoria Vesna and Stephanie Maxwell.

If you're interested in that sort of thing, read the full version of this post for some notes on using the Wordpress blog platform as a CMS for a small website project like ArtNano.

(more...)

Unintuitive E4X Gotcha in Actionscript 3

This is the second in a series of posts about actionscript 3 that I announced earlier, and my last for today. Here's the feed of posts tagged as3.

Testing for the existence of a property on an Object is simple in actionscript 3:

 
 
var object:Object = { sub: "sub" };
 
trace("testing object properties");
 
if (object.sub) {
	trace("CORRECT: object has a sub element");
}
else {
	trace("WRONG: object has no sub element");
}
 
if (object.nosub) {
	trace("WRONG: object has a nosub element");
}
else {
	trace("CORRECT: object has no nosub element");
}
 

This yields the correct output:

testing object properties
CORRECT: object has a sub element
CORRECT: object has no nosub element

However, the same thing isn't as intuitive with XML and E4X:

 
 
var xml:XML = <xml><sub>sub</sub></xml>;
 
trace("testing xml element");
 
// don't do this, it will give false positives!
if (xml.sub) {
	trace("CORRECT: xml has a sub element");
}
else {
	trace("WRONG: xml has no sub element");
}
 
// don't do this, it will give false positives!
if (xml.nosub) {
	trace("WRONG: xml has a nosub element");
}
else {
	trace("CORRECT: xml has no nosub element");
}
 

This gives incorrect output:

testing xml element
CORRECT: xml has a sub element
WRONG: xml has a nosub element

The way to test reliably for the existence of xml elements is to check the length() method on the element:

 
 
trace("testing xml length()");
 
// this works to test for the existence of an element
if (xml.sub.length()) {
	trace("CORRECT: xml has a sub element");
}
else {
	trace("WRONG: xml has no sub element");
}
 
// this also works to test for the non-existence of an element
if (xml.nosub.length()) {
	trace("WRONG: xml has a nosub element");
}
else {
	trace("CORRECT: xml has no nosub element");
}
 

This yields the correct output:

testing xml length()
CORRECT: xml has a sub element
CORRECT: xml has no nosub element

Hey, it's boring but true!

Optimising Actionscript 3

This is the first in a series of posts about actionscript 3 that I announced earlier. Here's the feed of posts tagged as3.

Even with the great speed improvements that actionscript 3 and Flash 9 offer over actionscript 2 and Flash 8, I've occasionally had the need to revisit a project and look for places to optimise.

Miscellaneous Tips

Here's a list of suggestions I recently made to Gabe, who has his own actionscripting notes, as he was revisiting an old project looking to reduce CPU usage.

Tweaking the framerate

Simply dropping the framerate of an application might work as far as CPU usage goes too.

(Be careful with this though - if you're watching exclusively watching the CPU meters, and not the app, you might be optimising for the wrong audience!)

You can do that with the SWF metadata tag above your main app class in Flex Builder. The main bit is [SWF(frameRate='15')] but check the help file as you might have background colours and width/height in there already.

Memoization

You probably won't need it, but the trick used in Digg Arc (for calculating arc points before redrawing them) was to use what's called memoization — it helps with space-time trade-offs.

The idea is to remember the results of calling a function with certain arguments and only recalculate when the arguments change.

e.g. You might have a function that you're calling it a lot with the same values e.g. calculatePoints(1,2,3,4) that looks a bit like this:

 
 
function calculatePoints(a,b,c,d):Array
{
  var points:Array = [];
  /*
    lots of heavy math with a,b,c,d and points
  */
  return points;
}
 

Instead of calculating every possible variation, you can create a hash of the common results:

 
private var results:Object = {};
 
function calculatePoints(a,b,c,d):Array
{
  var key:String = [a, b, c, d].join();
  var points:Array = results[key] as Array;
  if (points) {
    return points;
  }
  else {
    points = [];
  }
  /*
    lots of heavy math with a,b,c,d and points
  */
  results[key] = points;
  return points;
}
 

Then if calculatePoints is called again with 1,2,3,4 you grab the answer from your results Object and return that instead of calculating it again. You probably want to set a limit on the number of things you cache though, so don't use this technique without thinking about that, and only consider this if your performance bottleneck is definitely one maths-heavy function!

Caveat Optimisor!

The only way to be sure about optimisations is to time things, manually or in your development environment, and bear in mind that even to experienced coders some performance issues can be unintuitive.

A quick (less certain) note on using Lucene in Processing

In the spirit of continuing our impromptu database week on Processing Blogs (my post, toxi's post, then Florian Jennet updating the sql library), I thought I'd post another quick example using Lucene.

Last week Ryan and I needed a reliable way to search inside a data set we were working with. I had previously tried and failed to write my own useful search routine for the same data, so I wanted to take a look at Lucene instead. (This week, I might have used SQLite, but I hadn't tried it last week!).

Lucene isn't a relational database like MySQL or SQLite, although it has a few similarities with the way most database engines speed up queries using indexes. That's because Lucene is "just" the indexing part. You tell Lucene about your data, one part at a time, and then construct queries to ask it which parts of your data match the query. The key thing is that you keep hold of your data yourself and structure it any way you like, Lucene keeps its own representation of the data for searching. Because of this it can index much more data than you can store in memory.

Anyway, I put up an simple example Lucene applet here that indexes the text of Time Machine by H.G. Wells from Project Gutenberg and lets you type queries against it. The text itself and the Lucene index it builds are both quite small (tens of KB, compressed), but the applet is around 750KB. This is because Lucene's core jar file is about 500KB so it's more suited to standalone projects and applications.

The code is kind of documented, but Lucene was really too much for me to understand properly in just one day. Nevertheless, again, I hope people find it useful!

A Quick Note on using SQLite in Processing

SQLite is a great standalone SQL database engine - not ideal for every situation (particularly large websites), but more than good enough to have already made its way into desktop projects from Apple, and more recently Adobe and Google.

My colleague Mike recently used it as a way to distribute data from a 511.org transit website scraping project he's been doing. I wanted to see if I could download his data (gathered and processed using python) and access it using Processing.

Web searches for SQLite and Java (java wrapper sqlite, etc.) turn up lots of matches, including this promising tutorial from Tim Anderson, but sadly the most prominent matches didn't work for me. It's a real pain to get up and running and installed in a reliable way, mainly due to the need to compile SQLite natively for each platform and then talk to the native code using Java.

Enter this amazing project, a JDBC library for SQLite written in pure java that uses NestedVM to compile the C code for SQLite into something any Java VM can use. It's frightening to think about the amount of misdirection, abstraction and interpretation going on here, but it worked first time and plenty fast enough for my purposes.

Take a look after the jump for the code I ended up with. I hope people find it useful:

(more...)

Processing 0121 is ready

A new version of Processing is available now.

If you haven't looked at Processing for a while, it's definitely worth picking up again.  It's maturing very nicely - this week I've been using it at work, and the ability to create a single app with full-screen OpenGL graphics, PDF and movie output (thanks to Dan Shiffman's movie maker library) is absolutely great.

AI library for Processing

Over on the Processing.org forum, Aaron Steed is putting together some of the AI code he's written into a library, starting with examples of A* pathfinding, genetic algorithms and neural networks.

I did my own implementation of A* based on the same tutorial recently. I've put it online here since our coding styles are quite different and it might be useful to have an alternative example to work with.

Big Balls of Mud

In an ongoing discussion with Ben Gimpert on teaching with Processing, I note:

"I do have something to say about methodologies and Processing, I'm just not sure what it is. There's probably something profound about the Big Ball of Mud methodology and the Japanese 'craze' for hikaru dorodango."

Ruby on Rails? PHP + Smarty? C# / VB.NET? Something Java-esque? Python? Perl?

There's been a great deal of fuss recently about Ruby On Rails. Is it just hype, or is it really so simple?

I've got a few ideas for simple web-apps, and I'm looking to learn something new (you know, for fun). I like to code. I like elegance and neatness. I wouldn't mind being able to leave the graphic design to a real designer. I think I know what I'm doing, but I don't know where to start.

I'm not a databases person, but I know a reasonable amount about SQL Server, and I think I know how it differs from MySQL. I don't really know how things talk to databases - when I've done it in PHP/MySQL or ASP/SQL Server it's just been magic. I've played around with phpmyadmin (if that's what it's called).

I know what code-injection is. I know what session variables are. I know the difference between POST and GET. I know about HTTP headers and meta tags. I know why not to use client-side VB, and I'm pretty handy with javascript. I know the difference between HTML and XHTML. I know what a socket is. And a port. I've never really understood CGI. I don't really know what XSLT does to XML, and I don't think I want to either. I like the look of xpath. Unicode is pretty good, and essential.

I'm very Java-literate - that's my day job, and my hobby with Processing - but I've never looked into JSP seriously. Somebody said something about a TomCat. I can write proper C++ with templates and namespaces and exceptions and everything, but I probably write it like a Java programmer (and I never understood all the arguments about strings, and pointers). Would I like C#? If I'm familiar with ASP and VBscript, would I like VB.NET? I'm not a Microsoft person.

I've done a bit of bad C programming using cURL and OpenGL. I know a bit of Python, but I've never done anything serious with it. I know a bit of Perl, but it gives me the creeps. I know a bit of VBScript, but much of it seems back-asswards. I know a bit of PHP, but it likewise seems a bit messy (though intuitive, and I like the look of Smarty). LISP isn't out of the question. If someone wrote web-apps in Prolog, I would probably find it fascinating but it too would give me the creeps.

So... What are you using, and why? If you were learning from scratch, what would you choose, and why?

Why shouldn't I use Ruby On Rails? And what should I use instead?

Bear in mind you have to get me away from Why's Poignant Guide To Ruby...

Flickr and PHP

Niall Kennedy points to a presentation about Flickr and PHP given by Cal Henderson of Ludicorp. I'm pleased about this, because Cal used a screenshot of my Flickr Rainbow as an example of Flickr API usage. The rainbow is down at the moment, but a new version is coming soon. Exciting additions will include respect for Creative Commons licenses (only photos licensed for derivative works will be used) and a standalone non-applet version (I might even do a continuously updating screensaver version, one day).

← Before