Add vision of Python API
This commit is contained in:
parent
b273772722
commit
a09b71cdb3
2 changed files with 62 additions and 3 deletions
18
main.py
18
main.py
|
@ -1,5 +1,19 @@
|
|||
#!/bin/env python
|
||||
from pyority_queue import sum_as_string
|
||||
from pyority_queue import PureQueue, KeyedQueue
|
||||
|
||||
|
||||
print(sum_as_string(10, 15))
|
||||
def main() -> None:
|
||||
pure = PureQueue()
|
||||
pure["second"] = 3.4
|
||||
pure["first"] = 1.2
|
||||
pure["third"] = 5.6
|
||||
print(*pure)
|
||||
|
||||
keyed = KeyedQueue({"third": 5.6, "second": 3.4, "first": 7.8})
|
||||
del keyed["third"]
|
||||
keyed["first"] = 1.2
|
||||
print(*keyed)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
@ -1 +1,46 @@
|
|||
def sum_as_string(a: int, b: int) -> str: ...
|
||||
from typing import Self
|
||||
|
||||
|
||||
class PureQueue[T]:
|
||||
"""A min-queue that allows duplicates and provides a minimal API allowing insertions and the ability to iterate over the queue in-order"""
|
||||
def __init__(self, items: dict[T, float] | None = None) -> None:
|
||||
"""
|
||||
:param items: An optional mapping of items to priorities to initialize the queue with
|
||||
"""
|
||||
|
||||
def __len__(self) -> int:
|
||||
"""
|
||||
:return: The number of items in the queue
|
||||
"""
|
||||
|
||||
def __iter__(self) -> Self:
|
||||
"""
|
||||
:return: An iterator over the queue
|
||||
"""
|
||||
|
||||
def __next__(self) -> T:
|
||||
"""
|
||||
:return: The next item in the queue
|
||||
"""
|
||||
|
||||
def __setitem__(self, key: T, value: float) -> None:
|
||||
"""
|
||||
Inserts a new item into the queue
|
||||
:param key: The item to insert
|
||||
:param value: The priority of the item
|
||||
"""
|
||||
|
||||
|
||||
class KeyedQueue[T](PureQueue[T]):
|
||||
"""A min-queue that disallows duplicates but offers the ability to update priorities and delete arbitrary items"""
|
||||
def __setitem__(self, key: T, value: float) -> None:
|
||||
"""
|
||||
Inserts an item into the queue, or updates its priority if it already exists
|
||||
:param key: The item to insert or update
|
||||
:param value: The priority of the item
|
||||
"""
|
||||
|
||||
def __delitem__(self, key: T) -> None:
|
||||
"""
|
||||
:param key: The item to delete from the queue
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue