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

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

Към профила на Светлин Василев

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 5 успешни тест(а)
  • 13 неуспешни тест(а)

Код

class File:
def __init__(self, name, content):
self.name = name
self.content = content
self.text_size = len(content) + 1
self.is_directory = False
def append(self, text):
self.content += text
self.text_size += len(text)
def truncate(self, text):
self.content = str(self.text_size)
class Directory:
def __init__(self, name):
self.name = name
self.directories = []
self.files = []
self.nodes = []
self.is_directory = True
class FileSystem:
def __init__(self, size, path=''):
self.size = size
self.available_size = size - 1
self.root = Directory(path)
def get_node(self, path):
parts = path.split('/')
name = parts[-1]
searcher = self.root
if name is searcher.name:
return searcher
for part in parts:
for element in searcher.directories:
if name == element.name:
return element
if part == element.name:
searcher = element
break
for element in searcher.files:
if name == element.name:
return element
def create(self, path, directory=False, content=''):
name = path.split('/')[-1]
if not self.get_node(path[:path.rfind('/')]):
raise DestinationNodeDoesNotExistError
current = self.get_node(path[:path.rfind('/')])
for element in current.nodes:
if name == element.name:
raise DestinationNodeExistsError
if directory is False:
if self.available_size < len(content) + 1:
raise NotEnoughSpaceError
else:
current.files.append(File(name, content))
current.nodes.append(self.get_node(path))
self.available_size -= len(content) + 1
else:
if self.available_size < 1:
raise NotEnoughSpaceError
else:
current.directories.append(Directory(name))
current.nodes.append(self.get_node(path))
self.available_size -= 1
def remove(self, path, directory=False, force=True):
name = path.split('/')[-1]
current = self.get_node(path[:path.rfind('/')])
if not any(name == element.name for element in current.nodes):
raise NodeDoesNotExistError
if directory is False:
if type(self.get_node(path)) is Directory:
raise NonexcplicitDirectoryDeletionError()
for element in current.files:
if name == element.name:
self.available_size += element.text_size
current.files.remove(element)
current.nodes.remove(element)
break
else:
raise NodeDoesNotExistError()
if directory is True:
if force is False:
if self.get_node(path).nodes:
raise NonEmptyDirectoryDeletionError()
else:
current.nodes.remove(self.get_node(path))
current.directories.remove(self.get_node(path))
self.available_size += 1
else:
self.available_size += self.get_size(self.get_node(path), 0)+1
current.nodes.remove(self.get_node(path))
current.directories.remove(self.get_node(path))
def get_size(self, current, counter):
def helper(path, help_counter):
last_files = 0
for element in path.files:
help_counter += element.text_size
last_files += element.text_size
for element in path.directories:
help_counter += 1
return help_counter + helper(element, help_counter)
return last_files
return counter + helper(current, counter)
def move(self, source, destination):
current_source = self.get_node(source[:source.rfind('/')])
current_destination = self.get_node(destination)
previous_destination = self.get_node(destination[:source.rfind('/')])
source_name = source.split('/')[-1]
destination_name = destination.split('/')[-1]
if type(self.get_node(destination)) is not Directory:
raise DestinationNotADirectoryError
if not any(source_name == element.name
for element in current_source.nodes):
raise NodeDoesNotExistError
if not any(destination_name == element.name
for element in previous_destination.directories):
raise DestinationNodeDoesNotExistError
for element in current_destination.nodes:
if source_name == element.name:
raise DestinationNodeExistsError
if type(self.get_node(source)) is File:
current_destination.files.append(self.get_node(source))
current_destination.nodes.append(self.get_node(source))
current_source.nodes.remove(self.get_node(source))
current_source.files.remove(self.get_node(source))
else:
current_destination.directories.append(self.get_node(source))
current_destination.nodes.append(self.get_node(source))
current_source.nodes.remove(self.get_node(source))
current_source.directories.remove(self.get_node(source))
class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NonexcplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass

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

.EEEEEE.E.E..EEEEE
======================================================================
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_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 26.177s

FAILED (errors=13)

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

Светлин обнови решението на 30.04.2015 15:43 (преди над 9 години)

