{"id":19283886,"url":"https://github.com/tencentblueking/bkflow-feel","last_synced_at":"2025-04-22T03:31:19.412Z","repository":{"id":207128849,"uuid":"716607151","full_name":"TencentBlueKing/bkflow-feel","owner":"TencentBlueKing","description":"bkflow-feel 是一款基于 Python 的 FEEL (Friendly Enough Expression Language) 语法解析器，用于对 FEEL 语法表达式进行解析和运算，得到对应的 Python 对象作为计算结果。","archived":false,"fork":false,"pushed_at":"2023-12-04T06:52:37.000Z","size":55,"stargazers_count":7,"open_issues_count":1,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-16T03:56:14.121Z","etag":null,"topics":["dmn-engine","feel-language"],"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/TencentBlueKing.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-09T13:47:26.000Z","updated_at":"2024-05-07T01:43:01.000Z","dependencies_parsed_at":"2023-11-14T08:45:33.589Z","dependency_job_id":"9a9fc9d8-3b40-45c4-9cd1-a8a827e0209f","html_url":"https://github.com/TencentBlueKing/bkflow-feel","commit_stats":null,"previous_names":["tencentblueking/bkflow-feel"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentBlueKing%2Fbkflow-feel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentBlueKing%2Fbkflow-feel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentBlueKing%2Fbkflow-feel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TencentBlueKing%2Fbkflow-feel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TencentBlueKing","download_url":"https://codeload.github.com/TencentBlueKing/bkflow-feel/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250167558,"owners_count":21386004,"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":["dmn-engine","feel-language"],"created_at":"2024-11-09T21:35:41.100Z","updated_at":"2025-04-22T03:31:19.147Z","avatar_url":"https://github.com/TencentBlueKing.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bkflow-feel: A FEEL (Friendly Enough Expression Language) Parser for Python\n\n## 简介\nbkflow-feel 是一款基于 Python 的 FEEL (Friendly Enough Expression Language) 语法解析器，用于对 FEEL 语法表达式进行解析和运算，得到对应的 Python 对象作为计算结果。\n\n[FEEL](https://www.omg.org/spec/DMN/) 是 OMG (Object Management Group) 定义的 DMN (Decision Model and Notation) 规范中的一部分。\n所以，bkflow-feel 可用于决策引擎 (DMN) 中决策表达式的解析。\n\n除此之外，bkflow-feel 也可用于流程引擎 (BPMN) 中分支网关条件表达式的解析。\n\n## Quick Start\n\n### 1. 安装依赖\n\n```\n$ pip install bkflow-feel\n```\n\n### 2. 表达式解析\n\n```python\nfrom bkflow_feel.api import parse_expression\n\nprint(parse_expression(expression=\"1+1\"))  # print(2)\nprint(parse_expression(expression=\"a \u003e b\", context={\"a\": 2, \"b\": 1})) # print(True)\nprint(parse_expression(expression=\"[1,2,3,4][3]\")) # print(3)\n```\n\n通过 parse_expression 函数，可以传入表达式并进行计算，parse_expression 接收以下参数：\n- expression: string 类型，FEEL 表达式文本\n- context: dict 类型，非必填，默认为 None，计算表达式时使用的上下文\n- raise_exception: boolean 类型，非必填，默认为 True，如果解析和计算过程中校验失败或异常，是否抛出异常，如果为 False 则异常时返回 None\n\n### 3. 注册并调用自定义函数\n\n注册自定义函数支持两种方式，但请尽量选择一种方式进行注册，推荐使用第一种\n\na. 通过定义类进行注册\n\n```python\nfrom bkflow_feel.utils import BaseFEELInvocation\n\n\n# 类初始化时注册\nclass HelloWorldWithParamsFunc(BaseFEELInvocation):\n    class Meta:\n        func_name = \"hello world with params\"  # FEEL 语法中调用的函数名\n\n    def invoke(self, a, b, c=2, *args, **kwargs):\n        return {\"a\": a, \"b\": b, \"c\": c, \"args\": args, \"kwargs\": kwargs}\n```\n\nb. 通过调用函数进行注册\n\n```python\nfrom bkflow_feel.utils import FEELFunctionsManager\n\n\ndef func_with_params(a, b, c):\n    return \"With params: {}, {}, {}\".format(a, b, c)\n\n\nREGISTER_FUNCS = {\n    \"func with params\": \"path.to.func_with_params\",  # FEEL 语法中调用的函数名 和 对应的函数路径\n}\nFEELFunctionsManager.register_funcs(REGISTER_FUNCS)  # 注册\n\n```\n\n自定义函数支持参数校验\n\n如果通过第二种方式进行函数定义，可以通过定义 Inputs 类（继承 InvocationInputsModel）来定义函数的输入，并进行校验。\n\nInputs 类的定义和字段校验逻辑可参考 [pydantic](https://docs.pydantic.dev/1.10/)。\n\n同时，为了保证不具名传参的调用方式校验逻辑也能将各个位置的参数准确映射到对应的校验字段，需要在 Meta 类中定义对应的字段顺序。\n\n示例如下：\n\n```python\nfrom bkflow_feel.utils import BaseFEELInvocation, InvocationInputsModel\n\nclass FuncWithInputsValidation(BaseFEELInvocation):\n    class Meta:\n        func_name = \"func with inputs validation\"\n\n    class Inputs(InvocationInputsModel):\n        a: int\n        b: int\n        c: int\n        d = 20\n\n        class Meta:\n            ordering = [\"a\", \"b\", \"c\", \"d\"]\n\n    def invoke(self, a, b, *args, **kwargs):\n        return {\"a\": a, \"b\": b, \"args\": args, \"kwargs\": kwargs}\n```\n\n函数调用\n\n```python\nfrom bkflow_feel.api import parse_expression\n\nparse_expression(expression=\"hello world with params(a:1,b:2)\")  # {'a': 1, 'b': 2, 'c': 2, 'args': (), 'kwargs': {}}\n\nparse_expression(expression=\"func with params(1,2,3)\")  # With params: 1, 2, 3\n```\n\n## 支持语法详情\n见[语法文档](./docs/grammer.md)\n\n## benchmark\n这里通过 pytest-benchmark 执行单测得到对应的解析性能结果：\n![](./docs/pics/benchmark.svg)\n\n可以看到，对于单测样例，bkflow-feel 的解析处理时间大致在 40+us - 200+us，换算成每秒可处理的简单表达式个数约为 5000 - 25000 个。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftencentblueking%2Fbkflow-feel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftencentblueking%2Fbkflow-feel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftencentblueking%2Fbkflow-feel/lists"}