1 #=======================================================================
3 __version__
= '''0.5.14'''
4 __sub_version__
= '''20041101000543'''
5 __copyright__
= '''(c) Alex A. Naanou 2003'''
8 #-----------------------------------------------------------------------
10 from __future__
import generators
14 #-----------------------------------------------------------------------
15 #---------------------------------------------------------------apply---
16 apply = lambda func
, *pargs
, **nargs
: func(*pargs
, **nargs
)
19 #--------------------------------------------------------------lcurry---
20 lcurry
= lambda func
, *pargs
, **nargs
:\
22 func(*pargs
+ p
, **dict(nargs
.items() + n
.items()))
25 #---------------------------------------------------------------curry---
29 #--------------------------------------------------------------rcurry---
30 # NOTE: this adds the curried args to the tail...
31 rcurry
= lambda func
, *pargs
, **nargs
:\
33 func(*p
+ pargs
, **dict(nargs
.items() + n
.items()))
36 #-----------------------------------------------------------fastcurry---
37 # Originally written by Alex Martelli
38 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/229472
39 # NOTE: though this is faster, it is also less flexible than the above
41 # NOTE: here 'arg' can not be None.
42 fastcurry
= lambda func
, arg
:\
43 new
.instancemethod(func
, arg
, object)
46 #--------------------------------------------------------------LCurry---
49 this is the left curry class.
51 def __new__(cls
, func
, *args
, **kw
):
52 obj
= object.__new
__(cls
)
53 if isinstance(func
, LCurry
) or isinstance(func
, RCurry
):
54 obj
._curry
_func
= func
._curry
_func
55 obj
._curry
_args
= (func
._curry
_args
[0] + args
, func
._curry
_args
[1])
56 obj
._curry
_kw
= kw
= kw
.copy()
57 kw
.update(func
._curry
_kw
)
59 obj
._curry
_func
= func
60 obj
._curry
_args
= (args
, ())
61 obj
._curry
_kw
= kw
.copy()
63 def __call__(self
, *args
, **kw
):
64 self
._curry
_func
(*self
._curry
_args
[0] + args
+ self
._curry
_args
[1], **dict(self
._curry
_kw
.items() + kw
.items()))
67 #---------------------------------------------------------------Curry---
71 #--------------------------------------------------------------RCurry---
74 this is the right curry class.
76 def __new__(cls
, func
, *args
, **kw
):
77 obj
= object.__new
__(cls
)
78 if isinstance(func
, LCurry
) or isinstance(func
, RCurry
):
79 obj
._curry
_func
= func
._curry
_func
80 obj
._curry
_args
= (func
._curry
_args
[0] ,func
._curry
_args
[1] + args
)
81 obj
._curry
_kw
= kw
= kw
.copy()
82 kw
.update(func
._curry
_kw
)
84 obj
._curry
_func
= func
85 obj
._curry
_args
= ((), args
)
86 obj
._curry
_kw
= kw
.copy()
88 def __call__(self
, *args
, **kw
):
89 self
._curry
_func
(*self
._curry
_args
[0] + args
+ self
._curry
_args
[1], **dict(self
._curry
_kw
.items() + kw
.items()))
92 #--------------------------------------------------------------negate---
93 # this will return a function that will return the oposite result
94 # (boolean) to the original....
96 lambda *p
, **n
: not f(*p
, **n
)
99 #------------------------------------------------------raise_on_false---
100 def raise_on_false(func
, exception
=Exception, name
=None, msg
=''):
102 this will return a function wraping func so as to raise exception(msg) if it returns false.
104 # sanity checks... ##!! TEST !!##
106 raise TypeError, 'func must not be None.'
107 if hasattr(func
, '_raise_on_false_wrapped') and func
._raise
_on
_false
_wrapped
:
108 raise TypeError, '%s is already wrapped, won\'t wrap agen.' % func
110 # define the function code...
111 func_txt
= """def %(function_name)s(*p, **n):
112 '''wrapper of %(function_object)s callable.'''
117 # genreate a good name if one is not given...
119 name
= hasattr(func
, '__name__') and func
.__name
__ != '<lambda>' and func
.__name
__ or 'Unnamed'
120 exec (func_txt
% {'function_name':name
, 'function_object':func
}) in locals(), globals()
122 # marck the predicate returned... (to avoid repeated wrapping (see
124 f
._raise
_on
_false
_wrapped
= True
128 ###------------------------------------------------------------iterator---
129 ##class _iterator(object):
132 ## def __init__(self, iter_next, iter_init, *p, **n):
135 ## init(self, *p, **n)
136 ## self.next = new.instancemethod(iter_next, self, iterator)
137 ## def __iter__(self):
142 #-----------------------------------------------------------------------
143 ##!! THE FOLLOWING ARE EXPERIMENTAL !!##
144 #-----------------------------------------------------------------seq---
147 seq(f0[, f1[, ...]]) -> res
148 seq(name, f0[, f1[, ...]]) -> res
150 this will return a function that when called will sequence the functions given,
151 passing its arguments into each one, and returning the list of their results.
153 NOTE: in the second form the name is used as name for the resulting function.
154 NOTE: atleast one function must be given.
158 raise TypeError, 'need atleast one callable in the sequence (got: 0).'
160 func_txt
= """def %(function_name)s(*p, **n):
161 '''sequence wrapper of %(functions)s.'''
164 res += [func(*p, **n)]
166 exec (func_txt
% {'function_name':f0
, 'functions':f
}) in locals(), globals()
173 for func
in (f0
,) + f
:
174 res
+= [func(*p
, **n
)]
180 #-----------------------------------------------------------------------
181 #--------------------------------------------------------getclasstree---
182 def getclasstree(cls
, predicate
=None):
184 for c
in cls
.__subclasses
__():
185 if predicate
!= None and predicate(c
):
186 l
+= [(c
, getclasstree(c
, predicate
))]
187 elif predicate
== None:
188 l
+= [(c
, getclasstree(c
))]
192 #---------------------------------------------------classtreeiterdown---
193 # this is my first recursive iterator... :)
194 def classtreeiterdown(cls
, predicate
=None):
196 this will iterate the inheritance tree branch down.
198 for c
in cls
.__subclasses
__():
199 if predicate
!= None and predicate(c
):
201 for cls
in classtreeiterdown(c
, predicate
):
203 elif predicate
== None:
205 for cls
in classtreeiterdown(c
):
209 #-----------------------------------------------------classtreeiterup---
210 def classtreeiterup(cls
, predicate
=None):
212 this will iterate the inheritance tree up.
214 NOTE: this will not remove duplicate classes.
216 for c
in cls
.__bases
__:
217 if predicate
!= None and predicate(c
):
219 for cls
in classtreeiterup(c
, predicate
):
221 elif predicate
== None:
223 for cls
in classtreeiterup(c
):
228 #=======================================================================
229 # vim:set ts=4 sw=4 nowrap :