Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# Fortran namelist files in Python
# A Python module to parse Fortran namelist files

Read in a namelist file:
```
from namelist_python import read_namelist_file
namelist = read_namelist_file('SIM_CONFIG.nl')
import namelist_python
namelist = namelist_python.read_namelist_file('config.dat')
namelist.groups['foo']['bar']
```

`namelist` is an instance of `namelist_python.Namelist` and all groups are
stored in the attribute `groups` with each variable in a nested dictionary
structure (using `OrderedDict` so that the order will be remembered).
This creates an instance of `namelist_python.Namelist` whose attribute
`groups` holds the data in a nested ordered case-insensitive dictionary
structure.

Write a `Namelist` object back to a file:
```
with open('NEW_FILE.nl', 'w') as f:
with open('new_file.dat', 'w') as f:
f.write(namelist.dump())
```

`dump` takes an optional argument `array_inline` a boolean which sets whether
arrays should be inline or given in index notation.

If you use ipython there is usefull attribute called `data` which allows you to
do tab completion on the group and variable names, and do assignment:
If you use ipython or a another interactive REPL prompt you may want to use
the `data` attribute which allows you to do tab completion on the group and
variable names:

```
In [7]: namelist.data.ATHAM_SETUP.dt
Expand All @@ -38,17 +40,17 @@ Out[9]: 4.0

## Features
- Parses ints, floats, booleans, escaped strings and complex numbers.
- Parses arrays in index notation and inlined.
- Can output in namelist format.
- Tab-completion and variable assignment in interactive console

## Missing features
- Currently can't handle variable definitions across multiple lines
- Currently can't handle line continuations
- Currently can't handle lines with several parameters
- Comments are not kept, and so won't exist in output.
- Module does not help to create a Namelist object from scratch

## Contribute
Please send any namelist files that don't parse correctly or fix the code
yourself and send me a pull request :)
yourself and send a pull request :)

Thanks,
Leif
Thanks
4 changes: 3 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from namelist import read_namelist_file, Namelist, AttributeMapper

__all__ = ['namelist']
from .namelist import *
Loading