Skip to main content

Posts

Migrating Hg Repos with hg-fast-export and Windows Subsystem for Linux

Introduction I prefer Mercurial (hg) to git . I don’t really have any reason for this preference - they both do the same thing, and the user experience for 90% of the use cases is the same. It probably comes from the conditions of the DVCS landscape when I started using these systems. Some of this may have been perception only, but it looked like this: GitHub didn’t have free private repos BitBucket did have free private repos BitBucket was very hg-friendly Joel Spolsky had an amazing tutorial that served as both a how-to for hg as well as a general intro to DVCS hg was much more Windows-friendly than git Since hg was written in python, I felt like extending it would be easier than doing so for git if I ever needed to (admittedly, this is a pretty ridiculous reason) hg felt like a more unified, “coherent” system than the very linux-y feeling git and its extensions (also pretty ridiculous) Where they differed, I liked the verbs hg used better than git’s counterparts ...

Reminder to Self

You don’t always have to use an existing framework/toolset/platform to accomplish a common task. You’re a coder. You can code it. I was reminded of this in two different scenarios recently. Unit Testing I’ve been taking a lot of algorithmic coding tests for potential employers on sites like HackerRank , Codility , CodeSignal , etc. Usually it works like this: You have a problem: given these types of inputs, produce this type of output You are given constraints for the inputs You are given example inputs and the expected output There is some scaffolding code to read the input and print the output There is some way for you to enter custom input for testing Sometimes there are some basic test scenarios that you can see that your code will go through Always there are unseen test scenarios that your code will go through One big problem here is that when trying to test (and retest, and remember) all of the edge cases, manually entering custom input data isn’t very efficient or r...

Enabling Globalization Invariant Mode for .NET Core App on Raspberry Pi Running LibreElec

I had an app I wanted to run on my Raspberry Pi 3 running LibreElec . In LibreElec you can install the dotnet core 2.2 runtime as an addon, and in Visual Studio you can compile for ARM processors with ‘Target Runtime’ set to ‘linux-arm’ in the publish profile. So, I published to a folder from VS using that profile, and I copied the output over to my RPi which had the dotnet runtime installed. I did a simple dotnet Whatever.dll to run the app (actually in this case, it was /storage/.kodi/addons/tools.dotnet-runtime/bin/dotnet Whatever.dll because of the way the addon is installed) and was met with this error: FailFast: Couldn't find a valid ICU package installed on the system. Set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support. at System.Environment.FailFast(System.String) at System.Globalization.GlobalizationMode.GetGlobalizationInvariantMode() at System.Globalization.GlobalizationMode..cctor() at Syste...

C#-like Events in Python

I've been experimenting with Python recently for a few different reasons: It's fun to learn new things I'm planning to build a site for a friend on top of Django and Wagtail I have some ideas for Kodi Addons I had the need to implement an observer pattern for decoupled publish/subscribe messaging between independent Python modules. C# has this built in to the language with events. I found  this Stack Overflow post where user  Jason Orendorff 's  answer provided the following excellent decorator: This is difficult at first to understand as a Python noob, but once you get it you appreciate the simplicity and the power the language has. Starting at the bottom, the boundevent class stores the event handlers and iterates over them when the function is invoked (i.e., the event is 'raised'). The event decorator exposes the boundevent (that is stored on the object instance) for a given event (which is really just a method) when it is accessed via the '...

Whatever Happened to Pong ... err ... SharpMock?

Just short of four years ago, I came up with an idea for replacing static/sealed methods in legacy code (the Michael Feathers kind) with fakes for testing purposes. Here's a quick rundown of what happened after I had that idea: Messed my brain up trying, for several hours over the course of many days, to figure out how to bend PostSharp to my will Left it alone for months Came back to it and spent several hours re-learning what I needed to know Left it alone for months Came back to it again and spent several hours re-learning what I needed to know Left it alone for months Came back to it and decided to try using LinFu as the AOP framework driving things Had what seemed like a pretty decent prototype working based on a dozen or so unit tests that fell apart under a little more weight Gave up on LinFu and left it alone for a few months Started over using Microsoft CCI Code Model to create my own specialized AOP framework to drive the mocking framework Made minor advan...

