Zum Inhalt

utils

dsstools.utils

Module containing various helper functions.

NumpyEncoder

Bases: JSONEncoder

Json encoder for numpy arrays.

PositionKeyCoder()

Provides methods to consistently en- & decode position data for nx.Graphs

decode_typed_keys(dct)

object_hook for json.load() that recognises the prefixes set by the encoder.

Parameters:

Name Type Description Default
dct dict

A dictionary from a json file.

required

Returns:

Type Description
dict

A decoded version of the dictionary respectively node position data

encode_typed_keys(obj)

Recursively unpacks json-formats that we use for saving positions.

Use this to prepare a positions file for json.dumps. This ensures that integers can be set as keys respectively nodes.

Parameters:

Name Type Description Default
obj any

The json-content that needs to be encoded.

required

Returns:

Type Description
dict | list

A valid format for the json.dump()-function.

ensure_file_format(path, *, default_format, format_filter=None)

Ensures that the provided path has a saving format and its parents exist.

If the saving format is not in the defined format_filter, a TypeError will be raised.

Parameters:

Name Type Description Default
path str | Path

the path that needs to be validated

required
default_format str

the format a programmer can set that will be used as default, if no format was provided. Leading periods can be included.

required
format_filter set | None

this specifies a filter of accepted formats. Leading periods can be included. Adding the default parameter isn't mandatory, since it is added dynamically but should be best practice.

None

Returns:

Type Description
tuple[Path, str]

the filepath and format (without leading dot) as an 2-Tuple