nvm.aux_srsly package

Submodules

nvm.aux_srsly.aux_srsly module

nvm.aux_srsly.aux_srsly.yamlstr(obj, prefix='got:\\n', indent=5, kwargs=None)[source]

Get indented yaml string from mapping.

This function comes handy for logging (or just printing) more complex mappings (e.g., dictionaries or dict-like objects/structures).

Parameters
  • obj (Mapping) – Mapping (e.g., dictionary or dict-like object) to be parsed.

  • prefix (str) – Prefix string (defaults to "got:\n").

  • indent (int) – Extra (additional) indentation (defaults to 5).

  • kwargs (Mapping) – Extra arguments for srsly.yaml_dumps. For example: indent_mapping, indent_sequence, indent_offset and sort_keys.

Returns

string representation of the parsed mapping object.

Return type

str

Examples

>>> from nvm.aux_srsly import yamlstr
>>> dict0 = dict(a=1, b=2, c=dict(d=4, e=5))
>>> print(yamlstr(dict0))
got:
     a: 1
     b: 2
     c:
       d: 4
       e: 5
nvm.aux_srsly.aux_srsly.json_serializable_or_repr(obj, content=True)[source]

Return dictionary without JSON non-serializable items.

Parameters
  • obj (Mapping) – Mapping (e.g., dictionary or dict-like object) to be parsed.

  • content (bool) – Replace unserializable data with its string representation. If False use type description instead.

Returns

Parsed dictionary.

Return type

Dict

Examples

>>> from nvm.aux_srsly import json_serializable_or_repr as jsonable
>>> import numpy as np
>>> import srsly
>>> import textwrap
>>>
>>> dict0 = dict(
>>>     check="yes",
>>>     items=list([1, 2, 3, "a", "b", "c"]),
>>>     test=np.linspace(42, 44, 10),
>>>     )
>>> print(
>>>     f"METADATA:\n{textwrap.indent(srsly.yaml_dumps(jsonable(dict0)), '   ')}"
>>> )
METADATA:
   check: yes
   items:
     - 1
     - 2
     - 3
     - a
     - b
     - c
   test: "[42. 42.22222222 42.44444444 42.66666667 42.88888889 43.11111111\n\
     \ 43.33333333 43.55555556 43.77777778 44.]"
>>>
>>> content = False
>>> print(
>>>     f"METADATA:\n{textwrap.indent(srsly.yaml_dumps(jsonable(dict0, content=content)), '   ')}"
>>> )
METADATA:
   check: yes
   items:
     - 1
     - 2
     - 3
     - a
     - b
     - c
   test: '<<non-serializable: ndarray>>'

Module contents