Skip to content

Expose builtin functions + "load/save" helpers. #36

Description

@balbasty

Should we expose a slightly nicer way of calling non-spm functions?

I've been using this in some of my code:

class Builtin:
    # Wrapper for calls to builtin functions

    class WrappedBuiltin:

        def __init__(self, name):
            self.name = name

        def __call__(self, *args, **kwargs):
            return Runtime.call(self.name, *args, **kwargs)

    def __getattr__(self, name):
        return self.WrappedBuiltin(name)

    def import_in(self, env, *args, **kwargs):
        for name in args:
            env[name] = getattr(self, name)
        for as_name, name in kwargs.items():
            env[as_name] = getattr(self, name)


builtin = Builtin()

matlab_figure = builtin.figure  # or: builtin.import_in(locals(), matlab_figure="figure")

And, related, I use this helper to save variables in a mat file:

save = Runtime.call("eval", "@(f,x) save(f,'-struct',x)")

x = ...
save("path/to/file.mat", {"x": x})

which we could implement somewhere in __wrapper__, since it's quite commonly used. Or maybe I should just use

Runtime.call("eval", "path/to/file.mat", "-struct", {"x": x})

?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions