diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2018-01-12 06:06:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-12 06:06:34 (GMT) |
commit | 0c3be9651f4f149f4a78bb7043d26db9e75cabc0 (patch) | |
tree | 02b29ce454a2bbe8a18d7afa9a54cea5c6421c06 /Doc/library/heapq.rst | |
parent | 02556fbade5e1e864dd09d5768a8dbbf5b3a0dac (diff) | |
download | cpython-0c3be9651f4f149f4a78bb7043d26db9e75cabc0.zip cpython-0c3be9651f4f149f4a78bb7043d26db9e75cabc0.tar.gz cpython-0c3be9651f4f149f4a78bb7043d26db9e75cabc0.tar.bz2 |
bpo-31145: Use dataclasses to create a prioritization wrapper (#5153)
Diffstat (limited to 'Doc/library/heapq.rst')
-rw-r--r-- | Doc/library/heapq.rst | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/Doc/library/heapq.rst b/Doc/library/heapq.rst index e36ca8d..0b1a3c8 100644 --- a/Doc/library/heapq.rst +++ b/Doc/library/heapq.rst @@ -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. |