[docs]classDirectory:"""A file structure representation. Organized as Directory nodes that have lists of their Directory children. Directories for a package are created by calling :meth:`PackageImporter.file_structure`."""def__init__(self,name:str,is_dir:bool):self.name=nameself.is_dir=is_dirself.children:Dict[str,Directory]={}def_get_dir(self,dirs:List[str])->"Directory":"""Builds path of Directories if not yet built and returns last directory in list. Args: dirs (List[str]): List of directory names that are treated like a path. Returns: :class:`Directory`: The last Directory specified in the dirs list. """iflen(dirs)==0:returnselfdir_name=dirs[0]ifdir_namenotinself.children:self.children[dir_name]=Directory(dir_name,True)returnself.children[dir_name]._get_dir(dirs[1:])def_add_file(self,file_path:str):"""Adds a file to a Directory. Args: file_path (str): Path of file to add. Last element is added as a file while other paths items are added as directories. """*dirs,file=file_path.split("/")dir=self._get_dir(dirs)dir.children[file]=Directory(file,False)
[docs]defhas_file(self,filename:str)->bool:"""Checks if a file is present in a :class:`Directory`. Args: filename (str): Path of file to search for. Returns: bool: If a :class:`Directory` contains the specified file. """lineage=filename.split("/",maxsplit=1)child=lineage[0]grandchildren=lineage[1]iflen(lineage)>1elseNoneifchildinself.children.keys():ifgrandchildrenisNone:returnTrueelse:returnself.children[child].has_file(grandchildren)returnFalse
def__str__(self):str_list:List[str]=[]self._stringify_tree(str_list)return"".join(str_list)def_stringify_tree(self,str_list:List[str],preamble:str="",dir_ptr:str="─── "):"""Recursive method to generate print-friendly version of a Directory."""space=" "branch="│ "tee="├── "last="└── "# add this directory's representationstr_list.append(f"{preamble}{dir_ptr}{self.name}\n")# add directory's children representationsifdir_ptr==tee:preamble=preamble+branchelse:preamble=preamble+spacefile_keys:List[str]=[]dir_keys:List[str]=[]forkey,valinself.children.items():ifval.is_dir:dir_keys.append(key)else:file_keys.append(key)forindex,keyinenumerate(sorted(dir_keys)):if(index==len(dir_keys)-1)andlen(file_keys)==0:self.children[key]._stringify_tree(str_list,preamble,last)else:self.children[key]._stringify_tree(str_list,preamble,tee)forindex,fileinenumerate(sorted(file_keys)):pointer=lastif(index==len(file_keys)-1)elseteestr_list.append(f"{preamble}{pointer}{file}\n")
def_create_directory_from_file_list(filename:str,file_list:List[str],include:"GlobPattern"="**",exclude:"GlobPattern"=(),)->Directory:"""Return a :class:`Directory` file structure representation created from a list of files. Args: filename (str): The name given to the top-level directory that will be the relative root for all file paths found in the file_list. file_list (List[str]): List of files to add to the top-level directory. include (Union[List[str], str]): An optional pattern that limits what is included from the file_list to files whose name matches the pattern. exclude (Union[List[str], str]): An optional pattern that excludes files whose name match the pattern. Returns: :class:`Directory`: a :class:`Directory` file structure representation created from a list of files. """glob_pattern=GlobGroup(include,exclude=exclude,separator="/")top_dir=Directory(filename,True)forfileinfile_list:ifglob_pattern.matches(file):top_dir._add_file(file)returntop_dir
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.