Skip to main content

Conversion to/from a human-oriented formula representation for mapbox-gl expression language

Project description

Formula syntax for Mapbox GL expressions

A library for converting Mapbox GL expressions to/from a formula syntax more familiar to humans (extended JavaScript-like grammar).

Syntax examples

// Expression
["+", 3, 4]
// Formula
3 + 4
// Expression
["==", ["coalesce", ["get", "foo"], -1], 1]
// Formula
(get("foo") ?? -1) == 1
// Expression
["min", ["zoom"], ["log2", 3.14]]
// Formula
min(zoom(), log2(3.14))
// Expression
[
    "!=",
    ["+", ["*", 3, 4], ["get", "foo"]],
    ["-", 4, ["/", 3, 2]]
]
// Formula
3 * 4 + get("foo") != 4 - 3 / 2
// Expression
["concat", "id: ", ["number-format", ["get", "id"], {}]]
// Formula
"label: " & number_format(get("id"), {})

Usage

from mgl_formulae import FormulaCompiler, FormulaDecompiler
compiler = FormulaCompiler.create()
decompiler = FormulaDecompiler.create()

assert compiler.compile('3 + 4') == ['+', 3, 4]
assert decompiler.decompile(['+', 3, 4]) == '3 + 4'

Features

  • Transparent function calls: any unknown expression turns into a function call automatically, simplifying forward compatibility when new language functions are added
  • Automatic conversion between dash in expressions and underscore in function names: e.g. number-format and number_format
  • Rich infix operator set for common operations with JavaScript precedence/associativity
  • Automatic conversion between if/else chains, ternary conditionals and case expressions
  • Full support for all json literal values, including literal conversion for lists and objects
  • Support for functions with keyword arguments (e.g. format)
  • Syntactic sugar for let/var: let $foo = 42; 1 + $foo

Non-trivial operator table

Operator Expression
?? coalesce
|| any
&& all
& concat
a ? b : c case/3

Other common operators are preserved as-is, while having the expected JavaScript-like semantics.

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

mgl_formulae-0.1.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

mgl_formulae-0.1.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file mgl_formulae-0.1.0.tar.gz.

File metadata

  • Download URL: mgl_formulae-0.1.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.9.13 Linux/5.17.12-100.fc34.x86_64

File hashes

Hashes for mgl_formulae-0.1.0.tar.gz
Algorithm Hash digest
SHA256 98366039e1ca1901d32eba6f29c3e0c79976a3341d47ccacb6062c81af51cf47
MD5 cd1eb1f31846e40667861b3f34d6619a
BLAKE2b-256 59c27aa5ae97c3032bf62b69f15081f6743dffc096ae3307da9a79c6fbeabe71

See more details on using hashes here.

File details

Details for the file mgl_formulae-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mgl_formulae-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.9.13 Linux/5.17.12-100.fc34.x86_64

File hashes

Hashes for mgl_formulae-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5d28ff54e5ff734870fb418cd8540ee092d67916b352fd4cb819c7b7036dad49
MD5 a09ad97cf4d02bf6601b15efe5f16704
BLAKE2b-256 792987deded67122adfb86f324df3db028040397e2916fe3396640e867d9b1d8

See more details on using hashes here.

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