StokeBloke.com

Archive for the ‘Programming’ Category

Python timing decorator

Tuesday, January 27th, 2009

I have been using python as my main scripting language for some time, but recently I wanted to profile some of my scripts to see why it was taking so long.

I looked at the python profiler but I keep looking for some reason.

Finally I found this code snippet (I don’t remember from where, maybe here one night at mccool s online )

You can use python @ decorator to wrap functions with timing metrics.

def print_timing(func):
  def wrapper(*arg):
    t1 = time.time()
    res = func(*arg)
    t2 = time.time()
    print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
    return res
  return wrapper

Then simply use like this

@print_timing
def some_func(n):
    //do work

This allows lots of control as you can put metrics on 1 or more functions.

Java Runtime and cmd.exe unicode issue

Thursday, November 13th, 2008

For many years our product used the following code to open URLs or files on Windows 2000 and XP.

final String[] cmds = { "cmd.exe", "/C", "start", "/B ", "/WAIT", url };
Runtime.getRuntime().exec(cmds);

This code is really simply and works well. If you have different editors configured for a specific file type the start launches the correct viewer/editor. It also opens the users preferred web browser too.

We didn’t have many issues with this until we started added more localisations to our product and supporting unicode fully.

This code in theory looks good, but it didn’t work in many cases. I found that the cmd.exe process on a European Windows system runs in ISO-8859-1 so any arguments we passed it get completely messed up.

This means the cmd.exe cannot be used as it doesn’t support unicode.

I spent days looking for another alternative but couldnt find a java only fix.  In the end we had to use JNI and use the Windows ShellExecuteExW and ShellExecuteEx calls (based on whether we used a unicode or none unicode string)

Using subversion from behind a proxy (HTTPS)

Tuesday, September 2nd, 2008

Subversion can be used from behind a proxy quiet easily.

Go to either (Windows)

sideways online download %APPDATA%\Subversion

or (Linux)

~/.subversion

watch nomad online and edit the servers file.

You need to have a entry like this in the file.

[global]
http-proxy-host = someproxy.name.com
http-proxy-port = 9999

Now you can connect out to external subversion servers.
You may need to read about (and set) the http-proxy-exceptions variable if you have internal and external servers.

There is also a

  • http-proxy-password
  • http-proxy-username

variable which may need to be set if your proxy requires authentication.

scanf + printf with doubles

Wednesday, July 23rd, 2008

I recently had a problem that I was using printf with %E and doubles, but when I did a scanf to read the values back it they always failed.

I couldn’t figure out the problem, till I realised that %E, scientific notation, isnt supposed to be used for doubles.

Its very surprising that printf works one way, but the scanf doesnt work on the way back.

I wrote this little test program. It clearly shows that you should always is &lf when reading and writing doubles.

#include 

int main(int argc, char** argv) {

  const char * str = "-1.234E003";
  double d = 0;

  // check scanf
  printf("  scanf\n");
  sscanf(str, "%E", &d);
  printf("%%E = %lfE\n", d);

  d = 0;
  sscanf(str, "%G", &d);
  printf("%%G = %lf\n", d);

  d = 0;
  sscanf(str, "%f", &d);
  printf("%%f = %lf\n", d);

  d = 0;
  sscanf(str, "%lf", &d);
  printf("%%lf = %lf\n", d);

  // now check printf
  printf("  printf\n");
  printf("%%E = %E\n", d);
  printf("%%G = %G\n", d);
  printf("%%f = %f\n", d);
  printf("%%lf = %lf\n", d);

  return 0;
}
download the ice storm onlin
When you compile this and run it you get this
  scanf
%E = 0.000000E
%G = 0.000000
%f = 0.000000
%lf = -1234.000000
  printf
%E = -1.234000E+003
%G = -1234
%f = -1234.000000
%lf = -1234.000000

Clear the printf works fine but the scanf cant cope with the doubles.

I’m not sure whether its documented what should happen when you use the incorrect printf or scanf variables (e.g %d) with the incorrect argument type (i.e float not a integer).

Firefox tab at the bottom

Thursday, June 12th, 2008

I’ve always liked how Opera used to put the tab bar at the bottom.  I’ve always configured firefox to be the same too.

I recently updated to Firefox 3 rc3 and I lost this setting.  After a little searching I found that you simply need to add

/* Puts the tabbar at the bottom of firefox */
#content > tabbox { -moz-box-direction: reverse; }

to the following file.

C:\Documents and Settings\<username>\Application Data\Mozilla\
Firefox\Profiles\xxxxxx.default\chrome\userChrome.css

You may need to create the file as it doesn’t exist by default.

Once you restart firefox, the tabs are at the bottom again.