Monday, December 15, 2008

AppleScript: Send link from Safari to Evernote.app

I wrote a tiny AppleScript that helps to work with Mac OS X version of Evernote (Evernote.app). With this script, you can easily add link to web page opened in Safari.

How To Use

  1. Launch Script Editor.app (at /Applications/AppleScript/Script Editor.app)
  2. Just copy the script (below or here) from your browser, paste, and run.
  3. Now you see a link to an opened web page is added in Evernote.

The Script

Wednesday, October 15, 2008

Some useful printf-style formats of java.util.Formatter

java.util.Formatter which is introduced in J2SE 5.0 provides an interpreter for printf-style format strings. You can use pritf-style format through java.io.PrintStream instance.

int n = 1000000;
long millis = System.currentTimeMillis();
theLongRunningFunction(n);
System.out.printf("%d items - %d msecs elapsed\n",
    n, System.currentTimeMillis() - millis);

// => 1000000 items - 164 msecs elapsed

The elapsed seconds is formatted by using %f conversion and precision specifier.

int n = 1000000;
long millis = System.currentTimeMillis();
theLongRunningFunction(n);
System.out.printf("%d items - %.2f secs elapsed\n",
    n, (System.currentTimeMillis() - millis)/1000.0);

// => 1000000 items - 0.16 secs elapsed

The locale-specific grouping separators is also supported.

int n = 1000000;
long millis = System.currentTimeMillis();
theLongRunningFunction(n);
System.out.printf("%,d items - %.2f secs elapsed\n",
    n, (System.currentTimeMillis() - millis)/1000.0);

// => 1,000,000 items - 0.16 secs elapsed

Saturday, September 27, 2008

Tokyo Street Cats

Today, I went for a stroll through the streets of Kitano and Kitakarasuyama.


大きな地図で見る

Meeting lots of cats and having happy moments with them.

20080927_0107_1 20080927_0111_1 20080927_0115_1

Hide and seek? :-)

20080927_0112_1 20080927_0118_1

Friday, September 26, 2008

Python version check

Just use sys.hexversion instead of the string equivalent (sys.version, sys.version_info, ...). It's flexible and fast.

>>> import sys
>>> sys.hexversion
33882608
>>> hex(sys.hexversion)
'0x20501f0'
>>> if sys.hexversion >= 0x2050100:
...   print "Python 2.5.1 or higher"
... 
Python 2.5.1 or higher

For example, the explicit relative imports is introduced in Python 2.5, you can check this function as:

HAS_RELATIVE_IMPORTS = (sys.hexversion >= 0x2050000)

NOTE: sys.hexversion is available since 1.5.2. It's not worth to support older versions.

Wednesday, September 24, 2008

pylint: disable/enable message for a block or statement

One of my favorite things about pylint is a feature that allows to disable/enable message for a block or statement.

For instance, consider the function below:

def scan_code(co, module):
    assert co and module
    if DEBUG: print co.co_filename
    ...

The function scan_code has so many debug prints. For performance reason, however, they are if DEBUG: print statements. Now pylint would report so many messages (C: means convention, for programming standard violation).

% pylint scan_code
...
C:107:scan_code: More than one statement on a single line
C:110:scan_code: More than one statement on a single line
C:114:scan_code: More than one statement on a single line
C:117:scan_code: More than one statement on a single line
C:123:scan_code: More than one statement on a single line

You can disable these messages for scan_code function. # pylint: disable-msg=... style comment can be used everywhere in the source code and affects a block or statement.


# pylint: disable-msg=C0321
def scan_code(co, module):
    assert co and module
    if DEBUG: print co.co_filename
    ...

See Also

Tuesday, September 23, 2008

The Power of Python's Generator expressions and Tuple pack/unpack

Python's Generator expressions and Tuple pack/unpack is more powerful than I thought.

for k, v in d.iteritems():
  if v > 123:
    yield k, v

is equivalent to:

return (item for item in d.iteritems() if item[1] > 123)

Some simple generators can be coded using Generator expressions, and Tuple pack/unpack is an easy way to return multiple values.

% python
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {'apple': 256, 'hoge': 123, 'orange': 555}
>>> 
>>> def fn_yield():
...   for k, v in d.iteritems():
...     if v > 123:
...       yield k, v
... 
>>> def fn_generator():
...   return (item for item in d.iteritems() if item[1] > 123)
... 
>>> for k, v in fn_yield():
...   print k, v
... 
orange 555
apple 256
>>> for k, v in fn_generator():
...   print k, v
... 
orange 555
apple 256

Monday, September 22, 2008

Do you know Universal Newline Support in Python?

Do you know Universal Newline Support in Python? To be honest, I didn't know that. The PEP 278 describes in detail:

In a Python with universal newline support open() the mode parameter can also be "U", meaning "open for input as a text file with universal newline interpretation". Mode "rU" is also allowed, for symmetry with "rb". Mode "U" cannot be combined with other mode flags such as "+". Any line ending in the input file will be seen as a '\n' in Python, so little other code has to change to handle universal newlines.

Conversion of newlines happens in all calls that read data: read(), readline(), readlines(), etc.

Here is an example:

% printf 'Hello, World!\r\n' > /tmp/hello.txt
% cat /tmp/hello.txt 
Hello, World!
% python 
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> open('/tmp/hello.txt').read()
'Hello, World!\r\n'
>>> open('/tmp/hello.txt', 'U').read()
'Hello, World!\n'

If Python was built with the --with-universal-newlines option to configure (the default), File objects have newlines attribute, so testing whether the current Python has universal newline support can be done with:

if hasattr(sys.__stdout__, "newlines"):
    print "universal line endings"