I am having a rather trivial problem here, for which I couldn't figure out a solution yet.
I need to pass string values from ADO code to Python code, and the strings may have any characters, including the quotes (potentially unbalanced) and dollar signs.
Since the characters in the string values are user input, I can't precode them with e.g. a char() function as a constant, etc.
Consider the following example (save this as adoprint.ado):
Code:
version 16.0 program define adoprint syntax , param(string) display _asis `"`macval(param)'"' python : pyprint("`macval(param)'") // ^^^ doesn't seem to support `" "' quotes here, since this is a python statement end python: def pyprint(p): print(p) # here must be able to print any string parameter p passed to this procedure asis! end // END OF FILE
Code:
clear all
capture noisily adoprint, param("x=45") // works fine
capture noisily adoprint, param(`"x="$45""') // results in a syntax error
capture noisily adoprint, param(`"x="\$45""') // results in a syntax error
// END OF FILE
I am looking for a solution that would allow me to pass any combination of characters from ado code to python and access it in python code (in the example it is trivial printing, but in reality the string is meaningfully processed).
Any characters must be supported in any combination, but of specific concern are, of course, the quotes, double quotes, dollar sign, figure brackets, and other characters which may be triggering Stata syntax reactions. In the adocode (as demonstrated in the above example) I can utilize the _asis modifier of the display command and utilize the compound quotes to achieve relative safety, but this doesn't seem to work for calling the python procedure.
The best I can do at this time, is to avoid passing the value as a parameter, and peek directly into the local macro as defined in Stata's scope with Macro.getLocal("param") from the Python code. But I'd rather avoid this approach and have declared parameters for the sake of transparency of the interface.
Thank you, Sergiy
0 Response to Passing string values from ADO to Python code
Post a Comment