[rcbth]

Archive for the ‘Code Hints’ Category

Speed Up String Usage in Perl

Friday, July 18th, 2008

Perl, like the shell, will check double quoted strings for variable substitution while leaving single quoted strings alone. If your like me you might still end up with a lot of double quoted static strings in your modules. A quick and easy way to rout them all out is this regular expression:

s/(?<\\)\"(([^"$@]|\\\"|\\\$|\\\@)*)(?<!\\)\"/'$2'/g

It seems to be error-free, but please let me know if you have a suggestion for improvement.

Unix Timestamps in Python

Monday, March 24th, 2008

I use Unix timestamps for a great many things and just spent some time looking this up:

from time import time
mySeconds = time()

It’s simple enough, but remember: this function returns a float! It generally yields about two digits of precision. For straight Unix seconds use floor (or just the int cast), as rounding the number may put you one second ahead.

from time import time
myUnixSeconds = int(time())

You may also want to have a look at the official Python documentation for the Time module.