From 89129e40693de5840630cea6423914b81d679771 Mon Sep 17 00:00:00 2001 From: alex_nanou Date: Thu, 3 Nov 2005 13:21:02 +0000 Subject: [PATCH] *** empty log message *** --- CHANGES | 11 +++++++++- PKG-INFO | 2 +- pli/logictypes.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index f23c4ab..475287c 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,15 @@ -# this file was generated on [200510131910] +# this file was generated on [200511031618] pli changes: +version 0.0.162 (200511031616): +- added the folowing to pli.logictypes: + OfType (originaly named oftype. the old name is still there and will likely be used on as an inline constructor) + OfTypeWithPredicate + OfTypeWithArgPredicate + + isodd + iseven + version 0.0.161 (200510131908): - added sf patch #1260933.... ToDo revise :) diff --git a/PKG-INFO b/PKG-INFO index 046b794..22db67f 100644 --- a/PKG-INFO +++ b/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 1.0 Name: pli -Version: 0.0.161 +Version: 0.0.162 Summary: PLI: a utility library for the Python language. Home-page: http://pli.sourceforge.net/ Author: Alex A. Naanou diff --git a/pli/logictypes.py b/pli/logictypes.py index 9872d64..0960713 100755 --- a/pli/logictypes.py +++ b/pli/logictypes.py @@ -1,7 +1,7 @@ #======================================================================= -__version__ = '''0.1.19''' -__sub_version__ = '''20050902235933''' +__version__ = '''0.1.21''' +__sub_version__ = '''20051103161541''' __copyright__ = '''(c) Alex A. Naanou 2003''' __doc__ = '''\ @@ -100,7 +100,7 @@ class Pattern(object): #--------------------------------------------------------------oftype--- -class oftype(Pattern): +class OfType(Pattern): ''' this will create an object that can be used as a predicate to test type, and it will copare True to objects of that type. @@ -118,7 +118,8 @@ class oftype(Pattern): return isinstance(other, self._types) is True __eq__ = __call__ def __ne__(self, other): - return not isinstance(other, self._types) is True + return not self(other) +## return not isinstance(other, self._types) is True def __gt__(self, other): return False def __ge__(self, other): @@ -129,6 +130,51 @@ class oftype(Pattern): return True #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +oftype = OfType + + +#-------------------------------------------------OfTypeWithPredicate--- +class OfTypeWithPredicate(OfType): + ''' + ''' + def __call__(self, other): + ''' + ''' + if self.__predicate__(other): + return super(OfTypeWithPredicate, self).__call__(other) + return False + __eq__ = __call__ + # NOTE: this is intended to be oveloaded... (by default it will + # break the object...) + def __predicate__(self, other): + ''' + this will check the object. + + must return True or False. + ''' + raise NotImplementedError + + +#----------------------------------------------OfTypeWithArgPredicate--- +# WARNING: this might not be compatible with CPythons pickle... +class OfTypeWithArgPredicate(OfTypeWithPredicate): + ''' + ''' + def __init__(self, *p, **n): + ''' + ''' + super(OfTypeWithArgPredicate, self).__init__(*p, **n) + if 'predicate' in n: + self.__predicate__ = n['predicate'] + else: + raise TypeError, 'a predicate must be given.' + +#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +ofptype = OfTypeWithArgPredicate + + +#----------------------------------------------------------------------- # simple type predicates... isint = oftype(int) isfloat = oftype(float) @@ -142,6 +188,12 @@ istuple = oftype(tuple) isdict = oftype(dict) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# other odd predicates :) +isodd = ofptype(int, predicate=lambda o: o%2 != 0) +iseven = ofptype(int, predicate=lambda o: o%2 == 0) + + +#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # general type groups predicates... isnumber = oftype(int, float, complex) isstring = oftype(str, unicode) @@ -566,6 +618,8 @@ if __name__ == '__main__': print 'a' in D + print isodd(3), isodd(6) + -- 2.11.4.GIT