Решение на In-memory файлова система от Теодора Костова

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

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

Резултати

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

Код

class FileSystemError(Exception):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class Node():
def __init__(self, path, is_directory, file_system):
self.path = path
self.is_directory = is_directory
self.file_system = file_system
def __repr__(self):
return self.path
@property
def size(self):
return 0
class File(Node):
def __init__(self, path, content, file_system):
Node.__init__(self, path, False, file_system)
splitted_path = path.split('/')
self.content = content
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
@property
def size(self):
return len(self.content) + 1
class Directory(Node):
def __init__(self, path, file_system):
Node.__init__(self, path, True, file_system)
self._nodes = []
@property
def nodes(self):
return self._nodes
@property
def directories(self):
return [node for node in self.nodes if node.is_directory == True]
@property
def files(self):
return [node for node in self.nodes if node.is_directory == False]
@property
def size(self):
return 1
class FileSystem():
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.file_system = {'/': Directory('/', self)}
def __str__(self):
s = ''
for k in self.file_system.keys():
s += str(k)
return s
def get_path_to(self, path):
splitted_path = path.split('/')
if '' in splitted_path:
splitted_path.remove('')
splitted_path = splitted_path[:-1]
for i in range(1, len(splitted_path)):
splitted_path[i] = '/' + splitted_path[i]
path_to = '/'
for p in splitted_path:
path_to += p
return path_to
def create(self, path, directory=False, content=''):
#'/a'
path_to = self.get_path_to(path)
if path_to not in self.file_system.keys():
raise DestinationNodeDoesNotExistError()
if path in self.file_system.keys():
raise DestinationNodeExistsError()
new_node = None
if directory == True:
new_node = Directory(path, self)
else:
new_node = File(path, content, self)
self.available_size -= new_node.size
if self.available_size < 0:
raise NotEnoughSpaceError()
self.file_system[path_to].nodes.append(new_node)
self.file_system[path] = new_node
def get_node(self, path):
if path not in self.file_system.keys():
raise NodeDoesNotExistError()
return self.file_system[path]
def recursive_remove(self, path):
if path not in self.file_system.keys():
return
if self.file_system[path].is_directory == False:
self.file_system.pop(path)
self.available_size += self.file_system[path].size
return
for s in self.file_system[path].nodes:
self.recursive_remove(s.path)
if s.path in self.file_system.keys():
self.available_size += 1
self.file_system.pop(s.path)
def create_node(self, node):
content = ''
if not node.is_directory:
content = node.content
try:
self.create(node.path, node.is_directory, content)
except:
pass
def remove(self, path, directory=False, force=True):
node = None
try:
node = self.get_node(path)
except:
raise
path_to = self.get_path_to(path)
if not directory and node.is_directory:
raise NonExplicitDirectoryDeletionError()
if directory and len(node.nodes) > 0 and not force:
raise NonEmptyDirectoryDeletionError()
if directory and force:
self.recursive_remove(path_to)
self.file_system[path_to].nodes.remove(node)
def move(self, source, destination):
source_node = None
try:
source_node = self.get_node(source)
except NodeDoesNotExistError:
raise SourceNodeDoesNotExistError()
destination_node = None
try:
destination_node = self.get_node(destination)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError()
if destination_node.is_directory == False:
raise DestinationNotADirectoryError()
node_name = '/' + source.split('/')[-1]
if node_name in [str(n) for n in destination_node.nodes]:
raise DestinationNodeExistsError()
if destination == '/':
source_node.path = destination + node_name[1:]
else:
source_node.path = destination + node_name
self.create_node(source_node)
self.remove(source, source_node.is_directory)

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

..EEEEE.E.E..EEEEE
======================================================================
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 24.162s

FAILED (errors=12)

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

Теодора обнови решението на 29.04.2015 23:10 (преди над 9 години)

