1 #=======================================================================
3 __version__
= '''0.0.04'''
4 __sub_version__
= '''20040927223921'''
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 #------------------------------------------------------dict2interface---
76 def dict2interface(dict, name
=None, doc
=None):
79 ns
= {'__format__': dict}
82 return interface
._Interface
(name
== None and 'unnamed' or name
, (object,), ns
)
84 ###-------------------------------------------------------obj2interface---
85 ##interface = dict2interface
88 #-------------------------------------------------------obj2interface---
90 def obj2interface(obj
, name
=None, doc
=None, methods_writable
=False):
92 this will generate an interface from an example object.
96 name
= 'I' + hasattr(obj
, '__name__') and obj
.__name
__ or obj
.__class
__.__name
__
102 format
[n
] = likevalue(getattr(obj
, n
), method_writable
=methods_writable
)
103 class I(interface
.Interface
):
112 #=======================================================================
113 # vim:set ts=4 sw=4 nowrap :