Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pythonic way deal with DataError: Invalid input of type: 'dict'. Convert to a bytes, string, int or float first.?

redis veersion 3.4.1 must be use hash, can't use str or other data type data:

{'_anno': {
    'ctp': 'list',
    'dt': [],
    'ml': 0,
    'na': 'apple',
    'pos': -1,
    'rel': '',
    'st_var': '',
    'tp': 'object'},
'_att': {'_cuser': 'apple card',
         '_last_editor': 'apple card',
         '_protext': 'authorize',
         '_status': 'normal',
         '_theme_id': 'apple card',
         '_view': '12'},
    }

my code

pool = redis.ConnectionPool(host=host, port=port)
conn = redis.StrictRedis(connection_pool=pool)

conn.hmset("aaaaaa",data)

raise error

DataError: Invalid input of type: 'dict'. Convert to a bytes, string, int or float first.

now code

pool = redis.ConnectionPool(host=host, port=port)
conn = redis.StrictRedis(connection_pool=pool)
new_data={}
for key,value in data.items():
    new_data[key]=json.dumps(value)
conn.hmset("aaaaaa",new_data)

Is there a more pythonic way?

like image 690
xin.chen Avatar asked Nov 06 '22 09:11

xin.chen


1 Answers

The solution for you problem is to use hexdigest() or digest() to convert your dictionary, you can use that:

hashlib.sha256(mdp.encode()).hexdigest()
like image 170
StarGit Avatar answered Nov 15 '22 15:11

StarGit