Home Correct formating and re-use of code blocks
Reply: 0

Correct formating and re-use of code blocks

user2155
1#
user2155 Published in July 23, 2018, 1:45 pm

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)
    <ipython-input-4-03280b8af912> in <module>()
         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"
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.773433 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO