how to access python variables from outside the script - python

I am using a python script that runs in a curses wrapper loop. I would like to have a conky display show the values of various str vars in the script, updating every second. I know i could have the python script write data to a text file and have conky read the file, but that seems inefficient and unscalable. What are some ways to do this without text files? I imagine using sockets or pipes and a helper script called from conky like this
${exec "helper.py --get-variable=var_a"}
I havent found much on how to do this. I am on manjaro linux. What is the best way?

Related

Maya Python creating a script with a script

I've got a kind of weird question--but would be immensely useful if it is possible--in Maya using Python, can I take in several points of user input and have Python create a separate script for me? In this instance, I want to take in controller and locator names and have Python spit out a complete IKFK match script also in Python (it's really just a lot of getAttr and setAttr commands, although with 6 if statements per limb for PV matching.) The only other wrinkle there is that it has to be able to prefix hierarchy names in the script if necessary if the rig is imported into a new scene rather than just opened. There's an expression component to my switches that it would be nice if Python could make for me, too.
Is this possible or am I crazy?
That's no problem. Just write a textfile with a .py extension into a path where maya can find it. Then you have to import it somewhere. Creating expressions is not a problem either.
Maybe it could make sense to think about the approach you choose. Imagine you have written a dozen of these new python files and you discover a problem in the script, you will have to redo it. I'd try to collect all data and only write the required informations into a textfile e.g. in json format. Then you can read the data and rebuild your skeletons.

How to start and stop scripts using another script?

I don't have a lot of experience coding so I'm sorry if this has been answered before; I couldn't find anything that helped.
I just completed a project on a Raspberry Pi that runs some RGB LED strips via PWM. I have a program that runs the lights and works fine with a few different modes (rainbow shifting, strobe, solid color) but with each new mode I add the program get longer and more convoluted. I would like to have each separate mode be its own script that gets started or stopped by a sort of master script. That way I could easily add a new mode by simply writing a separate program and adding it to the list on the master script instead of mucking around inside a giant program with everything in it and hoping I don't break something. I guess what I want is a simple way to start a python script with some specific setting (Determined by variables passed from the master script) and be able to kill that script when the master script receives the command to change modes.
Keeping your code modulable is indeed a good practice ! If your code is not Objet oriented, the best way is to create another python file (let's call it util.py) in the same directory as your "main". You can simply include util.py with the following command at the beginning of your main code :
import util
And then when you want to use a function that you've defined in your util.py file, juste use :
util.myFunction(param1, param2,...)

How can I call a python script with arguments from within Processing?

I have a python script which outputs a JSON when called with different arguments. I am looking for a way to call that script from within Processing and load the output using something like loadJSONObject()
The problem is that I don't know how to call the python script with arguments from within Processing.
Any tip will be appreciated, thanks!
One option, as pointed out in the comments, is to use open, and then load the file that generates the normal way.
Another -arguably much better- way is to not do this and to run your python script as services with a web interface instead, so that your python scripts sits listening on http://localhost:1234, for instance, and your Processing sketch can simply load a file "http://localhost:1234/somefile?input=whatever" and not even care what is actually generating the content.
The upside there is also that you can run your script anywhere that can be reached via URLs, and those things don't need to rely on python being available as an executable.

How to run multiple files successively in a single process using python

I am working in Windows, and just learning to use python (python 2.7).
I have a bunch of script files ("file1.script", "file2.script", "file3.script"....) that are executed in TheProgram.exe. Python has already given me the ability to automatically create these script files, but now I want to successively run each of these script files, back-to-back, in TheProgram.exe.
So far I have figured out how to use the subprocess module in python to start "TheProgram.exe" in a new process (child process?) and load the first script file as follows:
my_process = subprocess.Popen(["Path to TheProgram.exe", "Path to File1.script"])
As seen, simply "opening" the script file in TheProgram.exe, or passing it as an argument in this case, will execute it. Once File1.script is done, TheProgram.exe generates an output file, and then just sits there. It does not terminate. This is I want, because now I would like to load File2.script in the same process without terminating (file2.script is dependent on file1.script completing successfully), then File3.script etc.
Is this possible? And if so how? I cannot seem to find any documentation or anyone else who has had this problem. If I can provide other information please let me know, I am also new to posting to these forums. Thanks so much for any assistance.

executing a python(or iron python) script via iron python

is this possible? I want to execute a different scriptfile from a iron python script. My main file is getting way to big and I want to make it more readable.
Why don't you reorganize your script into, say a Python module? Have a look at the Python Docs about Modules
#OP's comment: If you truely have a ~5000 lines of code in a single function you should definitely think about reorganizing your code. If you are sure about that "execute a different script" thing, take a look at subprocess.Popen, although I wouldn't advise it.

Categories

Resources