Repository navigation
Create a JSON decoder subclass with a hook for pseudo types. - #4585
Conversation
|
Very cool. Thanks for the pull request @grandquista! |
|
@danielmewes --should this be in a milestone? |
|
2.1.x I think once we have that. Putting in 2.1-polish for now. |
|
Tangentially related: #1016 |
|
They are similar, but #1016 would provide a more convenient interface for specifying individual additional types, that would also work across drivers (not just on Python). I think the main work for #1016 would be on the client, but the server might need some minor additions as well to detect the fact that a certain object is actually a user-defined pseudo type. |
|
I'm getting "Unexpected response received" on many (if not all) queries: Python 2.7.3 (default, Jun 22 2015, 19:33:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rethinkdb as r
>>> r.connect().repl()
<rethinkdb.net.DefaultConnection object at 0x7f12a1b75a10>
>>> r.expr(1).run()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rethinkdb/ast.py", line 118, in run
return c._start(self, **global_optargs)
File "rethinkdb/net.py", line 565, in _start
return self._instance.run_query(q, global_optargs.get('noreply', False))
File "rethinkdb/net.py", line 426, in run_query
res = self._read_response(query.token)
File "rethinkdb/net.py", line 475, in _read_response
raise RqlDriverError("Unexpected response received.")
rethinkdb.errors.RqlDriverError: Unexpected response received.
>>>Any ideas @grandquista ? (Edit: Replaced example by a simpler query) |
There was a problem hiding this comment.
This code needs to handle the case where the result is not for a cursor (e.g. r.expr(1)).
There was a problem hiding this comment.
I have a patch to handle the first call to _read_response.
|
Never mind, found the problem I think (I added a comment in the code). |
|
I hacked in a quick fix, and ran the benchmark from #4569 (comment) . @Tryneus I think this might be next after migration? |
There was a problem hiding this comment.
This should be convert_time right? At least it's called by that name in line 728
There was a problem hiding this comment.
Yes, that would be consistent with how I was naming the other convert methods.
|
Actually my measurements were with a slightly modified version of the benchmark from #4569 (comment) that used numeric timestamps and not ReQL time objects in the table. With real time objects, your patch is still much faster though: ~18 seconds instead of ~25 seconds. |
|
I'm excited, this is potentially fixes several longstanding issues. Nice work! (I've looked over the patch as well, and the only things I noticed Daniel already mentioned) |
The cursor (if needed) gets constructed after the first call to `_read_response`.
|
Ok, finally got around to this. Looks good for the most part, although it doesn't change the other |
Create a JSON decoder subclass with a hook for pseudo types.
|
Thanks for all the help, @grandquista, merged! |
|
@Tryneus once you are done transferring this to the other connection backends, please also backport this to |
|
The changes were applied to the |
|
For reference, here's an example for using UltraJSON in the driver (https://github.com/esnme/ultrajson): import rethinkdb as r
import ujson
class UjsonDecoder(object):
def __init__(self, reql_format_opts={}):
pass
def decode(self, s):
return ujson.loads(s)
def create_decoder(format_opts):
return UjsonDecoder(format_opts)
conn = r.connect()
conn._get_json_decoder = create_decoder
# `conn` now uses UltraJSON to parse server responsesNote that UltraJSON doesn't currently allow registering a hook for processing pseudo types. So RethinkDB pseudo types (binary data, date/time values) will not be converted into native Python types in this case. |
|
The change was included in the latest Python driver release, "2.1.0.post1" |
|
@danielmewes Seems like an odd API to override a private method on the connection, could it be exposed as a public API? |
|
@sontek -- I think we should use the API you proposed originally: # Specify default encoder/decoder on the connection
r.connect(..., json_encoder=ujon.loads, json_decoder=ujson.dumps)
# Override for a particular query
query.run(..., json_encoder=jon.loads, json_decoder=json.dumps)We didn't add it yet because the existing version is pretty simple, and we wanted to get it out fast. I can open a new issue for this if people think the API proposed above is reasonable. |
|
For reference: @grandquista put an implementation of the simplified API up as a pull request: #4825 We're planning to include that with RethinkDB 2.2. |
There was a problem hiding this comment.
This is making a mutable global object at import time. This should be something like this instead:
def __init__(self, reql_format_opts=None):
self.reql_form_opts = reql_format_opts or {}There was a problem hiding this comment.
>>> from rethinkdb.ast import ReQLDecoder
>>> one = ReQLDecoder()
>>> two = ReQLDecoder()
>>> one.reql_format_opts['boom'] = 'shaka'
>>> one.reql_format_opts
{'boom': 'shaka'}
>>> two.reql_format_opts
{'boom': 'shaka'}There was a problem hiding this comment.
Oh good catch! Do you want to open a pull request? (otherwise I'm just going to copy over your patch, if that's ok).
|
I was able to shave off 50% of my time using this API: |
Create a JSON decoder subclass with a hook for pseudo types.
This is my proposed solution for #3506 and #4569