Решение на In-memory файлова система от Aнтония Чекръкчиева

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

Към профила на Aнтония Чекръкчиева

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 7 успешни тест(а)
  • 11 неуспешни тест(а)

Код

import copy
class FileSystemError(Exception):
def __init__(self):
super().__init__()
self.message = "FileSystemError"
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NodeDoesNotExistError"
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
self.message = 'SourceNodeDoesNotExistError'
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
self.message = 'DestinationNodeDoesNotExistError'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DestinationNodeExistsError"
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NotEnoughSpaceError"
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NonExplicitDirectoryDeletionError"
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NonEmptyDirectoryDeletionError"
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DestinationNotADirectoryError"
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DirectoryHardLinkError"
class FileSystemMountError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "FileSystemMountError"
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointDoesNotExistError"
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointNotADirectoryError"
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointNotEmptyError"
class SystemObject:
def __init__(self, path, directory=False, content=' '):
self.path = path
self.is_directory = directory
self.content = content
self.nodes = []
self.directories = []
self.files = []
def add_children(self, child):
sub_dir = self.get_sib_dir(child.path)
if self.get_node(sub_dir):
self.get_node(sub_dir).add_children(child)
else:
if child.is_directory:
self.directories.append(child)
else:
self.files.append(child)
self.nodes.append(child)
def append(self, text):
self.content += text
def change_path(self, new_path_name):
if new_path_name == '/':
self.path = new_path_name + self.path.split('/')[-1]
else:
self.path = new_path_name + '/' + self.path.split('/')[-1]
def check_if_subnode_exist(self, child):
if not self.get_sib_dir(child.path):
return True
if self.get_node(self.get_sib_dir(child.path)):
return True
return False
def get_sib_dir(self, path):
return '/'.join(path.split('/')[:-1])
def get_size(self):
return len(self.content)
def get_children(self, path):
if path == self.path:
return self.content
def get_node(self, path):
for i in self.nodes:
if path == i.path:
return i
return i.get_node(path)
def have_children(self):
return len(self.nodes) != 0
def __iter__(self):
return iter(self.nodes)
def show_tree(self):
if self.path:
print(self.path)
for i in self.nodes:
i.show_tree()
def remove_path(self, path):
for i in self.nodes:
if path == i.path:
if i.is_directory:
self.directories.remove(i)
else:
self.files.remove(i)
self.nodes.remove(i)
return True
i.remove_path(path)
def truncate(self, text):
self.content = ''
class FileSystem:
def __init__(self, size):
self.root = SystemObject('/')
self.size = size
self.available_size = size - self.root.get_size()
def get_node(self, path):
if path == self.root.path:
return self.root
if(self.root.get_node(path)):
return self.root.get_node(path)
else:
raise NodeDoesNotExistError
def create(self, path, directory=False, content=' '):
if self.available_size < len(content):
raise NotEnoughSpaceError
if self.root.get_node(path):
raise DestinationNodeExistsError
children = SystemObject(path, directory, content)
self.root.add_children(children)
if not self.root.check_if_subnode_exist(children):
raise DestinationNodeDoesNotExistError
if directory:
self.available_size -= 1
else:
self.available_size -= len(content) + 1
def remove(self, path, directory=False, force=True):
if not self.root.get_node(path):
raise NodeDoesNotExistError
if self.root.get_node(path).is_directory != directory:
raise NonExplicitDirectoryDeletionError
if directory and self.root.get_node(path).have_children(path) and not force:
raise NonEmptyDirectoryDeletionError
if directory:
self.available_size += 1
else:
self.available_size += len(self.root.get_node(path).content)
self.root.remove_path(path)
def move(self, source, destination):
if not self.get_node(source):
raise SourceNodeDoesNotExistError
if not self.get_node(destination):
raise DestinationNodeDoesNotExistError
if not self.get_node(destination).is_directory:
raise DestinationNotADirectoryError
if self.get_node(destination).is_directory and self.get_node(destination).have_children():
raise DestinationNodeExistsError
sr = copy.deepcopy(self.get_node(source))
sr.change_path(destination)
self.remove(self.get_node(source).path)
self.get_node(destination).add_children(sr)
def link(self, source, destination, symbolic=True):
if symbolic and not self.get_node(source):
raise NodeDoesNotExistError
if not symbolic and self.get_node(source).is_directory:
raise DirectoryHardLinkError
if not self.get_node(source):
raise SourceNodeDoesNotExistError
def mount(self, file_system, path):
if self.get_node(path).have_children():
raise MountPointNotEmptyError
if not self.get_node.is_directory:
raise MountPointNotADirectoryError
if not self.get_node(path):
raise MountPointDoesNotExistError
def unmount(self, path):
if not self.get_node(path):
raise NodeDoesNotExistError

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

