vb2python

1392
7
05-06-2012 11:51 PM
FelixMuyoba
New Contributor
hi there,
can anyone assist me please on this query?
a script that will require an IF statement for summing numbers cumulatively if they satisfy a certain condition or not summing them if they do not satisfy the condition. This is what i had in VB but wants that to be PYTHON:
Static rec As Double
Dim pStart As Double
Dim pInterval As Double
pStart = 1
pInterval = 1
If (rec = 0) Then
rec = pStart
Else
rec = rec + pInterval
End If

can anyone help please?
Tags (2)
0 Kudos
7 Replies
MarcinGasior
Occasional Contributor III
At Calculate Field examples help page there's a script in Python:
Code Block:
rec=0
def autoIncrement():
 global rec
 pStart = 1 #adjust start value, if req'd 
 pInterval = 1 #adjust interval value, if req'd
 if (rec == 0): 
  rec = pStart 
 else: 
  rec = rec + pInterval 
 return rec

Expression:
autoIncrement()
0 Kudos
FelixMuyoba
New Contributor
thanks, gasior..will look at it
0 Kudos
FelixMuyoba
New Contributor
hi gasior,
i tried those codes and they don't work and the results window in geoprocessing shows PYTHON 9.3..is there something I'm missing?
0 Kudos
MarcinGasior
Occasional Contributor III
Which code doesn't work? In which tool? What errors are generated?

I tried to run the code in ArcGIS 9.3 Calculate Field tool. It works ok. The settings are here:
[ATTACH=CONFIG]14197[/ATTACH]
0 Kudos
FelixMuyoba
New Contributor
thanks, Gasior everything works perfectly now..i guess it was just an oversight from side..now, how do i modify these codes to python format?
Static rec As Long
Static dSum As Double
Dim sourceField
If ([Offset] > 0) Then
  dSum = sourceField
Else
  dSum = sourceField + dSum
End If
rec = rec + 1

please help!
0 Kudos
MarcinGasior
Occasional Contributor III
I'm not sure what exactly do you want to calculate. Please explain.
Here is a thought how Code Block may be:
def conditional(sourceField, Offset):
  dSum = 0
  if Offset > 0:
    dSum = sourceField
  else:
    dSum += sourceField
  return dSum

Expression:
conditional(!sourceField!,!Offset!)
0 Kudos
FelixMuyoba
New Contributor
gasior,
this looks exactly what I am looking for because what i want is a script that will require an IF statement for summing numbers cumulatively if they satisfy a certain condition or not summing them if they do not satisfy the condition.
0 Kudos