@@ -410,6 +410,10 @@ msgid ""
410410"except NameError:\n"
411411" cache = {}"
412412msgstr ""
413+ "try:\n"
414+ "cache\n"
415+ "except NameError:\n"
416+ "cache = {}"
413417
414418#: ../Doc/library/importlib.rst:187
415419msgid ""
@@ -496,6 +500,15 @@ msgid ""
496500" +-- FileLoader\n"
497501" +-- SourceLoader"
498502msgstr ""
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"
499512
500513#: ../Doc/library/importlib.rst:240
501514msgid "An abstract base class representing a :term:`meta path finder`."
@@ -2502,7 +2515,7 @@ msgid ""
25022515msgstr ""
25032516"import importlib\n"
25042517"\n"
2505- "itertools = importlib.import_module(‘ itertools’ )"
2518+ "itertools = importlib.import_module(' itertools' )"
25062519
25072520#: ../Doc/library/importlib.rst:1550
25082521msgid "Checking if a module can be imported"
@@ -2543,6 +2556,22 @@ msgid ""
25432556"else:\n"
25442557" print(f\" can't find the {name!r} module\" )"
25452558msgstr ""
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}\" )"
25462575
25472576#: ../Doc/library/importlib.rst:1578
25482577msgid "Importing a source file directly"
@@ -2594,6 +2623,25 @@ msgid ""
25942623"# Similar outcome as `import json`.\n"
25952624"json = import_from_path(module_name, file_path)"
25962625msgstr ""
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)"
25972645
25982646#: ../Doc/library/importlib.rst:1611
25992647msgid "Implementing lazy imports"
@@ -2622,6 +2670,22 @@ msgid ""
26222670">>> lazy_typing.TYPE_CHECKING\n"
26232671"False"
26242672msgstr ""
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"
26252689
26262690#: ../Doc/library/importlib.rst:1634
26272691msgid "Setting up an importer"
@@ -2675,6 +2739,26 @@ msgid ""
26752739"# of priority.\n"
26762740"sys.path_hooks.append(SpamPathEntryFinder.path_hook(loader_details))"
26772741msgstr ""
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))"
26782762
26792763#: ../Doc/library/importlib.rst:1668
26802764msgid "Approximating :func:`importlib.import_module`"
@@ -2725,6 +2809,35 @@ msgid ""
27252809" setattr(parent_module, child_name, module)\n"
27262810" return module"
27272811msgstr ""
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"
27282841
27292842#: ../Doc/library/importlib.rst:443
27302843msgid "universal newlines"
0 commit comments