Friday, January 27, 2012

TDD as path to sloth

One of the lesser-sung attributes of Test-Driven Development is that it can actually save you development effort.

In TDD, you start by creating a failing test. Then you write code to make it pass. Sometimes the test will not fail. Perhaps you're written the test incorrectly, but other times, the functionality you planned to implement is already implemented elsewhere (e.g. in the framework / library you're using). That means you can skip implementing useless code. Remember, functionality is an asset, but code is a liability.

This happened to me yesterday, when I was refactoring some Launchpad code. Turned out Zope sets the Content-length http header if you don't. It could have happened today, because Zope also encodes Unicode strings to utf-8 for you, but I was unit-testing at a different level, so I implemented, then removed, manual utf-8 encoding.

Wednesday, June 01, 2011

Guessing relevant test modules with Fault Line

Launchpad's test suite takes a long time to run. Far too long to wait for. And the likelihood that you've broken any given test is pretty small. So you want to test a small subset, at least at first.

You can usually guess which tests to run; if you've changed "archive.py", you should probably run "test_archive". But some connections are easier to miss, so I've hacked up a Fault Line, a bzr plugin that uses past changes to guess which test files correlate to the files you've changed. You can run it like so:

bin/test -m $(bzr fault-line --module-regex)

This will look at all the files you've changed, look at their recent history, and see which files tended to change in the last 100 revisions where you changed the specified files. The --module-regex option causes it to output a regular expression, assuming that the test files are modules. Otherwise, it would just output a list of the test files it found.

Thanks to Jelmer Vernoij, this is even easier to achieve for testing bzr. You just need to can run "bzr selftest --auto".

Yes, Fault Line is a hacky, limited tool. But they have their place. Who knows, maybe it will become more general.

Monday, December 20, 2010

Early impressions of the Viewsonic G tablet

I got a tablet
I used to dream about pervasive Internet. I used to dream about using a tablet to read web sites on a streetcar. That future is here, but it's in beta.

More recently, a colleague of mine got an iPad. I think the coolest thing about it is its long battery life. I sometimes take long trips, and I need my entertainment. I deal with jet-lag by staying awake until it's night at my destination, so I'm often too dazed for reading. Video is crucial. On long flights, I bring a laptop, an extra battery and a netbook. Cutting back to a tablet and a laptop would be nice.

I know Android's not really ready for tablets-- in six months, 3.0 devices should be plentiful and wonderful. But I've got travel coming up, and I lost my patience. So I got a Viewsonic G tablet. The premier Android tablet is Samsung's Galaxy Tab, but its screen is 7 inches, and I think that's too small for enjoying video. I considered the Archos 101, but it was hard to find. I didn't find the Advent Vega until after I'd ordered.

So what's the G tablet like? Is it worth it?

Viewing Angle
If you use a landscape orientation, slight changes in viewing angle affect the brightness a lot, but it is easy to find angles that look good. If you use a portrait orientation, each of your eyes has a different viewing angle. The screen has a simultaneously bright-dark look that may be familiar if you've used red-blue 3D glasses or polarized glasses. This makes the tablet an inferior e-book reader, but a decent video player.

Rectangular pixels
The G tablet's 1024x600 screen has slightly rectangular pixels; they are roughly 87:84. This means that the launcher icons, and presumably other raster art, are slightly distorted. It's fairly subtle, though, and I didn't notice for a while. It probably won't affect your enjoyment of the device.

Size and weight
At 1.8 lbs, this is an object that you don't want to hold in one hand. You can do it if you hold the top corner in your fingers and rest the opposite corner against your inner elbow, but it feels like a stunt. It's fine to hold two handed, or reclining and resting it on your body, or lying in bed.

Processor and chipset
Performance is very good in most apps, like Angry Birds or video playback. Bonsai Blast is unusably slow. The G Tablet is based on Nvidia's Tegra 2 chipset, which is rumoured to be the reference platform for Android 3.0, so things can only improve as more software is optimized for Tegra 2.

Software
Before buying the G tablet, I knew that the software it ships with was unusable, so I bought it knowing I would have to install a hacked firmware from xda-developers. That's getting routine. I've also installed OpenWRT on a router and Ubuntu on my new laptop in the past couple of weeks.

I've installed the TnT lite firmware and it works. The Android Market works, and I've got Flash installed. Most things either work or aren't available on the Market. "On Air", a program I sometimes use to wirelessly transfer files, doesn't work. "Tape Machine", which I've used for rehearsal recordings, seems confused about whether I've purchased it or not.

I get a lot of "Force close" messages, though. I think some of it is because it's easy to accidentally hard-reset the tablet, leaving broken data files. The problem is that sometimes the "shutdown menu" takes too long to show up, and if you hold the power button down long enough, it does a hard shutdown (just like a modern desktop or laptop).

Occasionally, the sound stops functioning correctly, and just makes white noise. This is unpleasant, and can't be controlled with the volume control.

One point of confusion with the stock and TnT lite firmwares is that the internal storage location is labelled "sdcard". This made me think it was the (micro-)SD card.

