Skip to content

Instantly share code, notes, and snippets.

@guilhermewop
Created January 23, 2017 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermewop/8561e86b40cc3cefb9ddb493c498f376 to your computer and use it in GitHub Desktop.
Save guilhermewop/8561e86b40cc3cefb9ddb493c498f376 to your computer and use it in GitHub Desktop.
filesystem-simulator.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
class SistemaDeArquivo:
def __init__(self, nome, tamanho = 0):
self.nome = nome
self.tamanho = tamanho
def get_nome(self):
return self.nome
def get_tamanho(self):
return self.tamanho
def listar(self):
print ""
print "Nome:", self.get_nome(), "// Tamanho:", self.get_tamanho(), "kb"
print "-------------------"
class Arquivo(SistemaDeArquivo):
pass
class Diretorio(SistemaDeArquivo):
def __init__(self, nome):
SistemaDeArquivo.__init__(self, nome)
self.diretorios = []
self.arquivos = []
def adicionar_diretorio(self, diretorio):
if not isinstance(diretorio, Diretorio):
return False
self.diretorios.append(diretorio)
def adicionar_arquivo(self, arquivo):
if not isinstance(arquivo, Arquivo):
return False
self.arquivos.append(arquivo)
def __calcular_tamanho(self, tamanho = 0):
tamanho_total = self.tamanho if tamanho == 0 else tamanho + self.tamanho
for arquivo in self.arquivos:
tamanho_total = tamanho_total + arquivo.get_tamanho()
for diretorio in self.diretorios:
tamanho_total = diretorio.__calcular_tamanho(tamanho_total)
return tamanho_total
def get_tamanho(self):
return self.__calcular_tamanho();
# testes
diretorio1 = Diretorio("diretorio 1")
diretorio2 = Diretorio("diretorio 2")
diretorio3 = Diretorio("diretorio 3")
diretorio4 = Diretorio("diretorio 4")
arquivo1 = Arquivo("arquivo 1", 5)
arquivo2 = Arquivo("arquivo 2", 10)
arquivo3 = Arquivo("arquivo 3", 15)
arquivo4 = Arquivo("arquivo 4", 20)
diretorio1.adicionar_arquivo(arquivo3)
diretorio1.adicionar_arquivo(arquivo4)
diretorio2.adicionar_arquivo(arquivo1)
diretorio2.adicionar_arquivo(arquivo3)
diretorio3.adicionar_arquivo(arquivo1)
diretorio3.adicionar_arquivo(arquivo2)
diretorio3.adicionar_arquivo(arquivo3)
diretorio3.adicionar_arquivo(arquivo4)
diretorio4.adicionar_arquivo(arquivo1)
diretorio4.adicionar_arquivo(arquivo3)
diretorio1.adicionar_diretorio(diretorio2)
diretorio1.adicionar_diretorio(diretorio3)
diretorio3.adicionar_diretorio(diretorio4)
# diretório 1 contém os diretórios 2, 3 e 4
print diretorio1.listar()
print diretorio2.listar()
print diretorio3.listar()
print diretorio4.listar()
print arquivo1.listar()
print arquivo2.listar()
print arquivo3.listar()
print arquivo4.listar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment