DISPATCH
f90nml.parser.Parser Class Reference
Inheritance diagram for f90nml.parser.Parser:
Collaboration diagram for f90nml.parser.Parser:

Public Member Functions

def __init__ (self)
 
def comment_tokens (self)
 
def comment_tokens (self, value)
 
def default_start_index (self)
 
def default_start_index (self, value)
 
def sparse_arrays (self)
 
def sparse_arrays (self, value)
 
def global_start_index (self)
 
def global_start_index (self, value)
 
def row_major (self)
 
def row_major (self, value)
 
def strict_logical (self)
 
def strict_logical (self, value)
 
def read (self, nml_fname, nml_patch_in=None, patch_fname=None)
 

Public Attributes

 tokens
 
 token
 
 prior_token
 
 pfile
 

Detailed Description

Fortran namelist parser.

Definition at line 21 of file parser.py.

Constructor & Destructor Documentation

◆ __init__()

Member Function Documentation

◆ comment_tokens() [1/2]

def f90nml.parser.Parser.comment_tokens (   self)
List of tokens used to designate comments in a namelist file.

Some Fortran programs will introduce alternative comment tokens (e.g.
``#``) for internal preprocessing.

If you need to support these tokens, create a ``Parser`` object and set
the comment token as follows:

>>> parser = f90nml.Parser()
>>> parser.comment_tokens += '#'
>>> nml = Parser.read('sample.nml')

Be aware that this is non-standard Fortran and could mangle any strings
using the ``#`` characters.  Characters inside string delimiters should
be protected.

Definition at line 43 of file parser.py.

References f90nml.parser.Parser._comment_tokens.

Referenced by f90nml.parser.Parser.comment_tokens(), and f90nml.parser.Parser.read().

◆ comment_tokens() [2/2]

def f90nml.parser.Parser.comment_tokens (   self,
  value 
)
Validate and set the comment token string.

Definition at line 63 of file parser.py.

References f90nml.parser.Parser._comment_tokens, and f90nml.parser.Parser.comment_tokens().

◆ default_start_index() [1/2]

def f90nml.parser.Parser.default_start_index (   self)
Assumed starting index for a vector (Default: 1).

Since Fortran allows users to set an arbitrary start index, it is not
always possible to assign an index to values when no index range has
been provided.

For example, in the namelist ``idx.nml`` shown below, the index of the
values in the second assignment are ambiguous and depend on the
implicit starting index.

.. code-block:: fortran

   &idx_nml
       v(3:5) = 3, 4, 5
       v = 1, 2
   /

The indices of the second entry in ``v`` are ambiguous.  The result for
different values of ``default_start_index`` are shown below.

>>> parser = f90nml.Parser()
>>> parser.default_start_index = 1
>>> nml = parser.read('idx.nml')
>>> nml['idx_nml']['v']
[1, 2, 3, 4, 5]

>>> parser.default_start_index = 0
>>> nml = parser.read('idx.nml')
>>> nml['idx_nml']['v']
[1, 2, None, 3, 4, 5]

Definition at line 70 of file parser.py.

References f90nml.parser.Parser._default_start_index, and f90nml.namelist.Namelist._default_start_index.

Referenced by f90nml.parser.Parser.default_start_index(), and f90nml.parser.Parser.read().

◆ default_start_index() [2/2]

def f90nml.parser.Parser.default_start_index (   self,
  value 
)
Validate and set the default start index.

Definition at line 105 of file parser.py.

References f90nml.parser.Parser._default_start_index, f90nml.namelist.Namelist._default_start_index, and f90nml.parser.Parser.default_start_index().

◆ global_start_index() [1/2]

def f90nml.parser.Parser.global_start_index (   self)
Define an explicit start index for all vectors.

When set to ``None``, vectors are assumed to start at the lowest
specified index.  If no index appears in the namelist, then
``default_start_index`` is used.

When ``global_start_index`` is set, then all vectors will be created
using this starting index.

For the namelist file ``idx.nml`` shown below,

.. code-block:: fortran

   &idx_nml
      v(3:5) = 3, 4, 5
   /

the following Python code behaves as shown below.

>>> parser = f90nml.Parser()
>>> nml = parser.read('idx.nml')
>>> nml['idx_nml']['v']
[3, 4, 5]

>>> parser.global_start_index = 1
>>> nml = parser.read('idx.nml')
>>> nml['idx_nml']['v']
[None, None, 3, 4, 5]

Currently, this property expects a scalar, and applies this value to
all dimensions.

Definition at line 132 of file parser.py.

References f90nml.parser.Parser._global_start_index.

Referenced by f90nml.parser.Parser.global_start_index(), and f90nml.parser.Parser.read().

◆ global_start_index() [2/2]

def f90nml.parser.Parser.global_start_index (   self,
  value 
)
Set the global start index.

Definition at line 168 of file parser.py.

References f90nml.parser.Parser._global_start_index, and f90nml.parser.Parser.global_start_index().

◆ read()

◆ row_major() [1/2]

def f90nml.parser.Parser.row_major (   self)
Read multidimensional arrays in row-major format.

Multidimensional array data contiguity is preserved by default, so that
column-major Fortran data is represented as row-major Python list of
lists.

The ``row_major`` flag will reorder the data to preserve the index
rules between Fortran to Python, but the data will be converted to
row-major form (with respect to Fortran).

Definition at line 176 of file parser.py.

References f90nml.parser.Parser._row_major.

Referenced by f90nml.parser.Parser.read(), and f90nml.parser.Parser.row_major().

◆ row_major() [2/2]

def f90nml.parser.Parser.row_major (   self,
  value 
)
Validate and set row-major format for multidimensional arrays.

Definition at line 190 of file parser.py.

References f90nml.parser.Parser._row_major, and f90nml.parser.Parser.row_major().

◆ sparse_arrays() [1/2]

def f90nml.parser.Parser.sparse_arrays (   self)
Store unset rows of multidimensional arrays as empty lists.

Enabling this flag will replace rows of unset values with empty lists,
and will also not pad any existing rows when other rows are expanded.

This is not a true sparse representation, but rather is slightly more
sparse than the default dense array representation.

Definition at line 113 of file parser.py.

References f90nml.parser.Parser._sparse_arrays.

Referenced by f90nml.parser.Parser.read(), and f90nml.parser.Parser.sparse_arrays().

◆ sparse_arrays() [2/2]

def f90nml.parser.Parser.sparse_arrays (   self,
  value 
)
Validate and enable spare arrays.

Definition at line 125 of file parser.py.

References f90nml.parser.Parser._sparse_arrays, and f90nml.parser.Parser.sparse_arrays().

◆ strict_logical() [1/2]

def f90nml.parser.Parser.strict_logical (   self)
Use strict rules for parsing logical data value parsing.

The ``strict_logical`` flag will limit the parsing of non-delimited
logical strings as logical values.  The default value is ``True``.

When ``strict_logical`` is enabled, only ``.true.``, ``.t.``, ``true``,
and ``t`` are interpreted as ``True``, and only ``.false.``, ``.f.``,
``false``, and ``f`` are interpreted as false.

When ``strict_logical`` is disabled, any value starting with ``.t`` or
``t`` is interpreted as ``True``, while any string starting with ``.f``
or ``f`` is interpreted as ``False``, as described in the Fortran
specification.  However, it can interfere with namelists which contain
strings which do not use delimiters.

Definition at line 200 of file parser.py.

References f90nml.parser.Parser._strict_logical.

Referenced by f90nml.parser.Parser.read(), and f90nml.parser.Parser.strict_logical().

◆ strict_logical() [2/2]

def f90nml.parser.Parser.strict_logical (   self,
  value 
)
Validate and set the strict logical flag.

Definition at line 219 of file parser.py.

References f90nml.parser.Parser._strict_logical, and f90nml.parser.Parser.strict_logical().


The documentation for this class was generated from the following file: