DictWriter

class dictIO.dict_writer.DictWriter

Bases: object

Writer for dictionaries in dictIO native file format, as well as JSON, XML and OpenFoam.

__init__() → None

Create a writer for dictionary files.

Return type:

None

Methods

__init__()

Create a writer for dictionary files.

write(source_dict[, target_file, mode, ...])

Serialize a dictionary and write it to a file.

static write(source_dict: MutableMapping[K, V], target_file: str | PathLike[str] | None = None, mode: str = 'a', *, order: bool = False, formatter: Formatter | None = None) → None

Serialize a dictionary and write it to a file.

The file format is selected from the target file extension: no file ending -> dictIO native dict file ‘.cpp’ -> dictIO native dict file ‘.foam’ -> Foam dictionary file ‘.json’ -> Json dictionary file ‘.xml’ -> XML file

In append mode, an existing dictionary is read and merged with source_dict before writing. In overwrite mode, the target is replaced.

Parameters:
  • source_dict (MutableMapping[K, V]) – Dictionary content to serialize.

  • target_file (str | PathLike[str] | None) – Target file name. If None, infer the target from source_dict’s source file; if it has no source file, log an error and do not write a file. Defaults to None.

  • mode (str) – “a” to merge with an existing target before writing, or “w” to overwrite it. An unsupported value logs a warning and writes without merging. Defaults to “a”.

  • order (bool) – Whether to sort dictionary keys before serialization. Defaults to False.

  • formatter (Formatter | None) – Formatter to use. If None, select one based on the target file format. Defaults to None.

Return type:

None