lib.py :  » Web-Services » RPyC » rpyc-3.0.7 » rpyc » utils » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Services » RPyC 
RPyC » rpyc 3.0.7 » rpyc » utils » lib.py
"""
various library utilities (also for compatibility with python2.4)
"""
try:
    from struct import Struct
except ImportError:
    import struct
    class Struct(object):
        __slots__ = ["format", "size"]
        def __init__(self, format):
            self.format = format
            self.size = struct.calcsize(format)
        def pack(self, *args):
            return struct.pack(self.format, *args)
        def unpack(self, data):
            return struct.unpack(self.format, data)

try:
    all = all
except NameError:
    def all(seq):
        for elem in seq:
            if not elem:
                return False
        return True

try:
    callable = callable
except NameError:
    def callable(obj):
        return hasattr(obj, "__call__")

from threading import Lock,RLock,Event

import weakref
#from weakref import WeakValueDictionary as WeakValueDict

class WeakValueDict(object):
    """a light-weight version of weakref.WeakValueDictionary"""
    __slots__ = ("_dict",)
    def __init__(self):
        self._dict = {}
    def __repr__(self):
        return repr(self._dict)
    def __iter__(self):
        return self.iterkeys()
    def __len__(self):
        return len(self._dict)
    def __contains__(self, key):
        try:
            self[key]
        except KeyError:
            return False
        else:
            return True
    def get(self, key, default = None):
        try:
            return self[key]
        except KeyError:
            return default
    def __getitem__(self, key):
        obj = self._dict[key]()
        if obj is None:
            raise KeyError(key)
        return obj
    def __setitem__(self, key, value):
        def remover(wr, _dict = self._dict, key = key):
            _dict.pop(key, None)
        self._dict[key] = weakref.ref(value, remover)
    def __delitem__(self, key):
        del self._dict[key]
    def iterkeys(self):
        return self._dict.iterkeys()
    def keys(self):
        return self._dict.keys()
    def itervalues(self):
        for k in self:
            yield self[k]
    def values(self):
        return list(self.itervalues())
    def iteritems(self):
        for k in self:
            yield k, self[k]
    def items(self):
        return list(self.iteritems())
    def clear(self):
        self._dict.clear()

class RefCountingColl(object):
    """a set-like object that implements refcounting on its contained objects"""
    __slots__ = ("_lock", "_dict")
    def __init__(self):
        self._lock = Lock()
        self._dict = {}
    def __repr__(self):
        return repr(self._dict)
    def add(self, obj):
        self._lock.acquire()
        try:
            key = id(obj)
            slot = self._dict.get(key, None)
            if slot is None:
                slot = [obj, 0]
            else:
                slot[1] += 1
            self._dict[key] = slot
        finally:
            self._lock.release()
    def clear(self):
        self._lock.acquire()
        try:
            self._dict.clear()
        finally:
            self._lock.release()
    def decref(self, key):
        self._lock.acquire()
        try:
            slot = self._dict[key]
            if slot[1] <= 1:
                del self._dict[key]
            else:
                slot[1] -= 1
                self._dict[key] = slot
        finally:
            self._lock.release()
    def __getitem__(self, key):
        self._lock.acquire()
        try:
            return self._dict[key][0]
        finally:
            self._lock.release()


class MissingModule(object):
    __slots__ = ["__name"]
    def __init__(self, name):
        self.__name = name
    def __getattr__(self, name):
        raise ImportError("module %r not found" % (self.__name,))

def safe_import(name):
    try:
        mod = __import__(name, None, None, "*")
    except ImportError:
        mod = MissingModule(name)
    return mod











www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.