tags:

views:

57

answers:

1

Hi, 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 total 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.

A: 

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.

nFreeze
@nFreeze.. Its giving me an error " > from gluon.main import abspath > ImportError: cannot import name abspath
Sudeep
Sorry, I tried to get fancy. Fixed.
nFreeze