ynot269 ynot269 - 1 year ago 42
Python Question

Running a python script from another script while passing args from another script/json generated by script?

So I have 3 scripts, for the sake of simplicity lets call them a.py,b.py, and c.py

now a.py creates a json containing some data i want to pass to b which uses this data to create another json containing another related subset of data
and I want to do this action from c.py. how can I go about getting b to read from the json generated by a, and then pass the json generated by b into c. I've thought about importing like so

import a
import b

id assume I would import functions from b, but realistically I need all the functions within b, would I just import all functions from b and pass the arguments normally or would there be another way to handle this? For a little more context,

c.py will create a directory that the json from b.py will be stored in.
also will i need an init.py in my working directory to do these imports, they are all in the same directory.

Answer Source

(From comments to question)

Consider the following files:

#-- b.py ----------------------------------------------------------------------------------
def main():
    name = input("Enter your name: ")         # Python 3, use raw_input for Python 2
    print("Your name is:", name)

#-- a.py ----------------------------------------------------------------------------------
import json

# Some method of generating a json
j = json.dumps({'name': 'Alice'})

And you want to have b.py's main function use the data in the json that is generated in a.py, instead of what main prompts the user for.

In other words, in the above example, you'd want to have the program print Your name is: Alice without prompting the user for input.

If that's the case, you might refactor your code to look something like:

#-- b.py ----------------------------------------------------------------------------------
def process(name):
    print("Your name is:", name)

def get_user_input():
    name = input("Enter your name: ")           # Python 3, use raw_input for Python 2
    process(name)                               # Prints: "Your name is: (whatever)"

#-- a.py ----------------------------------------------------------------------------------
import json
from b import process

# Some method of generating a json
j = json.dumps({'name': 'Alice'})

# (We created a json for no real reason, parse it back into a dict to access it)
d = json.loads(j)

# Call b.py's process function, and pass it the name in the json
process(d['name'])                              # Prints: "Your name is: Alice"

The "trick" here is to separate "jobs" that the main function does into two separate functions.

  • The first function (process), is given a name and "processes" it. In the example, all it does is print it out.
  • One second function (named get_user_input in the refactoring) gets the user's input and passes it to the first function.

After doing this, you can skip the user input (since you don't need it, you want to use the JSON data) and call process directly, even from other files.