<s> "<STR_LIT>" <EOL> import redis_ds . redis_config as redis_config <EOL> from redis_ds . serialization import PassThroughSerializer , PickleSerializer , JSONSerializer <EOL> class RedisList ( PassThroughSerializer ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , list_key , redis_client = redis_config . CLIENT ) : <EOL> "<STR_LIT>" <EOL> self . _client = redis_client <EOL> self . list_key = list_key <EOL> def __len__ ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _client . llen ( self . list_key ) <EOL> def __getitem__ ( self , key ) : <EOL> "<STR_LIT>" <EOL> if type ( key ) == int : <EOL> return self . deserialize ( self . _client . lindex ( self . list_key , key ) ) <EOL> elif hasattr ( key , '<STR_LIT:start>' ) and hasattr ( key , '<STR_LIT>' ) : <EOL> start = key . start or <NUM_LIT:0> <EOL> stop = key . stop or - <NUM_LIT:1> <EOL> values = self . _client . lrange ( self . list_key , start , stop ) <EOL> return [ self . deserialize ( value ) for value in values ] <EOL> else : <EOL> raise IndexError <EOL> def __setitem__ ( self , pos , val ) : <EOL> "<STR_LIT>" <EOL> val = self . serialize ( val ) <EOL> return self . _client . lset ( self . list_key , pos , val ) <EOL> def append ( self , val , head = False ) : <EOL> "<STR_LIT>" <EOL> val = self . serialize ( val ) <EOL> if head : <EOL> return self . _client . lpush ( self . list_key , val ) <EOL> else : <EOL> return self . _client . rpush ( self . list_key , val ) <EOL> def pop ( self , head = False , blocking = False ) : <EOL> "<STR_LIT>" <EOL> if head and blocking : <EOL> return self . deserialize ( self . _client . blpop ( self . list_key ) [ <NUM_LIT:1> ] ) <EOL> elif head : <EOL> return self . deserialize ( self . _client . lpop ( self . list_key ) ) <EOL> elif blocking : <EOL> return self . deserialize ( self . _client . brpop ( self . list_key ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> return self . deserialize ( self . _client . rpop ( self . list_key ) ) <EOL> def __unicode__ ( self ) : <EOL> "<STR_LIT>" <EOL> return u"<STR_LIT>" % ( self [ <NUM_LIT:0> : - <NUM_LIT:1> ] , ) <EOL> def __repr__ ( self ) : <EOL> "<STR_LIT>" <EOL> return self . __unicode__ ( ) <EOL> class PickleRedisList ( RedisList , PickleSerializer ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class JSONRedisList ( RedisList , JSONSerializer ) : <EOL> "<STR_LIT>" <EOL> pass </s>
