# Converting VB codeblock to Python in ArcGIS Field Calculator?

I have created a tool in Model Builder to plot telecoms data - the user clicks a point on the map, enters a radius and enters an azimuth. The model then creates the buffer and cuts a segment out of the buffer based on the azimuth angle.

In order to calculate the bearing of each line in the segment, I have added some code to the Field Calculator to ensure it takes into account there is 360 degrees in a circle...see below.

Expression:

``````val
``````

Expression Type:

``````VB
``````

Code Block:

``````dim val
If [Azimuth] < 60 Then
val = 360 - (60-[Azimuth])
Else
val = [Azimuth] - 60
End If
``````

I want to publish this tool as a geoprocessing service to ArcGIS Portal, to then use as a widget within WAB.

I cannot publish the tool with the codeblock written in VB and therefore need to convert to Python, can anyone help me with the syntax please?

I'm using ArcMap 10.5.

Python I'm testing now but getting an error of invalid syntax in line 1...

Expression:

``````calc(!Bearing_1!)
``````

Expression Type:

``````Python
``````

Code Block:

``````def calc(!Bearing_1!):
if !Azimuth! < 60
return 360 - (60 - !Azimuth!)
else:
return !Azimuth! - 60
``````
• Some advice you didn't ask for: drop the geoprocessing service idea. It's a super heavy solution to what seems to be some drawing/geometry operations. Yes the GP Service will work, but you can accomplish your end goal using the JS API and the local geometry engine. It'll operate magnitudes faster than the GP Service. – KHibma Jan 28 at 15:57

Ad a `new field` and apply the following expression in it, however, I am not expert in VB, but I think it should be something like this.

Pre-Logic Code Block:

``````def val(a):
if a < 60:
return 360 - (60 - a)
else:
return a - 60
``````

Expression:

``````val(!Azimuth!)
``````

Here is the expression in Field Calculator: Here is the output: In Python you don't need to declare the variable ahead of time. Python formatting of if statements is `if condition:` with the next line or lines indented. The word then is not included. Else statements are on the same indent as the if, followed again with : and an indented line or lines describing what to do in that condition. There is no explicit end if.

``````if azimuth < 60:
val = 360 - (60-azumuth)
else:
val = azimuth - 60
``````

Since you're doing this in the field calculator, you're likely going to have to wrap this in a function definition, like below. Pass the field variable into the function with expression = `functionname(!fieldname!)` e.g. `getval(!Azimuth!)`. Get the values back out using a return.

``````def getval(azimuth):
if azimuth < 60:
val = 360 - (60-azumuth)
else:
val = azimuth - 60
return val
``````

Python code blocks are not that different from VB.

If you read this page : ESRI help : python field calc you should be able to convert your code with no problem (there are lots of examples).