4

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.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Sudeep
  • 43
  • 4
  • 1
    just and add : I have written the code for loading the table and dropped it in models directory. It now loads at startup and is available globally .. – Sudeep Oct 24 '10 at 12:09

1 Answers1

4

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.

mr.freeze
  • 13,731
  • 5
  • 36
  • 42
  • . Its giving me an error " > from gluon.main import abspath > ImportError: cannot import name abspath – Sudeep Oct 24 '10 at 07:59