View Full Version : combining scripts
WilliamM
05-29-2008, 11:49 PM
I have written several scripts that need to be collected into one. Individually, they implement parts of the total functionality we need. However, I would like to keep the scripts seperate so that they are easier to edit and are organized within Portal. Thus, the best way to me seemed to be to import all of the desired scripts into a "compilation" script.
First, is this the correct way to do it?
from myScriptName import *
The portal compilation loads fine, however it doesn't keep the imported scripts seperated within Portal Pro, but lumps all of their functions together. When I try to load the node compilation I get the following error:
name 'snappyGen' is not defined
Also I am wondering if there will be conflicts due to the use of the same hooks in seperate imported scripts (ie: startup hook, 100ms hook, etc).
kbanks
05-30-2008, 09:07 AM
Spreading functionality across multiple source files is a good idea.
from someFile import *
...is the correct way to include/use one SNAPpy file from another one. You can see this used throughout the example scripts, as we import files like pinWakeup.py and evalBase.py.
In your Portal scripts, you can use all the forms of import.
import someFile
... makes the functions in someFile.py callable, by specifying the full name
someFile.foo()
someFile.bar()
from someFile import *
...lets you call the functions in someFile.py without having to prefix them.
foo()
bar()
I'm simplifying things here, refer to the standard Python docs for the full story...
The portal compilation loads fine, however it doesn't keep the imported scripts seperated within Portal Pro, but lumps all of their functions together.
I see what you mean about all the functions being lumped together. Perhaps we can do the same sort of "by module" breakdown we use for the SNAPpy scripts. (I will put your idea in our "feature ideas" queue).
When I try to load the node compilation I get the following error:
name 'snappyGen' is not defined
Also I am wondering if there will be conflicts due to the use of the same hooks in seperate imported scripts (ie: startup hook, 100ms hook, etc).
A given hook can only have one function associated with it.
I suggest you have all your hook setup in one master file, and have it import all the other files (don't have any hook setup in the other files)
WilliamM
05-31-2008, 12:28 PM
I did what you said about the hooks and now everything uploads fine. The nodes do their part (if I call their functions from the Node Info window) and don't seem to have any trouble communicating with Portal. But if I try to call a function on Portal that requires it to rpc the nodes I get the following error in the Event Log and it doesn't go any further than the rpc.
"An error occurred while trying to call the function findBridge on node Portal"
Another thing - I'm assuming I can't have variables of the same name in my imported scripts (unless intended). I've checked my current scripts to make sure this wouldn't be a problem, but I was wondering - do you guys at Synapse use any particular notation to help ensure this doesn't happen? It worries me especially because everything in python is declared as global. And python's use of "global" is very different!
WilliamM
06-02-2008, 01:25 AM
The portal script I was trying to build works fine if I simply copy + paste the individual scripts into a new one. (Not very convenient, but it works)
I used the "from someFile import *" method before to import the scripts... don't know what the problem could have been.
kbanks
06-02-2008, 09:41 AM
If you post (or email me) the set of scripts that did not work when you used multiple files, we will try to recreate your symptoms.
kbanks
06-02-2008, 09:46 AM
Do you guys at Synapse use any particular notation to help ensure this doesn't happen?
We don't have any sort of formal standard, but you could use a convention that incorporated the module name as a prefix or suffix (sort of a low-budget "namespaces")
In module foo.py
foo_var1 = 1
foo_var2 = 2
In module bar.py
bar_var1 = 3
bar_var2 = 4
In module mymain.py
from foo import *
from bar import *
print foo_var1
print bar_var1
vBulletin® v3.8.0, Copyright ©2000-2012, Jelsoft Enterprises Ltd.