Well, it’s been nice and all, but I’m moving this site off wordpress.com to host it on my own server.
The DNS should cut over in the next few hours … look out for URLs to change, and if you’re subscribed to the RSS feed you’ll need to do that over.
So long and see you on the other side!
Goodbye WordPress
2011-04-29Apple Magic Trackpad for Ubuntu Linux 10.10 Maverick
2011-01-05Well, I’ve decided to declare 2011 the year of finally getting my ergonomics sorted out, and to that end I went and splashed out on a
Kinesis Freestyle split keyboard, and an Apple Magic Trackpad. More on the keyboard later, this post is all about the Magic Trackpad.
Read the rest of this entry »
nontemplate-0.12
2010-09-25Finally got around to pushing nontemplate v0.12 out the door, fixing the most glaring problems with 0.1. It is amazing what eating your own dogfood can do for you
. It is still pre-alpha, and there’s still some bigger changes I’m thinking about for 0.2, but I’m pretty happy with the general direction at least.
(sort of) first class classes in C#
2010-08-10I find myself writing some C# code while still thinking in Python. One thing in particular caught me out … it seems, at first, that C# doesn’t have first class classes. This is annoying, because I’d started writing some device driver classes where each class is a type of device, and instances represent the individual devices themselves. And I wanted to construct a list of these classes, and call a “probe” classmethod on each of them to ask the class to go search out any devices which were available. In Python, this would look like:
device_classes = (FooDevice, BarDevice, BazDevice)
for device_class in device_classes:
device_class.probe()
See? The classes are being treated just like any other variable, because they are, they’re just instances of type ‘classobj’ . But the equivalent doesn’t work in C# — doing this:
Type[] DeviceClasses = {
FooDevice,
BarDevice,
BazDevice
};
… complains that “‘FooDevice’ is a ‘type’ but is used like a ‘variable’”. At first it seemed that C# didn’t have first class classes, and indeed a few web searches came up empty handed.
Thankfully after a bit more exploration it turns out that all that is needed is some syntactic nastiness … namely, typeof(), GetMethod() and Invoke() (Passing “null” to Invoke works for static methods).
Type[] DeviceClasses = {
typeof(FooDevice),
typeof(BarDevice),
typeof(BazDevice)
};
foreach (Type dct in DeviceClasses) {
dct.GetMethod("Probe").Invoke(null, new object[] {} );
}
Now, quite why a shiny new programming language has to get saddled with such godawful syntax is a bit beyond me, but so it goes.
As always, this is lovingly documented in MSDN, in such a way that the answer is clear so long as you already know what you’re looking for.
(As a bonus, yes, you can use reflection to find the list of Devices in the first place. It just wasn’t all that relevant to this example)
Hyperterminal ‘send text file’ eats ASCII LF / 0x0A
2010-07-31Hyperterminal “send text file” eats ASCII LF / 0x0A …
I stumbled across this problem because I was reading a protocol document for a device connected by RS-232. The document described what bytes to put in a text file, and how to use Hyperterminal’s “send text file” to send them. That all worked great. But as soon as I tried to get it working in C#, nothing nada zip. Unfortunately, “Line Feed” (ASCII LF, 0x0A) was one of those bytes.
This problem is mostly suffered by people trying to use Hyperterminal to send files to, eg: a microcontroller. But it looks like I’m not the only one to have stumbled upon this one:
“11 0d 0a 44 4d”
The document specifies it, hyperterminal eats it, and when my code actually sent it the device just ignores the whole message … Leave it out and bingo! The message, no longer corrupt, produces a response. What’s really odd is this: the device, when it replies, uses CRLF as its line separator. This raises the horrible suspicion that the alleged developers of this protocol wanted to send CRLF, found themselves stumped by Hyperterminal, and just gave in and changed the implementation instead of changing the protocol or using an non-broken client …
(if only it was a blood-pressure meter, that’d come in handy about now …)
FreeTDS without udp 1434
2010-07-02Just a quick note for anyone trying to do FreeTDS through a tunnel or a firewall pinhole or whatever: If you specify an Instance name, FreeTDS goes and probes UDP 1434 to determine the port number for that instance, even if you also explicitly specify the TCP port number you want it to use. The problem being that often that UDP connection won’t get through, and FreeTDS will just time out with a “Read from SQL server failed”.
[whatever]
host = whatever.example.com
port = 1433
instance = foo
This isn’t really documented anywhere, and seems very counterintuitive, but if you look in the FreeTDS source, there it is in src/tds/login.c:
if (!IS_TDS50(tds) && !tds_dstr_isempty(&connection->instance_name))
connection->port = tds7_get_instance_port(tds_dstr_cstr(&connection->ip_addr), tds_dstr_cstr(&connection->instance_name));
So now you know. If you don’t specify the instance name, it notices that you’ve specified the port and just goes there directly. Filed it as a bug on FreeTDS, just for fun.
Fibonacci Regex Perversity
2010-06-01Consider these two regex substitutions:
s/fi?b/i/
s/fii(i*)b/f$1bfi$1b/
For those unfamiliar with Perlish regexes: that first one says “replace the string ‘fb’ or ‘fib’ with the string ‘i’”. The second one says “replace a string ‘fiiXb’ with ‘fXbfiXb’, where X is zero or more ‘i’s.”
We can repeatedly apply these rules to a string until the string stops changing. So for example, our string might mutate as follows:
* fiiiiib
* fiiibfiiiib
* fibfiibfiibfiiib
* ifiibfiibfiiib
* ifbfibfbfibfibfiib
* iiiiiifbfib
* iiiiiiii
Expanding the path of fiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiib is left as an exercise to the reader
.
What on earth is this all this substituion doing? Well, it is calculating Fibonacci numbers of course!
Regexes don’t handle arithmetic well, so we represent numbers in unary … a string of n ‘i’s represents the number n. When dealing with unary, you can add numbers by simply appending them. ‘f’ and ‘b’ are like parens around the number we’re calculating the Fibonacci number of. So “iiiii” represents the number 5, and “fiiiiib” represents the fifth Fibonacci number.
So the sequence of strings above could also be written:
* fib(5)
* fib(3)+fib(4)
* fib(1)+fib(2)+fib(2)+fib(3)
* 1+fib(0)+fib(1)+fib(0)+fib(1)+fib(1)+fib(2)
* 6+fib(0)+fib(1)
* 8
So really, any language
that allows a sufficiently powerful regex mechanism is able to calculate Fibonacci numbers.
And it is pretty easy to see how to implement a Turing machine by representing each state transition as a regex substitution, so these languages are bound to be Turing complete as well, even if they do turn out to be Turing tarpits.
I’m quite interested in substitution as a kind of pure functional programming. More on that later.
NonTemplate
2010-04-30It is rather sketchy still, but I’ve just put up a little idea about a way to avoid doing template languages at all, and
embedding HTML into Python code directly instead.
It is called NonTemplate. Let me know what you think!
UPDATE: Some vague performance figures, using the same very simple benchmark as the previous template language performance comparison posts.
These figures are running on Python 2.6.5, on a linux laptop (x64) with output to /dev/null …
print: 1.612
Mako: 1.756
Jinja: 10.803
nontemplate: 18.198
django: 42.212
SimpleTAL: 59.024
genshi: 81.460
… mako is very very clearly the winner here … its code generation is head-and-shoulders above the rest, producing pretty much
exactly the same code as you’d get if you wrote a whole lot of “print” statements yourself. Nontemplate is stuck in the middle … unfortunately, all the ‘with’ shenanigans turns out to be pretty slow. On the other hand, it is still a lot quicker than Django templates, SimpleTAL or Genshi, and a lot smaller than any of them, so I guess it is not all bad news.
Posted by nickzoic 
