Source code for ChildProject.utils

import os

[docs]def path_is_parent(parent_path: str, child_path: str): # Smooth out relative path names, note: if you are concerned about symbolic links, you should use os.path.realpath too parent_path = os.path.abspath(parent_path) child_path = os.path.abspath(child_path) # Compare the common path of the parent and child path with the common path of just the parent path. Using the commonpath method on just the parent path will regularise the path name in the same way as the comparison that deals with both paths, removing any trailing path separator return os.path.commonpath([parent_path]) == os.path.commonpath( [parent_path, child_path] )
[docs]class Segment: def __init__(self, start, stop): self.start = start self.stop = stop
[docs] def length(self): return self.stop - self.start
def __repr__(self): return "Segment([{}, {}])".format(self.start, self.stop)
[docs]def intersect_ranges(xs, ys): # Try to get the first range in each iterator: try: x, y = next(xs), next(ys) except StopIteration: return while True: # Yield the intersection of the two ranges, if it's not empty: intersection = Segment(max(x.start, y.start), min(x.stop, y.stop)) if intersection.length() > 0: yield intersection # Try to increment the range with the earlier stopping value: try: if x.stop <= y.stop: x = next(xs) else: y = next(ys) except StopIteration: return
[docs]def get_audio_duration(filename): import sox if not os.path.exists(filename): return 0 duration = 0 try: duration = sox.file_info.duration(filename) except: pass return duration