Esri / mdcs-py

MDCS is an acronym for Mosaic Dataset Configuration Script and is the entry point to a collection of Python classes/libraries that could be consumed by a Python client application to complete a given workflow for creating a mosaic dataset, populating it with data, and setting all required/desired parameters.
Apache License 2.0
64 stars 29 forks source link

Calculate Value expression / code block - 'not defined' #117

Closed mapface closed 1 year ago

mapface commented 1 year ago

Having issues in making codeblocks work for the calculate value tool - followed the master.xml template but keep getting a Name Error:

    <CalculateValue>
                 <query></query>
         <fieldName>CAPTURESTART</fieldName>
         <expression>todate(!Name!)</expression>
         <expression_type>PYTHON</expression_type>
        <code_block>"""def todate(value):\n import dateutil.parser as dparser\n  date = dparser.parse(next((x for x in (value.split("_")) if x[0].isdigit())), fuzzy=True, ignoretz=True)\n  return date"""</code_block>
    </CalculateValue>

Results in:

ERROR 000539: Traceback (most recent call last): File "", line 1, in NameError: name 'todate' is not defined

Can confirm same codeblock works in ArcPro calculate field. Any advice?

vijaygit02 commented 1 year ago

@mapface < can you try giving the expression in double quotes

"todate(!Name!)"
mapface commented 1 year ago

Hi @vijaygit02

Double quotes gives a syntax error for me -

ERROR 000539: File "", line 1 "todate(u"Example_202205160903_1b_XY_Test")" ^ SyntaxError: invalid syntax

vijaygit02 commented 1 year ago

@mapface , can you share your mdcs config file

mapface commented 1 year ago

Hi @vijaygit02 - attached

calc_value_ex.txt

resQm3 commented 1 year ago

I had the same issue. Solved it by typing pure python code without the newline character. Looks a bit messy now since the line intent does not fit to the XML Tag anymore.


                <CalculateValues>
                    <CalculateValue>
                        <query>#</query>
                        <fieldName>GroupName</fieldName>
                        <expression>splitLayerName(!Name!)</expression>
                        <expression_type>PYTHON3</expression_type>
                        <code_block>import re
def splitLayerName(value):
  mo = re.search('^dtk(25|50|100)[_]\d{4,5}[_]\d{4}[_]\d+[_][a-z]{2}|^[lc]?\d{4}', value.lower())
  try:
    return mo.group()
  except AttributeError:
    return None
                        </code_block>
                    </CalculateValue>
                </CalculateValues>
mapface commented 1 year ago

Hi @resQm3 that appears to have done the trick, ran the process successfully.

vijaygit02 commented 1 year ago

@resQm3 , thanks for the solution. @mapface , I'll close the issue.