https://prefect.io logo
#prefect-community
Title
# prefect-community
j

Jason Motley

04/06/2022, 11:12 PM
What's the best approach to using a for loop that would normally be within the
main
section of a standard python script?
k

Kevin Kho

04/06/2022, 11:13 PM
Mapping would be the analog if the tasks are independent. If it’s a sequential loop, we have Task Looping
j

Jason Motley

04/06/2022, 11:20 PM
This seems a little on the side of what I was thinking of. Right now I have a couple functions, which are then used in a for loop at the bottom. E.g.
Copy code
for x in my dict:
        id= dict[x]
        response1= function1(id)
        file= function2(id, response1)
        result= functino3(id, file)
k

Kevin Kho

04/06/2022, 11:21 PM
Yes that should be able to be done by mapping
j

Jason Motley

04/06/2022, 11:23 PM
does my dictionary go in the .map(1, 2, 3) in the example?
k

Kevin Kho

04/06/2022, 11:23 PM
Use a task to either pull out the keys or values since you need to map over a List
j

Jason Motley

04/06/2022, 11:24 PM
if you were rewriting my example above then, there would be a task before the first row in the for loop, which gets at the keys, and then
I guess I'm trying to visualize this outside of the numerical example on the help page
k

Kevin Kho

04/06/2022, 11:26 PM
Copy code
with Flow() as flow:
    ids = get_keys(dict)
    response1 = function1.map(ids)
    files = function2.map(ids, response1)
    result = function3.map(ids, files)
response1
,
files
,
result
being Lists
j

Jason Motley

04/06/2022, 11:26 PM
Mapping cannot be done over dictionaries?
k

Kevin Kho

04/06/2022, 11:27 PM
Maybe it can, but I wouldn’t advise because it’s not explicit whether you’re mapping over keys or values
6 Views