.EEE..E.EEE..E.EEE
======================================================================
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_link_create (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_mounting (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_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_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 22.162s

FAILED (errors=11)

История (1 версия и 0 коментара)

Aнтония обнови решението на 30.04.2015 02:19 (преди над 9 години)

+import copy
+
+
+class FileSystemError(Exception):
+ def __init__(self):
+ super().__init__()
+ self.message = "FileSystemError"
+
+
+class NodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NodeDoesNotExistError"
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ super().__init__()
+ self.message = 'SourceNodeDoesNotExistError'
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ super().__init__()
+ self.message = 'DestinationNodeDoesNotExistError'
+
+
+class DestinationNodeExistsError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DestinationNodeExistsError"
+
+
+class NotEnoughSpaceError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NotEnoughSpaceError"
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NonExplicitDirectoryDeletionError"
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NonEmptyDirectoryDeletionError"
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DestinationNotADirectoryError"
+
+
+class DirectoryHardLinkError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DirectoryHardLinkError"
+
+
+class FileSystemMountError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "FileSystemMountError"
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+ self.message = "MountPointDoesNotExistError"
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+ self.message = "MountPointNotADirectoryError"
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+ self.message = "MountPointNotEmptyError"
+
+
+class SystemObject:
+
+ def __init__(self, path, directory=False, content=' '):
+ self.path = path
+ self.is_directory = directory
+ self.content = content
+ self.nodes = []
+ self.directories = []
+ self.files = []
+
+ def add_children(self, child):
+ sub_dir = self.get_sib_dir(child.path)
+ if self.get_node(sub_dir):
+ self.get_node(sub_dir).add_children(child)
+ else:
+ if child.is_directory:
+ self.directories.append(child)
+ else:
+ self.files.append(child)
+ self.nodes.append(child)
+
+ def append(self, text):
+ self.content += text
+
+ def change_path(self, new_path_name):
+ if new_path_name == '/':
+ self.path = new_path_name + self.path.split('/')[-1]
+ else:
+ self.path = new_path_name + '/' + self.path.split('/')[-1]
+
+ def check_if_subnode_exist(self, child):
+ if not self.get_sib_dir(child.path):
+ return True
+ if self.get_node(self.get_sib_dir(child.path)):
+ return True
+ return False
+
+ def get_sib_dir(self, path):
+ return '/'.join(path.split('/')[:-1])
+
+ def get_size(self):
+ return len(self.content)
+
+ def get_children(self, path):
+ if path == self.path:
+ return self.content
+
+ def get_node(self, path):
+ for i in self.nodes:
+ if path == i.path:
+ return i
+ return i.get_node(path)
+
+ def have_children(self):
+ return len(self.nodes) != 0
+
+ def __iter__(self):
+ return iter(self.nodes)
+
+ def show_tree(self):
+ if self.path:
+ print(self.path)
+ for i in self.nodes:
+ i.show_tree()
+
+ def remove_path(self, path):
+ for i in self.nodes:
+ if path == i.path:
+ if i.is_directory:
+ self.directories.remove(i)
+ else:
+ self.files.remove(i)
+ self.nodes.remove(i)
+ return True
+ i.remove_path(path)
+
+ def truncate(self, text):
+ self.content = ''
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.root = SystemObject('/')
+ self.size = size
+ self.available_size = size - self.root.get_size()
+
+ def get_node(self, path):
+ if path == self.root.path:
+ return self.root
+ if(self.root.get_node(path)):
+ return self.root.get_node(path)
+ else:
+ raise NodeDoesNotExistError
+
+ def create(self, path, directory=False, content=' '):
+ if self.available_size < len(content):
+ raise NotEnoughSpaceError
+
+ if self.root.get_node(path):
+ raise DestinationNodeExistsError
+
+ children = SystemObject(path, directory, content)
+ self.root.add_children(children)
+
+ if not self.root.check_if_subnode_exist(children):
+ raise DestinationNodeDoesNotExistError
+ if directory:
+ self.available_size -= 1
+ else:
+ self.available_size -= len(content) + 1
+
+ def remove(self, path, directory=False, force=True):
+ if not self.root.get_node(path):
+ raise NodeDoesNotExistError
+ if self.root.get_node(path).is_directory != directory:
+ raise NonExplicitDirectoryDeletionError
+ if directory and self.root.get_node(path).have_children(path) and not force:
+ raise NonEmptyDirectoryDeletionError
+
+ if directory:
+ self.available_size += 1
+ else:
+ self.available_size += len(self.root.get_node(path).content)
+
+ self.root.remove_path(path)
+
+ def move(self, source, destination):
+ if not self.get_node(source):
+ raise SourceNodeDoesNotExistError
+
+ if not self.get_node(destination):
+ raise DestinationNodeDoesNotExistError
+
+ if not self.get_node(destination).is_directory:
+ raise DestinationNotADirectoryError
+
+ if self.get_node(destination).is_directory and self.get_node(destination).have_children():
+ raise DestinationNodeExistsError
+
+ sr = copy.deepcopy(self.get_node(source))
+ sr.change_path(destination)
+ self.remove(self.get_node(source).path)
+ self.get_node(destination).add_children(sr)
+
+ def link(self, source, destination, symbolic=True):
+ if symbolic and not self.get_node(source):
+ raise NodeDoesNotExistError
+
+ if not symbolic and self.get_node(source).is_directory:
+ raise DirectoryHardLinkError
+
+ if not self.get_node(source):
+ raise SourceNodeDoesNotExistError
+
+ def mount(self, file_system, path):
+ if self.get_node(path).have_children():
+ raise MountPointNotEmptyError
+ if not self.get_node.is_directory:
+ raise MountPointNotADirectoryError
+ if not self.get_node(path):
+ raise MountPointDoesNotExistError
+
+ def unmount(self, path):
+ if not self.get_node(path):
+ raise NodeDoesNotExistError