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

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

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

Резултати

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

Код

class FileSystemError(Exception):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class Directory:
def __init__(self, name):
self.name = name
self.is_directory = True
self.directories = []
self.files = []
self.nodes = self.directories + self.files
def __iter__(self):
return iter(self.nodes)
class File:
def __init__(self, name, content=''):
self.name = name
self.is_directory = False
self.content = content
self.size = len(self.content) + 1
def content(self):
return self.content
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
def prev_path(path):
sample = ''
for i in path.split('/')[1:-1]:
sample += ('/' + i)
return sample
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = self.size - 1
self.path = Directory('/')
def __validate_existence_of_dir(self, dirs):
size = len(dirs) - 1
print(dirs)
if size is -1:
return True
root = self.path
for i in range(0, size):
current_dir = (r for r in root.directories if r.name == dirs[i])
if not current_dir:
return False
index = root.directories.index(list(current_dir)[0])
root = root.directories[index]
return True
def get_node(self, path):
if path == '' or path not in self.path.name:
raise NodeDoesNotExistError
elif self.path.is_directory:
return Directory(path)
else:
return File(path)
def create(self, path, directory=False, content=''):
paths = list(filter(None, path.split('/')))
if prev_path(path) not in self.path.name:
raise DestinationNodeDoesNotExistError
elif self.available_size - len(content) - 1 < 0:
raise NotEnoughSpaceError
if directory:
root_dir = self.get_node('/'.join(prev_path(path)))
child_dir = Directory(paths[-1])
root_dir.directories.append(child_dir)
self.available_size -= 1
else:
self.path = File(path, content)
self.available_size -= len(content) + 1
def remove(self, path, directory=False, force=True):
pass
def move(self, source, destination):
pass

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

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

FAILED (errors=17)

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

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

+class FileSystemError(Exception):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class Directory:
+ def __init__(self, name):
+ self.name = name
+ self.is_directory = True
+ self.directories = []
+ self.files = []
+ self.nodes = self.directories + self.files
+
+ def __iter__(self):
+ return iter(self.nodes)
+
+
+class File:
+ def __init__(self, name, content=''):
+ self.name = name
+ self.is_directory = False
+ self.content = content
+ self.size = len(self.content) + 1
+
+ def content(self):
+ return self.content
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+
+def prev_path(path):
+ sample = ''
+ for i in path.split('/')[1:-1]:
+ sample += ('/' + i)
+ return sample
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.size = size
+ self.available_size = self.size - 1
+ self.path = Directory('/')
+
+ def __validate_existence_of_dir(self, dirs):
+ size = len(dirs) - 1
+ print(dirs)
+ if size is -1:
+ return True
+ root = self.path
+ for i in range(0, size):
+ current_dir = (r for r in root.directories if r.name == dirs[i])
+ if not current_dir:
+ return False
+ index = root.directories.index(list(current_dir)[0])
+ root = root.directories[index]
+ return True
+
+ def get_node(self, path):
+ if path == '' or path not in self.path.name:
+ raise NodeDoesNotExistError
+ elif self.path.is_directory:
+ return Directory(path)
+ else:
+ return File(path)
+
+ def create(self, path, directory=False, content=''):
+ paths = list(filter(None, path.split('/')))
+ if prev_path(path) not in self.path.name:
+ raise DestinationNodeDoesNotExistError
+ elif self.available_size - len(content) - 1 < 0:
+ raise NotEnoughSpaceError
+ if directory:
+ root_dir = self.get_node('/'.join(prev_path(path)))
+ child_dir = Directory(paths[-1])
+ root_dir.directories.append(child_dir)
+ self.available_size -= 1
+ else:
+ self.path = File(path, content)
+ self.available_size -= len(content) + 1
+
+ def remove(self, path, directory=False, force=True):
+ pass
+
+ def move(self, source, destination):
+ pass