def onReload(self, event):
"""
Reload the code!
"""
if self.testFrame:
self.testFrame.Close()
try:
reload(testApp)
except NameError:
# reload doesn't exist in Python 3.
# Use importlib.reload in Python 3.4+
# or imp.reload in Python 3.0 - 3.3
import importlib
importlib.reload(testApp)
self.showApp()
else:
self.testFrame = None
python类reload()的实例源码
def reload(self):
self.load(self.filename)
def load(self, name, force_reload_if_unmanaged=False):
if name in sys.modules and name not in self.plugins:
# we're getting an already loaded module, which we has not been
# loaded through PluginManager, return it from sys.modules and
# add it to our list
module = sys.modules[name]
if force_reload_if_unmanaged:
importlib.reload(module)
else:
module = importlib.import_module(name)
self.plugins.add(name)
return module
def reload(self):
self.job_handlers = {}
for name in self.plugins:
module = importlib.import_module(name)
importlib.reload(module)
def setup_addon_modules(path, package_name):
"""
Imports and reloads all modules in this addon.
path -- __path__ from __init__.py
package_name -- __name__ from __init__.py
"""
def get_submodule_names(path = path[0], root = ""):
module_names = []
for importer, module_name, is_package in pkgutil.iter_modules([path]):
if is_package:
sub_path = os.path.join(path, module_name)
sub_root = root + module_name + "."
module_names.extend(get_submodule_names(sub_path, sub_root))
else:
module_names.append(root + module_name)
return module_names
def import_submodules(names):
modules = []
for name in names:
modules.append(importlib.import_module("." + name, package_name))
return modules
def reload_modules(modules):
for module in modules:
importlib.reload(module)
names = get_submodule_names()
modules = import_submodules(names)
if reload_event:
reload_modules(modules)
return modules
def reload(module):
"""**DEPRECATED**
Reload the module and return it.
The module must have been successfully imported before.
"""
return importlib.reload(module)
def setup_addon_modules(path, package_name):
"""
Imports and reloads all modules in this addon.
path -- __path__ from __init__.py
package_name -- __name__ from __init__.py
"""
def get_submodule_names(path=path[0], root=""):
module_names = []
for importer, module_name, is_package in pkgutil.iter_modules([path]):
if is_package:
sub_path = path + "\\" + module_name
sub_root = root + module_name + "."
module_names.extend(get_submodule_names(sub_path, sub_root))
else:
module_names.append(root + module_name)
return module_names
def import_submodules(names):
modules = []
for name in names:
modules.append(importlib.import_module("." + name, package_name))
return modules
def reload_modules(modules):
for module in modules:
importlib.reload(module)
names = get_submodule_names()
modules = import_submodules(names)
if reload_event:
reload_modules(modules)
return modules
developer_utils.py 文件源码
项目:Blender-WMO-import-export-scripts
作者: WowDevTools
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def setup_addon_modules(path, package_name, reload):
"""
Imports and reloads all modules in this addon.
path -- __path__ from __init__.py
package_name -- __name__ from __init__.py
Individual modules can define a __reload_order_index__ property which
will be used to reload the modules in a specific order. The default is 0.
"""
def get_submodule_names(path = path[0], root = ""):
module_names = []
for importer, module_name, is_package in pkgutil.iter_modules([path]):
if is_package:
sub_path = os.path.join(path, module_name)
sub_root = root + module_name + "."
module_names.extend(get_submodule_names(sub_path, sub_root))
else:
module_names.append(root + module_name)
return module_names
def import_submodules(names):
modules = []
for name in names:
modules.append(importlib.import_module("." + name, package_name))
return modules
def reload_modules(modules):
modules.sort(key = lambda module: getattr(module, "__reload_order_index__", 0))
for module in modules:
importlib.reload(module)
names = get_submodule_names()
modules = import_submodules(names)
if reload:
reload_modules(modules)
return modules
def load(name):
try:
obj = __import__(name)
reload(obj)
return obj
except:
pass
try:
import importlib
obj = importlib.__import__(name)
importlib.reload(obj)
return obj
except:
pass
def reload_zmirror(self, configs_dict=None):
self.del_temp_var()
import config
importlib.reload(config)
test_config_names = (name for name in dir(self.C) if name[:2] != '__' and name[-2:] != '__')
for config_name in test_config_names:
config_value = getattr(self.C, config_name)
setattr(config, config_name, config_value)
if configs_dict is not None:
for config_name, config_value in configs_dict.items():
setattr(config, config_name, config_value)
import zmirror.cache_system as cache_system
import zmirror.zmirror as zmirror
importlib.reload(cache_system)
importlib.reload(zmirror)
zmirror.app.config['TESTING'] = True
# ?????????, ? del_temp_var() ???
if hasattr(self.C, "my_host_port"):
port = getattr(self.C, "my_host_port", None)
my_host_name = getattr(self.C, "my_host_name", "127.0.0.1")
if port is not None:
self.C.my_host_name_no_port = my_host_name
self.C.my_host_name = self.C.my_host_name_no_port + ":" + str(port)
else:
self.C.my_host_name_no_port = my_host_name
elif hasattr(self.C, "my_host_name"):
self.C.my_host_name_no_port = self.C.my_host_name
self.client = zmirror.app.test_client() # type: FlaskClient
self.app = zmirror.app # type: Flask
self.zmirror = zmirror
def _reload(self, *, cog_name: str):
"""Reloads a module
Example: reload audio"""
module = cog_name.strip()
if "modules." not in module:
module = "modules." + module
try:
self._unload_cog(module, reloading=True)
except:
pass
try:
self._load_cog(module)
except CogNotFoundError:
await ctx.send("That cog cannot be found.")
except NoSetupError:
await ctx.send("That cog does not have a setup function.")
except CogLoadError as e:
logger.exception(e)
traceback.print_exc()
await ctx.send("That cog could not be loaded. Check your"
" console or logs for more information.")
else:
set_cog(module, True)
await self.disable_commands()
await ctx.send("The cog has been reloaded.")
def _load_cog(self, cogname):
if not self._does_cogfile_exist(cogname):
raise CogNotFoundError(cogname)
try:
mod_obj = importlib.import_module(cogname)
importlib.reload(mod_obj)
self.bot.load_extension(mod_obj.__name__)
except SyntaxError as e:
raise CogLoadError(*e.args)
except:
raise
def test():
import importlib
importlib.reload(p)
configs = p.generate_configs(4)
puzzles = p.generate_gpu(configs[-30:])
plot_image(p.batch_unswirl(puzzles)[-1], "lightsout_twisted_untwisted.png")
plot_image(p.batch_unswirl(puzzles)[-1].round(),"lightsout_twisted_untwisted__r.png")
plot_image((p.batch_unswirl(puzzles)[-1]+0.18).round(), "lightsout_twisted_untwisted__x.png")
def test_unbounded_frames(self):
from unittest.mock import patch
from pyspark.sql import functions as F
from pyspark.sql import window
import importlib
df = self.spark.range(0, 3)
def rows_frame_match():
return "ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" in df.select(
F.count("*").over(window.Window.rowsBetween(-sys.maxsize, sys.maxsize))
).columns[0]
def range_frame_match():
return "RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING" in df.select(
F.count("*").over(window.Window.rangeBetween(-sys.maxsize, sys.maxsize))
).columns[0]
with patch("sys.maxsize", 2 ** 31 - 1):
importlib.reload(window)
self.assertTrue(rows_frame_match())
self.assertTrue(range_frame_match())
with patch("sys.maxsize", 2 ** 63 - 1):
importlib.reload(window)
self.assertTrue(rows_frame_match())
self.assertTrue(range_frame_match())
with patch("sys.maxsize", 2 ** 127 - 1):
importlib.reload(window)
self.assertTrue(rows_frame_match())
self.assertTrue(range_frame_match())
importlib.reload(window)
def importer (directory, libpath, module = None):
fn = not libpath.endswith (".py") and libpath + ".py" or libpath
modpath = os.path.join (directory, fn)
hname = fn.split (".")[0]
p = directory.replace ("\\", "/")
if p.find (":") !=- 1:
p = "/" + p.replace (":", "")
while 1:
if "skitai.mounted." + hname in sys.modules:
p, l = os.path.split (p)
if not l:
raise SystemError ('module %s already imported, use reload')
hname = l + "." + hname
else:
hname = "skitai.mounted." + hname
break
loader = importlib.machinery.SourceFileLoader(hname, modpath)
mod = loader.load_module ()
return mod, mod.__file__
def reload_settings(self):
"""
Reload settings module with cleanup to restore it.
Returns:
dict: dictionary of the newly reloaded settings ``vars``
"""
importlib.reload(sys.modules['micromasters.settings'])
# Restore settings to original settings after test
self.addCleanup(importlib.reload, sys.modules['micromasters.settings'])
return vars(sys.modules['micromasters.settings'])
def reload_settings(self):
"""
Reload settings module with cleanup to restore it.
Returns:
dict: dictionary of the newly reloaded settings ``vars``
"""
importlib.reload(sys.modules['sga_lti.settings'])
# Restore settings to original settings after test
self.addCleanup(importlib.reload, sys.modules['sga_lti.settings'])
return vars(sys.modules['sga_lti.settings'])
def reload(module):
"""**DEPRECATED**
Reload the module and return it.
The module must have been successfully imported before.
"""
return importlib.reload(module)
def reset_all(*, reload_scripts=False):
"""
Sets the addon state based on the user preferences.
"""
import sys
# initializes addons_fake_modules
modules_refresh()
# RELEASE SCRIPTS: official scripts distributed in Blender releases
paths_list = paths()
for path in paths_list:
_bpy.utils._sys_path_ensure(path)
for mod_name, mod_path in _bpy.path.module_names(path):
is_enabled, is_loaded = check(mod_name)
# first check if reload is needed before changing state.
if reload_scripts:
import importlib
mod = sys.modules.get(mod_name)
if mod:
importlib.reload(mod)
if is_enabled == is_loaded:
pass
elif is_enabled:
enable(mod_name)
elif is_loaded:
print("\taddon_utils.reset_all unloading", mod_name)
disable(mod_name)
def register():
####################
# F8 - key
import importlib
importlib.reload(ms3d_ui)
# F8 - key
####################
ms3d_ui.register()
register_module(__name__)
INFO_MT_file_export.append(Ms3dExportOperator.menu_func)
INFO_MT_file_import.append(Ms3dImportOperator.menu_func)