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

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

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

Резултати

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

Код

class FileSystemError(Exception):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class File:
def __init__(self, name='',content=''):
self.name = name
self.content = content
def size(self):
return len(self.content) + 1
def append(self, text):
self.content += text
def truncate(self, text):
del self.content
self.content = text
class Directory:
directories =[]
files = []
nodes = []
def __init__(self, name='/'):
self.name = name
def add_file(self, name, content=''):
f = File(name, content)
self.nodes.append(f)
self.files.append(f)
def add_dir(self, name):
d = Directory(name)
self.nodes.append(d)
self.directories.append(d)
class FileSystem:
available_size = 0
def __init__(self, size):
self.size = size
self.available_size = self.size - 1
self.root = Directory()
self.root.add_dir('/')
def get_node(self, path):
names = []
names_of_files = []
names_of_dirs = []
for x in self.root.nodes:
names.append(x.name)
for y in self.root.files:
names_of_files.append(y.name)
for z in self.root.directories:
names_of_dirs.append(z.name)
if path not in names:
raise NodeDoesNotExistError
if path in names_of_dirs:
for directorie in self.root.directories:
if directorie.name == path:
return directorie
else:
for f in self.root.files:
if f.name == path:
return f
def create(self, path, directory=False, content=''):
if path.count('/') == 1:
a = self.get_node(path[:path.rfind('/') + 1])
else:
a = self.get_node(path[:path.rfind('/')])
if path.count('/') != len(a.nodes):
raise DestinationNodeExistsError
names = []
for x in self.root.nodes:
names.append(x.name)
if path.count('/') == 1:
if path[:path.rfind('/') + 1] not in names:
raise DestinationNodeDoesNotExistError
else:
if path[:path.rfind('/')] not in names:
raise DestinationNodeDoesNotExistError
if content != '':
if self.available_size < len(content) + 1:
raise NotEnoughSpaceError
self.root.add_file(path, content)
self.available_size -= len(content) + 1
else:
if self.available_size < 1:
raise DestinationNodeDoesNotExistError
self.root.add_dir(path)
self.available_size -= 1

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

EEEEEEE.EEEE.EEEEE
======================================================================
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_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 32.133s

FAILED (errors=16)

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

Цветан обнови решението на 30.04.2015 16:37 (преди над 9 години)

+class FileSystemError(Exception):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class File:
+ def __init__(self, name='',content=''):
+ self.name = name
+ self.content = content
+
+
+ def size(self):
+ return len(self.content) + 1
+
+
+ def append(self, text):
+ self.content += text
+
+
+ def truncate(self, text):
+ del self.content
+ self.content = text
+
+
+class Directory:
+ directories =[]
+ files = []
+ nodes = []
+ def __init__(self, name='/'):
+ self.name = name
+
+
+ def add_file(self, name, content=''):
+ f = File(name, content)
+ self.nodes.append(f)
+ self.files.append(f)
+
+
+ def add_dir(self, name):
+ d = Directory(name)
+ self.nodes.append(d)
+ self.directories.append(d)
+
+
+class FileSystem:
+ available_size = 0
+ def __init__(self, size):
+ self.size = size
+ self.available_size = self.size - 1
+ self.root = Directory()
+ self.root.add_dir('/')
+
+
+ def get_node(self, path):
+ names = []
+ names_of_files = []
+ names_of_dirs = []
+ for x in self.root.nodes:
+ names.append(x.name)
+
+ for y in self.root.files:
+ names_of_files.append(y.name)
+
+ for z in self.root.directories:
+ names_of_dirs.append(z.name)
+
+ if path not in names:
+ raise NodeDoesNotExistError
+
+ if path in names_of_dirs:
+ for directorie in self.root.directories:
+ if directorie.name == path:
+ return directorie
+
+ else:
+ for f in self.root.files:
+ if f.name == path:
+ return f
+
+
+ def create(self, path, directory=False, content=''):
+ if path.count('/') == 1:
+ a = self.get_node(path[:path.rfind('/') + 1])
+ else:
+ a = self.get_node(path[:path.rfind('/')])
+
+ if path.count('/') != len(a.nodes):
+ raise DestinationNodeExistsError
+ names = []
+ for x in self.root.nodes:
+ names.append(x.name)
+
+ if path.count('/') == 1:
+ if path[:path.rfind('/') + 1] not in names:
+ raise DestinationNodeDoesNotExistError
+ else:
+ if path[:path.rfind('/')] not in names:
+ raise DestinationNodeDoesNotExistError
+
+ if content != '':
+ if self.available_size < len(content) + 1:
+ raise NotEnoughSpaceError
+ self.root.add_file(path, content)
+ self.available_size -= len(content) + 1
+
+ else:
+ if self.available_size < 1:
+ raise DestinationNodeDoesNotExistError
+ self.root.add_dir(path)
+ self.available_size -= 1