Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Document.simplify() method #31

Open
OldhamMade opened this issue Jan 7, 2011 · 7 comments
Open

Document.simplify() method #31

OldhamMade opened this issue Jan 7, 2011 · 7 comments

Comments

@OldhamMade
Copy link

It would be useful if there was a .simplify() method on Document objects, which would recursively convert the object to the base data-types as defined in the document's structure, eg:

class Foo(Document):
    structure = {
      'title': unicode,
      'properties': {unicode:None}
     }

x = Foo()
x['title'] = u'my document'
x['properties'] = {u'active': True}
x.save()

print type(x) # mongokit.Document
print type(x['_id']) # pymongo.objectid.ObjectId
print type(x['properties']) # mongokit.helpers.DotedDict

y = x.simplify()
print type(x) # dict
print type(x['_id']) # unicode
print type(x['properties']) # dict
@namlook
Copy link
Owner

namlook commented Jan 7, 2011

DotedDict is an instance of dict. This shouldn't hurt. Do you have a usecase for this feature ?

@OldhamMade
Copy link
Author

When using a recursive function to turn the Document into a different type of output (in my case, XML), working with simple python data-types is much easier.

Thought of a quick work-around:

simple = json.loads(foo.to_json())

Not sure whether this will be faster than converting to python directly; I believe mongokit converts to simple python data-types before converting to json, so in this case I'm converting mongokit->python->json->python.

@namlook
Copy link
Owner

namlook commented Jan 7, 2011

DotedDict is instanciated when use_dot_notation is True otherwise, it is plain dict. If you really need performance, I suggest you to turn it off for the type of document. Otherwise, the json trick should be enough...

@OldhamMade
Copy link
Author

It's not just dicts causing problems; the ObjectId object is problemattic too. I'll use the json.loads(x.to_json()) method for now, but if the functionality is already in mongokit adding a direct method for it could be useful. :)

@namlook
Copy link
Owner

namlook commented Jan 7, 2011

ok I see. If I'd understood well, you'd like a serialize method to convert all mongo (ObjectId) and python types (datetime) into a understandable plain text without loosing the python dict structure. Am I right ?

@OldhamMade
Copy link
Author

Sort-of. I need to convert a document (mongokit.Document), all it's nested documents, and all pymongo (ObjectId) and mongokit objects (mongokit.helpers.DotedDict for example) into a nested dict of basic python types. json.loads(document.to_json()) would do this, but I'm a little concerned about performance; if mongo is simplifying the structure before it converts it to json output then making that public would be faster than converting to a json string and re-importing.

@namlook
Copy link
Owner

namlook commented Jan 7, 2011

Ok, it could be added without complicated the API. If you want, you can send a patch, I'll add it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants