Skip to content

Commit

Permalink
speed up VektonnBaseModel.json() for Vector model (see #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKostousov committed Feb 25, 2022
1 parent 054061c commit 59571a3
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/vektonn/dtos.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,26 @@ class Config:
json_dumps = orjson_dumps


class ToDictMixin:
def dict(
self,
*,
by_alias: bool = True,
exclude_none: bool = True,
**kwargs,
) -> dict:
return {
self.__fields__[field_name].alias if by_alias else field_name: value
for field_name, value in self
if value is not None or not exclude_none
}


class ErrorResult(VektonnBaseModel):
error_messages: List[str]


class Vector(VektonnBaseModel):
class Vector(ToDictMixin, VektonnBaseModel):
is_sparse: bool
coordinates: List[float]
coordinate_indices: Optional[List[int]]
Expand Down

0 comments on commit 59571a3

Please sign in to comment.