当前位置: 首页>>代码示例>>Python>>正文


Python WeakValueDictionary.iteritems方法代码示例

本文整理汇总了Python中weakref.WeakValueDictionary.iteritems方法的典型用法代码示例。如果您正苦于以下问题:Python WeakValueDictionary.iteritems方法的具体用法?Python WeakValueDictionary.iteritems怎么用?Python WeakValueDictionary.iteritems使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在weakref.WeakValueDictionary的用法示例。


在下文中一共展示了WeakValueDictionary.iteritems方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: WeakCollection

# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import iteritems [as 别名]
class WeakCollection(object):
    def __init__(self, seq=tuple()):
        self._d = WeakValueDictionary()
        for item in seq:
            self.add(item)
    def add(self, item):
        self._d[id(item)] = item
    def remove(self, item):
        del self._id[id(item)]
    def __iter__(self):
        for iid, item in self._d.iteritems():
            yield item
开发者ID:SapphireDensetsu,项目名称:enough,代码行数:14,代码来源:WeakCollection.py

示例2: Node

# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import iteritems [as 别名]
class Node(NanoResource):
    context_attributes = ['name', 'realm', 'address', 'node_monitor']

    def __init__(self, name, realm=None, address=None, node_monitor=None,
                 monitoring_groups=None, **kwargs):
        self.name = name
        self.realm = realm
        self.address = address or name
        self.node_monitor = node_monitor
        self.monitoring_groups = WeakValueDictionary()
        self._tasks = []
        if monitoring_groups:
            for group in monitoring_groups:
                if not isinstance(group, MonitoringGroup):
                    try:
                        group = MonitoringGroup.registry[group]
                    except KeyError:
                        logger.error("Unable to find MonitoringGroup '%s' "
                                     "in registry, skipping." % (group))
                group.add_node(self)

        super(Node, self).__init__(name, **kwargs)

    def build_context(self, monitoring_group, monitor):
        context = {}
        for obj in (monitoring_group, self, monitor):
            context = obj._build_context(context)
        return context

    @property
    def monitors(self):
        if self._tasks:
            return self._tasks
        for group_name, group in self.monitoring_groups.iteritems():
            for monitor_name, monitor in group.monitors.iteritems():
                self._tasks.append(self.build_context(group, monitor))
        return self._tasks
开发者ID:remotesyssupport,项目名称:nymms,代码行数:39,代码来源:resources.py

示例3: Data

# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import iteritems [as 别名]

#.........这里部分代码省略.........
            _on_update = {}
            _loader = {}

        self.main_key = main_key
        Items.main_key = main_key
        self.Item = Items

        # Buffer to make sure we don't discard items that often ...
        # As long as the item has a reference stored here or ANYWHERE else
        # he will not be discarted.
        if buffer_size < 1:
            self._buffer = None
        else:
            self._buffer = [None]*buffer_size

        # dictionary mapping item_name -> item object. This is a WEAKREF!
        from weakref import WeakValueDictionary
        self._loaded_items = WeakValueDictionary()
    

    def register(self, item, function, persistent=True, loader=False):
        """
        register(item, function, persistent=True)
        Register a function to be executed when item gets updated next time.
        Multiple functions can be registered, all will update when this happens.
        NOTES:
            o Function must take (item_object, item_name, old, new) as arguments.
            o Do not mass use this, otherwise its probably better to
               add it to the _item_set()/_item_load() special function.
            o loader keyword: The funciton is not called on set, but on a
               get event. (First time load). Only one function can be
               assigned. quietly overwrites all existing ones.
               Always persistent. The function MUST set the item.
        This will register for ALL items.
        """
        if loader == True:
            self.Item._loader[item] = function
            return
            
        if self.Item._on_update.has_key(item):
            self.Item._on_update[item].append([function, persistent])
            return
        self.Item._on_update[item] = [[function, persistent]]

    
    def unregister(self, item, function, loader):
        """
        Unregister a function again ...
        """
        if loader:
            del self.Item._loader[item]
            return
        try:
            self.Item._on_update[item].remove([function, True])
        except ValueError:
            self.Item._on_update[item].remove([function, False])


    def __getitem__(self, handle):
        try:
            ident = handle.lower()
        except AttributeError:
            ident = handle
        if not self._loaded_items.has_key(ident):
            return self._load(handle)
        #print handle, ident, self._loaded_items[ident].items
        return self._loaded_items[ident]


    def _load(self, handle):
        new_item = self.Item(handle)
        try:
            ident = handle.lower()
        except AttributeError:
            ident = handle
        
        self._loaded_items[ident] = new_item
        if self._buffer is not None:
            del self._buffer[0]
            self._buffer += [new_item]
        return new_item        


    def iteritems(self):
        """Iterator over all stored items.
        """
        return self._loaded_items.iteritems()
        
    def iterkeys(self):
        """Iterator over all stored keys.
        """
        return self._loaded_items.iterkeys()

    def itervalues(self):
        """Iterator over all stored items.
        """
        return self._loaded_items.itervalues()

    def __iter__(self):
        return self._loaded_items.__iter__()
开发者ID:seberg,项目名称:icsbot,代码行数:104,代码来源:_data.py

示例4: dir

# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import iteritems [as 别名]
# #         d['c'] = c
#     print 'i', i.key
#     print 'i', dir(i)


min_info = {}
weakdict = WeakValueDictionary()

for k, v in {'a': 1, 'b': 0, 'c': 3}.iteritems():
    inst = A(v)
    min_info[k] = inst
    weakdict[k] = inst

print 'min_info', min_info
print 'weakdict', [x() for x in weakdict.itervaluerefs()]
print 'weakdict', weakdict.iteritems()

for x in weakdict.itervaluerefs():
    print 'x', dir(weakdict.itervaluerefs())
    

# 10
# del a                       # удалить одну ссылку
# gc.collect()                # произвести сборку мусора
# 0
# d['primary']                # запись была автоматически удалена
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#     d['primary']
#   File "C:/python31/lib/weakref.py", line 46, in __getitem__
#     o = self.data[key]()
开发者ID:serg0987,项目名称:python,代码行数:33,代码来源:test_weakdict.py


注:本文中的weakref.WeakValueDictionary.iteritems方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。