def as_tag(obj: Union[Memo, MemoWeakProxy, type]) -> Tag
def as_tag(*items) -> CompositeTag
def as_tag(items: tuple) -> CompositeTag
Open SourceStandard
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.
In dbzero-pro, as_tag() can also create composite tags from two or more ordered elements.
Parameters
objUnion[Memo, MemoWeakProxy, type]
The Memo object or class to convert into a tag.*itemsAny dbzero-procommercial edition Two or more values to convert into one ordered composite tag. Items can include simple values such as strings, memo objects, memo classes, existing tags, and expired memo references.itemstuple dbzero-procommercial edition Tuple form for the same composite tag construction.
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)) == 1Using Composite Tags
dbzero-procommercial editionComposite tags represent a structured relationship with two or more elements. The order matters: db0.as_tag("GRANT-READ", workspace, user) is a different tag from db0.as_tag("GRANT-READ", user, workspace).
# Two-element relationship.
author_tag = db0.as_tag("AUTHOR", author)
db0.tags(book).add(author_tag)
# Three-element access control relationship.
read_grant = db0.as_tag("GRANT-READ", workspace, user)
db0.tags(document).add(read_grant)
# Four-element tenant-scoped classification.
overdue_invoice = db0.as_tag(("CATEGORY", tenant, "invoice", "overdue"))
db0.tags(invoice).add(overdue_invoice)Composite tags are queried with the exact same composite value:
readable_documents = db0.find(Document, db0.as_tag("GRANT-READ", workspace, user))
overdue_invoices = db0.find(Invoice, db0.as_tag("CATEGORY", tenant, "invoice", "overdue"))They are useful for practical relationships such as permissions, tenant-scoped categories, workflow assignment, and relation labels that need more than one object or value.
Tags 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]