Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm currently investigating if it is possible to use structured numpy arrays more or less directly as documents for mongodb insert operations.

In all examples I have found

db.collection.insert(doc)

doc is always a Python dict, but I wonder if not any instance that provides the mapping interface might be usable for insert operations.

I was thinking to subclass np.ndarray using DictMixin or MutableMapping so it really provides a dict interface. And then do something like this:

structured_array = np.zeros( (5,), dtype=[('i', '<i4'), ('f', '<f4')] )
structured_array['i'] = np.random.randint(42, size=5)
structured_array['f'] = np.random.rand(5)

for row in structured_array:
    # row is of type: np.void
    # so in order to let pymongo insert it into the DB, I create a 
    # view of row, which provides the dict-like interface
    row_dict_like = row.view(np_array_subclass_providing_dict_interface)
    db.collection.insert(row_dict_like)

Now, since I am a bloody beginner and have never ever subclassed np.ndarray and fear I might dump many hours into this, just to learn later, that the whole approach was not very smart, my question is: Do you see major problems in this approach? Is it Pythonic? Is my assumption, that any class providing the mapping interface can be used for mongodb insert operations, correct at all?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.