def set(term, anno): '''Returns a new version of this term with the annotation associated with this label added or updated.''' if not types.isAppl(anno): raise TypeError("annotation is not an application term", anno) if not types.isAppl(term): return term label = anno.name def withoutLabel(term): return not (types.isAppl(term) and term.name == label) annos = lists.filter(withoutLabel, term.annotations) annos = term.factory.makeCons(anno, annos) return term.setAnnotations(annos)
def testAnnotations(self): for terms1Str in self.identityTestCases: for term1Str in terms1Str: term1 = factory.parse(term1Str) if not types.isAppl(term1): continue term = term1 self.failUnlessEqual(term.annotations, factory.parse("[]")) term = annotation.set(term, factory.parse("A(1)")) self.failUnlessEqual(term.annotations, factory.parse("[A(1)]")) term = annotation.set(term, factory.parse("B(2)")) self.failUnlessEqual(annotation.get(term, "A"), factory.parse("A(1)")) self.failUnlessEqual(annotation.get(term, "B"), factory.parse("B(2)")) term = annotation.set(term, factory.parse("A(3)")) self.failUnlessEqual(annotation.get(term, "A"), factory.parse("A(3)")) self.failUnlessEqual(annotation.get(term, "B"), factory.parse("B(2)")) term = annotation.remove(term, "A") self.failUnlessEqual(term.annotations, factory.parse("[B(2)]")) try: annotation.get(term, "C(_)") self.fail() except ValueError: pass self.failUnless(term.isEquivalent(term1))
def testIdentity(self): annos = factory.parse('[1,2]') for terms1Str in self.identityTestCases: for terms2Str in self.identityTestCases: for term1Str in terms1Str: for term2Str in terms2Str: term1 = factory.parse(term1Str) term2 = factory.parse(term2Str) expectedResult = term1Str == term2Str result = term1.isEquivalent(term2) self.failUnlessEqual(result, expectedResult, msg = '%s <=> %s = %r (!= %r)' % (term1Str, term2Str, result, expectedResult)) result = term1.isEqual(term2) self.failUnlessEqual(result, expectedResult, msg = '%s == %s = %r (!= %r)' % (term1Str, term2Str, result, expectedResult)) if expectedResult and types.isAppl(term2): term2 = annotation.set(term2, factory.parse("A(1)")) result = term1.isEquivalent(term2) self.failUnlessEqual(result, True, msg = '%s <=> %s = %r (!= %r)' % (term1Str, term2Str, result, True)) result = term1.isEqual(term2) self.failUnlessEqual(result, False, msg = '%s == %s = %r (!= %r)' % (term1Str, term2Str, result, False))
def remove(term, label): '''Returns a copy of this term with the annotation associated with this label removed.''' if not isinstance(label, basestring): raise TypeError("label is not a string", label) if not types.isAppl(term): return term def withoutLabel(term): return not (types.isAppl(term) and term.name == label) annos = lists.filter(withoutLabel, term.annotations) return term.setAnnotations(annos)
def get(term, label): '''Gets an annotation associated with this label.''' if not isinstance(label, basestring): raise TypeError("label is not a string", label) if not types.isAppl(term): return term def withLabel(term): return types.isAppl(term) and term.name == label if term.annotations: anno = lists.fetch(withLabel, term.annotations) if anno is not None: return anno raise ValueError("undefined annotation", label)
def withoutLabel(term): return not (types.isAppl(term) and term.name == label)
def withLabel(term): return types.isAppl(term) and term.name == label