Hi! I am using `AssetsDefinition.from_op` in an as...
# ask-community
c
Hi! I am using
AssetsDefinition.from_op
in an asset factory. In order to not have number-indexed keys in my config for each of the assets made from my factory, I am calling an op factory within my asset factory (to rename the op). Is there a way to do this renaming at the asset factory level?
s
Hi @Cary Murray is my understanding correct that you basically want each op name that you configure to match the asset name that uses that op? If so, the way that you're describing currently doing it is the best I can think of right now. Would your ideal be to be able to provide an op_name argument or something to
AssetsDefinition.from_op
? Also, do you need to use`AssetsDefinition.from_op` at all if you're already using an asset factory?
c
Hi! Thanks for the response. The desired end is to generate some number of assets that are all named after the input table, which is variable at DAG definition time. Here’s what I have to achieve this so far.
Copy code
def transform_asset_factory(table_name: str = None) -> AssetsDefinition:
    transformed = transform_op_factory(
        name=f"transform_{table_name}",
    )
    return AssetsDefinition.from_op(
        transformed,
        keys_by_input_name={f"table": AssetKey(f"assigned_{table_name}")},
        keys_by_output_name={f"result": AssetKey(f"transformed_{table_name}")},
    )
If there was a way to use the asset factory’s
table_name
parameter to name the asset, I would prefer to go that route instead of having to define a new op that has the desired name that contains the
table_name
.
s
would something like this work:
Copy code
def transform_asset_factory(table_name: str = None) -> AssetsDefinition:
    @asset(
        name=f"transformed_{table_name}",
        ins={"assigned_table": AssetIn(key=f"assigned_{table_name}")}
    )
    def _asset(assigned_table):
        # whatever logic is inside transform_op_factory

    return _asset
c
Thank you for the help on this! And apologies for the delay, I took some time off work. I would like to keep the op definition in
transformed
as an op for backwards compatability (we are currently moving our pipelines over from ops+graphs to assets). Is there a way to “duplicate” and rename an op that backs an asset?
s
I see. There isn't currently a way in Dagster to rename the op when you call
AssetsDefinition.from_node
. If you want to file an issue for this, we might be able to get to it at some point. another idea here that could help is that you can invoke your op like a regular function, inside your asset. e.g.
Copy code
def transform_asset_factory(table_name: str = None) -> AssetsDefinition:
    @asset(
        name=f"transformed_{table_name}",
        ins={"assigned_table": AssetIn(key=f"assigned_{table_name}")}
    )
    def _asset(context, assigned_table):
        transform_op.compute_fn(context, assigned_table)

    return _asset