diff --git a/Clase 3.ipynb b/Clase 3.ipynb index a792215..aa1a6b2 100644 --- a/Clase 3.ipynb +++ b/Clase 3.ipynb @@ -58,11 +58,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting cuadratica.py\n" + ] + } + ], "source": [ "%%writefile cuadratica.py \n", "\n", @@ -77,6 +85,26 @@ " " ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%%writefile?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -86,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -104,13 +132,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "cuadratica.raices?" + "cuadratica.raices??" ] }, { @@ -146,7 +174,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(0.0, 0.0)" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cuad.raices(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "metadata": { "collapsed": false }, @@ -166,7 +216,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -178,13 +228,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "raices?" + "raices is cuadratica.raices" ] }, { @@ -204,20 +265,202 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Ejercicios\n", + "#### Ejercicios\n", "\n", "1. Cree un módulo `circunferencia.py` que defina una constante `PI` y una función `area(r)` que calcula el área para una circunferencia de radio `r`. \n", "\n", - "2. Desde una celda de la sesión intereactiva, importe todo el módulo como un alias `circle` y verifique `circle.PI` y `circle.area()`. Luego importe utilizando la forma `from circunferencia import ...` que importe también la función y la constante\n", + "2. Desde una celda de la sesión interactiva, importe todo el módulo como un alias `circle` y verifique `circle.PI` y `circle.area()`. Luego importe utilizando la forma `from circunferencia import ...` que importe también la función y la constante\n", "\n", "3. verifique que circle.area y area son el mismo objeto" ] }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Writing circunferencia.py\n" + ] + } + ], + "source": [ + "%%writefile circunferencia.py\n", + "\n", + "__all__ = ['area', 'PI']\n", + "\n", + "\n", + "\n", + "\n", + "from funciones_especiales import blah\n", + "from math import pi\n", + "PI = pi\n", + "\n", + "def area(r):\n", + " return PI*r**2" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "\n", + "class A:\n", + " PI = 32\n", + " \n", + " def __init__(self, pi=3.14):\n", + " self.pi = pi\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "mi_a = A()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "32" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "A.PI" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3.141592653589793" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import circunferencia as circle\n", + "\n", + "circle.PI" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "314.1592653589793" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "circle.area(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3.141592653589793" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "circle.pi" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "706.8583470577034" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from circunferencia import area\n", + "\n", + "area(15)" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "#### Paquetes: módulos de módulos\n", + "### Paquetes: módulos de módulos\n", "\n", "Cuando tenemos muchos módulos que están relacionados es bueno armar un **paquete**. Un paquete de modulos es un simple directorio con un módulo especial llamado `__init__.py` (que puede estar vacio) y tantos modulos y subpaquetes como queramos. \n", "\n", @@ -235,34 +478,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mkdir: no se puede crear el directorio «paquete»: El archivo ya existe\r\n" + ] + } + ], "source": [ "%mkdir paquete # creamos un directorio \"paquete\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting paquete/__init__.py\n" + ] + } + ], "source": [ - "%%writefile paquete/__init__.py # el archivo __init__.py vacio\n", + "%%writefile paquete/__init__.py \n", + "\n", " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\r\n", + " " + ] + } + ], + "source": [ + "!cat paquete/__init__.py" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting paquete/modulo.py\n" + ] + } + ], "source": [ "%%writefile paquete/modulo.py\n", "\n", @@ -293,7 +581,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": { "collapsed": false }, @@ -307,6 +595,13 @@ "# ![](files/M.bmp)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](files/M.bmp)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -354,22 +649,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Component,Mol fraction\r\n", + "n2,0.46\r\n", + "co2,3.36\r\n", + "c1,62.36\r\n", + "c2,8.9\r\n", + "c3,5.31\r\n", + "c4,3.01\r\n", + "c5,1.5\r\n", + "c6,1.05\r\n", + "c7,2.0\r\n", + "c7+,12.049\r\n" + ] + } + ], "source": [ "%cat data/near_critical_oil.csv" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[['Component', 'Mol fraction'],\n", + " ['n2', '0.46'],\n", + " ['co2', '3.36'],\n", + " ['c1', '62.36'],\n", + " ['c2', '8.9'],\n", + " ['c3', '5.31'],\n", + " ['c4', '3.01'],\n", + " ['c5', '1.5'],\n", + " ['c6', '1.05'],\n", + " ['c7', '2.0'],\n", + " ['c7+', '12.049']]" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import csv\n", "\n", @@ -380,6 +714,17 @@ "critical_oil" ] }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "csv?" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -389,7 +734,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": { "collapsed": true }, @@ -404,11 +749,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Nombre,Peso\r", + "\r\n", + "Juan,92\r", + "\r\n", + "\"La \"\"Mole\"\" Moli\",121\r", + "\r\n", + "Martín,5 kilos de más\r", + "\r\n" + ] + } + ], "source": [ "%cat pesos.csv" ] @@ -449,100 +809,299 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Números aleatorios\n", - "\n", - "Todas las funciones relacionadas a la aleatoriedad están en el módulo `random`. \n", - "\n", - "Ver [documentación](https://docs.python.org/3/library/random.html)" + "### Números aleatorios\n", + "\n", + "Todas las funciones relacionadas a la aleatoriedad están en el módulo `random`. \n", + "\n", + "Ver [documentación](https://docs.python.org/3/library/random.html)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.5155237329761995" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import random\n", + "\n", + "# la función más básica\n", + "random.random() # float aleatorio, 0.0 <= x < 1.0" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "90" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.randrange(100, 10, -5) # análogo a range() devuelve un numero aleatorio de la serie" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "random.choice([0.3, 10, 'A']) # elige un elemento al azar de una secuencia no vacía" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[0.3, 10]" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "import random\n", - "\n", - "# la función más básica\n", - "random.random() # float aleatorio, 0.0 <= x < 1.0" + "l = [0.3, 10, 'A']\n", + "random.sample(l, k=2) # elige k elementos de la poblacion dada " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[7, 4, 2, 3, 6, 8, 1, 9, 0, 5]" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = list(range(10))\n", + "random.shuffle(l) # \"desordena\" una lista (inline)\n", + "l.pop" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, "source": [ - "random.randrange(10) # análogo a range() devuelve un numero aleatorio de la serie" + "También tiene muchas funciones de probabilidad" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['BPF',\n", + " 'LOG4',\n", + " 'NV_MAGICCONST',\n", + " 'RECIP_BPF',\n", + " 'Random',\n", + " 'SG_MAGICCONST',\n", + " 'SystemRandom',\n", + " 'TWOPI',\n", + " '_BuiltinMethodType',\n", + " '_MethodType',\n", + " '_Sequence',\n", + " '_Set',\n", + " '__all__',\n", + " '__builtins__',\n", + " '__cached__',\n", + " '__doc__',\n", + " '__file__',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " '_acos',\n", + " '_ceil',\n", + " '_cos',\n", + " '_e',\n", + " '_exp',\n", + " '_inst',\n", + " '_log',\n", + " '_pi',\n", + " '_random',\n", + " '_sha512',\n", + " '_sin',\n", + " '_sqrt',\n", + " '_test',\n", + " '_test_generator',\n", + " '_urandom',\n", + " '_warn',\n", + " 'betavariate',\n", + " 'choice',\n", + " 'expovariate',\n", + " 'gammavariate',\n", + " 'gauss',\n", + " 'getrandbits',\n", + " 'getstate',\n", + " 'lognormvariate',\n", + " 'normalvariate',\n", + " 'paretovariate',\n", + " 'randint',\n", + " 'random',\n", + " 'randrange',\n", + " 'sample',\n", + " 'seed',\n", + " 'setstate',\n", + " 'shuffle',\n", + " 'triangular',\n", + " 'uniform',\n", + " 'vonmisesvariate',\n", + " 'weibullvariate']" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "random.choice([0.3, 10, 'A']) # elige un elemento al azar de una secuencia no vacía" + "dir(random)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['betavariate',\n", + " 'expovariate',\n", + " 'gammavariate',\n", + " 'lognormvariate',\n", + " 'normalvariate',\n", + " 'paretovariate',\n", + " 'vonmisesvariate',\n", + " 'weibullvariate']" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "random.sample(l, k=3) # elige k elementos de la poblacion dada " + "[method for method in dir(random) if method.endswith('variate')]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "l = list(range(10))\n", - "random.shuffle(l) # \"desordena\" una lista (inline)\n", - "l " + "random.normalvariate??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "También tiene muchas funciones de probabilidad" + "#### Ejercicio\n", + "\n", + "1. Crear un generador de 1000 números aleatorios pertecientes a una curva de probabilidad normal con media 1 y variancia 0.25. \n", + "\n", + "2. Verificar que la media y la variancia son cercanas a las esperadas (Tip: investigar las funciones del módulo `statistics`)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "[method for method in dir(random) if method.endswith('variate')]" + "normal = [random.normalvariate(1, 0.25) for n in range(1000)]\n", + "\n", + "import statistics as stats" ] }, { - "cell_type": "markdown", - "metadata": {}, + "cell_type": "code", + "execution_count": 80, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0.25535015504245945" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#### Ejercicio\n", - "\n", - "1. Crear un generador de 1000 números aleatorios pertecientes a una curva de probabilidad normal con media 1 y variancia 0.25. \n", - "\n", - "2. Verificar que la media y la variancia son cercanas a las esperadas (Tip: investigar las funciones del módulo `statistics`)\n" + "stats.stdev(normal)" ] }, { @@ -556,11 +1115,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "b'\\x80\\x03]q\\x00(X\\x05\\x00\\x00\\x00puedeq\\x01X\\x03\\x00\\x00\\x00serq\\x02}q\\x03(X\\t\\x00\\x00\\x00cualquierq\\x04KdX\\x04\\x00\\x00\\x00casiq\\x05]q\\x06X\\x04\\x00\\x00\\x00cosaq\\x07X\\x01\\x00\\x00\\x00!q\\x08\\x85q\\tue.'" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "información = ['puede', 'ser', {'casi': [], 'cualquier': 100, 'cosa': ('!',)}]\n", "\n", @@ -571,11 +1141,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['puede', 'ser', {'casi': [], 'cosa': ('!',), 'cualquier': 100}]" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# en sentido contrario\n", "pickle.loads(_)" @@ -583,7 +1164,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "metadata": { "collapsed": false }, @@ -596,11 +1177,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['puede', 'ser', {'casi': [], 'cosa': ('!',), 'cualquier': 100}]" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "pickle.load(open('datos.pkl', 'rb'))" ] @@ -614,17 +1206,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "b'\\x80\\x03c__main__\\nA\\nq\\x00)\\x81q\\x01}q\\x02X\\x05\\x00\\x00\\x00valorq\\x03X\\x08\\x00\\x00\\x00un valorq\\x04sb.'" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "class A:\n", " \"una clase que no hace absolutamente nada\"\n", - " pass\n", + " \n", + " def __init__(self, valor):\n", + " self.valor = valor\n", + " \n", "\n", - "a = A()\n", + "a = A('un valor')\n", "\n", "a_serializado = pickle.dumps(a)\n", "a_serializado" @@ -632,26 +1238,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "bytes" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "type(a_serializado) # la serializados es como datos binarios \"crudos\"" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 92, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ "b= pickle.loads(_)" ] }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a is b" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -679,11 +1318,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "Can't get attribute 'A' on ", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mA\u001b[0m \u001b[1;31m# borramos la clase A del espacio de nombres global\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mpickle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma_serializado\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m: Can't get attribute 'A' on " + ] + } + ], "source": [ "del A # borramos la clase A del espacio de nombres global\n", "pickle.loads(a_serializado)" @@ -698,11 +1349,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'[\"puede\", \"ser\", {\"cualquier\": 100, \"casi\": [], \"cosa\": [\"!\"]}]'" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import json\n", "\n", @@ -720,7 +1382,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 98, "metadata": { "collapsed": true }, @@ -739,14 +1401,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 100, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "IPython.utils.text.SList" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "mole = !cat pesos.csv | grep \"Mole\" \n", - "mole" + "type(mole)" ] }, { @@ -760,11 +1433,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import subprocess\n", "\n", @@ -773,11 +1457,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 102, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "b'hola python, soy echo\\n'" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "subprocess.check_output(['echo', 'hola python, soy echo']) # se ejecuta en el \"kernel\" (ver consola)" ] @@ -799,7 +1494,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 104, "metadata": { "collapsed": true }, @@ -812,28 +1507,74 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "OrderedDict es... un diccionario que sí queda ordenado" + "OrderedDict es... un diccionario que sí queda ordenado" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'Dictionary that remembers insertion order'" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "OrderedDict.__doc__ # para http://twitter.com/Obvio" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 12, 'b': 10}" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "OrderedDict.__doc__ # para http://twitter.com/Obvio" + "d = dict()\n", + "d['a'] = 12\n", + "d['b'] = 10\n", + "d" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 115, "metadata": { "collapsed": false, "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('item_1', 1)\n", + "('item_2', '1 millon')\n", + "('item_3', None)\n" + ] + } + ], "source": [ "d = OrderedDict()\n", "d['item_1'] = 1\n", @@ -852,11 +1593,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 105, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2})" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "contador = Counter('abracadabra')\n", "contador" @@ -864,13 +1616,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 108, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('a', 5), ('b', 2)]" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "contador.most_common(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({1: 3, 2: 2, 'hola': 1})" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "contador.most_common(3)" + "Counter([1, 2, 'hola', 1, 1, 2])" ] }, { @@ -882,11 +1667,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 118, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Raices(x1=-0.6180339887498949, x2=1.618033988749895)" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "\n", "Raices = namedtuple('Raices', ['x1', 'x2'])\n", @@ -915,13 +1711,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 120, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1.618033988749895" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "soluciones[0], soluciones[1]" + "soluciones[1]" ] }, { @@ -933,22 +1740,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 121, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "-0.6180339887498949" + ] + }, + "execution_count": 121, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "soluciones.x1" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 122, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1.618033988749895" + ] + }, + "execution_count": 122, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "soluciones.x2" ] @@ -962,11 +1791,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 123, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "can't set attribute", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msoluciones\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mx1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m: can't set attribute" + ] + } + ], "source": [ "soluciones.x1 = 10" ] @@ -986,7 +1827,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 125, "metadata": { "collapsed": true }, @@ -997,7 +1838,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 126, "metadata": { "collapsed": false }, @@ -1010,11 +1851,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 127, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola curso!\n", + "\n", + "\n", + "Los quiero mucho :D\n" + ] + } + ], "source": [ "print(contenido)" ] @@ -1030,7 +1882,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 134, "metadata": { "collapsed": false }, @@ -1039,6 +1891,97 @@ "simbolos = set(\"#$%&\\'()*+,-./:;<=>?@[\\]^_`{|}~\")" ] }, + { + "cell_type": "code", + "execution_count": 135, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "url = 'http://www.lavoz.com.ar'\n", + "with urlopen(url) as response:\n", + " contenido = response.read().decode('utf8') # el decode es para \"decodificar\" bytes crudos como texto unicode" + ] + }, + { + "cell_type": "code", + "execution_count": 139, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'#', 'a'}" + ] + }, + "execution_count": 139, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set('#hola') & set('#dfdfdfa')" + ] + }, + { + "cell_type": "code", + "execution_count": 141, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('comun', 44),\n", + " ('clearfix\"', 40),\n", + " ('sinimagen', 23),\n", + " ('destacada', 19),\n", + " ('página', 14),\n", + " ('formato', 14),\n", + " ('segmentación', 14),\n", + " ('clearfix', 14),\n", + " ('sobre', 10),\n", + " ('galeria', 10)]" + ] + }, + "execution_count": 141, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "palabras = [palabra.lower() for palabra in contenido.split() if len(palabra) > 4 and not set(palabra) & simbolos]\n", + "\n", + "Counter(palabras).most_common(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 142, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "ImportError", + "evalue": "No module named 'pyquery'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mpyquery\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mPyQuery\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mImportError\u001b[0m: No module named 'pyquery'" + ] + } + ], + "source": [ + "from pyquery import PyQuery" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1053,7 +1996,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 145, "metadata": { "collapsed": false }, @@ -1069,14 +2012,14 @@ "source": [ "%%writefile ejemplo_script.py\n", "\n", - "saludo = \"Hola Mundo\"\n", + "saludo = \"Hola Terremoto!\"\n", "\n", "print(saludo)" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 146, "metadata": { "collapsed": false }, @@ -1085,7 +2028,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Hola Mundo\r\n" + "Hola Terremoto!\r\n" ] } ], @@ -1102,7 +2045,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 147, "metadata": { "collapsed": false }, @@ -1111,14 +2054,31 @@ "name": "stdout", "output_type": "stream", "text": [ - "Hola Mundo\n", - "Hola Mundo\n" + "Hola Terremoto!\n" ] } ], "source": [ - "from ejemplo_script import saludo\n", - "print(saludo)" + "from ejemplo_script import saludoº" + ] + }, + { + "cell_type": "code", + "execution_count": 148, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola Terremoto!\n" + ] + } + ], + "source": [ + "sak" ] }, { @@ -1132,7 +2092,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 149, "metadata": { "collapsed": false }, @@ -1141,7 +2101,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Writing ejemplo_script2.py\n" + "Overwriting ejemplo_script2.py\n" ] } ], @@ -1158,25 +2118,32 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 150, "metadata": { "collapsed": false }, "outputs": [ { - "data": { - "text/plain": [ - "'Hola'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "Hola Mundo\r\n" + ] } ], "source": [ - "from ejemplo_script2 import saludo\n", - "saludo[:4]" + "!python ejemplo_script2.py" + ] + }, + { + "cell_type": "code", + "execution_count": 151, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from ejemplo_script2 import saludo" ] }, { @@ -1212,7 +2179,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 166, "metadata": { "collapsed": false }, @@ -1221,7 +2188,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Writing ejemplo_argv.py\n" + "Overwriting ejemplo_argv.py\n" ] } ], @@ -1230,12 +2197,14 @@ "\n", "if __name__ == '__main__':\n", " import sys\n", - " print(sys.argv)" + " print(sys.argv)\n", + " print(sys.argv[2] * int(sys.argv[1]))\n", + " " ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 170, "metadata": { "collapsed": false }, @@ -1244,12 +2213,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "['ejemplo_argv.py', 'parametro', '10', '--allthenight', '--shampein']\r\n" + "['ejemplo_argv.py', '80', '_']\r\n", + "________________________________________________________________________________\r\n" ] } ], "source": [ - "!python ejemplo_argv.py --allthenight --shampein" + "!python ejemplo_argv.py 80 _" ] }, { @@ -1270,7 +2240,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 171, "metadata": { "collapsed": false }, @@ -1303,7 +2273,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 176, "metadata": { "collapsed": false }, @@ -1312,18 +2282,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "usage: prog.py [-h] [--sum] N [N ...]\r\n", - "prog.py: error: the following arguments are required: N\r\n" + "72\r\n" ] } ], "source": [ - "!python3 prog.py" + "!python3 prog.py --sum 10 4 56 2 " ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 177, "metadata": { "collapsed": false }, @@ -1351,7 +2320,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 178, "metadata": { "collapsed": false }, @@ -1366,7 +2335,7 @@ } ], "source": [ - "!python3 prog.py 10 2 --sum 45 " + "!python3 prog.py 10 2 --sum" ] }, {