simpleQL 0.1.2.2
Efficient filtering of SQL tables with generator expressions.
This module allows you to access a (DB API 2) SQL table using nothing but Python to build the query:
>>> import re
>>> from pysqlite2 import dbapi2 as sqlite
>>> from simpleql.table import Table
>>> conn = sqlite.connect(":memory:")
>>> curs = conn.cursor()
>>> curs.execute("CREATE TABLE test (a integer, b char(1))")
>>> curs.executemany("INSERT INTO test (a, b) VALUES (?, ?)", ([1,'a'], [2,'b'], [3,'c']))
>>> conn.commit()
>>> table = Table(conn, "test", verbose=1)
>>> for row in table:
... print row
...
SELECT a, b FROM test;
{'a': 1, 'b': u'a'}
{'a': 2, 'b': u'b'}
{'a': 3, 'b': u'c'}
Note that each row in the table is a dictionary. We can filter this using a generator expression:
>>> aspan = (1, 3)
>>> for row in (t for t in table if min(aspan) < t['a'] < max(aspan)):
... print row
...
SELECT a, b FROM test WHERE (1<a) AND (a<3);
{'a': 2, 'b': u'b'}
(This is a fake example, the filtering does not work in interactive mode.)
As you can see, the query string is built from a generator expression. You can also use list comprehensions. Regular expressions are supported by the use of the re.search method:
>>> filtered = [t for t in table if re.search('a', t['b'])]
SELECT a, b FROM test WHERE b LIKE "%a%";
>>> print filtered
[{'a': 1, 'b': u'a'}]
The advantage of this approach over the similar recipe is that if the (efficient) query builder fails when it encounters a complex filter the data will still be filtered (unefficiently) by the generator expression.
| File | Type | Py Version | Uploaded on | Size | # downloads |
|---|---|---|---|---|---|
| simpleQL-0.1.2.2-py2.4.egg (md5) | Python Egg | 2.4 | 2006-10-16 16:36:00 | 12KB | 552 |
| simpleQL-0.1.2.2.tar.gz (md5) | Source | 2006-10-16 16:35:37 | 5KB | 469 | |
- Author: Roberto De Almeida <roberto at dealmeida net>
- Home Page: http://dealmeida.net/projects/simpleql
- Keywords: sql pythonic
- License: MIT
- Categories
- Package Index Owner: roberto
- DOAP record: simpleQL-0.1.2.2.xml
Log in to rate this package.
