Skip to content
Reference

Fixed lookups

A fixed lookup keeps the membership chosen at construction. It retains its members strongly, and the member objects can still change internally. The factories and classes below are available from the package root:

Import fixed lookup interfaces
from lookups import EmptyLookup, SimpleLookup, fixed, singleton

They implement the common query, result and item interfaces. For the full exported surface, see Public API.

Constructor selection

Interface Membership and implementation
fixed(*members) -> Lookup Selects EmptyLookup for no members, the singleton implementation for one member, or SimpleLookup for two or more.
singleton(member, id_=None) -> Lookup One member, with an optional item identifier.
EmptyLookup() No members.
SimpleLookup(*instances) The supplied members in argument order, including repetitions. Zero arguments is valid.

Every supplied member must be non-None. Both factories and SimpleLookup raise ValueError('None cannot be a lookup member') if a member is None. False-valued objects such as 0 and False are members too. Absence is represented by None.

fixed() takes members as separate arguments. Passing a list as one argument registers that list itself, while fixed(*members) registers its contents. The factories return Lookup, so callers can use the common interface without depending on the selected concrete class.

fixed

fixed(*members)

Create a lookup whose membership is fixed at construction.

No arguments select EmptyLookup, one selects the singleton implementation, and several select SimpleLookup. Members are retained strongly.

Parameters:

Name Type Description Default
members object

Objects to make available, in argument order.

()

Raises:

Type Description
ValueError

A supplied member is None.

Empty lookup

EmptyLookup() and fixed() expose no members. lookup() and lookup_item() return None, and lookup_expected() raises NoneLookupError. lookup_all() returns an empty tuple.

lookup_result(cls) returns an empty result whose for_class is cls. Its instance and item collections are empty tuples, and its class collection is an empty frozenset.

EmptyLookup

Bases: Lookup

A fixed lookup with no members and no automatic change notifications.

Methods:

lookup
lookup(cls)

Return None for every searched type.

lookup_result
lookup_result(cls)

Return an empty result for the searched type.

Singleton lookup

singleton(member, id_=None) exposes one object. Queries match the member using isinstance(member, cls), including searches for its parent classes. A matching instance collection contains the member once. When the type does not match, queries have the empty behaviour described above.

The optional id_ is the item's identifier and display name. When it is None, both use str(member) when read. An explicit empty string is accepted. The default identifier can therefore change if the member's string representation changes. Choose an explicit identifier when the application needs one independent of that representation, and arrange its uniqueness and persistence within the application.

Inspect singleton metadata
settings = {"colour": "blue"}
lookup = singleton(settings, id_="settings")

assert lookup.lookup(dict) is settings
item = lookup.lookup_item(dict)
assert item is not None
assert item.get_id() == "settings"
assert item.get_display_name() == "settings"

settings["colour"] = "green"
assert lookup.lookup_expected(dict)["colour"] == "green"

The concrete class is SingletonLookup in lookups.singleton. It is not a package-root export. Use the public singleton factory to construct one.

singleton

singleton(member, id_=None)

Create a fixed lookup holding one member strongly.

Parameters:

Name Type Description Default
member object

The sole member, whose internal state can still change.

required
id_ str | None

Optional item identifier. Defaults to str(member).

None

Raises:

Type Description
ValueError

member is None.

Simple lookup

SimpleLookup(*instances) stores members in argument order. Matching uses issubclass(item.get_type(), cls), so parent-type searches include members of subclasses. lookup() and lookup_item() select the first matching member or item in that order.

Result item collections preserve matching members in argument order, including repeated objects and equal objects. Instance collections preserve that order and those repetitions. all_classes() returns a frozenset of matching concrete types, removing duplicate types.

Query a fixed collection
lookup = fixed("first", 7, "second", "first")

assert lookup.lookup(str) == "first"
assert lookup.lookup_all(str) == ("first", "second", "first")
assert lookup.lookup_result(str).all_classes() == frozenset({str})

Each item reports the member's concrete type. Its identifier and display name use str(member). Use singleton when an explicit identifier is needed for one member, because SimpleLookup and fixed do not accept one.

SimpleLookup

Bases: Lookup

Hold fixed membership in argument order, retaining members strongly.

Member objects remain mutable. A direct query returns the first matching member, while a result describes all matching members.

Methods:

__init__
__init__(*instances)

Create a fixed lookup from the supplied objects.

Parameters:

Name Type Description Default
instances object

Members in query order, including repetitions.

()

Raises:

Type Description
ValueError

Any member is None.

lookup
lookup(cls)
lookup_result
lookup_result(cls)

Membership and notifications

These interfaces provide no operation to add, remove or replace members after construction. A member can still change internally while remaining registered, because fixing membership does not freeze or copy the object.

Fixed results expose a listeners registry for the common subscription interface but dispatch no automatic change notifications. Mutating a member does not trigger a result notification. For changing membership, use dynamic membership and conversion.