def tags(*objects: Memo) -> ObjectTagManager
Get a tag manager interface for given Memo objects.
Parameters
*objectsMemo
One or more Memo objects to manage tags for.
Returns
An ObjectTagManager interface for given Memo objects. The manager has the following methods:
add(self, *tag: Union[Tag, Iterable[Tag]]):
Add one or more tags to the managed objects. Can be passed as individual tags, or collections of tags.remove(self, *tag: Union[Tag, Iterable[Tag]]):
Remove one or more tags associated with the object(s).
Examples
Adding a single tag
You can easily add a single string tag to an object.
import dbzero as db0
@db0.memo
class Book:
def __init__(self, title):
self.title = title
book = Book("The Hitchhiker's Guide to the Galaxy")
db0.tags(book).add("sci-fi")
# You can now find this book using the tag
sci_fi_books = list(db0.find("sci-fi"))
assert len(sci_fi_books) == 1Adding multiple tags from a list
The .add() method accepts iterables, so you can add multiple tags at once from a list or tuple.
import dbzero as db0
@db0.memo
class Task:
def __init__(self, description):
self.description = description
task = Task("Write API documentation")
db0.tags(task).add(["docs", "urgent", "writing"])
# Find the task by a combination of its tags
urgent_writing_tasks = list(db0.find("urgent", "writing"))
assert len(urgent_writing_tasks) == 1Removing tags from multiple objects
The tags() method can accept multiple objects, allowing you to perform batch operations.
import dbzero as db0
@db0.memo
class Product:
def __init__(self, name):
self.name = name
product1 = Product("Laptop")
product2 = Product("Mouse")
# Add a "sale" tag to both products
db0.tags(product1, product2).add("sale")
assert len(list(db0.find("sale"))) == 2
# Later, remove the "sale" tag from both
db0.tags(product1, product2).remove("sale")
assert len(list(db0.find("sale"))) == 0