X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/11cb1537835786275c57afa5461dbd71b5a710b2..ca37e9cde814dd093cf2cf5d36f178854d768b3e:/client-libraries/python/redis.py diff --git a/client-libraries/python/redis.py b/client-libraries/python/redis.py index 6187cf9d..881dfd1d 100644 --- a/client-libraries/python/redis.py +++ b/client-libraries/python/redis.py @@ -32,17 +32,18 @@ class Redis(object): """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: @@ -76,7 +77,7 @@ class Redis(object): def ping(self): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.ping() 'PONG' >>> @@ -87,7 +88,7 @@ class Redis(object): def set(self, name, value, preserve=False): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.set('a', 'pippo') 'OK' >>> try: @@ -118,8 +119,8 @@ class Redis(object): 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' @@ -130,9 +131,9 @@ class Redis(object): >>> 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') >>> """ @@ -142,7 +143,7 @@ class Redis(object): 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') @@ -155,7 +156,7 @@ class Redis(object): def incr(self, name, amount=1): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('a') 1 >>> r.incr('a') @@ -175,7 +176,7 @@ class Redis(object): def decr(self, name, amount=1): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> if r.get('a'): ... r.delete('a') ... else: @@ -198,7 +199,7 @@ class Redis(object): def exists(self, name): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.exists('dsjhfksjdhfkdsjfh') 0 >>> r.set('a', 'a') @@ -213,7 +214,7 @@ class Redis(object): def delete(self, name): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('dsjhfksjdhfkdsjfh') 0 >>> r.set('a', 'a') @@ -230,17 +231,24 @@ class Redis(object): 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') @@ -263,7 +271,7 @@ class Redis(object): def randomkey(self): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.set('a', 'a') 'OK' >>> isinstance(r.randomkey(), str) @@ -277,7 +285,7 @@ class Redis(object): def rename(self, src, dst, preserve=False): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> try: ... r.rename('a', 'a') ... except ResponseError, e: @@ -302,11 +310,26 @@ class Redis(object): 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') @@ -333,7 +356,7 @@ class Redis(object): def llen(self, name): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('l') 1 >>> r.push('l', 'a') @@ -352,7 +375,7 @@ class Redis(object): def lrange(self, name, start, end): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('l') 1 >>> r.lrange('l', 0, 1) @@ -379,7 +402,7 @@ class Redis(object): def ltrim(self, name, start, end): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('l') 1 >>> try: @@ -409,7 +432,7 @@ class Redis(object): def lindex(self, name, index): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> res = r.delete('l') >>> r.lindex('l', 0) >>> r.push('l', 'aaa') @@ -431,7 +454,7 @@ class Redis(object): def pop(self, name, tail=False): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('l') 1 >>> r.pop('l') @@ -461,7 +484,7 @@ class Redis(object): def lset(self, name, index, value): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('l') 1 >>> try: @@ -494,7 +517,7 @@ class Redis(object): def lrem(self, name, value, num=0): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('l') 1 >>> r.push('l', 'aaa') @@ -531,7 +554,7 @@ class Redis(object): 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') @@ -592,7 +615,7 @@ class Redis(object): def sadd(self, name, value): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> res = r.delete('s') >>> r.sadd('s', 'a') 1 @@ -613,7 +636,7 @@ class Redis(object): def srem(self, name, value): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('s') 1 >>> r.srem('s', 'aaa') @@ -639,7 +662,7 @@ class Redis(object): def sismember(self, name, value): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('s') 1 >>> r.sismember('s', 'b') @@ -665,7 +688,7 @@ class Redis(object): def sinter(self, *args): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> res = r.delete('s1') >>> res = r.delete('s2') >>> res = r.delete('s3') @@ -697,7 +720,7 @@ class Redis(object): def sinterstore(self, dest, *args): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> res = r.delete('s1') >>> res = r.delete('s2') >>> res = r.delete('s3') @@ -721,7 +744,7 @@ class Redis(object): def smembers(self, name): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.delete('s') 1 >>> r.sadd('s', 'a') @@ -743,14 +766,14 @@ class Redis(object): 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') >>> @@ -761,28 +784,26 @@ class Redis(object): 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' >>> """ @@ -792,7 +813,7 @@ class Redis(object): def save(self, background=False): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> r.save() 'OK' >>> try: @@ -813,7 +834,7 @@ class Redis(object): def lastsave(self): """ >>> import time - >>> r = Redis() + >>> r = Redis(db=9) >>> t = int(time.time()) >>> r.save() 'OK' @@ -827,11 +848,10 @@ class Redis(object): 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() @@ -840,7 +860,7 @@ class Redis(object): def info(self): """ - >>> r = Redis() + >>> r = Redis(db=9) >>> info = r.info() >>> info and isinstance(info, dict) True @@ -889,17 +909,13 @@ class Redis(object): 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): @@ -912,10 +928,11 @@ class Redis(object): 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): @@ -928,9 +945,11 @@ class Redis(object): 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 +