Python-Utility-functions
Tons of Python Utility functions required for Dev purposes
Contribute and add your secret script.
📝 NOTES
Get All the files in a directory
Script: Get All Files in a dir
def get_all_files_in_dir(dir_path, file_ext):
"""
Get all files in a directory with a specific extension
:param dir_path: Directory path
:param file_ext: File extension
:return: List of files
"""
return [f for f in listdir(dir_path) if isfile(join(dir_path, f)) and f.endswith(file_ext)]
Flatten all iterables (lists, tuples, dicts, etc), into one lazy generator-stream
Script: Flatten all iterables
def flatten(l):
"""
Flatten all iterables (lists, tuples, dicts, etc), into one lazy generator-stream
"""
if isinstance(l, dict):
l = l.items()
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el