Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What about using an in-memory SQLite database via the <a href="http://docs.python.org/library/sqlite3.html">sqlite3 standard library module</a>, using the special value <code>:memory:</code> for the connection? If you don't want to write your on SQL statements, you can always use an ORM, like <a href="http://www.sqlalchemy.org/docs/dialects/sqlite.html#connect-strings">SQLAlchemy</a>, to access an in-memory SQLite database.</p> <p><strong>EDIT</strong>: I noticed you stated that the values may be Python objects, and also that you require avoiding serialization. Requiring arbitrary Python objects be stored in a database also necessitates serialization.</p> <p>Can I propose a practical solution if you must keep those two requirements? Why not just use Python dictionaries as indices into your collection of Python dictionaries? It sounds like you will have idiosyncratic needs for building each of your indices; figure out what values you're going to query on, then write a function to generate and index for each. The possible values for one key in your list of dicts will be the keys for an index; the values of the index will be a list of dictionaries. Query the index by giving the value you're looking for as the key.</p> <pre><code>import collections import itertools def make_indices(dicts): color_index = collections.defaultdict(list) age_index = collections.defaultdict(list) for d in dicts: if 'favorite_color' in d: color_index[d['favorite_color']].append(d) if 'age' in d: age_index[d['age']].append(d) return color_index, age_index def make_data_dicts(): ... data_dicts = make_data_dicts() color_index, age_index = make_indices(data_dicts) # Query for those with a favorite color is simply values with_color_dicts = list( itertools.chain.from_iterable(color_index.values())) # Query for people over 16 over_16 = list( itertools.chain.from_iterable( v for k, v in age_index.items() if age &gt; 16) ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload