The aim of OMPC is to give user the ability to seamlessly cross from the world of MALTAB into the free and much more colorful world of Python. Python has all the functionality available, but it is hidden behind different syntax.
There will be two major ways of using OMPC.
To start using OMPC the user is supposed to do the following:
Behind this very simple statement the python instance is supposed to import all the numerical modules and install a hook to the import routine that will allow importing MATLAB's m-files.
The module ihooks provides this functionality, we attach our implementation of ModuleLoader to the ihooks module. After installing our hook when a module is searched for we need to include '*.m' (possible mex, dll and others) files in the search. The implementation is in the file ompc_ihooks.py.
To make it possible to import MATLAB importable files a hook has to be installed to the import process. All the import in Python go through the __import__ function. This function searches on the Python path for files with matching filename and supported extensions. The module ompc_ihooks.py installs these hooks and makes it possible to transparently import m-files. At every first import of a Python module this module has to be compiled. The same is true for every m-file, at the time of first import or reload the m-file is compiled. Compilation consists of MATLAB to Python adaptation and comilation of the wrapper to a regular .pyc or .pyo file.
The implementation is inspired by three examples:
MATLAB makes functions accessible based on filesystem information. The function addpath
We should consider creating a completely separate variable sys.mpath to allow possible optimization of lookup and filesystem watching in the future.
OMPC initialization takes the following steps:
In the initial stage numpy module is used as numerical backend. Numpy has all the features that matlab offers and implementing especially slicing should require only little effort.
Hotswapping is a feature of MATLAB that reloads all scripts from files whenever they are called. Python, however, compiles a source script and unless a reload of the module is forced Python reuses compiled code object irrespectable of changes made to its source.
The second role of hot-swapping also called lazy import is to allow transparent import of modules whose lookup in the globals() fails.
A service constantly watching all the dependencies of a running script needs to be implemented. The performance hit of this module might be considerable in case of scripts and therefore an option should be available to turn of this feature. With this feature turned of Python compiler determines the location of dependencies of a script and insert necessary import statements.
For interactive session to assure full MATLAB experience this feature should be always on.
The steps are summarized in the following list:
Look at the OMPC Numerical Array Features page for list of all features emulation by the ompcArray object.
There is no way to tell functions apart from variables in MATLAB, we cannot make any assumption.
All the problems here can be solved by treating the all MATLAB objects, functions and arrays, equally. The impact of implementing functions as objects has to be studied, The Python equivalent after translation from M-file should look as follows:
The syntax of MATLAB's and Python's regural expressions is slightly different. This translation is done on the level of the OMPC's implementation of the corresponding Python function.