diff --git a/README.md b/README.md index fa0237d..3adbf6a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # OpenBootCampPython Repository to store Python boot camp. + +Ejercicio 8.B: En este segundo ejercicio, tendréis que crear un archivo py y dentro crearéis una clase Vehículo, haréis un objeto de ella, lo guardaréis en un archivo y luego lo cargamos. diff --git a/src/exerciseeight/saveobject.py b/src/exerciseeight/saveobject.py new file mode 100644 index 0000000..8bcc100 --- /dev/null +++ b/src/exerciseeight/saveobject.py @@ -0,0 +1,67 @@ +from pickle import dump, load + + +class ObjectManager: + """Conceptual representation for a class manager + :param _path: path pointed to file directory + :type _path: str + :param _filename: binary file name + :type _filename: str + :param _file: binary I/O stream + :type _file: BufferedWriter + :param _objeto: class to manage + :type _objeto: class + """ + _path = None + _filename = None + _file = None + _objeto = None + + def __init__(self, path: str, filename: str, objeto=None): + """Constructor method + """ + self._path = path + self._filename = filename + self._objeto = objeto + self._file = None + + def __del__(self): + """Destructor method + """ + del self._path, self._filename, self._file, self._objeto + + def _openFile(self, mode: str): + """Private method to open a binary file + :param mode: mode to open the binary file + :type mode: str + """ + self._file = open(f'{self._path}{self._filename}', mode) + + def _closeFile(self): + """Private method to close a file + """ + self._file.close() + self._file = None + + def saveObject(self): + """Method to save a class if provided + """ + if self._objeto is None: + pass + else: + self._openFile('wb') + dump(self._objeto, self._file) + self._closeFile() + + def loadObject(self): + """Method to load and return a class from a binary file + :return: loaded class or None if not loaded + :rtype: class + """ + try: + self._openFile('rb') + self._objeto = load(self._file) + self._closeFile() + return self._objeto + except FileNotFoundError: + return None diff --git a/src/exerciseeight/vehiculo.py b/src/exerciseeight/vehiculo.py new file mode 100644 index 0000000..0c2fdad --- /dev/null +++ b/src/exerciseeight/vehiculo.py @@ -0,0 +1,22 @@ +class Vehiculo: + """Conceptual representation of a vehicle + :param color: representation of vehicle color + :type color: str + :param ruedas: number of wheels + :type ruedas: int + :param puertas: number of doors + :type puertas: int + """ + def __init__(self, color='', ruedas=-1, puertas=-1): + """Constructor method + """ + self.color = color + self.ruedas = ruedas + self.puertas = puertas + + def getProperties(self) -> tuple[str, int, int]: + """ + :return: return class attributes + :rtype: tuple[str, int, int] + """ + return self.color, self.ruedas, self.puertas diff --git a/src/files/path.txt b/src/files/path.txt new file mode 100644 index 0000000..24781d2 --- /dev/null +++ b/src/files/path.txt @@ -0,0 +1,3 @@ +PATH=src/files/ +FILENAME=file.txt +BFILENAME=file.bin \ No newline at end of file diff --git a/src/main.py b/src/main.py index a47b6a3..59ae799 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,57 @@ +# LIBRARIES +from src.exerciseeight.saveobject import ObjectManager +from src.exerciseeight.vehiculo import Vehiculo + +# CONSTANT +PATH = 'src/files/path.txt' + + +# FUNCTION +def readPaths() -> tuple[str, str, str]: + """Function to read a file with path.txt data + :return: return path.txt, file name, binary file name + :rtype: tuple[str, str, str] + """ + f = open(PATH, 'r') + paths = f.readlines() + f.close() + split = [] + for path in paths: + split.append(path.split('=')) + bfilename = '' + path = '' + filename = '' + for linea in split: + if linea[0] == 'PATH': + path = linea[1].replace('\n', '') + if linea[0] == 'FILENAME': + filename = linea[1].replace('\n', '') + if linea[0] == 'BFILENAME': + bfilename = linea[1].replace('\n', '') + return path, filename, bfilename + + +# MAIN FUNCTION def main(): - """Main script for pythonBootCamp project + """Main script for exercise eight B :return: None :rtype: NoneType """ - pass + coche = Vehiculo(color='carbon', ruedas=4, puertas=5) + print(coche.getProperties()) + path, _, bfile = readPaths() + obj = ObjectManager(path, bfile, coche) + print('Aparcar coche') + obj.saveObject() + print('Olvidar coche') + del coche, obj + obj = ObjectManager(path, bfile) + print('Recordar coche') + coche = obj.loadObject() + if coche is None: + pass + else: + print(coche.getProperties()) if __name__ == '__main__':