Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for an an empty Redis key in python

Tags:

python

redis

I am using redis-py (https://redis-py.readthedocs.org/en/latest/).

lets say I have some redis keys saved and av. 'av' has no assigned value so using the command line, I can do:

>>> redis.get('saved')
Out[4]: 'None'
>>> redis.get('av')
>>> redis.get('saved')
Out[6]: 'None'

How would you test for 'no assigned value' for a key in this context?

like image 303
user1592380 Avatar asked Jul 07 '15 17:07

user1592380


1 Answers

You should use the method exists, which returns a boolean - True if your key is set, False otherwise. You should avoid the use of get for that, as it can raise an exception if you have a list value for that key.

Example:

>>> redis.exists("av")
False
like image 184
Jundiaius Avatar answered Oct 04 '22 01:10

Jundiaius