简体   繁体   中英

calling python function with arguments using tcl script

I have a python script (sample.py) that has a function with 3 arguments. where 1st two arguments are in hex and 3rd is the string.

def sampleFun(gen_lane,sw_state,test_name):`
    
    ### code segment 

    output = "some string"
    return output

I have to use tcl script to call this function sampleFun present in sample.py.

How to do that? I have tried this tcl command:

proc call_python { } {
    set gen_lane 0x10500000
    set sw_state 0x0000000B
    set test_case "linkup"
    set result [exec python -c "import sample; print sample.sampleFun($gen_lane,$sw_state,$test_case)"]
    puts $result

}

But I am getting the error that the name "linkup" is not defined.

So, how to pass a string argument to the python function from tcl?

You need to quote it for Python.

For an almost-arbitrary value like that, you'd try this:

set result [exec python -c "import sample; print sample.print_file($gen_lane,$sw_state,r'''$test_case''')"]

This uses the fact that ''' -quoted strings in Python can contain newlines and single ' characters, and r strings can contain backslashes. The cases remaining that could possibly cause problems are rare.

You are aware that your example Python code said sampleFun but your test code said sample.print_file ? I assume you can fix that up…

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