简体   繁体   中英

How can i run a python 2.x function from a 3.x built with arguments? PSS/E

Hi I'm having a function that looks like it is only working in a 2.x (2.7) system. But the rest of my program is written in python 3.4

The file a.py (version 2.7) was a script i could run in 2.7 by calling the script as:

import psspy    
openPath='busSystem.raw'
saveToPath='busSystem_out.raw'

#Open a case file
psspy.read(0,openPath)
do some calculation...

#Save to another case file
psspy.rawd_2(0,1,[1,1,1,0,0,0,0],0,saveToPath)

And then calling the following code from python 3.4 in b.py workes

import os
os.system('c:\python27\python a.py')

But then I wanted to change the script in a.py to be a function with kwargs such as:

def run(openPath='busSystem.raw',saveToPath='busSystem_out.raw')
    #Open a case file
    psspy.read(0,openPath)
    do some calculation...

    #Save to another case file
    psspy.rawd_2(0,1,[1,1,1,0,0,0,0],0,saveToPath)
    do something more...

So I want to do something like

import os
in = 'busSystem.raw'
out = 'busSystem_out.raw'
os.system('c:\python27\python a.py run(in, out)')
# Or
os.system('c:\python27\python a.py run(openPath=in,saveToPath=out)')

So the question is:

  • how can I send parameters to another script's function?
  • can I use both args and kwargs?

I know if I could have run the script with python 3.4 i could have just imported the function as

from a import run
run(in,out)

My solution for this would be to read the whole python script as a string, use str.replace('busSystem.raw',in) and str.replace(''busSystem_out.raw',out) and save it back as a a_new.py and run it as mentioned before.

The script in a.py need to be in python version 2.7, because it is interacting with Siemens PSS/E 33, which only communicates through py2.7.

Function calls work only within a single process and, generally, only within a single language. So you have a script that can be run with no arguments. Now you want this script to process command line arguments. This has nothing, really, to do with function calls and keyword arguments.

You should read the Argparse Tutorial in the Python documentation. It introduces the concept of command line arguments, since you seem to be unfamiliar with it, and then shows some examples of using the built-in argparse module to do the hard parts of parsing the arguments.

Then you should read about the subprocess module. It will work better for you than os.system() .

Alternatively, you could update the script so that works correctly in Python 3. This is what I would start with.

Here is some untested example code.

In your existing script a.py

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('openPath')
parser.add_argument('saveToPath')

args = parser.parse_args()

openPath=args.openPath
saveToPath=args.saveToPath

# ... the rest of the existing script

In your other program:

import subprocess
in_ = 'busSystem.raw'
out = 'busSystem_out.raw'
subprocess.call([r'c:\python27\python', in, out])

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