signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def connect(self):
if self._sock:<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>sock = self._connect()<EOL><DEDENT>except socket.error:<EOL><INDENT>e = sys.exc_info()[<NUM_LIT:1>]<EOL>raise ConnectionError(self._error_message(e))<EOL><DEDENT>self._sock = sock<EOL>try:<EOL><INDENT>self.on_connect()<EOL><DEDENT>except SSDBError:<EOL><INDENT>self.disconnect()<EOL>raise<EOL><DEDENT>for callback in self._connect_callbacks:<EOL><INDENT>callback(self)<EOL><DEDENT>
Connects to the SSDB server if not already connected
f13248:c4:m6
def _connect(self):
<EOL>err = None<EOL>for res in socket.getaddrinfo(self.host, self.port, <NUM_LIT:0>,<EOL>socket.SOCK_STREAM):<EOL><INDENT>family, socktype, proto, canonname, socket_address = res<EOL>sock = None<EOL>try:<EOL><INDENT>sock = socket.socket(family, socktype, proto)<EOL>sock.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY, <NUM_LIT:1>)<EOL>if self.socket_keepalive:<EOL><INDENT>sock.setsockopt(socket.SOL_SOCKET,socket.SO_KEEPALIVE, <NUM_LIT:1>)<EOL>for k, v in iteritems(self.socket_keepalive_options):<EOL><INDENT>sock.setsockopt(socket.SOL_TCP, k, v) <EOL><DEDENT><DEDENT>sock.settimeout(self.socket_connect_timeout)<EOL>sock.connect(socket_address)<EOL>sock.settimeout(self.socket_timeout)<EOL>return sock <EOL><DEDENT>except socket.error as _:<EOL><INDENT>err = _<EOL>if sock is not None:<EOL><INDENT>sock.close()<EOL><DEDENT><DEDENT><DEDENT>if err is not None:<EOL><INDENT>raise err<EOL><DEDENT>raise socket.error("<STR_LIT>")<EOL>
Create a TCP socket connection
f13248:c4:m7
def _error_message(self, exception):
if len(exception.args) == <NUM_LIT:1>:<EOL><INDENT>return "<STR_LIT>" %(self.host, self.port, exception.args[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT>" %(exception.args[<NUM_LIT:0>], self.host, self.port, exception.args[<NUM_LIT:1>])<EOL><DEDENT>
args for socket.error can either be (errno, "message") or just "message"
f13248:c4:m8
def on_connect(self):
self._parser.on_connect(self)<EOL>
Initialize the connection
f13248:c4:m9
def disconnect(self):
self._parser.on_disconnect()<EOL>if self._sock is None:<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>self._sock.shutdown(socket.SHUT_RDWR)<EOL>self._sock.close()<EOL><DEDENT>except socket.error:<EOL><INDENT>pass<EOL><DEDENT>self._sock = None<EOL>
Disconnects from the SSDB server
f13248:c4:m10
def send_packed_command(self, command):
if not self._sock:<EOL><INDENT>self.connect()<EOL><DEDENT>try:<EOL><INDENT>if isinstance(command, str):<EOL><INDENT>command = [command]<EOL><DEDENT>for item in command:<EOL><INDENT>self._sock.sendall(item) <EOL><DEDENT><DEDENT>except socket.timeout:<EOL><INDENT>self.disconnect()<EOL>raise TimeoutError("<STR_LIT>") <EOL><DEDENT>except socket.error:<EOL><INDENT>e = sys.exc_info()[<NUM_LIT:1>]<EOL>self.disconnect()<EOL>if len(e.args) == <NUM_LIT:1>:<EOL><INDENT>_errno, errmsg = '<STR_LIT>', e.args[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>_errno, errmsg = e.args<EOL><DEDENT>raise ConnectionError("<STR_LIT>" %<EOL>(_errno, errmsg))<EOL><DEDENT>except:<EOL><INDENT>self.disconnect()<EOL>raise<EOL><DEDENT>
Send an already packed command to the SSDB server
f13248:c4:m11
def send_command(self, *args):
self.send_packed_command(self.pack_command(*args))<EOL>
Pack and send a command to the SSDB server
f13248:c4:m12
def can_read(self, timeout=<NUM_LIT:0>):
sock = self._sock<EOL>if not sock:<EOL><INDENT>self.connect()<EOL>sock = self._sock<EOL><DEDENT>return self._parser.can_read() orbool(select([sock], [], [], timeout)[<NUM_LIT:0>])<EOL>
Poll the socket to see if there's data that can be read.
f13248:c4:m13
def read_response(self):
try:<EOL><INDENT>response = self._parser.read_response()<EOL><DEDENT>except:<EOL><INDENT>self.disconnect()<EOL>raise<EOL><DEDENT>if isinstance(response, ResponseError):<EOL><INDENT>raise response<EOL><DEDENT>return response<EOL>
Read the response from a previously sent command
f13248:c4:m14
def encode(self, value):
if isinstance(value, Token):<EOL><INDENT>return b(value.value) <EOL><DEDENT>if isinstance(value, bytes):<EOL><INDENT>return value<EOL><DEDENT>elif isinstance(value, (int, long)):<EOL><INDENT>value = b(str(value)) <EOL><DEDENT>elif isinstance(value, float):<EOL><INDENT>value = repr(value)<EOL><DEDENT>elif not isinstance(value, basestring):<EOL><INDENT>value = str(value)<EOL><DEDENT>if isinstance(value, unicode):<EOL><INDENT>value = value.encode(self.encoding, self.encoding_errors)<EOL><DEDENT>return value<EOL>
Return a bytestring representation of the value
f13248:c4:m15
def pack_command(self, *args):
<EOL>command = args[<NUM_LIT:0>]<EOL>if '<STR_LIT:U+0020>' in command:<EOL><INDENT>args = tuple([Token(s) for s in command.split('<STR_LIT:U+0020>')]) + args[<NUM_LIT:1>:]<EOL><DEDENT>else:<EOL><INDENT>args = (Token(command),) + args[<NUM_LIT:1>:]<EOL><DEDENT>args_output = SYM_EMPTY.join([<EOL>SYM_EMPTY.join((<EOL>b(str(len(k))),<EOL>SYM_LF,<EOL>k,<EOL>SYM_LF<EOL>)) for k in imap(self.encode, args)<EOL>])<EOL>output = "<STR_LIT>" % (args_output,SYM_LF)<EOL>return output<EOL>
Pack a series of arguments into a value SSDB command
f13248:c4:m16
def pack_commands(self, commands):
output = []<EOL>pieces = []<EOL>buffer_length = <NUM_LIT:0><EOL>for cmd in commands:<EOL><INDENT>for chunk in self.pack_command(*cmd):<EOL><INDENT>pieces.append(chunk)<EOL>buffer_length += len(chunk)<EOL><DEDENT>if buffer_length > <NUM_LIT>:<EOL><INDENT>output.append(SYM_EMPTY.join(pieces))<EOL>buffer_length = <NUM_LIT:0><EOL>pieces = []<EOL><DEDENT><DEDENT>if pieces:<EOL><INDENT>output.append(SYM_EMPTY.join(pieces))<EOL><DEDENT>return output<EOL>
Pack multiple commands into the SSDB protocol
f13248:c4:m17
def __init__(self, connection_class=Connection, max_connections=None,<EOL>**connection_kwargs):
max_connections = max_connections or <NUM_LIT:2> ** <NUM_LIT><EOL>if not isinstance(max_connections, (int, long)) or max_connections < <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>') <EOL><DEDENT>self.connection_class = connection_class<EOL>self.connection_kwargs = connection_kwargs<EOL>self.max_connections = max_connections<EOL>self.reset()<EOL>
Create a connection pool. If max_connections is set, then this object raises ssdb.ConnectionError when the pool's limit is reached. By default, TCP connections are created connection_class is specified. Any additionan keyword arguments are passed to the constructor of connection_class.
f13248:c5:m0
def get_connection(self, command_name, *keys, **options):
self._checkpid()<EOL>try:<EOL><INDENT>connection = self._available_connections.pop()<EOL><DEDENT>except IndexError:<EOL><INDENT>connection = self.make_connection()<EOL><DEDENT>self._in_use_connections.add(connection)<EOL>return connection<EOL>
Get a connection from pool.
f13248:c5:m4
def make_connection(self):
if self._created_connections >= self.max_connections:<EOL><INDENT>raise ConnectionError("<STR_LIT>")<EOL><DEDENT>self._created_connections += <NUM_LIT:1><EOL>return self.connection_class(**self.connection_kwargs)<EOL>
Create a new connection
f13248:c5:m5
def release(self, connection):
self._checkpid()<EOL>if connection.pid != self.pid:<EOL><INDENT>return <EOL><DEDENT>self._in_use_connections.remove(connection)<EOL>self._available_connections.append(connection)<EOL>
Release the connection back to the pool.
f13248:c5:m6
def disconnect(self):
all_conns = chain(self._available_connections,<EOL>self._in_use_connections)<EOL>for connection in all_conns:<EOL><INDENT>connection.disconnect()<EOL><DEDENT>
Disconnects all connections in the pool.
f13248:c5:m7
def make_connection(self):
connection = self.connection_class(**self.connection_kwargs)<EOL>self._connections.append(connection)<EOL>return connection<EOL>
Make a fresh connection.
f13248:c6:m2
def get_connection(self, command_name, *keys, **options):
<EOL>self._checkpid()<EOL>connection = None<EOL>try:<EOL><INDENT>connection = self.pool.get(block=True,timeout=self.timeout)<EOL><DEDENT>except Empty:<EOL><INDENT>raise ConnectionError("<STR_LIT>")<EOL><DEDENT>if connection is None:<EOL><INDENT>connection = self.make_connection()<EOL><DEDENT>return connection<EOL>
Get a connection, blocking for ``self.timeout`` until a connection is available from the pool. If the connection returned is ``None`` then creates a new connection. Because we use a last-in first-out queue, the existing connections (having been returned to the pool after the initial ``None`` values were added) will be returned before ``None`` values. This means we only create new connections when we need to, i.e.: the actual number of connections will only increase in response to demand.
f13248:c6:m3
def release(self, connection):
<EOL>self._checkpid()<EOL>if connection.pid != self.pid:<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>self.pool.put_nowait(connection)<EOL><DEDENT>except Full:<EOL><INDENT>pass<EOL><DEDENT>
Releases the connection back to the pool.
f13248:c6:m4
def disconnect(self):
for connection in self._connections:<EOL><INDENT>connection.disconnect()<EOL><DEDENT>
Disconnects all connections in the pool.
f13248:c6:m5
def execute(self, raise_on_error=True):
stack = self.command_stack<EOL>if not stack:<EOL><INDENT>return []<EOL><DEDENT>execute = self._execute_pipeline<EOL>conn = self.connection<EOL>if not conn:<EOL><INDENT>conn = self.connection_pool.get_connection('<STR_LIT>')<EOL>self.connection = conn<EOL><DEDENT>try:<EOL><INDENT>return execute(conn, stack, raise_on_error)<EOL><DEDENT>except ConnectionError:<EOL><INDENT>conn.disconnect()<EOL>return execute(conn, stack, raise_on_error)<EOL><DEDENT>finally:<EOL><INDENT>self.reset()<EOL><DEDENT>
Execute all the commands in the current pipeline
f13249:c0:m11
def timestamp_to_datetime(response):
if not response:<EOL><INDENT>return None<EOL><DEDENT>try:<EOL><INDENT>response = int(response)<EOL><DEDENT>except ValueError:<EOL><INDENT>return None<EOL><DEDENT>return datetime.datetime.fromtimestamp(response)<EOL>
Converts a unix timestamp to a Python datetime object.
f13250:m1
def parse_debug_object(response):
<EOL>response = nativestr(response)<EOL>response = '<STR_LIT>' + response<EOL>response = dict([kv.split('<STR_LIT::>') for kv in response.split()])<EOL>int_fields = ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>')<EOL>for field in int_fields:<EOL><INDENT>if field in response:<EOL><INDENT>response[field] = int(response[field])<EOL><DEDENT><DEDENT>return response<EOL>
Parse the results of ssdb's DEBUG OBJECT command into a Python dict
f13250:m9
def parse_object(response, infotype):
if infotype in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>return int(response)<EOL><DEDENT>return response<EOL>
Parse the results of an OBJECT command
f13250:m10
def set_response_callback(self, command, callback):
self.response_callbacks[command] = callback<EOL>
Set a custom Response Callback
f13250:c0:m2
def execute_command(self, *args, **options):
pool = self.connection_pool<EOL>command_name = args[<NUM_LIT:0>]<EOL>connection = pool.get_connection(command_name, **options)<EOL>try:<EOL><INDENT>connection.send_command(*args)<EOL>return self.parse_response(connection, command_name, **options)<EOL><DEDENT>except ConnectionError:<EOL><INDENT>connection.disconnect()<EOL>connection.send_command(*args)<EOL>return self.parse_response(connection, command_name, **options)<EOL><DEDENT>finally:<EOL><INDENT>pool.release(connection)<EOL><DEDENT>
Execute a command and return a parsed response.
f13250:c0:m3
def parse_response(self, connection, command_name, **options):
response = connection.read_response()<EOL>if command_name in self.response_callbacks and len(response):<EOL><INDENT>status = nativestr(response[<NUM_LIT:0>])<EOL>if status == RES_STATUS.OK:<EOL><INDENT>return self.response_callbacks[command_name](response[<NUM_LIT:1>:],<EOL>**options)<EOL><DEDENT>elif status == RES_STATUS.NOT_FOUND:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>raise DataError(RES_STATUS_MSG[status]+'<STR_LIT::>'.join(response))<EOL><DEDENT><DEDENT>return response<EOL>
Parses a response from the ssdb server
f13250:c0:m4
def get(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Return the value at key ``name``, or ``None`` if the key doesn't exist Like **Redis.GET** :param string name: the key name :return: the value at key ``name``, or ``None`` if the key doesn't exist :rtype: string >>> ssdb.get("set_abc") 'abc' >>> ssdb.get("set_a") 'a' >>> ssdb.get("set_b") 'b' >>> ssdb.get("not_exists_abc") >>>
f13250:c0:m5
def set(self, name, value):
return self.execute_command('<STR_LIT>', name, value)<EOL>
Set the value at key ``name`` to ``value`` . Like **Redis.SET** :param string name: the key name :param string value: a string or an object can be converted to string :return: ``True`` on success, ``False`` if not :rtype: bool >>> ssdb.set("set_cde", 'cde') True >>> ssdb.set("set_cde", 'test') True >>> ssdb.set("hundred", 100) True
f13250:c0:m6
def setnx(self, name, value):
return self.execute_command('<STR_LIT>', name, value)<EOL>
Set the value at key ``name`` to ``value`` if and only if the key doesn't exist. Like **Redis.SETNX** :param string name: the key name :param string value: a string or an object can be converted to string :return: ``True`` on success, ``False`` if not :rtype: bool >>> ssdb.setnx("setnx_test", 'abc') True >>> ssdb.setnx("setnx_test", 'cde') False
f13250:c0:m7
def getset(self, name, value):
return self.execute_command('<STR_LIT>', name, value)<EOL>
Set the value at key ``name`` to ``value`` if key doesn't exist Return the value at key ``name`` atomically. Like **Redis.GETSET** :param string name: the key name :param string value: a string or an object can be converted to string :return: ``True`` on success, ``False`` if not :rtype: bool >>> ssdb.getset("getset_a", 'abc') None >>> ssdb.getset("getset_a", 'def') 'abc' >>> ssdb.getset("getset_a", 'ABC') 'def' >>> ssdb.getset("getset_a", 123) 'ABC'
f13250:c0:m8
def delete(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Delete the key specified by ``name`` . Like **Redis.DELETE** .. note:: ``Delete`` **can't delete** the `Hash`_ or `Zsets`_, use `hclear`_ for `Hash`_ and `zclear`_ for `Zsets`_ :param string name: the key name :return: ``True`` on deleting successfully, or ``False`` if the key doesn't exist or failure :rtype: bool >>> ssdb.delete('set_abc') True >>> ssdb.delete('set_a') True >>> ssdb.delete('set_abc') False >>> ssdb.delete('not_exist') False
f13250:c0:m9
def expire(self, name, ttl):
if isinstance(time, datetime.timedelta):<EOL><INDENT>time = time.seconds + time.days * <NUM_LIT> * <NUM_LIT> <EOL><DEDENT>return self.execute_command('<STR_LIT>', name, ttl)<EOL>
Set an expire flag on key ``name`` for ``ttl`` seconds. ``ttl`` can be represented by an integer or a Python timedelta object. Like **Redis.EXPIRE** .. note:: ``Expire`` **only expire** the `Key/Value`_ . :param string name: the key name :param int ttl: number of seconds to live :return: ``True`` on success, or ``False`` if the key doesn't exist or failure :rtype: bool >>> ssdb.expire('set_abc', 6) True >>> ssdb.expire('not_exist') False
f13250:c0:m10
def ttl(self, name):
if isinstance(time, datetime.timedelta):<EOL><INDENT>time = time.seconds + time.days * <NUM_LIT> * <NUM_LIT> <EOL><DEDENT>return self.execute_command('<STR_LIT>', name, ttl)<EOL>
Returns the number of seconds until the key ``name`` will expire. Like **Redis.TTL** .. note:: ``ttl`` **can only** be used to the `Key/Value`_ . :param string name: the key name :return: the number of seconds, or ``-1`` if the key doesn't exist or have no ttl :rtype: int >>> ssdb.ttl('set_abc') 6 >>> ssdb.ttl('not_exist') -1
f13250:c0:m11
def exists(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Return a boolean indicating whether key ``name`` exists. Like **Redis.EXISTS** .. note:: ``exists`` **can't indicate** whether any `Hash`_, `Zsets`_ or `Queue`_ exists, use `hash_exists`_ for `Hash`_ , `zset_exists`_ for `Zsets`_ and `queue_exists`_ for `Queue`_ . :param string name: the key name :return: ``True`` if the key exists, ``False`` if not :rtype: bool >>> ssdb.exists('set_abc') True >>> ssdb.exists('set_a') True >>> ssdb.exists('not_exist') False
f13250:c0:m12
def incr(self, name, amount=<NUM_LIT:1>):
amount = get_integer('<STR_LIT>', amount)<EOL>return self.execute_command('<STR_LIT>', name, amount)<EOL>
Increase the value at key ``name`` by ``amount``. If no key exists, the value will be initialized as ``amount`` . Like **Redis.INCR** :param string name: the key name :param int amount: increments :return: the integer value at key ``name`` :rtype: int >>> ssdb.incr('set_count', 3) 13 >>> ssdb.incr('set_count', 1) 14 >>> ssdb.incr('set_count', -2) 12 >>> ssdb.incr('temp_count', 42) 42
f13250:c0:m13
def decr(self, name, amount=<NUM_LIT:1>):
amount = get_positive_integer('<STR_LIT>', amount)<EOL>return self.execute_command('<STR_LIT>', name, amount)<EOL>
Decrease the value at key ``name`` by ``amount``. If no key exists, the value will be initialized as 0 - ``amount`` . Like **Redis.DECR** :param string name: the key name :param int amount: decrements :return: the integer value at key ``name`` :rtype: int >>> ssdb.decr('set_count', 3) 7 >>> ssdb.decr('set_count', 1) 6 >>> ssdb.decr('temp_count', 42) -42
f13250:c0:m14
def getbit(self, name, offset):
offset = get_positive_integer('<STR_LIT>', offset)<EOL>return self.execute_command('<STR_LIT>', name, offset)<EOL>
Returns a boolean indicating the value of ``offset`` in ``name`` Like **Redis.GETBIT** :param string name: the key name :param int offset: the bit position :param bool val: the bit value :return: the bit at the ``offset`` , ``False`` if key doesn't exist or offset exceeds the string length. :rtype: bool >>> ssdb.set('bit_test', 1) True >>> ssdb.getbit('bit_test', 0) True >>> ssdb.getbit('bit_test', 1) False
f13250:c0:m15
def setbit(self, name, offset, val):
val = int(get_boolean('<STR_LIT>', val))<EOL>offset = get_positive_integer('<STR_LIT>', offset)<EOL>return self.execute_command('<STR_LIT>', name, offset, val)<EOL>
Flag the ``offset`` in ``name`` as ``value``. Returns a boolean indicating the previous value of ``offset``. Like **Redis.SETBIT** :param string name: the key name :param int offset: the bit position :param bool val: the bit value :return: the previous bit (False or True) at the ``offset`` :rtype: bool >>> ssdb.set('bit_test', 1) True >>> ssdb.setbit('bit_test', 1, 1) False >>> ssdb.get('bit_test') 3 >>> ssdb.setbit('bit_test', 2, 1) False >>> ssdb.get('bit_test') 7
f13250:c0:m16
def countbit(self, name, start=None, size=None):
if start is not None and size is not None:<EOL><INDENT>start = get_integer('<STR_LIT:start>', start)<EOL>size = get_integer('<STR_LIT:size>', size)<EOL>return self.execute_command('<STR_LIT>', name, start, size)<EOL><DEDENT>elif start is not None:<EOL><INDENT>start = get_integer('<STR_LIT:start>', start) <EOL>return self.execute_command('<STR_LIT>', name, start)<EOL><DEDENT>return self.execute_command('<STR_LIT>', name)<EOL>
Returns the count of set bits in the value of ``key``. Optional ``start`` and ``size`` paramaters indicate which bytes to consider. Similiar with **Redis.BITCOUNT** :param string name: the key name :param int start: Optional, if start is negative, count from start'th character from the end of string. :param int size: Optional, if size is negative, then that many characters will be omitted from the end of string. :return: the count of the bit 1 :rtype: int >>> ssdb.set('bit_test', 1) True >>> ssdb.countbit('bit_test') 3 >>> ssdb.set('bit_test','1234567890') True >>> ssdb.countbit('bit_test', 0, 1) 3 >>> ssdb.countbit('bit_test', 3, -3) 16
f13250:c0:m17
def substr(self, name, start=None, size=None):
if start is not None and size is not None:<EOL><INDENT>start = get_integer('<STR_LIT:start>', start)<EOL>size = get_integer('<STR_LIT:size>', size)<EOL>return self.execute_command('<STR_LIT>', name, start, size)<EOL><DEDENT>elif start is not None:<EOL><INDENT>start = get_integer('<STR_LIT:start>', start) <EOL>return self.execute_command('<STR_LIT>', name, start)<EOL><DEDENT>return self.execute_command('<STR_LIT>', name)<EOL>
Return a substring of the string at key ``name``. ``start`` and ``size`` are 0-based integers specifying the portion of the string to return. Like **Redis.SUBSTR** :param string name: the key name :param int start: Optional, the offset of first byte returned. If start is negative, the returned string will start at the start'th character from the end of string. :param int size: Optional, number of bytes returned. If size is negative, then that many characters will be omitted from the end of string. :return: The extracted part of the string. :rtype: string >>> ssdb.set('str_test', 'abc12345678') True >>> ssdb.substr('str_test', 2, 4) 'c123' >>> ssdb.substr('str_test', -2, 2) '78' >>> ssdb.substr('str_test', 1, -1) 'bc1234567'
f13250:c0:m18
def strlen(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Return the number of bytes stored in the value of ``name`` Like **Redis.STRLEN** :param string name: the key name :return: The number of bytes of the string, if key not exists, returns 0. :rtype: int >>> ssdb.set('str_test', 'abc12345678') True >>> ssdb.strlen('str_test') 11
f13250:c0:m19
def multi_set(self, **kvs):
return self.execute_command('<STR_LIT>', *dict_to_list(kvs))<EOL>
Set key/value based on a mapping dictionary as kwargs. Like **Redis.MSET** :param dict kvs: a key/value mapping dict :return: the number of successful operation :rtype: int >>> ssdb.multi_set(set_a='a', set_b='b', set_c='c', set_d='d') 4 >>> ssdb.multi_set(set_abc='abc',set_count=10) 2
f13250:c0:m20
def multi_get(self, *names):
return self.execute_command('<STR_LIT>', *names)<EOL>
Return a dictionary mapping key/value by ``names`` Like **Redis.MGET** :param list names: a list of keys :return: a dict mapping key/value :rtype: dict >>> ssdb.multi_get('a', 'b', 'c', 'd') {'a': 'a', 'c': 'c', 'b': 'b', 'd': 'd'} >>> ssdb.multi_get('set_abc','set_count') {'set_abc': 'set_abc', 'set_count': '10'}
f13250:c0:m21
def multi_del(self, *names):
return self.execute_command('<STR_LIT>', *names)<EOL>
Delete one or more keys specified by ``names`` Like **Redis.DELETE** :param list names: a list of keys :return: the number of successful deletion :rtype: int >>> ssdb.multi_del('a', 'b', 'c', 'd') 4 >>> ssdb.multi_del('set_abc','set_count') 2
f13250:c0:m22
def keys(self, name_start, name_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit)<EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Return a list of the top ``limit`` keys between ``name_start`` and ``name_end`` Similiar with **Redis.KEYS** .. note:: The range is (``name_start``, ``name_end``]. ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The lower bound(not included) of keys to be returned, empty string ``''`` means -inf :param string name_end: The upper bound(included) of keys to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a list of keys :rtype: list >>> ssdb.keys('set_x1', 'set_x3', 10) ['set_x2', 'set_x3'] >>> ssdb.keys('set_x ', 'set_xx', 3) ['set_x1', 'set_x2', 'set_x3'] >>> ssdb.keys('set_x ', '', 3) ['set_x1', 'set_x2', 'set_x3', 'set_x4'] >>> ssdb.keys('set_zzzzz ', '', ) []
f13250:c0:m23
def scan(self, name_start, name_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Scan and return a dict mapping key/value in the top ``limit`` keys between ``name_start`` and ``name_end`` in ascending order Similiar with **Redis.SCAN** .. note:: The range is (``name_start``, ``name_end``]. ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The lower bound(not included) of keys to be returned, empty string ``''`` means -inf :param string name_end: The upper bound(included) of keys to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a dict mapping key/value in ascending order :rtype: OrderedDict >>> ssdb.scan('set_x1', 'set_x3', 10) {'set_x2': 'x2', 'set_x3': 'x3'} >>> ssdb.scan('set_x ', 'set_xx', 3) {'set_x1': 'x1', 'set_x2': 'x2', 'set_x3': 'x3'} >>> ssdb.scan('set_x ', '', 10) {'set_x1': 'x1', 'set_x2': 'x2', 'set_x3': 'x3', 'set_x4': 'x4'} >>> ssdb.scan('set_zzzzz ', '', 10) {}
f13250:c0:m24
def rscan(self, name_start, name_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Scan and return a dict mapping key/value in the top ``limit`` keys between ``name_start`` and ``name_end`` in descending order .. note:: The range is (``name_start``, ``name_end``]. ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The upper bound(not included) of keys to be returned, empty string ``''`` means +inf :param string name_end: The lower bound(included) of keys to be returned, empty string ``''`` means -inf :param int limit: number of elements will be returned. :return: a dict mapping key/value in descending order :rtype: OrderedDict >>> ssdb.scan('set_x3', 'set_x1', 10) {'set_x2': 'x2', 'set_x1': 'x1'} >>> ssdb.scan('set_xx', 'set_x ', 3) {'set_x4': 'x4', 'set_x3': 'x3', 'set_x2': 'x2'} >>> ssdb.scan('', 'set_x ', 10) {'set_x4': 'x4', 'set_x3': 'x3', 'set_x2': 'x2', 'set_x1': 'x1'} >>> ssdb.scan('', 'set_zzzzz', 10) {}
f13250:c0:m25
def hget(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Get the value of ``key`` within the hash ``name`` Like **Redis.HGET** :param string name: the hash name :param string key: the key name :return: the value at ``key`` within hash ``name`` , or ``None`` if the ``name`` or ``key`` doesn't exist :rtype: string >>> ssdb.hget("hash_1", 'a') 'A' >>> ssdb.hget("hash_1", 'b') 'B' >>> ssdb.hget("hash_1", 'z') >>> >>> ssdb.hget("hash_2", 'key1') '42'
f13250:c0:m26
def hset(self, name, key, value):
return self.execute_command('<STR_LIT>', name, key, value)<EOL>
Set the value of ``key`` within the hash ``name`` to ``value`` Like **Redis.HSET** :param string name: the hash name :param string key: the key name :param string value: a string or an object can be converted to string :return: ``True`` if ``hset`` created a new field, otherwise ``False`` :rtype: bool >>> ssdb.hset("hash_3", 'yellow', '#FFFF00') True >>> ssdb.hset("hash_3", 'red', '#FF0000') True >>> ssdb.hset("hash_3", 'blue', '#0000FF') True >>> ssdb.hset("hash_3", 'yellow', '#FFFF00') False
f13250:c0:m27
def hdel(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Remove the ``key`` from hash ``name`` Like **Redis.HDEL** :param string name: the hash name :param string key: the key name :return: ``True`` if deleted successfully, otherwise ``False`` :rtype: bool >>> ssdb.hdel("hash_2", 'key1') True >>> ssdb.hdel("hash_2", 'key2') True >>> ssdb.hdel("hash_2", 'key3') True >>> ssdb.hdel("hash_2", 'key_not_exist') False >>> ssdb.hdel("hash_not_exist", 'key1') False
f13250:c0:m28
def hclear(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
**Clear&Delete** the hash specified by ``name`` :param string name: the hash name :return: the length of removed elements :rtype: int >>> ssdb.hclear('hash_1') 7 >>> ssdb.hclear('hash_1') 0 >>> ssdb.hclear('hash_2') 6 >>> ssdb.hclear('not_exist') 0
f13250:c0:m29
def hexists(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Return a boolean indicating whether ``key`` exists within hash ``name`` Like **Redis.HEXISTS** :param string name: the hash name :param string key: the key name :return: ``True`` if the key exists, ``False`` if not :rtype: bool >>> ssdb.hexists('hash_1', 'a') True >>> ssdb.hexists('hash_2', 'key2') True >>> ssdb.hexists('hash_not_exist', 'a') False >>> ssdb.hexists('hash_1', 'z_not_exists') False >>> ssdb.hexists('hash_not_exist', 'key_not_exists') False
f13250:c0:m30
def hincr(self, name, key, amount=<NUM_LIT:1>):
amount = get_integer('<STR_LIT>', amount) <EOL>return self.execute_command('<STR_LIT>', name, key, amount)<EOL>
Increase the value of ``key`` in hash ``name`` by ``amount``. If no key exists, the value will be initialized as ``amount`` Like **Redis.HINCR** :param string name: the hash name :param string key: the key name :param int amount: increments :return: the integer value of ``key`` in hash ``name`` :rtype: int >>> ssdb.hincr('hash_2', 'key1', 7) 49 >>> ssdb.hincr('hash_2', 'key2', 3) 6 >>> ssdb.hincr('hash_2', 'key_not_exists', 101) 101 >>> ssdb.hincr('hash_not_exists', 'key_not_exists', 8848) 8848
f13250:c0:m31
def hdecr(self, name, key, amount=<NUM_LIT:1>):
amount = get_positive_integer('<STR_LIT>', amount) <EOL>return self.execute_command('<STR_LIT>', name, key, amount)<EOL>
Decrease the value of ``key`` in hash ``name`` by ``amount``. If no key exists, the value will be initialized as 0 - ``amount`` :param string name: the hash name :param string key: the key name :param int amount: increments :return: the integer value of ``key`` in hash ``name`` :rtype: int >>> ssdb.hdecr('hash_2', 'key1', 7) 35 >>> ssdb.hdecr('hash_2', 'key2', 3) 0 >>> ssdb.hdecr('hash_2', 'key_not_exists', 101) -101 >>> ssdb.hdecr('hash_not_exists', 'key_not_exists', 8848) -8848
f13250:c0:m32
def hsize(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Return the number of elements in hash ``name`` Like **Redis.HLEN** :param string name: the hash name :return: the size of hash `name`` :rtype: int >>> ssdb.hsize('hash_1') 7 >>> ssdb.hsize('hash_2') 6 >>> ssdb.hsize('hash_not_exists') 0
f13250:c0:m33
def multi_hset(self, name, **kvs):
return self.execute_command('<STR_LIT>', name, *dict_to_list(kvs))<EOL>
Set key to value within hash ``name`` for each corresponding key and value from the ``kvs`` dict. Like **Redis.HMSET** :param string name: the hash name :param list keys: a list of keys :return: the number of successful creation :rtype: int >>> ssdb.multi_hset('hash_4', a='AA', b='BB', c='CC', d='DD') 4 >>> ssdb.multi_hset('hash_4', a='AA', b='BB', c='CC', d='DD') 0 >>> ssdb.multi_hset('hash_4', a='AA', b='BB', c='CC', d='DD', e='EE') 1
f13250:c0:m34
def multi_hget(self, name, *keys):
return self.execute_command('<STR_LIT>', name, *keys)<EOL>
Return a dictionary mapping key/value by ``keys`` from hash ``names`` Like **Redis.HMGET** :param string name: the hash name :param list keys: a list of keys :return: a dict mapping key/value :rtype: dict >>> ssdb.multi_hget('hash_1', 'a', 'b', 'c', 'd') {'a': 'A', 'c': 'C', 'b': 'B', 'd': 'D'} >>> ssdb.multi_hget('hash_2', 'key2', 'key5') {'key2': '3.1415926', 'key5': 'e'}
f13250:c0:m35
def multi_hdel(self, name, *keys):
return self.execute_command('<STR_LIT>', name, *keys)<EOL>
Remove ``keys`` from hash ``name`` Like **Redis.HMDEL** :param string name: the hash name :param list keys: a list of keys :return: the number of successful deletion :rtype: int >>> ssdb.multi_hdel('hash_1', 'a', 'b', 'c', 'd') 4 >>> ssdb.multi_hdel('hash_1', 'a', 'b', 'c', 'd') 0 >>> ssdb.multi_hdel('hash_2', 'key2_not_exist', 'key5_not_exist') 0
f13250:c0:m36
def hkeys(self, name, key_start, key_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit)<EOL>return self.execute_command('<STR_LIT>', name, key_start, key_end, limit)<EOL>
Return a list of the top ``limit`` keys between ``key_start`` and ``key_end`` in hash ``name`` Similiar with **Redis.HKEYS** .. note:: The range is (``key_start``, ``key_end``]. The ``key_start`` isn't in the range, but ``key_end`` is. :param string name: the hash name :param string key_start: The lower bound(not included) of keys to be returned, empty string ``''`` means -inf :param string key_end: The upper bound(included) of keys to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a list of keys :rtype: list >>> ssdb.hkeys('hash_1', 'a', 'g', 10) ['b', 'c', 'd', 'e', 'f', 'g'] >>> ssdb.hkeys('hash_2', 'key ', 'key4', 3) ['key1', 'key2', 'key3'] >>> ssdb.hkeys('hash_1', 'f', '', 10) ['g'] >>> ssdb.hkeys('hash_2', 'keys', '', 10) []
f13250:c0:m37
def hgetall(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Return a Python dict of the hash's name/value pairs Like **Redis.HGETALL** :param string name: the hash name :return: a dict mapping key/value :rtype: dict >>> ssdb.hgetall('hash_1') {"a":'aa',"b":'bb',"c":'cc'}
f13250:c0:m38
def hlist(self, name_start, name_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit)<EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Return a list of the top ``limit`` hash's name between ``name_start`` and ``name_end`` in ascending order .. note:: The range is (``name_start``, ``name_end``]. The ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The lower bound(not included) of hash names to be returned, empty string ``''`` means -inf :param string name_end: The upper bound(included) of hash names to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a list of hash's name :rtype: list >>> ssdb.hlist('hash_ ', 'hash_z', 10) ['hash_1', 'hash_2'] >>> ssdb.hlist('hash_ ', '', 3) ['hash_1', 'hash_2'] >>> ssdb.hlist('', 'aaa_not_exist', 10) []
f13250:c0:m39
def hrlist(self, name_start, name_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit)<EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Return a list of the top ``limit`` hash's name between ``name_start`` and ``name_end`` in descending order .. note:: The range is (``name_start``, ``name_end``]. The ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The lower bound(not included) of hash names to be returned, empty string ``''`` means +inf :param string name_end: The upper bound(included) of hash names to be returned, empty string ``''`` means -inf :param int limit: number of elements will be returned. :return: a list of hash's name :rtype: list >>> ssdb.hrlist('hash_ ', 'hash_z', 10) ['hash_2', 'hash_1'] >>> ssdb.hrlist('hash_ ', '', 3) ['hash_2', 'hash_1'] >>> ssdb.hrlist('', 'aaa_not_exist', 10) []
f13250:c0:m40
def hscan(self, name, key_start, key_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name, key_start, key_end, limit)<EOL>
Return a dict mapping key/value in the top ``limit`` keys between ``key_start`` and ``key_end`` within hash ``name`` in ascending order Similiar with **Redis.HSCAN** .. note:: The range is (``key_start``, ``key_end``]. The ``key_start`` isn't in the range, but ``key_end`` is. :param string name: the hash name :param string key_start: The lower bound(not included) of keys to be returned, empty string ``''`` means -inf :param string key_end: The upper bound(included) of keys to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a dict mapping key/value in ascending order :rtype: OrderedDict >>> ssdb.hscan('hash_1', 'a', 'g', 10) {'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G'} >>> ssdb.hscan('hash_2', 'key ', 'key4', 3) {'key1': '42', 'key2': '3.1415926', 'key3': '-1.41421'} >>> ssdb.hscan('hash_1', 'f', '', 10) {'g': 'G'} >>> ssdb.hscan('hash_2', 'keys', '', 10) {}
f13250:c0:m41
def hrscan(self, name, key_start, key_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name, key_start, key_end, limit)<EOL>
Return a dict mapping key/value in the top ``limit`` keys between ``key_start`` and ``key_end`` within hash ``name`` in descending order .. note:: The range is (``key_start``, ``key_end``]. The ``key_start`` isn't in the range, but ``key_end`` is. :param string name: the hash name :param string key_start: The upper bound(not included) of keys to be returned, empty string ``''`` means +inf :param string key_end: The lower bound(included) of keys to be returned, empty string ``''`` means -inf :param int limit: number of elements will be returned. :return: a dict mapping key/value in descending order :rtype: OrderedDict >>> ssdb.hrscan('hash_1', 'g', 'a', 10) {'f': 'F', 'e': 'E', 'd': 'D', 'c': 'C', 'b': 'B', 'a': 'A'} >>> ssdb.hrscan('hash_2', 'key7', 'key1', 3) {'key6': 'log', 'key5': 'e', 'key4': '256'} >>> ssdb.hrscan('hash_1', 'c', '', 10) {'b': 'B', 'a': 'A'} >>> ssdb.hscan('hash_2', 'keys', '', 10) {}
f13250:c0:m42
def zset(self, name, key, score=<NUM_LIT:1>):
score = get_integer('<STR_LIT>', score) <EOL>return self.execute_command('<STR_LIT>', name, key, score)<EOL>
Set the score of ``key`` from the zset ``name`` to ``score`` Like **Redis.ZADD** :param string name: the zset name :param string key: the key name :param int score: the score for ranking :return: ``True`` if ``zset`` created a new score, otherwise ``False`` :rtype: bool >>> ssdb.zset("zset_1", 'z', 1024) True >>> ssdb.zset("zset_1", 'a', 1024) False >>> ssdb.zset("zset_2", 'key_10', -4) >>> >>> ssdb.zget("zset_2", 'key1') 42
f13250:c0:m43
def zget(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Return the score of element ``key`` in sorted set ``name`` Like **Redis.ZSCORE** :param string name: the zset name :param string key: the key name :return: the score, ``None`` if the zset ``name`` or the ``key`` doesn't exist :rtype: int >>> ssdb.zget("zset_1", 'a') 30 >>> ssdb.zget("zset_1", 'b') 20 >>> ssdb.zget("zset_1", 'z') >>> >>> ssdb.zget("zset_2", 'key1') 42
f13250:c0:m44
def zdel(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Remove the specified ``key`` from zset ``name`` Like **Redis.ZREM** :param string name: the zset name :param string key: the key name :return: ``True`` if deleted success, otherwise ``False`` :rtype: bool >>> ssdb.zdel("zset_2", 'key1') True >>> ssdb.zdel("zset_2", 'key2') True >>> ssdb.zdel("zset_2", 'key3') True >>> ssdb.zdel("zset_2", 'key_not_exist') False >>> ssdb.zdel("zset_not_exist", 'key1') False
f13250:c0:m45
def zclear(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
**Clear&Delete** the zset specified by ``name`` :param string name: the zset name :return: the length of removed elements :rtype: int >>> ssdb.zclear('zset_1') 7 >>> ssdb.zclear('zset_1') 0 >>> ssdb.zclear('zset_2') 6 >>> ssdb.zclear('not_exist') 0
f13250:c0:m46
def zexists(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Return a boolean indicating whether ``key`` exists within zset ``name`` :param string name: the zset name :param string key: the key name :return: ``True`` if the key exists, ``False`` if not :rtype: bool >>> ssdb.zexists('zset_1', 'a') True >>> ssdb.zexists('zset_2', 'key2') True >>> ssdb.zexists('zset_not_exist', 'a') False >>> ssdb.zexists('zset_1', 'z_not_exists') False >>> ssdb.zexists('zset_not_exist', 'key_not_exists') False
f13250:c0:m47
def zincr(self, name, key, amount=<NUM_LIT:1>):
amount = get_integer('<STR_LIT>', amount) <EOL>return self.execute_command('<STR_LIT>', name, key, amount)<EOL>
Increase the score of ``key`` in zset ``name`` by ``amount``. If no key exists, the value will be initialized as ``amount`` Like **Redis.ZINCR** :param string name: the zset name :param string key: the key name :param int amount: increments :return: the integer value of ``key`` in zset ``name`` :rtype: int >>> ssdb.zincr('zset_2', 'key1', 7) 49 >>> ssdb.zincr('zset_2', 'key2', 3) 317 >>> ssdb.zincr('zset_2', 'key_not_exists', 101) 101 >>> ssdb.zincr('zset_not_exists', 'key_not_exists', 8848) 8848
f13250:c0:m48
def zdecr(self, name, key, amount=<NUM_LIT:1>):
amount = get_positive_integer('<STR_LIT>', amount)<EOL>return self.execute_command('<STR_LIT>', name, key, amount)<EOL>
Decrease the value of ``key`` in zset ``name`` by ``amount``. If no key exists, the value will be initialized as 0 - ``amount`` :param string name: the zset name :param string key: the key name :param int amount: increments :return: the integer value of ``key`` in zset ``name`` :rtype: int >>> ssdb.zdecr('zset_2', 'key1', 7) 36 >>> ssdb.zdecr('zset_2', 'key2', 3) 311 >>> ssdb.zdecr('zset_2', 'key_not_exists', 101) -101 >>> ssdb.zdecr('zset_not_exists', 'key_not_exists', 8848) -8848
f13250:c0:m49
def zsize(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Return the number of elements in zset ``name`` Like **Redis.ZCARD** :param string name: the zset name :return: the size of zset `name`` :rtype: int >>> ssdb.zsize('zset_1') 7 >>> ssdb.zsize('zset_2') 6 >>> ssdb.zsize('zset_not_exists') 0
f13250:c0:m50
def multi_zset(self, name, **kvs):
for k,v in kvs.items():<EOL><INDENT>kvs[k] = get_integer(k, int(v))<EOL><DEDENT>return self.execute_command('<STR_LIT>', name, *dict_to_list(kvs))<EOL>
Return a dictionary mapping key/value by ``keys`` from zset ``names`` :param string name: the zset name :param list keys: a list of keys :return: the number of successful creation :rtype: int >>> ssdb.multi_zset('zset_4', a=100, b=80, c=90, d=70) 4 >>> ssdb.multi_zset('zset_4', a=100, b=80, c=90, d=70) 0 >>> ssdb.multi_zset('zset_4', a=100, b=80, c=90, d=70, e=60) 1
f13250:c0:m51
def multi_zget(self, name, *keys):
return self.execute_command('<STR_LIT>', name, *keys)<EOL>
Return a dictionary mapping key/value by ``keys`` from zset ``names`` :param string name: the zset name :param list keys: a list of keys :return: a dict mapping key/value :rtype: dict >>> ssdb.multi_zget('zset_1', 'a', 'b', 'c', 'd') {'a': 30, 'c': 100, 'b': 20, 'd': 1} >>> ssdb.multi_zget('zset_2', 'key2', 'key5') {'key2': 314, 'key5': 0}
f13250:c0:m52
def multi_zdel(self, name, *keys):
return self.execute_command('<STR_LIT>', name, *keys)<EOL>
Remove ``keys`` from zset ``name`` :param string name: the zset name :param list keys: a list of keys :return: the number of successful deletion :rtype: int >>> ssdb.multi_zdel('zset_1', 'a', 'b', 'c', 'd') 4 >>> ssdb.multi_zdel('zset_1', 'a', 'b', 'c', 'd') 0 >>> ssdb.multi_zdel('zset_2', 'key2_not_exist', 'key5_not_exist') 0
f13250:c0:m53
def zlist(self, name_start, name_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit)<EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Return a list of the top ``limit`` zset's name between ``name_start`` and ``name_end`` in ascending order .. note:: The range is (``name_start``, ``name_end``]. The ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The lower bound(not included) of zset names to be returned, empty string ``''`` means -inf :param string name_end: The upper bound(included) of zset names to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a list of zset's name :rtype: list >>> ssdb.zlist('zset_ ', 'zset_z', 10) ['zset_1', 'zset_2'] >>> ssdb.zlist('zset_ ', '', 3) ['zset_1', 'zset_2'] >>> ssdb.zlist('', 'aaa_not_exist', 10) []
f13250:c0:m54
def zrlist(self, name_start, name_end, limit=<NUM_LIT:10>):
limit = get_positive_integer('<STR_LIT>', limit)<EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Return a list of the top ``limit`` zset's name between ``name_start`` and ``name_end`` in descending order .. note:: The range is (``name_start``, ``name_end``]. The ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The lower bound(not included) of zset names to be returned, empty string ``''`` means +inf :param string name_end: The upper bound(included) of zset names to be returned, empty string ``''`` means -inf :param int limit: number of elements will be returned. :return: a list of zset's name :rtype: list >>> ssdb.zlist('zset_ ', 'zset_z', 10) ['zset_2', 'zset_1'] >>> ssdb.zlist('zset_ ', '', 3) ['zset_2', 'zset_1'] >>> ssdb.zlist('', 'aaa_not_exist', 10) []
f13250:c0:m55
def zkeys(self, name, key_start, score_start, score_end, limit=<NUM_LIT:10>):
score_start = get_integer_or_emptystring('<STR_LIT>', score_start)<EOL>score_end = get_integer_or_emptystring('<STR_LIT>', score_end) <EOL>limit = get_positive_integer('<STR_LIT>', limit)<EOL>return self.execute_command('<STR_LIT>', name, key_start, score_start,<EOL>score_end, limit)<EOL>
Return a list of the top ``limit`` keys after ``key_start`` from zset ``name`` with scores between ``score_start`` and ``score_end`` .. note:: The range is (``key_start``+``score_start``, ``key_end``]. That means (key.score == score_start && key > key_start || key.score > score_start) :param string name: the zset name :param string key_start: The lower bound(not included) of keys to be returned, empty string ``''`` means -inf :param string key_end: The upper bound(included) of keys to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a list of keys :rtype: list >>> ssdb.zkeys('zset_1', '', 0, 200, 10) ['g', 'd', 'b', 'a', 'e', 'c'] >>> ssdb.zkeys('zset_1', '', 0, 200, 3) ['g', 'd', 'b'] >>> ssdb.zkeys('zset_1', 'b', 20, 200, 3) ['a', 'e', 'c'] >>> ssdb.zkeys('zset_1', 'c', 100, 200, 3) []
f13250:c0:m56
def zscan(self, name, key_start, score_start, score_end, limit=<NUM_LIT:10>):
score_start = get_integer_or_emptystring('<STR_LIT>', score_start)<EOL>score_end = get_integer_or_emptystring('<STR_LIT>', score_end)<EOL>limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name, key_start, score_start,<EOL>score_end, limit)<EOL>
Return a dict mapping key/score of the top ``limit`` keys after ``key_start`` with scores between ``score_start`` and ``score_end`` in zset ``name`` in ascending order Similiar with **Redis.ZSCAN** .. note:: The range is (``key_start``+``score_start``, ``key_end``]. That means (key.score == score_start && key > key_start || key.score > score_start) :param string name: the zset name :param string key_start: The key related to score_start, could be empty string ``''`` :param int score_start: The minimum score related to keys(may not be included, depend on key_start), empty string ``''`` means -inf :param int score_end: The maximum score(included) related to keys, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a dict mapping key/score in ascending order :rtype: OrderedDict >>> ssdb.zscan('zset_1', '', 0, 200, 10) {'g': 0, 'd': 1, 'b': 20, 'a': 30, 'e': 64, 'c': 100} >>> ssdb.zscan('zset_1', '', 0, 200, 3) {'g': 0, 'd': 1, 'b': 20} >>> ssdb.zscan('zset_1', 'b', 20, 200, 3) {'a': 30, 'e': 64, 'c': 100} >>> ssdb.zscan('zset_1', 'c', 100, 200, 3) {}
f13250:c0:m57
def zrscan(self, name, key_start, score_start, score_end, limit=<NUM_LIT:10>):
score_start = get_integer_or_emptystring('<STR_LIT>', score_start)<EOL>score_end = get_integer_or_emptystring('<STR_LIT>', score_end) <EOL>limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name, key_start, score_start,<EOL>score_end, limit)<EOL>
Return a dict mapping key/score of the top ``limit`` keys after ``key_start`` with scores between ``score_start`` and ``score_end`` in zset ``name`` in descending order .. note:: The range is (``key_start``+``score_start``, ``key_end``]. That means (key.score == score_start && key < key_start || key.score < score_start) :param string name: the zset name :param string key_start: The key related to score_start, could be empty string ``''`` :param int score_start: The maximum score related to keys(may not be included, depend on key_start), empty string ``''`` means +inf :param int score_end: The minimum score(included) related to keys, empty string ``''`` means -inf :param int limit: number of elements will be returned. :return: a dict mapping key/score in descending order :rtype: OrderedDict >>> ssdb.zrscan('zset_1', '', '', '', 10) {'c': 100, 'e': 64, 'a': 30, 'b': 20, 'd': 1, 'g': 0, 'f': -3} >>> ssdb.zrscan('zset_1', '', 1000, -1000, 3) {'c': 100, 'e': 64, 'a': 30} >>> ssdb.zrscan('zset_1', 'a', 30, -1000, 3) {'b': 20, 'd': 1, 'g': 0} >>> ssdb.zrscan('zset_1', 'g', 0, -1000, 3) {'g': 0}
f13250:c0:m58
def zrank(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Returns a 0-based value indicating the rank of ``key`` in zset ``name`` Like **Redis.ZRANK** .. warning:: This method may be extremly SLOW! May not be used in an online service. :param string name: the zset name :param string key: the key name :return: the rank of ``key`` in zset ``name``, **-1** if the ``key`` or the ``name`` doesn't exists :rtype: int >>> ssdb.zrank('zset_1','d') 2 >>> ssdb.zrank('zset_1','f') 0 >>> ssdb.zrank('zset_1','x') -1
f13250:c0:m59
def zrrank(self, name, key):
return self.execute_command('<STR_LIT>', name, key)<EOL>
Returns a 0-based value indicating the descending rank of ``key`` in zset .. warning:: This method may be extremly SLOW! May not be used in an online service. :param string name: the zset name :param string key: the key name :return: the reverse rank of ``key`` in zset ``name``, **-1** if the ``key`` or the ``name`` doesn't exists :rtype: int >>> ssdb.zrrank('zset_1','d') 4 >>> ssdb.zrrank('zset_1','f') 6 >>> ssdb.zrrank('zset_1','x') -1
f13250:c0:m60
def zrange(self, name, offset, limit):
offset = get_nonnegative_integer('<STR_LIT>', offset) <EOL>limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name, offset, limit)<EOL>
Return a dict mapping key/score in a range of score from zset ``name`` between ``offset`` and ``offset+limit`` sorted in ascending order. Like **Redis.ZRANGE** .. warning:: This method is SLOW for large offset! :param string name: the zset name :param int offset: zero or positive,the returned pairs will start at this offset :param int limit: number of elements will be returned :return: a dict mapping key/score in ascending order :rtype: OrderedDict >>> ssdb.zrange('zset_1', 2, 3) {'d': 1, 'b': 20, 'a': 30} >>> ssdb.zrange('zset_1', 0, 2) {'f': -3, 'g': 0} >>> ssdb.zrange('zset_1', 10, 10) {}
f13250:c0:m61
def zrrange(self, name, offset, limit):
offset = get_nonnegative_integer('<STR_LIT>', offset) <EOL>limit = get_positive_integer('<STR_LIT>', limit) <EOL>return self.execute_command('<STR_LIT>', name, offset, limit)<EOL>
Return a dict mapping key/score in a range of score from zset ``name`` between ``offset`` and ``offset+limit`` sorted in descending order. .. warning:: This method is SLOW for large offset! :param string name: the zset name :param int offset: zero or positive,the returned pairs will start at this offset :param int limit: number of elements will be returned :return: a dict mapping key/score in ascending order :rtype: OrderedDict >>> ssdb.zrrange('zset_1', 0, 4) {'c': 100, 'e': 64, 'a': 30, 'b': 20} >>> ssdb.zrrange('zset_1', 4, 5) {'d': 1, 'g': 0, 'f': -3} >>> ssdb.zrrange('zset_1', 10, 10) {}
f13250:c0:m62
def zcount(self, name, score_start, score_end):
score_start = get_integer_or_emptystring('<STR_LIT>', score_start)<EOL>score_end = get_integer_or_emptystring('<STR_LIT>', score_end)<EOL>return self.execute_command('<STR_LIT>', name, score_start, score_end)<EOL>
Returns the number of elements in the sorted set at key ``name`` with a score between ``score_start`` and ``score_end``. Like **Redis.ZCOUNT** .. note:: The range is [``score_start``, ``score_end``] :param string name: the zset name :param int score_start: The minimum score related to keys(included), empty string ``''`` means -inf :param int score_end: The maximum score(included) related to keys, empty string ``''`` means +inf :return: the number of keys in specified range :rtype: int >>> ssdb.zount('zset_1', 20, 70) 3 >>> ssdb.zcount('zset_1', 0, 100) 6 >>> ssdb.zcount('zset_1', 2, 3) 0
f13250:c0:m63
def zsum(self, name, score_start, score_end):
score_start = get_integer_or_emptystring('<STR_LIT>', score_start)<EOL>score_end = get_integer_or_emptystring('<STR_LIT>', score_end)<EOL>return self.execute_command('<STR_LIT>', name, score_start, score_end)<EOL>
Returns the sum of elements of the sorted set stored at the specified key which have scores in the range [score_start,score_end]. .. note:: The range is [``score_start``, ``score_end``] :param string name: the zset name :param int score_start: The minimum score related to keys(included), empty string ``''`` means -inf :param int score_end: The maximum score(included) related to keys, empty string ``''`` means +inf :return: the sum of keys in specified range :rtype: int >>> ssdb.zsum('zset_1', 20, 70) 114 >>> ssdb.zsum('zset_1', 0, 100) 215 >>> ssdb.zsum('zset_1', 2, 3) 0
f13250:c0:m64
def zavg(self, name, score_start, score_end):
score_start = get_integer_or_emptystring('<STR_LIT>', score_start)<EOL>score_end = get_integer_or_emptystring('<STR_LIT>', score_end)<EOL>return self.execute_command('<STR_LIT>', name, score_start, score_end)<EOL>
Returns the average of elements of the sorted set stored at the specified key which have scores in the range [score_start,score_end]. .. note:: The range is [``score_start``, ``score_end``] :param string name: the zset name :param int score_start: The minimum score related to keys(included), empty string ``''`` means -inf :param int score_end: The maximum score(included) related to keys, empty string ``''`` means +inf :return: the average of keys in specified range :rtype: int >>> ssdb.zavg('zset_1', 20, 70) 38 >>> ssdb.zavg('zset_1', 0, 100) 35 >>> ssdb.zavg('zset_1', 2, 3) 0
f13250:c0:m65
def zremrangebyrank(self, name, rank_start, rank_end):
rank_start = get_nonnegative_integer('<STR_LIT>', rank_start)<EOL>rank_end = get_nonnegative_integer('<STR_LIT>', rank_end)<EOL>return self.execute_command('<STR_LIT>', name, rank_start,<EOL>rank_end)<EOL>
Remove the elements of the zset which have rank in the range [rank_start,rank_end]. .. note:: The range is [``rank_start``, ``rank_end``] :param string name: the zset name :param int rank_start: zero or positive,the start position :param int rank_end: zero or positive,the end position :return: the number of deleted elements :rtype: int >>> ssdb.zremrangebyrank('zset_1', 0, 2) 3 >>> ssdb.zremrangebyrank('zset_1', 1, 4) 5 >>> ssdb.zremrangebyrank('zset_1', 0, 0) 1
f13250:c0:m66
def zremrangebyscore(self, name, score_start, score_end):
score_start = get_integer_or_emptystring('<STR_LIT>', score_start)<EOL>score_end = get_integer_or_emptystring('<STR_LIT>', score_end)<EOL>return self.execute_command('<STR_LIT>', name, score_start,<EOL>score_end)<EOL>
Delete the elements of the zset which have rank in the range [score_start,score_end]. .. note:: The range is [``score_start``, ``score_end``] :param string name: the zset name :param int score_start: The minimum score related to keys(included), empty string ``''`` means -inf :param int score_end: The maximum score(included) related to keys, empty string ``''`` means +inf :return: the number of deleted elements :rtype: int >>> ssdb.zremrangebyscore('zset_1', 20, 70) 3 >>> ssdb.zremrangebyscore('zset_1', 0, 100) 6 >>> ssdb.zremrangebyscore('zset_1', 2, 3) 0
f13250:c0:m67
def qget(self, name, index):
index = get_integer('<STR_LIT:index>', index)<EOL>return self.execute_command('<STR_LIT>', name, index)<EOL>
Get the element of ``index`` within the queue ``name`` :param string name: the queue name :param int index: the specified index, can < 0 :return: the value at ``index`` within queue ``name`` , or ``None`` if the element doesn't exist :rtype: string
f13250:c0:m68
def qset(self, name, index, value):
index = get_integer('<STR_LIT:index>', index) <EOL>return self.execute_command('<STR_LIT>', name, index, value)<EOL>
Set the list element at ``index`` to ``value``. :param string name: the queue name :param int index: the specified index, can < 0 :param string value: the element value :return: Unknown :rtype: True
f13250:c0:m69
def qpush_back(self, name, *items):
return self.execute_command('<STR_LIT>', name, *items)<EOL>
Push ``items`` onto the tail of the list ``name`` Like **Redis.RPUSH** :param string name: the queue name :param list items: the list of items :return: length of queue :rtype: int
f13250:c0:m70
def qpush_front(self, name, *items):
return self.execute_command('<STR_LIT>', name, *items)<EOL>
Push ``items`` onto the head of the list ``name`` Like **Redis.LPUSH** :param string name: the queue name :param int index: the specified index :param string value: the element value :return: length of queue :rtype: int
f13250:c0:m71
def qpop_front(self, name, size=<NUM_LIT:1>):
size = get_positive_integer("<STR_LIT:size>", size)<EOL>return self.execute_command('<STR_LIT>', name, size)<EOL>
Remove and return the first ``size`` item of the list ``name`` Like **Redis.LPOP** :param string name: the queue name :param int size: the length of result :return: the list of pop elements :rtype: list
f13250:c0:m72
def qpop_back(self, name, size=<NUM_LIT:1>):
size = get_positive_integer("<STR_LIT:size>", size)<EOL>return self.execute_command('<STR_LIT>', name, size)<EOL>
Remove and return the last ``size`` item of the list ``name`` Like **Redis.RPOP** :param string name: the queue name :param int size: the length of result :return: the list of pop elements :rtype: list
f13250:c0:m73
def qsize(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
Return the length of the list ``name`` . If name does not exist, it is interpreted as an empty list and 0 is returned. Like **Redis.LLEN** :param string name: the queue name :return: the queue length or 0 if the queue doesn't exist. :rtype: int >>> ssdb.qsize('queue_1') 7 >>> ssdb.qsize('queue_2') 6 >>> ssdb.qsize('queue_not_exists') 0
f13250:c0:m74
def qclear(self, name):
return self.execute_command('<STR_LIT>', name)<EOL>
**Clear&Delete** the queue specified by ``name`` :param string name: the queue name :return: the length of removed elements :rtype: int
f13250:c0:m75
def qlist(self, name_start, name_end, limit):
limit = get_positive_integer("<STR_LIT>", limit)<EOL>return self.execute_command('<STR_LIT>', name_start, name_end, limit)<EOL>
Return a list of the top ``limit`` keys between ``name_start`` and ``name_end`` in ascending order .. note:: The range is (``name_start``, ``name_end``]. ``name_start`` isn't in the range, but ``name_end`` is. :param string name_start: The lower bound(not included) of keys to be returned, empty string ``''`` means -inf :param string name_end: The upper bound(included) of keys to be returned, empty string ``''`` means +inf :param int limit: number of elements will be returned. :return: a list of keys :rtype: list >>> ssdb.qlist('queue_1', 'queue_2', 10) ['queue_2'] >>> ssdb.qlist('queue_', 'queue_2', 10) ['queue_1', 'queue_2'] >>> ssdb.qlist('z', '', 10) []
f13250:c0:m76