+class FileSystemError(Exception):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class Node():
+
+ def __init__(self, path, is_directory, file_system):
+ self.path = path
+ self.is_directory = is_directory
+ self.file_system = file_system
+
+ def __repr__(self):
+ return self.path
+
+ @property
+ def size(self):
+ return 0
+
+
+class File(Node):
+
+ def __init__(self, path, content, file_system):
+ Node.__init__(self, path, False, file_system)
+ splitted_path = path.split('/')
+ self.content = content
+
+ # def __repr__(self):
+ # return self.content
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ @property
+ def size(self):
+ return len(self.content) + 1
+
+
+class Directory(Node):
+
+ def __init__(self, path, file_system):
+ Node.__init__(self, path, True, file_system)
+
+ @property
+ def directories(self):
+ return [node for node in self.file_system.file_system[self.path] if node.is_directory]
+
+ @property
+ def files(self):
+ return [node for node in self.file_system.file_system[self.path] if not node.is_directory]
+
+ @property
+ def nodes(self):
+ return self.file_system.file_system[self.path]
+
+ @property
+ def size(self):
+ return 1
+
+
+class FileSystem():
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.file_system = {'/': []}
+
+ # MNOGO, MNOGO GROZNO, UJASNO
+ def get_path_to(self, path):
+ splitted_path = path.split('/')
+ if '' in splitted_path:
+ splitted_path.remove('')
+ splitted_path = splitted_path[:-1]
+ for i in range(1, len(splitted_path)):
+ splitted_path[i] = '/' + splitted_path[i]
+ path_to = '/'
+ for p in splitted_path:
+ path_to += p
+
+ return path_to
+
+ def create(self, path, directory=False, content=''):
+ path_to = self.get_path_to(path)
+ if path_to not in self.file_system.keys():
+ raise DestinationNodeDoesNotExistError()
+
+ if path in self.file_system.keys():
+ raise DestinationNodeExistsError()
+
+ new_node = None
+ if directory == True:
+ new_node = Directory(path, self)
+ else:
+ new_node = File(path, content, self)
+
+ self.available_size -= new_node.size
+
+ if self.available_size < 0:
+ raise NotEnoughSpaceError()
+
+ self.file_system[path_to].append(new_node)
+ if directory:
+ self.file_system[path] = []
+
+ def get_node(self, path):
+ path_to = self.get_path_to(path)
+
+ if path_to not in self.file_system.keys():
+ raise NodeDoesNotExistError()
+
+ for p in self.file_system[path_to]:
+ if p.path == path:
+ return p
+ if path in self.file_system.keys():
+ return Directory(path, self)
+ raise NodeDoesNotExistError()
+
+ def recursive_remove(self, path):
+ if path not in self.file_system.keys():
+ return
+ for s in self.file_system[path]:
+ self.recursive_remove(s.path)
+ if s.path in self.file_system.keys():
+ self.file_system.pop(s.path)
+
+ def create_node(self, node):
+ content = ''
+ if not node.is_directory:
+ content = node.content
+ try:
+ self.create(node.path, node.is_directory, content)
+ except:
+ pass
+
+ def remove(self, path, directory=False, force=True):
+ node = None
+ try:
+ node = self.get_node(path)
+ except:
+ raise
+ path_to = self.get_path_to(path)
+ if not directory and node.is_directory:
+ raise NonExplicitDirectoryDeletionError()
+ if directory and len(node.nodes) > 0 and not force:
+ raise NonEmptyDirectoryDeletionError()
+ if directory and force:
+ self.recursive_remove(path_to)
+ self.file_system[path_to].remove(node)
+
+ def move(self, source, destination):
+ node = None
+ try:
+ node = self.get_node(source)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError()
+ destination_node = None
+ try:
+ destination_node = self.get_node(destination)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError()
+
+ if destination_node.is_directory == False:
+ raise DestinationNotADirectoryError()
+ if node in self.file_system[destination]:
+ raise DestinationNodeExistsError()
+
+ node_name = source.split('/')[-1]
+ node.path = destination + '/' + node_name
+ self.create_node(node)
+ self.remove(source, node.is_directory)

Теодора обнови решението на 30.04.2015 12:30 (преди над 9 години)

