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
range tuple[int] Integer range in the form: START:STEP:END
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

Methods:

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
298
299
300
301
302
303
304
305
306
307
308
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
226
227
228
229
230
231
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
383
384
385
386
387
388
389
390
391
392
393
394
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
397
398
399
400
401
402
403
404
405
406
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)