Monday, March 23, 2009

Almost Famous

{

Yours truly, looking embarrassed in the middle.

 

Here is the link to Phil's write up.

}

Friday, March 20, 2009

Voidspace on Dot Net Rocks

{

Michael Foord, aka Voidspace, is on a Dot Net Rocks episode talking about IronPython.

}

Wednesday, March 04, 2009

Currying with C# and Algotron

{

Friend and coworker Algotron just posted an intro and sample of currying with C#. I decided to try out currying with the idea of successive regular expressions on a string array - I found it easy to approach first with anonymous delegate syntax and then use lambda expressions. Once this was in place it was easy to understand the use of an extension method to curry any binary function. 

Func<string, string[], Func<string, string[]>> fil = 
delegate(string pattern, string[] lines){
return delegate(string pattern2)
{
return lines
.Where(p=> Regex.IsMatch(p, pattern))
.Where(p => Regex.IsMatch(p, pattern2))
.ToArray();
};
};


Func<string, string[], Func<string, string[]>> regFil =
(pattern, input) =>
(pattern2) =>
input
.Where(p => Regex.IsMatch(p, pattern))
.Where(p => Regex.IsMatch(p, pattern2))
.ToArray();

// assume some address data
string[] data = new string[]{"1234 Somewhere", "this", "777 dakota"};

var filterNumeric = regFil(@"\d", data); // make sure it has a number
var filterUCase = filterNumeric("[A-Z]"); // make sure it has an uppercase character

Console.Write(String.Join(",", filterUCase));


That's cool stuff, worth looking at for a while to try to understand.



}

Tuesday, March 03, 2009

IronPython + SilverlightFX

{

SilverlightFX is an interesting library for declaratively attaching behaviors to objects in XAML. People new to the Silverlight world will like how they can use it to get simple animations out of simple code. Here's a step by step:

1. If you don't have it done already, set up an IronPython / Silverlight project.

2. Download SilverlightFX

3. Copy SilverlightFX binaries into your /app directory.

3. Edit the AppManifest.xaml to include the SilverlightFX binaries. Here is an example:

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RuntimeVersion="2.0.31005.00"
EntryPointAssembly="Microsoft.Scripting.Silverlight"
EntryPointType="Microsoft.Scripting.Silverlight.DynamicApplication">
<Deployment.Parts>
<!-- Add additional assemblies here -->
<AssemblyPart Source="Microsoft.Scripting.ExtensionAttribute.dll" />
<AssemblyPart Source="Microsoft.Scripting.Silverlight.dll" />
<AssemblyPart Source="Microsoft.Scripting.Core.dll" />
<AssemblyPart Source="Microsoft.Scripting.dll" />
<AssemblyPart Source="IronPython.dll" />
<AssemblyPart Source="IronPython.Modules.dll" />
<AssemblyPart Source="System.Windows.Controls.dll" />
<AssemblyPart Source="System.Windows.Controls.Data.dll" />
<AssemblyPart Source="Silverlight.FX.dll" />
</Deployment.Parts>
</Deployment>


4. Edit your app.xaml file to include references to the SilverlightFX namespaces. Here is an example:



<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="System.Windows.Controls.UserControl"
xmlns:fxui="clr-namespace:Silverlight.FX.UserInterface;assembly=Silverlight.FX"
xmlns:fxeffects="clr-namespace:Silverlight.FX.UserInterface.Effects;assembly=Silverlight.FX"
>


5. Party on with some declarative effects.



<Border Grid.Row="1" x:Name="redRect" Opacity="0.2">
<TextBlock x:Name="outPut" TextWrapping="Wrap"
Text="Lorem ipsum dolor sit amet,..." />
<fxui:Interaction.Behaviors>
<fxui:HoverEffect>
<fxeffects:Fade FadeOpacity="1" />
</fxui:HoverEffect>
</fxui:Interaction.Behaviors>
</Border>


6. Read Nikhil's related blog entries.



}

