Repository: peter-wangxu/persist-queue

Issue:
Add #.empty
Hello @peter-wangxu, would you accept a PR adding the #.empty method to your queue implementations?

We could also add #.full but since the SQLite queue does not support `maxsize` I guess it would not make much sense.

Discussion:

Patch:
diff --git a/persistqueue/queue.py b/persistqueue/queue.py
index dac8ec6..0a1e301 100644
--- a/persistqueue/queue.py
+++ b/persistqueue/queue.py
@@ -138,6 +138,9 @@ class Queue(object):
     def _qsize(self):
         return self.info['size']
 
+    def empty(self):
+        return self.qsize() == 0
+
     def put(self, item, block=True, timeout=None):
         "Interface for putting item in disk-based queue."
         self.not_full.acquire()
diff --git a/persistqueue/sqlackqueue.py b/persistqueue/sqlackqueue.py
index d91c033..45ea0f4 100644
--- a/persistqueue/sqlackqueue.py
+++ b/persistqueue/sqlackqueue.py
@@ -215,6 +215,9 @@ class SQLiteAckQueue(sqlbase.SQLiteBase):
     def qsize(self):
         return self.size
 
+    def empty(self):
+        return self.size == 0
+
     def __len__(self):
         return self.size
 
diff --git a/persistqueue/sqlqueue.py b/persistqueue/sqlqueue.py
index 1471b04..f01eec7 100644
--- a/persistqueue/sqlqueue.py
+++ b/persistqueue/sqlqueue.py
@@ -115,6 +115,9 @@ class SQLiteQueue(sqlbase.SQLiteBase):
     def qsize(self):
         return self.size
 
+    def empty(self):
+        return self.size == 0
+
     def __len__(self):
         return self.size
 


Test patch:
diff --git a/persistqueue/tests/test_queue.py b/persistqueue/tests/test_queue.py
index a6b69c8..f528ba3 100644
--- a/persistqueue/tests/test_queue.py
+++ b/persistqueue/tests/test_queue.py
@@ -46,6 +46,16 @@ class PersistTest(unittest.TestCase):
         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"""
diff --git a/persistqueue/tests/test_sqlackqueue.py b/persistqueue/tests/test_sqlackqueue.py
index 465a675..2914eec 100644
--- a/persistqueue/tests/test_sqlackqueue.py
+++ b/persistqueue/tests/test_sqlackqueue.py
@@ -35,6 +35,16 @@ class SQLite3AckQueueTest(unittest.TestCase):
         # 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"""
 
diff --git a/persistqueue/tests/test_sqlqueue.py b/persistqueue/tests/test_sqlqueue.py
index a2fdaef..d71d661 100644
--- a/persistqueue/tests/test_sqlqueue.py
+++ b/persistqueue/tests/test_sqlqueue.py
@@ -36,6 +36,16 @@ class SQLite3QueueTest(unittest.TestCase):
         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"""