1 #=======================================================================
3 __version__
= '''0.5.08'''
4 __sub_version__
= '''20040328151509'''
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 then 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 #--------------------------------------------------------getclasstree---
145 def getclasstree(cls
, predicate
=None):
147 for c
in cls
.__subclasses
__():
148 if predicate
!= None and predicate(c
):
149 l
+= [(c
, getclasstree(c
, predicate
))]
150 elif predicate
== None:
151 l
+= [(c
, getclasstree(c
))]
155 #---------------------------------------------------classtreeiterdown---
156 # this is my first recursive iterator... :)
157 def classtreeiterdown(cls
, predicate
=None):
159 this will iterate the inheritance tree branch down.
161 for c
in cls
.__subclasses
__():
162 if predicate
!= None and predicate(c
):
164 for cls
in classtreeiterdown(c
, predicate
):
166 elif predicate
== None:
168 for cls
in classtreeiterdown(c
):
172 #-----------------------------------------------------classtreeiterup---
173 def classtreeiterup(cls
, predicate
=None):
175 this will iterate the inheritance tree up.
177 NOTE: this will not remove duplicate classes.
179 for c
in cls
.__bases
__:
180 if predicate
!= None and predicate(c
):
182 for cls
in classtreeiterup(c
, predicate
):
184 elif predicate
== None:
186 for cls
in classtreeiterup(c
):
191 #=======================================================================
192 # vim:set ts=4 sw=4 nowrap :