Home Correct formating and re-use of code blocks

Correct formating and re-use of code blocks

Marct
1#
Marct Published in 2018-01-12 18:16:20Z
 So I had a requirement to build out a script that accessed an API to pull reports from a firewall, below is the hash job I made, for those of you who are fluent in python please be kind, I realize I am probably breaking every rule there is. So the questions: (the script is available here - https://github.com/Marct27/PAN_reporter) I have a limited understanding of functions I have noticed that I am re-using certain parts of the script that I could pass arguments to. #Parse xml and get root key value tree = ET.parse(resp) root = tree.getroot() #From return xml find job id of report j=root.find('status') jID=j.find('job')  I would assume that the function would look something like this:  def parseXML(arg1, arg2): #Parse xml and get root key value tree = ET.parse(resp) root = tree.getroot() #From return xml find job id of report j=root.find(arg1) jID=j.find(arg2) print 'Job ID :', jID.text return jID  However when I try to call the function with: parseXML('status','job')  I end up with the following error: NameErrorTraceback (most recent call last) in () 62 time.sleep(5) 63 #From xml get job status ---> 64 apiPullReport = '/api/? type=report&action=get&job-id=%s&key=%s' % (jID.text, apiKey) 65 req = urllib2.Request(host+apiPullReport, head) 66 resp = urllib2.urlopen(req, context = ctx) NameError: name 'jID' is not defined  Do I need to declare a global variable that the function would up date or is there another way for the variable to be defined ? Should I be looking at using a class (not sure if that is the right term) for something like this? or what would the suggested correct formatting be to make it more "python correct"
Marct
2#
 As per the comment by coldspeed for pointing out that the variable required to be returned by the function needs to be defined in order to use it. jID = parseXML('status','job') first, and then you can use it outside its function. – cᴏʟᴅsᴘᴇᴇᴅ 53 mins ago Updated the guthub files if anyone wants to review. Thanks again for the help