Hi! I'm looking to build several `repository` in m...
# ask-community
j
Hi! I'm looking to build several
repository
in my workspace using an asset generator. Currently, my code looks like this:
Copy code
@repository
def A():
    return build_assets("A")
@repository
def B() :
    return build_assets("B")
@repository
def C() :
    return build_assets("C")
...
I'm looking for a more concise code by having something like this:
Copy code
for repository_name in ["A", "B", "C"]:
    @repository(name=repository_name)
    def repo():
        return build_assets(repository_name)
The problem I'm having here is that I'm only getting the last built repository. Is there a way to do this cleanly? Thanks in advance
s
seems like you'll need your
repo
function name to vary as well based on
repository_name
(otherwise they all get declared with the same variable name, and the last one shadows)
j
Yes, I had made this observation too. However, I don't see an obvious solution to variabilize the function name
y
instead of using the decorator, you could
return RepositoryDefinition(...)
directly https://docs.dagster.io/_apidocs/repositories#dagster.repository
j
Hmm yes, except that I can't put assets, jobs, schedules directly in
RepositoryDefinition
. It seems I have to go through
RepositoryData
which is an internal object and complex to handle. The only alternative I see is this one:
Copy code
TEMPLATE = """
@repository
def {0}():
    return build_asset("{0}")
"""

for repo_name in ["A", "B", "C"]:
    exec(TEMPLATE.format(repo_name))