{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install pydantic","metadata":{"execution":{"iopub.status.busy":"2023-04-04T05:30:50.520314Z","iopub.execute_input":"2023-04-04T05:30:50.521232Z","iopub.status.idle":"2023-04-04T05:31:02.992010Z","shell.execute_reply.started":"2023-04-04T05:30:50.521170Z","shell.execute_reply":"2023-04-04T05:31:02.990709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# util.py\n\nfrom enum import Enum\n\n\ndef hyphenate(s: str) -> str:\n    return s.replace(\"_\", \"-\")\n\n\ndef to_snake_case(s: str) -> str:\n    return s[0].lower() + \"\".join(\n        c if c.islower() else (\"_\" + c.lower()) for c in s[1:]\n    )\n\n\nclass StrIntEnum(Enum):\n    def _generate_next_value_(name: str, _start, count: int, _last_values):\n        return (to_snake_case(name), count)\n\n    @classmethod\n    def __get_validators__(cls):\n        yield cls.validate\n\n    @classmethod\n    def validate(cls, v):\n        if isinstance(v, int):\n            return cls.from_int(v)\n        elif isinstance(v, str):\n            return cls.from_str(v)\n        else:\n            raise TypeError()\n\n    @classmethod\n    def int_to_str(cls):\n        if not hasattr(cls, \"_INT_TO_STR\"):\n            setattr(\n                cls,\n                \"_INT_TO_STR\",\n                {i: s for s, i in (v.value for v in cls.__members__.values())},\n            )\n        return getattr(cls, \"_INT_TO_STR\")\n\n    @classmethod\n    def str_to_int(cls):\n        if not hasattr(cls, \"_STR_TO_INT\"):\n            setattr(\n                cls,\n                \"_STR_TO_INT\",\n                {s: i for s, i in (v.value for v in cls.__members__.values())},\n            )\n        return getattr(cls, \"_STR_TO_INT\")\n\n    @classmethod\n    def from_int(cls, v: int):\n        return cls.int_to_str()[v]\n\n    @classmethod\n    def from_str(cls, v: str):\n        return cls.str_to_int()[v]","metadata":{"execution":{"iopub.status.busy":"2023-04-04T05:31:05.359545Z","iopub.execute_input":"2023-04-04T05:31:05.360553Z","iopub.status.idle":"2023-04-04T05:31:05.375811Z","shell.execute_reply.started":"2023-04-04T05:31:05.360492Z","shell.execute_reply":"2023-04-04T05:31:05.374569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data_models.py\n\nfrom pydantic import BaseModel\nfrom enum import Enum, auto\nfrom typing import List, Union\n\n#from util import StrIntEnum, hyphenate\n\n\nclass Source(str, Enum):\n    Generated = \"generated\"\n    Extracted = \"extracted\"\n\n\nclass ChartType(StrIntEnum):\n    Dot = auto()\n    HorizontalBar = auto()\n    VerticalBar = auto()\n    Line = auto()\n    Scatter = auto()\n\n\nclass TextRole(StrIntEnum):\n    ChartTitle = auto()\n    AxisTitle = auto()\n    LegendLabel = auto()\n    TickLabel = auto()\n    TickGrouping = auto()\n    Other = auto()\n\n\nclass TickTypes(StrIntEnum):\n    Separators = auto()\n    Markers = auto()\n\n\nclass ValueTypes(StrIntEnum):\n    Categorical = auto()\n    Numerical = auto()\n\n\nclass BoundingBox(BaseModel):\n    height: float\n    width: float\n    x0: float\n    y0: float\n\n\nclass Polygon(BaseModel):\n    x0: float\n    y0: float\n    x1: float\n    y1: float\n    x2: float\n    y2: float\n    x3: float\n    y3: float\n\n\nclass Text(BaseModel):\n    id: int\n    role: TextRole\n    polygon: Polygon\n    text: str\n\n\nclass DataSeries(BaseModel):\n    x: Union[str, float]\n    y: Union[str, float]\n\n\nclass TickPoint(BaseModel):\n    x: float\n    y: float\n\n\nclass Tick(BaseModel):\n    id: int\n    tick_pt: TickPoint\n\n\nclass AxisInner(BaseModel):\n    ticks: List[Tick]\n    tick_type: TickTypes\n    values_type: ValueTypes\n\n    class Config:\n        alias_generator = hyphenate\n\n\nclass Axes(BaseModel):\n    x_axis: AxisInner\n    y_axis: AxisInner\n\n    class Config:\n        alias_generator = hyphenate\n\n\nclass Annotation(BaseModel):\n    source: Source\n    chart_type: ChartType\n    plot_bb: BoundingBox\n    data_series: List[DataSeries]\n    axes: Axes\n    text: List[Text]\n\n    class Config:\n        alias_generator = hyphenate","metadata":{"execution":{"iopub.status.busy":"2023-04-04T05:32:07.780995Z","iopub.execute_input":"2023-04-04T05:32:07.781753Z","iopub.status.idle":"2023-04-04T05:32:07.804003Z","shell.execute_reply.started":"2023-04-04T05:32:07.781710Z","shell.execute_reply":"2023-04-04T05:32:07.802968Z"},"trusted":true},"execution_count":null,"outputs":[]}]}