Skip to content

card_file

Classes

CardFileDecoder

Bases: Transformer

Decoder performs translation from *.card file to a dictionary.

The translation rules are:

*.card Python Comment
section dict[str, dict] Top-level keys are sections
key-value dict Each item is a sigle kv pair
list list Empty or with elements
nested list list[list] List of lists
string str Unquoted (with chars: -./${}*) and double quoted
integer number int ---
real number float Including scientific notation
true/y True Case insensitive
false/n False Case insensitive

Functions

decode
decode(text: str) -> dict

Decode *.card text into dictionary.

Parameters:

  • text
    (str) –

    content of the *.card file

Returns:

  • dict ( dict ) –

    Decoded *.card file

Source code in ipsl_common/modipsl/card_file.py
291
292
293
294
295
296
297
298
299
300
301
def decode(self, text: str) -> dict:
    """Decode `*.card` text into dictionary.

    Args:
        text: content of the `*.card` file

    Returns:
        dict: Decoded `*.card` file
    """
    parse_tree = self._parser.parse(text)
    return self.transform(parse_tree)

CardFileEncoder

Functions

flatten_dict_with_sections

flatten_dict_with_sections(position_map: dict) -> dict
Source code in ipsl_common/modipsl/card_file.py
221
222
223
224
225
226
def flatten_dict_with_sections(position_map: dict) -> dict:
    new_position_map = {}
    for section, values in position_map.items():
        for key, position in values.items():
            new_position_map[f"{section}.{key}"] = position
    return new_position_map

load

load(buffer: TextIOBase)

Load *.card text/file buffer into dictionary.

Parameters:

  • buffer

    (TextIOBase) –

    text or file buffer with the *.card file

Returns:

  • dict

    Loaded *.card file

Source code in ipsl_common/modipsl/card_file.py
373
374
375
376
377
378
379
380
381
382
383
384
def load(buffer: TextIOBase):
    """Load `*.card` text/file buffer into dictionary.

    Args:
        buffer: text or file buffer with the `*.card` file

    Returns:
        dict: Loaded `*.card` file
    """
    if not buffer.readable():
        raise ValueError("Text buffer (TextIOBase) must be readable")
    return loads(buffer.read())

loads

loads(text: str)

Load *.card file string into dictionary.

Parameters:

  • text

    (str) –

    content of the *.card file

Returns:

  • dict

    Loaded *.card file

Source code in ipsl_common/modipsl/card_file.py
387
388
389
390
391
392
393
394
395
396
def loads(text: str):
    """Load `*.card` file string into dictionary.

    Args:
        text: content of the `*.card` file

    Returns:
        dict: Loaded `*.card` file
    """
    return CardFileDecoder().decode(text)