Common utilities.
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: json.encoder.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.
copier
(src: pathlib.Path, dest: pathlib.Path) → None[source]¶Copy file, ensuring it can be overwritten.
parselglossy.utils.
default_outfile
(*, fname: Union[str, pathlib.Path], suffix: str) → str[source]¶Default name for output file.
fname (Union[str, Path]) – Name to use as stencil.
suffix (str) – Suffix to append.
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.
d (JSONDict) – Nested dictionary
list – Flattened list
List[Any]
parselglossy.utils.
flatten_list
(nested_list: List[Any]) → List[Any][source]¶Flattens a nested list into a flat list.
nested_list (List[Any]) – Nested list
Flattened list
List[Any]
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']
.
address (Tuple[str]) –
dict_name (str) –
where
str
parselglossy.utils.
nested_get
(d: Dict[str, Any], *ks: str) → Optional[Any][source]¶Get value from a nested dictionary.
d (JSONDict) –
ks (str) –
v
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.
d (JSONDict) –
ks (Tuple[str]) –
v (Any) –
Notes
Adapted from: https://stackoverflow.com/a/13688108/2528668
parselglossy.utils.
path_resolver
(f: Union[str, pathlib.Path], *, touch: bool = True) → pathlib.Path[source]¶Resolve a path.
f (Union[str, Path]) – File whose path needs to be resolved.
touch (bool) – Create file is not already existent. Default: True
path – File as a Path
object.
Path
Notes
The file will be created if not already existent.