Skip to main content

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

Project description

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 provides a simple, high level API that allows reading and writing Python dicts from/to dict files:

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 above task can also be invoked 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 default dictionary 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 dict file format used by dictIO, see File Format in dictIO's documentation on GitHub Pages.

Development Setup

  1. Install Python 3.9 or higher, i.e. Python 3.10 or Python 3.11

  2. Update pip and setuptools:

    python -m pip install --upgrade pip setuptools
    
  3. git clone the dictIO repository into your local development directory:

    git clone https://github.com/dnv-opensource/dictIO path/to/your/dev/dictIO
    
  4. In the dictIO root folder:

    Create a Python virtual environment:

    python -m venv .venv
    

    Activate the virtual environment:

    ..on Windows:

    > .venv\Scripts\activate.bat
    

    ..on Linux:

    source .venv/bin/activate
    

    Update pip and setuptools:

    (.venv) $ python -m pip install --upgrade pip setuptools
    

    Install dictIO's dependencies:

    (.venv) $ pip install -r requirements-dev.txt
    

    This should return without errors.

    Finally, install dictIO itself, yet not as a regular package but as an editable package instead, using the pip install option -e:

    (.venv) $ pip install -e .
    
  5. Test that the installation works (in the dictIO root folder):

    (.venv) $ pytest .
    

Meta

Copyright (c) 2024 DNV open source

Frank Lumpitzsch – @LinkedInfrank.lumpitzsch@dnv.com

Claas Rostock – @LinkedInclaas.rostock@dnv.com

Seunghyeon Yoo – @LinkedInseunghyeon.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 your branch (git checkout -b myBranchName)
  3. Commit your changes (e.g. git commit -m 'place a descriptive commit message here')
  4. Push to the branch (e.g. git push origin myBranchName)
  5. 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.3.3.tar.gz (68.5 kB view hashes)

Uploaded Source

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