microsoft / jupyter-Kqlmagic

Extension (Magic) to Jupyter notebook and Jupyter lab, that enable notebook experience working with Kusto, ApplicationInsights, and LogAnalytics data.
Other
85 stars 31 forks source link

Add support for curly brackets in curly brackets #63

Closed miflower closed 4 years ago

miflower commented 4 years ago

Current implementation fails when attempting to replace curly braces when queries have multiple levels of curly braces.

Below query will work:

> i = 1
> query = """
> print {i}
> """
> print(Parameterizer(query).apply({'i':1}, **{'enable_curly_brackets_params':True}).query)
print long(1)

however, next query will not work.

> i = 1
> query = """
> let one_plus_i = (){ {i} + 1; };
> print one_plus_i()
> """
> print(Parameterizer(query).apply({'i':1}, **{'enable_curly_brackets_params':True}).query)
let one_plus_i = (){ long(1) + 1; };
print one_plus_i()

PR enhances Parameterizer to support this use-case.

mbnshtck commented 4 years ago

This is not a BUG. You are not using the curly brackets properly. When you enable curly bracket parametrization in the query string, Strings within curly brackets will be evaluated as python expressions limited to the parameters dictionary namespace that you need to provide. If you need to use curly brackets within the query string for other meaning, you need to double them: '{' should be mapped to '{{' and '}' should be mapped to '}}'

your query should be modified as follows:

i = 1 query = """ let one_plus_i = (){{ {i} + 1; }}; print one_plus_i() """ print(Parameterizer(query).apply({'i':1}, **{'enable_curly_brackets_params':True}).query) let one_plus_i = (){ long(1) + 1; }; print one_plus_i()

=========================

Any python expression is allowed within the curly brackets. For example:

i = 1 query = """ let one_plus_i+7 = (){{ {i + 7} + 1; }}; print one_plus_i_7() """ print(Parameterizer(query).apply({'i':1}, **{'enable_curly_brackets_params':True}).query) let one_plus_i_7 = (){ long(8) + 1; }; print one_plus_i_7()