Решение на In-memory файлова система от Калоян Евтимов

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

Към профила на Калоян Евтимов

Резултати

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

Код

class File:
def __init__(self, name, content):
self.__name = name
self.__content = content
def __str__(self):
return self.__name
def __repr__(self):
return self.__str__()
def __eq__(self, other):
return str(self) == str(other)
def is_directory(self):
return False
def content(self):
return self.__content
def size(content):
return 20
class Directory:
def __init__(self, name):
self.__name = name
def __eq__(self, other):
return str(self) == str(other)
def __str__(self):
return self.__name
def __repr__(self):
return self.__str__()
def is_directory(self):
return True
def file_splitter(path):
splitted = path.split('/')
file_name = splitted[-1]
if len(splitted) == 2:
return ('/', file_name)
real_path = "/".join(splitted[:-1])
return (real_path, file_name)
class FileSystem:
def __init__(self, size):
self.__size = size
self.__available_size = size - 1
self.__file_s = {'/': []}
def get_dict(self):
return self.__file_s
def size(self):
return self.__size
def available_size(self):
return self.__available_size
def get_node(self, path):
if path in self.__file_s.keys():
real_path, file_name = file_splitter(path)
for curr_file in self.__file_s[real_path]:
if file_name == str(curr_file):
return curr_file
else:
return
def has_path(self, path):
if path in self.__file_s.keys():
return True
else:
return False
def is_empty(self, path):
if len(self.__file_s[path]) == 0:
return True
else:
return False
def create(self, path, directory=False, content=''):
if self.has_path(path):
print("da")
pass
real_path, file_name = file_splitter(path)
if self.has_path(real_path):
if directory:
if self.__available_size >= 1:
self.__file_s[real_path].append(Directory(file_name))
self.__file_s[path] = []
self.__available_size -= 1
else:
return
else:
if self.__available_size >= size(content):
self.__file_s[real_path].append(File(file_name, content))
self.__file_s[path] = ""
self.__available_size -= size(content)
else:
return
else:
print("ne")
return
def remove(self, path, directory=False, force=True):
real_path, file_name = file_splitter(path)
if self.has_path(path):
if self.get_node(path).is_directory():
if directory:
if self.is_empty(path):
del(self.__file_s[path])
self.__file_s[real_path].remove(file_name)
else:
if force:
pass
else:
pass
else:
pass
else:
pass
def has_file(self, destination, source):
pass
def deep_remove(self, source):
del(self.__file_s[source])
keys = self.__file_s.keys()
to_be_removed = []
for elem in keys:
if source == elem[:len(source)]:
to_be_removed.append(elem)
for elem in to_be_removed:
del(self.__file_s[elem])
def deep_copy(self, path, cp_path):
curr_path = path
for elem in self.__file_s[curr_path]:
if elem.is_directory():
self.create(cp_path + '/' + str(elem),
directory=True)
return self.deep_copy(path + '/' + str(elem),
cp_path + str(elem))
else:
self.create(cp_path + '/' + str(elem),
content=elem.content())
def move(self, source, destination):
if self.has_path(source):
if self.has_path(destination):
if self.get_node(destination).is_directory():
if not self.has_file(destination, source):
self.deep_copy(source, destination)
self.deep_remove(source)
else:
pass
else:
pass
else:
pass
else:
pass

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

.EEEEEEEEEEEEEEEEE
======================================================================
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_minimal (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.130s

FAILED (errors=17)

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

Калоян обнови решението на 30.04.2015 16:54 (преди почти 9 години)

+class File:
+ def __init__(self, name, content):
+ self.__name = name
+ self.__content = content
+
+ def __str__(self):
+ return self.__name
+
+ def __repr__(self):
+ return self.__str__()
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def is_directory(self):
+ return False
+
+ def content(self):
+ return self.__content
+
+
+def size(content):
+ return 20
+
+
+class Directory:
+
+ def __init__(self, name):
+ self.__name = name
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return self.__name
+
+ def __repr__(self):
+ return self.__str__()
+
+ def is_directory(self):
+ return True
+
+
+def file_splitter(path):
+ splitted = path.split('/')
+ file_name = splitted[-1]
+ if len(splitted) == 2:
+ return ('/', file_name)
+ real_path = "/".join(splitted[:-1])
+ return (real_path, file_name)
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.__size = size
+ self.__available_size = size - 1
+ self.__file_s = {'/': []}
+
+ def get_dict(self):
+ return self.__file_s
+
+ def size(self):
+ return self.__size
+
+ def available_size(self):
+ return self.__available_size
+
+ def get_node(self, path):
+ if path in self.__file_s.keys():
+ real_path, file_name = file_splitter(path)
+ for curr_file in self.__file_s[real_path]:
+ if file_name == str(curr_file):
+ return curr_file
+ else:
+ return
+
+ def has_path(self, path):
+ if path in self.__file_s.keys():
+ return True
+ else:
+ return False
+
+ def is_empty(self, path):
+ if len(self.__file_s[path]) == 0:
+ return True
+ else:
+ return False
+
+ def create(self, path, directory=False, content=''):
+ if self.has_path(path):
+ print("da")
+ pass
+
+ real_path, file_name = file_splitter(path)
+ if self.has_path(real_path):
+ if directory:
+ if self.__available_size >= 1:
+ self.__file_s[real_path].append(Directory(file_name))
+ self.__file_s[path] = []
+ self.__available_size -= 1
+ else:
+ return
+ else:
+ if self.__available_size >= size(content):
+ self.__file_s[real_path].append(File(file_name, content))
+ self.__file_s[path] = ""
+ self.__available_size -= size(content)
+ else:
+ return
+ else:
+ print("ne")
+ return
+
+ def remove(self, path, directory=False, force=True):
+ real_path, file_name = file_splitter(path)
+ if self.has_path(path):
+ if self.get_node(path).is_directory():
+ if directory:
+ if self.is_empty(path):
+ del(self.__file_s[path])
+ self.__file_s[real_path].remove(file_name)
+ else:
+ if force:
+ pass
+ else:
+ pass
+ else:
+ pass
+ else:
+ pass
+
+ def has_file(self, destination, source):
+ pass
+
+ def deep_remove(self, source):
+ del(self.__file_s[source])
+ keys = self.__file_s.keys()
+ to_be_removed = []
+ for elem in keys:
+ if source == elem[:len(source)]:
+ to_be_removed.append(elem)
+
+ for elem in to_be_removed:
+ del(self.__file_s[elem])
+
+ def deep_copy(self, path, cp_path):
+ curr_path = path
+ for elem in self.__file_s[curr_path]:
+ if elem.is_directory():
+ self.create(cp_path + '/' + str(elem),
+ directory=True)
+ return self.deep_copy(path + '/' + str(elem),
+ cp_path + str(elem))
+ else:
+ self.create(cp_path + '/' + str(elem),
+ content=elem.content())
+
+ def move(self, source, destination):
+ if self.has_path(source):
+ if self.has_path(destination):
+ if self.get_node(destination).is_directory():
+ if not self.has_file(destination, source):
+ self.deep_copy(source, destination)
+ self.deep_remove(source)
+ else:
+ pass
+ else:
+ pass
+ else:
+ pass
+ else:
+ pass