effortless weakrefs to objects, methods, and functions with annotations
Project description
Ref Util
effortless weakrefs to objects, methods, and functions with annotations
Preamble
There is a fairly common need to reference some data in the callback of a weakref when the object is being garbage collected. This package provides a simple interface for creating new annotated, slotted, reference types, which subclass weakref.ref in the standard library.
Documentation consists of this README and the code.
Table of Contents
Technologies
- python >=2.5
Example
Code
import collections
import refutil
Subscription = collections.namedtuple('Subscription', ['callback', 'topic', 'subscription_id'])
SubscriptionWeakRef = refutil.reftype('SubscriptionWeakRef', ['subscription_id'])
class Subscriptions:
def __init__(self):
self.subscriptions = {}
def _weak_remove_callback(self, ref):
print('weak callback triggered')
self.remove(ref.subscription_id)
def add(self, topic, callback, subscription_id):
# make a weak reference to the callback, so that it will be garbage
# collected, but store the subscription_id in the reference
# and then add a method callback, which will be weakly referenced as well,
# to handle when the callback is garbage collected
wrcallback = SubscriptionWeakRef(
callback,
self._weak_remove_callback,
subscription_id=subscription_id
)
subscription = Subscription(
topic=topic,
callback=wrcallback,
subscription_id=subscription_id
)
self.subscriptions[subscription_id] = subscription
print('subscription added', subscription_id)
def remove(self, subscription_id):
print('subscription removed', subscription_id)
del self.subscriptions[subscription_id]
subs = Subscriptions()
def my_callback():
pass
subs.add('topic', my_callback, 'sub-1')
subs.add('topic', my_callback, 'sub-2')
print(subs.subscriptions)
print('deleting callback')
del my_callback
print(subs.subscriptions)
Output
subscription added sub-1
subscription added sub-2
{'sub-1': Subscription(callback=<weakref at 0x01874300; to 'function' at 0x01873C48 (my_callback)>, topic='topic', subscription_id='sub-1'), 'sub-2': Subscription(callback=<weakref at 0x018743F0; to 'function' at 0x01873C48 (my_callback)>, topic='topic', subscription_id='sub-2')}
deleting callback
weak callback triggered
subscription removed sub-2
weak callback triggered
subscription removed sub-1
{}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file refutil-1.0.2.tar.gz.
File metadata
- Download URL: refutil-1.0.2.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c43e712701cd86bdac2ec4e780c84895f33b9b735b7012e9af39bc1ed656444
|
|
| MD5 |
13d83a8aebae2fb2d2daa9fcd9fea5a8
|
|
| BLAKE2b-256 |
d58d8c24e1a0f8f2d0df7ae3bd860a144687e0fb9261f78b6bbcef42ec19ac32
|
File details
Details for the file refutil-1.0.2-py3-none-any.whl.
File metadata
- Download URL: refutil-1.0.2-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca2a25859947026e4e05a5d89c2ebd913049e7e6d644cd3b7da885d7435df91c
|
|
| MD5 |
8af7c3b62a51cfb6af8be207664235ca
|
|
| BLAKE2b-256 |
697f39dbb8d04ebc0ea0938ab044ade7782667004c95a27de1e3568e0d15dbea
|