Switch between transform orientations in python?

I’ve found (or trial-and-error’d) how to create and list transform orientations, but for some reason I can’t find a way to simply SWITCH between them (e.g. “global” to “local”) from the command line. Any help is appreciated. (Version 2.66)

They are a View3D space property:

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        area.spaces[0].transform_orientation = 'GLOBAL'

If you’re running from the View3d context:

bpy.context.space_data.transform_orientation = 'GLOBAL'

Custom transform orientations are located here: bpy.context.scene.orientations
But you can’t set them active directly, like transform_orientation = scene.orientations[‘Custom’],
instead, you need to use the name - your_orientation.name;
in case of duplicate names, this is going to be problematic (the built-in orientations are always prefered however).

There’s also an operator to select an orientation, but it requires the view3d context and an orientation name as well:

bpy.ops.transform.select_orientation()

From outside the view3d context:

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        bpy.ops.transform.select_orientation({'area': area}, orientation="LOCAL")
        break