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.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

simpleQL-0.1-py2.4.egg (12.5 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for simpleQL-0.1.tar.gz
Algorithm Hash digest
SHA256 f95729fd42e7e9da787e928e951a43e4214dee6a03b277727fc3df8e4dfdf602
MD5 40af070838846f664e8c8174a35c701b
BLAKE2b-256 8fd91a137333aa1ce4735d51b23a69c7833e19fb898e494f313c85dc127bd1bd

See more details on using hashes here.

Provenance

File details

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

File metadata

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

File hashes

Hashes for simpleQL-0.1-py2.4.egg
Algorithm Hash digest
SHA256 c21415be1ab0d92a13b31e9a85ff68d5752ce370e361d5fe95139bd19217aa0e
MD5 ef2d757fe75f8f287f1115c5f6298df4
BLAKE2b-256 c4d3fbcb95dd0e12af7d17b8f71154940abe60bbd2dd563d424a8c637147d095

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