Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
67 changes: 67 additions & 0 deletions src/exerciseeight/saveobject.py
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions src/exerciseeight/vehiculo.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/files/path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PATH=src/files/
FILENAME=file.txt
BFILENAME=file.bin
52 changes: 50 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down