{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python: Brief Tour of the Standard Library\n", "Gonzalo Rios - grios@dim.uchile.cl\n", "\n", "https://docs.python.org/3/library/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## itertools: Functions creating iterators for efficient looping" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:25:17.673736Z", "start_time": "2018-10-02T19:25:17.670161Z" } }, "outputs": [], "source": [ "import itertools as it" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:25:40.790503Z", "start_time": "2018-10-02T19:25:40.781647Z" } }, "outputs": [], "source": [ "it.count(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:25:53.280804Z", "start_time": "2018-10-02T19:25:46.412366Z" }, "scrolled": true }, "outputs": [], "source": [ "for i in it.count(10, 2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:26:32.265081Z", "start_time": "2018-10-02T19:26:27.762536Z" }, "scrolled": true }, "outputs": [], "source": [ "for i in it.cycle(range(5)):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:27:18.633455Z", "start_time": "2018-10-02T19:27:14.657904Z" } }, "outputs": [], "source": [ "for i in it.repeat(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:29:40.138840Z", "start_time": "2018-10-02T19:29:40.130864Z" } }, "outputs": [], "source": [ "i = it.filterfalse(lambda x: (x % 3)%2 == 0, it.count())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:29:47.960648Z", "start_time": "2018-10-02T19:29:47.954922Z" } }, "outputs": [], "source": [ "next(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:29:51.982509Z", "start_time": "2018-10-02T19:29:48.443736Z" }, "scrolled": true }, "outputs": [], "source": [ "for i in it.filterfalse(lambda x: (x % 3)%2 == 0, it.count()) :\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:30:14.281254Z", "start_time": "2018-10-02T19:30:14.275880Z" } }, "outputs": [], "source": [ "data = [x for x in it.accumulate(range(8))]\n", "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:30:51.454503Z", "start_time": "2018-10-02T19:30:51.450883Z" } }, "outputs": [], "source": [ "[(x % 3)%2 != 1 for x in data]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:31:01.763873Z", "start_time": "2018-10-02T19:31:01.759966Z" } }, "outputs": [], "source": [ "[x for x in data if ((x % 3)%2 != 1)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:31:12.110785Z", "start_time": "2018-10-02T19:31:12.106735Z" } }, "outputs": [], "source": [ "[x // 2 % 11 != 10 for x in data]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:31:18.647834Z", "start_time": "2018-10-02T19:31:18.638879Z" } }, "outputs": [], "source": [ "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:32:25.230000Z", "start_time": "2018-10-02T19:32:25.224351Z" } }, "outputs": [], "source": [ "list(it.takewhile(lambda x: x // 2 % 11 != 10, data))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:32:39.197758Z", "start_time": "2018-10-02T19:32:39.189128Z" } }, "outputs": [], "source": [ "for i in it.dropwhile(lambda x: x // 2 % 11 != 10, data):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:34:30.680278Z", "start_time": "2018-10-02T19:34:30.676518Z" } }, "outputs": [], "source": [ "list(it.chain(range(8), range(2), range(6), range(3), range(4)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:34:59.498337Z", "start_time": "2018-10-02T19:34:59.492073Z" } }, "outputs": [], "source": [ "list(it.product(range(3), range(4)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:35:32.797928Z", "start_time": "2018-10-02T19:35:32.792745Z" } }, "outputs": [], "source": [ "list(it.islice(range(10), 2, 9, 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:35:53.070385Z", "start_time": "2018-10-02T19:35:53.065597Z" } }, "outputs": [], "source": [ "it.tee?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:36:37.015565Z", "start_time": "2018-10-02T19:36:37.012485Z" } }, "outputs": [], "source": [ "tee0, tee1 = it.tee( it.count(2) ) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:37:15.086838Z", "start_time": "2018-10-02T19:37:15.083759Z" } }, "outputs": [], "source": [ "print(next(tee0), next(tee0), next(tee0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:37:12.241920Z", "start_time": "2018-10-02T19:37:12.234448Z" } }, "outputs": [], "source": [ "print(next(tee1), next(tee1), next(tee1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:38:53.023725Z", "start_time": "2018-10-02T19:38:53.014903Z" } }, "outputs": [], "source": [ "for i in it.starmap(lambda x, y: (x, y, x + y), \n", " [(\"Python\", \"Lake\"), (\"Adil\", \"Waseem\")\n", " , (1,2)]):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:40:25.623334Z", "start_time": "2018-10-02T19:40:25.612234Z" } }, "outputs": [], "source": [ "list(it.compress(range(8), \n", " [True, True, False, False, True, False, True, True]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:40:43.303338Z", "start_time": "2018-10-02T19:40:43.296700Z" }, "scrolled": true }, "outputs": [], "source": [ "list(it.permutations(range(4), 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:44:01.295149Z", "start_time": "2018-10-02T19:44:01.291416Z" } }, "outputs": [], "source": [ "set([frozenset(i) for i in it.combinations(range(4), 3)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:41:41.986162Z", "start_time": "2018-10-02T19:41:41.979485Z" }, "scrolled": true }, "outputs": [], "source": [ "list(it.combinations_with_replacement(range(4), 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:46:11.333994Z", "start_time": "2018-10-02T19:46:11.330028Z" } }, "outputs": [], "source": [ "city_list = [('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL'),\n", " ('Anchorage', 'AK'), ('Nome', 'AK'),\n", " ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ')]\n", "\n", "for state, city_iter in it.groupby(city_list, lambda s: s[1]):\n", " for c in city_iter:\n", " print(state, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## functools: Higher-order functions and operations on callable objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:47:04.086374Z", "start_time": "2018-10-02T19:47:04.084205Z" } }, "outputs": [], "source": [ "import functools, operator" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:47:35.011418Z", "start_time": "2018-10-02T19:47:35.005645Z" } }, "outputs": [], "source": [ "functools.reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:48:30.530695Z", "start_time": "2018-10-02T19:48:30.518695Z" } }, "outputs": [], "source": [ "functools.reduce(operator.concat, ['A', 'BB', 'C'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:48:52.606040Z", "start_time": "2018-10-02T19:48:52.594358Z" } }, "outputs": [], "source": [ "functools.reduce(operator.mul, [1, 2, 3], 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:49:59.004485Z", "start_time": "2018-10-02T19:49:59.000520Z" } }, "outputs": [], "source": [ "functools.reduce(operator.mul, [1, 2, 3], 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:50:09.111285Z", "start_time": "2018-10-02T19:50:09.105530Z" } }, "outputs": [], "source": [ "functools.reduce(operator.mul, [], 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:50:14.174692Z", "start_time": "2018-10-02T19:50:14.161817Z" } }, "outputs": [], "source": [ "functools.reduce(operator.add, [1, 2, 3, 4], 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:50:17.383824Z", "start_time": "2018-10-02T19:50:17.371820Z" } }, "outputs": [], "source": [ "functools.reduce(operator.add, [1, 2, 3, 4], 7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:53:03.178402Z", "start_time": "2018-10-02T19:53:03.174611Z" } }, "outputs": [], "source": [ "basetwo = functools.partial(lambda x, y: x * y, y=2)\n", "basetwo" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:53:37.105717Z", "start_time": "2018-10-02T19:53:37.102499Z" } }, "outputs": [], "source": [ "basetwo('10010')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:53:37.581155Z", "start_time": "2018-10-02T19:53:37.575070Z" } }, "outputs": [], "source": [ "basetwo = functools.partial(int, base=2)\n", "basetwo('10010')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:54:58.477771Z", "start_time": "2018-10-02T19:54:58.475043Z" } }, "outputs": [], "source": [ "from functools import singledispatch\n", "\n", "@singledispatch\n", "def fun(arg, verbose=False):\n", " if verbose:\n", " print(\"Let me just say,\", end=\" \")\n", " print(arg)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:54:58.856694Z", "start_time": "2018-10-02T19:54:58.852640Z" } }, "outputs": [], "source": [ "fun('bye!', False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:54:59.411915Z", "start_time": "2018-10-02T19:54:59.406071Z" } }, "outputs": [], "source": [ "fun('bye!', True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:55:04.725460Z", "start_time": "2018-10-02T19:55:04.722410Z" } }, "outputs": [], "source": [ "fun(3, True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:55:13.352400Z", "start_time": "2018-10-02T19:55:13.350016Z" } }, "outputs": [], "source": [ "fun([1,2,3], True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:56:18.916342Z", "start_time": "2018-10-02T19:56:18.911942Z" } }, "outputs": [], "source": [ "@fun.register(int)\n", "def _(arg, verbose=False):\n", " if verbose:\n", " print(\"Strength in numbers, eh?\", end=\" \")\n", " print(arg)\n", "\n", "@fun.register(list)\n", "def _(arg, verbose=False):\n", " if verbose:\n", " print(\"Enumerate this:\")\n", " for i, elem in enumerate(arg):\n", " print(i, elem)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:56:29.476692Z", "start_time": "2018-10-02T19:56:29.473935Z" } }, "outputs": [], "source": [ "fun('bye!', True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:56:34.787568Z", "start_time": "2018-10-02T19:56:34.784517Z" } }, "outputs": [], "source": [ "fun(3, True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:56:36.157122Z", "start_time": "2018-10-02T19:56:36.154457Z" } }, "outputs": [], "source": [ "fun([1,2,3], True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T19:56:39.084865Z", "start_time": "2018-10-02T19:56:39.081839Z" } }, "outputs": [], "source": [ "fun(None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:00:18.890676Z", "start_time": "2018-10-02T20:00:18.887173Z" } }, "outputs": [], "source": [ "def nothing(arg, verbose=False):\n", " print(\"Nothing.\")\n", " \n", "fun.register(type(None), nothing)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:02:50.265787Z", "start_time": "2018-10-02T20:02:50.263092Z" } }, "outputs": [], "source": [ "fun(None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:02:52.933542Z", "start_time": "2018-10-02T20:02:52.925216Z" } }, "outputs": [], "source": [ "fun(1.23)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:02:57.353758Z", "start_time": "2018-10-02T20:02:57.351074Z" } }, "outputs": [], "source": [ "@fun.register(float)\n", "def fun_num(arg, verbose=False):\n", " assert arg != 0.0, \"arg cannot be zero!\"\n", " if verbose:\n", " print(\"Half of your number:\", end=\" \")\n", " print(arg / 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:03:40.798919Z", "start_time": "2018-10-02T20:03:40.796149Z" } }, "outputs": [], "source": [ "fun_num(1.23)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:03:45.163413Z", "start_time": "2018-10-02T20:03:45.155189Z" } }, "outputs": [], "source": [ "fun(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:03:45.885824Z", "start_time": "2018-10-02T20:03:45.878519Z" } }, "outputs": [], "source": [ "fun(1.23)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:03:47.068155Z", "start_time": "2018-10-02T20:03:47.065009Z" } }, "outputs": [], "source": [ "fun_num == fun" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:03:55.029250Z", "start_time": "2018-10-02T20:03:55.023623Z" } }, "outputs": [], "source": [ "fun(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:04:15.281665Z", "start_time": "2018-10-02T20:04:15.271997Z" } }, "outputs": [], "source": [ "fun_num(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:04:15.847803Z", "start_time": "2018-10-02T20:04:15.834749Z" } }, "outputs": [], "source": [ "fun(0.0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:04:31.557236Z", "start_time": "2018-10-02T20:04:31.552817Z" } }, "outputs": [], "source": [ "fun.dispatch(float)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:04:34.552205Z", "start_time": "2018-10-02T20:04:34.548085Z" } }, "outputs": [], "source": [ "fun.dispatch(str)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### collections: Container datatypes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:08:15.741085Z", "start_time": "2018-10-02T20:08:15.736321Z" } }, "outputs": [], "source": [ "from collections import deque\n", "\n", "# d = deque(('g','h','i'))\n", "d = deque('ghi')\n", "d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:08:17.140060Z", "start_time": "2018-10-02T20:08:17.136261Z" } }, "outputs": [], "source": [ "d.append('j')\n", "d.appendleft('f')\n", "d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:08:30.012158Z", "start_time": "2018-10-02T20:08:30.004872Z" } }, "outputs": [], "source": [ "d.pop()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:08:32.197994Z", "start_time": "2018-10-02T20:08:32.195027Z" } }, "outputs": [], "source": [ "d.popleft()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:08:45.669789Z", "start_time": "2018-10-02T20:08:45.666508Z" } }, "outputs": [], "source": [ "list(reversed(d))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:09:22.008474Z", "start_time": "2018-10-02T20:09:22.002816Z" } }, "outputs": [], "source": [ "from collections import namedtuple\n", "\n", "Point = namedtuple('Point', ['x', 'y'])\n", "\n", "pt = Point(11,12)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:09:40.215171Z", "start_time": "2018-10-02T20:09:40.211878Z" } }, "outputs": [], "source": [ "pt[0], pt[1], pt.x, pt.y" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:11:43.056327Z", "start_time": "2018-10-02T20:11:43.051663Z" } }, "outputs": [], "source": [ "from collections import OrderedDict\n", "\n", "od = OrderedDict() #{}\n", "od['a'] = 1\n", "od['b'] = 2\n", "od['c'] = 3\n", "od['d'] = 4\n", "od['e'] = 5\n", "for k,v in od.items():\n", " print(k,v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numeric and Mathematical Modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:12:00.049101Z", "start_time": "2018-10-02T20:12:00.043160Z" } }, "outputs": [], "source": [ "import math, cmath" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:12:12.137918Z", "start_time": "2018-10-02T20:12:12.127034Z" } }, "outputs": [], "source": [ "math.degrees(math.pi)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:13:01.215639Z", "start_time": "2018-10-02T20:13:01.206086Z" } }, "outputs": [], "source": [ "math.gamma(math.erf(math.log1p(math.acosh(math.e))))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:13:22.078847Z", "start_time": "2018-10-02T20:13:22.073218Z" } }, "outputs": [], "source": [ "cmath.exp(1+2j)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:13:27.321661Z", "start_time": "2018-10-02T20:13:27.313750Z" } }, "outputs": [], "source": [ "import decimal, fractions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:13:29.832813Z", "start_time": "2018-10-02T20:13:29.829670Z" } }, "outputs": [], "source": [ "1/7" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:13:40.200559Z", "start_time": "2018-10-02T20:13:40.194974Z" } }, "outputs": [], "source": [ "decimal.Decimal(1/7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:14:14.766723Z", "start_time": "2018-10-02T20:14:14.763275Z" } }, "outputs": [], "source": [ "decimal.Decimal(1) / decimal.Decimal(7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:14:33.312835Z", "start_time": "2018-10-02T20:14:33.307122Z" } }, "outputs": [], "source": [ "decimal.getcontext().prec = 100\n", "decimal.Decimal(1) / decimal.Decimal(7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:14:43.556275Z", "start_time": "2018-10-02T20:14:43.551978Z" } }, "outputs": [], "source": [ "decimal.getcontext().prec = 6\n", "decimal.Decimal(1) / decimal.Decimal(7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:14:47.893919Z", "start_time": "2018-10-02T20:14:47.890296Z" } }, "outputs": [], "source": [ "decimal.Decimal(7)*(decimal.Decimal(1) / decimal.Decimal(7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:15:20.205819Z", "start_time": "2018-10-02T20:15:20.200425Z" } }, "outputs": [], "source": [ "fractions.Fraction(1,7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:15:27.064580Z", "start_time": "2018-10-02T20:15:27.056882Z" } }, "outputs": [], "source": [ "7*fractions.Fraction(1,7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:15:30.346665Z", "start_time": "2018-10-02T20:15:30.343358Z" } }, "outputs": [], "source": [ "float(7*fractions.Fraction(1,7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:15:43.557397Z", "start_time": "2018-10-02T20:15:43.555173Z" } }, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:15:57.061773Z", "start_time": "2018-10-02T20:15:57.057183Z" } }, "outputs": [], "source": [ "random.randint(0,10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:16:13.891228Z", "start_time": "2018-10-02T20:16:13.881162Z" } }, "outputs": [], "source": [ "[random.randint(0,1) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:16:25.115284Z", "start_time": "2018-10-02T20:16:25.105425Z" } }, "outputs": [], "source": [ "[random.randrange(0, 10, 2) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:16:54.176033Z", "start_time": "2018-10-02T20:16:54.172216Z" } }, "outputs": [], "source": [ "[random.choice([0, 'a', []]) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:17:08.570057Z", "start_time": "2018-10-02T20:17:08.566489Z" } }, "outputs": [], "source": [ "a = list(range(10))\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:17:24.151746Z", "start_time": "2018-10-02T20:17:24.142202Z" } }, "outputs": [], "source": [ "random.shuffle(a)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:17:56.922843Z", "start_time": "2018-10-02T20:17:56.917293Z" } }, "outputs": [], "source": [ "random.sample(a, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:19:08.969365Z", "start_time": "2018-10-02T20:19:08.965600Z" } }, "outputs": [], "source": [ "[random.random() for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:19:07.217338Z", "start_time": "2018-10-02T20:19:07.212023Z" } }, "outputs": [], "source": [ "random.seed(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:19:04.497794Z", "start_time": "2018-10-02T20:19:04.492926Z" } }, "outputs": [], "source": [ "[random.random() for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:20:55.576892Z", "start_time": "2018-10-02T20:20:55.574489Z" } }, "outputs": [], "source": [ "state = random.getstate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:21:51.144916Z", "start_time": "2018-10-02T20:21:51.140808Z" } }, "outputs": [], "source": [ "random.randint(1,2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:21:51.286244Z", "start_time": "2018-10-02T20:21:51.148248Z" } }, "outputs": [], "source": [ "state == random.getstate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:22:05.251861Z", "start_time": "2018-10-02T20:22:05.246257Z" } }, "outputs": [], "source": [ "random.setstate(state)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:25:21.341978Z", "start_time": "2018-10-02T20:25:21.338252Z" } }, "outputs": [], "source": [ "[random.uniform(0,100) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:25:37.489098Z", "start_time": "2018-10-02T20:25:37.483663Z" } }, "outputs": [], "source": [ "[random.normalvariate(10,20) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:25:40.261813Z", "start_time": "2018-10-02T20:25:40.257918Z" } }, "outputs": [], "source": [ "[random.expovariate(1) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:26:20.081853Z", "start_time": "2018-10-02T20:26:10.165892Z" } }, "outputs": [], "source": [ "%timeit [random.gauss(10,20) for i in range(10)]\n", "%timeit [random.normalvariate(10,20) for i in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:26:52.249569Z", "start_time": "2018-10-02T20:26:52.237231Z" }, "scrolled": true }, "outputs": [], "source": [ "import statistics\n", "\n", "g = [random.gauss(0,1) for i in range(100)]\n", "g" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:27:01.943212Z", "start_time": "2018-10-02T20:27:01.937355Z" } }, "outputs": [], "source": [ "statistics.mean(g)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:27:03.230689Z", "start_time": "2018-10-02T20:27:03.226355Z" } }, "outputs": [], "source": [ "statistics.variance(g)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:27:05.101852Z", "start_time": "2018-10-02T20:27:05.097627Z" } }, "outputs": [], "source": [ "statistics.stdev(g)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:27:07.475587Z", "start_time": "2018-10-02T20:27:07.471380Z" } }, "outputs": [], "source": [ "statistics.pstdev(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operating System Libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:29:25.465557Z", "start_time": "2018-10-02T20:29:25.459851Z" } }, "outputs": [], "source": [ "import os\n", "\n", "path = os.getcwd()\n", "path" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:29:47.634667Z", "start_time": "2018-10-02T20:29:47.631411Z" } }, "outputs": [], "source": [ "os.path.isfile(path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:29:53.077267Z", "start_time": "2018-10-02T20:29:53.073660Z" } }, "outputs": [], "source": [ "os.path.isdir(path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:30:00.406492Z", "start_time": "2018-10-02T20:30:00.396644Z" } }, "outputs": [], "source": [ "files = os.listdir()\n", "files" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:30:37.510493Z", "start_time": "2018-10-02T20:30:37.504987Z" } }, "outputs": [], "source": [ "os.path.isfile('{}/{}'.format(path, files[0]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:31:33.231985Z", "start_time": "2018-10-02T20:31:33.225743Z" }, "scrolled": true }, "outputs": [], "source": [ "new_dir = path + '/new_dir'\n", "os.makedirs(new_dir, exist_ok=True)\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:32:19.250342Z", "start_time": "2018-10-02T20:32:19.246547Z" } }, "outputs": [], "source": [ "os.chdir(new_dir)\n", "os.getcwd()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:32:31.316996Z", "start_time": "2018-10-02T20:32:31.308989Z" } }, "outputs": [], "source": [ "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:32:58.937846Z", "start_time": "2018-10-02T20:32:58.928272Z" } }, "outputs": [], "source": [ "os.system('touch test1.py test2.py')\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:33:11.150094Z", "start_time": "2018-10-02T20:33:11.144533Z" } }, "outputs": [], "source": [ "os.rename('test1.py', 'renamed.py')\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:33:19.178416Z", "start_time": "2018-10-02T20:33:19.172549Z" } }, "outputs": [], "source": [ "os.remove('renamed.py')\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:33:39.533014Z", "start_time": "2018-10-02T20:33:39.527276Z" } }, "outputs": [], "source": [ "os.chdir('../')\n", "os.getcwd()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:33:49.692911Z", "start_time": "2018-10-02T20:33:49.689811Z" } }, "outputs": [], "source": [ "try:\n", " os.rmdir(new_dir)\n", "except OSError as error:\n", " print(error)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:34:37.496607Z", "start_time": "2018-10-02T20:34:37.488650Z" } }, "outputs": [], "source": [ "for root, dirs, files in os.walk(new_dir):\n", " for name in files:\n", " print('deleting file: ', name)\n", " os.remove(os.path.join(root, name))\n", " for name in dirs:\n", " print('deleting dir: ', name)\n", " os.rmdir(os.path.join(root, name))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:34:45.397916Z", "start_time": "2018-10-02T20:34:45.388316Z" } }, "outputs": [], "source": [ "os.rmdir(new_dir)\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:35:32.623658Z", "start_time": "2018-10-02T20:35:32.618725Z" } }, "outputs": [], "source": [ "empty_dir = new_dir + '/empty'\n", "os.makedirs(empty_dir, exist_ok=True)\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:35:54.435857Z", "start_time": "2018-10-02T20:35:54.429207Z" } }, "outputs": [], "source": [ "os.rmdir(new_dir)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:36:07.102324Z", "start_time": "2018-10-02T20:36:07.093154Z" } }, "outputs": [], "source": [ "os.removedirs(empty_dir)\n", "os.listdir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:02.731840Z", "start_time": "2018-10-02T20:40:02.729653Z" } }, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:12.249187Z", "start_time": "2018-10-02T20:40:12.245910Z" } }, "outputs": [], "source": [ "sys.platform" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:14.485554Z", "start_time": "2018-10-02T20:40:14.474618Z" } }, "outputs": [], "source": [ "sys.version" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:21.848111Z", "start_time": "2018-10-02T20:40:21.845048Z" } }, "outputs": [], "source": [ "sys.implementation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:29.061768Z", "start_time": "2018-10-02T20:40:29.058357Z" } }, "outputs": [], "source": [ "sys.api_version" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:31.200441Z", "start_time": "2018-10-02T20:40:31.197247Z" } }, "outputs": [], "source": [ "sys.executable" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:50.279573Z", "start_time": "2018-10-02T20:40:50.274516Z" } }, "outputs": [], "source": [ "sys.exec_prefix" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:40:50.536319Z", "start_time": "2018-10-02T20:40:50.528943Z" } }, "outputs": [], "source": [ "sys.argv" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:41:18.665703Z", "start_time": "2018-10-02T20:41:18.655511Z" }, "scrolled": true }, "outputs": [], "source": [ "sys.path" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:41:28.603548Z", "start_time": "2018-10-02T20:41:28.583151Z" } }, "outputs": [], "source": [ "sys.modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:41:37.943927Z", "start_time": "2018-10-02T20:41:37.936809Z" } }, "outputs": [], "source": [ "sys.int_info" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:41:39.149888Z", "start_time": "2018-10-02T20:41:39.146879Z" } }, "outputs": [], "source": [ "sys.float_info" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:41:46.982276Z", "start_time": "2018-10-02T20:41:46.978723Z" } }, "outputs": [], "source": [ "sys.getdefaultencoding()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:41:50.057483Z", "start_time": "2018-10-02T20:41:50.053370Z" } }, "outputs": [], "source": [ "sys.getsizeof('')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:41:52.975534Z", "start_time": "2018-10-02T20:41:52.965690Z" } }, "outputs": [], "source": [ "sys.getsizeof('123456')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:42:04.999767Z", "start_time": "2018-10-02T20:42:04.994160Z" } }, "outputs": [], "source": [ "sys.stdin, sys.stdout, sys.stderr" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:42:18.151603Z", "start_time": "2018-10-02T20:42:18.142366Z" }, "scrolled": true }, "outputs": [], "source": [ "for k in range(100):\n", " sys.stdout.write('out'+str(k)+' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:42:33.867801Z", "start_time": "2018-10-02T20:42:33.855431Z" } }, "outputs": [], "source": [ "for k in range(100):\n", " sys.stderr.write('error'+str(k)+' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-02T20:42:59.320976Z", "start_time": "2018-10-02T20:42:59.286499Z" } }, "outputs": [], "source": [ "%%bash\n", "python -c \"import sys; sys.stdout.write(sys.stdin.read())\" < filename.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Time and Date libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:24:00.049271Z", "start_time": "2018-10-04T19:24:00.040264Z" } }, "outputs": [], "source": [ "import time\n", "\n", "tic = time.time()\n", "tic" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:24:21.382370Z", "start_time": "2018-10-04T19:24:21.379008Z" } }, "outputs": [], "source": [ "time.time() - tic" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:24:29.521100Z", "start_time": "2018-10-04T19:24:29.517418Z" } }, "outputs": [], "source": [ "time.asctime()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:24:38.196882Z", "start_time": "2018-10-04T19:24:38.193363Z" } }, "outputs": [], "source": [ "time.gmtime()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:24:55.011491Z", "start_time": "2018-10-04T19:24:55.002014Z" } }, "outputs": [], "source": [ "time.tzname" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:25:05.769179Z", "start_time": "2018-10-04T19:25:05.765186Z" } }, "outputs": [], "source": [ "time.timezone, time.timezone/(60*60)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:25:08.075225Z", "start_time": "2018-10-04T19:25:08.065795Z" } }, "outputs": [], "source": [ "time.localtime()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:26:28.750548Z", "start_time": "2018-10-04T19:26:28.743519Z" } }, "outputs": [], "source": [ "time.strptime(\"30 Nov 00\", \"%d %b %y\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:27:03.017432Z", "start_time": "2018-10-04T19:27:03.009254Z" } }, "outputs": [], "source": [ "time.strftime(\"%a, %d %b %Y %H:%M:%S +0000\", time.localtime())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:27:40.224308Z", "start_time": "2018-10-04T19:27:35.212611Z" } }, "outputs": [], "source": [ "for i in range(5):\n", " print(i)\n", " time.sleep(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:27:57.944394Z", "start_time": "2018-10-04T19:27:57.938659Z" } }, "outputs": [], "source": [ "import datetime\n", "\n", "mydate = datetime.date(1985, 9, 28)\n", "mydate" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:28:05.505408Z", "start_time": "2018-10-04T19:28:05.496969Z" } }, "outputs": [], "source": [ "print(mydate)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:28:38.126681Z", "start_time": "2018-10-04T19:28:38.118159Z" } }, "outputs": [], "source": [ "mydate.ctime()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:28:50.660637Z", "start_time": "2018-10-04T19:28:50.653335Z" } }, "outputs": [], "source": [ "mydate.timetuple()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:29:02.139411Z", "start_time": "2018-10-04T19:29:02.133824Z" } }, "outputs": [], "source": [ "mydate.isoweekday()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:29:02.889383Z", "start_time": "2018-10-04T19:29:02.885406Z" } }, "outputs": [], "source": [ "mydate.isocalendar()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:29:23.482401Z", "start_time": "2018-10-04T19:29:23.473071Z" } }, "outputs": [], "source": [ "now = datetime.datetime.now()\n", "now" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:29:23.913964Z", "start_time": "2018-10-04T19:29:23.910515Z" } }, "outputs": [], "source": [ "print(now)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:29:37.013430Z", "start_time": "2018-10-04T19:29:37.008961Z" } }, "outputs": [], "source": [ "now.strftime(\"%Y-%m-%d-%H-%M\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:29:53.794051Z", "start_time": "2018-10-04T19:29:53.790564Z" } }, "outputs": [], "source": [ "now.hour, now.minute, now.second" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:30:13.269934Z", "start_time": "2018-10-04T19:30:13.262815Z" } }, "outputs": [], "source": [ "print(datetime.datetime.fromtimestamp(time.time()-3600))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:30:18.866475Z", "start_time": "2018-10-04T19:30:18.861048Z" } }, "outputs": [], "source": [ "now" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:31:53.347605Z", "start_time": "2018-10-04T19:31:53.335076Z" } }, "outputs": [], "source": [ "d = datetime.timedelta(days=1, minutes=10, seconds = -31)\n", "d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:31:53.633645Z", "start_time": "2018-10-04T19:31:53.627570Z" } }, "outputs": [], "source": [ "now - d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:31:54.024856Z", "start_time": "2018-10-04T19:31:54.018463Z" } }, "outputs": [], "source": [ "now + d" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:32:22.697794Z", "start_time": "2018-10-04T19:32:22.694662Z" } }, "outputs": [], "source": [ "mydatetime = datetime.datetime.combine(mydate, datetime.time(hour=9))\n", "print(mydatetime)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:32:31.858245Z", "start_time": "2018-10-04T19:32:31.854893Z" } }, "outputs": [], "source": [ "myage = now - mydatetime\n", "myage" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:32:55.845967Z", "start_time": "2018-10-04T19:32:55.842296Z" } }, "outputs": [], "source": [ "myage.total_seconds()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Text and Data Libraries" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:36:23.487556Z", "start_time": "2018-10-04T19:36:23.475076Z" }, "scrolled": true }, "outputs": [], "source": [ "import json\n", "\n", "data = [{'a': 'A', 'b': (2, 4), 'c': 3.0}]\n", "print('DATA :', data)\n", "\n", "data_string = json.dumps(data)\n", "print('ENCODED:', data_string)\n", "\n", "decoded = json.loads(data_string)\n", "print('DECODED:', decoded)\n", "\n", "print('SORTED :', json.dumps(data, sort_keys=True))\n", "print('INDENT :', json.dumps(data, sort_keys=True, indent=2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:37:22.813371Z", "start_time": "2018-10-04T19:37:22.273355Z" } }, "outputs": [], "source": [ "import urllib.request\n", "\n", "req = urllib.request.urlopen('https://docs.python.org/3/')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:37:41.649794Z", "start_time": "2018-10-04T19:37:41.639211Z" } }, "outputs": [], "source": [ "pydocb = req.read()\n", "pydocb" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:38:20.082694Z", "start_time": "2018-10-04T19:38:20.071448Z" } }, "outputs": [], "source": [ "import pprint\n", "pydoc = pydocb.decode('utf-8')\n", "pprint.pprint(pydoc)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:38:49.138842Z", "start_time": "2018-10-04T19:38:49.125844Z" }, "scrolled": true }, "outputs": [], "source": [ "from IPython.display import HTML\n", "\n", "HTML(pydoc)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:39:23.813129Z", "start_time": "2018-10-04T19:39:23.807712Z" } }, "outputs": [], "source": [ "import re\n", "\n", "href = re.compile('href\\=\"(.+?)\"')\n", "href" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:39:46.027590Z", "start_time": "2018-10-04T19:39:46.014189Z" }, "scrolled": true }, "outputs": [], "source": [ "for url in re.findall(href, pydoc):\n", " print(url)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:40:47.301517Z", "start_time": "2018-10-04T19:40:47.186747Z" }, "scrolled": true }, "outputs": [], "source": [ "import csv\n", "\n", "with open('urls.csv', 'w', newline='') as csvfile:\n", " writer = csv.writer(csvfile, delimiter=',')\n", " for i, url in enumerate(re.findall(href, pydoc)):\n", " writer.writerow([i, url])\n", " \n", "!cat urls.csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:41:36.452739Z", "start_time": "2018-10-04T19:41:36.341558Z" }, "scrolled": true }, "outputs": [], "source": [ "with open('urls.csv', 'w') as csvfile:\n", " fieldnames = ['num', 'link']\n", " writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n", " writer.writeheader()\n", " for i, url in enumerate(re.findall(href, pydoc)):\n", " writer.writerow({'num': i, 'link': url})\n", " \n", "!cat urls.csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:41:51.205387Z", "start_time": "2018-10-04T19:41:51.199376Z" }, "scrolled": true }, "outputs": [], "source": [ "with open('urls.csv', 'r') as csvfile:\n", " reader = csv.DictReader(csvfile)\n", " for row in reader:\n", " print(row)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:43:22.664902Z", "start_time": "2018-10-04T19:43:22.641191Z" } }, "outputs": [], "source": [ "import sqlite3\n", "conn = sqlite3.connect('urls.sqlite')\n", "\n", "c = conn.cursor()\n", "c.execute('''CREATE TABLE links (num, url ,link)''')\n", "\n", "with open('urls.csv', 'r') as csvfile:\n", " reader = csv.DictReader(csvfile)\n", " for row in reader:\n", " c.execute(\"INSERT INTO links VALUES ('{}', 'https://docs.python.org/3/', '{}')\".format(row['num'], row['link']))\n", "\n", "conn.commit()\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:43:31.588583Z", "start_time": "2018-10-04T19:43:31.577671Z" }, "scrolled": true }, "outputs": [], "source": [ "conn = sqlite3.connect('urls.sqlite')\n", "c = conn.cursor()\n", "\n", "for row in c.execute('SELECT * FROM links ORDER BY link'):\n", " print(row)\n", "\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:44:08.646020Z", "start_time": "2018-10-04T19:44:08.632116Z" } }, "outputs": [], "source": [ "import gzip, shutil\n", "\n", "with open('urls.csv', 'rb') as f_in:\n", " with gzip.open('urls.csv.gz', 'wb') as f_out:\n", " shutil.copyfileobj(f_in, f_out)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:44:10.462535Z", "start_time": "2018-10-04T19:44:10.347223Z" } }, "outputs": [], "source": [ "!cat urls.csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:44:24.095063Z", "start_time": "2018-10-04T19:44:23.981815Z" } }, "outputs": [], "source": [ "!ls -lh" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:44:33.384946Z", "start_time": "2018-10-04T19:44:33.274498Z" } }, "outputs": [], "source": [ "!rm urls*" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:44:33.762757Z", "start_time": "2018-10-04T19:44:33.572787Z" } }, "outputs": [], "source": [ "!ls -lh" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 2 }