Skip to main content

nmlform

nmlform is a formatter and round-trip parser for Fortran namelists.

Documentation

Unlike value-oriented readers, it keeps the source lines as the source of truth and derives a location-tagged tree over them, so comments, spacing, quoting, and layout survive a round trip. That makes it safe to reformat a namelist, or to edit individual values, without disturbing anything you did not touch.

Installation

nmlform has no runtime dependencies, so a plain virtual environment is all you need:

python -m venv .venv
source .venv/bin/activate  # on Windows: .venv\Scripts\activate

Then install this package into the environment:

git clone https://github.com/ken-lauer/nmlform
cd nmlform
python -m pip install .

To install optional extras, use one of the following:

# Optional parsers for YAML/TOML `--values` files (JSON needs nothing extra):
$ python -m pip install .[all]
# The test suite requirements:
$ python -m pip install .[test]
# The documentation requirements:
$ python -m pip install .[doc]
# Everything:
$ python -m pip install .[all,test,doc]

Command-line usage

Installing the package provides an nmlform console script that reformats one or more namelist files, writing the result to standard output. Use - to read from standard input.

Given a messy example.nml:

&plot a=1  bb = 2.0
    long_name= 'value'   ! a comment
/

Running nmlform reformats it:

$ nmlform example.nml
&plot
  a         = 1
  bb        = 2.0
  long_name = 'value'  ! a comment
/

Common options:

nmlform --in-place *.nml         # rewrite files in place
nmlform --check *.nml            # exit non-zero if any file would change
nmlform --diff *.nml             # show a unified diff of what would change
cat example.nml | nmlform -      # read from stdin, write to stdout

Layout is controlled by flags that mirror the API's formatting options (--indent-size, --tabs, --field-case, --align-equals / --no-align-equals, --align-comments / --no-align-comments, --blank-line-after-group / --no-blank-line-after-group). See nmlform --help for the full list.

Setting and removing values

The nmlform-set console script edits a single namelist file, setting and/or removing values and writing the result to standard output (or to a file with -o, or back to the input file with -i). Edits are surgical: the source layout, comments, and untouched values are preserved verbatim.

Given plot.nml:

&plot
  a = 1  ! a comment
  bb = 2.0
/
$ nmlform-set --set plot a 42 --set plot cc "'new'" --remove plot bb plot.nml
&plot
  a = 42  ! a comment
  cc = 'new'
/

--set and --remove are repeatable. Use NAMELIST#N (1-based) to target the N-th of a repeated group. Pass --reformat to also apply the formatting flags above; otherwise only the edited values change. See nmlform-set --help.

Values can also come from a file with --values (repeatable; - reads stdin):

$ nmlform-set --values overrides.json plot.nml
$ nmlform-set --values overrides.yaml --set plot a 99 plot.nml   # --set wins

The format is inferred from the extension (override with --values-format):

  • JSON (built in), YAML, and TOML map groups to {key: value} tables. A string becomes a quoted Fortran string, numbers and booleans become Fortran literals (.true.), a list becomes a value list, and null removes the key. YAML and TOML need the optional parsers: pip install nmlform[all].
  • A namelist file (.nml/.init) is applied group by group, field by field, copying each raw value verbatim — handy for merging one namelist's values into another.

For example, overrides.json:

{ "plot": { "a": 42, "title": "hello", "bb": null } }

produces the same edit as --set plot a 42 --set plot title "'hello'" --remove plot bb.

API usage

Parse a file (or a string) into a NamelistFile, then render it. Rendering without options reproduces the source verbatim; passing NamelistFormatOptions reformats it:

from nmlform import NamelistFile, NamelistFormatOptions

nml = NamelistFile.from_file("example.nml")

# Round-trip: byte-for-byte identical to the source.
assert nml.render() == open("example.nml").read()

# Reformat with explicit options.
print(nml.render(NamelistFormatOptions(indent_size=4, field_case="upper")))

Access and edit groups and their assignments. Edits are surgical — only the touched value is spliced, so surrounding comments and layout are preserved:

from nmlform import NamelistFile

nml = NamelistFile.parse("&plot a = 1  bb = 2.0 /\n")

plot = nml.get_namelist("plot")
print(plot.get("a").value)          # -> the value Token for `a`

plot.set("a", "42")                 # update in place
plot.set("cc", "'new'")             # append a new key before the `/`
plot.remove("bb")                   # drop an assignment

print(nml.render())

You can also update or create groups in bulk:

nml.update_namelist("plot", {"a": "1", "title": "'demo'"})

Values are kept as raw source tokens. Use unquote_value / quote_value to convert between a Fortran string literal and its content:

from nmlform import quote_value, unquote_value

quote_value("it's")                 # -> "'it''s'"

See the documentation for the full API reference.

Metadata

Release files for nmlform 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nmlform 0.0.3
File Size Uploaded
nmlform-0.0.3.tar.gz 62.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nmlform 0.0.3
File Interpreter ABI Platform
nmlform-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 118.5 kB

Release files / nmlform-0.0.3.tar.gz

Download URL nmlform-0.0.3.tar.gz
Size 62.9 kB
Tags Source
SHA-256 checksum
How to use checksums
52ece0a470696dae5b03fb0e763b73a1990c9e50ff9315e6f4f758e8c4cc023c
BLAKE2b-256 checksum
How to use checksums
9c13c8d65b911ca3a82e52ab7e8b763d226b5541c7b1dcd5e3b653e24de62291
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.

Transparency log

Release files / nmlform-0.0.3-py3-none-any.whl

Download URL nmlform-0.0.3-py3-none-any.whl
Size 55.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f9f1f55606f93565a3807676845234e09147579ac8628b6b3c6510723d3500d
BLAKE2b-256 checksum
How to use checksums
4f586621968e92b293fb2e3f2e836ad08fb0db16e727e40fad42fdf523b57e03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page