Dec-08-2018, 11:47 AM
If you use classes, you can have a master script that instantiates an instance of the class in each of the other modules which will make all of the class methods reachable from the master.
The methods that create the lists can contain a return statement with attribute of the lists
then you can merge in the master.
Here's a (very simplistic) example:
script1.py:
The methods that create the lists can contain a return statement with attribute of the lists
then you can merge in the master.
Here's a (very simplistic) example:
script1.py:
import os class Script1: def __init__(self): ''' the following statement assures that you are in the directory where all scripts are located. The proper way to do this is to use __init__.py, but this will work for this example. ''' os.chdir(os.path.abspath(os.path.dirname(__file__))) def list_1(self): self.list = [1,2,3,4] return self.listScript2.py
import os class Script2: def __init__(self): ''' the following statement assures that you are in the directory where all scripts are located. The proper way to do this is to use __init__.py, but this will work for this example. ''' os.chdir(os.path.abspath(os.path.dirname(__file__))) def list_2(self): self.list = [7,8,9,10] return self.listMaster.py
import os import Script1 import Script2 class Master: def __init__(self): ''' the following statement assures that you are in the directory where all scripts are located. The proper way to do this is to use __init__.py, but this will work for this example. ''' os.chdir(os.path.abspath(os.path.dirname(__file__))) self.script1 = Script1.Script1() self.script2 = Script2.Script2() def merge_lists(self): newlist = self.script1.list_1() + self.script2.list_2() print('The new list: {}'.format(newlist)) if __name__ == '__main__': mm = Master() mm.merge_lists()running Master.py:
Output:(try_venv) ../TryStuff $ python ../Master.py
The new list: [1, 2, 3, 4, 7, 8, 9, 10]
(try_venv) ../TryStuff $