Esempio n. 1
0
 def fromPairs(wrappedPairs):
     from typhon.objects.collections.lists import unwrapList
     d = monteMap()
     for obj in unwrapList(wrappedPairs):
         pair = unwrapList(obj)
         assert len(pair) == 2, "Not a pair!"
         d[pair[0]] = pair[1]
     return ConstMap(d)
Esempio n. 2
0
 def fromPairs(wrappedPairs):
     from typhon.objects.collections.lists import unwrapList
     d = monteMap()
     for obj in unwrapList(wrappedPairs):
         pair = unwrapList(obj)
         assert len(pair) == 2, "Not a pair!"
         d[pair[0]] = pair[1]
     return ConstMap(d)
Esempio n. 3
0
 def sortValues(self):
     # Same as sortKeys/0.
     d = monteMap()
     l = [(k, v) for k, v in self.objectMap.iteritems()]
     ValueSorter(l).sort()
     for k, v in l:
         d[k] = v
     return d
Esempio n. 4
0
 def reverse(self):
     d = monteMap()
     l = [(k, v) for k, v in self.objectMap.iteritems()]
     # Reverse it!
     l.reverse()
     for k, v in l:
         d[k] = v
     return d
Esempio n. 5
0
 def slice(self, start):
     if start < 0:
         raise userError(u"slice/1: Negative start")
     items = self.objectMap.items()[start:]
     rv = monteMap()
     for k, v in items:
         rv[k] = v
     return rv
Esempio n. 6
0
File: maps.py Progetto: dckc/typhon
 def sortValues(self):
     # Same as sortKeys/0.
     d = monteMap()
     l = [(k, v) for k, v in self.objectMap.iteritems()]
     ValueSorter(l).sort()
     for k, v in l:
         d[k] = v
     return d
Esempio n. 7
0
 def sortKeys(self):
     # Extract a list, sort it, pack it back into a dict.
     d = monteMap()
     l = [(k, v) for k, v in self.objectMap.iteritems()]
     KeySorter(l).sort()
     for k, v in l:
         d[k] = v
     return d
Esempio n. 8
0
File: maps.py Progetto: dckc/typhon
 def slice(self, start):
     if start < 0:
         raise userError(u"slice/1: Negative start")
     items = self.objectMap.items()[start:]
     rv = monteMap()
     for k, v in items:
         rv[k] = v
     return rv
Esempio n. 9
0
File: maps.py Progetto: dckc/typhon
 def sortKeys(self):
     # Extract a list, sort it, pack it back into a dict.
     d = monteMap()
     l = [(k, v) for k, v in self.objectMap.iteritems()]
     KeySorter(l).sort()
     for k, v in l:
         d[k] = v
     return d
Esempio n. 10
0
File: maps.py Progetto: dckc/typhon
 def reverse(self):
     d = monteMap()
     l = [(k, v) for k, v in self.objectMap.iteritems()]
     # Reverse it!
     l.reverse()
     for k, v in l:
         d[k] = v
     return d
Esempio n. 11
0
 def coerce(self, specimen, ej):
     from typhon.objects.collections.helpers import monteMap
     from typhon.objects.constants import NullObject
     from typhon.objects.ejectors import theThrower
     if ej is NullObject:
         ej = theThrower
     checkDeepFrozen(specimen, monteMap(), ej, specimen)
     return specimen
Esempio n. 12
0
 def coerce(self, specimen, ej):
     from typhon.objects.collections.helpers import monteMap
     from typhon.objects.constants import NullObject
     from typhon.objects.ejectors import theThrower
     if ej is NullObject:
         ej = theThrower
     checkDeepFrozen(specimen, monteMap(), ej, specimen)
     return specimen
Esempio n. 13
0
 def fromPairs(wrappedPairs):
     from typhon.objects.collections.lists import unwrapList
     d = monteMap()
     for obj in unwrapList(wrappedPairs):
         pair = unwrapList(obj)
         if len(pair) != 2:
             raise userError(u"fromPairs/1: Not a pair")
         d[pair[0]] = pair[1]
     return ConstMap(d)
Esempio n. 14
0
 def fromPairs(wrappedPairs):
     from typhon.objects.collections.lists import unwrapList
     d = monteMap()
     for obj in unwrapList(wrappedPairs):
         pair = unwrapList(obj)
         if len(pair) != 2:
             raise userError(u"fromPairs/1: Not a pair")
         d[pair[0]] = pair[1]
     return ConstMap(d)
Esempio n. 15
0
 def recv(self, atom, args):
     if atom is IMPORT_1:
         path = unwrapStr(args[0])
         # this is a hack, but the whole class is a hack :)
         if path == u"unittest":
             d = monteMap()
             d[args[0]] = self.importList.call(u"get", args)
             return ConstMap(d)
         return self.importer.performModule(path, self.importList)
     raise Refused(self, atom, args)
Esempio n. 16
0
 def slice(self, start, stop=-1):
     assert start >= 0
     if stop < 0:
         items = self.objectMap.items()[start:]
     else:
         items = self.objectMap.items()[start:stop]
     rv = monteMap()
     for k, v in items:
         rv[k] = v
     return ConstMap(rv)
Esempio n. 17
0
 def recv(self, atom, args):
     if atom is IMPORT_1:
         path = unwrapStr(args[0])
         # this is a hack, but the whole class is a hack :)
         if path == u"unittest":
             d = monteMap()
             d[args[0]] = self.importList.call(u"get", args)
             return ConstMap(d)
         return self.importer.performModule(path, self.importList)
     raise Refused(self, atom, args)
Esempio n. 18
0
 def slice(self, start, stop=-1):
     assert start >= 0
     if stop < 0:
         items = self.objectMap.items()[start:]
     else:
         items = self.objectMap.items()[start:stop]
     rv = monteMap()
     for k, v in items:
         rv[k] = v
     return ConstMap(rv)
Esempio n. 19
0
    def recv(self, atom, args):
        from typhon.objects.constants import wrapBool
        from typhon.objects.collections.helpers import monteMap
        from typhon.objects.user import Audition
        if atom is AUDIT_1:
            audition = args[0]
            if not isinstance(audition, Audition):
                raise userError(u"not an Audition")
            return wrapBool(self.audit(audition))

        if atom is COERCE_2:
            from typhon.objects.constants import NullObject
            from typhon.objects.ejectors import theThrower
            ej = args[1]
            if ej is NullObject:
                ej = theThrower
            checkDeepFrozen(args[0], monteMap(), ej, args[0])
            return args[0]

        if atom is SUPERSETOF_1:
            return wrapBool(deepFrozenSupersetOf(args[0]))
        raise Refused(self, atom, args)
Esempio n. 20
0
    def recv(self, atom, args):
        # _makeIterator/0: Create an iterator for this collection's contents.
        if atom is _MAKEITERATOR_0:
            return self._makeIterator()

        if atom is _PRINTON_1:
            printer = args[0]
            self.printOn(printer)
            return NullObject

        # contains/1: Determine whether an element is in this collection.
        if atom is CONTAINS_1:
            return wrapBool(self.contains(args[0]))

        # size/0: Get the number of elements in the collection.
        if atom is SIZE_0:
            return IntObject(self.size())

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_1:
            start = unwrapInt(args[0])
            try:
                return self.slice(start)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_2:
            start = unwrapInt(args[0])
            stop = unwrapInt(args[1])
            try:
                return self.slice(start, stop)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # snapshot/0: Create a new constant collection with a copy of the
        # current collection's contents.
        if atom is SNAPSHOT_0:
            return self.snapshot()

        from typhon.objects.collections.lists import ConstList

        if atom is _UNCALL_0:
            return ConstList(self._uncall())

        if atom is ASSET_0:
            from typhon.objects.collections.sets import ConstSet
            return ConstSet(self.objectMap)

        if atom is DIVERGE_0:
            return FlexMap(self.objectMap)

        if atom is FETCH_2:
            key = args[0]
            thunk = args[1]
            rv = self.objectMap.get(key, None)
            if rv is None:
                rv = thunk.call(u"run", [])
            return rv

        if atom is GETKEYS_0:
            return ConstList(self.objectMap.keys())

        if atom is GETVALUES_0:
            return ConstList(self.objectMap.values())

        if atom is GET_1:
            key = args[0]
            try:
                return self.objectMap[key]
            except KeyError:
                raise userError(u"Key not found: %s" % (key.toString(), ))

        # or/1: Unify the elements of this collection with another.
        if atom is OR_1:
            return self._or(args[0])

        if atom is REVERSE_0:
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            # Reverse it!
            l.reverse()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is SORTKEYS_0:
            # Extract a list, sort it, pack it back into a dict.
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            KeySorter(l).sort()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is SORTVALUES_0:
            # Same as sortKeys/0.
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            ValueSorter(l).sort()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is WITH_2:
            # Replace by key.
            key = args[0]
            value = args[1]
            d = self.objectMap.copy()
            d[key] = value
            return ConstMap(d)

        if atom is WITHOUT_1:
            key = args[0]
            d = self.objectMap.copy()
            # Ignore the case where the key wasn't in the map.
            if key in d:
                del d[key]
            return ConstMap(d)

        if atom is PUT_2:
            key = args[0]
            value = args[1]
            self.put(key, value)
            return NullObject

        if atom is REMOVEKEY_1:
            key = args[0]
            self.removeKey(key)
            return NullObject

        if atom is POP_0:
            return self.pop()

        raise Refused(self, atom, args)
