diff options
Diffstat (limited to 'logilab/common/tasksqueue.py')
-rw-r--r-- | logilab/common/tasksqueue.py | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/logilab/common/tasksqueue.py b/logilab/common/tasksqueue.py index 7d561ca..4e3434e 100644 --- a/logilab/common/tasksqueue.py +++ b/logilab/common/tasksqueue.py @@ -19,8 +19,9 @@ __docformat__ = "restructuredtext en" -from bisect import insort_left +from typing import Iterator, List +from bisect import insort_left import queue LOW = 0 @@ -35,16 +36,15 @@ PRIORITY = { REVERSE_PRIORITY = dict((values, key) for key, values in PRIORITY.items()) - class PrioritizedTasksQueue(queue.Queue): - def _init(self, maxsize): + def _init(self, maxsize: int) -> None: """Initialize the queue representation""" self.maxsize = maxsize # ordered list of task, from the lowest to the highest priority - self.queue = [] + self.queue: List['Task'] = [] # type: ignore - def _put(self, item): + def _put(self, item: 'Task') -> None: """Put a new item in the queue""" for i, task in enumerate(self.queue): # equivalent task @@ -60,14 +60,14 @@ class PrioritizedTasksQueue(queue.Queue): return insort_left(self.queue, item) - def _get(self): + def _get(self) -> 'Task': """Get an item from the queue""" return self.queue.pop() - def __iter__(self): + def __iter__(self) -> Iterator['Task']: return iter(self.queue) - def remove(self, tid): + def remove(self, tid: str) -> None: """remove a specific task from the queue""" # XXX acquire lock for i, task in enumerate(self): @@ -76,26 +76,23 @@ class PrioritizedTasksQueue(queue.Queue): return raise ValueError('not task of id %s in queue' % tid) -class Task(object): - def __init__(self, tid, priority=LOW): +class Task: + def __init__(self, tid: str, priority: int = LOW) -> None: # task id self.id = tid # task priority self.priority = priority - def __repr__(self): + def __repr__(self) -> str: return '<Task %s @%#x>' % (self.id, id(self)) - def __cmp__(self, other): - return cmp(self.priority, other.priority) - - def __lt__(self, other): + def __lt__(self, other: 'Task') -> bool: return self.priority < other.priority - def __eq__(self, other): - return self.id == other.id + def __eq__(self, other: object) -> bool: + return isinstance(other, type(self)) and self.id == other.id __hash__ = object.__hash__ - def merge(self, other): + def merge(self, other: 'Task') -> None: pass |