Skip to content

Remove reference to spm and allow downstream package to setup their own ctf #3

@balbasty

Description

@balbasty

The mpython code currently imports spm

What I would suggest is transform all of the Runtime's@staticmethod to @classmethod and let downstream package define their own runtime that inherits from the mpython one.

Something like

In mpython:

import abc

class Runtime(abc.ABC):
    _instance = None
    verbose = True

    @classmethod
    def instance(cls):
        if cls._instance is None:
            if cls.verbose:
                print("Initializing Matlab Runtime...")
            cls._initialize()
        return cls._instance

    @classmethod
    def call(cls, fn, *args, **kwargs):
        (args, kwargs) = cls._process_argin(*args, **kwargs)
        res = cls.instance().mpython_endpoint(fn, *args, **kwargs)
        return cls._process_argout(res)

    @classmethod
    def _process_argin(cls, *args, **kwargs):
        to_runtime = MatlabType._to_runtime
        args = tuple(map(to_runtime, args))
        kwargs = dict(zip(kwargs.keys(), map(to_runtime, kwargs.values())))
        return args, kwargs

    @classmethod
    def _process_argout(cls, res):
        return MatlabType._from_runtime(res)

    @classmethod
    @abc.abstractmethod
    def _initialize(cls):
        ...

And in spm:

import mpython

class Runtime(mpython.Runtime):
    
    @classmethod
    def _initialize(cls):
        from spm._spm import initialize
        cls._instance = initialize()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions