Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for redis-py 3+ #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ highscore_lb.score_for('david')
### Ranking a member across multiple leaderboards

```python
highscore_lb.rank_member_across(['highscores', 'more_highscores'], 'david', 50000, { 'member_name': 'david' })
highscore_lb.rank_member_across(['highscores', 'more_highscores'], 'david', 50000, str({'member_name': 'david'}))
```

### Alternate leaderboard types
Expand Down
14 changes: 7 additions & 7 deletions leaderboard/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ def rank_member_in(
'''
pipeline = self.redis_connection.pipeline()
if isinstance(self.redis_connection, Redis):
pipeline.zadd(leaderboard_name, member, score)
pipeline.zadd(leaderboard_name, {member: score})
else:
pipeline.zadd(leaderboard_name, score, member)
pipeline.zadd(leaderboard_name, {score: member})
if member_data:
pipeline.hset(
self._member_data_key(leaderboard_name),
Expand All @@ -159,9 +159,9 @@ def rank_member_across(
pipeline = self.redis_connection.pipeline()
for leaderboard_name in leaderboards:
if isinstance(self.redis_connection, Redis):
pipeline.zadd(leaderboard_name, member, score)
pipeline.zadd(leaderboard_name, {member: score})
else:
pipeline.zadd(leaderboard_name, score, member)
pipeline.zadd(leaderboard_name, {score: member})
if member_data:
pipeline.hset(
self._member_data_key(leaderboard_name),
Expand Down Expand Up @@ -241,9 +241,9 @@ def rank_members_in(self, leaderboard_name, members_and_scores):
pipeline = self.redis_connection.pipeline()
for member, score in grouper(2, members_and_scores):
if isinstance(self.redis_connection, Redis):
pipeline.zadd(leaderboard_name, member, score)
pipeline.zadd(leaderboard_name, {member: score})
else:
pipeline.zadd(leaderboard_name, score, member)
pipeline.zadd(leaderboard_name, {score: member})
pipeline.execute()

def member_data_for(self, member):
Expand Down Expand Up @@ -546,7 +546,7 @@ def change_score_for_member_in(self, leaderboard_name, member, delta, member_dat
@param member_data [String] Optional member data.
'''
pipeline = self.redis_connection.pipeline()
pipeline.zincrby(leaderboard_name, member, delta)
pipeline.zincrby(leaderboard_name, delta, member)
if member_data:
pipeline.hset(
self._member_data_key(leaderboard_name),
Expand Down
21 changes: 9 additions & 12 deletions leaderboard/tie_ranking_leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def change_score_for_member_in(self, leaderboard_name, member, delta, member_dat

pipeline = self.redis_connection.pipeline()
if isinstance(self.redis_connection, Redis):
pipeline.zadd(leaderboard_name, member, new_score)
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name), str(float(new_score)), new_score)
pipeline.zadd(leaderboard_name, {member: new_score})
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name), {str(float(new_score)): new_score})
else:
pipeline.zadd(leaderboard_name, new_score, member)
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name), new_score, str(float(new_score)))
pipeline.zadd(leaderboard_name, {new_score: member})
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name), {new_score: str(float(new_score))})
if member_data:
pipeline.hset(
self._member_data_key(leaderboard_name),
Expand Down Expand Up @@ -92,16 +92,13 @@ def rank_member_in(

pipeline = self.redis_connection.pipeline()
if isinstance(self.redis_connection, Redis):
pipeline.zadd(leaderboard_name, member, score)
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name),
str(float(score)), score)
pipeline.zadd(leaderboard_name, {member: score})
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name), {str(float(score)): score})
else:
pipeline.zadd(leaderboard_name, score, member)
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name),
score, str(float(score)))
pipeline.zadd(leaderboard_name, {score: member})
pipeline.zadd(self._ties_leaderboard_key(leaderboard_name), {score: str(float(score))})
if can_delete_score:
pipeline.zrem(self._ties_leaderboard_key(leaderboard_name),
str(float(member_score)))
pipeline.zrem(self._ties_leaderboard_key(leaderboard_name), str(float(member_score)))
if member_data:
pipeline.hset(
self._member_data_key(leaderboard_name),
Expand Down
8 changes: 4 additions & 4 deletions test/leaderboard/competition_ranking_leaderboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def test_allow_you_to_include_or_exclude_missing_members_using_the_include_missi
def __rank_members_in_leaderboard(self, members_to_add=6):
for index in range(1, members_to_add):
self.leaderboard.rank_member(
'member_%s' %
index, index, {
'member_name': 'Leaderboard member %s' %
index})
'member_%s' % index,
index,
str({'member_name': 'Leaderboard member %s' % index})
)
13 changes: 6 additions & 7 deletions test/leaderboard/leaderboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def test_members_data_for(self):
def test_update_member_data(self):
self.__rank_members_in_leaderboard()
self.leaderboard.update_member_data(
'member_1', {
'member_name': 'Updated Leaderboard member 1'})
'member_1', str({'member_name': 'Updated Leaderboard member 1'}))
self.leaderboard.member_data_for('member_1').should.eql(
str({'member_name': 'Updated Leaderboard member 1'}))

Expand Down Expand Up @@ -498,7 +497,7 @@ def test_rank_members(self):

def test_rank_member_across(self):
self.leaderboard.rank_member_across(
['highscores', 'more_highscores'], 'david', 50000, {'member_name': 'david'})
['highscores', 'more_highscores'], 'david', 50000, str({'member_name': 'david'}))
len(self.leaderboard.leaders_in('highscores', 1)).should.equal(1)
len(self.leaderboard.leaders_in('more_highscores', 1)).should.equal(1)

Expand Down Expand Up @@ -692,7 +691,7 @@ def test_ranked_in_list_with_include_missing_sort_by_score_and_negative_and_zero
def __rank_members_in_leaderboard(self, members_to_add=6):
for index in range(1, members_to_add):
self.leaderboard.rank_member(
'member_%s' %
index, index, {
'member_name': 'Leaderboard member %s' %
index})
'member_%s' % index,
index,
str({'member_name': 'Leaderboard member %s' % index})
)
8 changes: 4 additions & 4 deletions test/leaderboard/reverse_tie_ranking_leaderboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_it_should_output_the_correct_rank_when_initial_score_is_0_and_then_late
def __rank_members_in_leaderboard(self, members_to_add=6):
for index in range(1, members_to_add):
self.leaderboard.rank_member(
'member_%s' %
index, index, {
'member_name': 'Leaderboard member %s' %
index})
'member_%s' % index,
index,
str({'member_name': 'Leaderboard member %s' % index})
)
8 changes: 4 additions & 4 deletions test/leaderboard/tie_ranking_leaderboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_it_should_correctly_pop_ties_namespace_from_options(self):
def __rank_members_in_leaderboard(self, members_to_add=6):
for index in range(1, members_to_add):
self.leaderboard.rank_member(
'member_%s' %
index, index, {
'member_name': 'Leaderboard member %s' %
index})
'member_%s' % index,
index,
str({'member_name': 'Leaderboard member %s' % index})
)