hibtc / cpymad

Cython binding to MAD-X
http://hibtc.github.io/cpymad/
Other
27 stars 18 forks source link

What is the preferred way to retrieve the command (string) with which an Element was created? #112

Closed Dominik1123 closed 1 year ago

Dominik1123 commented 1 year ago

Consider the following example test.madx script:

base: quadrupole, l = 0.1;
q01: base, k1 = 0.1;

seq: sequence, l = 0.1;
q01, at = 0.05;
endsequence;

beam;
use, sequence = seq;

I can get the element q01 in the following way:

>>> from subprocess import DEVNULL
>>> from cpymad.madx import Madx
>>> m = Madx(stdout=DEVNULL)
>>> m.call(file='test.madx')
>>> m.elements['q01']
q01: base, k1=0.1;

The repr shows me that this element was created with command base. I can access this parent and the base via parent and base_type respectively:

>>> m.elements['q01'].parent
base: quadrupole, l=0.1;
>>> m.elements['q01'].base_type
quadrupole, at=1e+20, l=0.0, kmax=0.0, kmin=0.0, calib=0.0, polarity=0.0, tilt=0.0, k1=0.0, k1s=0.0, ktap=0.0, mech_sep=0.0, v_pos=0.0, aperture={0.0}, aper_offset={0.0}, aper_tol={0.0,0.0,0.0}, aper_vx={-1.0}, aper_vy={-1.0}, knl={0.0}, ksl={0.0}, slice=1, thick=false, magnet=1, slot_id=0, assembly_id=0, model=-1, method=-1, exact=-1, nst=-1, apertype=circle, fringe=0, bend_fringe=false, kill_ent_fringe=false, kill_exi_fringe=false, dx=0.0, dy=0.0, ds=0.0, dtheta=0.0, dphi=0.0, dpsi=0.0, aper_tilt=0.0, k0=0.0;

But how can I retrieve the information that the parent command is "base" and the base command is "quadrupole"? I.e. for a given element I would like get the commands that were used to create that element as strings. I realized that these are stored in the _attr instance attribute, but that doesn't seem to be part of the public API due to underscore prefix. So I wondered what is the preferred way to retrieve that information?

>>> m.elements['q01']._attr
{'name': 'q01', 'length': 0.1, 'parent': 'base', 'base_type': 'quadrupole', 'index': 45}
coldfix commented 1 year ago

But how can I retrieve the information that the parent command is "base" and the base command is "quadrupole"?

are you looking for elem.parent.name and elem.base_type.name?

I would like get the commands that were used to create that element as strings.

that might not be possible exactly in full generality.

I realized that these are stored in the _attr instance attribute, but that doesn't seem to be part of the public API due to underscore prefix

If it contains what you need, I recommend to just use it anyway.As you know "private" in python just means that the developers reserve the possibility to change it between versions without prior notice, so you may have to change code when upgrading, but in this case it's unlikely to change anytime soon.

Dominik1123 commented 1 year ago

Thank you for the information. The .name attribute seems to be what I need. I considered dir(m.elements['q01']) and the docs for information on what's available, but name isn't listed there, so that's why I ended up asking this question.

coldfix commented 1 year ago

Okay. I guess the docs should be expanded to hint at what attributes might be available. Feel free to submit a PR.