Reasons PHP sucks #753

Another good example of a PHP “quirk” is the way PHP handles constants. It was one of the major factors affecting performance. Just removing all the constants allowed us to improve the performance by almost 2x (we left one constant to be precise).

From The Need for Speed.

That’s right - PHP is up to 2X faster if you don’t use constants. You know, that means hardcode values in…

Patch for Mercurial.tmbundle

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

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     #! /usr/bin/env ruby
 2     
 3     require ENV["TM_SUPPORT_PATH"] + "/lib/tm/executor"
 4     
 5     command = [ENV["TM_PROLOG"] || "swipl", "-s", ENV["TM_FILEPATH"]]
 6     two_line = false
 7     
 8     welcome = /^(Welcome to SWI-Prolog)|(Copyright )|(SWI-Prolog comes with)|(and you are welcome)|(Please visit)|(For help, use)/
 9     
10     TextMate::Executor.run(command) do |str, type|
11       if type == :err
12         if two_line
13           two_line = false
14           # this line is part of the previous message
15           "#{str}</div>"
16         # Is this a warning line?
17         elsif str =~ /(Warning):\s(.*):(\d+):/
18           warn, file, line = $1, $2, $3
19           filename = file.split('/')[-1]
20           two_line = true
21           file_link = "<a class=\"near\" href=\"txmt://open?line=#{line}&url=file://#{file}\">#{filename}</a>"
22           "<div class=\"#{warn}\">#{warn}: #{file_link}, line #{line}:"
23         elsif str =~ /(ERROR):\s(.*):(\d+):(\d+):\s(.*)/
24           file, line, char, message = $2, $3, $4, $5
25           filename = file.split('/')[-1]
26           file_link = "<a class=\"near\" href=\"txmt://open?line=#{line}&column=#{char}&url=file://#{file}\">#{filename}</a>"
27           "<div class=\"err\">ERROR: #{file_link}, line #{line}, col #{char}: #{message}</div>"
28         elsif str =~ /ERROR:\s(.*)/
29           message = $1
30           "<div class=\"test fail\">ERROR: #{message}</div>"
31         elsif str =~ /%\s(.*)\scompiled\s(.*)\ssec,\s(.*)\sbytes/
32           file, time, length = $1, $2, $3
33           filename = file.split('/')[-1]
34           file_link = "<a class=\"near\" href=\"txmt://open?url=file://#{file}\">#{filename}</a>"
35           "<div class=\"test ok\"> #{file_link} (#{length} bytes) compiled in #{time} sec.</div>"
36         elsif str =~ welcome
37           "<span class=\"copyright\" style=\"font-size:xx-small;\">#{str}</span> "
38         else
39           "<div class=\"output\">#{str}</div>"
40         end
41       else
42         "<div class=\"output\">#{str}</div>"
43       end
44     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     #! /usr/bin/env ruby
 2     
 3     command = [ENV["TM_PYTHON"] || "python", "-u", "#{ENV['TM_PROJECT_DIRECTORY']}/manage.py", "test", "--noinput"]
 4     
 5     File.open(ENV['TM_FILEPATH']) do |f|
 6       f.readlines.each do |line|
 7         if line =~ /class (.*)\(.*TestCase\):/
 8           test_case = $1
 9           app_name = ENV['TM_FILEPATH'].split(ENV['TM_PROJECT_DIRECTORY'])[1].split('/')[1]
10           test_name = "#{app_name}.#{test_case}"
11           command << test_name
12         end
13       end
14     end
15     
16     require ENV["TM_SUPPORT_PATH"] + "/lib/tm/executor"
17     
18     ENV["PYTHONPATH"] = ENV["TM_BUNDLE_SUPPORT"] + (ENV.has_key?("PYTHONPATH") ? ":" + ENV["PYTHONPATH"] : "")
19     
20     TextMate::Executor.run(command) do |str, type|
21       if type == :err
22         if str =~ /\A[\.EF]*\Z/
23           str.gsub!(/(\.)/, "<span class=\"test ok\">\1</span>")
24           str.gsub!(/(E|F)/, "<span class=\"test fail\">\1</span>")
25           str + "<br/>\n"
26         elsif str =~ /\A(FAILED.*)\Z/
27           "<div class=\"test fail\">#{htmlize $1}</div>\n"
28         elsif str =~ /\A(OK.*)\Z/
29           "<div class=\"test ok\">#{htmlize $1}</div>\n"
30         elsif str =~ /^(\s+)File "(.+)", line (\d+), in (.*)/
31           indent = $1
32           file   = $2
33           line   = $3
34           method = $4
35           indent += " " if file.sub!(/^\"(.*)\"/,"\1")
36           url = "&url=file://" + e_url(file)
37           display_name = file.split('/').last 
38           "#{htmlize(indent)}<a class=\"near\" href=\"txmt://open?line=#{line + url}\">" +
39             (method ? "method #{method}" : "<em>at top level</em>") +
40             "</a> in <strong>#{display_name}</strong> at line #{line}<br/>\n"
41         end
42       end
43     end

Flaky Internet at 8:21pm

I seem to recall my internet going down, and then coming back up, at around the same time every night.

Tonight it flaked at 8:21pm.

What is the opposite of iTunes Genius?

I didn’t realise until today that there was a part of iTunes Genius that makes suggestions when browsing the iTunes store.

I went to click on one item, and got:

Now, why would you go and recommend something you can’t give me? That is just plain mean!

Python Line Continuations

Using TextMate, it is very easy to get snippets and commands to do things that you often do. However, the python bundle is a bit lacking, and this is a great opportunity to improve that.

I’ve created a Command that will enter a newline, and if not inside a list, function call, dictionary or multi-line string, automatically add a trailing .

I’ve hooked it in to the Enter key, and the other settings can be seen in the screenshot below:

The actual code follows:

 1 #!/usr/bin/env ruby
 2 
 3 scope = ENV['TM_SCOPE'].split
 4 
 5 no_trail = ['punctuation.definition.arguments.end.python',
 6             'meta.structure.list.python',
 7             'meta.structure.dictionary.python',
 8             'string.quoted.single.block.python',
 9             'string.quoted.double.block.python']
10 
11 print (scope & no_trail) == [] ? "\\n" : "\n"

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.