1 #=======================================================================
3 __version__
= '''0.5.14'''
4 __sub_version__
= '''20061124145258'''
5 __copyright__
= '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 ##from __future__ import generators
13 ##import pli.pattern.proxy as proxy
16 #-----------------------------------------------------------------------
17 #---------------------------------------------------------------apply---
18 apply = lambda func
, *pargs
, **nargs
: func(*pargs
, **nargs
)
21 #--------------------------------------------------------------lcurry---
22 lcurry
= lambda func
, *pargs
, **nargs
:\
24 func(*pargs
+ p
, **dict(nargs
.items() + n
.items()))
27 #---------------------------------------------------------------curry---
31 #--------------------------------------------------------------rcurry---
32 # NOTE: this adds the curried args to the tail...
33 rcurry
= lambda func
, *pargs
, **nargs
:\
35 func(*p
+ pargs
, **dict(nargs
.items() + n
.items()))
38 #-----------------------------------------------------------fastcurry---
39 # Originally written by Alex Martelli
40 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/229472
41 # NOTE: though this is faster, it is also less flexible than the above
43 # NOTE: here 'arg' can not be None.
44 fastcurry
= lambda func
, arg
:\
45 new
.instancemethod(func
, arg
, object)
48 #-------------------------------------------------------AbstractCurry---
49 class AbstractCurry(object):
55 #--------------------------------------------------------------LCurry---
56 class LCurry(AbstractCurry
):
58 this is the left curry class.
60 def __new__(cls
, func
, *args
, **kw
):
61 obj
= object.__new
__(cls
)
62 if isinstance(func
, AbstractCurry
):
63 obj
._curry
_func
= func
._curry
_func
64 obj
._curry
_args
= (func
._curry
_args
[0] + args
, func
._curry
_args
[1])
65 obj
._curry
_kw
= kw
= kw
.copy()
66 kw
.update(func
._curry
_kw
)
68 obj
._curry
_func
= func
69 obj
._curry
_args
= (args
, ())
70 obj
._curry
_kw
= kw
.copy()
72 def __call__(self
, *args
, **kw
):
73 return self
._curry
_func
(*self
._curry
_args
[0] + args
+ self
._curry
_args
[1], \
74 **dict(self
._curry
_kw
.items() + kw
.items()))
77 #---------------------------------------------------------------Curry---
81 #--------------------------------------------------------------RCurry---
82 class RCurry(AbstractCurry
):
84 this is the right curry class.
86 def __new__(cls
, func
, *args
, **kw
):
87 obj
= object.__new
__(cls
)
88 if isinstance(func
, AbstractCurry
):
89 obj
._curry
_func
= func
._curry
_func
90 obj
._curry
_args
= (func
._curry
_args
[0], func
._curry
_args
[1] + args
)
91 obj
._curry
_kw
= kw
= kw
.copy()
92 kw
.update(func
._curry
_kw
)
94 obj
._curry
_func
= func
95 obj
._curry
_args
= ((), args
)
96 obj
._curry
_kw
= kw
.copy()
98 def __call__(self
, *args
, **kw
):
99 return self
._curry
_func
(*self
._curry
_args
[0] + args
+ self
._curry
_args
[1],
100 **dict(self
._curry
_kw
.items() + kw
.items()))
103 #---------------------------------------------------------LCurryProxy---
105 ##class LCurryProxy(AbstractCurry, proxy.TransparentInheritAndOverrideProxy):
108 ## def __init__(self, target, *p, **n):
111 ## # handle recursive curry....
112 ## if hasattr(target, '_curry_pargs'):
113 ## p = p + target._curry_pargs
114 ## if hasattr(target, '_curry_kw'):
115 ## n = dict(n.items() + target._curry_kw.items())
116 ## # assign the args...
117 ## self._curry_pargs = p
118 ## # XXX do we need to copy the dict???
119 ## self._curry_kw = n
120 ## def __call__(self, *p, **n):
123 ## super(CurryProxy, self).__call__(*(p + self._curry_pargs), \
124 ## **dict(n.items() + self._curry_kw.items()))
127 #---------------------------------------------------------RCurryProxy---
129 ##class RCurryProxy(AbstractCurry, proxy.TransparentInheritAndOverrideProxy):
132 ## def __init__(self, target, *p, **n):
135 ## # handle recursive curry....
136 ## if hasattr(target, '_curry_pargs'):
137 ## p = target._curry_pargs + p
138 ## if hasattr(target, '_curry_kw'):
139 ## n = dict(n.items() + target._curry_kw.items())
140 ## # assign the args...
141 ## self._curry_pargs = p
142 ## # XXX do we need to copy the dict???
143 ## self._curry_kw = n
144 ## def __call__(self, *p, **n):
147 ## super(CurryProxy, self).__call__(*(self._curry_pargs + p), \
148 ## **dict(n.items() + self._curry_kw.items()))
151 #--------------------------------------------------------------negate---
152 # this will return a function that will return the oposite result
153 # (boolean) to the original....
155 lambda *p
, **n
: not f(*p
, **n
)
158 #------------------------------------------------------raise_on_false---
159 def raise_on_false(func
, exception
=Exception, name
=None, msg
=''):
161 this will return a function wraping func so as to raise exception(msg) if it returns false.
165 raise TypeError, 'func must not be None.'
166 if hasattr(func
, '_raise_on_false_wrapped') and func
._raise
_on
_false
_wrapped
:
167 raise TypeError, '%s is already wrapped, won\'t wrap agen.' % func
169 # define the function code...
170 func_txt
= """def %(function_name)s(*p, **n):
171 '''wrapper of %(function_object)s callable.'''
176 # genreate a good name if one is not given...
178 name
= hasattr(func
, '__name__') and func
.__name
__ != '<lambda>' and func
.__name
__ or 'Unnamed'
179 exec (func_txt
% {'function_name':name
, 'function_object':func
}) in locals(), globals()
181 # marck the predicate returned... (to avoid repeated wrapping (see
183 f
._raise
_on
_false
_wrapped
= True
187 ###------------------------------------------------------------iterator---
188 ##class _iterator(object):
191 ## def __init__(self, iter_next, iter_init, *p, **n):
194 ## init(self, *p, **n)
195 ## self.next = new.instancemethod(iter_next, self, iterator)
196 ## def __iter__(self):
201 #-----------------------------------------------------------------------
202 ##!! THE FOLLOWING ARE EXPERIMENTAL !!##
203 #-----------------------------------------------------------------seq---
206 seq(f0[, f1[, ...]]) -> res
207 seq(name, f0[, f1[, ...]]) -> res
209 this will return a function that when called will sequence the functions given,
210 passing its arguments into each one, and returning the list of their results.
212 NOTE: in the second form the name is used as name for the resulting function.
213 NOTE: atleast one function must be given.
217 raise TypeError, 'need atleast one callable in the sequence (got: 0).'
219 func_txt
= """def %(function_name)s(*p, **n):
220 '''sequence wrapper of %(functions)s.'''
223 res += [func(*p, **n)]
225 exec (func_txt
% {'function_name':f0
, 'functions':f
}) in locals(), globals()
232 for func
in (f0
,) + f
:
233 res
+= [func(*p
, **n
)]
239 #-----------------------------------------------------------------------
240 #--------------------------------------------------------getclasstree---
241 def getclasstree(cls
, predicate
=None):
243 for c
in cls
.__subclasses
__():
244 if predicate
!= None and predicate(c
):
245 l
+= [(c
, getclasstree(c
, predicate
))]
246 elif predicate
== None:
247 l
+= [(c
, getclasstree(c
))]
251 #---------------------------------------------------classtreeiterdown---
252 # this is my first recursive iterator... :)
253 def classtreeiterdown(cls
, predicate
=None):
255 this will iterate the inheritance tree branch down.
257 for c
in cls
.__subclasses
__():
258 if predicate
!= None and predicate(c
):
260 for cls
in classtreeiterdown(c
, predicate
):
262 elif predicate
== None:
264 for cls
in classtreeiterdown(c
):
268 #-----------------------------------------------------classtreeiterup---
269 def classtreeiterup(cls
, predicate
=None):
271 this will iterate the inheritance tree up.
273 NOTE: this will not remove duplicate classes.
275 for c
in cls
.__bases
__:
276 if predicate
!= None and predicate(c
):
278 for cls
in classtreeiterup(c
, predicate
):
280 elif predicate
== None:
282 for cls
in classtreeiterup(c
):
287 #=======================================================================
288 # vim:set ts=4 sw=4 nowrap :