Skip to content

Instantly share code, notes, and snippets.

@swdevbali
Created November 6, 2015 15:19
Show Gist options
  • Save swdevbali/4784eda924affad5d3cf to your computer and use it in GitHub Desktop.
Save swdevbali/4784eda924affad5d3cf to your computer and use it in GitHub Desktop.
Simply json.dumps() => save to redis => redis.get => json.loads() => Python dict with proper data type created
__author__ = 'ekowibowo'
import json
import os
import redis
REDIS_HOST = os.getenv('SS_ABTEST_REDIS_HOST', '192.168.59.103')
r = redis.StrictRedis(host=REDIS_HOST)
for k in r.keys('abtest:experiments:*'):
r.delete(k)
exp1 = {
"id": 1,
"name": "testExperiment",
"device": "desktop",
"active": False,
"buckets": [
{
"name": "bucket1"
},
{
"name": "bucket2"
}
]
}
exp1_json_dumps = json.dumps(exp1)
print 'json.dumps:', exp1_json_dumps, ' type=', type(exp1_json_dumps)
r.set('abtest:experiments:1', exp1_json_dumps)
exp1_from_redis = r.get('abtest:experiments:1')
print 'exp1_from_redis', exp1_from_redis, ' type=', type(exp1_from_redis)
exp1_from_redis_json_loads = json.loads(exp1_json_dumps)
print 'exp1_from_redis_json_loads', exp1_from_redis_json_loads, 'type=', type(exp1_from_redis_json_loads)
@cjgarciaj
Copy link

YOUR WROOOOONG!!!!

Sorry, @swdevbali if this was too rush but, isn't what you say are going to do.

I've found this because is the first Google's suggestion.
And guess what! You never load the redis get.
Your just reloading the same json dump, not the redis one.
json.loads(exp1_json_dumps)
you should use
json.loads(exp1_from_redis)

This is your code:

exp1_json_dumps = json.dumps(exp1)
print 'json.dumps:', exp1_json_dumps, ' type=', type(exp1_json_dumps)
r.set('abtest:experiments:1', exp1_json_dumps)
exp1_from_redis = r.get('abtest:experiments:1')
print 'exp1_from_redis', exp1_from_redis, ' type=', type(exp1_from_redis)
exp1_from_redis_json_loads = json.loads(exp1_json_dumps)

But, flash news! It won't work!
Anyway, I'm still stuck at this and will continue with my research.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment