Решение на In-memory файлова система от Милка Ферезлийска

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

Към профила на Милка Ферезлийска

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 6 успешни тест(а)
  • 12 неуспешни тест(а)

Код

class FileSystemError(Exception):
def __init__(self):
self.message = 'FileSystemError'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
super(DestinationNodeExistsError, self).__init__()
self.message = 'DestinationNodeExistsError'
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
super(NotEnoughSpaceError, self).__init__()
self.message = 'NotEnoughSpaceError'
class DestinationNodeDoesNotExistError(FileSystemError):
def __init__(self):
super(DestinationNodeDoesNotExistError, self).__init__()
self.message = 'NotEnoughSpaceError'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
super(NodeDoesNotExistError, self).__init__()
self.message = 'NodeDoesNotExistError'
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
super(NonExplicitDirectoryDeletionError, self).__init__()
self.message = 'NonExplicitDirectoryDeletionError'
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
super(NonExplicitDirectoryDeletionError, self).__init__()
self.message = 'NonEmptyDirectoryDeletionError'
class File:
def __init__(self, name, content=''):
self.content = str(content)
self.size = len(self.content) + 1
self.name = name
@property
def is_directory(self):
return False
def append(self, text):
self.content += text
self.size += len(text)
def truncate(self, text):
self.content = text
self.size = len(text) + 1
def __str__(self):
return self.content
class Directory:
def __init__(self, name):
self.name = name
self.size = 1
self.files = []
self.directories = []
self.nodes = []
def __str__(self):
return self.name
@property
def is_directory(self):
return True
def add(self, obj):
self.nodes.append(obj)
if type(obj) is File:
self.files.append(obj)
else:
self.directories.append(obj)
self.size = len(self.directories) + sum(
[file.size for file in self.files]) + 1
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
self._directories = {'/': Directory('/')}
def get_node(self, path):
if path in self._directories:
return self._directories[path]
raise NodeDoesNotExistError
def create(self, path, directory=False, content=''):
path = path[:-1] if path[-1] == '/' else path
if self.available_size < 2 or self.available_size < len(content) + 1:
raise NotEnoughSpaceError
elif path in self._directories:
raise DestinationNodeExistsError
else:
filename = path.split('/')[-1]
old_path = '/'.join(
[x for x in path.split('/') if x != filename])
if old_path == '':
old_path = '/'
if old_path not in self._directories:
raise DestinationNodeDoesNotExistError
else:
obj = self._directories[old_path]
new_obj = (
Directory(filename)
if directory else File(filename, content)
)
self._directories[path] = new_obj
obj.add(new_obj)
self.available_size -= new_obj.size
def remove(self, path, directory=False, force=True):
obj = self._directories[path]
if type(obj) is Directory and not directory:
if force:
pass
else:
raise NonEmptyDirectoryDeletionError
elif type(obj) is Directory:
raise NonExplicitDirectoryDeletionError
del self._directories[path]

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

..EEEEE.EEE..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_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_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 24.156s

FAILED (errors=12)

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

Милка обнови решението на 30.04.2015 15:08 (преди почти 9 години)

