1

I'm using Redis (phpredis) to share data between my Laravel and my Python app.

Within Laravel, I save on a specific channel my array:

$data = MyModel::where('start_date','>', "2018-05-02 10:00:00")->get();

// $data = [{'id':1, 'start_date': "2018-05-03 12:00:00", 'name': "}…]

Redis::set("barChannel", json_encode($data));

Within Python, I read this data:

import redis
import json

myredis = redis.Redis()

data = json.loads(myredis.get('barChannel'));
// data = [{'id':1, 'start_date': "2018-05-03 12:00:00", 'name': "}…]

So basically the data passes successfully but instead of having directly a datetime value in my python list, I have a string which I have to convert. With the above example I can use then datetime.datetime.strptime but in the case of more complicated data structure (nested lists of dicts)... it's quite a pain to convert each variable.

Is there a way to store datetimes in Redis which would be natively recognized in Python?

Sebastien D
  • 4,369
  • 4
  • 18
  • 46

2 Answers2

1

No there is not, after little research you would know, that Redis stores its values in string representation only, thus your only option is to store serialized data and unserialize when needed.

Start with following:

Kyslik
  • 8,217
  • 5
  • 54
  • 87
0

An update on the issue faced above. Two excellent posts about the use of the json module solved my problem of importing deeply nested datetimes from redis (and exporting them back):

How to convert to a Python datetime object with JSON.loads? (IMPORT)

def date_hook(json_dict):
    for (key, value) in json_dict.items():
        try:
            json_dict[key] = datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
        except:
            pass
    return json_dict

data = json.loads(myredis.get('barChannel'), object_hook=date_hook)

How to overcome "datetime.datetime not JSON serializable"? (EXPORT)

def json_serial(obj):
    """JSON serializer for objects not serializable by default json code"""

    if isinstance(obj, (datetime, date)):
        return obj.strftime('%Y-%m-%d %H:%M:%S')
    raise TypeError ("Type %s not serializable" % type(obj))

json_values = json.dumps(data, default=json_serial, separators=(',', ':'))
myredis.publish('barChannel', json_values);
Sebastien D
  • 4,369
  • 4
  • 18
  • 46