"""The main Redis client.
"""
- def __init__(self, host=None, port=None, timeout=None):
+ def __init__(self, host=None, port=None, timeout=None, db=None):
self.host = host or 'localhost'
self.port = port or 6379
if timeout:
socket.setdefaulttimeout(timeout)
self._sock = None
self._fp = None
+ self.db = db
def _write(self, s):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.connect()
>>> r._sock.close()
>>> try:
def ping(self):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.ping()
'PONG'
>>>
def set(self, name, value, preserve=False):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.set('a', 'pippo')
'OK'
>>> try:
def get(self, name):
"""
- >>> r = Redis()
- >>> r.set('a', 'pippo'), r.set('b', 15), r.set('c', '\\r\\naaa\\nbbb\\r\\ncccc\\nddd\\r\\n'), r.set('d', '\\r\\n')
+ >>> r = Redis(db=9)
+ >>> r.set('a', 'pippo'), r.set('b', 15), r.set('c', ' \\r\\naaa\\nbbb\\r\\ncccc\\nddd\\r\\n '), r.set('d', '\\r\\n')
('OK', 'OK', 'OK', 'OK')
>>> r.get('a')
'pippo'
>>> r.get('b')
'15'
>>> r.get('c')
- '\\r\\naaa\\nbbb\\r\\ncccc\\nddd\\r\\n'
+ ' \\r\\naaa\\nbbb\\r\\ncccc\\nddd\\r\\n '
>>> r.get('c')
- '\\r\\naaa\\nbbb\\r\\ncccc\\nddd\\r\\n'
+ ' \\r\\naaa\\nbbb\\r\\ncccc\\nddd\\r\\n '
>>> r.get('ajhsd')
>>>
"""
def mget(self, *args):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.set('a', 'pippo'), r.set('b', 15), r.set('c', '\\r\\naaa\\nbbb\\r\\ncccc\\nddd\\r\\n'), r.set('d', '\\r\\n')
('OK', 'OK', 'OK', 'OK')
>>> r.mget('a', 'b', 'c', 'd')
def incr(self, name, amount=1):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('a')
1
>>> r.incr('a')
def decr(self, name, amount=1):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> if r.get('a'):
... r.delete('a')
... else:
def exists(self, name):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.exists('dsjhfksjdhfkdsjfh')
0
>>> r.set('a', 'a')
def delete(self, name):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('dsjhfksjdhfkdsjfh')
0
>>> r.set('a', 'a')
self._write('DEL %s\r\n' % name)
return self.get_response()
- def key_type(self, name):
+ def get_type(self, name):
"""
- Not yet implemented.
+ >>> r = Redis(db=9)
+ >>> r.set('a', 3)
+ 'OK'
+ >>> r.get_type('a')
+ 'string'
+ >>> r.get_type('zzz')
+ >>>
"""
self.connect()
self._write('TYPE %s\r\n' % name)
- return self.get_response()
+ res = self.get_response()
+ return None if res == 'none' else res
def keys(self, pattern):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.flush()
'OK'
>>> r.set('a', 'a')
def randomkey(self):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.set('a', 'a')
'OK'
>>> isinstance(r.randomkey(), str)
def rename(self, src, dst, preserve=False):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> try:
... r.rename('a', 'a')
... except ResponseError, e:
return self.get_response()
else:
self._write('RENAME %s %s\r\n' % (src, dst))
- return self.get_response().strip()
+ return self.get_response() #.strip()
+
+ def expire(self, name, time):
+ """
+ >>> r = Redis(db=9)
+ >>> r.set('a', 1)
+ 'OK'
+ >>> r.expire('a', 1)
+ 1
+ >>> r.expire('zzzzz', 1)
+ 0
+ >>>
+ """
+ self.connect()
+ self._write('EXPIRE %s %s\r\n' % (name, time))
+ return self.get_response()
def push(self, name, value, tail=False):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> r.push('l', 'a')
def llen(self, name):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> r.push('l', 'a')
def lrange(self, name, start, end):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> r.lrange('l', 0, 1)
def ltrim(self, name, start, end):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> try:
def lindex(self, name, index):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> res = r.delete('l')
>>> r.lindex('l', 0)
>>> r.push('l', 'aaa')
def pop(self, name, tail=False):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> r.pop('l')
def lset(self, name, index, value):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> try:
def lrem(self, name, value, num=0):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> r.push('l', 'aaa')
def sort(self, name, by=None, get=None, start=None, num=None, desc=False, alpha=False):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('l')
1
>>> r.push('l', 'ccc')
def sadd(self, name, value):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> res = r.delete('s')
>>> r.sadd('s', 'a')
1
def srem(self, name, value):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('s')
1
>>> r.srem('s', 'aaa')
def sismember(self, name, value):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('s')
1
>>> r.sismember('s', 'b')
def sinter(self, *args):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> res = r.delete('s1')
>>> res = r.delete('s2')
>>> res = r.delete('s3')
def sinterstore(self, dest, *args):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> res = r.delete('s1')
>>> res = r.delete('s2')
>>> res = r.delete('s3')
def smembers(self, name):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('s')
1
>>> r.sadd('s', 'a')
def select(self, db):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.delete('a')
1
- >>> r.select(1)
+ >>> r.select(10)
'OK'
>>> r.set('a', 1)
'OK'
- >>> r.select(0)
+ >>> r.select(9)
'OK'
>>> r.get('a')
>>>
def move(self, name, db):
"""
- >>> r = Redis()
- >>> r.select(0)
- 'OK'
+ >>> r = Redis(db=9)
>>> r.set('a', 'a')
'OK'
- >>> r.select(1)
+ >>> r.select(10)
'OK'
>>> if r.get('a'):
... r.delete('a')
... else:
... print 1
1
- >>> r.select(0)
+ >>> r.select(9)
'OK'
- >>> r.move('a', 1)
+ >>> r.move('a', 10)
1
>>> r.get('a')
- >>> r.select(1)
+ >>> r.select(10)
'OK'
>>> r.get('a')
'a'
- >>> r.select(0)
+ >>> r.select(9)
'OK'
>>>
"""
def save(self, background=False):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.save()
'OK'
>>> try:
def lastsave(self):
"""
>>> import time
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> t = int(time.time())
>>> r.save()
'OK'
def flush(self, all_dbs=False):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.flush()
'OK'
- >>> r.flush(all_dbs=True)
- 'OK'
+ >>> # r.flush(all_dbs=True)
>>>
"""
self.connect()
def info(self):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> info = r.info()
>>> info and isinstance(info, dict)
True
if c != '$':
raise InvalidResponse("Unkown response prefix for '%s'" % data)
buf = []
- while i > 0:
+ while True:
data = self._read()
i -= len(data)
- if len(data) > i:
- # we got the ending crlf
- data = data.rstrip()
buf.append(data)
- if i == 0:
- # the data has a trailing crlf embedded, let's restore it
- buf.append(self._read())
- return ''.join(buf)
+ if i < 0:
+ break
+ return ''.join(buf)[:-2]
def disconnect(self):
if isinstance(self._sock, socket.socket):
def connect(self):
"""
- >>> r = Redis()
+ >>> r = Redis(db=9)
>>> r.connect()
>>> isinstance(r._sock, socket.socket)
True
+ >>> r.disconnect()
>>>
"""
if isinstance(self._sock, socket.socket):
else:
self._sock = sock
self._fp = self._sock.makefile('r')
-
+ if self.db:
+ self.select(self.db)
+
if __name__ == '__main__':
import doctest
doctest.testmod()
-
\ No newline at end of file
+