Python 2.7 list of dictionaries in memory between page trips

57 Views Asked by At

I researched up and down and I'm not seeing any answers that I'm quite understanding so I thought to post my own question.

I building a web application (specifically in web2py but that shouldn't matter I don't believe) on Python 2.7 to be hosted on Windows.

I have a list of about 2000 items in a database table. The user will be opening the application which will initially select all 2000 items into Python and return the list to the user's browser. After that the user will be filtering the list based on one-to-many values of one-to-many attributes of the items.

I'm wanting Python to hold the unadulterated list of 2000 items in-memory between the user's changes of filtering options.

  • Every time the user changes their filter options,
  • trip the change back to Python,
  • apply the filter to the in-memory list and
  • return the subset to the user's browser.

This is to avoid hitting the database with every change of filter options. And to avoid passing the list in session over and over.

Most of this I'm just fine with. What I'm seeking you advise on is how to get Python to hold the list in-memory. In c# you would just make it a static object.

How do you do a static (or whatever other scheme that applies) in Python?

Thanks for your remarks.


While proofreading this I see I'm still probably passing at least big portions of the list back and forth anyway so I will probably manage the whole list in the browser.

But I still like to hear you suggestions. Maybe something you say will help.

1

There are 1 best solutions below

0
On BEST ANSWER

As you seem to conclude, there isn't much reason to be sending requests back and forth to the server given that the server isn't generating any new data that isn't already held in the browser. Just do all the filtering directly in the browser.

If you did need to do some manipulation on the server, though, don't necessarily assume it would be more efficient to search/filter a large dataset in Python rather than querying the database. You should do some testing to figure out which is more efficient (and whether any efficiency gains are worth the hassle of adding complexity to your code).