Archive for the 'Uncategorized' Category

Berlin, city of culture for the bizarre

Thursday, October 1st, 2009
Gorilla outside the gallery shop

Gorilla outside the gallery shop

I’m in love with Berlin already. There’s graffiti all over the place, art galleries of the bizarre and obscure, fantastic displays of architecture, restaurants to cater for anyones taste the world over, and enough to keep you busy for a very long time.

Just as the tube map looks remarkably similar, Berlin looks and feels like it could be London’s long lost brother. Hopefully I feel this way after 5 days here. I found one free art gallery hidden away down a side street off of Friedrich Strasse which left me wanting to buy everything. I’ll most likely compromise and buy a postcard instead. Iron exhibits both huge and small fill the central courtyard and various huts off to the side as bunch of those artists responsible sat around making more.

It felt like I’d walked into some sort of rennaisance fraternity in gotham city – they had their own bar, which nobody seemed to be interested in using, a group huddled around an open fire, and a small burger van tucked away in the corner.  If the rest of Berlin stays like this (and first impressions indicate it will), I might not even mind staying in a city for longer than days.

Talk like a Pirate Day, Arrrr.

Saturday, September 19th, 2009

Shiver me timbers, fire th’ canons! Etc. ‘Tis September 19th, talk like a pirate day, arrr.

So don’t be surprised that ye’ll find everythin’ here Piratised fer th’ day. Sorry :)

Arrrrrr.

Arrrrrr.

Rescuscitating AMM with Amazon Web Service signed requests

Wednesday, August 26th, 2009

A few days ago Amazon added a requirement to their AWS that all requests to the service be signed, lest they be rejected. I’ve been using Sozu’s excellent Amazon Media Manager plugin for a while now to manage the currently reading list on this blog. It’s been a great way to keep track of exactly what I have read (avoiding the need to, like, remember), as well as masking my illiteracy by pasting a giant list of what is commonly known as airport trash.

Unfortunately, this is one plugin that hasn’t been updated in quite a while (after all, if ain’t broke…), so it broke. Being the sort of developer that’s quite happy to pick up a block of php and hack it until it works, I stumbled across this blog entitled ‘Amazon® AWS HMAC signed request using PHP‘, which has a function to download.

So to fix AMM:

1. Download that file, and copy the contents into the bottom of amm_parser.php.

2. In the same file (amm_parser.php), replace your _setUrl function with this one:

function &_setUrl() {
	//Build URL from base URL and other required parameters
	switch ($this->_locale) {				
		case 'uk':
			$region = 'co.uk';
			break;
		case 'de':
			$region .= 'de';
			break;
		case 'jp':
			$region .= 'co.jp';
			break;
		case 'fr':
			$region .= 'fr';
			break;
		case 'ca':
			$region .= 'ca';
			break;
		case 'us':
		default:
			$region = 'com';
			break;
	}
	$public_key = "< < Your Access Key ID >>";
	$private_key = "< < Your Secret Access Key >>";
	$url = aws_signed_request($region, array(
			"Operation"=> "ItemSearch",
			"Keywords" => $this->_parameters[Keywords],
			"ResponseGroup"=>$this->_parameters[ResponseGroup],
			"SearchIndex" => $this->_parameters[SearchIndex],
			"AssociateTag" => urlencode($this->_associate_tag)),
			$public_key, $private_key);
	return $url;
}

3. Oddly enough, this new method requires a private secret key which Amazon recommends to not give to anyone. So I’m not going to post mine here, even though it’s required for the plugin to work. So before I ponder that particular nugget of madness, you’ll need to sign up for an AWS developer account, and find your own keys via the Access Identifiers page. These need to be added into the function above.

That should be enough to get you back up and running again, although selfishly I’ve only really tested it for my needs alone. So please let me know if it works, or fails miserably.

For all the legal bits, I’m not at all affiliated with Amazon or Sozu – so please use at your own risk :)

C# Joins with Linq and Lambdas

Saturday, August 8th, 2009

I’m always forgetting the syntax for lambda joins in C#, because I never use them enough and get bored looking for reminders enough that I just revert back my old ways and use the query expression instead. So rather than find a good tutorial and bookmark it, I’ll post it here instead. By the time it falls off the front page, I’ll just about have remembered how to do it without needing this anyway :)

Query Syntax

var products = from audio in DbContext.DataContext.ProductAudios
join product in DbContext.DataContext.ProductAudios on audio.ProductId equals product.ProductId
select new { Product = product, Audio = audio };

Lambda Syntax

var products = DbContext.DataContext.ProductAudios.Join(
                DbContext.DataContext.Products,
                audio => audio.ProductId,
                product => product.ProductId,
                (audio, product) => new { Product = product, Audio = audio });

It might look like more code because of my formatting, but I find the lambda syntax much convenient when chaining queries together with other where’s and groupby’s, especially when that might be split across different methods. It also isolates your join nicely, whereas I find the query syntax will start to get particularly unreadable with more complex queries.

Last but not least, another piece of linq-join-related syntax I’m finding myself always having to look up a lot is for left outer joins. Fortunately I always end up at MSDN for that one, so I’ll just link to it here:
How to: Perform Left Outer Joins

Running Ruby methods within C# / .NET

Thursday, July 23rd, 2009

The last example might have been a little too trivial, even by my standards. Even I struggled to imagine a scenario where I might ever need to use it. So hopefully this one will be a little bit more interesting and demonstrate something more useful.

Useful, but still just as simple as the previous examples, that is. Again – you’ll need your references from the downloaded IronRuby bin/ folder. And as you’ve come to expect, a very simple ruby script defining a lambda function.

$m = lambda {
            a = Array.new
            a.push(2, 3)
            (4..50).each do
               |i|
               (2..(Math.sqrt(i).ceil)).each do
                  |thing|
                  if (i.divmod(thing)[1] == 0)
                     a.push i
                     break
                  end
               end
            end
            return a
         }

From this, we’ll get an array of the prime numbers. The function can then be executed rather nicely from within your .NET code like this:

var ruby = Ruby.GetEngine(Ruby.CreateRuntime());
ruby.Execute(@"
    $m = lambda {
//.. snip..
            return a
         }
");
var rubyContext = Ruby.GetExecutionContext(ruby);
var m = (Proc)rubyContext.GetGlobalVariable("m");
var rubyArray = (RubyArray) m.Call();
foreach (var o in rubyArray)
{
    Console.Write(string.Format("{0},", o));
}

Now we’re really starting to leverage that syntactical beauty of ruby within .NET and jumping (almost) seamlessly between the two. Now, I really should do some demos on something more useful than prime numbers, and perhaps get into one of the big areas of interest of Ruby – testing frameworks. Not tonight though :)

Demo project available as usual: