Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedDict turns to normal dict when document is loaded from database #76

Open
M4rtinK opened this issue May 13, 2017 · 1 comment
Open

Comments

@M4rtinK
Copy link
Contributor

M4rtinK commented May 13, 2017

Looks like Document instance attributes that hold ordered dicts turn to normal dicts when the document is reatrieved from the database, short reproducer:

#!/usr/bin/python3

from collections import OrderedDict
import tempfile
import blitzdb

with tempfile.TemporaryDirectory() as temp_dir_name:
    db = blitzdb.FileBackend(temp_dir_name)
    original_document = blitzdb.Document()
    original_document.od = OrderedDict()
    original_document.od["foo"] = 1
    original_document.od["bar"] = 2
    original_document.od["baz"] = 3

    print("original document")
    print(original_document.od)
    print(isinstance(original_document.od, OrderedDict))

    original_document.save(db)
    db.commit()

    loaded_document = db.get(blitzdb.Document, {})

    print("loaded document")
    print(loaded_document.od)
    print(isinstance(loaded_document.od, OrderedDict))
@adewes
Copy link
Owner

adewes commented May 14, 2017

Unfortunately this is not supported by BlitzDB, so if you want to retain the order of your keys you should store the items in the document instead and convert it back to an ordered dict after retrieving the object from the database (you could write a custom Document type that could do that for you automatically).

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

No branches or pull requests

2 participants