def set(iterable: Optional[Iterable[Any]] = None, /) -> SetObject
Creates a persistent, mutable, unordered collection of unique elements, analogous to Python's built-in set.
Elements must be hashable, just like in a standard Python set. This includes types like strings, numbers and tuples (if their elements are hashable).
In addition to regular types, dbzero.set also supports the use of dbzero.memo objects and dbzero.enum values. In case of memo objects, an instance UUID is used for hashing and comparison. It means that a single memo object can appear only once in the set and the exact same instance have to be used when checking for inclusion in the set (in operator).
Familiar API: This class is designed as a persistent counterpart to Python's built-in collection. It implements the same interface and supports all standard operations, allowing you to use it easily as a drop-in replacement while providing automatic persistance.
Parameters
iterableIterable[Any], optional
Optional iterable (list, tuple, set) to initialize the set.
Returns
A dbzero.set object.
Examples
Creating and modifying a set
You can create an empty set and add values or initialize it from an iterable.
# Create an empty db0.set
users = db0.set()
# Add elements
users.add("alice")
users.add("bob")
users.add("charlie")
users.add("alice") # Duplicates are ignored
print(len(users))
# Expected output: 3
print("bob" in users)
# Expected output: True
# Create from an iterable
initial_data = [1, 2, 3, 4, 5, 1, 2]
my_numbers = db0.set(initial_data)
print(my_numbers)
# Expected output (order may vary): {1, 2, 3, 4, 5}Using memo objects and enum values
dbzero.set can store memo objects and enum values.
# Create a memo class
@db0.memo
class User:
def __init__(self, name):
self.name = name
# Create an enum
Status = db0.enum("Status", ["ACTIVE", "INACTIVE", "PENDING"])
# Create a set with memo objects
user1 = User("Alice")
user2 = User("Bob")
user_set = db0.set([user1, user2])
# Try to add the same instance again (ignored)
user_set.add(user1)
print(len(user_set))
# Expected output: 2
# Check membership using the same instance
print(user1 in user_set)
# Expected output: True
# Create a set with enum values
status_set = db0.set([Status.ACTIVE, Status.PENDING])
status_set.add(Status.INACTIVE)
print(len(status_set))
# Expected output: 3Set operations
dbzero.set supports all standard set operations, like union, intersection, and difference, also with native Python sets.
# A persistent set of approved users
approved_users = db0.set(["eva", "frank", "grace"])
# A standard Python set of new applicants
new_applicants = {"grace", "heidi", "ivan"}
# Get all unique users
all_users = approved_users.union(new_applicants)
print(all_users)
# Expected output (order may vary): {'frank', 'ivan', 'eva', 'grace', 'heidi'}
# Get users who are both approved and new applicants
overlapping_users = approved_users.intersection(new_applicants)
# You can also use the '&' operator
# overlapping_users = approved_users & new_applicants
print(overlapping_users)
# Expected output: {'grace'}