pymad/pymad — pull request #49 by coldfix (2014-03-25 03:51:33+00:00)
Title: extend libmadx.py API to access sequence information

I am planning to add and alter some functionality in *libmadx* to make the *madx* module more useful on its own. My idea is the following:

Create a class `Sequence` that internally just stores the name of the sequence. It has `@property`s to lazily obtain more information when/if desired:
- beam (ex, ey, ...)
- last twiss table (directly via the sequence)
- access to elements (as you started in your branch)
- more ideas?
- ~~there will be a classmethod `get_sequences` returning a list of instances.~~ scrub that, it can't be a classmethod, since:
- `Sequence` objects are instanciated via `Madx.get_sequence(name)`, `Madx.get_sequences()` or similar (they need to be specific to a `Madx` instance)

Most of this should be possible to add without touching any backward compatibility issues. Changes to the current `libmadx.py`:

- add `get_twiss(sequence) -> numpy.array`
- add `get_beam(sequence) -> dict`
- add `get_elements(sequence) -> list(Element)` (saving this for #38, though)
- add `get_expanded_elements(sequence) -> list(Element)`
- add `get_current_sequence() -> name` making it possible to use `Madx.*` methods without specifying the sequence name
- modify `get_sequences() -> list(names)` (incompatible, but much nicer!)

I am not fully decided whether to store a pointer to the C-struct or the name of the sequence within `class Sequence`. I tend towards storing the name, because
- although the pointer is slightly better in terms of performance
- there may be memory issues with obsolete pointers, when a sequence gets destroyed or moved (is that possible? anyway, it might be someday)
- storing the pointer as class member means it must be a `cdef class`, instances must be created from *libmadx*. This in turn creates additional complexity when marshaling the instances between processes.

Furthermore, I'd like to remove the print statements from *libmadx.get_table* and replace them with `raise RuntimeError` or similar. The print statements in *libmadx.get_sequences* will be obsolete anyway, the properties can then be examined via the class.

By the way, with this and #38, my GUI application will probably become useful for plain MADX files, very soon.

Any thoughts?