Pydgraph (feature request): Automatically parse datetime strings

I think it would be a useful feature to automatically parse dgraph’s datetime strings into native python datetime objects. Other DB modules (e.g. SQL, MongoDB) also automatically convert datetimes.

Hi, I’m the maintainer of PyDgraph. Could you help out by putting in a pull request? I can help you with the pull request but my plate is quite full now to actually add features.

Hi,

I browsed the code, and noticed that the client object is not aware of the schema. So I guess, the first step would be have the client retrieve the schema from dgraph, then create an attribute that contains a list of all predicates with type datetime. Only then the client “knows” where to look for fields that need parsing. So if there is a query, then the txn object needs to iterate over the response and search for all keys in the datetime list.

So, I can try enhancing the DgraphClient class first?

excellent!

cool, I think I’ll get around it in the next two weeks or so

1 Like

I looked deeper in the issue and realized that result object (res) from client.txn(read_only=True).query(query) just contains a raw byte string as attribute res.json. This is the translated into a native Python object via the built-in json module (e.g. data = json.loads(res.json).

Therefore, I came to the conclusion that it is much simpler to add a custom object_hook at the json-decoder stage.

A pull request with an example and some documentation will follow in the next days.

1 Like

Ah cool, so PyDgraph itself is not affected. I like the process.