fagfunksjoner.formats package

fagfunksjoner.formats.formats module

class SsbFormat(start_dict=None)

Bases: dict[Any, Any]

Custom dictionary class designed to handle specific formatting conventions, including mapping intervals (defined as range strings) even when they map to the same value.

Initializes the SsbFormat instance.

Parameters:

start_dict (dict[str | int, Any] | dict[str, Any] | None) – Initial dictionary to populate SsbFormat.

static check_if_na(key)

Checks if the specified key represents a NA (Not Available) value.

Parameters:

key (Any) – Key to be checked for NA value.

Returns:

True if the key represents NA, False otherwise.

Return type:

bool

int_str_confuse(key)

Handles conversion between integer and string keys.

Parameters:

key (str | int | float | NAType | None) – Key to be converted or checked for existence in the dictionary.

Return type:

None | Any

Returns:

The value associated with the key (if found) or None.

look_in_ranges(key)

Returns the mapping value for the key if it falls within any defined range.

The method attempts to convert the key to a float and then checks if it lies within any of the stored range intervals. If the key is None, NA, or not of a convertible type, the method returns None.

Return type:

None | Any

Parameters:

key (str | int | float | NAType | None)

set_na_value()

Sets the value for NA (Not Available) keys in the SsbFormat.

Returns:

True if NA value is successfully set, False otherwise.

Return type:

bool

set_other_as_lowercase()

Ensures that the ‘other’ key is stored in lowercase.

If a key matching ‘other’ in any other case is found, its value is reassigned to ‘other’.

Return type:

None

store(output_path, force=False)

Stores the SsbFormat instance in a specified output path.

Parameters:
  • output_path (str | Path) – Path where the format will be stored.

  • force (bool) – Flag to force storing even for cached instances.

Raises:

ValueError – If storing a cached SsbFormat might lead to an unexpectedly large number of keys.

Return type:

None

store_ranges()

Stores ranges by converting range-string keys into tuple keys.

For example, a key “0-18” with value “A” will be stored as {(0.0, 18.0): “A”}.

Return type:

None

update_format()

Update method to set special instance attributes.

Return type:

None

get_format(filepath)

Retrieves the format from a json-format-file from path.

Parameters:

filepath (str | Path) – Send in the full path to the format directly.

Return type:

SsbFormat | None

Returns:

The formatted dictionary or defaultdict for the specified format and date. If the format contains a “other” key, a defaultdict will be returned. If the

format contains the SAS-value for missing: “.”, or another recognized “empty-datatype”: Many known keys for empty values, will be inserted in the dict, to hopefully map these correctly.

store_format(anyformat, output_path)

Takes a nested or unnested dictionary and saves it to prodsone-folder as a timestamped json.

Parameters:
  • anyformat (SsbFormat | dict[Any, Any]) – Dictionary containing format information. The values of the dictionary are the dict contents of the formats.¨

  • output_path (str | Path) – Path to store the format data. Not including the filename itself, only the base folder.

Return type:

None