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.

download turistas movie

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 download digby the biggest dog in the world dvd 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.

download my daughter s secret movie

apocalypto dvd

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

download factotum dvd

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

Comment system for photographs

Sunday, July 27th, 2008

Well I finally got around to adding the ability to comment on every photograph.

watch absolute power online download bled Feel free to give it a go.

Neil

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.

dragon hunters download

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

#include <stdio.h>
 
int main(int argc, char** argv) {
 
  const char * str = "-1.234E003";
  double d = 0;
 
  // check scanf
  printf("  scanf\n");
  sscanf(str, "%E", &#038;d);
  printf("%%E = %lfE\n", d);
 
  d = 0;
  sscanf(str, "%G", &#038;d);
  printf("%%G = %lf\n", d);
 
  d = 0;
  sscanf(str, "%f", &#038;d);
  printf("%%f = %lf\n", d);
 
  d = 0;
  sscanf(str, "%lf", &#038;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 online

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).

download alive lost stallions the journey home movie