Example #1
0
    def coerce(self, value):
        if value is None:
            return None

        if isinstance(value, Entity):
            return value.identity

        if isinstance(value, identity.Identity):
            return value

        value_repr = None
        try:
            value_repr = repr(value)
        except Exception as e:
            raise TypeError(
                ("Object passed to coerce is not an identity. Additionally, "
                 "calling repr(object) raised %s.") % e)

        if superposition.insuperposition(value):
            if superposition.state_type(value) == identity.Identity:
                return value

            raise TypeError(
                "Object being coerced as identity is a superposition %s of "
                "variant type %r." % (value_repr, value.type_name))

        raise TypeError("%s is not an identity." % value_repr)
Example #2
0
    def coerce(self, value):
        if value is None:
            return None

        if isinstance(value, Entity):
            return value.identity

        if isinstance(value, identity.Identity):
            return value

        value_repr = None
        try:
            value_repr = repr(value)
        except Exception as e:
            raise TypeError(
                ("Object passed to coerce is not an identity. Additionally, "
                 "calling repr(object) raised %s.") % e)

        if superposition.insuperposition(value):
            if superposition.state_type(value) == identity.Identity:
                return value

            raise TypeError(
                "Object being coerced as identity is a superposition %s of "
                "variant type %r." % (value_repr, value.type_name))

        raise TypeError("%s is not an identity." % value_repr)
Example #3
0
    def issuperset(self, other):
        if isinstance(other, type(self)):
            return self._delegate >= other._delegate

        if not superposition.insuperposition(other):
            return self.hasstate(superposition.getstate(other))

        other_states = self._make_delegate()
        other_states.update(superposition.getstates(other))

        return self._delegate >= other_states
Example #4
0
    def issuperset(self, other):
        if isinstance(other, type(self)):
            return self._delegate >= other._delegate

        if not superposition.insuperposition(other):
            return self.hasstate(superposition.getstate(other))

        other_states = self._make_delegate()
        other_states.update(superposition.getstates(other))

        return self._delegate >= other_states
Example #5
0
    def visit_Let(self, expr, **_):
        saved_bindings = self.bindings
        if isinstance(expr, expression.LetAny):
            union_semantics = True
        elif isinstance(expr, expression.LetEach):
            union_semantics = False
        else:
            union_semantics = None

        if not isinstance(expr.context, expression.Binding):
            raise ValueError(
                "Left operand of Let must be a Binding expression.")

        # Context to rebind to. This is the key that will be selected from
        # current bindings and become the new bindings for ever subexpression.
        context = expr.context.value

        try:
            rebind = associative.resolve(saved_bindings, context)

            if not rebind:  # No value from context.
                return None

            if union_semantics is None:
                # This is a simple let, which does not permit superposition
                # semantics.
                if superposition.insuperposition(rebind):
                    raise TypeError(
                        "A Let expression doesn't permit superposition "
                        "semantics. Use LetEach or LetAny instead.")

                self.bindings = rebind
                return self.visit(expr.expression)

            # If we're using union or intersection semantics, the type of
            # rebind MUST be a Superposition, even if it happens to have
            # only one state. If the below throws a type error then the
            # query is invalid and should fail here.
            result = False
            for state in superposition.getstates(rebind):
                self.bindings = state
                result = self.visit(expr.expression)
                if result and union_semantics:
                    return result

                if not result and not union_semantics:
                    return False

            return result
        finally:
            self.bindings = saved_bindings
Example #6
0
    def visit_Let(self, expr, **_):
        saved_bindings = self.bindings
        if isinstance(expr, expression.LetAny):
            union_semantics = True
        elif isinstance(expr, expression.LetEach):
            union_semantics = False
        else:
            union_semantics = None

        if not isinstance(expr.context, expression.Binding):
            raise ValueError(
                "Left operand of Let must be a Binding expression.")

        # Context to rebind to. This is the key that will be selected from
        # current bindings and become the new bindings for ever subexpression.
        context = expr.context.value

        try:
            rebind = associative.resolve(saved_bindings, context)

            if not rebind:  # No value from context.
                return None

            if union_semantics is None:
                # This is a simple let, which does not permit superposition
                # semantics.
                if superposition.insuperposition(rebind):
                    raise TypeError(
                        "A Let expression doesn't permit superposition "
                        "semantics. Use LetEach or LetAny instead.")

                self.bindings = rebind
                return self.visit(expr.expression)

            # If we're using union or intersection semantics, the type of
            # rebind MUST be a Superposition, even if it happens to have
            # only one state. If the below throws a type error then the
            # query is invalid and should fail here.
            result = False
            for state in superposition.getstates(rebind):
                self.bindings = state
                result = self.visit(expr.expression)
                if result and union_semantics:
                    return result

                if not result and not union_semantics:
                    return False

            return result
        finally:
            self.bindings = saved_bindings
Example #7
0
    def testCreation(self):
        """Test that creation is reasonable."""
        # Providing the same object twice will still build a superposition.
        s = superposition.superposition("foo", "foo")
        # This object is a superposition type...
        self.assertIsInstance(s, superposition.ISuperposition)

        # ...but it is not IN superposition.
        self.assertFalse(superposition.insuperposition(s))

        # Using meld is sometimes more convenient for this.
        s = superposition.meld("foo", "foo")
        # This object is actually a string.
        self.assertIsInstance(s, six.string_types)
        # It can still be manipulated with the superposition-aware protocol,
        # as can any scalar.
        self.assertEqual(s, superposition.getstate(s))
Example #8
0
    def testCreation(self):
        """Test that creation is reasonable."""

        # Providing the same object twice will still build a superposition.
        s = superposition.superposition("foo", "foo")
        # This object is a superposition type...
        self.assertIsInstance(s, superposition.ISuperposition)

        # ...but it is not IN superposition.
        self.assertFalse(superposition.insuperposition(s))

        # Using meld is sometimes more convenient for this.
        s = superposition.meld("foo", "foo")
        # This object is actually a string.
        self.assertIsInstance(s, basestring)
        # It can still be manipulated with the superposition-aware protocol,
        # as can any scalar.
        self.assertEqual(s, superposition.getstate(s))