Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Doc/library/heapq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ a tie-breaker so that two tasks with the same priority are returned in the order
they were added. And since no two entry counts are the same, the tuple
comparison will never attempt to directly compare two tasks.

Another solution to the problem of non-comparable tasks is to create a wrapper
class that ignores the task item and only compares the priority field::

from dataclasses import dataclass, field
from typing import Any

@dataclass(order=True)
class PrioritizedItem:
priority: int
item: Any=field(compare=False)

The remaining challenges revolve around finding a pending task and making
changes to its priority or removing it entirely. Finding a task can be done
with a dictionary pointing to an entry in the queue.
Expand Down
10 changes: 10 additions & 0 deletions Doc/library/queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ The :mod:`queue` module defines the following classes and exceptions:
one returned by ``sorted(list(entries))[0]``). A typical pattern for entries
is a tuple in the form: ``(priority_number, data)``.

If the *data* elements are not comparable, the data can be wrapped in a class
that ignores the data item and only compares the priority number::

from dataclasses import dataclass, field
from typing import Any

@dataclass(order=True)
class PrioritizedItem:
priority: int
item: Any=field(compare=False)

.. exception:: Empty

Expand Down