Python importlib.import_module() Examples
The following are 30
code examples of importlib.import_module().
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
importlib
, or try the search function
.
Example #1
Source File: ingester.py From drydock with Apache License 2.0 | 7 votes |
def enable_plugin(self, plugin): """Enable a ingester plugin for use parsing design documents. :params plugin: - A string naming a class object denoting the ingester plugin to be enabled """ if plugin is None or plugin == '': self.log.error("Cannot have an empty plugin string.") try: (module, x, classname) = plugin.rpartition('.') if module == '': raise Exception() mod = importlib.import_module(module) klass = getattr(mod, classname) self.registered_plugin = klass() except Exception as ex: self.logger.error( "Could not enable plugin %s - %s" % (plugin, str(ex))) if self.registered_plugin is None: self.logger.error("Could not enable at least one plugin") raise Exception("Could not enable at least one plugin")
Example #2
Source File: views.py From comport with BSD 3-Clause "New" or "Revised" License | 7 votes |
def complaints(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department = extractor.first_department() request_json = request.json added_rows = 0 updated_rows = 0 complaint_class = getattr(importlib.import_module("comport.data.models"), "CitizenComplaint{}".format(department.short_name)) for incident in request_json['data']: added = complaint_class.add_or_update_incident(department, incident) if added is True: added_rows += 1 elif added is False: updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
Example #3
Source File: views.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
def assaults(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department = extractor.first_department() request_json = request.json added_rows = 0 updated_rows = 0 assaults_class = getattr(importlib.import_module("comport.data.models"), "AssaultOnOfficer{}".format(department.short_name)) for incident in request_json['data']: added = assaults_class.add_or_update_incident(department, incident) if added is True: added_rows += 1 elif added is False: updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
Example #4
Source File: get_module.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_module_chain(names): r"""Attempt to load a module from an ordered list of module names until one succeeds. Module names may be given fully as: EXOSIMS.OpticalSystem.Nemati or as wildcards like: EXOSIMS.*.Nemati Wildcards, if given, must match only one module. """ for name in names: # replace name by its expansion if needed if '*' in name: name = wildcard_expand(name) try: full_module = importlib.import_module(name) #module_name = package #source = package return full_module except ImportError: continue return None
Example #5
Source File: tasks.py From django-notifs with MIT License | 6 votes |
def _import_channel(channel_alias): """ helper to import channels aliases from string paths. raises an AttributeError if a channel can't be found by it's alias """ try: channel_path = settings.NOTIFICATIONS_CHANNELS[channel_alias] except KeyError: raise AttributeError( '"%s" is not a valid delivery channel alias. ' 'Check your applications settings for NOTIFICATIONS_CHANNELS' % channel_alias ) package, attr = channel_path.rsplit('.', 1) return getattr(importlib.import_module(package), attr)
Example #6
Source File: utils.py From django-anonymizer with MIT License | 6 votes |
def get_anonymizers(app_config): anonymizers_module = app_config.name + '.anonymizers' mod = importlib.import_module(anonymizers_module) anonymizers = [] for value in mod.__dict__.values(): if value is Anonymizer: continue is_anonymizer = False try: if issubclass(value, Anonymizer) and value.model is not None: is_anonymizer = True except TypeError: continue if is_anonymizer: anonymizers.append(value) anonymizers.sort(key=lambda c: c.order) return anonymizers
Example #7
Source File: __init__.py From spleeter with MIT License | 6 votes |
def get_model_function(model_type): """ Get tensorflow function of the model to be applied to the input tensor. For instance "unet.softmax_unet" will return the softmax_unet function in the "unet.py" submodule of the current module (spleeter.model). Params: - model_type: str the relative module path to the model function. Returns: A tensorflow function to be applied to the input tensor to get the multitrack output. """ relative_path_to_module = '.'.join(model_type.split('.')[:-1]) model_name = model_type.split('.')[-1] main_module = '.'.join((__name__, 'functions')) path_to_module = f'{main_module}.{relative_path_to_module}' module = importlib.import_module(path_to_module) model_function = getattr(module, model_name) return model_function
Example #8
Source File: mobile_page_object.py From toolium with Apache License 2.0 | 6 votes |
def __new__(cls, driver_wrapper=None): """Instantiate android or ios page object from base page object depending on driver configuration Base, Android and iOS page objects must be defined with following structure: FOLDER/base/MODULE_NAME.py class BasePAGE_OBJECT_NAME(MobilePageObject) FOLDER/android/MODULE_NAME.py class AndroidPAGE_OBJECT_NAME(BasePAGE_OBJECT_NAME) FOLDER/ios/MODULE_NAME.py class IosPAGE_OBJECT_NAME(BasePAGE_OBJECT_NAME) :param driver_wrapper: driver wrapper instance :returns: android or ios page object instance """ if cls.__name__.startswith('Base'): __driver_wrapper = driver_wrapper if driver_wrapper else DriverWrappersPool.get_default_wrapper() __os_name = 'ios' if __driver_wrapper.is_ios_test() else 'android' __class_name = cls.__name__.replace('Base', __os_name.capitalize()) try: return getattr(importlib.import_module(cls.__module__), __class_name)(__driver_wrapper) except AttributeError: __module_name = cls.__module__.replace('.base.', '.{}.'.format(__os_name)) return getattr(importlib.import_module(__module_name), __class_name)(__driver_wrapper) else: return super(MobilePageObject, cls).__new__(cls)
Example #9
Source File: views.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
def use_of_force(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department = extractor.first_department() request_json = request.json added_rows = 0 updated_rows = 0 uof_class = getattr(importlib.import_module("comport.data.models"), "UseOfForceIncident{}".format(department.short_name)) for incident in request_json['data']: added = uof_class.add_or_update_incident(department, incident) if added is True: added_rows += 1 elif added is False: updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
Example #10
Source File: pipeline.py From Turku-neural-parser-pipeline with Apache License 2.0 | 6 votes |
def add_step(self,module_name_and_params, extra_args): config=module_name_and_params.split() module_name=config[0] params=config[1:] # collect extra arguments from command line meant for this particular module if extra_args is not None: for _name, _value in extra_args.__dict__.items(): if _name.startswith(module_name): _modname,_argname=_name.split(".",1) # for example lemmatizer_mod.gpu params.append("--"+_argname) params.append(str(_value)) mod=importlib.import_module(module_name) step_in=self.q_out self.q_out=Queue(self.max_q_size) #new pipeline end args=mod.argparser.parse_args(params) process=Process(target=mod.launch,args=(args,step_in,self.q_out)) process.daemon=True process.start() self.processes.append(process)
Example #11
Source File: tfutil.py From disentangling_conditional_gans with MIT License | 6 votes |
def __init__(self, name=None, # Network name. Used to select TensorFlow name and variable scopes. func=None, # Fully qualified name of the underlying network construction function. **static_kwargs): # Keyword arguments to be passed in to the network construction function. self._init_fields() self.name = name self.static_kwargs = dict(static_kwargs) # Init build func. module, self._build_func_name = import_module(func) self._build_module_src = inspect.getsource(module) self._build_func = find_obj_in_module(module, self._build_func_name) # Init graph. self._init_graph() self.reset_vars()
Example #12
Source File: views.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
def officer_involved_shooting(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department = extractor.first_department() request_json = request.json added_rows = 0 updated_rows = 0 ois_class = getattr(importlib.import_module("comport.data.models"), "OfficerInvolvedShooting{}".format(department.short_name)) for incident in request_json['data']: added = ois_class.add_or_update_incident(department, incident) if added is True: added_rows += 1 elif added is False: updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
Example #13
Source File: manage.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _make_context(): ''' Return context dict for a shell session. ''' # generate a list of all the incident classes class_lookup = [ {"UseOfForceIncident": ["IMPD", "BPD", "LMPD"]}, {"CitizenComplaint": ["IMPD", "BPD"]}, {"OfficerInvolvedShooting": ["IMPD", "BPD"]}, {"AssaultOnOfficer": ["IMPD"]} ] incident_classes = {} for guide in class_lookup: prefix, departments = guide.popitem() for name in departments: class_name = prefix + name incident_classes[class_name] = getattr(importlib.import_module("comport.data.models"), class_name) context = {'app': app, 'db': db, 'User': User, 'Department': Department, 'Extractor': Extractor, 'IncidentsUpdated': IncidentsUpdated, 'JSONTestClient': JSONTestClient} # update the context with the incident classes context.update(incident_classes) return context
Example #14
Source File: test_functional.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_csv_filtered_by_dept(self, testapp): # create a department department1 = Department.create(name="IM Police Department", short_name="IMPD", load_defaults=False) department2 = Department.create(name="B Police Department", short_name="BPD", load_defaults=False) incidentclass1 = getattr(importlib.import_module("comport.data.models"), "UseOfForceIncident{}".format(department1.short_name)) incidentclass2 = getattr(importlib.import_module("comport.data.models"), "UseOfForceIncident{}".format(department2.short_name)) incidentclass1.create(opaque_id="123ABC", department_id=department1.id) incidentclass2.create(opaque_id="123XYZ", department_id=department2.id) response1 = testapp.get("/department/{}/uof.csv".format(department1.id)) response2 = testapp.get("/department/{}/uof.csv".format(department2.id)) incidents1 = list(csv.DictReader(io.StringIO(response1.text))) incidents2 = list(csv.DictReader(io.StringIO(response2.text))) assert len(incidents1) == 1 and len(incidents2) == 1 assert incidents1[0]['id'] == '123ABC' and incidents2[0]['id'] == '123XYZ'
Example #15
Source File: function_loader.py From pymoo with Apache License 2.0 | 6 votes |
def load(self, func_name=None, _type="auto"): FUNCTIONS = get_functions() if _type == "auto": _type = "cython" if self.is_compiled else "python" if func_name not in FUNCTIONS: raise Exception("Function %s not found: %s" % (func_name, FUNCTIONS.keys())) func = FUNCTIONS[func_name] if _type not in func: raise Exception("Module not available in %s." % _type) func = func[_type] # either provide a function or a string to the module (used for cython) if not callable(func): module = importlib.import_module(func) func = getattr(module, func_name) return func
Example #16
Source File: views.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
def pursuits(): username = request.authorization.username extractor = Extractor.query.filter_by(username=username).first() department = extractor.first_department() request_json = request.json added_rows = 0 updated_rows = 0 pursuit_class = getattr(importlib.import_module("comport.data.models"), "Pursuit{}".format(department.short_name)) for incident in request_json['data']: added = pursuit_class.add_or_update_incident(department, incident) if added is True: added_rows += 1 elif added is False: updated_rows += 1 extractor.next_month = None extractor.next_year = None extractor.save() return json.dumps({"added": added_rows, "updated": updated_rows})
Example #17
Source File: __init__.py From drydock with Apache License 2.0 | 6 votes |
def register_all(): # NOTE(sh8121att) - Import all versioned objects so # they are available via RPC. Any new object definitions # need to be added here. importlib.import_module('drydock_provisioner.objects.network') importlib.import_module('drydock_provisioner.objects.node') importlib.import_module('drydock_provisioner.objects.hostprofile') importlib.import_module('drydock_provisioner.objects.hwprofile') importlib.import_module('drydock_provisioner.objects.site') importlib.import_module('drydock_provisioner.objects.promenade') importlib.import_module('drydock_provisioner.objects.rack') importlib.import_module('drydock_provisioner.objects.bootaction') importlib.import_module('drydock_provisioner.objects.task') importlib.import_module('drydock_provisioner.objects.builddata') importlib.import_module('drydock_provisioner.objects.validation') # Utility class for calculating inheritance
Example #18
Source File: storage.py From pywren-ibm-cloud with Apache License 2.0 | 6 votes |
def __init__(self, storage_config, executor_id=None): self.config = storage_config self.backend = self.config['backend'] self.bucket = self.config['bucket'] self.executor_id = executor_id self._created_cobjects_n = itertools.count() try: module_location = 'pywren_ibm_cloud.storage.backends.{}'.format(self.backend) sb_module = importlib.import_module(module_location) StorageBackend = getattr(sb_module, 'StorageBackend') self.storage_handler = StorageBackend(self.config[self.backend], bucket=self.bucket, executor_id=self.executor_id) except Exception as e: raise NotImplementedError("An exception was produced trying to create the " "'{}' storage backend: {}".format(self.backend, e))
Example #19
Source File: local.py From ALF with Apache License 2.0 | 6 votes |
def load_project(project_name): # load project and check that it looks okay try: importlib.import_module(project_name) except ImportError as e: try: #TODO: relative module imports in a projects/Project will fail for some reason importlib.import_module("projects.%s" % project_name) except ImportError as e: log.error("Failed to import project %s", project_name, exc_info=1) sys.exit(1) if len(_registered) != 1: log.error("Project must register itself using alf.register(). " "%d projects registered, expecting 1.", len(_registered)) sys.exit(1) project_cls = _registered.pop() if not issubclass(project_cls, Fuzzer): raise TypeError("Expecting a Fuzzer, not '%s'" % type(project_cls)) return project_cls
Example #20
Source File: storage.py From pywren-ibm-cloud with Apache License 2.0 | 6 votes |
def __init__(self, pywren_config, storage_backend): self.pywren_config = pywren_config self.backend = storage_backend self._created_cobjects_n = itertools.count() try: module_location = 'pywren_ibm_cloud.storage.backends.{}'.format(self.backend) sb_module = importlib.import_module(module_location) storage_config = self.pywren_config[self.backend] storage_config['user_agent'] = 'pywren-ibm-cloud/{}'.format(__version__) StorageBackend = getattr(sb_module, 'StorageBackend') self.storage_handler = StorageBackend(storage_config) except Exception as e: raise NotImplementedError("An exception was produced trying to create the " "'{}' storage backend: {}".format(self.backend, e))
Example #21
Source File: usr_dir.py From fine-lm with MIT License | 6 votes |
def import_usr_dir(usr_dir): """Import module at usr_dir, if provided.""" if not usr_dir: return if usr_dir == INTERNAL_USR_DIR_PACKAGE: # The package has been installed with pip under this name for Cloud ML # Engine so just import it. importlib.import_module(INTERNAL_USR_DIR_PACKAGE) return dir_path = os.path.abspath(os.path.expanduser(usr_dir).rstrip("/")) containing_dir, module_name = os.path.split(dir_path) tf.logging.info("Importing user module %s from path %s", module_name, containing_dir) sys.path.insert(0, containing_dir) importlib.import_module(module_name) sys.path.pop(0)
Example #22
Source File: __init__.py From aws-ops-automator with Apache License 2.0 | 5 votes |
def _get_module(module_name): """ Gets a module by its name :param module_name: Name of the module :return: The loaded module """ handler_module = sys.modules.get(module_name) if handler_module is None: handler_module = importlib.import_module(module_name) return handler_module
Example #23
Source File: util.py From lirpg with MIT License | 5 votes |
def import_function(spec): """Import a function identified by a string like "pkg.module:fn_name". """ mod_name, fn_name = spec.split(':') module = importlib.import_module(mod_name) fn = getattr(module, fn_name) return fn
Example #24
Source File: __init__.py From aws-ops-automator with Apache License 2.0 | 5 votes |
def _get_module(module_name): """ Gets a module by its name :param module_name: Name of the module :return: The loaded module """ the_module = sys.modules.get(module_name) if the_module is None: the_module = importlib.import_module(module_name) return the_module
Example #25
Source File: all_problems.py From fine-lm with MIT License | 5 votes |
def import_modules(modules): errors = [] for module in modules: try: importlib.import_module(module) except ImportError as error: errors.append((module, error)) _handle_errors(errors)
Example #26
Source File: __init__.py From controller with MIT License | 5 votes |
def _scheduler(self): mod = importlib.import_module(settings.SCHEDULER_MODULE) return mod.SchedulerClient(settings.SCHEDULER_URL, settings.K8S_API_VERIFY_TLS)
Example #27
Source File: componentFactory.py From skelebot with MIT License | 5 votes |
def __init__(self): """Initializes the default list of components in Skelebot""" self.COMPONENTS = { Plugin.__name__.lower(): Plugin, Jupyter.__name__.lower(): Jupyter, Kerberos.__name__.lower(): Kerberos, Bump.__name__.lower(): Bump, Prime.__name__.lower(): Prime, Dexec.__name__.lower(): Dexec, Artifactory.__name__.lower(): Artifactory, Registry.__name__.lower(): Registry, Repository.__name__.lower(): Repository } # Add the plugin components to the master list pluginsHome = os.path.expanduser(PLUGINS_HOME) if (os.path.exists(pluginsHome)): sys.path.append(pluginsHome) for pluginName in os.listdir(pluginsHome): if (pluginName[0] != "_"): try: module = importlib.import_module("{name}.{name}".format(name=pluginName)) plugin = getattr(module, pluginName[0].upper() + pluginName[1:]) self.COMPONENTS[pluginName.lower()] = plugin except Exception: print(PLUGIN_WARNING.format(pluginName)) pluginsQuarantine = os.path.expanduser(PLUGINS_QUARANTINE) if not os.path.exists(pluginsQuarantine): os.makedirs(pluginsQuarantine) src = "{folder}/{name}".format(name=pluginName, folder=pluginsHome) dst = "{folder}/{name}".format(name=pluginName, folder=pluginsQuarantine) shutil.move(src, dst)
Example #28
Source File: utils.py From py2swagger with MIT License | 5 votes |
def _load_class_obj(module_path, class_name, package=None): class_obj = None try: module = importlib.import_module(module_path, package) class_obj = getattr(module, class_name, None) except ImportError: pass return class_obj
Example #29
Source File: create_anonymizers.py From django-anonymizer with MIT License | 5 votes |
def handle_app_config(self, app_config, **options): anonymizers_module_parent = ".".join( app_config.models_module.__name__.split(".")[:-1]) mod = importlib.import_module(anonymizers_module_parent) parent, discard = os.path.split(mod.__file__) # lop off __init__.pyc path = os.path.join(parent, 'anonymizers.py') # and add anonymizers. if os.path.exists(path): raise CommandError("File '%s' already exists." % path) module = introspect.create_anonymizers_module(app_config) with open(path, "w") as fd: fd.write(module)
Example #30
Source File: config.py From quart with MIT License | 5 votes |
def from_object(self, instance: Union[object, str]) -> None: """Load the configuration from a Python object. This can be used to reference modules or objects within modules for example, .. code-block:: python app.config.from_object('module') app.config.from_object('module.instance') from module import instance app.config.from_object(instance) are valid. Arguments: instance: Either a str referencing a python object or the object itself. """ if isinstance(instance, str): try: path, config = instance.rsplit(".", 1) except ValueError: path = instance instance = importlib.import_module(path) else: module = importlib.import_module(path) instance = getattr(module, config) for key in dir(instance): if key.isupper(): self[key] = getattr(instance, key)