Here are the examples of the python api sys.modules.update taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
9 Examples
3
Example 1
def setUp(self):
modules = {
'win32api': mock.Mock(),
'win32com': mock.Mock(),
'win32com.shell': mock.Mock(),
'win32serviceutil': mock.Mock(),
'pywintypes': mock.Mock(),
'win32service': mock.Mock()
}
sys.modules.update(modules)
from chalmers.service.win32_system_service import Win32SystemService
self.modules = modules
self.Win32SystemService = Win32SystemService
0
Example 2
def setUp(self):
self.settings = imp.new_module('test_settings')
sys.modules.update(test_settings=self.settings)
setattr(self.settings, 'UNITS', 'override')
setattr(self.settings, 'NEW_OPTION', 'new')
0
Example 3
def __exit__(self, *ignore_exc):
sys.modules.update(self.original_modules)
0
Example 4
def tearDown(self):
sys.modules.update(self.mods)
0
Example 5
Project: CNCGToolKit Source File: Qt.py
def init():
"""Try loading each binding in turn
Please note: the entire Qt module is replaced with this code:
sys.modules["Qt"] = binding()
This means no functions or variables can be called after
this has executed.
For debugging and testing, this module may be accessed
through `Qt.__shim__`.
"""
preferred = os.getenv("QT_PREFERRED_BINDING")
verbose = os.getenv("QT_VERBOSE") is not None
bindings = (_pyside2, _pyqt5, _pyside, _pyqt4)
if preferred:
# Internal flag (used in installer)
if preferred == "None":
self.__wrapper_version__ = self.__version__
return
preferred = preferred.split(os.pathsep)
available = {
"PySide2": _pyside2,
"PyQt5": _pyqt5,
"PySide": _pyside,
"PyQt4": _pyqt4
}
try:
bindings = [available[binding] for binding in preferred]
except KeyError:
raise ImportError(
"Available preferred Qt bindings: "
"\n".join(preferred)
)
for binding in bindings:
_log("Trying %s" % binding.__name__, verbose)
try:
binding = binding()
except ImportError as e:
_log(" - ImportError(\"%s\")" % e, verbose)
continue
else:
# Reference to this module
binding.__shim__ = self
binding.QtCompat = self
sys.modules.update({
__name__: binding,
# Fix #133, `from Qt.QtWidgets import QPushButton`
__name__ + ".QtWidgets": binding.QtWidgets
})
return
# If not binding were found, throw this error
raise ImportError("No Qt binding were found.")
0
Example 6
def mock_modules():
from mock import MagicMock
try:
import yappi # noqa
except ImportError:
MOCK_MODULES = ['yappi']
else:
MOCK_MODULES = []
try:
import zmq # noqa
except ImportError:
MOCK_MODULES.append('zmq')
try:
import line_profiler # noqa
except ImportError:
MOCK_MODULES.append('line_profiler')
class Mock(MagicMock):
@classmethod
def __getattr__(cls, name):
return Mock()
def __call__(self, *args, **kwards):
return Mock()
def __name__(self, other):
return 'Mock'
sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)
print 'mocking {}'.format(MOCK_MODULES)
0
Example 7
def mock_modules():
""" Mock missing modules if necessary """
MOCK_MODULES = []
MOCK_TYPES = []
try:
import cffi
except ImportError:
MOCK_MODULES = ['cffi']
MOCK_TYPES = []
else:
del cffi
TYPES = {
mock_type: type(mock_type, bases, {'__module__': path})
for path, mock_type, bases in MOCK_TYPES}
class DocMock(object):
def __init__(self, *args, **kwds):
if '__doc_mocked_name__' in kwds:
self.__docmock_name__ = kwds['__docmocked_name__']
else:
self.__docmock_name__ = 'Unknown'
def __getattr__(self, name):
if name in ('__file__', '__path__'):
return '/dev/null'
elif name == '__all__':
return []
else:
return TYPES.get(name, DocMock(__docmock_name__=name))
def __call__(self, *args, **kwards):
return DocMock()
def __iter__(self):
return self
def __next__(self):
raise StopIteration()
def next(self):
raise StopIteration()
@property
def __name__(self):
return self.__docmock_name__
def __repr__(self):
return '<DocMock.{}>'.format(self.__name__)
sys.modules.update(
(mod_name, DocMock(mocked_name=mod_name)) for mod_name in MOCK_MODULES)
print('mocking modules {} and types {}'.format(MOCK_MODULES, MOCK_TYPES))
0
Example 8
Project: Qt.py Source File: Qt.py
def init():
"""Try loading each binding in turn
Please note: the entire Qt module is replaced with this code:
sys.modules["Qt"] = binding()
This means no functions or variables can be called after
this has executed.
For debugging and testing, this module may be accessed
through `Qt.__shim__`.
"""
bindings = (_pyside2, _pyqt5, _pyside, _pyqt4)
if QT_PREFERRED_BINDING:
# Internal flag (used in installer)
if QT_PREFERRED_BINDING == "None":
self.__wrapper_version__ = self.__version__
return
preferred = QT_PREFERRED_BINDING.split(os.pathsep)
available = {
"PySide2": _pyside2,
"PyQt5": _pyqt5,
"PySide": _pyside,
"PyQt4": _pyqt4
}
try:
bindings = [available[binding] for binding in preferred]
except KeyError:
raise ImportError(
"Available preferred Qt bindings: "
"\n".join(preferred)
)
for binding in bindings:
_log("Trying %s" % binding.__name__, QT_VERBOSE)
try:
binding = binding()
except ImportError as e:
_log(" - ImportError(\"%s\")" % e, QT_VERBOSE)
continue
else:
# Reference to this module
binding.QtCompat = self
binding.__shim__ = self # DEPRECATED
sys.modules.update({
__name__: binding,
# Fix #133, `from Qt.QtWidgets import QPushButton`
__name__ + ".QtWidgets": binding.QtWidgets
})
return
# If not binding were found, throw this error
raise ImportError("No Qt binding were found.")
0
Example 9
def tearDown(self):
sys.modules.update(self._prev)