Python sys.modules() Examples
The following are 30
code examples of sys.modules().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sys
, or try the search function
.

Example #1
Source File: conf.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 7 votes |
def mock_pywin32(): """Mock pywin32 module. Resulting in Linux hosts, including ReadTheDocs, and other environments that don't have pywin32 can generate the docs properly including the PDF version. See: http://read-the-docs.readthedocs.org/en/latest/faq.html#i-get-import-errors-on-libraries-that-depend-on-c-modules """ if try_import('win32api'): return from unittest import mock MOCK_MODULES = [ 'win32api', 'win32con', 'win32event', 'win32service', 'win32serviceutil', ] for mod_name in MOCK_MODULES: sys.modules[mod_name] = mock.MagicMock()
Example #2
Source File: reprconf.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def build_Name(self, o): name = o.id if name == 'None': return None if name == 'True': return True if name == 'False': return False # See if the Name is a package or module. If it is, import it. try: return modules(name) except ImportError: pass # See if the Name is in builtins. try: return getattr(builtins, name) except AttributeError: pass raise TypeError('unrepr could not resolve the name %s' % repr(name))
Example #3
Source File: reprconf.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def attributes(full_attribute_name): """Load a module and retrieve an attribute of that module.""" # Parse out the path, module, and attribute last_dot = full_attribute_name.rfind('.') attr_name = full_attribute_name[last_dot + 1:] mod_path = full_attribute_name[:last_dot] mod = modules(mod_path) # Let an AttributeError propagate outward. try: attr = getattr(mod, attr_name) except AttributeError: raise AttributeError("'%s' object has no attribute '%s'" % (mod_path, attr_name)) # Return a reference to the attribute. return attr
Example #4
Source File: adapter.py From django-click with MIT License | 6 votes |
def __call__(self, func): module = sys.modules[func.__module__] # Get the command name as Django expects it self.name = func.__module__.rsplit(".", 1)[-1] # Build the click command decorators = [ click.command(name=self.name, cls=self.cls, **self.kwargs), ] + self.get_params(self.name) for decorator in reversed(decorators): func = decorator(func) # Django expects the command to be callable (it instantiates the class # pointed at by the `Command` module-level property)... # ...let's make it happy. module.Command = lambda: func return func
Example #5
Source File: loader.py From friendly-telegram with GNU Affero General Public License v3.0 | 6 votes |
def unloadmodcmd(self, message): """Unload module by class name""" args = utils.get_args(message) if not args: await utils.answer(message, self.strings["what_class"]) return clazz = args[0] worked = self.allmodules.unload_module(clazz) without_prefix = [] for mod in worked: assert mod.startswith("friendly-telegram.modules."), mod without_prefix += [unescape_percent(mod[len("friendly-telegram.modules."):])] it = set(self._db.get(__name__, "loaded_modules", [])).difference(without_prefix) self._db.set(__name__, "loaded_modules", list(it)) it = set(self._db.get(__name__, "unloaded_modules", [])).union(without_prefix) self._db.set(__name__, "unloaded_modules", list(it)) if worked: await utils.answer(message, self.strings["unloaded"]) else: await utils.answer(message, self.strings["not_unloaded"])
Example #6
Source File: __init__.py From neuropythy with GNU Affero General Public License v3.0 | 6 votes |
def reload_neuropythy(): ''' reload_neuropythy() reloads all of the modules of neuropythy and returns the reloaded neuropythy module. This is similar to reload(neuropythy) except that it reloads all the neuropythy submodules prior to reloading neuropythy. Example: import neuropythy as ny # ... some nonsense that breaks the library ... ny = ny.reload_neuropythy() ''' import sys, six if not six.PY2: try: from importlib import reload except Exception: from imp import reload for mdl in submodules: if mdl in sys.modules: sys.modules[mdl] = reload(sys.modules[mdl]) return reload(sys.modules['neuropythy'])
Example #7
Source File: torch.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def _init_torch_module(): """List and add all the torch backed ndarray functions to current module.""" plist = ctypes.POINTER(FunctionHandle)() size = ctypes.c_uint() check_call(_LIB.MXListFunctions(ctypes.byref(size), ctypes.byref(plist))) module_obj = sys.modules[__name__] for i in range(size.value): hdl = FunctionHandle(plist[i]) function = _make_torch_function(hdl) # if function name starts with underscore, register as static method of NDArray if function is not None: setattr(module_obj, function.__name__, function) # Initialize the NDArray module
Example #8
Source File: settings.py From Paradrop with Apache License 2.0 | 6 votes |
def iterate_module_attributes(module): """ Iterate over the attributes in a module. This is a generator function. Returns (name, value) tuples. """ for name in dir(module): # Ignore fields marked as private or builtin. if name.startswith('_'): continue value = getattr(module, name) # Ignore callable objects (functions) and loaded modules. if callable(value) or isinstance(value, types.ModuleType): continue yield (name, value)
Example #9
Source File: settings.py From Paradrop with Apache License 2.0 | 6 votes |
def load_from_file(path): """ Load settings from an INI file. This will check the configuration file for a lowercase version of all of the settings in this module. It will look in a section called "base". The example below will set PORTAL_SERVER_PORT. [base] portal_server_port = 4444 """ config = configparser.SafeConfigParser() config.read(path) mod = sys.modules[__name__] for name, _ in iterate_module_attributes(mod): # Check if lowercase version exists in the file and load the # appropriately-typed value. key = name.lower() if config.has_option(constants.BASE_SETTINGS_SECTION, key): value = config.get(constants.BASE_SETTINGS_SECTION, key) setattr(mod, name, parseValue(value))
Example #10
Source File: settings.py From Paradrop with Apache License 2.0 | 6 votes |
def updatePaths(configHomeDir, runtimeHomeDir="/var/run/paradrop"): # Get a handle to our settings defined above mod = sys.modules[__name__] mod.CONFIG_HOME_DIR = configHomeDir mod.RUNTIME_HOME_DIR = runtimeHomeDir mod.FC_CHUTESTORAGE_FILE = os.path.join(mod.CONFIG_HOME_DIR, "chutes") mod.EXTERNAL_DATA_DIR = os.path.join(mod.CONFIG_HOME_DIR, "chute-data/{chute}/") mod.EXTERNAL_SYSTEM_DIR = os.path.join(runtimeHomeDir, "system", "{chute}") mod.LOG_DIR = os.path.join(mod.CONFIG_HOME_DIR, "logs/") mod.KEY_DIR = os.path.join(mod.CONFIG_HOME_DIR, "keys/") mod.MISC_DIR = os.path.join(mod.CONFIG_HOME_DIR, "misc/") mod.CONFIG_FILE = os.path.join(mod.CONFIG_HOME_DIR, "config") mod.HOST_CONFIG_FILE = os.path.join(mod.CONFIG_HOME_DIR, "hostconfig.yaml") mod.DEFAULT_HOST_CONFIG_FILE = os.path.join(mod.CONFIG_HOME_DIR, "hostconfig.default.yaml") mod.UCI_CONFIG_DIR = os.path.join(mod.CONFIG_HOME_DIR, "uci/config.d/") mod.UCI_BACKUP_DIR = os.path.join(mod.CONFIG_HOME_DIR, "uci/config-backup.d/") mod.PDCONFD_WRITE_DIR = os.path.join(mod.RUNTIME_HOME_DIR, 'pdconfd')
Example #11
Source File: test_artifact.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_iter_builders_side_effect(self): # inject dummy module and add cleanup mod = ModuleType('calmjs_testing_dummy') mod.complete = generic_builder self.addCleanup(sys.modules.pop, 'calmjs_testing_dummy') sys.modules['calmjs_testing_dummy'] = mod working_dir = utils.mkdtemp(self) utils.make_dummy_dist(self, ( ('entry_points.txt', '\n'.join([ '[calmjs.artifacts]', 'artifact.js = calmjs_testing_dummy:complete', ])), ), 'app', '1.0', working_dir=working_dir) mock_ws = WorkingSet([working_dir]) registry = ArtifactRegistry('calmjs.artifacts', _working_set=mock_ws) registry.update_artifact_metadata('app', {}) root = join(working_dir, 'app-1.0.egg-info', 'calmjs_artifacts') self.assertFalse(exists(root)) ep, toolchain, spec = next(registry.iter_builders_for('app')) self.assertFalse(exists(root)) # directory only created after the toolchain is executed toolchain(spec) self.assertTrue(exists(root))
Example #12
Source File: test_indexer.py From calmjs with GNU General Public License v2.0 | 6 votes |
def test_missing_distribution(self): d_egg_root = join(mkdtemp(self), 'dummyns') make_dummy_dist(self, (( 'namespace_packages.txt', 'not_ns\n', ), ( 'entry_points.txt', '[dummyns]\n' 'dummyns = dummyns:attr\n', ),), 'dummyns', '2.0', working_dir=d_egg_root) working_set = pkg_resources.WorkingSet([ d_egg_root, self.ds_egg_root, ]) dummyns_ep = next(working_set.iter_entry_points('dummyns')) with pretty_logging(stream=StringIO()) as fd: p = indexer.resource_filename_mod_entry_point( 'dummyns', dummyns_ep) # not stubbed working_set, so this is derived using fallback # value from the sys.modules['dummyns'] location self.assertEqual(normcase(p), normcase(self.dummyns_path)) self.assertIn("distribution 'dummyns 2.0' not found", fd.getvalue())
Example #13
Source File: _patch.py From quart with MIT License | 6 votes |
def _patch_modules() -> None: if "flask" in sys.modules: raise ImportError("Cannot mock flask, already imported") # Create a set of Flask modules, prioritising those within the # flask_patch namespace over simple references to the Quart # versions. flask_modules = {} for name, module in list(sys.modules.items()): if name.startswith("quart.flask_patch._"): continue elif name.startswith("quart.flask_patch"): setattr(module, "_QUART_PATCHED", True) flask_modules[name.replace("quart.flask_patch", "flask")] = module elif name.startswith("quart.") and not name.startswith("quart.serving"): flask_name = name.replace("quart.", "flask.") if flask_name not in flask_modules: flask_modules[flask_name] = _convert_module(flask_name, module) sys.modules.update(flask_modules)
Example #14
Source File: helpers.py From quart with MIT License | 6 votes |
def find_package(name: str) -> Tuple[Optional[Path], Path]: """Finds packages install prefix (or None) and it's containing Folder """ module = name.split(".")[0] loader = pkgutil.get_loader(module) if name == "__main__" or loader is None: package_path = Path.cwd() else: if hasattr(loader, "get_filename"): filename = loader.get_filename(module) # type: ignore else: __import__(name) filename = sys.modules[name].__file__ package_path = Path(filename).resolve().parent if hasattr(loader, "is_package"): is_package = loader.is_package(module) # type: ignore if is_package: package_path = Path(package_path).resolve().parent sys_prefix = Path(sys.prefix).resolve() try: package_path.relative_to(sys_prefix) except ValueError: return None, package_path else: return sys_prefix, package_path
Example #15
Source File: keplerSTM_indprop.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, x0, mu, epsmult = 4.0, noc = False): #determine number of planets and validate input nplanets = x0.size/6. if (nplanets - np.floor(nplanets) > 0): raise Exception('The length of x0 must be a multiple of 6.') if (mu.size != nplanets): raise Exception('The length of mu must be the length of x0 divided by 6') self.nplanets = int(nplanets) self.mu = np.squeeze(mu) if (self.mu.size == 1): self.mu = np.array(mu) self.epsmult = epsmult if not(noc) and ('EXOSIMS.util.KeplerSTM_C.CyKeplerSTM' in sys.modules): self.havec = True self.x0 = np.squeeze(x0) else: self.havec = False self.updateState(np.squeeze(x0))
Example #16
Source File: interact_test.py From glazier with Apache License 2.0 | 6 votes |
def testKeystroke(self, sleep): msvcrt = mock.Mock() msvcrt.kbhit.return_value = False sys.modules['msvcrt'] = msvcrt # no reply result = interact.Keystroke('mesg', timeout=1) self.assertEqual(result, None) self.assertEqual(sleep.call_count, 1) # special character reply msvcrt.kbhit.side_effect = iter([False, False, False, False, True]) msvcrt.getch.return_value = b'0xe0' result = interact.Keystroke('mesg', timeout=100) self.assertEqual(result, '0xe0') self.assertEqual(sleep.call_count, 6) # reply msvcrt.kbhit.side_effect = iter([False, False, False, False, True]) msvcrt.getch.return_value = b'v' result = interact.Keystroke('mesg', timeout=100) self.assertEqual(result, 'v') self.assertEqual(sleep.call_count, 11) # validation miss msvcrt.kbhit.side_effect = iter([True]) result = interact.Keystroke('mesg', validator='[0-9]') self.assertEqual(result, None)
Example #17
Source File: reloader_helpers.py From sanic with MIT License | 6 votes |
def _iter_module_files(): """This iterates over all relevant Python files. It goes through all loaded files from modules, all files in folders of already loaded modules as well as all files reachable through a package. """ # The list call is necessary on Python 3 in case the module # dictionary modifies during iteration. for module in list(sys.modules.values()): if module is None: continue filename = getattr(module, "__file__", None) if filename: old = None while not os.path.isfile(filename): old = filename filename = os.path.dirname(filename) if filename == old: break else: if filename[-4:] in (".pyc", ".pyo"): filename = filename[:-1] yield filename
Example #18
Source File: cloudpickle.py From pywren-ibm-cloud with Apache License 2.0 | 6 votes |
def _whichmodule(obj, name): """Find the module an object belongs to. This function differs from ``pickle.whichmodule`` in two ways: - it does not mangle the cases where obj's module is __main__ and obj was not found in any module. - Errors arising during module introspection are ignored, as those errors are considered unwanted side effects. """ module_name = getattr(obj, '__module__', None) if module_name is not None: return module_name # Protect the iteration by using a list copy of sys.modules against dynamic # modules that trigger imports of other modules upon calls to getattr. for module_name, module in list(sys.modules.items()): if module_name == '__main__' or module is None: continue try: if _getattribute(module, name)[0] is obj: return module_name except Exception: pass return None
Example #19
Source File: suite.py From jawfish with MIT License | 6 votes |
def _handleModuleTearDown(self, result): previousModule = self._get_previous_module(result) if previousModule is None: return if result._moduleSetUpFailed: return try: module = sys.modules[previousModule] except KeyError: return tearDownModule = getattr(module, 'tearDownModule', None) if tearDownModule is not None: _call_if_exists(result, '_setupStdout') try: tearDownModule() except Exception as e: if isinstance(result, _DebugResult): raise errorName = 'tearDownModule (%s)' % previousModule self._addClassOrModuleLevelException(result, e, errorName) finally: _call_if_exists(result, '_restoreStdout')
Example #20
Source File: test_setup.py From unicorn-hat-hd with MIT License | 5 votes |
def test_setup(): """Test that the library sets up correctly with numpy and spidev.""" sys.modules['spidev'] = mock.MagicMock() import unicornhathd unicornhathd.setup()
Example #21
Source File: test_setup.py From unicorn-hat-hd with MIT License | 5 votes |
def test_brightness(): """Test brightness API change and alias.""" sys.modules['spidev'] = mock.MagicMock() import unicornhathd unicornhathd.setup() unicornhathd.set_brightness(0.5) assert unicornhathd.set_brightness == unicornhathd.brightness
Example #22
Source File: test_setup.py From unicorn-hat-hd with MIT License | 5 votes |
def test_rotation(): """Test rotation API change and alias.""" sys.modules['spidev'] = mock.MagicMock() import unicornhathd unicornhathd.setup() unicornhathd.set_rotation(90) assert unicornhathd.set_rotation == unicornhathd.rotation
Example #23
Source File: test_setup.py From unicorn-hat-hd with MIT License | 5 votes |
def test_tuple_colour(): """Test valid text colour.""" sys.modules['spidev'] = mock.MagicMock() import unicornhathd unicornhathd.setup() unicornhathd.set_pixel(0, 0, (255, 0, 0)) assert tuple(unicornhathd._buf[0, 0]) == (255, 0, 0)
Example #24
Source File: test_setup.py From unicorn-hat-hd with MIT License | 5 votes |
def test_valid_text_colour(): """Test valid text colour.""" sys.modules['spidev'] = mock.MagicMock() import unicornhathd unicornhathd.setup() unicornhathd.set_pixel(0, 0, "Teal") assert tuple(unicornhathd._buf[0, 0]) == unicornhathd.COLORS['teal']
Example #25
Source File: _hook.py From apm-python-agent-principle with MIT License | 5 votes |
def load_module(self, fullname): print('load_module {}'.format(fullname)) if fullname in sys.modules: return sys.modules[fullname] # 先从 sys.meta_path 中删除自定义的 finder # 防止下面执行 import_module 的时候再次触发此 finder # 从而出现递归调用的问题 finder = sys.meta_path.pop(0) # 导入 module module = importlib.import_module(fullname) module_hook(fullname, module) sys.meta_path.insert(0, finder) return module
Example #26
Source File: hook.py From apm-python-agent-principle with MIT License | 5 votes |
def load_module(self, fullname): print('load_module {}'.format(fullname)) if fullname in sys.modules: return sys.modules[fullname] # 先从 sys.meta_path 中删除自定义的 finder # 防止下面执行 import_module 的时候再次触发此 finder # 从而出现递归调用的问题 finder = sys.meta_path.pop(0) # 导入 module module = importlib.import_module(fullname) module_hook(fullname, module) sys.meta_path.insert(0, finder) return module
Example #27
Source File: meta_path1.py From apm-python-agent-principle with MIT License | 5 votes |
def load_module(self, fullname): print('load_module {}'.format(fullname)) sys.modules[fullname] = sys return sys
Example #28
Source File: hook.py From apm-python-agent-principle with MIT License | 5 votes |
def load_module(self, fullname): print('load_module {}'.format(fullname)) if fullname in sys.modules: return sys.modules[fullname] # 先从 sys.meta_path 中删除自定义的 finder # 防止下面执行 import_module 的时候再次触发此 finder # 从而出现递归调用的问题 finder = sys.meta_path.pop(0) # 导入 module module = importlib.import_module(fullname) module_hook(fullname, module) sys.meta_path.insert(0, finder) return module
Example #29
Source File: plugins.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def sysfiles(self): """Return a Set of sys.modules filenames to monitor.""" search_mod_names = filter( re.compile(self.match).match, list(sys.modules.keys()), ) mods = map(sys.modules.get, search_mod_names) return set(filter(None, map(self._file_for_module, mods)))
Example #30
Source File: reprconf.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def modules(modulePath): """Load a module and retrieve a reference to that module.""" __import__(modulePath) return sys.modules[modulePath]