本文整理汇总了Python中redis.StrictRedis.zincrby方法的典型用法代码示例。如果您正苦于以下问题:Python StrictRedis.zincrby方法的具体用法?Python StrictRedis.zincrby怎么用?Python StrictRedis.zincrby使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.StrictRedis
的用法示例。
在下文中一共展示了StrictRedis.zincrby方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RedisWordCountBolt
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
class RedisWordCountBolt(WordCountBolt):
def initialize(self, conf, ctx):
self.redis = StrictRedis()
self.total = 0
def _increment(self, word, inc_by):
self.total += inc_by
self.redis.zincrby("words", word, inc_by)
示例2: __init__
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
class WordList:
def __init__(self):
self.conn = Redis()
self.CACHE_SIZE = 50
self.CACHE_KEYS = "words-keys"
self.CACHE_STORE = "words-store"
self.WORD_FILE = os.path.join(os.path.expanduser("~"), '.words.txt')
def _reorganize(self):
pop_n = self.conn.zcard(self.CACHE_KEYS) - self.CACHE_SIZE
if pop_n >= 0:
to_pop = self.conn.zrange(self.CACHE_KEYS, 0, pop_n)
#print pop_n, to_pop
self.conn.zremrangebyrank(self.CACHE_KEYS, 0, pop_n)
for k in to_pop:
self.conn.hdel(self.CACHE_STORE, k)
def _add_word(self, key, value):
result = self.conn.hget(self.CACHE_STORE, key)
if result:
self.conn.zincrby(self.CACHE_KEYS, key, 1.0)
else:
self._reorganize()
self.conn.hset(self.CACHE_STORE, key, value)
self.conn.zadd(self.CACHE_KEYS, 1, key)
def _get_words(self):
try:
words = self.conn.zrevrange(self.CACHE_KEYS, 0, -1, True)
#hashs = self.conn.hgetall(self.CACHE_STORE)
#print words
#print hashs
return words
except redis.exceptions.ConnectionError:
return None
def dump_console(self):
if os.path.isfile(self.WORD_FILE):
with open(self.WORD_FILE, 'r') as f:
print f.read()
def write_file(self):
words = self._get_words()
if words is None:
return
content = '\n'.join(["%d. %s\t %d"%(i, x[0], int(x[1])) for i, x in enumerate(words)])
with open(self.WORD_FILE, 'w+') as f:
f.write(content)
f.write('\n')
def add_word(self, key):
try:
self._add_word(key,key)
self.write_file()
except redis.exceptions.ConnectionError:
return
示例3: WordCounter
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
class WordCounter(Bolt):
def initialize(self, conf, ctx):
self.counts = Counter()
self.redis = StrictRedis()
def process(self, tup):
word = tup.values[0]
# Increment the word count in redis
self.redis.zincrby("wordcount", word)
# increase the word count locally
self.counts[word] += 1
self.emit([word, self.counts[word]])
# log the count for displaying
self.log('%s: %d' % (word, self.counts[word]))
示例4: WordCountBolt
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
class WordCountBolt(Bolt):
def initialize(self, conf, ctx):
self.redis = StrictRedis()
self.counter = Counter()
self.pid = os.getpid()
def process(self, tup):
word, = tup.values
inc_by = 10 if word == "dog" else 1
self.redis.zincrby("words", word, inc_by)
self.log_count(word)
def log_count(self, word):
ct = self.counter
ct[word] += 1; ct["total"] += 1
total = ct["total"]
if total % 1000 == 0:
self.log("counted [{:,}] words [pid={}]"
.format(total, self.pid))
示例5: RedisWordCountBolt
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
class RedisWordCountBolt(Bolt):
outputs = ["word", "count"]
def initialize(self, conf, ctx):
self.redis = StrictRedis()
self.total = 0
def _increment(self, word, inc_by):
self.total += inc_by
return self.redis.zincrby("words", word, inc_by)
def process(self, tup):
word = tup.values[0]
count = self._increment(word, 10 if word == "dog" else 1)
if self.total % 1000 == 0:
self.logger.info("counted %i words", self.total)
self.emit([word, count])
示例6: GistStorage
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
class GistStorage(object):
SUPER_KEY = 'live'
def __init__(self, host, port, password):
self.client = StrictRedis(host=host, port=port, password=password)
def upvote(self, gist_id):
return self.client.zincrby(self.SUPER_KEY, gist_id, 1)
def get_rank(self, gist_id):
return self.client.zrevrank(self.SUPER_KEY, gist_id)
def get_points(self, gist_id):
return self.client.zscore(self.SUPER_KEY, gist_id)
def get_rank_list(self, offset=0, num=-1):
return self.client.zrevrange(self.SUPER_KEY, offset, num, withscores=True, score_cast_func=float)
示例7: RedisTopTalkerTracker
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
class RedisTopTalkerTracker(object):
def __init__(self, size=16384, redis_host='localhost', redis_port=6379,
redis_table='top_talkers'):
self.size = size
self.redis_table = 'top_talkers'
self.client = StrictRedis(host=redis_host, port=redis_port)
self.is_saturated = self.decide_is_saturated()
def decide_is_saturated(self):
return self.client.zcard(self.redis_table) >= self.size
def clear(self):
self.client.zremrangebyrank(self.redis_table, 0, -1)
self.is_saturated = self.decide_is_saturated()
def is_full(self):
if self.is_saturated:
return True
self.is_saturated = self.decide_is_saturated()
return self.is_saturated
def get(self, key):
count = self.client.zscore(self.redis_table, key)
if count is None:
return count
return int(count)
def contains(self, key):
count = self.client.zscore(self.redis_table, key)
return count is not None
def add(self, key):
# If it's already in there, increment its count and we're done.
count = self.client.zscore(self.redis_table, key)
if count is not None:
self.client.zincrby(self.redis_table, key, 1)
return
# Else if the key is new to us but we're full, pop the lowest key/count
# pair and insert the new key as count + 1.
if self.is_full():
keys_counts = self.client.zrange(
self.redis_table, 0, 0, withscores=True, score_cast_func=int)
old_count = keys_counts[0][1]
self.client.zremrangebyrank(self.redis_table, 0, 0)
new_count = old_count + 1
self.client.zadd(self.redis_table, new_count, key)
return
# Or if the key is new to us and we have space, just insert it.
self.client.zadd(self.redis_table, 1, key)
def top_n_keys(self, n):
return self.client.zrevrange(
self.redis_table, 0, n - 1, score_cast_func=int)
def top_n_keys_counts(self, n):
keys_counts = self.client.zrevrange(
self.redis_table, 0, n - 1, withscores=True, score_cast_func=int)
if self.is_full():
lowest_keys_counts = self.client.zrange(
self.redis_table, 0, 0, withscores=True, score_cast_func=int)
the_min = lowest_keys_counts[0][1] - 1
else:
the_min = 0
return map(lambda (key, count): (key, count - the_min), keys_counts)
示例8: Leaderboard
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
#.........这里部分代码省略.........
def _leaders_with_ranks(self, key, offset, end):
total = self.r.zcard(key)
l = self._range(key, offset, end)
if self.timed_ties:
l = [((m,) + self._decode_value_with_time(s)) for (m, s) in l]
else:
l = [(m, s, 0) for (m, s) in l]
log.info(l)
with_ranks = self._add_ranks(l, offset)
return total, with_ranks
def set_metric(self, user, value, ts=None):
"""
Set a new peak value for this user, e.g. high score
"""
if self.timed_ties:
value = self._encode_value_with_time(value, ts)
for r in self.ranges:
key = self._board_key(r)
self.r.zadd(key, value, user)
if r != self.RANGE_ALLTIME:
self.r.expire(key, r.expiration)
def inc_metric(self, user, value, ts=None):
"""
Increment the current value for this user, e.g. total earned
"""
if ts:
log.warn('inc_metric: timestamps not supported yet')
for r in self.ranges:
key = self._board_key(r)
self.r.zincrby(key, user, value)
if r != self.RANGE_ALLTIME:
self.r.expire(key, r.expiration)
def leaders(self, range, limit=-1, offset=0, id=None, slots_ago=0):
"""
Retrieve a list of global leaders.
:param range: The TimeRange to query
:param limit: Maximum number of entries to return
:param offset: Rank to start at, ignored if id is provided
:param id: Member to center the range of entries around, i.e. "leaders near me"
:param slots_ago: number of time slots prior, e.g. 1 for yesterday, last week, etc.
"""
key = self._board_key(range, slots_ago)
if id:
if self.reverse:
rank = self.r.zrevrank(key, id)
else:
rank = self.r.zrank(key, id)
log.debug('uid: %r, rank: %r', id, rank)
if rank is None:
log.warn('specified id %r not found in board %r', id, key)
rank = 0
offset = max(0, rank - int(round(limit / 2.0)) + 1)
end = rank + limit / 2 if limit > 0 else -1
else:
end = offset + limit - 1 if limit > 0 else -1
total, with_ranks = self._leaders_with_ranks(key, offset, end)
start, end = range.date_range(slots_ago)
return Leaders(total, start, end, with_ranks)
示例9: Graph
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
#.........这里部分代码省略.........
>>> e = g.add_edge(john, mary, rel='friends', weight=20)
:param parent: edge start node (rgr.Node or node ID)
:param child: edge end node (rgr.Node or node ID)
:param kwargs: edge properties to initialize
:return: rgr.Edge representing the edge you just created.
"""
new_eid = self.redis.get(self.next_eid_key)
new_edge = Edge(self, new_eid)
self.redis.sadd(self.edges_key, new_eid)
for k in kwargs:
new_edge.prop.__setattr__(k, kwargs[k])
self._index(new_edge.name, k, kwargs[k])
if type(parent) is Node:
parent = parent.id
else:
parent = str(parent)
if type(child) is Node:
child = child.id
else:
child = str(child)
if not self.redis.sismember(self.nodes_key, parent):
raise ValueError(parent)
if not self.redis.sismember(self.nodes_key, child):
raise ValueError(child)
self.redis.set('{}:e:{}:in'.format(self.name, new_eid), parent)
self.redis.set('{}:e:{}:on'.format(self.name, new_eid), child)
self.redis.sadd('{}:n:{}:oe'.format(self.name, parent), new_eid)
self.redis.sadd('{}:n:{}:ie'.format(self.name, child), new_eid)
#parents / children: zset (sorted set)
# where: weight is number of children.
# zincrby() will add a member with weight 1 to set if it doesn't exist,
# and will increment the currently existing value if it does.
# zincrby(name, value, amount=1)
self.redis.zincrby('{}:n:{}:cn'.format(self.name, parent), child)
self.redis.zincrby('{}:n:{}:pn'.format(self.name, child), parent)
self.redis.incr(self.next_eid_key)
return new_edge
def del_node(self, node):
"""
Delete a node from the graph. Adjacent edges are also deleted.
Usage::
>>> g.del_node(n) #with Node() object
>>> g.del_node(254) #by node id
>>> del n #you should probably do this too
:param node: rgr.Node or node ID to delete.
"""
if type(node) is Node:
node_obj = node
node = node.id
else:
node = str(node)
node_obj = Node(self, node)
if not self.redis.sismember(self.nodes_key, node):
raise ValueError(node)
in_edges = self.redis.smembers('{}:n:{}:ie'.format(self.name, node))
out_edges = self.redis.smembers('{}:n:{}:oe'.format(self.name, node))
for e in in_edges | out_edges:
self.del_edge(e)
props = node_obj.properties()
for p in props.keys():
self._deindex(node_obj.name, p, props[p])
self.redis.delete('{}:n:{}:p'.format(self.name, node)) #might be unnecessary
示例10: print
# 需要导入模块: from redis import StrictRedis [as 别名]
# 或者: from redis.StrictRedis import zincrby [as 别名]
print("Terminating due to SIGTERM")
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGTERM, sigterm_handler)
r = False
while not r:
try:
r = StrictRedis(host="hotlist-redis", port=6379, db=0)
except:
print("Waiting for redis...")
time.sleep(2)
last_time = None
try:
while True:
timestamp = time.time()
urls = r.zrangebyscore(KEY, 0, timestamp)
if type(urls) == list and len(urls) > 0:
for url_key in urls:
r.zincrby(URL_HOTLIST_KEY, url_key, -1.0)
num = r.zremrangebyscore(URL_HOTLIST_KEY, 0, 0.0)
print("Removed %d old URL entries from %s" % (num, URL_HOTLIST_KEY))
num = r.zremrangebyscore(KEY, 0, timestamp)
print("Removed %d old URL scores from %s" % (num, KEY))
time.sleep(INTERVAL_SECONDS)
finally:
print("Exiting")