Esempio n. 21
0
        if stop < 0:
            items = self.objectMap.items()[start:]
        else:
            items = self.objectMap.items()[start:stop]
        rv = monteMap()
        for k, v in items:
            rv[k] = v
        return ConstMap(rv)

    def size(self):
        return len(self.objectMap)

    def snapshot(self):
        return ConstMap(self.objectMap.copy())

EMPTY_MAP = ConstMap(monteMap())


@autohelp
@audited.Transparent
class FlexMap(Object):
    """
    An ordered map of objects.
    """

    def __init__(self, objectMap):
        self.objectMap = objectMap

    def asDict(self):
        return self.objectMap
Esempio n. 22
0
def deepFrozenSupersetOf(guard):
    from typhon.objects.collections.helpers import monteMap
    from typhon.objects.collections.lists import ConstList
    from typhon.objects.constants import wrapBool
    from typhon.objects.ejectors import Ejector
    from typhon.objects.refs import Promise
    from typhon.objects.guards import (
        AnyOfGuard, BoolGuard, BytesGuard, CharGuard, DoubleGuard,
        FinalSlotGuard, IntGuard, SameGuard, StrGuard, SubrangeGuard,
        VoidGuard)
    from typhon.prelude import getGlobalValue
    if guard is deepFrozenGuard:
        return True
    if guard is deepFrozenStamp:
        return True
    if isinstance(guard, Promise):
        guard = guard.resolution()
    if isinstance(guard, BoolGuard):
        return True
    if isinstance(guard, BytesGuard):
        return True
    if isinstance(guard, CharGuard):
        return True
    if isinstance(guard, DoubleGuard):
        return True
    if isinstance(guard, IntGuard):
        return True
    if isinstance(guard, StrGuard):
        return True
    if isinstance(guard, VoidGuard):
        return True

    if isinstance(guard, SameGuard):
        ej = Ejector()
        try:
            v = guard.value
            checkDeepFrozen(v, monteMap(), ej, v)
            return True
        except Ejecting:
            return False

    if isinstance(guard, FinalSlotGuard):
        return deepFrozenSupersetOf(guard.valueGuard)
    for superGuardName in [u"List", u"NullOk", u"Set"]:
        superGuard = getGlobalValue(superGuardName)
        if superGuard is None:
            continue
        ej = Ejector()
        try:
            subGuard = superGuard.call(u"extractGuard", [guard, ej])
            return deepFrozenSupersetOf(subGuard)
        except Ejecting:
            # XXX lets other ejectors get through
            pass
    for pairGuardName in [u"Map", u"Pair"]:
        pairGuard = getGlobalValue(pairGuardName)
        if pairGuard is None:
            continue
        ej = Ejector()
        try:
            guardPair = pairGuard.call(u"extractGuards", [guard, ej])
            if isinstance(guardPair, ConstList) and guardPair.size() == 2:
                return (
                    (deepFrozenSupersetOf(guardPair.strategy.fetch(
                        guardPair, 0))) and
                    (deepFrozenSupersetOf(guardPair.strategy.fetch(
                        guardPair, 1))))
        except Ejecting:
            # XXX lets other ejectors get through
            pass
    if (SubrangeGuard(deepFrozenGuard).call(u"passes", [guard])
            is wrapBool(True)):
        return True
    if isinstance(guard, AnyOfGuard):
        for g in guard.subguards:
            if not deepFrozenSupersetOf(g):
                return False
        return True
    return False
