<s> from __future__ import with_statement <EOL> from itertools import chain <EOL> import datetime <EOL> import sys <EOL> import warnings <EOL> import time <EOL> import threading <EOL> import time as mod_time <EOL> from redis . _compat import ( b , basestring , bytes , imap , iteritems , iterkeys , <EOL> itervalues , izip , long , nativestr , unicode , <EOL> safe_unicode ) <EOL> from redis . connection import ( ConnectionPool , UnixDomainSocketConnection , <EOL> SSLConnection , Token ) <EOL> from redis . lock import Lock , LuaLock <EOL> from redis . exceptions import ( <EOL> ConnectionError , <EOL> DataError , <EOL> ExecAbortError , <EOL> NoScriptError , <EOL> PubSubError , <EOL> RedisError , <EOL> ResponseError , <EOL> TimeoutError , <EOL> WatchError , <EOL> ) <EOL> SYM_EMPTY = b ( '<STR_LIT>' ) <EOL> def list_or_args ( keys , args ) : <EOL> try : <EOL> iter ( keys ) <EOL> if isinstance ( keys , ( basestring , bytes ) ) : <EOL> keys = [ keys ] <EOL> except TypeError : <EOL> keys = [ keys ] <EOL> if args : <EOL> keys . extend ( args ) <EOL> return keys <EOL> def timestamp_to_datetime ( response ) : <EOL> "<STR_LIT>" <EOL> if not response : <EOL> return None <EOL> try : <EOL> response = int ( response ) <EOL> except ValueError : <EOL> return None <EOL> return datetime . datetime . fromtimestamp ( response ) <EOL> def string_keys_to_dict ( key_string , callback ) : <EOL> return dict . fromkeys ( key_string . split ( ) , callback ) <EOL> def dict_merge ( * dicts ) : <EOL> merged = { } <EOL> for d in dicts : <EOL> merged . update ( d ) <EOL> return merged <EOL> def parse_debug_object ( response ) : <EOL> "<STR_LIT>" <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> if field in response : <EOL> response [ field ] = int ( response [ field ] ) <EOL> return response <EOL> def parse_object ( response , infotype ) : <EOL> "<STR_LIT>" <EOL> if infotype in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return int_or_none ( response ) <EOL> return response <EOL> def parse_info ( response ) : <EOL> "<STR_LIT>" <EOL> info = { } <EOL> response = nativestr ( response ) <EOL> def get_value ( value ) : <EOL> if '<STR_LIT:U+002C>' not in value or '<STR_LIT:=>' not in value : <EOL> try : <EOL> if '<STR_LIT:.>' in value : <EOL> return float ( value ) <EOL> else : <EOL> return int ( value ) <EOL> except ValueError : <EOL> return value <EOL> else : <EOL> sub_dict = { } <EOL> for item in value . split ( '<STR_LIT:U+002C>' ) : <EOL> k , v = item . rsplit ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> sub_dict [ k ] = get_value ( v ) <EOL> return sub_dict <EOL> for line in response . splitlines ( ) : <EOL> if line and not line . startswith ( '<STR_LIT:#>' ) : <EOL> if line . find ( '<STR_LIT::>' ) != - <NUM_LIT:1> : <EOL> key , value = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> info [ key ] = get_value ( value ) <EOL> else : <EOL> info . setdefault ( '<STR_LIT>' , [ ] ) . append ( line ) <EOL> return info <EOL> SENTINEL_STATE_TYPES = { <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT:port>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : int <EOL> } <EOL> def parse_sentinel_state ( item ) : <EOL> result = pairs_to_dict_typed ( item , SENTINEL_STATE_TYPES ) <EOL> flags = set ( result [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) ) <EOL> for name , flag in ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> result [ name ] = flag in flags <EOL> return result <EOL> def parse_sentinel_master ( response ) : <EOL> return parse_sentinel_state ( imap ( nativestr , response ) ) <EOL> def parse_sentinel_masters ( response ) : <EOL> result = { } <EOL> for item in response : <EOL> state = parse_sentinel_state ( imap ( nativestr , item ) ) <EOL> result [ state [ '<STR_LIT:name>' ] ] = state <EOL> return result <EOL> def parse_sentinel_slaves_and_sentinels ( response ) : <EOL> return [ parse_sentinel_state ( imap ( nativestr , item ) ) for item in response ] <EOL> def parse_sentinel_get_master ( response ) : <EOL> return response and ( response [ <NUM_LIT:0> ] , int ( response [ <NUM_LIT:1> ] ) ) or None <EOL> def pairs_to_dict ( response ) : <EOL> "<STR_LIT>" <EOL> it = iter ( response ) <EOL> return dict ( izip ( it , it ) ) <EOL> def pairs_to_dict_typed ( response , type_info ) : <EOL> it = iter ( response ) <EOL> result = { } <EOL> for key , value in izip ( it , it ) : <EOL> if key in type_info : <EOL> try : <EOL> value = type_info [ key ] ( value ) <EOL> except : <EOL> pass <EOL> result [ key ] = value <EOL> return result <EOL> def zset_score_pairs ( response , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not response or not options [ '<STR_LIT>' ] : <EOL> return response <EOL> score_cast_func = options . get ( '<STR_LIT>' , float ) <EOL> it = iter ( response ) <EOL> return list ( izip ( it , imap ( score_cast_func , it ) ) ) <EOL> def sort_return_tuples ( response , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not response or not options [ '<STR_LIT>' ] : <EOL> return response <EOL> n = options [ '<STR_LIT>' ] <EOL> return list ( izip ( * [ response [ i : : n ] for i in range ( n ) ] ) ) <EOL> def int_or_none ( response ) : <EOL> if response is None : <EOL> return None <EOL> return int ( response ) <EOL> def float_or_none ( response ) : <EOL> if response is None : <EOL> return None <EOL> return float ( response ) <EOL> def bool_ok ( response ) : <EOL> return nativestr ( response ) == '<STR_LIT:OK>' <EOL> def parse_client_list ( response , ** options ) : <EOL> clients = [ ] <EOL> for c in nativestr ( response ) . splitlines ( ) : <EOL> clients . append ( dict ( [ pair . split ( '<STR_LIT:=>' ) for pair in c . split ( '<STR_LIT:U+0020>' ) ] ) ) <EOL> return clients <EOL> def parse_config_get ( response , ** options ) : <EOL> response = [ nativestr ( i ) if i is not None else None for i in response ] <EOL> return response and pairs_to_dict ( response ) or { } <EOL> def parse_scan ( response , ** options ) : <EOL> cursor , r = response <EOL> return long ( cursor ) , r <EOL> def parse_hscan ( response , ** options ) : <EOL> cursor , r = response <EOL> return long ( cursor ) , r and pairs_to_dict ( r ) or { } <EOL> def parse_zscan ( response , ** options ) : <EOL> score_cast_func = options . get ( '<STR_LIT>' , float ) <EOL> cursor , r = response <EOL> it = iter ( r ) <EOL> return long ( cursor ) , list ( izip ( it , imap ( score_cast_func , it ) ) ) <EOL> def parse_slowlog_get ( response , ** options ) : <EOL> return [ { <EOL> '<STR_LIT:id>' : item [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : int ( item [ <NUM_LIT:1> ] ) , <EOL> '<STR_LIT>' : int ( item [ <NUM_LIT:2> ] ) , <EOL> '<STR_LIT>' : b ( '<STR_LIT:U+0020>' ) . join ( item [ <NUM_LIT:3> ] ) <EOL> } for item in response ] <EOL> class StrictRedis ( object ) : <EOL> """<STR_LIT>""" <EOL> RESPONSE_CALLBACKS = dict_merge ( <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> bool <EOL> ) , <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> int <EOL> ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , float ) , <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' , <EOL> lambda r : isinstance ( r , long ) and r or nativestr ( r ) == '<STR_LIT:OK>' <EOL> ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , sort_return_tuples ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , float_or_none ) , <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> bool_ok <EOL> ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , lambda r : r and tuple ( r ) or None ) , <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' , <EOL> lambda r : r and set ( r ) or set ( ) <EOL> ) , <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' , <EOL> zset_score_pairs <EOL> ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , int_or_none ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , lambda r : True ) , <EOL> { <EOL> '<STR_LIT>' : lambda r : r and nativestr ( r ) , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : parse_client_list , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : parse_config_get , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : parse_debug_object , <EOL> '<STR_LIT>' : lambda r : r and pairs_to_dict ( r ) or { } , <EOL> '<STR_LIT>' : parse_hscan , <EOL> '<STR_LIT>' : parse_info , <EOL> '<STR_LIT>' : timestamp_to_datetime , <EOL> '<STR_LIT>' : parse_object , <EOL> '<STR_LIT>' : lambda r : nativestr ( r ) == '<STR_LIT>' , <EOL> '<STR_LIT>' : lambda r : r and r or None , <EOL> '<STR_LIT>' : parse_scan , <EOL> '<STR_LIT>' : lambda r : list ( imap ( bool , r ) ) , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : nativestr , <EOL> '<STR_LIT>' : parse_sentinel_get_master , <EOL> '<STR_LIT>' : parse_sentinel_master , <EOL> '<STR_LIT>' : parse_sentinel_masters , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : parse_sentinel_slaves_and_sentinels , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : parse_sentinel_slaves_and_sentinels , <EOL> '<STR_LIT>' : lambda r : r and nativestr ( r ) == '<STR_LIT:OK>' , <EOL> '<STR_LIT>' : parse_slowlog_get , <EOL> '<STR_LIT>' : int , <EOL> '<STR_LIT>' : bool_ok , <EOL> '<STR_LIT>' : parse_scan , <EOL> '<STR_LIT>' : lambda x : ( int ( x [ <NUM_LIT:0> ] ) , int ( x [ <NUM_LIT:1> ] ) ) , <EOL> '<STR_LIT>' : parse_zscan <EOL> } <EOL> ) <EOL> @ classmethod <EOL> def from_url ( cls , url , db = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> connection_pool = ConnectionPool . from_url ( url , db = db , ** kwargs ) <EOL> return cls ( connection_pool = connection_pool ) <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , <EOL> db = <NUM_LIT:0> , password = None , socket_timeout = None , <EOL> socket_connect_timeout = None , <EOL> socket_keepalive = None , socket_keepalive_options = None , <EOL> connection_pool = None , unix_socket_path = None , <EOL> encoding = '<STR_LIT:utf-8>' , encoding_errors = '<STR_LIT:strict>' , <EOL> charset = None , errors = None , <EOL> decode_responses = False , retry_on_timeout = False , <EOL> ssl = False , ssl_keyfile = None , ssl_certfile = None , <EOL> ssl_cert_reqs = None , ssl_ca_certs = None , <EOL> max_connections = None ) : <EOL> if not connection_pool : <EOL> if charset is not None : <EOL> warnings . warn ( DeprecationWarning ( <EOL> '<STR_LIT>' ) ) <EOL> encoding = charset <EOL> if errors is not None : <EOL> warnings . warn ( DeprecationWarning ( <EOL> '<STR_LIT>' ) ) <EOL> encoding_errors = errors <EOL> kwargs = { <EOL> '<STR_LIT>' : db , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT>' : socket_timeout , <EOL> '<STR_LIT>' : encoding , <EOL> '<STR_LIT>' : encoding_errors , <EOL> '<STR_LIT>' : decode_responses , <EOL> '<STR_LIT>' : retry_on_timeout , <EOL> '<STR_LIT>' : max_connections <EOL> } <EOL> if unix_socket_path is not None : <EOL> kwargs . update ( { <EOL> '<STR_LIT:path>' : unix_socket_path , <EOL> '<STR_LIT>' : UnixDomainSocketConnection <EOL> } ) <EOL> else : <EOL> kwargs . update ( { <EOL> '<STR_LIT:host>' : host , <EOL> '<STR_LIT:port>' : port , <EOL> '<STR_LIT>' : socket_connect_timeout , <EOL> '<STR_LIT>' : socket_keepalive , <EOL> '<STR_LIT>' : socket_keepalive_options , <EOL> } ) <EOL> if ssl : <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : SSLConnection , <EOL> '<STR_LIT>' : ssl_keyfile , <EOL> '<STR_LIT>' : ssl_certfile , <EOL> '<STR_LIT>' : ssl_cert_reqs , <EOL> '<STR_LIT>' : ssl_ca_certs , <EOL> } ) <EOL> connection_pool = ConnectionPool ( ** kwargs ) <EOL> self . connection_pool = connection_pool <EOL> self . _use_lua_lock = None <EOL> self . response_callbacks = self . __class__ . RESPONSE_CALLBACKS . copy ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( type ( self ) . __name__ , repr ( self . connection_pool ) ) <EOL> def set_response_callback ( self , command , callback ) : <EOL> "<STR_LIT>" <EOL> self . response_callbacks [ command ] = callback <EOL> def pipeline ( self , transaction = True , shard_hint = None ) : <EOL> """<STR_LIT>""" <EOL> return StrictPipeline ( <EOL> self . connection_pool , <EOL> self . response_callbacks , <EOL> transaction , <EOL> shard_hint ) <EOL> def transaction ( self , func , * watches , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> shard_hint = kwargs . pop ( '<STR_LIT>' , None ) <EOL> value_from_callable = kwargs . pop ( '<STR_LIT>' , False ) <EOL> watch_delay = kwargs . pop ( '<STR_LIT>' , None ) <EOL> with self . pipeline ( True , shard_hint ) as pipe : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> if watches : <EOL> pipe . watch ( * watches ) <EOL> func_value = func ( pipe ) <EOL> exec_value = pipe . execute ( ) <EOL> return func_value if value_from_callable else exec_value <EOL> except WatchError : <EOL> if watch_delay is not None and watch_delay > <NUM_LIT:0> : <EOL> time . sleep ( watch_delay ) <EOL> continue <EOL> def lock ( self , name , timeout = None , sleep = <NUM_LIT:0.1> , blocking_timeout = None , <EOL> lock_class = None , thread_local = True ) : <EOL> """<STR_LIT>""" <EOL> if lock_class is None : <EOL> if self . _use_lua_lock is None : <EOL> try : <EOL> LuaLock . register_scripts ( self ) <EOL> self . _use_lua_lock = True <EOL> except ResponseError : <EOL> self . _use_lua_lock = False <EOL> lock_class = self . _use_lua_lock and LuaLock or Lock <EOL> return lock_class ( self , name , timeout = timeout , sleep = sleep , <EOL> blocking_timeout = blocking_timeout , <EOL> thread_local = thread_local ) <EOL> def pubsub ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return PubSub ( self . connection_pool , ** kwargs ) <EOL> def execute_command ( self , * args , ** options ) : <EOL> "<STR_LIT>" <EOL> pool = self . connection_pool <EOL> command_name = args [ <NUM_LIT:0> ] <EOL> connection = pool . get_connection ( command_name , ** options ) <EOL> try : <EOL> connection . send_command ( * args ) <EOL> return self . parse_response ( connection , command_name , ** options ) <EOL> except ( ConnectionError , TimeoutError ) as e : <EOL> connection . disconnect ( ) <EOL> if not connection . retry_on_timeout and isinstance ( e , TimeoutError ) : <EOL> raise <EOL> connection . send_command ( * args ) <EOL> return self . parse_response ( connection , command_name , ** options ) <EOL> finally : <EOL> pool . release ( connection ) <EOL> def parse_response ( self , connection , command_name , ** options ) : <EOL> "<STR_LIT>" <EOL> response = connection . read_response ( ) <EOL> if command_name in self . response_callbacks : <EOL> return self . response_callbacks [ command_name ] ( response , ** options ) <EOL> return response <EOL> def bgrewriteaof ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def bgsave ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def client_kill ( self , address ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , address ) <EOL> def client_list ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def client_getname ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def client_setname ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def config_get ( self , pattern = "<STR_LIT:*>" ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , pattern ) <EOL> def config_set ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def config_resetstat ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def config_rewrite ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def dbsize ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def debug_object ( self , key ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , key ) <EOL> def echo ( self , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , value ) <EOL> def flushall ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def flushdb ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def info ( self , section = None ) : <EOL> """<STR_LIT>""" <EOL> if section is None : <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> else : <EOL> return self . execute_command ( '<STR_LIT>' , section ) <EOL> def lastsave ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def object ( self , infotype , key ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , infotype , key , infotype = infotype ) <EOL> def ping ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def sentinel ( self , * args ) : <EOL> "<STR_LIT>" <EOL> warnings . warn ( <EOL> DeprecationWarning ( '<STR_LIT>' ) ) <EOL> def sentinel_get_master_addr_by_name ( self , service_name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , <EOL> service_name ) <EOL> def sentinel_master ( self , service_name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , service_name ) <EOL> def sentinel_masters ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def sentinel_monitor ( self , name , ip , port , quorum ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , ip , port , quorum ) <EOL> def sentinel_remove ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def sentinel_sentinels ( self , service_name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , service_name ) <EOL> def sentinel_set ( self , name , option , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , option , value ) <EOL> def sentinel_slaves ( self , service_name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , service_name ) <EOL> def shutdown ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> self . execute_command ( '<STR_LIT>' ) <EOL> except ConnectionError : <EOL> return <EOL> raise RedisError ( "<STR_LIT>" ) <EOL> def slaveof ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> if host is None and port is None : <EOL> return self . execute_command ( '<STR_LIT>' , Token ( '<STR_LIT>' ) , Token ( '<STR_LIT>' ) ) <EOL> return self . execute_command ( '<STR_LIT>' , host , port ) <EOL> def slowlog_get ( self , num = None ) : <EOL> """<STR_LIT>""" <EOL> args = [ '<STR_LIT>' ] <EOL> if num is not None : <EOL> args . append ( num ) <EOL> return self . execute_command ( * args ) <EOL> def slowlog_len ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def slowlog_reset ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def time ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def wait ( self , num_replicas , timeout ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , num_replicas , timeout ) <EOL> def append ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , key , value ) <EOL> def bitcount ( self , key , start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> params = [ key ] <EOL> if start is not None and end is not None : <EOL> params . append ( start ) <EOL> params . append ( end ) <EOL> elif ( start is not None and end is None ) or ( end is not None and start is None ) : <EOL> raise RedisError ( "<STR_LIT>" ) <EOL> return self . execute_command ( '<STR_LIT>' , * params ) <EOL> def bitop ( self , operation , dest , * keys ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , operation , dest , * keys ) <EOL> def bitpos ( self , key , bit , start = None , end = None ) : <EOL> """<STR_LIT>""" <EOL> if bit not in ( <NUM_LIT:0> , <NUM_LIT:1> ) : <EOL> raise RedisError ( '<STR_LIT>' ) <EOL> params = [ key , bit ] <EOL> start is not None and params . append ( start ) <EOL> if start is not None and end is not None : <EOL> params . append ( end ) <EOL> elif start is None and end is not None : <EOL> raise RedisError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return self . execute_command ( '<STR_LIT>' , * params ) <EOL> def decr ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , amount ) <EOL> def delete ( self , * names ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , * names ) <EOL> def __delitem__ ( self , name ) : <EOL> self . delete ( name ) <EOL> def dump ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def exists ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> __contains__ = exists <EOL> def expire ( self , name , time ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( time , datetime . timedelta ) : <EOL> time = time . seconds + time . days * <NUM_LIT> * <NUM_LIT> <EOL> return self . execute_command ( '<STR_LIT>' , name , time ) <EOL> def expireat ( self , name , when ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( when , datetime . datetime ) : <EOL> when = int ( mod_time . mktime ( when . timetuple ( ) ) ) <EOL> return self . execute_command ( '<STR_LIT>' , name , when ) <EOL> def get ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT:GET>' , name ) <EOL> def __getitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> value = self . get ( name ) <EOL> if value : <EOL> return value <EOL> raise KeyError ( name ) <EOL> def getbit ( self , name , offset ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , offset ) <EOL> def getrange ( self , key , start , end ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , key , start , end ) <EOL> def getset ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def incr ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , amount ) <EOL> def incrby ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . incr ( name , amount ) <EOL> def incrbyfloat ( self , name , amount = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , amount ) <EOL> def keys ( self , pattern = '<STR_LIT:*>' ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , pattern ) <EOL> def mget ( self , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , * args ) <EOL> def mset ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> if len ( args ) != <NUM_LIT:1> or not isinstance ( args [ <NUM_LIT:0> ] , dict ) : <EOL> raise RedisError ( '<STR_LIT>' ) <EOL> kwargs . update ( args [ <NUM_LIT:0> ] ) <EOL> items = [ ] <EOL> for pair in iteritems ( kwargs ) : <EOL> items . extend ( pair ) <EOL> return self . execute_command ( '<STR_LIT>' , * items ) <EOL> def msetnx ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> if len ( args ) != <NUM_LIT:1> or not isinstance ( args [ <NUM_LIT:0> ] , dict ) : <EOL> raise RedisError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> kwargs . update ( args [ <NUM_LIT:0> ] ) <EOL> items = [ ] <EOL> for pair in iteritems ( kwargs ) : <EOL> items . extend ( pair ) <EOL> return self . execute_command ( '<STR_LIT>' , * items ) <EOL> def move ( self , name , db ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , db ) <EOL> def persist ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def pexpire ( self , name , time ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( time , datetime . timedelta ) : <EOL> ms = int ( time . microseconds / <NUM_LIT:1000> ) <EOL> time = ( time . seconds + time . days * <NUM_LIT> * <NUM_LIT> ) * <NUM_LIT:1000> + ms <EOL> return self . execute_command ( '<STR_LIT>' , name , time ) <EOL> def pexpireat ( self , name , when ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( when , datetime . datetime ) : <EOL> ms = int ( when . microsecond / <NUM_LIT:1000> ) <EOL> when = int ( mod_time . mktime ( when . timetuple ( ) ) ) * <NUM_LIT:1000> + ms <EOL> return self . execute_command ( '<STR_LIT>' , name , when ) <EOL> def psetex ( self , name , time_ms , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( time_ms , datetime . timedelta ) : <EOL> ms = int ( time_ms . microseconds / <NUM_LIT:1000> ) <EOL> time_ms = ( time_ms . seconds + time_ms . days * <NUM_LIT> * <NUM_LIT> ) * <NUM_LIT:1000> + ms <EOL> return self . execute_command ( '<STR_LIT>' , name , time_ms , value ) <EOL> def pttl ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def randomkey ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def rename ( self , src , dst ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , src , dst ) <EOL> def renamenx ( self , src , dst ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , src , dst ) <EOL> def restore ( self , name , ttl , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , ttl , value ) <EOL> def set ( self , name , value , ex = None , px = None , nx = False , xx = False ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ name , value ] <EOL> if ex : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> if isinstance ( ex , datetime . timedelta ) : <EOL> ex = ex . seconds + ex . days * <NUM_LIT> * <NUM_LIT> <EOL> pieces . append ( ex ) <EOL> if px : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> if isinstance ( px , datetime . timedelta ) : <EOL> ms = int ( px . microseconds / <NUM_LIT:1000> ) <EOL> px = ( px . seconds + px . days * <NUM_LIT> * <NUM_LIT> ) * <NUM_LIT:1000> + ms <EOL> pieces . append ( px ) <EOL> if nx : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> if xx : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> return self . execute_command ( '<STR_LIT>' , * pieces ) <EOL> def __setitem__ ( self , name , value ) : <EOL> self . set ( name , value ) <EOL> def setbit ( self , name , offset , value ) : <EOL> """<STR_LIT>""" <EOL> value = value and <NUM_LIT:1> or <NUM_LIT:0> <EOL> return self . execute_command ( '<STR_LIT>' , name , offset , value ) <EOL> def setex ( self , name , time , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( time , datetime . timedelta ) : <EOL> time = time . seconds + time . days * <NUM_LIT> * <NUM_LIT> <EOL> return self . execute_command ( '<STR_LIT>' , name , time , value ) <EOL> def setnx ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def setrange ( self , name , offset , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , offset , value ) <EOL> def strlen ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def substr ( self , name , start , end = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , start , end ) <EOL> def ttl ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def type ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def watch ( self , * names ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( DeprecationWarning ( '<STR_LIT>' ) ) <EOL> def unwatch ( self ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( <EOL> DeprecationWarning ( '<STR_LIT>' ) ) <EOL> def blpop ( self , keys , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if timeout is None : <EOL> timeout = <NUM_LIT:0> <EOL> if isinstance ( keys , basestring ) : <EOL> keys = [ keys ] <EOL> else : <EOL> keys = list ( keys ) <EOL> keys . append ( timeout ) <EOL> return self . execute_command ( '<STR_LIT>' , * keys ) <EOL> def brpop ( self , keys , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if timeout is None : <EOL> timeout = <NUM_LIT:0> <EOL> if isinstance ( keys , basestring ) : <EOL> keys = [ keys ] <EOL> else : <EOL> keys = list ( keys ) <EOL> keys . append ( timeout ) <EOL> return self . execute_command ( '<STR_LIT>' , * keys ) <EOL> def brpoplpush ( self , src , dst , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if timeout is None : <EOL> timeout = <NUM_LIT:0> <EOL> return self . execute_command ( '<STR_LIT>' , src , dst , timeout ) <EOL> def lindex ( self , name , index ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , index ) <EOL> def linsert ( self , name , where , refvalue , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , where , refvalue , value ) <EOL> def llen ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def lpop ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def lpush ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , * values ) <EOL> def lpushx ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def lrange ( self , name , start , end ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , start , end ) <EOL> def lrem ( self , name , count , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , count , value ) <EOL> def lset ( self , name , index , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , index , value ) <EOL> def ltrim ( self , name , start , end ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , start , end ) <EOL> def rpop ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def rpoplpush ( self , src , dst ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , src , dst ) <EOL> def rpush ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , * values ) <EOL> def rpushx ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def sort ( self , name , start = None , num = None , by = None , get = None , <EOL> desc = False , alpha = False , store = None , groups = False ) : <EOL> """<STR_LIT>""" <EOL> if ( start is not None and num is None ) or ( num is not None and start is None ) : <EOL> raise RedisError ( "<STR_LIT>" ) <EOL> pieces = [ name ] <EOL> if by is not None : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> pieces . append ( by ) <EOL> if start is not None and num is not None : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> pieces . append ( start ) <EOL> pieces . append ( num ) <EOL> if get is not None : <EOL> if isinstance ( get , basestring ) : <EOL> pieces . append ( Token ( '<STR_LIT:GET>' ) ) <EOL> pieces . append ( get ) <EOL> else : <EOL> for g in get : <EOL> pieces . append ( Token ( '<STR_LIT:GET>' ) ) <EOL> pieces . append ( g ) <EOL> if desc : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> if alpha : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> if store is not None : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> pieces . append ( store ) <EOL> if groups : <EOL> if not get or isinstance ( get , basestring ) or len ( get ) < <NUM_LIT:2> : <EOL> raise DataError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> options = { '<STR_LIT>' : len ( get ) if groups else None } <EOL> return self . execute_command ( '<STR_LIT>' , * pieces , ** options ) <EOL> def scan ( self , cursor = <NUM_LIT:0> , match = None , count = None ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ cursor ] <EOL> if match is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , match ] ) <EOL> if count is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , count ] ) <EOL> return self . execute_command ( '<STR_LIT>' , * pieces ) <EOL> def scan_iter ( self , match = None , count = None ) : <EOL> """<STR_LIT>""" <EOL> cursor = '<STR_LIT:0>' <EOL> while cursor != <NUM_LIT:0> : <EOL> cursor , data = self . scan ( cursor = cursor , match = match , count = count ) <EOL> for item in data : <EOL> yield item <EOL> def sscan ( self , name , cursor = <NUM_LIT:0> , match = None , count = None ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ name , cursor ] <EOL> if match is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , match ] ) <EOL> if count is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , count ] ) <EOL> return self . execute_command ( '<STR_LIT>' , * pieces ) <EOL> def sscan_iter ( self , name , match = None , count = None ) : <EOL> """<STR_LIT>""" <EOL> cursor = '<STR_LIT:0>' <EOL> while cursor != <NUM_LIT:0> : <EOL> cursor , data = self . sscan ( name , cursor = cursor , <EOL> match = match , count = count ) <EOL> for item in data : <EOL> yield item <EOL> def hscan ( self , name , cursor = <NUM_LIT:0> , match = None , count = None ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ name , cursor ] <EOL> if match is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , match ] ) <EOL> if count is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , count ] ) <EOL> return self . execute_command ( '<STR_LIT>' , * pieces ) <EOL> def hscan_iter ( self , name , match = None , count = None ) : <EOL> """<STR_LIT>""" <EOL> cursor = '<STR_LIT:0>' <EOL> while cursor != <NUM_LIT:0> : <EOL> cursor , data = self . hscan ( name , cursor = cursor , <EOL> match = match , count = count ) <EOL> for item in data . items ( ) : <EOL> yield item <EOL> def zscan ( self , name , cursor = <NUM_LIT:0> , match = None , count = None , <EOL> score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ name , cursor ] <EOL> if match is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , match ] ) <EOL> if count is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , count ] ) <EOL> options = { '<STR_LIT>' : score_cast_func } <EOL> return self . execute_command ( '<STR_LIT>' , * pieces , ** options ) <EOL> def zscan_iter ( self , name , match = None , count = None , <EOL> score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> cursor = '<STR_LIT:0>' <EOL> while cursor != <NUM_LIT:0> : <EOL> cursor , data = self . zscan ( name , cursor = cursor , match = match , <EOL> count = count , <EOL> score_cast_func = score_cast_func ) <EOL> for item in data : <EOL> yield item <EOL> def sadd ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , * values ) <EOL> def scard ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def sdiff ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , * args ) <EOL> def sdiffstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , dest , * args ) <EOL> def sinter ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , * args ) <EOL> def sinterstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , dest , * args ) <EOL> def sismember ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def smembers ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def smove ( self , src , dst , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , src , dst , value ) <EOL> def spop ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def srandmember ( self , name , number = None ) : <EOL> """<STR_LIT>""" <EOL> args = number and [ number ] or [ ] <EOL> return self . execute_command ( '<STR_LIT>' , name , * args ) <EOL> def srem ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , * values ) <EOL> def sunion ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , * args ) <EOL> def sunionstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , dest , * args ) <EOL> def zadd ( self , name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ ] <EOL> if args : <EOL> if len ( args ) % <NUM_LIT:2> != <NUM_LIT:0> : <EOL> raise RedisError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> pieces . extend ( args ) <EOL> for pair in iteritems ( kwargs ) : <EOL> pieces . append ( pair [ <NUM_LIT:1> ] ) <EOL> pieces . append ( pair [ <NUM_LIT:0> ] ) <EOL> return self . execute_command ( '<STR_LIT>' , name , * pieces ) <EOL> def zcard ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def zcount ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , min , max ) <EOL> def zincrby ( self , name , value , amount = <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , amount , value ) <EOL> def zinterstore ( self , dest , keys , aggregate = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _zaggregate ( '<STR_LIT>' , dest , keys , aggregate ) <EOL> def zlexcount ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , min , max ) <EOL> def zrange ( self , name , start , end , desc = False , withscores = False , <EOL> score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> if desc : <EOL> return self . zrevrange ( name , start , end , withscores , <EOL> score_cast_func ) <EOL> pieces = [ '<STR_LIT>' , name , start , end ] <EOL> if withscores : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> options = { <EOL> '<STR_LIT>' : withscores , <EOL> '<STR_LIT>' : score_cast_func <EOL> } <EOL> return self . execute_command ( * pieces , ** options ) <EOL> def zrangebylex ( self , name , min , max , start = None , num = None ) : <EOL> """<STR_LIT>""" <EOL> if ( start is not None and num is None ) or ( num is not None and start is None ) : <EOL> raise RedisError ( "<STR_LIT>" ) <EOL> pieces = [ '<STR_LIT>' , name , min , max ] <EOL> if start is not None and num is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , start , num ] ) <EOL> return self . execute_command ( * pieces ) <EOL> def zrevrangebylex ( self , name , max , min , start = None , num = None ) : <EOL> """<STR_LIT>""" <EOL> if ( start is not None and num is None ) or ( num is not None and start is None ) : <EOL> raise RedisError ( "<STR_LIT>" ) <EOL> pieces = [ '<STR_LIT>' , name , max , min ] <EOL> if start is not None and num is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , start , num ] ) <EOL> return self . execute_command ( * pieces ) <EOL> def zrangebyscore ( self , name , min , max , start = None , num = None , <EOL> withscores = False , score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> if ( start is not None and num is None ) or ( num is not None and start is None ) : <EOL> raise RedisError ( "<STR_LIT>" ) <EOL> pieces = [ '<STR_LIT>' , name , min , max ] <EOL> if start is not None and num is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , start , num ] ) <EOL> if withscores : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> options = { <EOL> '<STR_LIT>' : withscores , <EOL> '<STR_LIT>' : score_cast_func <EOL> } <EOL> return self . execute_command ( * pieces , ** options ) <EOL> def zrank ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def zrem ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , * values ) <EOL> def zremrangebylex ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , min , max ) <EOL> def zremrangebyrank ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , min , max ) <EOL> def zremrangebyscore ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , min , max ) <EOL> def zrevrange ( self , name , start , end , withscores = False , <EOL> score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ '<STR_LIT>' , name , start , end ] <EOL> if withscores : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> options = { <EOL> '<STR_LIT>' : withscores , <EOL> '<STR_LIT>' : score_cast_func <EOL> } <EOL> return self . execute_command ( * pieces , ** options ) <EOL> def zrevrangebyscore ( self , name , max , min , start = None , num = None , <EOL> withscores = False , score_cast_func = float ) : <EOL> """<STR_LIT>""" <EOL> if ( start is not None and num is None ) or ( num is not None and start is None ) : <EOL> raise RedisError ( "<STR_LIT>" ) <EOL> pieces = [ '<STR_LIT>' , name , max , min ] <EOL> if start is not None and num is not None : <EOL> pieces . extend ( [ Token ( '<STR_LIT>' ) , start , num ] ) <EOL> if withscores : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> options = { <EOL> '<STR_LIT>' : withscores , <EOL> '<STR_LIT>' : score_cast_func <EOL> } <EOL> return self . execute_command ( * pieces , ** options ) <EOL> def zrevrank ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def zscore ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , value ) <EOL> def zunionstore ( self , dest , keys , aggregate = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _zaggregate ( '<STR_LIT>' , dest , keys , aggregate ) <EOL> def _zaggregate ( self , command , dest , keys , aggregate = None ) : <EOL> pieces = [ command , dest , len ( keys ) ] <EOL> if isinstance ( keys , dict ) : <EOL> keys , weights = iterkeys ( keys ) , itervalues ( keys ) <EOL> else : <EOL> weights = None <EOL> pieces . extend ( keys ) <EOL> if weights : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> pieces . extend ( weights ) <EOL> if aggregate : <EOL> pieces . append ( Token ( '<STR_LIT>' ) ) <EOL> pieces . append ( aggregate ) <EOL> return self . execute_command ( * pieces ) <EOL> def pfadd ( self , name , * values ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , * values ) <EOL> def pfcount ( self , * sources ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , * sources ) <EOL> def pfmerge ( self , dest , * sources ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , dest , * sources ) <EOL> def hdel ( self , name , * keys ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , * keys ) <EOL> def hexists ( self , name , key ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , key ) <EOL> def hget ( self , name , key ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , key ) <EOL> def hgetall ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def hincrby ( self , name , key , amount = <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name , key , amount ) <EOL> def hincrbyfloat ( self , name , key , amount = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , key , amount ) <EOL> def hkeys ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def hlen ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def hset ( self , name , key , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , key , value ) <EOL> def hsetnx ( self , name , key , value ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , key , value ) <EOL> def hmset ( self , name , mapping ) : <EOL> """<STR_LIT>""" <EOL> if not mapping : <EOL> raise DataError ( "<STR_LIT>" ) <EOL> items = [ ] <EOL> for pair in iteritems ( mapping ) : <EOL> items . extend ( pair ) <EOL> return self . execute_command ( '<STR_LIT>' , name , * items ) <EOL> def hmget ( self , name , keys , * args ) : <EOL> "<STR_LIT>" <EOL> args = list_or_args ( keys , args ) <EOL> return self . execute_command ( '<STR_LIT>' , name , * args ) <EOL> def hvals ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , name ) <EOL> def publish ( self , channel , message ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , channel , message ) <EOL> def eval ( self , script , numkeys , * keys_and_args ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , script , numkeys , * keys_and_args ) <EOL> def evalsha ( self , sha , numkeys , * keys_and_args ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , sha , numkeys , * keys_and_args ) <EOL> def script_exists ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , * args ) <EOL> def script_flush ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def script_kill ( self ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' ) <EOL> def script_load ( self , script ) : <EOL> "<STR_LIT>" <EOL> return self . execute_command ( '<STR_LIT>' , script ) <EOL> def register_script ( self , script ) : <EOL> """<STR_LIT>""" <EOL> return Script ( self , script ) <EOL> class Redis ( StrictRedis ) : <EOL> """<STR_LIT>""" <EOL> RESPONSE_CALLBACKS = dict_merge ( <EOL> StrictRedis . RESPONSE_CALLBACKS , <EOL> { <EOL> '<STR_LIT>' : lambda r : r >= <NUM_LIT:0> and r or None , <EOL> '<STR_LIT>' : lambda r : r >= <NUM_LIT:0> and r or None , <EOL> } <EOL> ) <EOL> def pipeline ( self , transaction = True , shard_hint = None ) : <EOL> """<STR_LIT>""" <EOL> return Pipeline ( <EOL> self . connection_pool , <EOL> self . response_callbacks , <EOL> transaction , <EOL> shard_hint ) <EOL> def setex ( self , name , value , time ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( time , datetime . timedelta ) : <EOL> time = time . seconds + time . days * <NUM_LIT> * <NUM_LIT> <EOL> return self . execute_command ( '<STR_LIT>' , name , time , value ) <EOL> def lrem ( self , name , value , num = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . execute_command ( '<STR_LIT>' , name , num , value ) <EOL> def zadd ( self , name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ ] <EOL> if args : <EOL> if len ( args ) % <NUM_LIT:2> != <NUM_LIT:0> : <EOL> raise RedisError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> pieces . extend ( reversed ( args ) ) <EOL> for pair in iteritems ( kwargs ) : <EOL> pieces . append ( pair [ <NUM_LIT:1> ] ) <EOL> pieces . append ( pair [ <NUM_LIT:0> ] ) <EOL> return self . execute_command ( '<STR_LIT>' , name , * pieces ) <EOL> class PubSub ( object ) : <EOL> """<STR_LIT>""" <EOL> PUBLISH_MESSAGE_TYPES = ( '<STR_LIT:message>' , '<STR_LIT>' ) <EOL> UNSUBSCRIBE_MESSAGE_TYPES = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , connection_pool , shard_hint = None , <EOL> ignore_subscribe_messages = False ) : <EOL> self . connection_pool = connection_pool <EOL> self . shard_hint = shard_hint <EOL> self . ignore_subscribe_messages = ignore_subscribe_messages <EOL> self . connection = None <EOL> conn = connection_pool . get_connection ( '<STR_LIT>' , shard_hint ) <EOL> try : <EOL> self . encoding = conn . encoding <EOL> self . encoding_errors = conn . encoding_errors <EOL> self . decode_responses = conn . decode_responses <EOL> finally : <EOL> connection_pool . release ( conn ) <EOL> self . reset ( ) <EOL> def __del__ ( self ) : <EOL> try : <EOL> self . reset ( ) <EOL> except Exception : <EOL> pass <EOL> def reset ( self ) : <EOL> if self . connection : <EOL> self . connection . disconnect ( ) <EOL> self . connection . clear_connect_callbacks ( ) <EOL> self . connection_pool . release ( self . connection ) <EOL> self . connection = None <EOL> self . channels = { } <EOL> self . patterns = { } <EOL> def close ( self ) : <EOL> self . reset ( ) <EOL> def on_connect ( self , connection ) : <EOL> "<STR_LIT>" <EOL> if self . channels : <EOL> channels = { } <EOL> for k , v in iteritems ( self . channels ) : <EOL> if not self . decode_responses : <EOL> k = k . decode ( self . encoding , self . encoding_errors ) <EOL> channels [ k ] = v <EOL> self . subscribe ( ** channels ) <EOL> if self . patterns : <EOL> patterns = { } <EOL> for k , v in iteritems ( self . patterns ) : <EOL> if not self . decode_responses : <EOL> k = k . decode ( self . encoding , self . encoding_errors ) <EOL> patterns [ k ] = v <EOL> self . psubscribe ( ** patterns ) <EOL> def encode ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if self . decode_responses and isinstance ( value , bytes ) : <EOL> value = value . decode ( self . encoding , self . encoding_errors ) <EOL> elif not self . decode_responses and isinstance ( value , unicode ) : <EOL> value = value . encode ( self . encoding , self . encoding_errors ) <EOL> return value <EOL> @ property <EOL> def subscribed ( self ) : <EOL> "<STR_LIT>" <EOL> return bool ( self . channels or self . patterns ) <EOL> def execute_command ( self , * args , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> if self . connection is None : <EOL> self . connection = self . connection_pool . get_connection ( <EOL> '<STR_LIT>' , <EOL> self . shard_hint <EOL> ) <EOL> self . connection . register_connect_callback ( self . on_connect ) <EOL> connection = self . connection <EOL> self . _execute ( connection , connection . send_command , * args ) <EOL> def _execute ( self , connection , command , * args ) : <EOL> try : <EOL> return command ( * args ) <EOL> except ( ConnectionError , TimeoutError ) as e : <EOL> connection . disconnect ( ) <EOL> if not connection . retry_on_timeout and isinstance ( e , TimeoutError ) : <EOL> raise <EOL> connection . connect ( ) <EOL> return command ( * args ) <EOL> def parse_response ( self , block = True , timeout = <NUM_LIT:0> ) : <EOL> "<STR_LIT>" <EOL> connection = self . connection <EOL> if not block and not connection . can_read ( timeout = timeout ) : <EOL> return None <EOL> return self . _execute ( connection , connection . read_response ) <EOL> def psubscribe ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> args = list_or_args ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] ) <EOL> new_patterns = { } <EOL> new_patterns . update ( dict . fromkeys ( imap ( self . encode , args ) ) ) <EOL> for pattern , handler in iteritems ( kwargs ) : <EOL> new_patterns [ self . encode ( pattern ) ] = handler <EOL> ret_val = self . execute_command ( '<STR_LIT>' , * iterkeys ( new_patterns ) ) <EOL> self . patterns . update ( new_patterns ) <EOL> return ret_val <EOL> def punsubscribe ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> args = list_or_args ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] ) <EOL> return self . execute_command ( '<STR_LIT>' , * args ) <EOL> def subscribe ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> args = list_or_args ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] ) <EOL> new_channels = { } <EOL> new_channels . update ( dict . fromkeys ( imap ( self . encode , args ) ) ) <EOL> for channel , handler in iteritems ( kwargs ) : <EOL> new_channels [ self . encode ( channel ) ] = handler <EOL> ret_val = self . execute_command ( '<STR_LIT>' , * iterkeys ( new_channels ) ) <EOL> self . channels . update ( new_channels ) <EOL> return ret_val <EOL> def unsubscribe ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> args = list_or_args ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] ) <EOL> return self . execute_command ( '<STR_LIT>' , * args ) <EOL> def listen ( self ) : <EOL> "<STR_LIT>" <EOL> while self . subscribed : <EOL> response = self . handle_message ( self . parse_response ( block = True ) ) <EOL> if response is not None : <EOL> yield response <EOL> def get_message ( self , ignore_subscribe_messages = False , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> response = self . parse_response ( block = False , timeout = timeout ) <EOL> if response : <EOL> return self . handle_message ( response , ignore_subscribe_messages ) <EOL> return None <EOL> def handle_message ( self , response , ignore_subscribe_messages = False ) : <EOL> """<STR_LIT>""" <EOL> message_type = nativestr ( response [ <NUM_LIT:0> ] ) <EOL> if message_type == '<STR_LIT>' : <EOL> message = { <EOL> '<STR_LIT:type>' : message_type , <EOL> '<STR_LIT>' : response [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : response [ <NUM_LIT:2> ] , <EOL> '<STR_LIT:data>' : response [ <NUM_LIT:3> ] <EOL> } <EOL> else : <EOL> message = { <EOL> '<STR_LIT:type>' : message_type , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : response [ <NUM_LIT:1> ] , <EOL> '<STR_LIT:data>' : response [ <NUM_LIT:2> ] <EOL> } <EOL> if message_type in self . UNSUBSCRIBE_MESSAGE_TYPES : <EOL> subscribed_dict = None <EOL> if message_type == '<STR_LIT>' : <EOL> subscribed_dict = self . patterns <EOL> else : <EOL> subscribed_dict = self . channels <EOL> try : <EOL> del subscribed_dict [ message [ '<STR_LIT>' ] ] <EOL> except KeyError : <EOL> pass <EOL> if message_type in self . PUBLISH_MESSAGE_TYPES : <EOL> handler = None <EOL> if message_type == '<STR_LIT>' : <EOL> handler = self . patterns . get ( message [ '<STR_LIT>' ] , None ) <EOL> else : <EOL> handler = self . channels . get ( message [ '<STR_LIT>' ] , None ) <EOL> if handler : <EOL> handler ( message ) <EOL> return None <EOL> else : <EOL> if ignore_subscribe_messages or self . ignore_subscribe_messages : <EOL> return None <EOL> return message <EOL> def run_in_thread ( self , sleep_time = <NUM_LIT:0> ) : <EOL> for channel , handler in iteritems ( self . channels ) : <EOL> if handler is None : <EOL> raise PubSubError ( "<STR_LIT>" ) <EOL> for pattern , handler in iteritems ( self . patterns ) : <EOL> if handler is None : <EOL> raise PubSubError ( "<STR_LIT>" ) <EOL> thread = PubSubWorkerThread ( self , sleep_time ) <EOL> thread . start ( ) <EOL> return thread <EOL> class PubSubWorkerThread ( threading . Thread ) : <EOL> def __init__ ( self , pubsub , sleep_time ) : <EOL> super ( PubSubWorkerThread , self ) . __init__ ( ) <EOL> self . pubsub = pubsub <EOL> self . sleep_time = sleep_time <EOL> self . _running = False <EOL> def run ( self ) : <EOL> if self . _running : <EOL> return <EOL> self . _running = True <EOL> pubsub = self . pubsub <EOL> sleep_time = self . sleep_time <EOL> while pubsub . subscribed : <EOL> pubsub . get_message ( ignore_subscribe_messages = True , <EOL> timeout = sleep_time ) <EOL> pubsub . close ( ) <EOL> self . _running = False <EOL> def stop ( self ) : <EOL> self . pubsub . unsubscribe ( ) <EOL> self . pubsub . punsubscribe ( ) <EOL> class BasePipeline ( object ) : <EOL> """<STR_LIT>""" <EOL> UNWATCH_COMMANDS = set ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def __init__ ( self , connection_pool , response_callbacks , transaction , <EOL> shard_hint ) : <EOL> self . connection_pool = connection_pool <EOL> self . connection = None <EOL> self . response_callbacks = response_callbacks <EOL> self . transaction = transaction <EOL> self . shard_hint = shard_hint <EOL> self . watching = False <EOL> self . reset ( ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . reset ( ) <EOL> def __del__ ( self ) : <EOL> try : <EOL> self . reset ( ) <EOL> except Exception : <EOL> pass <EOL> def __len__ ( self ) : <EOL> return len ( self . command_stack ) <EOL> def reset ( self ) : <EOL> self . command_stack = [ ] <EOL> self . scripts = set ( ) <EOL> if self . watching and self . connection : <EOL> try : <EOL> self . connection . send_command ( '<STR_LIT>' ) <EOL> self . connection . read_response ( ) <EOL> except ConnectionError : <EOL> self . connection . disconnect ( ) <EOL> self . watching = False <EOL> self . explicit_transaction = False <EOL> if self . connection : <EOL> self . connection_pool . release ( self . connection ) <EOL> self . connection = None <EOL> def multi ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . explicit_transaction : <EOL> raise RedisError ( '<STR_LIT>' ) <EOL> if self . command_stack : <EOL> raise RedisError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . explicit_transaction = True <EOL> def execute_command ( self , * args , ** kwargs ) : <EOL> if ( self . watching or args [ <NUM_LIT:0> ] == '<STR_LIT>' ) and not self . explicit_transaction : <EOL> return self . immediate_execute_command ( * args , ** kwargs ) <EOL> return self . pipeline_execute_command ( * args , ** kwargs ) <EOL> def immediate_execute_command ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> command_name = args [ <NUM_LIT:0> ] <EOL> conn = self . connection <EOL> if not conn : <EOL> conn = self . connection_pool . get_connection ( command_name , <EOL> self . shard_hint ) <EOL> self . connection = conn <EOL> try : <EOL> conn . send_command ( * args ) <EOL> return self . parse_response ( conn , command_name , ** options ) <EOL> except ( ConnectionError , TimeoutError ) as e : <EOL> conn . disconnect ( ) <EOL> if not conn . retry_on_timeout and isinstance ( e , TimeoutError ) : <EOL> raise <EOL> try : <EOL> if not self . watching : <EOL> conn . send_command ( * args ) <EOL> return self . parse_response ( conn , command_name , ** options ) <EOL> except ConnectionError : <EOL> conn . disconnect ( ) <EOL> self . reset ( ) <EOL> raise <EOL> def pipeline_execute_command ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> self . command_stack . append ( ( args , options ) ) <EOL> return self <EOL> def _execute_transaction ( self , connection , commands , raise_on_error ) : <EOL> cmds = chain ( [ ( ( '<STR_LIT>' , ) , { } ) ] , commands , [ ( ( '<STR_LIT>' , ) , { } ) ] ) <EOL> all_cmds = connection . pack_commands ( [ args for args , _ in cmds ] ) <EOL> connection . send_packed_command ( all_cmds ) <EOL> errors = [ ] <EOL> try : <EOL> self . parse_response ( connection , '<STR_LIT:_>' ) <EOL> except ResponseError : <EOL> errors . append ( ( <NUM_LIT:0> , sys . exc_info ( ) [ <NUM_LIT:1> ] ) ) <EOL> for i , command in enumerate ( commands ) : <EOL> try : <EOL> self . parse_response ( connection , '<STR_LIT:_>' ) <EOL> except ResponseError : <EOL> ex = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> self . annotate_exception ( ex , i + <NUM_LIT:1> , command [ <NUM_LIT:0> ] ) <EOL> errors . append ( ( i , ex ) ) <EOL> try : <EOL> response = self . parse_response ( connection , '<STR_LIT:_>' ) <EOL> except ExecAbortError : <EOL> if self . explicit_transaction : <EOL> self . immediate_execute_command ( '<STR_LIT>' ) <EOL> if errors : <EOL> raise errors [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> raise sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if response is None : <EOL> raise WatchError ( "<STR_LIT>" ) <EOL> for i , e in errors : <EOL> response . insert ( i , e ) <EOL> if len ( response ) != len ( commands ) : <EOL> self . connection . disconnect ( ) <EOL> raise ResponseError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if raise_on_error : <EOL> self . raise_first_error ( commands , response ) <EOL> data = [ ] <EOL> for r , cmd in izip ( response , commands ) : <EOL> if not isinstance ( r , Exception ) : <EOL> args , options = cmd <EOL> command_name = args [ <NUM_LIT:0> ] <EOL> if command_name in self . response_callbacks : <EOL> r = self . response_callbacks [ command_name ] ( r , ** options ) <EOL> data . append ( r ) <EOL> return data <EOL> def _execute_pipeline ( self , connection , commands , raise_on_error ) : <EOL> all_cmds = connection . pack_commands ( [ args for args , _ in commands ] ) <EOL> connection . send_packed_command ( all_cmds ) <EOL> response = [ ] <EOL> for args , options in commands : <EOL> try : <EOL> response . append ( <EOL> self . parse_response ( connection , args [ <NUM_LIT:0> ] , ** options ) ) <EOL> except ResponseError : <EOL> response . append ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> if raise_on_error : <EOL> self . raise_first_error ( commands , response ) <EOL> return response <EOL> def raise_first_error ( self , commands , response ) : <EOL> for i , r in enumerate ( response ) : <EOL> if isinstance ( r , ResponseError ) : <EOL> self . annotate_exception ( r , i + <NUM_LIT:1> , commands [ i ] [ <NUM_LIT:0> ] ) <EOL> raise r <EOL> def annotate_exception ( self , exception , number , command ) : <EOL> cmd = safe_unicode ( '<STR_LIT:U+0020>' ) . join ( imap ( safe_unicode , command ) ) <EOL> msg = unicode ( '<STR_LIT>' ) % ( <EOL> number , cmd , safe_unicode ( exception . args [ <NUM_LIT:0> ] ) ) <EOL> exception . args = ( msg , ) + exception . args [ <NUM_LIT:1> : ] <EOL> def parse_response ( self , connection , command_name , ** options ) : <EOL> result = StrictRedis . parse_response ( <EOL> self , connection , command_name , ** options ) <EOL> if command_name in self . UNWATCH_COMMANDS : <EOL> self . watching = False <EOL> elif command_name == '<STR_LIT>' : <EOL> self . watching = True <EOL> return result <EOL> def load_scripts ( self ) : <EOL> scripts = list ( self . scripts ) <EOL> immediate = self . immediate_execute_command <EOL> shas = [ s . sha for s in scripts ] <EOL> exists = immediate ( '<STR_LIT>' , '<STR_LIT>' , * shas , ** { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> if not all ( exists ) : <EOL> for s , exist in izip ( scripts , exists ) : <EOL> if not exist : <EOL> s . sha = immediate ( '<STR_LIT>' , '<STR_LIT>' , s . script , <EOL> ** { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def execute ( self , raise_on_error = True ) : <EOL> "<STR_LIT>" <EOL> stack = self . command_stack <EOL> if not stack : <EOL> return [ ] <EOL> if self . scripts : <EOL> self . load_scripts ( ) <EOL> if self . transaction or self . explicit_transaction : <EOL> execute = self . _execute_transaction <EOL> else : <EOL> execute = self . _execute_pipeline <EOL> conn = self . connection <EOL> if not conn : <EOL> conn = self . connection_pool . get_connection ( '<STR_LIT>' , <EOL> self . shard_hint ) <EOL> self . connection = conn <EOL> try : <EOL> return execute ( conn , stack , raise_on_error ) <EOL> except ( ConnectionError , TimeoutError ) as e : <EOL> conn . disconnect ( ) <EOL> if not conn . retry_on_timeout and isinstance ( e , TimeoutError ) : <EOL> raise <EOL> if self . watching : <EOL> raise WatchError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return execute ( conn , stack , raise_on_error ) <EOL> finally : <EOL> self . reset ( ) <EOL> def watch ( self , * names ) : <EOL> "<STR_LIT>" <EOL> if self . explicit_transaction : <EOL> raise RedisError ( '<STR_LIT>' ) <EOL> return self . execute_command ( '<STR_LIT>' , * names ) <EOL> def unwatch ( self ) : <EOL> "<STR_LIT>" <EOL> return self . watching and self . execute_command ( '<STR_LIT>' ) or True <EOL> def script_load_for_pipeline ( self , script ) : <EOL> "<STR_LIT>" <EOL> if not script . sha : <EOL> script . sha = self . immediate_execute_command ( '<STR_LIT>' , '<STR_LIT>' , <EOL> script . script , <EOL> ** { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . scripts . add ( script ) <EOL> class StrictPipeline ( BasePipeline , StrictRedis ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class Pipeline ( BasePipeline , Redis ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class Script ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , registered_client , script ) : <EOL> self . registered_client = registered_client <EOL> self . script = script <EOL> self . sha = '<STR_LIT>' <EOL> def __call__ ( self , keys = [ ] , args = [ ] , client = None ) : <EOL> "<STR_LIT>" <EOL> if client is None : <EOL> client = self . registered_client <EOL> args = tuple ( keys ) + tuple ( args ) <EOL> if isinstance ( client , BasePipeline ) : <EOL> client . script_load_for_pipeline ( self ) <EOL> try : <EOL> return client . evalsha ( self . sha , len ( keys ) , * args ) <EOL> except NoScriptError : <EOL> self . sha = client . script_load ( self . script ) <EOL> return client . evalsha ( self . sha , len ( keys ) , * args ) </s>
