Skip to main content

bare-script

Project description

PyPI - Status PyPI GitHub PyPI - Python Version

BareScript is a light-weight scripting and expression language.

Executing BareScript

To execute a BareScript script, parse the script using the parse_script function. Then execute the script using the execute_script function. For example:

>>> from bare_script import execute_script, parse_script
...
>>> # Parse the script
... script = parse_script('''\
... # Double a number
... function double(n):
...     return n * 2
... endfunction
...
... return N + ' times 2 is ' + double(N)
... ''')
...
>>> # Execute the script
... globals = {'N': 10}
>>> print(execute_script(script, {'globals': globals}))
10 times 2 is 20

The BareScript Library

The BareScript Library includes a set of built-in functions for mathematical operations, object manipulation, array manipulation, regular expressions, HTTP fetch and more. The following example demonstrates the use of the systemFetch, objectGet, and arrayLength functions.

>>> import urllib.request
...
>>> from bare_script import execute_script, fetch_http, parse_script
...
>>> # Parse the script
... script = parse_script('''\
... # Fetch the BareScript library documentation JSON
... docs = jsonParse(systemFetch('https://craigahobbs.github.io/bare-script-py/library/library.json'))
...
... # Return the number of library functions
... return 'The BareScript Library has ' + arrayLength(objectGet(docs, 'functions')) + ' functions'
... ''')
...
>>> # Execute the script
... print(execute_script(script, {'fetchFn': fetch_http}))
The BareScript Library has 106 functions

Evaluating BareScript Expressions

To evaluate a BareScript expression, parse the expression using the parse_expression function. Then evaluate the expression using the evaluate_expression function.

Expression evaluation includes the BareScript Expression Library, a set of built-in, spreadsheet-like functions.

For example:

>>> from bare_script import evaluate_expression, parse_expression
...
>>> # Parse the expression
... expr = parse_expression('2 * max(a, b, c)')
...
>>> # Evaluate the expression
... variables = {'a': 1, 'b': 2, 'c': 3}
>>> print(evaluate_expression(expr, None, variables))
6.0

The BareScript Command-Line Interface (CLI)

You can run BareScript from the command line using the BareScript CLI, “bare”. BareScript script files use the “.bare” file extension.

bare script.bare

Note: In the BareScript CLI, import statements and the systemFetch function read non-URL paths from the local file system.

MarkdownUp, a Markdown Viewer with BareScript

MarkdownUp is a Markdown Viewer that executes BareScript embedded within Markdown documents. MarkdownUp extends its standard library with functions for dynamically rendering Markdown text, drawing SVG images, etc.

For example:

# Markdown Application

This is a Markdown document with embedded BareScript:

~~~ markdown-script
markdownPrint('Hello, Markdown!')
~~~

Development

This package is developed using python-build. It was started using python-template as follows:

template-specialize python-template/template/ bare-script/ -k package bare-script -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs'

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

bare-script-0.9.3.tar.gz (35.1 kB view hashes)

Uploaded Source

Built Distribution

bare_script-0.9.3-py3-none-any.whl (37.5 kB view hashes)

Uploaded Python 3

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