def parseOpenSearch(self,xmldata,format="atom"): """ OpenSearchの出力XMLをパースします。 xmldata=OpenSearch("keyword")[1] format=atomまたはrss """ root=XML(xmldata)#xmldata if format == "rss": entrys=[elm for elm in root.getchildren()[0].getchildren() if "item" == elm.tag] elif format == "atom": entrys=[elm for elm in root if elm.tag.endswith("entry")] else: raise Exception("Unknown format : %s" % format)#xmlのフォーマットはatom/rssのみ対応 hits=len(entrys) entrys=[entry.getiterator for entry in entrys] entrys=[j for j in [i() for i in entrys]] ret=[] h="content" for entry in entrys: buf={} for val in entry: dual=1 if val != None and val.text != None: if not val.text.startswith("\n"): if buf.get(val.tag[val.tag.find("}")+1:]) == None: buf[val.tag[val.tag.find("}")+1:]] = val.text else: dual= dual+1 buf[val.tag[val.tag.find("}")+1:] + str(dual)] = val.text if h in buf.keys():buf[h]=self.stripHTMLTags(buf[h]) ret.append(buf) return hits,ret#data[0]["name"] で著者
def get_status_code(output): try: tree = XML(output) child = tree.getchildren()[0] return int(child.get('status_code')) except: return None
def get_property(output, key): try: tree = XML(output) child = tree.getchildren()[0] # first tag return child.get(key) except IndexError: return None
def queryUniprot(id, expand=[], regex=True): """Query Uniprot with *id* and return a `dict` containing the raw results. Regular users should use :func:`searchUniprot` instead. :arg expand: entries through which you want to loop dictElements until there aren't any elements left :type expand: list """ if not isinstance(id, str): raise TypeError('id should be a string') try: record_file = openURL( 'http://www.uniprot.org/uniprot/{0}.xml'.format(id)) except: raise ValueError('No Uniprot record found with that id') data = record_file.read() record_file.close() data = XML(data) data = dictElement(data.getchildren()[0], '{http://uniprot.org/uniprot}', number_multiples=True) for key in data: value = data[key] if not key.startswith('dbReference'): continue try: if value.get('type') != 'PDB': continue except AttributeError: continue pdbid = value.get('id') refdata = {'PDB': pdbid} for prop in value: prop_key = prop.get('type') prop_val = prop.get('value') refdata[prop_key] = prop_val data[key] = refdata if expand: keys = [] if regex: for lt in expand: lt_re = re.compile(lt) for key in data: if lt_re.match(key): keys.append(key) else: keys = expand data = dictElementLoop(data, keys, '{http://uniprot.org/uniprot}') return data
def queryUniprot(id, expand=[], regex=True): """Query Uniprot with *id* and return a `dictionary` containing the results :arg expand: entries through which you want to loop dictElements until there aren't any elements left :type expand: list """ if not isinstance(id, str): raise TypeError('id should be a string') try: record_file = openURL('http://www.uniprot.org/uniprot/{0}.xml'.format(id)) except: raise ValueError('No Uniprot record found with that id') data = record_file.read() record_file.close() data = XML(data) data = dictElement(data.getchildren()[0], '{http://uniprot.org/uniprot}', number_multiples=True) for key in data: value = data[key] if not key.startswith('dbReference'): continue try: if value.get('type') != 'PDB': continue except AttributeError: continue pdbid = value.get('id') refdata = {'PDB': pdbid} for prop in value: prop_key = prop.get('type') prop_val = prop.get('value') refdata[prop_key] = prop_val data[key] = refdata if expand: keys = [] if regex: for lt in expand: lt_re = re.compile(lt) for key in data.keys(): if lt_re.match(key): keys.append(key) else: keys = expand data = dictElementLoop(data, keys, '{http://uniprot.org/uniprot}') return data
def get_property(output, key): tree = XML(output) child = tree.getchildren()[0] return child.get(key)