소스 검색

Fix confidence comparisons

Now Handlers are correctly compared based on their confidence, so that
the one with the highest confidence is first to be retrieved from the
minheap.
Clara Hobbs 6 년 전
부모
커밋
caedb4cdc2
2개의 변경된 파일35개의 추가작업 그리고 0개의 파일을 삭제
  1. 1
    0
      kayleevc/kaylee.py
  2. 34
    0
      kayleevc/plugins/__init__.py

+ 1
- 0
kayleevc/kaylee.py 파일 보기

@@ -167,6 +167,7 @@ class Kaylee:
167 167
                 # Handle the command
168 168
                 handler(self.plugin_tts)
169 169
                 self._log_history(plugin, text)
170
+                break
170 171
             except queue.Empty:
171 172
                 break
172 173
                 # If an unknown error occurs, ignore it and keep trying

+ 34
- 0
kayleevc/plugins/__init__.py 파일 보기

@@ -41,6 +41,11 @@ class Handler:
41 41
     """Base class for Kaylee plugin handlers
42 42
 
43 43
     Plugins should subclass this for their own command handlers.
44
+
45
+    Rich comparison methods are defined for use with queue.PriorityQueue.
46
+    They compare Handler objects based on their confidence alone, with
47
+    Handlers of high confidence comparing less than ones of low
48
+    confidence.
44 49
     """
45 50
 
46 51
     def __init__(self, confidence):
@@ -61,3 +66,32 @@ class Handler:
61 66
         When called, ``tts`` is a function which takes a string as its only
62 67
         parameter and passes it to Kaylee's configured text-to-speech system.
63 68
         """
69
+
70
+    def __eq__(self, other):
71
+        if isinstance(other, Handler):
72
+            return (self.confidence == other.confidence)
73
+        return NotImplemented
74
+
75
+    def __lt__(self, other):
76
+        if isinstance(other, Handler):
77
+            # Intentionally reversed
78
+            return (self.confidence > other.confidence)
79
+        return NotImplemented
80
+
81
+    def __le__(self, other):
82
+        if isinstance(other, Handler):
83
+            # Intentionally reversed
84
+            return (self.confidence >= other.confidence)
85
+        return NotImplemented
86
+
87
+    def __ge__(self, other):
88
+        if isinstance(other, Handler):
89
+            # Intentionally reversed
90
+            return (self.confidence <= other.confidence)
91
+        return NotImplemented
92
+
93
+    def __gt__(self, other):
94
+        if isinstance(other, Handler):
95
+            # Intentionally reversed
96
+            return (self.confidence < other.confidence)
97
+        return NotImplemented

Loading…
취소
저장