From c4ceed4411eed9761c0c6787525d32c2b404c292 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 2 Nov 2008 14:28:01 +0300 Subject: [PATCH] added pli.logictypes.AT... --- pli/logictypes.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/pli/logictypes.py b/pli/logictypes.py index ce0f91a..9042fee 100755 --- a/pli/logictypes.py +++ b/pli/logictypes.py @@ -1,7 +1,7 @@ #======================================================================= __version__ = '''0.1.21''' -__sub_version__ = '''20081004173331''' +__sub_version__ = '''20081102142629''' __copyright__ = '''(c) Alex A. Naanou 2003''' __doc__ = '''\ @@ -159,6 +159,43 @@ class IN(Pattern): return self.container != other or self.obj not in other +#------------------------------------------------------------------AT--- +class AT(Pattern): + ''' + AT(A, B) == X iff A in X and X[B] == A + + NOTE: this works as good as python's containers accept patterns. + this mostly amounts to staying clear of using patterns as + indexes. + ''' + def __init__(self, obj, position=ANY): + self.obj = obj + self.position = position + def __repr__(self): + return 'AT(%r, %r)' % (self.obj, self.position) + def __eq__(self, other): + try: + if other == SEQUENCE: + return self.obj in other \ + and other[self.position] == self.obj + else: + # mappings... + return self.obj in other.values() \ + and other[self.position] == self.obj + except (KeyError, IndexError): + return False + def __ne__(self, other): + try: + if other == SEQUENCE: + return self.obj not in other \ + or other[self.position] != self.obj + else: + return self.obj not in other.values() \ + or other[self.position] != self.obj + except (KeyError, IndexError): + return False + + #----------------------------------------------------------------------- # this is to define basic pattern combination mathematics. @@ -874,6 +911,10 @@ if __name__ == '__main__': print OR((1, ANY), (ANY, 2)) == (1, 8) print OR((1, ANY), (ANY, 2)) != (7, 8) + print AT(1, 10) == range(100) + print AT(10, 10) == range(100) + + #======================================================================= # vim:set ts=4 sw=4 nowrap : -- 2.11.4.GIT