{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# py350" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PEP 448" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5\n" ] } ], "source": [ "print(*[1], *[2], 3, *[4, 5])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4\n" ] } ], "source": [ "def fn(a, b, c, d):\n", " print(a, b, c, d)\n", "fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4})" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0, 1, 2, 3, 4)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "*range(4), 4" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[*range(4), 4]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 3, 4, 5, 6, 7}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{*range(4), 4, *(5, 6, 7)}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'x': 1, 'y': 2}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{'x': 1, **{'y': 2}}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PEP 461" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "b'Hello World!'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b'Hello %b!' % b'World'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PEP 465" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 1., 1., 1.])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy\n", "x = numpy.ones(3)\n", "x" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 0.],\n", " [ 0., 1., 0.],\n", " [ 0., 0., 1.]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = numpy.eye(3)\n", "m" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for @: 'numpy.ndarray' and 'numpy.ndarray'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m@\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for @: 'numpy.ndarray' and 'numpy.ndarray'" ] } ], "source": [ "x @ m" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'1.9.3'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numpy.version.version" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PEP 484" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "Can't convert 'int' object to str implicitly", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgreeting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34m'Hello '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mgreeting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mgreeting\u001b[0;34m(name)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgreeting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;34m'Hello '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mgreeting\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly" ] } ], "source": [ "def greeting(name: str) -> str:\n", " return 'Hello ' + name\n", "greeting(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Untitled.ipynb\n" ] } ], "source": [ "import os\n", "for entry in os.scandir('.'):\n", " if not entry.name.startswith('.') and entry.is_file():\n", " print(entry.name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PEP 492" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "coro 2: waiting for lock\n", "coro 2: holding the lock\n", "coro 1: waiting for lock\n", "coro 2: releasing the lock\n", "coro 1: holding the lock\n", "coro 1: releasing the lock\n" ] } ], "source": [ "import asyncio\n", "async def coro(name, lock):\n", " print('coro {}: waiting for lock'.format(name))\n", " async with lock:\n", " print('coro {}: holding the lock'.format(name))\n", " await asyncio.sleep(1)\n", " print('coro {}: releasing the lock'.format(name))\n", "\n", "loop = asyncio.get_event_loop()\n", "lock = asyncio.Lock()\n", "coros = asyncio.gather(coro(1, lock), coro(2, lock))\n", "try:\n", " loop.run_until_complete(coros)\n", "finally:\n", " loop.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PEP 475 ?" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import io\n", "io.open" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PEP 485" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math\n", "a = 5.0\n", "b = 4.99998" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.isclose(a, b, rel_tol=1e-5)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.isclose(a, b, rel_tol=1e-6)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.isclose(a, b, abs_tol=0.00003)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.isclose(a, b, abs_tol=0.00001)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## enum" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import enum\n", "Animal = enum.Enum('Animal', 'cat dog', start=10)\n", "Animal" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Animal.cat" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Animal.dog" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## heap que" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "http://d.hatena.ne.jp/dwarfjay/20140418/1397825281" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import heapq\n", "a = ['9', '777', '55555']\n", "b = ['88', '6666']" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['9', '88', '777', '6666', '55555']" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(heapq.merge(a, b, key=len))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['55555', '6666', '777', '88', '9']" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(heapq.merge(reversed(a), reversed(b), key=len, reverse=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## logging" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "ERROR:root:exception\n", "Traceback (most recent call last):\n", " File \"\", line 3, in \n", " 1/0\n", "ZeroDivisionError: division by zero\n" ] } ], "source": [ "import logging\n", "try:\n", " 1/0\n", "except ZeroDivisionError as ex:\n", " logging.error('exception', exc_info=ex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## math" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math\n", "import fractions" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "fractions.gcd(4, 6)\n", "2\n", "# /Users/xxx/.pyenv/versions/venv350/lib/python3.5/site-packages/ipykernel/__main__.py:1: DeprecationWarning: fractions.gcd() is deprecated. Use math.gcd() instead.\n", " if __name__ == '__main__':" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.gcd(4, 6)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "nan" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.nan" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(math.nan)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.inf" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(math.inf)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('inf')" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "nan" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('nan')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changes in Python behavior" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "Generator expression must be parenthesized if not sole argument (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print(1 for x in [1], *[2])\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Generator expression must be parenthesized if not sole argument\n" ] } ], "source": [ "print(1 for x in [1], *[2])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 3.4.3\n", "# print(1 for x in [1], *[2])\n", "# at 0x103e0ffc0> 2" ] } ], "metadata": { "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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }