LimitedSet
Kind-of Set (or priority queue) with limitations.
Attributes
| Attribute | Type | Description |
|---|---|---|
| max_heap_percent_overload | int = 15 | Threshold percentage of heap size relative to data size that triggers a full heap refresh to maintain performance. |
| pop_value | callable | Alias 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
| Name | Type | Description |
|---|---|---|
| maxlen | int = 0 | Maximum number of items allowed in the set. Items exceeding this limit are removed based on oldest insertion time. |
| expires | float = 0 | Time-to-live (TTL) for items in seconds. Items older than this value are purged. |
| data | Sequence = null | Initial data to populate the set, such as a dictionary, iterable, or another LimitedSet. |
| minlen | int = 0 | The 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
| Name | Type | Description |
|---|---|---|
| item | Any | The object to be added to the set or updated. |
| now | float = None | The 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
| Name | Type | Description |
|---|---|---|
| other | Iterable | A 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
| Name | Type | Description |
|---|---|---|
| item | Any | The member to remove from the set. |
purge()
@classmethod
def purge(
now: float = None
)
Check oldest items and remove them if needed.
Parameters
| Name | Type | Description |
|---|---|---|
| now | float = None | Time 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
| Name | Type | Description |
|---|---|---|
| default | Any = None | The value to return if the set is empty. |
Returns
| Type | Description |
|---|---|
Any | The 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
| Type | Description |
|---|---|
Dict | A dictionary mapping member keys to their respective insertion timestamps. |