Example #1
0
def test_specify_name():
    class A:
        pass

    def f():
        return 1

    instance = A()
    forge_method(instance, f, name="jambalaya")
    assert hasattr(instance, "jambalaya")
    assert instance.jambalaya() == 1
Example #2
0
def test_add_staticmethod():
    """Test adding custom static methods."""
    class A(object):
        pass

    def function_to_be_method(b):
        return b

    instance = A()

    forge_method(instance, function_to_be_method)
    assert function_to_be_method(3) == 3
Example #3
0
    def add_members_from_pyfile(self, pyfile):
        """
        Initialize members of this WorkDir from a python file.

        The following attributes are not added as members of the WorkDir:

        1) imported modules
        2) built-ins and private objects, i.e. if the name starts with an underscore
        3) objects that are imported from other modules using `from ... import ...`

        The only exception to 3. is if the imported function has a command-line interface,
        i.e. `@click.option`-decorated functions added to the workdir so that they can be
        called from the command line.

        Parameters
        ----------
        pyfile : path-like object
            Absolute path of a python file.

        Notes
        -----
        The function arguments `workdir` and `here` of imported functions
        are replaced by the WorkDir instance and the directory containing the
        pyfile, respectively.
        """
        pymod = import_from_file(pyfile)

        for (name, object) in inspect.getmembers(pymod):
            # skip all imports
            if inspect.ismodule(object):
                continue
            # skip all built-ins and private objects
            if name.startswith("_"):
                continue
            # skip all objects that are not defined in this module (if they are not command-line callables)
            if (hasattr(object, "__module__")
                    and object.__module__ != "pyfile_module"
                    and not hasattr(object, "__click_params__")):
                continue
            self.custom_attributes[name] = str(pyfile)
            if inspect.isfunction(object):
                forge_method(self,
                             object,
                             replace_args={
                                 "workdir": self,
                                 "here": pathlib.Path(os.path.dirname(pyfile))
                             },
                             name=name)
            else:
                setattr(self, name, object)
Example #4
0
def test_add_method_replace_args():
    """Testing a function to a class as a method."""
    class A(object):
        def __init__(self, value):
            self.internal_value = value

    def function_to_be_method(b, instance):
        return b + instance.internal_value

    instance = A(2)
    forge_method(instance,
                 function_to_be_method,
                 replace_args={"instance": instance})
    assert hasattr(instance, "function_to_be_method")
    assert instance.function_to_be_method(3) == 5

    instance2 = A(3)
    assert not hasattr(instance2, "function_to_be_method")
Example #5
0
def test_add_method_click_options():
    """Test if replaced args are added as hidden click options."""
    class A(object):
        def __init__(self, value):
            self.internal_value = value

    def function_to_be_method(b, instance, o_ther):
        return b + instance.internal_value

    instance = A(2)
    forge_method(instance,
                 function_to_be_method,
                 replace_args={
                     "instance": instance,
                     "o_ther": 2
                 })
    assert hasattr(instance, "function_to_be_method")
    assert hasattr(instance.function_to_be_method, "__click_params__")
    options = instance.function_to_be_method.__click_params__
    assert len(options) == 2
    assert options[0].name == "instance"
    assert options[1].name == "o_ther"