From fe37805979e94aa36d1e7d8b3a958ecfa446c224 Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 19 Feb 2024 10:59:33 +0000 Subject: [PATCH] make attribute name more pythonic --- demo/charts.py | 6 ++--- src/python-fastui/fastui/components/charts.py | 22 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/demo/charts.py b/demo/charts.py index c110c426..8b6c7e4d 100644 --- a/demo/charts.py +++ b/demo/charts.py @@ -69,9 +69,9 @@ def charts_content_view(kind: str) -> list[AnyComponent]: width='100%', height=300, data=data_list, - xKey='name', - yKeys=['pv', 'uv', 'amt'], - yKeysNames=['Page Views', 'Unique Views', 'Amount'], + x_key='name', + y_keys=['pv', 'uv', 'amt'], + y_keys_names=['Page Views', 'Unique Views', 'Amount'], colors=['#8884d8', '#82ca9d', '#ffc658'], ), ] diff --git a/src/python-fastui/fastui/components/charts.py b/src/python-fastui/fastui/components/charts.py index 3a6e38a7..80669344 100644 --- a/src/python-fastui/fastui/components/charts.py +++ b/src/python-fastui/fastui/components/charts.py @@ -1,7 +1,7 @@ import typing as _t from abc import ABC -import pydantic +import pydantic as _p from .. import class_name as _class_name @@ -9,10 +9,10 @@ pass -DataPoint = _t.TypeVar('DataPoint', bound=pydantic.BaseModel) +DataPoint = _t.TypeVar('DataPoint', bound=_p.BaseModel) -class BaseChart(pydantic.BaseModel, ABC, defer_build=True): +class BaseChart(_p.BaseModel, ABC, defer_build=True): title: str width: _t.Union[int, str] = '100%' height: _t.Union[int, str] @@ -22,16 +22,16 @@ class BaseChart(pydantic.BaseModel, ABC, defer_build=True): class RechartsLineChart(BaseChart): type: _t.Literal['RechartsLineChart'] = 'RechartsLineChart' - xKey: str - yKeys: _t.List[str] - yKeysNames: _t.Union[_t.List[str], None] = None + x_key: str = _p.Field(..., serialization_alias='xKey') + y_keys: _t.List[str] = _p.Field(..., serialization_alias='yKeys') + y_keys_names: _t.Union[_t.List[str], None] = _p.Field(None, serialization_alias='yKeysNames') colors: _t.List[str] tooltip: bool = True - @pydantic.model_validator(mode='after') + @_p.model_validator(mode='after') def check_length_of_y_keys_colors_and_y_keys_names(self): - if len(self.yKeys) != len(self.colors): - raise pydantic.ValidationError('Length of yKeys and colors must be the same') - if self.yKeysNames and len(self.yKeys) != len(self.yKeysNames): - raise pydantic.ValidationError('Length of yKeys and yKeysNames must be the same') + if len(self.y_keys) != len(self.colors): + raise _p.ValidationError('Length of y_keys and colors must be the same') + if self.y_keys_names and len(self.y_keys) != len(self.y_keys_names): + raise _p.ValidationError('Length of y_keys and y_keys_names must be the same') return self