MOD Parser¶
- class dendrotweaks.biophys.io.parser.MODFileParser[source]¶
Bases:
object
A parser for MOD files that uses a Pyparsing grammar to parse the content of the file.
- get_ast() Dict [source]¶
Get the abstract syntax tree of the parsed content. Available after parsing the content of the file.
- parse_block(block_name: str, block_content: List[str]) List[Dict] [source]¶
Parse a block of the MOD file. Ensures that parsing is independent for each block.
- parse(blocks: Dict[str, List[str]], verbose: bool = True) None [source]¶
Parse the entire content of the file.
- Parameters:
blocks (Dict[str, List[str]]) – A dictionary with the blocks of the MOD file.
- postprocess(restore_expressions=True)[source]¶
Postprocess the parsed AST.
- Parameters:
restore_expressions (bool) – Whether to restore the expressions in the AST to their original form after parsing.
- find_in_blocks(pattern, block_types=['ASSIGNED', 'PROCEDURE', 'PARAMETER'])[source]¶
Find a pattern in the specified block types.
- Parameters:
pattern (str) – The regex pattern to search for in the blocks.
block_types (list) – A list of block types to search for the pattern.
- Returns:
A list of matching strings if found, a single matching string if there’s only one, or None if no matches are found.
- Return type:
str or list or None
Examples
Find the name of the variable representing time constant of a state variable: >>> pattern = re.compile(‘tau’, re.IGNORECASE) >>> parser.find_in_blocks(‘tau’) [‘m_tau’, ‘hTau’, ‘ntau’]
- replace_in_blocks(replacements, block_types=['FUNCTION', 'PROCEDURE'])[source]¶
Replace the variable names with their values or another variable name in the specified block types.
- Parameters:
replacements (dict) – A dictionary with the constants as keys and their replacement values.
block_types (list) – A list of block types to apply the replacements to.
Examples
Replace the Faraday constant with its value in every FUNCTION block: >>> parser.replace({‘FARADAY’: 96485.309}, block_types=[‘FUNCTION’])