Skip to content

Commit

Permalink
Add: Response and Request Types
Browse files Browse the repository at this point in the history
  • Loading branch information
happycastle114 committed Nov 16, 2023
1 parent 4b7579d commit 5cea864
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
from contextlib import asynccontextmanager
from typing import Dict
from model import MongoDB
from model.ResponseType import ChartResponse, SexInfo, PartyInfo, AgeInfo


@asynccontextmanager
async def initMongo(app: FastAPI):
MongoDB.MongoDB().connect()
yield
MongoDB.MongoDB().close()

new = ChartResponse[SexInfo]

app = FastAPI(lifespan=initMongo, responses={404: {"description": "Not found"}})


app.include_router(scrapResult.router)
37 changes: 37 additions & 0 deletions model/ResponseType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from pydantic import BaseModel
from typing import TypeVar, Generic

class LocalInfo(BaseModel):
name : str
id : int

class RegionInfo(BaseModel):
name : str
id : int
local: list[LocalInfo]

class PartyInfo(BaseModel):
name : str
color : int

class Diversity(BaseModel):
action_type : str
value : float

class AgeInfo(BaseModel):
minAge: int
maxAge: int
count: int

class PartyInfo(BaseModel):
party : str
count : int

class SexInfo(BaseModel):
sex: str
count: int

T = TypeVar("T", SexInfo, PartyInfo, AgeInfo)

class ChartResponse(BaseModel, Generic[T]):
data : list[T]
2 changes: 1 addition & 1 deletion model/ScrapResult.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import BaseModel, Field
from enum import Enum, StrEnum
from enum import StrEnum

class CouncilType(StrEnum):
local_council = "local_council"
Expand Down
8 changes: 7 additions & 1 deletion utils/diversity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import math

def count(data, stair = 0):
return Counter(data)
"""
Count the number of occurrences of each value in a dataset
"""
counts = Counter()
for row in data:
counts[row[stair]] += 1
return counts

def gini_simpson(data, stair, opts):
"""
Expand Down

0 comments on commit 5cea864

Please sign in to comment.