Skip to content

Commit

Permalink
TEST-modin-project#2290: extend get_unique_filename functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Myskov <alexander.myskov@intel.com>
  • Loading branch information
amyskov committed Dec 1, 2020
1 parent e77fcc3 commit 54bd6c6
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions modin/pandas/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,12 +868,43 @@ def generate_none_dfs():
return df, df2


def make_dict_hash(dict_to_hash):
"""Makes a hash from a dictionary considering nested
dictionaries, lists, sets or callables.
Parameters
----------
dict_to_hash: dict
Dictionary that should be hashed.
Returns
-------
Dictionary hash.
"""

def _make_hash(object_to_hash):
if isinstance(object_to_hash, (set, list)):
new_object = hash(frozenset(object_to_hash))
elif not isinstance(object_to_hash, dict):
new_object = hash(object_to_hash)
elif callable(object_to_hash):
new_object = hash(object_to_hash.__name__)
else:
new_object = object_to_hash

return new_object

new_dict = {key: _make_hash(value) for key, value in dict_to_hash.items()}
return hash(frozenset(new_dict))


def get_unique_filename(
test_name: str,
kwargs: dict = {},
extension: str = "csv",
data_dir: str = IO_OPS_DATA_DIR,
suffix: str = "",
debug_mode=False,
):
"""Returns unique file name with specified parameters.
Expand All @@ -889,34 +920,44 @@ def get_unique_filename(
Data directory where test files will be created.
suffix: str
String to append to the resulted name.
debug_mode: bool
Get unique filename containing kwargs values.
Otherwise kwargs values will be replaced with hash equivalent.
Returns
-------
Unique file name.
"""
# shortcut if kwargs parameter os not provided
if len(kwargs) == 0 and extension == "csv" and suffix == "":
return os.path.join(data_dir, (test_name + f"_{suffix}" + f".{extension}"))

assert "." not in extension, "please provide pure extension name without '.'"
prohibited_chars = ['"', "\n"]
non_prohibited_char = "np_char"
char_counter = 0
kwargs_name = dict(kwargs)
for key, value in kwargs_name.items():
for char in prohibited_chars:
if isinstance(value, str) and char in value or callable(value):
kwargs_name[key] = non_prohibited_char + str(char_counter)
char_counter += 1
parameters_values = "_".join(
[
str(value)
if not isinstance(value, (list, tuple))
else "_".join([str(x) for x in value])
for value in kwargs_name.values()
]
)
return os.path.join(data_dir, parameters_values + f"_{suffix}" + f".{extension}")
suffix_part = f"_{suffix}" if suffix else ""
if debug_mode:
# shortcut if kwargs parameter os not provided
if len(kwargs) == 0 and extension == "csv" and suffix == "":
return os.path.join(data_dir, (test_name + suffix_part + f".{extension}"))

assert "." not in extension, "please provide pure extension name without '.'"
prohibited_chars = ['"', "\n"]
non_prohibited_char = "np_char"
char_counter = 0
kwargs_name = dict(kwargs)
for key, value in kwargs_name.items():
for char in prohibited_chars:
if isinstance(value, str) and char in value or callable(value):
kwargs_name[key] = non_prohibited_char + str(char_counter)
char_counter += 1
parameters_values = "_".join(
[
str(value)
if not isinstance(value, (list, tuple))
else "_".join([str(x) for x in value])
for value in kwargs_name.values()
]
)
return os.path.join(data_dir, parameters_values + suffix_part + f".{extension}")
else:
unique_id = make_dict_hash(kwargs)
return os.path.join(
data_dir, (test_name + f"_{str(unique_id)}" + suffix_part + f".{extension}")
)


def get_random_string():
Expand Down

0 comments on commit 54bd6c6

Please sign in to comment.