import _psyco
import sys

ticks = 0
depth = 10
funcs = {}

def f(frame, event, arg):
    if event != 'call':  return
    c = frame.f_code.co_code
    fn = frame.f_code.co_name
    g = frame.f_globals
    if not funcs.has_key(c):
        funcs[c] = 1
    if funcs[c] != None:
        funcs[c] = funcs[c] + 1
        if funcs[c] > ticks and g.has_key(fn):
            g[fn] = _psyco.proxy(g[fn], depth)
            funcs[c] = None
            print 'psyco rebinding function:', fn

sys.setprofile(f)

@@ -7,16 +7,93 @@
  _psyco%0A
+_psyco.selective(1) # Argument is number of invocations before rebinding%0A%0A#  
 import s
@@ -96,16 +96,19 @@
 rt sys%0A%0A
+#  
 ticks = 
@@ -109,16 +109,19 @@
 cks = 0%0A
+#  
 depth = 
@@ -123,16 +123,19 @@
 th = 10%0A
+#  
 funcs = 
@@ -138,16 +138,19 @@
 s = %7B%7D%0A%0A
+#  
 def f(fr
@@ -167,16 +167,19 @@
 , arg):%0A
+#  
     if e
@@ -202,16 +202,54 @@
  return%0A
+#      print type(frame.f_globals)%0A#  
     c = 
@@ -269,16 +269,19 @@
 co_code%0A
+#  
     fn =
@@ -302,20 +302,23 @@
 co_name%0A
+#
     
+  
 g = fram
@@ -329,16 +329,19 @@
 globals%0A
+#  
     if n
@@ -361,16 +361,19 @@
 key(c):%0A
+#  
         
@@ -385,20 +385,23 @@
 %5Bc%5D = 1%0A
+# 
     
+ 
 if funcs
@@ -413,16 +413,19 @@
 = None:%0A
+#  
         
@@ -448,24 +448,25 @@
 %5Bc%5D + 1%0A
+#
         
 if funcs
@@ -453,24 +453,26 @@
  1%0A#        
+  
 if funcs%5Bc%5D 
@@ -498,16 +498,19 @@
 ey(fn):%0A
+#  
         
@@ -548,16 +548,19 @@
  depth)%0A
+#  
         
@@ -579,16 +579,18 @@
  = None%0A
+# 
         
@@ -589,24 +589,25 @@
             
+ 
 print 'psyco
@@ -633,16 +633,19 @@
 :', fn%0A%0A
+#  
 sys.setp
@@ -654,8 +654,9 @@
 file(f)%0A
+%0A

Use c-version of the selective compilation