Skip to content

Support for inserting and retrieving picklable objects in Python #241

Closed

Description

@alexbw

Currently, attempting to save a NumPy array as value in a dictionary in the Python driver is not supported. Try:

import numpy
r.table('users').insert({'name': 'Slava', 'age': 29,   
'some_2d_array':numpy.random.random((100,100)) }).run()

will return the error:
AttributeError: 'numpy.ndarray' object has no attribute 'iteritems'

NumPy arrays, along with many built-in Python types, can pickle themselves into compact binary representations using

import cPickle
binary_string = cPickle.dumps(numpy.random.random((100,100)))
r.table('users').insert({'name': 'Slava', 'age': 29,   
'some_2d_array':binary_string }).run()
doc = r.table('users').filter(lambda doc: doc['name'] == 'Slava').run()
retrieved_array = cPickle.loads(str(doc['some_2d_array']))

It would be very desirable to be able to work with objects that can pickle themselves without having to add wrapper code, and seems like an achievable feature.

Activity

  1. al3xandru commented on Jan 18, 2013

    @al3xandru
    Contributor

    IMO such extensions fit better into higher level libraries and not directly in the driver. Some users may want to explicitly serialize custom objects to JSON, or pickle, or other protocols.

  2. coffeemug commented on Jan 18, 2013

    @coffeemug
    Contributor

    We had a proposal for extending json with additional types which will eventually be implemented. The question then, is which python types would pickle, which ones would be coerced to native rethinkdb types, etc.

    I'm moving to post-protobuf-improvements, let's discuss this when 1.4 is out. (I think doing something about this after 1.4 is a bit premature, but it'll become more clear then).

  3. al3xandru commented on Jan 18, 2013

    @al3xandru
    Contributor

    A different approach that could be very extensible is to allow drivers to register serializers/deserializers (in the way let's say Python's json/simplejson does it).

  4. alexbw commented on Jan 18, 2013

    @alexbw
    Author

    This is the extension style that MongoDB provides.
    http://api.mongodb.org/python/current/examples/custom_type.html
    You write a custom serializer/deserializer and register it with a database. I've used that in the past, and once set-up, works quite well.

  5. al3xandru commented on Jan 21, 2013

    @al3xandru
    Contributor

    @alexbw ha... and I thought my idea was unique 😉

  6. alexbw commented on Jun 13, 2013

    @alexbw
    Author

    Any thoughts on this issue? Would be of enormous use to many people.

  7. coffeemug commented on Jun 14, 2013

    @coffeemug
    Contributor

    I'll write up a concrete proposal for this shortly.

  8. coffeemug commented on Jun 14, 2013

    @coffeemug
    Contributor

    Ok, I've opened a concrete proposal in #1016. I'm closing this issue, please follow #1016 for updates.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions