Skip to main content

Extrinsic Key References

See src/zc/extrinsicreference/README.txt for details.

Detailed Documentation

Extrinsic References

Extrinsic reference registries record a key and one or more values to which they refer. The key and all values must be adaptable to zope.app.keyreference.interfaces.IKeyReference.

>>> import zc.extrinsicreference
>>> references = zc.extrinsicreference.ExtrinsicReferences()
>>> references.add(1, 2)
Traceback (most recent call last):
...
TypeError: ('Could not adapt', 1...
>>> from zope import interface, component
>>> from zope.app.keyreference.interfaces import IKeyReference
>>> class IMyObject(interface.Interface):
...     "An interface for which we register an IKeyReference adapter"
...     id = interface.Attribute("An id unique to IMyObject instances")
...
>>> class MyObject(object):
...     interface.implements(IMyObject)
...     _id_counter  = 0
...     @classmethod
...     def _getId(cls):
...         val = cls._id_counter
...         cls._id_counter += 1
...         return val
...     def __init__(self):
...         self.id = self._getId()
...
>>> class DummyKeyReference(object):
...     interface.implements(IKeyReference)
...     component.adapts(IMyObject)
...     key_type_id = 'zc.extrinsicreference.doctest'
...     def __init__(self, obj):
...         self.object = obj
...     def __call__(self):
...         """Get the object this reference is linking to.
...         """
...         return self.object
...     def __hash__(self):
...         """Get a unique identifier of the referenced object.
...         """
...         return hash(self.object.id)
...     def __cmp__(self, other):
...         """Compare the reference to another reference.
...         """
...         if self.key_type_id == other.key_type_id:
...             return cmp(self.object.id, other.object.id)
...         return cmp(self.key_type_id, other.key_type_id)
...
>>> component.provideAdapter(DummyKeyReference)
>>> object1 = MyObject()
>>> references.add(object1, 2)
Traceback (most recent call last):
...
TypeError: ('Could not adapt', 2...
>>> value1 = MyObject()
>>> value2 = MyObject()
>>> references.add(object1, value1)
>>> references.add(object1, value2)

Values can be retrieved by their key:

>>> set(references.get(object1)) == set((value1, value2))
True

References can be removed:

>>> references.remove(object1, value1)
>>> list(references.get(object1)) == [value2]
True

But if the reference is not registered, removing it raises a KeyError.

>>> references.remove(object1, value1)
Traceback (most recent call last):
...
KeyError:...
>>> object2 = MyObject()
>>> references.remove(object2, value2)
Traceback (most recent call last):
...
KeyError:...

If you prefer to silently ignore these errors, use discard.

>>> references.discard(object1, value1)
>>> references.discard(object2, value2)

Otherwise, you can use contains to determine if the reference exists:

>>> references.contains(object1, value1)
False
>>> references.contains(object2, value2)
False
>>> references.contains(object1, value2)
True

If a key has no associated values, an empty iterable is returned:

>>> references.discard(object1, value2)
>>> list(references.get(object1))
[]

Adding a value more than once does not cause the value to be included in the result sequence more than once:

>>> references.add(object1, value1)
>>> references.add(object1, value1)
>>> list(references.get(object1)) == [value1]
True

The set method destructively sets the given values for the object. Repeated objects are collapsed to a single instance.

>>> references.set(object1, (value2, object2, value2, value2, object2))
>>> references.contains(object1, value1)
False
>>> len(list(references.get(object1)))
2
>>> set(references.get(object1)) == set((value2, object2))
True
>>> references.set(object1, ())
>>> len(list(references.get(object1)))
0

The update method adds values to the previous values, non-destructively.

>>> references.add(object1, value1)
>>> references.update(object1, (value2, object2, value2))
>>> len(list(references.get(object1)))
3
>>> set(references.get(object1)) == set((value1, value2, object2))
True

CHANGES

0.3.0 (2009-08-27)

  • Fix errors introduced in 0.2.0 refactoring:

    1. Stop referring to extrinsicreference module from ZCML. The code is now in __init__.py.

    2. Revert registerShortcut and unregisterShortcut changes. These two functions are handlers in track_shortcuts.zcml.

    3. Add more package infrastructure, such as this file.

0.2.0 (2009-08-26)

  • Refactored an existing stable code.

  • Initial release as an egg.

Release files for zc.extrinsicreference 0.3.0

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

Source distribution (sdist)

Source distribution for zc.extrinsicreference 0.3.0
File Size Uploaded
zc.extrinsicreference-0.3.0.tar.gz 6.6 kB Details

Release files / zc.extrinsicreference-0.3.0.tar.gz

Download URL zc.extrinsicreference-0.3.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9ecccdd1f928a3c14b3fe18b50ba59aecf4fc67fe0ae580970bcac238f6a6cca
BLAKE2b-256 checksum
How to use checksums
1ba0eebd3f076177c8b52389ae6b38d3c65c682e2ba95bb817efc35a9515c513
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.0 This release

1 release file

0.2.0

1 release file

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