Решение на In-memory файлова система от Васил Николов

Обратно към всички решения

Към профила на Васил Николов

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 17 неуспешни тест(а)

Код

class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size
self.root = {'/': {}}
def get_node(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
print(pathway[i], "-", d)
def create(self, path, directory=False, content=""):
if directory == True and content == "":
check_index = 1
elif directory == False and content != "":
check_index = 0
else:
print("invalid_input")
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-check_index):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
if directory == True and pathway[-1] not in d:
self.available_size -= 1
if self.size > self.available_size > 0:
d[pathway[-1]] = {}
else:
return print("NotEnoughSpaceError")
elif directory == False and content not in d:
self.available_size -= len(content)
if self.size > self.available_size > 0:
d[content] = "file"
else:
return print("NotEnoughSpaceError")
else:
print("Exist")
def remove(self, path, directory=False, force=True):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
if pathway[-1] not in d:
return print("NodeDoesNotExistError")
elif directory == False and type(d[pathway[-1]]) is dict:
return print("NonExplicitDirectoryDeletionError")
elif force == False and d[pathway[-1]] != {}:
return print("NonEmptyDirectoryDeletionError")
else:
if type(d[pathway[-1]]) is dict:
size_red_l = []
def recurse(d):
if type(d) != dict:
size_red_l.append(len(d))
else:
size_red_l.append(1)
for k in d:
recurse(d[k])
recurse(d[pathway[-1]])
size_red = 0
for i in size_red_l:
size_red += i
self.available_size += size_red
else:
self.available_size += len(content)
del d[pathway[-1]]
def move(self, source, destination):
sourceway = [i for i in source.split("/") if i != ""]
s = self.root["/"]
for i in range(len(sourceway)-1):
if sourceway[i] in s.keys():
s = s[sourceway[i]]
else:
return print("SourceNodeDoesNotExistError")
if sourceway[-1] not in s:
return print("SourceNodeDoesNotExistError")
destway = [i for i in destination.split("/") if i != ""]
de = self.root["/"]
for e in range(len(destway)):
if destway[e] in de.keys():
de = de[destway[e]]
else:
return print("DestinationNodeDoesNotExistError")
if type(de) != dict:
return print("DestinationNotADirectoryError")
if sourceway[-1] in de:
return print("DestinationNodeExistsError")
else:
de[sourceway[-1]] = s[sourceway[-1]]
c = self.available_size
self.remove(source,True,True)
self.available_size = c
def link(self, source, destination, symbolic=True):
if symbolic == True:
sourceway = [i for i in source.split("/") if i != ""]
s = self.root["/"]
for i in range(len(sourceway)-1):
if sourceway[i] in s.keys():
s = s[sourceway[i]]
else:
return print("SourceNodeDoesNotExistError")
if sourceway[-1] not in s:
return print("SourceNodeDoesNotExistError")
destway = [i for i in destination.split("/") if i != ""]
de = self.root["/"]
for e in range(len(destway)):
if destway[e] in de.keys():
de = de[destway[e]]
else:
return print("DestinationNodeDoesNotExistError")
print(de)
de[str(destination)] = s[sourceway[-1]]
else:
pass
def mount(self, file_system, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
d[file_system] = file_system.root["/"]
def unmount(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
# raise Exception("NodeDoesNotExistError")
return print("NodeDoesNotExistError")
system = eval(pathway[-1])
if system in d:
del d[system]
else:
return print("NotAMountpointError")

Лог от изпълнението

EEEEEEEE.EEEEEEEEE
======================================================================
ERROR: test_create_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_create_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_create (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_space_consumption (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_to_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_to_missing_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_link_create (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_minimal (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_move_not_to_a_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_move_overwrite (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_out_of_space (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_overwrite (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_empty_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_nonempty_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_symlink_to_missing_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_valid_move (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 18 tests in 34.141s

FAILED (errors=17)

История (6 версии и 2 коментара)

Васил обнови решението на 27.04.2015 08:15 (преди почти 9 години)

+class FileSystem:
+ root = {'/': {}}
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size
+
+ def get_node(self, path):
+ pathway = [i for i in path.split("/") if i != ""]
+ d = self.root["/"]
+ for i in range(len(pathway)):
+ if pathway[i] in d.keys():
+ d = d[pathway[i]]
+ else:
+ return print("DestinationNodeDoesNotExistError")
+ print(pathway[i], "-", d)
+
+ def create(self, path, directory=False, content=""):
+ if directory == True and content == "":
+ check_index = 1
+ elif directory == False and content != "":
+ check_index = 0
+ else:
+ print("invalid_input")
+ pathway = [i for i in path.split("/") if i != ""]
+ d = self.root["/"]
+ for i in range(len(pathway)-check_index):
+ if pathway[i] in d.keys():
+ d = d[pathway[i]]
+ else:
+ return print("DestinationNodeDoesNotExistError")
+ if directory == True and pathway[-1] not in d:
+ self.available_size -= 1
+ if self.size > self.available_size > 0:
+ d[pathway[-1]] = {}
+ else:
+ return print("NotEnoughSpaceError")
+ elif directory == False and content not in d:
+ self.available_size -= len(content)
+ if self.size > self.available_size > 0:
+ d[content] = "file"
+ else:
+ return print("NotEnoughSpaceError")
+ else:
+ print("Exist")
+
+ def remove(self, path, directory=False, force=True):
+ pathway = [i for i in path.split("/") if i != ""]
+ d = self.root["/"]
+ for i in range(len(pathway)-1):
+ if pathway[i] in d.keys():
+ d = d[pathway[i]]
+ else:
+ return print("NodeDoesNotExistError")
+ if pathway[-1] not in d:
+ return print("NodeDoesNotExistError")
+ elif directory == False and type(d[pathway[-1]]) is dict:
+ return print("NonExplicitDirectoryDeletionError")
+ elif force == False and d[pathway[-1]] != {}:
+ return print("NonEmptyDirectoryDeletionError")
+ else:
+ if type(d[pathway[-1]]) is dict:
+ size_red_l = []
+ def recurse(d):
+ if type(d) != dict:
+ size_red_l.append(len(d))
+ else:
+ size_red_l.append(1)
+ for k in d:
+ recurse(d[k])
+ recurse(d[pathway[-1]])
+ size_red = 0
+ for i in size_red_l:
+ size_red += i
+
+ self.available_size += size_red
+ else:
+ self.available_size += len(content)
+ del d[pathway[-1]]
+
+
+ def move(self, source, destination):
+ sourceway = [i for i in source.split("/") if i != ""]
+ s = self.root["/"]
+ for i in range(len(sourceway)-1):
+ if sourceway[i] in s.keys():
+ s = s[sourceway[i]]
+ else:
+ return print("SourceNodeDoesNotExistError")
+ if sourceway[-1] not in s:
+ return print("SourceNodeDoesNotExistError")
+ destway = [i for i in destination.split("/") if i != ""]
+ de = self.root["/"]
+ for e in range(len(destway)):
+ if destway[e] in de.keys():
+ de = de[destway[e]]
+ else:
+ return print("DestinationNodeDoesNotExistError")
+ if type(de) != dict:
+ return print("DestinationNotADirectoryError")
+ if sourceway[-1] in de:
+ return print("DestinationNodeExistsError")
+ else:
+ de[sourceway[-1]] = s[sourceway[-1]]
+ c = self.available_size
+ self.remove(source,True,True)
+ self.available_size = c
+
+ def link(source, destination, symbolic=True):
+ pass
+
+ def mount(file_system, path):
+ pass
+
+ def unmount(path):
+ pass

Извинявам се за не дозавършения код, но имам известни проблеми с терминологията в методите "link", "mount" и "unmount". Възможно ли е да ми дадете някакъв линк къде мога да прочета повечко по въпроса? Колебаех се дали да пиша във форума или коментар по кода, но на пръв поглед другите хора от курса са по напред и това изглежда не е проблем за тях.

Другото нещо което ме притеснява е имплементацията на еррорите ми. Достигнах до заключение, че не разбирам системата, как сам да си правя еррори и да ги реизвам при определени ситуации. Ще е възможноли да ме насочите и там, за някакви примери на готов код в нета?

Васил обнови решението на 28.04.2015 20:45 (преди почти 9 години)

class FileSystem:
- root = {'/': {}}
def __init__(self, size):
self.size = size
self.available_size = size
+ self.root = {'/': {}}
def get_node(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
print(pathway[i], "-", d)
def create(self, path, directory=False, content=""):
if directory == True and content == "":
check_index = 1
elif directory == False and content != "":
check_index = 0
else:
print("invalid_input")
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-check_index):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
if directory == True and pathway[-1] not in d:
self.available_size -= 1
if self.size > self.available_size > 0:
d[pathway[-1]] = {}
else:
return print("NotEnoughSpaceError")
elif directory == False and content not in d:
self.available_size -= len(content)
if self.size > self.available_size > 0:
d[content] = "file"
else:
return print("NotEnoughSpaceError")
else:
print("Exist")
def remove(self, path, directory=False, force=True):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
if pathway[-1] not in d:
return print("NodeDoesNotExistError")
elif directory == False and type(d[pathway[-1]]) is dict:
return print("NonExplicitDirectoryDeletionError")
elif force == False and d[pathway[-1]] != {}:
return print("NonEmptyDirectoryDeletionError")
else:
if type(d[pathway[-1]]) is dict:
size_red_l = []
def recurse(d):
if type(d) != dict:
size_red_l.append(len(d))
else:
size_red_l.append(1)
for k in d:
recurse(d[k])
recurse(d[pathway[-1]])
size_red = 0
for i in size_red_l:
size_red += i
self.available_size += size_red
else:
self.available_size += len(content)
del d[pathway[-1]]
def move(self, source, destination):
sourceway = [i for i in source.split("/") if i != ""]
s = self.root["/"]
for i in range(len(sourceway)-1):
if sourceway[i] in s.keys():
s = s[sourceway[i]]
else:
return print("SourceNodeDoesNotExistError")
if sourceway[-1] not in s:
return print("SourceNodeDoesNotExistError")
destway = [i for i in destination.split("/") if i != ""]
de = self.root["/"]
for e in range(len(destway)):
if destway[e] in de.keys():
de = de[destway[e]]
else:
return print("DestinationNodeDoesNotExistError")
if type(de) != dict:
return print("DestinationNotADirectoryError")
if sourceway[-1] in de:
return print("DestinationNodeExistsError")
else:
de[sourceway[-1]] = s[sourceway[-1]]
c = self.available_size
self.remove(source,True,True)
self.available_size = c
def link(source, destination, symbolic=True):
pass
- def mount(file_system, path):
- pass
+ def mount(self, file_system, path):
+ pathway = [i for i in path.split("/") if i != ""]
+ d = self.root["/"]
+ for i in range(len(pathway)-1):
+ if pathway[i] in d.keys():
+ d = d[pathway[i]]
+ else:
+ return print("DestinationNodeDoesNotExistError")
+ d[pathway[-1]] = {file_system : file_system.root["/"]}
- def unmount(path):
- pass
+ def unmount(self, path):
+ pathway = [i for i in path.split("/") if i != ""]
+ d = self.root["/"]
+ for i in range(len(pathway)-1):
+ if pathway[i] in d.keys():
+ d = d[pathway[i]]
+ else:
+ return print("NodeDoesNotExistError")
+ if type(d[pathway[-1]]) == FileSystem:
+ del d[pathway[-1]]
+ else:
+ return print("NotAMountpointError")

Васил обнови решението на 28.04.2015 21:02 (преди почти 9 години)

class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size
self.root = {'/': {}}
def get_node(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
print(pathway[i], "-", d)
def create(self, path, directory=False, content=""):
if directory == True and content == "":
check_index = 1
elif directory == False and content != "":
check_index = 0
else:
print("invalid_input")
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-check_index):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
if directory == True and pathway[-1] not in d:
self.available_size -= 1
if self.size > self.available_size > 0:
d[pathway[-1]] = {}
else:
return print("NotEnoughSpaceError")
elif directory == False and content not in d:
self.available_size -= len(content)
if self.size > self.available_size > 0:
d[content] = "file"
else:
return print("NotEnoughSpaceError")
else:
print("Exist")
def remove(self, path, directory=False, force=True):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
if pathway[-1] not in d:
return print("NodeDoesNotExistError")
elif directory == False and type(d[pathway[-1]]) is dict:
return print("NonExplicitDirectoryDeletionError")
elif force == False and d[pathway[-1]] != {}:
return print("NonEmptyDirectoryDeletionError")
else:
if type(d[pathway[-1]]) is dict:
size_red_l = []
def recurse(d):
if type(d) != dict:
size_red_l.append(len(d))
else:
size_red_l.append(1)
for k in d:
recurse(d[k])
recurse(d[pathway[-1]])
size_red = 0
for i in size_red_l:
size_red += i
self.available_size += size_red
else:
self.available_size += len(content)
del d[pathway[-1]]
def move(self, source, destination):
sourceway = [i for i in source.split("/") if i != ""]
s = self.root["/"]
for i in range(len(sourceway)-1):
if sourceway[i] in s.keys():
s = s[sourceway[i]]
else:
return print("SourceNodeDoesNotExistError")
if sourceway[-1] not in s:
return print("SourceNodeDoesNotExistError")
destway = [i for i in destination.split("/") if i != ""]
de = self.root["/"]
for e in range(len(destway)):
if destway[e] in de.keys():
de = de[destway[e]]
else:
return print("DestinationNodeDoesNotExistError")
if type(de) != dict:
return print("DestinationNotADirectoryError")
if sourceway[-1] in de:
return print("DestinationNodeExistsError")
else:
de[sourceway[-1]] = s[sourceway[-1]]
c = self.available_size
self.remove(source,True,True)
self.available_size = c
def link(source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
d[pathway[-1]] = {file_system : file_system.root["/"]}
def unmount(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
- if type(d[pathway[-1]]) == FileSystem:
- del d[pathway[-1]]
+ system = eval(pathway[-1])
+ print(system)
+ print(d)
+ if system in d:
+ print("kkk")
+ del d[system]
else:
- return print("NotAMountpointError")
+ return print("NotAMountpointError")

Васил обнови решението на 28.04.2015 21:02 (преди почти 9 години)

class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size
self.root = {'/': {}}
def get_node(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
print(pathway[i], "-", d)
def create(self, path, directory=False, content=""):
if directory == True and content == "":
check_index = 1
elif directory == False and content != "":
check_index = 0
else:
print("invalid_input")
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-check_index):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
if directory == True and pathway[-1] not in d:
self.available_size -= 1
if self.size > self.available_size > 0:
d[pathway[-1]] = {}
else:
return print("NotEnoughSpaceError")
elif directory == False and content not in d:
self.available_size -= len(content)
if self.size > self.available_size > 0:
d[content] = "file"
else:
return print("NotEnoughSpaceError")
else:
print("Exist")
def remove(self, path, directory=False, force=True):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
if pathway[-1] not in d:
return print("NodeDoesNotExistError")
elif directory == False and type(d[pathway[-1]]) is dict:
return print("NonExplicitDirectoryDeletionError")
elif force == False and d[pathway[-1]] != {}:
return print("NonEmptyDirectoryDeletionError")
else:
if type(d[pathway[-1]]) is dict:
size_red_l = []
def recurse(d):
if type(d) != dict:
size_red_l.append(len(d))
else:
size_red_l.append(1)
for k in d:
recurse(d[k])
recurse(d[pathway[-1]])
size_red = 0
for i in size_red_l:
size_red += i
self.available_size += size_red
else:
self.available_size += len(content)
del d[pathway[-1]]
def move(self, source, destination):
sourceway = [i for i in source.split("/") if i != ""]
s = self.root["/"]
for i in range(len(sourceway)-1):
if sourceway[i] in s.keys():
s = s[sourceway[i]]
else:
return print("SourceNodeDoesNotExistError")
if sourceway[-1] not in s:
return print("SourceNodeDoesNotExistError")
destway = [i for i in destination.split("/") if i != ""]
de = self.root["/"]
for e in range(len(destway)):
if destway[e] in de.keys():
de = de[destway[e]]
else:
return print("DestinationNodeDoesNotExistError")
if type(de) != dict:
return print("DestinationNotADirectoryError")
if sourceway[-1] in de:
return print("DestinationNodeExistsError")
else:
de[sourceway[-1]] = s[sourceway[-1]]
c = self.available_size
self.remove(source,True,True)
self.available_size = c
def link(source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
d[pathway[-1]] = {file_system : file_system.root["/"]}
def unmount(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
system = eval(pathway[-1])
- print(system)
- print(d)
if system in d:
print("kkk")
del d[system]
else:
- return print("NotAMountpointError")
+ return print("NotAMountpointError")

Васил обнови решението на 29.04.2015 10:03 (преди почти 9 години)

class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size
self.root = {'/': {}}
def get_node(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
print(pathway[i], "-", d)
def create(self, path, directory=False, content=""):
if directory == True and content == "":
check_index = 1
elif directory == False and content != "":
check_index = 0
else:
print("invalid_input")
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-check_index):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
if directory == True and pathway[-1] not in d:
self.available_size -= 1
if self.size > self.available_size > 0:
d[pathway[-1]] = {}
else:
return print("NotEnoughSpaceError")
elif directory == False and content not in d:
self.available_size -= len(content)
if self.size > self.available_size > 0:
d[content] = "file"
else:
return print("NotEnoughSpaceError")
else:
print("Exist")
def remove(self, path, directory=False, force=True):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
if pathway[-1] not in d:
return print("NodeDoesNotExistError")
elif directory == False and type(d[pathway[-1]]) is dict:
return print("NonExplicitDirectoryDeletionError")
elif force == False and d[pathway[-1]] != {}:
return print("NonEmptyDirectoryDeletionError")
else:
if type(d[pathway[-1]]) is dict:
size_red_l = []
def recurse(d):
if type(d) != dict:
size_red_l.append(len(d))
else:
size_red_l.append(1)
for k in d:
recurse(d[k])
recurse(d[pathway[-1]])
size_red = 0
for i in size_red_l:
size_red += i
self.available_size += size_red
else:
self.available_size += len(content)
del d[pathway[-1]]
def move(self, source, destination):
sourceway = [i for i in source.split("/") if i != ""]
s = self.root["/"]
for i in range(len(sourceway)-1):
if sourceway[i] in s.keys():
s = s[sourceway[i]]
else:
return print("SourceNodeDoesNotExistError")
if sourceway[-1] not in s:
return print("SourceNodeDoesNotExistError")
destway = [i for i in destination.split("/") if i != ""]
de = self.root["/"]
for e in range(len(destway)):
if destway[e] in de.keys():
de = de[destway[e]]
else:
return print("DestinationNodeDoesNotExistError")
if type(de) != dict:
return print("DestinationNotADirectoryError")
if sourceway[-1] in de:
return print("DestinationNodeExistsError")
else:
de[sourceway[-1]] = s[sourceway[-1]]
c = self.available_size
self.remove(source,True,True)
self.available_size = c
def link(source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
d[pathway[-1]] = {file_system : file_system.root["/"]}
def unmount(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
system = eval(pathway[-1])
if system in d:
- print("kkk")
del d[system]
else:
return print("NotAMountpointError")

Васил обнови решението на 30.04.2015 11:17 (преди почти 9 години)

class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size
self.root = {'/': {}}
def get_node(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
print(pathway[i], "-", d)
def create(self, path, directory=False, content=""):
if directory == True and content == "":
check_index = 1
elif directory == False and content != "":
check_index = 0
else:
print("invalid_input")
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-check_index):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("DestinationNodeDoesNotExistError")
if directory == True and pathway[-1] not in d:
self.available_size -= 1
if self.size > self.available_size > 0:
d[pathway[-1]] = {}
else:
return print("NotEnoughSpaceError")
elif directory == False and content not in d:
self.available_size -= len(content)
if self.size > self.available_size > 0:
d[content] = "file"
else:
return print("NotEnoughSpaceError")
else:
print("Exist")
def remove(self, path, directory=False, force=True):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
return print("NodeDoesNotExistError")
if pathway[-1] not in d:
return print("NodeDoesNotExistError")
elif directory == False and type(d[pathway[-1]]) is dict:
return print("NonExplicitDirectoryDeletionError")
elif force == False and d[pathway[-1]] != {}:
return print("NonEmptyDirectoryDeletionError")
else:
if type(d[pathway[-1]]) is dict:
size_red_l = []
def recurse(d):
if type(d) != dict:
size_red_l.append(len(d))
else:
size_red_l.append(1)
for k in d:
recurse(d[k])
recurse(d[pathway[-1]])
size_red = 0
for i in size_red_l:
size_red += i
self.available_size += size_red
else:
self.available_size += len(content)
del d[pathway[-1]]
def move(self, source, destination):
sourceway = [i for i in source.split("/") if i != ""]
s = self.root["/"]
for i in range(len(sourceway)-1):
if sourceway[i] in s.keys():
s = s[sourceway[i]]
else:
return print("SourceNodeDoesNotExistError")
if sourceway[-1] not in s:
return print("SourceNodeDoesNotExistError")
destway = [i for i in destination.split("/") if i != ""]
de = self.root["/"]
for e in range(len(destway)):
if destway[e] in de.keys():
de = de[destway[e]]
else:
return print("DestinationNodeDoesNotExistError")
if type(de) != dict:
return print("DestinationNotADirectoryError")
if sourceway[-1] in de:
return print("DestinationNodeExistsError")
else:
de[sourceway[-1]] = s[sourceway[-1]]
c = self.available_size
self.remove(source,True,True)
self.available_size = c
- def link(source, destination, symbolic=True):
- pass
+ def link(self, source, destination, symbolic=True):
+ if symbolic == True:
+ sourceway = [i for i in source.split("/") if i != ""]
+ s = self.root["/"]
+ for i in range(len(sourceway)-1):
+ if sourceway[i] in s.keys():
+ s = s[sourceway[i]]
+ else:
+ return print("SourceNodeDoesNotExistError")
+ if sourceway[-1] not in s:
+ return print("SourceNodeDoesNotExistError")
+ destway = [i for i in destination.split("/") if i != ""]
+ de = self.root["/"]
+ for e in range(len(destway)):
+ if destway[e] in de.keys():
+ de = de[destway[e]]
+ else:
+ return print("DestinationNodeDoesNotExistError")
+ print(de)
+ de[str(destination)] = s[sourceway[-1]]
+ else:
+ pass
def mount(self, file_system, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
- for i in range(len(pathway)-1):
+ for i in range(len(pathway)):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
- return print("DestinationNodeDoesNotExistError")
- d[pathway[-1]] = {file_system : file_system.root["/"]}
+ return print("DestinationNodeDoesNotExistError")
+ d[file_system] = file_system.root["/"]
def unmount(self, path):
pathway = [i for i in path.split("/") if i != ""]
d = self.root["/"]
for i in range(len(pathway)-1):
if pathway[i] in d.keys():
d = d[pathway[i]]
else:
+ # raise Exception("NodeDoesNotExistError")
return print("NodeDoesNotExistError")
system = eval(pathway[-1])
if system in d:
del d[system]
else:
return print("NotAMountpointError")