Skip to main content

LimitedSet

Kind-of Set (or priority queue) with limitations.

Attributes

AttributeTypeDescription
max_heap_percent_overloadint = 15Threshold percentage of heap size relative to data size that triggers a full heap refresh to maintain performance.
pop_valuecallableAlias for the discard method used to mark an existing item as removed from the set.

Constructor

Signature

def LimitedSet(
maxlen: int = 0,
expires: float = 0,
data: Sequence = null,
minlen: int = 0
) - > null

Parameters

NameTypeDescription
maxlenint = 0Maximum number of items allowed in the set. Items exceeding this limit are removed based on oldest insertion time.
expiresfloat = 0Time-to-live (TTL) for items in seconds. Items older than this value are purged.
dataSequence = nullInitial data to populate the set, such as a dictionary, iterable, or another LimitedSet.
minlenint = 0The minimum residual size of the set; expired items are only deleted if the set size exceeds this value.

Methods


clear()

@classmethod
def clear()

Clear all data, start from scratch again.


add()

@classmethod
def add(
item: Any,
now: float = None
)

Add a new item, or reset the expiry time of an existing item.

Parameters

NameTypeDescription
itemAnyThe object to be added to the set or updated.
nowfloat = NoneThe timestamp to use as the insertion time, defaulting to the current monotonic time.

update()

@classmethod
def update(
other: Iterable
)

Update this set from other LimitedSet, dict or iterable.

Parameters

NameTypeDescription
otherIterableA collection of items, a dictionary of key-timestamp pairs, or another LimitedSet instance to merge.

discard()

@classmethod
def discard(
item: Any
)

Removes an item from the set if it is present, marking it for eventual removal from the internal heap.

Parameters

NameTypeDescription
itemAnyThe member to remove from the set.

purge()

@classmethod
def purge(
now: float = None
)

Check oldest items and remove them if needed.

Parameters

NameTypeDescription
nowfloat = NoneTime of purging -- by default right now. This can be useful for unit testing.

pop()

@classmethod
def pop(
default: Any = None
) - > Any

Remove and return the oldest item, or :const:None when empty.

Parameters

NameTypeDescription
defaultAny = NoneThe value to return if the set is empty.

Returns

TypeDescription
AnyThe oldest item in the set based on insertion time, or the default value if the set is empty.

as_dict()

@classmethod
def as_dict() - > Dict

Whole set as serializable dictionary.

Returns

TypeDescription
DictA dictionary mapping member keys to their respective insertion timestamps.