I wish the status bar wasn't on the top; in a 16:9 device, every pixel of vertical real estate is precious.

Ports, etc.
The tablet has two usb ports; one for accessing its storage like a USB stick, and one so that it can access USB devices like USB sticks. It's a shame that neither port can be used for charging, like my Nexus One. Instead, it has a 12V/2A "wall-wart" adapter. I've managed to get it to work with USB sticks, but neither of my USB hard disks works. Oh, and it takes mini-USB instead of micro-USB, like my Nexus One, so that's one more cable to take when I travel.

It would be nice to have an HDMI port, like the Archos 101 or the Advent Vega, but I can't really see a need. I can use my HTPC or laptop for that. I do wish it took a line-level input, so I could use it for live recording. Hmm, would it make a good control surface for mixing?

It has a micro-SD slot, but I kinda wish it took regular SD. My laptop and camera both use regular SD, so it would be easier to swap in. That said, I can use my laptop to access the generous internal storage like a USB stick, and I guess I can use an SD reader in the USB port if I need to transfer photos directly from my camera. (Or plug my camera in as a USB device?)

Battery life
So far, I've only gotten it down to 58% battery life in two days of occasional use. This is pretty good in my opinion.

Touchscreen
The touch screen seems a little bit flaky when I'm playing Angry Birds. (When I tap to make the blue bird split into multiple birds, it often doesn't work.) I'm not sure if this is a general problem.

Hard Keys
Unlike my Nexus One, the hard keys aren't backlit. This means they're hard to use in the dark, but the status bar provides soft keys you can use. Well, mostly. The soft "Home" key won't launch the task switcher if you long-press it. The real problem is that without backlighting, it's easy to hit the hard keys accidentally.

Conclusion
Wait six months or so for a proper Android tablet if you can. If you need a tablet today, consider an iPad or Galaxy Tab. You should only get a G tablet if you can stomach poor performance as portrait display, and put up with immature hacked firmwares. The software situation is improving, but the screen may be a deal-breaker if you want to use it as a portrait device.

Tuesday, March 31, 2009

Upgrading to Jaunty: kill fglrx

I've just upgraded one of my boxen to Ubuntu Jaunty Jackalope, and I wish it had been easier. The problem is that the new version ATI's proprietary fglrx driver doesn't seem to support my video card. For other reasons, single user mode was also broken. So I even had trouble getting a root shell.

But on the bright side, the open source drivers have "gotten there" for me-- this is a Home Theatre PC (HTPC), and I now get smooth, tear-free playback at 1920x1080. I don't get 3D acceleration, though.

fglrx is a greedy constellation of software that tries to control your hardware even when you've selected the open source drivers, and this stops the open source drivers from working correctly. Even if they show the display correctly, fglrx's Direct Rendering Manager can interfere with the standard Direct Rendering Manager, preventing video acceleration. So I recommend uninstalling fglrx before upgrading. Then you'll know whether the Free drivers are up to scratch on your hardware, and you can try fglrx if they're not.

To remove fglrx, open a terminal and type: "sudo apt-get remove --purge xorg-driver-fglrx fglrx-kernel-source; sudo reboot"

After the reboot, the open source drivers should kick in, and you'll be able to upgrade to Jaunty while they're in use. Once you've got Jaunty working, you can try your luck with fglrx again if you like. I probably won't :-)

Sunday, December 16, 2007

Bazaar 1.0: not just a number

I've been working on Bazaar since before it started. Back before Martin had started coding Bazaar, I worked on an implementation of the same ideas (as I understood them), called "BaZing". Now, more than 2.5 years later, we've finally reached 1.0.

In a sense, these numbers are arbitrary. Bazaar has been usable for many years, depending on your needs. So to us, 1.0 is a signal that says "Hey, look at us. We're ready for you." It's a number we could choose to use at any time. We've chosen to use it now, but that doesn't mean it's meaningless.

The act of declaring 1.0 meant that several people pushed back hard. We have a new storage format called "packs". It's safer and faster, and almost lockless. People said we weren't ready until our new, faster, safer format was the default. They said we couldn't reasonably expect people to benchmark a non-default format. They were right. So Robert Collins pushed hard on getting "packs" up to scratch, and we delayed the release until they were ready.

Meanwhile, for me, 1.0 meant I decided I should fix some problems that I'd known about for a while, but had never been enthusiastic about fixing: problems with case-insensitive (or case-preserving) filesystems. Bazaar can now detect case-insensitive filesystems and avoid trying to create files whose names differ only in case.

Others also put work into solving the kind of bugs that give bad first impressions.

Originally, this release was called "0.93". But if we'd released it that way, it would have been a completely different release. We may not have done everything we wanted to do, but I'm proud of Bazaar 1.0.

Wednesday, February 21, 2007

Living the sysadmin dream

There's a legendary threat of sysadmins: "Go away, or I will replace you with a very small shell script".

Well, today I got a taste of that.

At work, we've got a new web site almost ready to go. The content is stored the plainest-possible XHTML. That makes it somewhat future-proof. We process that through Kid templates to make it look snazzy.

