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())
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)
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)
def getMethods(self): d = monteSet() for atom in self.atoms: d[ComputedMethod(atom.arity, None, atom.verb)] = None return d
def testToString(self): d = monteSet() d[IntObject(42)] = None self.assertEqual(ConstSet(d).toString(), u"[42].asSet()")
def testToStringEmpty(self): d = monteSet() self.assertEqual(ConstSet(d).toString(), u"[].asSet()")
def getMethods(self): return monteSet()
def asSet(self): from typhon.objects.collections.sets import monteSet d = monteSet() for c in self._bs: d[IntObject(ord(c))] = None return d
def asSet(self): from typhon.objects.collections.sets import monteSet d = monteSet() for o in self.objs: d[o] = None return d
def getMethods(self): d = monteSet() for atom, docstring in self.atoms.iteritems(): d[ComputedMethod(atom.arity, docstring, atom.verb)] = None return d
def asSet(self): from typhon.objects.collections.sets import monteSet d = monteSet() for c in self._s: d[CharObject(c)] = None return d
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