本文整理汇总了Python中weakref.WeakValueDictionary方法的典型用法代码示例。如果您正苦于以下问题:Python weakref.WeakValueDictionary方法的具体用法?Python weakref.WeakValueDictionary怎么用?Python weakref.WeakValueDictionary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weakref
的用法示例。
在下文中一共展示了weakref.WeakValueDictionary方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clear
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def clear(self):
self.user = None
self._users = weakref.WeakValueDictionary()
self._emojis = {}
self._calls = {}
self._guilds = {}
self._voice_clients = {}
# LRU of max size 128
self._private_channels = OrderedDict()
# extra dict to look up private channels by user id
self._private_channels_by_user = {}
self._messages = self.max_messages and deque(maxlen=self.max_messages)
# In cases of large deallocations the GC should be called explicitly
# To free the memory more immediately, especially true when it comes
# to reconnect loops which cause mass allocations and deallocations.
gc.collect()
示例2: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def __init__(self, moveEvent, acceptable):
self.enter = False
self.acceptable = acceptable
self.exit = False
self.__clickItems = weakref.WeakValueDictionary()
self.__dragItems = weakref.WeakValueDictionary()
self.currentItem = None
if moveEvent is not None:
self._scenePos = moveEvent.scenePos()
self._screenPos = moveEvent.screenPos()
self._lastScenePos = moveEvent.lastScenePos()
self._lastScreenPos = moveEvent.lastScreenPos()
self._buttons = moveEvent.buttons()
self._modifiers = moveEvent.modifiers()
else:
self.exit = True
示例3: test_weak_valued_dict_update
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def test_weak_valued_dict_update(self):
self.check_update(weakref.WeakValueDictionary,
{1: C(), 'a': C(), C(): C()})
# errors
self.assertRaises(TypeError, weakref.WeakValueDictionary.update)
d = weakref.WeakValueDictionary()
self.assertRaises(TypeError, d.update, {}, {})
self.assertRaises(TypeError, d.update, (), ())
self.assertEqual(list(d.keys()), [])
# special keyword arguments
o = Object(3)
for kw in 'self', 'dict', 'other', 'iterable':
d = weakref.WeakValueDictionary()
d.update(**{kw: o})
self.assertEqual(list(d.keys()), [kw])
self.assertEqual(d[kw], o)
示例4: test_deepcopy_weakvaluedict
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def test_deepcopy_weakvaluedict(self):
class C(object):
def __init__(self, i):
self.i = i
a, b, c, d = [C(i) for i in xrange(4)]
u = weakref.WeakValueDictionary()
u[a] = b
u[c] = d
# Keys are copied, values aren't
v = copy.deepcopy(u)
self.assertNotEqual(v, u)
self.assertEqual(len(v), 2)
(x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i)
self.assertFalse(x is a)
self.assertEqual(x.i, a.i)
self.assertTrue(y is b)
self.assertFalse(z is c)
self.assertEqual(z.i, c.i)
self.assertTrue(t is d)
del x, y, z, t
del d
self.assertEqual(len(v), 1)
示例5: global_cache
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def global_cache(srctype, ffi, funcname, *args, **kwds):
key = kwds.pop('key', (funcname, args))
assert not kwds
try:
return ffi._typecache[key]
except KeyError:
pass
try:
res = getattr(ffi._backend, funcname)(*args)
except NotImplementedError as e:
raise NotImplementedError("%s: %r: %s" % (funcname, srctype, e))
# note that setdefault() on WeakValueDictionary is not atomic
# and contains a rare bug (http://bugs.python.org/issue19542);
# we have to use a lock and do it ourselves
cache = ffi._typecache
with global_lock:
res1 = cache.get(key)
if res1 is None:
cache[key] = res
return res
else:
return res1
示例6: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def __init__(self, shorthand_name=None):
"""
Creates a new :class:`TransformNode`.
**shorthand_name** - a string representing the "name" of this
transform. The name carries no significance
other than to improve the readability of
``str(transform)`` when DEBUG=True.
"""
# Parents are stored in a WeakValueDictionary, so that if the
# parents are deleted, references from the children won't keep
# them alive.
self._parents = WeakValueDictionary()
# TransformNodes start out as invalid until their values are
# computed for the first time.
self._invalid = 1
self._shorthand_name = shorthand_name or ''
示例7: insert
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def insert(self, index, value):
assert self.ref_classdef.isinstance(value.classdef)
self.references.insert(index, self.next_free_key)
objects = {}
objects[index] = value
# increment all cached object with > indices +1
for key, value in self.objects.items():
if key >= index:
objects[key+1] = value
else:
objects[key] = value
self.next_free_key += 1
if self.attached:
self.objects = weakref.WeakValueDictionary(objects)
else:
self.objects = objects
self.attach()
示例8: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def __init__(self, parent, pid, format, version=PROPERTY_VERSION):
super(StrongRefSetProperty, self).__init__(parent, pid, format, version)
self.references = {}
self.index_name = None
self.next_free_key = 0
self.last_free_key = 0xFFFFFFFF
# Pid of the referenced objects unique_key
self.key_pid = None
self.key_size = None
if self.attached:
self.objects = weakref.WeakValueDictionary()
else:
self.objects = {}
示例9: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def __init__(self, func):
self.srcfile = inspect.getsourcefile(func)
self.srcline = inspect.getsourcelines(func)[0]
self.func = func
functools.update_wrapper(self, func)
self.calls = 0
self.name = None
# register the block
myhdl._simulator._blocks.append(self)
self.bound_functions = WeakValueDictionary()
示例10: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def __init__(self, ref, close_cb):
self.ref = ref
self._close = close_cb
self._referrers = weakref.WeakValueDictionary()
示例11: close
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def close(self):
ref, self.ref = self.ref, None
close, self._close = self._close, None
referrers = self._referrers
self._referrers = weakref.WeakValueDictionary()
for referrer in referrers.valuerefs():
referrer = referrer()
if referrer is not None:
referrer.close()
if ref is not None and close is not None:
close(ref)
示例12: locked
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def locked() -> Callable:
"""
Allows the user to only run one instance of the decorated command at a time.
Subsequent calls to the command from the same author are ignored until the command has completed invocation.
This decorator must go before (below) the `command` decorator.
"""
def wrap(func: Callable) -> Callable:
func.__locks = WeakValueDictionary()
@wraps(func)
async def inner(self: Cog, ctx: Context, *args, **kwargs) -> None:
lock = func.__locks.setdefault(ctx.author.id, Lock())
if lock.locked():
embed = Embed()
embed.colour = Colour.red()
log.debug("User tried to invoke a locked command.")
embed.description = (
"You're already using this command. Please wait until it is done before you use it again."
)
embed.title = random.choice(ERROR_REPLIES)
await ctx.send(embed=embed)
return
async with func.__locks.setdefault(ctx.author.id, Lock()):
await func(self, ctx, *args, **kwargs)
return inner
return wrap
示例13: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def __init__(cls, *args, **kwargs):
cls.__instances = weakref.WeakValueDictionary()
cls.__strong_cache = OrderedDict()
cls.__strong_cache_size = 8
示例14: __init__
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def __init__(self, connector=None, *, proxy=None, proxy_auth=None, loop=None, unsync_clock=True):
self.loop = asyncio.get_event_loop() if loop is None else loop
self.connector = connector
self.__session = None # filled in static_login
self._locks = weakref.WeakValueDictionary()
self._global_over = asyncio.Event()
self._global_over.set()
self.token = None
self.bot_token = False
self.proxy = proxy
self.proxy_auth = proxy_auth
self.use_clock = not unsync_clock
user_agent = 'DiscordBot (https://github.com/Rapptz/discord.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}'
self.user_agent = user_agent.format(__version__, sys.version_info, aiohttp.__version__)
示例15: prune
# 需要导入模块: import weakref [as 别名]
# 或者: from weakref import WeakValueDictionary [as 别名]
def prune(self):
"""prune unreferenced, non-dirty states."""
ref_count = len(self)
dirty = [s.obj() for s in self.all_states() if s.modified]
# work around http://bugs.python.org/issue6149
keepers = weakref.WeakValueDictionary()
keepers.update(self)
self._dict.clear()
self._dict.update(keepers)
self.modified = bool(dirty)
return ref_count - len(self)