Example #1
0
 def replace_list_flow():
     job1 = Job(add, function_args=(1, ))
     job2 = Job(add, function_args=(job1.output, ))
     job3 = Job(add, function_args=(5, ))
     job4 = Job(add, function_args=(job3.output, ))
     flow1 = Flow([job1, job2], output={"1": job1.output, "2": job2.output})
     flow2 = Flow([job3, job4], output={"3": job3.output, "4": job4.output})
     return Response(replace=[flow1, flow2])
Example #2
0
def start_timing_jobs(websites: List[str]):
    from jobflow.core.job import Response

    jobs = []
    for website in websites:
        time_job = time_website(website)
        time_job.name = f"time {website}"
        jobs.append(time_job)

    output = [j.output for j in jobs]
    return Response(replace=Flow(jobs, output))
Example #3
0
 def make(self, a, b):
     detour = self.add_maker.make(a, b)
     return Response(detour=detour)
Example #4
0
 def add_response(a, b):
     return Response(output=a + b, stop_children=True)
Example #5
0
 def add_schema_replace(a, b):
     new_job = Job(add, function_args=(a, b), output_schema=AddSchema)
     return Response(replace=new_job)
Example #6
0
 def add_schema_response(a, b):
     return Response(output=AddSchema(result=a + b))
Example #7
0
 def add_schema_response_dict(a, b):
     return Response(output={"result": a + b})
Example #8
0
def test_response():
    # no need to test init as it is just a dataclass, instead test from_job_returns
    # test no job returns
    from pydantic import BaseModel

    from jobflow.core.job import Response

    response = Response.from_job_returns(None)
    assert response == Response()

    # test single output
    response = Response.from_job_returns(1)
    assert response.output == 1

    # test list output
    response = Response.from_job_returns([1, 2, 3])
    assert response.output == [1, 2, 3]

    # test tuple output
    response = Response.from_job_returns((1, 2, 3))
    assert response.output == (1, 2, 3)

    # test dict output
    response = Response.from_job_returns({"a": 1, "b": 2})
    assert response.output == {"a": 1, "b": 2}

    # test Response output
    response_original = Response(output=(1, 2, 3), stop_children=True)
    response_processed = Response.from_job_returns(response_original)
    assert response_original == response_processed

    # test Response and another output
    with pytest.raises(ValueError):
        Response.from_job_returns([response_original, 5])

    # test schema
    class MySchema(BaseModel):
        number: int
        name: str

    response = Response.from_job_returns({
        "number": "5",
        "name": "Ian"
    },
                                         output_schema=MySchema)
    assert response.output.__class__.__name__ == "MySchema"
    assert response.output.number == 5
    assert response.output.name == "Ian"

    # test schema does not match output
    from pydantic import ValidationError

    with pytest.raises(ValidationError):
        Response.from_job_returns({"number": "5"}, output_schema=MySchema)
Example #9
0
 def detour_job():
     job = Job(add, function_args=(1, ))
     return Response(detour=job)
Example #10
0
 def addition_job():
     job = Job(add, function_args=(1, ))
     return Response(addition=job)
Example #11
0
    def replace_flow():
        from jobflow import Flow

        job = Job(add, function_args=(1, ))
        flow = Flow([job], output=job.output)
        return Response(replace=flow)
Example #12
0
 def replace_list_job():
     job1 = Job(add, function_args=(1, ))
     job2 = Job(add, function_args=(job1.output, ))
     return Response(replace=[job1, job2])
Example #13
0
 def replace_job():
     job = Job(add, function_args=(1, ))
     return Response(replace=job)
Example #14
0
 def replace_flow_multioutput():
     job1 = Job(add, function_args=(1, ))
     job2 = Job(add, function_args=(job1.output, ))
     flow = Flow([job1, job2], output={"1": job1.output, "2": job2.output})
     return Response(replace=flow)