Home > Mobile >  I want to use my variables in one method in another method
I want to use my variables in one method in another method

Time:07-01

I want to use the variables in my "Person Builder" function in the JSON I will create, but I cannot pull variables like "PersonID" in the "jsonAPI" function. How do I solve this problem?

My Code :

def PersonBuilder():
        PersonID = 1
        PersonName = "Behzat"
        PersonSurname = "Çözer"
        PersonCompany = "EGM"
        PersonTitle = "Başkomiser"
        return {'PersonID': PersonID,'PersonName': PersonName, 'PersonSurname': PersonSurname,'PersonCompany': PersonCompany, 'PersonTitle': PersonTitle}

def PhoneNumberBuilder():
        PhoneID = 1
        PhoneCountry = "Turkey"
        PhoneOperator = "TR XXXXXX"
        PhoneNumber = " 905XXXXXXXX"
        return {'PhoneID': PhoneID,'PhoneCountry': PhoneCountry, 'PhoneOperator': PhoneOperator,'PhoneNumber': PhoneNumber}

def jsonAPI():

    myjson3 = {  
        "Person":{ 
            'PersonID' : PersonID,
            'PersonName' : PersonName,
            'PersonSurname': PersonSurname,
            'PersonCompany': PersonCompany,
            'PersonTitle': PersonTitle,
            'PhoneID':PhoneID,
            'PhoneCountry': PhoneCountry,
            'PhoneOperator':PhoneOperator,
            'PhoneNumber':PhoneNumber
            }
    }

    out_file = open("myfile.json", "w") 
    json.dump(myjson3, out_file, indent = 6)
    jsonify(myjson3)

if __name__ == "__main__":

CodePudding user response:

Call the methods and use the values they return.

def jsonAPI():

    myjson3 = PersonBuilder() | PhoneNumberBuilder()

    # ... etc

CodePudding user response:

The functions return dictionaries. You can combine these two dictionaries to get the Person dictionary in myjson3.

def jsonAPI():
    myjson3 = {
        "Person": PersonBuilder() | PhoneNumberBuilder()
    }
    with open("myfile.json", "w") as out_file:
        json.dump(myjson3, out_file, index = 6)
        jsonify(myjson3)
  • Related