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

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

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

Резултати

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

Код

def prev_path(path):
sample = ''
for i in path.split('/')[1:-1]:
sample += ('/' + i)
return sample
class File:
def __init__(self, content, path):
self.content = content
self.path = path
def is_directory(self):
return False
class Directory:
def is_empty(self):
return self.empty
def __init__(self, path):
self.path = path
self.empty = True
self.files = []
self.directories = []
def is_directory(self):
return True
class FileSystem(File, Directory):
def __init__(self, memo):
self.size = memo
self.avaliable_size = memo - 1
self.paths = {'': Directory('')}
def get_node(self, path):
if path not in self.paths:
raise NodeDoesNotExistError
else:
return self.paths[path]
def create(self, path, directory=False, content=''):
if prev_path(path) not in self.paths:
raise DestinationNodeDoesNotExistError
if self.avaliable_size - len(content) - 1 < 0:
raise NotEnoughSpaceError
if path in self.paths:
raise DestinationNodeExistsError
self.avaliable_size = self.avaliable_size - len(content) - 1
if self.paths[prev_path(path)].is_directory():
self.paths[prev_path(path)].empty = False
if directory is False:
self.paths[path] = File(content, path)
self.paths[prev_path(path)].files.append(self.get_node(path))
self.paths[prev_path(path)].files.append(path)
self.paths[prev_path(path)].files.append(content)
else:
self.paths[path] = Directory(path)
self.paths[prev_path(path)].directories.append(self.get_node(path))
self.paths[prev_path(path)].directories.append(path)
def remove(self, path, directory=False, force=True):
if self.paths[path].is_directory():
if directory is False:
raise NonExplicitDirectoryDeletionError
if self.paths[path].is_empty() is True and force is not True:
raise NonEmptyDirectoryDeletionError
if path not in self.paths:
raise NodeDoesNotExistError
if self.paths[path].files is not []:
for i in self.paths[path].files:
remove(i, get_node(path).is_directory(), force=True)
del self.paths[path]
else:
self.avaliable_size += (len(self.paths[path].content) + 1)
del self.paths[path]
def move(self, source, destination):
if source not in self.paths:
raise SourceNodeDoesNotExistError
if destination not in self.paths:
raise DestinationNodeDoesNotExistError
if get_node(destination).is_directory is False:
raise DestinationNotADirectoryError
if (destination + '/' + source.split('/')[-1]) in self.paths:
raise DestinationNodeExistsError
self.remove(source, directory=False, force=True)
self.create(destination, directory=False, content='')
def mount(self, file_system, path):
if self.get_node(path).is_empty is False:
raise MountPointNotEmptyError
if self.get_node(path).is_directory is False:
raise MountPointNotADirectoryError
if path not in self.paths:
raise MountPointDoesNotExistError
self.paths[path] = file_system
def unmount(self, path):
if path not in self.paths:
raise NodeDoesNotExistError
if type(self.paths[path]) is not FileSystem:
raise NotAMountpointError
self.paths[path] = Directory(path)
def link(self, source, destination, symbolic=True):
if source not in self.paths and symbolic is True:
raise NodeDoesNotExistError
if self.paths[source].is_directory and symbolic is False:
raise DirectoryHardLinkError
if symbolic is False and destination not in self.paths:
raise SourceNodeDoesNotExistError
class FileSystemError:
pass
class NodeDoesNotExistError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError, FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError, FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError, FileSystemError):
pass
class MountPointNotADirectoryError(FileSystemMountError, FileSystemError):
pass
class MountPointNotEmptyError(FileSystemMountError, FileSystemError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class SourceNodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(FileSystemError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass

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

.EEEEEEEEEEEEEEEEE
======================================================================
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_minimal (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.138s

FAILED (errors=17)

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

Виктор обнови решението на 30.04.2015 02:16 (преди над 9 години)

+def prev_path(path):
+ sample = ''
+ for i in path.split('/')[1:-1]:
+ sample += ('/' + i)
+ return sample
+
+
+class File:
+ def __init__(self, content, path):
+ self.content = content
+ self.path = path
+
+ def is_directory(self):
+ return False
+
+
+class Directory:
+ def is_empty(self):
+ return self.empty
+
+ def __init__(self, path):
+ self.path = path
+ self.empty = True
+ self.files = []
+ self.directories = []
+
+ def is_directory(self):
+ return True
+
+
+class FileSystem(File, Directory):
+ def __init__(self, memo):
+ self.size = memo
+ self.avaliable_size = memo - 1
+ self.paths = {'': Directory('')}
+
+ def get_node(self, path):
+ if path not in self.paths:
+ raise NodeDoesNotExistError
+ else:
+ return self.paths[path]
+
+ def create(self, path, directory=False, content=''):
+ if prev_path(path) not in self.paths:
+ raise DestinationNodeDoesNotExistError
+ if self.avaliable_size - len(content) - 1 < 0:
+ raise NotEnoughSpaceError
+ if path in self.paths:
+ raise DestinationNodeExistsError
+ self.avaliable_size = self.avaliable_size - len(content) - 1
+ if self.paths[prev_path(path)].is_directory():
+ self.paths[prev_path(path)].empty = False
+ if directory is False:
+ self.paths[path] = File(content, path)
+ self.paths[prev_path(path)].files.append(self.get_node(path))
+ self.paths[prev_path(path)].files.append(path)
+ self.paths[prev_path(path)].files.append(content)
+ else:
+ self.paths[path] = Directory(path)
+ self.paths[prev_path(path)].directories.append(self.get_node(path))
+ self.paths[prev_path(path)].directories.append(path)
+
+ def remove(self, path, directory=False, force=True):
+ if self.paths[path].is_directory():
+ if directory is False:
+ raise NonExplicitDirectoryDeletionError
+ if self.paths[path].is_empty() is True and force is not True:
+ raise NonEmptyDirectoryDeletionError
+ if path not in self.paths:
+ raise NodeDoesNotExistError
+ if self.paths[path].files is not []:
+ for i in self.paths[path].files:
+ remove(i, get_node(path).is_directory(), force=True)
+ del self.paths[path]
+ else:
+ self.avaliable_size += (len(self.paths[path].content) + 1)
+ del self.paths[path]
+
+ def move(self, source, destination):
+ if source not in self.paths:
+ raise SourceNodeDoesNotExistError
+ if destination not in self.paths:
+ raise DestinationNodeDoesNotExistError
+ if get_node(destination).is_directory is False:
+ raise DestinationNotADirectoryError
+ if (destination + '/' + source.split('/')[-1]) in self.paths:
+ raise DestinationNodeExistsError
+ self.remove(source, directory=False, force=True)
+ self.create(destination, directory=False, content='')
+
+ def mount(self, file_system, path):
+ if self.get_node(path).is_empty is False:
+ raise MountPointNotEmptyError
+ if self.get_node(path).is_directory is False:
+ raise MountPointNotADirectoryError
+ if path not in self.paths:
+ raise MountPointDoesNotExistError
+ self.paths[path] = file_system
+
+ def unmount(self, path):
+ if path not in self.paths:
+ raise NodeDoesNotExistError
+ if type(self.paths[path]) is not FileSystem:
+ raise NotAMountpointError
+ self.paths[path] = Directory(path)
+
+ def link(self, source, destination, symbolic=True):
+ if source not in self.paths and symbolic is True:
+ raise NodeDoesNotExistError
+ if self.paths[source].is_directory and symbolic is False:
+ raise DirectoryHardLinkError
+ if symbolic is False and destination not in self.paths:
+ raise SourceNodeDoesNotExistError
+
+
+class FileSystemError:
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError, FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError, FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError, FileSystemError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError, FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError, FileSystemError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass