Skip to main content

Python package to read, write and manipulate dictionary text files. Supports dictIOs native file format, as well as JSON, XML and OpenFOAM.

Project description

pypi versions license ci docs

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.

https://github.com/dnv-opensource/dictIO

Contributing

  1. Fork it (https://github.com/dnv-opensource/dictIO/fork)
  2. Create an issue in your GitHub repo
  3. Create your branch based on the issue number and type (git checkout -b issue-name)
  4. Evaluate and stage the changes you want to commit (git add -i)
  5. Commit your changes (git commit -am 'place a descriptive commit message here')
  6. Push to the branch (git push origin issue-name)
  7. Create a new Pull Request in GitHub

For your contribution, please make sure you follow the STYLEGUIDE before creating the Pull Request.

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

dictio-0.4.0.tar.gz (95.7 kB view details)

Uploaded Source

Built Distribution

dictio-0.4.0-py3-none-any.whl (59.4 kB view details)

Uploaded Python 3

File details

Details for the file dictio-0.4.0.tar.gz.

File metadata

  • Download URL: dictio-0.4.0.tar.gz
  • Upload date:
  • Size: 95.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for dictio-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c637ed257923c8f3991b8fccb7c103d9240512ccb60322b5f43a76203f450487
MD5 ece363e48cc4c3813e28f3fb597bc485
BLAKE2b-256 7cd6afa4be318fdd3ea287dd4300145d505646732ea712717ed3a342d3af06cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for dictio-0.4.0.tar.gz:

Publisher: publish_release.yml on dnv-opensource/dictIO

Attestations:

File details

Details for the file dictio-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: dictio-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 59.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for dictio-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 460a0b03f19a0eb68d4455407e416c457a5068f1b06f8c522c44a4fe7a207145
MD5 5cc0a4195d2f6314cc4adf7e43e5a424
BLAKE2b-256 e4599921cc5fd537572bbd88ac6ab2a0b6bac0ea8f3fd200934f7a310dcecf0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dictio-0.4.0-py3-none-any.whl:

Publisher: publish_release.yml on dnv-opensource/dictIO

Attestations:

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