Monday, February 22, 2010

Simple Filter to Extract Links from a Pidgin Log

I often trade political links via the Pidgin IM client with my friend Jeremiah. Last week, he had the idea that we should coauthor a blog about these links. Towards this end I decided I harvest all of the links from my Pidgin log. This script will do that:

grep http ~/.purple/logs/aim/yourimid/friendsimid/* | grep -v -E "content-type|funpic\.hu|funnyjunk" | sed -e "s/^.*href=\"//" -e "s/\">.*//" | grep -v "font color"

The first grep finds anything that looks like a link, the second filters out any sites you don't care about. You can add more to that list by adding more "|sitename" clauses to the regex. The sed command scours off the html that Pidgin puts around anything and the last grep kills off some oddball lines that made it through the filters.

I'm sure this could all be made more efficient, but it did the job and unless you had an enormous quantity of logs to search, it's efficient enough.

Thursday, February 18, 2010

Ditching the Euro?

I know it's a little premature to forecast the abandonment of the Euro and a return to national currencies, but I was surprised to find that some people are already discussing the topic.

Wednesday, February 17, 2010

Blogging, JavaScript and CSS

I've started posting little bits of code to my blog, and I thought it would be nice to have some sort of a code tag, similar to how blocks of code are set apart on Gentoo Forums. My first thought was that there must certainly already be something like that already for Blogger. Well, no. Then I decided to just start using <pre> tags. That works, but then you lose line wrapping. The then realized that I was going to have to write some CSS. I hate having to delve into CSS because despite the fact that I spend all day, every day coding in Java in Eclipse, I don't have much time for web programming and I find writing HTML or programming in loosely typed languages to be distasteful.

I won't go into all of the details, but even that has turned out to be more involved than I was hoping. At least to do it right. After trying several things, I've finally come up with a little bit of CSS and JavaScript that work together to do a mediocre job of what I want to accomplish. You'll see the results in this blog posting.

First, I created this CSS that I added directly to my Blogger template in Layout -> Edit HTML:

.code {
padding: .5em;
border-right: #d1d7dc 1px solid;
border-top: #d1d7dc 1px solid;
border-left: #d1d7dc 1px solid;
color: #000000;
border-bottom: #d1d7dc 1px solid;
font-family: 'Liberation Mono', Courier, 'Courier New', monospace;
font-style: bold;
background-color: #ffffcc
}

That gives me the block that I want, setting off the code from the article text. But then I noticed that any extra spaces were being lost as happens with HTML. That led to this bit of JavaScript added to the HTML/JavaScript block in Layout -> Page Elements:

<script type="text/javascript">
var divs = document.getElementsByTagName('div');
for(var i = 0; i < divs.length; i++) {
if(divs[i].className == 'code') {
str = divs[i].innerHTML;
str = str.replace(/ /g, '&nbsp;');
divs[i].innerHTML = str;
}
}
</script>

This is very sub-optimal, though, as the regex in replace() is overly simplistic. I tried several expressions, trying to get something better, but had no luck. What needs to happen is that all blocks of multiple spaces at the beginning of a code line need to be replaced with &nbsp;. I simply could not figure out a single expression that would replace a variable number of spaces with a corresponding quantity of &nbsp;, and only do it on the beginning of a line. If you can think of something that might work, let me know. Otherwise, I may just write some code to inspect the line and figure out what spaces need to be replaced.

I'll come back around to this when I have more time. Or, I might just use Alex Gorbatchev's Syntax Highlighter, but that was way more involved than I wanted to deal with today.

Monday, February 15, 2010

Awesome Steaks

My wife has some friends who are chefs who told me the way to cook a steak in an oven and have it come out excellent. Here is the procedure:
  • Ahead of time, whip butter with salt and pepper, fresh chopped thyme, a little fresh lemon juice, fold in roquefort cheese. Roll in wax paper into a cylinder shape and chill. The wax paper should be rolled around the butter like a tube, not rolled up with the butter like a newspaper.
  • Steaks should be at least 1" thick. I used 1.5".
  • Preheat oven to 375° to 400° F.
  • Pat steaks dry. Season to taste.
  • Heat a little oil in a pan over medium heat. When pepper dropped into the oil sizzles, add steaks. Sear on each side for 2 to 3 minutes. I went a little short because the steaks started looking done VERY fast. Do not be alarmed. Next time I'll do the entire 3 minutes on my thick steaks.
  • Place whole pan in oven for 3-5 minutes until the steaks are done to taste.
  • Cut butter cylinder into slices. Place butter slices on steaks while they are hot from the oven so the butter melts over the steak.
  • Eat.

