Category Archives: Projects

ColorMine.org - Color Converters and Delta E Calculators

ColorMine.org

I’m still putting together my 2013 goals, but I do know that one of them is to launch more sites.

I’ve been doing this whole internet thing for a long time now, I really aught to have more to show for it.

Knowing that perfect is the enemy of good enough I’ve opted to take a release early and release often approach to launching sites.

First up is colormine.org. It’s a simple site that wraps a small color library I wrote.
Continue reading

Delving into C#

This year I’ve decided to really get into C#. My .NET experience is shall at best so aiming to rectify, I picked up C# in Depth and commenced skimming!

Now, I’ve made my fair share of M$ snide asides, but I’m having a hard time coming to gripes with C#. Everything I run into either “just works” or exceeds my expectations. And the cool features are in fact, quite cool! Color me impressed!

Noob!

Noob!


For fun I rewrote a few of my Project Euler Solutions to buff up on the syntax. After I got the semi-colons and brackets all figured out, I moved on to something a little bigger.

I wanted a simple program to run and benchmark my solutions, so I wouldn’t have to do as much leg work every time I converted a problem. I figured this would be a simple enough thing to do, and it would provide a good foundation for a future gui application and beginning unit testing.

I wanted to share some particulars that I thought were pretty cool, you can grab the code I’m talking about from the google code repository, and follow along…or something.

Generics, Delegates and Lambdas
Generic Collections provide a data structure that I can access and use just like an array, but also provides methods for dealing with delegates.

Delegates are very similar to closures, blocks, procs, and lambdas like I’ve worked with in other languages, so the transition was smooth. The lambda syntax was particularly reminiscent of pythonic list comprehensions.

Thanks to delegates, I can turn this:

