Skip to content

Data Utilities

set_seed(seed)

Set seed function imported from transformers.

Source code in src/deeponto/utils/data_utils.py
22
23
24
def set_seed(seed):
    """Set seed function imported from transformers."""
    t_set_seed(seed)

sort_dict_by_values(dic, desc=True, k=None)

Return a sorted dict by values with first k reserved if provided.

Source code in src/deeponto/utils/data_utils.py
27
28
29
30
def sort_dict_by_values(dic: dict, desc: bool = True, k: Optional[int] = None):
    """Return a sorted dict by values with first k reserved if provided."""
    sorted_items = list(sorted(dic.items(), key=lambda item: item[1], reverse=desc))
    return dict(sorted_items[:k])

uniqify(ls)

Return a list of unique elements without messing around the order

Source code in src/deeponto/utils/data_utils.py
33
34
35
36
def uniqify(ls):
    """Return a list of unique elements without messing around the order"""
    non_empty_ls = list(filter(lambda x: x != "", ls))
    return list(dict.fromkeys(non_empty_ls))

print_dict(dic)

Pretty print a dictionary.

Source code in src/deeponto/utils/data_utils.py
39
40
41
42
43
def print_dict(dic: dict):
    """Pretty print a dictionary."""
    pretty_print = json.dumps(dic, indent=4, separators=(",", ": "))
    # print(pretty_print)
    return pretty_print

Last update: February 1, 2023
Created: January 22, 2023
GitHub: @Lawhy   Personal Page: yuanhe.wiki