HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Interpolating code delimited with character that can appear in code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
candelimitedwithcharacterinterpolatingthatcodeappear

Problem

I've got a string that consists of an arbitrary combination of text and {} delimited python code, for instance, A plus b is {a + b}. However, braces are used for dictionary and set literals in python, so You chose the {{1:"first", 2:"second"}[choice]} option should also be interpretted correctly. It's also valid to have more than one python expression in the input, so You picked {choice1} and {choice2} is valid.

Here's my current code:

protected String ParseStringForVariable([NotNull] String str)
{
    PythonEngine.PythonEngine pythonEngine = GetCurrentCore().PythonEngine;

    for (int i = 0; i  opening
                                   select new
                                          {
                                              Template = str.Substring(opening, closing - opening + 1),
                                              Code = str.Substring(opening + 1, closing - opening - 1)
                                          })
        {
            PythonByteCode compiled;
            try
            {
                compiled = pythonEngine.Compile(expression.Code, PythonByteCode.SourceCodeType.Expression);
            }
            catch (PythonParseException)
            {
                // not valid python, try next expression
                continue;
            }
            String result = pythonEngine.Evaluate(compiled).ToString();
            str = str.Replace(expression.Template, result);
            break;
        }
    }

    return str;
}


It works by looking at progressively longer strings, attempting to parse them, and ignoring them if it's not valid python. This is done with the PythonEngine class, which is a wrapper around the IronPython interpretter, and has an extensive test suite, so can assumed to be correct.

-
It appears to burp slightly if the value of the first of multiple python expression contains an opening brace: {"{"} {"}"}, what's the best way to prevent that?

-
Resharper is complaining about

Solution

I see an extra unnecessary variable that you can get rid of, or change another variable so that it makes more sense.

int opening = i;


you use this inside of a for loop. You can do 2 things with this.

  • Just use the variable i where ever you had the variable opening



-
change the i variable to opening in the for loop like

for (int opening = 0; opening < str.Length; opening++)


hopefully this shows you how unnecessary that extra variable is, it isn't used anywhere else for anything other than the loops.

Other than that I would need to play with it and spend some time with it to see if it plays well with others.

Code Snippets

int opening = i;
for (int opening = 0; opening < str.Length; opening++)

Context

StackExchange Code Review Q#24969, answer score: 2

Revisions (0)

No revisions yet.