def __init__(self, message, exception): """Class DatasetExecutionError constructor. :param message: The error message. :type message: str :param exception: The exception that caused this error. :type exception: Exception """ self.inner_exception = exception self.error_code = getattr(exception, 'error_code', None) self.validation_error_code = getattr(exception, 'validation_error_code', None) self.compliant_message = getattr(exception, 'compliant_message', None) super().__init__(message)
def get_probability_density_func(self): """ Calculates the probabilities for the PoissonVariable x_values. """ dist = poisson(mu=getattr(self, 'lambda')) self.probabilities = dist.pmf(self.x_values)
def add_duck(self, duck: Duck) -> None: fly_method = getattr(duck, "fly", None) if callable(fly_method): self.flock.append(duck) else: raise TypeError("Cannot add duck, are you sure it's not a " + str(type(duck).__name__))
def test_setup_logging(): with ExitStack() as stack: mock_fh = stack.enter_context( mock.patch('logging.FileHandler', mock.Mock())) mock_lbc = stack.enter_context( mock.patch('logging.basicConfig', mock.Mock())) stack.enter_context(mock.patch('sys.argv', ['test'])) stack.enter_context( mock.patch('socket.gethostname', mock.Mock(return_value='test'))) mock_linf = stack.enter_context(mock.patch('logging.info', mock.Mock())) stack.enter_context(mock.patch('logging.getLogger', mock.Mock())) setup_logging(None, None) mock_fh.assert_not_called() mock_lbc.has_call( mock.call(format="%(asctime)s - %(levelname)-8s - %(message)s", level=logging.INFO)) mock_linf.has_call(mock.call('test')) mock_linf.has_call(mock.call('Hostname: test')) with ExitStack() as stack: mock_fh = stack.enter_context( mock.patch('logging.FileHandler', mock.Mock())) mock_lbc = stack.enter_context( mock.patch('logging.basicConfig', mock.Mock())) stack.enter_context(mock.patch('logging.getLogger', mock.Mock())) stack.enter_context(mock.patch('sys.argv', ['test'])) stack.enter_context( mock.patch('socket.gethostname', mock.Mock(return_value='test'))) setup_logging('test_log', None, verbose=True) mock_fh.assert_called_once() mock_lbc.has_call( mock.call(format="%(asctime)s - %(levelname)-8s - %(message)s", level=logging.DEBUG)) for level in ['debug', 'info', 'warning', 'error', 'critical'][::-1]: setup_logging(None, level) mock_lbc.has_call( mock.call(format="%(asctime)s - %(levelname)-8s - %(message)s", level=getattr(logging, level.upper()))) with ExitStack() as stack: mock_fh = stack.enter_context( mock.patch('logging.FileHandler', mock.Mock())) mock_lbc = stack.enter_context( mock.patch('logging.basicConfig', mock.Mock())) stack.enter_context(mock.patch('logging.getLogger', mock.Mock())) stack.enter_context(mock.patch('sys.argv', ['test'])) stack.enter_context( mock.patch('socket.gethostname', mock.Mock(side_effect=Exception('hostname exception')))) mock_linf = stack.enter_context(mock.patch('logging.info', mock.Mock())) setup_logging(None, None, verbose=True) mock_fh.assert_not_called() mock_lbc.has_call( mock.call(format="%(asctime)s - %(levelname)-8s - %(message)s", level=logging.DEBUG)) mock_linf.has_call(mock.call('hostname exception'))
def decodeHttpResponseAttribute(httpObject, attribute): if hasattr(httpObject, attribute): return getattr(httpObject, attribute).decode('UTF-8') else: raise NameError( f"HTTP response object does not have attribute with name {attribute}" )
def get_resamp_vals(self, samp_size): """ Inputs: samp_size- the number of samples to generate according to the distribution Overrides the Variable class get_resamp_vals to align with a discrete Poisson distribution. """ comm = MPI_COMM_WORLD size = comm.size rank = comm.rank base = samp_size // size rem = samp_size % size count = base + (rank < rem) ranks = np.arange(0, size, dtype=int) seq_count = (ranks < rem) + base seq_disp = base * ranks + (ranks >= rem) * rem + (ranks < rem) * ranks self.resample = np.zeros(samp_size) resample = np.random.poisson(lam=getattr(self, 'lambda'), size=count).astype(float) comm.Allgatherv([resample, count, MPI_DOUBLE], [self.resample, seq_count, seq_disp, MPI_DOUBLE]) return self.resample
def __getitem__(self, name): import english_parser if name == "__tracebackhide__": return False # for py.test print(("Reflector __getitem__ %s" % str(name))) if name in the.params: the.result = english_parser.do_evaluate(the.params[name]) elif name in the.variables: the.result = english_parser.do_evaluate(the.variables[name].value) elif name in locals(): return locals()[name] elif name in globals(): return globals()[name] elif __builtin__.hasattr(__builtin__, name): # name in __builtin__: return __builtin__.getattr(__builtin__, name) elif name == 'reverse': return list.reverse elif name in the.methods: m = the.methods[name] if isinstance(m, nodes.FunctionDef): raise Exception("%s must be declared or imported before!" % m) m = m.body # INJECT! return m else: print(("UNKNOWN ITEM %s" % name)) return name # kast.name(name) # raise Exception("UNKNOWN ITEM %s" % name) return the.result
def inner(self, obj, argument): if isinstance(argument, str) and argument.endswith(_njk_signature): # a njk-originated access will always be assuming a dict lookup before an attr final_method_name = "getitem" final_argument = argument[:-len(_njk_signature)] else: final_argument = argument final_method_name = method_name # pleasantly surprised that super() works in this context retval = builtins.getattr(super(), final_method_name)(obj, final_argument) if argument == f"length{_njk_signature}" and isinstance(retval, jinja2.runtime.Undefined) \ and isinstance(obj, Sized): return len(obj) if isinstance(argument, str) \ and argument.endswith(_njk_signature) \ and isinstance(retval, _builtin_function_or_method_type): # the lookup has probably gone looking for attributes and found a builtin method. because # any njk-originated lookup will have been made to prefer dict lookups over attributes, we # can be fairly sure there isn't a dict key matching this - so we should just call this a # failure. return self.undefined(obj=obj, name=final_argument) return retval
def factor_plots(self, graph_dir, var_list, plot_data, plot_name, verify=False): """ Inputs: graph_dir- file location where to put plots var_list- list of variables plot_data- the data to be plotted plot_name- 'Predicted' or 'Error'; what data is being plotted verify- if these points are the verification points or the input points Generates plots for each variable against plot_data. """ var_count = len(var_list) attr = '' stand = '' if is_manager and self.verbose: print(f'Generating {plot_name} vs Factor graphs\n') if self.standardize: attr = ''.join((attr, 'std_')) stand = ' (Standardized)' if verify: attr = ''.join((attr, 'verify_')) attr = ''.join((attr, 'vals')) j = rank while j < var_count: curr_var = var_list[j] plt.scatter(getattr(curr_var, attr), plot_data) plt.title(f'{plot_name} vs {curr_var.name}{stand}') plt.xlabel(f'{curr_var.name}') plt.ylabel(f'{plot_name}') image_path = f'{graph_dir}/{plot_name}_vs_{curr_var.name}' if isinstance(curr_var, DiscreteVariable): if hasattr(curr_var, 'categories'): if not self.standardize: ticks = curr_var.unstandardize_points( curr_var.x_values) else: ticks = curr_var.x_values plt.xticks(ticks, curr_var.categories) plt.savefig(image_path, dpi=600, bbox_inches='tight') plt.clf() j += size
def evaluate_points(func, begin, total_samps, var_list, attr): """ Inputs: func- the lambda function used to generate the data from the evaluation vector begin- the index to start at in the `attr` array total_samps- the total number of samples to generate var_list- list of the variables attr- the attribute that holds the values to be used in the evaluation vector Identical to evaluate_points_verbose, but doesn't check for a verbose option every iteration. This version also deals with indexing only part of eval_vect. """ var_count = len(var_list) term_count = func(np.zeros(var_count)).shape if len(term_count) > 0: term_count = term_count[1] # len(func(np.zeros(var_count))) else: term_count = 1 eval_vect = np.zeros([total_samps, var_count]) matrix = np.zeros([total_samps, term_count]) end = begin + total_samps for j in range(var_count): attr_arr = getattr(var_list[j], attr) eval_vect[:, j] = attr_arr[begin:end].T for i in range(total_samps): matrix[i, :] = func(eval_vect[i, :]) return matrix
def getattr(obj, name, default=None): if hasattr(obj, name): # exists return builtins.getattr(obj, name) # attr not exists if default is not None: return default # default return getattr_info(obj, name)
def send_activation(self): if not self.activated and not self.forced_expired: if self.key: base_url = getattr(settings, 'BASE_URL', 'http://127.0.0.1:8000/') key_path = reverse("account:email-activate", kwargs={'key': self.key}) # use reverse path = "{base}{path}".format(base=base_url, path=key_path) context = {'path': path, 'email': self.email} txt_ = get_template("registration/emails/verify.txt").render( context) html_ = get_template("registration/emails/verify.html").render( context) subject = '1-Click Email Verification' from_email = settings.DEFAULT_FROM_EMAIL recipient_list = [self.email] sent_mail = send_mail( subject, txt_, from_email, recipient_list, html_message=html_, fail_silently=False, ) return sent_mail return False
def get_command_handler(self, command): try: command_handler = getattr(self, "command_{}".format(command)) except AttributeError: raise CurlFrameworkException("Unknown command: {}".format(command)) return command_handler
def command_run(self, *args, **kwargs): print_status("Running module {}...".format(self.current_module)) sub_command = args[0] try: getattr(self.current_module, sub_command)() except KeyboardInterrupt: print_info() print_error("Operation cancelled by user") except AttributeError: if sub_command is '': print_error("Usage: run <request method: {}>".format( "get, post, put, delete")) else: print_error("Unknown command [{}]".format(sub_command)) except Exception as ex: print_error(str(ex))
def complete(self, text, state): if state == 0: original_line = readline.get_line_buffer() line = original_line.lstrip() stripped = len(original_line) - len(line) start_index = readline.get_begidx() - stripped end_index = readline.get_endidx() - stripped if start_index > 0: cmd, args, _ = self.parse_line(line) if cmd == "": complete_function = self.default_completer else: try: complete_function = getattr(self, "complete_" + cmd) except AttributeError: complete_function = self.default_completer else: complete_function = self.raw_command_completer self.completion_matches = complete_function( text, line, start_index, end_index) try: return self.completion_matches[state] except IndexError: return None
def getattr(attr_name): blacklist = ('__debug__', '__import__', 'eval', 'exec' , 'open', 'compile') if attr_name in blacklist: raise Exception("Not allowed") else: import builtins return builtins.getattr(attr_name)
def __call__( self, space: Space ) -> Union[Dict, Discrete, NamedDiscrete, Scalar, Tuple, Box, Sequence]: field = space.WhichOneof("value") if field is None: return None res = self.message_converter(getattr(space, field)) return res
def standardize(self, orig, std_vals): """ Inputs: orig- the un-standardized values std_vals- the attribue name for the standardized vals Overrides the Variable class standardize to align with a discrete uniform distribution. """ original = getattr(self, orig) rng = self.interval_high - self.interval_low mean = rng / 2 + self.interval_low stdev = rng / 2 standard = (original[:] - mean) / stdev setattr(self, std_vals, standard) return getattr(self, std_vals)
def prop(name, o): """Returns a function that when supplied an object returns the indicated property of that object, if it exists""" try: return builtins.getattr(o, name) except (AttributeError, TypeError): try: return o[name] except KeyError: return None
def __setattr__( self, name : str, valueProxy : (types.LambdaType, types.FunctionType, types.MethodType) ) \ -> None: cls = self.__class__ if builtins.getattr(self.__Func, name, self.__Func) is self.__Func: builtins.setattr(self.__Func, name, valueProxy())
def _permission_controller_forsure(self, user_obj, **kwargs): l = [] print(kwargs) for key, value in kwargs.items(): if callable(getattr(user_obj, 'has_perm')): print("{0}.{1}".format(key, value)) l.append(user_obj.has_perm("{0}.{1}".format(key, value))) else: l.append(False) print("in _permission_controller_forsure and result is:") print(l)
def standardize(self, orig, std_vals): """ Inputs: orig- the un-standardized values std_vals- the attribue name for the standardized vals Overrides the Variable class standardize to align with a discrete poisson distribution. """ standardized = getattr(self, orig) - self.interval_low setattr(self, std_vals, standardized) return standardized
def read_blueprints(): blueprint_instances = [] files = os.listdir(blueprints_folder) for f in files: if f not in Config.exclude_files: import_name = f.split(".py")[0] # blueprint_instance = importlib.import_module() module = importlib.import_module(".%s" % import_name, package="blueprints") blueprint_name = "%s_blueprint" % import_name blueprint_instance = getattr(module, blueprint_name) blueprint_instances.append(blueprint_instance) return blueprint_instances
def check_settings(self, input_file, arg_options, args): """ Inputs: input_file- the input (.yaml) file containing the Variable and Settings (optional) information arg_options- the names of the available command-line arguments args- the command-line arguments Removes the Settings from the input file, warns users if an argument used isn't valid, and checks the backend in the environment varaibles. """ var_dict, settings, atmos = read_input_file(input_file) try: for key in list(settings): try: # do values from input file agree with values from commandline if key not in arg_options: warn(f'Setting {key} in {input_file} is not a valid ' 'setting.') cl_set = settings[key] file_set = getattr(args, key) if cl_set != file_set: try: # order [2] vs 2 shouldn't alert users np.isclose(cl_set, file_set) except TypeError: warn(f'{key} changing from {settings[key]} to ' f'{getattr(args, key)}.') except AttributeError: # attribute doesn't exist pass except TypeError: pass # no commands in the input file if 'MPLBACKEND' in os.environ: backend = os.environ['MPLBACKEND'] try: # if the backend hasn't been set in input file if settings['backend'].upper() != backend.upper(): warn( f'backend changing from OS env {backend} to input file ' f'{settings["backend"]}.') backend = settings['backend'] settings['backend'] = backend except KeyError: # backend isn't in file pass return var_dict, settings, atmos
def command_add(self, *args, **kwargs): key = args[0] if key in self.current_module.options: templist = getattr(self.current_module, key) print_info("Header name:") value = input() print_info("Header value:") templist[value] = input() setattr(self.current_module, key, templist) self.current_module.module_attributes[key][0][value] = templist[ value] print_success("{} => {}: {}".format(key, value, templist[value]))
def find_high_lim(self): """ Finds the high interval to use in calculations for the variable basis and univariate norm squared values. """ low_percent = 8e-17 high_percent = 1 - low_percent stand_dist = poisson(mu=getattr(self, 'lambda')) high = np.ceil(stand_dist.ppf(high_percent)) low = np.floor(stand_dist.ppf(low_percent)) self.x_values = np.arange(low, high + 1)
def check_num_string(self): """ Searches to replace sring 'pi' with its numpy equivalent in any of the values that might contain it. """ lambd = getattr(self, 'lambda') if isinstance(self.interval_low, str) and 'pi' in self.interval_low: self.interval_low = float( self.interval_low.replace('pi', str(np.pi))) if isinstance(lambd, str) and 'pi' in lambd: setattr(self, 'lambda', float(lambd.replace('pi', str(np.pi))))
def _generate_media_url(class_instance, class_attibute_name, default_image=False): class_base_url = urljoin(urljoin(urljoin("http://{}".format(settings.STATIC_HOSTNAME), settings.STATIC_URL), settings.MEDIA_PREFIX), class_instance.__tablename__ + "/") class_attribute = getattr(class_instance, class_attibute_name) if class_attribute is not None: return urljoin(urljoin(urljoin(urljoin(class_base_url, class_attribute), str(class_instance.id) + "/"), class_attibute_name + "/"), class_attribute) else: if default_image: return urljoin(urljoin(class_base_url, class_attibute_name + "/"), settings.DEFAULT_IMAGE_NAME) else: return class_attribute
def _dataprep_error_handler(e, message, is_dprep_exception): user_exception_list = ["Authentication", "NotFound", "Validation", "FieldNotFound", "AlreadyExists", "FieldConflict", "StepTranslation", "DataError", "NoColumnsError", "Assertion", "AuthenticationContextMismatch", "CreateTable", "WriteTable"] if is_dprep_exception: message = message + " ErrorCode: {}".format(e.error_code) for item in user_exception_list: if _contains(item, getattr(e, 'error_code', 'Unexpected')): raise UserErrorException(message, inner_exception=e) raise AzureMLException(message, inner_exception=e)
def todict(cls): """ Returns a list of all of class cls' private members and caches them in a dict. :type cls: Class :rtype: dict """ try: return cls._cached_member_dict except AttributeError: cls._cached_member_dict = {name: getattr(cls, name) for name in dir(cls) if not name.startswith('_')} return cls._cached_member_dict
def getattr_info(obj, name): # no default :) but we can show more info for debug fullname = objectname(obj, fullname=True) _type = gettype(obj) err = None # todo: isgenerator, istraceback, etc... if inspect.isbuiltin(obj) and inspect.isroutine(obj): _type = "builtin function" fullname = fullname.split(".")[-1] if isobject(obj) and getclass(obj) is not None: # default: 'clsname' object has no attribute 'name' cls = getclass(obj) if cls: fullname = objectname(cls, fullname=True) err = "%s object has no attribute '%s'" % (fullname, name) # _type = "object" if _type: err = "%s '%s' has no attribute '%s'" % (_type, fullname, name) if err: raise AttributeError(err) # default builtins.getattr(obj, name)
def sub_cb(topic, msg): global c print((topic, msg)) try: # 获取方法名 fun = topic.decode().split('/')[-1] print(fun) # 获取方法 if hasattr(opt, fun): fun = getattr(opt, fun) fun(msg) c.publish(topic + b"State", msg) except Exception as e: print(e)
def command_delete(self, *args, **kwargs): key, _, value = args[0].partition(" ") if key in self.current_module.options: templist = getattr(self.current_module, key) if not isinstance(templist, dict): print_error( "Cannot delete attribute {}, only dictionary entries can be deleted, overwrite it instead" .format(key)) return del templist[value] setattr(self.current_module, key, templist) del self.current_module.module_attributes[key][0][value] print_error("{} => {}".format(key, value))
def GetProxy(module, key, **kwargs): version = paraview.compatibility.GetVersion() if version < 5.5: if key == "Clip": # in PV 5.5 we changed the default for Clip's InsideOut property to 1 instead of 0 # also InsideOut was changed to Invert in 5.5 clip = builtins.getattr(module, key)(**kwargs) clip.Invert = 0 return clip if version < 5.7: if key == "ExodusRestartReader" or key == "ExodusIIReader": # in 5.7, we changed the names for blocks, this preserves old # behavior reader = builtins.getattr(module, key)(**kwargs) reader.UseLegacyBlockNamesWithElementTypes = 1 return reader if version <= 5.9: if key == "PlotOverLine": ## in 5.10, we changed the backend of Plot Over Line ## This restores the previous backend. probeLine = builtins.getattr(module, "PlotOverLineLegacy")(**kwargs) return probeLine if key in ["RenderView", "OrthographicSliceView", "ComparativeRenderView"]: view = builtins.getattr(module, key)(**kwargs) view.UseColorPaletteForBackground = 0 return view if key == "Calculator": # In 5.10, we changed the array calculator's parser. # This restores the previous calculator expression parser # to handle syntax it supports. calculator = builtins.getattr(module, key)(**kwargs) calculator.FunctionParserType = 0 return calculator if key == "Gradient": # In 5.10, 'Gradient' and 'GradientOfUnstructuredDataSet' were merged # into a unique 'Gradient" filter. gradient = builtins.getattr(module, "GradientLegacy")(**kwargs) return gradient if version <= 5.10: if key in ["ParticleTracer, ParticlePath, StreakLine"]: # in 5.11, we changed the StaticMesh flag of ParticleTracer, ParticlePath and StreakLine # This restores the previous StaticMesh. particleTracerBase = builtins.getattr(module, key)(**kwargs) particleTracerBase.MeshOverTime = 0 return particleTracerBase return builtins.getattr(module, key)(**kwargs)
def _changeColor(color): global _currentColor if _currentColor != color: _currentColor = color std_out_handle = getattr(ctypes.windll.kernel32, 'GetStdHandle')(STD_OUTPUT_HANDLE) getattr(ctypes.windll.kernel32, 'SetConsoleTextAttribute')(std_out_handle, color)
def _get_attr(self, attr_name): if attr_name in self.blacklist: raise NotAllowedError(the_member_name, "Not allowed") else: return builtins.getattr(attr_name)
from pyramda.function.curry import curry import builtins getattr = curry(lambda name, o: builtins.getattr(o, name))
def getattr(attr, obj): return __builtin__.getattr(obj, attr)