Thursday, February 26, 2009

DLR + IronPython + Silverlight in 5 Steps (with pictures)

{

A simple step by step1.

1. Download the Silverlight DLR SDK

2. Map the binaries (the folder with Chiron2 et. al.)  in your PATH Environment variable.

3. Download this starter template.

4. Run chiron /w from the command line in the directory of the starter.



a) Make sure you're in the correct folder
b) The command is
chiron /w
c) You'll get a message that Chiron is serving at http://localhost:2060a

5. Navigate to http://localhost:2060 and hit index.html to see what's up.



 
The starter3 should make it easy to get up and running. Open the app.py and app.xaml files and you will be able to modify the xaml and write code to customize it into your own creation.

1. Michael Foord aka Voidspace is the guy when it comes to IronPython. He wrote a book on it. He has a "getting started" tutorial that is much more extensive if you want more details. His tutorial isn't as terse so I thought I'd write this one for people who just want a step by step before poking around. There are other people like JimmyThinking who offer a lot of help to beginners but lean towards IronRuby.

2. The name Chiron for a tool makes me think about a few things: first, being born out of time since most BigCo shops today name computers and tools in a hollow, mechanistic fashion. In the salad days of computing on Unix, tools had cool names like ed or yacc. The second thing I think is that Dmitri (and I'm sure there's an "et. al.") must be a cool, interdisciplinary fellow to come up with a name like that.

3. The exchange in the sample is from Swahili classes at Rusinga Primary School in Nairobi.

}

Wednesday, February 25, 2009

nRegex 9

{

I've been getting up to speed with Silverlight and decided to rewrite nRegex to leverage it as a client side processor for what the original nRegex shipped to the server with AJAX calls.  In the original nRegex this worked fine until you had to deal with large documents but once you were in the large document space things began to go awry. My solution was to allow for a "manual" evalution of the regular expression (rather than a time interval based evaluation) but I'm hoping that Silverlight will prove an elegant solution since not only can it harness the power of the client but it also has features to run code asynchronously.

In my first iteration I leveraged a lot of the JavaScript interop for the Silverlight control and avoided XAML for display. I think in the next iteration I'm going to spend more time on displaying results via XAML.

Feel free to present any comment or suggestion on nRegex and its direction.

}

Thursday, February 19, 2009

DDD

{

Not to be confused with D&D... Domain Driven Design for those of us who don't keep up with all the acronyms.

I'm seeing this more and more in places I lurk. DDD is the next buzz like Agile?

Next steps: relisten to Hanselminutes, ALT.NET Podcast, lurk on DDD site, and pay more attention on the ALT.NET mailing list.

}

Friday, January 02, 2009

Another "Dead Java" Proclamation

{

I was browsing to Elliote Rusty Harold's blog to see if he had any predictions for XML in 2009 and to my surprise I found his post: "Java is Dead. Long Live Python!"

My familiarity with Harold comes from a time in my life where I worked heavily with XML and later Java and it was his books on both subjects that proved invaluable when I hit my rather frequent roadblocks.  As invested as he is in Java, what would make him say as much? In a nutshell, it's the way the language has been implemented over the last few years - claims which, even if you don't agree with the premise that Java is "dead like COBOL", are worth some attention.

What is interesting to me about Java right now is not the language itself but dynamic languages and their platforms built on top of the JVM. JRuby is what comes to mind first and I'm recently interested in what a JRuby/Glassfish world looks and feels like.

}

Wednesday, December 31, 2008

Using SGMLParser With IronPython

