本文整理汇总了Python中redis.StrictRedis.sadd方法的典型用法代码示例。如果您正苦于以下问题:Python StrictRedis.sadd方法的具体用法?Python StrictRedis.sadd怎么用?Python StrictRedis.sadd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.StrictRedis
的用法示例。
在下文中一共展示了StrictRedis.sadd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Redis
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class Redis(BaseDB):
def __init__(self, host='localhost', port=6379, db=0):
super().__init__()
self.db = StrictRedis(host=host, port=port, db=db)
self.available = False
def push_to_available(self, user_id):
self.db.sadd('available', user_id) # Add user to awaiting queue
self.check_available()
def pop_first_available(self, self_user_id):
while not self.available:
threading.Event().wait() # Prevent busy wait and CPU load
res = [int(x) for x in self.db.smembers('available')] # Get all available possible interlocutors
shuffle(res) # Randomize queue
if self_user_id in res: # User may be listed too
res.remove(self_user_id) # If so, remove him
res = res.pop() # Get random interlocutor
self.check_available()
return res
def remove(self, user_id):
self.db.srem('available', user_id)
self.check_available()
def check_available(self):
self.available = True if self.db.scard('available') - 1 > 0 else False # If there are more than 1 user in queue
示例2: ProxyCheckSpider
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class ProxyCheckSpider(Spider):
''' Spider to crawl free proxy servers for intern
'''
name = 'proxy_check'
def __init__(self, mode='prod', *args, **kwargs):
if mode == 'prod':
LOCAL_CONFIG_YAML = './hq-proxies.yml'
elif mode == 'test':
LOCAL_CONFIG_YAML = './hq-proxies.test.yml'
with open(LOCAL_CONFIG_YAML, 'r', encoding='utf-8') as f:
LOCAL_CONFIG = yaml.load(f)
self.redis_db = StrictRedis(
host=LOCAL_CONFIG['REDIS_HOST'],
port=LOCAL_CONFIG['REDIS_PORT'],
password=LOCAL_CONFIG['REDIS_PASSWORD'],
db=LOCAL_CONFIG['REDIS_DB']
)
self.validator_pool = set([])
for validator in LOCAL_CONFIG['PROXY_VALIDATORS']:
self.validator_pool.add((validator['url'], validator['startstring']))
self.PROXY_COUNT = LOCAL_CONFIG['PROXY_COUNT']
self.PROXY_SET = LOCAL_CONFIG['PROXY_SET']
def start_requests(self):
logger.info('测试代理池内代理质量...')
self.redis_db.set(self.PROXY_COUNT, self.redis_db.scard(self.PROXY_SET))
for proxy in self.redis_db.smembers(self.PROXY_SET):
proxy = proxy.decode('utf-8')
vaurl, vastart = random.choice(list(self.validator_pool))
yield Request(url=vaurl, meta={'proxy': proxy, 'startstring': vastart}, callback=self.checkin, dont_filter=True)
def checkin(self, response):
res = response.body_as_unicode()
if 'startstring' in response.meta and res.startswith(response.meta['startstring']):
proxy = response.meta['proxy']
self.redis_db.sadd(self.PROXY_SET, proxy)
logger.info('可用代理+1 %s' % proxy)
yield None
else:
proxy = response.url if 'proxy' not in response.meta else response.meta['proxy']
self.redis_db.srem(self.PROXY_SET, proxy)
logger.info('无效代理 %s' % proxy)
yield None
def closed(self, reason):
pcount = self.redis_db.scard(self.PROXY_SET)
logger.info('代理池测试完成,有效代理数: %s' % pcount)
self.redis_db.set(self.PROXY_COUNT, pcount)
示例3: RedisBroker
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class RedisBroker(object):
def __init__(self, **redis_c):
self.ip_set_name = redis_c.pop('ip_set_name')
self._r = StrictRedis(**redis_c)
def ip_notebook(self, ip):
(count, status) = self._r.hmget(ip, 'count', 'status')
print count, status
if not count:
self._r.hmset(ip, {'count': 1, 'status': 0})
self._r.sadd(self.ip_set_name, ip)
else:
self._r.hincrby(ip, 'count', amount=1)
示例4: add_tweets_to_redis
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
def add_tweets_to_redis(tweet_file):
"""
'DVD: FBI WARNING Me: oh boy here we go DVD: The board advises you
to have lots of fun watching this Hollywood movie Me: Ah.. It's a
nice one'
"""
redis_client = StrictRedis(host='localhost', port=6379, db=0)
with open(tweet_file, 'r') as tweets:
for line in tweets:
# again, dealing with weird error here
try:
tweet = line.strip().split('|', 2)[2]
# need to investigate whether one-by-one inserting
# or building a list of tweets and doing a single insert
# would be more efficient
if not redis_client.sismember('tweets', tweet):
result = redis_client.sadd('tweets', tweet)
if not result:
print('error occurred adding tweet: "{}" to redis'
.format(tweet))
else:
# found all new tweets
break
except IndexError:
continue
redis_client.save()
示例5: test_vote_page_check_scope
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
def test_vote_page_check_scope(self):
cache = StrictRedis(db=config.tokens_cache_redis_db)
h = Headers()
h.add('Authorization',
'Basic ' + base64.b64encode(config.test_token + ':'))
rv = Client.open(self.client, path='/people/v1/vote/hhauer',
headers=h)
self.assert_200(rv)
cache.srem(config.people_scope_vote, '[email protected]')
rv = Client.open(self.client, path='/people/v1/vote/hhauer',
headers=h)
self.assert_403(rv)
cache.sadd(config.people_scope_vote, '[email protected]')
示例6: Persist
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class Persist(object):
""" Sequential writer for Carbon server.
The story is simple, fetch data from redis, write them, wait, loop.
This code is supervised by Carbon daemon.
"""
def __init__(self, path="/tmp/"):
self.redis = Redis()
self.path = path
self.dirs = set()
self.redis.sadd(METRICS, METRIC_POINTS, METRIC_WRITE)
def metric(self, name, value):
"Add some metrics : make your own dogfood, just before lunch."
timestamp = time.time()
serialized = struct.pack('!ff', timestamp, value)
pipe = self.redis.pipeline()
pipe.zadd(name, timestamp, serialized)
pipe.publish(name, serialized)
pipe.execute()
def run(self):
while True:
before = time.time()
self.handle()
after = time.time()
self.metric(METRIC_WRITE, (after - before) * 1000)
time.sleep(PERIOD - int(before) + int(after))
def handle(self):
points = 0
for metric in self.redis.smembers(METRICS):
values = self.redis.zrange(metric, 0, -1)
points += len(values)
f = target_to_path(self.path, metric)
d = os.path.dirname(f)
if d not in self.dirs:
if not os.path.isdir(d):
os.makedirs(d)
self.dirs.add(d)
if not os.path.exists(f):
whisper.create(f, [(10, 1000)]) # [FIXME] hardcoded values
whisper.update_many(f, [struct.unpack('!ff', a) for a in values])
if len(values):
self.redis.zrem(metric, *values)
self.metric(METRIC_POINTS, points)
示例7: MispRedisConnector
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class MispRedisConnector(object):
def __init__(self):
Base = automap_base()
engine = create_engine("mysql://{}:{}@{}/{}".format(user, password, host, dbname))
# reflect the tables
Base.prepare(engine, reflect=True)
metadata = MetaData()
metadata.reflect(bind=engine)
self.connection = engine.connect()
self.events = Table("events", metadata, autoload=True)
self.attributes = Table("attributes", metadata, autoload=True)
self.r = StrictRedis(unix_socket_path=redis_socket)
def add_value(self, value, event_uuid, attribute_uuid):
hash_value = SHA.new(value).hexdigest()
self.r.set("v:" + hash_value, attribute_uuid)
self.r.set("v:" + attribute_uuid, value)
self.r.sadd(event_uuid, attribute_uuid)
self.r.sadd(attribute_uuid, event_uuid)
self.r.sadd(hash_value, event_uuid)
def make_correlations(self):
# Build hashtable of event ID - event UUID
eid_uuid = {}
results = self.connection.execute(select([self.events]))
for event in results:
eid_uuid[event["id"]] = event["uuid"]
# Create correlations
results = self.connection.execute(select([self.attributes]))
for attribute in results:
try:
self.add_value(attribute["value1"], eid_uuid[attribute["event_id"]], attribute["uuid"])
except Exception as e:
print(e)
print(attribute)
if len(attribute["value2"]) > 0:
self.add_value(attribute["value2"], eid_uuid[attribute["event_id"]], attribute["uuid"])
示例8: Storage
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class Storage(object):
def __init__(self, host=None, port=None, *args, **kwargs):
if host is None:
host = os.environ.get('REDIS_HOST', 'localhost')
if port is None:
port = os.environ.get('REDIS_PORT', '6379')
self.redis = StrictRedis(host, port, *args, **kwargs)
def add_labels(self, labels):
self.redis.sadd('labels', *labels)
def add_image(self, image_url, labels):
p = self.redis.pipeline()
for label in labels:
p.sadd(label, image_url)
p.setnx('repr_img:{}'.format(label), image_url)
p.execute()
示例9: __init__
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class BadWords:
def __init__(self, filename):
redis_url = "localhost"
self.redis = StrictRedis(host=redis_url, db=1)
readed = open(filename)
self.badwords = readed.read()
readed.close()
self.bad_word_tokens = self.tokenize_bad_words()
def tokenize_bad_words(self):
return tokenize.word_tokenize(self.badwords)
def badWordsInRedis(self):
for word in self.bad_word_tokens:
pronunciations = cmudict.dict().get(word,[])
for pron in pronunciations:
for i in range(2,len(pron)):
self.redis.sadd(":".join(pron[-i:]), word)
示例10: new_group
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
def new_group(request):
groupname = request.POST.get("groupname")
totwords = request.POST.get("totwords")
totmembers = request.POST.get('totmembers')
pref = settings.MY_PREFIX
prefg = pref+":"+groupname
user = str(request.user)
rd = StrictRedis()
# the above statements are self explanatory
exists = rd.exists(pref+":"+groupname)
if exists:
# return error, can't create the group with that name
# don't do that, just add this user to the already existing group
# if group size < totmembers
d = rd.hgetall(prefg+":hash")
response = {'facility':""}
if int(d['totmembers'])>int(d['curmembers']):
rd.hincrby(prefg+":hash", 'curmembers')
#d = rd.hgetall(prefg+":hash")
response['facility'] = groupname
response['new_group_created'] = False
rd.sadd(prefg, user)
#now notify the others that this user joined the group!
redis_publisher = RedisPublisher(facility = pref, broadcast = True)
mydict = {"type":"new_join", 'who':user, 'name':groupname}
msgstring = json.dumps(mydict)
print "broadcast message:"+msgstring
message = RedisMessage(msgstring)
redis_publisher.publish_message(message)
# now check if the competition is ready to start
if int(d['totmembers'])-1 == int(d['curmembers']):
start_competition(request,groupname)
return JsonResponse(response)
# so the group doesn't exist
rd.sadd(prefg, user)
# add this user to the set of all the users that are part of this group
hashdict = {'totwords':totwords, 'totmembers':totmembers, "owner":user, "name":groupname, 'curmembers':1}
# adding group name is redundant but it simplifies things a lot
rd.hmset(prefg+":hash", hashdict)
# using hash greatly simplifies the things(dict interconversion hgetall()), though i feel that the
# naming convention used is horrible
redis_publisher = RedisPublisher(facility = pref, broadcast = True)
mydict = {"type":"new_group"}
mydict.update(hashdict)
msgstring = json.dumps(mydict)
print msgstring
message = RedisMessage(msgstring)
redis_publisher.publish_message(message)
# notify the others about this new group that was created
rd.sadd(pref+":groups", groupname)
return JsonResponse({'facility':groupname, 'new_group_created':True})
示例11: Redis
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class Redis():
INVITE_EXPIRATION = 300
def __init__(self, host='pulsing.jhk.org', port=6379, password='wsad', db=0, **kwargs):
super().__init__()
self.logger = logging.getLogger(__name__)
self.__client = StrictRedis(host=host, port=port, password=password, db=0, **kwargs)
def storeInvitation(self, to_user_id, from_user_id, invitation_id, invitationType):
expiration = datetime.utcnow() + timedelta(seconds=Redis.INVITE_EXPIRATION)
self.__client.setex(invitation_id, '1', Redis.INVITE_EXPIRATION)
self.__client.sadd('INVITATIONS_'+to_user_id,
json.dumps(getInvitation(from_user_id, invitationType, invitation_id, expiration.timestamp() * 1000.0)))
def removeInvitation(self, user_id, invitation_id):
s_invitations = self.__client.smembers('INVITATIONS_'+user_id)
self.logger.debug('removeInvitation fetched - %s ', s_invitations)
invitations = json.loads(s_invitations)
self.logger.debug('removeInvitation parsed - %s ', invitations)
invitation = [invite for invite in invitations if (invite['invitationId'] == invitation_id)]
self.__client.delete(invitation_id)
return invitation
@property
def client(self):
return self.__client
def __eq__(self, other):
return self.__client == other.__client
def __str__(self):
return self.__client.__str__()
def __hash__(self):
return self.__client.__hash__()
示例12: TestPredis
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class TestPredis(object):
def setUp(self):
self.redis = StrictRedis(db=9)
self.predis = Predis(prefix="test", db=9)
def tearDown(self):
self.redis.flushdb()
def test_set(self):
self.predis.set("foo", "bar")
assert self.redis.get("test:foo") == "bar"
def test_get(self):
self.redis.set("test:foo", "bar")
assert self.predis.get("foo") == "bar"
def test_sunionstore(self):
self.redis.sadd("test:foo", "bar")
self.redis.sadd("test:boo", "bat")
self.redis.sadd("test:goo", "bak")
self.predis.sunionstore("blah", ["foo", "boo"], "goo")
assert self.redis.smembers("test:blah") == set(["bar", "bat", "bak"])
示例13: build_db
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
def build_db():
json_data = []
with open("artist.json", 'r') as _file:
for line in _file.readlines():
# json stringをdic型に変換
json_data.append(loads(line))
path = "./config.ini"
redis_config = RedisConfig(path)
host, port, db = redis_config.read_config()
r = StrictRedis(host=host, port=port, db=db)
# 接続しているDBを全消し
r.flushdb()
# Redisに登録
for dic in json_data:
if "tags" in dic:
for tag in dic["tags"]:
# ユニークID + アーティスト名, 被タグ数_タグのリストの組み合わせ
r.sadd(str(dic["id"]) + "_" + dic["name"],
str(tag["count"]) + "_" + tag["value"])
示例14: signin
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
def signin(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/')
form = SignInForm(request.POST or None)
if form.is_valid():
username_or_email = form.cleaned_data['username_or_email']
password = form.cleaned_data['password']
user_obj = None
try:
user_obj = User.objects.get(username=username_or_email)
except:
user_obj = User.objects.get(email=username_or_email)
if user_obj:
if check_password(password, user_obj.password):
user = authenticate(username=user_obj.username, password=password)
if user:
if user.is_active:
login(request, user)
request.session['username'] = user_obj.username
# Append the user to online users set in redis.
# Also create a notifications object which will be used
# to initiate and send notifications
redis_obj = StrictRedis(db=9)
redis_obj.sadd('online:users', user_obj.username)
next = request.GET.get('next', '')
if next:
return HttpResponseRedirect(next)
else:
return HttpResponseRedirect(reverse('profile', args=(str(user_obj.username),)))
else:
messages.info(request, 'Please activate your account first.')
return HttpResponseRedirect(reverse('accounts.views.signin.'))
return render(request, 'accounts/signin.html', {
'form': form,
})
示例15: Redis
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import sadd [as 别名]
class Redis(Backend):
"""Default backend for MimicDB. Initiated with identical parameters
as ``redis.StrictRedis``.
:param args \*args, \**kwargs: StrictRedis.__init__() parameters
.. code-block:: python
from mimicdb.backends.default import Redis
redis = Redis(host='localhost', port=6379, db=0)
"""
def __init__(self, *args, **kwargs):
self._redis = StrictRedis(*args, **kwargs)
def keys(self, pattern='*'):
return self._redis.keys(pattern)
def delete(self, *names):
return self._redis.delete(*names)
def sadd(self, name, *values):
return self._redis.sadd(name, *values)
def srem(self, name, *values):
return self._redis.srem(name, *values)
def sismember(self, name, value):
return self._redis.sismember(name, value)
def smembers(self, name):
return self._redis.smembers(name)
def hmset(self, name, mapping):
return self._redis.hmset(name, mapping)
def hgetall(self, name):
return self._redis.hgetall(name)