From df8a550ce794e030e014c44104b0a1d6f73ce391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Tue, 11 Oct 2022 09:49:50 +0800 Subject: [PATCH] changes --- MyFunc/OperatingProfitRatio.py | 3 ++- MyFunc/ReturnOnNetAssets.py | 3 ++- MyFunc/ReturnOnTotalAssets.py | 3 ++- main.py | 39 ++++++++++++++++++++++------------ 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/MyFunc/OperatingProfitRatio.py b/MyFunc/OperatingProfitRatio.py index 9d824de..4c53bef 100644 --- a/MyFunc/OperatingProfitRatio.py +++ b/MyFunc/OperatingProfitRatio.py @@ -6,4 +6,5 @@ class OperatingProfitRatio(BaseModel): operating_income: float def exec(self): - return self.the_total_profit/self.operating_income + result = self.the_total_profit / self.operating_income + return round(result, 6) diff --git a/MyFunc/ReturnOnNetAssets.py b/MyFunc/ReturnOnNetAssets.py index 32cb82f..445485b 100644 --- a/MyFunc/ReturnOnNetAssets.py +++ b/MyFunc/ReturnOnNetAssets.py @@ -7,4 +7,5 @@ class ReturnOnNetAssets(BaseModel): previous_period_total_owner_equity: float def exec(self): - return self.net_profit / ((self.current_period_total_owner_equity + self.previous_period_total_owner_equity) / 2) + result = self.net_profit / ((self.current_period_total_owner_equity + self.previous_period_total_owner_equity) / 2) + return result diff --git a/MyFunc/ReturnOnTotalAssets.py b/MyFunc/ReturnOnTotalAssets.py index f372caf..734ee82 100644 --- a/MyFunc/ReturnOnTotalAssets.py +++ b/MyFunc/ReturnOnTotalAssets.py @@ -8,4 +8,5 @@ class ReturnOnTotalAssets(BaseModel): last_total_assets: float def exec(self): - return (self.the_total_profit + self.interest_expense) / ((self.current_total_assets + self.last_total_assets) / 2) + result = (self.the_total_profit + self.interest_expense) / ((self.current_total_assets + self.last_total_assets) / 2) + return result diff --git a/main.py b/main.py index 491762d..9cd57b6 100644 --- a/main.py +++ b/main.py @@ -1,17 +1,20 @@ -import json - -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel +from pydantic.error_wrappers import ValidationError -class Item(BaseModel): +class Args(BaseModel): func_name: str = "ReturnOnNetAssets" # 指标函数名称 - func_args: str = "{'net_profit': 1, 'current_period_total_owner_equity': 2, 'previous_period_total_owner_equity': 3}" # 指标函数参数 + func_args: dict = {'net_profit': 1, 'current_period_total_owner_equity': 2, 'previous_period_total_owner_equity': 3} # 指标函数参数 -app = FastAPI() +app = FastAPI( + title="指标函数计算接口", + description="发送指标计算请求,返回计算后的指标数值", + version="v1.0.0" +) app.add_middleware( CORSMiddleware, @@ -23,14 +26,22 @@ app.add_middleware( @app.post("/api/003/indicator_calculator/execute/", summary='输入参数,计算指标数值', description='接口描述', tags=['计算指标数值']) -async def root(item: Item): - func_name = item.func_name - func_args = json.loads(json.dumps(eval(item.func_args))) +async def root(args: Args): - str1 = "from MyFunc.{} import {}".format(func_name, func_name) - str2 = "{}(**func_args).exec()".format(func_name) + try: + func_name = dict(args)["func_name"] + func_args = dict(args)["func_args"] - exec(str1) - result = eval(str2) + str1 = "from MyFunc.{} import {}".format(func_name, func_name) + str2 = "{}(**func_args).exec()".format(func_name) - return result + exec(str1) + result = eval(str2) + + return result + + except ModuleNotFoundError: + raise HTTPException(status_code=400, detail="Function Not Found") + + except ValidationError as e: + raise HTTPException(status_code=400, detail="FuncArgs Incorrect")