Select to view content in your preferred language

Please confirm: Possible BUG with in-line variable substitution and decimal point

3915
4
Jump to solution
05-19-2012 02:47 AM
MarcoBoeringa
MVP Regular Contributor
Hi all,

For the record: ArcGIS 10.0 SP4.

Just getting my feet wet for the first time with more advanced ModelBuilder techniques in combination with Python, I noticed there is a possible issue / bug with the in-line variable substitution in non-English locals. My computer is set to Dutch, and displays "," as the decimal point character. However, if I have a numeric variable with a decimal point in it (in my case the values were Double), think of "1234,56789", the in-line variable substitution leads to a fault as soon as the variable is inserted in a Python function's argument list, as Python uses the comma "," character to separate out different arguments.

I saw this issue popping up when I tried using two Double variables as arguments in the Calculate Value tool of ModelBuilder. See the attached screenshots. The first screenshot shows the original Python code I inserted in the Calculate Value tool. This code returned the error seen in screenshot 2. Notice it clearly states that the "getMBRsqrt" function I defined "takes exactly 2 arguments (4 given)" while 4 given!. Also look at the string of numbers in the red lettered error description. It shows the two variables I passed also being separated by the decimal comma of the values themselves, causing 4 "pseudo arguments" to be passed over to the function, instead of two numbers using decimal points. This is of course wrong, and leads to the fault, as my function only accepts two arguments and must have the decimal values / doubles passed over. Only when I elaborately changed the function, and passed the variables as strings, converting them back to numbers, could I run it. See screenshot 3 for the modified code in the Calculate Value tool that did run properly.

Before I needlessly start reporting this to ESRI, can someone else confirm this issue?

I tried finding it in the list of fixed issues for SP5, but no such issue seems listed for fixing.

Marco
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Original User: curtvprice

Could you elaborate a bit more on this specific subject regarding pathnames? I haven't read about it before. And is the "r" before the inline variable a typing error, or does it need to be there?


The "r" prefix to the string instructs python to treat this as a "raw" string. Without the "r" prefix, a path string like , "C:\table_folder" gets interpreted by Python as "C:<tab>able_folder".

1. You enter into the tool:
r"%my_path_element%"

2. Modelbuilder substitutes and passes to Python:
r"C:\table_folder"


By the way, there's a handy ModelBuilder tool ("Parse Path") included at 10.0 that does path manipulations for you, so you can avoid some of this path painfulness.

While we're on the subject of paths, there is some critical information here:

Arc 10 Help: Paths explained: Absolute, relative, UNC, and URL

View solution in original post

0 Kudos
4 Replies
by Anonymous User
Not applicable
Original User: curtvprice

Only when I elaborately changed the function, and passed the variables as strings, converting them back to numbers, could I run it.


This is the way to do it. It seems elaborate but there is no other way as ModelBuilder accesses those variables accessed with "%" syntax as strings -- not objects. The commas separating arguments is python syntax, there is no way around that.

The way this all works is the the input expression to Calculate Value is interpreted by model builder (variables substituted in) -- and the resulting function is then passed to python literally. This is the same reason that pathnames must be used with Calculate Value like this:

def func(r"%mbuilder path variable%")


Hope this helps!
0 Kudos
MarcoBoeringa
MVP Regular Contributor
This is the way to do it. It seems elaborate but there is no other way as ModelBuilder accesses those variables accessed with "%" syntax as strings -- not objects. The commas separating arguments is python syntax, there is no way around that.


Thanks Curtis, it is appreciated. This is a clear explanation, and I was suspecting something like that. As I am just starting with Python, it is all a bit new to me and getting to know these specific issues and how to handle them, takes time. 

The way this all works is the the input expression to Calculate Value is interpreted by model builder (variables substituted in) -- and the resulting function is then passed to python literally. This is the same reason that pathnames must be used with Calculate Value like this:

def func(r"%mbuilder path variable%")


Hope this helps!


Could you elaborate a bit more on this specific subject regarding pathnames? I haven't read about it before. And is the "r" before the inline variable a typing error, or does it need to be there?
0 Kudos
by Anonymous User
Not applicable
Original User: curtvprice

Could you elaborate a bit more on this specific subject regarding pathnames? I haven't read about it before. And is the "r" before the inline variable a typing error, or does it need to be there?


The "r" prefix to the string instructs python to treat this as a "raw" string. Without the "r" prefix, a path string like , "C:\table_folder" gets interpreted by Python as "C:<tab>able_folder".

1. You enter into the tool:
r"%my_path_element%"

2. Modelbuilder substitutes and passes to Python:
r"C:\table_folder"


By the way, there's a handy ModelBuilder tool ("Parse Path") included at 10.0 that does path manipulations for you, so you can avoid some of this path painfulness.

While we're on the subject of paths, there is some critical information here:

Arc 10 Help: Paths explained: Absolute, relative, UNC, and URL
0 Kudos
MarcoBoeringa
MVP Regular Contributor
Thanks Curtis, this is very useful information, certainly a help for future development with ModelBuilder.
0 Kudos