Skip to main content

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
{}

Metadata

Release files for refutil 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for refutil 1.0.2
File Size Uploaded Provenance Actions
refutil-1.0.2.tar.gz 3.3 kB Without provenance Details for refutil-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for refutil 1.0.2
File Interpreter ABI Platform Actions
refutil-1.0.2-py3-none-any.whl Python 3 none any Details for refutil-1.0.2-py3-none-any.whl

Total release size: 6.6 kB

Release files / refutil-1.0.2.tar.gz

Download URL refutil-1.0.2.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8c43e712701cd86bdac2ec4e780c84895f33b9b735b7012e9af39bc1ed656444
BLAKE2b-256 checksum
How to use checksums
d58d8c24e1a0f8f2d0df7ae3bd860a144687e0fb9261f78b6bbcef42ec19ac32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / refutil-1.0.2-py3-none-any.whl

Download URL refutil-1.0.2-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca2a25859947026e4e05a5d89c2ebd913049e7e6d644cd3b7da885d7435df91c
BLAKE2b-256 checksum
How to use checksums
697f39dbb8d04ebc0ea0938ab044ade7782667004c95a27de1e3568e0d15dbea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Security for refutil 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page