Exception Handling? WTF??

A recent Daily WTF explored a Guard class which throws an ArgumentNullException if a parameter is null. Most of the commenters correctly point out that the code is not a WTF but rather a good practice because it Enforces the code contract - this parameter is not allowed to be null Provides better stack trace information Causes the app to fail fast One principle at work here is the idea of adding information to an exception. Without guarding against potentially invalid input in this situation, a NullReferenceException will (presumably) be thrown at some point. But why? What's null? What made it null? Even with the stack trace, this can be painful to debug... annoying at the least. An ArgumentNullException cuts straight to the problem and tells us exactly what is wrong. Anytime you can add information to an exception (or potential exception in this case), you should do it* . I think this is a good principle to follow when deciding whether or not to catch an exception (t...

Considerations for Multi-Client Applications

Often when dealing with applications/systems that serve multiple clients, we encounter a situation where Clients share some core functionality and algorithms Clients have custom logic that extends the core functionality Clients have customized data structures that extend core data structures The key to making this work in a maintainable way (that is, without infusing the code with crazy branching statements or writing the whole application in dynamic SQL) is maintaining a distinct separation between core and client. Here I present components from a simple, hypothetical application that demonstrates such a separation. The application is meant to evaluate whether or not a client should acquire a player for fantasy football. It contains some core functionality for doing the evaluation (it doesn't pick up a player who has never scored a point) and a core data structure that represents a player. There are two clients who extend this core functionality: me and a stupid ...

Duck Typing with Anonymous Types - Part 3

Continuing my short series , it's time to add some polish to what we have so far. Overload Resolution At first I thought overload resolution was going to be a huge pain, but really the compiler does everything for us. As long as we have assigned an implementation with exactly matching parameters for the method that the compiler calls, we're good to go. The necessary modifications to the interceptor fit into a small reflection utility method. private Delegate GetMethod(IInvocation invocation) { var methodProperty = anonymous.GetPropertyValue(invocation.Method.Name); if (methodProperty is Delegate) { return methodProperty as Delegate; } var overloads = methodProperty as Delegate[]; return overloads.Where(m => m.Method.SignatureMatches(invocation.Method)).First(); } internal static bool SignatureMatches(this MethodInfo method, MethodInfo other) { var theseParameters = method.GetParameters(); var thoseParameters = other.GetParameters(); ...

Duck Typing with Anonymous Types - Part 2

Previously I looked at forwarding property calls of an interface to those of an anonymous type instance. Now I'd like to see if we can forward method calls as well. But how? We can't create an anonymous type instance with methods, and we can't assign assign anonymous methods as properties. This doesn't work: var duck = new { Color = "white", Quack = () => "Quack!" }; but this does: [Test] public void CanInvokeMethod() { var duck = new { Color = "white", Quack = (Func<string>)(() => "Quack!") }; } Let's modify our interceptor once again, this time to retrieve a delegate property named the same as the invoked method and invoke it. internal class DuckTypingInterceptor : IInterceptor { [... snip ...] public void Intercept(IInvocation invocation) { [... snip ...] if (invocation.IsMethodCall()) { var method = GetMethod(invocation.Metho...

Duck Typing with Anonymous Types

If you can create an anonymous method and an anonymous type , why not an 'anonymous class'? Wouldn't it be nice to be able to plug properties and methods into and object and then consume that object like any other class or interface ? What we're talking about is essentially duck typing (with strongly-typed ducks). There are examples all over of how to do this, especially with Castle Dynamic Proxy. This is my implementation of the same. First let's look at a test: public interface IDuck { string Color { get; } Direction Fly(); Direction Fly(Direction direction); string Quack(); } [Test] public void CanStubColorProperty() { var rubberDuck = new { Color = "yellow" }; var typedRubberDuck = rubberDuck.As<IDuck>(); Assert.That(typedRubberDuck.Color == "yellow"); } That As<IDuck> part is an extension method that does nothing right now, so this test will fail. What we want that extensions method to do is to re...

Mercurial / hg-git / Github Setup Link

This is just so I don't forget or lose it: this post has probably the best guide to getting TortoiseHg set up as a github client on Windows: james mckay dot net - TortoiseHg as a github client on Windows Apparently I don't know how to use Google, because I always struggle to find this. UPDATE: Be careful not to wrap the hggit extension path in quotes in the mercurial config file *even if the path has a directory with a space in it*. I was setting mine like this: [extensions] hggit = "C:\ABC DEF\hg-git\hggit" but that was causing this error: *** failed to import extension hggit from "C:\ABC DEF\hg-git\hggit": [Errno 22] Invalid argument Removing the quotes fixed the problem: [extensions] hggit = C:\ABC DEF\hg-git\hggit

Serializing Anonymous Methods

I’m taking some time off from not blogging to do a little blogging. I hope this doesn’t inconvenience absolutely nobody. I was doing some [binary] serialization work recently when I came across a problem – I wanted to serialize objects with delegate fields that were populated with anonymous methods at runtime. To wit, I had types like this: public delegate void MakeMove(); public class AdrianPeterson { public int GameOneYards { get; set; } public Football Ball { get; set; } public MakeMove Move { get; set; } } Populated like this: var explicitDirections = new List<string> { "left", "right", "left" }; var ap = new AdrianPeterson(); var apName = ap.GetType().Name; ap.GameOneYards = 87; ap.Move = () => Moves.Weave(apName, explicitDirections); So, I pop a [ Serializable ] on AdrianPeterson (and Football ), and I’m set, right? Wrong. Wrong like getting away from running AP in the second half when the only receiving threat you have is bei...

Micro Fluency

I’ve been having a lot of conversations at work lately about how comments are a code smell , and if you’re not writing self-documenting code , ur doin it rong . Then I found myself in a position I’ve been in quite a few times… The general description is this: I have a method that takes two parameters. The action the method performs is best written in English in the form “{SomethingTheObjectDoesWith} {parameter1} {On/With/For/SomeOtherPreposition} {parameter2}”. The only way to name the method is “ SomethingTheObjectDoesOn/With/For(parameter1, parameter2)” . It seems like I run into this situation too often. The API for all of the concerned code doesn’t necessitate building out a huge fluent interface for readability, but it would be nice to have things read just a little better in this case. An example is certainly in order. I have an ASP.NET server control that activates a certain index on an associated MultiView . In order to let client code know whether or not it activates an in...

When an Object Isn’t an Object

I really wish there were more guidance on how to handle static reflection situations. Some kind of helper library would be nice. I’m actually surprised one doesn’t exist given its usefulness and the quirks involved. Consider this scenario – you’re reflecting over the details of a method call and the parameters passed. You actually care about the values that have been passed. How do you discover them? It goes something like this, I think: var argument = GetExpression(); if (argument.Type.IsValueType) { argument = Expression.Convert(argument, typeof(object)); } var getValueExpression = Expression.Lambda<Func<object>>(argument); var getValue = getValueExpression.Compile(); var value = getValue(); I found that here . If you don’t explicitly convert the value type to an object, you’ll get an exception that will confuse you (something to the effect of “Expression of type 'System.Decimal' cannot be used for return type 'System.Object'”). It’s confusing bec...

Teaser

These tests all pass: [Test] [ExpectedException(typeof(AssertionException))] public void ThrowsAssertionExceptionForWrongArgument() { Expect.Call(() => Console.WriteLine("A specific string.")); StaticClass.CodeUnderTestThatCallsConsoleWriteLine(); Verify.AllExpectations(); } [Test] public void VerifiesCallWithoutActuallyPerformingTheCall() { Expect.Call(() => Console.WriteLine("You shouldn't see me.")); StaticClass.CodeUnderTestThatCallsConsoleWriteLine(); Verify.AllExpectations(); } [Test] public void PerformsAlternativeAction() { Expect.Call(() => Console.WriteLine("YOU SHOULDN'T SEE THIS")).IgnoreArguments() .Do(() => Console.WriteLine("You should see this instead.")); StaticClass.CodeUnderTestThatCallsConsoleWriteLine(); Verify.AllExpectations(); } [Test] public void IgnoresArguments() { Expect.Call(() => Console.WriteLine("")).Ign...

VS Extensibility - It's So Easy!

(Note: This is an old post that was stuck in my backlog. I am publishing it now with the intent of writing more VS extensibility posts.) I have a colleague who laments the desire of developers to have (and tool developers to put) ‘everything in the IDE’. Anyone who has struggled with integrated source control fussing up a solution structure can probably identify with this view to some degree. Still, anyone who has used the NUnit GUI test runner for lack of having ReSharper or TestDriven.NET has had a taste of the case for IDE integration. When building small custom tools to enhance developer productivity and make it easy to perform mechanical tasks, why not stick them in Visual Studio? It turns out, it’s pretty easy to do. Consider a situation in which a developer has user accounts for each tenant of a multitenant application. These accounts get deactivated after a period of non-use, so they need to get reactivated in order for the developer to troubleshoot problems that manifest onl...

Defeating the Purpose of a DSL

First, I neglected to mention in my previous post that I found Paul Cowan's blog to be a tremendous help in figuring out how build a DSL with Boo and Rhino DSL . Second, I wanted to point out that, because this is an internal DSL written in Boo, it's still possible to use familiar programming constructs in the DSL scripts. For example, I could dynamically generate the team definitions from some other datasource, like so: stats = GetData() for row as DataRow in stats.Rows: define row["name"]: defense Determine.DefenseFromRanking(cast(int,row["defenseRanking"])) pass_offense Determine.PassOffenseFromRanking(cast(int,row["passOffenseRanking"])), with_qb_type(Determine.QbTypeFromRatingAndExperience(cast(double,row["qbRating"]), cast(int,row["qbExperience"]))) run_offense Determine.RunOffenseFromRanking(cast(int,row["runOffenseRanking"])) where GetData() returns a DataTable with multiple team statistics, and Det...

Building a Simple Data Definition DSL with Boo

In my daily work, I find myself writing a lot of one-off SQL scripts to handle various data issues. In particular, I've had to tweak existing sets of metadata with scripts that look something like the following: DECLARE @SomeId INT DECLARE @SomeOtherId INT SELECT @SomeId = SomeTableId FROM SomeTable WHERE SomeName = 'Something' SELECT @SomeOtherId = SomeOtherTableid FROM SomeOtherTable WHERE SomeOtherName = 'SomethingElse' IF NOT EXISTS( SELECT 1 FROM SomeMetadataTable WHERE Name = 'MetadataName') BEGIN INSERT INTO SomeMetadataTable (Name, Property1, Property2) VALUES ('MetadataName', @SomeId, @SomeOtherId) END ELSE BEGIN UPDATE SomeMetadataTable SET Property1 = @SomeId , Property2 = @SomeOtherId WHERE Name = 'MetadataName' END Just writing a few of these gets to be tedious, and master script...

A Single-Featured App – Introducing ‘Queue View’

I’ve been working with NServiceBus at work lately, and it’s useful to be able to look at the messages that are landing in the queues. Unfortunately, the MMC snap-in provided by Microsoft is woefully inadequate when it comes to this task. There are some commercial tools available, but I thought I should be able to find a free tool out there somewhere with this seemingly simple functionality. I found Msmq Studio , a nice little app that enables you to do many different queue management tasks both locally and remotely. However, it doesn’t seem to work on my machine now (or any of the servers at work), though it did at one time. I need something reliably do one simple thing , so I decided to take the opportunity to experiment with WPF, a platform for which I’ve never written an app. The result is what I’ve tentatively titled ‘Queue View’:   This app does one thing and one thing only – it shows, in XML format, the contents of messages in private queues on the local machine (assuming, o...