Skip to content

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

Closed

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

  1. added theissue type on Mar 24, 2025
  2. johmedr commented on Mar 24, 2025

    @johmedr
    Collaborator

    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?

  3. locked and limited conversation to collaborators on Jun 23, 2025
  4. converted this issue into a discussion #52 on Jun 23, 2025
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