-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from gunthercox/history
Redesign recent_statements to hold the entire current conversation
- Loading branch information
Showing
4 changed files
with
70 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class ResponseQueue(object): | ||
""" | ||
This is a data structure like a queue. | ||
Only a fixed number of items can be added. | ||
Once the maximum is reached, when a new item | ||
is added the oldest item in the queue will | ||
be removed. | ||
""" | ||
|
||
def __init__(self, maxsize=10): | ||
self.maxsize = maxsize | ||
self.queue = [] | ||
|
||
def append(self, item): | ||
if len(self.queue) == self.maxsize: | ||
# Remove an element from the top of the list | ||
self.queue.pop(0) | ||
|
||
self.queue.append(item) | ||
|
||
def __getitem__(self, index): | ||
if self.queue: | ||
return self.queue[index] | ||
return None | ||
|
||
def __contains__(self, item): | ||
""" | ||
Check if an element is in this queue. | ||
""" | ||
return item in self.queue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from unittest import TestCase | ||
from chatterbot.utils.queues import ResponseQueue | ||
|
||
|
||
class ResponseQueueTests(TestCase): | ||
|
||
def setUp(self): | ||
self.queue = ResponseQueue(maxsize=2) | ||
|
||
def test_append(self): | ||
self.queue.append(0) | ||
self.assertIn(0, self.queue) | ||
|
||
def test_contains(self): | ||
self.queue.queue.append(0) | ||
self.assertIn(0, self.queue) | ||
|
||
def test_maxsize(self): | ||
self.queue.append(0) | ||
self.queue.append(1) | ||
self.queue.append(2) | ||
|
||
self.assertNotIn(0, self.queue) | ||
self.assertIn(1, self.queue) | ||
self.assertIn(2, self.queue) |