Skip to main content

Efficient filtering of SQL tables with generator expressions.

Project description

This module allows you to access a (DB API 2) SQL table using nothing but Python to build the query:

>>> from pysqlite2 import dbapi2 as sqlite
>>> conn = sqlite.connect("coastline.db")

>>> from simpleql.table import Table
>>> table = Table(conn, "coastline", verbose=True)
>>> print table
{'longitude': <simpleql.table.Col object at 0x547070>, 'latitude': <simpleql.table.Col object at 0x547050>}

>>> for row in (r for r in table if r.latitude > 83 and (r.longitude < 300 or r.longitude > 320)):
...     print row
SELECT longitude, latitude FROM coastline WHERE (latitude>83) AND ((longitude<300) OR (longitude>320));
{'longitude': 292.53553099999999, 'latitude': 83.016946000000004}
{'longitude': 292.188199, 'latitude': 83.019293000000005}
{'longitude': 290.23328500000002, 'latitude': 83.019293000000005}
{'longitude': 289.97044, 'latitude': 83.031026999999995}
{'longitude': 289.65127000000001, 'latitude': 83.014599000000004}
<snip>

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:

>>> table = Table(conn, "table")
>>> filtered = (r for r in table if re.search("string", r['column']))
>>> for row in filtered:
...     print row
SELECT column FROM table WHERE column LIKE "%string%";
<snip>

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simpleQL-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

simpleQL-0.1.1-py2.4.egg (12.9 kB view details)

Uploaded Source

File details

Details for the file simpleQL-0.1.1.tar.gz.

File metadata

  • Download URL: simpleQL-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for simpleQL-0.1.1.tar.gz
Algorithm Hash digest
SHA256 340ed10a666212424bb18e6d5c71fc4048f6098639a5164f66438bcbf88df7e9
MD5 3ef5bf4b6b1cc79a5e48a19eebc6a524
BLAKE2b-256 43639ba2b9005ffd1466c5096d6ae0a491e23d89b29df87d176b89609447d7df

See more details on using hashes here.

Provenance

File details

Details for the file simpleQL-0.1.1-py2.4.egg.

File metadata

  • Download URL: simpleQL-0.1.1-py2.4.egg
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for simpleQL-0.1.1-py2.4.egg
Algorithm Hash digest
SHA256 6abfb779c0f1fa88433f956a108156e9d36b45dbc87b74ac6fd5949e9f09a51d
MD5 de85b9609a8a3d6312369f3ea9a2eef8
BLAKE2b-256 aecb5a5f5abea140286d45a0053eaa50d1ad173f24d5ab4955fc550e7088e7d9

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page