https://dagster.io/ logo
#ask-community
Title
# ask-community
j

Jordan

03/15/2023, 10:37 PM
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

sri raghavan

03/15/2023, 10:38 PM
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

Jordan

03/15/2023, 11:04 PM
Yes, I had made this observation too. However, I don't see an obvious solution to variabilize the function name
y

yuhan

03/15/2023, 11:05 PM
instead of using the decorator, you could
return RepositoryDefinition(...)
directly https://docs.dagster.io/_apidocs/repositories#dagster.repository
j

Jordan

03/16/2023, 5:55 PM
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))
3 Views