Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object handles: add support for Pydantic models, (data)classes & plots #154

Open
fzumstein opened this issue Oct 13, 2024 · 0 comments
Open

Comments

@fzumstein
Copy link
Member

fzumstein commented Oct 13, 2024

As a starting point, converting to dict already works:

def test_pydantic_model():
    from pydantic import BaseModel
    
    class MyModel(BaseModel, arbitrary_types_allowed=True):
        a: str
        d: dict
        df: pd.DataFrame
    mymodel = MyModel(a="b", d={"a": 1}, df=pd.DataFrame({"a": [1], "b": [2]}))
    mymodel2 = MyModel.model_validate(deserialize(serialize(dict(mymodel))))
    assert mymodel.a == mymodel2.a
    assert mymodel.d == mymodel2.d
    assert_frame_equal(mymodel.df, mymodel2.df)


def test_dataclass():
    from dataclasses import dataclass

    @dataclass
    class MyDataClass:
        a: str
        d: dict
        df: pd.DataFrame

    mydataclass = MyDataClass(a="b", d={"a": 1}, df=pd.DataFrame({"a": [1], "b": [2]}))
    mydataclass2 = MyDataClass(**deserialize(serialize(mydataclass.__dict__)))
    assert mydataclass.a == mydataclass2.a
    assert mydataclass.d == mydataclass2.d
    assert_frame_equal(mydataclass.df, mydataclass2.df)
@fzumstein fzumstein changed the title Object handles: add support for Pydantic models Object handles: add support for Pydantic models and (data)classes Oct 14, 2024
@fzumstein fzumstein changed the title Object handles: add support for Pydantic models and (data)classes Object handles: add support for Pydantic models, (data)classes & plots Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant