{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modulos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Las librerias de python se denominan modulos, definen funciones y/o clases que podemos importar en nuestro programa" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Podemos importar definiciones individuales, por ejemplo aqui importamos la funcion ctime del modulo time que convierte en milisegundos desde el epoch unix a una fecha, o la funcion que da el tiempo local en la estructura definida en time" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Thu Feb 12 17:21:16 2009'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from time import ctime\n", "\n", "ctime(1234455676)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "time.struct_time(tm_year=2019, tm_mon=2, tm_mday=5, tm_hour=14, tm_min=20, tm_sec=57, tm_wday=1, tm_yday=36, tm_isdst=0)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from time import localtime\n", "\n", "localtime()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Podemos asignar un alias a lo que importamos" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "time.struct_time(tm_year=2019, tm_mon=2, tm_mday=5, tm_hour=14, tm_min=20, tm_sec=58, tm_wday=1, tm_yday=36, tm_isdst=0)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from time import localtime as lt\n", "lt()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Podemos importar todas las definiciones de un modulo y a partir de ahi podremos usarlas indicando el nombre del modulo al que pertenecen" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sat Jul 10 23:26:53 2010'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "\n", "time.ctime(1278797213)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "O podemos importar todas las definiciones de modulo indicando el nombre que usaremos para acceder a ellas, en este caso importamos la estructura heapq que permite usar las listas como un heap" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 1, 3, 7]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import heapq as hp \n", "\n", "a = []\n", "hp.heappush(a, 1)\n", "hp.heappush(a, 7)\n", "hp.heappush(a, 3)\n", "hp.heappush(a, -1)\n", "\n", "a" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hp.heappop(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python nos permite acceder a la informacion de los modulos a partir de ciertos atributos especiales, **\\__dict__** da toda la informacion disponible" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'__name__': 'time',\n", " '__doc__': 'This module provides various functions to manipulate time values.\\n\\nThere are two standard representations of time. One is the number\\nof seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\\nor a floating point number (to represent fractions of seconds).\\nThe Epoch is system-defined; on Unix, it is generally January 1st, 1970.\\nThe actual value can be retrieved by calling gmtime(0).\\n\\nThe other representation is a tuple of 9 integers giving local time.\\nThe tuple items are:\\n year (including century, e.g. 1998)\\n month (1-12)\\n day (1-31)\\n hours (0-23)\\n minutes (0-59)\\n seconds (0-59)\\n weekday (0-6, Monday is 0)\\n Julian day (day in the year, 1-366)\\n DST (Daylight Savings Time) flag (-1, 0 or 1)\\nIf the DST flag is 0, the time is given in the regular time zone;\\nif it is 1, the time is given in the DST time zone;\\nif it is -1, mktime() should guess based on the date and time.\\n',\n", " '__package__': '',\n", " '__loader__': _frozen_importlib.BuiltinImporter,\n", " '__spec__': ModuleSpec(name='time', loader=, origin='built-in'),\n", " 'time': ,\n", " 'clock': ,\n", " 'clock_gettime': ,\n", " 'clock_settime': ,\n", " 'clock_getres': ,\n", " 'sleep': ,\n", " 'gmtime': ,\n", " 'localtime': ,\n", " 'asctime': ,\n", " 'ctime': ,\n", " 'mktime': ,\n", " 'strftime': ,\n", " 'strptime': ,\n", " 'tzset': ,\n", " 'monotonic': ,\n", " 'process_time': ,\n", " 'perf_counter': ,\n", " 'get_clock_info': ,\n", " 'timezone': -3600,\n", " 'altzone': -7200,\n", " 'daylight': 1,\n", " 'tzname': ('CET', 'CEST'),\n", " 'CLOCK_REALTIME': 0,\n", " 'CLOCK_MONOTONIC': 1,\n", " 'CLOCK_MONOTONIC_RAW': 4,\n", " 'CLOCK_PROCESS_CPUTIME_ID': 2,\n", " 'CLOCK_THREAD_CPUTIME_ID': 3,\n", " '_STRUCT_TM_ITEMS': 11,\n", " 'struct_time': time.struct_time}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time.__dict__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "o solo la documentacion" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This module provides various functions to manipulate time values.\\n\\nThere are two standard representations of time. One is the number\\nof seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\\nor a floating point number (to represent fractions of seconds).\\nThe Epoch is system-defined; on Unix, it is generally January 1st, 1970.\\nThe actual value can be retrieved by calling gmtime(0).\\n\\nThe other representation is a tuple of 9 integers giving local time.\\nThe tuple items are:\\n year (including century, e.g. 1998)\\n month (1-12)\\n day (1-31)\\n hours (0-23)\\n minutes (0-59)\\n seconds (0-59)\\n weekday (0-6, Monday is 0)\\n Julian day (day in the year, 1-366)\\n DST (Daylight Savings Time) flag (-1, 0 or 1)\\nIf the DST flag is 0, the time is given in the regular time zone;\\nif it is 1, the time is given in the DST time zone;\\nif it is -1, mktime() should guess based on the date and time.\\n'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time.__doc__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "El atributo **\\__all__** permite acceder a todas las definiciones del modulo (si esta definida)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['heappush',\n", " 'heappop',\n", " 'heapify',\n", " 'heapreplace',\n", " 'merge',\n", " 'nlargest',\n", " 'nsmallest',\n", " 'heappushpop']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hp.__all__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.6" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "toc_cell": false, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }