So I'm new at multiprocessing and subprocessing and I am not sure if I am doing it right.
I have two scripts. One runs the main GUI, and has buttons that run other scrips. I want my entry boxes to be read by my other script, so that it can change the axis of a graph.For now, I simplified it so that it can print it so I can see that the values are being passed to begin with.
When I run the scrips like this:
###class_testing.py### (main script)
class Amplifier_Data_Analysis:
def saving_graph_stuff(self):
global int_startfreq,int_stopfreq,float_steps,float_add_tick
STARTFREQUENCY = self.Start_Freq.get()
int_startfreq = int(STARTFREQUENCY)
STOPFREQUENCY = self.Stop_Freq.get()
int_stopfreq = int(STOPFREQUENCY)
STEPS = self.Steps.get()
float_steps = float(STEPS)
ADD_TICK = self.Add_Tick.get()
float_add_tick = float(ADD_TICK)
print(int_startfreq,int_stopfreq,float_steps,float_add_tick)
return int_startfreq,int_stopfreq,float_steps,float_add_tick
def testreport(self):
subprocess.Popen([sys.executable,'test.py'])
###test.py###
from class_testing import *
int_startfreq,int_stopfreq,float_steps,float_add_tick = Amplifier_Data_Analysis.saving_graph_stuff()
print(startfrequency)
print(stopfrequency)
I get
int_startfreq,int_stopfreq,float_steps,float_add_tick = Amplifier_Data_Analysis.saving_graph_stuff()
TypeError: saving_graph_stuff() missing 1 required positional argument: 'self'
But when I put self, It says it is not defined which makes sense since it's a different script from the main. The GUI is generated form the PAGE app so it's very lengthy, but this is how it looks like: GUI
How do I pass or read variables between two scripts?
It's a class - you have to initialize it
int_startfreq,int_stopfreq,float_steps,float_add_tick = Amplifier_Data_Analysis().saving_graph_stuff()
Related
So, I started python 2 weeks ago.
Something important bugs me, what I just don't understand.
When I have functions why my code stop running? Without defs it's working.
In my example I can't call just kezdo(), because i get an empty console.
Process finished with exit code 0
Anyone can explain it with my example code?
def kerdes(self):
self.open1 = self.open("kerdesek.txt")
self.open2 = self.open1.read()
print(self.open2)
return kezdo(self)
def kezdo(self):
print(self.open2)
user = int(input("valasz egy kerdest: "))
kerdesek = self.open2
valaszok = ("egyvalasz", "ketvalasz")
kv = {kerdesek[user]: valaszok[user]}
print(kv)
if I add kezdo()
then I get an error:
Error: kezdo() missing 1 required positional argument: 'self'
Thanks!
you should call the functions. like this:
kerdes()
kezdo()
I can only take a guess based on the limited information but it appears as if you are defining these methods (kezdo() and kerdes()) in some class.
In that case, you need to use self when calling these methods, or Python won't know what you mean.
It needs to be something along the lines of:
def kerdes(self):
self.open1 = self.open("kerdesek.txt")
self.open2 = self.open1.read()
print(self.open2)
return self.kezdo(self)
# ^^^^
# You need to use self here.
I am trying to get one boolean attribute (A) to change another(B). The one to be controlled (B) already has a script job running it and so I can't create a set driven key, direct connection, or expression to control it, so I'm trying another script job, since running the script function by itself achieves the desired result. I just can't figure out how to tie that script to run to the attribute change (B) that I want to drive it by (A).
This is placed in a script node set to the open gui trigger (to load when maya opens as I understand it). Here's a screenshot.
What am I missing here?
import maya.cmds as cmds
def togglePicker(pickerAttr):
cmds.setAttr(pickerAttr, not 0)
nameSpace = cmds.ls(sl=True)[0].rpartition(':')[0]
if len(nameSpace) > 0:
pickerAttr = nameSpace + ':Main.picker'
myPickerAttr = nameSpace + ':MoverMain_Cntrl.Picker'
else:
pickerAttr = 'Main.picker'
myPickerAttr = 'MoverMain_Cntrl.Picker'
cmds.scriptJob(attributeChange=[myPickerAttr,togglePicker])
Your script node is executed every time when maya loads a scene, not when it is started, at least that's what the docs say. So every time you load a scene, a new scriptJob is created.
Your script should show an error message since the togglePicker() function is called without an argument, but it requires an argument. Even if it works, it will not work.. what you do at the moment is the following:
As soon as you turn on the MoverMain_Cntrl.Picker attribute, the togglePicker() function is called and turns it on, even if you turn it off. The pickerAttrvariable is not used. So you should have a look at your program logic.
You can solve the agrument problem by using the partial function like this:
import maya.cmds as cmds
from functools import partial
def togglePicker(pickerAttr):
cmds.setAttr(pickerAttr, not 0)
nameSpace = cmds.ls(sl=True)[0].rpartition(':')[0]
if len(nameSpace) > 0:
pickerAttr = nameSpace + ':Main.picker'
myPickerAttr = nameSpace + ':MoverMain_Cntrl.Picker'
else:
pickerAttr = 'Main.picker'
myPickerAttr = 'MoverMain_Cntrl.Picker'
cmds.scriptJob(attributeChange=[myPickerAttr,partial(togglePicker, pickerAttr)])
I got it to work! (previously I had switched to the script node to MEL so I could test the mel command mentioned in the comments that did work, but I forgot to switch back to python when I realized the selection issue I also mentioned in the comments).
So here's what worked, where I know I'll have to manually change the namespace name in case the scene file name changes:
import maya.cmds as cmds
def togglePicker():
cmds.setAttr(pickerAttr, not 0)
if cmds.namespace(exists='ExtremeBallRig_v008'):
pickerAttr = 'ExtremeBallRig_v008:Main.picker'
myPickerAttr = 'ExtremeBallRig_v008:MoverMain_Cntrl.Picker'
else:
pickerAttr = 'Main.picker'
myPickerAttr = 'MoverMain_Cntrl.Picker'
cmds.scriptJob(attributeChange=[myPickerAttr,togglePicker])
Maybe I am completely off track here (and above my paygrade for sure), but what I want to do is to give users of my app (That I am writing in Python since that's the language I know) a python interpreter to control some objects within my app. Something similar like many 3D and VFX softwares have (Maya, Blender, Nuke). This is the code I got so far:
#main.py
import code
import networkx as nx
class Env():
def __init__(self):
self.graph = nx.graph.DiGraph()
# load library with functions that will be availabel for user inside the app.
import mylib
functions = {f: getattr(mylib, f) for f in dir(mylib) if not f.startswith('__')}
self._interpreter = code.InteractiveInterpreter(locals=functions)
def execute_node(self, node=None):
# In IRL the main object to be pass1ed to users' interpreter will be the self.graph object
# but I made it more clear for this question.
self._interpreter.locals['var'] = 42
node = "print(var)\nprint(some_function())\nvar = 67" # Let's pretend node object contains this code.
self._interpreter.runcode(node)
if __name__ == '__main__':
e = Env()
# some code, node creation and so on...
e.execute_code()
print(e.locals['var'])
#mylib.py
var = None # I have to put this here because if there is no variable function fails at import
def some_function():
print(var)
Output:
42 # This prints as expected
None # The print within the function prints the value that was there when module was initialized
67 # The last print returns expected value
So, it is clear that python interprets the functions on first import and "bakes" the global variables that it had at the import time. Now the question is can I somehow easily make it use the globals passed from the code.InteractiveInterpreter() or I should look for a completely different solution (and which one) :)? Of course the idea is that the two python programs should communicate, the user should use a special library to operate the software and the backend code should not be exposed to them. Do I make any sense? Thanks :)
This is the one-ish instance where you do want to use the exec() function, but please remember that the user may be able to run any Python code, including stuff that could run forever, mess up your main program, write (or delete) files, etc.
def run_code(code, add_locals={}):
code_locals = {}
code_locals.update(add_locals) # Copy in the additional locals so that dict could be reused
exec(
code,
{}, # no globals (you may wish to replace this),
code_locals,
)
return code_locals # return updated locals
class Beeper: # define a toy object
def beep(self, times):
print("Beep! " * times)
beeper = Beeper() # instantiate the object to play with
# Some user code...
user_code = """
x = 5
beeper.beep(x)
x += 3
"""
new_locals = run_code(user_code, {"beeper": beeper})
print(new_locals)
This outputs
Beep! Beep! Beep! Beep! Beep!
{'beeper': <__main__.Beeper>, 'x': 8}
So you can see we can use the locals the user has modified if need be.
Hello i am trying to make a simple GUI, i have a button which calls a function to import some excel data, i would then like to process those data in another function within the same class. Below is my code for the two functions, rhe first one imports the data, then i would like to use the data from OpendirREF in the function Confidens. Can anyone help?
def OpendirREF(self):
filePath_REF = str(QtGui.QFileDialog.getOpenFileName(self, 'Single File', '*.xlsx')) # \n *.txt')
fileHandle_REF = os.path.basename(filePath_REF)
data_REF = pd.read_excel(fileHandle_REF)
return data_REF
def Confidens(self):
imported_data = self.OpendirREF()
print imported_data
I believe your asking how to call the function in same class .if so
If you want to refer to it, something like this
classname.OpendirREF(self).
I'm new to python and am using Spyder's IDE. One feature I appreciate about it is it's variable explorer. However, based on some research, I found that it only shows global variables. A workaround that I found for that is by using the inspect module:
import inspect
local_vars = {}
def main():
global local_vars
a = 2
b = 4
c = a+b
local_vars = inspect.currentframe().f_locals
return c
main()
This works well, however, I have other functions that are called from within main() and I'd like to see those variables in the variable explorer as well. I mimicked what was done for the variables in the main function and the dict does not appear. I noticed that when I disable the setting to "exclude unsupported data types" in Spyder's variable explorer options, the second dict appears with the right size attribute, however, I am unable to open/view it. Any ideas on a possible work around? This is my first time posting BTW.
Thanks!!
Here is a working example of my issue and I've traced it down to pylab subplots.
import inspect, pylab
mainVars = {}
def main():
global mainVars
a = 1
b = 2
fig = pylab.figure()
subPlot = fig.add_subplot(211) ## line of interest
pylab.close('all')
mainVars = inspect.currentframe().f_locals
main()
When the line of interest is commented out, the dict is created successfully and can be viewed. It appears that the object created using fig.add_subplot() is not being handled properly by the dict. It seems to be an unsupported datatype.
Hope this helps clarify the issue.
Thanks again.
To view the contents of local variables when some of them are unsupported, you have to follow these steps:
Go to the options menu of the Variable Explorer (the last icon from left to right on it).
Select the option called Exclude unsupported data types.
Then you'll see all local variables saved in the f_locals dict, even if you're unable to double click on it.
All of these workarounds are making your code significantly harder to read for outsiders. You have two options to inspect the values of the variables inside your function. First, you could just return the variables you are interested in:
def main():
a = 2
b = 4
c = a+b
return a, b, c
a, b, c = main()
Second, if you just want to check that the function works as expected or debug it, you can debug the function and step into it. So select Run|Debug from the menu instead of running the file directly. Then you can step into the function - the values of the variables will be visible in the variable explorer when the execution is inside the function.