Skip to main content

CQL parser for Python

Project description

pycql

Build Status

A pure python CQL parser.

Installation

pip install pycql

Usage

The basic functionality parses the input string to an abstract syntax tree (AST) representation. This AST can then be used to build database filters or similar functionality.

import pycql

ast = pycql.parse(filter_expression)

Testing

The basic functionality can be tested using pytest.

python -m pytest

There is a test project/app to test the Django integration. This is tested using the following command:

python manage.py test testapp

Django integration

For Django there is a default bridging implementation, where all the filters are translated to the Django ORM. In order to use this integration, we need two dictionaries, one mapping the available fields to the Django model fields, and one to map the fields that use choices. Consider the following example models:

from django.contrib.gis.db import models


optional = dict(null=True, blank=True)

class Record(models.Model):
    identifier = models.CharField(max_length=256, unique=True, null=False)
    geometry = models.GeometryField()

    float_attribute = models.FloatField(**optional)
    int_attribute = models.IntegerField(**optional)
    str_attribute = models.CharField(max_length=256, **optional)
    datetime_attribute = models.DateTimeField(**optional)
    choice_attribute = models.PositiveSmallIntegerField(choices=[
                                                                 (1, 'ASCENDING'),
                                                                 (2, 'DESCENDING'),],
                                                        **optional)


class RecordMeta(models.Model):
    record = models.ForeignKey(Record, on_delete=models.CASCADE, related_name='record_metas')

    float_meta_attribute = models.FloatField(**optional)
    int_meta_attribute = models.IntegerField(**optional)
    str_meta_attribute = models.CharField(max_length=256, **optional)
    datetime_meta_attribute = models.DateTimeField(**optional)
    choice_meta_attribute = models.PositiveSmallIntegerField(choices=[
                                                                      (1, 'X'),
                                                                      (2, 'Y'),
                                                                      (3, 'Z')],
                                                             **optional)

Now we can specify the field mappings and mapping choices to be used when applying the filters:

FIELD_MAPPING = {
    'identifier': 'identifier',
    'geometry': 'geometry',
    'floatAttribute': 'float_attribute',
    'intAttribute': 'int_attribute',
    'strAttribute': 'str_attribute',
    'datetimeAttribute': 'datetime_attribute',
    'choiceAttribute': 'choice_attribute',

    # meta fields
    'floatMetaAttribute': 'record_metas__float_meta_attribute',
    'intMetaAttribute': 'record_metas__int_meta_attribute',
    'strMetaAttribute': 'record_metas__str_meta_attribute',
    'datetimeMetaAttribute': 'record_metas__datetime_meta_attribute',
    'choiceMetaAttribute': 'record_metas__choice_meta_attribute',
}

MAPPING_CHOICES = {
    'choiceAttribute': dict(Record._meta.get_field('choice_attribute').choices),
    'choiceMetaAttribute': dict(RecordMeta._meta.get_field('choice_meta_attribute').choices),
}

Finally we are able to connect the CQL AST to the Django database models. We also provide factory functions to parse the timestamps, durations, geometries and envelopes, so that they can be used with the ORM layer:

from pycql.integrations.django import to_filter

cql_expr = 'strMetaAttribute LIKE "%parent%" AND datetimeAttribute BEFORE 2000-01-01T00:00:01Z'

ast = pycql.parse(
    cql_expr, GEOSGeometry, Polygon.from_bbox, parse_datetime,
    parse_duration
)
filters = to_filter(ast, mapping, mapping_choices)

qs = Record.objects.filter(**filters)

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

pycql-0.0.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

pycql-0.0.1-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

Details for the file pycql-0.0.1.tar.gz.

File metadata

  • Download URL: pycql-0.0.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for pycql-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1862fafcb1b600abc89b305fcb9a01c6dae43f2f83577d0063a46f2772cfbb01
MD5 4454cf8ff20813949125d60d11e99b41
BLAKE2b-256 4bc8e4e6bebeaa7210d2c74dcd231d233ccc809065fcbc24b9a15b4535abc60b

See more details on using hashes here.

Provenance

File details

Details for the file pycql-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pycql-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for pycql-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4481a79d58490b928e1d755a1f8f0bfb682d5c7b67a90c4b78dad5067d57ccb
MD5 4d52fec6906b01b18d5fa9a07fa56578
BLAKE2b-256 ae934984f95672d41b8c1aabafbc2411f844c3f5abb66efc5a377a0985bf5efe

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