About this user

Steven James http://soundstripe.net

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

Python Towers of Hanoi

Use the following as a template, replace the print line with any work for moving the discs using your data structure. The code below just prints the necessary moves.

def hanoi(n, a='A', b='B', c='C'):
    """
    move n discs from a to c using b as middle
    """
    if n == 0:
        return
    hanoi(n-1, a, c, b)
    print a, '->', c
    hanoi(n-1, b, a, c)

hanoi(3)

Win32 Shortcuts (.lnk files) with com in python

Class and general usage for accessing windows shortcuts in Python.

import pythoncom

class Win32Shortcut:
    def __init__(self, lnkname):
        self.shortcut = pythoncom.CoCreateInstance(
            shell.CLSID_ShellLink, None,
            pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
        self.shortcut.QueryInterface(pythoncom.IID_IPersistFile).Load(lnkname)

    def __getattr__(self, name):
        return getattr(self.shortcut, name)

s = Win32Shortcut(path)
iconPath = s.GetIconLocation()[0]
itemPath = s.GetPath(0)[0]



Psyco Decorator

This will cause decorated functions to be run using the psyco optimizer. It will allow the code to continue working even if psyco is not available

Decorator:
try:
    import psyco
except:
    pass

def optimize(func):
    try:
        return psyco.proxy(func)
    except:
        return func


Usage:
@optimize
def complex_function(n):
    ... do complex stuff with n ...
« Newer Snippets
Older Snippets »
3 total  XML / RSS feed