comparison engine/extensions/pychan/tools.py @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 9a1529f9625e
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 # coding: utf-8
2
3 ### Functools ###
4
5 def applyOnlySuitable(func,**kwargs):
6 """
7 This nifty little function takes another function and applies it to a dictionary of
8 keyword arguments. If the supplied function does not expect one or more of the
9 keyword arguments, these are silently discarded. The result of the application is returned.
10 This is useful to pass information to callbacks without enforcing a particular signature.
11 """
12 if hasattr(func,'im_func'):
13 code = func.im_func.func_code
14 varnames = code.co_varnames[1:code.co_argcount]#ditch bound instance
15 else:
16 code = func.func_code
17 varnames = code.co_varnames[0:code.co_argcount]
18
19 #http://docs.python.org/lib/inspect-types.html
20 if code.co_flags & 8:
21 return func(**kwargs)
22 for name,value in kwargs.items():
23 if name not in varnames:
24 del kwargs[name]
25 return func(**kwargs)
26
27 def callbackWithArguments(callback,*args,**kwargs):
28 """
29 Curries a function with extra arguments to
30 create a suitable callback.
31
32 If you don't know what this means, don't worry.
33 It is designed for the case where you need
34 different buttons to execute basically the same code
35 with different argumnets.
36
37 Usage::
38 # The target callback
39 def printStuff(text):
40 print text
41 # Mapping the events
42 gui.mapEvents({
43 'buttonHello' : callbackWithArguments(printStuff,"Hello"),
44 'buttonBye' : callbackWithArguments(printStuff,"Adieu")
45 })
46 """
47 def real_callback():
48 callback(*args,**kwargs)
49 return real_callback
50
51
52 def this_is_deprecated(func,message=None):
53 if message is None:
54 message = repr(func)
55 def wrapped_func(*args,**kwargs):
56 print "PyChan: You are using the DEPRECATED functionality: %s" % message
57 return func(*args,**kwargs)
58 return wrapped_func