jaygrth
jaygrth

Reputation: 31

Python Syntax / REDIS

When using the built in redis-cli for REDIS on Linux, I can enter the following command with no issue

redis-cli -p 6379 mset SWSUIT "{\"D0:\": {\"install\": {\"rules\": [[0.62, -0.34], [-0.00, 4.95], [0.00, 0.00, 0.18]], \"name\": \"DO\", \"slows\": [[[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"type\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]]], \"suit\": [0, 1, 1, 1], \"test\": [1.0, 1.0, 1.0, 1.0]}, \"pal\": [100.0, 100.0, 100.0, 100.0], \"alto\": [0.0, 0.0, 0.0, 0.0]}}"

    r.mset('SWSUIT', '"{\"D0:\": {\"install\": {\"rules\": [[0.62, -0.34], [-0.00, 4.95], [0.00, 0.00, 0.18]], \"name\": \"DO\", \"slows\": [[[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"type\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]]], \"suit\": [0, 1, 1, 1], \"test\": [1.0, 1.0, 1.0, 1.0]}, \"pal\": [100.0, 100.0, 100.0, 100.0], \"alto\": [0.0, 0.0, 0.0, 0.0]}}"
')

When using redis-py in a python script to enter this value I cannot make it work. I keep getting an error saying I must insert a kwal or dictionary. I am assuming this is because python is interpreting the quotes and backslashes differently. Is there any way I can perform the command without changing the value? It must be formatted exactly as is seen above. I have also tried the os package to perform the command.

os.system('redis-cli -p 6379 mset SWSUIT "{\"D0:\": {\"install\": {\"rules\": [[0.62, -0.34], [-0.00, 4.95], [0.00, 0.00, 0.18]], \"name\": \"DO\", \"slows\": [[[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"type\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]]], \"suit\": [0, 1, 1, 1], \"test\": [1.0, 1.0, 1.0, 1.0]}, \"pal\": [100.0, 100.0, 100.0, 100.0], \"alto\": [0.0, 0.0, 0.0, 0.0]}}"

')

Which sort of works, but enters the value into REDIS incorrectly formatted

Thank you.

Upvotes: 2

Views: 334

Answers (1)

Tague Griffith
Tague Griffith

Reputation: 4173

This isn't about the formatting of the JSON string. The error you are getting back is indicating that you're passing the wrong arguments to the function. The mset call in redis-py expects either keyword arguments or a Python dictionary as input. The MSET command in Redis is for setting the value of multiple keys in a single action, so the Python API expects one of those two forms mapping Redis keys to their value. If you change your code to something like:

myDict = { 'SWSUIT': '"{\"D0:\": {\"install\": {\"rules\": [[0.62, -0.34], [-0.00, 4.95], [0.00, 0.00, 0.18]], \"name\": \"DO\", \"slows\": [[[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"type\": \"c\", \"gfx\": 0, \"N\": 4}]], [[{\"bit\": \"p\", \"order\": 1, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"p\", \"order\": 2, \"gfx\": 0, \"N\": 4}], [{\"bit\": \"c\", \"gfx\": 0, \"N\": 4}]]], \"suit\": [0, 1, 1, 1], \"test\": [1.0, 1.0, 1.0, 1.0]}, \"pal\": [100.0, 100.0, 100.0, 100.0], \"alto\": [0.0, 0.0, 0.0, 0.0]}}"' }
r.mset(myDict)

You'll get the results you want. Given that you're only trying to set a single data type, the set method would be a better choice.

Upvotes: 1

Related Questions