Skip to content

Commit

Permalink
[Minor] print_directory_tree returns a string
Browse files Browse the repository at this point in the history
ghstack-source-id: d57f19dd8efcef06676fca40a4d6f95367ff1d55
Pull Request resolved: #1086

(cherry picked from commit 2b19ef1)
  • Loading branch information
vmoens committed Nov 14, 2024
1 parent d64c33d commit 05c0fe7
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions tensordict/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ def _is_json_serializable(item):
return isinstance(item, (str, int, float, bool)) or item is None


def print_directory_tree(path, indent="", display_metadata=True):
def print_directory_tree(path, indent="", display_metadata=True) -> str:
"""Prints the directory tree starting from the specified path.
Args:
Expand All @@ -2147,7 +2147,11 @@ def print_directory_tree(path, indent="", display_metadata=True):
display_metadata (bool): if ``True``, metadata of the dir will be
displayed too.
Returns:
the string printed with the logger.
"""
string = []
if display_metadata:

def get_directory_size(path="."):
Expand All @@ -2169,17 +2173,23 @@ def format_size(size):

total_size_bytes = get_directory_size(path)
formatted_size = format_size(total_size_bytes)
logger.info(f"Directory size: {formatted_size}")
string.append(f"Directory size: {formatted_size}")
logger.info(string[-1])

if os.path.isdir(path):
logger.info(indent + os.path.basename(path) + "/")
string.append(indent + os.path.basename(path) + "/")
logger.info(string[-1])
indent += " "
for item in os.listdir(path):
print_directory_tree(
os.path.join(path, item), indent=indent, display_metadata=False
string.append(
print_directory_tree(
os.path.join(path, item), indent=indent, display_metadata=False
)
)
else:
logger.info(indent + os.path.basename(path))
string.append(indent + os.path.basename(path))
logger.info(string[-1])
return "\n".join(string)


def isin(
Expand Down

0 comments on commit 05c0fe7

Please sign in to comment.