Skip to content

Python

Python API documentation for the cql2 package. Install from PyPI:

python -m pip install cql2

API

cql2.parse_file(path)

Parses CQL2 from a filesystem path.

Parameters:

  • path (PathLike | str) –

    The input path

Returns:

  • Expr ( Expr ) –

    The CQL2 expression

Examples:

>>> from cql2 import Expr
>>> expr = Expr.parse_file("fixtures/text/example01.txt")

cql2.parse_json(s)

Parses cql2-json.

Parameters:

  • s (str) –

    The cql2-json string

Returns:

  • Expr ( Expr ) –

    The CQL2 expression

Raises:

  • ParseError

    Raised if the string does not parse as cql2-json

Examples:

>>> from cql2 import Expr
>>> expr = Expr.parse_json('{"op":"=","args":[{"property":"landsat:scene_id"},"LC82030282019133LGN00"]}')

cql2.parse_text(s)

Parses cql2-text.

Parameters:

  • s (str) –

    The cql2-text

Returns:

  • Expr ( Expr ) –

    The CQL2 expression

Raises:

  • ParseError

    Raised if the string does not parse as cql2-text

Examples:

>>> from cql2 import Expr
>>> expr = Expr.parse_text("landsat:scene_id = 'LC82030282019133LGN00'")

cql2.Expr(cql2)

A CQL2 expression.

The cql2 can either be a cql2-text string, a cql2-json string, or a cql2-json dictionary.

Parameters:

  • cql2 (str | dict[str, Any]) –

    The input CQL2

Examples:

>>> from cql2 import Expr
>>> expr = Expr("landsat:scene_id = 'LC82030282019133LGN00'")
>>> expr = Expr({"op":"=","args":[{"property":"landsat:scene_id"},"LC82030282019133LGN00"]})

to_json()

Converts this cql2 expression to a cql2-json dictionary.

Returns:

  • dict[str, Any]

    dict[str, Any]: The cql2-json

Examples:

>>> from cql2 import Expr
>>> expr = Expr("landsat:scene_id = 'LC82030282019133LGN00'")
>>> expr.to_json()
{'op': '=', 'args': [{'property': 'landsat:scene_id'}, 'LC82030282019133LGN00']}

to_sql()

Converts this cql2 expression to a SQL query.

Returns:

  • SqlQuery ( SqlQuery ) –

    The SQL query and parameters

Examples:

>>> from cql2 import Expr
>>> expr = Expr("landsat:scene_id = 'LC82030282019133LGN00'")
>>> q.query
'("landsat:scene_id" = $1)'
>>> q.params
['LC82030282019133LGN00']

to_text()

Converts this cql2 expression to cql2-text.

Returns:

  • str ( str ) –

    The cql2-text

Examples:

>>> from cql2 import Expr
>>> expr = Expr({"op":"=","args":[{"property":"landsat:scene_id"},"LC82030282019133LGN00"]})
>>> expr.to_text()
'("landsat:scene_id" = 'LC82030282019133LGN00')'

validate()

Validates this expression using json-schema.

Raises:

Examples:

>>> from cql2 import Expr
>>> expr = Expr("landsat:scene_id = 'LC82030282019133LGN00'")
>>> expr.validate()

cql2.SqlQuery

A SQL query

params: list[str] instance-attribute

The parameters, to use for binding.

query: str instance-attribute

The query, with parameterized fields.

cql2.ParseError

Bases: Exception

An error raised when cql2 parsing fails.

cql2.ValidationError

Bases: Exception

An error raised when cql2 json-schema validation fails.