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

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

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

Резултати

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

Код

class FileSystemError(Exception):
def __init__(self, message):
self.message = message
class NodeDoesNotExistError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class DirectoryHardLinkError(FileSystemError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class File:
def __init__(self, content):
self.__is_directory = False
self.__content = content
self.__size = len(content) + 1
@property
def content(self):
return self.__content
@property
def size(self):
return self.__size
@property
def is_directory(self):
return self.__is_directory
def append(self, text):
self.__content += text
def truncate(self, text):
self.__content = text
self.__size = len(text) + 1
class Directory:
def __init__(self, name):
self.__name = name
self.__is_directory = True
self.__directories = []
self.__files = []
self.__nodes = []
@property
def files(self):
return self.__files
@property
def directories(self):
return self.__directories
@property
def nodes(self):
return self.__files + self.__directories
@property
def name(self):
return self.__name
class FileSystem:
def __init__(self, size):
self.__size = size
self.__available_size = size - 1 # 1 byte for root(/)
self.__root = Directory('/')
@property
def size(self):
return self.__size
@property
def available_size(self):
return self.__available_size
# not implemented yet
def get_node(self, path):
path_directories = path.split('/')
path_directories = list(filter(lambda x: x != '', path_directories))
# doesn't work, don't bother reading
def create(self, path, directory=False, content=''):
path_directories = path.split('/')
path_directories = list(filter(lambda x: x != '', path_directories))
if len(path_directories) == 1:
if directory is True:
self.root.directories.append(Directory(path_directories[0]))
else:
self.root.files.append(File(content))
else:
current_dir = self.root.directories[0]
destination_dir = path_directories[len(path_directories) - 1]
for i in range(0, len(path_directories) - 1):
if (current_dir.name != path_directories[i] and
path_directories[i] == destination_dir):
if directory is True:
if self.__available_size > 0:
new_dir = Directory(path_directories[i])
current_dir.directories.append(new_dir)
else:
raise NotEnoughSpaceError
else:
if len(content + 1) <= self.__available_size:
current_dir.files.append(File(content))
else:
raise NotEnoughSpaceError

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

EEEEEEE.EEEEEEEEEE
======================================================================
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_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_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.178s

FAILED (errors=17)

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

Иван обнови решението на 30.04.2015 12:07 (преди над 9 години)

+class FileSystemError(Exception):
+
+ def __init__(self, message):
+ self.message = message
+
+
+class NodeDoesNotExistError(FileSystemError):
+
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+
+ pass
+
+
+class DirectoryHardLinkError(FileSystemError):
+
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ pass
+
+
+class File:
+
+ def __init__(self, content):
+ self.__is_directory = False
+ self.__content = content
+ self.__size = len(content) + 1
+
+ @property
+ def content(self):
+ return self.__content
+
+ @property
+ def size(self):
+ return self.__size
+
+ @property
+ def is_directory(self):
+ return self.__is_directory
+
+ def append(self, text):
+ self.__content += text
+
+ def truncate(self, text):
+ self.__content = text
+ self.__size = len(text) + 1
+
+
+class Directory:
+
+ def __init__(self, name):
+ self.__name = name
+ self.__is_directory = True
+ self.__directories = []
+ self.__files = []
+ self.__nodes = []
+
+ @property
+ def files(self):
+ return self.__files
+
+ @property
+ def directories(self):
+ return self.__directories
+
+ @property
+ def nodes(self):
+ return self.__files + self.__directories
+
+ @property
+ def name(self):
+ return self.__name
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.__size = size
+ self.__available_size = size - 1 # 1 byte for root(/)
+ self.__root = Directory('/')
+
+ @property
+ def size(self):
+ return self.__size
+
+ @property
+ def available_size(self):
+ return self.__available_size
+
+ # not implemented yet
+ def get_node(self, path):
+ path_directories = path.split('/')
+ path_directories = list(filter(lambda x: x != '', path_directories))
+
+ # doesn't work, don't bother reading
+ def create(self, path, directory=False, content=''):
+ path_directories = path.split('/')
+ path_directories = list(filter(lambda x: x != '', path_directories))
+ if len(path_directories) == 1:
+ if directory is True:
+ self.root.directories.append(Directory(path_directories[0]))
+ else:
+ self.root.files.append(File(content))
+ else:
+ current_dir = self.root.directories[0]
+ destination_dir = path_directories[len(path_directories) - 1]
+ for i in range(0, len(path_directories) - 1):
+ if (current_dir.name != path_directories[i] and
+ path_directories[i] == destination_dir):
+ if directory is True:
+ if self.__available_size > 0:
+ new_dir = Directory(path_directories[i])
+ current_dir.directories.append(new_dir)
+ else:
+ raise NotEnoughSpaceError
+ else:
+ if len(content + 1) <= self.__available_size:
+ current_dir.files.append(File(content))
+ else:
+ raise NotEnoughSpaceError