Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding #.empty to relevant queues #115

Merged
merged 3 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions persistqueue/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def qsize(self):
def _qsize(self):
return self.info['size']

def empty(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please name it to is_empty, empty maybe treat as clear my clear by some people.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @peter-wangxu, I called them #.empty so they are consistent with python's queues: https://docs.python.org/3/library/queue.html#queue.Queue.empty so that some code can transparently use python's queues or yours. But I can change it if you feel sticking to python's - admittedly weird - naming is unnecessary. What do you think?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's follow the convention.

return self.qsize() == 0

def put(self, item, block=True, timeout=None):
"Interface for putting item in disk-based queue."
self.not_full.acquire()
Expand Down
3 changes: 3 additions & 0 deletions persistqueue/sqlackqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ def size(self):
def qsize(self):
return self.size

def empty(self):
return self.size == 0

def __len__(self):
return self.size

Expand Down
3 changes: 3 additions & 0 deletions persistqueue/sqlqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def size(self):
def qsize(self):
return self.size

def empty(self):
return self.size == 0

def __len__(self):
return self.size

Expand Down
10 changes: 10 additions & 0 deletions persistqueue/tests/test_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def test_open_close_single(self, serializer):
q.task_done()
del q

def test_empty(self):
q = Queue(self.path)
self.assertEqual(q.empty(), True)

q.put('var1')
self.assertEqual(q.empty(), False)

q.get()
self.assertEqual(q.empty(), True)

@params(*serializer_params)
def test_open_close_1000(self, serializer):
"""Write 1000 items, close, reopen checking if all items are there"""
Expand Down
10 changes: 10 additions & 0 deletions persistqueue/tests/test_sqlackqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def test_raise_empty(self):
# assert with negative timeout
self.assertRaises(ValueError, q.get, block=True, timeout=-1.0)

def test_empty(self):
q = SQLiteAckQueue(self.path, auto_commit=self.auto_commit)
self.assertEqual(q.empty(), True)

q.put('first')
self.assertEqual(q.empty(), False)

q.get()
self.assertEqual(q.empty(), True)

def test_open_close_single(self):
"""Write 1 item, close, reopen checking if same item is there"""

Expand Down
10 changes: 10 additions & 0 deletions persistqueue/tests/test_sqlqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def test_raise_empty(self):
self.assertRaises(ValueError, q.get, block=True, timeout=-1.0)
del q

def test_empty(self):
q = SQLiteQueue(self.path, auto_commit=self.auto_commit)
self.assertEqual(q.empty(), True)

q.put('first')
self.assertEqual(q.empty(), False)

q.get()
self.assertEqual(q.empty(), True)

def test_open_close_single(self):
"""Write 1 item, close, reopen checking if same item is there"""

Expand Down