1 #=======================================================================
3 __version__
= '''0.0.04'''
4 __sub_version__
= '''20040831161913'''
5 __copyright__
= '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
13 import pli
.functional
as func
14 import pli
.interface
.interface
as interface
18 #-----------------------------------------------------------------------
19 #-------------------------------------------------callablescompatible---
20 # TODO find a better name and place....
21 def callablescompatible(c0
, c1
, method_aware
=False):
23 this will compare two callables by signature...
25 argspec
= [None, None]
28 if type(c
) is types
.MethodType
and method_aware
== True:
29 argspec
[i
] = inspect
.getargspec(c
)
32 argspec
[i
] = list(argspec
[i
])
34 # remove the first argument...
35 argspec
[i
][0] = argspec
[i
][0][1:]
36 if len(argspec
[i
][3]) > len(argspec
[i
][0]):
37 # this is a rare but just in case.... (for methods
38 # of the form: meth(self=<...>, ...) )
39 argspec
[i
][3] = argspec
[i
][3][1:]
40 argspec
[i
] = tuple(argspec
[i
])
41 elif type(c
) in (types
.MethodType
, types
.FunctionType
):
42 argspec
[i
] = inspect
.getargspec(c
)
44 argspec
[i
] = inspect
.getargspec(c
.__call
__)
46 return argspec
[0] == argspec
[1]
50 #-----------------------------------------------------------------------
51 #-----------------------------------------------------------likevalue---
53 def likevalue(obj
, method_writable
=False):
55 this will create an interface property dict that describes the argument.
57 NOTE: this is usefull as it will create a predicate that will check
58 function/method signature.
61 # type or predicate...
63 # function signature...
64 res
['predicate'] = func
.curry(callablescompatible
, obj
, method_aware
=True)
65 if not method_writable
:
66 res
['writable'] = False
68 res
['type'] = type(obj
)
70 if hasattr(obj
, '__doc__'):
71 res
['doc'] = obj
.__doc
__
75 #-----------------------------------------------------createinterface---
77 def createinterface(obj
, name
=None, doc
=None, methods_writable
=False):
79 this will generate an interface from an example object.
83 name
= 'I' + hasattr(obj
, '__name__') and obj
.__name
__ or obj
.__class
__.__name
__
89 format
[n
] = likevalue(getattr(obj
, n
), method_writable
=methods_writable
)
90 class I(interface
.Interface
):
99 #=======================================================================
100 # vim:set ts=4 sw=4 nowrap :