Решение на In-memory файлова система от Цветан Иванов

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

Към профила на Цветан Иванов

Резултати

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

Код

class Node():
def __init__(self, is_directory, parent=""):
self.is_directory = is_directory
self.parent = parent
class File(Node):
def __init__(self, path, content, parent=""):
Node.__init__(self, False, parent)
self.content = content
self.path = path
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
@property
def size(self):
return len(self.content) + 1
class Directory(Node):
def __init__(self, path, parent=""):
Node.__init__(self, True, parent)
self.path = path
self.directories = {}
self.files = {}
@property
def nodes(self):
return dict(self.files, **self.directories)
@property
def size(self):
return 1
class FileSystemError(Exception):
def __init__(self):
self.message = 'FileSystemError'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
self.message = 'NodeDoesNotExistError'
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
self.message = 'DestinationNotADirectoryError'
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'DestinationNodeDoesNotExistError'
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'SourceNodeDoesNotExistError'
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'NonEmptyDirectoryDeletionError'
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'NonExplicitDirectoryDeletionError'
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
self.message = 'Not Enough Space Error'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
self.message = 'Node exists'
class DestinationNodeDoesNotExistError(FileSystemError):
def __init__(self):
self.message = 'Destination node does not exist'
class FileSystem():
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.nodes = {}
self.nodes["/"] = Directory("/")
if self.available_size < 0:
raise NotEnoughSpaceError
def get_node(self, path):
if path not in self.nodes:
raise NodeDoesNotExistError
return self.nodes[path]
def create(self, path, directory=False, content=''):
if path in self.nodes:
raise DestinationNodeExistsError
if self.available_size < len(content) + 1:
raise NotEnoughSpaceError
parent_path = path[0: path.rindex('/')]
if len(parent_path) == 0:
parent_path = '/'
if parent_path not in self.nodes:
raise DestinationNodeDoesNotExistError
if directory:
self.nodes[path] = Directory(path, self.nodes[parent_path])
self.nodes[parent_path].directories[path] = self.nodes[path]
else:
self.nodes[path] = File(path, content, self.nodes[parent_path])
self.nodes[parent_path].files[path] = self.nodes[path]
self.available_size -= len(content) + 1
def remove(self, path, directory=False, force=True):
if path not in self.nodes:
raise NodeDoesNotExistError
if not directory and self.nodes[path].is_directory:
raise NonExplicitDirectoryDeletionError
if directory and len(self.nodes[path].nodes) > 0 and not force:
raise NonEmptyDirectoryDeletionError
self.available_size += self.nodes[path].size
if directory:
del self.nodes[path].parent.directories[path]
else:
del self.nodes[path].parent.files[path]
del self.nodes[path]
def move(self, source, destination):
if source not in self.nodes:
raise SourceNodeDoesNotExistError
if destination not in self.nodes:
raise DestinationNodeDoesNotExistError
if not self.nodes[destination].is_directory:
raise DestinationNotADirectoryError
def link(self, source, destination, symbolic=True):
return
def mount(self, file_system, path):
return
def unmount(self, path):
return

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

..EEEEE.E.E..E.EEE
======================================================================
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_mounting (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.175s

FAILED (errors=11)

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

Цветан обнови решението на 30.04.2015 14:04 (преди над 9 години)

+class Node():
+
+ def __init__(self, is_directory, parent=""):
+ self.is_directory = is_directory
+ self.parent = parent
+
+
+class File(Node):
+
+ def __init__(self, path, content, parent=""):
+ Node.__init__(self, False, parent)
+ self.content = content
+ self.path = path
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ @property
+ def size(self):
+ return len(self.content) + 1
+
+
+class Directory(Node):
+
+ def __init__(self, path, parent=""):
+ Node.__init__(self, True, parent)
+ self.path = path
+ self.directories = {}
+ self.files = {}
+
+ @property
+ def nodes(self):
+ return dict(self.files, **self.directories)
+
+ @property
+ def size(self):
+ return 1
+
+class FileSystemError(Exception):
+
+ def __init__(self):
+ self.message = 'FileSystemError'
+
+class NodeDoesNotExistError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NodeDoesNotExistError'
+
+
+class DestinationNotADirectoryError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'DestinationNotADirectoryError'
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'DestinationNodeDoesNotExistError'
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'SourceNodeDoesNotExistError'
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NonEmptyDirectoryDeletionError'
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NonExplicitDirectoryDeletionError'
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'Not Enough Space Error'
+
+
+class DestinationNodeExistsError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'Node exists'
+
+
+class DestinationNodeDoesNotExistError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'Destination node does not exist'
+
+
+class FileSystem():
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.nodes = {}
+ self.nodes["/"] = Directory("/")
+
+ if self.available_size < 0:
+ raise NotEnoughSpaceError
+
+ def get_node(self, path):
+ if path not in self.nodes:
+ raise NodeDoesNotExistError
+
+ return self.nodes[path]
+
+ def create(self, path, directory=False, content=''):
+ if path in self.nodes:
+ raise DestinationNodeExistsError
+ if self.available_size < len(content) + 1:
+ raise NotEnoughSpaceError
+
+ parent_path = path[0: path.rindex('/')]
+ if len(parent_path) == 0:
+ parent_path = '/'
+
+ if parent_path not in self.nodes:
+ raise DestinationNodeDoesNotExistError
+
+ if directory:
+ self.nodes[path] = Directory(path, self.nodes[parent_path])
+ self.nodes[parent_path].directories[path] = self.nodes[path]
+ else:
+ self.nodes[path] = File(path, content, self.nodes[parent_path])
+ self.nodes[parent_path].files[path] = self.nodes[path]
+
+ self.available_size -= len(content) + 1
+
+ def remove(self, path, directory=False, force=True):
+ if path not in self.nodes:
+ raise NodeDoesNotExistError
+ if not directory and self.nodes[path].is_directory:
+ raise NonExplicitDirectoryDeletionError
+ if directory and len(self.nodes[path].nodes) > 0 and not force:
+ raise NonEmptyDirectoryDeletionError
+
+ self.available_size += self.nodes[path].size
+
+ if directory:
+ del self.nodes[path].parent.directories[path]
+ else:
+ del self.nodes[path].parent.files[path]
+
+ del self.nodes[path]
+
+ def move(self, source, destination):
+ if source not in self.nodes:
+ raise SourceNodeDoesNotExistError
+
+ if destination not in self.nodes:
+ raise DestinationNodeDoesNotExistError
+
+ if not self.nodes[destination].is_directory:
+ raise DestinationNotADirectoryError
+
+
+ def link(self, source, destination, symbolic=True):
+ return
+
+ def mount(self, file_system, path):
+ return
+
+ def unmount(self, path):
+ return