简体   繁体   中英

How to pass a string variable from one function to another?

If i wanted let's say to execute test1 and to pass the result to the excel and then execute test2 and then pass the result, how would i do it? The way it is now all the tests will have to be executed together. Is that possible?

import os
import xlswriter
from datetime import datetime
import time


def save_results():
    os.chdir(r'C:\Users\vrozakos\Documents\10P_Results')
    workbook = xlsxwriter.Workbook(datetime_output_results+'.xlsx')
    worksheet = workbook.add_worksheet()
    bold = workbook.add_format({'bold': True})
    worksheet.set_column('A:A', 20)
    worksheet.write('B1', test_1())
    worksheet.write('B2', test2())
    workbook.close()



def test1():
    output = str(ser.read(1000).decode())
    output = str(output)
    if "0x1" in output :
        print('Pass')
        return 'Pass'
    else:
        print('Fail')
        return 'Fail'


def test2():
    output2 = str(ser.read(1000).decode())
    print(output2)
    test2_output = str(output2)
    if "0x1" in test2_output:
        print('Pass')
        return 'Pass'
    else:
        print('Fail')
        return 'Fail'

It's really quite simple, not a lot has to change:

def save_results():
    os.chdir(r'C:\Users\user\Documents\Results')
    workbook = xlsxwriter.Workbook(datetime_output_results+'.xlsx')
    worksheet = workbook.add_worksheet()
    bold = workbook.add_format({'bold': True})
    worksheet.set_column('A:A', 20)
    worksheet.write('A1', test1())
    worksheet.write('B1', test2())
    workbook.close()

Notice I just called the functions where you need their results. Other variables that are missing here I assume are already defined elsewhere in your code.

EDIT:

Alternatively, as I understand from your comments, you can call save_results from within each test, only when it is run, but you have to make save_results accept some arguments:

def save_results(result, target_cell):
    os.chdir(r'C:\Users\vrozakos\Documents\10P_Results')
    workbook = xlsxwriter.Workbook(datetime_output_results+'.xlsx')
    worksheet = workbook.add_worksheet()
    bold = workbook.add_format({'bold': True})
    worksheet.set_column('A:A', 20)
    worksheet.write(target_cell, result)
    workbook.close()

def test1():
    output = str(ser.read(1000).decode())
    output = str(output)
    if "0x1" in output :
        result = 'Pass'
    else:
        result = 'Fail'
    print(result)
    save_results(result, 'A1')

def test2():
    output2 = str(ser.read(1000).decode())
    print(output2)
    test2_output = str(output2)
    if "0x1" in test2_output:
        result = 'Pass'
    else:
        result = 'Fail'
    print(result)
    save_results(result, 'B1')

However you should note, calling your save_results adds a new worksheet every time it is called, you probably want to modify this function to write in the same worksheet every time (but that is the subject for another question on the matter).

Like I said, your code has a lot of problems, but the basic logic here (in both of my examples) is that there is no problem calling functions inside one another to get their results (or pass arguments to them).

The best solution seems to be this one, you should follow this:

def save_results():
    os.chdir(r'C:\Users\user\Documents\Results')
    workbook = xlsxwriter.Workbook(datetime_output_results+'.xlsx')
    worksheet = workbook.add_worksheet()
    bold = workbook.add_format({'bold': True})
    worksheet.set_column('A:A', 20)
    worksheet.write('A1', test1())
    worksheet.write('B1', test2())
    workbook.close()
def save_results(result):
    os.chdir(r'C:\Users\vrozakos\Documents\10P_Results')
    workbook = xlsxwriter.Workbook(datetime_output_results+'.xlsx')
    worksheet = workbook.add_worksheet()
    bold = workbook.add_format({'bold': True})
    worksheet.set_column('A:A', 20)
    worksheet.write('A1', 'PLL TEST', bold)
    worksheet.write('B1',result) --> result is string value from pll_test function
    workbook.close()

def pll_test():
    output = str(ser.read(1000).decode())
    alarm_output = str(output)
    if "pll reg=0x1 val=0x3f" in alarm_output :
        save_results("pass") --> pass value to function
        print('Pass')
    else:
        save_results("fail")
        print('fail')

You can receive the value in your main function from pll_test function. You have to add the line as return after the print statement if you intend to return after the print statement.

Now the return value then passed as argument to call save_results function.

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