Esempio n. 23
0
        Like Monte m`self.with(k :Str, v)`.
        """

        return ConstMap(self._with(StrObject(k), v))

    def iteritems(self):
        """
        Iterate over (key, value) tuples.

        The normal caveats apply.
        """

        return self.objectMap.iteritems()


EMPTY_MAP = ConstMap(monteMap())


@autohelp
class FlexMap(Object):
    """
    An ordered map of objects.
    """
    def __init__(self, objectMap):
        self.objectMap = objectMap

    @method("Void", "Any")
    def _printOn(self, printer):
        printer.call(u"print", [StrObject(u"[")])
        i = 0
        for k, v in self.objectMap.iteritems():
Esempio n. 24
0
def deepFrozenSupersetOf(guard):
    from typhon.objects.collections.helpers import monteMap
    from typhon.objects.collections.lists import unwrapList
    from typhon.objects.constants import wrapBool
    from typhon.objects.ejectors import Ejector
    from typhon.objects.refs import Promise
    from typhon.objects.guards import (AnyOfGuard, BoolGuard, BytesGuard,
                                       CharGuard, DoubleGuard, FinalSlotGuard,
                                       IntGuard, SameGuard, StrGuard,
                                       SubrangeGuard, VoidGuard)
    from typhon.prelude import getGlobalValue
    if guard is deepFrozenGuard:
        return True
    if guard is deepFrozenStamp:
        return True
    if isinstance(guard, Promise):
        guard = guard.resolution()
    if isinstance(guard, BoolGuard):
        return True
    if isinstance(guard, BytesGuard):
        return True
    if isinstance(guard, CharGuard):
        return True
    if isinstance(guard, DoubleGuard):
        return True
    if isinstance(guard, IntGuard):
        return True
    if isinstance(guard, StrGuard):
        return True
    if isinstance(guard, VoidGuard):
        return True

    if isinstance(guard, SameGuard):
        with Ejector() as ej:
            try:
                v = guard.value
                checkDeepFrozen(v, monteMap(), ej, v)
                return True
            except Ejecting:
                return False

    if isinstance(guard, FinalSlotGuard):
        return deepFrozenSupersetOf(guard.valueGuard)
    for superGuardName in [u"List", u"NullOk", u"Set"]:
        superGuard = getGlobalValue(superGuardName)
        if superGuard is None:
            continue
        with Ejector() as ej:
            try:
                subGuard = superGuard.call(u"extractGuard", [guard, ej])
                return deepFrozenSupersetOf(subGuard)
            except Ejecting as e:
                # Just keep going.
                if e.ejector is not ej:
                    raise
    for pairGuardName in [u"Map", u"Pair"]:
        pairGuard = getGlobalValue(pairGuardName)
        if pairGuard is None:
            continue
        with Ejector() as ej:
            try:
                guardPair = pairGuard.call(u"extractGuards", [guard, ej])
                l = unwrapList(guardPair, ej)
                if len(l) == 2:
                    return deepFrozenSupersetOf(l[0]) and deepFrozenSupersetOf(
                        l[1])
            except Ejecting as e:
                if e.ejector is not ej:
                    raise
    if (SubrangeGuard(deepFrozenGuard).call(u"passes", [guard]) is
            wrapBool(True)):
        return True
    if isinstance(guard, AnyOfGuard):
        for g in guard.subguards:
            if not deepFrozenSupersetOf(g):
                return False
        return True
    return False
Esempio n. 25
0
def deepFrozenSupersetOf(guard):
    from typhon.objects.collections.helpers import monteMap
    from typhon.objects.collections.lists import ConstList
    from typhon.objects.constants import wrapBool
    from typhon.objects.ejectors import Ejector
    from typhon.objects.refs import Promise
    from typhon.objects.guards import (
        AnyOfGuard, BoolGuard, BytesGuard, CharGuard, DoubleGuard,
        FinalSlotGuard, IntGuard, SameGuard, StrGuard, SubrangeGuard,
        VoidGuard)
    from typhon.prelude import getGlobalValue
    if guard is deepFrozenGuard:
        return True
    if guard is deepFrozenStamp:
        return True
    if isinstance(guard, Promise):
        guard = guard.resolution()
    if isinstance(guard, BoolGuard):
        return True
    if isinstance(guard, BytesGuard):
        return True
    if isinstance(guard, CharGuard):
        return True
    if isinstance(guard, DoubleGuard):
        return True
    if isinstance(guard, IntGuard):
        return True
    if isinstance(guard, StrGuard):
        return True
    if isinstance(guard, VoidGuard):
        return True

    if isinstance(guard, SameGuard):
        ej = Ejector()
        try:
            v = guard.value
            checkDeepFrozen(v, monteMap(), ej, v)
            return True
        except Ejecting:
            return False

    if isinstance(guard, FinalSlotGuard):
        return deepFrozenSupersetOf(guard.valueGuard)
    for superGuardName in [u"List", u"NullOk", u"Set"]:
        superGuard = getGlobalValue(superGuardName)
        if superGuard is None:
            continue
        ej = Ejector()
        try:
            subGuard = superGuard.call(u"extractGuard", [guard, ej])
            return deepFrozenSupersetOf(subGuard)
        except Ejecting:
            # XXX lets other ejectors get through
            pass
    for pairGuardName in [u"Map", u"Pair"]:
        pairGuard = getGlobalValue(pairGuardName)
        if pairGuard is None:
            continue
        ej = Ejector()
        try:
            guardPair = pairGuard.call(u"extractGuards", [guard, ej])
            if isinstance(guardPair, ConstList) and guardPair.size() == 2:
                return (
                    (deepFrozenSupersetOf(guardPair.strategy.fetch(
                        guardPair, 0))) and
                    (deepFrozenSupersetOf(guardPair.strategy.fetch(
                        guardPair, 1))))
        except Ejecting:
            # XXX lets other ejectors get through
            pass
    if (SubrangeGuard(deepFrozenGuard).call(u"passes", [guard])
            is wrapBool(True)):
        return True
    if isinstance(guard, AnyOfGuard):
        for g in guard.subguards:
            if not deepFrozenSupersetOf(g):
                return False
        return True
    return False
Esempio n. 26
0
    def recv(self, atom, args):
        # _makeIterator/0: Create an iterator for this collection's contents.
        if atom is _MAKEITERATOR_0:
            return self._makeIterator()

        if atom is _PRINTON_1:
            printer = args[0]
            self.printOn(printer)
            return NullObject

        # contains/1: Determine whether an element is in this collection.
        if atom is CONTAINS_1:
            return wrapBool(self.contains(args[0]))

        # size/0: Get the number of elements in the collection.
        if atom is SIZE_0:
            return IntObject(self.size())

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_1:
            start = unwrapInt(args[0])
            try:
                return self.slice(start)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_2:
            start = unwrapInt(args[0])
            stop = unwrapInt(args[1])
            try:
                return self.slice(start, stop)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # snapshot/0: Create a new constant collection with a copy of the
        # current collection's contents.
        if atom is SNAPSHOT_0:
            return self.snapshot()

        from typhon.objects.collections.lists import ConstList

        if atom is _UNCALL_0:
            return ConstList(self._uncall())

        if atom is ASSET_0:
            from typhon.objects.collections.sets import ConstSet
            return ConstSet(self.objectMap)

        if atom is DIVERGE_0:
            return FlexMap(self.objectMap)

        if atom is FETCH_2:
            key = args[0]
            thunk = args[1]
            rv = self.objectMap.get(key, None)
            if rv is None:
                rv = thunk.call(u"run", [])
            return rv

        if atom is GETKEYS_0:
            return ConstList(self.objectMap.keys())

        if atom is GETVALUES_0:
            return ConstList(self.objectMap.values())

        if atom is GET_1:
            key = args[0]
            try:
                return self.objectMap[key]
            except KeyError:
                raise userError(u"Key not found: %s" % (key.toString(),))

        # or/1: Unify the elements of this collection with another.
        if atom is OR_1:
            return self._or(args[0])

        if atom is REVERSE_0:
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            # Reverse it!
            l.reverse()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is SORTKEYS_0:
            # Extract a list, sort it, pack it back into a dict.
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            KeySorter(l).sort()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is SORTVALUES_0:
            # Same as sortKeys/0.
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            ValueSorter(l).sort()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is WITH_2:
            # Replace by key.
            key = args[0]
            value = args[1]
            d = self.objectMap.copy()
            d[key] = value
            return ConstMap(d)

        if atom is WITHOUT_1:
            key = args[0]
            d = self.objectMap.copy()
            # Ignore the case where the key wasn't in the map.
            if key in d:
                del d[key]
            return ConstMap(d)

        if atom is PUT_2:
            key = args[0]
            value = args[1]
            self.put(key, value)
            return NullObject

        if atom is REMOVEKEY_1:
            key = args[0]
            self.removeKey(key)
            return NullObject

        if atom is POP_0:
            return self.pop()

        raise Refused(self, atom, args)
Esempio n. 27
0
 def __init__(self, objectMap, keyGuard=None, valueGuard=None):
     self._kg = keyGuard
     self._vg = valueGuard
     self.objectMap = monteMap()
     for k, v in objectMap.iteritems():
         self.objectMap[self.coerceKey(k)] = self.coerceValue(v)