Wednesday, October 17, 2007

C# .NET Regular Expressions gotcha

I sure do love me some regular expressions but haven't really had the need in any of my C# code until now.

A few things did catch me out though, in particular the constructor for the Regex class.

The constructor signature I'm interested in is this one: (string pattern, RegexOptions options). All well and good until you want to set multiple options!

Now I might be slightly naive but why doesn't the constructor use the params keyword allowing it to accept a variable number of RegexOptions? The constructor would then look something like (string pattern, params RegexOptions[] options). I think this follows the LOLA, unlike the current method of passing multiple regex options to the constructor.

Oh, guess how you are supposed to do it? Via a bitwise OR of the enum values ofcourse! Like my NUnit fun, at least I can now just get on with it.


 1 Regex matchFieldValue =
 2   new Regex(@"
 3     (?<Field>\w+) #capture 1 or more word characters to the named group 'Field'
 4     \s*=\s*       #followed by 0 or more spaces, an = sign and 0 or more spaces
 5     (?<Value> '   #match an apostrophe
 6         ([^'] |   #match 0 or more of any character that is not an ' OR
 7          ''.)*    #double apostrophe's followed by any character          
 8     ')            #match the closing ' and capture to named group 'Value'
 9             ",
10              RegexOptions.Compiled |
11              RegexOptions.IgnoreCase |
12              RegexOptions.IgnorePatternWhitespace);

Friday, October 12, 2007

NUnit configuration woes

I've been going through writing NUnit tests for a .NET web app I'm writing at work and all was going well until I hit some code that was using the ConfigurationManager.

1 private static readonly string CONNECTION_STRING =
2  ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;

For backgrounds sake I've created a test dll that sits outside the web app that calls a dll used by the web app. The connection string information is stored in Web.config.

Whenever NUnit loaded up the tests requiring that connection it barfed this at me:

Test.TestDataAccessUtility.GetNameValueTOList : System.TypeInitializationException : The type initializer for 'DataAccessUtility' threw an exception.
----> System.NullReferenceException : Object reference not set to an instance of an object.


Nice, this essentially means that ConfigurationManager had crapped itself.

ConfigurationManager.ConnectionStrings.Count was returning 1.

ConfigurationManager.ConnectionStrings[0].ConnectionString sent back a conn string for a SQLExpress db with some configuration string I sure didn't set.

It would appear that the NUnit loads it's config file over your application's config file.

To fix, I just replaced the contents of the NUnit config file: Project1.config with the contents of my Web.config. There could be a better solution, but it's working now and I can move on with my life.

Thursday, October 4, 2007

Sequence Mapping Functions

I've slowly been making my way through Peter Seibel's Practical Common Lisp. Even though I'm also in between a few other books at the moment I've managed to let some of it soak in.

I came across the following example that shows off the quintessential map function.

1 (map 'vector #'* #(1 2 3 4 5) #(10 9 8 7 6))

It simply produces a new sequence by multiplying the subsequent elements of the supplied sequences, resulting in: => #(10 18 24 28 30)

To put it in perspective I decided to have a crack and see what an equivalent function would look like in Ruby. After a bit of mucking around I came up with this:

1 [1,2,3,4,5].zip([10,9,8,7,6]).map{|x,y| x * y}

I'm sure someone could probably come up with something nicer as there is more than one way to skin a cat in ruby (I haven't tried, honest - ..to skin a cat that is). Regardless I think we should all sit back and appreciate the aesthetics of the lisp map.

Monday, October 1, 2007

A slipshod guide to replacing 2003 Ford Focus tail and headlights

This was not fun and a bad way to kill a few hours. L's drivers side rear tail-light and front passenger side head-light decided to cark it at the same time. Now, I've changed a few globes in my time but this was ridiculous. What were those freaking Germans thinking when they put this car together?

I'm documenting this due to the shitty Focus car manual and lack of good info on ye olde' internet. Remember I am a nerd, not a grease monkey so if you break something, sucked in for taking my advice :)

For the easier of the two, I present the rear tail-light:
  1. Open the hatch, you'll see a screw next to the light mounting. Of course it's not a phillips or flat-head screw, it's a bastard hexalobular type thingamajig screw, look here for more information than you could ever care to want to know about the stupid screw: http://en.wikipedia.org/wiki/Torx. Luckily i had an attachment in my drill-bit set and managed to whip it off.

  2. This is where the manual is crap, it has a picture of a screw in the light? What the hell? No, the screw is actually behind the light, not that they tell you that. So climb into the boot if you are nimble or reach your hand into the boot kind of behind where the light fitting is. You can undo this screw with your hand as it's quiet large.

  3. Now you can just pull the light fitting off, and unscrew the little cylindrical plastic thingy that is housing the globe.

  4. Change it, put it all back together

  5. Sup on some Coopers Sparkling Ale (you're going to need it for the passenger side head-light).

Ok, the rear tail-light wasn't that bad but this is where those conniving German engineers decided to play nasty shenanigans.
  1. The battery is on the passenger side right near where we need to shove our hands. Remove the battery cover as it will make life a little bit easier.

  2. You won't be able to see a thing because the light cover recess is underneath the front of the bonnet. I read on a few forums that people have done crazy things to get to it, like jack the car up and remove the front wheel, or completely dismantle the front-end of the car, we don't want to be doing that.

  3. Grab a mirror and put it in the engine-bay so you can get a better look at what you're fiddling with.

  4. You'll see the black light housing, there is a metal hinge that runs horizontally across it, you need to flick it down with a bit of force.

  5. The housing also has two clips attaching at the top. These are bastards. I just bunted it with the handle of the screwdriver and managed to pop it off. Remove it, and put it to the side.

  6. It's best to do this bit by feel, because it is insanely hard to hold a mirror and do this at the same time. Reach in to where abouts you think the globe is and to the right of it there is another metal hook. Push it in and kind of pull up on it, it should spring release, move it to the left, out of the way.

  7. Now you can reach in and pull the globe out and change it.

Probably the hardest bit for me was getting the housing back in, even though I aligned the two clips no matter how hard i pushed or angled it, it would not go back in and seal the light fitting. Probably didn't help that that I had bugger all lighting when I was doing it.

Hopefully this has saved someone else a headache or a $60 visit to your mechanic / ford dealer, let's see if I can talk L into trading the Focus in for an ESP XD, atleast i can change the globes in them :)