indexcalculation/Indicators/Profit/BITDAProfitMargin.py

64 lines
1.6 KiB
Python

"""
BITDA利润率
"""
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from Common.schemas import IndicatorDescription
router = APIRouter()
# 指标描述
ENAME = "BITDA_profit_margin"
CNAME = "BITDA利润率"
NATURE = "定量"
CATEGORY = "盈利能力"
DESCRIPTION = "BITDA利润率 = 利润总额 / 营业收入"
# 参数描述
PARAM1_ENAME = "total_profit"
PARAM1_CNAME = "利润总额"
PARAM1_DESCR = "利润表-利润总额"
PARAM2_ENAME = "operating_income"
PARAM2_CNAME = "营业收入"
PARAM2_DESCR = "利润表-营业收入"
# 输入参数
class Parameter(BaseModel):
total_profit: float = "利润总额"
operating_income: float = "营业收入"
# 计算接口
@router.post("/{}/".format(ENAME), tags=[CATEGORY], summary=CNAME, description=DESCRIPTION)
def calculation(parameter: Parameter):
try:
result = parameter.total_profit / parameter.operating_income
return round(result, 6)
except ZeroDivisionError:
return "+inf"
except Exception:
raise HTTPException(status_code=400, detail="Calculate Failed")
# 描述接口
@router.get("/{}/description".format(ENAME),
response_model=IndicatorDescription,
tags=[CATEGORY], summary=CNAME, description=DESCRIPTION)
def description():
body = {
"ename": ENAME,
"cname": CNAME,
"nature": NATURE,
"category": CATEGORY,
"description": DESCRIPTION,
"parameters": [
{"ename": PARAM1_ENAME, "cname": PARAM1_CNAME, "description": PARAM1_DESCR},
{"ename": PARAM2_ENAME, "cname": PARAM2_CNAME, "description": PARAM2_DESCR}
],
}
return body