def test_set(): fishes = get_set('fishes') assert 'nemo' not in fishes fishes.add('nemo') assert 'nemo' in fishes for item in fishes: assert item == 'nemo'
def __init__(self, node, sub_type, prefix, system = 'default'): if prefix is not None: self.prefix = prefix pass else: self.prefix = node pass self.sub_type = sub_type self.name = node self.__refs = weakref.WeakValueDictionary() self.__keys = redis_wrap.get_set(self.name) self.system = system pass
def test_set(self): fishes = get_set(u'fishes') try: self.assertNotIn(u'nemo', fishes) except AttributeError: self.assertTrue(u'nemo' not in fishes) fishes.add(u'nemo') try: self.assertIn(u'nemo', fishes) except AttributeError: self.assertTrue(u'nemo' in fishes) self.assertTrue(all(fish == u'nemo' for fish in fishes))
def follow_by_ids(follower, followee): """Creates an asymmetric connection between two users. Uses internal numeric ids""" following = get_set("users.%s.following" % follower) following.add(followee) followers = get_set("users.%s.followers" % followee) followers.add(follower)
def get_friend_list(user): """Returns a list of the user's friends""" user = get_numeric_user_id(user) return extract(get_set("users.%s.friends" % user))
def delete_connection_by_ids(user1, user2): """Deletes a 'friendship' between two users. Uses internal numeric ids""" u1 = get_set('users.%s.friends' % user1) u2 = get_set('users.%s.friends' % user2) u1.remove(user2) u2.remove(user1)
def create_connection_by_ids(user1, user2): """Creates a 'friendship' between two users. Uses internal numeric ids""" u1 = get_set('users.%s.friends' % user1) u2 = get_set('users.%s.friends' % user2) u1.add(user2) u2.add(user1)
def delete_user(user): """ Deletes the user and removes all friendships """ friends = get_set("users.%s.friends" % user) for friend in friends: get_set("users.%s.friends" % friend).remove(user) get_redis().delete("users.%s" % user)
def get_following_list(user): """Returns a list of the users the user is following""" user = get_numeric_user_id(user) return extract( get_set("users.%s.following" % user) )
#!/usr/bin/env python import redis from redis_wrap import setup_system, get_hash, get_set, get_list import Config clients = {'default': redis.Redis(Config.REDIS_HOST, Config.REDIS_PORT)} setup_system('default', Config.REDIS_HOST, Config.REDIS_PORT) def Redis(k = 'default'): return clients[k] if __name__ == '__main__': h = get_hash('test') print set(get_set('haha'))
def get_set(name, system=REDIS_NAME): return redis_wrap.get_set('{}:{}'.format(PREFIX, name), system=system)
def test_set(): fishes = get_set('fishes') assert len(fishes) == 0 assert 'nemo' not in fishes assert raises(lambda: fishes.remove('nemo'), KeyError) fishes.discard('nemo') # it's ok to .discard() nonexistant items fishes.add('nemo') assert len(fishes) == 1 assert 'nemo' in fishes for item in fishes: assert item == 'nemo' fishes.remove('nemo') assert len(fishes) == 0 fishes.add('dory') assert fishes.pop() == 'dory' assert raises(lambda: fishes.pop(), KeyError) fishes.add('marlin') assert len(fishes) == 1 fishes.clear() assert len(fishes) == 0 fishes.update(('nemo', 'marlin')) assert set(fishes) == set(['nemo', 'marlin']) fishes |= ('dory', 'crush') assert set(fishes) == set(['nemo', 'marlin', 'dory', 'crush']) other_fishes = get_set('other_fishes') other_fishes.update(('gill', 'bloat', 'flo')) fishes |= other_fishes assert set(fishes) == set( ['nemo', 'marlin', 'dory', 'crush', 'gill', 'bloat', 'flo']) fishes.intersection_update( ('nemo', 'marlin', 'dory', 'crush', 'gill', 'deb', 'bloat')) assert set(fishes) == set( ['nemo', 'marlin', 'dory', 'crush', 'gill', 'bloat']) fishes &= ('nemo', 'marlin', 'dory', 'gill', 'bloat', 'gurgle') assert set(fishes) == set(['nemo', 'marlin', 'dory', 'gill', 'bloat']) fishes &= other_fishes assert set(fishes) == set(['gill', 'bloat']) fishes.clear() fishes.update(('nemo', 'marlin', 'dory', 'gill', 'bloat')) fishes.difference_update(('gill', 'bloat', 'flo')) assert set(fishes) == set(['nemo', 'marlin', 'dory']) fishes.clear() fishes.update(('nemo', 'marlin', 'dory', 'gill', 'bloat')) fishes -= ('gill', 'bloat', 'flo') assert set(fishes) == set(['nemo', 'marlin', 'dory']) fishes.clear() fishes.update(('nemo', 'marlin', 'dory', 'gill', 'bloat')) fishes -= other_fishes assert set(fishes) == set(['nemo', 'marlin', 'dory']) fishes.clear() fishes.update(('nemo', 'marlin', 'dory', 'gill', 'bloat')) fishes.symmetric_difference_update(('gill', 'bloat', 'flo')) assert set(fishes) == set(['nemo', 'marlin', 'dory', 'flo']) fishes.clear() fishes.update(('nemo', 'marlin', 'dory', 'gill', 'bloat')) fishes ^= ('gill', 'bloat', 'flo') assert set(fishes) == set(['nemo', 'marlin', 'dory', 'flo']) fishes.clear() fishes.update(('nemo', 'marlin', 'dory', 'gill', 'bloat')) fishes ^= other_fishes assert set(fishes) == set(['nemo', 'marlin', 'dory', 'flo']) print(sys._getframe(0).f_code.co_name, 'ok.')
def test_set(): fishes = get_set('fishes') assert len(fishes) == 0 assert 'nemo' not in fishes assert raises(lambda: fishes.remove('nemo'), KeyError) fishes.discard('nemo') # it's ok to .discard() nonexistant items fishes.add('nemo') assert len(fishes) == 1 assert 'nemo' in fishes for item in fishes: assert item == 'nemo' fishes.remove('nemo') assert len(fishes) == 0 fishes.add('dory') assert fishes.pop() == 'dory' assert raises(lambda: fishes.pop(), KeyError) fishes.add('marlin') assert len(fishes) == 1 fishes.clear() assert len(fishes) == 0 fishes.update(('nemo','marlin')) assert set(fishes) == set (['nemo','marlin']) fishes |= ('dory','crush') assert set(fishes) == set (['nemo','marlin', 'dory', 'crush']) other_fishes = get_set('other_fishes') other_fishes.update(('gill', 'bloat', 'flo')) assert set(fishes | other_fishes) == set(['nemo','marlin', 'dory', 'crush', 'gill', 'bloat', 'flo']) assert set(fishes) == set (['nemo','marlin', 'dory', 'crush']) fishes |= other_fishes assert set(fishes) == set(['nemo','marlin', 'dory', 'crush', 'gill', 'bloat', 'flo']) fishes.intersection_update(('nemo','marlin', 'dory', 'crush', 'gill', 'deb', 'bloat')) assert set(fishes) == set(['nemo','marlin', 'dory', 'crush', 'gill', 'bloat']) fishes &= ('nemo','marlin', 'dory', 'gill', 'bloat', 'gurgle') assert set(fishes) == set(['nemo','marlin', 'dory', 'gill', 'bloat']) assert set(fishes & other_fishes) == set(['gill', 'bloat']) assert set(fishes) == set(['nemo','marlin', 'dory', 'gill', 'bloat']) fishes &= other_fishes assert set(fishes) == set(['gill', 'bloat']) fishes.clear() fishes.update(('nemo','marlin', 'dory', 'gill', 'bloat')) fishes.difference_update(('gill', 'bloat', 'flo')) assert set(fishes) == set(['nemo','marlin', 'dory']) fishes.clear() fishes.update(('nemo','marlin', 'dory', 'gill', 'bloat')) fishes -= ('gill', 'bloat', 'flo') assert set(fishes) == set(['nemo','marlin', 'dory']) fishes.clear() fishes.update(('nemo','marlin', 'dory', 'gill', 'bloat')) assert set(fishes-other_fishes) == set(['nemo','marlin', 'dory']) assert set(fishes) == set(['nemo','marlin', 'dory', 'gill', 'bloat']) fishes -= other_fishes assert set(fishes) == set(['nemo','marlin', 'dory']) fishes.clear() fishes.update(('nemo','marlin', 'dory', 'gill', 'bloat')) fishes.symmetric_difference_update(('gill', 'bloat', 'flo')) assert set(fishes) == set(['nemo','marlin', 'dory', 'flo']) fishes.clear() fishes.update(('nemo','marlin', 'dory', 'gill', 'bloat')) fishes ^= ('gill', 'bloat', 'flo') assert set(fishes) == set(['nemo','marlin', 'dory', 'flo']) fishes.clear() fishes.update(('nemo','marlin', 'dory', 'gill', 'bloat')) assert set(fishes ^ other_fishes) == set(['nemo','marlin', 'dory', 'flo']) assert set(fishes) == set(['nemo','marlin', 'dory', 'gill', 'bloat']) fishes ^= other_fishes assert set(fishes) == set(['nemo','marlin', 'dory', 'flo']) print sys._getframe(0).f_code.co_name, 'ok.'
def _getter(self): redis_key = gen_property_key(self.__class__, self.idx, key) return get_set(redis_key)
def get_task_set(queue_name, serialized_type='json'): """ 得到 一个 task_md5 -> task 的字典对象 """ return get_set('ztq:set:task:' + queue_name, serialized_type=serialized_type)
def get_cron_set(): """ 定时任务list TODO """ return get_set('ztq:set:cron')
def unfollow_by_ids(follower, followee): """Deletes an asymmetric connection between two users. Uses internal numeric ids""" following = get_set("users.%s.following" % follower) following.remove(followee) followers = get_set("users.%s.followers" % followee) followers.remove(follower)