Created
April 3, 2022 18:17
-
-
Save TheSithPadawan/425a9ffff08d8a4d26a259780eaed403 to your computer and use it in GitHub Desktop.
Amazon OO Design Linux Find Command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
implemnet linux find command as an api ,the api willl support finding files that has given size requirements and a file with a certain format like | |
find all file >5mb | |
find all xml | |
Assume file class | |
{ | |
get name() | |
directorylistfile() | |
getFile() | |
create a library flexible that is flexible | |
Design clases,interfaces. | |
""" | |
from abc import ABC, abstractmethod | |
# FileObject and FileSystems are copied directly from LC 588 | |
class FileObject: | |
def __init__(self, path=None): | |
self.path = path | |
# handles subdirectories | |
self.children = dict() | |
self.is_file = False | |
self.contents = [] | |
self.size = 0 | |
class FileSystem: | |
def __init__(self): | |
self.rootdir = FileObject() | |
@property | |
def root_dir(self): | |
return self.rootdir | |
# create directory or file | |
def create_file(self, path: str) -> None: | |
paths = path.split('/') | |
itr = self.rootdir | |
for p in paths: | |
if not p: | |
continue | |
if p not in itr.children: | |
itr.children[p] = FileObject(p) | |
itr = itr.children[p] | |
itr.is_file = True | |
itr.type = itr.path.split('.')[-1] | |
class Filter(ABC): | |
@abstractmethod | |
def apply(self, fileobj): | |
pass | |
class SizeFilter(Filter): | |
def __init__(self, size_limit=0) -> None: | |
self.size_limit = size_limit | |
def apply(self, fileobj): | |
return fileobj.size < self.size_limit | |
class TypeFilter(Filter): | |
def __init__(self, filetypes=[]) -> None: | |
self.filetypes = filetypes | |
def apply(self, fileobj): | |
return fileobj.type in self.filetypes | |
class Finder: | |
def __init__(self, filterobj, rootdir): | |
self.filter = filterobj | |
self.rootdir = rootdir | |
def find(self): | |
results = [] | |
self.dfs(self.rootdir, [], results) | |
return results | |
def dfs(self, node, prefix, results): | |
if node.is_file and self.filter.apply(node): | |
results.append('/'.join(prefix)) | |
return | |
for c in node.children: | |
prefix.append(c) | |
self.dfs(node.children[c], prefix, results) | |
prefix.pop() | |
""" | |
example usage | |
""" | |
filesys = FileSystem() | |
filesys.create_file('/rongrong/coding/soln.cpp') | |
filesys.create_file('/rongrong/company/amz.cpp') | |
filesys.create_file('/rongrong/design/ood.ipynb') | |
filesys.create_file('/rongrong/entertain/photo.jpg') | |
type_finder = Finder(TypeFilter(filetypes=['cpp']), filesys.root_dir) | |
# output: ['rongrong/coding/soln.cpp', 'rongrong/company/amz.cpp'] | |
type_finder.find() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment