snowmobile.core.utils.parsing
¶
Contains parsing utilities used by:
Module Contents¶
Functions¶
| Recursively merges dictionaries |
| Utility to truncate upper-casing strings as opposed to str.upper(). |
| Utility to strip a variety whitespace from a string. |
| Recursively flattens dictionary to its atomic elements. |
| Utility to parse cfg from dot-prefixed object if included. |
-
snowmobile.core.utils.parsing.
rmerge_dicts
(d1: Dict, d2: Dict) → Dict¶ Recursively merges dictionaries
d1
&d2
.Used for combining contents of snowmobile.toml and snowmobile-ext.toml at the appropriate level within a pair of nested dictionaries.
Note
The order of
d1
andd2
matters; if an overlapping key at the same level exists in both dictionaries, the value withind1
will take precedent over the value ind2
.This function is invoked within the __init__() method of
Configuration
to mergesnowmobile.toml
andsnowmobile-ext.toml
, and the prioritization of overlapping values described above is the reason that specifications insnowmobile.toml
will take precedent over any overlapping configurations insnowmobile.toml
.- Returns (dict):
Dictionary containing recursively merged contents from
d1
andd2
-
snowmobile.core.utils.parsing.
up
(nm: str)¶ Utility to truncate upper-casing strings as opposed to str.upper().
-
snowmobile.core.utils.parsing.
strip
(val: str, trailing: bool = True, blanks: bool = True, whitespace: bool = False) → str¶ Utility to strip a variety whitespace from a string.
-
snowmobile.core.utils.parsing.
dict_flatten
(attrs: dict, delim: Optional[str] = None, indent_char: Optional[str] = None, bullet_char: Optional[str] = None) → List[Tuple[str, str, str]]¶ Recursively flattens dictionary to its atomic elements.
Flattens a dictionary to its atomic state and performs parsing operations, separating each child key with a delim relative to its parent key.
This flattening enables the parsing of a nested dictionary into a valid string of markdown containing correspondingly nested bullets.
- Parameters
attrs (dict) – Dictionary of attributes; most likely the
attrs_parsed
fromStatement
.delim (str) – Delimiter to use for separating nested keys; defaults to ‘~’;
indent_char (str) – Character to use for indents; defaults to a tab (‘ ).
bullet_char (str) – Character to use for bullets/sub-bullets; defaults to ‘-‘.
- Returns (List[Tuple[str, str, str]]):
- A list of tuples containing:
A string of the indentation to use; for 1st-level attributes, this will just be the bullet_char.
The fully stratified key, including parents; for 1st-level attributes this will mirror the original key that was pr.
The value of the associated key; this will always mirror the value that was pr.