0

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.

ynot269
  • 305
  • 2
  • 14
  • If, inside b.py you have a function `bfunc()`, then from inside a.py you can `import b` and call `b.bfunc()`. Alternatively, you could specify the functions you want to import into a's namespace (`from b import bfunc`) and then just call `bfunc()` as you would inside b.py. If you want *all* symbols from b you could use `from b import *`. (See [this](http://stackoverflow.com/q/15696461/736937) related). A different approach, likely unnecessary in your case, would be to dump the JSON to a file, and load it from within b.py. – jedwards Jul 30 '16 at 23:16
  • actually ive been dumping the json into a json file, how would this work with passing arguments into main without using cmd line, lets say i want to call b.main() (and main prompts for input of some sort) however I want to pass data from the json generated instead of prompt for input. – ynot269 Jul 30 '16 at 23:20
  • Sounds like some refactoring of your code is in order. Because of space, I'll throw a silly example in an answer of what I mean about refactoring. – jedwards Jul 30 '16 at 23:25

1 Answers1

2

(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.

jedwards
  • 29,432
  • 3
  • 65
  • 92