-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdesign-in-memory-file-system.py
96 lines (65 loc) · 2.74 KB
/
design-in-memory-file-system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from typing import Dict, Union, List
class FileSystemUnit:
pass
class Directory(FileSystemUnit):
def __init__(self) -> None:
self.children: Dict[str, Union[Directory, File]] = {}
class File(FileSystemUnit):
def __init__(self) -> None:
self.content: List[str] = []
def append(self, content: str) -> None:
self.content.extend(content.split("\n"))
def read(self) -> str:
return "".join(self.content)
class FileSystem:
def __init__(self):
self.root = Directory()
def _norm_path(self, path: str) -> List[str]:
return list(filter(bool, path.split("/")))
def _find_path(
self, path: List[str], path_pos: int, unit: Union[Directory, File]
) -> Union[Directory, File]:
if path_pos == len(path):
return unit
if not isinstance(unit, Directory):
raise ValueError("We can't dive into non-directory")
child = unit.children.setdefault(path[path_pos], File())
return self._find_path(path, path_pos + 1, child)
def _mkdir(
self, path: List[str], path_pos: int, unit: Union[Directory, File]
) -> None:
if path_pos == len(path):
return
if not isinstance(unit, Directory):
raise ValueError("We can't dive into non-directory")
child = unit.children.setdefault(path[path_pos], Directory())
return self._mkdir(path, path_pos + 1, child)
def ls(self, path: str) -> List[str]:
path_norm = self._norm_path(path)
resolved_path = self._find_path(path_norm, 0, self.root)
# It is a dir, return its contents
if isinstance(resolved_path, Directory):
return list(sorted(resolved_path.children.keys()))
# It was a file, return its name
return [path_norm[-1]]
def mkdir(self, path: str) -> None:
path_norm = self._norm_path(path)
self._mkdir(path_norm, 0, self.root)
def addContentToFile(self, path: str, content: str) -> None:
path_norm = self._norm_path(path)
resolved_path = self._find_path(path_norm, 0, self.root)
if isinstance(resolved_path, Directory):
raise ValueError("Can't add content to directory")
resolved_path.append(content)
def readContentFromFile(self, path: str) -> str:
path_norm = self._norm_path(path)
resolved_path = self._find_path(path_norm, 0, self.root)
if isinstance(resolved_path, Directory):
raise ValueError("Can't read from directory")
return resolved_path.read()
# Your FileSystem object will be instantiated and called as such:
# obj = FileSystem()
# param_1 = obj.ls(path)
# obj.mkdir(path)
# obj.addContentToFile(filePath,content)
# param_4 = obj.readContentFromFile(filePath)