https://dagster.io/ logo
#ask-ai
Title
# ask-ai
l

Lucas Humbert

02/22/2024, 11:00 AM
How to union a list of string designing assets and an Asset selection as an asset selection for define_asset_job parameter
s

Scout

02/22/2024, 11:00 AM
To union a list of strings designating assets with an
AssetSelection
object for use in the
selection
parameter of
define_asset_job
, you can use the union operator
|
. The list of strings can be converted into an
AssetSelection
using the
.assets()
method, and then combined with another
AssetSelection
object. Here's an example based on the documentation:
Copy code
python from dagster import define_asset_job, AssetSelection  # Assuming you have a list of asset keys as strings asset_keys_list = ["asset1", "asset2"]  # Convert the list of strings to an AssetSelection asset_selection_from_list = AssetSelection.assets(*asset_keys_list)  # Assuming you have another AssetSelection object another_asset_selection = AssetSelection.assets("asset3")  # Union the two AssetSelections combined_selection = asset_selection_from_list | another_asset_selection  # Define the job with the combined AssetSelection job = define_asset_job("my_union_job", selection=combined_selection)
In this example,
asset_selection_from_list
is created from a list of asset key strings, and
another_asset_selection
is another
AssetSelection
object. The
|
operator is used to create a union of these two selections, which is then passed to
define_asset_job
as the
selection
parameter. Remember that the
AssetSelection.assets()
method can take multiple asset keys as arguments, and the
|
operator is used to perform the union operation as described in the documentation.
l

Lucas Humbert

02/22/2024, 11:14 AM
@Scout his code doesn't work
Copy code
asset_selection_from_list = AssetSelection.assets(*asset_keys_list)
I get this error :
AttributeError: 'list' object has no attribute 'keys'@Scout
s

Scout

02/22/2024, 11:14 AM
I apologize for the confusion. The
AssetSelection.assets()
method expects
AssetKey
objects or strings that represent asset keys, not a list. If you are providing a list of strings, you should ensure that each string in the list is passed as a separate argument to the method. Here's how you can correctly create an
AssetSelection
from a list of strings:
Copy code
python from dagster import define_asset_job, AssetSelection, AssetKey  # Assuming you have a list of asset keys as strings asset_keys_list = ["asset1", "asset2"]  # Convert the list of strings to an AssetSelection by unpacking the list asset_selection_from_list = AssetSelection.keys(*[AssetKey(key) for key in asset_keys_list])  # Assuming you have another AssetSelection object another_asset_selection = AssetSelection.keys(AssetKey("asset3"))  # Union the two AssetSelections combined_selection = asset_selection_from_list | another_asset_selection  # Define the job with the combined AssetSelection job = define_asset_job("my_union_job", selection=combined_selection)
In this corrected example, we first convert each string in
asset_keys_list
to an
AssetKey
object within a list comprehension. Then we unpack this list of
AssetKey
objects into the
AssetSelection.keys()
method. This should resolve the
AttributeError
you encountered. Please try this corrected approach, and if you encounter any further issues, feel free to ask for additional assistance.
4 Views