def as_tag(obj: Union[Memo, MemoWeakProxy, type]) -> Tag
The dbzero.as_tag() method creates a searchable Tag object from a Memo instance or a class. Allows to use Memo object or class as a Tag for other objects. Tags created from objects are stable identifiers that will work even if the original object is deleted.
Parameters
objUnion[Memo, MemoWeakProxy, type]
The Memo object or class to convert into a tag.
Returns
A Tag object. These objects are hashable and support equality checks (==, !=), but they don't support ordering comparisons (like <, >).
Examples
Using an Instance as a Tag
You can tag an object with another instance to model relationships.
# Create an object that will serve as the tag
category_object = MemoClassForTags("category_A")
# Create an object to be tagged
data_object = MemoClassForTags(101)
# Apply the tag using as_tag()
db0.tags(data_object).add(db0.as_tag(category_object))
# Find all objects tagged with category_object
results = db0.find(db0.as_tag(category_object))
assert list(results) == [data_object]Using a Class as a Tag
You can also use a memo class as a tag. It's important to know the difference between querying for instances of a class versus objects tagged with a class.
# Create an object
obj = MemoClassForTags()
# Tag the object with the MemoTestClass class
db0.tags(obj).add(db0.as_tag(MemoTestClass))
# This finds instances of MemoTestClass (returns 0)
assert len(db0.find(MemoTestClass)) == 0
# This finds objects tagged with MemoTestClass (returns 1)
assert len(db0.find(db0.as_tag(MemoTestClass))) == 1Using Foreign Objects as Tags
It is possible to use an object from one prefix to tag an object in another.
# 'foreign_obj' is created on the default prefix
foreign_obj = MemoTestSingleton()
# Switch to a new prefix and create a new object
db0.open("some-other-prefix", "rw")
obj_in_new_prefix = MemoTestClass()
# Tag the new object with 'foreign_obj' from the other prefix
db0.tags(obj_in_new_prefix).add(db0.as_tag(foreign_obj))
# You can now find the object using the foreign tag,
# even though the search is initiated from the new prefix.
results = db0.find(db0.as_tag(foreign_obj))
assert len(list(results)) == 1Tags from Expired Objects
A tag made from an instance stays valid even after the original object is deleted. This lets you find related items even after the original "tagging" object is gone.
# Create an object that will be used for tagging
tagging_obj = MemoTestClass()
# Create an object and tag it
obj_to_be_tagged = MemoTestClass()
db0.tags(obj_to_be_tagged).add(db0.as_tag(tagging_obj))
# Create a weak reference to see the expiration status
weak_ref = db0.weak_proxy(tagging_obj)
# Now, delete the original tagging object
del tagging_obj
db0.commit()
# The weak reference is now expired
assert db0.expired(weak_ref) is True
# Despite the original object being gone, you can still
# find related objects using the tag created from the (now expired) weak reference.
results = db0.find(db0.as_tag(weak_ref))
assert list(results) == [obj_to_be_tagged]