at.load.file_input#

Generic text parsers for conversion of lattices in different formats to AT.

Functions

ignore_class(classname, baseclass[, module])

Generate a class for ignored elements.

skip_class(classname, baseclass[, module])

Generate a class for skipped elements.

Classes

AnyDescr(*args, **kwargs)

Base class for source object descriptors.

BaseParser(env[, strict, verbose, always_force])

Generic file parser.

DictNoDot(*args[, verbose])

ElementDescr(*args[, origin])

Simple representation of an element as a dict.

LowerCaseParser(env[, strict, verbose, ...])

Case independent parser.

SequenceDescr(*args, **kwargs)

Simple representation of a sequence of elements as a list.

UnorderedParser(env, **kwargs)

Parser allowing definitions in any order.

UpperCaseParser(env[, strict, verbose, ...])

Case independent parser.

class AnyDescr(*args, **kwargs)[source]#

Bases: object

Base class for source object descriptors.

classmethod argparser(parser, argcount, argstr)[source]#

Specialised argument parser.

expand(parser)[source]#

Iterator on the generated AT elements.

inverted(copy=False)[source]#

Return a reversed element or line.

update(*args, **kwargs)[source]#
bool_attr: ClassVar[set[str]] = {}#

list of names of bool attributes

pos_args: ClassVar[tuple[str, ...]] = ()#

list of names of positional arguments

str_attr: ClassVar[set[str]] = {}#

list of names of str attributes

class BaseParser(env, strict=True, verbose=False, always_force=True, **kwargs)[source]#

Bases: DictNoDot, StrParser

Generic file parser.

Analyses files with the following MAD-like format:

variable = value

label : command [,attribute=value] [,attribute=value]...

The parser builds a database of all the defined objects

Parameters:
  • env (dict[str, Any]) – global namespace used for evaluating commands

  • verbose (bool) – If True, print detail on the processing

  • strict (bool) – If False, assign 0 to undefined variables

  • **kwargs – dict initialiser.

clear()[source]#

Clear the database: remove all parameters and objects.

evaluate(expr)[source]#

Evaluate the right side of an expression.

Parameters:

expr (str) – expression to evaluate

Returns:

value – evaluated expression

expand(key)[source]#

iterator over AT objects generated by a source object.

lattice(use='ring', **kwargs)[source]#

Create a lattice from the selected sequence.

Parameters:

use – Name of the sequence or line containing the desired lattice. Default: ring

Keyword Arguments:
  • name (str) – Name of the lattice. Default: sequence name.

  • particle (Particle) – Circulating particle. Default: Particle(“relativistic”)

  • energy (float) – Energy of the lattice [eV]. Default: 1.0 GeV

  • periodicity (int) – Number of periods. Default: 1

  • * – All other keywords will be set as Lattice attributes

parse_files(*filenames, final=True, prolog=None, epilog=None, **kwargs)[source]#

Process files and fill the database.

Parameters:
  • *filenames (str | Path) – Files to process

  • final (bool) – If True, signals that the undefined variables may be set to the default value

  • prolog (None | int | Callable[[...], None])

  • epilog (Callable[[...], None] | None)

  • **kwargs – Initial variable definitions

parse_lines(lines, *, final=True, **kwargs)[source]#

Process input lines and fill the database.

Parameters:
  • lines (Iterable[str]) – Iterable of input lines

  • final (bool) – If True, signals that the undefined variables may be set to the default value

  • **kwargs – Initial variable definitions

property ignored#

Set of ignored commands.

in_file: list[str]#
property missing#

Set of missing definitions

postponed: list[tuple]#
property sequences#

List of available sequences or lines.

class DictNoDot(*args, verbose=False, **kwargs)[source]#

Bases: dict

get(key, *args)[source]#

Return the value for key if key is in the dictionary, else default.

class ElementDescr(*args, origin=None, **kwargs)[source]#

Bases: AnyDescr, dict

Simple representation of an element as a dict.

classmethod from_at(kwargs)[source]#
static attr_format(value)[source]#
static meval(params)[source]#

Evaluation of superfluous parameters.

expand(parser)[source]#

Iterator on the generated AT elements.

to_at(*args, **params)[source]#

Generate the AT element. Must be overloaded for each specific element.

array_fmt = {40: 123, 41: 125, 91: 123, 93: 125}#
at2mad: ClassVar[dict[str, str]] = {'Length': 'L'}#
bool_fmt: ClassVar[dict[bool, str]] = {False: 'False', True: 'True'}#
property length: float#

Element length.

class LowerCaseParser(env, strict=True, verbose=False, always_force=True, **kwargs)[source]#

Bases: BaseParser

Case independent parser.

Parameters:
  • env (dict[str, Any]) – global namespace used for evaluating commands

  • verbose (bool) – If True, print detail on the processing

  • strict (bool) – If False, assign 0 to undefined variables

  • **kwargs – dict initialiser.

class SequenceDescr(*args, **kwargs)[source]#

Bases: AnyDescr, list

Simple representation of a sequence of elements as a list.

property length: float#
class UnorderedParser(env, **kwargs)[source]#

Bases: BaseParser

Parser allowing definitions in any order.

This is done by storing the failed statements in a queue and iteratively trying to execute them after all input statements have been processed, until the number of failures is constant (hopefully zero)

Parameters:
  • env (dict[str, Any]) – global namespace used for evaluating commands

  • verbose – If True, print detail on the processing

  • strict – If False, assign 0 to undefined variables

  • **kwargs – dict initialiser.

class UpperCaseParser(env, strict=True, verbose=False, always_force=True, **kwargs)[source]#

Bases: BaseParser

Case independent parser.

Parameters:
  • env (dict[str, Any]) – global namespace used for evaluating commands

  • verbose (bool) – If True, print detail on the processing

  • strict (bool) – If False, assign 0 to undefined variables

  • **kwargs – dict initialiser.

ignore_class(classname, baseclass, module=None, **kwargs)[source]#

Generate a class for ignored elements.

The element generates an AT Drift or Marker element depending on its length.

Parameters:
  • classname (str) – Name of the generated class

  • baseclass (type[ElementDescr]) – Base class, must be a subclass of ElementDescr

  • module (str | None) – Name of the module where the class is defined. If None, use the module of the base class.

  • **kwargs – dictionary of additional attributes and methods. See type().

Returns:

cls – Element class, converted to Marker or Drift when generating AT elements

skip_class(classname, baseclass, module=None, **kwargs)[source]#

Generate a class for skipped elements.

No AT element is generated for these elements.

Parameters:
  • classname (str) – Name of the generated class

  • baseclass (type[ElementDescr]) – Base class, must be a subclass of ElementDescr

  • module (str | None) – Name of the module where the class is defined. If None, use the module of the base class.

  • **kwargs – dictionary of additional attributes and methods. See type().

Returns:

cls – Element class, skipped when generating AT elements