Running Python in Java (or vice versa) - python

I'm currently making a java GUI (JPanel) that is supposed to display output from a python model. So far I'm trying to have the java code run python, which I have no idea how to do. The main goal is to have both java and python running simultaneously so that any results from the python model can be displayed on the java GUI in real time.
Doing the reverse (running the java GUI in python) could work as well, but the GUI has a lot of settings/packages/JVM arguments that I am not experienced enough to deal with, so I really don't want to go down that route unless I have to.
So far, I have tried ProcessBuilder with BufferReader in java, but that only displays results after the python model finishes running, not in real time. I thought about using Jython, but I heard that it doesn't work for python 3.x versions.
How can I run python in java? If I can't/shouldn't what should I do then?

Related

Is it possible to bind a C executable with Python?

Recently, the company I work for has been modernizing their codebases to mostly Python. Last week, I was tasked with looking into converting a utility program we rely on to manage some USB devices. I was quick to discover this program is highly specialized and is likely impossible to covert to Python (nor do I want to attempt it if I'm being honest). I have access to the source code for this program.
This program runs on Linux and currently compiles to an executable, not a shared object (.so).
Is binding Python to this program something that can be reasonably done? If so, I've already looked into ctypes, cffi, cython, pybind11, and writing raw C bindings, but I am lost as to what the best approach would be. If not, there is no major loss--I'll just have to call the program via subprocess and parse its output.
Thank you for your time.

Controlling a program via arm-none-eabi-gdb-py from Python

I want to do something kinda weird to automate some processes involving embedded processors and other hardware.
I would like to control an ARM processor via GDB to run some tests by monitoring and changing some variables. I want to control the whole test from Python so I can synchronize other instruments/hardware when I trigger certain events on the processor. I think arm-none-eabi-gdb-py would be very helpful in this, but how would that work in practice?
Do I start GDB from a Python subprocess, and then run Python from within GDB? It seems a bit convoluted, but Python for arm-none-eabi-gdb-py seems to require being run from within the GDB process. So I am a little confused how I would tie that into the outer 'test driver'.

How to build application with python and C# in Visual studio

I want to build an application designed in C# forms Visual studio to get user input and want to process user input using python code? In python code, it has different methods to execute for each user input. I used simply for loops for that in python code.
How is it possible?
C# and Python are not compatible in that way. C# runs on .NET platform and Python runs in an interpreter written in C. You could try using PythonSharp, but it will be far from ideal (it's an old project). You could also write the whole application in Python with GUI toolkits. Most popular ones I know of are Tkinter and PyQT.
Edit: another project you might be interested in is IronPython.

Is it possible to make a Python script its own OS?

I have written a Python script that I want to be able to run separately from the OS. As a result, I want it to be able to run as pretty much its own OS. Everything is shell based and there are no GUI components. How would I go about making a bootable version of this Python script, and is it even possible? Or would i have to put a bare-bones OS with the Python script installed as an addition?
For all practical purposes, the answer is no, it's not possible. The bare-bones os with a python install is your best option.
It is theoretically possible to implement a stand-alone Python interpreter, which runs as the OS of a system. That has been done with several interpreter languages. First of all, BASIC, then APL, Forth, Smalltalk, Java, and most likely some I'm not aware of. There is no reason why this couldn't be done with Python, it merely needs a bit of implementation work.
To give a rough estimation of the amount of work: a caffeine-driven expert coder takes about two days to implement a stand alone Forth interpreter, doubling as operating system, from scratch, on a platform (s)he's familiar with. By then the system will be far from finished or complete, but it will compile source, and allow extending itself with the results of that compilation. Other languages would differ. In case of Java, only porting an assembly implementation from one to an entirely different CPU took a team of 4 people about 2 weeks, while the back end of Java, the JVM, has a considerable resemblance to Forth. I'm not knowledgeable enough about Python inner workings to be able to give a meaningful estimate of the effort to implement a stand alone Python interpreter, but it could be roughly comparable to the effort of implementing a Java VM.

How is Python interpreted with wxPython, PyQt, PySide or Tkinter?

I got curious and have been reading about GUI development using Python for the past hour. After reading documentation of wxPython, PyQt, Nokia's Python bindings for Qt along with Tkinter a question came to my mind.
When I create a console application with Python, it runs using the embedded Python interpreter (which I assume is usually if not always in my case cpython).
So I was wondering, what's the case with these "widget toolkits"?
How is the Python code executed and what interprets it (or executed it)?
Which part of my Python code is interpreted using the Python
interpreter?
Or does the Python code get lexically analysed and then parsed by the widget's
toolkit which then interpretes and executes (or compile during build)?
I am looking forward to understanding what goes on in the background in comparison with Python applications' (a bit simpler to understand) interpretation with the Python interpreter.
Thank you.
PS. To whichever genius thinks that this question deserves to be closed;
A lot of people wonder the internals of external libraries and systems. Especially those which are not as simple as they look. There currently is not any question explaining this on SE.
This is just a really generalized high-level explanation about "GUI toolkits"...
Lets say you decide to use the Qt framework. This framework is written in C++. There are two different python bindings that can be used, allowing one to write a GUI application in python against the same API as the C++ version.
The python bindings provide a wrapping around calls into the C++ code. PyQt4 for instance uses sip, while PySide uses shiboken. These are just language wrapping tools that take specifications for how to map between the C++ objects and their intended python interface.
Ok, so you start using PyQt... All of the code you write has to pass through the python interpreter. Some of it may be pure python. Some of it will call into C++ libs to create things like your widgets. In Qt, there will be a C++ pointer associated with the python instance counterpart.
It is the C++ layer that is then communicating with the window manager of your platform, to turn platform-independent API calls into something platform specific, like how to exactly draw a button or menu.
Whether you create a console only or GUI based python application, it all goes through the python interpreter to interpret your python code. Something must interpret the python language for you.

Categories

Resources