File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed
Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -187,6 +187,17 @@ a tie-breaker so that two tasks with the same priority are returned in the order
187187they were added. And since no two entry counts are the same, the tuple
188188comparison will never attempt to directly compare two tasks.
189189
190+ Another solution to the problem of non-comparable tasks is to create a wrapper
191+ class that ignores the task item and only compares the priority field::
192+
193+ from dataclasses import dataclass, field
194+ from typing import Any
195+
196+ @dataclass(order=True)
197+ class PrioritizedItem:
198+ priority: int
199+ item: Any=field(compare=False)
200+
190201The remaining challenges revolve around finding a pending task and making
191202changes to its priority or removing it entirely. Finding a task can be done
192203with a dictionary pointing to an entry in the queue.
Original file line number Diff line number Diff line change @@ -56,6 +56,16 @@ The :mod:`queue` module defines the following classes and exceptions:
5656 one returned by ``sorted(list(entries))[0] ``). A typical pattern for entries
5757 is a tuple in the form: ``(priority_number, data) ``.
5858
59+ If the *data * elements are not comparable, the data can be wrapped in a class
60+ that ignores the data item and only compares the priority number::
61+
62+ from dataclasses import dataclass, field
63+ from typing import Any
64+
65+ @dataclass(order=True)
66+ class PrioritizedItem:
67+ priority: int
68+ item: Any=field(compare=False)
5969
6070.. exception :: Empty
6171
You can’t perform that action at this time.
0 commit comments