<s> import datetime <EOL> import errno <EOL> import socket <EOL> import threading <EOL> import warnings <EOL> from redis . exceptions import ConnectionError , ResponseError , InvalidResponse <EOL> from redis . exceptions import RedisError , AuthenticationError <EOL> class ConnectionManager ( threading . local ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> self . connections = { } <EOL> def make_connection_key ( self , host , port , db ) : <EOL> "<STR_LIT>" <EOL> return '<STR_LIT>' % ( host , port , db ) <EOL> def get_connection ( self , host , port , db , password ) : <EOL> "<STR_LIT>" <EOL> key = self . make_connection_key ( host , port , db ) <EOL> if key not in self . connections : <EOL> self . connections [ key ] = Connection ( host , port , db , password ) <EOL> return self . connections [ key ] <EOL> def get_all_connections ( self ) : <EOL> "<STR_LIT>" <EOL> return self . connections . values ( ) <EOL> connection_manager = ConnectionManager ( ) <EOL> class Connection ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , db = <NUM_LIT:0> , password = None ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . db = db <EOL> self . password = password <EOL> self . _sock = None <EOL> self . _fp = None <EOL> def connect ( self , redis_instance ) : <EOL> "<STR_LIT>" <EOL> if self . _sock : <EOL> return <EOL> try : <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( ( self . host , self . port ) ) <EOL> except socket . error , e : <EOL> raise ConnectionError ( "<STR_LIT>" % ( e . args [ <NUM_LIT:0> ] , self . host , self . port , e . args [ <NUM_LIT:1> ] ) ) <EOL> sock . setsockopt ( socket . SOL_TCP , socket . TCP_NODELAY , <NUM_LIT:1> ) <EOL> self . _sock = sock <EOL> self . _fp = sock . makefile ( '<STR_LIT:r>' ) <EOL> redis_instance . _setup_connection ( ) <EOL> def disconnect ( self ) : <EOL> "<STR_LIT>" <EOL> if self . _sock is None : <EOL> return <EOL> try : <EOL> self . _sock . close ( ) <EOL> except socket . error : <EOL> pass <EOL> self . _sock = None <EOL> self . _fp = None <EOL> def send ( self , command , redis_instance ) : <EOL> "<STR_LIT>" <EOL> self . connect ( redis_instance ) <EOL> try : <EOL> self . _sock . sendall ( command ) <EOL> except socket . error , e : <EOL> if e . args [ <NUM_LIT:0> ] == errno . EPIPE : <EOL> self . disconnect ( ) <EOL> raise ConnectionError ( "<STR_LIT>" % e . args ) <EOL> def read ( self , length = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if length is not None : <EOL> return self . _fp . read ( length ) <EOL> return self . _fp . readline ( ) <EOL> except socket . error , e : <EOL> self . disconnect ( ) <EOL> if e . args and e . args [ <NUM_LIT:0> ] == errno . EAGAIN : <EOL> raise ConnectionError ( "<STR_LIT>" % e . args [ <NUM_LIT:1> ] ) <EOL> return '<STR_LIT>' <EOL> def list_or_args ( command , keys , args ) : <EOL> oldapi = bool ( args ) <EOL> try : <EOL> i = iter ( keys ) <EOL> if isinstance ( keys , basestring ) : <EOL> oldapi = True <EOL> except TypeError : <EOL> oldapi = True <EOL> keys = [ keys ] <EOL> if oldapi : <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % command <EOL> ) ) <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 ( [ ( key , callback ) for key in key_string . split ( ) ] ) <EOL> def dict_merge ( * dicts ) : <EOL> merged = { } <EOL> [ merged . update ( d ) for d in dicts ] <EOL> return merged <EOL> def parse_info ( response ) : <EOL> "<STR_LIT>" <EOL> info = { } <EOL> def get_value ( value ) : <EOL> if '<STR_LIT:U+002C>' not in value : <EOL> return value <EOL> sub_dict = { } <EOL> for item in value . split ( '<STR_LIT:U+002C>' ) : <EOL> k , v = item . split ( '<STR_LIT:=>' ) <EOL> try : <EOL> sub_dict [ k ] = int ( v ) <EOL> except ValueError : <EOL> sub_dict [ k ] = v <EOL> return sub_dict <EOL> for line in response . splitlines ( ) : <EOL> key , value = line . split ( '<STR_LIT::>' ) <EOL> try : <EOL> info [ key ] = int ( value ) <EOL> except ValueError : <EOL> info [ key ] = get_value ( value ) <EOL> return info <EOL> def zset_score_pairs ( response , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not response or not options [ '<STR_LIT>' ] : <EOL> return response <EOL> return zip ( response [ : : <NUM_LIT:2> ] , map ( float , response [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) ) <EOL> class Redis ( threading . local ) : <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> int <EOL> ) , <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' , <EOL> lambda r : isinstance ( r , int ) and r or r == '<STR_LIT:OK>' <EOL> ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , <EOL> lambda r : r is not None and float ( r ) or r ) , <EOL> string_keys_to_dict ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> lambda r : r == '<STR_LIT:OK>' <EOL> ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , <EOL> lambda r : r and set ( r ) or r <EOL> ) , <EOL> string_keys_to_dict ( '<STR_LIT>' , zset_score_pairs ) , <EOL> { <EOL> '<STR_LIT>' : lambda r : r == '<STR_LIT>' , <EOL> '<STR_LIT>' : parse_info , <EOL> '<STR_LIT>' : timestamp_to_datetime , <EOL> '<STR_LIT>' : lambda r : r == '<STR_LIT>' , <EOL> '<STR_LIT>' : lambda r : r and r or None , <EOL> '<STR_LIT>' : lambda r : r != - <NUM_LIT:1> and r or None , <EOL> } <EOL> ) <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , <EOL> db = <NUM_LIT:0> , password = None , <EOL> charset = '<STR_LIT:utf-8>' , errors = '<STR_LIT:strict>' ) : <EOL> self . encoding = charset <EOL> self . errors = errors <EOL> self . select ( host , port , db , password ) <EOL> def _get_host ( self ) : <EOL> return self . connection . host <EOL> host = property ( _get_host ) <EOL> def _get_port ( self ) : <EOL> return self . connection . port <EOL> port = property ( _get_port ) <EOL> def _get_db ( self ) : <EOL> return self . connection . db <EOL> db = property ( _get_db ) <EOL> def pipeline ( self ) : <EOL> return Pipeline ( self . connection , self . encoding , self . errors ) <EOL> def _execute_command ( self , command_name , command , ** options ) : <EOL> self . connection . send ( command , self ) <EOL> return self . parse_response ( command_name , ** options ) <EOL> def execute_command ( self , command_name , command , ** options ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> return self . _execute_command ( command_name , command , ** options ) <EOL> except ConnectionError : <EOL> self . connection . disconnect ( ) <EOL> return self . _execute_command ( command_name , command , ** options ) <EOL> def _parse_response ( self , command_name , catch_errors ) : <EOL> conn = self . connection <EOL> response = conn . read ( ) . strip ( ) <EOL> if not response : <EOL> self . connection . disconnect ( ) <EOL> raise ConnectionError ( "<STR_LIT>" ) <EOL> if response in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return None <EOL> byte , response = response [ <NUM_LIT:0> ] , response [ <NUM_LIT:1> : ] <EOL> if byte == '<STR_LIT:->' : <EOL> if response . startswith ( '<STR_LIT>' ) : <EOL> response = response [ <NUM_LIT:4> : ] <EOL> raise ResponseError ( response ) <EOL> elif byte == '<STR_LIT:+>' : <EOL> return response <EOL> elif byte == '<STR_LIT::>' : <EOL> return int ( response ) <EOL> elif byte == '<STR_LIT:$>' : <EOL> length = int ( response ) <EOL> if length == - <NUM_LIT:1> : <EOL> return None <EOL> response = length and conn . read ( length ) or '<STR_LIT>' <EOL> conn . read ( <NUM_LIT:2> ) <EOL> return response <EOL> elif byte == '<STR_LIT:*>' : <EOL> length = int ( response ) <EOL> if length == - <NUM_LIT:1> : <EOL> return None <EOL> if not catch_errors : <EOL> return [ self . _parse_response ( command_name , catch_errors ) <EOL> for i in range ( length ) ] <EOL> else : <EOL> data = [ ] <EOL> for i in range ( length ) : <EOL> try : <EOL> data . append ( <EOL> self . _parse_response ( command_name , catch_errors ) <EOL> ) <EOL> except Exception , e : <EOL> data . append ( e ) <EOL> return data <EOL> raise InvalidResponse ( "<STR_LIT>" % command_name ) <EOL> def parse_response ( self , command_name , catch_errors = False , ** options ) : <EOL> "<STR_LIT>" <EOL> response = self . _parse_response ( command_name , catch_errors ) <EOL> if command_name in self . RESPONSE_CALLBACKS : <EOL> return self . RESPONSE_CALLBACKS [ command_name ] ( response , ** options ) <EOL> return response <EOL> def encode ( self , value ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( value , str ) : <EOL> return value <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( self . encoding , self . errors ) <EOL> return str ( value ) <EOL> def format_inline ( self , * args , ** options ) : <EOL> "<STR_LIT>" <EOL> cmd = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( [ self . encode ( a ) for a in args ] ) <EOL> return self . execute_command ( args [ <NUM_LIT:0> ] , cmd , ** options ) <EOL> def format_bulk ( self , * args , ** options ) : <EOL> "<STR_LIT>" <EOL> bulk_value = self . encode ( args [ - <NUM_LIT:1> ] ) <EOL> cmd = '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' . join ( [ self . encode ( a ) for a in args [ : - <NUM_LIT:1> ] ] ) , <EOL> len ( bulk_value ) , <EOL> bulk_value , <EOL> ) <EOL> return self . execute_command ( args [ <NUM_LIT:0> ] , cmd , ** options ) <EOL> def format_multi_bulk ( self , * args , ** options ) : <EOL> "<STR_LIT>" <EOL> cmd_count = len ( args ) <EOL> cmds = [ ] <EOL> for i in args : <EOL> enc_value = self . encode ( i ) <EOL> cmds . append ( '<STR_LIT>' % ( len ( enc_value ) , enc_value ) ) <EOL> return self . execute_command ( <EOL> args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' % ( cmd_count , '<STR_LIT>' . join ( cmds ) ) , <EOL> ** options <EOL> ) <EOL> def get_connection ( self , host , port , db , password ) : <EOL> "<STR_LIT>" <EOL> conn = connection_manager . get_connection ( host , port , db , password ) <EOL> conn . password = password <EOL> return conn <EOL> def _setup_connection ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . connection . password : <EOL> if not self . format_inline ( '<STR_LIT>' , self . connection . password ) : <EOL> raise AuthenticationError ( "<STR_LIT>" ) <EOL> self . format_inline ( '<STR_LIT>' , self . connection . db ) <EOL> def select ( self , host , port , db , password = None ) : <EOL> """<STR_LIT>""" <EOL> self . connection = self . get_connection ( host , port , db , password ) <EOL> def bgsave ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def dbsize ( self ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def delete ( self , * names ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , * names ) <EOL> __delitem__ = delete <EOL> def flush ( self , all_dbs = False ) : <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if all_dbs : <EOL> return self . flushall ( ) <EOL> return self . flushdb ( ) <EOL> def flushall ( self ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def flushdb ( self ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def info ( self ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def lastsave ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def ping ( self ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def decr ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' , name , amount ) <EOL> def exists ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> __contains__ = exists <EOL> def expire ( self , name , time ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name , time ) <EOL> def get ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT:GET>' , name ) <EOL> __getitem__ = get <EOL> def getset ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def incr ( self , name , amount = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' , name , amount ) <EOL> def keys ( self , pattern = '<STR_LIT:*>' ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , pattern ) <EOL> def mget ( self , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> keys = list_or_args ( '<STR_LIT>' , keys , args ) <EOL> return self . format_inline ( '<STR_LIT>' , * keys ) <EOL> def mset ( self , mapping ) : <EOL> "<STR_LIT>" <EOL> items = [ ] <EOL> [ items . extend ( pair ) for pair in mapping . iteritems ( ) ] <EOL> return self . format_multi_bulk ( '<STR_LIT>' , * items ) <EOL> def msetnx ( self , mapping ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> [ items . extend ( pair ) for pair in mapping . iteritems ( ) ] <EOL> return self . format_multi_bulk ( '<STR_LIT>' , * items ) <EOL> def move ( self , name , db ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name , db ) <EOL> def randomkey ( self ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' ) <EOL> def rename ( self , src , dst , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs : <EOL> if '<STR_LIT>' in kwargs : <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> return self . renamenx ( src , dst ) <EOL> return self . format_inline ( '<STR_LIT>' , src , dst ) <EOL> def renamenx ( self , src , dst ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , src , dst ) <EOL> def set ( self , name , value , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs : <EOL> if '<STR_LIT>' in kwargs : <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> return self . getset ( name , value ) <EOL> if '<STR_LIT>' in kwargs : <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> return self . setnx ( name , value ) <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> __setitem__ = set <EOL> def setnx ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def ttl ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def type ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def blpop ( self , keys , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> keys = list ( keys ) <EOL> keys . append ( timeout ) <EOL> return self . format_inline ( '<STR_LIT>' , * keys ) <EOL> def brpop ( self , keys , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> keys = list ( keys ) <EOL> keys . append ( timeout ) <EOL> return self . format_inline ( '<STR_LIT>' , * keys ) <EOL> def lindex ( self , name , index ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' , name , index ) <EOL> def llen ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def lpop ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def lpush ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def lrange ( self , name , start , end ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' , name , start , end ) <EOL> def lrem ( self , name , value , num = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . format_bulk ( '<STR_LIT>' , name , num , value ) <EOL> def lset ( self , name , index , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , index , value ) <EOL> def ltrim ( self , name , start , end ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' , name , start , end ) <EOL> def pop ( self , name , tail = False ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if tail : <EOL> return self . rpop ( name ) <EOL> return self . lpop ( name ) <EOL> def push ( self , name , value , head = False ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if head : <EOL> return self . lpush ( name , value ) <EOL> return self . rpush ( name , value ) <EOL> def rpop ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def rpoplpush ( self , src , dst ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' , src , dst ) <EOL> def rpush ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def sort ( self , name , start = None , num = None , by = None , get = None , <EOL> desc = False , alpha = False , store = 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 = [ name ] <EOL> if by is not None : <EOL> pieces . append ( '<STR_LIT>' % by ) <EOL> if start is not None and num is not None : <EOL> pieces . append ( '<STR_LIT>' % ( start , num ) ) <EOL> if get is not None : <EOL> pieces . append ( '<STR_LIT>' % get ) <EOL> if desc : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> if alpha : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> if store is not None : <EOL> pieces . append ( '<STR_LIT>' % store ) <EOL> return self . format_inline ( '<STR_LIT>' , * pieces ) <EOL> def sadd ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def scard ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def sdiff ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> keys = list_or_args ( '<STR_LIT>' , keys , args ) <EOL> return self . format_inline ( '<STR_LIT>' , * keys ) <EOL> def sdiffstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> keys = list_or_args ( '<STR_LIT>' , keys , args ) <EOL> return self . format_inline ( '<STR_LIT>' , dest , * keys ) <EOL> def sinter ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> keys = list_or_args ( '<STR_LIT>' , keys , args ) <EOL> return self . format_inline ( '<STR_LIT>' , * keys ) <EOL> def sinterstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> keys = list_or_args ( '<STR_LIT>' , keys , args ) <EOL> return self . format_inline ( '<STR_LIT>' , dest , * keys ) <EOL> def sismember ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def smembers ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def smove ( self , src , dst , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , src , dst , value ) <EOL> def spop ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def srandmember ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def srem ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def sunion ( self , keys , * args ) : <EOL> "<STR_LIT>" <EOL> keys = list_or_args ( '<STR_LIT>' , keys , args ) <EOL> return self . format_inline ( '<STR_LIT>' , * keys ) <EOL> def sunionstore ( self , dest , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> keys = list_or_args ( '<STR_LIT>' , keys , args ) <EOL> return self . format_inline ( '<STR_LIT>' , dest , * keys ) <EOL> def zadd ( self , name , value , score ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , score , value ) <EOL> def zcard ( self , name ) : <EOL> "<STR_LIT>" <EOL> return self . format_inline ( '<STR_LIT>' , name ) <EOL> def zincr ( self , key , member , value = <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> warnings . warn ( DeprecationWarning ( <EOL> "<STR_LIT>" <EOL> ) ) <EOL> return self . zincrby ( key , member , value ) <EOL> def zincrby ( self , name , value , amount = <NUM_LIT:1> ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , amount , value ) <EOL> def zrange ( self , name , start , end , desc = False , withscores = False ) : <EOL> """<STR_LIT>""" <EOL> if desc : <EOL> return self . zrevrange ( name , start , end , withscores ) <EOL> pieces = [ '<STR_LIT>' , name , start , end ] <EOL> if withscores : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> return self . format_inline ( * pieces , ** { '<STR_LIT>' : withscores } ) <EOL> def zrangebyscore ( self , name , min , max , start = None , num = None , withscores = 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 = [ '<STR_LIT>' , name , min , max ] <EOL> if start is not None and num is not None : <EOL> pieces . extend ( [ '<STR_LIT>' , start , num ] ) <EOL> if withscores : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> return self . format_inline ( * pieces , ** { '<STR_LIT>' : withscores } ) <EOL> def zrem ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def zremrangebyscore ( self , name , min , max ) : <EOL> """<STR_LIT>""" <EOL> return self . format_inline ( '<STR_LIT>' , name , min , max ) <EOL> def zrevrange ( self , name , start , num , withscores = False ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ '<STR_LIT>' , name , start , num ] <EOL> if withscores : <EOL> pieces . append ( '<STR_LIT>' ) <EOL> return self . format_inline ( * pieces , ** { '<STR_LIT>' : withscores } ) <EOL> def zscore ( self , name , value ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , value ) <EOL> def hget ( self , name , key ) : <EOL> "<STR_LIT>" <EOL> return self . format_bulk ( '<STR_LIT>' , name , key ) <EOL> def hset ( self , name , key , value ) : <EOL> """<STR_LIT>""" <EOL> return self . format_multi_bulk ( '<STR_LIT>' , name , key , value ) <EOL> class Pipeline ( Redis ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection , charset , errors ) : <EOL> self . connection = connection <EOL> self . encoding = charset <EOL> self . errors = errors <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> self . command_stack = [ ] <EOL> self . format_inline ( '<STR_LIT>' ) <EOL> def execute_command ( self , command_name , command , ** options ) : <EOL> """<STR_LIT>""" <EOL> if command_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return super ( Pipeline , self ) . execute_command ( <EOL> command_name , command , ** options ) <EOL> else : <EOL> self . command_stack . append ( ( command_name , command , options ) ) <EOL> return self <EOL> def _execute ( self , commands ) : <EOL> all_cmds = '<STR_LIT>' . join ( [ c for _1 , c , _2 in commands ] ) <EOL> self . connection . send ( all_cmds , self ) <EOL> for i in range ( len ( commands ) - <NUM_LIT:1> ) : <EOL> _ = self . parse_response ( '<STR_LIT:_>' ) <EOL> response = self . parse_response ( '<STR_LIT:_>' , catch_errors = True ) <EOL> commands = [ ( c [ <NUM_LIT:0> ] , c [ <NUM_LIT:2> ] ) for c in commands [ <NUM_LIT:1> : - <NUM_LIT:1> ] ] <EOL> if len ( response ) != len ( commands ) : <EOL> raise ResponseError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> data = [ ] <EOL> for r , cmd in zip ( response , commands ) : <EOL> if not isinstance ( r , Exception ) : <EOL> if cmd [ <NUM_LIT:0> ] in self . RESPONSE_CALLBACKS : <EOL> r = self . RESPONSE_CALLBACKS [ cmd [ <NUM_LIT:0> ] ] ( r , ** cmd [ <NUM_LIT:1> ] ) <EOL> data . append ( r ) <EOL> return data <EOL> def execute ( self ) : <EOL> "<STR_LIT>" <EOL> self . format_inline ( '<STR_LIT>' ) <EOL> stack = self . command_stack <EOL> self . reset ( ) <EOL> try : <EOL> return self . _execute ( stack ) <EOL> except ConnectionError : <EOL> self . connection . disconnect ( ) <EOL> return self . _execute ( stack ) <EOL> def select ( self , host , port , db ) : <EOL> raise RedisError ( "<STR_LIT>" ) </s>
