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

Already on GitHub? Sign in to your account

added __exit__ and __enter__ to EJDB obj to use inside with constructs. #4

Merged
merged 4 commits into from Jan 23, 2014

Conversation

Projects
None yet
2 participants
Contributor

deddu commented Jan 22, 2014

for the ones that are used to query mongodb inside with constructs
and miss to automatically close the DB.
(and to mimic more closely Mongo usage)

example:

import pyejdb
def create(entries, collection='mycoll', dbname='mydb'):
    '''
    creates a new instance of ejdb with the content of a json file or with the list of dictionaries passed in entries.
    '''
    import  json
    with pyejdb.EJDB(dbname, pyejdb.DEFAULT_OPEN_MODE | pyejdb.JBOTRUNC) as db:
        if isinstance(entries,str):
            with open(entries,'r') as f:
                    entries = json.load(f)

        for addme in entries:
            db.save(collection,addme)

if __name__=="__main__":
    import sys
    create(sys.argv[1])

adamansky added a commit that referenced this pull request Jan 23, 2014

Merge pull request #4 from deddu/master
added __exit__ and __enter__ to EJDB obj to use inside with constructs.

@adamansky adamansky merged commit f0dc208 into Softmotions:master Jan 23, 2014

Owner

adamansky commented Jan 23, 2014

Thank you!

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