You can use eval() function to evaluate Python source code.
Quoting the documentation:
Answer from Laurent LAPORTE on Stack Overfloweval(expression, globals=None, locals=None)
The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.
Videos
What types of Python code can be converted to C?
What types of C code can be converted to Python?
What types of R code can be converted to Python?
You can use eval() function to evaluate Python source code.
Quoting the documentation:
eval(expression, globals=None, locals=None)
The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.
Try this:
exec ("%s == 3" % x)
For detailed info, read the documentation of eval and exec in python 2.x/3.x. A similar question was asked previously.Here's the link
If the C variant needs x hours less, then I'd invest that time in letting the algorithms run longer/again
"invest" isn't the right word here.
Build a working implementation in Python. You'll finish this long before you'd finish a C version.
Measure performance with the Python profiler. Fix any problems you find. Change data structures and algorithms as necessary to really do this properly. You'll finish this long before you finish the first version in C.
If it's still too slow, manually translate the well-designed and carefully constructed Python into C.
Because of the way hindsight works, doing the second version from existing Python (with existing unit tests, and with existing profiling data) will still be faster than trying to do the C code from scratch.
This quote is important.
Thompson's Rule for First-Time Telescope Makers
It is faster to make a four-inch mirror and then a six-inch mirror than to make a six-inch mirror.Bill McKeenan
Wang Institute
Yes. Look at Cython. It does just that: Converts Python to C for speedups.
There are several tools for converting Matlab to Python code.
The only one that's seen recent activity (last commit from June 2018) is Small Matlab to Python compiler (also developed here: SMOP@chiselapp).
Other options include:
- LiberMate: translate from Matlab to Python and SciPy (Requires Python 2, last update 4 years ago).
- OMPC: Matlab to Python (a bit outdated).
- Mat2py: Matlab to Python (Requires Python 2).
Also, for those interested in an interface between the two languages and not conversion:
pymatlab: communicate from Python by sending data to the MATLAB workspace, operating on them with scripts and pulling back the resulting data.- Python-Matlab wormholes: both directions of interaction supported.
- Python-Matlab bridge: use Matlab from within Python, offers matlab_magic for iPython, to execute normal matlab code from within ipython.
- PyMat: Control Matlab session from Python.
pymat2: continuation of the seemingly abandoned PyMat.mlabwrap, mlabwrap-purepy: make Matlab look like Python library (based on PyMat).oct2py(repository): run GNU Octave commands from within Python.pymex: Embeds the Python Interpreter in Matlab, also on File Exchange.matpy: Access MATLAB in various ways: create variables, access .mat files, direct interface to MATLAB engine (requires MATLAB be installed).- MatPy: Python package for numerical linear algebra and plotting with a MatLab-like interface.
Btw might be helpful to look here for other migration tips:
- http://bci2000.org/downloads/BCPy2000/Migration.html
On a different note, for people who might find it useful there is:
matlab2fortran
There's also oct2py which can call .m files within python
https://pypi.python.org/pypi/oct2py
It requires GNU Octave, which is highly compatible with MATLAB.
https://www.gnu.org/software/octave/