{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "c1bdbec8", "metadata": { "id": "c1bdbec8" }, "source": [ "# Paquetes\n", "* Son los directorios (o carpetas) donde se almacenan los módulos que estén relacionados entre si. \n", "* Sirven para organizar y reutilizar el código de los módulos.\n", "* Se construye creando una carpeta que debe incluir un fichero \\_\\_init\\_\\_.py\n", "* La carpeta para crear nuestro paquete se llamará *calculos* y dentro de ella crearemos el archivo \\_\\_init\\_\\_.py\n", "* La ruta será:\n", " - pyCourse/jupyter/calculos/\\_\\_init\\_\\_.py" ] }, { "cell_type": "code", "execution_count": null, "id": "1d4859bc", "metadata": { "id": "1d4859bc" }, "outputs": [], "source": [ "from calculos.calculos_generales import potencia\n", "\n", "potencia(2, 10)" ] }, { "cell_type": "code", "execution_count": null, "id": "9b099dc0", "metadata": { "id": "9b099dc0" }, "outputs": [], "source": [ "from calculos.calculos_generales import *\n", "\n", "dividir(120, 17)\n", "redondear(120/17, 2)" ] }, { "cell_type": "markdown", "id": "0c312cd6", "metadata": { "id": "0c312cd6" }, "source": [ "De igual forma que hemos creado el módulo calculos_generales.py dentro de la carpeta *calculos* podemos ir creando más módulos y formar en esa carpeta un paquete con módulos que contengan cálculos relacionados.\n", "\n", "El intérprete de Python crea automáticamente una carpeta denominada \\_\\_pycache\\_\\_ que está dentro de la carpeta *calculos*. \n", "\n", "![calclos](https://github.com/financieras/pyCourse/blob/main/jupyter/img/calculos.png?raw=1)" ] }, { "cell_type": "markdown", "id": "951fc73b", "metadata": { "id": "951fc73b" }, "source": [ "## Subpaquetes\n", "Dentro del paquete *calculos* vamos a crear otras carpetas que actuarán como subpaquetes denominadas:\n", "- *basicos*\n", "- *estadisticos*\n", "\n", "Para que estas carpetas sean realmente subpaquete deben contener ambas, dentro, el archivo \\_\\_init\\_\\_.py \n", "![paquetes](https://github.com/financieras/pyCourse/blob/main/jupyter/img/paquetes.png?raw=1)\n", "\n", "* Dentro del subpaquete basicos creamos el archivo operaciones.py\n", "* Dentro del subpaquete estadisticos creamos el archivo centrales.py" ] }, { "cell_type": "markdown", "id": "7860de4c", "metadata": { "id": "7860de4c" }, "source": [ "#### operaciones.py" ] }, { "cell_type": "code", "execution_count": null, "id": "64802b50", "metadata": { "id": "64802b50" }, "outputs": [], "source": [ "def sumar(n1, n2):\n", " print(f\"El resultado de la suma es {n1+n2}\")\n", "def restar(n1, n2):\n", " print(f\"El resultado de la resta es {n1-n2}\")\n", "def multiplicar(n1, n2):\n", " print(f\"El resultado del producto es {n1*n2}\")\n", "def dividir(n1, n2):\n", " print(f\"El resultado de dividir {n1} entre {n2} es {n1/n2}\")" ] }, { "cell_type": "markdown", "id": "71fddd7b", "metadata": { "id": "71fddd7b" }, "source": [ "#### centrales.py" ] }, { "cell_type": "code", "execution_count": null, "id": "9c143dc6", "metadata": { "id": "9c143dc6" }, "outputs": [], "source": [ "def media(*valores): # el parámetro no es una lista sino una serie de valores\n", " total = 0\n", " for k, v in enumerate(valores):\n", " total += v\n", " print(f\"La media es {total/(k+1)}\")\n", "\n", "def moda(lista):\n", " frequency = {}\n", " for value in lista:\n", " frequency[value] = frequency.get(value, 0) + 1\n", " most_frequent = max(frequency.values())\n", " modas = [key for key, value in frequency.items() if value == most_frequent]\n", " return modas" ] }, { "cell_type": "markdown", "id": "31349b45", "metadata": { "id": "31349b45" }, "source": [ "Vamos a usar estos subpaquetes.\n", "\n", "from calculos.basicos.operaciones import dividir\n", "\n", "* calculos → paquete principal\n", "* basicos → subpaquete\n", "* operaciones → modulo\n", "* dividir → función" ] }, { "cell_type": "code", "execution_count": null, "id": "77a5d966", "metadata": { "id": "77a5d966", "outputId": "2fe78dca-fb9d-414a-92b2-c3f3195b3da9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "El resultado de dividir 101 entre 11 es 9.181818181818182\n" ] } ], "source": [ "from calculos.basicos.operaciones import dividir\n", "\n", "dividir(101, 11)" ] }, { "cell_type": "code", "execution_count": null, "id": "58f335cd", "metadata": { "id": "58f335cd", "outputId": "7691af89-ca20-48dc-cae7-7ec6c74a3c22" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "La media es 5.0\n", "[9]\n" ] }, { "data": { "text/plain": [ "['NIKE']" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from calculos.estadisticos.centrales import *\n", "\n", "media(3, 4, 5, 6, 7)\n", "print(moda([3, 4, 5, 6, 7, 7, 9, 9, 9, 10]))\n", "moda(['ADIDAS', 'BESSEL', 'CONVERSE', 'NIKE', 'NEW BALANCE', 'REEBOK', 'VANS', 'VANS', 'NIKE', 'NIKE', 'ADIDAS'])" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.11" }, "colab": { "name": "0170_paquetes.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }