@@ -410,6 +410,10 @@ msgid ""
410
410
"except NameError:\n"
411
411
" cache = {}"
412
412
msgstr ""
413
+ "try:\n"
414
+ "cache\n"
415
+ "except NameError:\n"
416
+ "cache = {}"
413
417
414
418
#: ../Doc/library/importlib.rst:187
415
419
msgid ""
@@ -496,6 +500,15 @@ msgid ""
496
500
" +-- FileLoader\n"
497
501
" +-- SourceLoader"
498
502
msgstr ""
503
+ "object\n"
504
+ "+-- MetaPathFinder\n"
505
+ "+-- PathEntryFinder\n"
506
+ "+-- Loader\n"
507
+ "+-- ResourceLoader --------+\n"
508
+ "+-- InspectLoader |\n"
509
+ "+-- ExecutionLoader --+\n"
510
+ "+-- FileLoader\n"
511
+ "+-- SourceLoader"
499
512
500
513
#: ../Doc/library/importlib.rst:240
501
514
msgid "An abstract base class representing a :term:`meta path finder`."
@@ -2502,7 +2515,7 @@ msgid ""
2502
2515
msgstr ""
2503
2516
"import importlib\n"
2504
2517
"\n"
2505
- "itertools = importlib.import_module(‘ itertools’ )"
2518
+ "itertools = importlib.import_module(' itertools' )"
2506
2519
2507
2520
#: ../Doc/library/importlib.rst:1550
2508
2521
msgid "Checking if a module can be imported"
@@ -2543,6 +2556,22 @@ msgid ""
2543
2556
"else:\n"
2544
2557
" print(f\" can't find the {name!r} module\" )"
2545
2558
msgstr ""
2559
+ "import importlib.util\n"
2560
+ "import sys\n"
2561
+ "\n"
2562
+ "# Con fines ilustrativos.\n"
2563
+ "name = 'itertools'\n"
2564
+ "\n"
2565
+ "if name in sys.modules:\n"
2566
+ "print(f\" {name!r} already in sys.modules\" )\n"
2567
+ "elif (spec := importlib.util.find_spec(name)) is not None:\n"
2568
+ "# Si eligió realizar la importación real ...\n"
2569
+ "module = importlib.util.module_from_spec(spec)\n"
2570
+ "sys.modules[name] = module\n"
2571
+ "spec.loader.exec_module(module)\n"
2572
+ "print(f\" {name!r} ha sido importado\" )\n"
2573
+ "else:\n"
2574
+ "print(f\" no puede ser encontrado el módulo {name!r}\" )"
2546
2575
2547
2576
#: ../Doc/library/importlib.rst:1578
2548
2577
msgid "Importing a source file directly"
@@ -2594,6 +2623,25 @@ msgid ""
2594
2623
"# Similar outcome as `import json`.\n"
2595
2624
"json = import_from_path(module_name, file_path)"
2596
2625
msgstr ""
2626
+ "import importlib.util\n"
2627
+ "import sys\n"
2628
+ "\n"
2629
+ "\n"
2630
+ "def import_from_path(module_name, file_path):\n"
2631
+ "spec = importlib.util.spec_from_file_location(module_name, file_path)\n"
2632
+ "module = importlib.util.module_from_spec(spec)\n"
2633
+ "sys.modules[module_name] = module\n"
2634
+ "spec.loader.exec_module(module)\n"
2635
+ "return module\n"
2636
+ "\n"
2637
+ "\n"
2638
+ "# Sólo con fines ilustrativos. (el uso de `json` es arbitrario).\n"
2639
+ "import json\n"
2640
+ "file_path = json.__file__\n"
2641
+ "module_name = json.__name__\n"
2642
+ "\n"
2643
+ "# Resultado similar a `import json`.\n"
2644
+ "json = import_from_path(module_name, file_path)"
2597
2645
2598
2646
#: ../Doc/library/importlib.rst:1611
2599
2647
msgid "Implementing lazy imports"
@@ -2622,6 +2670,22 @@ msgid ""
2622
2670
">>> lazy_typing.TYPE_CHECKING\n"
2623
2671
"False"
2624
2672
msgstr ""
2673
+ ">>> import importlib.util\n"
2674
+ ">>> import sys\n"
2675
+ ">>> def lazy_import(name):\n"
2676
+ "...spec = importlib.util.find_spec(name)\n"
2677
+ "...loader = importlib.util.LazyLoader(spec.loader)\n"
2678
+ "...spec.loader = loader\n"
2679
+ "...module = importlib.util.module_from_spec(spec)\n"
2680
+ "...sys.modules[name] = module\n"
2681
+ "...loader.exec_module(module)\n"
2682
+ "...return module\n"
2683
+ "...\n"
2684
+ ">>> lazy_typing = lazy_import(\" typing\" )\n"
2685
+ ">>> #lazy_typing es un objeto de módulo real,\n"
2686
+ ">>> #pero aún no está cargado en la memoria.\n"
2687
+ ">>> lazy_typing.TYPE_CHECKING\n"
2688
+ "False"
2625
2689
2626
2690
#: ../Doc/library/importlib.rst:1634
2627
2691
msgid "Setting up an importer"
@@ -2675,6 +2739,26 @@ msgid ""
2675
2739
"# of priority.\n"
2676
2740
"sys.path_hooks.append(SpamPathEntryFinder.path_hook(loader_details))"
2677
2741
msgstr ""
2742
+ "import importlib.machinery\n"
2743
+ "import sys\n"
2744
+ "\n"
2745
+ "# Sólo con fines ilustrativos.\n"
2746
+ "SpamMetaPathFinder = importlib.machinery.PathFinder\n"
2747
+ "SpamPathEntryFinder = importlib.machinery.FileFinder\n"
2748
+ "detalles_del_cargador = (importlib.machinery.SourceFileLoader,\n"
2749
+ "importlib.machinery.SOURCE_SUFFIXES)\n"
2750
+ "\n"
2751
+ "# Configuración de un meta buscador de rutas.\n"
2752
+ "# Asegúrese de colocar el buscador en la ubicación adecuada en la lista en términos "
2753
+ "de\n"
2754
+ "# prioridad.\n"
2755
+ "sys.meta_path.append(SpamMetaPathFinder)\n"
2756
+ "\n"
2757
+ "# Configuración de un buscador de entradas de ruta.\n"
2758
+ "# Asegúrese de colocar el gancho de ruta en la ubicación adecuada en la lista en "
2759
+ "términos\n"
2760
+ "# de prioridad.\n"
2761
+ "sys.path_hooks.append(SpamPathEntryFinder.path_hook(detalles_del_cargador))"
2678
2762
2679
2763
#: ../Doc/library/importlib.rst:1668
2680
2764
msgid "Approximating :func:`importlib.import_module`"
@@ -2725,6 +2809,35 @@ msgid ""
2725
2809
" setattr(parent_module, child_name, module)\n"
2726
2810
" return module"
2727
2811
msgstr ""
2812
+ "import importlib.util\n"
2813
+ "import sys\n"
2814
+ "\n"
2815
+ "def import_module(name, package=None):\n"
2816
+ "\"\"\" Una implementación aproximada de la importación.\"\"\" \n"
2817
+ "nombre_absoluto = importlib.util.resolve_name(name, package)\n"
2818
+ "try:\n"
2819
+ "return sys.modules[nombre_absoluto]\n"
2820
+ "except KeyError:\n"
2821
+ "pass\n"
2822
+ "\n"
2823
+ "path = None\n"
2824
+ "if '.' in nombre_absoluto:\n"
2825
+ "parent_name, _, child_name = nombre_absoluto.rpartition('.')\n"
2826
+ "parent_module = import_module(parent_name)\n"
2827
+ "path = parent_module.__spec__.submodule_search_locations\n"
2828
+ "for finder in sys.meta_path:\n"
2829
+ "spec = finder.find_spec(nombre_absoluto, path)\n"
2830
+ "if spec is not None:\n"
2831
+ "break\n"
2832
+ "else:\n"
2833
+ "msg = f'Ningún módulo llamado {nombre_absoluto!r}'\n"
2834
+ "raise ModuleNotFoundError(msg, name=nombre_absoluto)\n"
2835
+ "module = importlib.util.module_from_spec(spec)\n"
2836
+ "sys.modules[nombre_absoluto] = module\n"
2837
+ "spec.loader.exec_module(module)\n"
2838
+ "if path is not None:\n"
2839
+ "setattr(parent_module, child_name, module)\n"
2840
+ "return module"
2728
2841
2729
2842
#: ../Doc/library/importlib.rst:443
2730
2843
msgid "universal newlines"
0 commit comments