dipas.madx.parser module¶
Functionality for parsing MADX files.
Some details of the parsing procedure are configured (and may be altered) by the following module-level attributes.
-
dipas.madx.parser.
replacement_string_for_dots_in_variable_names
¶ The parser does not support dots in variable names (it only supports variable names that are valid Python identifiers) and so each dot in a variable name will be replaced by the string indicated by this attribute.
- Type
str
-
dipas.madx.parser.
negative_offset_tolerance
¶ If during sequence expansion (pad_sequence more specifically) a negative offset between two elements is encountered the parser will raise a ParserError if this offset is smaller than this attribute.
- Type
float
-
dipas.madx.parser.
minimum_offset_for_drift
¶ During sequence expansion (pad_sequence more specifically) an implicit drift space will be padded by an explicit drift space only if the offset between the two involved elements is greater than this attribute.
- Type
float
-
dipas.madx.parser.
allow_popup_variables
¶ If an expression consists of a single variable name which cannot be resolved (i.e. that variable was not defined before), then the parser will fallback on (float) zero. If this parameter is false a ParserError will be raised instead. Example for a “popup variable”:
quadrupole, k1 = xyz
wherexyz
was not defined before. This will either fall back on0.0
or raise an error, depending on this parameter.- Type
bool, default = True
-
dipas.madx.parser.
rng_default_seed
¶ The default seed for the random number generator used for evaluating calls to functions such as “ranf” (np.random.random) or “gauss” (np.random.normal).
- Type
int
-
dipas.madx.parser.
command_str_attributes
¶ Command attributes with names that are part of this set are assumed to be strings and will hence not be evaluated (i.e. not name resolution, evaluation of formulas, etc). By default this only lists argument names that are strings by MADX definitions.
- Type
set
-
dipas.madx.parser.
special_names
¶ During evaluation of expressions this dict will be used for resolving any names that are encountered. By default this contains functions such as
"asin": np.arcsin
or constants like"pi": np.pi
.- Type
dict
-
dipas.madx.parser.
particle_dict
¶ Given a BEAM command the parser computes the relativistic beta and gamma factors from the given quantities (actually it augments the BEAM command by all other quantities). This dict is used for resolving particles names given by
BEAM, particle = xyz;
(i.e. selects the corresponding charge and mass).- Type
dict
-
dipas.madx.parser.
patterns
¶ Contains regex patterns for MADX statements (e.g. comments, variable definitions, etc) mapped to by a corresponding (descriptive) name. If a pattern is matched a corresponding statement handler will be invoked which must have been previously registered via register_handler (or inserted into statement_parsers).
- Type
dict
-
dipas.madx.parser.
statement_handlers
¶ This dict maps pattern names (keys of the
patterns
dict) to corresponding statement handlers. A handler will be invoked when the corresponding statement pattern matched a statement. For more details on the signature of such a handler, seeregister_handler()
.- Type
dict
-
dipas.madx.parser.
VARIABLE_INDICATOR
¶ The string which is used to indicate flow variables in comments (by default this is
[flow] variable
).- Type
str
-
dipas.madx.parser.
prepare_script
¶ Contains functions that that perform preparation steps prior to parsing the script. All the listed preparation steps are performed in order on the particular previous result. This signature of such preparation step function should be
(str) -> str
, i.e. accept the current version of the (partially) prepared script and return a new version. The last function must return a list of single statements when given the prepared script (i.e.(str) -> List[str]
).- Type
list
-
dipas.madx.parser.
prepare_statement
¶ Contains functions that perform preparation steps for each single statement in order, prior to parsing it. The signature of these functions should be
(str) -> str
, i.e. accepting the current version of the (partially) prepared statement and return a new version.- Type
list
-
dipas.madx.parser.
parse_file
(f_name: str) → dipas.madx.parser.Script¶ - Auxiliary function for parse_script working on file names which also resolves references to other scripts
via
CALL, file = ...
.
- Parameters
f_name (str) – File name pointing to the MADX script.
- Returns
- Return type
See
parse_script()
.
-
dipas.madx.parser.
parse_script
(script: str) → dipas.madx.parser.Script¶ Parses a MADX script and returns the relevant commands list as well as command variable definitions.
Flow variables should be declared on a separate statement and indicated using one of the following syntax options:
q1_k1 = 0; // < optional text goes here > [flow] variable q1: quadrupole, l=1, k1=q1_k1; // < optional text goes here > [flow] variable q1_k1 = 0; q1: quadrupole, l=1, k1=q1_k1; q1: quadrupole, l=1, k1=0; q1->k1 = 0; // < optional text goes here > [flow] variable
Important
If the script contains any
CALL, file = ...
commands these will not be resolved (just parsed as such). Use parse_file for that purpose.- Parameters
script (str) – The MADX script’s content.
- Returns
script
- Return type
Script
- Raises
ParserError – If a misplaced variable indicator is encountered, e.g. not followed by a variable definition.