Beispiel #1
0
class Var(ARef, Settable, IFn, IPrintable):
    def __init__(self, *args):
        """Var initializer

        Valid calls:
        - Var(namespace, symbol, root)
        - Var(namespace, symbol) -- unbound Var
        - Var(root) -- anonymous Var
        - Var() -- anonymous, unbound Var
        """
        self.ns = args[0] if len(args) >= 2 else None
        self.sym = args[1] if len(args) >= 2 else None
        root = args[-1] if len(args) % 2 else UNKNOWN
        self.root = AtomicReference(root if root != UNKNOWN else Unbound(self))
        self.threadBound = False
        self._meta = EMPTY
        self.dynamic = False
        self.public = True

    def setDynamic(self, val=True):
        self.dynamic = val
        return self

    def isDynamic(self):
        return self.dynamic

    def setPublic(self, public=True):
        self.public = public

    def isPublic(self):
        return self.public

    def isBound(self):
        return self.getThreadBinding() is not None \
                or not isinstance(self.root.get(), Unbound)

    def set(self, val):
        self.validate(self.getValidator(), val)
        b = self.getThreadBinding()
        if b is not None:
            if currentThread() != b.thread:
                raise IllegalStateException(
                    "Can't set!: {0} from non-binding thread".format(self.sym))
            b.val = val
            return self

        raise IllegalStateException(
            "Can't change/establish root binding of: {0} with set".format(
                self.sym))

    def alterRoot(self, fn, args):
        return self.root.mutate(lambda old: fn(old, *(args if args else ())))

    def hasRoot(self):
        return not isinstance(self.root.get(), Unbound)

    def bindRoot(self, root):
        self.validate(self.getValidator(), root)
        self.root.set(root)
        return self

    def __call__(self, *args, **kw):
        """Exists for Python interop, don't use in clojure code"""
        return self.deref()(*args, **kw)

    def deref(self):
        b = self.getThreadBinding()
        if b is not None:
            return b.val
        return self.root.get()

    def getThreadBinding(self):
        if self.threadBound:
            e = dvals.get(Frame).bindings.entryAt(self)
            if e is not None:
                return e.getValue()
        return None

    def setMeta(self, meta):
        self._meta = meta
        if self._meta and self._meta[STATIC_KEY]:
            self.setDynamic(False)
        return self

    def setMacro(self):
        self.alterMeta(lambda x, y, z: x.assoc(y, z), macrokey, True)

    def writeAsString(self, writer):
        writer.write(repr(self))

    def writeAsReplString(self, writer):
        writer.write(repr(self))

    def __repr__(self):
        if self.ns is not None:
            return "#'{0}/{1}".format(self.ns.__name__, self.sym)
        return "#<Var: {0}>".format(self.sym or "--unnamed--")
Beispiel #2
0
class Var(ARef, Settable, IFn, IPrintable):
    def __init__(self, ns, sym, root=UKNOWN):

        self.ns = ns
        self.sym = sym
        self.threadBound = False

        if root == UKNOWN:
            self.root = AtomicReference(Unbound(self))
        else:
            self.root = AtomicReference(root)

        self._meta = EMPTY
        self.rev = 0
        self.dynamic = False
        self.public = True

        if isinstance(self.root.get(), Unbound):
            self.rev += 1

    def setDynamic(self, val=True):
        self.dynamic = val
        return self

    def isDynamic(self):
        return self.dynamic

    def setPublic(self, public=True):
        self.public = public

    def isPublic(self):
        return self.public

    def isBound(self):
        return not isinstance(self.root.get(), Unbound)

    def set(self, val):
        self.validate(self.getValidator(), val)
        b = self.getThreadBinding()
        if b is not None:
            if currentThread() != b.thread:
                raise IllegalStateException("Can't set!: " + str(self.sym) +
                                            " from non-binding thread")
            b.val = val
            return self

        raise IllegalStateException("Can't change/establish root binding "
                                    "of: %s with set" % str(self.sym))

    def alterRoot(self, fn, args):
        return self.root.mutate(lambda old: fn(old, *(args if args else ())))

    def hasRoot(self):
        return not isinstance(self.root.get(), Unbound)

    def bindRoot(self, root):
        self.validate(self.getValidator(), root)
        oldroot = self.root.get()
        self.root.set(root)
        self.rev += 1
        return self

    def __call__(self, *args, **kw):
        """Exists for Python interop, don't use in clojure code"""
        return self.deref()(*args, **kw)

    def deref(self):
        b = self.getThreadBinding()
        if b is not None:
            return b.val
        return self.root.get()

    def getThreadBinding(self):
        if self.threadBound:
            e = dvals.get(lambda: Frame()).bindings.entryAt(self)
            if e is not None:
                return e.getValue()
        return None

    def setMeta(self, meta):
        self._meta = meta
        if self._meta and self._meta[STATIC_KEY]:
            self.setDynamic(False)
        return self

    def setMacro(self):
        self.alterMeta(lambda x, y, z: x.assoc(y, z), macrokey, True)

    def writeAsString(self, writer):
        writer.write(repr(self))

    def writeAsReplString(self, writer):
        writer.write(repr(self))

    def __repr__(self):
        if self.ns is not None:
            return "#'" + str(self.ns.__name__) + "/" + str(self.sym)
        return ("#<Var: " +
                (str(self.sym) if self.sym is not None else "--unnamed--") +
                ">")
