I am trying to programatically generate an `op` us...
# ask-community
a
I am trying to programatically generate an
op
using the
OpDefinition
instantiation but I am struggling to provide
compute_fn
parameter for that. As I understand, it cannot directly be the base function that you use the
@op
decorator on. But not sure how to generate a valid
compute_fn
to pass to
OpDefinition
. Any ideas or references?
j
Hi @Anoop Sharma I wrote up a quick example of creating an op using
OpDefinition
. However, in almost all cases you should define ops using the
@op
decorator. If you can tell us a bit more about your use case, we might be able to help figure out how to do it using
@op
!
Copy code
def my_func(context, _):
    yield Output(1, "my_out")

op_def = OpDefinition(name="op_def", input_defs=[], compute_fn=my_func, output_defs=[OutputDefinition(name="my_out", dagster_type=int)])
This is what I am trying to achieve.
j
ok. from that description I'd imagine you could do something like this (semi-pseudocode)
Copy code
@op    
def run_script(context, script_file):
    # code to run the script (subprocess.call(script_file), etc)

@op(out=DynamicOut())
def get_scripts():
    for f in directory:
         yield DynamicOut(f, mapping_key=f)

@graph     
def run_all_scripts():
    scripts = get_scripts()
    scripts.map(run_script)
This will collect all of the scripts in some directory in
get_scripts
and then because you are working with dynamic outputs, a
run_script
op will run once for each script returned from
get_scripts
. Then in
run_script
you can write whatever logic you need to execute the contents of a script
👍 1