{"id":18551318,"url":"https://github.com/morvanzhou/whenact","last_synced_at":"2025-04-09T22:31:33.660Z","repository":{"id":63404562,"uuid":"567612469","full_name":"MorvanZhou/whenact","owner":"MorvanZhou","description":"Conditional decision flow","archived":false,"fork":false,"pushed_at":"2023-02-02T05:31:19.000Z","size":53,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-21T00:37:58.266Z","etag":null,"topics":["conditional-statements","decision-making","pipeline","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MorvanZhou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-18T06:58:34.000Z","updated_at":"2024-09-21T12:15:55.000Z","dependencies_parsed_at":"2023-02-17T14:35:16.505Z","dependency_job_id":null,"html_url":"https://github.com/MorvanZhou/whenact","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorvanZhou%2Fwhenact","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorvanZhou%2Fwhenact/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorvanZhou%2Fwhenact/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorvanZhou%2Fwhenact/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MorvanZhou","download_url":"https://codeload.github.com/MorvanZhou/whenact/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248123589,"owners_count":21051499,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conditional-statements","decision-making","pipeline","python"],"created_at":"2024-11-06T21:08:37.487Z","updated_at":"2025-04-09T22:31:33.324Z","avatar_url":"https://github.com/MorvanZhou.png","language":"Python","readme":"# WhenAct\n\nWhenAct is a module that defines a decision flow. A\n[中文文档](https://github.com/MorvanZhou/whenact/tree/main/README_CN.md) is available to.\n\nThe executing flow looks like:\n\n```text\ndecision1: [when0] \u003e [action0]\ndecision2: [when1] \u003e [action10 \u003e action11]\ndecision3: [when20 \u003e when21] \u003e [action2]\ndecision4: [when31 \u003e when32] \u003e [action3]\n```\n\nWhen all `when` in one decision is satisfied, than it runs to it's following `action`.\n\nThis decision process has two modes,\n\n1. `auto_break=True`, when runs into the first `when=True`, this flow will finish after it's following `act`.\n2. `auto_break=False`, the flow will continue flow even when `when=False`.\n\nFor example:\n\n```text\n# auto_break = True\ndecision1: [when0=True] \u003e [action0]\ndecision2: X\ndecision3: X\ndecision4: X\n```\n\n```text\n# auto_break = False\ndecision1: [when0=True] \u003e [action0]\ndecision2: [when1=False] \u003e X\ndecision3: [when20=True \u003e when21=True] \u003e [action2]\ndecision4: [when31=False \u003e X] \u003e X\n```\n\n# Install\n\n```shell\npip install whenact\n```\n\n# Create WhenAct decision flow\n\nSimpling use `whenact.add()` to add new decision to the flow process.\n\n```python\nimport whenact\n\ndef w1(ctx):\n    return True\n\ndef a1(ctx):\n    return \"done\"\n\n\nwhenact.add(when=w1, act=a1)\n\nwhenact.print_flow()\n# p0: [w1] \u003e [a1]\n\nhist = whenact.run()\nassert hist.last_output == \"done\"\nassert hist.outputs == [\"done\"]\n```\n\nMore complex pipeline can include more than one decision. Using `whenact.add()` to accumulate decisions.\n\n```python\nimport whenact\n\n\ndef w_false(ctx):\n    return False\n\n\ndef w_true(ctx):\n    return True\n\n\ndef a1(ctx):\n    return 1\n\n\ndef a2(ctx):\n    return 2\n\n\ndef a3(ctx):\n    return 3\n\n\nwhenact.add(when=w_false, act=a1)\nwhenact.add(when=w_true, act=[a2, a3])\n\nwhenact.print_flow()\n# p0: [w1] \u003e [a1]\n# p1: [w2] \u003e [a2 \u003e a3]\n\nhist = whenact.run()\nprint(hist.summary)\n# [p1: w1, p2: w2 \u003e a2 \u003e a3]\n\nassert hist.first_output == 2\nassert hist.last_output == 3\nassert hist.outputs == [2, 3]\n```\n\nThe context(ctx) in each `when` and `act` function passes context information from outside. You can store external\ninformation in context, then pass it to the flow. Moreover, values can be set to the context when inside those\nfunctions, then be carried out once the flow is finished.\n\n```python\nimport whenact\n\n\ndef w_false(ctx):\n    return False\n\n\ndef w_true(ctx):\n    return True\n\n\ndef a1(ctx):\n    ctx[\"action\"] = \"a1 action\"\n\n\ndef a2(ctx):\n    ctx[\"action\"] = \"a2 action\"\n\n\ndef a3(ctx):\n    ctx[\"action\"] += \" with a3\"\n\n\nwhenact.add(when=w_false, act=a1)\nwhenact.add(when=w_true, act=[a2, a3])\n\nwhenact.print_flow()\n\n\n# p0: [w1] \u003e [a1]\n# p1: [w2] \u003e [a2 \u003e a3]\n\nclass TestContext(whenact.BaseContext):\n    pass\n\n\nctx = TestContext()\n\nhist = whenact.run(ctx)\nprint(hist.summary)\n# [p1: w1, p2: w2 \u003e a2 \u003e a3]\n\nassert hist.last_output is None\nassert hist.outputs == [None, None]\nassert ctx[\"action\"] == \"a2 action with a3\"\n```\n\nThere is another way to set a decision flow.\n\n```python\nimport whenact\n\n\ndef w_false(ctx):\n    return False\n\n\ndef w_true(ctx):\n    return True\n\n\ndef a1(ctx):\n    return 1\n\n\ndef a2(ctx):\n    return 2\n\n\nflow = whenact.DecisionFlow([\n    whenact.Decision(when=[w_false], act=[a1], name=\"D1\"),\n    whenact.Decision(when=[w_true], act=[a2], name=\"D2\"),\n]\n)\nprint(flow)\n# D1: [w_false] \u003e [a1]\n# D2: [w_true] \u003e [a2]\n\nhist = flow.run()\nassert hist.first_output == 2\n```\n\n# More examples\n\nMore examples can be found in [tests](https://github.com/MorvanZhou/whenact/tree/main/tests)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorvanzhou%2Fwhenact","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorvanzhou%2Fwhenact","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorvanzhou%2Fwhenact/lists"}