Spray WD-40 using the nozzle around the lip of the stuck glass.
Run hot water around the bottom glass, fill the top glass up with cold water.
Smash on the lip with a skewer or more appropriate implement.
Bob's your uncle.
Merry Christmas
Monday, December 24, 2007
Sunday, December 9, 2007
Sventon for my homies
I'm a big fan of sventon, the guys have created one of the kickingest svn repository viewers around.
Much to my dismay after an upgrade to 1.3.0, stuff stopped working. I didn't have time to look at it until recently but I dived into the tomcat logs and pulled up this.
org.springframework.beans.MethodInvocationException: Property 'archiveFileCharset' threw exception; nested exception is java.nio.charset.UnsupportedCharsetException: cp437
Not so cool.
cp437 is the old dos-us character set which sure isn't installed on our svn box, hence the error.
So if this saves anyone else some time, go into the WEB-INF directory of sventon and edit sventon-servlet.xml. There is a property called archiveFileCharset. Change the value to whatever is appropriate for you. In my case it was cp1252 which is en-au amongst others, but should be good enough for most engrish speakers.
I thought Unicode had pretty much made Windows Code Pages obsolete, oh well.
Much to my dismay after an upgrade to 1.3.0, stuff stopped working. I didn't have time to look at it until recently but I dived into the tomcat logs and pulled up this.
org.springframework.beans.MethodInvocationException: Property 'archiveFileCharset' threw exception; nested exception is java.nio.charset.UnsupportedCharsetException: cp437
Not so cool.
cp437 is the old dos-us character set which sure isn't installed on our svn box, hence the error.
So if this saves anyone else some time, go into the WEB-INF directory of sventon and edit sventon-servlet.xml. There is a property called archiveFileCharset. Change the value to whatever is appropriate for you. In my case it was cp1252 which is en-au amongst others, but should be good enough for most engrish speakers.
I thought Unicode had pretty much made Windows Code Pages obsolete, oh well.
Friday, November 16, 2007
Get full name for Windows user from Ruby
Lately I've been writing a few automation programs and it was suggested by a colleague that I write one for logging a job with EDS, our "service" provider.
The web page is nasty to navigate, I think they make it hard so people decide to give up and go get a beer instead of logging a job :)
Anyway, one of the fields on the page requires the full name of the person logging the job. It's trivial to grab the username of the logged on user:
However, I wasn't sure of the easiest way to grab the user's full name. There are a few packages out there, for example the sys-admin gem. It didn't work though, I dug around in the source and it hardcodes a cimv2 path, no good.
Seeing this is a quick hack running on windows for windows, rah rah rah, might as well boogey with it.
Enter ADSI.
The following interrogates our AD and grabs what I want. Too easy.
The web page is nasty to navigate, I think they make it hard so people decide to give up and go get a beer instead of logging a job :)
Anyway, one of the fields on the page requires the full name of the person logging the job. It's trivial to grab the username of the logged on user:
1 puts ENV['USERNAME']
However, I wasn't sure of the easiest way to grab the user's full name. There are a few packages out there, for example the sys-admin gem. It didn't work though, I dug around in the source and it hardcodes a cimv2 path, no good.
Seeing this is a quick hack running on windows for windows, rah rah rah, might as well boogey with it.
Enter ADSI.
The following interrogates our AD and grabs what I want. Too easy.
1 require 'win32ole'
2 puts WIN32OLE.connect("WinNT://your.ad.box.sa.gov.au/#{ENV['USERNAME']}").FullName
2 puts WIN32OLE.connect("WinNT://your.ad.box.sa.gov.au/#{ENV['USERNAME']}").FullName
Wednesday, November 14, 2007
Blowing away IE
ASP.NET development is great fun, especially when you are targeting Internet Explorer 7 (as if there are any other browsers anyway) :).
It doesn't take much to confuse IE so I find myself in the continuous loop of trying a code change, clearing the cache in IE, rebuilding the app and running it again. Numerous times a day, too many. Dancing the Microsoft Visual Studio developer jig if you will.
Well, enough is enough. Time to automate it.
Consider the following ruby script that uses WSH. I'm aware of watir and other winole jiggery, but why not just send the keystrokes, I say. This script blows out the IE cache, quits it, goes into visual studio and restarts the project, hurrah. Obviously you'll want to replace the AppActivate parameters to match your window titles.
It doesn't take much to confuse IE so I find myself in the continuous loop of trying a code change, clearing the cache in IE, rebuilding the app and running it again. Numerous times a day, too many. Dancing the Microsoft Visual Studio developer jig if you will.
Well, enough is enough. Time to automate it.
Consider the following ruby script that uses WSH. I'm aware of watir and other winole jiggery, but why not just send the keystrokes, I say. This script blows out the IE cache, quits it, goes into visual studio and restarts the project, hurrah. Obviously you'll want to replace the AppActivate parameters to match your window titles.
1 require 'win32ole'
2
3 wsh = WIN32OLE.new('Wscript.Shell')
4 wsh.AppActivate('Generic Auditing Tool')
5 sleep(2)
6 wsh.SendKeys('%{T}') #ALT-T: select 'Tools'
7 sleep(1)
8 wsh.SendKeys('o') #select 'Internet Options'
9 sleep(1)
10 wsh.SendKeys('%{D}') #select 'Delete'
11 sleep(1)
12 wsh.SendKeys('a') #select 'Delete all'
13 sleep(1)
14 wsh.SendKeys('d') #select 'also delete files and settings stored by add-ons'
15 sleep(1)
16 wsh.SendKeys('y') #select 'Yes'
17 sleep(8)
18 wsh.SendKeys('%{F4}') #quit 'Internet Options'
19 sleep(1)
20 wsh.SendKeys('%{F4}') #quit IE
21
22 wsh.AppActivate('NAT - Microsoft Visual Studio') #grab the visual studio window
23 sleep(2)
24 wsh.SendKeys('^{F5}') #start the project without debugging
2
3 wsh = WIN32OLE.new('Wscript.Shell')
4 wsh.AppActivate('Generic Auditing Tool')
5 sleep(2)
6 wsh.SendKeys('%{T}') #ALT-T: select 'Tools'
7 sleep(1)
8 wsh.SendKeys('o') #select 'Internet Options'
9 sleep(1)
10 wsh.SendKeys('%{D}') #select 'Delete'
11 sleep(1)
12 wsh.SendKeys('a') #select 'Delete all'
13 sleep(1)
14 wsh.SendKeys('d') #select 'also delete files and settings stored by add-ons'
15 sleep(1)
16 wsh.SendKeys('y') #select 'Yes'
17 sleep(8)
18 wsh.SendKeys('%{F4}') #quit 'Internet Options'
19 sleep(1)
20 wsh.SendKeys('%{F4}') #quit IE
21
22 wsh.AppActivate('NAT - Microsoft Visual Studio') #grab the visual studio window
23 sleep(2)
24 wsh.SendKeys('^{F5}') #start the project without debugging
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.
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);
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.
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.
1 private static readonly string CONNECTION_STRING =
2 ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
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.
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:
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.
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:
Ok, the rear tail-light wasn't that bad but this is where those conniving German engineers decided to play nasty shenanigans.
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 :)
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:
- 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.
- 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.
- Now you can just pull the light fitting off, and unscrew the little cylindrical plastic thingy that is housing the globe.
- Change it, put it all back together
- 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.
- 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.
- 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.
- Grab a mirror and put it in the engine-bay so you can get a better look at what you're fiddling with.
- 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.
- 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.
- 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.
- 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 :)
Friday, September 21, 2007
Code Voyeur
I like perusing public code repositories, be they CVS/SVN/Mercurial/Git whatever. I'm not sure if this is some kind of sick code voyeurism fetish but I think it's kind of healthy in degrees. It's a good place to pick up tricks. Kind of like doing the weekly ruby quiz but looking at the code before you know what the task is and seeing how closely you think they correlate to the spec.
Scott Hanselman is also on the kick of reading other peoples code to become a better developer, could it be the path to enlightment?
It dawned upon me that at work I have rarely had the need to tread outside my projects. All this time i've been searching far and wide for great code when I had a veritable treasure trove of unread code right under my nose.
We have a VB contractor doing some work for us and I caught wind that someone asked him to do it in C# ASP.NET. Wow. Might not be so bad I thought. So I had a look in SVN and came across this:
1 public static string Proper(string StringIn) {
2 string Temp = StringIn;
3 if (Temp.CompareTo(String.Empty) != 0) {
4
5 char[] Delimiter = { ' ' };
6 string[] Words = Temp.Split(Delimiter);
7 Temp = String.Empty;
8
9 for (int i = Words.GetLowerBound(0); i <= Words.GetUpperBound(0); i++)
10 if (Words[i].Trim().CompareTo(String.Empty) != 0)
11 Temp += Words[i].Substring(0, 1).ToUpper() +
12 Words[i].Substring(1).ToLower() + " ";
13 return Temp;
14 }
15 else
16 return String.Empty;
17 }
18
Apart from the upper case local variable names, in-efficient string concatenation and re-inventing the wheel, the dude did alright. Absolute respect to him, he's smart, he got it working, but it is obvious that C# is not his native tongue. That is textbook VB in C#.
Here is something roughly equivalent I came up with that produces the same output (don't lambast me for not getting the current culture, "en-AU" does weird shit that "en" doesn't) :)
1 using System.Globalization;
2 public static string TitleCase(string str) {
3 return String.IsNullOrEmpty(str) ? String.Empty
4 : new CultureInfo("en").TextInfo.ToTitleCase(str.ToLower());
5 }
It's not his fault really. He was asked to code in a language he wasn't familiar with and he got by. But what he did was speak English in down-town Nairobi.
Now I don't know what my point is but just look at the API's if you are using an unfamiliar language. I'm not shit-hot with C#, give me C, Java or Ruby and I've got a fighting chance. The thing is I'd assume that .NET would provide a way to title case a string. Instead of re-inventing the wheel I did a google search and came across the TextInfo class. You think he would have twigged that if VB has StrConv there might be something vaguely similar in C#.
Moral of the story, if you're asked to code in a language you are not familiar with, knock it back or make it clear it might not look pretty. But i guess who doesn't like getting paid?
Scott Hanselman is also on the kick of reading other peoples code to become a better developer, could it be the path to enlightment?
It dawned upon me that at work I have rarely had the need to tread outside my projects. All this time i've been searching far and wide for great code when I had a veritable treasure trove of unread code right under my nose.
We have a VB contractor doing some work for us and I caught wind that someone asked him to do it in C# ASP.NET. Wow. Might not be so bad I thought. So I had a look in SVN and came across this:
1 public static string Proper(string StringIn) {
2 string Temp = StringIn;
3 if (Temp.CompareTo(String.Empty) != 0) {
4
5 char[] Delimiter = { ' ' };
6 string[] Words = Temp.Split(Delimiter);
7 Temp = String.Empty;
8
9 for (int i = Words.GetLowerBound(0); i <= Words.GetUpperBound(0); i++)
10 if (Words[i].Trim().CompareTo(String.Empty) != 0)
11 Temp += Words[i].Substring(0, 1).ToUpper() +
12 Words[i].Substring(1).ToLower() + " ";
13 return Temp;
14 }
15 else
16 return String.Empty;
17 }
18
Apart from the upper case local variable names, in-efficient string concatenation and re-inventing the wheel, the dude did alright. Absolute respect to him, he's smart, he got it working, but it is obvious that C# is not his native tongue. That is textbook VB in C#.
Here is something roughly equivalent I came up with that produces the same output (don't lambast me for not getting the current culture, "en-AU" does weird shit that "en" doesn't) :)
1 using System.Globalization;
2 public static string TitleCase(string str) {
3 return String.IsNullOrEmpty(str) ? String.Empty
4 : new CultureInfo("en").TextInfo.ToTitleCase(str.ToLower());
5 }
It's not his fault really. He was asked to code in a language he wasn't familiar with and he got by. But what he did was speak English in down-town Nairobi.
Now I don't know what my point is but just look at the API's if you are using an unfamiliar language. I'm not shit-hot with C#, give me C, Java or Ruby and I've got a fighting chance. The thing is I'd assume that .NET would provide a way to title case a string. Instead of re-inventing the wheel I did a google search and came across the TextInfo class. You think he would have twigged that if VB has StrConv there might be something vaguely similar in C#.
Moral of the story, if you're asked to code in a language you are not familiar with, knock it back or make it clear it might not look pretty. But i guess who doesn't like getting paid?
Tuesday, September 18, 2007
Meta-Meta-Meta Programming
Now this isn't particularly clever or something you should even really consider doing but i've wasted my time so now you don't have to :). L and I got a lovely new bookshelf a few weeks ago and I noticed my beloved copy of The Pragmatic Programmer eyeing me off in the corner, so I started to thumb through it.
The astute amongst you that have read it would remember their example on meta-programming that uses Perl to take an ordinary text file and generate Pascal and C source code from it. I thought i'd update it a bit and use Ruby to generate Java, and, well Ruby.
I did it in a similar way to the prag guys i.e. the "proper" way but then I got a little bit curious about the notion of source that generates source that generates source. Plus, I hadn't really dug into Ruby meta-programming and this seemed like something harmless to play with.
Seeing we all love Shoes, let's have a look at a file called Shoe.txt that in pretty much plain English, models a shoe.
1 C Shoe
2 M brand String
3 M colour String
4 M size int 7
5 M isTrendy boolean true
6 M scent Scent
7 E
For clarification, the beginning of each line identifies what it is modelling:
C is the name of the class
M is a member of the class, specifying it's name, type and default value
E signals the end of the class.
Pretty straight forward, eh.
So how do we turn that into:
Shoe.Java
1 class Shoe {
2 private String brand = null;
3 public String getBrand {
4 return brand;
5 }
6 public void setBrand(String brand)
7 this.brand = brand;
8 }
9
10 private String colour = null;
11 public String getColour {
12 return colour;
13 }
14 public void setColour(String colour)
15 this.colour = colour;
16 }
17
18 private int size = 7;
19 public int getSize {
20 return size;
21 }
22 public void setSize(int size)
23 this.size = size;
24 }
25
26 private boolean isTrendy = true;
27 public boolean getIstrendy {
28 return isTrendy;
29 }
30 public void setIstrendy(boolean isTrendy)
31 this.isTrendy = isTrendy;
32 }
33
34 private Scent scent = null;
35 public Scent getScent {
36 return scent;
37 }
38 public void setScent(Scent scent)
39 this.scent = scent;
40 }
41
42
43 }
and Shoe.rb?
1 class Shoe
2 attr_accessor :brand, :colour, :size, :isTrendy, :scent
3 def initialize
4 @colour = nil
5 @size = 7
6 @isTrendy = true
7 @scent = nil
8 end
9 end
Well, there is the smart way i.e. the pragmatic way, or there is the meta-meta-meta programming way.
language_generator.rb
1 langs = %w(ruby java)
2 class LangGen
3 end
4
5 langs.each do |lang|
6 LangGen.class_eval <<-LETS_DANCE
7 $first = true
8 $init = ""
9
10 def #{lang}_class_start(name)
11 out = "class " + eval(\"name.chomp\") + ' '
12 out << "{" if '#{lang}' == 'java'
13 out << "\n"
14 out
15 end
16
17 def #{lang}_class_end
18 out = ""
19 out << "\n}" if '#{lang}' == 'java'
20 if '#{lang}' == 'ruby'
21 out << $init
22 out << " end"
23 out << "\nend"
24 end
25 out
26 end
27
28 def #{lang}_members(name, type, value)
29 out = ""
30 if '#{lang}' == 'java'
31 value ||= 'null'
32 out << " private " + eval(\"type\")+' '+eval(\"name\")+' = '+
33 eval(\"value\")+";\n"
34 end
35
36 out << "" if '#{lang}' == 'ruby'
37 out
38 end
39
40 def #{lang}_accessors(name, type, value)
41 accessors = ""
42 if '#{lang}' == 'java'
43 accessors << " public " + eval(\"type\")+
44 " get"+eval(\"name.capitalize\")+" {\n"
45 accessors << " return "+eval(\"name\")+";\n"
46 accessors << " }\n"
47 accessors << " public void set"+eval(\"name.capitalize\")+ '('+
48 eval(\"type\")+ ' '+ eval(\"name\")+")\n"
49 accessors << " this."+eval(\"name\")+" = "+eval(\"name\")+";\n"
50 accessors << " }\n\n"
51 end
52 if '#{lang}' == 'ruby'
53 value ||= 'nil'
54 if $first
55 $init << "\n def initialize \n"
56 accessors << " attr_accessor :" + eval(\"name\")
57 end
58 accessors << ", :" + eval(\"name\") unless $first
59 $init << ' @'+ eval(\"name\")+' = '+eval(\"value\")+"\n" unless $first
60 $first = false
61 end
62 accessors
63 end
64
65 LETS_DANCE
66 end
67
68 gen = LangGen.new
69 langs.each do |lang|
70 File.open('Shoe.txt').each do |line|
71 if line =~ /^C/
72 print gen.send("#{lang}_class_start".to_sym, line.gsub(/^C\s+/,''))
73 end
74 print gen.send("#{lang}_class_end".to_sym) if line =~ /^E/
75 if line =~ /^M\s+(\w+)\s+(\w+)\s+(\w+)?/
76 name, type, value = $1, $2, $3
77 print gen.send("#{lang}_members".to_sym, name, type, value)
78 print gen.send("#{lang}_accessors".to_sym, name, type, value)
79 end
80 end
81 printf("\n"+'*'*50 + "\n")
82 end
The astute amongst you that have read it would remember their example on meta-programming that uses Perl to take an ordinary text file and generate Pascal and C source code from it. I thought i'd update it a bit and use Ruby to generate Java, and, well Ruby.
I did it in a similar way to the prag guys i.e. the "proper" way but then I got a little bit curious about the notion of source that generates source that generates source. Plus, I hadn't really dug into Ruby meta-programming and this seemed like something harmless to play with.
Seeing we all love Shoes, let's have a look at a file called Shoe.txt that in pretty much plain English, models a shoe.
1 C Shoe
2 M brand String
3 M colour String
4 M size int 7
5 M isTrendy boolean true
6 M scent Scent
7 E
For clarification, the beginning of each line identifies what it is modelling:
C is the name of the class
M is a member of the class, specifying it's name, type and default value
E signals the end of the class.
Pretty straight forward, eh.
So how do we turn that into:
Shoe.Java
1 class Shoe {
2 private String brand = null;
3 public String getBrand {
4 return brand;
5 }
6 public void setBrand(String brand)
7 this.brand = brand;
8 }
9
10 private String colour = null;
11 public String getColour {
12 return colour;
13 }
14 public void setColour(String colour)
15 this.colour = colour;
16 }
17
18 private int size = 7;
19 public int getSize {
20 return size;
21 }
22 public void setSize(int size)
23 this.size = size;
24 }
25
26 private boolean isTrendy = true;
27 public boolean getIstrendy {
28 return isTrendy;
29 }
30 public void setIstrendy(boolean isTrendy)
31 this.isTrendy = isTrendy;
32 }
33
34 private Scent scent = null;
35 public Scent getScent {
36 return scent;
37 }
38 public void setScent(Scent scent)
39 this.scent = scent;
40 }
41
42
43 }
and Shoe.rb?
1 class Shoe
2 attr_accessor :brand, :colour, :size, :isTrendy, :scent
3 def initialize
4 @colour = nil
5 @size = 7
6 @isTrendy = true
7 @scent = nil
8 end
9 end
Well, there is the smart way i.e. the pragmatic way, or there is the meta-meta-meta programming way.
language_generator.rb
1 langs = %w(ruby java)
2 class LangGen
3 end
4
5 langs.each do |lang|
6 LangGen.class_eval <<-LETS_DANCE
7 $first = true
8 $init = ""
9
10 def #{lang}_class_start(name)
11 out = "class " + eval(\"name.chomp\") + ' '
12 out << "{" if '#{lang}' == 'java'
13 out << "\n"
14 out
15 end
16
17 def #{lang}_class_end
18 out = ""
19 out << "\n}" if '#{lang}' == 'java'
20 if '#{lang}' == 'ruby'
21 out << $init
22 out << " end"
23 out << "\nend"
24 end
25 out
26 end
27
28 def #{lang}_members(name, type, value)
29 out = ""
30 if '#{lang}' == 'java'
31 value ||= 'null'
32 out << " private " + eval(\"type\")+' '+eval(\"name\")+' = '+
33 eval(\"value\")+";\n"
34 end
35
36 out << "" if '#{lang}' == 'ruby'
37 out
38 end
39
40 def #{lang}_accessors(name, type, value)
41 accessors = ""
42 if '#{lang}' == 'java'
43 accessors << " public " + eval(\"type\")+
44 " get"+eval(\"name.capitalize\")+" {\n"
45 accessors << " return "+eval(\"name\")+";\n"
46 accessors << " }\n"
47 accessors << " public void set"+eval(\"name.capitalize\")+ '('+
48 eval(\"type\")+ ' '+ eval(\"name\")+")\n"
49 accessors << " this."+eval(\"name\")+" = "+eval(\"name\")+";\n"
50 accessors << " }\n\n"
51 end
52 if '#{lang}' == 'ruby'
53 value ||= 'nil'
54 if $first
55 $init << "\n def initialize \n"
56 accessors << " attr_accessor :" + eval(\"name\")
57 end
58 accessors << ", :" + eval(\"name\") unless $first
59 $init << ' @'+ eval(\"name\")+' = '+eval(\"value\")+"\n" unless $first
60 $first = false
61 end
62 accessors
63 end
64
65 LETS_DANCE
66 end
67
68 gen = LangGen.new
69 langs.each do |lang|
70 File.open('Shoe.txt').each do |line|
71 if line =~ /^C/
72 print gen.send("#{lang}_class_start".to_sym, line.gsub(/^C\s+/,''))
73 end
74 print gen.send("#{lang}_class_end".to_sym) if line =~ /^E/
75 if line =~ /^M\s+(\w+)\s+(\w+)\s+(\w+)?/
76 name, type, value = $1, $2, $3
77 print gen.send("#{lang}_members".to_sym, name, type, value)
78 print gen.send("#{lang}_accessors".to_sym, name, type, value)
79 end
80 end
81 printf("\n"+'*'*50 + "\n")
82 end
Tuesday, August 28, 2007
"Ruby off the Rails?" quid pro quo
I came across Ruby off the Rails? by Paul Turner and he issued a challenge at the end of his post, and I bit :)
Read it here (including the comments) then come back for my some-what larger response.
Begin communique:
I agree with you that Microsoft adding more and more languages to the framework is a bad idea. J# anyone? . Their ploy is to lower barrier to entry, so what you end up with is some developers using anything but C#, because, well why bother learning it if you don't have to. Have you tried proposing C#.NET to a VB developer when they can use VB.NET? I have, it's not fun. It might just be me but I question the maintainability of an application written in 7 different languages.
To answer your question:
Read the following with the caveat of using the "right tool for the right job". Obviously if you are targetting a windows desktop app, it is hard to trump WinForms. However i think .NET is licked in every other category.
I can safely say that you are the first Microsoft proponent that I have come across that knows the difference between open-source and open-standard :). Some people claim that .NET is open-source. No; it's a huge difference. Java is open-source: http://www.sun.com/2006-1113/feature/. Ruby is open-source: http://www.ruby-lang.org/en/LICENSE.txt. But you hit the nail on the head C# is an open standard: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf.
This might not be a kicker for a lot of people, but it is to me. I like being able to delve into source and see what's going on and change it to suit me. This is how i found a bug in the Sun implementation of the CachedRowSet, i thought i was going insane but i was able to get in there and figure why it wasn't working and then report it. Kudos for the open-standard, but one up it and go the whole hog.
I was also working as a government employee when I discovered that bug. If the source hadn't been open I would not have been able to temporarily patch it and get on with what I was doing.
I also have to disagree about the 'purity' of keeping SQL code in SQL Server. It's fine if you have resigned yourself to paying for SQL Server subscriptions the rest of your life. But i'd like to bet on a bit of flexibility and write ANSI compliant SQL that i could quite easily drop into Postgres or any other db for that matter. Ofcourse there are extenuating circumstances (performance reasons and the like) to dabble in sql server extensions, but it is a rarity. I wish i also had the energy to go the age old 'dynamic sql vs stored procedures' argument with you too :)
Rather than bemoan the fact that is IronRuby, maybe have a look at it, who knows you might give up your C# day job :)
Read it here (including the comments) then come back for my some-what larger response.
Begin communique:
I agree with you that Microsoft adding more and more languages to the framework is a bad idea. J# anyone? . Their ploy is to lower barrier to entry, so what you end up with is some developers using anything but C#, because, well why bother learning it if you don't have to. Have you tried proposing C#.NET to a VB developer when they can use VB.NET? I have, it's not fun. It might just be me but I question the maintainability of an application written in 7 different languages.
To answer your question:
Read the following with the caveat of using the "right tool for the right job". Obviously if you are targetting a windows desktop app, it is hard to trump WinForms. However i think .NET is licked in every other category.
- Portability (Mono still doesn't implement the full .NET 2.0 API)
- Expressiveness of language:
- Cost $$$$$$$$$$$$$$$
- MSDN documentation is, well crap, contrast:
- Closed source / Open standard
1 for (int i = 0; i < 5; i++) {
2 Console.Out.Write("c# loop ");
3 }
2 Console.Out.Write("c# loop ");
3 }
vs.
1 5.times { print "ruby loop " }
http://msdn2.microsoft.com/en-us/library/system.io.file(vs.85).aspx
and
http://java.sun.com/javase/6/docs/api/java/io/File.html
I can safely say that you are the first Microsoft proponent that I have come across that knows the difference between open-source and open-standard :). Some people claim that .NET is open-source. No; it's a huge difference. Java is open-source: http://www.sun.com/2006-1113/feature/. Ruby is open-source: http://www.ruby-lang.org/en/LICENSE.txt. But you hit the nail on the head C# is an open standard: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf.
This might not be a kicker for a lot of people, but it is to me. I like being able to delve into source and see what's going on and change it to suit me. This is how i found a bug in the Sun implementation of the CachedRowSet, i thought i was going insane but i was able to get in there and figure why it wasn't working and then report it. Kudos for the open-standard, but one up it and go the whole hog.
I was also working as a government employee when I discovered that bug. If the source hadn't been open I would not have been able to temporarily patch it and get on with what I was doing.
I also have to disagree about the 'purity' of keeping SQL code in SQL Server. It's fine if you have resigned yourself to paying for SQL Server subscriptions the rest of your life. But i'd like to bet on a bit of flexibility and write ANSI compliant SQL that i could quite easily drop into Postgres or any other db for that matter. Ofcourse there are extenuating circumstances (performance reasons and the like) to dabble in sql server extensions, but it is a rarity. I wish i also had the energy to go the age old 'dynamic sql vs stored procedures' argument with you too :)
Rather than bemoan the fact that is IronRuby, maybe have a look at it, who knows you might give up your C# day job :)
Friday, August 17, 2007
Automating SVN stuff with Ruby
Ahoy hoy.
The other day at work I was looking at upgrading an SVN installation at an off-site and came across a good 70 odd repositories dumped to backup. Rather than take the typical windows muppet route and unzip each one, create an svn directory and import them manually, I scripted the whole thing.
Ruby lends itself to this sort of work. I've used it in the past to automate daily and weekly backups of another repository so I knew it would be pretty easy to sort this out. The best part is it didn't take more than 15 minutes from go to whoah. Here it is in it's entirety, it's not a shining example of best practice, nor does it profess to be, but it does what it's supposed to do.
1 ################################################################################
2 #
3 # SVN importer
4 #
5 # rubyzip must be installed: gem install rubyzip
6 #
7 ################################################################################
8
9 require 'zip/zipfilesystem'
10
11 SVN_REPOS_URL = "http://paleale:8090/svn-repos/"
12 SVN_REPOS_PATH = "C:/svn-repos"
13 BACKUP_DIR = "//sparkling/SVNbackups/"
14 UNZIP_DIR = "C:/Documents and Settings/dan/My Documents/"
15
16 # unzip all the dump files into the UNZIP_DIR
17 # we make the assumption that the dump file inside the zip shares the same name
18 # i.e. web_indicator.dump.zip has web.indicator.dump inside it
19 Dir["#{BACKUP_DIR}*/*.zip"].each do |zipFile|
20 dumpFile = zipFile.gsub(/^.*\//,'').gsub(/\.zip/,'')
21 Zip::ZipFile.open(zipFile) do |unZip|
22 unZip.extract(dumpFile, UNZIP_DIR + dumpFile)
23 end
24 end
25
26 # create svn directories for each dumpFile, load into SVN then delete the file
27 Dir.glob("#{UNZIP_DIR}*.dump").each do |dumpFile|
28 projName = dumpFile.gsub(/^.*\//,'').gsub(/\.dump/,'')
29 cmd = %{svn mkdir -m "automated: creating project structure from import" }
30 cmd << SVN_REPOS_URL << projName
31 %x"#{cmd}"
32 cmd = "svnadmin load --parent-dir #{projName} #{SVN_REPOS_PATH}"
33 cmd << %{ < "#{dumpFile}"}
34 %x"#{cmd}"
35 File.delete(dumpFile)
36 end
The other day at work I was looking at upgrading an SVN installation at an off-site and came across a good 70 odd repositories dumped to backup. Rather than take the typical windows muppet route and unzip each one, create an svn directory and import them manually, I scripted the whole thing.
Ruby lends itself to this sort of work. I've used it in the past to automate daily and weekly backups of another repository so I knew it would be pretty easy to sort this out. The best part is it didn't take more than 15 minutes from go to whoah. Here it is in it's entirety, it's not a shining example of best practice, nor does it profess to be, but it does what it's supposed to do.
1 ################################################################################
2 #
3 # SVN importer
4 #
5 # rubyzip must be installed: gem install rubyzip
6 #
7 ################################################################################
8
9 require 'zip/zipfilesystem'
10
11 SVN_REPOS_URL = "http://paleale:8090/svn-repos/"
12 SVN_REPOS_PATH = "C:/svn-repos"
13 BACKUP_DIR = "//sparkling/SVNbackups/"
14 UNZIP_DIR = "C:/Documents and Settings/dan/My Documents/"
15
16 # unzip all the dump files into the UNZIP_DIR
17 # we make the assumption that the dump file inside the zip shares the same name
18 # i.e. web_indicator.dump.zip has web.indicator.dump inside it
19 Dir["#{BACKUP_DIR}*/*.zip"].each do |zipFile|
20 dumpFile = zipFile.gsub(/^.*\//,'').gsub(/\.zip/,'')
21 Zip::ZipFile.open(zipFile) do |unZip|
22 unZip.extract(dumpFile, UNZIP_DIR + dumpFile)
23 end
24 end
25
26 # create svn directories for each dumpFile, load into SVN then delete the file
27 Dir.glob("#{UNZIP_DIR}*.dump").each do |dumpFile|
28 projName = dumpFile.gsub(/^.*\//,'').gsub(/\.dump/,'')
29 cmd = %{svn mkdir -m "automated: creating project structure from import" }
30 cmd << SVN_REPOS_URL << projName
31 %x"#{cmd}"
32 cmd = "svnadmin load --parent-dir #{projName} #{SVN_REPOS_PATH}"
33 cmd << %{ < "#{dumpFile}"}
34 %x"#{cmd}"
35 File.delete(dumpFile)
36 end
Monday, August 13, 2007
Linux eye for the VMS guy
Recently I traded my comfy life with Fedora and Solaris for Windows and VMS. Yes, I'm crazy, but I wanted to see how the other half live, plus it's what they use at my new job.
What I propose to do is make my VMS life as cruisy as possible. Therefore I'm trying to map as many *nix commands as i can to DCL. If you don't know what a LOGIN.COM file is, it's analogous to your .bashrc if you use the BASH shell. It also funnily enough gets executed when you login. My first triumph was getting VIM installed, thanks to this guy for building the binary, absolute champion.
Here is my login.com, no doubt it will grow and i'll try and update it as i add stuff.
1 $! LOGIN.COM
2 $!
3 $!
4 $ set noon
5 $ IF F$MODE().NES. "INTERACTIVE" THEN GOTO BATCH_LOGIN
6 $ vt200
7 $ set term/inq
8 $ set term/color
9 $ set control = "Y"
10 $ set prompt="Dan$ "
11 $! VIM
12 $ define /nolog VIM dev_users:[dan.VIM]VIM.EXE
13 $ define /nolog VIMRUNTIME dev_users:[dan.VIM.VIM71]
14 $ define /nolog TMP SYS$SCRATCH
15 $ vim :== $dev_users:[dan.VIM]VIM.EXE
16 $! Other UNIX stuff
17 $ who == show users
18 $ date == show time
19 $ ls == "dir/size/prot/date=create/width=(filename=28,size=5)"
20 $ pwd == "show default"
21 $ clear =="type/page nl:"
22 $ more == "type/page"
23 $ cat == "type/nopage"
24 $ cd == "set default"
25 $ top == "monitor process/topcpu/interval=10"
26 $!
27 $ set protection = (s:rwed,o:rwed,g:e,w)/default
28 $ EXIT
What I propose to do is make my VMS life as cruisy as possible. Therefore I'm trying to map as many *nix commands as i can to DCL. If you don't know what a LOGIN.COM file is, it's analogous to your .bashrc if you use the BASH shell. It also funnily enough gets executed when you login. My first triumph was getting VIM installed, thanks to this guy for building the binary, absolute champion.
Here is my login.com, no doubt it will grow and i'll try and update it as i add stuff.
1 $! LOGIN.COM
2 $!
3 $!
4 $ set noon
5 $ IF F$MODE().NES. "INTERACTIVE" THEN GOTO BATCH_LOGIN
6 $ vt200
7 $ set term/inq
8 $ set term/color
9 $ set control = "Y"
10 $ set prompt="Dan$ "
11 $! VIM
12 $ define /nolog VIM dev_users:[dan.VIM]VIM.EXE
13 $ define /nolog VIMRUNTIME dev_users:[dan.VIM.VIM71]
14 $ define /nolog TMP SYS$SCRATCH
15 $ vim :== $dev_users:[dan.VIM]VIM.EXE
16 $! Other UNIX stuff
17 $ who == show users
18 $ date == show time
19 $ ls == "dir/size/prot/date=create/width=(filename=28,size=5)"
20 $ pwd == "show default"
21 $ clear =="type/page nl:"
22 $ more == "type/page"
23 $ cat == "type/nopage"
24 $ cd == "set default"
25 $ top == "monitor process/topcpu/interval=10"
26 $!
27 $ set protection = (s:rwed,o:rwed,g:e,w)/default
28 $ EXIT
Wednesday, August 1, 2007
Pander to familiarity
The first step of getting out of the familiarity trap is to feel confident in sizing up what else is on offer.
A simple set of criteria as defined by Sebesta in 'Concepts of Programming Languages' includes:
For example:
That looping construct makes sense to a veritable legion of programmers (pick your printf or println statement). Is it readable and writable because you've done it a gazillion times or is it inherently simple?
Contrast that with:
If you pulled a layman off the street and showed them both I could take a guess at which one would be considered more readable.
In Steve Yegge's (in)famous next big language post his number #1 rule for the next triumphant language is 'C like syntax'. I can tell he feels oh so dirty saying that, but 'you gotta give the programmers what they want'.
Things like orthogonality, control structures and data types don't seem to rank as highly. Whatever happened to the best tool for the job? If I know there is something out there that works I don't care whether it's functional, applicative, imperative, logical, whatever. It's just a side-effect. If you have a brain in your head, you can work it out.
A simple set of criteria as defined by Sebesta in 'Concepts of Programming Languages' includes:
- Readability
- Writability
- Reliability
For example:
1 for (int i = 0; i < 5; i++) {
2 Console.Out.Write("hi ");
3 }
2 Console.Out.Write("hi ");
3 }
That looping construct makes sense to a veritable legion of programmers (pick your printf or println statement). Is it readable and writable because you've done it a gazillion times or is it inherently simple?
Contrast that with:
1 5.times do
2 print 'hi '
3 end
2 print 'hi '
3 end
If you pulled a layman off the street and showed them both I could take a guess at which one would be considered more readable.
In Steve Yegge's (in)famous next big language post his number #1 rule for the next triumphant language is 'C like syntax'. I can tell he feels oh so dirty saying that, but 'you gotta give the programmers what they want'.
Things like orthogonality, control structures and data types don't seem to rank as highly. Whatever happened to the best tool for the job? If I know there is something out there that works I don't care whether it's functional, applicative, imperative, logical, whatever. It's just a side-effect. If you have a brain in your head, you can work it out.
Friday, July 6, 2007
Big O Notation
There is something compelling in knowing that you've figured something out; not just for the sake of solving it, but understanding it to the point of not doubting yourself.
Complexity notation is one of those things that helps you determine how long something will take you in proportion to how much of it there is.
This is useful in that you can gauge the most amount of work (worst case scenario) for a task that you will ever have to do.
Complexity can be of any order but here is what you're generally up against, for a problem where n is your input size.
O(1): Order 1: This is the holy grail. Regardless of how much work you have, it will always take you the same amount of time to complete.
O(n): Order n: This is usually most common and linear, basically the amount of time it will take you to do your work is proportional to the amount of work you have on your plate.
O(n²): Order n squared: This is where shit may well and truly have hit the fan; the time it will take you to do you work is growing beyond the rate at which it is landing on your plate. This might be manageable if it's a small amount of work but it is going to hurt if you can't knock it down quick.
Big O Notation is usually used for describing algorthimic optimisations but I think it has applications that transcend far beyond any one discipline. In one way or another it helps my fragile little mind know which fights to pick and which to flee.
Complexity notation is one of those things that helps you determine how long something will take you in proportion to how much of it there is.
This is useful in that you can gauge the most amount of work (worst case scenario) for a task that you will ever have to do.
Complexity can be of any order but here is what you're generally up against, for a problem where n is your input size.
O(1): Order 1: This is the holy grail. Regardless of how much work you have, it will always take you the same amount of time to complete.
O(n): Order n: This is usually most common and linear, basically the amount of time it will take you to do your work is proportional to the amount of work you have on your plate.
O(n²): Order n squared: This is where shit may well and truly have hit the fan; the time it will take you to do you work is growing beyond the rate at which it is landing on your plate. This might be manageable if it's a small amount of work but it is going to hurt if you can't knock it down quick.
Big O Notation is usually used for describing algorthimic optimisations but I think it has applications that transcend far beyond any one discipline. In one way or another it helps my fragile little mind know which fights to pick and which to flee.
Subscribe to:
Posts (Atom)