简体   繁体   中英

Using variables from a function in another python program

So I'm trying to write a python program to integrate into a greater in-house developed python application. This program I'm writing needs to generate an xml document and populate the fields with data stored in variables from another function in a different module.

After realizing I can't have both programs import each other (main program needs to call xmlgen.py to generate the xml doc, while xmlgen.py needs to utilize variables in the main program to generate that doc), I'm a little bit at a loss as to what to do here.

In the example shown below, xmlgen.py needs to use variables from the function sendFax in Faxer.py. Faxer.py needs to call xmlgen.py to generate the document.

snippet from xmlgen.py:

from lxml import etree
from Faxer import coverPage, ourOrg, ourPhonenum, ourFaxnum, emailAddr, sendReceipt, webAddr, comments
from Faxer import sendFax

def generateXml():
    #xml file structure
    root = etree.Element('schedule_fax')
...
~ A bunch of irrelevant xml stuff
...

    grandchild_recipient_name = etree.Element('name')
    grandchild_recipient_name.text = cliName
    child_recipient.append(grandchild_recipient_name)

Now the piece of the main program I need to utilize the "cliName" variable from...

def sendFax(destOrg, destFax, cliName, casenum, attachments, errEAddr, comment, destName):
    creds=requests.auth.HTTPBasicAuth(user,password)

    allData=''
    allData+='<schedule_fax>\n'

    allData+='<cover_page>\n'
    allData+='<url>'+prepXMLString(coverPage)+'</url>\n'
    allData+='<enabled>true</enabled>\n'
    allData+='<subject>'+prepXMLString(cliName)+' - case # '+str(casenum)+'</subject>\n'

Now when I try to import sendFax function from Faxer.py, I'm unable to call any of the variables from the function like,

grandchild_recipient_name.text = sendFax.cliName

does not work. What am i doing wrong here?? I'm not a python guru and am in fact quite new to all of this, so I'm hoping it's something simple. Should I just dump everything into a new function in the main program?

As pointed out above, you are trying to reference cliName as if it is an attribute of the function. This would be closer to being correct if sendFax was a class, but that's another subject. The snippet you have provided is simply a function definition. It doesn't guarantee that this function is ever actually used or give you any idea what cliName actually is, cliName is just the name used by the function internallt to describe the 3rd value supplied as input.

What you need to do is find where sendFax is actually used, rather than where it is defined. Then look at what the variables are called which are passed into it. There are two ways to pass variables into a function: by position and by name. If the variables are being passed by pposition you will find something like:

sendFax(some_name,some_other_name,yet_another_name,...

The third one of these will be the variable which becomes cliName inside the function.

If being passed by name you will see something like

sendFax(cliName=yet_another_name,...

Where once again yet_another_name is the thing which becomes cliName.

Depending on how the programme is structured you may be able to refer to yet_another_name from your program and get the value you need.

from Faxer import yet_another_name

But this will only work if Faxer runs and finishes with the one and only value of yet_another_name assigned. If Faxer iterates through lots of values of yet_another_name, or simply doesn't run sensibly when called as an import you'll need a more sophisticated approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM