Skip to content

Conversation

@EZoni
Copy link
Member

@EZoni EZoni commented Apr 30, 2025

Following @cemitch99's hint about the default ordering of attributes applied when we call Pydantic's default model_dump (i.e., upper-case has precedence over lower-case, then alphabetical order), we could also rename kind as Type (upper-case, so it is treated with precedence) and MagneticMultipoleP as magnetic_multipole_parameters (lower-case, so it is treated like all other attributes).

Other options for magnetic_multipole_parameters could work too, e.g., magnetic_multipole.

The result for the FODO example, without further tweaks, would be the following:

  • YAML format:
Type: Line
line:
- Type: Drift
  length: 0.25
  name: drift1
- Type: Quadrupole
  length: 1.0
  magnetic_multipole_parameters:
    Bn1: 1.0
  name: quad1
- Type: Drift
  length: 0.5
  name: drift2
- Type: Quadrupole
  length: 1.0
  magnetic_multipole_parameters:
    Bn1: -1.0
  name: quad2
- Type: Drift
  length: 0.5
  name: drift3
  • JSON format:
{
  "Type": "Line",
  "line": [
    {
      "Type": "Drift",
      "length": 0.25,
      "name": "drift1"
    },
    {
      "Type": "Quadrupole",
      "length": 1.0,
      "magnetic_multipole_parameters": {
        "Bn1": 1.0
      },
      "name": "quad1"
    },
    {
      "Type": "Drift",
      "length": 0.5,
      "name": "drift2"
    },
    {
      "Type": "Quadrupole",
      "length": 1.0,
      "magnetic_multipole_parameters": {
        "Bn1": -1.0
      },
      "name": "quad2"
    },
    {
      "Type": "Drift",
      "length": 0.5,
      "name": "drift3"
    }
  ]
}

@EZoni EZoni requested review from DavidSagan, cemitch99 and jlvay April 30, 2025 19:01
@cemitch99
Copy link

Personally, I like this solution very much. If we wanted to stay close to the existing standard document, this label could also be magnetic_multipole_p, but I think more explicit is better.

@EZoni
Copy link
Member Author

EZoni commented Apr 30, 2025

Personally, I like this solution very much. If we wanted to stay close to the existing standard document, this label could also be magnetic_multipole_p, but I think more explicit is better.

Yes, to be discussed within PALS, but I would vote for changing the name in the standard, as the p or P alone are not very descriptive nor self-explanatory.

@jlvay
Copy link
Member

jlvay commented May 5, 2025

In the same vain, would changing name to Name automatically put it at the top of the list of attributes?

@DavidSagan
Copy link
Member

Do other parsers do the same thing? I would not like to make a change just for pydantic.

@DavidSagan
Copy link
Member

One way to preserve order is to use lists and then all parsers will do what we want.

@EZoni EZoni changed the title Rename Attributes kind and MagneticMultipoleP [WIP] Rename Attributes kind and MagneticMultipoleP May 21, 2025
@ax3l
Copy link
Member

ax3l commented Jul 16, 2025

We overwrite model_dump now since #14. I tried a quick sorting but did not succeed. If anyone likes feel free to give it another stab.

We can definitely not enforce ordering in PALS, because most YAML/JSON/etc. implementations use dictionaries that do not preserve insertion order. But for Python PALS, we might be able to give some preferred ordering when we serialize.

@EZoni
Copy link
Member Author

EZoni commented Jul 21, 2025

Closing as obsolete.

@EZoni EZoni closed this Jul 21, 2025
@EZoni EZoni deleted the kind_special_attr branch July 21, 2025 21:31
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.

5 participants