dictIO¶
dictIO is a Python package to read, write and manipulate dictionary text files.
It was designed to leverage the versatility of text based dictionary files, or ‘dict files’ in short, while easing their use in Python through seamless support for Python dicts.
dictIO supports
reading and writing Python dicts in dict files.
usage of references and expressions in dict files, dynamically resolved during reading.
usage of cascaded dict files, allowing separation of a case-agnostic configuration dict and its case-specific parameterization: baseDict + paramDict = caseDict
Further, dictIO
is widely tolerant in reading different flavours (quotes, preserving comments, etc.)
can read and write also JSON, XML and OpenFOAM (with some limitations)
Installation¶
pip install dictIO
Usage Example¶
dictIO’s core class is SDict
, a generic data structure for serializable dictionaries.
SDict
inherits from Python’s builtin dict
. It can hence be used transparently in any context where a dict
or any other MutableMapping
type is expected.
You can use SDict
the same way you use dict
. E.g. you can pass a dict literal to its constructor:
from dictIO import SDict
my_dict: SDict[str, int] = SDict(
{
"foo": 1,
"bar": 2,
}
)
The simplest way to to dump and load a dict to / from a file, is to use SDict’s dump()
and load()
instance methods:
To dump my_dict
to a file, use .dump()
:
my_dict.dump("myDict")
To load the formerly dumped file into a new dict, use .load()
:
my_dict_loaded: SDict[str, int] = SDict().load("myDict")
In cases where you need more control over how dict files are read and written,
dictIO’s DictReader
and DictWriter
classes offer this flexibility, while still maintaining a simple and high level API:
from dictIO import DictReader, DictWriter
my_dict = DictReader.read('myDict')
DictWriter.write(my_dict, 'parsed.myDict')
The above example reads a dict file, merges any (sub-)dicts included through #include directives, evaluates expressions contained in the dict, and finally saves the read and evaluated dict with prefix ‘parsed’ as ‘parsed.myDict’.
This sequence of reading, evaluating and writing a dict is also called ‘parsing’ in dictIO.
Because this task is so common, dictIO provides a convenience class for it:
Using DictParser.parse()
the above task can be accomplished in one line of code:
from dictIO import DictParser
DictParser.parse('myDict')
The parse
operation can also be executed from the command line, using the ‘dictParser’ command line script installed with dictIO:
dictParser myDict
For more examples and usage, please refer to dictIO’s documentation.
File Format¶
The native file format used by dictIO shares, by intention, some commonalities with the OpenFOAM file format, but is kept simpler and more tolerant to different flavours of string formatting.
With some limitations, dictIO supports also reading from and writing to OpenFOAM, Json and XML.
For a detailed documentation of the native file format used by dictIO, see File Format in dictIO’s documentation on GitHub Pages.
Development Setup¶
1. Install uv¶
This project uses uv
as package manager.
If you haven’t already, install uv, preferably using it’s “Standalone installer” method:
..on Windows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
..on MacOS and Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh
(see docs.astral.sh/uv for all / alternative installation methods.)
Once installed, you can update uv
to its latest version, anytime, by running:
uv self update
2. Install Python¶
This project requires Python 3.10 or later.
If you don’t already have a compatible version installed on your machine, the probably most comfortable way to install Python is through uv
:
uv python install
This will install the latest stable version of Python into the uv Python directory, i.e. as a uv-managed version of Python.
Alternatively, and if you want a standalone version of Python on your machine, you can install Python either via winget
:
winget install --id Python.Python
or you can download and install Python from the python.org website.
3. Clone the repository¶
Clone the dictIO repository into your local development directory:
git clone https://github.com/dnv-opensource/dictIO path/to/your/dev/dictIO
4. Install dependencies¶
Run uv sync
to create a virtual environment and install all project dependencies into it:
uv sync
5. (Optional) Activate the virtual environment¶
When using uv
, there is in almost all cases no longer a need to manually activate the virtual environment.
uv
will find the .venv
virtual environment in the working directory or any parent directory, and activate it on the fly whenever you run a command via uv
inside your project folder structure:
uv run <command>
However, you still can manually activate the virtual environment if needed.
When developing in an IDE, for instance, this can in some cases be necessary depending on your IDE settings.
To manually activate the virtual environment, run one of the “known” legacy commands:
..on Windows:
.venv\Scripts\activate.bat
..on Linux:
source .venv/bin/activate
6. Install pre-commit hooks¶
The .pre-commit-config.yaml
file in the project root directory contains a configuration for pre-commit hooks.
To install the pre-commit hooks defined therein in your local git repository, run:
uv run pre-commit install
All pre-commit hooks configured in .pre-commit-config.yaml
will now run each time you commit changes.
7. Test that the installation works¶
To test that the installation works, run pytest in the project root folder:
uv run pytest
Meta¶
Copyright (c) 2024 DNV SE. All rights reserved.
Frank Lumpitzsch - @LinkedIn - frank.lumpitzsch@dnv.com
Claas Rostock - @LinkedIn - claas.rostock@dnv.com
Seunghyeon Yoo - @LinkedIn - seunghyeon.yoo@dnv.com
Distributed under the MIT license. See LICENSE for more information.
Contributing¶
Create an issue in your GitHub repo
Create your branch based on the issue number and type (
git checkout -b issue-name
)Evaluate and stage the changes you want to commit (
git add -i
)Commit your changes (
git commit -am 'place a descriptive commit message here'
)Push to the branch (
git push origin issue-name
)Create a new Pull Request in GitHub
For your contribution, please make sure you follow the STYLEGUIDE before creating the Pull Request.