Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
balbasty opened this issue Mar 24, 2025 · 1 comment
Open

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

balbasty opened this issue Mar 24, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@balbasty
Copy link
Contributor

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})

?

@balbasty balbasty added the enhancement New feature or request label Mar 24, 2025
@johmedr
Copy link
Collaborator

johmedr commented Mar 24, 2025

That's a good idea. I like the implementation, it's quite generic. However, wouldn't be the goal of having a class for builtins to expose a fixed list of builtins? (e.g., save, load, ...), so that these functions have a nice signature and description, and users know they can use them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants