0

When using redis sorted set, I want to make class methods more readable. In redis-py, especially in sorted set, push and update operation work same. For example,

class A(object):
    def push(self, key, value, score):
        return redis.zadd(key, {value: score})

    def update(self, key, value, score):
        return self.push(key, value, score)

if __name__ == 'main':
    a = A()
    # push item1 in redis sorted set
    a.push('sorted_set', 'item1', 1)

    # update item1 in redis sorted set to score 2
    # but I also know that this is same with
    # a.push('sorted_set', 'item1', 2)
    a.update('sorted_set', 'item1', 2)

However, I wondering that there is better way to solve this issue. Please let me know.

sogo
  • 351
  • 5
  • 20

1 Answers1

1

I have never seen this use, so this might not be "recommended", but technically you can do this.

class A(object):
    def push(self, key, value, score):
        return redis.zadd(key, {value: score})

    update = push

Also see this.

T Tse
  • 786
  • 7
  • 19
  • Thanks for sharing. I met function re-naming and backward comparability for the first time as a keyword. – sogo Apr 17 '19 at 10:30