Решение на In-memory файлова система от Александър Танков

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

Към профила на Александър Танков

Резултати

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

Код

class File():
def __init__(self, name, content=''):
self.name = name
self.content = content
self.is_directory = False
self.size = len(content) + 1
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
def size(self):
return len(self.content) + 1
class Directory(object):
def __init__(self, name):
self.name = name
self.files = {name: {}}
self.directories = []
self.nodes = []
self.is_directory = True
def _get_index_for_removing_node(self, name, directory=False):
if directory:
for dirs in range(0, len(self.directories)):
if self.directories[dirs].name == name:
return dirs
else:
for node in range(0, len(self.nodes)):
if self.nodes[node].name == name:
return node
return False
class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass # ???
class DestinationNodeExistsError(FileSystemError):
pass # ????
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class FileSystemRemoveError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemRemoveError):
pass
class NonEmptyDirectoryDeletionError(FileSystemRemoveError):
pass
class FileSystem():
def __init__(self, size):
self.root = Directory('')
self.size = size
self.available_size = size - 1
def __get_directory(self, directory, path_list):
temp = directory
if len(path_list) > 2:
#is valid path
for idx in range(0, len(path_list) - 1):
temp = temp[path_list[idx]]
elif len(path_list) == 2:
if path_list[1] in directory.files[path_list[0]]:
temp = temp[path_list[0]]
elif directory.files[''] == {}:
return directory
else:
raise DestinationNodeDoesNotExistError
else:
return directory[path_list[0]]
def get_node(self, path):
if path == '/':
new_path = ['']
else:
new_path = path.split('/')
return self.__get_directory(self.root, new_path)
def create(self, path, directory=False, content=''):
new_path = path.split('/')
name = new_path[-1:][0]
new_directory = self.__get_directory(self.root, new_path)
if directory:
if (name not in new_directory.files):
if self.available_size > 0:
new_directory.directories.append(Directory(name))
if new_directory.files[''] == {}:
new_directory = new_directory.files['']
new_directory[name] = Directory(name)
else:
new_directory.files[name] = Directory(name)
self.available_size -= 1
else:
raise NotEnoughSpaceError
else:
raise DestinationNodeExistsError
else:
if new_directory.is_directory and name not in new_directory.files:
if self.available_size >= len(content):
new_directory.nodes.append(File(name))
if new_directory.files[''] == {}:
new_directory = new_directory.files['']
new_directory[name] = File(name, content)
else:
new_directory.files[name] = File(name, content)
self.available_size -= (len(content) + 1)
else:
raise NotEnoughSpaceError
else:
raise DestinationNodeExistsError
def remove(self, path, directory=False, force=True):
new_path = path.split('/')
name = new_path[-1:][0]
new_directory = self.__get_directory(self.root, new_path[:1])
if directory:
if name in new_directory.files:
if new_directory.files[name] == {} or\
(new_directory.files[name] != {} and force):
self.available_size += 1
del new_directory.files[name]
else:
raise NonEmptyDirectoryDeletionError
else:
raise NodeDoesNotExistError
elif not directory and new_directory.files[name].is_directory:
raise NonExplicitDirectoryDeletionError
else:
if name in new_directory:
self.available_size += new_directory.files[name]
del new_directory.files[name]
else:
raise NodeDoesNotExistError
def move(self, source, destination):
pass
def link(self, source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pass
def unmount(self, path):
pass
if __name__ == '__main__':
a = FileSystem(100)
a.create('/data')
b = a.get_node('/data')
a.remove('/data')
print(a.available_size)

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

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

FAILED (errors=17)

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

Александър обнови решението на 30.04.2015 14:37 (преди над 9 години)

+class File():
+
+ def __init__(self, name, content=''):
+ self.name = name
+ self.content = content
+ self.is_directory = False
+ self.size = len(content) + 1
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ def size(self):
+ return len(self.content) + 1
+
+
+class Directory(object):
+ def __init__(self, name):
+ self.name = name
+ self.files = {name: {}}
+ self.directories = []
+ self.nodes = []
+ self.is_directory = True
+
+ def _get_index_for_removing_node(self, name, directory=False):
+ if directory:
+ for dirs in range(0, len(self.directories)):
+ if self.directories[dirs].name == name:
+ return dirs
+ else:
+ for node in range(0, len(self.nodes)):
+ if self.nodes[node].name == name:
+ return node
+ return False
+
+
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass # ???
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass # ????
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class FileSystemRemoveError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemRemoveError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemRemoveError):
+ pass
+
+
+class FileSystem():
+
+ def __init__(self, size):
+ self.root = Directory('')
+ self.size = size
+ self.available_size = size - 1
+
+ def __get_directory(self, directory, path_list):
+ temp = directory
+ if len(path_list) > 2:
+ #is valid path
+ for idx in range(0, len(path_list) - 1):
+ temp = temp[path_list[idx]]
+ elif len(path_list) == 2:
+ if path_list[1] in directory.files[path_list[0]]:
+ temp = temp[path_list[0]]
+ elif directory.files[''] == {}:
+ return directory
+ else:
+ raise DestinationNodeDoesNotExistError
+ else:
+ return directory[path_list[0]]
+
+ def get_node(self, path):
+ if path == '/':
+ new_path = ['']
+ else:
+ new_path = path.split('/')
+ return self.__get_directory(self.root, new_path)
+
+ def create(self, path, directory=False, content=''):
+ new_path = path.split('/')
+ name = new_path[-1:][0]
+ new_directory = self.__get_directory(self.root, new_path)
+ if directory:
+ if (name not in new_directory.files):
+ if self.available_size > 0:
+ new_directory.directories.append(Directory(name))
+ if new_directory.files[''] == {}:
+ new_directory = new_directory.files['']
+ new_directory[name] = Directory(name)
+ else:
+ new_directory.files[name] = Directory(name)
+ self.available_size -= 1
+ else:
+ raise NotEnoughSpaceError
+ else:
+ raise DestinationNodeExistsError
+ else:
+ if new_directory.is_directory and name not in new_directory.files:
+ if self.available_size >= len(content):
+ new_directory.nodes.append(File(name))
+ if new_directory.files[''] == {}:
+ new_directory = new_directory.files['']
+ new_directory[name] = File(name, content)
+ else:
+ new_directory.files[name] = File(name, content)
+ self.available_size -= (len(content) + 1)
+ else:
+ raise NotEnoughSpaceError
+ else:
+ raise DestinationNodeExistsError
+
+ def remove(self, path, directory=False, force=True):
+ new_path = path.split('/')
+ name = new_path[-1:][0]
+ new_directory = self.__get_directory(self.root, new_path[:1])
+
+ if directory:
+ if name in new_directory.files:
+ if new_directory.files[name] == {} or\
+ (new_directory.files[name] != {} and force):
+ self.available_size += 1
+ del new_directory.files[name]
+ else:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ raise NodeDoesNotExistError
+ elif not directory and new_directory.files[name].is_directory:
+ raise NonExplicitDirectoryDeletionError
+ else:
+ if name in new_directory:
+ self.available_size += new_directory.files[name]
+ del new_directory.files[name]
+ else:
+ raise NodeDoesNotExistError
+
+ def move(self, source, destination):
+ pass
+
+ def link(self, source, destination, symbolic=True):
+ pass
+
+ def mount(self, file_system, path):
+ pass
+
+ def unmount(self, path):
+ pass
+
+
+if __name__ == '__main__':
+ a = FileSystem(100)
+ a.create('/data')
+ b = a.get_node('/data')
+ a.remove('/data')
+ print(a.available_size)