Skip to content

Conversation

@EZoni
Copy link
Member

@EZoni EZoni commented Jul 7, 2025

This is an implementation of the suggestion made in #12 (comment).

@jlvay
Copy link
Member

jlvay commented Jul 7, 2025

But the name is written twice then, which is redundant.

Also, why not

line:
  kind: Line
  ...

?

@ax3l
Copy link
Member

ax3l commented Jul 7, 2025

Discussed: same problem as #12 (comment)

@EZoni EZoni changed the title Alternative implementation of name based listing [WIP] Alternative implementation of name based listing Jul 8, 2025
@EZoni EZoni changed the title [WIP] Alternative implementation of name based listing [WIP] Implement line as a list of dict Jul 9, 2025
Comment on lines +22 to +26
@model_serializer
def _serialize(self):
data = self.__dict__.copy()
data.pop("name", None)
return data
Copy link
Member Author

@EZoni EZoni Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ax3l

This (i.e., overloading the model serialization) is the only way I found so far to produce an output like

kind: Line
line:
- drift1:
    kind: Drift
    length: 0.25
- quad1:
    MagneticMultipoleP:
      Bn1: 1.0
    kind: Quadrupole
    length: 1.0
- drift2:
    kind: Drift
    length: 0.5
- quad2:
    MagneticMultipoleP:
      Bn1: -1.0
    kind: Quadrupole
    length: 1.0
- drift3:
    kind: Drift
    length: 0.5

instead of

kind: Line
line:
- drift1:
    kind: Drift
    length: 0.25
    name: drift1
- quad1:
    MagneticMultipoleP:
      Bn1: 1.0
    kind: Quadrupole
    length: 1.0
    name: quad1
- drift2:
    kind: Drift
    length: 0.5
    name: drift2
- quad2:
    MagneticMultipoleP:
      Bn1: -1.0
    kind: Quadrupole
    length: 1.0
    name: quad2
- drift3:
    kind: Drift
    length: 0.5
    name: drift3

with the name attributes printed out as well (hence repeated).

Do you see other ways to obtain this, other than overloading the model serialization?

I guess we would have to do something similar for the deserialization (through @model_validator(mode="before")?), although I have not been able to make that work just yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the custom deserialization is necessary if we expect that this

yaml_data = yaml.dump(line.model_dump(), default_flow_style=False)
loaded_line = Line(**yaml_data)

should lead to line and loaded_line being identical (which we currently assert in the tests and examples).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible alternative posted in #14 that does keep the API user-friendly :)

@EZoni EZoni changed the title [WIP] Implement line as a list of dict Implement line as a list of dict Jul 9, 2025
@ax3l ax3l mentioned this pull request Jul 9, 2025
ax3l added a commit that referenced this pull request Jul 16, 2025
Overwrite validation and model dump.

Alternate implementation of #13 that does not make the Python API harder
to use.

---------

Co-authored-by: Edoardo Zoni <ezoni@lbl.gov>
@ax3l
Copy link
Member

ax3l commented Jul 16, 2025

Replaced by #14 :)

@ax3l ax3l closed this Jul 16, 2025
@EZoni EZoni deleted the schema_use_dicts branch July 21, 2025 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants