{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyPebs4mQaoK8iUWphpUn3z4", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Aprendiendo a manejar Clases y crear objetos Workbook y WorkSheet" ], "metadata": { "id": "2TimfL67yBZd" } }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6hblQZWhHKFa", "outputId": "77f86cc9-c6a3-48d7-efc9-f1773ebe23c6" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['Hola', 'Que', 'Tal']\n", "['Hola', 'Que', 'Tal']\n", "['Hola', 'Que', 'Tal']\n", "['Hola', 'Que', 'Tal']\n", "['Hola', 'Que', 'Tal']\n", "Tal\n" ] } ], "source": [ "class WorkSheet:\n", "\tdef __init__(self, value1, value2, value3):\n", "\t\tself.vector = [value1, value2, value3];\n", "\n", "myArray=[]\n", "\n", "for i in range(5):\n", "\tmyArray.append(WorkSheet(\"Hola\", \"Que\", \"Tal\"))\n", "\n", "for i in range(5):\n", "\tprint(myArray[i].vector)\n", "\n", "\n", "print(myArray[3].vector[2])" ] }, { "cell_type": "code", "source": [ "class Indexado():\n", " def __init__(self):\n", " self.data = [5,6,7,8,9,10]\n", " def __getitem__(self, item):\n", " if isinstance(item, int):\n", " print(\"index: \", item)\n", " return self.data[item]\n", " else:\n", " print(\"slice: \", item)\n", " return self.data[item]\n", " def __setitem__(self, key, value):\n", " self.data[key] = value\n", "\n", "vector = Indexado()\n", "print(\"primer elemento del vector: \", vector[0])\n", "vector[0] = 50\n", "print(\"los tres primeros elementos del vector: \", vector[0:4])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wR8R6UpKFjuv", "outputId": "fdafc907-9b01-4370-ab1c-ed9c00f650d5" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "index: 0\n", "primer elemento del vector: 5\n", "slice: slice(0, 4, None)\n", "los tres primeros elementos del vector: [50, 6, 7, 8]\n" ] } ] }, { "cell_type": "code", "source": [ "class Coche():\n", " def __init__(self, marchas):\n", " self.marchas = marchas\n", "\n", "mondeo = Coche([\"R\",\"primera\",\"segunda\",\"tercera\",\"directa\"])\n", "mondeo.marchas[4]" ], "metadata": { "id": "gzlyx2AXohh1", "outputId": "1710db56-64ec-4210-816c-3718f6cabe56", "colab": { "base_uri": "https://localhost:8080/", "height": 36 } }, "execution_count": 4, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'directa'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 4 } ] }, { "cell_type": "code", "source": [ "class Persona():\n", " def __init__(self, nombre, apellido):\n", " self.persona = [nombre, apellido]\n", "\n", "juan = Persona('Juan', 'Perez')\n", "#print(juan[0]) # da error: 'Persona' object is not subscriptable\n", "juan.persona[0]" ], "metadata": { "id": "8QNGxgvFrbCY", "outputId": "a0587c36-9e93-4d5d-d6e8-3052fff6e34c", "colab": { "base_uri": "https://localhost:8080/", "height": 36 } }, "execution_count": 8, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Juan'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 8 } ] }, { "cell_type": "code", "source": [ "# Otra versiĆ³n incompleta\n", "\n", "class Hoja:\n", " def __init__(self, nombre_hoja, permutaciones, algoritmo):\n", " self.hoja = [nombre_hoja, permutaciones, algoritmo]\n", "\n", "n = 10\n", "matriz = []\n", "for i in range(n): # i+1 es la hoja en la que estoy\n", " p = permutations(list(range(1, i+2)))\n", " lista_permutaciones = [list(j) for j in p]\n", " algo = \"algoritmo\"\n", " matriz.append(Hoja(\"Sheet\"+str(i+1), lista_permutaciones, algo))\n", " print(matriz[i].hoja)\n", " ws = wb[\"Sheet\"+str(i+1)] # accediendo a la hoja i+1\n", " ws['E10'] = \"Permutaciones\"\n", " for k in range(i):\n", "\n", "#wb = openpyxl.load_workbook('pru.xlsx')\n", "\n", "#for i in range(n):\n", "# ws = wb[matriz[i][0]]\n", "# print(ws)\n", "wb.save('pru.xlsx')" ], "metadata": { "id": "BA_dWIZI459b" }, "execution_count": null, "outputs": [] } ] }