Python / MongoDB interface

43 views
Skip to first unread message

Peter O'Malley

unread,
Jan 16, 2014, 1:08:25 PM1/16/14
to prudence-...@googlegroups.com
Hi again,

I'm trying to expand upon the tutorial to get familiar with Prudence. Is there a built-in python / mongodb interface, the way there is for javascript (I mean that sincerity has javascript mongo package)? Or should I just use pymongo? In that case, do I need to store my database settings in app.gobals rather than app.settings.mongoDb as for the javascript driver?

Thanks,
Peter

Tal Liron

unread,
Jan 16, 2014, 1:35:11 PM1/16/14
to prudence-...@googlegroups.com
You will indeed need to use pymongo, which is very good, but does not inherently recognize app.settings.

(The JavaScript MongoDB driver was designed specifically with support for Sincerity and Prudence, so it has that convenience built in.)

But it's easy to write your own small wrapper to use the same settings used by the JavaScript driver, if you want to conform:

import pymongo

def get_mongo(application):
  mongo = application.globals['mongoDb.defaultClient']
  if mongo is None:
    uris = application.globals['mongoDb.defaultUris'] or 'localhost:27017'
    mongo = application.getGlobal('mongoDb.defaultClient', pymongo.MongoClient(uris))
  return mongo

def get_default_db(application):
  return getattr(get_mongo(application), application.globals['mongoDb.defaultDb'])

You can use them like so:

get_default_db(application).my_collection.find_one(...)

Note the important use of the "getGlobal" API, which guarantees that the MongoClient instance will be unique. Read more about the reason for it here:

http://threecrickets.com/prudence/manual/programming/#state-and-scope_concurrency
--
You received this message because you are subscribed to the Google Groups "Prudence Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prudence-commun...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Peter O'Malley

unread,
Jan 16, 2014, 2:58:20 PM1/16/14
to prudence-...@googlegroups.com
OK, thanks again for the quick response. This is more or less what I
had figured I needed to do, except for the part about stashing the
client in the globals.

In general, how much of the javascript API is available from python?
(For example, looking at the documentation here:
http://threecrickets.com/api/javascript/?namespace=application)
> You received this message because you are subscribed to a topic in the
> Google Groups "Prudence Community" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/prudence-community/Gc2HGXOKkq8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Tal Liron

unread,
Jan 16, 2014, 11:35:12 PM1/16/14
to prudence-...@googlegroups.com
The API overview divides them into groups, so you'll see exactly what belongs only to JavaScript:

http://threecrickets.com/prudence/manual/programming/#apis

Peter O'Malley

unread,
Jan 18, 2014, 11:32:42 AM1/18/14
to prudence-community
OK thanks. I think I hadn't quite made it to that part of the manual
yet but it makes sense now.

But I have other questions re: Python and Prudence/Scripturian:
1) Would it be possible to get better error/exception output on the
Prudence debug page? For example, I have /resouces/api.m.py which has
"import mongo" which is /libraries/mongo.py. If I have an error in
mongo.py the only thing I get in the debug page is a Scripturian
Execution Error for api.m.py, marked at the line number of (one after)
the "import mongo" line. It would be pretty helpful to get the line
number in the imported files.

2) I don't quite understand how the Prudence/Scripturian updating for
modified files works. If I modify api.m.py it picks up the changes,
but seemingly not if I modify the imported mongo.py. When I restart
prudence then it picks up the changes to mongo.py.
...except that sometimes it does pick up the changes. So, basically,
this is the most useless question ever, but is there anything you're
aware of that would cause this? I can also try to track down this
issue more specifically if that would help.

Thanks again,
Peter

Tal Liron

unread,
Jan 18, 2014, 12:03:49 PM1/18/14
to prudence-...@googlegroups.com
These are good questions.


On 01/19/2014 12:32 AM, Peter O'Malley wrote:
1) Would it be possible to get better error/exception output on the
Prudence debug page? For example, I have /resouces/api.m.py which has
"import mongo" which is /libraries/mongo.py. If I have an error in
mongo.py the only thing I get in the debug page is a Scripturian
Execution Error for api.m.py, marked at the line number of (one after)
the "import mongo" line. It would be pretty helpful to get the line
number in the imported files.
This is part of ongoing work in Scripturian to properly extract the Jython exception information. Please open a bug about it (in Scripturian) and I'll see what I can do to fix it... The Jython code base is notoriously difficult to get through.

2) I don't quite understand how the Prudence/Scripturian updating for
modified files works. If I modify api.m.py it picks up the changes,
but seemingly not if I modify the imported mongo.py. When I restart
prudence then it picks up the changes to mongo.py.
...except that sometimes it does pick up the changes.
Scripturian creates a dependency tree per file, so that if any dependent files are changed, that file would be considered changed, too. However, it only knows about these dependencies if you use its API: document.require, for example.

However, you can also explicitly create a dependency link. Two APIs would be of use. You can use addDependency to add regular documents, which would have to exist in your Sincerity container in the usual places (/librararies/scripturian/):

http://threecrickets.com/api/javascript/?namespace=document&item=document.addDependency

That should probably fit what you need. However, you can also create a dependency on any file:

http://threecrickets.com/api/javascript/?namespace=document&item=document.addFileDependency

This would actually work with truly any file, not just source code files.

Peter O'Malley

unread,
Jan 18, 2014, 10:55:30 PM1/18/14
to prudence-community
Thanks again for the help! I don't think I could get document.require
to actually import in python, so I removed it, but I'll put it back in
(together with the import) so that the dependency is created. Here's
the issue for the other problem:

http://code.google.com/p/scripturian/issues/detail?id=2

I'd also like to use the requests library in python, but "sincerity
easy_install requests" fails. Here's the issue I opened for that:

http://code.google.com/p/sincerity/issues/detail?id=1

(Aside: does sincerity actually use python2.5?)

Tal Liron

unread,
Jan 19, 2014, 1:05:27 AM1/19/14
to prudence-...@googlegroups.com
Sincerity uses Jython, which does not support all Python libraries, unfortunately...

Also, it's probably best to use Prudence's own external requests API. It's not a Pythonic as the "requests" lib, but it's very scalable and powerful:

http://threecrickets.com/prudence/manual/web-data/#external-requests
Reply all
Reply to author
Forward
0 new messages