Wednesday, February 10, 2010

Simple Filters in Perl

The other day I needed to pull some XML out of a log file. Some of the XML is in human readable format, spanning multiple lines. I changed my logging to spit out a tag before and after the XML to make it easy to mechanically separate the XML from the rest of the log.1 The tags I used were "--- XML Request" or "--- XML Response" at the beginning of the line before the XML and "--- End XML" at the beginning of the line after the end of the XML. The Perl I came up with to filter these logs is:

#!/usr/bin/perl -w

$in_xml = 0;

while (<>&) {
if ($in_xml) {
print $_;
if ($_ =~ /^--- End/) {
$in_xml = 0;
}
} else {
if ($_ =~ /^--- XML/) {
print $_;
$in_xml = 1;
}
}
}

I then remembered that I didn't have to specify the =~ or $_, Perl being able to assume that for you, my revised version is:

#!/usr/bin/perl -w

$in_xml = 0;

while (<>) {
if ($in_xml) {
print $_;
if (/^--- End/) {
$in_xml = 0;
}
} else {
if (/^--- XML/) {
print $_;
$in_xml = 1;
}
}
}

1 This is entirely unlike mechanically separated chicken.

Out of Scope

Why is it that every time I opine aloud that Java would benefit from a destructor, something that gets called the moment an object goes out of scope, people start whining about memory leaks and saying memory management is hard? I didn't say ditch the garbage collector. Since you have brought up the subject, let me say again that memory management is not hard.

Monday, February 01, 2010

Java Needs Destructors

I've been a Java programmer for seven years now. Java is no C++, but it's not a bad language, especially in it's Java 6 incarnation. There are just a few things about it, though, that continue to really get under my skin. This biggest is the lack of destructors in Java. In C++, you'll often see code like this:
public void foo() {
ResourceIntensiveClass ric = new ResourceIntensiveClass("have bugs");
ric.initiateResourceLockingStuff();
ric.exceptionRiddledFunction();
}
And you can be comfortable that, if written correctly, ResourceIntesiveClass' destructor will be called and all resources freed when ric goes out of scope, regardless of whether it was under control or because of an exception thrown by ResourceIntensiveClass::exceptionRiddledFunction().

Unfortunately, when Java was being designed, the designers seem to want to get rid of every aspect of C++ that gave people trouble. Many people have trouble with destructors, so they are completely non-existent in Java. The only possible replacement, the finally clause to the try/catch block, requires the programmers who use a class to remember to call resource freeing methods of a class in their finally blocks. Every time. Without fail. Good luck. Java code is needlessly much more verbose for the same task than many other languages. Consider this Java equivalent to the code above:
public void foo() throws Exception {
ResourceIntensiveClass ric = null;
try {
ric = new ResourceIntensiveClass("have bugs");
ric.initiateResourceLockingStuff();
ric.exceptionRiddledFunction();
} finally {
if (ric != null) {
ric.manuallyCalledCleanupMethod();
}
}
}
Twelve lines to the first example's five lines. More typing and more opportunities for programmer induced bugs.

When is Java going to grow up and get destructors?

Ungovernable by Design

I often find myself saying that one form of tyranny is the tyranny of the majority electorate over the minority electorate. This is generally during a conversation discussing the Constitution and how the United States of America is a republic, not a democracy. There seem to be those with grand ideas about how the USA could be a "better" country who seem to find the Constitution and the, at least intended, weakness of the federal government to be a hindrance to their plans. Apparently one such person has even bemoaned that America has become ungovernable. Adam Graham has written an excellent article about how and why this is so. The point is, be glad that it is.