reusing a codeblock?

1386
6
09-23-2016 12:55 PM
KeithD1
New Contributor III

I'm using arcpy.CalculateField_management() with a large-ish codeblock. Is there a way to store the codeblock so it can be easily reused multiple times, and more importantly, easily edited? This is part of a larger script.

For instance (psuedo-code):

codeblock = "

line 1 statements

line 2 statements

   some indented statements

    # a comment

line 3 statements "

arcpy.CalculateField_management("1st_dataset_name","field_to_calculate","codeblock")

arcpy.CalculateField_management("2nd_dataset_name","field_to_calculate","codeblock")

What would be the proper way of doing this?

Thanks!

0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

just save it as a text file even inside a python script.  You can enclose it inside triple-single or triple double quotes so that it won't execute, but it willl maintain its python-esque shape and form.

# my code blocks enclosed in triple-double quotes...

"""

codeblock = "
line 1 statements
line 2 statements
   some indented statements
    # a comment
line 3 statements "

"""
KeithD1
New Contributor III

I'm not sure what you mean by "save it as a text file even inside a python script".  If I enclose everything in triple double quotes, then it won't execute, but it will still become a variable?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Ok... if you can save your def as a script you can import the function from within the script and reuse the code block... I suspect this isn't the easy solution that you want short of getting into importing modules, namespace and the like.  but here goes.

>>> from angles_demo import _demo
>>> import inspect
>>>
>>> codeblock = inspect.getsource(_demo)
>>> print(codeblock)
def _demo():
    """A sample run of compass returning compass notations for 20 deg
       increments.  Change to suit
    """
    angles = np.arange(0, 360, 20) 
    for i in angles:
        print("{} => {}".format(i, compass(i)))
    rose = c[np.digitize(angles, a)]
    comp_rose = list(zip(angles,rose))
    print("\nCompass rose examples\n{}".format(comp_rose))
    return comp_rose
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

and codeblock is a string.  For you, the easiest thing would just to keep a script of your code blocks in the same folder as the script you are using... import the code block from the script and get the script.  Or better still, you could just copy and paste the 'stuff' into the code you are working on... whatever is easiest is the best... but I suspect you aren't creating packages as of yet that you need to maintain.

KeithD1
New Contributor III

Thanks Dan, I appreciate the response. A little over my head at this point, but maybe for my next Python project I'll be able to incorporate what you described!  For now, I'll just redundantly copy-paste the codeblock where I need it to go.

0 Kudos
DanPatterson_Retired
MVP Emeritus

good luk Keith... this topic is on my blog list.  I use it all the time.  It is easy once you ge the hang of it.... but copy paste is just as good...as long as you can find it

0 Kudos
DarrenWiens2
MVP Honored Contributor

If your codeblock is stored in a variable named "codeblock", then access it using the variable codeblock (no quotes - quotes means it's a string that literally is "codeblock")

arcpy.CalculateField_management("1st_dataset_name","field_to_calculate",codeblock)