def load_chain_from_files(file_names, tree_name): """ Load TTree with tree_name from multiple files in a TChain. @param file_names List with ROOT file names or basestring "*" is allowed @param tree_name TTree with this name is loaded from files @return TChain """ file_name_list = None if isinstance(file_names, basestring): if '*' in file_names: file_name_list = glob.glob(file_names) else: file_name_list = [file_names] elif isinstance(file_names, list): file_name_list = file_names else: raise TypeError("%s is not a str or list of str" % file_names) data_chain = TChain(tree_name) added_files = 0 for file in file_name_list: if not os.path.isfile(file): raise IOError("File %s does not exist." % file) added_files += data_chain.AddFile(file) return data_chain, added_files
def convert_to_unicode(object): ''' Converts given object to its unicode string representation like \ python's native "builtins.str()" method. ''' if builtins.isinstance(object, builtins.Exception): if builtins.hasattr(object, '__unicode__'): return object.__unicode__() if builtins.hasattr(object, 'message'): object = object.message if builtins.isinstance(object, builtins.unicode): ''' NOTE: To force specified encoding we should encode and than \ decode here. ''' return object if builtins.isinstance(object, builtins.str): return builtins.unicode(object, ENCODING) if not builtins.isinstance(object, builtins.type): return convert_type_to_unicode(object) ''' NOTE: We have to avoid using an explicit encoding to mimic python \ native "builtins.str()" method behavior for getting a string \ representation. ''' return builtins.unicode(object)
def getResultSetId(self, top=None): if ( fullResultSetNameCheck == 0 or self.boolean.value in ['not', 'prox'] ): return "" if top is None: topLevel = 1 top = self else: topLevel = 0 # Iterate over operands and build a list rsList = [] if isinstance(self.leftOperand, Triple): rsList.extend(self.leftOperand.getResultSetId(top)) else: rsList.append(self.leftOperand.getResultSetId(top)) if isinstance(self.rightOperand, Triple): rsList.extend(self.rightOperand.getResultSetId(top)) else: rsList.append(self.rightOperand.getResultSetId(top)) if topLevel == 1: # Check all elements are the same # if so we're a fubar form of present if (len(rsList) == rsList.count(rsList[0])): return rsList[0] else: return "" else: return rsList
def custom_email(label, to, cc=None, attachments=None, context=None): if not isinstance(to, (list, tuple)): to = [to] if not isinstance(cc, (list, tuple)): cc = [cc] full_context = dict() full_context.update({} or context) mail_obj = EmailMessage() mail_obj.to = to mail_obj.cc = cc mail_obj.subject = render_to_string('email/{}/subject.txt'.format(label), context=full_context) mail_obj.from_email = settings.EMAIL_HOST_USER mail_obj.body = render_to_string('email/{}/message.html'.format(label), context=full_context) if attachments: for file_name in attachments: if os.path.exists(file_name): mail_obj.attach_file(file_name) else: logging.debug( "file is not available in specified location: {}".format( file_name)) mail_obj.content_subtype = "html" try: return mail_obj.send() except Exception as e: msg = u"sending email failed\n" msg += unicode(e) print >> sys.stderr, e
def _eq_rec(self, a, b, cache=[]): """ Replaces the == operator because of circulating references (e.g. analyte <-> well) Adapted solution taken from http://stackoverflow.com/questions/31415844/using-the-operator-on-circularly-defined-dictionaries """ cache = cache + [a, b] if isinstance(a, DomainObjectMixin): a = a.__dict__ if isinstance(b, DomainObjectMixin): b = b.__dict__ if not isinstance(a, dict) or not isinstance(b, dict): return a == b set_keys = set(a.keys()) if set_keys != set(b.keys()): return False for key in set_keys: if any(a[key] is i for i in cache): continue elif any(b[key] is i for i in cache): continue elif a[key].__class__.__name__ == "MagicMock" and a[ key].__class__.__name__ == "MagicMock": # TODO: Move this to the tests. The domain objects shouldn't have to directly know about this # filter out mocked fields continue elif not self._eq_rec(a[key], b[key], cache): return False return True
def getResultSetId(self, top=None): if (fullResultSetNameCheck == 0 or self.boolean.value in ['not', 'prox']): return "" if top is None: topLevel = 1 top = self else: topLevel = 0 # Iterate over operands and build a list rsList = [] if isinstance(self.leftOperand, Triple): rsList.extend(self.leftOperand.getResultSetId(top)) else: rsList.append(self.leftOperand.getResultSetId(top)) if isinstance(self.rightOperand, Triple): rsList.extend(self.rightOperand.getResultSetId(top)) else: rsList.append(self.rightOperand.getResultSetId(top)) if topLevel == 1: # Check all elements are the same # if so we're a fubar form of present if (len(rsList) == rsList.count(rsList[0])): return rsList[0] else: return "" else: return rsList
def _is_multiple_type(cls, type): # # ''' Check whether a given specification allows multiple types. Examples: >>> CheckObject._is_multiple_type(()) False >>> CheckObject._is_multiple_type(('hans')) False >>> CheckObject._is_multiple_type(('hans', 5)) False >>> CheckObject._is_multiple_type((str, int)) True >>> CheckObject._is_multiple_type([str, int, bool]) True >>> CheckObject._is_multiple_type((str,)) False >>> CheckObject._is_multiple_type([str]) False ''' return ( builtins.isinstance(type, (builtins.tuple, builtins.list)) and builtins.len(type) > 1 and builtins.isinstance(type[0], builtins.type))
def _eq_rec(self, a, b, cache=[]): """ Replaces the == operator because of circulating references (e.g. analyte <-> well) Adapted solution taken from http://stackoverflow.com/questions/31415844/using-the-operator-on-circularly-defined-dictionaries """ cache = cache + [a, b] if isinstance(a, DomainObjectMixin): a = a.__dict__ if isinstance(b, DomainObjectMixin): b = b.__dict__ if not isinstance(a, dict) or not isinstance(b, dict): return a == b set_keys = set(a.keys()) if set_keys != set(b.keys()): return False for key in set_keys: if any(a[key] is i for i in cache): continue elif any(b[key] is i for i in cache): continue elif a[key].__class__.__name__ == "MagicMock" and a[key].__class__.__name__ == "MagicMock": # TODO: Move this to the tests. The domain objects shouldn't have to directly know about this # filter out mocked fields continue elif not self._eq_rec(a[key], b[key], cache): return False return True
def _handle_given_decorator(self, function, method): # # ''' Another decorator was given to this instance. This decorator \ should additionally be used on given function. ''' self.__func__ = function self.method_type = method if self.__func__ is not None: if builtins.isinstance(self.__func__, FunctionDecorator): ''' If we are wrapping a nested instance of this class we \ propagate inspected informations to lower decorator. This \ case is given if one instances of this class wraps \ another one. The last wrapping instance additionally uses \ a common or manageable wrapper. ''' self.wrapped_decorator = self.__func__ self.method_type = self.__func__.method_type self.__func__ = self.__func__.__func__ elif(builtins.isinstance(method, builtins.type) and builtins.issubclass(method, FunctionDecorator)): ''' If we are wrapping a nested class of this type we \ propagate inspected informations to lower decorator. This \ case is given if one instances of this class wraps \ another one and the lower one is given via argument. ''' self.wrapped_decorator = self.method_type(method=self.__func__) self.method_type = self.wrapped_decorator.method_type self.__func__ = self.wrapped_decorator.__func__ elif self.method_type in self.COMMON_DECORATORS: self.__func__ = self.method_type(self.__func__) return self
def __init__(self,data): self.response = TcpLoginResponse() try: if len(data) == 0 : exit; self.data = data udata = unicode(data,"utf-8") if not isinstance(udata,unicode): return dicts = json.loads(udata) if not isinstance(dicts,dict): return self.dicts = dicts if "cmdtype" in dicts: cmdtype = dicts["cmdtype"] self.cmd = cmdtype ''' query db for islogin ''' if cmdtype in self._cmds: # print "cmd ",cmdtype return eval("self."+cmdtype)() else: self.response = None exit else: exit except : print "unknown err" return
def is_equation(eqn, check_true=True): ''' Return True if it is an equation rather than a boolean value. If it is False, raise a ContradictionException. We never want anything that might be False. Optionally, we can turn the check off, but THE DEFAULT VALUE SHOULD ALWAYS BE TRUE. Otherwise bad things will happen. >>> x, y = sympy.symbols('x y') >>> eq1 = sympy.Eq(x, y) >>> eq2 = sympy.Eq(x, x) >>> eq3 = sympy.Eq(x, y).subs(y, x) >>> eq4 = sympy.Eq(2*x*y, 2) >>> is_equation(eq1) True >>> is_equation(eq2) False >>> is_equation(eq3) False >>> is_equation(eq4) True Now check that it raises exceptions for the right things >>> is_equation(0) False ''' if sympy.__version__ == '0.7.5': return isinstance(eqn, sympy.Equality) elif re.match('1\..*', sympy.__version__): return isinstance(eqn, sympy.Equality) else: return eqn is True
def _is_callable(self, object): # # """Indicates if given method is a callable or a callable wrapper.""" return ( builtins.callable(object) or builtins.isinstance(object, builtins.classmethod) or builtins.isinstance(object, builtins.staticmethod) )
def isinstance(obj, t): if not __builtin__.isinstance(t, type(())): return __builtin__.isinstance(obj, _builtin_type_map.get(t, t)) else: for typ in t: if __builtin__.isinstance(obj, _builtin_type_map.get(typ, typ)): return True return False
def __find_same_layers(root,lyr_ids): group_names = [l.name for l in root.children() if isinstance(l, QgsLayerTreeGroup)] for tl in root.children(): if isinstance(tl, QgsLayerTreeGroup): lyr_ids = UniumPlugin.__find_same_layers(tl, lyr_ids) elif isinstance(tl, QgsLayerTreeLayer): if tl.layerName() in group_names: lyr_ids.append(tl.layerId()) return lyr_ids
def isinstance(obj, clsinfo): import __builtin__ if type(clsinfo) in (tuple, list): for cls in clsinfo: if cls is type: cls = types.ClassType if __builtin__.isinstance(obj, cls): return 1 return 0 else: return __builtin__.isinstance(obj, clsinfo)
def extractWhere(parsedStatement): for item in parsedStatement.tokens: if isinstance(item, sqlparse.sql.Where): for subItem in item.tokens: if isinstance(subItem, sqlparse.sql.Comparison): yield subItem.left elif isinstance(subItem, sqlparse.sql.Function): for returnItem in extractFunctionArgument(subItem): yield returnItem
def _handle_multiple_types( self, value, given_type, expected_types, name='return value' ): # # ''' Check an argument which is specified with multiple types. Examples: >>> class A(CheckObject): pass >>> A()._handle_multiple_types( ... 'hans', str, (str, int) ... ) # doctest: +ELLIPSIS Object of "A" with class object "None", object "None", called ... >>> a = A() >>> a.__func__ = A._handle_multiple_types >>> a._handle_multiple_types( ... 'hans', str, (bool, int) ... ) # doctest: +ELLIPSIS +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... SignatureError: "_handle_multiple_types()" expects one instance ... >>> a._handle_multiple_types( ... 'hans', str, [True, 4, 'hans'] ... ) # doctest: +ELLIPSIS Object of "A" with class object "None", object "None", called ... >>> a._handle_multiple_types( ... 'hans', str, [True, 4, 'peter'] ... ) # doctest: +ELLIPSIS +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... SignatureError: "_handle_multiple_types()" expects one value of ... ''' if(builtins.isinstance(expected_types, builtins.tuple) and not self._check_again_multiple_types( value, given_type, expected_types)): raise __exception__( '"{function_path}()" expects one instance of {types} for ' '"{name}" but received "{type_name}".'.format( function_path=self.get_function_path(), types=self._join_types(types=expected_types), name=name, type_name=given_type.__name__)) elif(builtins.isinstance(expected_types, builtins.list) and value not in expected_types): raise __exception__( '"{function_path}()" expects one value of {values} for ' '"{name}" but received "{type_name}".'.format( function_path=self.get_function_path(), values=self._join_types( types=expected_types, meta_type=False), name=name, type_name=given_type.__name__)) return self
def validate_backend_server_info(module, backend_servers, check_weight, default_weight=None): """ Validate backend server information provided by user for add, set and remove action :param module: Ansible module object :param backend_servers: backend severs information to validate (list of dictionaries or string) :param check_weight: specifies whether to check for weight key in dictionary :param default_weight: assigns default weight, if provided, for a backend server to set/add """ server_id_param = 'server_id' weight_param = 'weight' VALID_PARAMS = (server_id_param, weight_param) for backend_server in backend_servers: if check_weight: if isinstance(backend_server, dict) is False: module.fail_json( msg='Invalid backend_server parameter type [%s].' % type(backend_server)) for k in backend_server: if k not in VALID_PARAMS: module.fail_json( msg='Invalid backend_server parameter \'{}\''.format( k)) server_id = get_alias_value(backend_server, [server_id_param]) if server_id is None: module.fail_json(msg='server_id is mandatory') weight = get_alias_value(backend_server, [weight_param]) if weight is None: if default_weight is not None: backend_server[weight_param] = default_weight else: module.fail_json(msg='Weight is mandatory') else: #verifying weight parameter for non numeral string and limit validation try: w = int(weight) if w < 1 or w > 100: module.fail_json(msg='Invalid weight parameter.') except Exception as e: module.fail_json(msg='Invalid weight parameter.') else: if isinstance(backend_server, str) is False: module.fail_json( msg='Invalid backend_server parameter type [%s].' % type(backend_server))
def value(self,width=None): if width is None: width = self.width if self.connectedTo is None: raise ValueError('attempting to get value of a wire with no value') if isinstance(self.connectedTo, basestring): all_ones_list = ["allones", "ones", "one", "allhigh", "high", "true", "~0"] all_zeros_list = ["allzeros", "zeros", "zero", "alllow", "low", "false", "0"] if self.connectedTo.lower() in all_ones_list: if width is None: return "~0" elif width == 1: return "1'b1" else: return "{(" + str(width) + "){1'b1}}" if self.connectedTo.lower() in all_zeros_list: if width is None: return "0" elif width == 1: return "1'b0" else: return "{(" + str(width) + "){1'b0}}" return self.connectedTo if isinstance(self.connectedTo, int): if width is None: return str(self.connectedTo) else: return self.hdlwriter.toHDLhex(self.connectedTo, width) if isinstance(self.connectedTo, Wire): minwidth = width if self.connectedTo.width is not None: minwidth = min(width, self.connectedTo.width) if self.connectedTo.name is None: if self.connectedTo.isConnected(): val = self.connectedTo.value(minwidth) else: raise ValueError('Dangling Wire...') else: val = self.connectedTo.name return self.__normalizeWidth(val,width,minwidth) raise ValueError('wire value has an unknown type')
def mergeSequenceTree(sequence): "Merge this sequence into a tree and make it into a dict" temp = {} if __builtin__.isinstance(sequence, types.TupleType) or __builtin__.isinstance(sequence, types.ListType): for i in sequence: locator = temp for j in i: if not j in locator: locator[j] = {} locator = locator[j] return temp
def mergeSequenceTree(sequence): "Merge this sequence into a tree and make it into a dict" temp = {} if __builtin__.isinstance(sequence, types.TupleType) or __builtin__.isinstance( sequence, types.ListType): for i in sequence: locator = temp for j in i: if not j in locator: locator[j] = {} locator = locator[j] return temp
def rec_get_layer_path(root,layer_id,path): check = False for node in root.children(): if isinstance(node,QgsLayerTreeGroup): (check,path) = UniumPlugin.rec_get_layer_path(node,layer_id,path) if check: path.append(root.name()) return (check,path) elif isinstance(node,QgsLayerTreeLayer): if node.layerId() == layer_id: path.append(root.name()) check = True return (check,path)
def __init__( self, file=None, queue=None, support_multiprocessing=False, force_string=False ): # # ''' Saves the file path in the current instance. If "file" is "None" \ an instance variable is used as buffer. Examples: >>> Buffer( ... file=__test_folder__.path + '__init__' ... ).file # doctest: +ELLIPSIS Object of "Handler" with path "...__init__" ... >>> Buffer( ... queue=True, support_multiprocessing=True ... ).queue # doctest: +ELLIPSIS <multiprocessing.queues.Queue object at ...> ''' # # python3.5 pass self.force_string = force_string '''Saves the last written input.''' self.last_written = '' if support_multiprocessing: self._lock = multiprocessing.Lock() '''Saves the file handler instance for writing content into.''' self.file = None '''Saves the queue instance for writing content into.''' self.queue = None if queue is not None: self.queue = native_queue.Queue() if support_multiprocessing: self.queue = multiprocessing.Queue() if(builtins.isinstance(queue, native_queue.Queue) or support_multiprocessing and builtins.isinstance(queue, multiprocessing.queues.Queue)): self.queue = queue elif file is not None: self.file = FileHandler(location=file) ''' A lock object to guarantee that no other thread read from buffer \ during truncating or writing. ''' self._lock = threading.Lock() '''Saves the current buffer content.''' # # python3.5 # # self._content = '' self._content = builtins.str() if self.force_string else ''
def get_layer_path(root,layer_id): path = [] for node in root.children(): if isinstance(node,QgsLayerTreeGroup): check = False (check,path) = UniumPlugin.rec_get_layer_path(node,layer_id,path) if check: path.append(root.name()) break elif isinstance(node,QgsLayerTreeLayer): if node.layerId() == layer_id: path.append(root.name()) path.reverse() return chr(92).join([p for p in path if len(p) > 0])
def isinstance(obj, clsinfo): import __builtin__ as __builtin__ if type(clsinfo) in (types.TupleType, types.ListType): for cls in clsinfo: if cls is type: cls = types.ClassType if __builtin__.isinstance(obj, cls): return 1 continue return 0 else: return __builtin__.isinstance(obj, clsinfo)
def process_xpath(self, session, xpath, maps={}): global prefixRe if (isinstance(xpath, list)): xpath = repr(xpath[0]) if not any([xpath.startswith('/'), xpath.endswith(')')]): xpath = "//" + xpath if maps: retval = self.dom.xpath(xpath, namespaces=maps) else: retval = self.dom.xpath(xpath) if isinstance(retval, list): return retval else: return [retval]
def onPGChanged(self, event): _, _, item = self.GetSelectedItem() if item is None: raise Exception("Sth strange happend! Can't find item to store attrs.") return p = event.GetProperty() if (not p) or (not p.IsEnabled()): ### No property in event or a group parent. return ### Get attr from property control. key = str(p.GetName()) ### Important!!! Make sure 'key' not unicode. if key.find('.') > 0: ### If child of a group. key = key.split('.')[-1] v = p.GetValue() if isinstance(v, basestring): v = v.strip().encode('utf8') elif isinstance(v, types.BooleanType): v = str(v).lower() else: v = str(v) ### Update attr. uv = to_unicode(v); udv = to_unicode(p.GetDefaultValue()) if uv == '' or uv == udv: try: del item.get_attributes()[key] except: pass # Restore the display value to default. p.SetValue(p.GetDefaultValue()) v = p.GetDefaultValue() else: item.get_attributes()[key] = add_double_quote(v) ### Change PG background if value is different from default. if uv == udv: p.SetBackgroundColour(self.m_tree.GetBackgroundColour(), -1) else: p.SetBackgroundColour('#ffffc0', -1) ### Update view. self.is_data_changed = True self.update_graph() return
def test_laughter_against_the_machine(self, m): # https://means.tv/programs/latm?categoryId=20473 with open(_LATM_CHAPTERS_JSON, "r") as response_file: response_json = json.load(response_file) m.get( 'https://means.tv/api/chapters/?ids%5B%5D=1119397&ids%5B%5D=1119398&ids%5B%5D=1119399&ids%5B%5D=1119400&ids%5B%5D=1119401&ids%5B%5D=1119402&ids%5B%5D=1119404&ids%5B%5D=1711409', json=response_json) chapters = load_chapters([ 1119397, 1119398, 1119399, 1119400, 1119401, 1119402, 1119404, 1711409 ]) self.assertEquals(len(chapters), 7) self.assertTrue(all([isinstance(c, ChapterVideo) for c in chapters])) self.assertEquals(chapters[0].title, 'Episode 1 - Arizona') self.assertEquals(chapters[0].position, 1) self.assertEquals(chapters[1].title, 'Episode 2 - Chicago') self.assertEquals(chapters[1].position, 2) self.assertEquals(chapters[2].title, 'Episode 3 - Dearborn') self.assertEquals(chapters[2].position, 3) self.assertEquals(chapters[3].title, 'Episode 4 - Wisconsin') self.assertEquals(chapters[3].position, 4) self.assertEquals(chapters[4].title, 'Episode 5 - NYC & DC') self.assertEquals(chapters[4].position, 5) self.assertEquals(chapters[5].title, 'Episode 6 - New Orleans') self.assertEquals(chapters[5].position, 6) self.assertEquals(chapters[6].title, 'Episode 7 - Oakland') self.assertEquals(chapters[6].position, 7)
def get_content(self): ''' Getter for the current content. Examples: >>> Buffer().write('test').content 'test' >>> Buffer(queue=True).write('test').content 'test' ''' with self._lock: if self.file is not None: self._content = self.file.content elif self.queue: self._content = '' temp_buffer = [] while not self.queue.empty(): # # python3.5 # # temp_buffer.append(self.queue.get()) temp_buffer.append(convert_to_unicode( self.queue.get())) # # self._content += temp_buffer[-1] for content in temp_buffer: self.queue.put(content) # # python3.5 # # pass if self.force_string and builtins.isinstance( self._content, builtins.unicode ): self._content = convert_to_string(self._content) # # return self._content
def _checkSaxXPathAttr(self, pred, attrs): # Namespacey if (not pred[0] in attrs): if ((None, pred[0]) in attrs): pred[0] = (None, pred[0]) else: return 0 rel = pred[1] # -Much- faster than eval if isinstance(pred[2], float): attrValue = float(attrs[pred[0]]) else: attrValue = attrs[pred[0]] comp = cmp(attrValue, pred[2]) if rel == "=": return comp == 0 elif rel == ">": return comp == 1 elif rel == "<": return comp == -1 elif rel == "<=": return comp in (-1, 0) elif rel == ">=": return comp in (1, 0) elif rel == "!=": return comp in (1, -1) else: raise (NotImplementedError)
def reversed(data): if not isinstance(data, list): data = list(data) reversed_data = [] for index in xrange(len(data) - 1, -1, -1): reversed_data.append(data[index]) return reversed_data
def process_xpath(self, session, xpath, maps={}): global prefixRe if (isinstance(xpath, list)): xpath = repr(xpath[0]) if not any( [xpath.startswith('/'), xpath.endswith(')')] ): xpath = "//" + xpath if maps: retval = self.dom.xpath(xpath, namespaces=maps) else: retval = self.dom.xpath(xpath) if isinstance(retval, list): return retval else: return [retval]
def add_cache(self, key, value): if isinstance(key, basestring) and value is not None: self.__store[key] = value return True else: Log(1, 'add_cache[%s=%s] fail,as' % (str(key), str(value))) return False
def mergeDistinct(db,destDb,srcDbList,tableName,distinctKey,whereLimit="1=1"): eventKeys = {} rows = [] db.selectDB(destDb) tableColumns = db.select("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE table_name ='%s' and table_schema ='%s';"%(tableName,destDb)) tableColumnStr = getTableColumnsStr(tableColumns).strip() columnStrs = tableColumnStr.split(",") colIndex = columnStrs.index("`"+distinctKey+"`") for i in xrange(0,len(srcDbList)): mergeDistinctStep2(db, srcDbList[i], eventKeys, colIndex, tableColumnStr, rows, tableName, whereLimit) db.selectDB(destDb) insertSql = ""; for row in rows: insertSql += "(" for i in xrange(len(row)): val = row[i] if isinstance(val, unicode): insertSql += "'"+val+"'," else: insertSql += str(val)+"," insertSql = insertSql[:-1] insertSql += ")," if len(insertSql) > 0: insertSql = "INSERT INTO `%s` VALUES %s"%(tableName,insertSql[:-1]) db.execute(insertSql)
def load_trees_from_file(file_name): """ Load all TTree from a ROOT file @param file_name Name of ROOT file @return dict with name of tree as key and the tree as value, ROOT file """ if not os.path.isfile(file_name): raise IOError("File %s does not exist." % file_name) root_file = TFile(file_name) if root_file.IsZombie(): raise IOError("Can't open root file %s." % file_name) keys = root_file.GetListOfKeys() trees = {} for key in keys: tree = root_file.Get(key.GetName()) if isinstance(tree, TTree): trees[key.GetName()] = tree return trees, root_file
def __init__(self, data=''): if not isinstance(data, str): raise ValueError, type(data) # expecting str # TODO other types object.__init__(self) self.data = data self.state = self.State(data) # TODO strview or similar in the state self.stack = []
def get_header_dict(self): header_dict = OrderedDict() header_dict.update(self.update_static_header_info()) def key_attribute(field): return field.code if isinstance(self.form_model, EntityFormModel): entity_questions = self.form_model.base_entity_questions else: entity_questions = self.form_model.entity_questions entity_question_dict = dict( (self._get_entity_question_path(field), field) for field in entity_questions) headers = header_fields(self.form_model, key_attribute) for field_code, val in headers.items(): if field_code in entity_question_dict.keys(): self.add_unique_id_field(entity_question_dict.get(field_code), header_dict) else: key = es_questionnaire_field_name(field_code.replace(".", " "), self.form_model.id) header_dict.update({key: val}) return header_dict
def extractTable(parsedStatement): from_seen = False innerJoinPresent = False innerJoinFound = False #search for inner join statement if not innerJoinFound: tokenIter = iter(parsedStatement.tokens) for item in tokenIter: if innerJoinPresent and not innerJoinFound: innerJoinFound = True yield next(tokenIter) break if str(item) == "INNER JOIN": innerJoinPresent = True logging.info("Found INNER JOIN - check statement: " + str(parsedStatement)) statementsToRecheck.add(str(parsedStatement)) for item in parsedStatement.tokens: if from_seen and item.ttype is not Whitespace: if item.ttype is Keyword or isinstance(item, sqlparse.sql.Where) and str(item): raise StopIteration # do something if not item.ttype is sqlparse.tokens.Error: yield item elif item.ttype is Keyword and item.value.upper() == 'FROM': from_seen = True
def getVal(object, key): val = None if type(key) == str: if isinstance(object, collections.Iterable): if key in object: val = object[key] return val
def update_TView(self): if self.selected_id in self.layers.keys(): self.dockwidget.layersBox.enabled = False attrs_names = [u"Идентификатор",u"Наименование",u"Описание",u"Условный знак"] attrs_values = [] lif = self.iface.legendInterface() lyrs = lif.layers() for lyr in lyrs: all_case = (self.selected_id == '0' and lif.isLayerVisible(lyr)) if isinstance(lyr, QgsVectorLayer) and (lyr.id() == self.selected_id or all_case): #attrs_names = [a.name() for a in lyr.fields()] features = lyr.getFeatures() attrs_values += [[feat[i] for i in xrange(len(attrs_names))] for feat in features] if not all_case: break self.dockwidget.tableView.setRowCount(len(attrs_values)) self.dockwidget.tableView.setColumnCount(len(attrs_names)) self.dockwidget.tableView.setHorizontalHeaderLabels(attrs_names) for row in xrange(len(attrs_values)): for col in xrange(len(attrs_names)): if col < len(attrs_names) - 1: item = QTableWidgetItem(u'%s' % attrs_values[row][col]) else: pm = self.get_sign_image(attrs_values[row][col]) if pm: item = QTableWidgetItem(self.config["signs"][attrs_values[row][col]].get("alias","")) item.setData(Qt.DecorationRole, pm.scaled(20, 20)) else: item = QTableWidgetItem(u'%s' % attrs_values[row][col]) self.dockwidget.tableView.setItem(row,col,item) self.dockwidget.layersBox.enabled = True
def _checkSaxXPathAttr(self, pred, attrs): # Namespacey if (not pred[0] in attrs): if ((None, pred[0]) in attrs): pred[0] = (None, pred[0]) else: return 0 rel = pred[1] # -Much- faster than eval if isinstance(pred[2], float): attrValue = float(attrs[pred[0]]) else: attrValue = attrs[pred[0]] comp = cmp(attrValue, pred[2]) if rel == "=": return comp == 0 elif rel == ">": return comp == 1 elif rel == "<": return comp == -1 elif rel == "<=": return comp in (-1, 0) elif rel == ">=": return comp in (1, 0) elif rel == "!=": return comp in (1, -1) else: raise(NotImplementedError)
def smart_set(self, data): if isinstance(data, dict): for k, v in data.iteritems(): self.etcd.set('%s/%s' % (self.root_path, k), v) return Result(0) return Result(0, INVALID_PARAM_ERR, 'Not support the data.')
def extractFunctionArgument(tokenStream): for item in tokenStream.tokens: if isinstance(item, sqlparse.sql.Parenthesis): #for subItem in item.tokens: #if subItem.ttype is not Whitespace and subItem.ttype is not Punctuation: #yield subItem for returnItem in isolateArguments(item): yield returnItem
def _render_properties(self, properties, code_file): # # ''' If a given code property is marked as executable respectively \ dynamic it's value will be determined. Examples: >>> code_file = FileHandler( ... location=__test_folder__.path + '_render_properties.cpp') >>> FileHandler('temp_render_properties_main.py').content = '' >>> run = Run() >>> run._render_properties({ ... 'commands': { ... 'compile': 'g++ "<%code_file.path%>"', ... 'run': '<%code_file.basename%>', ... }, ... 'code_manager': { ... 'file_path': 'Makefile', ... 'commands': {'compile': 'make build'} ... }, ... 'extensions': ('cpp', 'c') ... }, code_file) # doctest: +ELLIPSIS +SKIP {'commands': {'compile': 'g++ "...runner.cpp"', 'run': '...} >>> run._render_properties({'hans': 'peter'}, code_file) {'hans': 'peter'} ''' rendered_properties = copy(properties) for key, value in rendered_properties.items(): if builtins.isinstance(value, builtins.dict): rendered_properties[key] = self._render_properties( properties=value, code_file=code_file) # # python3.5 # # elif builtins.isinstance(value, builtins.str): elif builtins.isinstance(value, builtins.unicode): # # rendered_properties[key] = TemplateParser( template=value, string=True ).render( code_file=code_file, arguments=' '.join(self._command_line_arguments), path_separator=os.sep ).output return rendered_properties
def retrieve_value_for_display(self, values, key, converter=None, default=("","%s")): found = values.get(key, ['']) x=default[0] f="%s" if found: x=found if x is not None and converter is not None: x=converter(x) if isinstance(x, int): found = int(x) f="%d" elif isinstance(x, float): found = float(x) f="%d" else: found = x f="%s" return (found,f)
def is_str(val): response = False if \ type(val) == str \ or \ isinstance(val, unicode): response = True return response
def starts_with(self, s): r""" TODO """ if not isinstance(s, str): raise ValueError, (s, ) # expecting str for _i, _c in enumerate(s): if self[_i] != _c: return False # does not match return True # matches
def get_field_set_fields(form_model_id, fields, parent_field_code=None): field_set_field_dict = {} for field in fields: if isinstance(field, FieldSet): field_set_field_dict.update( {es_questionnaire_field_name(field.code, form_model_id, parent_field_code): field}) group_field_code = field.code if field.is_group() else None field_set_field_dict.update(get_field_set_fields(form_model_id, field.fields, group_field_code)) return field_set_field_dict
def consume(self, n, *args, **kwargs): r""" Consumes state data. """ if not isinstance(n, int): raise ValueError, n # expecting int if not n >= 0: raise ValueError, n # expecting >= 0 _consumed = self.state.data[:n] self.state.data = self.state.data[n:] return _consumed
def get_layers(self): self.layers = {"0": {"name": u"Все", "path":"", "full_name": u"Все"}} for lyr in self.iface.legendInterface().layers(): if isinstance(lyr, QgsVectorLayer): path = UniumPlugin.get_layer_path(QgsProject.instance().layerTreeRoot(),lyr.id()) self.layers[lyr.id()] = {'name': lyr.name(), 'subset': lyr.subsetString(), 'path': path, 'full_name':chr(92).join([path,lyr.name()])}
def convert_type_to_unicode(object): '''Converts a generic string representable object to unicode.''' if builtins.hasattr(object, '__unicode__'): return object.__unicode__() elif builtins.hasattr(object, '__str__'): try: object = object.__str__() except builtins.UnicodeEncodeError: if builtins.isinstance(object, Iterable): for index, item in builtins.enumerate(object): object[index] = convert_to_unicode(item) object = object.__str__() else: raise if builtins.isinstance(object, builtins.unicode): return object return builtins.unicode(object, ENCODING) return builtins.unicode(object)
def convert_to_string(object): ''' Converts given object to its str string representation like \ python's native "builtins.str()" method. ''' '''NOTE: We check for string type to boost already converted values.''' if builtins.isinstance(object, builtins.str): return object return convert_to_unicode(object).encode(ENCODING)
def differing_fields(self, other): if isinstance(other, self.__class__): ret = [] for key in self.__dict__: if self.__dict__.get(key, None) != other.__dict__.get(key, None): ret.append(key) return ret else: return None
def enrich_analysis_data(record, questionnaire, submission_id, is_export=False): try: dbm = questionnaire._dbm if isinstance(questionnaire, EntityFormModel): entity_questions = questionnaire.base_entity_questions else: entity_questions = questionnaire.entity_questions linked_id_details = [ _get_linked_id_details(dbm, field, parent_field_types=[]) for field in entity_questions ] for linked_id_detail in linked_id_details: _update_record_with_linked_id_details(dbm, record, linked_id_detail, questionnaire.id, nested=False) if isinstance(record, dict): record_as_dict = record else: record_as_dict = record.to_dict() for key, value in record_as_dict.iteritems(): del record[key] record[key.replace(".", " ")] = value if isinstance(value, basestring): try: value_obj = json.loads(value) if isinstance(value_obj, list) and not is_export: _transform_nested_question_answer( key, value_obj, record, questionnaire, submission_id) except Exception as e: continue except Exception as ex: logger.exception('Unable to enrich analysis data') return record
def differing_fields(self, other): if isinstance(other, self.__class__): ret = [] for key in self.__dict__: if self.__dict__.get(key, None) != other.__dict__.get( key, None): ret.append(key) return ret else: return None
def test_search(self, m): with open(_SEARCH_RESULTS_1_JSON, "r") as response_file: response_json = json.load(response_file) m.get('https://means.tv/api/contents?search=left&page=1', json=response_json) with open(_SEARCH_RESULTS_2_JSON, "r") as response_file: response_json = json.load(response_file) m.get('https://means.tv/api/contents?search=left&page=2', json=response_json) with open(_SEARCH_RESULTS_3_JSON, "r") as response_file: response_json = json.load(response_file) m.get('https://means.tv/api/contents?search=left&page=3', json=response_json) search_results = get_search_results('left') self.assertEquals(len(search_results), 2) self.assertTrue(isinstance(search_results[0], Collection)) self.assertEquals(search_results[0].title, 'Left Trigger') self.assertTrue(isinstance(search_results[1], Video)) self.assertEquals(search_results[1].title, 'PIIGS')
def __getitem__(self, k): if isinstance(k, int): try: return self.modifiers[k] except: return None for m in self.modifiers: if (str(m.type) == k or m.type.value == k): return m return None