parselglossy.utils module#

Common utilities.

class parselglossy.utils.ComplexEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: JSONEncoder

JSON encoder for complex numbers.

default(obj)[source]#

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
parselglossy.utils.as_complex(dct)[source]#

JSON decoder for complex numbers.

parselglossy.utils.copier(src: Path, dest: Path) None[source]#

Copy file, ensuring it can be overwritten.

parselglossy.utils.default_outfile(*, fname: Union[str, Path], suffix: str) str[source]#

Default name for output file.

Parameters
  • fname (Union[str, Path]) – Name to use as stencil.

  • suffix (str) – Suffix to append.

Return type

The name of the output file.

parselglossy.utils.dict_to_list(d: Dict[str, Any]) List[Any][source]#

Converts a nested dictionary to a nested list.

Parameters

d (JSONDict) – Nested dictionary

Returns

list – Flattened list

Return type

List[Any]

parselglossy.utils.flatten_list(nested_list: List[Any]) List[Any][source]#

Flattens a nested list into a flat list.

Parameters

nested_list (List[Any]) – Nested list

Returns

Flattened list

Return type

List[Any]

parselglossy.utils.folder_permissions(folder: Path) None[source]#

Recursively set permissions with chmod uga+rw

parselglossy.utils.location_in_dict(*, address: Tuple, dict_name: str = 'user') str[source]#

Convert tuple of keys of a JSONDict to its representation in code.

For example, given ("a", "b", "c") returns the string user['a']['b']['c'].

Parameters
  • address (Tuple[str]) –

  • dict_name (str) –

Returns

where

Return type

str

parselglossy.utils.nested_get(d: Dict[str, Any], *ks: str) Optional[Any][source]#

Get value from a nested dictionary.

Parameters
  • d (JSONDict) –

  • ks (str) –

Returns

v

Return type

Optional[Any]

Notes

Adapted from: https://stackoverflow.com/a/40675868/2528668

parselglossy.utils.nested_set(d: Dict[str, Any], ks: Tuple[Any, ...], v: Any) None[source]#

Set value in nested dictionary.

Parameters
  • d (JSONDict) –

  • ks (Tuple[str]) –

  • v (Any) –

Notes

Adapted from: https://stackoverflow.com/a/13688108/2528668

parselglossy.utils.path_resolver(f: Union[str, Path], *, touch: bool = True) Path[source]#

Resolve a path.

Parameters
  • f (Union[str, Path]) – File whose path needs to be resolved.

  • touch (bool) – Create file is not already existent. Default: True

Returns

path – File as a Path object.

Return type

Path

Notes

The file will be created if not already existent.