Transformation module

TIPCommon.transformation.add_prefix_to_dict

TIPCommon.transformation.add_prefix_to_dict(given_dict, prefix)

Adds a prefix to the keys of a given dictionary.

Parameters
given_dict dict

The dictionary to add the prefix.

prefix str

The prefix to add.

Returns

The dictionary with the prefix added to the keys.

Return type

dict

TIPCommon.transformation.add_prefix_to_dict_keys

TIPCommon.transformation.add_prefix_to_dict_keys(target_dict, prefix)

Adds a prefix to the keys of a given dictionary.

Parameters
target_dict dict

The dictionary to add the prefix.

prefix str

The prefix to add.

Returns

The dictionary with the prefix added to the keys.

Return type

dict

TIPCommon.transformation.adjust_to_csv

TIPCommon.transformation.adjust_to_csv(value)

Adjusts a value to be suitable for inclusion in a CSV.

Parameters
value Any

The value to adjust.

Returns

The adjusted value.

Return type

str

TIPCommon.transformation.construct_csv

TIPCommon.transformation.construct_csv(list_of_dicts)

Constructs a CSV from a list of dictionaries.

Parameters
list_of_dicts list[dict]

The list of dictionaries to add to the CSV.

Returns

The CSV formatted list.

Return type

list[str]

TIPCommon.transformation.convert_comma_separated_to_list

TIPCommon.transformation.convert_comma_separated_to_list(comma_separated, delimiter=',')

Converts a comma-separated string to a list of values.

Parameters
delimiter The delimiter to parse the string with.
Default is ','.
comma_separated str

The comma-separated string to convert.

Returns

The list of values.

Return type

list

TIPCommon.transformation.convert_dict_to_json_result_dict

TIPCommon.transformation.convert_dict_to_json_result_dict(json_result, title_key='Entity', results_key='EntityResult')

Converts the key, value JSON result into the JSON result object list and organizes the entity JSON result object to a set format.

The example of how a dict transforms to the JSON result is as follows:

{k1: v1, k2:v2, ...} =>
[
   {
       title_key: k1,
       result_key: v1
   },
   {
       title_key: k2,
       result_key: v1
   }
   ...
]
Parameters
json_result dict[str, Any] | str

The key, value JSON result.

title_key str

The key under which the name of each key will be.

result_key str

The key under which the name of each value will be.

Examples

The default example for the entity result JSON format where title_key='Entity', results_key='EntityResult' is as follows:

[
   {
       'Entity': 'key1 in json_result',
       'EntityResult: {
           json_result['key1']
       }
   },
   {
       'Entity': 'key2 in json_result',
       'EntityResult: {
           json_result['key2']
       }
   }
]

Returns

(list[dict[str, Any]]) List of entity JSON result objects

Raises

  • InternalJSONDecoderError – if json_result is a string and couldn't be parsed to dictionary using json.loads().
  • ValueError – if the json_result isn't a dict (the check is done after loading it from a string, if needed).

TIPCommon.transformation.convert_list_to_comma_string

TIPCommon.transformation.convert_list_to_comma_string(values_list, delimiter=',')

Converts a list of values to a comma-separated string.

Parameters
delimiter The delimiter to be used in the string.
Default is ','.
values_list list

The list of values to convert.

Returns

The comma-separated string.

Return type

str

TIPCommon.transformation.dict_to_flat

TIPCommon.transformation.dict_to_flat(target_dict)

Receives a nested dictionary and returns it as a flat dictionary.

Parameters
target_dict dict

The dictionary to flatten.

Returns

The flattened dictionary.

Return type

dict

TIPCommon.transformation.flat_dict_to_csv

TIPCommon.transformation.flat_dict_to_csv(flat_dict, property_header='Property', value_header='Value')

Turns a flat dictionary into a list of strings in CSV format.

The property_header and value_header arguments are used to customize the CSV header.

Parameters
flat_dict dict

The dictionary to convert to CSV format.

property_header str

The header for the property column.
Default is Property.

value_header str

The header for the value column.
Default is Value.

Returns

The list of strings in CSV format.

Return type

list

TIPCommon.transformation.get_unicode

TIPCommon.transformation.get_unicode(value)

Gets the unicode of a value.

Parameters
value Any

The value to convert to unicode.

Returns

The unicode representation of value.

Return type

unicode (unicode)

TIPCommon.transformation.removeprefix

TIPCommon.transformation.removeprefix(string: str, prefix: str) → str

Self-implementation for str.removeprefix() that exists in Python 3.9 and later.

If the string starts with the prefix string, the method returns string[len(prefix):]. Otherwise, returns a copy of the original string.

Parameters
string str

The string to remove the prefix from.

prefix str

The prefix to remove from the string.

Returns

The resulting string.

TIPCommon.transformation.removesuffix

TIPCommon.transformation.removesuffix(string: str, suffix: str) → str

Self-implementation for str.removesuffix() that exists in Python 3.9 and later.

If the string ends with the suffix string, the method returns string[:-len(prefix)]. Otherwise, returns a copy of the original string.

Parameters
string str

The string to remove the suffix from.

suffix str

The suffix to remove from the string.

Returns

The resulting string.

TIPCommon.transformation.rename_dict_key

TIPCommon.transformation.rename_dict_key(a_dict: dict, current_key: Hashable,new_key: Hashable) → None

Renames a key in a dictionary.

Parameters
a_dict dict

The dict to rename a key in.

current_key Hashable

The key in a_dict to rename.

new_key Hashable

The renamed key.

TIPCommon.transformation.string_to_multi_value

TIPCommon.transformation.string_to_multi_value(string_value, delimiter=',', only_unique=False)

Converts a string containing a comma-separated list of values to a list of values.

Parameters
string_value str

The string to convert.

delimiter Optional

str

The delimiter to split the string on.

Default is ','.

only_unique Optional

bool

If set to True, the parameter only includes unique values in the returned list.

Default is False.

Returns

The list of values.

Return type

list