Is there a simply way to create a macro like thing...
# general
g
Is there a simply way to create a macro like thing with classes so I can actually import and use other python modules? I have this current macro that I need to make smarter:
Copy code
def docker_cache_from(repository: str, image_tags: list[str]):
    cache_from = []
    for image_tag in image_tags:
        cache_from.append(
            {
                "type": "registry",
                "ref": f"our-register-url/{repository}:{image_tag}",
            }
        )

    return cache_from
The macro is used as an argument to docker_image cache_from param.
I decided just to workaround this with:
Copy code
my_module = __import__('module_name')
It would be nice if there was an easy way to add a macro that could use imports.
For history, other ways to bypass
Copy code
os_module = getattr(__builtins__, '__import__')('os')
Copy code
imp = "".join(['i', 'm', 'p', 'o', 'r', 't'])
exec(f"{imp} os")
Copy code
os_module = eval('__import__("os")')
b
What are you importing? This is potentially a recipe for explosions and confusion
1
(
os
in particular seems like an risky one!)