var matchingTypes = new List<Type>();
foreach(t in CurrentTypes) {
	if(t.IsSubclassOf(parentType) {
		matchingTypes.Add(t);
	}
}
return matchingTypes;

Into this:

return CurrentTypes.FindAll(
	delegate(Type t)
	{
		return t.IsSubclassOf(parentType);
	}
);

And finally, via lambda, to this!

return CurrentTypes.FindAll(
	t => t.IsSubclassOf(parentType)
);

Not too shabby, eh?

Reflection
Most of my programming has been in ColdFusion, JavaScript and Ruby. There’s been a little bit of this and a little bit of that peppered in there, particually C and Java while I was at UCF, but for the most part I’ve enjoyed working with dynamic and/or interpreted languages. Meta-programming is common in these types of languages, but I was surprised and impressed to read up on reflection. In this case, reflection allows me to dynamically detect and run my problems, which makes it easier (and cleaner) for me to add new solutions.

Here’s a simplified “ClassMaster” class I use to wrap my reflection calls for listing and creating classes, so you can see what I’m on about:

class ClassMaster
{
	private Assembly CurrentAssembly { get; set; }
	private List<Type> CurrentTypes { get; set; }

	public ClassMaster()
	{
		CurrentAssembly = Assembly.GetExecutingAssembly();
		CurrentTypes = new List<Type>(CurrentAssembly.GetTypes());
	}

	// should probably take arguments to pass thru...somehow
	public Object CreateClass(Type classType)
	{
		return CurrentAssembly.CreateInstance(classType.FullName);
	}

	public List<Type> getTypesByParentClass(Type parentType)
	{
		return CurrentTypes.FindAll(
			t => t.IsSubclassOf(parentType)
		);
	}
}

That’s it for now. I’ll be looking into LINQ and unit testing in the next couple weeks, and then I’m on to the gui. ASP, SilverLight, and WPF here I come!

Here are those links again:
Release
Latest Version

Announcing GeoHashFlash!

Okay, so it’s hardly an announcement, but I’ve been working on a flex app for determining your daily official xkcd meetup location via the geohashing method described in comic 426.

Much love to:

I’m still learning Flex and this is my first adventure with Google Map, so it’s rough. And there’s still a lot of work to do, like, validation, error checking, or testing for example. But hey, first pass!

Here’s the code for the actual GeoHash class. I have “view source” disabled because it currently contains my API key, but you can download the key-less zip.

public class GeoHasher {

	private var date:Date;
	private var dow:Number;
	private var latitude:Number;
	private var longitude:Number;

	private var dateHash:String;
	private var dowHash:String;

	private var ready:Boolean;

	public function GeoHasher() {
		this.ready = false;
	}

	public function reset(
		date:Date,
		dow:Number,
		latitude:Number,
		longitude:Number
	):void {
		this.date      = date;
		this.dow       = dow;
		this.latitude  = latitude;
		this.longitude = longitude;

		this.ready     = true;

		setHashValues();
	}

	public function getDestination():Object {
		if(ready) {
			var hash:Object = getHashAsDecimal();
			return {
				latitude: toOrdinal(latitude, hash.date),
				longitude: toOrdinal(longitude, hash.dow)
			};	
		}
		return { latitude: "", longitude: "" };
	}

	// privates

	private function setHashValues():void {
		var formattedDate:String = Utilities.dateFormat(date,"-");
		var string:String = formattedDate + "-" + dow.toString();
		var hash:String   = MD5.encrypt(string); 

		dateHash = hash.slice(0,hash.length / 2);
		dowHash  = hash.slice(hash.length / 2,hash.length);
	}

	private function toOrdinal(pre:Number, post:Number):String {
		return (Math.floor(pre) + post).toString();
	}

	private function getHash():Object {
		return { date: dateHash, dow: dowHash };
	}

	private function getHashAsDecimal():Object {
		var hash:Object = getHash();
		return {
				date: Utilities.fractionalHexToDecimal(hash.date),
				dow: Utilities.fractionalHexToDecimal(hash.dow)
		};
	}
}

Mid Year Resolutions

I’ve always liked the month of June. It’s my birth month, but it’s also close enough to the middle of the year, which makes it a great time to re-evaluate my New Years Resolutions.

I’ve done pretty terrible on that list. However, I’ve made a lot of progress in areas I hadn’t anticipated 6 months ago.

All in all, it’s been a pretty good year and things are looking up!

Flex Game of Life Pt 2

I made a few user interface updates to my crappy Flex version of Conway’s Game of Life.

The board now wraps, and I added a Timer with an adjustable delay via the lovely HSlider control. I wanted to replace the cells with Sprites but it’s giving me grief and I want to play The Sims.

I’m still making my mind up about Flex, but I have to admit that aside from the Sprite issue I’ve been pleasantly surprised at how easy everything’s been coming together.

You can view source on the application, or you can download the zip.

Fetching Local Tweets in Ruby

Tests take forever because of all the HTTPRequests

Tests take forever because of all the HTTPRequests

As planned, I wrote a little class incorporating Geocoder and twitter_search so you can search for tweets within x miles (or kilometers) of an address.Geocoder does an excellent job of parsing addresses and twitter_search is simple and efficient.

Four thumbs up

Next phase I’d like to set up a little web interface showing tweets and tweeters. There’s something funny about hitting the internet to find out what’s going on around you.

Here’s the class, as you can see there are a few overridable defaults

require 'rubygems'
require 'geocoder'
require 'twitter_search'

class Twitter_Interface

  attr_accessor :tpp, :distance
  attr_reader   :address, :geocode
  attr          :geocoder, :client 

  def initialize addr = "", dist = "2mi", pp = 15
    @client   = TwitterSearch::Client.new
    @tpp      = pp
    @distance = dist
    @geocoder = initialize_geocoder
    set_location addr
  end

  # Could also use Yahoo API, but it requires API key.
  def initialize_geocoder geocoder = Geocoder::GeoCoderUs.new
    geocoder
  end

  def format_geocode geocode = @geocode
    if is_geocode? geocode
      return "#{geocode.latitude},#{geocode.longitude},#{@distance}"
    end
    ""
  end

  def tweets
    @client.query :geocode => format_geocode, :tpp => @tpp
  end

  def address_to_geocode addr = @address
    if addr == ""
      return ""
    end
    @geocoder.geocode addr
  end

  def is_geocode? geocode
    geocode.respond_to? "success?" and geocode.success?
  end

  def set_location addr
    @address = addr
    @geocode = address_to_geocode @address
  end

end

And here’s how you use it

t = Twitter_Interface.new "1600 pennsylvania ave nw washington dc"

#print the twitter query formatted geocode, default distance is 2 miles
puts t.format_geocode

#=>"38.898748,-77.037684,2mi"

#fetch the Twitter_Search::Tweets within 2 miles of the white house!
tweets = t.tweets

Download a .zip of the code / tests

Gosu Extensions Update

tests_complete

I’ve finally finished updating my Gosu Extensions. It’s far from perfect, but it’s in a good spot and I’m relatively happy with it. It feels good to get something “done”.

Although my coding process hasn’t been 100% TDD, I would say at least it’s been test centric and irregardless of whether I actually do anything with this, I think it’s been a good experience.

I plan on updating some of my “games” to use this new version, so I can see if these extensions actually save me any work.

Here are a few of the highlights

  • JavaScript like elements and events ( on_click, on_focus etc )
  • Wrappers for Gosu Images, Samples, Fonts and Text
  • Bounding Boxes for basic collision detection
  • Easy cursor support
  • Basic grid / matrix support
  • Basic scheduling system

Enough chit chat, Download the code!

ID3Mapper – A Ruby ID3 renaming utility

I have a Zune. At the time I had a working Windows box, hated iTunes, and thought I would actually use the radio tuner and I actually really like it. The desktop software…not so much.

There’s only a Windows version, it runs horribly when you have a lot of artists, it only picks up half of my podcasts. I figured out a while back that the reason my podcasts weren’t showing up had to do with the id3 tags, so I wrote a horrible little ruby script ( utilizing id3lib-ruby ) that would set the correct genre of “podcast” for everything in my podcast directory.

For fun, I thought it would be cool to write a little desktop app using the most popular Ruby gui toolkit Shoes that would make it easier for me to correct some other id3 tag irregularities in my media collection.

The first step was to re-factor my id3 script. I’ve been tooling around with automated testing a bit over the last couple months and I thought this would be an excellent chance for me to attempt a full-on test first methodology. I did, and I’m pretty happy how it turned out, although it certainly didn’t save me any time.

Click to download the new and improved id3mapper.

I’ll eventually package this up into it’s own module, but for now this is how it works:

require '../settings'
i = ID3Mapper_Console.new

i.set_artist "/home/joe/Podcasts/WNYC Radio Lab", "Radio Lab"
i.set_genre "/home/joe/Podcasts/WNYC Radio Lab", "podcast"

Finding and Fixing Broken Images with Ruby

A family members was having a problem with some mixed up image names on a static html site. I could have fixed it manually in a few shakes, but that’s no fun. Instead I used hpricot to scrape, open-uri to test for broken-ness, Find to search and some good old fashion regex to correct.

This was my first time messing around with hpricot and I found it to be powerful and easy to use, two thumbs up. I foresee some scraping and spidering posts in the near future.

On to the code:

My final script was a bit hairy so I broke out the bit I used to find the broken images.

If you run the script it’ll print the offending paths to screen:

ruby image_scanner.rb http://site.com/busted.html

Or you can call the get_broken_images method to get an array back:

require 'image_scanner'
scanner = Image_Scanner.new
broken_images = scanner.get_broken_images "http://site.com/busted.html"

In case you’re interested, I’ve also uploaded the full code that I used to search for and correct the images although it’s implementation specific, riddled with lazy and is poorly tested. Read the disclaimer!

Just run it and be amazed!

ruby image_scanner.rb http://site.com/busted.html /media_folder /busted.html /fixed.html

Download only the broken image scanner
Download the full script

Ruby Line Formatting

I wrote a little utility to turn something like this

this=that,
something_else="something else entirely"

Into the much easier to read

this           = that,
something_else = "something else entirely"

I use it in conjunction with my clipboard utility so I can just copy, run, paste. I mostly use this when I’m working with ColdFusion, so it also works for those pesky cfsets.

Usage: (After copying your target text to your clipboard)

ruby clipboard_format_set.rb

Download It!

PS: I’d love to set some of my scripts up as Eclipse shortcuts so I’ve been toying with the idea of writing an Eclipse plug-in to act as a proxy. I realize you can set up and run “External Tools”, but I have yet to see a way to bind a key. I’d like the plug-in to take care of the “pasting” to save me that extra ctrl-v. Maybe this would make a good New Years Project?