{ "metadata": { "name": "", "signature": "sha256:b3abe7284544265a761b02099f40e06adcca0ef9f956ed2ce75c34a39ffb7ae9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u0430\u043a \u0440\u0430\u0432\u043d\u043e\u043f\u0440\u0430\u0432\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def f():\n", " print(1)\n", " \n", "g = f\n", "g()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "svn.py" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "\n", "def checkout(*args):\n", " print('checkout{}'.format(args))\n", "\n", "def commit(*args):\n", " print('commit{}'.format(args))\n", "\n", "\n", "commands = {\n", " 'checkout': checkout,\n", " 'co': checkout,\n", " 'commit': commit,\n", " 'ci': commit\n", " }\n", "\n", "cmd, *args = 'commit', 'arg1', 'arg2' #sys.argv[1:]\n", "commands[cmd](*args)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "commit('arg1', 'arg2')\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "\u0410\u043d\u0430\u043b\u043e\u0433 filter" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def filter(function, iterable):\n", " for e in iterable:\n", " if function(e):\n", " yield e\n", "\n", "def is_even(number):\n", " return number % 2 == 0\n", "\n", "\n", "a = [1, 2, 3, 4, 5]\n", "for e in filter(is_even, a):\n", " print(e)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n", "4\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "a = range(6)\n", "for e in filter(is_even, a):\n", " print(e)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "2\n", "4\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "\u041d\u0435\u043f\u043e\u043b\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u043e\u0433 map" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def map(function, iterable):\n", " for e in iterable:\n", " yield function(e)\n", " \n", "def square(x):\n", " return x ** 2\n", "\n", "\n", "for e in map(square, [1, 2, 3]):\n", " print(e)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "4\n", "9\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "\u0410\u043d\u0430\u043b\u043e\u0433 map" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def map(function, *iterables):\n", " iters = [iter(iterable) for iterable in iterables]\n", " while True:\n", " try:\n", " args = [next(it) for it in iters]\n", " yield function(*args)\n", " except StopIteration:\n", " break\n", " \n", "def add(a, b):\n", " return a + b\n", "\n", "\n", "for e in map(add, [1, 2, 3], [4, 5, 6]):\n", " print(e)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n", "7\n", "9\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "lambda" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for e in map(lambda a, b: a + b, [1, 2, 3], [4, 5, 6]):\n", " print(e)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n", "7\n", "9\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0424\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add(a, b):\n", " return a + b\n", "\n", "def partial(function, *args): \n", " def new_function(*more_args):\n", " all_args = args + more_args\n", " return function(*all_args)\n", " return new_function\n", " \n", "inc = partial(add, 1)\n", "inc(4)\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "5" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import functools\n", "import operator\n", "\n", "\n", "inc = functools.partial(operator.add, 1)\n", "print(inc(4))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "\u041a\u0435\u0448\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import timeit\n", "\n", "def caching(function):\n", " computed_results = {}\n", " def new_function(*args):\n", " if args not in computed_results:\n", " computed_results[args] = function(*args)\n", " return computed_results[args]\n", " return new_function\n", "\n", "\n", "def fibonacci_number(i):\n", " if i == 0 or i == 1:\n", " return 1\n", " return fibonacci_number(i - 1) + fibonacci_number(i - 2)\n", "\n", "print(timeit.timeit('fibonacci_number(30)', number=3, setup='from __main__ import fibonacci_number'))\n", "fibonacci_number = caching(fibonacci_number)\n", "print(timeit.timeit('fibonacci_number(30)', number=3, setup='from __main__ import fibonacci_number'))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2.3664230789872818\n", "5.887000588700175e-05\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import timeit\n", "\n", "def caching(function):\n", " computed_results = {}\n", " def new_function(*args):\n", " if args not in computed_results:\n", " computed_results[args] = function(*args)\n", " return computed_results[args]\n", " return new_function\n", "\n", "\n", "@caching\n", "def fibonacci_number(i):\n", " if i == 0 or i == 1:\n", " return 1\n", " return fibonacci_number(i - 1) + fibonacci_number(i - 2)\n", "\n", "print(timeit.timeit('fibonacci_number(30)', number=3, setup='from __main__ import fibonacci_number'))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5.275104194879532e-05\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0427\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x):\n", " print(x)\n", " \n", "print(f.__name__)\n", "g = f\n", "print(g.__name__)\n", "g = lambda: 1\n", "print(g.__name__)\n", "print(f.__module__)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "f\n", "f\n", "\n", "__main__\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "docstrings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def caching(function):\n", " '''Caching decorator for functions with positional arguments.'''\n", "\n", " computed_results = {}\n", " def new_function(*args):\n", " if args not in computed_results:\n", " computed_results[args] = function(*args)\n", " return computed_results[args]\n", " new_function.__name__ = function.__name__\n", " new_function.__doc__ = function.__doc__\n", " return new_function\n", "\n", "@caching\n", "def fibonacci_number(i):\n", " '''Function for calculating i-th Fibonacci number.'''\n", "\n", " if i == 0 or i == 1:\n", " return 1\n", " return fibonacci_number(i - 1) + fibonacci_number(i - 2)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "import caching\n", "help(caching) # $ pydoc caching" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on module caching:\n", "\n", "NAME\n", " caching\n", "\n", "FUNCTIONS\n", " caching(function)\n", " Caching decorator for functions with positional arguments.\n", " \n", " fibonacci_number(*args)\n", " Function for calculating i-th Fibonacci number.\n", "\n", "FILE\n", " /Users/tswr/python/caching.py\n", "\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import HTML\n", "with open('caching.html') as f: # $ pydoc -w caching\n", " h = HTML(f.read())\n", "h" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "Python: module caching\n", "\n", "\n", "\n", "\n", "
 
\n", " 
caching
index
/Users/tswr/python/caching.py
\n", "

\n", "

\n", "\n", "\n", "\n", " \n", "\n", "
 
\n", "Functions
       
caching(function)
Caching decorator for functions with positional arguments.
\n", "
fibonacci_number(*args)
Function for calculating i-th Fibonacci number.
\n", "
\n", "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "code object" ] }, { "cell_type": "code", "collapsed": false, "input": [ "caching.caching.__code__.co_code" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "b'i\\x00\\x00\\x89\\x00\\x00\\x87\\x00\\x00\\x87\\x01\\x00f\\x02\\x00d\\x01\\x00d\\x02\\x00\\x86\\x00\\x00}\\x01\\x00\\x88\\x01\\x00j\\x00\\x00|\\x01\\x00_\\x00\\x00\\x88\\x01\\x00j\\x01\\x00|\\x01\\x00_\\x01\\x00|\\x01\\x00S'" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "import dis\n", "dis.dis(caching.caching)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 4 0 BUILD_MAP 0\n", " 3 STORE_DEREF 0 (computed_results)\n", "\n", " 5 6 LOAD_CLOSURE 0 (computed_results)\n", " 9 LOAD_CLOSURE 1 (function)\n", " 12 BUILD_TUPLE 2\n", " 15 LOAD_CONST 1 ()\n", " 18 LOAD_CONST 2 ('caching..new_function')\n", " 21 MAKE_CLOSURE 0\n", " 24 STORE_FAST 1 (new_function)\n", "\n", " 9 27 LOAD_DEREF 1 (function)\n", " 30 LOAD_ATTR 0 (__name__)\n", " 33 LOAD_FAST 1 (new_function)\n", " 36 STORE_ATTR 0 (__name__)\n", "\n", " 10 39 LOAD_DEREF 1 (function)\n", " 42 LOAD_ATTR 1 (__doc__)\n", " 45 LOAD_FAST 1 (new_function)\n", " 48 STORE_ATTR 1 (__doc__)\n", "\n", " 11 51 LOAD_FAST 1 (new_function)\n", " 54 RETURN_VALUE\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "import inspect\n", "sig = inspect.signature(caching.caching)\n", "print(sig)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(function)\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "print(sig.parameters)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "OrderedDict([('function', )])\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "print(inspect.getsource(caching.caching))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "def caching(function):\n", " '''Caching decorator for functions with positional arguments.'''\n", "\n", " computed_results = {}\n", " def new_function(*args):\n", " if args not in computed_results:\n", " computed_results[args] = function(*args)\n", " return computed_results[args]\n", " new_function.__name__ = function.__name__\n", " new_function.__doc__ = function.__doc__\n", " return new_function\n", "\n" ] } ], "prompt_number": 9 } ], "metadata": {} } ] }