We wanted to reuse a lot of the content from our existing site, which is a mix of HTML and PHP with some abuse of divs and other incidental wackiness. We knew the error of our ways, and we wanted to get pure, so we hired a contractor to convert our site over. He knew Python. He seemed to have a good grasp of advanced web site architectures.

Boy, were we ever wrong.

Originally expected to take one month, the job ended up taking three. In the process, I had to write a script to do automated testing of the guy's work, because he was screwing up on the mechanical stuff. Problems that can be solved with search and replace, he was choosing to solve by hand, and getting wrong. It was insulting getting work with the same mechanical errors time and time again.

What I had, perhaps naively, expected was that he would write a conversion script, eyeball the results, and fix up any problems.

There were a few files we'd asked him not to convert, but we eventually decided to convert some of them after all. The task fell to me, and it was a real joy to set to it. After all that time spent poring over his work, I'd developed a very clear idea of what I thought he should have done in the first place.

Phase 1: Text Substitution
In this phase, we convert PHP to HTML. In the general case, you can't do this without implementing PHP. But our existing site uses PHP only as a templating system, so there are only a couple of stock phrases to worry about. re.sub handles this nicely.

Another thing we do is remove any ® symbols we encounter, because our house style has changed, and we no longer want to affix this to every mention of our brand.


Phase 2: Tidy HTML
This phase consists of shoving all our files through Tidy, so that they become well-formed, almost-valid HTML. It's wonderful to have a utility like Tidy, because programs can't feel pain.

Phase 3: Ending div abuse

Our existing content uses div classes for headings all over the place. To tackle these problems, we should really operate in the XML domain, so that we avoid parsing mistakes. Now that tidy has made the documents well-formed, we can parse them with ElementTree's HTML parser. Then we process it through a Kid Template with a series of match rules. These turn divs into h2s, and so on. We can also clean up our document title (which has an inexplicable trailing non-breaking spaces)

Phase 4: Whitespace cleanup
The kid processing has fixed the semantics of the document and exported it as XHTML, but we're left with swaths of whitespace and line breaks in unreasonable places. Another run through Tidy fixes that.

Automation is wunnerful
Not only did I manage to get a basic conversion routine in place in about three hours, I actually stayed at long past my notional end-of-shift, because I was enjoying what I was doing. Writing a script to convert files to XHTML is fun. Doing the conversion yourself is not. That's a problem our contractor faced. Not only was he slow when he was actually working, but he had trouble getting motivated to work, so he didn't put in as much time as we wanted him to.

They say that good programmers are 10x more productive than average programmers, and truly excellent programmers are 10x more productive again. I suspect automation is one of the factors in that difference. Unless you stop them, an excellent programmer will find a way to eliminate the boring bits. Even if automating the job takes just as long as doing it by hand, a programmer will be happier and more motivated because the work is much more stimulating.

So today, I replaced a contractor with a reasonably small Python script. I wish I'd done it months ago.

Saturday, October 28, 2006

Workarounds for SQLAlchemy and Turbogears 1.0b

I've started work on a new TurboGears project to track merge requests for Bazaar. I was already familiar with earlier versions of TurboGears, but I decided to upgrade to 1.0b, and try out this new SQLAlchemy thing that everyone's raving about. It's supposed to be the official object-relational-mapper in TurboGears 1.1.

You have the option of using it in 1.0b, but the integration is not smooth.

Although you can use the identity framework with SQLAlchemy, the default model.py that TurboGears provides is broken. TurboGears uses the ActiveMapper extension, but the default model.py defines relationship between groups and users, and between groups and permissions in two places. Even though the definitions are equivalent, this is not allowed, and it fails silently.

So to use groups/permissions effectively, comment out the two many_to_many lines in Group. You'll still get users and properties attributes on Group, but they'll be created by the 'backref' parameters on the many_to_many statements in User and Property.

I've learned to like unit-testing. But the Turbogears TestCase object does not handle SQLAlchemy objects. So I wrote my own:

class TestMessages(TestCase):

def iter_active_mappers(self):
for key, item in model.__dict__.items():
try:
if issubclass(item, ActiveMapper):
if item is not ActiveMapper:
yield item
except TypeError:
pass

def setUp(self):
for active_mapper in self.iter_active_mappers():
active_mapper.table.create()

def tearDown(self):
for active_mapper in self.iter_active_mappers():
active_mapper.table.drop()

Finally, I also had to force it to provide my own database configuration:
config.update({'sqlalchemy.dburi': 'sqlite:///:memory:'})

The tg-admin tool has quite limited support for SQLAlchemy: it only does sql create. I've done sql drop quite a lot in past projects. I could adapt the tearDown method above, but I'm currently using SQLite, so rm devdata is an adequate subsitute.

I hope these tips are helpful.

Update:
SQLAlchemy emits reams and reams of SQL kipple; To disable that, place sqlalchemy.echo = False in your config file.

It's great that TurboGears is so clearly committed to delivering best-of-breed components that they'll switch ORMs and templating libraries for their 1.1 release. But 1.1 isn't here yet, and while you can integrate SQLAlchemy and TurboGears today, there's a certain amount of pain involved.