TextMate return codes

From the TextMate manual:

These functions only work when the initial output option is not set as “Show as HTML”. The list of functions is as follows:

  • exit_discard
  • exit_replace_text
  • exit_replace_document
  • exit_insert_text
  • exit_insert_snippet
  • exit_show_html
  • exit_show_tool_tip
  • exit_create_new_document  

This is all well and good, but what about when you are in another language?

Simple. Just ensure your exit code matches. The values start at 200, for exit_discard, and 205 is exit_show_html.

This is probably not the best way to do it, as these may change in the future. But, I couldn’t think of another way, at least not offline.

Patch for Mercurial.tmbundle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
diff -r 5e13047a2284 Support/hg_commit.rb
    --- a/Support/hg_commit.rb	Mon Apr 27 11:38:15 2009 +0930
    +++ b/Support/hg_commit.rb	Mon Apr 27 11:39:00 2009 +0930
    @@ -79,7 +79,7 @@
       commit_paths_array = matches_to_paths(commit_matches)
       commit_status = matches_to_status(commit_matches).join(":")
       commit_path_text = commit_paths_array.collect{|path| path.quote_filename_for_shell }.join(" ")
    -  commit_args = %x{"#{commit_tool}" --status #{commit_status} #{commit_path_text} }
    +  commit_args = %x{"#{commit_tool}" --diff-cmd hg,diff --status #{commit_status} #{commit_path_text} }
     
       status = $CHILD_STATUS
       if status != 0
    

SWI-Prolog Bundle

I’ve cloned and updated the included Prolog bundle to use my Run script.

This can be found at GitHub: SWI-Prolog.tmbundle.

Run Prolog Program

In the continuing effort to do ‘productive’ tasks, but not actually the project I am supposed to be working on, I present a TextMate command to run the current prolog file, with nice HTML output, and input via a dialog box.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    #! /usr/bin/env ruby
    
    require ENV["TM_SUPPORT_PATH"] + "/lib/tm/executor"
    
    command = [ENV["TM_PROLOG"] || "swipl", "-s", ENV["TM_FILEPATH"]]
    two_line = false
    
    welcome = /^(Welcome to SWI-Prolog)|(Copyright )|(SWI-Prolog comes with)|(and you are welcome)|(Please visit)|(For help, use)/
    
    TextMate::Executor.run(command) do |str, type|
      if type == :err
        if two_line
          two_line = false
          # this line is part of the previous message
          "#{str}</div>"
        # Is this a warning line?
        elsif str =~ /(Warning):\s(.*):(\d+):/
          warn, file, line = $1, $2, $3
          filename = file.split('/')[-1]
          two_line = true
          file_link = "<a class=\"near\" href=\"txmt://open?line=#{line}&url=file://#{file}\">#{filename}</a>"
          "<div class=\"#{warn}\">#{warn}: #{file_link}, line #{line}:"
        elsif str =~ /(ERROR):\s(.*):(\d+):(\d+):\s(.*)/
          file, line, char, message = $2, $3, $4, $5
          filename = file.split('/')[-1]
          file_link = "<a class=\"near\" href=\"txmt://open?line=#{line}&column=#{char}&url=file://#{file}\">#{filename}</a>"
          "<div class=\"err\">ERROR: #{file_link}, line #{line}, col #{char}: #{message}</div>"
        elsif str =~ /ERROR:\s(.*)/
          message = $1
          "<div class=\"test fail\">ERROR: #{message}</div>"
        elsif str =~ /%\s(.*)\scompiled\s(.*)\ssec,\s(.*)\sbytes/
          file, time, length = $1, $2, $3
          filename = file.split('/')[-1]
          file_link = "<a class=\"near\" href=\"txmt://open?url=file://#{file}\">#{filename}</a>"
          "<div class=\"test ok\"> #{file_link} (#{length} bytes) compiled in #{time} sec.</div>"
        elsif str =~ welcome
          "<span class=\"copyright\" style=\"font-size:xx-small;\">#{str}</span> "
        else
          "<div class=\"output\">#{str}</div>"
        end
      else
        "<div class=\"output\">#{str}</div>"
      end
    end

Run Django Tests from TextMate

It would be cool to be able to run my Django tests from within TextMate.

Update: this version will run just the tests from the active file, if there are any. Otherwise, it runs all of the tests in the whole project.

Here is a Command to do just that:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    #! /usr/bin/env ruby
    
    command = [ENV["TM_PYTHON"] || "python", "-u", "#{ENV['TM_PROJECT_DIRECTORY']}/manage.py", "test", "--noinput"]
    
    File.open(ENV['TM_FILEPATH']) do |f|
      f.readlines.each do |line|
        if line =~ /class (.*)\(.*TestCase\):/
          test_case = $1
          app_name = ENV['TM_FILEPATH'].split(ENV['TM_PROJECT_DIRECTORY'])[1].split('/')[1]
          test_name = "#{app_name}.#{test_case}"
          command << test_name
        end
      end
    end
    
    require ENV["TM_SUPPORT_PATH"] + "/lib/tm/executor"
    
    ENV["PYTHONPATH"] = ENV["TM_BUNDLE_SUPPORT"] + (ENV.has_key?("PYTHONPATH") ? ":" + ENV["PYTHONPATH"] : "")
    
    TextMate::Executor.run(command) do |str, type|
      if type == :err
        if str =~ /\A[\.EF]*\Z/
          str.gsub!(/(\.)/, "<span class=\"test ok\">\\1</span>")
          str.gsub!(/(E|F)/, "<span class=\"test fail\">\\1</span>")
          str + "<br/>\n"
        elsif str =~ /\A(FAILED.*)\Z/
          "<div class=\"test fail\">#{htmlize $1}</div>\n"
        elsif str =~ /\A(OK.*)\Z/
          "<div class=\"test ok\">#{htmlize $1}</div>\n"
        elsif str =~ /^(\s+)File "(.+)", line (\d+), in (.*)/
          indent = $1
          file   = $2
          line   = $3
          method = $4
          indent += " " if file.sub!(/^\"(.*)\"/,"\1")
          url = "&url=file://" + e_url(file)
          display_name = file.split('/').last 
          "#{htmlize(indent)}<a class=\"near\" href=\"txmt://open?line=#{line + url}\">" +
            (method ? "method #{method}" : "<em>at top level</em>") +
            "</a> in <strong>#{display_name}</strong> at line #{line}<br/>\n"
        end
      end
    end

SCM workflow in TextMate

I’m loving coding in TextMate - it makes ruby much more fun. And python, too.

It integrates really well with heaps of other tools, including my diffing app of choice (Changes.app), and my preferred DVCS (Mercurial).

It even has its own Commit pane that appears when you choose to commit. There is one problem with it, however. Invariably I have made a change to a file and can’t remember exactly what it was. You can’t view the changes using the Mercurial bundle and Changes, and leave that window open while you commit. So, I end up having a terminal window open that I type hg chdiff into.

Instead, we should be able to quickly see the changes made to the working copy. Perhaps using a button like below:

Of course, that’s just a mockup (although it is done in IB) - the button is not connected up to anything. I have no idea how to reverse engineer the Commit.app to do this. But it would be cool.

Update: It appears all you have to do is tell the CommitWindow.app tool that you want to use --diff-cmd "hg,diff", and it is all done.

Goals for 2009

What am I hoping to achieve in the coming year?

  • Learn two new languages: at this stage it looks like it might be two of Haskell, Ruby and Icon. Probably the first two.
  • Play in the National Touch League. Over 30s, but still the highest competition in the world.
  • Finish this degree, start the next. I’m hoping to get a headstart on my Honours degree, if they’ll let me.
  • Release some OS X software. I have three commercial programs in various stages of completion. I’m hoping to get one of them done and released.
  • Build a vegetable garden. Got the beds planned out - just need to make them.
  • Learn to type using a Dvorak key layout.
  • Reread SICP

I’m sure I’ll think of more things than this…

Sometimes, the bugs aren't mine...

I’ve just spent about five hours working out some kinks with data migration between an SQLite database, and a PostgreSQL database. I’m using SQLAlchemy as the transfer tool, since the newer PGSQL database is created and only ever used through this interface.

The big issue I’ve hit is with some Numeric types. One table has Numeric values, which are NULLABLE, but for some reason when I try to load objects from this table, decimal.Decimal throws an exception.

But here is the weird bit. If I rerun the query, and try to load the same object a second time, it succeeds.

Since I needed to access all of the rows from this table, I had to keep trying to load them until I’m no longer throwing exceptions, and then finish. I had a nice code sample from SubEthaEdit, but that seems to have a bit of a bug with the Copy As XHTML feature in the newest build (submitting a bug report now).

Using Self-signed Certificates with WSMethodInvocationRef

I’ve played quite a lot with the Cocoa methods for doing WebServices: lots of people whinge about how little support is given to SOAP from Cocoa, but I actually think it’s not too bad.

To create a SOAP request is quite complicated, but it’s easy to abstract most of this out.

The first step is to create the WSMethodInvocationRef, using WSMethodInvocationCreate. This takes three arguments, one of which is the endpoint URL, which for most cases will be the same across all of your application’s requests. The second is the method name, and the third is the protocol you are using.

You then just need to set the parameters, namespace, headers and so on. This takes care of generating all of the SOAP Envelope stuff, and you can then just use this object to pass to WSMethodInvocationInvoke. This returns a CFDictionaryRef, which is toll-free bridged with an NSDictionary, and gives you status information, as well as results. Notably, this dictionary contains one with the key “/Result”, which contains another dictionary with all of the result data - and this is the cool part - it has already been converted back from XML into whatever structure it represents!

I’ve wrapped this up a little further into some classes - they are still a little under construction, but I’ll release them on bitbucket or something when I’ve cleaned them up a little. I may even make it into a Framework. Which I have been meaning to learn how to do.

Artificial Intelligence and Ebby

What is AI? What implications does complexity theory have for AI? Give an example of something that is easy for humans but hard for computers. Explain.

There are many definitions of Artificial Intelligence. I think the most amusing is “AI is whatever humans can do, but computers cannot do yet.” - which reflects the idea that truly intelligent machines are not possible - there is always something that separates the machines from us.

There are four main types of systems that could be said to be intelligent - those that think like humans, those that act like humans, those that think rationally, and those that act rationally. (Russell & Norvig). One typical test of an artificially intelligent agent is the Turing Test - where a machine and a human both communicate (using some sort of text-based interface, at this stage) with another human, the judge. If the judge cannot tell which of the candidates is the computer, and which is the human, then the computer is said to have passed the Turing Test.

One task that is generally much harder for machines than humans is language interpretation and acquisition. Steven Pinkett, in his book “The Language Instinct” argues that we are hardwired to develop language complete with the syntactical rules that most languages contain, and indeed in instances with children grouped together they did indeed develop - in one generation - a complete creole that was just as complex as any of their parent’s languages.

Even if we simplify the problem of understanding language to that of text-based (rather than verbal, and speech recognition) communication, it is still a problem that is hard for computers to solve. Indeed, it appears that more progress has been made in the area of speech recognition than language recognition in recent times. Google now has the ability for a user of an iPhone to speak words into their device, and generating a search query of their database on the words that were spoken.

If we move back to the definition of AI as a machine thinking just like a human, then we arrive at the question: can a machine ever truly think like a human? Can it ever have a mind? Can it be intelligent? Are these the same thing?

I personally believe that in principle there is nothing preventing a computer, if it were complex enough itself, from for all intents and purposes being indistinguishable from a human mind. The so-called singularity point - where computing power reaches and then exceeds that of the human mind, is approaching quickly, and may provide us with untapped opportunity. If it walks like a duck and quacks like a duck…

I think that stating outright that a robot or computer cannot think is foolish. Even if I state that I can think (and wouldn’t a computer that was trying to make you believe it was a human do the same!), how do I really know that I do think? Even more to the point, how do I know that you can think? Some years ago, an article appeared in New Scientist about virtual worlds. I’m not talking about World of Warcraft, or even Second Life, I’m talking here about a complete simulation of a universe. Eventually, a civilisation will garner enough computing power to create such a simulation that can have self-aware (or programmed to believe they are self-aware, which might well be the same thing!). Once that point has been reached, then said technological society will be able to generate numerous such simulations. And some of those simulations will be able to create simulations, if they are left to run long enough.

So, once one civilisation reaches this tipping point, there will spring into existence a multitude of simulations, and perhaps only one real universe: statistically speaking are we likely to be that special one universe that hasn’t reached that point yet? We’ve already had rude shocks when we discovered that Earth isn’t at the centre of the universe, or even necessarily that special. Why should this be the special universe.

So, we’ve decided that we are a simulation. Since you and I both think that we can think, clearly simulatrons can think. Thankyou for your time.

In all seriousness (and I don’t think that I subscribe to the above argument), I’ll go back to the duck thing. Who cares if an intelligence is “real” or just simulated. As long as it makes the decisions that would be made in a similar way (or perhaps makes better decisions - although we know from Asimov’s stories that the Robots will eventually overthrow us - I for one welcome out new Robot Overlords), who cares? It’s like saying that women are not as good as men. It’s just siliconism. Think racism or sexism, if you like.

If I assess most people I come across (or virtually all, if you read the You-Tube comments), then they are not intelligent. They’d certainly fail the Turing Test when compared to some of the more advanced “AI” systems out there. They’d probably fail at quite a few other tests while they are at it, but that’s another story. I do recall reading some years ago that the amount of intelligence on a planet stays the same - it’s just that the number of people increases. So the average goes down. (I think this one was Herbert, not Asimov, but I can’t recall. It does feel a bit “Foundation”).

So, is the Turing Test a reasonable test? It certainly centres on language - something that lots of “ppl sm 2 not get now”. But I think it is still valid to some extent. Something that can have a level of success with the Turing Test, be it a human or machine, clearly has some level of intelligence, whether that intelligence is “natural”, or simulated. But I’m not sure that it is a real good measure of intelligence.

Other authors have examined a range of “other” intelligences. Dimasio, for instance, discusses Emotional Intelligence, and the “EG”, although this for me was somewhat unfulfilling. IQ tests are out of fashion now, although in many cases whilst they are culturally biased, this is no different to the bias that we have given to language in the Turing Test. All of these tests are based around human intelligence, and the secret here is that we think that this is the only type of intelligence. Because we don’t know about the other types.

My dog is almost certainly intelligent, so some degree. She remembers things, and when she could see and hear properly, was much more aware of her environment that at times I was. She still has a much better sense of smell than I ever could (or would want, but that’s another matter). She would fail a Turing Test, yet I would say she was smarter than many students I have taught…

Well, this started out as a practice essay for an exam I have tomorrow. Not sure that most of it will be that relevant. But it was fun.