Решение на In-memory файлова система от Момчил Сулов

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

Към профила на Момчил Сулов

Резултати

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

Код

class FileSystemError(Exception):
pass
class DestinationNodeExistsError(FileSystemError):
pass
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 NotAMountpointError(FileSystemMountError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class NonexcplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass
class DirectoryHardLinkError(FileSystemError):
pass
class Node:
def __init__(self, is_directory, name):
self.is_directory = is_directory
self.name = name
class File(Node):
def __init__(self, name, content):
Node.__init__(self, False, name)
self.content = content
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
def size(self):
return len(self.content) + 1
class Directory(Node):
def __init__(self, name, directories=[], files=[]):
Node.__init__(self, True, name)
self.directories = directories
self.files = files
# self.nodes = files + directories
self.__set_nodes__()
def __set_nodes__(self):
self.nodes = self.directories + self.files
def add_directory(self, name):
directory = Directory(name)
self.directories.append(directory)
self.__set_nodes__()
def add_file(self, name, content):
file = File(name, content)
self.files.append(file)
self.__set_nodes__()
def find_node(self, name):
try:
for node in self.nodes:
if node.name == name:
return node
raise NodeDoesNotExistError
except NodeDoesNotExistError:
print()
def split(word, res=[]):
print("path:")
print(word)
i = 1
while i < len(word) and word[i] != '/':
i += 1
if word[:i] != '':
res.append(word[:i])
# print(i)
# print(len(word))
if i >= len(word):
return res
split(word[i - 1:], res)
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.root = Directory('')
def get_node(self, path):
try:
if path == '':
return self.root
levels = split(path)
def find(levels, dir):
if levels[0] not in dir.nodes:
raise NodeDoesNotExistError
new_node = dir.find_node(levels[0])
if levels.length() == 1:
return new_node
find(levels[1:], new_node)
return find(levels, self.root)
except NodeDoesNotExistError:
pass
def create(self, path, directory=False, content=''):
try:
if len(content) + 1 > self.available_size:
raise NotEnoughSpaceError
i = len(path) - 1
while path[i] != '/':
i -= 1
levels = split(path[:i])
print("levels:")
print(levels)
name = path[i:]
def add(levels, name, previous):
try:
ind = previous.directories.index(levels[0])
except ValueError:
raise DestinationNodeDoesNotExistError
dir = previous.directories[ind]
if len(levels) == 1:
if name in dir.directories:
raise DestinationNodeExistsError
if directory:
dir.add_directory(name)
self.available_size -= 1
else:
dir.add_file(name, content)
self.available_size -= len(content) + 1
return
if dir not in previous.directories:
raise DestinationNodeDoesNotExistError
add(levels[1:], name, dir)
add(levels, name, self.root)
except NotEnoughSpaceError:
print("There's not enough space in the file system!")
except DestinationNodeDoesNotExistError:
print("This node does not exist!")

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

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.161s

FAILED (errors=17)

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

Момчил обнови решението на 30.04.2015 12:19 (преди над 9 години)

+class FileSystemError(Exception):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+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 NotAMountpointError(FileSystemMountError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class NonexcplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class DirectoryHardLinkError(FileSystemError):
+ pass
+
+
+class Node:
+ def __init__(self, is_directory, name):
+ self.is_directory = is_directory
+ self.name = name
+
+
+class File(Node):
+ def __init__(self, name, content):
+ Node.__init__(self, False, name)
+ self.content = content
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ def size(self):
+ return len(self.content) + 1
+
+
+class Directory(Node):
+ def __init__(self, name, directories=[], files=[]):
+ Node.__init__(self, True, name)
+ self.directories = directories
+ self.files = files
+ # self.nodes = files + directories
+ self.__set_nodes__()
+
+ def __set_nodes__(self):
+ self.nodes = self.directories + self.files
+
+ def add_directory(self, name):
+ directory = Directory(name)
+ self.directories.append(directory)
+ self.__set_nodes__()
+
+ def add_file(self, name, content):
+ file = File(name, content)
+ self.files.append(file)
+ self.__set_nodes__()
+
+ def find_node(self, name):
+ try:
+ for node in self.nodes:
+ if node.name == name:
+ return node
+ raise NodeDoesNotExistError
+ except NodeDoesNotExistError:
+ print()
+
+
+def split(word, res=[]):
+ print("path:")
+ print(word)
+ i = 1
+ while i < len(word) and word[i] != '/':
+ i += 1
+ if word[:i] != '':
+ res.append(word[:i])
+ # print(i)
+ # print(len(word))
+ if i >= len(word):
+ return res
+ split(word[i - 1:], res)
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.root = Directory('')
+
+ def get_node(self, path):
+ try:
+ if path == '':
+ return self.root
+ levels = split(path)
+
+ def find(levels, dir):
+ if levels[0] not in dir.nodes:
+ raise NodeDoesNotExistError
+ new_node = dir.find_node(levels[0])
+ if levels.length() == 1:
+ return new_node
+ find(levels[1:], new_node)
+ return find(levels, self.root)
+ except NodeDoesNotExistError:
+ pass
+
+ def create(self, path, directory=False, content=''):
+ try:
+ if len(content) + 1 > self.available_size:
+ raise NotEnoughSpaceError
+ i = len(path) - 1
+ while path[i] != '/':
+ i -= 1
+ levels = split(path[:i])
+ print("levels:")
+ print(levels)
+ name = path[i:]
+
+ def add(levels, name, previous):
+ try:
+ ind = previous.directories.index(levels[0])
+ except ValueError:
+ raise DestinationNodeDoesNotExistError
+ dir = previous.directories[ind]
+ if len(levels) == 1:
+ if name in dir.directories:
+ raise DestinationNodeExistsError
+ if directory:
+ dir.add_directory(name)
+ self.available_size -= 1
+ else:
+ dir.add_file(name, content)
+ self.available_size -= len(content) + 1
+ return
+ if dir not in previous.directories:
+ raise DestinationNodeDoesNotExistError
+ add(levels[1:], name, dir)
+
+ add(levels, name, self.root)
+
+ except NotEnoughSpaceError:
+ print("There's not enough space in the file system!")
+ except DestinationNodeDoesNotExistError:
+ print("This node does not exist!")