Sunday, October 09, 2011

Links for 9-Oct

The world has moved on, have you? Xml APIs you should avoid using. (Microsoft XML Team's WebLog) -- tl;dr = XmlText* classes should be retired in favour of objects minted by Xml*.Create() factories.

5 Awesome Free jQuery Plugins -- toast, sliders, tickers...


What you see vs what you get : matching source and IL with Mono.Cecil and PowerShell

Following on from yesterday, looking at how Mono.Cecil pairs up debug information in the .pdb file with just the sequence points the IL -- which follows the approach taken by coverage tools such as dot-net-coverage. The code is much the same as before:

with the addition of the line/column data in SequencePoint ("null" if the values are not given, "0xfeefee" for the well-known compiler generated fake line number).


Saturday, October 08, 2011

Launching PowerShell 2.0 into .net4 via F# Interactive or IronPython 2.7

With .net4 being 18 months old, and the Win8/PowerShell 3.0 being both in CTP and only for Win7+, it's a drag having to do things with .net 4 code when you really could do them faster and better in PowerShell. So, why not bootstrap ourselves from out of the box PowerShell 2.0 into the .net 4 world with a little help from another scripting language which is .net 4 aware? This saves messing about with environment activation variables, centralized registry settings or application config files, and takes advantage of the fact that PowerShell, like other scripting languages, can be hosted by another .net process.

The recipe is based around Bart de Smet's Option 2 – Hosting Windows PowerShell yourself.

Via F# interactive:

Via IronPython 2.7 (augmented to take command line arguments via the -Options parameter):

Since we can't pipe into Start-Process, the inner script is written to a temporary file; and then the initial PowerShell process waits so that it can safely delete it. If you would rather, it's reasonable to save the script to a constant .fsx or .py, replace $source with the path to that file, then the -Wait and Remove-Item -Force $source can be removed.

Replacing the fixed command line arguments for the F# interactive launched PowerShell with ones input to the script as per IronPython follows the same pattern. In either case, care has to be taken with getting your quotation marks right, and the IPy example is rather rough-and-ready. Besides, doing appropriate quotation parsing and escaping would obscure the real point of the exercise.

Note: Launching PowerShell directly from the .net4 command line launches PowerShell in the plain old .net 2 configuration; it is, alas, not sticky.

Note: These scripts are intended to give you a .net4 based interactive session; an alternative approach aimed at running individual commands can be found on Jason Stangroome's GitHub.

What you see vs what you get : matching source and IL with FxCop and PowerShell

The mapping between the code you write and the object code that gets generated is not always a simple one; F# code in particular is heavily restructured in the compilation phase. So when trying to perform code analysis to detect source level artefacts in the compiled code, some guide is useful to navigate between the two representations.

So, here is a PowerShell script to do just that, looking at the IL instructions or logical statements for a method and the corresponding source code as given via the .pdb debugging information -- PowerShell rather than F# interactive because the former has a much nicer user interaction model, especially when asking the user to resolve which overloaded method name was really intended.

As the script writes rich objects in the successful output to pipeline, it can be used as a starting point for further analysis; or the output can be piped into e.g. Out-GridView for inspection.

Sunday, October 02, 2011

Summer at last

So, we get the hottest days of the year at the start of October : it's like summer was put on back to front, going from warm, to April showers, to autumn, to high summer.

After clocking up to ~2705 miles in Q3 -- 800 miles in the quarter -- I took a ~40 mile spin on the bike yesterday, with lunch at the Carpenter's Arms in Gt. Wilbraham, then carrying on out past Six Mile Bottom, West Wratting, Balsham and back. With the lack of wind, and low humidity, it was a beguilingly easy ride (apart from the long grinds up out of Six Mile Bottom towards Brinkley; and from West Wratting to Balsham). So easy, that it wasn't obvious quite how much I was sweating, and thus how much I needed to rehydrate, until I finished a 75cl bottle of Lucozade Sport in pretty much one gulp.

In the evening, it was still so warm that we sat out in the garden until well after sunset, something that we never had weather for over the nominal summer.

Thursday, September 22, 2011

Recent reading

Kethani by Eric Brown

Aliens send artefacts to Earth to enslave the human race, as told through the perspective of the inhabitants of a Yorkshire village with an unusually high rate of cancer and suicide. Nobody seems to notice what the aliens are doing, not even the author.

Cosy British SF as it is done at the start of the new century.

Yellow Blue Tibia by Adam Roberts

Cold War nostalgia in a tale of an SF writer given an unusual assignment by Stalin, and then being told to forget everything, which works for the next 30-some years. Spends a long time setting up the mood, then infodumps a not-quite-resolution.

Rocket Girls and Rocket Girls: The Last Planet by Housuke Norjiri

The two light novels (each slim enough to finish during one bathtime) that were the original source of 2007's delightful anime series, now available in English. The anime was a very faithful adaptation of the source material -- just omitting the by then very dated visit to Mir at the end of the first book, and segueing that mission into the one at the start of the second.

Ouroboros Wave by Jyouji Hayashi

A series of almost puzzle-driven hard-SF shorts scattered across several decades in a fairly standard Earth vs space colonists setting -- a very modern execution of something that was at one time almost the backbone of the genre.

Friday, September 02, 2011

A summer evening at last

After work, along with a couple of colleagues, I cycled to the Toft Beer Festival, where we sat at the table outside in the warm evening sunshine and enjoyed the beer, until it came sunset, and time to be heading home.

For the first time this year, the warmth lingered as the light went. It was pleasant cycling in shorts and T-shirt; and even just a few minutes ago, going out to drop tea-bags and apple cores into the compost bin, wearing nothing but a towel around my waist, the paving was warm underfoot, and the air mild long after sunset.

I had in the past come to expect the light to be going before the warmth through July, but not so in recent years. Now that is a most unusual thing to happen until an Indian Summer day like today.


Sunday, August 28, 2011

Cambridge Film Festival

I pretty much started this blog as as a home for "My Cambridge Film Festival film reviews", however much my blogging has mutated over the years. And this weekend, the programme for this year's CFF is out -- and even more than last year, I'm feeling underwhelmed by the torrent of gritty and edgy features, right-on documentaries, and the usual run of "there's adultery in it, so it must be artistic". It's like it was all set out with a ground rule of "No fun allowed, SRS BZNS ONLY". Oh, and they're exhuming and showing the totally abysmal Silent Running (down there near the nadir of my list of the worst movies I've watched). Twice.

I suppose I could go watch The Seventh Seal and The Day the Earth Caught Fire again; but apart from being in two minds about this locally produced film, Dimensions (which could be another Time Crimes, but could equally well be artsy tosh with a few bits of stage dressing like non-genre writers generate when thinking they're doing SF), the new material singularly failed to catch my eye.

Thursday, August 25, 2011

Late summer cycling break

Same sort of gig as last year, only with different weather.


View August 2011 in a larger map

This time I arrived in Great Bircham about 10:30 on a beautiful summer's day, and headed out on the long run I'd meant to do last year, without the interruption by squall lines.

With the later start, I reached Little Walsingham after the first course of packed lunch (stopping for a pint at the Black Lion), and then kept going east as far as Binham (another pint at the Chequers Inn -- which, despite the name, does not seem to offer accommodation, but would have made a good base for another time), then looping back.

Second course of lunch -- cheesy rolls and a beefsteak tomato from the greenhouse -- at 15:00, partway along the coastal cyclepath, before heading for the first time through the grounds of Holkham Hall from the north. I'd steered clear of it, having previously taken other paths from the south which didn't make good cycling.

This side, the paths were properly made, easy going up and over the rolling terrain, then rolling downhill most of the way to Burnham Market; then the grind up the Ringstead road was where my legs started to tire, but still back to base not long after 17:00, having done about 48 miles.

Tuesday was wet, so read, did internet and generally rested up, with a little stroll around the local area late afternoon.

Wednesday was dry, if cool and generally cloudy, so another clockwise loop through the area (with one misstep when the path I'd meant to take was less obvious on the ground than the one I didn't), through lanes I'd mainly not taken before, with the goal of lunch at the Gin Trap Inn at Ringstead.

After their substantial club sandwich and chocolate mousse, I didn't want to take the rather punishing direct route to Sedgeford, but instead detoured out to Heacham, before finally ambling back (with a number of pauses where a full stomach made it more comfortable to get off and push rather than grind an uphill).

So that was about 28 miles; but between the end-of-day fade on Monday, and the push while digesting, my logged average speed went down from a steady 11 mph as it had been all summer, to only 10.8 :(

Monday, August 15, 2011

Powershell -- higher order functions with arguments that take arguments

Dead simple example based on this:

which results in

Name                                                  Prop                                                 
----                                                  ----                                                 
Junku                                                 **junku+junku**                                      

Also -- reminder to self : ShowUI -- WPF in PowerShell

Trivia

At end of day 11-Aug the car was reading 3474 miles; at the hour that I picked it up last year, 3485. Subtracting the 14 that were on the clock when I picked it up, 3460 or 3471 miles -- a lot less than in previous years.

Sunday evening, the bike was showing 2280 miles; 373 miles in the first 6 weeks of the new year, which is in keeping with last year's rate

The new guided busway cyclepath is a great new way into the town centre avoiding traffic, even though I have to do a mile and a bit on bridlepath or detour twice that distance to get onto the cyclepath feeding to it. If only there were paved spurs to the neighbouring villages. And even though the last hundred yards or so at the station end aren't finished yet.


In the garden, the plums have been in full flood for a couple of weeks -- much earlier than the usual Bank Holiday peak I usually plan for; indeed everything has been early, ever since the early onset of last winter.

Friday, August 05, 2011

What do you get when you iterate null?

In most cases, an exception; but in PowerShell you get $null.

Suppose I have folders with 0, 1 and more than 1 file in (called empty, full and two, say) and I want to enumerate the names of the files contained in each, with some general purpose code.

That should be no problem --

and let's write a little reporting function

When we report on the folders in descending order of contained files we get

test (2).txt
test.txt
System.Object[]
2
++test (2).txt++
++test.txt++
-----
test.txt
System.String
8
++test.txt++
-----
Split-Path : Cannot bind argument to parameter 'Path' because it is null.
At C:\Users\Steve\Documents\scratch\Untitled1.ps1:6 char:15
+     Split-Path <<<<  -Leaf $file
    + CategoryInfo          : InvalidData: (:) [Split-Path], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.Split 
   PathCommand
 
You cannot call a method on a null-valued expression.
At C:\Users\Steve\Documents\scratch\Untitled1.ps1:12 char:15
+   $arg.GetType <<<< ().FullName
    + CategoryInfo          : InvalidOperation: (GetType:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
++++

Not happy.

The first error is what you get when you work on the foreach over $null, yielding $null; and calling code has to special-case the single item return -- getting the .Length property of the return is not the number of files! For that replace $arg.Length by $arg | Measure-Object | % {write-host $_.Count} .

So try this

where we fight PowerShell's fragmentation of sequences all the way. Now we get what we expected

test (2).txt
test.txt
System.Object[]
2
++test (2).txt++
++test.txt++
-----
test.txt
System.Object[]
1
++test.txt++
-----
System.Object[]
0
-----

Oh, yeah -- the more PowerShell way of doing all this:

Sunday, July 31, 2011

Links for July

Microsoft Research's new F* language

Node.js 0.5.2 unstable branch now includes Windows

Scala.net reloaded -- still need to try it out with code that broke the old version.

July wrap-up

Well, it's been a busy month, hence quiet on the blogging front, starting out with:

A summer holiday

While Karen was in hospital last year, I noticed a flyer for Vitalise respite care holidays in amongst various papers, and thought "Let's do this next summer."

So we did.

We chose the first week of July at the Netley Waterside centre as a part of the country neither of us had been to for many years, went through all the paperwork about care requirements and doctor's reports, and crossed our fingers about how well we would get on.

In the end, it was wonderful.

It started off a bit fraught, though; as on the outward journey on a too hot and bright day for comfortable travel, as we were going past Tring, I realised that amongst the things I'd forgotten to pack was my wallet. So I just unloaded Karen and all the other bits, and turned around, to drive down again the next day with all the stuff we've forgotten (doing in two days way more miles than I drive in the average month).

Going back home in the heat was not much fun, especially when combined with stumbling into special traffic arrangements for the Henley Regatta just as everyone was heading home for the evening. But at home, I was cheered to get a phone call from Karen, who was enjoying being able to choose her own bed time.

Second time around I was actually able to find a parking space (as holidays run Saturday to Saturday, the first time there were arrivals and departures jostling for space), freshen up and join the Sunday chill-out.

The centre is really on the water's edge

View from the window

with the gardens sloping down to the path that runs along the sea wall, though the path is only wheelchair accessible in one direction.

Monday, we opted out of the excursion to the Sea Life Center, and after I'd scouted out the area, we went along to the local (the Prince Consort) and took advantage of the ramp up to the beer garden at back. to just sit, enjoy a pint and the summer weather, the two of us on holiday together for the first time in years.

Free flying

But that was the end of the good weather; cooler, windier and wet was the order of the day for the rest of the week. Tuesday's expedition to the Hawk Conservancy Trust at Andover was dampened by the rain arriving as we did, and continuing at varying strength until we left; but despite the weather, the display was spectacular, with sometimes a dozen birds in the air.

Lemur

We did manage to miss the rain for Wednesday's visit to Marwell Zoo, which we only manage to see less than half of; Thursday started wet, so the planned trip to Winchester became a shopping center trip, which we opted out of; and the Friday was an official at-leisure day where we took the opportunity to slope off to the pub (using the industrial strength ramp for the front step) for lunch.

And so, home.

For the holiday, there was the inevitable fact of separate hospital beds for sleeping; the food all home cooked, if unadventurous, with a very early 18:00 dinner time, with breakfast being as much a main meal as that in the evening.

Karen really enjoyed the comparative freedom to choose a bed time -- including at well gone midnight on Thursday, when it was party night.

And for me -- well, when I helped Karen with chasing the last morsels of a meal around a plate, I was told off, that one of the volunteers should do that while I enjoyed a break!

The volunteers are a key part of the operation -- a cheerful, enthusiastic and cosmopolitan crowd from all over the globe, working for bed and board, to just take over much of the step-and-fetch level care, from simply getting a cup of coffee, to pushing manual chairs while accompanying otherwise unaccompanied guests.

So, we'll be back, though next time, assuming I don't have to do the trip 3 times in quick succession, we may opt out of scheduled trips and do our own at our own pace (without being caught up in the half hour or more loading and unloading process).

Trivia -- at just before 10pm on Monday the 4th, I observed a 60-hour old nail-paring crescent moon setting over the port, the sun having set 40 minutes before.

30 years on

And today has been our Pearl wedding. Doesn't time fly!

Logistics being what they are, celebration was lunch, at the recently refurbished Carpenters Arms at Gt. Wilbraham. Being a Sunday, that meant roast, but by being so standard, allowed a like-for-like comparison : big chunks of roast, huge selection of veg, and the best Yorkshire pudding I've eaten in years.

Film — Detective Dee and the Mystery of the Phantom Flame

I this up picked up now it's out here on DVD, having heard of it some months ago.

The film is pure hokum wire-fu, and about as faithful to any of the source material as most movies (about on the level of the wartime Sherlock Holmes vs. Nazi spy films) but entertaining enough for an evening's viewing.

While van Gulik is in the writing credits according to imdb, I saw nothing of what he did by way of exhuming Chinese detective stories (except possibly the fact of popularising Judge Dee in the west) made it into the movie.

Film — Arietty

Seeing this after lunch today, we caught the UK theatrical dub (separate UK production team mentioned in the end credits, and some English regional accents -- most noticeably for the rat catchers -- amongst the cast).

It's been a long long time since I read the original (I have slightly more memories of Adrift and Aloft than of Afield), but the adaptation felt right, though one wonders if it wasn't the speech about the Borrowers dying out that attracted Miyazaki, something to slide in as a soapboxing scene.

Overall, the best thing out of Ghibli since The Cat Returns.

Monday, July 25, 2011

PowerShell strings, ints and comparisons

I hit this doing a little bit of playing around over the weekend, porting some old BASIC progams into PowerShell.

Most of the time treating strings of digits as numbers "just works":

but at times it all goes horribly wrong

with the ASCII value of '5' being 53, we see that these comparisons are being done lexically.

What is needed in these circumstances is an explicit coercion some place down the line; like: