开发者

web2py: Where should I store private, application-specific files?

I have just started working on web2py. Personally, I find it easier to learn than Django.

My query is that I have to load a file at application startup. Its a pickled hashtable. Where should I store this file so that the system is able to see it

My code is :

import cPickle as pickle
def index():
    """
    Load the file into memory and message the number of entries
    """
    f = open('tables.pkl','rb')
    session.tables = pickle.load(f)
    f.close()
    terms = len(session.tables.keys())
    message = 'The tota开发者_JAVA技巧l entries in table = ' + str(terms)
    return dict(message=message) 

As you can see, I have put the code in index() to load it at startup. At present I am using the absolute path upto the physical location of the 'tables.pkl' file. Where should i put it in my application folder.

Also, I want tables variable to be available to all functions in the controller. Is session.tables the right way to go? It is just a search app so there is no user login. The table has to be loaded only once for all users accessing the page. Thank you.


I think the private folder would be a good place for this. You can get the absolute path with:

import os
fp = os.path.join(request.folder,'private','tables.pkl')    

I would use cache instead of session if the file is not unique per user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