def set_variant(value): ''' Mitsuba 2 can be compiled to a great variety of different variants (e.g. 'scalar_rgb', 'gpu_autodiff_spectral_polarized', etc.) that each have their own Python bindings in addition to generic/non-templated code that lives in yet another module. Writing various different prefixes many times in import statements such as from mitsuba.render_gpu_autodiff_spectral_ext import Integrator from mitsuba.core_ext import FileStream can get rather tiring. For this reason, Mitsuba uses /virtual/ Python modules that dynamically resolve import statements to the right destination. The desired Mitsuba variant should be specified via this function. The above example then simplifies to import mitsuba mitsuba.set_variant('gpu_autodiff_spectral_polarized') from mitsuba.render import Integrator from mitsuba.core import FileStream The variant name can be changed at any time and will only apply to future imports. The variant name is a per-thread property, hence multiple independent threads can execute code in separate variants. ''' if variant() == value: return if value not in variants(): raise ImportError('Requested an unsupported variant "%s". The ' 'following variants are available: %s.' % (value, ", ".join(variants()))) modules = None try: modules = { 'mitsuba.core': (_import('mitsuba.core_ext'), _import('mitsuba.core_' + value + '_ext')), 'mitsuba.render': (_import('mitsuba.render_ext'), _import('mitsuba.render_' + value + '_ext')) } except ImportError as e: if not str(e).startswith('No module named'): raise pass if modules is None: raise ImportError('Mitsuba variant "%s" not found.' % value) _tls.modules = modules _tls.variant = value
def execute(self, inp): inp = inp.split() # print(inp) inp = make_s(replace_vars(inp)) inp = inp.split() # print(inp) # Get hidden commands as well f_list = get_func_list(True) try: f = inp[0] inp.pop(0) except IndexError: return if f in f_list: mod = 'bin.' + f m = _import(mod) try: m.main(inp) except TypeError: try: m.main() except TypeError: err(1) except AttributeError: err(1, f) elif f not in f_list: analyze(f)
def __get_dataset(): """ Loads all templates' intents and words from installed applications returns a dictionary with the same format as that of the App load_template method but rather for all installed apps. """ dataset = {} # get the templates' data for all installed # application and update to the dataset for app in INSTALLED_APPS: # make a instance of the app App then update # the dataset with the result from the instance's # laod_template method app_obj = _import(".".join(["apps", app])).App() dataset.update(app_obj.load_template()) return dataset
async def on_message(message): text_channel_list = [] for server in client.guilds: for channel in server.channels: if channel in server.text_channels: text_channel_list.append(channel) if message.content.startswith(CMDCHAR) and message.author != client.user: cmd, *args = message.content[1:].split(' ') print('Command Received: ' + cmd) for root, dirs, files in os.walk(COMMAND_PATH): cmd_file = cmd + '.py' print(files, cmd_file) if cmd_file in files: module = _import('Commands.' + cmd) for c in text_channel_list: await getattr(module, cmd)(client, c, *args) print("Done") break
""" Mitsuba Python extension library """ import types import sys import threading from importlib import import_module as _import if sys.version_info < (3, 6): raise ImportError("Mitsuba requires Python 3.6 or greater.") try: _import('mitsuba.core_ext') _import('mitsuba.render_ext') _tls = threading.local() except ImportError as e: from .config import PYTHON_EXECUTABLE import sys extra_msg = "" if 'Symbol not found' in str(e): pass elif PYTHON_EXECUTABLE != sys.executable: extra_msg = ("You're likely trying to use Mitsuba within a Python " "binary (%s) that is different from the one for which " "the native module was compiled (%s).") % ( sys.executable, PYTHON_EXECUTABLE) exc = ImportError("The 'mitsuba' native modules could not be " "imported. %s" % extra_msg) exc.__cause__ = e
def import_module(name, package=None): if not PY2 and package is not None: _import(package) return _import(name, package=package)
import os as _os from importlib import import_module as _import from .model import Model _dir = _os.path.dirname(_os.path.realpath(__file__)) _globals = globals() for _folder in _os.listdir(_dir): if _os.path.isfile(_os.path.join(_dir, _folder)): continue if _folder[0] == '_': continue _module_name = _os.path.splitext(_folder)[0] _class_name = ''.join(word.title() for word in _module_name.split('_')) _import('.' + _module_name, __name__) _module = _import('.' + _module_name, __name__ + '.' + _module_name) _globals[_class_name] = getattr(_module, _class_name)
def main(argv): if len(argv) < 1 or '-h' in argv: _help() return # The shell doesnt send the # command name in the arg list # so the next line is not needed # anymore # argv.pop(0) #remove the command # get the path to the file inp = make_s(argv) path = get_path() + inp # check if it is a file if inp in os.listdir(get_path()) and not os.path.isfile(path): err(3, add=inp + ' is a directory') return if inp not in os.listdir(get_path()): err(2, path) return # if is file read data with open(path) as f: data = f.readlines() # Now try executing each line for i in data: i = i.split() f_list = get_func_list() try: f = i[0] except IndexError: continue if f in f_list: i.pop(0) mod = 'bin.' + f m = _import(mod) try: m.main(i) except TypeError: m.main() except AttributeError: err(1, f) elif f not in f_list: i = make_s(i) try: e = eval(i) if e != None: print(e) except SyntaxError: try: exec(i) except SyntaxError as e: print(e) return except NameError as e: err(0, i) except ZeroDivisionError: print('Cannot divide by zero') except TypeError as e: print(e) except ValueError as e: print(e) except AttributeError as e: print(e)
def import_from(stage_module: str, stage_class: str) -> type: from importlib import __import__ as _import imported = _import(stage_module, globals(), locals(), [stage_class], 0) return imported.__dict__[stage_class]
# -*- coding: utf-8 -*- from copy import deepcopy as _deepcopy from collections import OrderedDict as _ODict from importlib import import_module as _import from os import remove from shutil import move from vipster.settings import _paramdict _formats = ["xyz", "pwInput", "pwOutput", "lammpsData", "lammpsCustom", "cube", "empire", "aimall", "cpmd", "turbomole", "xsf", "mol2"] _plugins = [] for i in _formats: _plugins.append(_import(".ioplugins." + i, package="vipster")) _indict = _ODict([(i.argument, i.parser) for i in _plugins]) _outdict = _ODict([(i.argument, i.writer) for i in _plugins if i.writer]) _guiInNames = _ODict([(i.name, i.argument) for i in _plugins]) _guiOutNames = _ODict([(i.name, i.argument) for i in _plugins if i.writer]) _defaultParams = _ODict([(i.argument, i.param) for i in _plugins if i.param]) for i in _defaultParams: for j in _defaultParams[i]: if j not in _paramdict[i]: _paramdict[i][j] = _defaultParams[i][j] inFormats = _indict.keys() outFormats = _outdict.keys() def readFile(filename, fmt): """ Read and parse a given file
# -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals from importlib import import_module as _import """ The settings here are all sensible defaults, but can be overriden in settings.py """ # Allow the django project to decide on sensible defaults for crypto CRYPTO_KDF_ITERATIONS = _import('django.contrib.auth.hashers').PBKDF2PasswordHasher.iterations DEFAULT_PASSWORD_LENGTH = 32 # Defaults used in creating members on the portal. DEFAULT_GROUP_NAME = 'Default' DEFAULT_ROLE_NAME = 'Normal' DEFAULT_PROXY_NAME = 'No Proxy' DEFAULT_LOCATION_TAG = 'Default' # The portal will reject this email. It must overridden. DEFAULT_MANAGER_EMAIL = 'root@localhost' # dictionary of passwords keyed by username used to authenticate a portal # attemping webservices authentication. # see views.wsauth. WSAUTH_PORTAL_CREDENTIALS = {}
from importlib import import_module as _import _import('nanogui.nanogui_ext') def get_cmake_dir(): from os import path file_dir = path.abspath(path.dirname(__file__)) cmake_path = path.join(file_dir, "share", "cmake", "nanogui") if not path.exists(cmake_path): raise ImportError("Cannot find NanoGUI CMake directory") return cmake_path