+class FileSystemError(Exception):
+ def __init__(self):
+ self.message = 'FileSystemError'
+
+
+class DestinationNodeExistsError(FileSystemError):
+ def __init__(self):
+ super(DestinationNodeExistsError, self).__init__()
+ self.message = 'DestinationNodeExistsError'
+
+
+class NotEnoughSpaceError(FileSystemError):
+ def __init__(self):
+ super(NotEnoughSpaceError, self).__init__()
+ self.message = 'NotEnoughSpaceError'
+
+
+class DestinationNodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ super(DestinationNodeDoesNotExistError, self).__init__()
+ self.message = 'NotEnoughSpaceError'
+
+
+class NodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ super(NodeDoesNotExistError, self).__init__()
+ self.message = 'NodeDoesNotExistError'
+
+
+class File:
+ def __init__(self, name, content=''):
+ self.content = str(content)
+ self.size = len(self.content) + 1
+ self.name = name
+
+ @property
+ def is_directory(self):
+ return False
+
+ def append(self, text):
+ self.content += text
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+ def __str__(self):
+ return self.content
+
+
+class Directory:
+ def __init__(self, name):
+ self.name = name
+ self.size = 1
+ self.files = []
+ self.directories = []
+ self.nodes = []
+
+ def __str__(self):
+ return self.name
+
+ @property
+ def is_directory(self):
+ return True
+
+ def add(self, obj):
+ self.nodes.append(obj)
+ if type(obj) is File:
+ self.files.append(obj)
+ else:
+ self.directories.append(obj)
+ self.size = len(self.directories) + sum(
+ [file.size for file in self.files]) + 1
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self._directories = {'/': Directory('/')}
+
+ def get_node(self, path):
+ if path in self._directories:
+ return self._directories[path]
+ raise NodeDoesNotExistError
+
+ def create(self, path, directory=False, content=''):
+ path = path[:-1] if path[-1] == '/' else path
+ if self.available_size < 2 or self.available_size < len(content) + 1:
+ raise NotEnoughSpaceError
+ elif path in self._directories:
+ raise DestinationNodeExistsError
+ else:
+ filename = path.split('/')[-1]
+ old_path = '/'.join(
+ [x for x in path.split('/') if x != filename])
+ if old_path == '':
+ old_path = '/'
+ if old_path not in self._directories:
+ raise DestinationNodeDoesNotExistError
+ else:
+ obj = self._directories[old_path]
+
+ new_obj = (
+ Directory(filename)
+ if directory else File(filename, content)
+ )
+
+ self._directories[path] = new_obj
+ obj.add(new_obj)
+ self.available_size -= new_obj.size

Милка обнови решението на 30.04.2015 16:58 (преди почти 9 години)

class FileSystemError(Exception):
def __init__(self):
self.message = 'FileSystemError'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
super(DestinationNodeExistsError, self).__init__()
self.message = 'DestinationNodeExistsError'
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
super(NotEnoughSpaceError, self).__init__()
self.message = 'NotEnoughSpaceError'
class DestinationNodeDoesNotExistError(FileSystemError):
def __init__(self):
super(DestinationNodeDoesNotExistError, self).__init__()
self.message = 'NotEnoughSpaceError'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
super(NodeDoesNotExistError, self).__init__()
self.message = 'NodeDoesNotExistError'
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super(NonExplicitDirectoryDeletionError, self).__init__()
+ self.message = 'NonExplicitDirectoryDeletionError'
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super(NonExplicitDirectoryDeletionError, self).__init__()
+ self.message = 'NonEmptyDirectoryDeletionError'
+
+
class File:
def __init__(self, name, content=''):
self.content = str(content)
self.size = len(self.content) + 1
self.name = name
@property
def is_directory(self):
return False
def append(self, text):
self.content += text
self.size += len(text)
def truncate(self, text):
self.content = text
self.size = len(text) + 1
def __str__(self):
return self.content
class Directory:
def __init__(self, name):
self.name = name
self.size = 1
self.files = []
self.directories = []
self.nodes = []
def __str__(self):
return self.name
@property
def is_directory(self):
return True
def add(self, obj):
self.nodes.append(obj)
if type(obj) is File:
self.files.append(obj)
else:
self.directories.append(obj)
self.size = len(self.directories) + sum(
[file.size for file in self.files]) + 1
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
self._directories = {'/': Directory('/')}
def get_node(self, path):
if path in self._directories:
return self._directories[path]
raise NodeDoesNotExistError
def create(self, path, directory=False, content=''):
path = path[:-1] if path[-1] == '/' else path
if self.available_size < 2 or self.available_size < len(content) + 1:
raise NotEnoughSpaceError
elif path in self._directories:
raise DestinationNodeExistsError
else:
filename = path.split('/')[-1]
old_path = '/'.join(
[x for x in path.split('/') if x != filename])
if old_path == '':
old_path = '/'
if old_path not in self._directories:
raise DestinationNodeDoesNotExistError
else:
obj = self._directories[old_path]
new_obj = (
Directory(filename)
if directory else File(filename, content)
)
self._directories[path] = new_obj
obj.add(new_obj)
- self.available_size -= new_obj.size
+ self.available_size -= new_obj.size
+
+ def remove(self, path, directory=False, force=True):
+ obj = self._directories[path]
+ if type(obj) is Directory and not directory:
+ if force:
+ pass
+ else:
+ raise NonEmptyDirectoryDeletionError
+ elif type(obj) is Directory:
+ raise NonExplicitDirectoryDeletionError
+ del self._directories[path]