return iter(x.items()) AttributeError:'int' object has no attribute'items' (python inserts an ordered set into the redis database)

There is a problem when inserting an ordered collection into redis using python:

def add(proxy, score=10):
    if not db.zscore(REDIS_KEY, proxy):
        return db.zadd(REDIS_KEY, score, proxy)

Insert picture description here
I don’t know why the code on the Internet is what I wrote above, but it may be due to version reasons, this method is no longer available, but since it prompts where the problem is, we simply look at the source code:
Insert picture description here
Obviously, this The method accepts two required parameters, name is the key name of the ordered set, mapping should be the dictionary form of the value and weight to be inserted, instead of passing in two parameters separately, and then looking down:
Insert picture description here
here execute the iteritems method to pass in After mapping, add the first two zadd key score1 value1 score2 value2...items of the return value to the new list, and then execute the insert statement, reminiscent of the redis insert statement , then the first item in the return list here should be the value, and the second item is the weight (note his order here) Adjustment), continue to look at the iteritems method: it
Insert picture description here
really is! Here is the key-value pair tuple of the passed-in dictionary is returned, so the correct insertion code should look like this:

def add(proxy, score=10):
    if not db.zscore(REDIS_KEY, proxy):
        return db.zadd(REDIS_KEY, {
    
    proxy: score})

Check if the insertion is successful:
Insert picture description here
perfect!

Guess you like

Origin blog.csdn.net/zzh2910/article/details/89603192