本文整理汇总了Python中weakref.WeakValueDictionary.itervalues方法的典型用法代码示例。如果您正苦于以下问题:Python WeakValueDictionary.itervalues方法的具体用法?Python WeakValueDictionary.itervalues怎么用?Python WeakValueDictionary.itervalues使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref.WeakValueDictionary
的用法示例。
在下文中一共展示了WeakValueDictionary.itervalues方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Node
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import itervalues [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 in self.monitoring_groups.itervalues():
for monitor in group.monitors.itervalues():
self._tasks.append(self._build_context(group, monitor))
return self._tasks
示例2: Data
# 需要导入模块: from weakref import WeakValueDictionary [as 别名]
# 或者: from weakref.WeakValueDictionary import itervalues [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__()