Skip to main content

A minimal DSL for Python

Project description

A minimal DSL for Python

https://travis-ci.org/fcurella/minilanguage.svg

Minilanguage is a minimal DSL for Python written with Ply.

Is it intended to be a starting point for writing more specific DSLs.

Currently, it only implements simple boolean logic and objects/strings evaluation.

Example

from minilanguage.grammar import FeatureParser

context = {
    'country': 'US',
    'user': {
        'username': 'regular_user',
        'data_bag': {
            "payload": 'abc',
        }
    }
}

parser = FeatureParser()
parser.build()

parser.evaluate("2 + 0.5", context)
# 2.5

parser.evaluate("12 + 12", context)
# 24

parser.evaluate("12 == 12", context)
# True

parser.evaluate("12 != 12", context)
# False

parser.evaluate("12 and False", context)
# False

parser.evaluate("12 or False", context)
# 12

parser.evaluate("12 > 10", context)
# True

parser.evaluate("12 < 10", context)
# False

parser.evaluate("12 <= 12", context)
# True

parser.evaluate("12 >= 12", context)
# True

parser.evaluate("country", context)
# 'US'

parser.evaluate("country == 'US'", context)
# True

parser.evaluate("user['username']", context)
# 'regular_user'

parser.evaluate("'a', 'b'", context)
# tuple('a', 'b')

parser.evaluate("user['data_bag']['payload']", context)
# 'abc'

parser.evaluate("user.get('missing_key', 'default')", context)
# 'default'

parser.evaluate("12 if False else 14", context)
# 14

License

This software is released under the MIT License.

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

minilanguage-1.0.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

minilanguage-1.0.0-py2.py3-none-any.whl (7.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file minilanguage-1.0.0.tar.gz.

File metadata

File hashes

Hashes for minilanguage-1.0.0.tar.gz
Algorithm Hash digest
SHA256 531277f6819dbc5f6399dd17c20345a8fdddc06e912e6d78ec3d5dc9d8bea323
MD5 0b7f150406ab3f372a54d3cbb35594e1
BLAKE2b-256 98e03202319ff261556bbe6ed390dbf14e71030310012228ecdd5754da878a96

See more details on using hashes here.

Provenance

File details

Details for the file minilanguage-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for minilanguage-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a088dc016659aea1913bf85977c4ef8c92d18568a7eccf427a4603178771e0d1
MD5 1d2f6302c2a8001cef48d75f5be7e54c
BLAKE2b-256 8663ea94cc646726d59ff45a7129acbd2f4df862d8dddb6b037f62e6d5b7627f

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