GEEK: Display method descriptions for all the methods in a Python module

Type the following in the python interpreter:

help()
modules
[name of module]

You should see a list of the methods in the module with their descriptions.

GEEK: How to tell where your script is located in Python

Suppose you’re executing a python script in the debugger, and you want to find out where the script you’re executing is located. This should do the trick:

import os
os.path.dirname( os.path.realpath( __file__ ) )

GEEK: simple python script to turn image filenames into img elements

It currently doesn’t take any options, though I plan to modify it to read from a filename in addition to stdin. Also, I should modify it to use imagemagick to get the image width and height values, since I don’t think there’s a guarantee that the metadata will store the image dimensions.

(more…)

Geek: notes to myself

I'm trying to select and display some rows from a FrontBase table. I'm accessing the table via a python script, which talks to the database via a pyobjc interface to an Objective-C adaptor to FrontBase.

“getSQLResultsDictionaryIter” is a method which returns an array of dictionaries containing the results.

For example, this snippet:


import objc
import ObjectModel
import FrontBase

fbc = FrontBase.FrontBaseConnection()

for m in fbc.getSQLResultsDictionaryIter("select * from MManager"):
  for k, v in m.items():
    print "%s : %s" % (k,v)

returns this:

joined: 2003-05-06 12:00:00
encryptedpassword : D2A55F132c83686A2a3sF9a4F5b7g0D5aCBFaB05
loginname : samplemember
mailtroubleindex : 0
firstname : Sample
lastname : Member
previouslogin : 2006-06-09 16:21:26
fullname : Sample Member
logincount : 176
Traceback (most recent call last):
  File "", line 2, in ?
  File "build/lib.darwin-8.6.0-Power_Macintosh-2.4/objc/_convenience.py", line 600, in 

It's barfing on the primarykey field from the first row returned. For some reason, the adaptor code doesn't want to print the primarykey. The primarykey object is of the following type:

print type(m['primarykey'])

I listed the methods for the m['primarykey'] object as follows:

methodList = [method for method in dir(m['primarykey'])

for method in methodList
FILE = open(“test2″,”w”
FILE.write(method)
FILE.write(“\n”)
FILE.close()

Among the 363 methods were returned, hexString() looked promising.

m['primarykey'].hexString().__str__

GEEK: Open a webbrowser from a python script

webbrowser.py allows you to open a url in an external browser (such as Firefox) from within a python script.  However, if you don't have the BROWSER environment variable set, you will get an error.

To get it to work you have to do the following:

% env | grep BROWSER
BROWSER=open
export BROWSER