{

Mark Pilgrim's excellent Dive Into Python has a section on using SGMLParser and having seen nothing similar (and imagining its many uses!) I thought I'd give it a whirl in IronPython. I thought a good proof of concept would be creating a database out of link heavy sites.  Since I visit Arts & Letters Daily every so often and the closet intellectual in me likes to hang onto what I find there, I thought I'd target it:

import urllib2
import sgmllib
from sgmllib import SGMLParser

import clr
clr.AddReference("System.Data")
clr.AddReference("System.Net")
from System import *
from System.Data import *
from System.Net import *

class AlReader(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.urls = []
self.pieces = []
self.track = 0
self.prePend = "No Category"
self.counter = 0

def start_a(self, attrs):
href = [v for k,v in attrs if k == "href"]
key = [v for k,v in attrs if k == "name"]
if href:
self.urls.extend(href)
self.track = 1
elif key:
self.prePend = attrs[0][1]

def handle_data(self, text):
if self.track:
self.pieces.append("|".join([self.prePend, text]))
self.counter = self.counter + 1

def end_a(self):
self.track = 0

def get_links(self):
links = []
for i in range(0, len(self.urls)):
links.append("|".join([self.pieces[i], self.urls[i]]))
return links
#print "%s %s" % (self.counter, "Total links")

def get_link_datatable(self):
d = DataTable()
d.Columns.Add(DataColumn("Category", Type.GetType("System.String")))
d.Columns.Add(DataColumn("Site", Type.GetType("System.String")))
d.Columns.Add(DataColumn("Url", Type.GetType("System.String")))

for text in self.get_links():
newRow = d.NewRow()
newRow["Category"], newRow["Site"], newRow["Url"] = text.split("|")
d.Rows.Add(newRow)

return d

response = urllib2.urlopen("http://www.aldaily.com")
a = AlReader()
a.feed(response.read())
linkdata = a.get_link_datatable()
# write it out to prove we got it.
ds = DataSet()
ds.Tables.Add(linkdata)
ds.WriteXml("c:\\temp\\arts and letters links.xml")


If you find tihs interesting do make sure you look at Pilgrim's chapter on HTML Processing



}

Saturday, December 20, 2008

Parameterized IN Queries

{

I haven't listened to the podcast yet but saw a cool trick from Joel Spolsky on approaching parameterized IN queries. Purists will bemoan its lack of premature optimization but I think it's novel enough to study because of the approach: using the SQL LIKE operator on your parameter rather than a field, which is what people like me are used to. There's code on the StackOverflow post but I thought I'd paste some of the poking around I did in Sql Management Studio:

-- setup
CREATE TABLE Person
(
PersonID INT IDENTITY(1,1) PRIMARY KEY,
FirstName VARCHAR(50) NULL
)
GO

-- some data
INSERT INTO Person VALUES('David')
INSERT INTO Person VALUES('Jonathan')
INSERT INTO Person VALUES('Zlatan')
INSERT INTO Person VALUES('Trilby')

-- here's the magic
DECLARE @FirstName VARCHAR(50)
SET @FirstName = '|David|Trilby|'
SELECT * FROM Person WHERE @FirstName like '%|' + FirstName + '|%'

-- ported to a proc
CREATE PROC uspPersonSelector
@FirstNames VARCHAR(500)
AS
SELECT * FROM Person WHERE @FirstNames like '%|' + FirstName + '|%'
GO

-- showing it works
uspPersonSelector '|David|Trilby|'

-- somewhere in the netherworld of C#:
/*
string[] names = {"David", "Trilby"};
SqlCommand cmd = GetACommandFromSomewhere();
cmd.Parameters.AddWithValue("@FirstNames", "|".Join(names));
*/

--teardown
Drop Table Person


 



}

Friday, December 19, 2008

Programmers as Goalkeepers

{

The 8th annual New York Times magazine Year in Ideas featured a section on Goalkeeper Science profiling this paper by some Israeli scientists called Action bias among elite soccer goalkeepers: The case of penalty kicks. In looking at the approach of keepers in some 286 penalty kicks they found that though 94 percent of the time they dived to the right or left, the chances of stopping the kick were highest when the goalie stayed in the center. The researchers theorized that the reason keepers behaved in this way was that they were afraid of appearing that they were doing nothing.

Immediately I remembered a blurb from an Paul Graham's What Business Can Learn from Open Source  essay where he expressed a similar dynamic for programmers:

"The other problem with pretend work is that it often looks better than real work. When I'm writing or hacking I spend as much time just thinking as I do actually typing. Half the time I'm sitting drinking a cup of tea, or walking around the neighborhood. This is a critical phase-- this is where ideas come from-- and yet I'd feel guilty doing this in most offices, with everyone else looking busy."

I wonder what Paul would say about the IBM commercial on ideating in which concludes that people should "start doing" after showing an image of people laying inert on an office floor, a stark portrayal of how a manager at IBM might see someone like Paul Graham.

As programmers much of what we should do may not appear to be work for the nonprogrammer and as a result many of us end up doing it at home. I spend a lot of time at home exploring different technologies in a kind of tangential approach that wouldn't look like "working" at work but often my best ideas and solutions come from here.  I also spend a lot of time reading technical books and blogs.

I'm wondering what it would look like if we could step back and look in a quantitative way at the performance deficits resulting from the desire to look busy at work. What would the workday look like? I'm wondering what an hour for reading, a few hours for exploratory/research programming, and the rest as project time would do for my own productivity.

If programming was goalkeeping was programming, Edwin Van der Sar would be quite the Python hacker. 

}

Thursday, December 18, 2008

Windows Forms + Web, WIB part II

{

A while back I made the case for applications that put together the strengths of Windows Forms and Web technologies (I thought of the catchy "WIB" as a name for this approach). The example I’d given then was a Windows Forms hosted Web Browser for local images that one could use for annotation that leveraged Windows for local file storage and a Web technology like jQuery for doing transitions in the user interface.

Today I thought of another use for this approach that wrapped itself nicely into a tool I've been using for some time to download mp3s from a given website1,2. I call the tool "Fortinbras" and if you find it useful I'd be delighted.

So how was Fortinbras changed?

Parsing the HTML for mp3 files was a little tricky. My initial approach was to use a regular expression against the text of the document which, truth be told, is a brittle approach. Part of why I never trumpted the tool was because I never completely perfected this tactic (while it worked well enough for me personally). My code looked as follows:

	WebClient wc = new WebClient();
string pageText = wc.DownloadString(browser.Url.ToString());
Regex re = new Regex("href=\"(?<url>.+?mp3)\"", RegexOptions.IgnoreCase);
Match mp3Matches = re.Match(text);
while (mp3Matches.Success)
{
string matchUrl = mp3Matches.Groups["url"].Value.ToString();
AddMp3(browser.Url.ToString(), data, matchUrl);
mp3Matches = mp3Matches.NextMatch();
}


Today my epiphany was that I didn't need to use a regular expression when I could use the DOM from Windows Forms to pull out the anchors that have mp3 destinations. Here's what that looks like:



	while (browser.Document.Body == null)Application.DoEvents();
HtmlElementCollection anchors = browser.Document.Body.GetElementsByTagName("a");
foreach (HtmlElement anch in anchors)
{
string linkUrl = anch.GetAttribute("href");
if (linkUrl.ToLower().EndsWith("mp3"))
{
AddMp3(browser.Url.ToString(), data, linkUrl);
}
}


As usual feedback is welcome - you can download a copy of the Fortinbras project here.



1I am aware of the Firefox extensions that do this but someday (imagine a pie in the sky look on my face) I was hoping to incorporate a "favorites" list with URLs / locations so that this would be a one stop shop for my downloading and organizing of podcasts. My goal here is embarrasment driven development so I'll probably be bummed enough about the code I've just posted to put in some enhancements as time permits.


2My friend's music blog is a great stop, try a couple of tracks at The Look Back.



}

Wednesday, December 17, 2008

Growing Open Source Community

{

Kevin Dangoor recorded an interesting screencast on some of the essentials of getting an open source project more widely circulated. Essentially Dangoor explains that having a successful open source project is not just about code, it's about good product management. I wanted to title this post with Dangoor's most quotable quote: "Rails is not where it is because of great code." But there are a lot of people who would take that as an opener for a religious war without seeing his real intent of highlighting marketing and management of a project.

I won't rehash it, it's worth watching it for yourself.

}

Tuesday, November 25, 2008

Bruce Eckel's Python Book

{

It escaped my notice but Eckel's been writing a new Python book. Should be worth digging into sometime in the future.

}

Sunday, November 23, 2008

How to steal time: On Programming and being a Father

{

It's been alluded to but here it is neither shaken nor stirred: I became a father exactly on month ago.  While it hasn't been without challenges the joy of being a father is something I understand now firsthand after being told or observing it in others. Even though I first was quite seduced by a "mystique" in programming - a nocturnal person drinking jolt with binary being reflected from mirror shades - I now realize not only that nearly every programmer and professional person I admire is a parent, but that they find their core of happiness in that well beyond the realm of work.

Nevertheless, I have been trying to keep up my personal work and interests so with that goal in mind I've adjusted my schedule to wake up as early as I can (usually 5am) and get a few hours of solid time while leaving the evenings as a time when I can help out and be a "family man."  It still doesn't feel natural but the payoff is that I can still advance my work with IronPython and other interests. It's already been pointed out to me that my productivity here and other places has dropped since my daughter was born.

So question for other programmer/dad people: how do you work the family schedule? Any tips and tricks for a one month father who is still daydreaming about what it was like to sleep all night?

}

Monday, November 10, 2008

Loop Reordering, Optimizing Face Recognition

{

Got this a few weeks ago from a friend but didn't have a chance to sit down and digest the entry until tonight. Pretty cool and although premature optimization gets a bad rap, it's a reminder that sometimes what seems intuitive and quick could actually be costing a lot -

}

Wednesday, November 05, 2008

Lake Wobegon Distribution

{

Not sure where I saw this one first but it's a great article. I wonder if the Lake Wobegon distribution applies as well to programmers...

}

Friday, October 31, 2008

LINQ to SQL death rumors

{

I think it all started with this post from Tim Mallalieu reporting some directions for LINQ to SQL and the ADO.NET Entity Framework. Ayende and others have had some strong reactions (LINQ to SQL is dead) and there's now a StackOverflow thread you can use to follow the discussion.

It will be interesting to track over the next few days what other responses pop up. A few things that come to my mind:

1. This is an opening for people using Linq to SQL to migrate to SubSonic or NHibernate. I've been using SubSonic lately and enjoying myself quite a bit.

2. A big disappointment for people like Benjamin Day who I saw speak at VSLive and who have incoprorated LINQ to SQL into their development projects. Next time wait for Microsoft to 2.0 something before spending too much time working it into your dev cycle.

3. I wonder if Microsoft hasn't thought of spinning off some of the tool makers as different companies. Perhaps there is an MBA out there who could draw some charts and convince management to have ADO.NET Entity Framework and the LINQ to SQL folks compete/copy in the same space. If the team isn't fully formed, why not make one? And then how cool would that be: profit centers that encourage developers to use Microsoft tools and competition to get the best ideas out the door. The trade off of Balkanization versus One True Product is that the former will mean that cool new ideas belong to you - the company won't have to get the ideas from outside sources. 

4. I wonder if we aren't moving towards a tipping point of Microsoft developers "getting" open source and shifting their tool/framework usage outside of Microsoft. I know lots of people who won't use __anything__ unless it has a Microsoft logo because it feels safer and more commoditized. Moves like this seem to go against that since open source projects tend to live a longer time and technologies / frameworks from companies become "obsolete" (how many versions of ADO can you count?) because of a product cycle that requires new purchases every 2 years or so. I would argue with my boss on a new project that it's safer to use NHibernate than ADO.NET Entity Framework if this indicates a chance that the Entity Framework could be the next LINQ to SQL project.

}