class FileSystemError(Exception):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class Node():
def __init__(self, path, is_directory, file_system):
self.path = path
self.is_directory = is_directory
self.file_system = file_system
def __repr__(self):
return self.path
@property
def size(self):
return 0
class File(Node):
def __init__(self, path, content, file_system):
Node.__init__(self, path, False, file_system)
splitted_path = path.split('/')
self.content = content
# def __repr__(self):
# return self.content
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
@property
def size(self):
return len(self.content) + 1
class Directory(Node):
def __init__(self, path, file_system):
Node.__init__(self, path, True, file_system)
+ self._nodes = []
@property
+ def nodes(self):
+ return self._nodes
+
+ @property
def directories(self):
- return [node for node in self.file_system.file_system[self.path] if node.is_directory]
+ return [node for node in self.nodes if node.is_directory == True]
@property
def files(self):
- return [node for node in self.file_system.file_system[self.path] if not node.is_directory]
+ return [node for node in self.nodes if node.is_directory == False]
@property
- def nodes(self):
- return self.file_system.file_system[self.path]
-
- @property
def size(self):
return 1
class FileSystem():
def __init__(self, size):
self.size = size
self.available_size = size - 1
- self.file_system = {'/': []}
+ self.file_system = {'/': Directory('/', self)}
- # MNOGO, MNOGO GROZNO, UJASNO
def get_path_to(self, path):
splitted_path = path.split('/')
if '' in splitted_path:
splitted_path.remove('')
splitted_path = splitted_path[:-1]
for i in range(1, len(splitted_path)):
splitted_path[i] = '/' + splitted_path[i]
path_to = '/'
for p in splitted_path:
path_to += p
return path_to
def create(self, path, directory=False, content=''):
path_to = self.get_path_to(path)
if path_to not in self.file_system.keys():
raise DestinationNodeDoesNotExistError()
-
if path in self.file_system.keys():
raise DestinationNodeExistsError()
-
new_node = None
if directory == True:
new_node = Directory(path, self)
else:
new_node = File(path, content, self)
-
self.available_size -= new_node.size
-
if self.available_size < 0:
raise NotEnoughSpaceError()
+ self.file_system[path_to].nodes.append(new_node)
+ self.file_system[path] = new_node
- self.file_system[path_to].append(new_node)
- if directory:
- self.file_system[path] = []
-
def get_node(self, path):
- path_to = self.get_path_to(path)
-
- if path_to not in self.file_system.keys():
+ if path not in self.file_system.keys():
raise NodeDoesNotExistError()
+ return self.file_system[path]
- for p in self.file_system[path_to]:
- if p.path == path:
- return p
- if path in self.file_system.keys():
- return Directory(path, self)
- raise NodeDoesNotExistError()
-
def recursive_remove(self, path):
if path not in self.file_system.keys():
return
- for s in self.file_system[path]:
+ if self.file_system[path].is_directory == False:
+ self.file_system.pop(path)
+ return
+ for s in self.file_system[path].nodes:
self.recursive_remove(s.path)
if s.path in self.file_system.keys():
self.file_system.pop(s.path)
def create_node(self, node):
content = ''
if not node.is_directory:
content = node.content
try:
self.create(node.path, node.is_directory, content)
except:
pass
def remove(self, path, directory=False, force=True):
node = None
try:
node = self.get_node(path)
except:
raise
path_to = self.get_path_to(path)
if not directory and node.is_directory:
raise NonExplicitDirectoryDeletionError()
if directory and len(node.nodes) > 0 and not force:
raise NonEmptyDirectoryDeletionError()
if directory and force:
self.recursive_remove(path_to)
- self.file_system[path_to].remove(node)
+ self.file_system[path_to].nodes.remove(node)
def move(self, source, destination):
- node = None
+ source_node = None
try:
- node = self.get_node(source)
+ source_node = self.get_node(source)
except NodeDoesNotExistError:
raise SourceNodeDoesNotExistError()
destination_node = None
try:
destination_node = self.get_node(destination)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError()
if destination_node.is_directory == False:
raise DestinationNotADirectoryError()
- if node in self.file_system[destination]:
+
+ node_name = '/' + source.split('/')[-1]
+ if node_name in [str(n) for n in destination_node.nodes]:
raise DestinationNodeExistsError()
+ if destination == '/':
+ source_node.path = destination + node_name[1:]
+ else:
+ source_node.path = destination + node_name
+ self.create_node(source_node)
+ self.remove(source, source_node.is_directory)
- node_name = source.split('/')[-1]
- node.path = destination + '/' + node_name
- self.create_node(node)
- self.remove(source, node.is_directory)

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

