|
31 | 31 | "from fastcore.utils import *\n", |
32 | 32 | "from fastcore.script import call_parse\n", |
33 | 33 | "\n", |
34 | | - "import multiprocessing\n", |
| 34 | + "import multiprocessing,types\n", |
35 | 35 | "try:\n", |
36 | 36 | " if sys.platform == 'darwin': multiprocessing.set_start_method(\"fork\")\n", |
37 | 37 | "except RuntimeError: pass # if re-running cell\n", |
38 | 38 | "\n", |
39 | 39 | "from IPython.core.interactiveshell import InteractiveShell, ExecutionInfo, ExecutionResult\n", |
40 | 40 | "from IPython.core.displayhook import DisplayHook\n", |
41 | 41 | "from IPython.utils.capture import capture_output\n", |
| 42 | + "from IPython.core.completer import IPCompleter,provisionalcompleter\n", |
| 43 | + "from IPython.core.hooks import CommandChainDispatcher\n", |
| 44 | + "from IPython.core.completerlib import module_completer\n", |
| 45 | + "from IPython.utils.strdispatch import StrDispatch\n", |
42 | 46 | "from IPython.display import display as disp, HTML\n", |
43 | 47 | "\n", |
44 | 48 | "import traceback\n", |
|
69 | 73 | "## CaptureShell -" |
70 | 74 | ] |
71 | 75 | }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": null, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "class SmartCompleter(IPCompleter):\n", |
| 83 | + " def __init__(self, shell, namespace=None):\n", |
| 84 | + " if namespace is None: namespace = shell.user_ns\n", |
| 85 | + " super().__init__(shell, namespace)\n", |
| 86 | + " self.use_jedi = False\n", |
| 87 | + "\n", |
| 88 | + " sdisp = StrDispatch()\n", |
| 89 | + " self.custom_completers = sdisp\n", |
| 90 | + " import_disp = CommandChainDispatcher()\n", |
| 91 | + " import_disp.add(types.MethodType(module_completer, shell))\n", |
| 92 | + " sdisp.add_s('import', import_disp)\n", |
| 93 | + " sdisp.add_s('from', import_disp)\n", |
| 94 | + "\n", |
| 95 | + " def __call__(self, c):\n", |
| 96 | + " if not c: return []\n", |
| 97 | + " with provisionalcompleter():\n", |
| 98 | + " return [o.text.rpartition('.')[-1]\n", |
| 99 | + " for o in self.completions(c, len(c))\n", |
| 100 | + " if o.type=='<unknown>']" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "cc = SmartCompleter(get_ipython())\n", |
| 110 | + "\n", |
| 111 | + "def test_set(a,b): return test_eq(set(a), set(b))\n", |
| 112 | + "\n", |
| 113 | + "class _f:\n", |
| 114 | + " def __init__(self): self.bar,self.baz,self.room = 0,0,0\n", |
| 115 | + "\n", |
| 116 | + "foo = _f()\n", |
| 117 | + "\n", |
| 118 | + "test_set(cc(\"b\"), ['bin', 'bool', 'break', 'breakpoint', 'bytearray', 'bytes'])\n", |
| 119 | + "test_set(cc(\"foo.b\"), ['bar', 'baz'])\n", |
| 120 | + "test_set(cc(\"x=1; x = foo.b\"), ['bar', 'baz'])\n", |
| 121 | + "test_set(cc(\"ab\"), ['abs'])\n", |
| 122 | + "test_set(cc(\"b = ab\"), ['abs'])\n", |
| 123 | + "test_set(cc(\"\"), [])\n", |
| 124 | + "test_set(cc(\"foo.\"), ['bar', 'baz', 'room'])\n", |
| 125 | + "test_set(cc(\"nonexistent.b\"), [])\n", |
| 126 | + "test_set(cc(\"foo.nonexistent.b\"), [])\n", |
| 127 | + "test_set(cc(\"import ab\"), ['abc'])\n", |
| 128 | + "test_set(cc(\"from abc import AB\"), ['ABC', 'ABCMeta'])" |
| 129 | + ] |
| 130 | + }, |
72 | 131 | { |
73 | 132 | "cell_type": "code", |
74 | 133 | "execution_count": null, |
|
1635 | 1694 | "This is the command-line version of `CaptureShell.execute`. Run `exec_nb -h` from the command line to see how to pass arguments. If you don't pass `dest` then the output notebook won't be saved; this is mainly useful for running tests." |
1636 | 1695 | ] |
1637 | 1696 | }, |
| 1697 | + { |
| 1698 | + "cell_type": "markdown", |
| 1699 | + "metadata": {}, |
| 1700 | + "source": [ |
| 1701 | + "## Completions" |
| 1702 | + ] |
| 1703 | + }, |
| 1704 | + { |
| 1705 | + "cell_type": "code", |
| 1706 | + "execution_count": null, |
| 1707 | + "metadata": {}, |
| 1708 | + "outputs": [], |
| 1709 | + "source": [ |
| 1710 | + "#| export\n", |
| 1711 | + "class SmartCompleter(IPCompleter):\n", |
| 1712 | + " def __init__(self, shell, namespace=None):\n", |
| 1713 | + " if namespace is None: namespace = shell.user_ns\n", |
| 1714 | + " super().__init__(shell, namespace)\n", |
| 1715 | + " self.use_jedi = False\n", |
| 1716 | + "\n", |
| 1717 | + " sdisp = StrDispatch()\n", |
| 1718 | + " self.custom_completers = sdisp\n", |
| 1719 | + " import_disp = CommandChainDispatcher()\n", |
| 1720 | + " import_disp.add(types.MethodType(module_completer, shell))\n", |
| 1721 | + " sdisp.add_s('import', import_disp)\n", |
| 1722 | + " sdisp.add_s('from', import_disp)\n", |
| 1723 | + "\n", |
| 1724 | + " def __call__(self, c):\n", |
| 1725 | + " if not c: return []\n", |
| 1726 | + " with provisionalcompleter():\n", |
| 1727 | + " return [o.text.rpartition('.')[-1]\n", |
| 1728 | + " for o in self.completions(c, len(c))\n", |
| 1729 | + " if o.type=='<unknown>']" |
| 1730 | + ] |
| 1731 | + }, |
| 1732 | + { |
| 1733 | + "cell_type": "code", |
| 1734 | + "execution_count": null, |
| 1735 | + "metadata": {}, |
| 1736 | + "outputs": [], |
| 1737 | + "source": [ |
| 1738 | + "cc = SmartCompleter(get_ipython())\n", |
| 1739 | + "\n", |
| 1740 | + "def test_set(a,b): return test_eq(set(a), set(b))\n", |
| 1741 | + "\n", |
| 1742 | + "class _f:\n", |
| 1743 | + " def __init__(self): self.bar,self.baz,self.room = 0,0,0\n", |
| 1744 | + "\n", |
| 1745 | + "foo = _f()\n", |
| 1746 | + "\n", |
| 1747 | + "assert set(cc(\"b\")).issuperset(['bin', 'bool', 'break', 'breakpoint', 'bytearray', 'bytes'])\n", |
| 1748 | + "test_set(cc(\"foo.b\"), ['bar', 'baz'])\n", |
| 1749 | + "test_set(cc(\"x=1; x = foo.b\"), ['bar', 'baz'])\n", |
| 1750 | + "test_set(cc(\"ab\"), ['abs'])\n", |
| 1751 | + "test_set(cc(\"b = ab\"), ['abs'])\n", |
| 1752 | + "test_set(cc(\"\"), [])\n", |
| 1753 | + "test_set(cc(\"foo.\"), ['bar', 'baz', 'room'])\n", |
| 1754 | + "test_set(cc(\"nonexistent.b\"), [])\n", |
| 1755 | + "test_set(cc(\"foo.nonexistent.b\"), [])\n", |
| 1756 | + "test_set(cc(\"import ab\"), ['abc'])\n", |
| 1757 | + "test_set(cc(\"from abc import AB\"), ['ABC', 'ABCMeta'])" |
| 1758 | + ] |
| 1759 | + }, |
1638 | 1760 | { |
1639 | 1761 | "cell_type": "markdown", |
1640 | 1762 | "metadata": {}, |
|
0 commit comments