diff --git a/.gitignore b/.gitignore index b86eb24..5e2f459 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,178 @@ .idea/* src/modelo/__pycache__/* src/logica/__pycache__/* +## Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### VisualStudioCode ### +.vscode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# Support for Project snippet scope \ No newline at end of file diff --git a/README.md b/README.md index a183dce..b02adf5 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,26 @@ # Tutorial de SQLAlchemy Para el desarrollo de este tutorial es importante cumplir con los siguientes requisitos: -* Tener Python y pip correctamente instalados y actualizados a una versión reciente. Si aún no los instala, puede descargar las versiones para los diferentes sistemas operativos en https://www.python.org/downloads/ y si es necesario revisar las instrucciones de instalación, se pueden seguir en sitios como https://realpython.com/installing-python/ -* Instalar un IDE. Se recomienda utilizar PyCharm Community Edition (Disponible en: https://www.jetbrains.com/es-es/pycharm/download/) ó Visual Studio Code (Disponible en: https://code.visualstudio.com/download) -* Descargar el código del repositorio https://github.com/MISW-4101-Practicas/TutorialCancionesUnittest -* Iniciar un virtual environment (venv) para separar el uso de librerías de este proyecto de otros proyectos. Si no ha creado un virtual environment, puede seguir las instrucciones del sitio https://docs.python.org/3/library/venv.html -* Instalar SQLAlchemy - -Con estos requisitos ya es posible comenzar con el tutorial. \ No newline at end of file + +- Para Instalar y configurar ambientes virtuales en Python puede usar [virtualenv](https://virtualenv.pypa.io/en/latest/userguide.html) + + ```bash + $ pip install virtualenv + ``` + +- Configuración de ambiente virtual + +```bash +python3 -m venv venv +.\venv\Scripts\activate +pip install -r requirements.txt +deactivate +``` + +- Tener Python y pip correctamente instalados y actualizados a una versión reciente. Si aún no los instala, puede descargar las versiones para los diferentes sistemas operativos en https://www.python.org/downloads/ y si es necesario revisar las instrucciones de instalación, se pueden seguir en sitios como https://realpython.com/installing-python/ +- Instalar un IDE. Se recomienda utilizar PyCharm Community Edition (Disponible en: https://www.jetbrains.com/es-es/pycharm/download/) ó Visual Studio Code (Disponible en: https://code.visualstudio.com/download) +- Descargar el código del repositorio https://github.com/MISW-4101-Practicas/TutorialCancionesUnittest +- Iniciar un virtual environment (venv) para separar el uso de librerías de este proyecto de otros proyectos. Si no ha creado un virtual environment, puede seguir las instrucciones del sitio https://docs.python.org/3/library/venv.html +- Instalar SQLAlchemy + +Con estos requisitos ya es posible comenzar con el tutorial. diff --git a/actualizacion_datos.py b/actualizacion_datos.py new file mode 100644 index 0000000..d9ebddd --- /dev/null +++ b/actualizacion_datos.py @@ -0,0 +1,17 @@ +from src.modelo.cancion import Cancion +from src.modelo.interprete import Interprete +from src.modelo.declarative_base import Session, engine, Base + + +if __name__ == '__main__': + session = Session() + cancion = session.query(Cancion).get(2) + interprete = session.query(Interprete).get(4) + + cancion.minutos = 5 + cancion.segundos = 30 + cancion.compositor = "Pedro Pérez" + cancion.interpretes.append(interprete) + session.add(cancion) + session.commit() + session.close() \ No newline at end of file diff --git a/aplicacion.sqlite b/aplicacion.sqlite index 62993bb..1041c0a 100644 Binary files a/aplicacion.sqlite and b/aplicacion.sqlite differ diff --git a/borrar_datos.py b/borrar_datos.py new file mode 100644 index 0000000..f156018 --- /dev/null +++ b/borrar_datos.py @@ -0,0 +1,15 @@ +from src.modelo.cancion import Cancion +from src.modelo.interprete import Interprete +from src.modelo.album import Album, Medio +from src.modelo.declarative_base import Session, engine, Base + +if __name__ == '__main__': + session = Session() + cancion2 = session.query(Cancion).get(2) + session.delete(cancion2) + session.commit() + session.close() + + """ En el ejemplo, y según la definición de las clases, se debe tener cuidado con el borrado de intérpretes, dado que, al no estar relacionados a través de la instrucción relationship, + es posible borrarlos y dejar las canciones con una relación a un intérprete inexistente. + Para profundizar en las relaciones se recomienda revisar esta referencia. """ diff --git a/recuperar.py b/recuperar.py new file mode 100644 index 0000000..d4c2253 --- /dev/null +++ b/recuperar.py @@ -0,0 +1,32 @@ +from src.modelo.cancion import Cancion +from src.modelo.interprete import Interprete +from src.modelo.album import Album, Medio +from src.modelo.declarative_base import Session, engine, Base + + +if __name__ == '__main__': + session = Session() + canciones = session.query(Cancion).all() + + print('Las canciones almacenadas son:') + for cancion in canciones: + print("Titulo: " + cancion.titulo + " (00:" + + str(cancion.minutos) + ":" + + str(cancion.segundos) + ")") + + print("Intérpretes") + for interprete in cancion.interpretes: + print(" - " + interprete.nombre) + + for album in cancion.albumes: + print(" -- Presente en el album: " + album.titulo) + + print("") + + + print('Los álbumes almacenados en discos son:') + albumes = session.query(Album).filter(Album.medio == Medio.DISCO).all() + for album in albumes: + print("Album: " + album.titulo) + + session.close() \ No newline at end of file