Beispiel #3
0
class Var(ARef, Settable, IFn, IPrintable):
    def __init__(self, ns, sym, root=UKNOWN):

        self.ns = ns
        self.sym = sym
        self.threadBound = False
        
        if root == UKNOWN:
            self.root = AtomicReference(Unbound(self))
        else:
            self.root = AtomicReference(root)
            
        self._meta = EMPTY
        self.rev = 0
        self.dynamic = False
        self.public = True

        
        if isinstance(self.root.get(), Unbound):
            self.rev += 1

    def setDynamic(self, val=True):
        self.dynamic = val
        return self

    def isDynamic(self):
        return self.dynamic
        
    def setPublic(self, public = True):
        self.public = public
        
    def isPublic(self):
        return self.public
        
    def isBound(self):
        return self.getThreadBinding() is not None \
                or not isinstance(self.root.get(), Unbound)

    def set(self, val):
        self.validate(self.getValidator(), val)
        b = self.getThreadBinding()
        if b is not None:
            if currentThread() != b.thread:
                raise IllegalStateException("Can't set!: " + str(self.sym) +
                                            " from non-binding thread")
            b.val = val
            return self

        raise IllegalStateException("Can't change/establish root binding "
                                    "of: %s with set" % str(self.sym))
        
    def alterRoot(self, fn, args):
        return self.root.mutate(lambda old: fn(old, *(args if args else ())))

    def hasRoot(self):
        return not isinstance(self.root.get(), Unbound)

    def bindRoot(self, root):
        self.validate(self.getValidator(), root)
        oldroot = self.root.get()
        self.root.set(root)
        self.rev += 1
        return self

    def __call__(self, *args, **kw):
        """Exists for Python interop, don't use in clojure code"""
        return self.deref()(*args, **kw)

    def deref(self):
        b = self.getThreadBinding()
        if b is not None:
            return b.val
        return self.root.get()

    def getThreadBinding(self):
        if self.threadBound:
            e = dvals.get(lambda: Frame()).bindings.entryAt(self)
            if e is not None:
                return e.getValue()
        return None

    def setMeta(self, meta):
        self._meta = meta
        if self._meta and self._meta[STATIC_KEY]:
            self.setDynamic(False)
        return self

    def setMacro(self):
        self.alterMeta(lambda x, y, z: x.assoc(y, z), macrokey, True)

    def writeAsString(self, writer):
        writer.write(repr(self))

    def writeAsReplString(self, writer):
        writer.write(repr(self))

    def __repr__(self):
        if self.ns is not None:
            return "#'" + str(self.ns.__name__) + "/" + str(self.sym)
        return ("#<Var: " +
                (str(self.sym)
                 if self.sym is not None else "--unnamed--") + ">")
Beispiel #4
0
class Var(ARef, Settable, IFn, IPrintable):
    def __init__(self, *args):
        """Var initializer

        Valid calls:
        - Var(namespace, symbol, root)
        - Var(namespace, symbol) -- unbound Var
        - Var(root) -- anonymous Var
        - Var() -- anonymous, unbound Var
        """
        self.ns = args[0] if len(args) >= 2 else None
        self.sym = args[1] if len(args) >= 2 else None
        root = args[-1] if len(args) % 2 else UNKNOWN
        self.root = AtomicReference(root if root != UNKNOWN else Unbound(self))
        self.threadBound = False
        self._meta = EMPTY
        self.dynamic = False
        self.public = True

    def setDynamic(self, val=True):
        self.dynamic = val
        return self

    def isDynamic(self):
        return self.dynamic
        
    def setPublic(self, public = True):
        self.public = public
        
    def isPublic(self):
        return self.public
        
    def isBound(self):
        return self.getThreadBinding() is not None \
                or not isinstance(self.root.get(), Unbound)

    def set(self, val):
        self.validate(self.getValidator(), val)
        b = self.getThreadBinding()
        if b is not None:
            if currentThread() != b.thread:
                raise IllegalStateException(
                    "Can't set!: {0} from non-binding thread".format(self.sym))
            b.val = val
            return self

        raise IllegalStateException(
            "Can't change/establish root binding of: {0} with set".
            format(self.sym))
        
    def alterRoot(self, fn, args):
        return self.root.mutate(lambda old: fn(old, *(args if args else ())))

    def hasRoot(self):
        return not isinstance(self.root.get(), Unbound)

    def bindRoot(self, root):
        self.validate(self.getValidator(), root)
        self.root.set(root)
        return self

    def __call__(self, *args, **kw):
        """Exists for Python interop, don't use in clojure code"""
        return self.deref()(*args, **kw)

    def deref(self):
        b = self.getThreadBinding()
        if b is not None:
            return b.val
        return self.root.get()

    def getThreadBinding(self):
        if self.threadBound:
            e = dvals.get(Frame).bindings.entryAt(self)
            if e is not None:
                return e.getValue()
        return None

    def setMeta(self, meta):
        self._meta = meta
        if self._meta and self._meta[STATIC_KEY]:
            self.setDynamic(False)
        return self

    def setMacro(self):
        self.alterMeta(lambda x, y, z: x.assoc(y, z), macrokey, True)

    def writeAsString(self, writer):
        writer.write(repr(self))

    def writeAsReplString(self, writer):
        writer.write(repr(self))

    def __repr__(self):
        if self.ns is not None:
            return "#'{0}/{1}".format(self.ns.__name__, self.sym)
        return "#<Var: {0}>".format(self.sym or "--unnamed--")