How to use python library(user defined) function in java program? - python

I am facing some issue with python to java.
I have my python library (test_lib) which have functions (addition, multiply) now I want to use test_lib function in java program but without using (test_lib) in java program. I don't want to show or include function definition in java program. I know if I add python file that have a function I can call in java program but then the user knows the function definition.
Has anyone ever tried python to java. Like with a high level approach?
Thank you.

Related

Run c++ class from python

Is there a way to run a c++ class from python without using any external libraries like Boost.Python, SWING ect? I don't want to pass any arguments to this class or call a specific method and in my c++ class I have only a void main method, I just want to run the main and that is all.
Or if this is not possible a saw this tutorial http://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html#manual-wrapping. But I didn't understand if I should put the hello_wrapper function in the same c++ class where I have the original hello function. And also how can I create a modulo in Python(second part in the tutorial) and where should I put this code
DL_EXPORT(void) inithello(void)
{
Py_InitModule("hello", HelloMethods);
}
Thanks
is there a way to run a c++ class
you don't run C++ classes. They are data types!
Boost.Python, SWING
It's called SWIG, not SWING :)
You can add your own C wrapper code that initializes a PyObject. I'd recommend reading the CPython docs and the examples in the tutorial on extending python. Since you didn't specify a version, I can't give you a discrete link.
Note that python is C, and C++ isn't; which means that you'll have to export several things with a C ABI, i.e. by using external "C" in your code. That might not be something for the uninitiated, and you should certainly evaluate whether not using external wrapper generators is really worth the trouble – especially since using e.g. SWIG properly (which is really a pain) you can get Python objects that really behave like python objects, e.g. you can extend them with python etc.

In Python, how to use a function (from COM) which requires byref parameter?

I need to call a function written in VB.net as part of my COM automation program.
The VB program requires a byref parameter, defined as
Function GetBeamWidth(ByRef pfBeamWidth As Single) As Integer
I tested the function in VB codes shown below, no problem at all,
Dim y As Single
myObject.GetBeamWidth(x)
'myOject is an instance of the ActiveX server object, which implements function GetBeamWidth
however, I don't have a clue on how to call this function in python. Everything I tried gave me "type not match" error. I am sure that I loaded the ActiveX server, tried early and late binding, tried passing x with the definition of x = [0].
I also tried ctype.byref(x), not working. The example I found online is not that easy to understand. If ctype.byreg is the cure, can someone here paste an easy-to-follow example?
I guess this must be a common problem for yo all experienced programmer out there. I know python doesn't support pointer, but sadly in real life we have to interface with functions written by others/other languages which need passing function parameters by reference.
Thanks in advance!
ByRef is passing by pointer, you can look at ctypes. It also exports byref.
Finally, it turns out to be pretty much impossible to accomplish what I asked for. Alternatively, I wrapped my core functions in Python class and register it as COM server.
Then, I just built the most outside shell in VB.net, which loads all COM servers, provides UI, and simply calls COM functions to do the work.
I guess that is one of reasons for why we have programming platforms other than Python still alive.

How to call a Lua function from Python?

I would like to know if there is any way we can call a function written in Lua from Python code. If so, could you please suggest me the best possible way to do the same?
Basically, I have 2 different modules one is written in Lua and other in Python and wanted to use them as it is without rewriting Lua module into Python.

How to import external Python script?

I am writing a game engine in Python and the thing is I am not sure how to handle external scripts (think source engine mods, LUA). Every scene, entity in a game can have custom script attached to it, but game engine is not aware of those scripts until the scene is being loaded. For example there could be a script, which would animate game cutscene and that script would be used only in one scene.
So, what i want to know is what's the best way to handle those scripts? I know I could import them with exec or eval, but someone said it's now safe. Why? I could also create some scripting language, which would be parsed during runtime, but I don't see a point in that considering that Python is scripting language itself. Any help would be greatly appreciated.
You can use __import__() for this. Say your entity has a script attribute that can be either None or the name of a script (that is in a known location, which is on sys.path), you could use the following code to run the main() function from that script:
if entity.script is not None:
custom_script = __import__(entity.script, globals(), locals(), [], -1)
custom_script.main()
Yes, exec and eval are not safe for user-supplied data, like handling expressions or Web input. But if you specifically want to give users the full power of Python, and you understand the risks (users can do anything – erase/read files on the computer, crash Python, enter an infinite loop, etc.), using exec is perfectly fine.
If you trust your game script designer(s), exec away. If the levels can come from random people on the Internet, at least let your players be aware of the risk.
You can run your external script either using by importing it using __import__() or using exec/execfile function. It is nice to know that:
import compiles script to byte-code and saves .pyc file at first usage
import tries to search for the module and eventually it creates module with its own namespace in contrast to exec/execfile that just executes the code in string/file.
it is possible to sandbox your code a bit by mangling with globals builtins during passing globals as parameters to __import__/execfile, although this method is not super safe, see my answer to this question for example.

Accessing MFC functions in python

I have just recently started using python for using it with my mfc program and it has been a decent journey so far. At the moment, I am trying to access a function from my mfc dll program that has a format given below:
void DLLDIR DrawEllipse ( CRect, CDC* );
I have used extern "C" and everything and I am able to access the function. In terms of declaring its restype and arguement type, I am facing some problems at the moment. Obviously the restype would be "None" but I am unable to understand as to how do I declare its arguement type which are CRect and CDC*.Would be great if someone already knows how to access the MFC functions and use them as arguements in your python functions.
Thanks in advance.
You can't, you need to wrap those structures as Python objects - either make you own, or use the 'native' Python equivalent of a rectangle (if there is one, I don't know of any). Look at Python win32 packages such as win32all to do the heavy lifting, so that you won't have to re-implement it all yourself.

Categories

Resources