From 964e390553f184be8e5b1dfa132a41779ad9ea5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Mon, 26 Sep 2022 00:02:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8C=87=E6=A0=87?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + MyFunc/OperatingProfitRatio.py | 9 +++++++++ MyFunc/__init__.py | 0 main.py | 26 ++++++++++++++++++++++++++ requirements.txt | 2 ++ test_main.http | 11 +++++++++++ 6 files changed, 49 insertions(+) create mode 100644 .gitignore create mode 100644 MyFunc/OperatingProfitRatio.py create mode 100644 MyFunc/__init__.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 test_main.http diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/MyFunc/OperatingProfitRatio.py b/MyFunc/OperatingProfitRatio.py new file mode 100644 index 0000000..9d824de --- /dev/null +++ b/MyFunc/OperatingProfitRatio.py @@ -0,0 +1,9 @@ +from pydantic import BaseModel + + +class OperatingProfitRatio(BaseModel): + the_total_profit: float + operating_income: float + + def exec(self): + return self.the_total_profit/self.operating_income diff --git a/MyFunc/__init__.py b/MyFunc/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..a3cb302 --- /dev/null +++ b/main.py @@ -0,0 +1,26 @@ +import json + +from fastapi import FastAPI +from pydantic import BaseModel + + +class Item(BaseModel): + func_name: str + func_args: str + + +app = FastAPI() + + +@app.post("/api/functions/") +async def root(item: Item): + func_name = item.func_name + func_args = json.loads(json.dumps(eval(item.func_args))) + + str1 = "from MyFunc.{} import {}".format(func_name, func_name) + str2 = "{}(**func_args).exec()".format(func_name) + + exec(str1) + result = eval(str2) + + return {"info": "success", "result": result} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d8177e2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi~=0.85.0 +pydantic~=1.10.2 \ No newline at end of file diff --git a/test_main.http b/test_main.http new file mode 100644 index 0000000..a2d81a9 --- /dev/null +++ b/test_main.http @@ -0,0 +1,11 @@ +# Test your FastAPI endpoints + +GET http://127.0.0.1:8000/ +Accept: application/json + +### + +GET http://127.0.0.1:8000/hello/User +Accept: application/json + +###