+class File:
+ def __init__(self, name, content):
+ self.name = name
+ self.content = content
+ self.text_size = len(content) + 1
+ self.is_directory = False
+
+ def append(self, text):
+ self.content += text
+ self.text_size += len(text)
+
+ def truncate(self, text):
+ self.content = str(self.text_size)
+
+
+class Directory:
+ def __init__(self, name):
+ self.name = name
+ self.directories = []
+ self.files = []
+ self.nodes = []
+ self.is_directory = True
+
+
+class FileSystem:
+ def __init__(self, size, path=''):
+ self.size = size
+ self.available_size = size - 1
+ self.root = Directory(path)
+
+ def get_node(self, path):
+ parts = path.split('/')
+ name = parts[-1]
+ searcher = self.root
+ if name is searcher.name:
+ return searcher
+ for part in parts:
+ for element in searcher.directories:
+ if name == element.name:
+ return element
+ if part == element.name:
+ searcher = element
+ break
+ for element in searcher.files:
+ if name == element.name:
+ return element
+
+ def create(self, path, directory=False, content=''):
+ name = path.split('/')[-1]
+ if not self.get_node(path[:path.rfind('/')]):
+ raise DestinationNodeDoesNotExistError
+ current = self.get_node(path[:path.rfind('/')])
+ for element in current.nodes:
+ if name == element.name:
+ raise DestinationNodeExistsError
+ if directory is False:
+ if self.available_size < len(content) + 1:
+ raise NotEnoughSpaceError
+ else:
+ current.files.append(File(name, content))
+ current.nodes.append(self.get_node(path))
+ self.available_size -= len(content) + 1
+ else:
+ if self.available_size < 1:
+ raise NotEnoughSpaceError
+ else:
+ current.directories.append(Directory(name))
+ current.nodes.append(self.get_node(path))
+ self.available_size -= 1
+
+ def remove(self, path, directory=False, force=True):
+ name = path.split('/')[-1]
+ current = self.get_node(path[:path.rfind('/')])
+ if not any(name == element.name for element in current.nodes):
+ raise NodeDoesNotExistError
+ if directory is False:
+ if type(self.get_node(path)) is Directory:
+ raise NonexcplicitDirectoryDeletionError()
+ for element in current.files:
+ if name == element.name:
+ self.available_size += element.text_size
+ current.files.remove(element)
+ current.nodes.remove(element)
+ break
+ else:
+ raise NodeDoesNotExistError()
+ if directory is True:
+ if force is False:
+ if self.get_node(path).nodes:
+ raise NonEmptyDirectoryDeletionError()
+ else:
+ current.nodes.remove(self.get_node(path))
+ current.directories.remove(self.get_node(path))
+ self.available_size += 1
+ else:
+ self.available_size += self.get_size(self.get_node(path), 0)+1
+ current.nodes.remove(self.get_node(path))
+ current.directories.remove(self.get_node(path))
+
+ def get_size(self, current, counter):
+ def helper(path, help_counter):
+ last_files = 0
+ for element in path.files:
+ help_counter += element.text_size
+ last_files += element.text_size
+ for element in path.directories:
+ help_counter += 1
+ return help_counter + helper(element, help_counter)
+ return last_files
+ return counter + helper(current, counter)
+
+ def move(self, source, destination):
+ current_source = self.get_node(source[:source.rfind('/')])
+ current_destination = self.get_node(destination)
+ previous_destination = self.get_node(destination[:source.rfind('/')])
+ source_name = source.split('/')[-1]
+ destination_name = destination.split('/')[-1]
+ if type(self.get_node(destination)) is not Directory:
+ raise DestinationNotADirectoryError
+ if not any(source_name == element.name
+ for element in current_source.nodes):
+ raise NodeDoesNotExistError
+ if not any(destination_name == element.name
+ for element in previous_destination.directories):
+ raise DestinationNodeDoesNotExistError
+ for element in current_destination.nodes:
+ if source_name == element.name:
+ raise DestinationNodeExistsError
+ if type(self.get_node(source)) is File:
+ current_destination.files.append(self.get_node(source))
+ current_destination.nodes.append(self.get_node(source))
+ current_source.nodes.remove(self.get_node(source))
+ current_source.files.remove(self.get_node(source))
+ else:
+ current_destination.directories.append(self.get_node(source))
+ current_destination.nodes.append(self.get_node(source))
+ current_source.nodes.remove(self.get_node(source))
+ current_source.directories.remove(self.get_node(source))
+
+
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonexcplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass