Is it possible to create a glue that makes it possible for python modules (more specifically, library bindings) to be used in node.js? Some of the data structures can be directly mapped to V8 objects - e.g. array, dict.
More importantly - would that be a more elegant way to create bindings than manually or through FFI. In short, would it be worth it?
Try this node.js module, that is a bridge: Node-Python,
NOTE: The Project is 7 years old and still stuck at v0.4. A lot of functionality like converting between Python and Node arrays is still missing. It may be safe to assume that it's no longer supported by its original author(s)
Edge.js does a fine job at this. It allows you to write a Python script and then call the routines from Node.js, which can be used to easily create bindings with python modules.
Related
There is a Scala library I'd like to use, namely BIDMach, however I need to be able to use it from Python rather than in Scala. I've been trying to think of different ways of possible being able communicate between the library and Python code, such as creating an HTTP server in Scala and calling this from Python, using something like JPype to try and use Scala libraries in Python, and different types of interprocess communication. However, none of them seem to work very well, and would seem to require a large amount of reimplementation of what is already in the library. Does anyone know of a good way of going about this?
edit: In terms of exactly what I think I'd like to do, ideally I'd be able to get close to almost all of the libraries functionality usable in Python, however that probably isn't realistic. It would be nice if some of the Scala classes were easily usable in Python, without too much repeated implementation effort. The reason I don't think what I've looked into so far will work well is because it requires a fair bit of reimplementation of what is already in the library (i.e. representing something like a matrix in JSON, as a way of transporting data to/from Python/Scala)
Currently I'm doing a project in Golang which need to call to Python.
In Python it's a library of singleton-like instance.
But I can't modify those library because It's too complicated. (for me)
Most thing I can do is wrap it with my own Python script.
So I'm finding a way to create multiple Python interpreter in Go.
Or maybe multiple sub-interpreter in Python.
Which mean I can create many python instance(same application).
Any ways I can do this?
As Chris Townsend and pie-o-pah said,
Trying to implement sub-interpreter is much more complicate.
Try to create the language interface is make senses to my case.
In this situation os/exec is the way to go.
And can even create ssh to remote my python module if my main server is overload.
Summary
I'm looking for an IPC/RPC protocol which:
Has libraries in both R and Python 2.7 which work on Windows
Allows me to pass data and make calls between the two languages
Does not embed one language inside the other (I want to use Python and R in my favourite IDEs, not embedding bits of R expression strings in Python code, vice/versa)
Supports circular references
Supports millisecond time data
Fast and efficient when I need to pass large amounts of data
I have seen this similar question, but unlike the OP for that question, I do not want to use R in a pythonic way. I want to use R in an R way in my RStudio IDE. And I want to use Python in a Pythonic way in a PyCharm IDE. I just want to occasionally pass data or make calls between the two languages, not to blend the languages into one.
Does anyone have any suggestions?
Background
I use both Python and R interactively (by typing into the console of my favourite IDE: PyCharm and RStudio respectively). I often need to pass data and call functions between the two languages ad-hoc when doing exploratory data analysis. For instance, I may start with processing data in Python, but later stumble across a great machine learning library in R that I want to try out, vice/versa.
I've looked at Python libraries PypeR and rpy2 but both embed R within Python, so I lose the ability to use R interactively within RStudio. I have also looked at RPython, but I use Windows and it does not work with Windows.
Additionally, I've looked at XML-RPC but some of my data has objects which contain circular references (e.g. a tree structure where child nodes have references to their parent). Problem is, Python's xmlrpclib does not support circular references, nor timestamps to millisecond precision which my data also contains.
I need to invoke Lua/Python Scripts from a String or a File with the possibility to bind Java objects, so I can use them directly inside the script.
Something like:
e.bind("varname", object);
e.call("object.toString()");
Unfortunately Jython, which is perfect for my purposes, does not work on the dalvik vm.
I also tried kahlua, but it seems that I can´t bind objects to scripts there.
Assuming SL4A does not meet your needs, you will need to turn Python into a library (Lua already is one) and attach it your your application via the NDK.
And, as neither Lua nor Python know anything about Java, so you will have to write the glue code to connect your Java objects up to the Lua and Python scripts. I am guessing that this is possible, but that is just a guess.
This work will not be trivial, in all likelihood.
I finally found a scripting language, which also fits my purposes well.
It is called BeanShell and itself is as simple as it could be. Almost like Java.
Just checkout the official site:
BeanShell
It is not longer developed by the orinigal programmer, but an active project on google-code called beanshell2 is the follower of BeanShell.
You can find it here: BeanShell2
i just discovered http://code.google.com/p/re2, a promising library that uses a long-neglected way (Thompson NFA) to implement a regular expression engine that can be orders of magnitudes faster than the available engines of awk, Perl, or Python.
so i downloaded the code and did the usual sudo make install thing. however, that action had seemingly done little more than adding /usr/local/include/re2/re2.h to my system. there seemed to be some *.a file in addition, but then what is it with this *.a extension?
i would like to use re2 from Python (preferrably Python 3.1) and was excited to see files like make_unicode_groups.py in the distro (maybe just used during the build process?). those however were not deployed on my machine.
how can i use re2 from Python?
update two friendly people have pointed out that i could try to build DLLs / *.so files from the sources and then use Python’s ctypes library to access those. can anyone give useful pointers how to do just that? i’m pretty much clueless here, especially with the first part (building the *.so files).
update i have also posted this question (earlier) to the re2 developers’ group, without reply till now (it is a small group), and today to the (somewhat more populous) comp.lang.py group [—thread here—]. the hope is that people from various corners can contact each other. my guess is a skilled person can do this in a few hours during their 20% your-free-time-belongs-google-too timeslice; it would tie me for weeks. is there a tool to automatically dumb-down C++ to whatever flavor of C that Python needs to be able to connect? then maybe getting a viable result can be reduced to clever tool chaining.
(rant)why is this so difficult? to think that in 2010 we still cannot have our abundant pieces of software just talk to each other. this is such a roadblock that whenever you want to address some C code from Python you must always cruft these linking bits. this requires a lot of work, but only delivers an extension module that is specific to the version of the C code and the version of Python, so it ages fast.(/rant) would it be possible to run such things in separate processes (say if i had an re2 executable that can produce results for data that comes in on, say, subprocess/Popen/communicate())? (this should not be a pure command-line tool that necessitates the opening of a process each time it is needed, but a single processs that runs continuously; maybe there exist wrappers that sort of ‘demonize’ such C code).
David Reiss has put together a Python wrapper for re2. It doesn't have all of the functionality of Python's re module, but it's a start. It's available here: http://github.com/facebook/pyre2.
Possible yes, easy no. Looking at the re2.h, this is a C++ library exposed as a class. There are two ways you could use it from Python.
1.) As Tuomas says, compile it as a DLL/so and use ctypes. In order to use it from python, though, you would need to wrap the object init and methods into c style externed functions. I've done this in the past with ctypes by externing functions that pass a pointer to the object around. The "init" function returns a void pointer to the object that gets passed on each subsequent method call. Very messy indeed.
2.) Wrap it into a true python module. Again those functions exposed to python would need to be extern "C". One option is to use Boost.Python, that would ease this work.
SWIG handles C++ (unlike ctypes), so it may be more straightforward to use it.
You could try to build re2 into its own DLL/so and use ctypes to call functions from that DLL/so. You will probably need to define your own entry points in the DLL/so.
You can use the python package https://pypi.org/project/google-re2/. Although look at the bottom, there are a few requirements to install yourself before installing the python package.