victorlei / smop

Small Matlab to Python compiler
MIT License
1.08k stars 409 forks source link

Observations about SMOP #189

Open dcox1776 opened 1 year ago

dcox1776 commented 1 year ago

I have cloned smop, have been trying it out, and would like to offer the following observations that hopefully will clarify a few things.

First and foremost, the documentation isn't, in my opinion, clear. Here are several issues:

SMOP can be installed in a couple different ways. One way is to simply install it using pip: pip install smop. I recommend creating and activating a virtual environment (e.g. python -m venv venv) before installing with pip. Doing so will keep smop out of your global environment and keep it contained to individual projects.

When you install smop using pip everything goes into a subdirectory under whatever directory you created for your virtual environment (I use "venv" on Windows). For me, smop is installed in venv\Lib\site-packages\smop. The directory site-packages is searched by python when resolving import statements in your code.

Additionally, when you install smop using pip, you will get a "smop" app. You can run the smop app at the command line to convert your matlab files to Python. For me, the smop.exe is installed in venv\Scripts.

Another way to install smop is to clone the git repository. Doing so will install everything in whatever directory git uses to clone the project. The directory structure you get is as follows:

-rwx------+ 1 dcox1 None 4359 Mar 5 09:49 HACKING.rst -rwx------+ 1 dcox1 None 1294 Mar 5 09:49 INSTALL -rwx------+ 1 dcox1 None 1085 Mar 5 09:49 LICENSE.MIT -rwx------+ 1 dcox1 None 51 Mar 5 09:49 MANIFEST.in -rwx------+ 1 dcox1 None 3562 Mar 5 09:49 NEWS.rst -rwx------+ 1 dcox1 None 9397 Mar 5 09:49 README -rwx------+ 1 dcox1 None 13100 Mar 5 09:49 README.rst -rwx------+ 1 dcox1 None 19439 Mar 5 09:49 SMOP.rst -rwx------+ 1 dcox1 None 1128 Mar 5 09:49 meta.yaml -rwx------+ 1 dcox1 None 70 Mar 5 09:49 run.py -rwx------+ 1 dcox1 None 1029 Mar 5 09:49 setup.py drwx------+ 1 dcox1 None 0 Mar 5 09:58 smop drwx------+ 1 dcox1 None 0 Mar 5 09:58 venv

You will not get a smop.exe app for converting matlab files. You will, however, get run.py which is essentially the same thing. run.py simply calls main() which is defined in smop/main.py. If you use VS Code, you can set a break point in run.py and step through the code to your hearts content. Of course, you can set a break point in any of the Python source files for smop in the smop subdirectory.

To use smop I create a subdirectory under the root directory for the cloned project (this is the same directory that you will find run.py). I typically call it "src" but the name can be anything. I place my matlab files in "src" for convenience.

With either the pip installed version or the git cloned version I did encounter syntax errors when first running smop. For example:

... Traceback (most recent call last): File "C:\Users\dcox1\Development\smop\smop\main.py", line 66, in main G = resolve.resolve(stmt_list) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\dcox1\Development\smop\smop\resolve.py", line 54, in resolve u = G.node[n]["ident"] ^^^^^^ AttributeError: 'DiGraph' object has no attribute 'node' Errors: 1

To get rid of this error open resolve.py and change "node" to "nodes" at lines 66 and 54. At line 54 I find:

    u = G.node[n]["ident"]

which should have an "s" on the end of "node":

    u = G.nodes[n]["ident"]

Similarly at line 66:

    G.node[n]["label"] = "%s\\n%s" % (n, u.props)

which should be:

    G.nodes[n]["label"] = "%s\\n%s" % (n, u.props)

Why the author hasn't fixed this is beyond me.

With this fix you should be good to go.

Note that smop is built as a Python package. You will find relative import statements in main.py:

from . import options from . import parse from . import resolve from . import backend from . import version

That dot in each line is important. I'll post more about that dot and packages later. Just note that to use smop, you can't execute it. You can only include it in another project. This is why run.py exists in the root directory of the project. run.py imports smop and then calls main(). If you want to build an app using smop as a library, then, place the smop directory as a subdirectory of your project.

All for now. David