<s> from redis_wrap import get_redis , get_hash , get_set , setup_system <EOL> import random <EOL> resn_settings = { <EOL> '<STR_LIT>' : <NUM_LIT:1000> , <EOL> '<STR_LIT>' : <NUM_LIT> * <NUM_LIT> , <EOL> } <EOL> def create_user ( id_attr , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> get_redis ( ) . incr ( "<STR_LIT>" ) <EOL> ctr = get_redis ( ) . get ( "<STR_LIT>" ) <EOL> user = get_hash ( "<STR_LIT>" + ctr ) <EOL> for key in kwargs : <EOL> user [ key ] = kwargs [ key ] <EOL> if id_attr not in kwargs : <EOL> raise KeyError , "<STR_LIT>" % id_attr <EOL> else : <EOL> get_redis ( ) . set ( "<STR_LIT>" % kwargs [ id_attr ] , ctr ) <EOL> user [ '<STR_LIT:id>' ] = kwargs [ id_attr ] <EOL> def get_numeric_user_id ( username ) : <EOL> return get_redis ( ) . get ( "<STR_LIT>" % username ) <EOL> def delete_user ( user ) : <EOL> """<STR_LIT>""" <EOL> friends = get_set ( "<STR_LIT>" % user ) <EOL> for friend in friends : <EOL> get_set ( "<STR_LIT>" % friend ) . remove ( user ) <EOL> get_redis ( ) . delete ( "<STR_LIT>" % user ) <EOL> def get_user_by_id ( user_id ) : <EOL> """<STR_LIT>""" <EOL> return get_hash ( '<STR_LIT>' + user_id ) <EOL> def get_user ( user ) : <EOL> """<STR_LIT>""" <EOL> return get_hash ( '<STR_LIT>' + get_numeric_user_id ( user ) ) <EOL> def create_connection_by_ids ( user1 , user2 ) : <EOL> """<STR_LIT>""" <EOL> u1 = get_set ( '<STR_LIT>' % user1 ) <EOL> u2 = get_set ( '<STR_LIT>' % user2 ) <EOL> u1 . add ( user2 ) <EOL> u2 . add ( user1 ) <EOL> def delete_connection_by_ids ( user1 , user2 ) : <EOL> """<STR_LIT>""" <EOL> u1 = get_set ( '<STR_LIT>' % user1 ) <EOL> u2 = get_set ( '<STR_LIT>' % user2 ) <EOL> u1 . remove ( user2 ) <EOL> u2 . remove ( user1 ) <EOL> def create_connection ( user1 , user2 ) : <EOL> """<STR_LIT>""" <EOL> u1 = get_numeric_user_id ( user1 ) <EOL> u2 = get_numeric_user_id ( user2 ) <EOL> create_connection_by_ids ( u1 , u2 ) <EOL> def delete_connection ( user1 , user2 ) : <EOL> """<STR_LIT>""" <EOL> u1 = get_numeric_user_id ( user1 ) <EOL> u2 = get_numeric_user_id ( user2 ) <EOL> delete_connection_by_ids ( u1 , u2 ) <EOL> def extract ( iterable ) : <EOL> return ( get_user_by_id ( user ) [ '<STR_LIT:id>' ] for user in iterable ) <EOL> def get_friend_list ( user ) : <EOL> """<STR_LIT>""" <EOL> user = get_numeric_user_id ( user ) <EOL> return extract ( get_set ( "<STR_LIT>" % user ) ) <EOL> def follow_by_ids ( follower , followee ) : <EOL> """<STR_LIT>""" <EOL> following = get_set ( "<STR_LIT>" % follower ) <EOL> following . add ( followee ) <EOL> followers = get_set ( "<STR_LIT>" % followee ) <EOL> followers . add ( follower ) <EOL> def follow ( follower , followee ) : <EOL> """<STR_LIT>""" <EOL> fr = get_numeric_user_id ( follower ) <EOL> fe = get_numeric_user_id ( followee ) <EOL> follow_by_ids ( fr , fe ) <EOL> def unfollow_by_ids ( follower , followee ) : <EOL> """<STR_LIT>""" <EOL> following = get_set ( "<STR_LIT>" % follower ) <EOL> following . remove ( followee ) <EOL> followers = get_set ( "<STR_LIT>" % followee ) <EOL> followers . remove ( follower ) <EOL> def unfollow ( follower , followee ) : <EOL> """<STR_LIT>""" <EOL> fr = get_numeric_user_id ( follower ) <EOL> fe = get_numeric_user_id ( followee ) <EOL> unfollow_by_ids ( fr , fe ) <EOL> def get_followers_list ( user ) : <EOL> """<STR_LIT>""" <EOL> user = get_numeric_user_id ( user ) <EOL> return extract ( get_set ( "<STR_LIT>" % user ) ) <EOL> def get_following_list ( user ) : <EOL> """<STR_LIT>""" <EOL> user = get_numeric_user_id ( user ) <EOL> return extract ( get_set ( "<STR_LIT>" % user ) ) <EOL> def new_update ( username , ** update ) : <EOL> """<STR_LIT>""" <EOL> user = get_numeric_user_id ( username ) <EOL> get_redis ( ) . incr ( "<STR_LIT>" ) <EOL> ctr = get_redis ( ) . get ( "<STR_LIT>" ) <EOL> upd = get_hash ( "<STR_LIT>" + ctr ) <EOL> upd [ '<STR_LIT:user>' ] = user <EOL> for key in update : <EOL> upd [ key ] = update [ key ] <EOL> friends = get_redis ( ) . sunion ( "<STR_LIT>" % user , "<STR_LIT>" % user ) <EOL> for friend in friends : <EOL> get_redis ( ) . lpush ( "<STR_LIT>" % friend , ctr ) <EOL> get_redis ( ) . ltrim ( "<STR_LIT>" % friend , <NUM_LIT:0> , resn_settings [ '<STR_LIT>' ] ) <EOL> get_redis ( ) . lpush ( "<STR_LIT>" % user , ctr ) <EOL> def delete_update ( update_id ) : <EOL> """<STR_LIT>""" <EOL> get_redis ( ) . delete ( "<STR_LIT>" + update_id ) <EOL> def get_updates_from_list ( feed ) : <EOL> """<STR_LIT>""" <EOL> newfeed = [ ] <EOL> for item in feed : <EOL> update = get_hash ( '<STR_LIT>' % item ) <EOL> if len ( update ) : <EOL> newfeed . append ( update ) <EOL> return newfeed <EOL> def get_feed ( username , limit = resn_settings [ '<STR_LIT>' ] ) : <EOL> """<STR_LIT>""" <EOL> user = get_numeric_user_id ( username ) <EOL> return get_updates_from_list ( get_redis ( ) . lrange ( "<STR_LIT>" % user , <NUM_LIT:0> , limit ) ) <EOL> def get_user_updates ( username , limit = resn_settings [ '<STR_LIT>' ] ) : <EOL> """<STR_LIT>""" <EOL> user = get_numeric_user_id ( username ) <EOL> return get_updates_from_list ( get_redis ( ) . lrange ( "<STR_LIT>" % user , <NUM_LIT:0> , limit ) ) <EOL> def generate_auth_token ( ) : <EOL> return '<STR_LIT>' . join ( random . choice ( '<STR_LIT>' ) for i in range ( <NUM_LIT> ) ) <EOL> def check_password ( username , password , password_field = '<STR_LIT:password>' ) : <EOL> """<STR_LIT>""" <EOL> user = get_hash ( '<STR_LIT>' % get_numeric_user_id ( username ) ) <EOL> if not user : <EOL> return False <EOL> return user [ password_field ] == password <EOL> def login_user ( username ) : <EOL> """<STR_LIT>""" <EOL> token = generate_auth_token ( ) <EOL> user = get_user ( username ) <EOL> user [ '<STR_LIT>' ] = token <EOL> get_redis ( ) . set ( "<STR_LIT>" % token , str ( get_numeric_user_id ( username ) ) ) <EOL> get_redis ( ) . expire ( "<STR_LIT>" % token , resn_settings [ '<STR_LIT>' ] ) <EOL> return token <EOL> def logout_user ( username ) : <EOL> """<STR_LIT>""" <EOL> user = get_user ( username ) <EOL> token = user [ '<STR_LIT>' ] <EOL> get_redis ( ) . delete ( "<STR_LIT>" % token ) <EOL> del user [ '<STR_LIT>' ] <EOL> def validate_token ( token ) : <EOL> """<STR_LIT>""" <EOL> user_id = get_redis ( ) . get ( "<STR_LIT>" % token ) <EOL> user = get_hash ( "<STR_LIT>" % user_id ) <EOL> if not len ( user ) : <EOL> return False <EOL> return user [ '<STR_LIT>' ] == token </s>