class FileSystemError(Exception):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class Node():
def __init__(self, path, is_directory, file_system):
self.path = path
self.is_directory = is_directory
self.file_system = file_system
def __repr__(self):
return self.path
-
+
@property
def size(self):
return 0
class File(Node):
def __init__(self, path, content, file_system):
Node.__init__(self, path, False, file_system)
splitted_path = path.split('/')
self.content = content
- # def __repr__(self):
- # return self.content
-
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
@property
def size(self):
return len(self.content) + 1
class Directory(Node):
def __init__(self, path, file_system):
Node.__init__(self, path, True, file_system)
self._nodes = []
-
+
@property
def nodes(self):
return self._nodes
@property
def directories(self):
return [node for node in self.nodes if node.is_directory == True]
@property
def files(self):
return [node for node in self.nodes if node.is_directory == False]
@property
def size(self):
return 1
class FileSystem():
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.file_system = {'/': Directory('/', self)}
-
+
+ def __str__(self):
+ s = ''
+ for k in self.file_system.keys():
+ s += str(k)
+
+ return s
+
def get_path_to(self, path):
splitted_path = path.split('/')
if '' in splitted_path:
splitted_path.remove('')
splitted_path = splitted_path[:-1]
for i in range(1, len(splitted_path)):
splitted_path[i] = '/' + splitted_path[i]
path_to = '/'
for p in splitted_path:
path_to += p
return path_to
def create(self, path, directory=False, content=''):
+ #'/a'
path_to = self.get_path_to(path)
if path_to not in self.file_system.keys():
raise DestinationNodeDoesNotExistError()
if path in self.file_system.keys():
raise DestinationNodeExistsError()
new_node = None
if directory == True:
new_node = Directory(path, self)
else:
new_node = File(path, content, self)
self.available_size -= new_node.size
if self.available_size < 0:
raise NotEnoughSpaceError()
self.file_system[path_to].nodes.append(new_node)
self.file_system[path] = new_node
def get_node(self, path):
if path not in self.file_system.keys():
raise NodeDoesNotExistError()
+
return self.file_system[path]
def recursive_remove(self, path):
if path not in self.file_system.keys():
return
if self.file_system[path].is_directory == False:
self.file_system.pop(path)
+ self.available_size += self.file_system[path].size
return
for s in self.file_system[path].nodes:
self.recursive_remove(s.path)
if s.path in self.file_system.keys():
+ self.available_size += 1
self.file_system.pop(s.path)
def create_node(self, node):
content = ''
if not node.is_directory:
content = node.content
try:
self.create(node.path, node.is_directory, content)
except:
pass
def remove(self, path, directory=False, force=True):
node = None
try:
node = self.get_node(path)
except:
raise
path_to = self.get_path_to(path)
if not directory and node.is_directory:
raise NonExplicitDirectoryDeletionError()
if directory and len(node.nodes) > 0 and not force:
raise NonEmptyDirectoryDeletionError()
if directory and force:
self.recursive_remove(path_to)
self.file_system[path_to].nodes.remove(node)
def move(self, source, destination):
source_node = None
try:
source_node = self.get_node(source)
except NodeDoesNotExistError:
raise SourceNodeDoesNotExistError()
destination_node = None
try:
destination_node = self.get_node(destination)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError()
if destination_node.is_directory == False:
raise DestinationNotADirectoryError()
node_name = '/' + source.split('/')[-1]
if node_name in [str(n) for n in destination_node.nodes]:
raise DestinationNodeExistsError()
if destination == '/':
source_node.path = destination + node_name[1:]
else:
source_node.path = destination + node_name
self.create_node(source_node)
self.remove(source, source_node.is_directory)
-