Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pipelines now have a __len__() method.

The length of a pipeline is defined as the length of its command
stack.  This makes it easy to inspect the number of batched commands
and to write conditional logic for empty pipelines.
  • Loading branch information...
commit 5827683b8d2be7bd060ab579d8ab4655e4820052 1 parent 395ca0e
Jon Parise authored
Showing with 19 additions and 0 deletions.
  1. +3 −0  redis/client.py
  2. +16 −0 tests/pipeline.py
3  redis/client.py
View
@@ -1624,6 +1624,9 @@ def __del__(self):
except:
pass
+ def __len__(self):
+ return len(self.command_stack)
+
def reset(self):
self.command_stack = []
self.scripts = set()
16 tests/pipeline.py
View
@@ -28,6 +28,22 @@ def test_pipeline(self):
]
)
+ def test_pipeline_length(self):
+ with self.client.pipeline() as pipe:
+ # Initially empty.
+ self.assertEquals(len(pipe), 0)
+ self.assertFalse(pipe)
+
+ # Fill 'er up!
+ pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
+ self.assertEquals(len(pipe), 3)
+ self.assertTrue(pipe)
+
+ # Execute calls reset(), so empty once again.
+ pipe.execute()
+ self.assertEquals(len(pipe), 0)
+ self.assertFalse(pipe)
+
def test_pipeline_no_transaction(self):
with self.client.pipeline(transaction=False) as pipe:
pipe.set('a', 'a1').set('b', 'b1').set('c', 'c1')
Please sign in to comment.
Something went wrong with that request. Please try again.