|
6 | 6 | "metadata": {}, |
7 | 7 | "outputs": [], |
8 | 8 | "source": [ |
9 | | - "#|default_exp foundation" |
| 9 | + "#| default_exp foundation" |
10 | 10 | ] |
11 | 11 | }, |
12 | 12 | { |
|
15 | 15 | "metadata": {}, |
16 | 16 | "outputs": [], |
17 | 17 | "source": [ |
18 | | - "#|export\n", |
| 18 | + "#| export\n", |
19 | 19 | "from fastcore.imports import *\n", |
20 | 20 | "from fastcore.basics import *\n", |
21 | 21 | "from functools import lru_cache\n", |
|
31 | 31 | "metadata": {}, |
32 | 32 | "outputs": [], |
33 | 33 | "source": [ |
34 | | - "#|hide\n", |
| 34 | + "#| hide\n", |
35 | 35 | "from fastcore.test import *\n", |
36 | 36 | "from nbdev.showdoc import *\n", |
37 | 37 | "from fastcore.nb_imports import *" |
|
59 | 59 | "metadata": {}, |
60 | 60 | "outputs": [], |
61 | 61 | "source": [ |
62 | | - "#|export\n", |
| 62 | + "#| export\n", |
63 | 63 | "@contextmanager\n", |
64 | 64 | "def working_directory(path):\n", |
65 | 65 | " \"Change working directory to `path` and return to previous on exit.\"\n", |
|
75 | 75 | "metadata": {}, |
76 | 76 | "outputs": [], |
77 | 77 | "source": [ |
78 | | - "#|export\n", |
| 78 | + "#| export\n", |
79 | 79 | "def add_docs(cls, cls_doc=None, **docs):\n", |
80 | 80 | " \"Copy values from `docs` to `cls` docstrings, and confirm all public methods are documented\"\n", |
81 | 81 | " if cls_doc is not None: cls.__doc__ = cls_doc\n", |
|
173 | 173 | "metadata": {}, |
174 | 174 | "outputs": [], |
175 | 175 | "source": [ |
176 | | - "#|hide\n", |
| 176 | + "#| hide\n", |
177 | 177 | "class _T:\n", |
178 | 178 | " def f(self): pass\n", |
179 | 179 | " @classmethod\n", |
|
191 | 191 | "metadata": {}, |
192 | 192 | "outputs": [], |
193 | 193 | "source": [ |
194 | | - "#|export\n", |
| 194 | + "#| export\n", |
195 | 195 | "def docs(cls):\n", |
196 | 196 | " \"Decorator version of `add_docs`, using `_docs` dict\"\n", |
197 | 197 | " add_docs(cls, **cls._docs)\n", |
|
303 | 303 | "metadata": {}, |
304 | 304 | "outputs": [], |
305 | 305 | "source": [ |
306 | | - "#|export\n", |
| 306 | + "#| export\n", |
307 | 307 | "def coll_repr(c, max_n=20):\n", |
308 | 308 | " \"String repr of up to `max_n` items of (possibly lazy) collection `c`\"\n", |
309 | 309 | " return f'(#{len(c)}) [' + ','.join(itertools.islice(map(repr,c), max_n)) + (\n", |
|
337 | 337 | "metadata": {}, |
338 | 338 | "outputs": [], |
339 | 339 | "source": [ |
340 | | - "#|export\n", |
| 340 | + "#| export\n", |
341 | 341 | "def is_bool(x):\n", |
342 | 342 | " \"Check whether `x` is a bool or None\"\n", |
343 | 343 | " return isinstance(x,(bool,NoneType)) or risinstance('bool_', x)" |
|
349 | 349 | "metadata": {}, |
350 | 350 | "outputs": [], |
351 | 351 | "source": [ |
352 | | - "#|export\n", |
| 352 | + "#| export\n", |
353 | 353 | "def mask2idxs(mask):\n", |
354 | 354 | " \"Convert bool mask or index list to index `L`\"\n", |
355 | 355 | " if isinstance(mask,slice): return mask\n", |
|
378 | 378 | "metadata": {}, |
379 | 379 | "outputs": [], |
380 | 380 | "source": [ |
381 | | - "#|export\n", |
| 381 | + "#| export\n", |
382 | 382 | "def cycle(o):\n", |
383 | 383 | " \"Like `itertools.cycle` except creates list of `None`s if `o` is empty\"\n", |
384 | 384 | " o = listify(o)\n", |
|
403 | 403 | "metadata": {}, |
404 | 404 | "outputs": [], |
405 | 405 | "source": [ |
406 | | - "#|export\n", |
| 406 | + "#| export\n", |
407 | 407 | "def zip_cycle(x, *args):\n", |
408 | 408 | " \"Like `itertools.zip_longest` but `cycle`s through elements of all but first argument\"\n", |
409 | 409 | " return zip(x, *map(cycle,args))" |
|
424 | 424 | "metadata": {}, |
425 | 425 | "outputs": [], |
426 | 426 | "source": [ |
427 | | - "#|export\n", |
| 427 | + "#| export\n", |
428 | 428 | "def is_indexer(idx):\n", |
429 | 429 | " \"Test whether `idx` will index a single item in a list\"\n", |
430 | 430 | " return isinstance(idx,int) or not getattr(idx,'ndim',1)" |
|
549 | 549 | "metadata": {}, |
550 | 550 | "outputs": [], |
551 | 551 | "source": [ |
552 | | - "#|export\n", |
| 552 | + "#| export\n", |
553 | 553 | "class CollBase:\n", |
554 | 554 | " \"Base class for composing a list of `items`\"\n", |
555 | 555 | " def __init__(self, items): self.items = items\n", |
|
597 | 597 | "metadata": {}, |
598 | 598 | "outputs": [], |
599 | 599 | "source": [ |
600 | | - "#|export\n", |
| 600 | + "#| export\n", |
601 | 601 | "class _L_Meta(type):\n", |
602 | 602 | " def __call__(cls, x=None, *args, **kwargs):\n", |
603 | 603 | " if not args and not kwargs and x is not None and isinstance(x,cls): return x\n", |
|
610 | 610 | "metadata": {}, |
611 | 611 | "outputs": [], |
612 | 612 | "source": [ |
613 | | - "#|export\n", |
| 613 | + "#| export\n", |
614 | 614 | "class L(GetAttr, CollBase, metaclass=_L_Meta):\n", |
615 | 615 | " \"Behaves like a list of `items` but can also index with list of indices or masks\"\n", |
616 | 616 | " _default='items'\n", |
|
719 | 719 | "metadata": {}, |
720 | 720 | "outputs": [], |
721 | 721 | "source": [ |
722 | | - "#|export\n", |
| 722 | + "#| export\n", |
723 | 723 | "add_docs(L,\n", |
724 | 724 | " __getitem__=\"Retrieve `idx` (can be list of indices, or mask, or int) items\",\n", |
725 | 725 | " range=\"Class Method: Same as `range`, but returns `L`. Can pass collection for `a`, to use `len(a)`\",\n", |
|
760 | 760 | "metadata": {}, |
761 | 761 | "outputs": [], |
762 | 762 | "source": [ |
763 | | - "#|export\n", |
764 | | - "#|hide\n", |
| 763 | + "#| export\n", |
| 764 | + "#| hide\n", |
765 | 765 | "# Here we are fixing the signature of L. What happens is that the __call__ method on the MetaClass of L shadows the __init__\n", |
766 | 766 | "# giving the wrong signature (https://stackoverflow.com/questions/49740290/call-from-metaclass-shadows-signature-of-init).\n", |
767 | 767 | "def _f(items=None, *rest, use_list=False, match=None): ...\n", |
|
774 | 774 | "metadata": {}, |
775 | 775 | "outputs": [], |
776 | 776 | "source": [ |
777 | | - "#|export\n", |
| 777 | + "#| export\n", |
778 | 778 | "Sequence.register(L);" |
779 | 779 | ] |
780 | 780 | }, |
|
896 | 896 | "metadata": {}, |
897 | 897 | "outputs": [], |
898 | 898 | "source": [ |
899 | | - "#|hide\n", |
| 899 | + "#| hide\n", |
900 | 900 | "# test set items with L of collections\n", |
901 | 901 | "x = L([[1,2,3], [4,5], [6,7]])\n", |
902 | 902 | "x[0] = [1,2]\n", |
|
2307 | 2307 | "metadata": {}, |
2308 | 2308 | "outputs": [], |
2309 | 2309 | "source": [ |
2310 | | - "#|export\n", |
| 2310 | + "#| export\n", |
2311 | 2311 | "def save_config_file(file, d, **kwargs):\n", |
2312 | 2312 | " \"Write settings dict to a new config file, or overwrite the existing one.\"\n", |
2313 | 2313 | " config = ConfigParser(**kwargs)\n", |
|
2321 | 2321 | "metadata": {}, |
2322 | 2322 | "outputs": [], |
2323 | 2323 | "source": [ |
2324 | | - "#|export\n", |
| 2324 | + "#| export\n", |
2325 | 2325 | "def read_config_file(file, **kwargs):\n", |
2326 | 2326 | " config = ConfigParser(**kwargs)\n", |
2327 | 2327 | " config.read(file, encoding='utf8')\n", |
|
2370 | 2370 | "metadata": {}, |
2371 | 2371 | "outputs": [], |
2372 | 2372 | "source": [ |
2373 | | - "#|export\n", |
| 2373 | + "#| export\n", |
2374 | 2374 | "class Config:\n", |
2375 | 2375 | " \"Reading and writing `ConfigParser` ini files\"\n", |
2376 | 2376 | " def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None, **cfg_kwargs):\n", |
|
2714 | 2714 | "metadata": {}, |
2715 | 2715 | "outputs": [], |
2716 | 2716 | "source": [ |
2717 | | - "#|hide\n", |
| 2717 | + "#| hide\n", |
2718 | 2718 | "import nbdev; nbdev.nbdev_export()" |
2719 | 2719 | ] |
2720 | 2720 | }, |
|
0 commit comments