Best Python code snippet using pyatom_python
irsg_querygen.py
Source:irsg_querygen.py
...19 words = query_str.split()20 if len(words) != 3: return None21 22 sub_struct = siom.mat_struct()23 sub_struct.__setattr__('names', words[0])24 25 obj_struct = siom.mat_struct()26 obj_struct.__setattr__('names', words[2])27 28 rel_struct = siom.mat_struct()29 rel_struct.__setattr__('subject', 0)30 rel_struct.__setattr__('predicate', words[1])31 rel_struct.__setattr__('object', 1)32 33 det_list = np.array([sub_struct, obj_struct], dtype=np.object)34 query_struct = siom.mat_struct()35 query_struct.__setattr__('objects', det_list)36 query_struct.__setattr__('unary_triples', np.array([]))37 query_struct.__setattr__('binary_triples', rel_struct)38 39 return query_struct40def gen_asro(query_str):41 words = query_str.split()42 if len(words) != 4: return None43 44 sub_attr_struct = siom.mat_struct()45 sub_attr_struct.__setattr__('subject', 0)46 sub_attr_struct.__setattr__('predicate', 'is')47 sub_attr_struct.__setattr__('object', words[0])48 49 query_struct = gen_sro(' '.join([words[1], words[2], words[3]]))50 query_struct.__setattr__('unary_triples', sub_attr_struct)51 52 return query_struct53def gen_srao(query_str):54 words = query_str.split()55 if len(words) != 4: return None56 57 obj_attr_struct = siom.mat_struct()58 obj_attr_struct.__setattr__('subject', 1)59 obj_attr_struct.__setattr__('predicate', 'is')60 obj_attr_struct.__setattr__('object', words[2])61 62 query_struct = gen_sro(' '.join([words[0], words[1], words[3]]))63 query_struct.__setattr__('unary_triples', obj_attr_struct)64 65 return query_struct66def gen_asrao(query_str):67 words = query_str.split()68 if len(words) != 5: return None69 70 obj_attr_struct = siom.mat_struct()71 obj_attr_struct.__setattr__('subject', 1)72 obj_attr_struct.__setattr__('predicate', 'is')73 obj_attr_struct.__setattr__('object', words[3])74 75 query_struct = gen_asro(' '.join([words[0], words[1], words[2], words[4]]))76 query_struct.__setattr__('unary_triples', obj_attr_struct)77 78 return query_struct79"""80 dog_walker holding leash attached_to dog81 ob1 rel1 ob2 rel2 ob382"""83def gen_two_rel_chain(query_str):84 words = query_str.split()85 if len(words) != 5: return None86 87 # generate the object list88 ob1_struct = siom.mat_struct()89 ob1_struct.__setattr__('names', words[0])90 91 ob2_struct = siom.mat_struct()92 ob2_struct.__setattr__('names', words[2])93 94 ob3_struct = siom.mat_struct()95 ob3_struct.__setattr__('names', words[4])96 97 ob_list = np.array([ob1_struct, ob2_struct, ob3_struct])98 99 # generate the first relation100 rel1_struct = siom.mat_struct()101 rel1_struct.__setattr__('subject', 0)102 rel1_struct.__setattr__('predicate', words[1])103 rel1_struct.__setattr__('object', 1)104 105 #rel1_objs = np.array([ob1_struct, ob2_struct], dtype=np.object)106 107 # generate the second relation108 rel2_struct = siom.mat_struct()109 rel2_struct.__setattr__('subject', 1)110 rel2_struct.__setattr__('predicate', words[3])111 rel2_struct.__setattr__('object', 2)112 113 #rel2_objs = np.array([ob2_struct, ob3_struct], dtype=np.object)114 115 # store the objects and relations116 rel_list = np.array([rel1_struct, rel2_struct])117 118 query_struct = siom.mat_struct()119 query_struct.__setattr__('objects', ob_list)120 query_struct.__setattr__('unary_triples', np.array([]))121 query_struct.__setattr__('binary_triples', rel_list)122 123 return query_struct124"""125 0 1 2 3 4 5 6126 dog_walker holding leash attached_to dog near dog_walker127 ob1 rel1 ob2 rel2 ob3 rel3 ob1128 129 expects this exact order of objects and relationships130"""131def gen_three_obj_loop(query_str):132 words = query_str.split()133 134 if len(words) != 7: return None135 if words[0] != words[6]: return None136 137 # generate the object list138 ob1_struct = siom.mat_struct()139 ob1_struct.__setattr__('names', words[0])140 141 ob2_struct = siom.mat_struct()142 ob2_struct.__setattr__('names', words[2])143 144 ob3_struct = siom.mat_struct()145 ob3_struct.__setattr__('names', words[4])146 147 ob_list = np.array([ob1_struct, ob2_struct, ob3_struct])148 149 # generate the first relation150 rel1_struct = siom.mat_struct()151 rel1_struct.__setattr__('subject', 0)152 rel1_struct.__setattr__('predicate', words[1])153 rel1_struct.__setattr__('object', 1)154 155 # generate the second relation156 rel2_struct = siom.mat_struct()157 rel2_struct.__setattr__('subject', 1)158 rel2_struct.__setattr__('predicate', words[3])159 rel2_struct.__setattr__('object', 2)160 161 # generate the third relation162 rel3_struct = siom.mat_struct()163 rel3_struct.__setattr__('subject', 2)164 rel3_struct.__setattr__('predicate', words[5])165 rel3_struct.__setattr__('object', 0)166 # store the objects and relations167 rel_list = np.array([rel1_struct, rel2_struct, rel3_struct])168 169 query_struct = siom.mat_struct()170 query_struct.__setattr__('objects', ob_list)171 query_struct.__setattr__('unary_triples', np.array([]))172 query_struct.__setattr__('binary_triples', rel_list)173 174 return query_struct175"""176 obj1, obj2, ... , objN; 1 relationship_str 2, ... , x relationship_str y177 178 player, player, table, net; 1 playing_pingpong_with 2, 1 at 3, 2 at 3, 4 on 3179"""180def gen_split_spec(query_str):181 object_rel_split = query_str.split(';')182 objects = object_rel_split[0]183 relationships = object_rel_split[1]184 185 obj_nodes = []186 for obj in objects.split(','):187 obj_mat_struct = siom.mat_struct()188 obj_mat_struct.__setattr__('names', obj.strip())189 obj_nodes.append(obj_mat_struct)190 191 ob_list = np.array(obj_nodes)192 193 rel_nodes = []194 for three_part_rel in relationships.split(','):195 rel_components = three_part_rel.strip().split(' ')196 rel_struct = siom.mat_struct()197 rel_struct.__setattr__('subject', int(rel_components[0].strip()))198 rel_struct.__setattr__('predicate', rel_components[1].strip())199 rel_struct.__setattr__('object', int(rel_components[2].strip()))200 rel_nodes.append(rel_struct)201 202 rel_list = np.array(rel_nodes)203 204 query_struct = siom.mat_struct()205 query_struct.__setattr__('objects', ob_list)206 query_struct.__setattr__('unary_triples', np.array([]))207 query_struct.__setattr__('binary_triples', rel_list)208 209 return query_struct210querygen_fns = {211 'sro': gen_sro,212 'asro' : gen_asro,213 'srao': gen_srao,214 'asrao' : gen_asrao,215 'two_rel_chain' : gen_two_rel_chain, # e.g. dog_walker holding leash attached_to dog216 'two_rel_loop' : gen_three_obj_loop, # e.g. dog_walker holding leash attached_to dog near dog_walker217 'split_spec' : gen_split_spec # e.g. dog_walker, dog; 1 walking 2...
metrics.py
Source:metrics.py
...53 key_for_send_to_dynatrace: Text54 def __init__(self, **kwargs):55 key = kwargs.get("key", "")56 value = kwargs.get("value", "").replace("label:", "")57 object.__setattr__(self, "key_for_get_func_create_entity_id", value)58 object.__setattr__(self, "key_for_create_entity_id", (value.replace("resource.labels.", "") or key))59 object.__setattr__(self, "key_for_fetch_metric", value or f'metric.labels.{key}')60 object.__setattr__(self, "key_for_send_to_dynatrace", key) # or 'resource|metric.labels.{key}' from response used by lib.metric_ingest.create_dimensions will use last part of source/value, as this is how this worked before - no breaking changes allowed61@dataclass(frozen=True)62class Metric:63 """Represents singular metric to be ingested."""64 name: str65 google_metric: Text66 google_metric_kind: Text67 dynatrace_name: Text68 dynatrace_metric_type: Text69 unit: Text70 dimensions: List[Dimension]71 ingest_delay: timedelta72 sample_period_seconds: timedelta73 value_type: str74 metric_type: str75 def __init__(self, **kwargs):76 gcp_options = kwargs.get("gcpOptions", {})77 object.__setattr__(self, "name", kwargs.get("name", ""))78 object.__setattr__(self, "metric_type", kwargs.get("type", ""))79 object.__setattr__(self, "google_metric", kwargs.get("value", "").replace("metric:", ""))80 object.__setattr__(self, "google_metric_kind", gcp_options.get("metricKind", ""))81 object.__setattr__(self, "dynatrace_name", kwargs.get("key", "").replace(":", "."))82 object.__setattr__(self, "dynatrace_metric_type", kwargs.get("type", ""))83 object.__setattr__(self, "unit", kwargs.get("gcpOptions", {}).get("unit", None))84 object.__setattr__(self, "value_type", gcp_options.get("valueType", ""))85 object.__setattr__(self, "dimensions", [Dimension(**x) for x in kwargs.get("dimensions", {})])86 ingest_delay = kwargs.get("gcpOptions", {}).get("ingestDelay", None)87 if ingest_delay:88 object.__setattr__(self, "ingest_delay", timedelta(seconds=ingest_delay))89 else:90 object.__setattr__(self, "ingest_delay", timedelta(seconds=60))91 sps = kwargs.get("gcpOptions", {}).get("samplePeriod", None)92 if sps:93 object.__setattr__(self, "sample_period_seconds", timedelta(seconds=sps))94 else:95 object.__setattr__(self, "sample_period_seconds", timedelta(seconds=60))96@dataclass(frozen=True)97class GCPService:98 """Describes singular GCP service to ingest data from."""99 name: Text100 technology_name: Text101 feature_set: Text102 dimensions: List[Dimension]103 metrics = List[Metric]104 monitoring_filter: Text105 activation: Dict[Text, Any]106 def __init__(self, **kwargs):107 object.__setattr__(self, "name", kwargs.get("service", ""))108 object.__setattr__(self, "feature_set", kwargs.get("featureSet", ""))109 object.__setattr__(self, "technology_name", kwargs.get("tech_name", "N/A"))110 object.__setattr__(self, "dimensions", [Dimension(**x) for x in kwargs.get("dimensions", {})])111 object.__setattr__(self, "metrics", [112 Metric(**x)113 for x114 in kwargs.get("metrics", {})115 if x.get("gcpOptions", {}).get("valueType", "").upper() != "STRING"116 ])117 object.__setattr__(self, "activation", kwargs.get("activation", {}))118 monitoring_filter = kwargs.get("gcpMonitoringFilter", "")119 if self.activation:120 for var_key, var_value in (self.activation.get("vars", {}) or {}).items():121 monitoring_filter = monitoring_filter.replace(f'{{{var_key}}}', var_value)\122 .replace(f'var:{var_key}', var_value)123 # remove not matched variables124 monitoring_filter = VARIABLE_BRACKETS_PATTERN.sub('', monitoring_filter)125 monitoring_filter = VARIABLE_VAR_PATTERN.sub('', monitoring_filter)126 object.__setattr__(self, "monitoring_filter", monitoring_filter)127DISTRIBUTION_VALUE_KEY = 'distributionValue'128BOOL_VALUE_KEY = 'boolValue'129DOUBLE_VALUE_KEY = 'doubleValue'130INT_VALUE_KEY = 'int64Value'131TYPED_VALUE_KEY_MAPPING = {132 'INT64': INT_VALUE_KEY,133 'DOUBLE': DOUBLE_VALUE_KEY,134 'BOOL': BOOL_VALUE_KEY,135 'DISTRIBUTION': DISTRIBUTION_VALUE_KEY...
cpp_bindings_raises_exception_test.py
Source:cpp_bindings_raises_exception_test.py
1# Copyright (C) 2018 ycmd contributors2#3# This file is part of ycmd.4#5# ycmd is free software: you can redistribute it and/or modify6# it under the terms of the GNU General Public License as published by7# the Free Software Foundation, either version 3 of the License, or8# (at your option) any later version.9#10# ycmd is distributed in the hope that it will be useful,11# but WITHOUT ANY WARRANTY; without even the implied warranty of12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13# GNU General Public License for more details.14#15# You should have received a copy of the GNU General Public License16# along with ycmd. If not, see <http://www.gnu.org/licenses/>.17from __future__ import unicode_literals18from __future__ import print_function19from __future__ import division20from __future__ import absolute_import21# Not installing aliases from python-future; it's unreliable and slow.22from builtins import * # noqaimport ycm_core23from ycmd.tests.test_utils import ClangOnly24from hamcrest import assert_that, calling, raises25import ycm_core26READONLY_MESSAGE = 'can\'t set attribute'27@ClangOnly28def CppBindings_ReadOnly_test():29 assert_that( calling( ycm_core.CompletionData().__setattr__ )30 .with_args( 'kind_', ycm_core.CompletionData().kind_ ),31 raises( AttributeError, READONLY_MESSAGE ) )32 assert_that( calling( ycm_core.Location().__setattr__ )33 .with_args( 'line_number_', 1 ),34 raises( AttributeError, READONLY_MESSAGE ) )35 assert_that( calling( ycm_core.Location().__setattr__ )36 .with_args( 'column_number_', 1 ),37 raises( AttributeError, READONLY_MESSAGE ) )38 assert_that( calling( ycm_core.Location().__setattr__ )39 .with_args( 'filename_', 'foo' ),40 raises( AttributeError, READONLY_MESSAGE ) )41 assert_that( calling( ycm_core.Range().__setattr__ )42 .with_args( 'end_', ycm_core.Range().end_ ),43 raises( AttributeError, READONLY_MESSAGE ) )44 assert_that( calling( ycm_core.Range().__setattr__ )45 .with_args( 'start_', ycm_core.Range().start_ ),46 raises( AttributeError, READONLY_MESSAGE ) )47 assert_that( calling( ycm_core.FixItChunk().__setattr__ )48 .with_args( 'range', ycm_core.FixItChunk().range ),49 raises( AttributeError, READONLY_MESSAGE ) )50 assert_that( calling( ycm_core.FixItChunk().__setattr__ )51 .with_args( 'replacement_text', 'foo' ),52 raises( AttributeError, READONLY_MESSAGE ) )53 assert_that( calling( ycm_core.FixIt().__setattr__ )54 .with_args( 'chunks', ycm_core.FixIt().chunks ),55 raises( AttributeError, READONLY_MESSAGE ) )56 assert_that( calling( ycm_core.FixIt().__setattr__ )57 .with_args( 'location', ycm_core.FixIt().location ),58 raises( AttributeError, READONLY_MESSAGE ) )59 assert_that( calling( ycm_core.FixIt().__setattr__ )60 .with_args( 'text', 'foo' ),61 raises( AttributeError, READONLY_MESSAGE ) )62 assert_that( calling( ycm_core.Diagnostic().__setattr__ )63 .with_args( 'ranges_', ycm_core.Diagnostic().ranges_ ),64 raises( AttributeError, READONLY_MESSAGE ) )65 assert_that( calling( ycm_core.Diagnostic().__setattr__ )66 .with_args( 'location_', ycm_core.Diagnostic().location_ ),67 raises( AttributeError, READONLY_MESSAGE ) )68 assert_that( calling( ycm_core.Diagnostic().__setattr__ )69 .with_args( 'location_extent_',70 ycm_core.Diagnostic().location_extent_ ),71 raises( AttributeError, READONLY_MESSAGE ) )72 assert_that( calling( ycm_core.Diagnostic().__setattr__ )73 .with_args( 'fixits_', ycm_core.Diagnostic().fixits_ ),74 raises( AttributeError, READONLY_MESSAGE ) )75 assert_that( calling( ycm_core.Diagnostic().__setattr__ )76 .with_args( 'text_', 'foo' ),77 raises( AttributeError, READONLY_MESSAGE ) )78 assert_that( calling( ycm_core.Diagnostic().__setattr__ )79 .with_args( 'long_formatted_text_', 'foo' ),80 raises( AttributeError, READONLY_MESSAGE ) )81 assert_that( calling( ycm_core.Diagnostic().__setattr__ )82 .with_args( 'kind_', ycm_core.Diagnostic().kind_.WARNING ),83 raises( AttributeError, READONLY_MESSAGE ) )84 assert_that( calling( ycm_core.DocumentationData().__setattr__ )85 .with_args( 'raw_comment', 'foo' ),86 raises( AttributeError, READONLY_MESSAGE ) )87 assert_that( calling( ycm_core.DocumentationData().__setattr__ )88 .with_args( 'brief_comment', 'foo' ),89 raises( AttributeError, READONLY_MESSAGE ) )90 assert_that( calling( ycm_core.DocumentationData().__setattr__ )91 .with_args( 'canonical_type', 'foo' ),92 raises( AttributeError, READONLY_MESSAGE ) )93 assert_that( calling( ycm_core.DocumentationData().__setattr__ )94 .with_args( 'display_name', 'foo' ),95 raises( AttributeError, READONLY_MESSAGE ) )96 assert_that( calling( ycm_core.DocumentationData().__setattr__ )97 .with_args( 'comment_xml', 'foo' ),98 raises( AttributeError, READONLY_MESSAGE ) )99 db = ycm_core.CompilationDatabase( 'foo' )100 assert_that( calling( db.__setattr__ )101 .with_args( 'database_directory', 'foo' ),102 raises( AttributeError, READONLY_MESSAGE ) )103 compilation_info = db.GetCompilationInfoForFile( 'foo.c' )104 assert_that( calling( compilation_info.__setattr__ )105 .with_args( 'compiler_working_dir_', 'foo' ),106 raises( AttributeError, READONLY_MESSAGE ) )107 assert_that( calling( compilation_info.__setattr__ )108 .with_args( 'compiler_flags_', ycm_core.StringVector() ),109 raises( AttributeError, READONLY_MESSAGE ) )110@ClangOnly111def CppBindings_CompilationInfo_NoInit_test():112 assert_that( calling( ycm_core.CompilationInfoForFile ),113 raises( TypeError, 'ycm_core.CompilationInfoForFile:'...
approx.py
Source:approx.py
2import numpy as np3from general.utility.base_classes import FrozenClass4class Approx(FrozenClass):5 def __init__(self, T, xdim, udim):6 object.__setattr__(self, 'T', T)7 object.__setattr__(self, 'xdim', xdim)8 object.__setattr__(self, 'udim', udim)9 pass10 def set(self, **kwargs):11 pass12 def set_t(self, t, **kwargs):13 pass14class CostApprox(Approx):15 def __init__(self, T, xdim, udim):16 Approx.__init__(self, T, xdim, udim)17 object.__setattr__(self, 'J', 0)18 object.__setattr__(self, 'l', np.zeros((T,)))19 object.__setattr__(self, 'lx', np.zeros((T, xdim)))20 object.__setattr__(self, 'lu', np.zeros((T, udim)))21 object.__setattr__(self, 'lxx', np.zeros((T, xdim, xdim)))22 object.__setattr__(self, 'luu', np.zeros((T, udim, udim)))23 object.__setattr__(self, 'lux', np.zeros((T, udim, xdim)))24 def set_t(self, t, **kwargs):25 self.l[t] = kwargs.pop('l')26 self.lx[t] = kwargs.pop('lx')27 self.lu[t] = kwargs.pop('lu')28 self.lxx[t] = kwargs.pop('lxx')29 self.luu[t] = kwargs.pop('luu')30 self.lux[t] = kwargs.pop('lux')31 self.J += np.sum(self.l[t])32 def __imul__(self, weight):33 weight = float(weight)34 if weight != 1:35 self.J *= weight36 self.l *= weight37 self.lx *= weight38 self.lu *= weight39 self.lxx *= weight40 self.luu *= weight41 self.lux *= weight42 return self43 def __iadd__(self, other):44 assert self.same_shape(other)45 self.J += other.J46 self.l += other.l47 self.lx += other.lx48 self.lu += other.lu49 self.lxx += other.lxx50 self.luu += other.luu51 self.lux += other.lux52 return self53 def same_shape(self, other):54 return \55 self.T == other.T and \56 self.xdim == other.xdim and \57 self.udim == other.udim58class DynamicsApprox(Approx):59 def __init__(self, T, xdim, udim):60 Approx.__init__(self, T, xdim, udim)61 object.__setattr__(self, 'Fx', np.zeros((T, xdim, xdim)))62 object.__setattr__(self, 'Fu', np.zeros((T, xdim, udim)))63 object.__setattr__(self, 'f0', np.zeros((T, xdim)))64 def set_t(self, t, **kwargs):65 self.Fx[t] = kwargs.pop('Fx')66 self.Fu[t] = kwargs.pop('Fu')67 self.f0[t] = kwargs.pop('f0')68class ValueApprox(Approx):69 def __init__(self, T, xdim):70 Approx.__init__(self, T, xdim, None)71 object.__setattr__(self, 'dV', np.zeros((T, 2)))72 object.__setattr__(self, 'Vx', np.zeros((T, xdim)))73 object.__setattr__(self, 'Vxx', np.zeros((T, xdim, xdim)))74 def set_t(self, t, **kwargs):75 self.dV[t] = kwargs.pop('dV')76 self.Vx[t] = kwargs.pop('Vx')77 self.Vxx[t] = kwargs.pop('Vxx')78class LocalValueApprox(Approx):79 def __init__(self, T, xdim, udim):80 Approx.__init__(self, T, xdim, udim)81 object.__setattr__(self, 'Qx', np.zeros((T, xdim)))82 object.__setattr__(self, 'Qu', np.zeros((T, udim)))83 object.__setattr__(self, 'Qxx', np.zeros((T, xdim, xdim)))84 object.__setattr__(self, 'Quu', np.zeros((T, udim, udim)))85 object.__setattr__(self, 'Qux', np.zeros((T, udim, xdim)))86 def set_t(self, t, **kwargs):87 self.Qx[t] = kwargs.pop('Qx')88 self.Qu[t] = kwargs.pop('Qu')89 self.Qxx[t] = kwargs.pop('Qxx')90 self.Quu[t] = kwargs.pop('Quu')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!