Skip to main content

Parsing Fortran namelists to Python dictionaries and back.

Project description

pipeline status coverage report

py-namelist

Parse Fortran Namelists to dict-like objects and back

Download

To get the latest release do

git clone https://git.geomar.de:martin-claus/py-namelist.git --branch v0.1.0 --depth=1

or, if you prefer the latest unstable version

git clone https://git.geomar.de:martin-claus/py-namelist.git

Usage

To parse a namelist file you call parse_namelist_file(fobj) where fobj is a file-like object offering a read() and seek() method (usually the standard python file object). Alternatively, you can parse a string using parse_namelist_string(str).

import namelist
with open(your_nml_file) as fobj:
	nmls = namelist.parse_namelist_file(fobj)

nmls will be a list of instances of the Namelist class.

Namelist is a subclass of OrderedDict (or dict if you use Python < 2.7). A Namelist instance, nml, is initialized with an name and optionally with initial values.

nml = Namelist("param", (("key1", val1), ...))

The name attribute will set the read-only property name of nml. To change, add or delete values the same methods are available as for dict.

print nml.name
nml.update({"eggs": 1, "spam": [1, 2, 3]})
del(nml["param"])

To create a Fortran readable string representation of the Namelist instance, just use the str() build-in

s = str(nml)

or just

print(nml)

A string representation of the Namelist instance that can be used by eval() to create copies of it can be created by repr()

print repr(nml)

Note: The parsing of namelist does not have to strictly follow the Fortran standard. Hence, some namelists that are perfectly accepted by some Fortran version are not guaranteed to be correctly parsed by parse_namelist_string(). Always check the content of your Namelist object. If you do find a namelist that does not work, please create an issue at https://git.geomar.de/martin-claus/py-namelist/ together with the namelist that does not work.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

namelist-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

namelist-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file namelist-0.1.0.tar.gz.

File metadata

  • Download URL: namelist-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for namelist-0.1.0.tar.gz
Algorithm Hash digest
SHA256 790a23227a76b491bcd8639ba9afbd912f2a5647082be4bfa43c8ff2c1f9d25b
MD5 f2c3aa9174ea6ab73538e6ddaf19a4e5
BLAKE2b-256 b265cffc162fe03fccd43652dd22bc477f290edd39321866f36de5efb1574f83

See more details on using hashes here.

File details

Details for the file namelist-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: namelist-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for namelist-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fff9bd6d1beaa1ecbf5d5466062cdbce5d2b66fba9c5efad8ddd0544d2f803c
MD5 6fd06ad4daa6626e97aadd06d9cc3f6c
BLAKE2b-256 66ab75336718a50474f56836fe5dd66cae6a6da041fc971856b045b361a79066

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page