Example #1
0
 def testHashEqual(self):
     d = monteSet()
     d[IntObject(42)] = None
     d[CharObject(u'¡')] = None
     a = ConstSet(d)
     b = ConstSet(d)
     self.assertEqual(a.hash(), b.hash())
Example #2
0
 def testHashEqual(self):
     d = monteSet()
     d[IntObject(42)] = None
     d[CharObject(u'¡')] = None
     a = ConstSet(d)
     b = ConstSet(d)
     self.assertEqual(a.hash(), b.hash())
Example #3
0
    def recv(self, atom, args):
        if atom is GETDOCSTRING_0:
            if self.docstring is not None:
                return StrObject(self.docstring)
            return NullObject

        if atom is GETMETHODS_0:
            d = monteSet()
            for atom in self.atoms:
                d[ComputedMethod(atom.arity, None, atom.verb)] = None
            return ConstSet(d)

        raise Refused(self, atom, args)
Example #4
0
    def recv(self, atom, args):
        if atom is GETDOCSTRING_0:
            if self.docstring is not None:
                return StrObject(self.docstring)
            return NullObject

        if atom is GETMETHODS_0:
            d = monteSet()
            for atom in self.atoms:
                d[ComputedMethod(atom.arity, None, atom.verb)] = None
            return ConstSet(d)

        raise Refused(self, atom, args)
Example #5
0
    def recv(self, atom, args):
        if atom is COERCE_2:
            return args[0]

        if atom is SUPERSETOF_1:
            return wrapBool(True)

        if atom is EXTRACTGUARDS_2:
            g = args[0]
            ej = args[1]
            if isinstance(g, AnyOfGuard):
                return ConstList(g.subguards)
            else:
                ej.call(u"run", [StrObject(u"Not an AnyOf guard")])

        if atom is GETMETHODS_0:
            return ConstSet(monteSet())

        if atom.verb == u"get":
            return AnyOfGuard(args)

        raise Refused(self, atom, args)
Example #6
0
    def recv(self, atom, args):
        if atom is COERCE_2:
            return args[0]

        if atom is SUPERSETOF_1:
            return wrapBool(True)

        if atom is EXTRACTGUARDS_2:
            g = args[0]
            ej = args[1]
            if isinstance(g, AnyOfGuard):
                return ConstList(g.subguards)
            else:
                ej.call(u"run", [StrObject(u"Not an AnyOf guard")])

        if atom is GETMETHODS_0:
            return ConstSet(monteSet())

        if atom.verb == u"get":
            return AnyOfGuard(args)

        raise Refused(self, atom, args)
Example #7
0
 def getMethods(self):
     d = monteSet()
     for atom in self.atoms:
         d[ComputedMethod(atom.arity, None, atom.verb)] = None
     return d
Example #8
0
 def testToString(self):
     d = monteSet()
     d[IntObject(42)] = None
     self.assertEqual(ConstSet(d).toString(), u"[42].asSet()")
Example #9
0
 def testToStringEmpty(self):
     d = monteSet()
     self.assertEqual(ConstSet(d).toString(), u"[].asSet()")
Example #10
0
 def getMethods(self):
     return monteSet()
Example #11
0
 def asSet(self):
     from typhon.objects.collections.sets import monteSet
     d = monteSet()
     for c in self._bs:
         d[IntObject(ord(c))] = None
     return d
Example #12
0
 def asSet(self):
     from typhon.objects.collections.sets import monteSet
     d = monteSet()
     for c in self._bs:
         d[IntObject(ord(c))] = None
     return d
Example #13
0
 def testToStringEmpty(self):
     d = monteSet()
     self.assertEqual(ConstSet(d).toString(), u"[].asSet()")
Example #14
0
File: lists.py Project: dckc/typhon
 def asSet(self):
     from typhon.objects.collections.sets import monteSet
     d = monteSet()
     for o in self.objs:
         d[o] = None
     return d
Example #15
0
 def asSet(self):
     from typhon.objects.collections.sets import monteSet
     d = monteSet()
     for o in self.objs:
         d[o] = None
     return d
Example #16
0
 def getMethods(self):
     d = monteSet()
     for atom, docstring in self.atoms.iteritems():
         d[ComputedMethod(atom.arity, docstring, atom.verb)] = None
     return d
Example #17
0
 def asSet(self):
     from typhon.objects.collections.sets import monteSet
     d = monteSet()
     for c in self._s:
         d[CharObject(c)] = None
     return d
Example #18
0
 def testToString(self):
     d = monteSet()
     d[IntObject(42)] = None
     self.assertEqual(ConstSet(d).toString(), u"[42].asSet()")
Example #19
0
 def asSet(self):
     from typhon.objects.collections.sets import monteSet
     d = monteSet()
     for c in self._s:
         d[CharObject(c)] = None
     return d
Example #20
0
def autoguard(*tys, **kwargs):
    """
    AutoGuard automatically generates a guard class for a given list of types.

    All types must be helped by AutoHelp first.

    The `subCoerce` kwarg allows for customizing the coercion test. Return
    None to signal failure.

    NOT_RPYTHON
    """

    from typhon.objects.collections.sets import monteSet
    from typhon.objects.ejectors import throwStr
    from typhon.objects.interfaces import ComputedMethod
    from typhon.objects.refs import resolution
    from typhon.objects.root import Object

    # Set up subCoerce.
    subCoerce = kwargs.pop("subCoerce", None)
    name = kwargs.pop("name", u"<autoguard>")
    # We can't iterate over tuples, so we have to iterate over a list instead.
    tys = list(tys)

    if subCoerce is None:
        def subCoerce(specimen):
            for ty in unrolling_iterable(tys):
                if isinstance(specimen, ty):
                    return specimen

    doc = tys[0].__doc__.decode("utf-8")
    methods = monteSet()
    # Since this is NOT_RPYTHON, we can use a relatively direct method to
    # double-check that we do not have overlapping or renegade atoms.
    seenAtoms = {}
    for notFirst, ty in enumerate(tys):
        neededAtoms = seenAtoms.copy()
        for _, (f, verb, args, _, _) in harvestMethods(ty).iteritems():
            ds = f.__doc__
            if ds is not None:
                ds = ds.decode("utf-8")
            key = len(args), verb
            assert not notFirst or key in seenAtoms, (
                    "Type %r has new method %s/%d not in previous types" %
                    (ty, verb, len(args)))
            if key in seenAtoms:
                neededAtoms.pop(key, None)
            else:
                seenAtoms[key] = ty
                cm = ComputedMethod(len(args), ds, verb)
                methods[cm] = None
        assert not neededAtoms, (
                "Type %r was missing methods: %s" %
                ",".join("%s/%d" % key for key in neededAtoms))

    @autohelp
    class Guard(Object):
        """
        A guard.
        """

        def toString(self):
            return name

        @method("Any", "Any", "Any")
        def coerce(self, specimen, ej):
            specimen = resolution(specimen)
            val = subCoerce(specimen)
            if val is None:
                try:
                    newspec = specimen.call(u"_conformTo", [self])
                except UserException:
                    msg = u"%s threw exception while conforming to %s" % (
                            specimen.toQuote(), self.toQuote())
                    throwStr(ej, msg)
                else:
                    val = subCoerce(newspec)
                    if val is None:
                        throwStr(ej, u"%s does not conform to %s" % (
                            specimen.toQuote(), self.toQuote()))
                    else:
                        return val
            else:
                return val

        @method.py("Bool", "Any")
        def supersetOf(self, other):
            return False

        @method("Str")
        def getDocstring(self):
            return doc

        @method("Set")
        def getMethods(self):
            return methods

    return Guard
Example #21
0
 def getMethods(self):
     d = monteSet()
     for atom in self.atoms:
         d[ComputedMethod(atom.arity, None, atom.verb)] = None
     return d
Example #22
0
def autoguard(*tys, **kwargs):
    """
    AutoGuard automatically generates a guard class for a given list of types.

    All types must be helped by AutoHelp first.

    The `subCoerce` kwarg allows for customizing the coercion test. Return
    None to signal failure.

    NOT_RPYTHON
    """

    from typhon.objects.collections.sets import monteSet
    from typhon.objects.ejectors import throwStr
    from typhon.objects.interfaces import ComputedMethod
    from typhon.objects.refs import resolution
    from typhon.objects.root import Object

    # Set up subCoerce.
    subCoerce = kwargs.pop("subCoerce", None)
    name = kwargs.pop("name", u"<autoguard>")
    # We can't iterate over tuples, so we have to iterate over a list instead.
    tys = list(tys)

    if subCoerce is None:
        def subCoerce(specimen):
            for ty in unrolling_iterable(tys):
                if isinstance(specimen, ty):
                    return specimen

    doc = tys[0].__doc__.decode("utf-8")
    methods = monteSet()
    # Since this is NOT_RPYTHON, we can use a relatively direct method to
    # double-check that we do not have overlapping or renegade atoms.
    seenAtoms = {}
    for notFirst, ty in enumerate(tys):
        neededAtoms = seenAtoms.copy()
        for _, (f, verb, args, _, _) in harvestMethods(ty).iteritems():
            ds = f.__doc__
            if ds is not None:
                ds = ds.decode("utf-8")
            key = len(args), verb
            assert not notFirst or key in seenAtoms, (
                    "Type %r has new method %s/%d not in previous types" %
                    (ty, verb, len(args)))
            if key in seenAtoms:
                neededAtoms.pop(key, None)
            else:
                seenAtoms[key] = ty
                cm = ComputedMethod(len(args), ds, verb)
                methods[cm] = None
        assert not neededAtoms, (
                "Type %r was missing methods: %s" %
                ",".join("%s/%d" % key for key in neededAtoms))

    @autohelp
    class Guard(Object):
        """
        A guard.
        """

        def toString(self):
            return name

        @method("Any", "Any", "Any")
        def coerce(self, specimen, ej):
            specimen = resolution(specimen)
            val = subCoerce(specimen)
            if val is None:
                try:
                    newspec = specimen.call(u"_conformTo", [self])
                except UserException:
                    msg = u"%s threw exception while conforming to %s" % (
                            specimen.toQuote(), self.toQuote())
                    throwStr(ej, msg)
                else:
                    val = subCoerce(newspec)
                    if val is None:
                        throwStr(ej, u"%s does not conform to %s" % (
                            specimen.toQuote(), self.toQuote()))
                    else:
                        return val
            else:
                return val

        @method.py("Bool", "Any")
        def supersetOf(self, other):
            return False

        @method("Str")
        def getDocstring(self):
            return doc

        @method("Set")
        def getMethods(self):
            return methods

    return Guard