Manage membership¶
Choose fixed membership when the available objects will stay the same. When
availability must change, keep an InstanceContent with the lookup owner and
give consumers its GenericLookup to query.
Choose fixed membership¶
Use fixed(*members) for a known collection, singleton(member) for one object,
or fixed() for an empty lookup. EmptyLookup() is also available when you want
to name the empty implementation explicitly.
from lookups import fixed, singleton
available = fixed("ready", 7)
assert available.lookup(str) == "ready"
assert available.lookup(int) == 7
one_member = singleton("ready")
assert one_member.lookup(str) == "ready"
empty = fixed()
assert empty.lookup(str) is None
Pass members as separate arguments. If you already have a collection, unpack it
with fixed(*members). Calling fixed(members) registers the collection itself.
Members must be non-None. Fixed membership does not freeze the objects or copy
them. See Fixed lookups for the available implementations.
Attach an owner handle¶
For changing membership, create an InstanceContent and pass it to
GenericLookup. Keep the content object with the owner so it can update
membership, while consumers query the lookup. Each content object belongs to
one lookup and cannot be attached again.
The remaining code blocks build one report example in reports.py. Start with a
consumer that accepts Lookup, keeping it independent of the owner's content
handle. The next two sections extend this example to add, remove and replace
reports.
from lookups import GenericLookup, InstanceContent, Lookup
class Report:
def __init__(self, title: str) -> None:
self.title = title
def current_report(available: Lookup) -> Report | None:
return available.lookup(Report)
content = InstanceContent()
lookup = GenericLookup(content)
assert current_report(lookup) is None
Add and remove direct members¶
Register an object with add(). To check whether it is directly registered, use
object in content, which tests membership using equality rather than searching
by type. For deferred members, retain their conversion keys
and convertors instead: direct membership checks do not test keys or converted
products. When a report is no longer available, remove it with the same object
passed to add(). Check membership first if it may already have been removed.
draft = Report("Draft")
content.add(draft)
assert draft in content
assert current_report(lookup) is draft
if draft in content:
content.remove(draft)
assert draft not in content
assert current_report(lookup) is None
Use is None to check whether a query found a report, as above. If several
reports are available, do not rely on their addition order to choose which one
lookup(Report) returns. Use lookup_all(Report) to retrieve
all matching reports when you need to choose between them.
Replace or clear the membership¶
When you have the complete desired collection, use set() to replace the whole
membership. Include every object that should remain available, including objects
of other types. Pass an empty collection to clear it.
published = Report("Published")
content.set([published])
assert published in content
assert draft not in content
assert current_report(lookup) is published
content.set([])
assert published not in content
assert current_report(lookup) is None
Registration uses equality and hashing to distinguish members. The Report
class above uses default object identity, so two reports with the same title
can remain independently available. See
InstanceContent for the membership API.
To react to membership changes, follow Observe results.