I have the following code to calculate formulas for a report.
total_good_parts_per_hour = 7713 * 3
# Calculate Choko and Bekido for each hour
chokoValues = []
bekidoValues = []
for i in range(1, 10):
goodPartsValue = data['goodParts24HR%d' % (14 + i)]
combinedValue = data['combined24HR%d' % (14 + i)]
if (goodPartsValue + combinedValue) != 0:
chokoValue = (goodPartsValue / (goodPartsValue + combinedValue)) * 100
else:
chokoValue = 0
if total_good_parts_per_hour != 0:
bekidoValue = (goodPartsValue / total_good_parts_per_hour) * 100
else:
bekidoValue = 0
# Store Choko and Bekido values in the data dictionary
data['Choko%d' % (14 + i)] = chokoValue
data['Bekido%d' % (14 + i)] = bekidoValue
Since it's in a report I don't know how to go about the debugging process due to the need for the tag historian Query I am using. What I am doing is looking at the XML of the report and seeing what values are being assigned.
<Bekido15>0</Bekido15>
<Bekido16>0</Bekido16>
<Bekido17>0</Bekido17>
<Bekido18>0</Bekido18>
<Bekido19>0</Bekido19>
<Bekido20>0</Bekido20>
<Bekido21>0</Bekido21>
<Bekido22>0</Bekido22>
<Bekido23>0</Bekido23>
<Choko15>0</Choko15>
<Choko16>0</Choko16>
<Choko17>0</Choko17>
<Choko18>0</Choko18>
<Choko19>0</Choko19>
<Choko20>97.6936856247201
<Choko21>0</Choko21>
<Choko22>0</Choko22>
<Choko23>0</Choko23>
These are the values I am getting. None of my combined vales or goodpcs values are 0.
<combined24HR15>179</combined24HR15>
<combined24HR16>172</combined24HR16>
<combined24HR17>58</combined24HR17>
<combined24HR18>51</combined24HR18>
<combined24HR19>269</combined24HR19>
<combined24HR20>412.0</combined24HR20>
<combined24HR21>104</combined24HR21>
<combined24HR22>378</combined24HR22>
<combined24HR23>48</combined24HR23>
<goodParts24HR15>12285</goodParts24HR15>
<goodParts24HR16>17008</goodParts24HR16>
<goodParts24HR17>7309</goodParts24HR17>
<goodParts24HR18>13133</goodParts24HR18>
<goodParts24HR19>11176</goodParts24HR19>
<goodParts24HR20>17452</goodParts24HR20>
<goodParts24HR21>13005</goodParts24HR21>
<goodParts24HR22>13561</goodParts24HR22>
<goodParts24HR23>16062</goodParts24HR23>
Is there some simple fix I am missing? Or is there some data error I have not found yet?
The modulo indexing syntax looks very strange.
goodPartsValue = data['goodParts24HR%d' % (14 + i)]
How is it supposed to work?
Can you post the data
so we can try your script.
The reason for the indexing is because I'm starting from 15. So I take 14 + i to go from 15 to 23.
The data is in the xml. I am pulling it from a tag historian query data source. There is no hard code for the values since they change daily.
Oh, yes. I only came across this once. It's modulo string formatting with a decimal conversion specifier so ['goodParts24HR%d' % (14 + i)]
results in,
['goodParts24HR15']
['goodParts24HR16']
...
['goodParts24HR25']
Carry on then ...
chokoValue = (goodPartsValue / (goodPartsValue + combinedValue)) * 100
Is that doing integer division, by any chance? Notice that 24HR20 = 412.0 (a float) and this gives a non-zero result.
Try
chokoValue = (goodPartsValue * 1.0 / (goodPartsValue + combinedValue)) * 100
to force float.
chokoValue = ((goodPartsValue* 1.00) / (goodPartsValue + combinedValue)) * 100
results in
<Bekido15>69.41527291585635</Bekido15>
<Bekido16>69.41527291585635</Bekido16>
<Bekido17>69.41527291585635</Bekido17>
<Bekido18>69.41527291585635</Bekido18>
<Bekido19>69.41527291585635</Bekido19>
<Bekido20>69.41527291585635</Bekido20>
<Bekido21>69.41527291585635</Bekido21>
<Bekido22>69.41527291585635</Bekido22>
<Bekido23>69.41527291585635</Bekido23>
<Choko15>99.70204841713222</Choko15>
<Choko16>99.70204841713222</Choko16>
<Choko17>99.70204841713222</Choko17>
<Choko18>99.70204841713222</Choko18>
<Choko19>99.70204841713222</Choko19>
<Choko20>99.70204841713222</Choko20>
<Choko21>99.70204841713222</Choko21>
<Choko22>99.70204841713222</Choko22>
<Choko23>99.70204841713222</Choko23>
But apparently
goodPartsValue = float(data[goodPartsKey])
combinedValue = float(data[combinedKey])
works fine
<Bekido15>53.092182030338385</Bekido15>
<Bekido16>73.50360862612905</Bekido16>
<Bekido17>31.587363325986427</Bekido17>
<Bekido18>56.756990362591296</Bekido18>
<Bekido19>48.299407926012364</Bekido19>
<Bekido20>75.42244695103506</Bekido20>
<Bekido21>56.203811746402174</Bekido21>
<Bekido22>58.60668136047366</Bekido22>
<Bekido23>69.41527291585635</Bekido23>
<Choko15>98.56386392811297</Choko15>
<Choko16>98.9988358556461</Choko16>
<Choko17>99.21270530745215</Choko17>
<Choko18>99.61316747572816</Choko18>
<Choko19>97.64962865880297</Choko19>
<Choko20>97.6936856247201</Choko20>
<Choko21>99.20665191852926</Choko21>
<Choko22>97.2881842312935</Choko22>
<Choko23>99.70204841713222</Choko23>
Good catch. They are all integer value type tags. I'm not sure why one got put in as a float while others did not.