{ "cells": [ { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x : None => 1\n" ] } ], "source": [ "from muster import Model, Member\n", "\n", "class A(Model):\n", "\n", " x = Member()\n", " \n", " _x_old : \"before setting x\"\n", " _x_change : \"after setting x\"\n", " \n", " def _x_old(self, member, data):\n", " data[\"old\"] = getattr(self, \"x\", None)\n", "\n", " def _x_change(self, member, data):\n", " msg = \"x : %r => %r\"\n", " info = (data[\"old\"], data[\"value\"])\n", " print(msg % info)\n", "\n", "A().x = 1" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x : None => 2\n", "x : None => 0\n" ] } ], "source": [ "A().x = 2\n", "A().x = 0" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "# A().x['Thing'] = 1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'code_obj': at 0x000000000610E390, file \"\", line 3>,\n", " 'old_excepthook': >,\n", " 'outflag': 1,\n", " 'result': ,\n", " 'self': }" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import inspect\n", "\n", "inspect.currentframe().f_back.f_locals" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 6.58 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "1000000 loops, best of 3: 1.04 µs per loop\n" ] } ], "source": [ "s = ['before setting x', 'something else']\n", "term = 'before'\n", "%timeit tuple(filter(lambda x: term in x, s))" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 4.08 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "1000000 loops, best of 3: 1.36 µs per loop\n" ] } ], "source": [ "rx = re.compile('^before (.*)$')\n", "%timeit m = list(map(rx.match, s))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am from A class\n", "[, , , , ]\n" ] } ], "source": [ "class A(object):\n", " def dothis(self):\n", " print('I am from A class')\n", "\n", "class B(A):\n", " pass\n", "\n", "class C(object):\n", " def dothis(self):\n", " print('I am from C class')\n", "\n", "class D(B, C):\n", " pass\n", "\n", "d_instance= D()\n", "d_instance.dothis()\n", "print(D.mro())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.1" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }