pHorseSpec pHorseSpec - 7 months ago 13
Python Question

Global Variables Passed into a Function Parameter List

I'm having difficulty with my

parseTwoPoleBreakers
function. I'm trying to input a list
inputList
into the function, so I know where to place the
brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c
, within that list. I get the following error when I run the script below
NameError: global name 'brk1_int_c' is not defined
. I'm pretty sure I define my variables with the line,
global brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c
, but I may be wrong, so any input as to how to get those variables defined and into the list
inputList
would be greatly appreciated.

#purpose2: To read in the 21 2 pole breaker panel data sheets, and parse them into the correct format, so they can be imported into the database.


import numpy as np
import pandas as pd



# 1. input data and store in data frame
with pd.ExcelFile("C:/Users/Documents/original.xlsx",
sheetname=['pnl1 Data ','pnl2 Data','pnl3 Data','pnl4 Data']) as xlsx:
df1 = pd.read_excel(xlsx, 'pnl1 Data ', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])
df2 = pd.read_excel(xlsx, 'pnl2 Data', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])
df3 = pd.read_excel(xlsx, 'pnl3 Data', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])
df4 = pd.read_excel(xlsx, 'pnl4 Data', skiprows=9, parse_cols="B:H", keep_default_na='FALSE', na_values=['NULL'])

# 2. convert data into a single row, in the correct order, and into csv format
df1numOfRows = len(df1.index)

# 2a. you need to look at the breaker table to see what the highest breaker id is and then use the number one higher than that
#fi_breakerTable = open(document,"r")

# 2b. store variables in a list that will be written to csv
breakerid=0
deviceid=0
NULL="NULL"

# 2bc. FDC Feed Braeker "List"
g_int_c=str(df1['Unnamed: 1'][6])
g_ext_c=str(df1['Unnamed: 2'][6])
breakertype="NULL"#should I keep this null or does it equal "FDC Feed Breaker"?
n_int_c=str(df1['Unnamed: 1'][5])
n_ext_c=str(df1['Unnamed: 2'][5])
a_int_c=str(df1['Unnamed: 1'][2])
a_ext_c=str(df1['Unnamed: 2'][2])
b_int_c=str(df1['Unnamed: 1'][3])
b_ext_c=str(df1['Unnamed: 2'][3])
c_int_c=str(df1['Unnamed: 1'][4])
c_ext_c=str(df1['Unnamed: 2'][4])
a_b=str(df1['Unnamed: 2'][0])
b_c=str(df1['Unnamed: 6'][0])
c_a=str(df1['Pnl 1 Data'][0])
fdcFeedBreakerList = [g_int_c,g_ext_c,NULL,NULL,n_int_c,n_ext_c,NULL,NULL,a_int_c,a_ext_c,NULL,NULL,b_int_c,b_ext_c,NULL,NULL,c_int_c,c_ext_c,NULL,NULL,a_b,c_a,b_c,NULL,breakertype,deviceid,breakerid] #3 pole breake riwht monitoring, an nuetral and a group (current & voltage)
breakerid+=1
fdcFeedBreakerString=",".join(map(str,fdcFeedBreakerList))
fdcFeedBreakerString=fdcFeedBreakerString+"\n"
print fdcFeedBreakerString


# 2bc. FDC Feed Braeker "List"
def parseTwoPoleBreakers(breakerStartVal1,breakerStartVal2,inputList,i):
global brk1_int_c,brk1_ext_c,brk2_int_c,brk2_ext_c
additionValue=(i*6)
#2 Pole breaker with monitring (current only) (a-b)
aStartVal=breakerStartVal1
bStartVal=breakerStartVal2
if i>=1:
aRowNum=aStartVal
bRowNum=bStartVal
else:
aRowNum=aStartVal+additionValue
bRowNum=bStartVal+additionValue
brk1_int_c=str(df1['Unnamed: 1'][aRowNum])#starts at row 7,13,19,25,31,37,43,49. addition of 6
print aRowNum
brk1_ext_c=str(df1['Unnamed: 2'][aRowNum])
brk2_int_c=str(df1['Unnamed: 1'][bRowNum])
brk2_ext_c=str(df1['Unnamed: 2'][bRowNum])
#two_pbwmco_a_b=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,a_int_c,a_ext_c,NULL,NULL,b_int_c,b_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]
two_pbwmco_a_b=inputList
breakerid+=1
two_pbwmco_a_bString=",".join(map(str,two_pbwmco_a_b))
two_pbwmco_a_bString=two_pbwmco_a_bString+"\n"
print two_pbwmco_a_bString

print breakerid

two_pbwmco_a_bList2=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,brk1_int_c,brk1_ext_c,NULL,NULL,brk2_int_c,brk2_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]
two_pbwmco_b_cList2=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,brk1_int_c,brk1_ext_c,NULL,NULL,brk2_int_c,brk2_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]
two_pbwmco_c_aList2=[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,brk2_int_c,brk2_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,brk1_int_c,brk1_ext_c,NULL,NULL,NULL,NULL,NULL,NULL,breakertype,deviceid,breakerid]
i=0
while (i<7):
parseTwoPoleBreakers(7,8,two_pbwmco_a_bList2,i)
parseTwoPoleBreakers(9,10,two_pbwmco_b_cList2,i)
parseTwoPoleBreakers(11,12,two_pbwmco_c_aList2,i)
i+=1

Answer

If it is supposed to be "global", you may have to first define it directly in the file, not in a function.

Try to put something like that at the beginning of your code file :

brk1_int_c = ''
brk1_ext_c = ''
brk2_int_c = ''
brk2_ext_c = ''
Comments